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 struct bump_base_commit_id_arg {
1451 struct got_object_id *base_commit_id;
1452 const char *path;
1453 size_t path_len;
1454 const char *entry_name;
1457 /* Bump base commit ID of all files within an updated part of the work tree. */
1458 static const struct got_error *
1459 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1461 struct bump_base_commit_id_arg *a = arg;
1463 if (a->entry_name) {
1464 if (strcmp(ie->path, a->path) != 0)
1465 return NULL;
1466 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1467 return NULL;
1469 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1470 return NULL;
1473 const struct got_error *
1474 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1475 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1476 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1478 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1479 struct got_commit_object *commit = NULL;
1480 struct got_object_id *tree_id = NULL;
1481 struct got_tree_object *tree = NULL;
1482 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1483 struct got_fileindex *fileindex = NULL;
1484 FILE *new_index = NULL;
1485 struct got_fileindex_diff_tree_cb diff_cb;
1486 struct diff_cb_arg arg;
1487 char *relpath = NULL, *entry_name = NULL;
1489 err = lock_worktree(worktree, LOCK_EX);
1490 if (err)
1491 return err;
1494 * Read the file index.
1495 * Checking out files is supposed to be an idempotent operation.
1496 * If the on-disk file index is incomplete we will try to complete it.
1498 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1499 if (err)
1500 goto done;
1502 err = got_opentemp_named(&new_fileindex_path, &new_index,
1503 fileindex_path);
1504 if (err)
1505 goto done;
1507 err = ref_base_commit(worktree, repo);
1508 if (err)
1509 goto done;
1511 err = got_object_open_as_commit(&commit, repo,
1512 worktree->base_commit_id);
1513 if (err)
1514 goto done;
1516 if (path[0]) {
1517 char *tree_path;
1518 int obj_type;
1519 relpath = strdup(path);
1520 if (relpath == NULL) {
1521 err = got_error_from_errno("strdup");
1522 goto done;
1524 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1525 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1526 path) == -1) {
1527 err = got_error_from_errno("asprintf");
1528 goto done;
1530 err = got_object_id_by_path(&tree_id, repo,
1531 worktree->base_commit_id, tree_path);
1532 free(tree_path);
1533 if (err)
1534 goto done;
1535 err = got_object_get_type(&obj_type, repo, tree_id);
1536 if (err)
1537 goto done;
1538 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1539 /* Split provided path into parent dir + entry name. */
1540 if (strchr(path, '/') == NULL) {
1541 relpath = strdup("");
1542 if (relpath == NULL) {
1543 err = got_error_from_errno("strdup");
1544 goto done;
1546 tree_path = strdup(worktree->path_prefix);
1547 if (tree_path == NULL) {
1548 err = got_error_from_errno("strdup");
1549 goto done;
1551 } else {
1552 err = got_path_dirname(&relpath, path);
1553 if (err)
1554 goto done;
1555 if (asprintf(&tree_path, "%s%s%s",
1556 worktree->path_prefix,
1557 got_path_is_root_dir(
1558 worktree->path_prefix) ? "" : "/",
1559 relpath) == -1) {
1560 err = got_error_from_errno("asprintf");
1561 goto done;
1564 err = got_object_id_by_path(&tree_id, repo,
1565 worktree->base_commit_id, tree_path);
1566 free(tree_path);
1567 if (err)
1568 goto done;
1569 entry_name = basename(path);
1570 if (entry_name == NULL) {
1571 err = got_error_from_errno2("basename", path);
1572 goto done;
1575 } else {
1576 relpath = strdup("");
1577 if (relpath == NULL) {
1578 err = got_error_from_errno("strdup");
1579 goto done;
1581 err = got_object_id_by_path(&tree_id, repo,
1582 worktree->base_commit_id, worktree->path_prefix);
1583 if (err)
1584 goto done;
1587 err = got_object_open_as_tree(&tree, repo, tree_id);
1588 if (err)
1589 goto done;
1591 if (entry_name &&
1592 got_object_tree_find_entry(tree, entry_name) == NULL) {
1593 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1594 goto done;
1597 diff_cb.diff_old_new = diff_old_new;
1598 diff_cb.diff_old = diff_old;
1599 diff_cb.diff_new = diff_new;
1600 arg.fileindex = fileindex;
1601 arg.worktree = worktree;
1602 arg.repo = repo;
1603 arg.progress_cb = progress_cb;
1604 arg.progress_arg = progress_arg;
1605 arg.cancel_cb = cancel_cb;
1606 arg.cancel_arg = cancel_arg;
1607 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1608 entry_name, repo, &diff_cb, &arg);
1610 if (checkout_err == NULL) {
1611 struct bump_base_commit_id_arg bbc_arg;
1612 bbc_arg.base_commit_id = worktree->base_commit_id;
1613 bbc_arg.entry_name = entry_name;
1614 bbc_arg.path = path;
1615 bbc_arg.path_len = strlen(path);
1616 err = got_fileindex_for_each_entry_safe(fileindex,
1617 bump_base_commit_id, &bbc_arg);
1618 if (err)
1619 goto done;
1622 /* Try to sync the fileindex back to disk in any case. */
1623 err = got_fileindex_write(fileindex, new_index);
1624 if (err)
1625 goto done;
1627 if (rename(new_fileindex_path, fileindex_path) != 0) {
1628 err = got_error_from_errno3("rename", new_fileindex_path,
1629 fileindex_path);
1630 unlink(new_fileindex_path);
1631 goto done;
1634 free(new_fileindex_path);
1635 new_fileindex_path = NULL;
1637 done:
1638 free(relpath);
1639 if (tree)
1640 got_object_tree_close(tree);
1641 if (commit)
1642 got_object_commit_close(commit);
1643 if (new_fileindex_path)
1644 unlink(new_fileindex_path);
1645 if (new_index)
1646 fclose(new_index);
1647 free(new_fileindex_path);
1648 free(fileindex_path);
1649 got_fileindex_free(fileindex);
1650 if (checkout_err)
1651 err = checkout_err;
1652 unlockerr = lock_worktree(worktree, LOCK_SH);
1653 if (unlockerr && err == NULL)
1654 err = unlockerr;
1655 return err;
1658 struct diff_dir_cb_arg {
1659 struct got_fileindex *fileindex;
1660 struct got_worktree *worktree;
1661 const char *status_path;
1662 size_t status_path_len;
1663 struct got_repository *repo;
1664 got_worktree_status_cb status_cb;
1665 void *status_arg;
1666 got_worktree_cancel_cb cancel_cb;
1667 void *cancel_arg;
1670 static const struct got_error *
1671 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1672 got_worktree_status_cb status_cb, void *status_arg,
1673 struct got_repository *repo)
1675 const struct got_error *err = NULL;
1676 unsigned char status = GOT_STATUS_NO_CHANGE;
1677 struct stat sb;
1678 struct got_object_id blob_id, commit_id;
1680 err = get_file_status(&status, &sb, ie, abspath, repo);
1681 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1682 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1683 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1684 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1685 &commit_id);
1687 return err;
1690 static const struct got_error *
1691 status_old_new(void *arg, struct got_fileindex_entry *ie,
1692 struct dirent *de, const char *parent_path)
1694 const struct got_error *err = NULL;
1695 struct diff_dir_cb_arg *a = arg;
1696 char *abspath;
1698 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1699 return got_error(GOT_ERR_CANCELLED);
1701 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1702 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1703 return NULL;
1705 if (parent_path[0]) {
1706 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1707 parent_path, de->d_name) == -1)
1708 return got_error_from_errno("asprintf");
1709 } else {
1710 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1711 de->d_name) == -1)
1712 return got_error_from_errno("asprintf");
1715 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1716 a->repo);
1717 free(abspath);
1718 return err;
1721 static const struct got_error *
1722 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1724 struct diff_dir_cb_arg *a = arg;
1725 struct got_object_id blob_id, commit_id;
1726 unsigned char status;
1728 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1729 return got_error(GOT_ERR_CANCELLED);
1731 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1732 return NULL;
1734 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1735 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1736 if (got_fileindex_entry_has_file_on_disk(ie))
1737 status = GOT_STATUS_MISSING;
1738 else
1739 status = GOT_STATUS_DELETE;
1740 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1741 &commit_id);
1744 static const struct got_error *
1745 status_new(void *arg, struct dirent *de, const char *parent_path)
1747 const struct got_error *err = NULL;
1748 struct diff_dir_cb_arg *a = arg;
1749 char *path = NULL;
1751 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1752 return got_error(GOT_ERR_CANCELLED);
1754 if (de->d_type == DT_DIR)
1755 return NULL;
1757 /* XXX ignore symlinks for now */
1758 if (de->d_type == DT_LNK)
1759 return NULL;
1761 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1762 return NULL;
1764 if (parent_path[0]) {
1765 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1766 return got_error_from_errno("asprintf");
1767 } else {
1768 path = de->d_name;
1771 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1772 NULL, NULL);
1773 if (parent_path[0])
1774 free(path);
1775 return err;
1778 const struct got_error *
1779 got_worktree_status(struct got_worktree *worktree, const char *path,
1780 struct got_repository *repo, got_worktree_status_cb status_cb,
1781 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1783 const struct got_error *err = NULL;
1784 DIR *workdir = NULL;
1785 char *fileindex_path = NULL;
1786 struct got_fileindex *fileindex = NULL;
1787 FILE *index = NULL;
1788 struct got_fileindex_diff_dir_cb fdiff_cb;
1789 struct diff_dir_cb_arg arg;
1790 char *ondisk_path = NULL;
1792 fileindex = got_fileindex_alloc();
1793 if (fileindex == NULL) {
1794 err = got_error_from_errno("got_fileindex_alloc");
1795 goto done;
1798 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1799 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1800 err = got_error_from_errno("asprintf");
1801 fileindex_path = NULL;
1802 goto done;
1805 index = fopen(fileindex_path, "rb");
1806 if (index == NULL) {
1807 if (errno != ENOENT) {
1808 err = got_error_from_errno2("fopen", fileindex_path);
1809 goto done;
1811 } else {
1812 err = got_fileindex_read(fileindex, index);
1813 fclose(index);
1814 if (err)
1815 goto done;
1818 if (asprintf(&ondisk_path, "%s%s%s",
1819 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1820 err = got_error_from_errno("asprintf");
1821 goto done;
1823 workdir = opendir(ondisk_path);
1824 if (workdir == NULL) {
1825 if (errno == ENOTDIR || errno == ENOENT) {
1826 struct got_fileindex_entry *ie;
1827 ie = got_fileindex_entry_get(fileindex, path);
1828 if (ie == NULL) {
1829 err = got_error(GOT_ERR_BAD_PATH);
1830 goto done;
1832 err = report_file_status(ie, ondisk_path,
1833 status_cb, status_arg, repo);
1834 goto done;
1835 } else {
1836 err = got_error_from_errno2("opendir", ondisk_path);
1837 goto done;
1840 fdiff_cb.diff_old_new = status_old_new;
1841 fdiff_cb.diff_old = status_old;
1842 fdiff_cb.diff_new = status_new;
1843 arg.fileindex = fileindex;
1844 arg.worktree = worktree;
1845 arg.status_path = path;
1846 arg.status_path_len = strlen(path);
1847 arg.repo = repo;
1848 arg.status_cb = status_cb;
1849 arg.status_arg = status_arg;
1850 arg.cancel_cb = cancel_cb;
1851 arg.cancel_arg = cancel_arg;
1852 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1853 path, repo, &fdiff_cb, &arg);
1854 done:
1855 if (workdir)
1856 closedir(workdir);
1857 free(ondisk_path);
1858 free(fileindex_path);
1859 got_fileindex_free(fileindex);
1860 return err;
1863 const struct got_error *
1864 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1865 const char *arg)
1867 const struct got_error *err = NULL;
1868 char *resolved, *path = NULL;
1869 size_t len;
1871 *wt_path = NULL;
1873 resolved = realpath(arg, NULL);
1874 if (resolved == NULL)
1875 return got_error_from_errno2("realpath", arg);
1877 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1878 strlen(got_worktree_get_root_path(worktree)))) {
1879 err = got_error(GOT_ERR_BAD_PATH);
1880 goto done;
1883 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
1884 err = got_path_skip_common_ancestor(&path,
1885 got_worktree_get_root_path(worktree), resolved);
1886 if (err)
1887 goto done;
1888 } else {
1889 path = strdup("");
1890 if (path == NULL) {
1891 err = got_error_from_errno("strdup");
1892 goto done;
1896 /* XXX status walk can't deal with trailing slash! */
1897 len = strlen(path);
1898 while (path[len - 1] == '/') {
1899 path[len - 1] = '\0';
1900 len--;
1902 done:
1903 free(resolved);
1904 if (err == NULL)
1905 *wt_path = path;
1906 else
1907 free(path);
1908 return err;
1911 const struct got_error *
1912 got_worktree_schedule_add(struct got_worktree *worktree,
1913 struct got_pathlist_head *ondisk_paths,
1914 got_worktree_status_cb status_cb, void *status_arg,
1915 struct got_repository *repo)
1917 struct got_fileindex *fileindex = NULL;
1918 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1919 FILE *index = NULL, *new_index = NULL;
1920 const struct got_error *err = NULL, *unlockerr = NULL;
1921 struct got_pathlist_entry *pe;
1923 err = lock_worktree(worktree, LOCK_EX);
1924 if (err)
1925 return err;
1928 fileindex = got_fileindex_alloc();
1929 if (fileindex == NULL) {
1930 err = got_error_from_errno("got_fileindex_alloc");
1931 goto done;
1934 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1935 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1936 err = got_error_from_errno("asprintf");
1937 fileindex_path = NULL;
1938 goto done;
1941 index = fopen(fileindex_path, "rb");
1942 if (index == NULL) {
1943 err = got_error_from_errno2("fopen", fileindex_path);
1944 goto done;
1947 err = got_fileindex_read(fileindex, index);
1948 if (err)
1949 goto done;
1951 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1952 struct got_fileindex_entry *ie = NULL;
1953 char *relpath;
1955 err = got_path_skip_common_ancestor(&relpath,
1956 got_worktree_get_root_path(worktree), pe->path);
1957 if (err)
1958 goto done;
1960 /* Re-adding an existing entry is a no-op. */
1961 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1962 continue;
1964 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1965 NULL, NULL);
1966 free(relpath);
1967 if (err)
1968 goto done;
1970 err = got_fileindex_entry_add(fileindex, ie);
1971 if (err) {
1972 got_fileindex_entry_free(ie);
1973 goto done;
1976 err = report_file_status(ie, pe->path, status_cb, status_arg,
1977 repo);
1978 if (err)
1979 goto done;
1982 err = got_opentemp_named(&new_fileindex_path, &new_index,
1983 fileindex_path);
1984 if (err)
1985 goto done;
1987 err = got_fileindex_write(fileindex, new_index);
1988 if (err)
1989 goto done;
1991 if (rename(new_fileindex_path, fileindex_path) != 0) {
1992 err = got_error_from_errno3("rename", new_fileindex_path,
1993 fileindex_path);
1994 goto done;
1997 free(new_fileindex_path);
1998 new_fileindex_path = NULL;
2000 done:
2001 if (index) {
2002 if (fclose(index) != 0 && err == NULL)
2003 err = got_error_from_errno("fclose");
2005 if (new_fileindex_path) {
2006 if (unlink(new_fileindex_path) != 0 && err == NULL)
2007 err = got_error_from_errno2("unlink",
2008 new_fileindex_path);
2009 free(new_fileindex_path);
2011 if (fileindex)
2012 got_fileindex_free(fileindex);
2013 unlockerr = lock_worktree(worktree, LOCK_SH);
2014 if (unlockerr && err == NULL)
2015 err = unlockerr;
2016 return err;
2019 const struct got_error *
2020 got_worktree_schedule_delete(struct got_worktree *worktree,
2021 const char *ondisk_path, int delete_local_mods,
2022 got_worktree_status_cb status_cb, void *status_arg,
2023 struct got_repository *repo)
2025 struct got_fileindex *fileindex = NULL;
2026 struct got_fileindex_entry *ie = NULL;
2027 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2028 FILE *index = NULL, *new_index = NULL;
2029 const struct got_error *err = NULL, *unlockerr = NULL;
2030 unsigned char status;
2031 struct stat sb;
2033 err = lock_worktree(worktree, LOCK_EX);
2034 if (err)
2035 return err;
2037 err = got_path_skip_common_ancestor(&relpath,
2038 got_worktree_get_root_path(worktree), ondisk_path);
2039 if (err)
2040 goto done;
2042 fileindex = got_fileindex_alloc();
2043 if (fileindex == NULL) {
2044 err = got_error_from_errno("got_fileindex_alloc");
2045 goto done;
2048 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2049 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2050 err = got_error_from_errno("asprintf");
2051 fileindex_path = NULL;
2052 goto done;
2055 index = fopen(fileindex_path, "rb");
2056 if (index == NULL) {
2057 err = got_error_from_errno2("fopen", fileindex_path);
2058 goto done;
2061 err = got_fileindex_read(fileindex, index);
2062 if (err)
2063 goto done;
2065 ie = got_fileindex_entry_get(fileindex, relpath);
2066 if (ie == NULL) {
2067 err = got_error(GOT_ERR_BAD_PATH);
2068 goto done;
2071 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2072 if (err)
2073 goto done;
2075 if (status != GOT_STATUS_NO_CHANGE) {
2076 if (status == GOT_STATUS_DELETE) {
2077 err = got_error_set_errno(ENOENT, ondisk_path);
2078 goto done;
2080 if (status != GOT_STATUS_MODIFY) {
2081 err = got_error(GOT_ERR_FILE_STATUS);
2082 goto done;
2084 if (!delete_local_mods) {
2085 err = got_error(GOT_ERR_FILE_MODIFIED);
2086 goto done;
2090 if (unlink(ondisk_path) != 0) {
2091 err = got_error_from_errno2("unlink", ondisk_path);
2092 goto done;
2095 got_fileindex_entry_mark_deleted_from_disk(ie);
2097 err = got_opentemp_named(&new_fileindex_path, &new_index,
2098 fileindex_path);
2099 if (err)
2100 goto done;
2102 err = got_fileindex_write(fileindex, new_index);
2103 if (err)
2104 goto done;
2106 if (rename(new_fileindex_path, fileindex_path) != 0) {
2107 err = got_error_from_errno3("rename", new_fileindex_path,
2108 fileindex_path);
2109 goto done;
2112 free(new_fileindex_path);
2113 new_fileindex_path = NULL;
2115 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2116 done:
2117 free(relpath);
2118 if (index) {
2119 if (fclose(index) != 0 && err == NULL)
2120 err = got_error_from_errno("fclose");
2122 if (new_fileindex_path) {
2123 if (unlink(new_fileindex_path) != 0 && err == NULL)
2124 err = got_error_from_errno2("unlink",
2125 new_fileindex_path);
2126 free(new_fileindex_path);
2128 if (fileindex)
2129 got_fileindex_free(fileindex);
2130 unlockerr = lock_worktree(worktree, LOCK_SH);
2131 if (unlockerr && err == NULL)
2132 err = unlockerr;
2133 return err;
2136 const struct got_error *
2137 got_worktree_revert(struct got_worktree *worktree,
2138 const char *ondisk_path,
2139 got_worktree_checkout_cb progress_cb, void *progress_arg,
2140 struct got_repository *repo)
2142 struct got_fileindex *fileindex = NULL;
2143 struct got_fileindex_entry *ie = NULL;
2144 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2145 char *tree_path = NULL, *parent_path, *te_name;
2146 FILE *index = NULL, *new_index = NULL;
2147 const struct got_error *err = NULL, *unlockerr = NULL;
2148 struct got_tree_object *tree = NULL;
2149 struct got_object_id id, *tree_id = NULL;
2150 const struct got_tree_entry *te;
2151 struct got_blob_object *blob = NULL;
2152 unsigned char status;
2153 struct stat sb;
2155 err = lock_worktree(worktree, LOCK_EX);
2156 if (err)
2157 return err;
2159 err = got_path_skip_common_ancestor(&relpath,
2160 got_worktree_get_root_path(worktree), ondisk_path);
2161 if (err)
2162 goto done;
2164 fileindex = got_fileindex_alloc();
2165 if (fileindex == NULL) {
2166 err = got_error_from_errno("got_fileindex_alloc");
2167 goto done;
2170 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2171 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2172 err = got_error_from_errno("asprintf");
2173 fileindex_path = NULL;
2174 goto done;
2177 index = fopen(fileindex_path, "rb");
2178 if (index == NULL) {
2179 err = got_error_from_errno2("fopen", fileindex_path);
2180 goto done;
2183 err = got_fileindex_read(fileindex, index);
2184 if (err)
2185 goto done;
2187 ie = got_fileindex_entry_get(fileindex, relpath);
2188 if (ie == NULL) {
2189 err = got_error(GOT_ERR_BAD_PATH);
2190 goto done;
2193 /* Construct in-repository path of tree which contains this blob. */
2194 err = got_path_dirname(&parent_path, ie->path);
2195 if (err) {
2196 if (err->code != GOT_ERR_BAD_PATH)
2197 goto done;
2198 parent_path = "/";
2200 if (got_path_is_root_dir(worktree->path_prefix)) {
2201 tree_path = strdup(parent_path);
2202 if (tree_path == NULL) {
2203 err = got_error_from_errno("strdup");
2204 goto done;
2206 } else {
2207 if (got_path_is_root_dir(parent_path)) {
2208 tree_path = strdup(worktree->path_prefix);
2209 if (tree_path == NULL) {
2210 err = got_error_from_errno("strdup");
2211 goto done;
2213 } else {
2214 if (asprintf(&tree_path, "%s/%s",
2215 worktree->path_prefix, parent_path) == -1) {
2216 err = got_error_from_errno("asprintf");
2217 goto done;
2222 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2223 tree_path);
2224 if (err)
2225 goto done;
2227 err = got_object_open_as_tree(&tree, repo, tree_id);
2228 if (err)
2229 goto done;
2231 te_name = basename(ie->path);
2232 if (te_name == NULL) {
2233 err = got_error_from_errno2("basename", ie->path);
2234 goto done;
2237 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2238 if (err)
2239 goto done;
2241 te = got_object_tree_find_entry(tree, te_name);
2242 if (te == NULL && status != GOT_STATUS_ADD) {
2243 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2244 goto done;
2247 switch (status) {
2248 case GOT_STATUS_ADD:
2249 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2250 got_fileindex_entry_remove(fileindex, ie);
2251 break;
2252 case GOT_STATUS_DELETE:
2253 case GOT_STATUS_MODIFY:
2254 case GOT_STATUS_CONFLICT:
2255 case GOT_STATUS_MISSING:
2256 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2257 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2258 if (err)
2259 goto done;
2260 err = install_blob(worktree, ondisk_path, ie->path,
2261 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2262 progress_arg);
2263 if (err)
2264 goto done;
2265 if (status == GOT_STATUS_DELETE) {
2266 err = update_blob_fileindex_entry(worktree,
2267 fileindex, ie, ondisk_path, ie->path, blob, 1);
2268 if (err)
2269 goto done;
2271 break;
2272 default:
2273 goto done;
2276 err = got_opentemp_named(&new_fileindex_path, &new_index,
2277 fileindex_path);
2278 if (err)
2279 goto done;
2281 err = got_fileindex_write(fileindex, new_index);
2282 if (err)
2283 goto done;
2285 if (rename(new_fileindex_path, fileindex_path) != 0) {
2286 err = got_error_from_errno3("rename", new_fileindex_path,
2287 fileindex_path);
2288 goto done;
2291 free(new_fileindex_path);
2292 new_fileindex_path = NULL;
2293 done:
2294 free(relpath);
2295 free(tree_path);
2296 if (blob)
2297 got_object_blob_close(blob);
2298 if (tree)
2299 got_object_tree_close(tree);
2300 free(tree_id);
2301 if (index) {
2302 if (fclose(index) != 0 && err == NULL)
2303 err = got_error_from_errno("fclose");
2305 if (new_fileindex_path) {
2306 if (unlink(new_fileindex_path) != 0 && err == NULL)
2307 err = got_error_from_errno2("unlink",
2308 new_fileindex_path);
2309 free(new_fileindex_path);
2311 if (fileindex)
2312 got_fileindex_free(fileindex);
2313 unlockerr = lock_worktree(worktree, LOCK_SH);
2314 if (unlockerr && err == NULL)
2315 err = unlockerr;
2316 return err;
2319 static void
2320 free_commitable(struct got_commitable *ct)
2322 free(ct->path);
2323 free(ct->in_repo_path);
2324 free(ct->ondisk_path);
2325 free(ct->blob_id);
2326 free(ct->base_blob_id);
2327 free(ct->base_commit_id);
2328 free(ct);
2331 struct collect_commitables_arg {
2332 struct got_pathlist_head *commitable_paths;
2333 struct got_repository *repo;
2334 struct got_worktree *worktree;
2337 static const struct got_error *
2338 collect_commitables(void *arg, unsigned char status, const char *relpath,
2339 struct got_object_id *blob_id, struct got_object_id *commit_id)
2341 struct collect_commitables_arg *a = arg;
2342 const struct got_error *err = NULL;
2343 struct got_commitable *ct = NULL;
2344 struct got_pathlist_entry *new = NULL;
2345 char *parent_path = NULL, *path = NULL;
2346 struct stat sb;
2348 if (status == GOT_STATUS_CONFLICT)
2349 return got_error(GOT_ERR_COMMIT_CONFLICT);
2351 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2352 status != GOT_STATUS_DELETE)
2353 return NULL;
2355 if (asprintf(&path, "/%s", relpath) == -1) {
2356 err = got_error_from_errno("asprintf");
2357 goto done;
2359 if (strcmp(path, "/") == 0) {
2360 parent_path = strdup("");
2361 if (parent_path == NULL)
2362 return got_error_from_errno("strdup");
2363 } else {
2364 err = got_path_dirname(&parent_path, path);
2365 if (err)
2366 return err;
2369 ct = calloc(1, sizeof(*ct));
2370 if (ct == NULL) {
2371 err = got_error_from_errno("calloc");
2372 goto done;
2375 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2376 relpath) == -1) {
2377 err = got_error_from_errno("asprintf");
2378 goto done;
2380 if (status == GOT_STATUS_DELETE) {
2381 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2382 } else {
2383 if (lstat(ct->ondisk_path, &sb) != 0) {
2384 err = got_error_from_errno2("lstat", ct->ondisk_path);
2385 goto done;
2387 ct->mode = sb.st_mode;
2390 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2391 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2392 relpath) == -1) {
2393 err = got_error_from_errno("asprintf");
2394 goto done;
2397 ct->status = status;
2398 ct->blob_id = NULL; /* will be filled in when blob gets created */
2399 if (ct->status != GOT_STATUS_ADD) {
2400 ct->base_blob_id = got_object_id_dup(blob_id);
2401 if (ct->base_blob_id == NULL) {
2402 err = got_error_from_errno("got_object_id_dup");
2403 goto done;
2405 ct->base_commit_id = got_object_id_dup(commit_id);
2406 if (ct->base_commit_id == NULL) {
2407 err = got_error_from_errno("got_object_id_dup");
2408 goto done;
2411 ct->path = strdup(path);
2412 if (ct->path == NULL) {
2413 err = got_error_from_errno("strdup");
2414 goto done;
2416 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2417 done:
2418 if (ct && (err || new == NULL))
2419 free_commitable(ct);
2420 free(parent_path);
2421 free(path);
2422 return err;
2425 static const struct got_error *write_tree(struct got_object_id **,
2426 struct got_tree_object *, const char *, struct got_pathlist_head *,
2427 got_worktree_status_cb status_cb, void *status_arg,
2428 struct got_repository *);
2430 static const struct got_error *
2431 write_subtree(struct got_object_id **new_subtree_id,
2432 struct got_tree_entry *te, const char *parent_path,
2433 struct got_pathlist_head *commitable_paths,
2434 got_worktree_status_cb status_cb, void *status_arg,
2435 struct got_repository *repo)
2437 const struct got_error *err = NULL;
2438 struct got_tree_object *subtree;
2439 char *subpath;
2441 if (asprintf(&subpath, "%s%s%s", parent_path,
2442 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2443 return got_error_from_errno("asprintf");
2445 err = got_object_open_as_tree(&subtree, repo, te->id);
2446 if (err)
2447 return err;
2449 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2450 status_cb, status_arg, repo);
2451 got_object_tree_close(subtree);
2452 free(subpath);
2453 return err;
2456 static const struct got_error *
2457 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2459 const struct got_error *err = NULL;
2460 char *ct_parent_path = NULL;
2462 *match = 0;
2464 if (strchr(ct->path, '/') == NULL) {
2465 *match = got_path_is_root_dir(path);
2466 return NULL;
2469 err = got_path_dirname(&ct_parent_path, ct->path);
2470 if (err)
2471 return err;
2472 *match = (strcmp(path, ct_parent_path) == 0);
2473 free(ct_parent_path);
2474 return err;
2477 static mode_t
2478 get_ct_file_mode(struct got_commitable *ct)
2480 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2483 static const struct got_error *
2484 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2485 struct got_tree_entry *te, struct got_commitable *ct)
2487 const struct got_error *err = NULL;
2489 *new_te = NULL;
2491 err = got_object_tree_entry_dup(new_te, te);
2492 if (err)
2493 goto done;
2495 (*new_te)->mode = get_ct_file_mode(ct);
2497 free((*new_te)->id);
2498 (*new_te)->id = got_object_id_dup(ct->blob_id);
2499 if ((*new_te)->id == NULL) {
2500 err = got_error_from_errno("got_object_id_dup");
2501 goto done;
2503 done:
2504 if (err && *new_te) {
2505 got_object_tree_entry_close(*new_te);
2506 *new_te = NULL;
2508 return err;
2511 static const struct got_error *
2512 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2513 struct got_commitable *ct)
2515 const struct got_error *err = NULL;
2516 char *ct_name;
2518 *new_te = NULL;
2520 *new_te = calloc(1, sizeof(**new_te));
2521 if (*new_te == NULL)
2522 return got_error_from_errno("calloc");
2524 ct_name = basename(ct->path);
2525 if (ct_name == NULL) {
2526 err = got_error_from_errno2("basename", ct->path);
2527 goto done;
2529 (*new_te)->name = strdup(ct_name);
2530 if ((*new_te)->name == NULL) {
2531 err = got_error_from_errno("strdup");
2532 goto done;
2535 (*new_te)->mode = get_ct_file_mode(ct);
2537 (*new_te)->id = got_object_id_dup(ct->blob_id);
2538 if ((*new_te)->id == NULL) {
2539 err = got_error_from_errno("got_object_id_dup");
2540 goto done;
2542 done:
2543 if (err && *new_te) {
2544 got_object_tree_entry_close(*new_te);
2545 *new_te = NULL;
2547 return err;
2550 static const struct got_error *
2551 insert_tree_entry(struct got_tree_entry *new_te,
2552 struct got_pathlist_head *paths)
2554 const struct got_error *err = NULL;
2555 struct got_pathlist_entry *new_pe;
2557 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2558 if (err)
2559 return err;
2560 if (new_pe == NULL)
2561 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2562 return NULL;
2565 static const struct got_error *
2566 report_ct_status(struct got_commitable *ct,
2567 got_worktree_status_cb status_cb, void *status_arg)
2569 const char *ct_path = ct->path;
2570 while (ct_path[0] == '/')
2571 ct_path++;
2572 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2575 static const struct got_error *
2576 match_modified_subtree(int *modified, struct got_tree_entry *te,
2577 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2579 const struct got_error *err = NULL;
2580 struct got_pathlist_entry *pe;
2581 char *te_path;
2583 *modified = 0;
2585 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2586 got_path_is_root_dir(base_tree_path) ? "" : "/",
2587 te->name) == -1)
2588 return got_error_from_errno("asprintf");
2590 TAILQ_FOREACH(pe, commitable_paths, entry) {
2591 struct got_commitable *ct = pe->data;
2592 *modified = got_path_is_child(ct->in_repo_path, te_path,
2593 strlen(te_path));
2594 if (*modified)
2595 break;
2598 free(te_path);
2599 return err;
2602 static const struct got_error *
2603 match_deleted_or_modified_ct(struct got_commitable **ctp,
2604 struct got_tree_entry *te, const char *base_tree_path,
2605 struct got_pathlist_head *commitable_paths)
2607 const struct got_error *err = NULL;
2608 struct got_pathlist_entry *pe;
2610 *ctp = NULL;
2612 TAILQ_FOREACH(pe, commitable_paths, entry) {
2613 struct got_commitable *ct = pe->data;
2614 char *ct_name = NULL;
2615 int path_matches;
2617 if (ct->status != GOT_STATUS_MODIFY &&
2618 ct->status != GOT_STATUS_DELETE)
2619 continue;
2621 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2622 continue;
2624 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2625 if (err)
2626 return err;
2627 if (!path_matches)
2628 continue;
2630 ct_name = basename(pe->path);
2631 if (ct_name == NULL)
2632 return got_error_from_errno2("basename", pe->path);
2634 if (strcmp(te->name, ct_name) != 0)
2635 continue;
2637 *ctp = ct;
2638 break;
2641 return err;
2644 static const struct got_error *
2645 write_tree(struct got_object_id **new_tree_id,
2646 struct got_tree_object *base_tree, const char *path_base_tree,
2647 struct got_pathlist_head *commitable_paths,
2648 got_worktree_status_cb status_cb, void *status_arg,
2649 struct got_repository *repo)
2651 const struct got_error *err = NULL;
2652 const struct got_tree_entries *base_entries = NULL;
2653 struct got_pathlist_head paths;
2654 struct got_tree_entries new_tree_entries;
2655 struct got_tree_entry *te, *new_te = NULL;
2656 struct got_pathlist_entry *pe;
2658 TAILQ_INIT(&paths);
2659 new_tree_entries.nentries = 0;
2660 SIMPLEQ_INIT(&new_tree_entries.head);
2662 /* Insert, and recurse into, newly added entries first. */
2663 TAILQ_FOREACH(pe, commitable_paths, entry) {
2664 struct got_commitable *ct = pe->data;
2665 char *child_path = NULL, *slash;
2667 if (ct->status != GOT_STATUS_ADD ||
2668 (ct->flags & GOT_COMMITABLE_ADDED))
2669 continue;
2671 if (!got_path_is_child(pe->path, path_base_tree,
2672 strlen(path_base_tree)))
2673 continue;
2675 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2676 pe->path);
2677 if (err)
2678 goto done;
2680 slash = strchr(child_path, '/');
2681 if (slash == NULL) {
2682 err = alloc_added_blob_tree_entry(&new_te, ct);
2683 if (err)
2684 goto done;
2685 err = report_ct_status(ct, status_cb, status_arg);
2686 if (err)
2687 goto done;
2688 ct->flags |= GOT_COMMITABLE_ADDED;
2689 } else {
2690 char *subtree_path;
2692 *slash = '\0'; /* trim trailing path components */
2693 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2694 got_path_is_root_dir(path_base_tree) ? "" : "/",
2695 child_path) == -1) {
2696 err = got_error_from_errno("asprintf");
2697 goto done;
2700 new_te = calloc(1, sizeof(*new_te));
2701 new_te->mode = S_IFDIR;
2702 new_te->name = strdup(child_path);
2703 if (new_te->name == NULL) {
2704 err = got_error_from_errno("strdup");
2705 got_object_tree_entry_close(new_te);
2706 new_te = NULL;
2707 goto done;
2709 err = write_tree(&new_te->id, NULL, subtree_path,
2710 commitable_paths, status_cb, status_arg, repo);
2711 free(subtree_path);
2712 if (err) {
2713 got_object_tree_entry_close(new_te);
2714 new_te = NULL;
2715 goto done;
2718 err = insert_tree_entry(new_te, &paths);
2719 if (err)
2720 goto done;
2723 if (base_tree) {
2724 /* Handle modified and deleted entries. */
2725 base_entries = got_object_tree_get_entries(base_tree);
2726 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2727 struct got_commitable *ct = NULL;
2729 if (S_ISDIR(te->mode)) {
2730 int modified;
2731 err = got_object_tree_entry_dup(&new_te, te);
2732 if (err)
2733 goto done;
2734 err = match_modified_subtree(&modified, te,
2735 path_base_tree, commitable_paths);
2736 if (err)
2737 goto done;
2738 /* Avoid recursion into unmodified subtrees. */
2739 if (modified) {
2740 free(new_te->id);
2741 err = write_subtree(&new_te->id, te,
2742 path_base_tree, commitable_paths,
2743 status_cb, status_arg, repo);
2744 if (err)
2745 goto done;
2747 err = insert_tree_entry(new_te, &paths);
2748 if (err)
2749 goto done;
2750 continue;
2753 err = match_deleted_or_modified_ct(&ct, te,
2754 path_base_tree, commitable_paths);
2755 if (ct) {
2756 /* NB: Deleted entries get dropped here. */
2757 if (ct->status == GOT_STATUS_MODIFY) {
2758 err = alloc_modified_blob_tree_entry(
2759 &new_te, te, ct);
2760 if (err)
2761 goto done;
2762 err = insert_tree_entry(new_te, &paths);
2763 if (err)
2764 goto done;
2766 err = report_ct_status(ct, status_cb,
2767 status_arg);
2768 if (err)
2769 goto done;
2770 } else {
2771 /* Entry is unchanged; just copy it. */
2772 err = got_object_tree_entry_dup(&new_te, te);
2773 if (err)
2774 goto done;
2775 err = insert_tree_entry(new_te, &paths);
2776 if (err)
2777 goto done;
2782 /* Write new list of entries; deleted entries have been dropped. */
2783 TAILQ_FOREACH(pe, &paths, entry) {
2784 struct got_tree_entry *te = pe->data;
2785 new_tree_entries.nentries++;
2786 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2788 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2789 done:
2790 got_object_tree_entries_close(&new_tree_entries);
2791 got_pathlist_free(&paths);
2792 return err;
2795 static const struct got_error *
2796 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2797 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2799 const struct got_error *err = NULL;
2800 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2801 struct got_fileindex *fileindex = NULL;
2802 FILE *new_index = NULL;
2803 struct got_pathlist_entry *pe;
2805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2806 if (err)
2807 return err;
2809 err = got_opentemp_named(&new_fileindex_path, &new_index,
2810 fileindex_path);
2811 if (err)
2812 goto done;
2814 TAILQ_FOREACH(pe, commitable_paths, entry) {
2815 struct got_fileindex_entry *ie;
2816 struct got_commitable *ct = pe->data;
2818 ie = got_fileindex_entry_get(fileindex, pe->path);
2819 if (ie) {
2820 if (ct->status == GOT_STATUS_DELETE) {
2821 got_fileindex_entry_remove(fileindex, ie);
2822 got_fileindex_entry_free(ie);
2823 } else
2824 err = got_fileindex_entry_update(ie,
2825 ct->ondisk_path, ct->blob_id->sha1,
2826 new_base_commit_id->sha1, 1);
2827 } else {
2828 err = got_fileindex_entry_alloc(&ie,
2829 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2830 new_base_commit_id->sha1);
2831 if (err)
2832 goto done;
2833 err = got_fileindex_entry_add(fileindex, ie);
2834 if (err)
2835 goto done;
2839 err = got_fileindex_write(fileindex, new_index);
2840 if (err)
2841 goto done;
2843 if (rename(new_fileindex_path, fileindex_path) != 0) {
2844 err = got_error_from_errno3("rename", new_fileindex_path,
2845 fileindex_path);
2846 unlink(new_fileindex_path);
2847 goto done;
2850 free(new_fileindex_path);
2851 new_fileindex_path = NULL;
2853 done:
2854 if (new_fileindex_path)
2855 unlink(new_fileindex_path);
2856 if (new_index)
2857 fclose(new_index);
2858 free(new_fileindex_path);
2859 free(fileindex_path);
2860 got_fileindex_free(fileindex);
2861 return err;
2864 static const struct got_error *
2865 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2866 struct got_object_id *head_commit_id)
2868 const struct got_error *err = NULL;
2869 struct got_object_id *id_in_head;
2872 * Require that modified/deleted files are based on the branch head.
2873 * This requirement could be relaxed to force less update operations
2874 * on users but, for now, we want to play it safe and see how it goes.
2876 * If this check is relaxed, it must still ensure that no modifications
2877 * were made to files *and their parents* in commits between the file's
2878 * base commit and the branch head. Otherwise, tree conflicts will not
2879 * be detected reliably.
2881 if (ct->status != GOT_STATUS_ADD) {
2882 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
2883 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2884 return NULL;
2887 /* Require that added files don't exist in the branch head. */
2888 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
2889 ct->in_repo_path);
2890 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2891 return err;
2892 if (id_in_head) {
2893 free(id_in_head);
2894 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2897 free(id_in_head);
2898 return NULL;
2901 const struct got_error *
2902 got_worktree_commit(struct got_object_id **new_commit_id,
2903 struct got_worktree *worktree, const char *ondisk_path,
2904 const char *author, const char *committer,
2905 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2906 got_worktree_status_cb status_cb, void *status_arg,
2907 struct got_repository *repo)
2909 const struct got_error *err = NULL, *unlockerr = NULL;
2910 struct collect_commitables_arg cc_arg;
2911 struct got_pathlist_head commitable_paths;
2912 struct got_pathlist_entry *pe;
2913 char *relpath = NULL;
2914 const char *head_ref_name = NULL;
2915 struct got_reference *head_ref = NULL;
2916 struct got_commit_object *head_commit = NULL;
2917 struct got_object_id *head_commit_id = NULL;
2918 struct got_reference *head_ref2 = NULL;
2919 struct got_object_id *head_commit_id2 = NULL;
2920 struct got_tree_object *head_tree = NULL;
2921 struct got_object_id *new_tree_id = NULL;
2922 struct got_object_id_queue parent_ids;
2923 struct got_object_qid *pid = NULL;
2924 char *logmsg = NULL;
2926 *new_commit_id = NULL;
2928 TAILQ_INIT(&commitable_paths);
2929 SIMPLEQ_INIT(&parent_ids);
2931 if (ondisk_path) {
2932 err = got_path_skip_common_ancestor(&relpath,
2933 worktree->root_path, ondisk_path);
2934 if (err)
2935 return err;
2938 err = lock_worktree(worktree, LOCK_EX);
2939 if (err)
2940 goto done;
2942 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2943 if (err)
2944 goto done;
2945 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2946 if (err)
2947 goto done;
2949 cc_arg.commitable_paths = &commitable_paths;
2950 cc_arg.worktree = worktree;
2951 cc_arg.repo = repo;
2952 err = got_worktree_status(worktree, relpath ? relpath : "",
2953 repo, collect_commitables, &cc_arg, NULL, NULL);
2954 if (err)
2955 goto done;
2957 if (TAILQ_EMPTY(&commitable_paths)) {
2958 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2959 goto done;
2962 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2963 if (err)
2964 goto done;
2966 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2967 struct got_commitable *ct = pe->data;
2968 err = check_ct_out_of_date(ct, repo, head_commit_id);
2969 if (err)
2970 goto done;
2973 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2974 if (err)
2975 goto done;
2977 if (commit_msg_cb != NULL) {
2978 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2979 if (err)
2980 goto done;
2983 if (logmsg == NULL || strlen(logmsg) == 0) {
2984 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2985 goto done;
2988 /* Create blobs from added and modified files and record their IDs. */
2989 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2990 struct got_commitable *ct = pe->data;
2991 char *ondisk_path;
2993 if (ct->status != GOT_STATUS_ADD &&
2994 ct->status != GOT_STATUS_MODIFY)
2995 continue;
2997 if (asprintf(&ondisk_path, "%s/%s",
2998 worktree->root_path, pe->path) == -1) {
2999 err = got_error_from_errno("asprintf");
3000 goto done;
3002 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3003 free(ondisk_path);
3004 if (err)
3005 goto done;
3008 /* Recursively write new tree objects. */
3009 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3010 status_cb, status_arg, repo);
3011 if (err)
3012 goto done;
3014 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3015 if (err)
3016 goto done;
3017 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3018 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3019 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3020 got_object_qid_free(pid);
3021 if (logmsg != NULL)
3022 free(logmsg);
3023 if (err)
3024 goto done;
3026 /* Check if a concurrent commit to our branch has occurred. */
3027 head_ref_name = got_worktree_get_head_ref_name(worktree);
3028 if (head_ref_name == NULL) {
3029 err = got_error_from_errno("got_worktree_get_head_ref_name");
3030 goto done;
3032 /* Lock the reference here to prevent concurrent modification. */
3033 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3034 if (err)
3035 goto done;
3036 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3037 if (err)
3038 goto done;
3039 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3040 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3041 goto done;
3043 /* Update branch head in repository. */
3044 err = got_ref_change_ref(head_ref2, *new_commit_id);
3045 if (err)
3046 goto done;
3047 err = got_ref_write(head_ref2, repo);
3048 if (err)
3049 goto done;
3051 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3052 if (err)
3053 goto done;
3055 err = ref_base_commit(worktree, repo);
3056 if (err)
3057 goto done;
3059 err = update_fileindex_after_commit(&commitable_paths,
3060 *new_commit_id, worktree);
3061 if (err)
3062 goto done;
3063 done:
3064 unlockerr = lock_worktree(worktree, LOCK_SH);
3065 if (unlockerr && err == NULL)
3066 err = unlockerr;
3067 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3068 struct got_commitable *ct = pe->data;
3069 free_commitable(ct);
3071 got_pathlist_free(&commitable_paths);
3072 if (head_tree)
3073 got_object_tree_close(head_tree);
3074 if (head_commit)
3075 got_object_commit_close(head_commit);
3076 free(relpath);
3077 free(head_commit_id);
3078 free(head_commit_id2);
3079 if (head_ref)
3080 got_ref_close(head_ref);
3081 if (head_ref2) {
3082 unlockerr = got_ref_unlock(head_ref2);
3083 if (unlockerr && err == NULL)
3084 err = unlockerr;
3085 got_ref_close(head_ref2);
3087 return err;
3090 const char *
3091 got_commitable_get_path(struct got_commitable *ct)
3093 return ct->path;
3096 unsigned int
3097 got_commitable_get_status(struct got_commitable *ct)
3099 return ct->status;