Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
34 #include <uuid.h>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
45 #include "got_lib_worktree.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("asprintf");
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_errno2("open", path);
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("dprintf");
84 goto done;
85 }
86 }
88 done:
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno("close");
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("asprintf");
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_errno2("fprintf", tmppath);
117 goto done;
121 if (rename(tmppath, path) != 0) {
122 err = got_error_from_errno3("rename", tmppath, path);
123 unlink(tmppath);
124 goto done;
127 done:
128 if (fclose(tmpfile) != 0 && err == NULL)
129 err = got_error_from_errno2("fclose", tmppath);
130 free(tmppath);
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("asprintf");
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_errno2("open", path);
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_errno2("flock", path));
162 goto done;
165 if (lstat(path, &sb) != 0) {
166 err = got_error_from_errno2("lstat", path);
167 goto done;
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_from_errno("calloc");
172 goto done;
175 n = read(fd, *content, sb.st_size);
176 if (n != sb.st_size) {
177 err = (n == -1 ? got_error_from_errno2("read", path) :
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_errno2("close", path_got);
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("asprintf");
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno2("mkdir", path);
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("asprintf");
242 goto done;
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno2("mkdir", path_got);
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("got_ref_to_str");
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("asprintf");
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("asprintf");
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("asprintf");
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_errno2("open", path_lock));
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("calloc");
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("strdup");
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_errno2("dirname", path);
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_errno2("close",
471 got_worktree_get_root_path(worktree));
472 free(worktree);
473 return err;
476 const char *
477 got_worktree_get_root_path(struct got_worktree *worktree)
479 return worktree->root_path;
482 const char *
483 got_worktree_get_repo_path(struct got_worktree *worktree)
485 return worktree->repo_path;
488 const char *
489 got_worktree_get_path_prefix(struct got_worktree *worktree)
491 return worktree->path_prefix;
494 const struct got_error *
495 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
496 const char *path_prefix)
498 char *absprefix = NULL;
500 if (!got_path_is_absolute(path_prefix)) {
501 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
502 return got_error_from_errno("asprintf");
504 *match = (strcmp(absprefix ? absprefix : path_prefix,
505 worktree->path_prefix) == 0);
506 free(absprefix);
507 return NULL;
510 const char *
511 got_worktree_get_head_ref_name(struct got_worktree *worktree)
513 return worktree->head_ref_name;
516 struct got_object_id *
517 got_worktree_get_base_commit_id(struct got_worktree *worktree)
519 return worktree->base_commit_id;
522 const struct got_error *
523 got_worktree_set_base_commit_id(struct got_worktree *worktree,
524 struct got_repository *repo, struct got_object_id *commit_id)
526 const struct got_error *err;
527 struct got_object *obj = NULL;
528 char *id_str = NULL;
529 char *path_got = NULL;
531 if (asprintf(&path_got, "%s/%s", worktree->root_path,
532 GOT_WORKTREE_GOT_DIR) == -1) {
533 err = got_error_from_errno("asprintf");
534 path_got = NULL;
535 goto done;
538 err = got_object_open(&obj, repo, commit_id);
539 if (err)
540 return err;
542 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
543 err = got_error(GOT_ERR_OBJ_TYPE);
544 goto done;
547 /* Record our base commit. */
548 err = got_object_id_str(&id_str, commit_id);
549 if (err)
550 goto done;
551 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
552 if (err)
553 goto done;
555 free(worktree->base_commit_id);
556 worktree->base_commit_id = got_object_id_dup(commit_id);
557 if (worktree->base_commit_id == NULL) {
558 err = got_error_from_errno("got_object_id_dup");
559 goto done;
561 done:
562 if (obj)
563 got_object_close(obj);
564 free(id_str);
565 free(path_got);
566 return err;
569 static const struct got_error *
570 lock_worktree(struct got_worktree *worktree, int operation)
572 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
573 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
574 : got_error_from_errno2("flock",
575 got_worktree_get_root_path(worktree)));
576 return NULL;
579 static const struct got_error *
580 add_dir_on_disk(struct got_worktree *worktree, const char *path)
582 const struct got_error *err = NULL;
583 char *abspath;
585 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
586 return got_error_from_errno("asprintf");
588 err = got_path_mkdir(abspath);
589 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
590 struct stat sb;
591 err = NULL;
592 if (lstat(abspath, &sb) == -1) {
593 err = got_error_from_errno2("lstat", abspath);
594 } else if (!S_ISDIR(sb.st_mode)) {
595 /* TODO directory is obstructed; do something */
596 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
599 free(abspath);
600 return err;
603 static const struct got_error *
604 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
606 const struct got_error *err = NULL;
607 uint8_t fbuf1[8192];
608 uint8_t fbuf2[8192];
609 size_t flen1 = 0, flen2 = 0;
611 *same = 1;
613 for (;;) {
614 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
615 if (flen1 == 0 && ferror(f1)) {
616 err = got_error_from_errno("fread");
617 break;
619 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
620 if (flen2 == 0 && ferror(f2)) {
621 err = got_error_from_errno("fread");
622 break;
624 if (flen1 == 0) {
625 if (flen2 != 0)
626 *same = 0;
627 break;
628 } else if (flen2 == 0) {
629 if (flen1 != 0)
630 *same = 0;
631 break;
632 } else if (flen1 == flen2) {
633 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
634 *same = 0;
635 break;
637 } else {
638 *same = 0;
639 break;
643 return err;
646 static const struct got_error *
647 check_files_equal(int *same, const char *f1_path, const char *f2_path)
649 const struct got_error *err = NULL;
650 struct stat sb;
651 size_t size1, size2;
652 FILE *f1 = NULL, *f2 = NULL;
654 *same = 1;
656 if (lstat(f1_path, &sb) != 0) {
657 err = got_error_from_errno2("lstat", f1_path);
658 goto done;
660 size1 = sb.st_size;
662 if (lstat(f2_path, &sb) != 0) {
663 err = got_error_from_errno2("lstat", f2_path);
664 goto done;
666 size2 = sb.st_size;
668 if (size1 != size2) {
669 *same = 0;
670 return NULL;
673 f1 = fopen(f1_path, "r");
674 if (f1 == NULL)
675 return got_error_from_errno2("open", f1_path);
677 f2 = fopen(f2_path, "r");
678 if (f2 == NULL) {
679 err = got_error_from_errno2("open", f2_path);
680 goto done;
683 err = check_file_contents_equal(same, f1, f2);
684 done:
685 if (f1 && fclose(f1) != 0 && err == NULL)
686 err = got_error_from_errno("fclose");
687 if (f2 && fclose(f2) != 0 && err == NULL)
688 err = got_error_from_errno("fclose");
690 return err;
693 /*
694 * Perform a 3-way merge where the file's version in the file index (blob2)
695 * acts as the common ancestor, the incoming blob (blob1) acts as the first
696 * derived version, and the file on disk acts as the second derived version.
697 */
698 static const struct got_error *
699 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
700 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
701 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
702 struct got_repository *repo,
703 got_worktree_checkout_cb progress_cb, void *progress_arg)
705 const struct got_error *err = NULL;
706 int merged_fd = -1;
707 struct got_blob_object *blob2 = NULL;
708 FILE *f1 = NULL, *f2 = NULL;
709 char *blob1_path = NULL, *blob2_path = NULL;
710 char *merged_path = NULL, *base_path = NULL;
711 char *id_str = NULL;
712 char *label1 = NULL;
713 int overlapcnt = 0, update_timestamps = 0;
714 char *parent;
716 parent = dirname(ondisk_path);
717 if (parent == NULL)
718 return got_error_from_errno2("dirname", ondisk_path);
720 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
721 return got_error_from_errno("asprintf");
723 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
724 if (err)
725 goto done;
727 free(base_path);
728 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
729 err = got_error_from_errno("asprintf");
730 base_path = NULL;
731 goto done;
734 err = got_opentemp_named(&blob1_path, &f1, base_path);
735 if (err)
736 goto done;
737 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
738 if (err)
739 goto done;
741 free(base_path);
742 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
743 err = got_error_from_errno("asprintf");
744 base_path = NULL;
745 goto done;
748 err = got_opentemp_named(&blob2_path, &f2, base_path);
749 if (err)
750 goto done;
751 if (got_fileindex_entry_has_blob(ie)) {
752 struct got_object_id id2;
753 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
754 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
755 if (err)
756 goto done;
757 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
758 if (err)
759 goto done;
760 } else {
761 /*
762 * If the file has no blob, this is an "add vs add" conflict,
763 * and we simply use an empty ancestor file to make both files
764 * appear in the merged result in their entirety.
765 */
768 err = got_object_id_str(&id_str, worktree->base_commit_id);
769 if (err)
770 goto done;
771 if (asprintf(&label1, "commit %s", id_str) == -1) {
772 err = got_error_from_errno("asprintf");
773 goto done;
776 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
777 blob2_path, ondisk_path, label1, path);
778 if (err)
779 goto done;
781 (*progress_cb)(progress_arg,
782 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
784 if (fsync(merged_fd) != 0) {
785 err = got_error_from_errno("fsync");
786 goto done;
789 /* Check if a clean merge has subsumed all local changes. */
790 if (overlapcnt == 0) {
791 err = check_files_equal(&update_timestamps, blob1_path,
792 merged_path);
793 if (err)
794 goto done;
797 if (chmod(merged_path, st_mode) != 0) {
798 err = got_error_from_errno2("chmod", merged_path);
799 goto done;
802 if (rename(merged_path, ondisk_path) != 0) {
803 err = got_error_from_errno3("rename", merged_path,
804 ondisk_path);
805 unlink(merged_path);
806 goto done;
809 /*
810 * Do not update timestamps of already modified files. Otherwise,
811 * a future status walk would treat them as unmodified files again.
812 */
813 err = got_fileindex_entry_update(ie, ondisk_path,
814 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
815 done:
816 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
817 err = got_error_from_errno("close");
818 if (f1 && fclose(f1) != 0 && err == NULL)
819 err = got_error_from_errno("fclose");
820 if (f2 && fclose(f2) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 if (blob2)
823 got_object_blob_close(blob2);
824 free(merged_path);
825 free(base_path);
826 if (blob1_path) {
827 unlink(blob1_path);
828 free(blob1_path);
830 if (blob2_path) {
831 unlink(blob2_path);
832 free(blob2_path);
834 free(id_str);
835 free(label1);
836 return err;
839 static const struct got_error *
840 update_blob_fileindex_entry(struct got_worktree *worktree,
841 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
842 const char *ondisk_path, const char *path, struct got_blob_object *blob,
843 int update_timestamps)
845 const struct got_error *err = NULL;
847 if (ie == NULL)
848 ie = got_fileindex_entry_get(fileindex, path);
849 if (ie)
850 err = got_fileindex_entry_update(ie, ondisk_path,
851 blob->id.sha1, worktree->base_commit_id->sha1,
852 update_timestamps);
853 else {
854 struct got_fileindex_entry *new_ie;
855 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
856 path, blob->id.sha1, worktree->base_commit_id->sha1);
857 if (!err)
858 err = got_fileindex_entry_add(fileindex, new_ie);
860 return err;
863 static const struct got_error *
864 install_blob(struct got_worktree *worktree, const char *ondisk_path,
865 const char *path, uint16_t te_mode, uint16_t st_mode,
866 struct got_blob_object *blob, int restoring_missing_file,
867 int reverting_versioned_file, struct got_repository *repo,
868 got_worktree_checkout_cb progress_cb, void *progress_arg)
870 const struct got_error *err = NULL;
871 int fd = -1;
872 size_t len, hdrlen;
873 int update = 0;
874 char *tmppath = NULL;
876 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
877 GOT_DEFAULT_FILE_MODE);
878 if (fd == -1) {
879 if (errno == ENOENT) {
880 char *parent = dirname(path);
881 if (parent == NULL)
882 return got_error_from_errno2("dirname", path);
883 err = add_dir_on_disk(worktree, parent);
884 if (err)
885 return err;
886 fd = open(ondisk_path,
887 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
888 GOT_DEFAULT_FILE_MODE);
889 if (fd == -1)
890 return got_error_from_errno2("open",
891 ondisk_path);
892 } else if (errno == EEXIST) {
893 if (!S_ISREG(st_mode)) {
894 /* TODO file is obstructed; do something */
895 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
896 goto done;
897 } else {
898 err = got_opentemp_named_fd(&tmppath, &fd,
899 ondisk_path);
900 if (err)
901 goto done;
902 update = 1;
904 } else
905 return got_error_from_errno2("open", ondisk_path);
908 if (restoring_missing_file)
909 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
910 else if (reverting_versioned_file)
911 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
912 else
913 (*progress_cb)(progress_arg,
914 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
916 hdrlen = got_object_blob_get_hdrlen(blob);
917 do {
918 const uint8_t *buf = got_object_blob_get_read_buf(blob);
919 err = got_object_blob_read_block(&len, blob);
920 if (err)
921 break;
922 if (len > 0) {
923 /* Skip blob object header first time around. */
924 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
925 if (outlen == -1) {
926 err = got_error_from_errno("write");
927 goto done;
928 } else if (outlen != len - hdrlen) {
929 err = got_error(GOT_ERR_IO);
930 goto done;
932 hdrlen = 0;
934 } while (len != 0);
936 if (fsync(fd) != 0) {
937 err = got_error_from_errno("fsync");
938 goto done;
941 if (update) {
942 if (rename(tmppath, ondisk_path) != 0) {
943 err = got_error_from_errno3("rename", tmppath,
944 ondisk_path);
945 unlink(tmppath);
946 goto done;
950 if (te_mode & S_IXUSR) {
951 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
952 err = got_error_from_errno2("chmod", ondisk_path);
953 goto done;
955 } else {
956 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
957 err = got_error_from_errno2("chmod", ondisk_path);
958 goto done;
962 done:
963 if (fd != -1 && close(fd) != 0 && err == NULL)
964 err = got_error_from_errno("close");
965 free(tmppath);
966 return err;
969 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
970 static const struct got_error *
971 get_modified_file_content_status(unsigned char *status, FILE *f)
973 const struct got_error *err = NULL;
974 const char *markers[3] = {
975 GOT_DIFF_CONFLICT_MARKER_BEGIN,
976 GOT_DIFF_CONFLICT_MARKER_SEP,
977 GOT_DIFF_CONFLICT_MARKER_END
978 };
979 int i = 0;
980 char *line;
981 size_t len;
982 const char delim[3] = {'\0', '\0', '\0'};
984 while (*status == GOT_STATUS_MODIFY) {
985 line = fparseln(f, &len, NULL, delim, 0);
986 if (line == NULL) {
987 if (feof(f))
988 break;
989 err = got_ferror(f, GOT_ERR_IO);
990 break;
993 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
994 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
995 == 0)
996 *status = GOT_STATUS_CONFLICT;
997 else
998 i++;
1002 return err;
1005 static const struct got_error *
1006 get_file_status(unsigned char *status, struct stat *sb,
1007 struct got_fileindex_entry *ie, const char *abspath,
1008 struct got_repository *repo)
1010 const struct got_error *err = NULL;
1011 struct got_object_id id;
1012 size_t hdrlen;
1013 FILE *f = NULL;
1014 uint8_t fbuf[8192];
1015 struct got_blob_object *blob = NULL;
1016 size_t flen, blen;
1018 *status = GOT_STATUS_NO_CHANGE;
1020 if (lstat(abspath, sb) == -1) {
1021 if (errno == ENOENT) {
1022 if (ie) {
1023 if (got_fileindex_entry_has_file_on_disk(ie))
1024 *status = GOT_STATUS_MISSING;
1025 else
1026 *status = GOT_STATUS_DELETE;
1027 sb->st_mode =
1028 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1029 & (S_IRWXU | S_IRWXG | S_IRWXO));
1030 } else
1031 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1032 return NULL;
1034 return got_error_from_errno2("lstat", abspath);
1037 if (!S_ISREG(sb->st_mode)) {
1038 *status = GOT_STATUS_OBSTRUCTED;
1039 return NULL;
1042 if (ie == NULL)
1043 return NULL;
1045 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1046 *status = GOT_STATUS_DELETE;
1047 return NULL;
1048 } else if (!got_fileindex_entry_has_blob(ie)) {
1049 *status = GOT_STATUS_ADD;
1050 return NULL;
1053 if (ie->ctime_sec == sb->st_ctime &&
1054 ie->ctime_nsec == sb->st_ctimensec &&
1055 ie->mtime_sec == sb->st_mtime &&
1056 ie->mtime_sec == sb->st_mtime &&
1057 ie->mtime_nsec == sb->st_mtimensec &&
1058 ie->size == (sb->st_size & 0xffffffff))
1059 return NULL;
1061 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1062 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1063 if (err)
1064 return err;
1066 f = fopen(abspath, "r");
1067 if (f == NULL) {
1068 err = got_error_from_errno2("fopen", abspath);
1069 goto done;
1071 hdrlen = got_object_blob_get_hdrlen(blob);
1072 for (;;) {
1073 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1074 err = got_object_blob_read_block(&blen, blob);
1075 if (err)
1076 goto done;
1077 /* Skip length of blob object header first time around. */
1078 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1079 if (flen == 0 && ferror(f)) {
1080 err = got_error_from_errno("fread");
1081 goto done;
1083 if (blen == 0) {
1084 if (flen != 0)
1085 *status = GOT_STATUS_MODIFY;
1086 break;
1087 } else if (flen == 0) {
1088 if (blen != 0)
1089 *status = GOT_STATUS_MODIFY;
1090 break;
1091 } else if (blen - hdrlen == flen) {
1092 /* Skip blob object header first time around. */
1093 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1094 *status = GOT_STATUS_MODIFY;
1095 break;
1097 } else {
1098 *status = GOT_STATUS_MODIFY;
1099 break;
1101 hdrlen = 0;
1104 if (*status == GOT_STATUS_MODIFY) {
1105 rewind(f);
1106 err = get_modified_file_content_status(status, f);
1108 done:
1109 if (blob)
1110 got_object_blob_close(blob);
1111 if (f)
1112 fclose(f);
1113 return err;
1116 static const struct got_error *
1117 update_blob(struct got_worktree *worktree,
1118 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1119 struct got_tree_entry *te, const char *path,
1120 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1121 void *progress_arg)
1123 const struct got_error *err = NULL;
1124 struct got_blob_object *blob = NULL;
1125 char *ondisk_path;
1126 unsigned char status = GOT_STATUS_NO_CHANGE;
1127 struct stat sb;
1129 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1130 return got_error_from_errno("asprintf");
1132 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1133 if (err)
1134 goto done;
1136 if (status == GOT_STATUS_OBSTRUCTED) {
1137 (*progress_cb)(progress_arg, status, path);
1138 goto done;
1141 if (ie && status != GOT_STATUS_MISSING) {
1142 if (got_fileindex_entry_has_commit(ie) &&
1143 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1144 SHA1_DIGEST_LENGTH) == 0) {
1145 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1146 path);
1147 goto done;
1149 if (got_fileindex_entry_has_blob(ie) &&
1150 memcmp(ie->blob_sha1, te->id->sha1,
1151 SHA1_DIGEST_LENGTH) == 0)
1152 goto done;
1155 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1156 if (err)
1157 goto done;
1159 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1160 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1161 te->mode, sb.st_mode, blob, repo, progress_cb,
1162 progress_arg);
1163 else if (status == GOT_STATUS_DELETE) {
1164 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1165 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1166 ondisk_path, path, blob, 0);
1167 if (err)
1168 goto done;
1169 } else {
1170 err = install_blob(worktree, ondisk_path, path, te->mode,
1171 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1172 repo, progress_cb, progress_arg);
1173 if (err)
1174 goto done;
1175 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1176 ondisk_path, path, blob, 1);
1177 if (err)
1178 goto done;
1180 got_object_blob_close(blob);
1181 done:
1182 free(ondisk_path);
1183 return err;
1186 static const struct got_error *
1187 remove_ondisk_file(const char *root_path, const char *path)
1189 const struct got_error *err = NULL;
1190 char *ondisk_path = NULL;
1192 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1193 return got_error_from_errno("asprintf");
1195 if (unlink(ondisk_path) == -1) {
1196 if (errno != ENOENT)
1197 err = got_error_from_errno2("unlink", ondisk_path);
1198 } else {
1199 char *parent = dirname(ondisk_path);
1200 while (parent && strcmp(parent, root_path) != 0) {
1201 if (rmdir(parent) == -1) {
1202 if (errno != ENOTEMPTY)
1203 err = got_error_from_errno2("rmdir",
1204 parent);
1205 break;
1207 parent = dirname(parent);
1210 free(ondisk_path);
1211 return err;
1214 static const struct got_error *
1215 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1216 struct got_fileindex_entry *ie, const char *parent_path,
1217 struct got_repository *repo,
1218 got_worktree_checkout_cb progress_cb, void *progress_arg)
1220 const struct got_error *err = NULL;
1221 unsigned char status;
1222 struct stat sb;
1223 char *ondisk_path;
1225 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1226 == -1)
1227 return got_error_from_errno("asprintf");
1229 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1230 if (err)
1231 return err;
1233 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1234 status == GOT_STATUS_ADD) {
1235 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1237 * Preserve the working file and change the deleted blob's
1238 * entry into a schedule-add entry.
1240 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1241 0);
1242 if (err)
1243 return err;
1244 } else {
1245 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1246 if (status == GOT_STATUS_NO_CHANGE) {
1247 err = remove_ondisk_file(worktree->root_path, ie->path);
1248 if (err)
1249 return err;
1251 got_fileindex_entry_remove(fileindex, ie);
1254 return err;
1257 struct diff_cb_arg {
1258 struct got_fileindex *fileindex;
1259 struct got_worktree *worktree;
1260 struct got_repository *repo;
1261 got_worktree_checkout_cb progress_cb;
1262 void *progress_arg;
1263 got_worktree_cancel_cb cancel_cb;
1264 void *cancel_arg;
1267 static const struct got_error *
1268 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1269 struct got_tree_entry *te, const char *parent_path)
1271 struct diff_cb_arg *a = arg;
1273 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1274 return got_error(GOT_ERR_CANCELLED);
1276 return update_blob(a->worktree, a->fileindex, ie, te,
1277 ie->path, a->repo, a->progress_cb, a->progress_arg);
1280 static const struct got_error *
1281 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1283 struct diff_cb_arg *a = arg;
1285 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1286 return got_error(GOT_ERR_CANCELLED);
1288 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1289 a->repo, a->progress_cb, a->progress_arg);
1292 static const struct got_error *
1293 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1295 struct diff_cb_arg *a = arg;
1296 const struct got_error *err;
1297 char *path;
1299 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1300 return got_error(GOT_ERR_CANCELLED);
1302 if (asprintf(&path, "%s%s%s", parent_path,
1303 parent_path[0] ? "/" : "", te->name)
1304 == -1)
1305 return got_error_from_errno("asprintf");
1307 if (S_ISDIR(te->mode))
1308 err = add_dir_on_disk(a->worktree, path);
1309 else
1310 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1311 a->repo, a->progress_cb, a->progress_arg);
1313 free(path);
1314 return err;
1317 const struct got_error *
1318 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1320 const struct got_error *err = NULL;
1321 char *uuidstr = NULL;
1322 uint32_t uuid_status;
1324 *refname = NULL;
1326 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1327 if (uuid_status != uuid_s_ok)
1328 return got_error_uuid(uuid_status);
1330 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1331 == -1) {
1332 err = got_error_from_errno("asprintf");
1333 *refname = NULL;
1335 free(uuidstr);
1336 return err;
1340 * Prevent Git's garbage collector from deleting our base commit by
1341 * setting a reference to our base commit's ID.
1343 static const struct got_error *
1344 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1346 const struct got_error *err = NULL;
1347 struct got_reference *ref = NULL;
1348 char *refname;
1350 err = got_worktree_get_base_ref_name(&refname, worktree);
1351 if (err)
1352 return err;
1354 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1355 if (err)
1356 goto done;
1358 err = got_ref_write(ref, repo);
1359 done:
1360 free(refname);
1361 if (ref)
1362 got_ref_close(ref);
1363 return err;
1366 static const struct got_error *
1367 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1368 struct got_worktree *worktree)
1370 const struct got_error *err = NULL;
1371 FILE *index = NULL;
1373 *fileindex_path = NULL;
1374 *fileindex = got_fileindex_alloc();
1375 if (*fileindex == NULL)
1376 return got_error_from_errno("got_fileindex_alloc");
1378 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1379 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1380 err = got_error_from_errno("asprintf");
1381 *fileindex_path = NULL;
1382 goto done;
1385 index = fopen(*fileindex_path, "rb");
1386 if (index == NULL) {
1387 if (errno != ENOENT)
1388 err = got_error_from_errno2("fopen", *fileindex_path);
1389 } else {
1390 err = got_fileindex_read(*fileindex, index);
1391 if (fclose(index) != 0 && err == NULL)
1392 err = got_error_from_errno("fclose");
1394 done:
1395 if (err) {
1396 free(*fileindex_path);
1397 *fileindex_path = NULL;
1398 free(*fileindex);
1399 *fileindex = NULL;
1401 return err;
1404 const struct got_error *
1405 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1406 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1407 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1409 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1410 struct got_commit_object *commit = NULL;
1411 struct got_object_id *tree_id = NULL;
1412 struct got_tree_object *tree = NULL;
1413 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1414 struct got_fileindex *fileindex = NULL;
1415 FILE *new_index = NULL;
1416 struct got_fileindex_diff_tree_cb diff_cb;
1417 struct diff_cb_arg arg;
1418 char *relpath = NULL, *entry_name = NULL;
1420 err = lock_worktree(worktree, LOCK_EX);
1421 if (err)
1422 return err;
1425 * Read the file index.
1426 * Checking out files is supposed to be an idempotent operation.
1427 * If the on-disk file index is incomplete we will try to complete it.
1429 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1430 if (err)
1431 goto done;
1433 err = got_opentemp_named(&new_fileindex_path, &new_index,
1434 fileindex_path);
1435 if (err)
1436 goto done;
1438 err = ref_base_commit(worktree, repo);
1439 if (err)
1440 goto done;
1442 err = got_object_open_as_commit(&commit, repo,
1443 worktree->base_commit_id);
1444 if (err)
1445 goto done;
1447 if (path[0]) {
1448 char *tree_path;
1449 int obj_type;
1450 relpath = strdup(path);
1451 if (relpath == NULL) {
1452 err = got_error_from_errno("strdup");
1453 goto done;
1455 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1456 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1457 path) == -1) {
1458 err = got_error_from_errno("asprintf");
1459 goto done;
1461 err = got_object_id_by_path(&tree_id, repo,
1462 worktree->base_commit_id, tree_path);
1463 free(tree_path);
1464 if (err)
1465 goto done;
1466 err = got_object_get_type(&obj_type, repo, tree_id);
1467 if (err)
1468 goto done;
1469 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1470 /* Split provided path into parent dir + entry name. */
1471 if (strchr(path, '/') == NULL) {
1472 relpath = strdup("");
1473 if (relpath == NULL) {
1474 err = got_error_from_errno("strdup");
1475 goto done;
1477 tree_path = strdup(worktree->path_prefix);
1478 if (tree_path == NULL) {
1479 err = got_error_from_errno("strdup");
1480 goto done;
1482 } else {
1483 err = got_path_dirname(&relpath, path);
1484 if (err)
1485 goto done;
1486 if (asprintf(&tree_path, "%s%s%s",
1487 worktree->path_prefix,
1488 got_path_is_root_dir(
1489 worktree->path_prefix) ? "" : "/",
1490 relpath) == -1) {
1491 err = got_error_from_errno("asprintf");
1492 goto done;
1495 err = got_object_id_by_path(&tree_id, repo,
1496 worktree->base_commit_id, tree_path);
1497 free(tree_path);
1498 if (err)
1499 goto done;
1500 entry_name = basename(path);
1501 if (entry_name == NULL) {
1502 err = got_error_from_errno2("basename", path);
1503 goto done;
1506 } else {
1507 relpath = strdup("");
1508 if (relpath == NULL) {
1509 err = got_error_from_errno("strdup");
1510 goto done;
1512 err = got_object_id_by_path(&tree_id, repo,
1513 worktree->base_commit_id, worktree->path_prefix);
1514 if (err)
1515 goto done;
1518 err = got_object_open_as_tree(&tree, repo, tree_id);
1519 if (err)
1520 goto done;
1522 if (entry_name &&
1523 got_object_tree_find_entry(tree, entry_name) == NULL) {
1524 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1525 goto done;
1528 diff_cb.diff_old_new = diff_old_new;
1529 diff_cb.diff_old = diff_old;
1530 diff_cb.diff_new = diff_new;
1531 arg.fileindex = fileindex;
1532 arg.worktree = worktree;
1533 arg.repo = repo;
1534 arg.progress_cb = progress_cb;
1535 arg.progress_arg = progress_arg;
1536 arg.cancel_cb = cancel_cb;
1537 arg.cancel_arg = cancel_arg;
1538 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1539 entry_name, repo, &diff_cb, &arg);
1541 /* Try to sync the fileindex back to disk in any case. */
1542 err = got_fileindex_write(fileindex, new_index);
1543 if (err)
1544 goto done;
1546 if (rename(new_fileindex_path, fileindex_path) != 0) {
1547 err = got_error_from_errno3("rename", new_fileindex_path,
1548 fileindex_path);
1549 unlink(new_fileindex_path);
1550 goto done;
1553 free(new_fileindex_path);
1554 new_fileindex_path = NULL;
1556 done:
1557 free(relpath);
1558 if (tree)
1559 got_object_tree_close(tree);
1560 if (commit)
1561 got_object_commit_close(commit);
1562 if (new_fileindex_path)
1563 unlink(new_fileindex_path);
1564 if (new_index)
1565 fclose(new_index);
1566 free(new_fileindex_path);
1567 free(fileindex_path);
1568 got_fileindex_free(fileindex);
1569 if (checkout_err)
1570 err = checkout_err;
1571 unlockerr = lock_worktree(worktree, LOCK_SH);
1572 if (unlockerr && err == NULL)
1573 err = unlockerr;
1574 return err;
1577 struct diff_dir_cb_arg {
1578 struct got_fileindex *fileindex;
1579 struct got_worktree *worktree;
1580 const char *status_path;
1581 size_t status_path_len;
1582 struct got_repository *repo;
1583 got_worktree_status_cb status_cb;
1584 void *status_arg;
1585 got_worktree_cancel_cb cancel_cb;
1586 void *cancel_arg;
1589 static const struct got_error *
1590 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1591 got_worktree_status_cb status_cb, void *status_arg,
1592 struct got_repository *repo)
1594 const struct got_error *err = NULL;
1595 unsigned char status = GOT_STATUS_NO_CHANGE;
1596 struct stat sb;
1597 struct got_object_id id;
1599 err = get_file_status(&status, &sb, ie, abspath, repo);
1600 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1601 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1602 err = (*status_cb)(status_arg, status, ie->path, &id);
1604 return err;
1607 static const struct got_error *
1608 status_old_new(void *arg, struct got_fileindex_entry *ie,
1609 struct dirent *de, const char *parent_path)
1611 const struct got_error *err = NULL;
1612 struct diff_dir_cb_arg *a = arg;
1613 char *abspath;
1615 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1616 return got_error(GOT_ERR_CANCELLED);
1618 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1619 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1620 return NULL;
1622 if (parent_path[0]) {
1623 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1624 parent_path, de->d_name) == -1)
1625 return got_error_from_errno("asprintf");
1626 } else {
1627 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1628 de->d_name) == -1)
1629 return got_error_from_errno("asprintf");
1632 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1633 a->repo);
1634 free(abspath);
1635 return err;
1638 static const struct got_error *
1639 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1641 struct diff_dir_cb_arg *a = arg;
1642 struct got_object_id id;
1643 unsigned char status;
1645 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1646 return got_error(GOT_ERR_CANCELLED);
1648 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1649 return NULL;
1651 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1652 if (got_fileindex_entry_has_file_on_disk(ie))
1653 status = GOT_STATUS_MISSING;
1654 else
1655 status = GOT_STATUS_DELETE;
1656 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1659 static const struct got_error *
1660 status_new(void *arg, struct dirent *de, const char *parent_path)
1662 const struct got_error *err = NULL;
1663 struct diff_dir_cb_arg *a = arg;
1664 char *path = NULL;
1666 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1667 return got_error(GOT_ERR_CANCELLED);
1669 if (de->d_type == DT_DIR)
1670 return NULL;
1672 /* XXX ignore symlinks for now */
1673 if (de->d_type == DT_LNK)
1674 return NULL;
1676 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1677 return NULL;
1679 if (parent_path[0]) {
1680 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1681 return got_error_from_errno("asprintf");
1682 } else {
1683 path = de->d_name;
1686 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1687 NULL);
1688 if (parent_path[0])
1689 free(path);
1690 return err;
1693 const struct got_error *
1694 got_worktree_status(struct got_worktree *worktree, const char *path,
1695 struct got_repository *repo, got_worktree_status_cb status_cb,
1696 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1698 const struct got_error *err = NULL;
1699 DIR *workdir = NULL;
1700 char *fileindex_path = NULL;
1701 struct got_fileindex *fileindex = NULL;
1702 FILE *index = NULL;
1703 struct got_fileindex_diff_dir_cb fdiff_cb;
1704 struct diff_dir_cb_arg arg;
1705 char *ondisk_path = NULL;
1707 fileindex = got_fileindex_alloc();
1708 if (fileindex == NULL) {
1709 err = got_error_from_errno("got_fileindex_alloc");
1710 goto done;
1713 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1714 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1715 err = got_error_from_errno("asprintf");
1716 fileindex_path = NULL;
1717 goto done;
1720 index = fopen(fileindex_path, "rb");
1721 if (index == NULL) {
1722 if (errno != ENOENT) {
1723 err = got_error_from_errno2("fopen", fileindex_path);
1724 goto done;
1726 } else {
1727 err = got_fileindex_read(fileindex, index);
1728 fclose(index);
1729 if (err)
1730 goto done;
1733 if (asprintf(&ondisk_path, "%s%s%s",
1734 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1735 err = got_error_from_errno("asprintf");
1736 goto done;
1738 workdir = opendir(ondisk_path);
1739 if (workdir == NULL) {
1740 if (errno == ENOTDIR || errno == ENOENT) {
1741 struct got_fileindex_entry *ie;
1742 ie = got_fileindex_entry_get(fileindex, path);
1743 if (ie == NULL) {
1744 err = got_error(GOT_ERR_BAD_PATH);
1745 goto done;
1747 err = report_file_status(ie, ondisk_path,
1748 status_cb, status_arg, repo);
1749 goto done;
1750 } else {
1751 err = got_error_from_errno2("opendir", ondisk_path);
1752 goto done;
1755 fdiff_cb.diff_old_new = status_old_new;
1756 fdiff_cb.diff_old = status_old;
1757 fdiff_cb.diff_new = status_new;
1758 arg.fileindex = fileindex;
1759 arg.worktree = worktree;
1760 arg.status_path = path;
1761 arg.status_path_len = strlen(path);
1762 arg.repo = repo;
1763 arg.status_cb = status_cb;
1764 arg.status_arg = status_arg;
1765 arg.cancel_cb = cancel_cb;
1766 arg.cancel_arg = cancel_arg;
1767 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1768 path, repo, &fdiff_cb, &arg);
1769 done:
1770 if (workdir)
1771 closedir(workdir);
1772 free(ondisk_path);
1773 free(fileindex_path);
1774 got_fileindex_free(fileindex);
1775 return err;
1778 const struct got_error *
1779 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1780 const char *arg)
1782 const struct got_error *err = NULL;
1783 char *resolved, *path = NULL;
1784 size_t len;
1786 *wt_path = NULL;
1788 resolved = realpath(arg, NULL);
1789 if (resolved == NULL)
1790 return got_error_from_errno2("realpath", arg);
1792 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1793 strlen(got_worktree_get_root_path(worktree)))) {
1794 err = got_error(GOT_ERR_BAD_PATH);
1795 goto done;
1798 path = strdup(resolved +
1799 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1800 if (path == NULL) {
1801 err = got_error_from_errno("strdup");
1802 goto done;
1805 /* XXX status walk can't deal with trailing slash! */
1806 len = strlen(path);
1807 while (path[len - 1] == '/') {
1808 path[len - 1] = '\0';
1809 len--;
1811 done:
1812 free(resolved);
1813 if (err == NULL)
1814 *wt_path = path;
1815 else
1816 free(path);
1817 return err;
1820 const struct got_error *
1821 got_worktree_schedule_add(struct got_worktree *worktree,
1822 struct got_pathlist_head *ondisk_paths,
1823 got_worktree_status_cb status_cb, void *status_arg,
1824 struct got_repository *repo)
1826 struct got_fileindex *fileindex = NULL;
1827 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1828 FILE *index = NULL, *new_index = NULL;
1829 const struct got_error *err = NULL, *unlockerr = NULL;
1830 struct got_pathlist_entry *pe;
1832 err = lock_worktree(worktree, LOCK_EX);
1833 if (err)
1834 return err;
1837 fileindex = got_fileindex_alloc();
1838 if (fileindex == NULL) {
1839 err = got_error_from_errno("got_fileindex_alloc");
1840 goto done;
1843 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1844 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1845 err = got_error_from_errno("asprintf");
1846 fileindex_path = NULL;
1847 goto done;
1850 index = fopen(fileindex_path, "rb");
1851 if (index == NULL) {
1852 err = got_error_from_errno2("fopen", fileindex_path);
1853 goto done;
1856 err = got_fileindex_read(fileindex, index);
1857 if (err)
1858 goto done;
1860 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1861 struct got_fileindex_entry *ie = NULL;
1862 char *relpath;
1864 err = got_path_skip_common_ancestor(&relpath,
1865 got_worktree_get_root_path(worktree), pe->path);
1866 if (err)
1867 goto done;
1869 /* Re-adding an existing entry is a no-op. */
1870 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1871 continue;
1873 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1874 NULL, NULL);
1875 free(relpath);
1876 if (err)
1877 goto done;
1879 err = got_fileindex_entry_add(fileindex, ie);
1880 if (err) {
1881 got_fileindex_entry_free(ie);
1882 goto done;
1885 err = report_file_status(ie, pe->path, status_cb, status_arg,
1886 repo);
1887 if (err)
1888 goto done;
1891 err = got_opentemp_named(&new_fileindex_path, &new_index,
1892 fileindex_path);
1893 if (err)
1894 goto done;
1896 err = got_fileindex_write(fileindex, new_index);
1897 if (err)
1898 goto done;
1900 if (rename(new_fileindex_path, fileindex_path) != 0) {
1901 err = got_error_from_errno3("rename", new_fileindex_path,
1902 fileindex_path);
1903 goto done;
1906 free(new_fileindex_path);
1907 new_fileindex_path = NULL;
1909 done:
1910 if (index) {
1911 if (fclose(index) != 0 && err == NULL)
1912 err = got_error_from_errno("fclose");
1914 if (new_fileindex_path) {
1915 if (unlink(new_fileindex_path) != 0 && err == NULL)
1916 err = got_error_from_errno2("unlink",
1917 new_fileindex_path);
1918 free(new_fileindex_path);
1920 if (fileindex)
1921 got_fileindex_free(fileindex);
1922 unlockerr = lock_worktree(worktree, LOCK_SH);
1923 if (unlockerr && err == NULL)
1924 err = unlockerr;
1925 return err;
1928 const struct got_error *
1929 got_worktree_schedule_delete(struct got_worktree *worktree,
1930 const char *ondisk_path, int delete_local_mods,
1931 got_worktree_status_cb status_cb, void *status_arg,
1932 struct got_repository *repo)
1934 struct got_fileindex *fileindex = NULL;
1935 struct got_fileindex_entry *ie = NULL;
1936 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1937 FILE *index = NULL, *new_index = NULL;
1938 const struct got_error *err = NULL, *unlockerr = NULL;
1939 unsigned char status;
1940 struct stat sb;
1942 err = lock_worktree(worktree, LOCK_EX);
1943 if (err)
1944 return err;
1946 err = got_path_skip_common_ancestor(&relpath,
1947 got_worktree_get_root_path(worktree), ondisk_path);
1948 if (err)
1949 goto done;
1951 fileindex = got_fileindex_alloc();
1952 if (fileindex == NULL) {
1953 err = got_error_from_errno("got_fileindex_alloc");
1954 goto done;
1957 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1958 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1959 err = got_error_from_errno("asprintf");
1960 fileindex_path = NULL;
1961 goto done;
1964 index = fopen(fileindex_path, "rb");
1965 if (index == NULL) {
1966 err = got_error_from_errno2("fopen", fileindex_path);
1967 goto done;
1970 err = got_fileindex_read(fileindex, index);
1971 if (err)
1972 goto done;
1974 ie = got_fileindex_entry_get(fileindex, relpath);
1975 if (ie == NULL) {
1976 err = got_error(GOT_ERR_BAD_PATH);
1977 goto done;
1980 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1981 if (err)
1982 goto done;
1984 if (status != GOT_STATUS_NO_CHANGE) {
1985 if (status == GOT_STATUS_DELETE) {
1986 err = got_error_set_errno(ENOENT, ondisk_path);
1987 goto done;
1989 if (status != GOT_STATUS_MODIFY) {
1990 err = got_error(GOT_ERR_FILE_STATUS);
1991 goto done;
1993 if (!delete_local_mods) {
1994 err = got_error(GOT_ERR_FILE_MODIFIED);
1995 goto done;
1999 if (unlink(ondisk_path) != 0) {
2000 err = got_error_from_errno2("unlink", ondisk_path);
2001 goto done;
2004 got_fileindex_entry_mark_deleted_from_disk(ie);
2006 err = got_opentemp_named(&new_fileindex_path, &new_index,
2007 fileindex_path);
2008 if (err)
2009 goto done;
2011 err = got_fileindex_write(fileindex, new_index);
2012 if (err)
2013 goto done;
2015 if (rename(new_fileindex_path, fileindex_path) != 0) {
2016 err = got_error_from_errno3("rename", new_fileindex_path,
2017 fileindex_path);
2018 goto done;
2021 free(new_fileindex_path);
2022 new_fileindex_path = NULL;
2024 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2025 done:
2026 free(relpath);
2027 if (index) {
2028 if (fclose(index) != 0 && err == NULL)
2029 err = got_error_from_errno("fclose");
2031 if (new_fileindex_path) {
2032 if (unlink(new_fileindex_path) != 0 && err == NULL)
2033 err = got_error_from_errno2("unlink",
2034 new_fileindex_path);
2035 free(new_fileindex_path);
2037 if (fileindex)
2038 got_fileindex_free(fileindex);
2039 unlockerr = lock_worktree(worktree, LOCK_SH);
2040 if (unlockerr && err == NULL)
2041 err = unlockerr;
2042 return err;
2045 const struct got_error *
2046 got_worktree_revert(struct got_worktree *worktree,
2047 const char *ondisk_path,
2048 got_worktree_checkout_cb progress_cb, void *progress_arg,
2049 struct got_repository *repo)
2051 struct got_fileindex *fileindex = NULL;
2052 struct got_fileindex_entry *ie = NULL;
2053 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2054 char *tree_path = NULL, *parent_path, *te_name;
2055 FILE *index = NULL, *new_index = NULL;
2056 const struct got_error *err = NULL, *unlockerr = NULL;
2057 struct got_tree_object *tree = NULL;
2058 struct got_object_id id, *tree_id = NULL;
2059 const struct got_tree_entry *te;
2060 struct got_blob_object *blob = NULL;
2061 unsigned char status;
2062 struct stat sb;
2064 err = lock_worktree(worktree, LOCK_EX);
2065 if (err)
2066 return err;
2068 err = got_path_skip_common_ancestor(&relpath,
2069 got_worktree_get_root_path(worktree), ondisk_path);
2070 if (err)
2071 goto done;
2073 fileindex = got_fileindex_alloc();
2074 if (fileindex == NULL) {
2075 err = got_error_from_errno("got_fileindex_alloc");
2076 goto done;
2079 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2080 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2081 err = got_error_from_errno("asprintf");
2082 fileindex_path = NULL;
2083 goto done;
2086 index = fopen(fileindex_path, "rb");
2087 if (index == NULL) {
2088 err = got_error_from_errno2("fopen", fileindex_path);
2089 goto done;
2092 err = got_fileindex_read(fileindex, index);
2093 if (err)
2094 goto done;
2096 ie = got_fileindex_entry_get(fileindex, relpath);
2097 if (ie == NULL) {
2098 err = got_error(GOT_ERR_BAD_PATH);
2099 goto done;
2102 /* Construct in-repository path of tree which contains this blob. */
2103 err = got_path_dirname(&parent_path, ie->path);
2104 if (err) {
2105 if (err->code != GOT_ERR_BAD_PATH)
2106 goto done;
2107 parent_path = "/";
2109 if (got_path_is_root_dir(worktree->path_prefix)) {
2110 tree_path = strdup(parent_path);
2111 if (tree_path == NULL) {
2112 err = got_error_from_errno("strdup");
2113 goto done;
2115 } else {
2116 if (got_path_is_root_dir(parent_path)) {
2117 tree_path = strdup(worktree->path_prefix);
2118 if (tree_path == NULL) {
2119 err = got_error_from_errno("strdup");
2120 goto done;
2122 } else {
2123 if (asprintf(&tree_path, "%s/%s",
2124 worktree->path_prefix, parent_path) == -1) {
2125 err = got_error_from_errno("asprintf");
2126 goto done;
2131 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2132 tree_path);
2133 if (err)
2134 goto done;
2136 err = got_object_open_as_tree(&tree, repo, tree_id);
2137 if (err)
2138 goto done;
2140 te_name = basename(ie->path);
2141 if (te_name == NULL) {
2142 err = got_error_from_errno2("basename", ie->path);
2143 goto done;
2146 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2147 if (err)
2148 goto done;
2150 te = got_object_tree_find_entry(tree, te_name);
2151 if (te == NULL && status != GOT_STATUS_ADD) {
2152 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2153 goto done;
2156 switch (status) {
2157 case GOT_STATUS_ADD:
2158 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2159 got_fileindex_entry_remove(fileindex, ie);
2160 break;
2161 case GOT_STATUS_DELETE:
2162 case GOT_STATUS_MODIFY:
2163 case GOT_STATUS_CONFLICT:
2164 case GOT_STATUS_MISSING:
2165 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2166 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2167 if (err)
2168 goto done;
2169 err = install_blob(worktree, ondisk_path, ie->path,
2170 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2171 progress_arg);
2172 if (err)
2173 goto done;
2174 if (status == GOT_STATUS_DELETE) {
2175 err = update_blob_fileindex_entry(worktree,
2176 fileindex, ie, ondisk_path, ie->path, blob, 1);
2177 if (err)
2178 goto done;
2180 break;
2181 default:
2182 goto done;
2185 err = got_opentemp_named(&new_fileindex_path, &new_index,
2186 fileindex_path);
2187 if (err)
2188 goto done;
2190 err = got_fileindex_write(fileindex, new_index);
2191 if (err)
2192 goto done;
2194 if (rename(new_fileindex_path, fileindex_path) != 0) {
2195 err = got_error_from_errno3("rename", new_fileindex_path,
2196 fileindex_path);
2197 goto done;
2200 free(new_fileindex_path);
2201 new_fileindex_path = NULL;
2202 done:
2203 free(relpath);
2204 free(tree_path);
2205 if (blob)
2206 got_object_blob_close(blob);
2207 if (tree)
2208 got_object_tree_close(tree);
2209 free(tree_id);
2210 if (index) {
2211 if (fclose(index) != 0 && err == NULL)
2212 err = got_error_from_errno("fclose");
2214 if (new_fileindex_path) {
2215 if (unlink(new_fileindex_path) != 0 && err == NULL)
2216 err = got_error_from_errno2("unlink",
2217 new_fileindex_path);
2218 free(new_fileindex_path);
2220 if (fileindex)
2221 got_fileindex_free(fileindex);
2222 unlockerr = lock_worktree(worktree, LOCK_SH);
2223 if (unlockerr && err == NULL)
2224 err = unlockerr;
2225 return err;
2228 static void
2229 free_commitable(struct got_commitable *ct)
2231 free(ct->path);
2232 free(ct->in_repo_path);
2233 free(ct->ondisk_path);
2234 free(ct->blob_id);
2235 free(ct->base_id);
2236 free(ct);
2239 struct collect_commitables_arg {
2240 struct got_pathlist_head *commitable_paths;
2241 struct got_repository *repo;
2242 struct got_worktree *worktree;
2245 static const struct got_error *
2246 collect_commitables(void *arg, unsigned char status, const char *relpath,
2247 struct got_object_id *id)
2249 struct collect_commitables_arg *a = arg;
2250 const struct got_error *err = NULL;
2251 struct got_commitable *ct = NULL;
2252 struct got_pathlist_entry *new = NULL;
2253 char *parent_path = NULL, *path = NULL;
2254 struct stat sb;
2256 if (status == GOT_STATUS_CONFLICT)
2257 return got_error(GOT_ERR_COMMIT_CONFLICT);
2259 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2260 status != GOT_STATUS_DELETE)
2261 return NULL;
2263 if (asprintf(&path, "/%s", relpath) == -1) {
2264 err = got_error_from_errno("asprintf");
2265 goto done;
2267 if (strcmp(path, "/") == 0) {
2268 parent_path = strdup("");
2269 if (parent_path == NULL)
2270 return got_error_from_errno("strdup");
2271 } else {
2272 err = got_path_dirname(&parent_path, path);
2273 if (err)
2274 return err;
2277 ct = calloc(1, sizeof(*ct));
2278 if (ct == NULL) {
2279 err = got_error_from_errno("calloc");
2280 goto done;
2283 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2284 relpath) == -1) {
2285 err = got_error_from_errno("asprintf");
2286 goto done;
2288 if (status == GOT_STATUS_DELETE) {
2289 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2290 } else {
2291 if (lstat(ct->ondisk_path, &sb) != 0) {
2292 err = got_error_from_errno2("lstat", ct->ondisk_path);
2293 goto done;
2295 ct->mode = sb.st_mode;
2298 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2299 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2300 relpath) == -1) {
2301 err = got_error_from_errno("asprintf");
2302 goto done;
2305 ct->status = status;
2306 ct->blob_id = NULL; /* will be filled in when blob gets created */
2307 if (ct->status != GOT_STATUS_ADD) {
2308 ct->base_id = got_object_id_dup(id);
2309 if (ct->base_id == NULL) {
2310 err = got_error_from_errno("got_object_id_dup");
2311 goto done;
2314 ct->path = strdup(path);
2315 if (ct->path == NULL) {
2316 err = got_error_from_errno("strdup");
2317 goto done;
2319 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2320 done:
2321 if (ct && (err || new == NULL))
2322 free_commitable(ct);
2323 free(parent_path);
2324 free(path);
2325 return err;
2328 static const struct got_error *write_tree(struct got_object_id **,
2329 struct got_tree_object *, const char *, struct got_pathlist_head *,
2330 got_worktree_status_cb status_cb, void *status_arg,
2331 struct got_repository *);
2333 static const struct got_error *
2334 write_subtree(struct got_object_id **new_subtree_id,
2335 struct got_tree_entry *te, const char *parent_path,
2336 struct got_pathlist_head *commitable_paths,
2337 got_worktree_status_cb status_cb, void *status_arg,
2338 struct got_repository *repo)
2340 const struct got_error *err = NULL;
2341 struct got_tree_object *subtree;
2342 char *subpath;
2344 if (asprintf(&subpath, "%s%s%s", parent_path,
2345 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2346 return got_error_from_errno("asprintf");
2348 err = got_object_open_as_tree(&subtree, repo, te->id);
2349 if (err)
2350 return err;
2352 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2353 status_cb, status_arg, repo);
2354 got_object_tree_close(subtree);
2355 free(subpath);
2356 return err;
2359 static const struct got_error *
2360 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2362 const struct got_error *err = NULL;
2363 char *ct_parent_path = NULL;
2365 *match = 0;
2367 if (strchr(ct->path, '/') == NULL) {
2368 *match = got_path_is_root_dir(path);
2369 return NULL;
2372 err = got_path_dirname(&ct_parent_path, ct->path);
2373 if (err)
2374 return err;
2375 *match = (strcmp(path, ct_parent_path) == 0);
2376 free(ct_parent_path);
2377 return err;
2380 static mode_t
2381 get_ct_file_mode(struct got_commitable *ct)
2383 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2386 static const struct got_error *
2387 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2388 struct got_tree_entry *te, struct got_commitable *ct)
2390 const struct got_error *err = NULL;
2392 *new_te = NULL;
2394 err = got_object_tree_entry_dup(new_te, te);
2395 if (err)
2396 goto done;
2398 (*new_te)->mode = get_ct_file_mode(ct);
2400 free((*new_te)->id);
2401 (*new_te)->id = got_object_id_dup(ct->blob_id);
2402 if ((*new_te)->id == NULL) {
2403 err = got_error_from_errno("got_object_id_dup");
2404 goto done;
2406 done:
2407 if (err && *new_te) {
2408 got_object_tree_entry_close(*new_te);
2409 *new_te = NULL;
2411 return err;
2414 static const struct got_error *
2415 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2416 struct got_commitable *ct)
2418 const struct got_error *err = NULL;
2419 char *ct_name;
2421 *new_te = NULL;
2423 *new_te = calloc(1, sizeof(**new_te));
2424 if (*new_te == NULL)
2425 return got_error_from_errno("calloc");
2427 ct_name = basename(ct->path);
2428 if (ct_name == NULL) {
2429 err = got_error_from_errno2("basename", ct->path);
2430 goto done;
2432 (*new_te)->name = strdup(ct_name);
2433 if ((*new_te)->name == NULL) {
2434 err = got_error_from_errno("strdup");
2435 goto done;
2438 (*new_te)->mode = get_ct_file_mode(ct);
2440 (*new_te)->id = got_object_id_dup(ct->blob_id);
2441 if ((*new_te)->id == NULL) {
2442 err = got_error_from_errno("got_object_id_dup");
2443 goto done;
2445 done:
2446 if (err && *new_te) {
2447 got_object_tree_entry_close(*new_te);
2448 *new_te = NULL;
2450 return err;
2453 static const struct got_error *
2454 insert_tree_entry(struct got_tree_entry *new_te,
2455 struct got_pathlist_head *paths)
2457 const struct got_error *err = NULL;
2458 struct got_pathlist_entry *new_pe;
2460 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2461 if (err)
2462 return err;
2463 if (new_pe == NULL)
2464 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2465 return NULL;
2468 static const struct got_error *
2469 report_ct_status(struct got_commitable *ct,
2470 got_worktree_status_cb status_cb, void *status_arg)
2472 const char *ct_path = ct->path;
2473 while (ct_path[0] == '/')
2474 ct_path++;
2475 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id);
2478 static const struct got_error *
2479 match_modified_subtree(int *modified, struct got_tree_entry *te,
2480 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2482 const struct got_error *err = NULL;
2483 struct got_pathlist_entry *pe;
2484 char *te_path;
2486 *modified = 0;
2488 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2489 got_path_is_root_dir(base_tree_path) ? "" : "/",
2490 te->name) == -1)
2491 return got_error_from_errno("asprintf");
2493 TAILQ_FOREACH(pe, commitable_paths, entry) {
2494 struct got_commitable *ct = pe->data;
2495 *modified = got_path_is_child(ct->in_repo_path, te_path,
2496 strlen(te_path));
2497 if (*modified)
2498 break;
2501 free(te_path);
2502 return err;
2505 static const struct got_error *
2506 match_deleted_or_modified_ct(struct got_commitable **ctp,
2507 struct got_tree_entry *te, const char *base_tree_path,
2508 struct got_pathlist_head *commitable_paths)
2510 const struct got_error *err = NULL;
2511 struct got_pathlist_entry *pe;
2513 *ctp = NULL;
2515 TAILQ_FOREACH(pe, commitable_paths, entry) {
2516 struct got_commitable *ct = pe->data;
2517 char *ct_name = NULL;
2518 int path_matches;
2520 if (ct->status != GOT_STATUS_MODIFY &&
2521 ct->status != GOT_STATUS_DELETE)
2522 continue;
2524 if (got_object_id_cmp(ct->base_id, te->id) != 0)
2525 continue;
2527 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2528 if (err)
2529 return err;
2530 if (!path_matches)
2531 continue;
2533 ct_name = basename(pe->path);
2534 if (ct_name == NULL)
2535 return got_error_from_errno2("basename", pe->path);
2537 if (strcmp(te->name, ct_name) != 0)
2538 continue;
2540 *ctp = ct;
2541 break;
2544 return err;
2547 static const struct got_error *
2548 write_tree(struct got_object_id **new_tree_id,
2549 struct got_tree_object *base_tree, const char *path_base_tree,
2550 struct got_pathlist_head *commitable_paths,
2551 got_worktree_status_cb status_cb, void *status_arg,
2552 struct got_repository *repo)
2554 const struct got_error *err = NULL;
2555 const struct got_tree_entries *base_entries = NULL;
2556 struct got_pathlist_head paths;
2557 struct got_tree_entries new_tree_entries;
2558 struct got_tree_entry *te, *new_te = NULL;
2559 struct got_pathlist_entry *pe;
2561 TAILQ_INIT(&paths);
2562 new_tree_entries.nentries = 0;
2563 SIMPLEQ_INIT(&new_tree_entries.head);
2565 /* Insert, and recurse into, newly added entries first. */
2566 TAILQ_FOREACH(pe, commitable_paths, entry) {
2567 struct got_commitable *ct = pe->data;
2568 char *child_path = NULL, *slash;
2570 if (ct->status != GOT_STATUS_ADD)
2571 continue;
2573 if (!got_path_is_child(pe->path, path_base_tree,
2574 strlen(path_base_tree)))
2575 continue;
2577 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2578 pe->path);
2579 if (err)
2580 goto done;
2582 slash = strchr(child_path, '/');
2583 if (slash == NULL) {
2584 err = alloc_added_blob_tree_entry(&new_te, ct);
2585 if (err)
2586 goto done;
2587 err = report_ct_status(ct, status_cb, status_arg);
2588 if (err)
2589 goto done;
2590 } else {
2591 char *subtree_path;
2592 struct got_pathlist_entry *pe2;
2593 int visited = 0;
2595 *slash = '\0'; /* trim trailing path components */
2596 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2597 got_path_is_root_dir(path_base_tree) ? "" : "/",
2598 child_path) == -1) {
2599 err = got_error_from_errno("asprintf");
2600 goto done;
2602 TAILQ_FOREACH(pe2, &paths, entry) {
2603 if (got_path_cmp(subtree_path, pe2->path) != 0)
2604 continue;
2605 visited = 1;
2606 break;
2608 if (visited) {
2609 free(subtree_path);
2610 continue;
2613 new_te = calloc(1, sizeof(*new_te));
2614 new_te->mode = S_IFDIR;
2615 new_te->name = strdup(child_path);
2616 if (new_te->name == NULL) {
2617 err = got_error_from_errno("strdup");
2618 got_object_tree_entry_close(new_te);
2619 new_te = NULL;
2620 goto done;
2622 err = write_tree(&new_te->id, NULL, subtree_path,
2623 commitable_paths, status_cb, status_arg, repo);
2624 free(subtree_path);
2625 if (err) {
2626 got_object_tree_entry_close(new_te);
2627 new_te = NULL;
2628 goto done;
2631 err = insert_tree_entry(new_te, &paths);
2632 if (err)
2633 goto done;
2636 if (base_tree) {
2637 /* Handle modified and deleted entries. */
2638 base_entries = got_object_tree_get_entries(base_tree);
2639 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2640 struct got_commitable *ct = NULL;
2642 if (S_ISDIR(te->mode)) {
2643 int modified;
2644 err = got_object_tree_entry_dup(&new_te, te);
2645 if (err)
2646 goto done;
2647 err = match_modified_subtree(&modified, te,
2648 path_base_tree, commitable_paths);
2649 if (err)
2650 goto done;
2651 /* Avoid recursion into unmodified subtrees. */
2652 if (modified) {
2653 free(new_te->id);
2654 err = write_subtree(&new_te->id, te,
2655 path_base_tree, commitable_paths,
2656 status_cb, status_arg, repo);
2657 if (err)
2658 goto done;
2660 err = insert_tree_entry(new_te, &paths);
2661 if (err)
2662 goto done;
2663 continue;
2666 err = match_deleted_or_modified_ct(&ct, te,
2667 path_base_tree, commitable_paths);
2668 if (ct) {
2669 /* NB: Deleted entries get dropped here. */
2670 if (ct->status == GOT_STATUS_MODIFY) {
2671 err = alloc_modified_blob_tree_entry(
2672 &new_te, te, ct);
2673 if (err)
2674 goto done;
2675 err = insert_tree_entry(new_te, &paths);
2676 if (err)
2677 goto done;
2679 err = report_ct_status(ct, status_cb, status_arg);
2680 if (err)
2681 goto done;
2682 } else {
2683 /* Entry is unchanged; just copy it. */
2684 err = got_object_tree_entry_dup(&new_te, te);
2685 if (err)
2686 goto done;
2687 err = insert_tree_entry(new_te, &paths);
2688 if (err)
2689 goto done;
2694 /* Write new list of entries; deleted entries have been dropped. */
2695 TAILQ_FOREACH(pe, &paths, entry) {
2696 struct got_tree_entry *te = pe->data;
2697 new_tree_entries.nentries++;
2698 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2700 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2701 done:
2702 got_object_tree_entries_close(&new_tree_entries);
2703 got_pathlist_free(&paths);
2704 return err;
2707 static const struct got_error *
2708 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2709 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2711 const struct got_error *err = NULL;
2712 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2713 struct got_fileindex *fileindex = NULL;
2714 FILE *new_index = NULL;
2715 struct got_pathlist_entry *pe;
2717 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2718 if (err)
2719 return err;
2721 err = got_opentemp_named(&new_fileindex_path, &new_index,
2722 fileindex_path);
2723 if (err)
2724 goto done;
2726 TAILQ_FOREACH(pe, commitable_paths, entry) {
2727 struct got_fileindex_entry *ie;
2728 struct got_commitable *ct = pe->data;
2730 ie = got_fileindex_entry_get(fileindex, pe->path);
2731 if (ie) {
2732 if (ct->status == GOT_STATUS_DELETE) {
2733 got_fileindex_entry_remove(fileindex, ie);
2734 got_fileindex_entry_free(ie);
2735 } else
2736 err = got_fileindex_entry_update(ie,
2737 ct->ondisk_path, ct->blob_id->sha1,
2738 new_base_commit_id->sha1, 1);
2739 } else {
2740 err = got_fileindex_entry_alloc(&ie,
2741 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2742 new_base_commit_id->sha1);
2743 if (err)
2744 goto done;
2745 err = got_fileindex_entry_add(fileindex, ie);
2746 if (err)
2747 goto done;
2751 err = got_fileindex_write(fileindex, new_index);
2752 if (err)
2753 goto done;
2755 if (rename(new_fileindex_path, fileindex_path) != 0) {
2756 err = got_error_from_errno3("rename", new_fileindex_path,
2757 fileindex_path);
2758 unlink(new_fileindex_path);
2759 goto done;
2762 free(new_fileindex_path);
2763 new_fileindex_path = NULL;
2765 done:
2766 if (new_fileindex_path)
2767 unlink(new_fileindex_path);
2768 if (new_index)
2769 fclose(new_index);
2770 free(new_fileindex_path);
2771 free(fileindex_path);
2772 got_fileindex_free(fileindex);
2773 return err;
2776 static const struct got_error *
2777 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2778 struct got_object_id *head_commit_id)
2780 const struct got_error *err = NULL;
2781 struct got_object_id *id_in_head;
2784 * XXX This should probably be checking each commit from
2785 * worktree's base_commit -> head and verify that the
2786 * same blob exists in each of these commits.
2787 * Removals+additions within this line of history could mean
2788 * that renames have occured in which case we should really
2789 * be forcing the user to run an update...
2791 err = got_object_id_by_path(&id_in_head, repo,
2792 head_commit_id, ct->in_repo_path);
2793 if (err) {
2794 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2795 return err;
2796 if (ct->status != GOT_STATUS_ADD)
2797 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2798 err = NULL;
2799 id_in_head = NULL;
2802 if (id_in_head && got_object_id_cmp(id_in_head, ct->base_id) != 0)
2803 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2805 free(id_in_head);
2806 return err;
2809 const struct got_error *
2810 got_worktree_commit(struct got_object_id **new_commit_id,
2811 struct got_worktree *worktree, const char *ondisk_path,
2812 const char *author, const char *committer,
2813 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2814 got_worktree_status_cb status_cb, void *status_arg,
2815 struct got_repository *repo)
2817 const struct got_error *err = NULL, *unlockerr = NULL;
2818 struct collect_commitables_arg cc_arg;
2819 struct got_pathlist_head commitable_paths;
2820 struct got_pathlist_entry *pe;
2821 char *relpath = NULL;
2822 const char *head_ref_name = NULL;
2823 struct got_reference *head_ref = NULL;
2824 struct got_commit_object *head_commit = NULL;
2825 struct got_object_id *head_commit_id = NULL;
2826 struct got_reference *head_ref2 = NULL;
2827 struct got_object_id *head_commit_id2 = NULL;
2828 struct got_tree_object *head_tree = NULL;
2829 struct got_object_id *new_tree_id = NULL;
2830 struct got_object_id_queue parent_ids;
2831 struct got_object_qid *pid = NULL;
2832 char *logmsg = NULL;
2834 *new_commit_id = NULL;
2836 TAILQ_INIT(&commitable_paths);
2837 SIMPLEQ_INIT(&parent_ids);
2839 if (ondisk_path) {
2840 err = got_path_skip_common_ancestor(&relpath,
2841 worktree->root_path, ondisk_path);
2842 if (err)
2843 return err;
2846 err = lock_worktree(worktree, LOCK_EX);
2847 if (err)
2848 goto done;
2850 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2851 if (err)
2852 goto done;
2853 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2854 if (err)
2855 goto done;
2857 cc_arg.commitable_paths = &commitable_paths;
2858 cc_arg.worktree = worktree;
2859 cc_arg.repo = repo;
2860 err = got_worktree_status(worktree, relpath ? relpath : "",
2861 repo, collect_commitables, &cc_arg, NULL, NULL);
2862 if (err)
2863 goto done;
2865 if (TAILQ_EMPTY(&commitable_paths)) {
2866 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2867 goto done;
2870 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2871 if (err)
2872 goto done;
2874 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2875 struct got_commitable *ct = pe->data;
2876 err = check_ct_out_of_date(ct, repo, head_commit_id);
2877 if (err)
2878 goto done;
2881 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2882 if (err)
2883 goto done;
2885 if (commit_msg_cb != NULL) {
2886 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2887 if (err)
2888 goto done;
2891 if (logmsg == NULL || strlen(logmsg) == 0) {
2892 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2893 goto done;
2896 /* Create blobs from added and modified files and record their IDs. */
2897 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2898 struct got_commitable *ct = pe->data;
2899 char *ondisk_path;
2901 if (ct->status != GOT_STATUS_ADD &&
2902 ct->status != GOT_STATUS_MODIFY)
2903 continue;
2905 if (asprintf(&ondisk_path, "%s/%s",
2906 worktree->root_path, pe->path) == -1) {
2907 err = got_error_from_errno("asprintf");
2908 goto done;
2910 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2911 free(ondisk_path);
2912 if (err)
2913 goto done;
2916 /* Recursively write new tree objects. */
2917 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2918 status_cb, status_arg, repo);
2919 if (err)
2920 goto done;
2922 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2923 if (err)
2924 goto done;
2925 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2926 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2927 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2928 got_object_qid_free(pid);
2929 if (logmsg != NULL)
2930 free(logmsg);
2931 if (err)
2932 goto done;
2934 /* Check if a concurrent commit to our branch has occurred. */
2935 head_ref_name = got_worktree_get_head_ref_name(worktree);
2936 if (head_ref_name == NULL) {
2937 err = got_error_from_errno("got_worktree_get_head_ref_name");
2938 goto done;
2940 /* Lock the reference here to prevent concurrent modification. */
2941 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
2942 if (err)
2943 goto done;
2944 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2945 if (err)
2946 goto done;
2947 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2948 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2949 goto done;
2951 /* Update branch head in repository. */
2952 err = got_ref_change_ref(head_ref2, *new_commit_id);
2953 if (err)
2954 goto done;
2955 err = got_ref_write(head_ref2, repo);
2956 if (err)
2957 goto done;
2959 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2960 if (err)
2961 goto done;
2963 err = ref_base_commit(worktree, repo);
2964 if (err)
2965 goto done;
2967 err = update_fileindex_after_commit(&commitable_paths,
2968 *new_commit_id, worktree);
2969 if (err)
2970 goto done;
2971 done:
2972 unlockerr = lock_worktree(worktree, LOCK_SH);
2973 if (unlockerr && err == NULL)
2974 err = unlockerr;
2975 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2976 struct got_commitable *ct = pe->data;
2977 free_commitable(ct);
2979 got_pathlist_free(&commitable_paths);
2980 if (head_tree)
2981 got_object_tree_close(head_tree);
2982 if (head_commit)
2983 got_object_commit_close(head_commit);
2984 free(relpath);
2985 free(head_commit_id);
2986 free(head_commit_id2);
2987 if (head_ref)
2988 got_ref_close(head_ref);
2989 if (head_ref2) {
2990 unlockerr = got_ref_unlock(head_ref2);
2991 if (unlockerr && err == NULL)
2992 err = unlockerr;
2993 got_ref_close(head_ref2);
2995 return err;