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_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 for (;;) {
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 struct got_pathlist_head *ondisk_paths,
1822 got_worktree_status_cb status_cb, void *status_arg,
1823 struct got_repository *repo)
1825 struct got_fileindex *fileindex = NULL;
1826 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1827 FILE *index = NULL, *new_index = NULL;
1828 const struct got_error *err = NULL, *unlockerr = NULL;
1829 struct got_pathlist_entry *pe;
1831 err = lock_worktree(worktree, LOCK_EX);
1832 if (err)
1833 return err;
1836 fileindex = got_fileindex_alloc();
1837 if (fileindex == NULL) {
1838 err = got_error_prefix_errno("got_fileindex_alloc");
1839 goto done;
1842 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1843 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1844 err = got_error_prefix_errno("asprintf");
1845 fileindex_path = NULL;
1846 goto done;
1849 index = fopen(fileindex_path, "rb");
1850 if (index == NULL) {
1851 err = got_error_prefix_errno2("fopen", fileindex_path);
1852 goto done;
1855 err = got_fileindex_read(fileindex, index);
1856 if (err)
1857 goto done;
1859 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1860 struct got_fileindex_entry *ie = NULL;
1861 char *relpath;
1863 err = got_path_skip_common_ancestor(&relpath,
1864 got_worktree_get_root_path(worktree), pe->path);
1865 if (err)
1866 goto done;
1868 /* Re-adding an existing entry is a no-op. */
1869 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1870 continue;
1872 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1873 NULL, NULL);
1874 free(relpath);
1875 if (err)
1876 goto done;
1878 err = got_fileindex_entry_add(fileindex, ie);
1879 if (err) {
1880 got_fileindex_entry_free(ie);
1881 goto done;
1884 err = report_file_status(ie, pe->path, status_cb, status_arg,
1885 repo);
1886 if (err)
1887 goto done;
1890 err = got_opentemp_named(&new_fileindex_path, &new_index,
1891 fileindex_path);
1892 if (err)
1893 goto done;
1895 err = got_fileindex_write(fileindex, new_index);
1896 if (err)
1897 goto done;
1899 if (rename(new_fileindex_path, fileindex_path) != 0) {
1900 err = got_error_prefix_errno3("rename", new_fileindex_path,
1901 fileindex_path);
1902 goto done;
1905 free(new_fileindex_path);
1906 new_fileindex_path = NULL;
1908 done:
1909 if (index) {
1910 if (fclose(index) != 0 && err == NULL)
1911 err = got_error_prefix_errno("fclose");
1913 if (new_fileindex_path) {
1914 if (unlink(new_fileindex_path) != 0 && err == NULL)
1915 err = got_error_prefix_errno2("unlink",
1916 new_fileindex_path);
1917 free(new_fileindex_path);
1919 if (fileindex)
1920 got_fileindex_free(fileindex);
1921 unlockerr = lock_worktree(worktree, LOCK_SH);
1922 if (unlockerr && err == NULL)
1923 err = unlockerr;
1924 return err;
1927 const struct got_error *
1928 got_worktree_schedule_delete(struct got_worktree *worktree,
1929 const char *ondisk_path, int delete_local_mods,
1930 got_worktree_status_cb status_cb, void *status_arg,
1931 struct got_repository *repo)
1933 struct got_fileindex *fileindex = NULL;
1934 struct got_fileindex_entry *ie = NULL;
1935 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1936 FILE *index = NULL, *new_index = NULL;
1937 const struct got_error *err = NULL, *unlockerr = NULL;
1938 unsigned char status;
1939 struct stat sb;
1941 err = lock_worktree(worktree, LOCK_EX);
1942 if (err)
1943 return err;
1945 err = got_path_skip_common_ancestor(&relpath,
1946 got_worktree_get_root_path(worktree), ondisk_path);
1947 if (err)
1948 goto done;
1950 fileindex = got_fileindex_alloc();
1951 if (fileindex == NULL) {
1952 err = got_error_prefix_errno("got_fileindex_alloc");
1953 goto done;
1956 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1957 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1958 err = got_error_prefix_errno("asprintf");
1959 fileindex_path = NULL;
1960 goto done;
1963 index = fopen(fileindex_path, "rb");
1964 if (index == NULL) {
1965 err = got_error_prefix_errno2("fopen", fileindex_path);
1966 goto done;
1969 err = got_fileindex_read(fileindex, index);
1970 if (err)
1971 goto done;
1973 ie = got_fileindex_entry_get(fileindex, relpath);
1974 if (ie == NULL) {
1975 err = got_error(GOT_ERR_BAD_PATH);
1976 goto done;
1979 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1980 if (err)
1981 goto done;
1983 if (status != GOT_STATUS_NO_CHANGE) {
1984 if (status == GOT_STATUS_DELETE) {
1985 err = got_error_set_errno(ENOENT, ondisk_path);
1986 goto done;
1988 if (status != GOT_STATUS_MODIFY) {
1989 err = got_error(GOT_ERR_FILE_STATUS);
1990 goto done;
1992 if (!delete_local_mods) {
1993 err = got_error(GOT_ERR_FILE_MODIFIED);
1994 goto done;
1998 if (unlink(ondisk_path) != 0) {
1999 err = got_error_prefix_errno2("unlink", ondisk_path);
2000 goto done;
2003 got_fileindex_entry_mark_deleted_from_disk(ie);
2005 err = got_opentemp_named(&new_fileindex_path, &new_index,
2006 fileindex_path);
2007 if (err)
2008 goto done;
2010 err = got_fileindex_write(fileindex, new_index);
2011 if (err)
2012 goto done;
2014 if (rename(new_fileindex_path, fileindex_path) != 0) {
2015 err = got_error_prefix_errno3("rename", new_fileindex_path,
2016 fileindex_path);
2017 goto done;
2020 free(new_fileindex_path);
2021 new_fileindex_path = NULL;
2023 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2024 done:
2025 free(relpath);
2026 if (index) {
2027 if (fclose(index) != 0 && err == NULL)
2028 err = got_error_prefix_errno("fclose");
2030 if (new_fileindex_path) {
2031 if (unlink(new_fileindex_path) != 0 && err == NULL)
2032 err = got_error_prefix_errno2("unlink",
2033 new_fileindex_path);
2034 free(new_fileindex_path);
2036 if (fileindex)
2037 got_fileindex_free(fileindex);
2038 unlockerr = lock_worktree(worktree, LOCK_SH);
2039 if (unlockerr && err == NULL)
2040 err = unlockerr;
2041 return err;
2044 const struct got_error *
2045 got_worktree_revert(struct got_worktree *worktree,
2046 const char *ondisk_path,
2047 got_worktree_checkout_cb progress_cb, void *progress_arg,
2048 struct got_repository *repo)
2050 struct got_fileindex *fileindex = NULL;
2051 struct got_fileindex_entry *ie = NULL;
2052 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2053 char *tree_path = NULL, *parent_path, *te_name;
2054 FILE *index = NULL, *new_index = NULL;
2055 const struct got_error *err = NULL, *unlockerr = NULL;
2056 struct got_tree_object *tree = NULL;
2057 struct got_object_id id, *tree_id = NULL;
2058 const struct got_tree_entry *te;
2059 struct got_blob_object *blob = NULL;
2060 unsigned char status;
2061 struct stat sb;
2063 err = lock_worktree(worktree, LOCK_EX);
2064 if (err)
2065 return err;
2067 err = got_path_skip_common_ancestor(&relpath,
2068 got_worktree_get_root_path(worktree), ondisk_path);
2069 if (err)
2070 goto done;
2072 fileindex = got_fileindex_alloc();
2073 if (fileindex == NULL) {
2074 err = got_error_prefix_errno("got_fileindex_alloc");
2075 goto done;
2078 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2079 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2080 err = got_error_prefix_errno("asprintf");
2081 fileindex_path = NULL;
2082 goto done;
2085 index = fopen(fileindex_path, "rb");
2086 if (index == NULL) {
2087 err = got_error_prefix_errno2("fopen", fileindex_path);
2088 goto done;
2091 err = got_fileindex_read(fileindex, index);
2092 if (err)
2093 goto done;
2095 ie = got_fileindex_entry_get(fileindex, relpath);
2096 if (ie == NULL) {
2097 err = got_error(GOT_ERR_BAD_PATH);
2098 goto done;
2101 /* Construct in-repository path of tree which contains this blob. */
2102 err = got_path_dirname(&parent_path, ie->path);
2103 if (err) {
2104 if (err->code != GOT_ERR_BAD_PATH)
2105 goto done;
2106 parent_path = "/";
2108 if (got_path_is_root_dir(worktree->path_prefix)) {
2109 tree_path = strdup(parent_path);
2110 if (tree_path == NULL) {
2111 err = got_error_prefix_errno("strdup");
2112 goto done;
2114 } else {
2115 if (got_path_is_root_dir(parent_path)) {
2116 tree_path = strdup(worktree->path_prefix);
2117 if (tree_path == NULL) {
2118 err = got_error_prefix_errno("strdup");
2119 goto done;
2121 } else {
2122 if (asprintf(&tree_path, "%s/%s",
2123 worktree->path_prefix, parent_path) == -1) {
2124 err = got_error_prefix_errno("asprintf");
2125 goto done;
2130 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2131 tree_path);
2132 if (err)
2133 goto done;
2135 err = got_object_open_as_tree(&tree, repo, tree_id);
2136 if (err)
2137 goto done;
2139 te_name = basename(ie->path);
2140 if (te_name == NULL) {
2141 err = got_error_prefix_errno2("basename", ie->path);
2142 goto done;
2145 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2146 if (err)
2147 goto done;
2149 te = got_object_tree_find_entry(tree, te_name);
2150 if (te == NULL && status != GOT_STATUS_ADD) {
2151 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2152 goto done;
2155 switch (status) {
2156 case GOT_STATUS_ADD:
2157 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2158 got_fileindex_entry_remove(fileindex, ie);
2159 break;
2160 case GOT_STATUS_DELETE:
2161 case GOT_STATUS_MODIFY:
2162 case GOT_STATUS_CONFLICT:
2163 case GOT_STATUS_MISSING:
2164 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2165 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2166 if (err)
2167 goto done;
2168 err = install_blob(worktree, ondisk_path, ie->path,
2169 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2170 progress_arg);
2171 if (err)
2172 goto done;
2173 if (status == GOT_STATUS_DELETE) {
2174 err = update_blob_fileindex_entry(worktree,
2175 fileindex, ie, ondisk_path, ie->path, blob, 1);
2176 if (err)
2177 goto done;
2179 break;
2180 default:
2181 goto done;
2184 err = got_opentemp_named(&new_fileindex_path, &new_index,
2185 fileindex_path);
2186 if (err)
2187 goto done;
2189 err = got_fileindex_write(fileindex, new_index);
2190 if (err)
2191 goto done;
2193 if (rename(new_fileindex_path, fileindex_path) != 0) {
2194 err = got_error_prefix_errno3("rename", new_fileindex_path,
2195 fileindex_path);
2196 goto done;
2199 free(new_fileindex_path);
2200 new_fileindex_path = NULL;
2201 done:
2202 free(relpath);
2203 free(tree_path);
2204 if (blob)
2205 got_object_blob_close(blob);
2206 if (tree)
2207 got_object_tree_close(tree);
2208 free(tree_id);
2209 if (index) {
2210 if (fclose(index) != 0 && err == NULL)
2211 err = got_error_prefix_errno("fclose");
2213 if (new_fileindex_path) {
2214 if (unlink(new_fileindex_path) != 0 && err == NULL)
2215 err = got_error_prefix_errno2("unlink",
2216 new_fileindex_path);
2217 free(new_fileindex_path);
2219 if (fileindex)
2220 got_fileindex_free(fileindex);
2221 unlockerr = lock_worktree(worktree, LOCK_SH);
2222 if (unlockerr && err == NULL)
2223 err = unlockerr;
2224 return err;
2227 struct commitable {
2228 char *path;
2229 char *in_repo_path;
2230 char *ondisk_path;
2231 unsigned char status;
2232 struct got_object_id *blob_id;
2233 struct got_object_id *base_id;
2234 mode_t mode;
2237 static void
2238 free_commitable(struct commitable *ct)
2240 free(ct->path);
2241 free(ct->in_repo_path);
2242 free(ct->ondisk_path);
2243 free(ct->blob_id);
2244 free(ct->base_id);
2245 free(ct);
2248 struct collect_commitables_arg {
2249 struct got_pathlist_head *commitable_paths;
2250 struct got_repository *repo;
2251 struct got_worktree *worktree;
2254 static const struct got_error *
2255 collect_commitables(void *arg, unsigned char status, const char *relpath,
2256 struct got_object_id *id)
2258 struct collect_commitables_arg *a = arg;
2259 const struct got_error *err = NULL;
2260 struct commitable *ct = NULL;
2261 struct got_pathlist_entry *new = NULL;
2262 char *parent_path = NULL, *path = NULL;
2263 struct stat sb;
2265 if (status == GOT_STATUS_CONFLICT)
2266 return got_error(GOT_ERR_COMMIT_CONFLICT);
2268 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2269 status != GOT_STATUS_DELETE)
2270 return NULL;
2272 if (asprintf(&path, "/%s", relpath) == -1) {
2273 err = got_error_prefix_errno("asprintf");
2274 goto done;
2276 if (strcmp(path, "/") == 0) {
2277 parent_path = strdup("");
2278 if (parent_path == NULL)
2279 return got_error_prefix_errno("strdup");
2280 } else {
2281 err = got_path_dirname(&parent_path, path);
2282 if (err)
2283 return err;
2286 ct = calloc(1, sizeof(*ct));
2287 if (ct == NULL) {
2288 err = got_error_prefix_errno("calloc");
2289 goto done;
2292 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2293 relpath) == -1) {
2294 err = got_error_prefix_errno("asprintf");
2295 goto done;
2297 if (status == GOT_STATUS_DELETE) {
2298 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2299 } else {
2300 if (lstat(ct->ondisk_path, &sb) != 0) {
2301 err = got_error_prefix_errno2("lstat", ct->ondisk_path);
2302 goto done;
2304 ct->mode = sb.st_mode;
2307 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2308 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2309 relpath) == -1) {
2310 err = got_error_prefix_errno("asprintf");
2311 goto done;
2314 ct->status = status;
2315 ct->blob_id = NULL; /* will be filled in when blob gets created */
2316 if (ct->status != GOT_STATUS_ADD) {
2317 ct->base_id = got_object_id_dup(id);
2318 if (ct->base_id == NULL) {
2319 err = got_error_prefix_errno("got_object_id_dup");
2320 goto done;
2323 ct->path = strdup(path);
2324 if (ct->path == NULL) {
2325 err = got_error_prefix_errno("strdup");
2326 goto done;
2328 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2329 done:
2330 if (ct && (err || new == NULL))
2331 free_commitable(ct);
2332 free(parent_path);
2333 free(path);
2334 return err;
2337 static const struct got_error *write_tree(struct got_object_id **,
2338 struct got_tree_object *, const char *, struct got_pathlist_head *,
2339 got_worktree_status_cb status_cb, void *status_arg,
2340 struct got_repository *);
2342 static const struct got_error *
2343 write_subtree(struct got_object_id **new_subtree_id,
2344 struct got_tree_entry *te, const char *parent_path,
2345 struct got_pathlist_head *commitable_paths,
2346 got_worktree_status_cb status_cb, void *status_arg,
2347 struct got_repository *repo)
2349 const struct got_error *err = NULL;
2350 struct got_tree_object *subtree;
2351 char *subpath;
2353 if (asprintf(&subpath, "%s%s%s", parent_path,
2354 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2355 return got_error_prefix_errno("asprintf");
2357 err = got_object_open_as_tree(&subtree, repo, te->id);
2358 if (err)
2359 return err;
2361 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2362 status_cb, status_arg, repo);
2363 got_object_tree_close(subtree);
2364 free(subpath);
2365 return err;
2368 static const struct got_error *
2369 match_ct_parent_path(int *match, struct commitable *ct, const char *path)
2371 const struct got_error *err = NULL;
2372 char *ct_parent_path = NULL;
2374 *match = 0;
2376 if (strchr(ct->path, '/') == NULL) {
2377 *match = got_path_is_root_dir(path);
2378 return NULL;
2381 err = got_path_dirname(&ct_parent_path, ct->path);
2382 if (err)
2383 return err;
2384 *match = (strcmp(path, ct_parent_path) == 0);
2385 free(ct_parent_path);
2386 return err;
2389 static mode_t
2390 get_ct_file_mode(struct commitable *ct)
2392 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2395 static const struct got_error *
2396 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2397 struct got_tree_entry *te, struct commitable *ct)
2399 const struct got_error *err = NULL;
2401 *new_te = NULL;
2403 err = got_object_tree_entry_dup(new_te, te);
2404 if (err)
2405 goto done;
2407 (*new_te)->mode = get_ct_file_mode(ct);
2409 free((*new_te)->id);
2410 (*new_te)->id = got_object_id_dup(ct->blob_id);
2411 if ((*new_te)->id == NULL) {
2412 err = got_error_prefix_errno("got_object_id_dup");
2413 goto done;
2415 done:
2416 if (err && *new_te) {
2417 got_object_tree_entry_close(*new_te);
2418 *new_te = NULL;
2420 return err;
2423 static const struct got_error *
2424 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2425 struct commitable *ct)
2427 const struct got_error *err = NULL;
2428 char *ct_name;
2430 *new_te = NULL;
2432 *new_te = calloc(1, sizeof(**new_te));
2433 if (*new_te == NULL)
2434 return got_error_prefix_errno("calloc");
2436 ct_name = basename(ct->path);
2437 if (ct_name == NULL) {
2438 err = got_error_prefix_errno2("basename", ct->path);
2439 goto done;
2441 (*new_te)->name = strdup(ct_name);
2442 if ((*new_te)->name == NULL) {
2443 err = got_error_prefix_errno("strdup");
2444 goto done;
2447 (*new_te)->mode = get_ct_file_mode(ct);
2449 (*new_te)->id = got_object_id_dup(ct->blob_id);
2450 if ((*new_te)->id == NULL) {
2451 err = got_error_prefix_errno("got_object_id_dup");
2452 goto done;
2454 done:
2455 if (err && *new_te) {
2456 got_object_tree_entry_close(*new_te);
2457 *new_te = NULL;
2459 return err;
2462 static const struct got_error *
2463 insert_tree_entry(struct got_tree_entry *new_te,
2464 struct got_pathlist_head *paths)
2466 const struct got_error *err = NULL;
2467 struct got_pathlist_entry *new_pe;
2469 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2470 if (err)
2471 return err;
2472 if (new_pe == NULL)
2473 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2474 return NULL;
2477 static const struct got_error *
2478 report_ct_status(struct commitable *ct,
2479 got_worktree_status_cb status_cb, void *status_arg)
2481 const char *ct_path = ct->path;
2482 while (ct_path[0] == '/')
2483 ct_path++;
2484 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id);
2487 static const struct got_error *
2488 match_modified_subtree(int *modified, struct got_tree_entry *te,
2489 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2491 const struct got_error *err = NULL;
2492 struct got_pathlist_entry *pe;
2493 char *te_path;
2495 *modified = 0;
2497 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2498 got_path_is_root_dir(base_tree_path) ? "" : "/",
2499 te->name) == -1)
2500 return got_error_prefix_errno("asprintf");
2502 TAILQ_FOREACH(pe, commitable_paths, entry) {
2503 struct commitable *ct = pe->data;
2504 *modified = got_path_is_child(ct->in_repo_path, te_path,
2505 strlen(te_path));
2506 if (*modified)
2507 break;
2510 free(te_path);
2511 return err;
2514 static const struct got_error *
2515 match_deleted_or_modified_ct(struct commitable **ctp,
2516 struct got_tree_entry *te, const char *base_tree_path,
2517 struct got_pathlist_head *commitable_paths)
2519 const struct got_error *err = NULL;
2520 struct got_pathlist_entry *pe;
2522 *ctp = NULL;
2524 TAILQ_FOREACH(pe, commitable_paths, entry) {
2525 struct commitable *ct = pe->data;
2526 char *ct_name = NULL;
2527 int path_matches;
2529 if (ct->status != GOT_STATUS_MODIFY &&
2530 ct->status != GOT_STATUS_DELETE)
2531 continue;
2533 if (got_object_id_cmp(ct->base_id, te->id) != 0)
2534 continue;
2536 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2537 if (err)
2538 return err;
2539 if (!path_matches)
2540 continue;
2542 ct_name = basename(pe->path);
2543 if (ct_name == NULL)
2544 return got_error_prefix_errno2("basename", pe->path);
2546 if (strcmp(te->name, ct_name) != 0)
2547 continue;
2549 *ctp = ct;
2550 break;
2553 return err;
2556 static const struct got_error *
2557 write_tree(struct got_object_id **new_tree_id,
2558 struct got_tree_object *base_tree, const char *path_base_tree,
2559 struct got_pathlist_head *commitable_paths,
2560 got_worktree_status_cb status_cb, void *status_arg,
2561 struct got_repository *repo)
2563 const struct got_error *err = NULL;
2564 const struct got_tree_entries *base_entries = NULL;
2565 struct got_pathlist_head paths;
2566 struct got_tree_entries new_tree_entries;
2567 struct got_tree_entry *te, *new_te = NULL;
2568 struct got_pathlist_entry *pe;
2570 TAILQ_INIT(&paths);
2571 new_tree_entries.nentries = 0;
2572 SIMPLEQ_INIT(&new_tree_entries.head);
2574 /* Insert, and recurse into, newly added entries first. */
2575 TAILQ_FOREACH(pe, commitable_paths, entry) {
2576 struct commitable *ct = pe->data;
2577 char *child_path = NULL, *slash;
2579 if (ct->status != GOT_STATUS_ADD)
2580 continue;
2582 if (!got_path_is_child(pe->path, path_base_tree,
2583 strlen(path_base_tree)))
2584 continue;
2586 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2587 pe->path);
2588 if (err)
2589 goto done;
2591 slash = strchr(child_path, '/');
2592 if (slash == NULL) {
2593 err = alloc_added_blob_tree_entry(&new_te, ct);
2594 if (err)
2595 goto done;
2596 err = report_ct_status(ct, status_cb, status_arg);
2597 if (err)
2598 goto done;
2599 } else {
2600 char *subtree_path;
2601 struct got_pathlist_entry *pe2;
2602 int visited = 0;
2604 *slash = '\0'; /* trim trailing path components */
2605 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2606 got_path_is_root_dir(path_base_tree) ? "" : "/",
2607 child_path) == -1) {
2608 err = got_error_prefix_errno("asprintf");
2609 goto done;
2611 TAILQ_FOREACH(pe2, &paths, entry) {
2612 if (got_path_cmp(subtree_path, pe2->path) != 0)
2613 continue;
2614 visited = 1;
2615 break;
2617 if (visited) {
2618 free(subtree_path);
2619 continue;
2622 new_te = calloc(1, sizeof(*new_te));
2623 new_te->mode = S_IFDIR;
2624 new_te->name = strdup(child_path);
2625 if (new_te->name == NULL) {
2626 err = got_error_prefix_errno("strdup");
2627 got_object_tree_entry_close(new_te);
2628 new_te = NULL;
2629 goto done;
2631 err = write_tree(&new_te->id, NULL, subtree_path,
2632 commitable_paths, status_cb, status_arg, repo);
2633 free(subtree_path);
2634 if (err) {
2635 got_object_tree_entry_close(new_te);
2636 new_te = NULL;
2637 goto done;
2640 err = insert_tree_entry(new_te, &paths);
2641 if (err)
2642 goto done;
2645 if (base_tree) {
2646 /* Handle modified and deleted entries. */
2647 base_entries = got_object_tree_get_entries(base_tree);
2648 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2649 struct commitable *ct = NULL;
2651 if (S_ISDIR(te->mode)) {
2652 int modified;
2653 err = got_object_tree_entry_dup(&new_te, te);
2654 if (err)
2655 goto done;
2656 err = match_modified_subtree(&modified, te,
2657 path_base_tree, commitable_paths);
2658 if (err)
2659 goto done;
2660 /* Avoid recursion into unmodified subtrees. */
2661 if (modified) {
2662 free(new_te->id);
2663 err = write_subtree(&new_te->id, te,
2664 path_base_tree, commitable_paths,
2665 status_cb, status_arg, repo);
2666 if (err)
2667 goto done;
2669 err = insert_tree_entry(new_te, &paths);
2670 if (err)
2671 goto done;
2672 continue;
2675 err = match_deleted_or_modified_ct(&ct, te,
2676 path_base_tree, commitable_paths);
2677 if (ct) {
2678 /* NB: Deleted entries get dropped here. */
2679 if (ct->status == GOT_STATUS_MODIFY) {
2680 err = alloc_modified_blob_tree_entry(
2681 &new_te, te, ct);
2682 if (err)
2683 goto done;
2684 err = insert_tree_entry(new_te, &paths);
2685 if (err)
2686 goto done;
2688 err = report_ct_status(ct, status_cb, status_arg);
2689 if (err)
2690 goto done;
2691 } else {
2692 /* Entry is unchanged; just copy it. */
2693 err = got_object_tree_entry_dup(&new_te, te);
2694 if (err)
2695 goto done;
2696 err = insert_tree_entry(new_te, &paths);
2697 if (err)
2698 goto done;
2703 /* Write new list of entries; deleted entries have been dropped. */
2704 TAILQ_FOREACH(pe, &paths, entry) {
2705 struct got_tree_entry *te = pe->data;
2706 new_tree_entries.nentries++;
2707 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2709 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2710 done:
2711 got_object_tree_entries_close(&new_tree_entries);
2712 got_pathlist_free(&paths);
2713 return err;
2716 static const struct got_error *
2717 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2718 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2720 const struct got_error *err = NULL;
2721 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2722 struct got_fileindex *fileindex = NULL;
2723 FILE *new_index = NULL;
2724 struct got_pathlist_entry *pe;
2726 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2727 if (err)
2728 return err;
2730 err = got_opentemp_named(&new_fileindex_path, &new_index,
2731 fileindex_path);
2732 if (err)
2733 goto done;
2735 TAILQ_FOREACH(pe, commitable_paths, entry) {
2736 struct got_fileindex_entry *ie;
2737 struct commitable *ct = pe->data;
2739 ie = got_fileindex_entry_get(fileindex, pe->path);
2740 if (ie) {
2741 if (ct->status == GOT_STATUS_DELETE) {
2742 got_fileindex_entry_remove(fileindex, ie);
2743 got_fileindex_entry_free(ie);
2744 } else
2745 err = got_fileindex_entry_update(ie,
2746 ct->ondisk_path, ct->blob_id->sha1,
2747 new_base_commit_id->sha1, 1);
2748 } else {
2749 err = got_fileindex_entry_alloc(&ie,
2750 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2751 new_base_commit_id->sha1);
2752 if (err)
2753 goto done;
2754 err = got_fileindex_entry_add(fileindex, ie);
2755 if (err)
2756 goto done;
2760 err = got_fileindex_write(fileindex, new_index);
2761 if (err)
2762 goto done;
2764 if (rename(new_fileindex_path, fileindex_path) != 0) {
2765 err = got_error_prefix_errno3("rename", new_fileindex_path,
2766 fileindex_path);
2767 unlink(new_fileindex_path);
2768 goto done;
2771 free(new_fileindex_path);
2772 new_fileindex_path = NULL;
2774 done:
2775 if (new_fileindex_path)
2776 unlink(new_fileindex_path);
2777 if (new_index)
2778 fclose(new_index);
2779 free(new_fileindex_path);
2780 free(fileindex_path);
2781 got_fileindex_free(fileindex);
2782 return err;
2785 static const struct got_error *
2786 check_ct_out_of_date(struct commitable *ct, struct got_repository *repo,
2787 struct got_object_id *head_commit_id)
2789 const struct got_error *err = NULL;
2790 struct got_object_id *id_in_head;
2793 * XXX This should probably be checking each commit from
2794 * worktree's base_commit -> head and verify that the
2795 * same blob exists in each of these commits.
2796 * Removals+additions within this line of history could mean
2797 * that renames have occured in which case we should really
2798 * be forcing the user to run an update...
2800 err = got_object_id_by_path(&id_in_head, repo,
2801 head_commit_id, ct->in_repo_path);
2802 if (err) {
2803 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2804 return err;
2805 if (ct->status != GOT_STATUS_ADD)
2806 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2807 err = NULL;
2808 id_in_head = NULL;
2811 if (id_in_head && got_object_id_cmp(id_in_head, ct->base_id) != 0)
2812 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2814 free(id_in_head);
2815 return err;
2818 const struct got_error *
2819 got_worktree_commit(struct got_object_id **new_commit_id,
2820 struct got_worktree *worktree, const char *ondisk_path,
2821 const char *author, const char *committer, const char *logmsg,
2822 got_worktree_status_cb status_cb, void *status_arg,
2823 struct got_repository *repo)
2825 const struct got_error *err = NULL, *unlockerr = NULL;
2826 struct collect_commitables_arg cc_arg;
2827 struct got_pathlist_head commitable_paths;
2828 struct got_pathlist_entry *pe;
2829 char *relpath = NULL;
2830 const char *head_ref_name = NULL;
2831 struct got_reference *head_ref = NULL;
2832 struct got_commit_object *head_commit = NULL;
2833 struct got_object_id *head_commit_id = NULL;
2834 struct got_reference *head_ref2 = NULL;
2835 struct got_object_id *head_commit_id2 = NULL;
2836 struct got_tree_object *head_tree = NULL;
2837 struct got_object_id *new_tree_id = NULL;
2838 struct got_object_id_queue parent_ids;
2839 struct got_object_qid *pid = NULL;
2841 *new_commit_id = NULL;
2843 TAILQ_INIT(&commitable_paths);
2844 SIMPLEQ_INIT(&parent_ids);
2846 if (ondisk_path) {
2847 err = got_path_skip_common_ancestor(&relpath,
2848 worktree->root_path, ondisk_path);
2849 if (err)
2850 return err;
2853 err = lock_worktree(worktree, LOCK_EX);
2854 if (err)
2855 goto done;
2857 err = got_ref_open(&head_ref, repo, worktree->head_ref_name);
2858 if (err)
2859 goto done;
2860 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2861 if (err)
2862 goto done;
2864 cc_arg.commitable_paths = &commitable_paths;
2865 cc_arg.worktree = worktree;
2866 cc_arg.repo = repo;
2867 err = got_worktree_status(worktree, relpath ? relpath : "",
2868 repo, collect_commitables, &cc_arg, NULL, NULL);
2869 if (err)
2870 goto done;
2872 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2873 if (err)
2874 goto done;
2876 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2877 struct commitable *ct = pe->data;
2878 err = check_ct_out_of_date(ct, repo, head_commit_id);
2879 if (err)
2880 goto done;
2883 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2884 if (err)
2885 goto done;
2887 /* TODO: collect commit message if not specified */
2889 /* Create blobs from added and modified files and record their IDs. */
2890 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2891 struct commitable *ct = pe->data;
2892 char *ondisk_path;
2894 if (ct->status != GOT_STATUS_ADD &&
2895 ct->status != GOT_STATUS_MODIFY)
2896 continue;
2898 if (asprintf(&ondisk_path, "%s/%s",
2899 worktree->root_path, pe->path) == -1) {
2900 err = got_error_prefix_errno("asprintf");
2901 goto done;
2903 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2904 free(ondisk_path);
2905 if (err)
2906 goto done;
2909 /* Recursively write new tree objects. */
2910 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2911 status_cb, status_arg, repo);
2912 if (err)
2913 goto done;
2915 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2916 if (err)
2917 goto done;
2918 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2919 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2920 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2921 got_object_qid_free(pid);
2922 if (err)
2923 goto done;
2925 /* Check if a concurrent commit to our branch has occurred. */
2926 /* XXX ideally we'd lock the reference file here to avoid a race */
2927 head_ref_name = got_worktree_get_head_ref_name(worktree);
2928 if (head_ref_name == NULL) {
2929 err = got_error_prefix_errno("got_worktree_get_head_ref_name");
2930 goto done;
2932 err = got_ref_open(&head_ref2, repo, head_ref_name);
2933 if (err)
2934 goto done;
2935 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2936 if (err)
2937 goto done;
2938 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2939 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2940 goto done;
2942 /* Update branch head in repository. */
2943 err = got_ref_change_ref(head_ref, *new_commit_id);
2944 if (err)
2945 goto done;
2946 err = got_ref_write(head_ref, repo);
2947 if (err)
2948 goto done;
2949 /* XXX race has ended here */
2951 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2952 if (err)
2953 goto done;
2955 err = ref_base_commit(worktree, repo);
2956 if (err)
2957 goto done;
2959 err = update_fileindex_after_commit(&commitable_paths,
2960 *new_commit_id, worktree);
2961 if (err)
2962 goto done;
2963 done:
2964 unlockerr = lock_worktree(worktree, LOCK_SH);
2965 if (unlockerr && err == NULL)
2966 err = unlockerr;
2967 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2968 struct commitable *ct = pe->data;
2969 free_commitable(ct);
2971 got_pathlist_free(&commitable_paths);
2972 if (head_tree)
2973 got_object_tree_close(head_tree);
2974 if (head_commit)
2975 got_object_commit_close(head_commit);
2976 free(relpath);
2977 free(head_commit_id);
2978 free(head_commit_id2);
2979 if (head_ref)
2980 got_ref_close(head_ref);
2981 if (head_ref2)
2982 got_ref_close(head_ref2);
2983 return err;