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"
43 #include "got_path.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_prefix_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_prefix_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_prefix_errno("dprintf");
84 goto done;
85 }
86 }
88 done:
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_prefix_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_prefix_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_prefix_errno2("fprintf", tmppath);
117 goto done;
121 if (rename(tmppath, path) != 0) {
122 err = got_error_prefix_errno3("rename", tmppath, path);
123 unlink(tmppath);
124 goto done;
127 done:
128 if (fclose(tmpfile) != 0 && err == NULL)
129 err = got_error_prefix_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_prefix_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_prefix_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_prefix_errno2("flock", path));
162 goto done;
165 if (lstat(path, &sb) != 0) {
166 err = got_error_prefix_errno2("lstat", path);
167 goto done;
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_errno("asprintf");
242 goto done;
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_errno("fread");
617 break;
619 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
620 if (flen2 == 0 && ferror(f2)) {
621 err = got_error_prefix_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_prefix_errno2("lstat", f1_path);
658 goto done;
660 size1 = sb.st_size;
662 if (lstat(f2_path, &sb) != 0) {
663 err = got_error_prefix_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_prefix_errno2("open", f1_path);
677 f2 = fopen(f2_path, "r");
678 if (f2 == NULL) {
679 err = got_error_prefix_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_prefix_errno("fclose");
687 if (f2 && fclose(f2) != 0 && err == NULL)
688 err = got_error_prefix_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_prefix_errno2("dirname", ondisk_path);
720 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
721 return got_error_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_errno2("chmod", merged_path);
799 goto done;
802 if (rename(merged_path, ondisk_path) != 0) {
803 err = got_error_prefix_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_prefix_errno("close");
818 if (f1 && fclose(f1) != 0 && err == NULL)
819 err = got_error_prefix_errno("fclose");
820 if (f2 && fclose(f2) != 0 && err == NULL)
821 err = got_error_prefix_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_prefix_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_prefix_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_prefix_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_prefix_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_prefix_errno("fsync");
938 goto done;
941 if (update) {
942 if (rename(tmppath, ondisk_path) != 0) {
943 err = got_error_prefix_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_prefix_errno2("chmod", ondisk_path);
953 goto done;
955 } else {
956 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
957 err = got_error_prefix_errno2("chmod", ondisk_path);
958 goto done;
962 done:
963 if (fd != -1 && close(fd) != 0 && err == NULL)
964 err = got_error_prefix_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 (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
995 *status = GOT_STATUS_CONFLICT;
996 else
997 i++;
1001 return err;
1004 static const struct got_error *
1005 get_file_status(unsigned char *status, struct stat *sb,
1006 struct got_fileindex_entry *ie, const char *abspath,
1007 struct got_repository *repo)
1009 const struct got_error *err = NULL;
1010 struct got_object_id id;
1011 size_t hdrlen;
1012 FILE *f = NULL;
1013 uint8_t fbuf[8192];
1014 struct got_blob_object *blob = NULL;
1015 size_t flen, blen;
1017 *status = GOT_STATUS_NO_CHANGE;
1019 if (lstat(abspath, sb) == -1) {
1020 if (errno == ENOENT) {
1021 if (ie) {
1022 if (got_fileindex_entry_has_file_on_disk(ie))
1023 *status = GOT_STATUS_MISSING;
1024 else
1025 *status = GOT_STATUS_DELETE;
1026 sb->st_mode =
1027 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1028 & (S_IRWXU | S_IRWXG | S_IRWXO));
1029 } else
1030 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1031 return NULL;
1033 return got_error_prefix_errno2("lstat", abspath);
1036 if (!S_ISREG(sb->st_mode)) {
1037 *status = GOT_STATUS_OBSTRUCTED;
1038 return NULL;
1041 if (ie == NULL)
1042 return NULL;
1044 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1045 *status = GOT_STATUS_DELETE;
1046 return NULL;
1047 } else if (!got_fileindex_entry_has_blob(ie)) {
1048 *status = GOT_STATUS_ADD;
1049 return NULL;
1052 if (ie->ctime_sec == sb->st_ctime &&
1053 ie->ctime_nsec == sb->st_ctimensec &&
1054 ie->mtime_sec == sb->st_mtime &&
1055 ie->mtime_sec == sb->st_mtime &&
1056 ie->mtime_nsec == sb->st_mtimensec &&
1057 ie->size == (sb->st_size & 0xffffffff))
1058 return NULL;
1060 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1061 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1062 if (err)
1063 return err;
1065 f = fopen(abspath, "r");
1066 if (f == NULL) {
1067 err = got_error_prefix_errno2("fopen", abspath);
1068 goto done;
1070 hdrlen = got_object_blob_get_hdrlen(blob);
1071 while (1) {
1072 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1073 err = got_object_blob_read_block(&blen, blob);
1074 if (err)
1075 goto done;
1076 /* Skip length of blob object header first time around. */
1077 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1078 if (flen == 0 && ferror(f)) {
1079 err = got_error_prefix_errno("fread");
1080 goto done;
1082 if (blen == 0) {
1083 if (flen != 0)
1084 *status = GOT_STATUS_MODIFY;
1085 break;
1086 } else if (flen == 0) {
1087 if (blen != 0)
1088 *status = GOT_STATUS_MODIFY;
1089 break;
1090 } else if (blen - hdrlen == flen) {
1091 /* Skip blob object header first time around. */
1092 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1093 *status = GOT_STATUS_MODIFY;
1094 break;
1096 } else {
1097 *status = GOT_STATUS_MODIFY;
1098 break;
1100 hdrlen = 0;
1103 if (*status == GOT_STATUS_MODIFY) {
1104 rewind(f);
1105 err = get_modified_file_content_status(status, f);
1107 done:
1108 if (blob)
1109 got_object_blob_close(blob);
1110 if (f)
1111 fclose(f);
1112 return err;
1115 static const struct got_error *
1116 update_blob(struct got_worktree *worktree,
1117 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1118 struct got_tree_entry *te, const char *path,
1119 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1120 void *progress_arg)
1122 const struct got_error *err = NULL;
1123 struct got_blob_object *blob = NULL;
1124 char *ondisk_path;
1125 unsigned char status = GOT_STATUS_NO_CHANGE;
1126 struct stat sb;
1128 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1129 return got_error_prefix_errno("asprintf");
1131 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1132 if (err)
1133 goto done;
1135 if (status == GOT_STATUS_OBSTRUCTED) {
1136 (*progress_cb)(progress_arg, status, path);
1137 goto done;
1140 if (ie && status != GOT_STATUS_MISSING) {
1141 if (got_fileindex_entry_has_commit(ie) &&
1142 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1143 SHA1_DIGEST_LENGTH) == 0) {
1144 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1145 path);
1146 goto done;
1148 if (got_fileindex_entry_has_blob(ie) &&
1149 memcmp(ie->blob_sha1, te->id->sha1,
1150 SHA1_DIGEST_LENGTH) == 0)
1151 goto done;
1154 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1155 if (err)
1156 goto done;
1158 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1159 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1160 te->mode, sb.st_mode, blob, repo, progress_cb,
1161 progress_arg);
1162 else if (status == GOT_STATUS_DELETE) {
1163 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1164 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1165 ondisk_path, path, blob, 0);
1166 if (err)
1167 goto done;
1168 } else {
1169 err = install_blob(worktree, ondisk_path, path, te->mode,
1170 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1171 repo, progress_cb, progress_arg);
1172 if (err)
1173 goto done;
1174 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1175 ondisk_path, path, blob, 1);
1176 if (err)
1177 goto done;
1179 got_object_blob_close(blob);
1180 done:
1181 free(ondisk_path);
1182 return err;
1185 static const struct got_error *
1186 remove_ondisk_file(const char *root_path, const char *path)
1188 const struct got_error *err = NULL;
1189 char *ondisk_path = NULL;
1191 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1192 return got_error_prefix_errno("asprintf");
1194 if (unlink(ondisk_path) == -1) {
1195 if (errno != ENOENT)
1196 err = got_error_prefix_errno2("unlink", ondisk_path);
1197 } else {
1198 char *parent = dirname(ondisk_path);
1199 while (parent && strcmp(parent, root_path) != 0) {
1200 if (rmdir(parent) == -1) {
1201 if (errno != ENOTEMPTY)
1202 err = got_error_prefix_errno2("rmdir",
1203 parent);
1204 break;
1206 parent = dirname(parent);
1209 free(ondisk_path);
1210 return err;
1213 static const struct got_error *
1214 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1215 struct got_fileindex_entry *ie, const char *parent_path,
1216 struct got_repository *repo,
1217 got_worktree_checkout_cb progress_cb, void *progress_arg)
1219 const struct got_error *err = NULL;
1220 unsigned char status;
1221 struct stat sb;
1222 char *ondisk_path;
1224 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1225 == -1)
1226 return got_error_prefix_errno("asprintf");
1228 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1229 if (err)
1230 return err;
1232 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1233 status == GOT_STATUS_ADD) {
1234 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1236 * Preserve the working file and change the deleted blob's
1237 * entry into a schedule-add entry.
1239 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1240 0);
1241 if (err)
1242 return err;
1243 } else {
1244 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1245 if (status == GOT_STATUS_NO_CHANGE) {
1246 err = remove_ondisk_file(worktree->root_path, ie->path);
1247 if (err)
1248 return err;
1250 got_fileindex_entry_remove(fileindex, ie);
1253 return err;
1256 struct diff_cb_arg {
1257 struct got_fileindex *fileindex;
1258 struct got_worktree *worktree;
1259 struct got_repository *repo;
1260 got_worktree_checkout_cb progress_cb;
1261 void *progress_arg;
1262 got_worktree_cancel_cb cancel_cb;
1263 void *cancel_arg;
1266 static const struct got_error *
1267 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1268 struct got_tree_entry *te, const char *parent_path)
1270 struct diff_cb_arg *a = arg;
1272 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1273 return got_error(GOT_ERR_CANCELLED);
1275 return update_blob(a->worktree, a->fileindex, ie, te,
1276 ie->path, a->repo, a->progress_cb, a->progress_arg);
1279 static const struct got_error *
1280 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1282 struct diff_cb_arg *a = arg;
1284 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1285 return got_error(GOT_ERR_CANCELLED);
1287 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1288 a->repo, a->progress_cb, a->progress_arg);
1291 static const struct got_error *
1292 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1294 struct diff_cb_arg *a = arg;
1295 const struct got_error *err;
1296 char *path;
1298 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1299 return got_error(GOT_ERR_CANCELLED);
1301 if (asprintf(&path, "%s%s%s", parent_path,
1302 parent_path[0] ? "/" : "", te->name)
1303 == -1)
1304 return got_error_prefix_errno("asprintf");
1306 if (S_ISDIR(te->mode))
1307 err = add_dir_on_disk(a->worktree, path);
1308 else
1309 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1310 a->repo, a->progress_cb, a->progress_arg);
1312 free(path);
1313 return err;
1316 const struct got_error *
1317 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1319 const struct got_error *err = NULL;
1320 char *uuidstr = NULL;
1321 uint32_t uuid_status;
1323 *refname = NULL;
1325 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1326 if (uuid_status != uuid_s_ok)
1327 return got_error_uuid(uuid_status);
1329 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1330 == -1) {
1331 err = got_error_prefix_errno("asprintf");
1332 *refname = NULL;
1334 free(uuidstr);
1335 return err;
1339 * Prevent Git's garbage collector from deleting our base commit by
1340 * setting a reference to our base commit's ID.
1342 static const struct got_error *
1343 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1345 const struct got_error *err = NULL;
1346 struct got_reference *ref = NULL;
1347 char *refname;
1349 err = got_worktree_get_base_ref_name(&refname, worktree);
1350 if (err)
1351 return err;
1353 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1354 if (err)
1355 goto done;
1357 err = got_ref_write(ref, repo);
1358 done:
1359 free(refname);
1360 if (ref)
1361 got_ref_close(ref);
1362 return err;
1365 static const struct got_error *
1366 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1367 struct got_worktree *worktree)
1369 const struct got_error *err = NULL;
1370 FILE *index = NULL;
1372 *fileindex_path = NULL;
1373 *fileindex = got_fileindex_alloc();
1374 if (*fileindex == NULL)
1375 return got_error_prefix_errno("got_fileindex_alloc");
1377 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1378 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1379 err = got_error_prefix_errno("asprintf");
1380 *fileindex_path = NULL;
1381 goto done;
1384 index = fopen(*fileindex_path, "rb");
1385 if (index == NULL) {
1386 if (errno != ENOENT)
1387 err = got_error_prefix_errno2("fopen", *fileindex_path);
1388 } else {
1389 err = got_fileindex_read(*fileindex, index);
1390 if (fclose(index) != 0 && err == NULL)
1391 err = got_error_prefix_errno("fclose");
1393 done:
1394 if (err) {
1395 free(*fileindex_path);
1396 *fileindex_path = NULL;
1397 free(*fileindex);
1398 *fileindex = NULL;
1400 return err;
1403 const struct got_error *
1404 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1405 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1406 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1408 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1409 struct got_commit_object *commit = NULL;
1410 struct got_object_id *tree_id = NULL;
1411 struct got_tree_object *tree = NULL;
1412 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1413 struct got_fileindex *fileindex = NULL;
1414 FILE *new_index = NULL;
1415 struct got_fileindex_diff_tree_cb diff_cb;
1416 struct diff_cb_arg arg;
1417 char *relpath = NULL, *entry_name = NULL;
1419 err = lock_worktree(worktree, LOCK_EX);
1420 if (err)
1421 return err;
1424 * Read the file index.
1425 * Checking out files is supposed to be an idempotent operation.
1426 * If the on-disk file index is incomplete we will try to complete it.
1428 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1429 if (err)
1430 goto done;
1432 err = got_opentemp_named(&new_fileindex_path, &new_index,
1433 fileindex_path);
1434 if (err)
1435 goto done;
1437 err = ref_base_commit(worktree, repo);
1438 if (err)
1439 goto done;
1441 err = got_object_open_as_commit(&commit, repo,
1442 worktree->base_commit_id);
1443 if (err)
1444 goto done;
1446 if (path[0]) {
1447 char *tree_path;
1448 int obj_type;
1449 relpath = strdup(path);
1450 if (relpath == NULL) {
1451 err = got_error_prefix_errno("strdup");
1452 goto done;
1454 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1455 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1456 path) == -1) {
1457 err = got_error_prefix_errno("asprintf");
1458 goto done;
1460 err = got_object_id_by_path(&tree_id, repo,
1461 worktree->base_commit_id, tree_path);
1462 free(tree_path);
1463 if (err)
1464 goto done;
1465 err = got_object_get_type(&obj_type, repo, tree_id);
1466 if (err)
1467 goto done;
1468 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1469 /* Split provided path into parent dir + entry name. */
1470 if (strchr(path, '/') == NULL) {
1471 relpath = strdup("");
1472 if (relpath == NULL) {
1473 err = got_error_prefix_errno("strdup");
1474 goto done;
1476 tree_path = strdup(worktree->path_prefix);
1477 if (tree_path == NULL) {
1478 err = got_error_prefix_errno("strdup");
1479 goto done;
1481 } else {
1482 err = got_path_dirname(&relpath, path);
1483 if (err)
1484 goto done;
1485 if (asprintf(&tree_path, "%s%s%s",
1486 worktree->path_prefix,
1487 got_path_is_root_dir(
1488 worktree->path_prefix) ? "" : "/",
1489 relpath) == -1) {
1490 err = got_error_prefix_errno("asprintf");
1491 goto done;
1494 err = got_object_id_by_path(&tree_id, repo,
1495 worktree->base_commit_id, tree_path);
1496 free(tree_path);
1497 if (err)
1498 goto done;
1499 entry_name = basename(path);
1500 if (entry_name == NULL) {
1501 err = got_error_prefix_errno2("basename", path);
1502 goto done;
1505 } else {
1506 relpath = strdup("");
1507 if (relpath == NULL) {
1508 err = got_error_prefix_errno("strdup");
1509 goto done;
1511 err = got_object_id_by_path(&tree_id, repo,
1512 worktree->base_commit_id, worktree->path_prefix);
1513 if (err)
1514 goto done;
1517 err = got_object_open_as_tree(&tree, repo, tree_id);
1518 if (err)
1519 goto done;
1521 if (entry_name &&
1522 got_object_tree_find_entry(tree, entry_name) == NULL) {
1523 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1524 goto done;
1527 diff_cb.diff_old_new = diff_old_new;
1528 diff_cb.diff_old = diff_old;
1529 diff_cb.diff_new = diff_new;
1530 arg.fileindex = fileindex;
1531 arg.worktree = worktree;
1532 arg.repo = repo;
1533 arg.progress_cb = progress_cb;
1534 arg.progress_arg = progress_arg;
1535 arg.cancel_cb = cancel_cb;
1536 arg.cancel_arg = cancel_arg;
1537 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1538 entry_name, repo, &diff_cb, &arg);
1540 /* Try to sync the fileindex back to disk in any case. */
1541 err = got_fileindex_write(fileindex, new_index);
1542 if (err)
1543 goto done;
1545 if (rename(new_fileindex_path, fileindex_path) != 0) {
1546 err = got_error_prefix_errno3("rename", new_fileindex_path,
1547 fileindex_path);
1548 unlink(new_fileindex_path);
1549 goto done;
1552 free(new_fileindex_path);
1553 new_fileindex_path = NULL;
1555 done:
1556 free(relpath);
1557 if (tree)
1558 got_object_tree_close(tree);
1559 if (commit)
1560 got_object_commit_close(commit);
1561 if (new_fileindex_path)
1562 unlink(new_fileindex_path);
1563 if (new_index)
1564 fclose(new_index);
1565 free(new_fileindex_path);
1566 free(fileindex_path);
1567 got_fileindex_free(fileindex);
1568 if (checkout_err)
1569 err = checkout_err;
1570 unlockerr = lock_worktree(worktree, LOCK_SH);
1571 if (unlockerr && err == NULL)
1572 err = unlockerr;
1573 return err;
1576 struct diff_dir_cb_arg {
1577 struct got_fileindex *fileindex;
1578 struct got_worktree *worktree;
1579 const char *status_path;
1580 size_t status_path_len;
1581 struct got_repository *repo;
1582 got_worktree_status_cb status_cb;
1583 void *status_arg;
1584 got_worktree_cancel_cb cancel_cb;
1585 void *cancel_arg;
1588 static const struct got_error *
1589 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1590 got_worktree_status_cb status_cb, void *status_arg,
1591 struct got_repository *repo)
1593 const struct got_error *err = NULL;
1594 unsigned char status = GOT_STATUS_NO_CHANGE;
1595 struct stat sb;
1596 struct got_object_id id;
1598 err = get_file_status(&status, &sb, ie, abspath, repo);
1599 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1600 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1601 err = (*status_cb)(status_arg, status, ie->path, &id);
1603 return err;
1606 static const struct got_error *
1607 status_old_new(void *arg, struct got_fileindex_entry *ie,
1608 struct dirent *de, const char *parent_path)
1610 const struct got_error *err = NULL;
1611 struct diff_dir_cb_arg *a = arg;
1612 char *abspath;
1614 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1615 return got_error(GOT_ERR_CANCELLED);
1617 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1618 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1619 return NULL;
1621 if (parent_path[0]) {
1622 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1623 parent_path, de->d_name) == -1)
1624 return got_error_prefix_errno("asprintf");
1625 } else {
1626 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1627 de->d_name) == -1)
1628 return got_error_prefix_errno("asprintf");
1631 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1632 a->repo);
1633 free(abspath);
1634 return err;
1637 static const struct got_error *
1638 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1640 struct diff_dir_cb_arg *a = arg;
1641 struct got_object_id id;
1642 unsigned char status;
1644 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1645 return got_error(GOT_ERR_CANCELLED);
1647 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1648 return NULL;
1650 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1651 if (got_fileindex_entry_has_file_on_disk(ie))
1652 status = GOT_STATUS_MISSING;
1653 else
1654 status = GOT_STATUS_DELETE;
1655 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1658 static const struct got_error *
1659 status_new(void *arg, struct dirent *de, const char *parent_path)
1661 const struct got_error *err = NULL;
1662 struct diff_dir_cb_arg *a = arg;
1663 char *path = NULL;
1665 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1666 return got_error(GOT_ERR_CANCELLED);
1668 if (de->d_type == DT_DIR)
1669 return NULL;
1671 /* XXX ignore symlinks for now */
1672 if (de->d_type == DT_LNK)
1673 return NULL;
1675 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1676 return NULL;
1678 if (parent_path[0]) {
1679 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1680 return got_error_prefix_errno("asprintf");
1681 } else {
1682 path = de->d_name;
1685 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1686 NULL);
1687 if (parent_path[0])
1688 free(path);
1689 return err;
1692 const struct got_error *
1693 got_worktree_status(struct got_worktree *worktree, const char *path,
1694 struct got_repository *repo, got_worktree_status_cb status_cb,
1695 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1697 const struct got_error *err = NULL;
1698 DIR *workdir = NULL;
1699 char *fileindex_path = NULL;
1700 struct got_fileindex *fileindex = NULL;
1701 FILE *index = NULL;
1702 struct got_fileindex_diff_dir_cb fdiff_cb;
1703 struct diff_dir_cb_arg arg;
1704 char *ondisk_path = NULL;
1706 fileindex = got_fileindex_alloc();
1707 if (fileindex == NULL) {
1708 err = got_error_prefix_errno("got_fileindex_alloc");
1709 goto done;
1712 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1713 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1714 err = got_error_prefix_errno("asprintf");
1715 fileindex_path = NULL;
1716 goto done;
1719 index = fopen(fileindex_path, "rb");
1720 if (index == NULL) {
1721 if (errno != ENOENT) {
1722 err = got_error_prefix_errno2("fopen", fileindex_path);
1723 goto done;
1725 } else {
1726 err = got_fileindex_read(fileindex, index);
1727 fclose(index);
1728 if (err)
1729 goto done;
1732 if (asprintf(&ondisk_path, "%s%s%s",
1733 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1734 err = got_error_prefix_errno("asprintf");
1735 goto done;
1737 workdir = opendir(ondisk_path);
1738 if (workdir == NULL) {
1739 if (errno == ENOTDIR || errno == ENOENT) {
1740 struct got_fileindex_entry *ie;
1741 ie = got_fileindex_entry_get(fileindex, path);
1742 if (ie == NULL) {
1743 err = got_error(GOT_ERR_BAD_PATH);
1744 goto done;
1746 err = report_file_status(ie, ondisk_path,
1747 status_cb, status_arg, repo);
1748 goto done;
1749 } else {
1750 err = got_error_prefix_errno2("opendir", ondisk_path);
1751 goto done;
1754 fdiff_cb.diff_old_new = status_old_new;
1755 fdiff_cb.diff_old = status_old;
1756 fdiff_cb.diff_new = status_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1759 arg.status_path = path;
1760 arg.status_path_len = strlen(path);
1761 arg.repo = repo;
1762 arg.status_cb = status_cb;
1763 arg.status_arg = status_arg;
1764 arg.cancel_cb = cancel_cb;
1765 arg.cancel_arg = cancel_arg;
1766 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1767 path, repo, &fdiff_cb, &arg);
1768 done:
1769 if (workdir)
1770 closedir(workdir);
1771 free(ondisk_path);
1772 free(fileindex_path);
1773 got_fileindex_free(fileindex);
1774 return err;
1777 const struct got_error *
1778 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1779 const char *arg)
1781 const struct got_error *err = NULL;
1782 char *resolved, *path = NULL;
1783 size_t len;
1785 *wt_path = NULL;
1787 resolved = realpath(arg, NULL);
1788 if (resolved == NULL)
1789 return got_error_prefix_errno2("realpath", arg);
1791 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1792 strlen(got_worktree_get_root_path(worktree)))) {
1793 err = got_error(GOT_ERR_BAD_PATH);
1794 goto done;
1797 path = strdup(resolved +
1798 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1799 if (path == NULL) {
1800 err = got_error_prefix_errno("strdup");
1801 goto done;
1804 /* XXX status walk can't deal with trailing slash! */
1805 len = strlen(path);
1806 while (path[len - 1] == '/') {
1807 path[len - 1] = '\0';
1808 len--;
1810 done:
1811 free(resolved);
1812 if (err == NULL)
1813 *wt_path = path;
1814 else
1815 free(path);
1816 return err;
1819 const struct got_error *
1820 got_worktree_schedule_add(struct got_worktree *worktree,
1821 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1822 struct got_repository *repo)
1824 struct got_fileindex *fileindex = NULL;
1825 struct got_fileindex_entry *ie = NULL;
1826 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1827 FILE *index = NULL, *new_index = NULL;
1828 const struct got_error *err = NULL, *unlockerr = NULL;
1829 int ie_added = 0;
1831 err = lock_worktree(worktree, LOCK_EX);
1832 if (err)
1833 return err;
1835 err = got_path_skip_common_ancestor(&relpath,
1836 got_worktree_get_root_path(worktree), ondisk_path);
1837 if (err)
1838 goto done;
1840 fileindex = got_fileindex_alloc();
1841 if (fileindex == NULL) {
1842 err = got_error_prefix_errno("got_fileindex_alloc");
1843 goto done;
1846 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1847 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1848 err = got_error_prefix_errno("asprintf");
1849 fileindex_path = NULL;
1850 goto done;
1853 index = fopen(fileindex_path, "rb");
1854 if (index == NULL) {
1855 err = got_error_prefix_errno2("fopen", fileindex_path);
1856 goto done;
1859 err = got_fileindex_read(fileindex, index);
1860 if (err)
1861 goto done;
1863 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1864 err = got_error_set_errno(EEXIST);
1865 goto done;
1868 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1869 if (err)
1870 goto done;
1872 err = got_fileindex_entry_add(fileindex, ie);
1873 if (err)
1874 goto done;
1875 ie_added = 1; /* now owned by fileindex; don't free separately */
1877 err = got_opentemp_named(&new_fileindex_path, &new_index,
1878 fileindex_path);
1879 if (err)
1880 goto done;
1882 err = got_fileindex_write(fileindex, new_index);
1883 if (err)
1884 goto done;
1886 if (rename(new_fileindex_path, fileindex_path) != 0) {
1887 err = got_error_prefix_errno3("rename", new_fileindex_path,
1888 fileindex_path);
1889 goto done;
1892 free(new_fileindex_path);
1893 new_fileindex_path = NULL;
1895 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1896 done:
1897 if (index) {
1898 if (fclose(index) != 0 && err == NULL)
1899 err = got_error_prefix_errno("fclose");
1901 if (new_fileindex_path) {
1902 if (unlink(new_fileindex_path) != 0 && err == NULL)
1903 err = got_error_prefix_errno2("unlink",
1904 new_fileindex_path);
1905 free(new_fileindex_path);
1907 if (ie && !ie_added)
1908 got_fileindex_entry_free(ie);
1909 if (fileindex)
1910 got_fileindex_free(fileindex);
1911 unlockerr = lock_worktree(worktree, LOCK_SH);
1912 if (unlockerr && err == NULL)
1913 err = unlockerr;
1914 free(relpath);
1915 return err;
1918 const struct got_error *
1919 got_worktree_schedule_delete(struct got_worktree *worktree,
1920 const char *ondisk_path, int delete_local_mods,
1921 got_worktree_status_cb status_cb, void *status_arg,
1922 struct got_repository *repo)
1924 struct got_fileindex *fileindex = NULL;
1925 struct got_fileindex_entry *ie = NULL;
1926 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1927 FILE *index = NULL, *new_index = NULL;
1928 const struct got_error *err = NULL, *unlockerr = NULL;
1929 unsigned char status;
1930 struct stat sb;
1932 err = lock_worktree(worktree, LOCK_EX);
1933 if (err)
1934 return err;
1936 err = got_path_skip_common_ancestor(&relpath,
1937 got_worktree_get_root_path(worktree), ondisk_path);
1938 if (err)
1939 goto done;
1941 fileindex = got_fileindex_alloc();
1942 if (fileindex == NULL) {
1943 err = got_error_prefix_errno("got_fileindex_alloc");
1944 goto done;
1947 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1948 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1949 err = got_error_prefix_errno("asprintf");
1950 fileindex_path = NULL;
1951 goto done;
1954 index = fopen(fileindex_path, "rb");
1955 if (index == NULL) {
1956 err = got_error_prefix_errno2("fopen", fileindex_path);
1957 goto done;
1960 err = got_fileindex_read(fileindex, index);
1961 if (err)
1962 goto done;
1964 ie = got_fileindex_entry_get(fileindex, relpath);
1965 if (ie == NULL) {
1966 err = got_error(GOT_ERR_BAD_PATH);
1967 goto done;
1970 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1971 if (err)
1972 goto done;
1974 if (status != GOT_STATUS_NO_CHANGE) {
1975 if (status == GOT_STATUS_DELETE) {
1976 err = got_error_set_errno(ENOENT);
1977 goto done;
1979 if (status != GOT_STATUS_MODIFY) {
1980 err = got_error(GOT_ERR_FILE_STATUS);
1981 goto done;
1983 if (!delete_local_mods) {
1984 err = got_error(GOT_ERR_FILE_MODIFIED);
1985 goto done;
1989 if (unlink(ondisk_path) != 0) {
1990 err = got_error_prefix_errno2("unlink", ondisk_path);
1991 goto done;
1994 got_fileindex_entry_mark_deleted_from_disk(ie);
1996 err = got_opentemp_named(&new_fileindex_path, &new_index,
1997 fileindex_path);
1998 if (err)
1999 goto done;
2001 err = got_fileindex_write(fileindex, new_index);
2002 if (err)
2003 goto done;
2005 if (rename(new_fileindex_path, fileindex_path) != 0) {
2006 err = got_error_prefix_errno3("rename", new_fileindex_path,
2007 fileindex_path);
2008 goto done;
2011 free(new_fileindex_path);
2012 new_fileindex_path = NULL;
2014 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2015 done:
2016 free(relpath);
2017 if (index) {
2018 if (fclose(index) != 0 && err == NULL)
2019 err = got_error_prefix_errno("fclose");
2021 if (new_fileindex_path) {
2022 if (unlink(new_fileindex_path) != 0 && err == NULL)
2023 err = got_error_prefix_errno2("unlink",
2024 new_fileindex_path);
2025 free(new_fileindex_path);
2027 if (fileindex)
2028 got_fileindex_free(fileindex);
2029 unlockerr = lock_worktree(worktree, LOCK_SH);
2030 if (unlockerr && err == NULL)
2031 err = unlockerr;
2032 return err;
2035 const struct got_error *
2036 got_worktree_revert(struct got_worktree *worktree,
2037 const char *ondisk_path,
2038 got_worktree_checkout_cb progress_cb, void *progress_arg,
2039 struct got_repository *repo)
2041 struct got_fileindex *fileindex = NULL;
2042 struct got_fileindex_entry *ie = NULL;
2043 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2044 char *tree_path = NULL, *parent_path, *te_name;
2045 FILE *index = NULL, *new_index = NULL;
2046 const struct got_error *err = NULL, *unlockerr = NULL;
2047 struct got_tree_object *tree = NULL;
2048 struct got_object_id id, *tree_id = NULL;
2049 const struct got_tree_entry *te;
2050 struct got_blob_object *blob = NULL;
2051 unsigned char status;
2052 struct stat sb;
2054 err = lock_worktree(worktree, LOCK_EX);
2055 if (err)
2056 return err;
2058 err = got_path_skip_common_ancestor(&relpath,
2059 got_worktree_get_root_path(worktree), ondisk_path);
2060 if (err)
2061 goto done;
2063 fileindex = got_fileindex_alloc();
2064 if (fileindex == NULL) {
2065 err = got_error_prefix_errno("got_fileindex_alloc");
2066 goto done;
2069 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2070 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2071 err = got_error_prefix_errno("asprintf");
2072 fileindex_path = NULL;
2073 goto done;
2076 index = fopen(fileindex_path, "rb");
2077 if (index == NULL) {
2078 err = got_error_prefix_errno2("fopen", fileindex_path);
2079 goto done;
2082 err = got_fileindex_read(fileindex, index);
2083 if (err)
2084 goto done;
2086 ie = got_fileindex_entry_get(fileindex, relpath);
2087 if (ie == NULL) {
2088 err = got_error(GOT_ERR_BAD_PATH);
2089 goto done;
2092 /* Construct in-repository path of tree which contains this blob. */
2093 err = got_path_dirname(&parent_path, ie->path);
2094 if (err) {
2095 if (err->code != GOT_ERR_BAD_PATH)
2096 goto done;
2097 parent_path = "/";
2099 if (got_path_is_root_dir(worktree->path_prefix)) {
2100 tree_path = strdup(parent_path);
2101 if (tree_path == NULL) {
2102 err = got_error_prefix_errno("strdup");
2103 goto done;
2105 } else {
2106 if (got_path_is_root_dir(parent_path)) {
2107 tree_path = strdup(worktree->path_prefix);
2108 if (tree_path == NULL) {
2109 err = got_error_prefix_errno("strdup");
2110 goto done;
2112 } else {
2113 if (asprintf(&tree_path, "%s/%s",
2114 worktree->path_prefix, parent_path) == -1) {
2115 err = got_error_prefix_errno("asprintf");
2116 goto done;
2121 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2122 tree_path);
2123 if (err)
2124 goto done;
2126 err = got_object_open_as_tree(&tree, repo, tree_id);
2127 if (err)
2128 goto done;
2130 te_name = basename(ie->path);
2131 if (te_name == NULL) {
2132 err = got_error_prefix_errno2("basename", ie->path);
2133 goto done;
2136 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2137 if (err)
2138 goto done;
2140 te = got_object_tree_find_entry(tree, te_name);
2141 if (te == NULL && status != GOT_STATUS_ADD) {
2142 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2143 goto done;
2146 switch (status) {
2147 case GOT_STATUS_ADD:
2148 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2149 got_fileindex_entry_remove(fileindex, ie);
2150 break;
2151 case GOT_STATUS_DELETE:
2152 case GOT_STATUS_MODIFY:
2153 case GOT_STATUS_CONFLICT:
2154 case GOT_STATUS_MISSING:
2155 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2156 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2157 if (err)
2158 goto done;
2159 err = install_blob(worktree, ondisk_path, ie->path,
2160 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2161 progress_arg);
2162 if (err)
2163 goto done;
2164 if (status == GOT_STATUS_DELETE) {
2165 err = update_blob_fileindex_entry(worktree,
2166 fileindex, ie, ondisk_path, ie->path, blob, 1);
2167 if (err)
2168 goto done;
2170 break;
2171 default:
2172 goto done;
2175 err = got_opentemp_named(&new_fileindex_path, &new_index,
2176 fileindex_path);
2177 if (err)
2178 goto done;
2180 err = got_fileindex_write(fileindex, new_index);
2181 if (err)
2182 goto done;
2184 if (rename(new_fileindex_path, fileindex_path) != 0) {
2185 err = got_error_prefix_errno3("rename", new_fileindex_path,
2186 fileindex_path);
2187 goto done;
2190 free(new_fileindex_path);
2191 new_fileindex_path = NULL;
2192 done:
2193 free(relpath);
2194 free(tree_path);
2195 if (blob)
2196 got_object_blob_close(blob);
2197 if (tree)
2198 got_object_tree_close(tree);
2199 free(tree_id);
2200 if (index) {
2201 if (fclose(index) != 0 && err == NULL)
2202 err = got_error_prefix_errno("fclose");
2204 if (new_fileindex_path) {
2205 if (unlink(new_fileindex_path) != 0 && err == NULL)
2206 err = got_error_prefix_errno2("unlink",
2207 new_fileindex_path);
2208 free(new_fileindex_path);
2210 if (fileindex)
2211 got_fileindex_free(fileindex);
2212 unlockerr = lock_worktree(worktree, LOCK_SH);
2213 if (unlockerr && err == NULL)
2214 err = unlockerr;
2215 return err;
2218 struct commitable {
2219 char *path;
2220 char *in_repo_path;
2221 char *ondisk_path;
2222 unsigned char status;
2223 struct got_object_id *blob_id;
2224 struct got_object_id *base_id;
2225 mode_t mode;
2228 static void
2229 free_commitable(struct 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 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_prefix_errno("asprintf");
2265 goto done;
2267 if (strcmp(path, "/") == 0) {
2268 parent_path = strdup("");
2269 if (parent_path == NULL)
2270 return got_error_prefix_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_prefix_errno("calloc");
2280 goto done;
2283 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2284 relpath) == -1) {
2285 err = got_error_prefix_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_prefix_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_prefix_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_prefix_errno("got_object_id_dup");
2311 goto done;
2314 ct->path = strdup(path);
2315 if (ct->path == NULL) {
2316 err = got_error_prefix_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_prefix_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 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 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 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_prefix_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 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_prefix_errno("calloc");
2427 ct_name = basename(ct->path);
2428 if (ct_name == NULL) {
2429 err = got_error_prefix_errno2("basename", ct->path);
2430 goto done;
2432 (*new_te)->name = strdup(ct_name);
2433 if ((*new_te)->name == NULL) {
2434 err = got_error_prefix_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_prefix_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 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_prefix_errno("asprintf");
2493 TAILQ_FOREACH(pe, commitable_paths, entry) {
2494 struct 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 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 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_prefix_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 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_prefix_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_prefix_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 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 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_prefix_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 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, const char *logmsg,
2813 got_worktree_status_cb status_cb, void *status_arg,
2814 struct got_repository *repo)
2816 const struct got_error *err = NULL, *unlockerr = NULL;
2817 struct collect_commitables_arg cc_arg;
2818 struct got_pathlist_head commitable_paths;
2819 struct got_pathlist_entry *pe;
2820 char *relpath = NULL;
2821 const char *head_ref_name = NULL;
2822 struct got_reference *head_ref = NULL;
2823 struct got_commit_object *head_commit = NULL;
2824 struct got_object_id *head_commit_id = NULL;
2825 struct got_reference *head_ref2 = NULL;
2826 struct got_object_id *head_commit_id2 = NULL;
2827 struct got_tree_object *head_tree = NULL;
2828 struct got_object_id *new_tree_id = NULL;
2829 struct got_object_id_queue parent_ids;
2830 struct got_object_qid *pid = NULL;
2832 *new_commit_id = NULL;
2834 TAILQ_INIT(&commitable_paths);
2835 SIMPLEQ_INIT(&parent_ids);
2837 if (ondisk_path) {
2838 err = got_path_skip_common_ancestor(&relpath,
2839 worktree->root_path, ondisk_path);
2840 if (err)
2841 return err;
2844 err = lock_worktree(worktree, LOCK_EX);
2845 if (err)
2846 goto done;
2848 err = got_ref_open(&head_ref, repo, worktree->head_ref_name);
2849 if (err)
2850 goto done;
2851 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2852 if (err)
2853 goto done;
2855 cc_arg.commitable_paths = &commitable_paths;
2856 cc_arg.worktree = worktree;
2857 cc_arg.repo = repo;
2858 err = got_worktree_status(worktree, relpath ? relpath : "",
2859 repo, collect_commitables, &cc_arg, NULL, NULL);
2860 if (err)
2861 goto done;
2863 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2864 if (err)
2865 goto done;
2867 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2868 struct commitable *ct = pe->data;
2869 err = check_ct_out_of_date(ct, repo, head_commit_id);
2870 if (err)
2871 goto done;
2874 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2875 if (err)
2876 goto done;
2878 /* TODO: collect commit message if not specified */
2880 /* Create blobs from added and modified files and record their IDs. */
2881 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2882 struct commitable *ct = pe->data;
2883 char *ondisk_path;
2885 if (ct->status != GOT_STATUS_ADD &&
2886 ct->status != GOT_STATUS_MODIFY)
2887 continue;
2889 if (asprintf(&ondisk_path, "%s/%s",
2890 worktree->root_path, pe->path) == -1) {
2891 err = got_error_prefix_errno("asprintf");
2892 goto done;
2894 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2895 free(ondisk_path);
2896 if (err)
2897 goto done;
2900 /* Recursively write new tree objects. */
2901 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2902 status_cb, status_arg, repo);
2903 if (err)
2904 goto done;
2906 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2907 if (err)
2908 goto done;
2909 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2910 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2911 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2912 got_object_qid_free(pid);
2913 if (err)
2914 goto done;
2916 /* Check if a concurrent commit to our branch has occurred. */
2917 /* XXX ideally we'd lock the reference file here to avoid a race */
2918 head_ref_name = got_worktree_get_head_ref_name(worktree);
2919 if (head_ref_name == NULL) {
2920 err = got_error_prefix_errno("got_worktree_get_head_ref_name");
2921 goto done;
2923 err = got_ref_open(&head_ref2, repo, head_ref_name);
2924 if (err)
2925 goto done;
2926 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2927 if (err)
2928 goto done;
2929 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2930 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2931 goto done;
2933 /* Update branch head in repository. */
2934 err = got_ref_change_ref(head_ref, *new_commit_id);
2935 if (err)
2936 goto done;
2937 err = got_ref_write(head_ref, repo);
2938 if (err)
2939 goto done;
2940 /* XXX race has ended here */
2942 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2943 if (err)
2944 goto done;
2946 err = ref_base_commit(worktree, repo);
2947 if (err)
2948 goto done;
2950 err = update_fileindex_after_commit(&commitable_paths,
2951 *new_commit_id, worktree);
2952 if (err)
2953 goto done;
2954 done:
2955 unlockerr = lock_worktree(worktree, LOCK_SH);
2956 if (unlockerr && err == NULL)
2957 err = unlockerr;
2958 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2959 struct commitable *ct = pe->data;
2960 free_commitable(ct);
2962 got_pathlist_free(&commitable_paths);
2963 if (head_tree)
2964 got_object_tree_close(head_tree);
2965 if (head_commit)
2966 got_object_commit_close(head_commit);
2967 free(relpath);
2968 free(head_commit_id);
2969 free(head_commit_id2);
2970 if (head_ref)
2971 got_ref_close(head_ref);
2972 if (head_ref2)
2973 got_ref_close(head_ref2);
2974 return err;