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_create.h"
52 #include "got_lib_diff.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 static const struct got_error *
59 create_meta_file(const char *path_got, const char *name, const char *content)
60 {
61 const struct got_error *err = NULL;
62 char *path;
63 int fd = -1;
65 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
66 err = got_error_from_errno();
67 path = NULL;
68 goto done;
69 }
71 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
72 GOT_DEFAULT_FILE_MODE);
73 if (fd == -1) {
74 err = got_error_from_errno();
75 goto done;
76 }
78 if (content) {
79 int len = dprintf(fd, "%s\n", content);
80 if (len != strlen(content) + 1) {
81 err = got_error_from_errno();
82 goto done;
83 }
84 }
86 done:
87 if (fd != -1 && close(fd) == -1 && err == NULL)
88 err = got_error_from_errno();
89 free(path);
90 return err;
91 }
93 static const struct got_error *
94 update_meta_file(const char *path_got, const char *name, const char *content)
95 {
96 const struct got_error *err = NULL;
97 FILE *tmpfile = NULL;
98 char *tmppath = NULL;
99 char *path = NULL;
101 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
102 err = got_error_from_errno();
103 path = NULL;
104 goto done;
107 err = got_opentemp_named(&tmppath, &tmpfile, path);
108 if (err)
109 goto done;
111 if (content) {
112 int len = fprintf(tmpfile, "%s\n", content);
113 if (len != strlen(content) + 1) {
114 err = got_error_from_errno();
115 goto done;
119 if (rename(tmppath, path) != 0) {
120 err = got_error_from_errno();
121 unlink(tmppath);
122 goto done;
125 done:
126 free(tmppath);
127 if (fclose(tmpfile) != 0 && err == NULL)
128 err = got_error_from_errno();
129 return err;
132 static const struct got_error *
133 read_meta_file(char **content, const char *path_got, const char *name)
135 const struct got_error *err = NULL;
136 char *path;
137 int fd = -1;
138 ssize_t n;
139 struct stat sb;
141 *content = NULL;
143 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
144 err = got_error_from_errno();
145 path = NULL;
146 goto done;
149 fd = open(path, O_RDONLY | O_NOFOLLOW);
150 if (fd == -1) {
151 if (errno == ENOENT)
152 err = got_error(GOT_ERR_WORKTREE_META);
153 else
154 err = got_error_from_errno();
155 goto done;
157 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
158 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
159 : got_error_from_errno());
160 goto done;
163 if (lstat(path, &sb) != 0) {
164 err = got_error_from_errno();
165 goto done;
167 *content = calloc(1, sb.st_size);
168 if (*content == NULL) {
169 err = got_error_from_errno();
170 goto done;
173 n = read(fd, *content, sb.st_size);
174 if (n != sb.st_size) {
175 err = (n == -1 ? got_error_from_errno() :
176 got_error(GOT_ERR_WORKTREE_META));
177 goto done;
179 if ((*content)[sb.st_size - 1] != '\n') {
180 err = got_error(GOT_ERR_WORKTREE_META);
181 goto done;
183 (*content)[sb.st_size - 1] = '\0';
185 done:
186 if (fd != -1 && close(fd) == -1 && err == NULL)
187 err = got_error_from_errno();
188 free(path);
189 if (err) {
190 free(*content);
191 *content = NULL;
193 return err;
196 const struct got_error *
197 got_worktree_init(const char *path, struct got_reference *head_ref,
198 const char *prefix, struct got_repository *repo)
200 const struct got_error *err = NULL;
201 struct got_object_id *commit_id = NULL;
202 uuid_t uuid;
203 uint32_t uuid_status;
204 int obj_type;
205 char *path_got = NULL;
206 char *refstr = NULL;
207 char *formatstr = NULL;
208 char *absprefix = NULL;
209 char *basestr = NULL;
210 char *uuidstr = NULL;
212 if (strcmp(path, got_repo_get_path(repo)) == 0) {
213 err = got_error(GOT_ERR_WORKTREE_REPO);
214 goto done;
217 err = got_ref_resolve(&commit_id, repo, head_ref);
218 if (err)
219 return err;
220 err = got_object_get_type(&obj_type, repo, commit_id);
221 if (err)
222 return err;
223 if (obj_type != GOT_OBJ_TYPE_COMMIT)
224 return got_error(GOT_ERR_OBJ_TYPE);
226 if (!got_path_is_absolute(prefix)) {
227 if (asprintf(&absprefix, "/%s", prefix) == -1)
228 return got_error_from_errno();
231 /* Create top-level directory (may already exist). */
232 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
233 err = got_error_from_errno();
234 goto done;
237 /* Create .got directory (may already exist). */
238 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
239 err = got_error_from_errno();
240 goto done;
242 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
243 err = got_error_from_errno();
244 goto done;
247 /* Create an empty lock file. */
248 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
249 if (err)
250 goto done;
252 /* Create an empty file index. */
253 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
254 if (err)
255 goto done;
257 /* Write the HEAD reference. */
258 refstr = got_ref_to_str(head_ref);
259 if (refstr == NULL) {
260 err = got_error_from_errno();
261 goto done;
263 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
264 if (err)
265 goto done;
267 /* Record our base commit. */
268 err = got_object_id_str(&basestr, commit_id);
269 if (err)
270 goto done;
271 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 if (err)
273 goto done;
275 /* Store path to repository. */
276 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
277 got_repo_get_path(repo));
278 if (err)
279 goto done;
281 /* Store in-repository path prefix. */
282 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
283 absprefix ? absprefix : prefix);
284 if (err)
285 goto done;
287 /* Generate UUID. */
288 uuid_create(&uuid, &uuid_status);
289 if (uuid_status != uuid_s_ok) {
290 err = got_error_uuid(uuid_status);
291 goto done;
293 uuid_to_string(&uuid, &uuidstr, &uuid_status);
294 if (uuid_status != uuid_s_ok) {
295 err = got_error_uuid(uuid_status);
296 goto done;
298 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 if (err)
300 goto done;
302 /* Stamp work tree with format file. */
303 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
304 err = got_error_from_errno();
305 goto done;
307 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 if (err)
309 goto done;
311 done:
312 free(commit_id);
313 free(path_got);
314 free(formatstr);
315 free(refstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 char *head_ref_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno();
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno();
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno());
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error(GOT_ERR_WORKTREE_META);
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno();
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno();
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status);
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
418 if (err)
419 goto done;
421 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
422 done:
423 if (repo)
424 got_repo_close(repo);
425 free(path_got);
426 free(path_lock);
427 free(head_ref_str);
428 free(base_commit_id_str);
429 free(uuidstr);
430 free(formatstr);
431 if (err) {
432 if (fd != -1)
433 close(fd);
434 if (*worktree != NULL)
435 got_worktree_close(*worktree);
436 *worktree = NULL;
437 } else
438 (*worktree)->lockfd = fd;
440 return err;
443 const struct got_error *
444 got_worktree_open(struct got_worktree **worktree, const char *path)
446 const struct got_error *err = NULL;
448 do {
449 err = open_worktree(worktree, path);
450 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
451 return err;
452 if (*worktree)
453 return NULL;
454 path = dirname(path);
455 if (path == NULL)
456 return got_error_from_errno();
457 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
459 return got_error(GOT_ERR_NOT_WORKTREE);
462 const struct got_error *
463 got_worktree_close(struct got_worktree *worktree)
465 const struct got_error *err = NULL;
466 free(worktree->root_path);
467 free(worktree->repo_path);
468 free(worktree->path_prefix);
469 free(worktree->base_commit_id);
470 if (worktree->head_ref)
471 got_ref_close(worktree->head_ref);
472 if (worktree->lockfd != -1)
473 if (close(worktree->lockfd) != 0)
474 err = got_error_from_errno();
475 free(worktree);
476 return err;
479 const char *
480 got_worktree_get_root_path(struct got_worktree *worktree)
482 return worktree->root_path;
485 const char *
486 got_worktree_get_repo_path(struct got_worktree *worktree)
488 return worktree->repo_path;
491 const char *
492 got_worktree_get_path_prefix(struct got_worktree *worktree)
494 return worktree->path_prefix;
497 const struct got_error *
498 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
499 const char *path_prefix)
501 char *absprefix = NULL;
503 if (!got_path_is_absolute(path_prefix)) {
504 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
505 return got_error_from_errno();
507 *match = (strcmp(absprefix ? absprefix : path_prefix,
508 worktree->path_prefix) == 0);
509 free(absprefix);
510 return NULL;
513 char *
514 got_worktree_get_head_ref_name(struct got_worktree *worktree)
516 return got_ref_to_str(worktree->head_ref);
519 struct got_reference *
520 got_worktree_get_head_ref(struct got_worktree *worktree)
522 return got_ref_dup(worktree->head_ref);
525 struct got_object_id *
526 got_worktree_get_base_commit_id(struct got_worktree *worktree)
528 return worktree->base_commit_id;
531 const struct got_error *
532 got_worktree_set_base_commit_id(struct got_worktree *worktree,
533 struct got_repository *repo, struct got_object_id *commit_id)
535 const struct got_error *err;
536 struct got_object *obj = NULL;
537 char *id_str = NULL;
538 char *path_got = NULL;
540 if (asprintf(&path_got, "%s/%s", worktree->root_path,
541 GOT_WORKTREE_GOT_DIR) == -1) {
542 err = got_error_from_errno();
543 path_got = NULL;
544 goto done;
547 err = got_object_open(&obj, repo, commit_id);
548 if (err)
549 return err;
551 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
552 err = got_error(GOT_ERR_OBJ_TYPE);
553 goto done;
556 /* Record our base commit. */
557 err = got_object_id_str(&id_str, commit_id);
558 if (err)
559 goto done;
560 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
561 if (err)
562 goto done;
564 free(worktree->base_commit_id);
565 worktree->base_commit_id = got_object_id_dup(commit_id);
566 if (worktree->base_commit_id == NULL) {
567 err = got_error_from_errno();
568 goto done;
570 done:
571 if (obj)
572 got_object_close(obj);
573 free(id_str);
574 free(path_got);
575 return err;
578 static const struct got_error *
579 lock_worktree(struct got_worktree *worktree, int operation)
581 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
582 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
583 : got_error_from_errno());
584 return NULL;
587 static const struct got_error *
588 add_dir_on_disk(struct got_worktree *worktree, const char *path)
590 const struct got_error *err = NULL;
591 char *abspath;
593 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
594 return got_error_from_errno();
596 err = got_path_mkdir(abspath);
597 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
598 struct stat sb;
599 err = NULL;
600 if (lstat(abspath, &sb) == -1) {
601 err = got_error_from_errno();
602 } else if (!S_ISDIR(sb.st_mode)) {
603 /* TODO directory is obstructed; do something */
604 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
607 free(abspath);
608 return err;
611 static const struct got_error *
612 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
614 const struct got_error *err = NULL;
615 uint8_t fbuf1[8192];
616 uint8_t fbuf2[8192];
617 size_t flen1 = 0, flen2 = 0;
619 *same = 1;
621 while (1) {
622 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
623 if (flen1 == 0 && ferror(f1)) {
624 err = got_error_from_errno();
625 break;
627 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
628 if (flen2 == 0 && ferror(f2)) {
629 err = got_error_from_errno();
630 break;
632 if (flen1 == 0) {
633 if (flen2 != 0)
634 *same = 0;
635 break;
636 } else if (flen2 == 0) {
637 if (flen1 != 0)
638 *same = 0;
639 break;
640 } else if (flen1 == flen2) {
641 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
642 *same = 0;
643 break;
645 } else {
646 *same = 0;
647 break;
651 return err;
654 static const struct got_error *
655 check_files_equal(int *same, const char *f1_path, const char *f2_path)
657 const struct got_error *err = NULL;
658 struct stat sb;
659 size_t size1, size2;
660 FILE *f1 = NULL, *f2 = NULL;
662 *same = 1;
664 if (lstat(f1_path, &sb) != 0) {
665 err = got_error_from_errno();
666 goto done;
668 size1 = sb.st_size;
670 if (lstat(f2_path, &sb) != 0) {
671 err = got_error_from_errno();
672 goto done;
674 size2 = sb.st_size;
676 if (size1 != size2) {
677 *same = 0;
678 return NULL;
681 f1 = fopen(f1_path, "r");
682 if (f1 == NULL)
683 return got_error_from_errno();
685 f2 = fopen(f2_path, "r");
686 if (f2 == NULL) {
687 err = got_error_from_errno();
688 goto done;
691 err = check_file_contents_equal(same, f1, f2);
692 done:
693 if (f1 && fclose(f1) != 0 && err == NULL)
694 err = got_error_from_errno();
695 if (f2 && fclose(f2) != 0 && err == NULL)
696 err = got_error_from_errno();
698 return err;
701 /*
702 * Perform a 3-way merge where the file's version in the file index (blob2)
703 * acts as the common ancestor, the incoming blob (blob1) acts as the first
704 * derived version, and the file on disk acts as the second derived version.
705 */
706 static const struct got_error *
707 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
708 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
709 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
710 struct got_repository *repo,
711 got_worktree_checkout_cb progress_cb, void *progress_arg)
713 const struct got_error *err = NULL;
714 int merged_fd = -1;
715 struct got_blob_object *blob2 = NULL;
716 FILE *f1 = NULL, *f2 = NULL;
717 char *blob1_path = NULL, *blob2_path = NULL;
718 char *merged_path = NULL, *base_path = NULL;
719 char *id_str = NULL;
720 char *label1 = NULL;
721 int overlapcnt = 0, update_timestamps = 0;
722 char *parent;
724 parent = dirname(ondisk_path);
725 if (parent == NULL)
726 return got_error_from_errno();
728 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
729 return got_error_from_errno();
731 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
732 if (err)
733 goto done;
735 free(base_path);
736 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
737 err = got_error_from_errno();
738 base_path = NULL;
739 goto done;
742 err = got_opentemp_named(&blob1_path, &f1, base_path);
743 if (err)
744 goto done;
745 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
746 if (err)
747 goto done;
749 free(base_path);
750 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
751 err = got_error_from_errno();
752 base_path = NULL;
753 goto done;
756 err = got_opentemp_named(&blob2_path, &f2, base_path);
757 if (err)
758 goto done;
759 if (got_fileindex_entry_has_blob(ie)) {
760 struct got_object_id id2;
761 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
762 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
763 if (err)
764 goto done;
765 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
766 if (err)
767 goto done;
768 } else {
769 /*
770 * If the file has no blob, this is an "add vs add" conflict,
771 * and we simply use an empty ancestor file to make both files
772 * appear in the merged result in their entirety.
773 */
776 err = got_object_id_str(&id_str, worktree->base_commit_id);
777 if (err)
778 goto done;
779 if (asprintf(&label1, "commit %s", id_str) == -1) {
780 err = got_error_from_errno();
781 goto done;
784 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
785 blob2_path, ondisk_path, label1, path);
786 if (err)
787 goto done;
789 (*progress_cb)(progress_arg,
790 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
793 if (fsync(merged_fd) != 0) {
794 err = got_error_from_errno();
795 goto done;
798 /* Check if a clean merge has subsumed all local changes. */
799 if (overlapcnt == 0) {
800 err = check_files_equal(&update_timestamps, blob1_path,
801 merged_path);
802 if (err)
803 goto done;
806 if (chmod(merged_path, st_mode) != 0) {
807 err = got_error_from_errno();
808 goto done;
811 if (rename(merged_path, ondisk_path) != 0) {
812 err = got_error_from_errno();
813 unlink(merged_path);
814 goto done;
817 /*
818 * Do not update timestamps of already modified files. Otherwise,
819 * a future status walk would treat them as unmodified files again.
820 */
821 err = got_fileindex_entry_update(ie, ondisk_path,
822 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
823 done:
824 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
825 err = got_error_from_errno();
826 if (f1 && fclose(f1) != 0 && err == NULL)
827 err = got_error_from_errno();
828 if (f2 && fclose(f2) != 0 && err == NULL)
829 err = got_error_from_errno();
830 if (blob2)
831 got_object_blob_close(blob2);
832 free(merged_path);
833 free(base_path);
834 if (blob1_path) {
835 unlink(blob1_path);
836 free(blob1_path);
838 if (blob2_path) {
839 unlink(blob2_path);
840 free(blob2_path);
842 free(id_str);
843 free(label1);
844 return err;
847 static const struct got_error *
848 update_blob_fileindex_entry(struct got_worktree *worktree,
849 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
850 const char *ondisk_path, const char *path, struct got_blob_object *blob,
851 int update_timestamps)
853 const struct got_error *err = NULL;
855 if (ie == NULL)
856 ie = got_fileindex_entry_get(fileindex, path);
857 if (ie)
858 err = got_fileindex_entry_update(ie, ondisk_path,
859 blob->id.sha1, worktree->base_commit_id->sha1,
860 update_timestamps);
861 else {
862 struct got_fileindex_entry *new_ie;
863 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
864 path, blob->id.sha1, worktree->base_commit_id->sha1);
865 if (!err)
866 err = got_fileindex_entry_add(fileindex, new_ie);
868 return err;
871 static const struct got_error *
872 install_blob(struct got_worktree *worktree, const char *ondisk_path,
873 const char *path, uint16_t te_mode, uint16_t st_mode,
874 struct got_blob_object *blob, int restoring_missing_file,
875 int reverting_versioned_file, struct got_repository *repo,
876 got_worktree_checkout_cb progress_cb, void *progress_arg)
878 const struct got_error *err = NULL;
879 int fd = -1;
880 size_t len, hdrlen;
881 int update = 0;
882 char *tmppath = NULL;
884 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
885 GOT_DEFAULT_FILE_MODE);
886 if (fd == -1) {
887 if (errno == ENOENT) {
888 char *parent = dirname(path);
889 if (parent == NULL)
890 return got_error_from_errno();
891 err = add_dir_on_disk(worktree, parent);
892 if (err)
893 return err;
894 fd = open(ondisk_path,
895 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 GOT_DEFAULT_FILE_MODE);
897 if (fd == -1)
898 return got_error_from_errno();
899 } else if (errno == EEXIST) {
900 if (!S_ISREG(st_mode)) {
901 /* TODO file is obstructed; do something */
902 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
903 goto done;
904 } else {
905 err = got_opentemp_named_fd(&tmppath, &fd,
906 ondisk_path);
907 if (err)
908 goto done;
909 update = 1;
911 } else
912 return got_error_from_errno();
915 if (restoring_missing_file)
916 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
917 else if (reverting_versioned_file)
918 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
919 else
920 (*progress_cb)(progress_arg,
921 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
923 hdrlen = got_object_blob_get_hdrlen(blob);
924 do {
925 const uint8_t *buf = got_object_blob_get_read_buf(blob);
926 err = got_object_blob_read_block(&len, blob);
927 if (err)
928 break;
929 if (len > 0) {
930 /* Skip blob object header first time around. */
931 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
932 if (outlen == -1) {
933 err = got_error_from_errno();
934 goto done;
935 } else if (outlen != len - hdrlen) {
936 err = got_error(GOT_ERR_IO);
937 goto done;
939 hdrlen = 0;
941 } while (len != 0);
943 if (fsync(fd) != 0) {
944 err = got_error_from_errno();
945 goto done;
948 if (update) {
949 if (rename(tmppath, ondisk_path) != 0) {
950 err = got_error_from_errno();
951 unlink(tmppath);
952 goto done;
956 if (te_mode & S_IXUSR) {
957 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
958 err = got_error_from_errno();
959 goto done;
961 } else {
962 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
963 err = got_error_from_errno();
964 goto done;
968 done:
969 if (fd != -1 && close(fd) != 0 && err == NULL)
970 err = got_error_from_errno();
971 free(tmppath);
972 return err;
975 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
976 static const struct got_error *
977 get_modified_file_content_status(unsigned char *status, FILE *f)
979 const struct got_error *err = NULL;
980 const char *markers[3] = {
981 GOT_DIFF_CONFLICT_MARKER_BEGIN,
982 GOT_DIFF_CONFLICT_MARKER_SEP,
983 GOT_DIFF_CONFLICT_MARKER_END
984 };
985 int i = 0;
986 char *line;
987 size_t len;
988 const char delim[3] = {'\0', '\0', '\0'};
990 while (*status == GOT_STATUS_MODIFY) {
991 line = fparseln(f, &len, NULL, delim, 0);
992 if (line == NULL) {
993 if (feof(f))
994 break;
995 err = got_ferror(f, GOT_ERR_IO);
996 break;
999 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1000 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1001 *status = GOT_STATUS_CONFLICT;
1002 else
1003 i++;
1007 return err;
1010 static const struct got_error *
1011 get_file_status(unsigned char *status, struct stat *sb,
1012 struct got_fileindex_entry *ie, const char *abspath,
1013 struct got_repository *repo)
1015 const struct got_error *err = NULL;
1016 struct got_object_id id;
1017 size_t hdrlen;
1018 FILE *f = NULL;
1019 uint8_t fbuf[8192];
1020 struct got_blob_object *blob = NULL;
1021 size_t flen, blen;
1023 *status = GOT_STATUS_NO_CHANGE;
1025 if (lstat(abspath, sb) == -1) {
1026 if (errno == ENOENT) {
1027 if (ie) {
1028 if (got_fileindex_entry_has_file_on_disk(ie))
1029 *status = GOT_STATUS_MISSING;
1030 else
1031 *status = GOT_STATUS_DELETE;
1032 sb->st_mode =
1033 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1034 & (S_IRWXU | S_IRWXG | S_IRWXO));
1035 } else
1036 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1037 return NULL;
1039 return got_error_from_errno();
1042 if (!S_ISREG(sb->st_mode)) {
1043 *status = GOT_STATUS_OBSTRUCTED;
1044 return NULL;
1047 if (ie == NULL)
1048 return NULL;
1050 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1051 *status = GOT_STATUS_DELETE;
1052 return NULL;
1053 } else if (!got_fileindex_entry_has_blob(ie)) {
1054 *status = GOT_STATUS_ADD;
1055 return NULL;
1058 if (ie->ctime_sec == sb->st_ctime &&
1059 ie->ctime_nsec == sb->st_ctimensec &&
1060 ie->mtime_sec == sb->st_mtime &&
1061 ie->mtime_sec == sb->st_mtime &&
1062 ie->mtime_nsec == sb->st_mtimensec &&
1063 ie->size == (sb->st_size & 0xffffffff))
1064 return NULL;
1066 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1067 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1068 if (err)
1069 return err;
1071 f = fopen(abspath, "r");
1072 if (f == NULL) {
1073 err = got_error_from_errno();
1074 goto done;
1076 hdrlen = got_object_blob_get_hdrlen(blob);
1077 while (1) {
1078 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1079 err = got_object_blob_read_block(&blen, blob);
1080 if (err)
1081 goto done;
1082 /* Skip length of blob object header first time around. */
1083 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1084 if (flen == 0 && ferror(f)) {
1085 err = got_error_from_errno();
1086 goto done;
1088 if (blen == 0) {
1089 if (flen != 0)
1090 *status = GOT_STATUS_MODIFY;
1091 break;
1092 } else if (flen == 0) {
1093 if (blen != 0)
1094 *status = GOT_STATUS_MODIFY;
1095 break;
1096 } else if (blen - hdrlen == flen) {
1097 /* Skip blob object header first time around. */
1098 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1099 *status = GOT_STATUS_MODIFY;
1100 break;
1102 } else {
1103 *status = GOT_STATUS_MODIFY;
1104 break;
1106 hdrlen = 0;
1109 if (*status == GOT_STATUS_MODIFY) {
1110 rewind(f);
1111 err = get_modified_file_content_status(status, f);
1113 done:
1114 if (blob)
1115 got_object_blob_close(blob);
1116 if (f)
1117 fclose(f);
1118 return err;
1121 static const struct got_error *
1122 update_blob(struct got_worktree *worktree,
1123 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1124 struct got_tree_entry *te, const char *path,
1125 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1126 void *progress_arg)
1128 const struct got_error *err = NULL;
1129 struct got_blob_object *blob = NULL;
1130 char *ondisk_path;
1131 unsigned char status = GOT_STATUS_NO_CHANGE;
1132 struct stat sb;
1134 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1135 return got_error_from_errno();
1137 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1138 if (err)
1139 goto done;
1141 if (status == GOT_STATUS_OBSTRUCTED) {
1142 (*progress_cb)(progress_arg, status, path);
1143 goto done;
1146 if (ie && status != GOT_STATUS_MISSING) {
1147 if (got_fileindex_entry_has_commit(ie) &&
1148 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1149 SHA1_DIGEST_LENGTH) == 0) {
1150 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1151 path);
1152 goto done;
1154 if (got_fileindex_entry_has_blob(ie) &&
1155 memcmp(ie->blob_sha1, te->id->sha1,
1156 SHA1_DIGEST_LENGTH) == 0)
1157 goto done;
1160 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1161 if (err)
1162 goto done;
1164 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1165 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1166 te->mode, sb.st_mode, blob, repo, progress_cb,
1167 progress_arg);
1168 else if (status == GOT_STATUS_DELETE) {
1169 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1170 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1171 ondisk_path, path, blob, 0);
1172 if (err)
1173 goto done;
1174 } else {
1175 err = install_blob(worktree, ondisk_path, path, te->mode,
1176 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1177 repo, progress_cb, progress_arg);
1178 if (err)
1179 goto done;
1180 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1181 ondisk_path, path, blob, 1);
1182 if (err)
1183 goto done;
1185 got_object_blob_close(blob);
1186 done:
1187 free(ondisk_path);
1188 return err;
1191 static const struct got_error *
1192 remove_ondisk_file(const char *root_path, const char *path)
1194 const struct got_error *err = NULL;
1195 char *ondisk_path = NULL;
1197 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1198 return got_error_from_errno();
1200 if (unlink(ondisk_path) == -1) {
1201 if (errno != ENOENT)
1202 err = got_error_from_errno();
1203 } else {
1204 char *parent = dirname(ondisk_path);
1205 while (parent && strcmp(parent, root_path) != 0) {
1206 if (rmdir(parent) == -1) {
1207 if (errno != ENOTEMPTY)
1208 err = got_error_from_errno();
1209 break;
1211 parent = dirname(parent);
1214 free(ondisk_path);
1215 return err;
1218 static const struct got_error *
1219 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1220 struct got_fileindex_entry *ie, const char *parent_path,
1221 struct got_repository *repo,
1222 got_worktree_checkout_cb progress_cb, void *progress_arg)
1224 const struct got_error *err = NULL;
1225 unsigned char status;
1226 struct stat sb;
1227 char *ondisk_path;
1229 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1230 == -1)
1231 return got_error_from_errno();
1233 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1234 if (err)
1235 return err;
1237 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1238 status == GOT_STATUS_ADD) {
1239 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1241 * Preserve the working file and change the deleted blob's
1242 * entry into a schedule-add entry.
1244 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1245 0);
1246 if (err)
1247 return err;
1248 } else {
1249 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1250 if (status == GOT_STATUS_NO_CHANGE) {
1251 err = remove_ondisk_file(worktree->root_path, ie->path);
1252 if (err)
1253 return err;
1255 got_fileindex_entry_remove(fileindex, ie);
1258 return err;
1261 struct diff_cb_arg {
1262 struct got_fileindex *fileindex;
1263 struct got_worktree *worktree;
1264 struct got_repository *repo;
1265 got_worktree_checkout_cb progress_cb;
1266 void *progress_arg;
1267 got_worktree_cancel_cb cancel_cb;
1268 void *cancel_arg;
1271 static const struct got_error *
1272 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1273 struct got_tree_entry *te, const char *parent_path)
1275 struct diff_cb_arg *a = arg;
1277 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1278 return got_error(GOT_ERR_CANCELLED);
1280 return update_blob(a->worktree, a->fileindex, ie, te,
1281 ie->path, a->repo, a->progress_cb, a->progress_arg);
1284 static const struct got_error *
1285 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1287 struct diff_cb_arg *a = arg;
1289 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1290 return got_error(GOT_ERR_CANCELLED);
1292 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1293 a->repo, a->progress_cb, a->progress_arg);
1296 static const struct got_error *
1297 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1299 struct diff_cb_arg *a = arg;
1300 const struct got_error *err;
1301 char *path;
1303 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1304 return got_error(GOT_ERR_CANCELLED);
1306 if (asprintf(&path, "%s%s%s", parent_path,
1307 parent_path[0] ? "/" : "", te->name)
1308 == -1)
1309 return got_error_from_errno();
1311 if (S_ISDIR(te->mode))
1312 err = add_dir_on_disk(a->worktree, path);
1313 else
1314 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1315 a->repo, a->progress_cb, a->progress_arg);
1317 free(path);
1318 return err;
1321 const struct got_error *
1322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1324 const struct got_error *err = NULL;
1325 char *uuidstr = NULL;
1326 uint32_t uuid_status;
1328 *refname = NULL;
1330 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1331 if (uuid_status != uuid_s_ok)
1332 return got_error_uuid(uuid_status);
1334 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1335 == -1) {
1336 err = got_error_from_errno();
1337 *refname = NULL;
1339 free(uuidstr);
1340 return err;
1344 * Prevent Git's garbage collector from deleting our base commit by
1345 * setting a reference to our base commit's ID.
1347 static const struct got_error *
1348 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1350 const struct got_error *err = NULL;
1351 struct got_reference *ref = NULL;
1352 char *refname;
1354 err = got_worktree_get_base_ref_name(&refname, worktree);
1355 if (err)
1356 return err;
1358 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1359 if (err)
1360 goto done;
1362 err = got_ref_write(ref, repo);
1363 done:
1364 free(refname);
1365 if (ref)
1366 got_ref_close(ref);
1367 return err;
1371 const struct got_error *
1372 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1373 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1374 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1376 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1377 struct got_commit_object *commit = NULL;
1378 struct got_object_id *tree_id = NULL;
1379 struct got_tree_object *tree = NULL;
1380 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1381 struct got_fileindex *fileindex = NULL;
1382 FILE *index = NULL, *new_index = NULL;
1383 struct got_fileindex_diff_tree_cb diff_cb;
1384 struct diff_cb_arg arg;
1385 char *relpath = NULL, *entry_name = NULL;
1387 err = lock_worktree(worktree, LOCK_EX);
1388 if (err)
1389 return err;
1391 fileindex = got_fileindex_alloc();
1392 if (fileindex == NULL) {
1393 err = got_error_from_errno();
1394 goto done;
1397 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1398 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1399 err = got_error_from_errno();
1400 fileindex_path = NULL;
1401 goto done;
1405 * Read the file index.
1406 * Checking out files is supposed to be an idempotent operation.
1407 * If the on-disk file index is incomplete we will try to complete it.
1409 index = fopen(fileindex_path, "rb");
1410 if (index == NULL) {
1411 if (errno != ENOENT) {
1412 err = got_error_from_errno();
1413 goto done;
1415 } else {
1416 err = got_fileindex_read(fileindex, index);
1417 fclose(index);
1418 if (err)
1419 goto done;
1422 err = got_opentemp_named(&new_fileindex_path, &new_index,
1423 fileindex_path);
1424 if (err)
1425 goto done;
1427 err = ref_base_commit(worktree, repo);
1428 if (err)
1429 goto done;
1431 err = got_object_open_as_commit(&commit, repo,
1432 worktree->base_commit_id);
1433 if (err)
1434 goto done;
1436 if (path[0]) {
1437 char *tree_path;
1438 int obj_type;
1439 relpath = strdup(path);
1440 if (relpath == NULL) {
1441 err = got_error_from_errno();
1442 goto done;
1444 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1445 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1446 path) == -1) {
1447 err = got_error_from_errno();
1448 goto done;
1450 err = got_object_id_by_path(&tree_id, repo,
1451 worktree->base_commit_id, tree_path);
1452 free(tree_path);
1453 if (err)
1454 goto done;
1455 err = got_object_get_type(&obj_type, repo, tree_id);
1456 if (err)
1457 goto done;
1458 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1459 /* Split provided path into parent dir + entry name. */
1460 if (strchr(path, '/') == NULL) {
1461 relpath = strdup("");
1462 if (relpath == NULL) {
1463 err = got_error_from_errno();
1464 goto done;
1466 tree_path = strdup(worktree->path_prefix);
1467 if (tree_path == NULL) {
1468 err = got_error_from_errno();
1469 goto done;
1471 } else {
1472 err = got_path_dirname(&relpath, path);
1473 if (err)
1474 goto done;
1475 if (asprintf(&tree_path, "%s%s%s",
1476 worktree->path_prefix,
1477 got_path_is_root_dir(
1478 worktree->path_prefix) ? "" : "/",
1479 relpath) == -1) {
1480 err = got_error_from_errno();
1481 goto done;
1484 err = got_object_id_by_path(&tree_id, repo,
1485 worktree->base_commit_id, tree_path);
1486 free(tree_path);
1487 if (err)
1488 goto done;
1489 entry_name = basename(path);
1490 if (entry_name == NULL) {
1491 err = got_error_from_errno();
1492 goto done;
1495 } else {
1496 relpath = strdup("");
1497 if (relpath == NULL) {
1498 err = got_error_from_errno();
1499 goto done;
1501 err = got_object_id_by_path(&tree_id, repo,
1502 worktree->base_commit_id, worktree->path_prefix);
1503 if (err)
1504 goto done;
1507 err = got_object_open_as_tree(&tree, repo, tree_id);
1508 if (err)
1509 goto done;
1511 if (entry_name &&
1512 got_object_tree_find_entry(tree, entry_name) == NULL) {
1513 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1514 goto done;
1517 diff_cb.diff_old_new = diff_old_new;
1518 diff_cb.diff_old = diff_old;
1519 diff_cb.diff_new = diff_new;
1520 arg.fileindex = fileindex;
1521 arg.worktree = worktree;
1522 arg.repo = repo;
1523 arg.progress_cb = progress_cb;
1524 arg.progress_arg = progress_arg;
1525 arg.cancel_cb = cancel_cb;
1526 arg.cancel_arg = cancel_arg;
1527 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1528 entry_name, repo, &diff_cb, &arg);
1530 /* Try to sync the fileindex back to disk in any case. */
1531 err = got_fileindex_write(fileindex, new_index);
1532 if (err)
1533 goto done;
1535 if (rename(new_fileindex_path, fileindex_path) != 0) {
1536 err = got_error_from_errno();
1537 unlink(new_fileindex_path);
1538 goto done;
1541 free(new_fileindex_path);
1542 new_fileindex_path = NULL;
1544 done:
1545 free(relpath);
1546 if (tree)
1547 got_object_tree_close(tree);
1548 if (commit)
1549 got_object_commit_close(commit);
1550 if (new_fileindex_path)
1551 unlink(new_fileindex_path);
1552 if (new_index)
1553 fclose(new_index);
1554 free(new_fileindex_path);
1555 free(fileindex_path);
1556 got_fileindex_free(fileindex);
1557 if (checkout_err)
1558 err = checkout_err;
1559 unlockerr = lock_worktree(worktree, LOCK_SH);
1560 if (unlockerr && err == NULL)
1561 err = unlockerr;
1562 return err;
1565 struct diff_dir_cb_arg {
1566 struct got_fileindex *fileindex;
1567 struct got_worktree *worktree;
1568 const char *status_path;
1569 size_t status_path_len;
1570 struct got_repository *repo;
1571 got_worktree_status_cb status_cb;
1572 void *status_arg;
1573 got_worktree_cancel_cb cancel_cb;
1574 void *cancel_arg;
1577 static const struct got_error *
1578 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1579 got_worktree_status_cb status_cb, void *status_arg,
1580 struct got_repository *repo)
1582 const struct got_error *err = NULL;
1583 unsigned char status = GOT_STATUS_NO_CHANGE;
1584 struct stat sb;
1585 struct got_object_id id;
1587 err = get_file_status(&status, &sb, ie, abspath, repo);
1588 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1589 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1590 err = (*status_cb)(status_arg, status, ie->path, &id);
1592 return err;
1595 static const struct got_error *
1596 status_old_new(void *arg, struct got_fileindex_entry *ie,
1597 struct dirent *de, const char *parent_path)
1599 const struct got_error *err = NULL;
1600 struct diff_dir_cb_arg *a = arg;
1601 char *abspath;
1603 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1604 return got_error(GOT_ERR_CANCELLED);
1606 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1607 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1608 return NULL;
1610 if (parent_path[0]) {
1611 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1612 parent_path, de->d_name) == -1)
1613 return got_error_from_errno();
1614 } else {
1615 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1616 de->d_name) == -1)
1617 return got_error_from_errno();
1620 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1621 a->repo);
1622 free(abspath);
1623 return err;
1626 static const struct got_error *
1627 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1629 struct diff_dir_cb_arg *a = arg;
1630 struct got_object_id id;
1631 unsigned char status;
1633 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1634 return got_error(GOT_ERR_CANCELLED);
1636 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1637 return NULL;
1639 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1640 if (got_fileindex_entry_has_file_on_disk(ie))
1641 status = GOT_STATUS_MISSING;
1642 else
1643 status = GOT_STATUS_DELETE;
1644 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1647 static const struct got_error *
1648 status_new(void *arg, struct dirent *de, const char *parent_path)
1650 const struct got_error *err = NULL;
1651 struct diff_dir_cb_arg *a = arg;
1652 char *path = NULL;
1654 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1655 return got_error(GOT_ERR_CANCELLED);
1657 if (de->d_type == DT_DIR)
1658 return NULL;
1660 /* XXX ignore symlinks for now */
1661 if (de->d_type == DT_LNK)
1662 return NULL;
1664 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1665 return NULL;
1667 if (parent_path[0]) {
1668 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1669 return got_error_from_errno();
1670 } else {
1671 path = de->d_name;
1674 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1675 NULL);
1676 if (parent_path[0])
1677 free(path);
1678 return err;
1681 const struct got_error *
1682 got_worktree_status(struct got_worktree *worktree, const char *path,
1683 struct got_repository *repo, got_worktree_status_cb status_cb,
1684 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1686 const struct got_error *err = NULL;
1687 DIR *workdir = NULL;
1688 char *fileindex_path = NULL;
1689 struct got_fileindex *fileindex = NULL;
1690 FILE *index = NULL;
1691 struct got_fileindex_diff_dir_cb fdiff_cb;
1692 struct diff_dir_cb_arg arg;
1693 char *ondisk_path = NULL;
1695 fileindex = got_fileindex_alloc();
1696 if (fileindex == NULL) {
1697 err = got_error_from_errno();
1698 goto done;
1701 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1702 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1703 err = got_error_from_errno();
1704 fileindex_path = NULL;
1705 goto done;
1708 index = fopen(fileindex_path, "rb");
1709 if (index == NULL) {
1710 if (errno != ENOENT) {
1711 err = got_error_from_errno();
1712 goto done;
1714 } else {
1715 err = got_fileindex_read(fileindex, index);
1716 fclose(index);
1717 if (err)
1718 goto done;
1721 if (asprintf(&ondisk_path, "%s%s%s",
1722 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1723 err = got_error_from_errno();
1724 goto done;
1726 workdir = opendir(ondisk_path);
1727 if (workdir == NULL) {
1728 if (errno == ENOTDIR || errno == ENOENT) {
1729 struct got_fileindex_entry *ie;
1730 ie = got_fileindex_entry_get(fileindex, path);
1731 if (ie == NULL) {
1732 err = got_error(GOT_ERR_BAD_PATH);
1733 goto done;
1735 err = report_file_status(ie, ondisk_path,
1736 status_cb, status_arg, repo);
1737 goto done;
1738 } else {
1739 err = got_error_from_errno();
1740 goto done;
1743 fdiff_cb.diff_old_new = status_old_new;
1744 fdiff_cb.diff_old = status_old;
1745 fdiff_cb.diff_new = status_new;
1746 arg.fileindex = fileindex;
1747 arg.worktree = worktree;
1748 arg.status_path = path;
1749 arg.status_path_len = strlen(path);
1750 arg.repo = repo;
1751 arg.status_cb = status_cb;
1752 arg.status_arg = status_arg;
1753 arg.cancel_cb = cancel_cb;
1754 arg.cancel_arg = cancel_arg;
1755 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1756 path, repo, &fdiff_cb, &arg);
1757 done:
1758 if (workdir)
1759 closedir(workdir);
1760 free(ondisk_path);
1761 free(fileindex_path);
1762 got_fileindex_free(fileindex);
1763 return err;
1766 const struct got_error *
1767 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1768 const char *arg)
1770 const struct got_error *err = NULL;
1771 char *resolved, *path = NULL;
1772 size_t len;
1774 *wt_path = NULL;
1776 resolved = realpath(arg, NULL);
1777 if (resolved == NULL)
1778 return got_error_from_errno();
1780 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1781 strlen(got_worktree_get_root_path(worktree)))) {
1782 err = got_error(GOT_ERR_BAD_PATH);
1783 goto done;
1786 path = strdup(resolved +
1787 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1788 if (path == NULL) {
1789 err = got_error_from_errno();
1790 goto done;
1793 /* XXX status walk can't deal with trailing slash! */
1794 len = strlen(path);
1795 while (path[len - 1] == '/') {
1796 path[len - 1] = '\0';
1797 len--;
1799 done:
1800 free(resolved);
1801 if (err == NULL)
1802 *wt_path = path;
1803 else
1804 free(path);
1805 return err;
1808 const struct got_error *
1809 got_worktree_schedule_add(struct got_worktree *worktree,
1810 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1811 struct got_repository *repo)
1813 struct got_fileindex *fileindex = NULL;
1814 struct got_fileindex_entry *ie = NULL;
1815 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1816 FILE *index = NULL, *new_index = NULL;
1817 const struct got_error *err = NULL, *unlockerr = NULL;
1818 int ie_added = 0;
1820 err = lock_worktree(worktree, LOCK_EX);
1821 if (err)
1822 return err;
1824 err = got_path_skip_common_ancestor(&relpath,
1825 got_worktree_get_root_path(worktree), ondisk_path);
1826 if (err)
1827 goto done;
1829 fileindex = got_fileindex_alloc();
1830 if (fileindex == NULL) {
1831 err = got_error_from_errno();
1832 goto done;
1835 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1836 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1837 err = got_error_from_errno();
1838 fileindex_path = NULL;
1839 goto done;
1842 index = fopen(fileindex_path, "rb");
1843 if (index == NULL) {
1844 err = got_error_from_errno();
1845 goto done;
1848 err = got_fileindex_read(fileindex, index);
1849 if (err)
1850 goto done;
1852 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1853 err = got_error_set_errno(EEXIST);
1854 goto done;
1857 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1858 if (err)
1859 goto done;
1861 err = got_fileindex_entry_add(fileindex, ie);
1862 if (err)
1863 goto done;
1864 ie_added = 1; /* now owned by fileindex; don't free separately */
1866 err = got_opentemp_named(&new_fileindex_path, &new_index,
1867 fileindex_path);
1868 if (err)
1869 goto done;
1871 err = got_fileindex_write(fileindex, new_index);
1872 if (err)
1873 goto done;
1875 if (rename(new_fileindex_path, fileindex_path) != 0) {
1876 err = got_error_from_errno();
1877 goto done;
1880 free(new_fileindex_path);
1881 new_fileindex_path = NULL;
1883 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1884 done:
1885 if (index) {
1886 if (fclose(index) != 0 && err == NULL)
1887 err = got_error_from_errno();
1889 if (new_fileindex_path) {
1890 if (unlink(new_fileindex_path) != 0 && err == NULL)
1891 err = got_error_from_errno();
1892 free(new_fileindex_path);
1894 if (ie && !ie_added)
1895 got_fileindex_entry_free(ie);
1896 if (fileindex)
1897 got_fileindex_free(fileindex);
1898 unlockerr = lock_worktree(worktree, LOCK_SH);
1899 if (unlockerr && err == NULL)
1900 err = unlockerr;
1901 free(relpath);
1902 return err;
1905 const struct got_error *
1906 got_worktree_schedule_delete(struct got_worktree *worktree,
1907 const char *ondisk_path, int delete_local_mods,
1908 got_worktree_status_cb status_cb, void *status_arg,
1909 struct got_repository *repo)
1911 struct got_fileindex *fileindex = NULL;
1912 struct got_fileindex_entry *ie = NULL;
1913 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1914 FILE *index = NULL, *new_index = NULL;
1915 const struct got_error *err = NULL, *unlockerr = NULL;
1916 unsigned char status;
1917 struct stat sb;
1919 err = lock_worktree(worktree, LOCK_EX);
1920 if (err)
1921 return err;
1923 err = got_path_skip_common_ancestor(&relpath,
1924 got_worktree_get_root_path(worktree), ondisk_path);
1925 if (err)
1926 goto done;
1928 fileindex = got_fileindex_alloc();
1929 if (fileindex == NULL) {
1930 err = got_error_from_errno();
1931 goto done;
1934 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1935 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1936 err = got_error_from_errno();
1937 fileindex_path = NULL;
1938 goto done;
1941 index = fopen(fileindex_path, "rb");
1942 if (index == NULL) {
1943 err = got_error_from_errno();
1944 goto done;
1947 err = got_fileindex_read(fileindex, index);
1948 if (err)
1949 goto done;
1951 ie = got_fileindex_entry_get(fileindex, relpath);
1952 if (ie == NULL) {
1953 err = got_error(GOT_ERR_BAD_PATH);
1954 goto done;
1957 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1958 if (err)
1959 goto done;
1961 if (status != GOT_STATUS_NO_CHANGE) {
1962 if (status == GOT_STATUS_DELETE) {
1963 err = got_error_set_errno(ENOENT);
1964 goto done;
1966 if (status != GOT_STATUS_MODIFY) {
1967 err = got_error(GOT_ERR_FILE_STATUS);
1968 goto done;
1970 if (!delete_local_mods) {
1971 err = got_error(GOT_ERR_FILE_MODIFIED);
1972 goto done;
1976 if (unlink(ondisk_path) != 0) {
1977 err = got_error_from_errno();
1978 goto done;
1981 got_fileindex_entry_mark_deleted_from_disk(ie);
1983 err = got_opentemp_named(&new_fileindex_path, &new_index,
1984 fileindex_path);
1985 if (err)
1986 goto done;
1988 err = got_fileindex_write(fileindex, new_index);
1989 if (err)
1990 goto done;
1992 if (rename(new_fileindex_path, fileindex_path) != 0) {
1993 err = got_error_from_errno();
1994 goto done;
1997 free(new_fileindex_path);
1998 new_fileindex_path = NULL;
2000 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2001 done:
2002 free(relpath);
2003 if (index) {
2004 if (fclose(index) != 0 && err == NULL)
2005 err = got_error_from_errno();
2007 if (new_fileindex_path) {
2008 if (unlink(new_fileindex_path) != 0 && err == NULL)
2009 err = got_error_from_errno();
2010 free(new_fileindex_path);
2012 if (fileindex)
2013 got_fileindex_free(fileindex);
2014 unlockerr = lock_worktree(worktree, LOCK_SH);
2015 if (unlockerr && err == NULL)
2016 err = unlockerr;
2017 return err;
2020 const struct got_error *
2021 got_worktree_revert(struct got_worktree *worktree,
2022 const char *ondisk_path,
2023 got_worktree_checkout_cb progress_cb, void *progress_arg,
2024 struct got_repository *repo)
2026 struct got_fileindex *fileindex = NULL;
2027 struct got_fileindex_entry *ie = NULL;
2028 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2029 char *tree_path = NULL, *parent_path, *te_name;
2030 FILE *index = NULL, *new_index = NULL;
2031 const struct got_error *err = NULL, *unlockerr = NULL;
2032 struct got_tree_object *tree = NULL;
2033 struct got_object_id id, *tree_id = NULL;
2034 const struct got_tree_entry *te;
2035 struct got_blob_object *blob = NULL;
2036 unsigned char status;
2037 struct stat sb;
2039 err = lock_worktree(worktree, LOCK_EX);
2040 if (err)
2041 return err;
2043 err = got_path_skip_common_ancestor(&relpath,
2044 got_worktree_get_root_path(worktree), ondisk_path);
2045 if (err)
2046 goto done;
2048 fileindex = got_fileindex_alloc();
2049 if (fileindex == NULL) {
2050 err = got_error_from_errno();
2051 goto done;
2054 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2055 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2056 err = got_error_from_errno();
2057 fileindex_path = NULL;
2058 goto done;
2061 index = fopen(fileindex_path, "rb");
2062 if (index == NULL) {
2063 err = got_error_from_errno();
2064 goto done;
2067 err = got_fileindex_read(fileindex, index);
2068 if (err)
2069 goto done;
2071 ie = got_fileindex_entry_get(fileindex, relpath);
2072 if (ie == NULL) {
2073 err = got_error(GOT_ERR_BAD_PATH);
2074 goto done;
2077 /* Construct in-repository path of tree which contains this blob. */
2078 err = got_path_dirname(&parent_path, ie->path);
2079 if (err) {
2080 if (err->code != GOT_ERR_BAD_PATH)
2081 goto done;
2082 parent_path = "/";
2084 if (got_path_is_root_dir(worktree->path_prefix)) {
2085 tree_path = strdup(parent_path);
2086 if (tree_path == NULL) {
2087 err = got_error_from_errno();
2088 goto done;
2090 } else {
2091 if (got_path_is_root_dir(parent_path)) {
2092 tree_path = strdup(worktree->path_prefix);
2093 if (tree_path == NULL) {
2094 err = got_error_from_errno();
2095 goto done;
2097 } else {
2098 if (asprintf(&tree_path, "%s/%s",
2099 worktree->path_prefix, parent_path) == -1) {
2100 err = got_error_from_errno();
2101 goto done;
2106 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2107 tree_path);
2108 if (err)
2109 goto done;
2111 err = got_object_open_as_tree(&tree, repo, tree_id);
2112 if (err)
2113 goto done;
2115 te_name = basename(ie->path);
2116 if (te_name == NULL) {
2117 err = got_error_from_errno();
2118 goto done;
2121 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2122 if (err)
2123 goto done;
2125 te = got_object_tree_find_entry(tree, te_name);
2126 if (te == NULL && status != GOT_STATUS_ADD) {
2127 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2128 goto done;
2131 switch (status) {
2132 case GOT_STATUS_ADD:
2133 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2134 got_fileindex_entry_remove(fileindex, ie);
2135 break;
2136 case GOT_STATUS_DELETE:
2137 case GOT_STATUS_MODIFY:
2138 case GOT_STATUS_CONFLICT:
2139 case GOT_STATUS_MISSING:
2140 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2141 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2142 if (err)
2143 goto done;
2144 err = install_blob(worktree, ondisk_path, ie->path,
2145 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2146 progress_arg);
2147 if (err)
2148 goto done;
2149 if (status == GOT_STATUS_DELETE) {
2150 err = update_blob_fileindex_entry(worktree,
2151 fileindex, ie, ondisk_path, ie->path, blob, 1);
2152 if (err)
2153 goto done;
2155 break;
2156 default:
2157 goto done;
2160 err = got_opentemp_named(&new_fileindex_path, &new_index,
2161 fileindex_path);
2162 if (err)
2163 goto done;
2165 err = got_fileindex_write(fileindex, new_index);
2166 if (err)
2167 goto done;
2169 if (rename(new_fileindex_path, fileindex_path) != 0) {
2170 err = got_error_from_errno();
2171 goto done;
2174 free(new_fileindex_path);
2175 new_fileindex_path = NULL;
2176 done:
2177 free(relpath);
2178 free(tree_path);
2179 if (blob)
2180 got_object_blob_close(blob);
2181 if (tree)
2182 got_object_tree_close(tree);
2183 free(tree_id);
2184 if (index) {
2185 if (fclose(index) != 0 && err == NULL)
2186 err = got_error_from_errno();
2188 if (new_fileindex_path) {
2189 if (unlink(new_fileindex_path) != 0 && err == NULL)
2190 err = got_error_from_errno();
2191 free(new_fileindex_path);
2193 if (fileindex)
2194 got_fileindex_free(fileindex);
2195 unlockerr = lock_worktree(worktree, LOCK_SH);
2196 if (unlockerr && err == NULL)
2197 err = unlockerr;
2198 return err;
2201 struct committable {
2202 unsigned char status;
2203 struct got_object_id *id;
2206 static void
2207 free_committable(struct committable *ct)
2209 free(ct->id);
2210 free(ct);
2213 static const struct got_error *
2214 collect_committables(void *arg, unsigned char status, const char *path,
2215 struct got_object_id *id)
2217 struct got_pathlist_head *paths = arg;
2218 const struct got_error *err = NULL;
2219 char *new_path = NULL;
2220 struct committable *ct = NULL;
2221 struct got_pathlist_entry *new = NULL;
2223 if (status == GOT_STATUS_CONFLICT)
2224 return got_error(GOT_ERR_COMMIT_CONFLICT);
2226 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2227 status != GOT_STATUS_DELETE)
2228 return NULL;
2230 new_path = strdup(path);
2231 if (new_path == NULL)
2232 return got_error_from_errno();
2234 ct = malloc(sizeof(*ct));
2235 if (ct == NULL) {
2236 err = got_error_from_errno();
2237 goto done;
2240 ct->status = status;
2241 ct->id = NULL;
2242 err = got_pathlist_insert(&new, paths, new_path, ct);
2243 done:
2244 if (err || new == NULL) {
2245 free(new_path);
2246 free_committable(ct);
2248 return err;
2251 const struct got_error *
2252 got_worktree_commit(struct got_object_id **new_commit_id,
2253 struct got_worktree *worktree, const char *ondisk_path,
2254 const char *logmsg, struct got_repository *repo)
2256 const struct got_error *err = NULL, *unlockerr = NULL;
2257 struct got_pathlist_head paths;
2258 struct got_pathlist_entry *pe;
2259 char *relpath = NULL;
2260 struct got_commit_object *base_commit = NULL;
2261 struct got_tree_object *base_tree = NULL;
2263 *new_commit_id = NULL;
2265 TAILQ_INIT(&paths);
2267 if (ondisk_path) {
2268 err = got_path_skip_common_ancestor(&relpath,
2269 worktree->root_path, ondisk_path);
2270 if (err)
2271 return err;
2274 err = lock_worktree(worktree, LOCK_EX);
2275 if (err)
2276 goto done;
2278 err = got_worktree_status(worktree, relpath ? relpath : "",
2279 repo, collect_committables, &paths, NULL, NULL);
2280 if (err)
2281 goto done;
2283 /* TODO: collect commit message if not specified */
2285 /* Create blobs from added and modified files and record their IDs. */
2286 TAILQ_FOREACH(pe, &paths, entry) {
2287 struct committable *ct = pe->data;
2288 char *ondisk_path;
2290 if (ct->status != GOT_STATUS_ADD &&
2291 ct->status != GOT_STATUS_MODIFY)
2292 continue;
2294 if (asprintf(&ondisk_path, "%s/%s",
2295 worktree->root_path, pe->path) == -1) {
2296 err = got_error_from_errno();
2297 goto done;
2299 err = got_object_blob_create(&ct->id, pe->path, repo);
2300 if (err)
2301 goto done;
2304 err = got_object_open_as_commit(&base_commit, repo,
2305 worktree->base_commit_id);
2306 if (err)
2307 goto done;
2308 err = got_object_open_as_tree(&base_tree, repo,
2309 base_commit->tree_id);
2310 if (err)
2311 goto done;
2313 /* TODO: walk base tree and patch it to create a new tree */
2314 printf("committables:\n");
2315 TAILQ_FOREACH(pe, &paths, entry) {
2316 unsigned char *status = pe->data;
2317 printf("%c %s\n", *status, pe->path);
2319 done:
2320 unlockerr = lock_worktree(worktree, LOCK_SH);
2321 if (unlockerr && err == NULL)
2322 err = unlockerr;
2323 TAILQ_FOREACH(pe, &paths, entry) {
2324 struct committable *ct = pe->data;
2325 free_committable(ct);
2327 got_pathlist_free(&paths);
2328 got_object_tree_close(base_tree);
2329 free(relpath);
2330 return err;