Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
34 #include <uuid.h>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 static const struct got_error *
61 create_meta_file(const char *path_got, const char *name, const char *content)
62 {
63 const struct got_error *err = NULL;
64 char *path;
65 int fd = -1;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 err = got_error_from_errno("asprintf");
69 path = NULL;
70 goto done;
71 }
73 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 GOT_DEFAULT_FILE_MODE);
75 if (fd == -1) {
76 err = got_error_from_errno2("open", path);
77 goto done;
78 }
80 if (content) {
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_from_errno("dprintf");
84 goto done;
85 }
86 }
88 done:
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno("close");
91 free(path);
92 return err;
93 }
95 static const struct got_error *
96 update_meta_file(const char *path_got, const char *name, const char *content)
97 {
98 const struct got_error *err = NULL;
99 FILE *tmpfile = NULL;
100 char *tmppath = NULL;
101 char *path = NULL;
103 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 err = got_error_from_errno("asprintf");
105 path = NULL;
106 goto done;
109 err = got_opentemp_named(&tmppath, &tmpfile, path);
110 if (err)
111 goto done;
113 if (content) {
114 int len = fprintf(tmpfile, "%s\n", content);
115 if (len != strlen(content) + 1) {
116 err = got_error_from_errno2("fprintf", tmppath);
117 goto done;
121 if (rename(tmppath, path) != 0) {
122 err = got_error_from_errno3("rename", tmppath, path);
123 unlink(tmppath);
124 goto done;
127 done:
128 if (fclose(tmpfile) != 0 && err == NULL)
129 err = got_error_from_errno2("fclose", tmppath);
130 free(tmppath);
131 return err;
134 static const struct got_error *
135 read_meta_file(char **content, const char *path_got, const char *name)
137 const struct got_error *err = NULL;
138 char *path;
139 int fd = -1;
140 ssize_t n;
141 struct stat sb;
143 *content = NULL;
145 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 err = got_error_from_errno("asprintf");
147 path = NULL;
148 goto done;
151 fd = open(path, O_RDONLY | O_NOFOLLOW);
152 if (fd == -1) {
153 if (errno == ENOENT)
154 err = got_error(GOT_ERR_WORKTREE_META);
155 else
156 err = got_error_from_errno2("open", path);
157 goto done;
159 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 : got_error_from_errno2("flock", path));
162 goto done;
165 if (lstat(path, &sb) != 0) {
166 err = got_error_from_errno2("lstat", path);
167 goto done;
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_from_errno("calloc");
172 goto done;
175 n = read(fd, *content, sb.st_size);
176 if (n != sb.st_size) {
177 err = (n == -1 ? got_error_from_errno2("read", path) :
178 got_error(GOT_ERR_WORKTREE_META));
179 goto done;
181 if ((*content)[sb.st_size - 1] != '\n') {
182 err = got_error(GOT_ERR_WORKTREE_META);
183 goto done;
185 (*content)[sb.st_size - 1] = '\0';
187 done:
188 if (fd != -1 && close(fd) == -1 && err == NULL)
189 err = got_error_from_errno2("close", path_got);
190 free(path);
191 if (err) {
192 free(*content);
193 *content = NULL;
195 return err;
198 static const struct got_error *
199 write_head_ref(const char *path_got, struct got_reference *head_ref)
201 const struct got_error *err = NULL;
202 char *refstr = NULL;
204 if (got_ref_is_symbolic(head_ref)) {
205 refstr = got_ref_to_str(head_ref);
206 if (refstr == NULL)
207 return got_error_from_errno("got_ref_to_str");
208 } else {
209 refstr = strdup(got_ref_get_name(head_ref));
210 if (refstr == NULL)
211 return got_error_from_errno("strdup");
213 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
214 free(refstr);
215 return err;
218 const struct got_error *
219 got_worktree_init(const char *path, struct got_reference *head_ref,
220 const char *prefix, struct got_repository *repo)
222 const struct got_error *err = NULL;
223 struct got_object_id *commit_id = NULL;
224 uuid_t uuid;
225 uint32_t uuid_status;
226 int obj_type;
227 char *path_got = NULL;
228 char *formatstr = NULL;
229 char *absprefix = NULL;
230 char *basestr = NULL;
231 char *uuidstr = NULL;
233 if (strcmp(path, got_repo_get_path(repo)) == 0) {
234 err = got_error(GOT_ERR_WORKTREE_REPO);
235 goto done;
238 err = got_ref_resolve(&commit_id, repo, head_ref);
239 if (err)
240 return err;
241 err = got_object_get_type(&obj_type, repo, commit_id);
242 if (err)
243 return err;
244 if (obj_type != GOT_OBJ_TYPE_COMMIT)
245 return got_error(GOT_ERR_OBJ_TYPE);
247 if (!got_path_is_absolute(prefix)) {
248 if (asprintf(&absprefix, "/%s", prefix) == -1)
249 return got_error_from_errno("asprintf");
252 /* Create top-level directory (may already exist). */
253 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
254 err = got_error_from_errno2("mkdir", path);
255 goto done;
258 /* Create .got directory (may already exist). */
259 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
260 err = got_error_from_errno("asprintf");
261 goto done;
263 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
264 err = got_error_from_errno2("mkdir", path_got);
265 goto done;
268 /* Create an empty lock file. */
269 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
270 if (err)
271 goto done;
273 /* Create an empty file index. */
274 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
275 if (err)
276 goto done;
278 /* Write the HEAD reference. */
279 err = write_head_ref(path_got, head_ref);
280 if (err)
281 goto done;
283 /* Record our base commit. */
284 err = got_object_id_str(&basestr, commit_id);
285 if (err)
286 goto done;
287 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
288 if (err)
289 goto done;
291 /* Store path to repository. */
292 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
293 got_repo_get_path(repo));
294 if (err)
295 goto done;
297 /* Store in-repository path prefix. */
298 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
299 absprefix ? absprefix : prefix);
300 if (err)
301 goto done;
303 /* Generate UUID. */
304 uuid_create(&uuid, &uuid_status);
305 if (uuid_status != uuid_s_ok) {
306 err = got_error_uuid(uuid_status);
307 goto done;
309 uuid_to_string(&uuid, &uuidstr, &uuid_status);
310 if (uuid_status != uuid_s_ok) {
311 err = got_error_uuid(uuid_status);
312 goto done;
314 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
315 if (err)
316 goto done;
318 /* Stamp work tree with format file. */
319 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
320 err = got_error_from_errno("asprintf");
321 goto done;
323 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
324 if (err)
325 goto done;
327 done:
328 free(commit_id);
329 free(path_got);
330 free(formatstr);
331 free(absprefix);
332 free(basestr);
333 free(uuidstr);
334 return err;
337 static const struct got_error *
338 open_worktree(struct got_worktree **worktree, const char *path)
340 const struct got_error *err = NULL;
341 char *path_got;
342 char *formatstr = NULL;
343 char *uuidstr = NULL;
344 char *path_lock = NULL;
345 char *base_commit_id_str = NULL;
346 int version, fd = -1;
347 const char *errstr;
348 struct got_repository *repo = NULL;
349 uint32_t uuid_status;
351 *worktree = NULL;
353 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
354 err = got_error_from_errno("asprintf");
355 path_got = NULL;
356 goto done;
359 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
360 err = got_error_from_errno("asprintf");
361 path_lock = NULL;
362 goto done;
365 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
366 if (fd == -1) {
367 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
368 : got_error_from_errno2("open", path_lock));
369 goto done;
372 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
373 if (err)
374 goto done;
376 version = strtonum(formatstr, 1, INT_MAX, &errstr);
377 if (errstr) {
378 err = got_error(GOT_ERR_WORKTREE_META);
379 goto done;
381 if (version != GOT_WORKTREE_FORMAT_VERSION) {
382 err = got_error(GOT_ERR_WORKTREE_VERS);
383 goto done;
386 *worktree = calloc(1, sizeof(**worktree));
387 if (*worktree == NULL) {
388 err = got_error_from_errno("calloc");
389 goto done;
391 (*worktree)->lockfd = -1;
393 (*worktree)->root_path = strdup(path);
394 if ((*worktree)->root_path == NULL) {
395 err = got_error_from_errno("strdup");
396 goto done;
398 err = read_meta_file(&(*worktree)->repo_path, path_got,
399 GOT_WORKTREE_REPOSITORY);
400 if (err)
401 goto done;
403 err = read_meta_file(&(*worktree)->path_prefix, path_got,
404 GOT_WORKTREE_PATH_PREFIX);
405 if (err)
406 goto done;
408 err = read_meta_file(&base_commit_id_str, path_got,
409 GOT_WORKTREE_BASE_COMMIT);
410 if (err)
411 goto done;
413 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
414 if (err)
415 goto done;
416 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
417 if (uuid_status != uuid_s_ok) {
418 err = got_error_uuid(uuid_status);
419 goto done;
422 err = got_repo_open(&repo, (*worktree)->repo_path);
423 if (err)
424 goto done;
426 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
427 base_commit_id_str);
428 if (err)
429 goto done;
431 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
432 GOT_WORKTREE_HEAD_REF);
433 done:
434 if (repo)
435 got_repo_close(repo);
436 free(path_got);
437 free(path_lock);
438 free(base_commit_id_str);
439 free(uuidstr);
440 free(formatstr);
441 if (err) {
442 if (fd != -1)
443 close(fd);
444 if (*worktree != NULL)
445 got_worktree_close(*worktree);
446 *worktree = NULL;
447 } else
448 (*worktree)->lockfd = fd;
450 return err;
453 const struct got_error *
454 got_worktree_open(struct got_worktree **worktree, const char *path)
456 const struct got_error *err = NULL;
458 do {
459 err = open_worktree(worktree, path);
460 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
461 return err;
462 if (*worktree)
463 return NULL;
464 path = dirname(path);
465 if (path == NULL)
466 return got_error_from_errno2("dirname", path);
467 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
469 return got_error(GOT_ERR_NOT_WORKTREE);
472 const struct got_error *
473 got_worktree_close(struct got_worktree *worktree)
475 const struct got_error *err = NULL;
476 free(worktree->root_path);
477 free(worktree->repo_path);
478 free(worktree->path_prefix);
479 free(worktree->base_commit_id);
480 free(worktree->head_ref_name);
481 if (worktree->lockfd != -1)
482 if (close(worktree->lockfd) != 0)
483 err = got_error_from_errno2("close",
484 got_worktree_get_root_path(worktree));
485 free(worktree);
486 return err;
489 const char *
490 got_worktree_get_root_path(struct got_worktree *worktree)
492 return worktree->root_path;
495 const char *
496 got_worktree_get_repo_path(struct got_worktree *worktree)
498 return worktree->repo_path;
501 const char *
502 got_worktree_get_path_prefix(struct got_worktree *worktree)
504 return worktree->path_prefix;
507 const struct got_error *
508 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 const char *path_prefix)
511 char *absprefix = NULL;
513 if (!got_path_is_absolute(path_prefix)) {
514 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 return got_error_from_errno("asprintf");
517 *match = (strcmp(absprefix ? absprefix : path_prefix,
518 worktree->path_prefix) == 0);
519 free(absprefix);
520 return NULL;
523 const char *
524 got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 return worktree->head_ref_name;
529 const struct got_error *
530 got_worktree_set_head_ref(struct got_worktree *worktree,
531 struct got_reference *head_ref)
533 const struct got_error *err = NULL;
534 char *path_got = NULL, *head_ref_name = NULL;
536 if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 GOT_WORKTREE_GOT_DIR) == -1) {
538 err = got_error_from_errno("asprintf");
539 path_got = NULL;
540 goto done;
543 head_ref_name = strdup(got_ref_get_name(head_ref));
544 if (head_ref_name == NULL) {
545 err = got_error_from_errno("strdup");
546 goto done;
549 err = write_head_ref(path_got, head_ref);
550 if (err)
551 goto done;
553 free(worktree->head_ref_name);
554 worktree->head_ref_name = head_ref_name;
555 done:
556 free(path_got);
557 if (err)
558 free(head_ref_name);
559 return err;
562 struct got_object_id *
563 got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 return worktree->base_commit_id;
568 const struct got_error *
569 got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 struct got_repository *repo, struct got_object_id *commit_id)
572 const struct got_error *err;
573 struct got_object *obj = NULL;
574 char *id_str = NULL;
575 char *path_got = NULL;
577 if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 GOT_WORKTREE_GOT_DIR) == -1) {
579 err = got_error_from_errno("asprintf");
580 path_got = NULL;
581 goto done;
584 err = got_object_open(&obj, repo, commit_id);
585 if (err)
586 return err;
588 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 err = got_error(GOT_ERR_OBJ_TYPE);
590 goto done;
593 /* Record our base commit. */
594 err = got_object_id_str(&id_str, commit_id);
595 if (err)
596 goto done;
597 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
598 if (err)
599 goto done;
601 free(worktree->base_commit_id);
602 worktree->base_commit_id = got_object_id_dup(commit_id);
603 if (worktree->base_commit_id == NULL) {
604 err = got_error_from_errno("got_object_id_dup");
605 goto done;
607 done:
608 if (obj)
609 got_object_close(obj);
610 free(id_str);
611 free(path_got);
612 return err;
615 static const struct got_error *
616 lock_worktree(struct got_worktree *worktree, int operation)
618 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
619 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
620 : got_error_from_errno2("flock",
621 got_worktree_get_root_path(worktree)));
622 return NULL;
625 static const struct got_error *
626 add_dir_on_disk(struct got_worktree *worktree, const char *path)
628 const struct got_error *err = NULL;
629 char *abspath;
631 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
632 return got_error_from_errno("asprintf");
634 err = got_path_mkdir(abspath);
635 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
636 struct stat sb;
637 err = NULL;
638 if (lstat(abspath, &sb) == -1) {
639 err = got_error_from_errno2("lstat", abspath);
640 } else if (!S_ISDIR(sb.st_mode)) {
641 /* TODO directory is obstructed; do something */
642 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
645 free(abspath);
646 return err;
649 static const struct got_error *
650 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
652 const struct got_error *err = NULL;
653 uint8_t fbuf1[8192];
654 uint8_t fbuf2[8192];
655 size_t flen1 = 0, flen2 = 0;
657 *same = 1;
659 for (;;) {
660 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
661 if (flen1 == 0 && ferror(f1)) {
662 err = got_error_from_errno("fread");
663 break;
665 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
666 if (flen2 == 0 && ferror(f2)) {
667 err = got_error_from_errno("fread");
668 break;
670 if (flen1 == 0) {
671 if (flen2 != 0)
672 *same = 0;
673 break;
674 } else if (flen2 == 0) {
675 if (flen1 != 0)
676 *same = 0;
677 break;
678 } else if (flen1 == flen2) {
679 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
680 *same = 0;
681 break;
683 } else {
684 *same = 0;
685 break;
689 return err;
692 static const struct got_error *
693 check_files_equal(int *same, const char *f1_path, const char *f2_path)
695 const struct got_error *err = NULL;
696 struct stat sb;
697 size_t size1, size2;
698 FILE *f1 = NULL, *f2 = NULL;
700 *same = 1;
702 if (lstat(f1_path, &sb) != 0) {
703 err = got_error_from_errno2("lstat", f1_path);
704 goto done;
706 size1 = sb.st_size;
708 if (lstat(f2_path, &sb) != 0) {
709 err = got_error_from_errno2("lstat", f2_path);
710 goto done;
712 size2 = sb.st_size;
714 if (size1 != size2) {
715 *same = 0;
716 return NULL;
719 f1 = fopen(f1_path, "r");
720 if (f1 == NULL)
721 return got_error_from_errno2("open", f1_path);
723 f2 = fopen(f2_path, "r");
724 if (f2 == NULL) {
725 err = got_error_from_errno2("open", f2_path);
726 goto done;
729 err = check_file_contents_equal(same, f1, f2);
730 done:
731 if (f1 && fclose(f1) != 0 && err == NULL)
732 err = got_error_from_errno("fclose");
733 if (f2 && fclose(f2) != 0 && err == NULL)
734 err = got_error_from_errno("fclose");
736 return err;
739 /*
740 * Perform a 3-way merge where the file's version in the file index (blob2)
741 * acts as the common ancestor, the incoming blob (blob1) acts as the first
742 * derived version, and the file on disk acts as the second derived version.
743 */
744 static const struct got_error *
745 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
746 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
747 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
748 struct got_repository *repo,
749 got_worktree_checkout_cb progress_cb, void *progress_arg)
751 const struct got_error *err = NULL;
752 int merged_fd = -1;
753 struct got_blob_object *blob2 = NULL;
754 FILE *f1 = NULL, *f2 = NULL;
755 char *blob1_path = NULL, *blob2_path = NULL;
756 char *merged_path = NULL, *base_path = NULL;
757 char *id_str = NULL;
758 char *label1 = NULL;
759 int overlapcnt = 0, update_timestamps = 0;
760 char *parent;
762 parent = dirname(ondisk_path);
763 if (parent == NULL)
764 return got_error_from_errno2("dirname", ondisk_path);
766 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
767 return got_error_from_errno("asprintf");
769 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
770 if (err)
771 goto done;
773 free(base_path);
774 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
775 err = got_error_from_errno("asprintf");
776 base_path = NULL;
777 goto done;
780 err = got_opentemp_named(&blob1_path, &f1, base_path);
781 if (err)
782 goto done;
783 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
784 if (err)
785 goto done;
787 free(base_path);
788 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
789 err = got_error_from_errno("asprintf");
790 base_path = NULL;
791 goto done;
794 err = got_opentemp_named(&blob2_path, &f2, base_path);
795 if (err)
796 goto done;
797 if (got_fileindex_entry_has_blob(ie)) {
798 struct got_object_id id2;
799 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
800 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
801 if (err)
802 goto done;
803 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
804 if (err)
805 goto done;
806 } else {
807 /*
808 * If the file has no blob, this is an "add vs add" conflict,
809 * and we simply use an empty ancestor file to make both files
810 * appear in the merged result in their entirety.
811 */
814 err = got_object_id_str(&id_str, worktree->base_commit_id);
815 if (err)
816 goto done;
817 if (asprintf(&label1, "commit %s", id_str) == -1) {
818 err = got_error_from_errno("asprintf");
819 goto done;
822 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
823 blob2_path, ondisk_path, label1, path);
824 if (err)
825 goto done;
827 (*progress_cb)(progress_arg,
828 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
830 if (fsync(merged_fd) != 0) {
831 err = got_error_from_errno("fsync");
832 goto done;
835 /* Check if a clean merge has subsumed all local changes. */
836 if (overlapcnt == 0) {
837 err = check_files_equal(&update_timestamps, blob1_path,
838 merged_path);
839 if (err)
840 goto done;
843 if (chmod(merged_path, st_mode) != 0) {
844 err = got_error_from_errno2("chmod", merged_path);
845 goto done;
848 if (rename(merged_path, ondisk_path) != 0) {
849 err = got_error_from_errno3("rename", merged_path,
850 ondisk_path);
851 unlink(merged_path);
852 goto done;
855 /*
856 * Do not update timestamps of already modified files. Otherwise,
857 * a future status walk would treat them as unmodified files again.
858 */
859 err = got_fileindex_entry_update(ie, ondisk_path,
860 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
861 done:
862 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
863 err = got_error_from_errno("close");
864 if (f1 && fclose(f1) != 0 && err == NULL)
865 err = got_error_from_errno("fclose");
866 if (f2 && fclose(f2) != 0 && err == NULL)
867 err = got_error_from_errno("fclose");
868 if (blob2)
869 got_object_blob_close(blob2);
870 free(merged_path);
871 free(base_path);
872 if (blob1_path) {
873 unlink(blob1_path);
874 free(blob1_path);
876 if (blob2_path) {
877 unlink(blob2_path);
878 free(blob2_path);
880 free(id_str);
881 free(label1);
882 return err;
885 static const struct got_error *
886 update_blob_fileindex_entry(struct got_worktree *worktree,
887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
888 const char *ondisk_path, const char *path, struct got_blob_object *blob,
889 int update_timestamps)
891 const struct got_error *err = NULL;
893 if (ie == NULL)
894 ie = got_fileindex_entry_get(fileindex, path);
895 if (ie)
896 err = got_fileindex_entry_update(ie, ondisk_path,
897 blob->id.sha1, worktree->base_commit_id->sha1,
898 update_timestamps);
899 else {
900 struct got_fileindex_entry *new_ie;
901 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
902 path, blob->id.sha1, worktree->base_commit_id->sha1);
903 if (!err)
904 err = got_fileindex_entry_add(fileindex, new_ie);
906 return err;
909 static const struct got_error *
910 install_blob(struct got_worktree *worktree, const char *ondisk_path,
911 const char *path, uint16_t te_mode, uint16_t st_mode,
912 struct got_blob_object *blob, int restoring_missing_file,
913 int reverting_versioned_file, struct got_repository *repo,
914 got_worktree_checkout_cb progress_cb, void *progress_arg)
916 const struct got_error *err = NULL;
917 int fd = -1;
918 size_t len, hdrlen;
919 int update = 0;
920 char *tmppath = NULL;
922 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
923 GOT_DEFAULT_FILE_MODE);
924 if (fd == -1) {
925 if (errno == ENOENT) {
926 char *parent = dirname(path);
927 if (parent == NULL)
928 return got_error_from_errno2("dirname", path);
929 err = add_dir_on_disk(worktree, parent);
930 if (err)
931 return err;
932 fd = open(ondisk_path,
933 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
934 GOT_DEFAULT_FILE_MODE);
935 if (fd == -1)
936 return got_error_from_errno2("open",
937 ondisk_path);
938 } else if (errno == EEXIST) {
939 if (!S_ISREG(st_mode)) {
940 /* TODO file is obstructed; do something */
941 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
942 goto done;
943 } else {
944 err = got_opentemp_named_fd(&tmppath, &fd,
945 ondisk_path);
946 if (err)
947 goto done;
948 update = 1;
950 } else
951 return got_error_from_errno2("open", ondisk_path);
954 if (restoring_missing_file)
955 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
956 else if (reverting_versioned_file)
957 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
958 else
959 (*progress_cb)(progress_arg,
960 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
962 hdrlen = got_object_blob_get_hdrlen(blob);
963 do {
964 const uint8_t *buf = got_object_blob_get_read_buf(blob);
965 err = got_object_blob_read_block(&len, blob);
966 if (err)
967 break;
968 if (len > 0) {
969 /* Skip blob object header first time around. */
970 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
971 if (outlen == -1) {
972 err = got_error_from_errno("write");
973 goto done;
974 } else if (outlen != len - hdrlen) {
975 err = got_error(GOT_ERR_IO);
976 goto done;
978 hdrlen = 0;
980 } while (len != 0);
982 if (fsync(fd) != 0) {
983 err = got_error_from_errno("fsync");
984 goto done;
987 if (update) {
988 if (rename(tmppath, ondisk_path) != 0) {
989 err = got_error_from_errno3("rename", tmppath,
990 ondisk_path);
991 unlink(tmppath);
992 goto done;
996 if (te_mode & S_IXUSR) {
997 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
998 err = got_error_from_errno2("chmod", ondisk_path);
999 goto done;
1001 } else {
1002 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1003 err = got_error_from_errno2("chmod", ondisk_path);
1004 goto done;
1008 done:
1009 if (fd != -1 && close(fd) != 0 && err == NULL)
1010 err = got_error_from_errno("close");
1011 free(tmppath);
1012 return err;
1015 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1016 static const struct got_error *
1017 get_modified_file_content_status(unsigned char *status, FILE *f)
1019 const struct got_error *err = NULL;
1020 const char *markers[3] = {
1021 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1022 GOT_DIFF_CONFLICT_MARKER_SEP,
1023 GOT_DIFF_CONFLICT_MARKER_END
1025 int i = 0;
1026 char *line;
1027 size_t len;
1028 const char delim[3] = {'\0', '\0', '\0'};
1030 while (*status == GOT_STATUS_MODIFY) {
1031 line = fparseln(f, &len, NULL, delim, 0);
1032 if (line == NULL) {
1033 if (feof(f))
1034 break;
1035 err = got_ferror(f, GOT_ERR_IO);
1036 break;
1039 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1040 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1041 == 0)
1042 *status = GOT_STATUS_CONFLICT;
1043 else
1044 i++;
1048 return err;
1051 static const struct got_error *
1052 get_file_status(unsigned char *status, struct stat *sb,
1053 struct got_fileindex_entry *ie, const char *abspath,
1054 struct got_repository *repo)
1056 const struct got_error *err = NULL;
1057 struct got_object_id id;
1058 size_t hdrlen;
1059 FILE *f = NULL;
1060 uint8_t fbuf[8192];
1061 struct got_blob_object *blob = NULL;
1062 size_t flen, blen;
1064 *status = GOT_STATUS_NO_CHANGE;
1066 if (lstat(abspath, sb) == -1) {
1067 if (errno == ENOENT) {
1068 if (ie) {
1069 if (got_fileindex_entry_has_file_on_disk(ie))
1070 *status = GOT_STATUS_MISSING;
1071 else
1072 *status = GOT_STATUS_DELETE;
1073 sb->st_mode =
1074 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1075 & (S_IRWXU | S_IRWXG | S_IRWXO));
1076 } else
1077 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1078 return NULL;
1080 return got_error_from_errno2("lstat", abspath);
1083 if (!S_ISREG(sb->st_mode)) {
1084 *status = GOT_STATUS_OBSTRUCTED;
1085 return NULL;
1088 if (ie == NULL)
1089 return NULL;
1091 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1092 *status = GOT_STATUS_DELETE;
1093 return NULL;
1094 } else if (!got_fileindex_entry_has_blob(ie)) {
1095 *status = GOT_STATUS_ADD;
1096 return NULL;
1099 if (ie->ctime_sec == sb->st_ctime &&
1100 ie->ctime_nsec == sb->st_ctimensec &&
1101 ie->mtime_sec == sb->st_mtime &&
1102 ie->mtime_sec == sb->st_mtime &&
1103 ie->mtime_nsec == sb->st_mtimensec &&
1104 ie->size == (sb->st_size & 0xffffffff))
1105 return NULL;
1107 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1108 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1109 if (err)
1110 return err;
1112 f = fopen(abspath, "r");
1113 if (f == NULL) {
1114 err = got_error_from_errno2("fopen", abspath);
1115 goto done;
1117 hdrlen = got_object_blob_get_hdrlen(blob);
1118 for (;;) {
1119 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1120 err = got_object_blob_read_block(&blen, blob);
1121 if (err)
1122 goto done;
1123 /* Skip length of blob object header first time around. */
1124 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1125 if (flen == 0 && ferror(f)) {
1126 err = got_error_from_errno("fread");
1127 goto done;
1129 if (blen == 0) {
1130 if (flen != 0)
1131 *status = GOT_STATUS_MODIFY;
1132 break;
1133 } else if (flen == 0) {
1134 if (blen != 0)
1135 *status = GOT_STATUS_MODIFY;
1136 break;
1137 } else if (blen - hdrlen == flen) {
1138 /* Skip blob object header first time around. */
1139 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1140 *status = GOT_STATUS_MODIFY;
1141 break;
1143 } else {
1144 *status = GOT_STATUS_MODIFY;
1145 break;
1147 hdrlen = 0;
1150 if (*status == GOT_STATUS_MODIFY) {
1151 rewind(f);
1152 err = get_modified_file_content_status(status, f);
1154 done:
1155 if (blob)
1156 got_object_blob_close(blob);
1157 if (f)
1158 fclose(f);
1159 return err;
1162 static const struct got_error *
1163 update_blob(struct got_worktree *worktree,
1164 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1165 struct got_tree_entry *te, const char *path,
1166 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1167 void *progress_arg)
1169 const struct got_error *err = NULL;
1170 struct got_blob_object *blob = NULL;
1171 char *ondisk_path;
1172 unsigned char status = GOT_STATUS_NO_CHANGE;
1173 struct stat sb;
1175 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1176 return got_error_from_errno("asprintf");
1178 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1179 if (err)
1180 goto done;
1182 if (status == GOT_STATUS_OBSTRUCTED) {
1183 (*progress_cb)(progress_arg, status, path);
1184 goto done;
1187 if (ie && status != GOT_STATUS_MISSING) {
1188 if (got_fileindex_entry_has_commit(ie) &&
1189 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1190 SHA1_DIGEST_LENGTH) == 0) {
1191 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1192 path);
1193 goto done;
1195 if (got_fileindex_entry_has_blob(ie) &&
1196 memcmp(ie->blob_sha1, te->id->sha1,
1197 SHA1_DIGEST_LENGTH) == 0)
1198 goto done;
1201 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1202 if (err)
1203 goto done;
1205 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1206 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1207 te->mode, sb.st_mode, blob, repo, progress_cb,
1208 progress_arg);
1209 else if (status == GOT_STATUS_DELETE) {
1210 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1211 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1212 ondisk_path, path, blob, 0);
1213 if (err)
1214 goto done;
1215 } else {
1216 err = install_blob(worktree, ondisk_path, path, te->mode,
1217 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1218 repo, progress_cb, progress_arg);
1219 if (err)
1220 goto done;
1221 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1222 ondisk_path, path, blob, 1);
1223 if (err)
1224 goto done;
1226 got_object_blob_close(blob);
1227 done:
1228 free(ondisk_path);
1229 return err;
1232 static const struct got_error *
1233 remove_ondisk_file(const char *root_path, const char *path)
1235 const struct got_error *err = NULL;
1236 char *ondisk_path = NULL;
1238 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1239 return got_error_from_errno("asprintf");
1241 if (unlink(ondisk_path) == -1) {
1242 if (errno != ENOENT)
1243 err = got_error_from_errno2("unlink", ondisk_path);
1244 } else {
1245 char *parent = dirname(ondisk_path);
1246 while (parent && strcmp(parent, root_path) != 0) {
1247 if (rmdir(parent) == -1) {
1248 if (errno != ENOTEMPTY)
1249 err = got_error_from_errno2("rmdir",
1250 parent);
1251 break;
1253 parent = dirname(parent);
1256 free(ondisk_path);
1257 return err;
1260 static const struct got_error *
1261 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1262 struct got_fileindex_entry *ie, const char *parent_path,
1263 struct got_repository *repo,
1264 got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 const struct got_error *err = NULL;
1267 unsigned char status;
1268 struct stat sb;
1269 char *ondisk_path;
1271 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1272 == -1)
1273 return got_error_from_errno("asprintf");
1275 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1276 if (err)
1277 return err;
1279 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1280 status == GOT_STATUS_ADD) {
1281 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1283 * Preserve the working file and change the deleted blob's
1284 * entry into a schedule-add entry.
1286 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1287 0);
1288 if (err)
1289 return err;
1290 } else {
1291 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1292 if (status == GOT_STATUS_NO_CHANGE) {
1293 err = remove_ondisk_file(worktree->root_path, ie->path);
1294 if (err)
1295 return err;
1297 got_fileindex_entry_remove(fileindex, ie);
1300 return err;
1303 struct diff_cb_arg {
1304 struct got_fileindex *fileindex;
1305 struct got_worktree *worktree;
1306 struct got_repository *repo;
1307 got_worktree_checkout_cb progress_cb;
1308 void *progress_arg;
1309 got_worktree_cancel_cb cancel_cb;
1310 void *cancel_arg;
1313 static const struct got_error *
1314 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1315 struct got_tree_entry *te, const char *parent_path)
1317 struct diff_cb_arg *a = arg;
1319 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 return got_error(GOT_ERR_CANCELLED);
1322 return update_blob(a->worktree, a->fileindex, ie, te,
1323 ie->path, a->repo, a->progress_cb, a->progress_arg);
1326 static const struct got_error *
1327 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1329 struct diff_cb_arg *a = arg;
1331 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1332 return got_error(GOT_ERR_CANCELLED);
1334 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1335 a->repo, a->progress_cb, a->progress_arg);
1338 static const struct got_error *
1339 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1341 struct diff_cb_arg *a = arg;
1342 const struct got_error *err;
1343 char *path;
1345 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1346 return got_error(GOT_ERR_CANCELLED);
1348 if (asprintf(&path, "%s%s%s", parent_path,
1349 parent_path[0] ? "/" : "", te->name)
1350 == -1)
1351 return got_error_from_errno("asprintf");
1353 if (S_ISDIR(te->mode))
1354 err = add_dir_on_disk(a->worktree, path);
1355 else
1356 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1357 a->repo, a->progress_cb, a->progress_arg);
1359 free(path);
1360 return err;
1363 const struct got_error *
1364 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1366 const struct got_error *err = NULL;
1367 char *uuidstr = NULL;
1368 uint32_t uuid_status;
1370 *refname = NULL;
1372 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1373 if (uuid_status != uuid_s_ok)
1374 return got_error_uuid(uuid_status);
1376 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1377 == -1) {
1378 err = got_error_from_errno("asprintf");
1379 *refname = NULL;
1381 free(uuidstr);
1382 return err;
1386 * Prevent Git's garbage collector from deleting our base commit by
1387 * setting a reference to our base commit's ID.
1389 static const struct got_error *
1390 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1392 const struct got_error *err = NULL;
1393 struct got_reference *ref = NULL;
1394 char *refname;
1396 err = got_worktree_get_base_ref_name(&refname, worktree);
1397 if (err)
1398 return err;
1400 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1401 if (err)
1402 goto done;
1404 err = got_ref_write(ref, repo);
1405 done:
1406 free(refname);
1407 if (ref)
1408 got_ref_close(ref);
1409 return err;
1412 static const struct got_error *
1413 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1414 struct got_worktree *worktree)
1416 const struct got_error *err = NULL;
1417 FILE *index = NULL;
1419 *fileindex_path = NULL;
1420 *fileindex = got_fileindex_alloc();
1421 if (*fileindex == NULL)
1422 return got_error_from_errno("got_fileindex_alloc");
1424 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1425 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1426 err = got_error_from_errno("asprintf");
1427 *fileindex_path = NULL;
1428 goto done;
1431 index = fopen(*fileindex_path, "rb");
1432 if (index == NULL) {
1433 if (errno != ENOENT)
1434 err = got_error_from_errno2("fopen", *fileindex_path);
1435 } else {
1436 err = got_fileindex_read(*fileindex, index);
1437 if (fclose(index) != 0 && err == NULL)
1438 err = got_error_from_errno("fclose");
1440 done:
1441 if (err) {
1442 free(*fileindex_path);
1443 *fileindex_path = NULL;
1444 free(*fileindex);
1445 *fileindex = NULL;
1447 return err;
1450 const struct got_error *
1451 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1452 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1453 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1455 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1456 struct got_commit_object *commit = NULL;
1457 struct got_object_id *tree_id = NULL;
1458 struct got_tree_object *tree = NULL;
1459 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1460 struct got_fileindex *fileindex = NULL;
1461 FILE *new_index = NULL;
1462 struct got_fileindex_diff_tree_cb diff_cb;
1463 struct diff_cb_arg arg;
1464 char *relpath = NULL, *entry_name = NULL;
1466 err = lock_worktree(worktree, LOCK_EX);
1467 if (err)
1468 return err;
1471 * Read the file index.
1472 * Checking out files is supposed to be an idempotent operation.
1473 * If the on-disk file index is incomplete we will try to complete it.
1475 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1476 if (err)
1477 goto done;
1479 err = got_opentemp_named(&new_fileindex_path, &new_index,
1480 fileindex_path);
1481 if (err)
1482 goto done;
1484 err = ref_base_commit(worktree, repo);
1485 if (err)
1486 goto done;
1488 err = got_object_open_as_commit(&commit, repo,
1489 worktree->base_commit_id);
1490 if (err)
1491 goto done;
1493 if (path[0]) {
1494 char *tree_path;
1495 int obj_type;
1496 relpath = strdup(path);
1497 if (relpath == NULL) {
1498 err = got_error_from_errno("strdup");
1499 goto done;
1501 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1502 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1503 path) == -1) {
1504 err = got_error_from_errno("asprintf");
1505 goto done;
1507 err = got_object_id_by_path(&tree_id, repo,
1508 worktree->base_commit_id, tree_path);
1509 free(tree_path);
1510 if (err)
1511 goto done;
1512 err = got_object_get_type(&obj_type, repo, tree_id);
1513 if (err)
1514 goto done;
1515 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1516 /* Split provided path into parent dir + entry name. */
1517 if (strchr(path, '/') == NULL) {
1518 relpath = strdup("");
1519 if (relpath == NULL) {
1520 err = got_error_from_errno("strdup");
1521 goto done;
1523 tree_path = strdup(worktree->path_prefix);
1524 if (tree_path == NULL) {
1525 err = got_error_from_errno("strdup");
1526 goto done;
1528 } else {
1529 err = got_path_dirname(&relpath, path);
1530 if (err)
1531 goto done;
1532 if (asprintf(&tree_path, "%s%s%s",
1533 worktree->path_prefix,
1534 got_path_is_root_dir(
1535 worktree->path_prefix) ? "" : "/",
1536 relpath) == -1) {
1537 err = got_error_from_errno("asprintf");
1538 goto done;
1541 err = got_object_id_by_path(&tree_id, repo,
1542 worktree->base_commit_id, tree_path);
1543 free(tree_path);
1544 if (err)
1545 goto done;
1546 entry_name = basename(path);
1547 if (entry_name == NULL) {
1548 err = got_error_from_errno2("basename", path);
1549 goto done;
1552 } else {
1553 relpath = strdup("");
1554 if (relpath == NULL) {
1555 err = got_error_from_errno("strdup");
1556 goto done;
1558 err = got_object_id_by_path(&tree_id, repo,
1559 worktree->base_commit_id, worktree->path_prefix);
1560 if (err)
1561 goto done;
1564 err = got_object_open_as_tree(&tree, repo, tree_id);
1565 if (err)
1566 goto done;
1568 if (entry_name &&
1569 got_object_tree_find_entry(tree, entry_name) == NULL) {
1570 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1571 goto done;
1574 diff_cb.diff_old_new = diff_old_new;
1575 diff_cb.diff_old = diff_old;
1576 diff_cb.diff_new = diff_new;
1577 arg.fileindex = fileindex;
1578 arg.worktree = worktree;
1579 arg.repo = repo;
1580 arg.progress_cb = progress_cb;
1581 arg.progress_arg = progress_arg;
1582 arg.cancel_cb = cancel_cb;
1583 arg.cancel_arg = cancel_arg;
1584 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1585 entry_name, repo, &diff_cb, &arg);
1587 /* Try to sync the fileindex back to disk in any case. */
1588 err = got_fileindex_write(fileindex, new_index);
1589 if (err)
1590 goto done;
1592 if (rename(new_fileindex_path, fileindex_path) != 0) {
1593 err = got_error_from_errno3("rename", new_fileindex_path,
1594 fileindex_path);
1595 unlink(new_fileindex_path);
1596 goto done;
1599 free(new_fileindex_path);
1600 new_fileindex_path = NULL;
1602 done:
1603 free(relpath);
1604 if (tree)
1605 got_object_tree_close(tree);
1606 if (commit)
1607 got_object_commit_close(commit);
1608 if (new_fileindex_path)
1609 unlink(new_fileindex_path);
1610 if (new_index)
1611 fclose(new_index);
1612 free(new_fileindex_path);
1613 free(fileindex_path);
1614 got_fileindex_free(fileindex);
1615 if (checkout_err)
1616 err = checkout_err;
1617 unlockerr = lock_worktree(worktree, LOCK_SH);
1618 if (unlockerr && err == NULL)
1619 err = unlockerr;
1620 return err;
1623 struct diff_dir_cb_arg {
1624 struct got_fileindex *fileindex;
1625 struct got_worktree *worktree;
1626 const char *status_path;
1627 size_t status_path_len;
1628 struct got_repository *repo;
1629 got_worktree_status_cb status_cb;
1630 void *status_arg;
1631 got_worktree_cancel_cb cancel_cb;
1632 void *cancel_arg;
1635 static const struct got_error *
1636 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1637 got_worktree_status_cb status_cb, void *status_arg,
1638 struct got_repository *repo)
1640 const struct got_error *err = NULL;
1641 unsigned char status = GOT_STATUS_NO_CHANGE;
1642 struct stat sb;
1643 struct got_object_id blob_id, commit_id;
1645 err = get_file_status(&status, &sb, ie, abspath, repo);
1646 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1647 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1648 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1649 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1650 &commit_id);
1652 return err;
1655 static const struct got_error *
1656 status_old_new(void *arg, struct got_fileindex_entry *ie,
1657 struct dirent *de, const char *parent_path)
1659 const struct got_error *err = NULL;
1660 struct diff_dir_cb_arg *a = arg;
1661 char *abspath;
1663 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1664 return got_error(GOT_ERR_CANCELLED);
1666 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1667 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1668 return NULL;
1670 if (parent_path[0]) {
1671 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1672 parent_path, de->d_name) == -1)
1673 return got_error_from_errno("asprintf");
1674 } else {
1675 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1676 de->d_name) == -1)
1677 return got_error_from_errno("asprintf");
1680 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1681 a->repo);
1682 free(abspath);
1683 return err;
1686 static const struct got_error *
1687 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1689 struct diff_dir_cb_arg *a = arg;
1690 struct got_object_id blob_id, commit_id;
1691 unsigned char status;
1693 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1694 return got_error(GOT_ERR_CANCELLED);
1696 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1697 return NULL;
1699 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1700 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1701 if (got_fileindex_entry_has_file_on_disk(ie))
1702 status = GOT_STATUS_MISSING;
1703 else
1704 status = GOT_STATUS_DELETE;
1705 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1706 &commit_id);
1709 static const struct got_error *
1710 status_new(void *arg, struct dirent *de, const char *parent_path)
1712 const struct got_error *err = NULL;
1713 struct diff_dir_cb_arg *a = arg;
1714 char *path = NULL;
1716 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1717 return got_error(GOT_ERR_CANCELLED);
1719 if (de->d_type == DT_DIR)
1720 return NULL;
1722 /* XXX ignore symlinks for now */
1723 if (de->d_type == DT_LNK)
1724 return NULL;
1726 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1727 return NULL;
1729 if (parent_path[0]) {
1730 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1731 return got_error_from_errno("asprintf");
1732 } else {
1733 path = de->d_name;
1736 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1737 NULL, NULL);
1738 if (parent_path[0])
1739 free(path);
1740 return err;
1743 const struct got_error *
1744 got_worktree_status(struct got_worktree *worktree, const char *path,
1745 struct got_repository *repo, got_worktree_status_cb status_cb,
1746 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1748 const struct got_error *err = NULL;
1749 DIR *workdir = NULL;
1750 char *fileindex_path = NULL;
1751 struct got_fileindex *fileindex = NULL;
1752 FILE *index = NULL;
1753 struct got_fileindex_diff_dir_cb fdiff_cb;
1754 struct diff_dir_cb_arg arg;
1755 char *ondisk_path = NULL;
1757 fileindex = got_fileindex_alloc();
1758 if (fileindex == NULL) {
1759 err = got_error_from_errno("got_fileindex_alloc");
1760 goto done;
1763 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1764 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1765 err = got_error_from_errno("asprintf");
1766 fileindex_path = NULL;
1767 goto done;
1770 index = fopen(fileindex_path, "rb");
1771 if (index == NULL) {
1772 if (errno != ENOENT) {
1773 err = got_error_from_errno2("fopen", fileindex_path);
1774 goto done;
1776 } else {
1777 err = got_fileindex_read(fileindex, index);
1778 fclose(index);
1779 if (err)
1780 goto done;
1783 if (asprintf(&ondisk_path, "%s%s%s",
1784 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1785 err = got_error_from_errno("asprintf");
1786 goto done;
1788 workdir = opendir(ondisk_path);
1789 if (workdir == NULL) {
1790 if (errno == ENOTDIR || errno == ENOENT) {
1791 struct got_fileindex_entry *ie;
1792 ie = got_fileindex_entry_get(fileindex, path);
1793 if (ie == NULL) {
1794 err = got_error(GOT_ERR_BAD_PATH);
1795 goto done;
1797 err = report_file_status(ie, ondisk_path,
1798 status_cb, status_arg, repo);
1799 goto done;
1800 } else {
1801 err = got_error_from_errno2("opendir", ondisk_path);
1802 goto done;
1805 fdiff_cb.diff_old_new = status_old_new;
1806 fdiff_cb.diff_old = status_old;
1807 fdiff_cb.diff_new = status_new;
1808 arg.fileindex = fileindex;
1809 arg.worktree = worktree;
1810 arg.status_path = path;
1811 arg.status_path_len = strlen(path);
1812 arg.repo = repo;
1813 arg.status_cb = status_cb;
1814 arg.status_arg = status_arg;
1815 arg.cancel_cb = cancel_cb;
1816 arg.cancel_arg = cancel_arg;
1817 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1818 path, repo, &fdiff_cb, &arg);
1819 done:
1820 if (workdir)
1821 closedir(workdir);
1822 free(ondisk_path);
1823 free(fileindex_path);
1824 got_fileindex_free(fileindex);
1825 return err;
1828 const struct got_error *
1829 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1830 const char *arg)
1832 const struct got_error *err = NULL;
1833 char *resolved, *path = NULL;
1834 size_t len;
1836 *wt_path = NULL;
1838 resolved = realpath(arg, NULL);
1839 if (resolved == NULL)
1840 return got_error_from_errno2("realpath", arg);
1842 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1843 strlen(got_worktree_get_root_path(worktree)))) {
1844 err = got_error(GOT_ERR_BAD_PATH);
1845 goto done;
1848 path = strdup(resolved +
1849 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1850 if (path == NULL) {
1851 err = got_error_from_errno("strdup");
1852 goto done;
1855 /* XXX status walk can't deal with trailing slash! */
1856 len = strlen(path);
1857 while (path[len - 1] == '/') {
1858 path[len - 1] = '\0';
1859 len--;
1861 done:
1862 free(resolved);
1863 if (err == NULL)
1864 *wt_path = path;
1865 else
1866 free(path);
1867 return err;
1870 const struct got_error *
1871 got_worktree_schedule_add(struct got_worktree *worktree,
1872 struct got_pathlist_head *ondisk_paths,
1873 got_worktree_status_cb status_cb, void *status_arg,
1874 struct got_repository *repo)
1876 struct got_fileindex *fileindex = NULL;
1877 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1878 FILE *index = NULL, *new_index = NULL;
1879 const struct got_error *err = NULL, *unlockerr = NULL;
1880 struct got_pathlist_entry *pe;
1882 err = lock_worktree(worktree, LOCK_EX);
1883 if (err)
1884 return err;
1887 fileindex = got_fileindex_alloc();
1888 if (fileindex == NULL) {
1889 err = got_error_from_errno("got_fileindex_alloc");
1890 goto done;
1893 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1894 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1895 err = got_error_from_errno("asprintf");
1896 fileindex_path = NULL;
1897 goto done;
1900 index = fopen(fileindex_path, "rb");
1901 if (index == NULL) {
1902 err = got_error_from_errno2("fopen", fileindex_path);
1903 goto done;
1906 err = got_fileindex_read(fileindex, index);
1907 if (err)
1908 goto done;
1910 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1911 struct got_fileindex_entry *ie = NULL;
1912 char *relpath;
1914 err = got_path_skip_common_ancestor(&relpath,
1915 got_worktree_get_root_path(worktree), pe->path);
1916 if (err)
1917 goto done;
1919 /* Re-adding an existing entry is a no-op. */
1920 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1921 continue;
1923 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1924 NULL, NULL);
1925 free(relpath);
1926 if (err)
1927 goto done;
1929 err = got_fileindex_entry_add(fileindex, ie);
1930 if (err) {
1931 got_fileindex_entry_free(ie);
1932 goto done;
1935 err = report_file_status(ie, pe->path, status_cb, status_arg,
1936 repo);
1937 if (err)
1938 goto done;
1941 err = got_opentemp_named(&new_fileindex_path, &new_index,
1942 fileindex_path);
1943 if (err)
1944 goto done;
1946 err = got_fileindex_write(fileindex, new_index);
1947 if (err)
1948 goto done;
1950 if (rename(new_fileindex_path, fileindex_path) != 0) {
1951 err = got_error_from_errno3("rename", new_fileindex_path,
1952 fileindex_path);
1953 goto done;
1956 free(new_fileindex_path);
1957 new_fileindex_path = NULL;
1959 done:
1960 if (index) {
1961 if (fclose(index) != 0 && err == NULL)
1962 err = got_error_from_errno("fclose");
1964 if (new_fileindex_path) {
1965 if (unlink(new_fileindex_path) != 0 && err == NULL)
1966 err = got_error_from_errno2("unlink",
1967 new_fileindex_path);
1968 free(new_fileindex_path);
1970 if (fileindex)
1971 got_fileindex_free(fileindex);
1972 unlockerr = lock_worktree(worktree, LOCK_SH);
1973 if (unlockerr && err == NULL)
1974 err = unlockerr;
1975 return err;
1978 const struct got_error *
1979 got_worktree_schedule_delete(struct got_worktree *worktree,
1980 const char *ondisk_path, int delete_local_mods,
1981 got_worktree_status_cb status_cb, void *status_arg,
1982 struct got_repository *repo)
1984 struct got_fileindex *fileindex = NULL;
1985 struct got_fileindex_entry *ie = NULL;
1986 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1987 FILE *index = NULL, *new_index = NULL;
1988 const struct got_error *err = NULL, *unlockerr = NULL;
1989 unsigned char status;
1990 struct stat sb;
1992 err = lock_worktree(worktree, LOCK_EX);
1993 if (err)
1994 return err;
1996 err = got_path_skip_common_ancestor(&relpath,
1997 got_worktree_get_root_path(worktree), ondisk_path);
1998 if (err)
1999 goto done;
2001 fileindex = got_fileindex_alloc();
2002 if (fileindex == NULL) {
2003 err = got_error_from_errno("got_fileindex_alloc");
2004 goto done;
2007 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2008 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2009 err = got_error_from_errno("asprintf");
2010 fileindex_path = NULL;
2011 goto done;
2014 index = fopen(fileindex_path, "rb");
2015 if (index == NULL) {
2016 err = got_error_from_errno2("fopen", fileindex_path);
2017 goto done;
2020 err = got_fileindex_read(fileindex, index);
2021 if (err)
2022 goto done;
2024 ie = got_fileindex_entry_get(fileindex, relpath);
2025 if (ie == NULL) {
2026 err = got_error(GOT_ERR_BAD_PATH);
2027 goto done;
2030 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2031 if (err)
2032 goto done;
2034 if (status != GOT_STATUS_NO_CHANGE) {
2035 if (status == GOT_STATUS_DELETE) {
2036 err = got_error_set_errno(ENOENT, ondisk_path);
2037 goto done;
2039 if (status != GOT_STATUS_MODIFY) {
2040 err = got_error(GOT_ERR_FILE_STATUS);
2041 goto done;
2043 if (!delete_local_mods) {
2044 err = got_error(GOT_ERR_FILE_MODIFIED);
2045 goto done;
2049 if (unlink(ondisk_path) != 0) {
2050 err = got_error_from_errno2("unlink", ondisk_path);
2051 goto done;
2054 got_fileindex_entry_mark_deleted_from_disk(ie);
2056 err = got_opentemp_named(&new_fileindex_path, &new_index,
2057 fileindex_path);
2058 if (err)
2059 goto done;
2061 err = got_fileindex_write(fileindex, new_index);
2062 if (err)
2063 goto done;
2065 if (rename(new_fileindex_path, fileindex_path) != 0) {
2066 err = got_error_from_errno3("rename", new_fileindex_path,
2067 fileindex_path);
2068 goto done;
2071 free(new_fileindex_path);
2072 new_fileindex_path = NULL;
2074 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2075 done:
2076 free(relpath);
2077 if (index) {
2078 if (fclose(index) != 0 && err == NULL)
2079 err = got_error_from_errno("fclose");
2081 if (new_fileindex_path) {
2082 if (unlink(new_fileindex_path) != 0 && err == NULL)
2083 err = got_error_from_errno2("unlink",
2084 new_fileindex_path);
2085 free(new_fileindex_path);
2087 if (fileindex)
2088 got_fileindex_free(fileindex);
2089 unlockerr = lock_worktree(worktree, LOCK_SH);
2090 if (unlockerr && err == NULL)
2091 err = unlockerr;
2092 return err;
2095 const struct got_error *
2096 got_worktree_revert(struct got_worktree *worktree,
2097 const char *ondisk_path,
2098 got_worktree_checkout_cb progress_cb, void *progress_arg,
2099 struct got_repository *repo)
2101 struct got_fileindex *fileindex = NULL;
2102 struct got_fileindex_entry *ie = NULL;
2103 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2104 char *tree_path = NULL, *parent_path, *te_name;
2105 FILE *index = NULL, *new_index = NULL;
2106 const struct got_error *err = NULL, *unlockerr = NULL;
2107 struct got_tree_object *tree = NULL;
2108 struct got_object_id id, *tree_id = NULL;
2109 const struct got_tree_entry *te;
2110 struct got_blob_object *blob = NULL;
2111 unsigned char status;
2112 struct stat sb;
2114 err = lock_worktree(worktree, LOCK_EX);
2115 if (err)
2116 return err;
2118 err = got_path_skip_common_ancestor(&relpath,
2119 got_worktree_get_root_path(worktree), ondisk_path);
2120 if (err)
2121 goto done;
2123 fileindex = got_fileindex_alloc();
2124 if (fileindex == NULL) {
2125 err = got_error_from_errno("got_fileindex_alloc");
2126 goto done;
2129 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2130 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2131 err = got_error_from_errno("asprintf");
2132 fileindex_path = NULL;
2133 goto done;
2136 index = fopen(fileindex_path, "rb");
2137 if (index == NULL) {
2138 err = got_error_from_errno2("fopen", fileindex_path);
2139 goto done;
2142 err = got_fileindex_read(fileindex, index);
2143 if (err)
2144 goto done;
2146 ie = got_fileindex_entry_get(fileindex, relpath);
2147 if (ie == NULL) {
2148 err = got_error(GOT_ERR_BAD_PATH);
2149 goto done;
2152 /* Construct in-repository path of tree which contains this blob. */
2153 err = got_path_dirname(&parent_path, ie->path);
2154 if (err) {
2155 if (err->code != GOT_ERR_BAD_PATH)
2156 goto done;
2157 parent_path = "/";
2159 if (got_path_is_root_dir(worktree->path_prefix)) {
2160 tree_path = strdup(parent_path);
2161 if (tree_path == NULL) {
2162 err = got_error_from_errno("strdup");
2163 goto done;
2165 } else {
2166 if (got_path_is_root_dir(parent_path)) {
2167 tree_path = strdup(worktree->path_prefix);
2168 if (tree_path == NULL) {
2169 err = got_error_from_errno("strdup");
2170 goto done;
2172 } else {
2173 if (asprintf(&tree_path, "%s/%s",
2174 worktree->path_prefix, parent_path) == -1) {
2175 err = got_error_from_errno("asprintf");
2176 goto done;
2181 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2182 tree_path);
2183 if (err)
2184 goto done;
2186 err = got_object_open_as_tree(&tree, repo, tree_id);
2187 if (err)
2188 goto done;
2190 te_name = basename(ie->path);
2191 if (te_name == NULL) {
2192 err = got_error_from_errno2("basename", ie->path);
2193 goto done;
2196 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2197 if (err)
2198 goto done;
2200 te = got_object_tree_find_entry(tree, te_name);
2201 if (te == NULL && status != GOT_STATUS_ADD) {
2202 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2203 goto done;
2206 switch (status) {
2207 case GOT_STATUS_ADD:
2208 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2209 got_fileindex_entry_remove(fileindex, ie);
2210 break;
2211 case GOT_STATUS_DELETE:
2212 case GOT_STATUS_MODIFY:
2213 case GOT_STATUS_CONFLICT:
2214 case GOT_STATUS_MISSING:
2215 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2216 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2217 if (err)
2218 goto done;
2219 err = install_blob(worktree, ondisk_path, ie->path,
2220 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2221 progress_arg);
2222 if (err)
2223 goto done;
2224 if (status == GOT_STATUS_DELETE) {
2225 err = update_blob_fileindex_entry(worktree,
2226 fileindex, ie, ondisk_path, ie->path, blob, 1);
2227 if (err)
2228 goto done;
2230 break;
2231 default:
2232 goto done;
2235 err = got_opentemp_named(&new_fileindex_path, &new_index,
2236 fileindex_path);
2237 if (err)
2238 goto done;
2240 err = got_fileindex_write(fileindex, new_index);
2241 if (err)
2242 goto done;
2244 if (rename(new_fileindex_path, fileindex_path) != 0) {
2245 err = got_error_from_errno3("rename", new_fileindex_path,
2246 fileindex_path);
2247 goto done;
2250 free(new_fileindex_path);
2251 new_fileindex_path = NULL;
2252 done:
2253 free(relpath);
2254 free(tree_path);
2255 if (blob)
2256 got_object_blob_close(blob);
2257 if (tree)
2258 got_object_tree_close(tree);
2259 free(tree_id);
2260 if (index) {
2261 if (fclose(index) != 0 && err == NULL)
2262 err = got_error_from_errno("fclose");
2264 if (new_fileindex_path) {
2265 if (unlink(new_fileindex_path) != 0 && err == NULL)
2266 err = got_error_from_errno2("unlink",
2267 new_fileindex_path);
2268 free(new_fileindex_path);
2270 if (fileindex)
2271 got_fileindex_free(fileindex);
2272 unlockerr = lock_worktree(worktree, LOCK_SH);
2273 if (unlockerr && err == NULL)
2274 err = unlockerr;
2275 return err;
2278 static void
2279 free_commitable(struct got_commitable *ct)
2281 free(ct->path);
2282 free(ct->in_repo_path);
2283 free(ct->ondisk_path);
2284 free(ct->blob_id);
2285 free(ct->base_blob_id);
2286 free(ct->base_commit_id);
2287 free(ct);
2290 struct collect_commitables_arg {
2291 struct got_pathlist_head *commitable_paths;
2292 struct got_repository *repo;
2293 struct got_worktree *worktree;
2296 static const struct got_error *
2297 collect_commitables(void *arg, unsigned char status, const char *relpath,
2298 struct got_object_id *blob_id, struct got_object_id *commit_id)
2300 struct collect_commitables_arg *a = arg;
2301 const struct got_error *err = NULL;
2302 struct got_commitable *ct = NULL;
2303 struct got_pathlist_entry *new = NULL;
2304 char *parent_path = NULL, *path = NULL;
2305 struct stat sb;
2307 if (status == GOT_STATUS_CONFLICT)
2308 return got_error(GOT_ERR_COMMIT_CONFLICT);
2310 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2311 status != GOT_STATUS_DELETE)
2312 return NULL;
2314 if (asprintf(&path, "/%s", relpath) == -1) {
2315 err = got_error_from_errno("asprintf");
2316 goto done;
2318 if (strcmp(path, "/") == 0) {
2319 parent_path = strdup("");
2320 if (parent_path == NULL)
2321 return got_error_from_errno("strdup");
2322 } else {
2323 err = got_path_dirname(&parent_path, path);
2324 if (err)
2325 return err;
2328 ct = calloc(1, sizeof(*ct));
2329 if (ct == NULL) {
2330 err = got_error_from_errno("calloc");
2331 goto done;
2334 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2335 relpath) == -1) {
2336 err = got_error_from_errno("asprintf");
2337 goto done;
2339 if (status == GOT_STATUS_DELETE) {
2340 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2341 } else {
2342 if (lstat(ct->ondisk_path, &sb) != 0) {
2343 err = got_error_from_errno2("lstat", ct->ondisk_path);
2344 goto done;
2346 ct->mode = sb.st_mode;
2349 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2350 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2351 relpath) == -1) {
2352 err = got_error_from_errno("asprintf");
2353 goto done;
2356 ct->status = status;
2357 ct->blob_id = NULL; /* will be filled in when blob gets created */
2358 if (ct->status != GOT_STATUS_ADD) {
2359 ct->base_blob_id = got_object_id_dup(blob_id);
2360 if (ct->base_blob_id == NULL) {
2361 err = got_error_from_errno("got_object_id_dup");
2362 goto done;
2364 ct->base_commit_id = got_object_id_dup(commit_id);
2365 if (ct->base_commit_id == NULL) {
2366 err = got_error_from_errno("got_object_id_dup");
2367 goto done;
2370 ct->path = strdup(path);
2371 if (ct->path == NULL) {
2372 err = got_error_from_errno("strdup");
2373 goto done;
2375 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2376 done:
2377 if (ct && (err || new == NULL))
2378 free_commitable(ct);
2379 free(parent_path);
2380 free(path);
2381 return err;
2384 static const struct got_error *write_tree(struct got_object_id **,
2385 struct got_tree_object *, const char *, struct got_pathlist_head *,
2386 got_worktree_status_cb status_cb, void *status_arg,
2387 struct got_repository *);
2389 static const struct got_error *
2390 write_subtree(struct got_object_id **new_subtree_id,
2391 struct got_tree_entry *te, const char *parent_path,
2392 struct got_pathlist_head *commitable_paths,
2393 got_worktree_status_cb status_cb, void *status_arg,
2394 struct got_repository *repo)
2396 const struct got_error *err = NULL;
2397 struct got_tree_object *subtree;
2398 char *subpath;
2400 if (asprintf(&subpath, "%s%s%s", parent_path,
2401 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2402 return got_error_from_errno("asprintf");
2404 err = got_object_open_as_tree(&subtree, repo, te->id);
2405 if (err)
2406 return err;
2408 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2409 status_cb, status_arg, repo);
2410 got_object_tree_close(subtree);
2411 free(subpath);
2412 return err;
2415 static const struct got_error *
2416 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2418 const struct got_error *err = NULL;
2419 char *ct_parent_path = NULL;
2421 *match = 0;
2423 if (strchr(ct->path, '/') == NULL) {
2424 *match = got_path_is_root_dir(path);
2425 return NULL;
2428 err = got_path_dirname(&ct_parent_path, ct->path);
2429 if (err)
2430 return err;
2431 *match = (strcmp(path, ct_parent_path) == 0);
2432 free(ct_parent_path);
2433 return err;
2436 static mode_t
2437 get_ct_file_mode(struct got_commitable *ct)
2439 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2442 static const struct got_error *
2443 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2444 struct got_tree_entry *te, struct got_commitable *ct)
2446 const struct got_error *err = NULL;
2448 *new_te = NULL;
2450 err = got_object_tree_entry_dup(new_te, te);
2451 if (err)
2452 goto done;
2454 (*new_te)->mode = get_ct_file_mode(ct);
2456 free((*new_te)->id);
2457 (*new_te)->id = got_object_id_dup(ct->blob_id);
2458 if ((*new_te)->id == NULL) {
2459 err = got_error_from_errno("got_object_id_dup");
2460 goto done;
2462 done:
2463 if (err && *new_te) {
2464 got_object_tree_entry_close(*new_te);
2465 *new_te = NULL;
2467 return err;
2470 static const struct got_error *
2471 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2472 struct got_commitable *ct)
2474 const struct got_error *err = NULL;
2475 char *ct_name;
2477 *new_te = NULL;
2479 *new_te = calloc(1, sizeof(**new_te));
2480 if (*new_te == NULL)
2481 return got_error_from_errno("calloc");
2483 ct_name = basename(ct->path);
2484 if (ct_name == NULL) {
2485 err = got_error_from_errno2("basename", ct->path);
2486 goto done;
2488 (*new_te)->name = strdup(ct_name);
2489 if ((*new_te)->name == NULL) {
2490 err = got_error_from_errno("strdup");
2491 goto done;
2494 (*new_te)->mode = get_ct_file_mode(ct);
2496 (*new_te)->id = got_object_id_dup(ct->blob_id);
2497 if ((*new_te)->id == NULL) {
2498 err = got_error_from_errno("got_object_id_dup");
2499 goto done;
2501 done:
2502 if (err && *new_te) {
2503 got_object_tree_entry_close(*new_te);
2504 *new_te = NULL;
2506 return err;
2509 static const struct got_error *
2510 insert_tree_entry(struct got_tree_entry *new_te,
2511 struct got_pathlist_head *paths)
2513 const struct got_error *err = NULL;
2514 struct got_pathlist_entry *new_pe;
2516 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2517 if (err)
2518 return err;
2519 if (new_pe == NULL)
2520 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2521 return NULL;
2524 static const struct got_error *
2525 report_ct_status(struct got_commitable *ct,
2526 got_worktree_status_cb status_cb, void *status_arg)
2528 const char *ct_path = ct->path;
2529 while (ct_path[0] == '/')
2530 ct_path++;
2531 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2534 static const struct got_error *
2535 match_modified_subtree(int *modified, struct got_tree_entry *te,
2536 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2538 const struct got_error *err = NULL;
2539 struct got_pathlist_entry *pe;
2540 char *te_path;
2542 *modified = 0;
2544 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2545 got_path_is_root_dir(base_tree_path) ? "" : "/",
2546 te->name) == -1)
2547 return got_error_from_errno("asprintf");
2549 TAILQ_FOREACH(pe, commitable_paths, entry) {
2550 struct got_commitable *ct = pe->data;
2551 *modified = got_path_is_child(ct->in_repo_path, te_path,
2552 strlen(te_path));
2553 if (*modified)
2554 break;
2557 free(te_path);
2558 return err;
2561 static const struct got_error *
2562 match_deleted_or_modified_ct(struct got_commitable **ctp,
2563 struct got_tree_entry *te, const char *base_tree_path,
2564 struct got_pathlist_head *commitable_paths)
2566 const struct got_error *err = NULL;
2567 struct got_pathlist_entry *pe;
2569 *ctp = NULL;
2571 TAILQ_FOREACH(pe, commitable_paths, entry) {
2572 struct got_commitable *ct = pe->data;
2573 char *ct_name = NULL;
2574 int path_matches;
2576 if (ct->status != GOT_STATUS_MODIFY &&
2577 ct->status != GOT_STATUS_DELETE)
2578 continue;
2580 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2581 continue;
2583 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2584 if (err)
2585 return err;
2586 if (!path_matches)
2587 continue;
2589 ct_name = basename(pe->path);
2590 if (ct_name == NULL)
2591 return got_error_from_errno2("basename", pe->path);
2593 if (strcmp(te->name, ct_name) != 0)
2594 continue;
2596 *ctp = ct;
2597 break;
2600 return err;
2603 static const struct got_error *
2604 write_tree(struct got_object_id **new_tree_id,
2605 struct got_tree_object *base_tree, const char *path_base_tree,
2606 struct got_pathlist_head *commitable_paths,
2607 got_worktree_status_cb status_cb, void *status_arg,
2608 struct got_repository *repo)
2610 const struct got_error *err = NULL;
2611 const struct got_tree_entries *base_entries = NULL;
2612 struct got_pathlist_head paths;
2613 struct got_tree_entries new_tree_entries;
2614 struct got_tree_entry *te, *new_te = NULL;
2615 struct got_pathlist_entry *pe;
2617 TAILQ_INIT(&paths);
2618 new_tree_entries.nentries = 0;
2619 SIMPLEQ_INIT(&new_tree_entries.head);
2621 /* Insert, and recurse into, newly added entries first. */
2622 TAILQ_FOREACH(pe, commitable_paths, entry) {
2623 struct got_commitable *ct = pe->data;
2624 char *child_path = NULL, *slash;
2626 if (ct->status != GOT_STATUS_ADD ||
2627 (ct->flags & GOT_COMMITABLE_ADDED))
2628 continue;
2630 if (!got_path_is_child(pe->path, path_base_tree,
2631 strlen(path_base_tree)))
2632 continue;
2634 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2635 pe->path);
2636 if (err)
2637 goto done;
2639 slash = strchr(child_path, '/');
2640 if (slash == NULL) {
2641 err = alloc_added_blob_tree_entry(&new_te, ct);
2642 if (err)
2643 goto done;
2644 err = report_ct_status(ct, status_cb, status_arg);
2645 if (err)
2646 goto done;
2647 ct->flags |= GOT_COMMITABLE_ADDED;
2648 } else {
2649 char *subtree_path;
2651 *slash = '\0'; /* trim trailing path components */
2652 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2653 got_path_is_root_dir(path_base_tree) ? "" : "/",
2654 child_path) == -1) {
2655 err = got_error_from_errno("asprintf");
2656 goto done;
2659 new_te = calloc(1, sizeof(*new_te));
2660 new_te->mode = S_IFDIR;
2661 new_te->name = strdup(child_path);
2662 if (new_te->name == NULL) {
2663 err = got_error_from_errno("strdup");
2664 got_object_tree_entry_close(new_te);
2665 new_te = NULL;
2666 goto done;
2668 err = write_tree(&new_te->id, NULL, subtree_path,
2669 commitable_paths, status_cb, status_arg, repo);
2670 free(subtree_path);
2671 if (err) {
2672 got_object_tree_entry_close(new_te);
2673 new_te = NULL;
2674 goto done;
2677 err = insert_tree_entry(new_te, &paths);
2678 if (err)
2679 goto done;
2682 if (base_tree) {
2683 /* Handle modified and deleted entries. */
2684 base_entries = got_object_tree_get_entries(base_tree);
2685 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2686 struct got_commitable *ct = NULL;
2688 if (S_ISDIR(te->mode)) {
2689 int modified;
2690 err = got_object_tree_entry_dup(&new_te, te);
2691 if (err)
2692 goto done;
2693 err = match_modified_subtree(&modified, te,
2694 path_base_tree, commitable_paths);
2695 if (err)
2696 goto done;
2697 /* Avoid recursion into unmodified subtrees. */
2698 if (modified) {
2699 free(new_te->id);
2700 err = write_subtree(&new_te->id, te,
2701 path_base_tree, commitable_paths,
2702 status_cb, status_arg, repo);
2703 if (err)
2704 goto done;
2706 err = insert_tree_entry(new_te, &paths);
2707 if (err)
2708 goto done;
2709 continue;
2712 err = match_deleted_or_modified_ct(&ct, te,
2713 path_base_tree, commitable_paths);
2714 if (ct) {
2715 /* NB: Deleted entries get dropped here. */
2716 if (ct->status == GOT_STATUS_MODIFY) {
2717 err = alloc_modified_blob_tree_entry(
2718 &new_te, te, ct);
2719 if (err)
2720 goto done;
2721 err = insert_tree_entry(new_te, &paths);
2722 if (err)
2723 goto done;
2725 err = report_ct_status(ct, status_cb,
2726 status_arg);
2727 if (err)
2728 goto done;
2729 } else {
2730 /* Entry is unchanged; just copy it. */
2731 err = got_object_tree_entry_dup(&new_te, te);
2732 if (err)
2733 goto done;
2734 err = insert_tree_entry(new_te, &paths);
2735 if (err)
2736 goto done;
2741 /* Write new list of entries; deleted entries have been dropped. */
2742 TAILQ_FOREACH(pe, &paths, entry) {
2743 struct got_tree_entry *te = pe->data;
2744 new_tree_entries.nentries++;
2745 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2747 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2748 done:
2749 got_object_tree_entries_close(&new_tree_entries);
2750 got_pathlist_free(&paths);
2751 return err;
2754 static const struct got_error *
2755 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2756 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2758 const struct got_error *err = NULL;
2759 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2760 struct got_fileindex *fileindex = NULL;
2761 FILE *new_index = NULL;
2762 struct got_pathlist_entry *pe;
2764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2765 if (err)
2766 return err;
2768 err = got_opentemp_named(&new_fileindex_path, &new_index,
2769 fileindex_path);
2770 if (err)
2771 goto done;
2773 TAILQ_FOREACH(pe, commitable_paths, entry) {
2774 struct got_fileindex_entry *ie;
2775 struct got_commitable *ct = pe->data;
2777 ie = got_fileindex_entry_get(fileindex, pe->path);
2778 if (ie) {
2779 if (ct->status == GOT_STATUS_DELETE) {
2780 got_fileindex_entry_remove(fileindex, ie);
2781 got_fileindex_entry_free(ie);
2782 } else
2783 err = got_fileindex_entry_update(ie,
2784 ct->ondisk_path, ct->blob_id->sha1,
2785 new_base_commit_id->sha1, 1);
2786 } else {
2787 err = got_fileindex_entry_alloc(&ie,
2788 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2789 new_base_commit_id->sha1);
2790 if (err)
2791 goto done;
2792 err = got_fileindex_entry_add(fileindex, ie);
2793 if (err)
2794 goto done;
2798 err = got_fileindex_write(fileindex, new_index);
2799 if (err)
2800 goto done;
2802 if (rename(new_fileindex_path, fileindex_path) != 0) {
2803 err = got_error_from_errno3("rename", new_fileindex_path,
2804 fileindex_path);
2805 unlink(new_fileindex_path);
2806 goto done;
2809 free(new_fileindex_path);
2810 new_fileindex_path = NULL;
2812 done:
2813 if (new_fileindex_path)
2814 unlink(new_fileindex_path);
2815 if (new_index)
2816 fclose(new_index);
2817 free(new_fileindex_path);
2818 free(fileindex_path);
2819 got_fileindex_free(fileindex);
2820 return err;
2823 static const struct got_error *
2824 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2825 struct got_object_id *head_commit_id)
2827 const struct got_error *err = NULL;
2828 struct got_object_id *id_in_head;
2831 * Require that modified/deleted files are based on the branch head.
2832 * This requirement could be relaxed to force less update operations
2833 * on users but, for now, we want to play it safe and see how it goes.
2835 * If this check is relaxed, it must still ensure that no modifications
2836 * were made to files *and their parents* in commits between the file's
2837 * base commit and the branch head. Otherwise, tree conflicts will not
2838 * be detected reliably.
2840 if (ct->status != GOT_STATUS_ADD) {
2841 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
2842 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2843 return NULL;
2846 /* Require that added files don't exist in the branch head. */
2847 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
2848 ct->in_repo_path);
2849 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2850 return err;
2851 if (id_in_head) {
2852 free(id_in_head);
2853 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2856 free(id_in_head);
2857 return NULL;
2860 const struct got_error *
2861 got_worktree_commit(struct got_object_id **new_commit_id,
2862 struct got_worktree *worktree, const char *ondisk_path,
2863 const char *author, const char *committer,
2864 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2865 got_worktree_status_cb status_cb, void *status_arg,
2866 struct got_repository *repo)
2868 const struct got_error *err = NULL, *unlockerr = NULL;
2869 struct collect_commitables_arg cc_arg;
2870 struct got_pathlist_head commitable_paths;
2871 struct got_pathlist_entry *pe;
2872 char *relpath = NULL;
2873 const char *head_ref_name = NULL;
2874 struct got_reference *head_ref = NULL;
2875 struct got_commit_object *head_commit = NULL;
2876 struct got_object_id *head_commit_id = NULL;
2877 struct got_reference *head_ref2 = NULL;
2878 struct got_object_id *head_commit_id2 = NULL;
2879 struct got_tree_object *head_tree = NULL;
2880 struct got_object_id *new_tree_id = NULL;
2881 struct got_object_id_queue parent_ids;
2882 struct got_object_qid *pid = NULL;
2883 char *logmsg = NULL;
2885 *new_commit_id = NULL;
2887 TAILQ_INIT(&commitable_paths);
2888 SIMPLEQ_INIT(&parent_ids);
2890 if (ondisk_path) {
2891 err = got_path_skip_common_ancestor(&relpath,
2892 worktree->root_path, ondisk_path);
2893 if (err)
2894 return err;
2897 err = lock_worktree(worktree, LOCK_EX);
2898 if (err)
2899 goto done;
2901 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2902 if (err)
2903 goto done;
2904 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2905 if (err)
2906 goto done;
2908 cc_arg.commitable_paths = &commitable_paths;
2909 cc_arg.worktree = worktree;
2910 cc_arg.repo = repo;
2911 err = got_worktree_status(worktree, relpath ? relpath : "",
2912 repo, collect_commitables, &cc_arg, NULL, NULL);
2913 if (err)
2914 goto done;
2916 if (TAILQ_EMPTY(&commitable_paths)) {
2917 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2918 goto done;
2921 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2922 if (err)
2923 goto done;
2925 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2926 struct got_commitable *ct = pe->data;
2927 err = check_ct_out_of_date(ct, repo, head_commit_id);
2928 if (err)
2929 goto done;
2932 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2933 if (err)
2934 goto done;
2936 if (commit_msg_cb != NULL) {
2937 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2938 if (err)
2939 goto done;
2942 if (logmsg == NULL || strlen(logmsg) == 0) {
2943 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2944 goto done;
2947 /* Create blobs from added and modified files and record their IDs. */
2948 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2949 struct got_commitable *ct = pe->data;
2950 char *ondisk_path;
2952 if (ct->status != GOT_STATUS_ADD &&
2953 ct->status != GOT_STATUS_MODIFY)
2954 continue;
2956 if (asprintf(&ondisk_path, "%s/%s",
2957 worktree->root_path, pe->path) == -1) {
2958 err = got_error_from_errno("asprintf");
2959 goto done;
2961 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2962 free(ondisk_path);
2963 if (err)
2964 goto done;
2967 /* Recursively write new tree objects. */
2968 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2969 status_cb, status_arg, repo);
2970 if (err)
2971 goto done;
2973 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2974 if (err)
2975 goto done;
2976 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2977 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2978 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2979 got_object_qid_free(pid);
2980 if (logmsg != NULL)
2981 free(logmsg);
2982 if (err)
2983 goto done;
2985 /* Check if a concurrent commit to our branch has occurred. */
2986 head_ref_name = got_worktree_get_head_ref_name(worktree);
2987 if (head_ref_name == NULL) {
2988 err = got_error_from_errno("got_worktree_get_head_ref_name");
2989 goto done;
2991 /* Lock the reference here to prevent concurrent modification. */
2992 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
2993 if (err)
2994 goto done;
2995 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2996 if (err)
2997 goto done;
2998 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2999 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3000 goto done;
3002 /* Update branch head in repository. */
3003 err = got_ref_change_ref(head_ref2, *new_commit_id);
3004 if (err)
3005 goto done;
3006 err = got_ref_write(head_ref2, repo);
3007 if (err)
3008 goto done;
3010 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3011 if (err)
3012 goto done;
3014 err = ref_base_commit(worktree, repo);
3015 if (err)
3016 goto done;
3018 err = update_fileindex_after_commit(&commitable_paths,
3019 *new_commit_id, worktree);
3020 if (err)
3021 goto done;
3022 done:
3023 unlockerr = lock_worktree(worktree, LOCK_SH);
3024 if (unlockerr && err == NULL)
3025 err = unlockerr;
3026 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3027 struct got_commitable *ct = pe->data;
3028 free_commitable(ct);
3030 got_pathlist_free(&commitable_paths);
3031 if (head_tree)
3032 got_object_tree_close(head_tree);
3033 if (head_commit)
3034 got_object_commit_close(head_commit);
3035 free(relpath);
3036 free(head_commit_id);
3037 free(head_commit_id2);
3038 if (head_ref)
3039 got_ref_close(head_ref);
3040 if (head_ref2) {
3041 unlockerr = got_ref_unlock(head_ref2);
3042 if (unlockerr && err == NULL)
3043 err = unlockerr;
3044 got_ref_close(head_ref2);
3046 return err;
3049 const char *
3050 got_commitable_get_path(struct got_commitable *ct)
3052 return ct->path;
3055 unsigned int
3056 got_commitable_get_status(struct got_commitable *ct)
3058 return ct->status;