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 path = strdup(resolved +
1884 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1885 if (path == NULL) {
1886 err = got_error_from_errno("strdup");
1887 goto done;
1890 /* XXX status walk can't deal with trailing slash! */
1891 len = strlen(path);
1892 while (path[len - 1] == '/') {
1893 path[len - 1] = '\0';
1894 len--;
1896 done:
1897 free(resolved);
1898 if (err == NULL)
1899 *wt_path = path;
1900 else
1901 free(path);
1902 return err;
1905 const struct got_error *
1906 got_worktree_schedule_add(struct got_worktree *worktree,
1907 struct got_pathlist_head *ondisk_paths,
1908 got_worktree_status_cb status_cb, void *status_arg,
1909 struct got_repository *repo)
1911 struct got_fileindex *fileindex = NULL;
1912 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1913 FILE *index = NULL, *new_index = NULL;
1914 const struct got_error *err = NULL, *unlockerr = NULL;
1915 struct got_pathlist_entry *pe;
1917 err = lock_worktree(worktree, LOCK_EX);
1918 if (err)
1919 return err;
1922 fileindex = got_fileindex_alloc();
1923 if (fileindex == NULL) {
1924 err = got_error_from_errno("got_fileindex_alloc");
1925 goto done;
1928 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1929 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1930 err = got_error_from_errno("asprintf");
1931 fileindex_path = NULL;
1932 goto done;
1935 index = fopen(fileindex_path, "rb");
1936 if (index == NULL) {
1937 err = got_error_from_errno2("fopen", fileindex_path);
1938 goto done;
1941 err = got_fileindex_read(fileindex, index);
1942 if (err)
1943 goto done;
1945 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1946 struct got_fileindex_entry *ie = NULL;
1947 char *relpath;
1949 err = got_path_skip_common_ancestor(&relpath,
1950 got_worktree_get_root_path(worktree), pe->path);
1951 if (err)
1952 goto done;
1954 /* Re-adding an existing entry is a no-op. */
1955 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1956 continue;
1958 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1959 NULL, NULL);
1960 free(relpath);
1961 if (err)
1962 goto done;
1964 err = got_fileindex_entry_add(fileindex, ie);
1965 if (err) {
1966 got_fileindex_entry_free(ie);
1967 goto done;
1970 err = report_file_status(ie, pe->path, status_cb, status_arg,
1971 repo);
1972 if (err)
1973 goto done;
1976 err = got_opentemp_named(&new_fileindex_path, &new_index,
1977 fileindex_path);
1978 if (err)
1979 goto done;
1981 err = got_fileindex_write(fileindex, new_index);
1982 if (err)
1983 goto done;
1985 if (rename(new_fileindex_path, fileindex_path) != 0) {
1986 err = got_error_from_errno3("rename", new_fileindex_path,
1987 fileindex_path);
1988 goto done;
1991 free(new_fileindex_path);
1992 new_fileindex_path = NULL;
1994 done:
1995 if (index) {
1996 if (fclose(index) != 0 && err == NULL)
1997 err = got_error_from_errno("fclose");
1999 if (new_fileindex_path) {
2000 if (unlink(new_fileindex_path) != 0 && err == NULL)
2001 err = got_error_from_errno2("unlink",
2002 new_fileindex_path);
2003 free(new_fileindex_path);
2005 if (fileindex)
2006 got_fileindex_free(fileindex);
2007 unlockerr = lock_worktree(worktree, LOCK_SH);
2008 if (unlockerr && err == NULL)
2009 err = unlockerr;
2010 return err;
2013 const struct got_error *
2014 got_worktree_schedule_delete(struct got_worktree *worktree,
2015 const char *ondisk_path, int delete_local_mods,
2016 got_worktree_status_cb status_cb, void *status_arg,
2017 struct got_repository *repo)
2019 struct got_fileindex *fileindex = NULL;
2020 struct got_fileindex_entry *ie = NULL;
2021 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2022 FILE *index = NULL, *new_index = NULL;
2023 const struct got_error *err = NULL, *unlockerr = NULL;
2024 unsigned char status;
2025 struct stat sb;
2027 err = lock_worktree(worktree, LOCK_EX);
2028 if (err)
2029 return err;
2031 err = got_path_skip_common_ancestor(&relpath,
2032 got_worktree_get_root_path(worktree), ondisk_path);
2033 if (err)
2034 goto done;
2036 fileindex = got_fileindex_alloc();
2037 if (fileindex == NULL) {
2038 err = got_error_from_errno("got_fileindex_alloc");
2039 goto done;
2042 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2043 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2044 err = got_error_from_errno("asprintf");
2045 fileindex_path = NULL;
2046 goto done;
2049 index = fopen(fileindex_path, "rb");
2050 if (index == NULL) {
2051 err = got_error_from_errno2("fopen", fileindex_path);
2052 goto done;
2055 err = got_fileindex_read(fileindex, index);
2056 if (err)
2057 goto done;
2059 ie = got_fileindex_entry_get(fileindex, relpath);
2060 if (ie == NULL) {
2061 err = got_error(GOT_ERR_BAD_PATH);
2062 goto done;
2065 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2066 if (err)
2067 goto done;
2069 if (status != GOT_STATUS_NO_CHANGE) {
2070 if (status == GOT_STATUS_DELETE) {
2071 err = got_error_set_errno(ENOENT, ondisk_path);
2072 goto done;
2074 if (status != GOT_STATUS_MODIFY) {
2075 err = got_error(GOT_ERR_FILE_STATUS);
2076 goto done;
2078 if (!delete_local_mods) {
2079 err = got_error(GOT_ERR_FILE_MODIFIED);
2080 goto done;
2084 if (unlink(ondisk_path) != 0) {
2085 err = got_error_from_errno2("unlink", ondisk_path);
2086 goto done;
2089 got_fileindex_entry_mark_deleted_from_disk(ie);
2091 err = got_opentemp_named(&new_fileindex_path, &new_index,
2092 fileindex_path);
2093 if (err)
2094 goto done;
2096 err = got_fileindex_write(fileindex, new_index);
2097 if (err)
2098 goto done;
2100 if (rename(new_fileindex_path, fileindex_path) != 0) {
2101 err = got_error_from_errno3("rename", new_fileindex_path,
2102 fileindex_path);
2103 goto done;
2106 free(new_fileindex_path);
2107 new_fileindex_path = NULL;
2109 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2110 done:
2111 free(relpath);
2112 if (index) {
2113 if (fclose(index) != 0 && err == NULL)
2114 err = got_error_from_errno("fclose");
2116 if (new_fileindex_path) {
2117 if (unlink(new_fileindex_path) != 0 && err == NULL)
2118 err = got_error_from_errno2("unlink",
2119 new_fileindex_path);
2120 free(new_fileindex_path);
2122 if (fileindex)
2123 got_fileindex_free(fileindex);
2124 unlockerr = lock_worktree(worktree, LOCK_SH);
2125 if (unlockerr && err == NULL)
2126 err = unlockerr;
2127 return err;
2130 const struct got_error *
2131 got_worktree_revert(struct got_worktree *worktree,
2132 const char *ondisk_path,
2133 got_worktree_checkout_cb progress_cb, void *progress_arg,
2134 struct got_repository *repo)
2136 struct got_fileindex *fileindex = NULL;
2137 struct got_fileindex_entry *ie = NULL;
2138 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2139 char *tree_path = NULL, *parent_path, *te_name;
2140 FILE *index = NULL, *new_index = NULL;
2141 const struct got_error *err = NULL, *unlockerr = NULL;
2142 struct got_tree_object *tree = NULL;
2143 struct got_object_id id, *tree_id = NULL;
2144 const struct got_tree_entry *te;
2145 struct got_blob_object *blob = NULL;
2146 unsigned char status;
2147 struct stat sb;
2149 err = lock_worktree(worktree, LOCK_EX);
2150 if (err)
2151 return err;
2153 err = got_path_skip_common_ancestor(&relpath,
2154 got_worktree_get_root_path(worktree), ondisk_path);
2155 if (err)
2156 goto done;
2158 fileindex = got_fileindex_alloc();
2159 if (fileindex == NULL) {
2160 err = got_error_from_errno("got_fileindex_alloc");
2161 goto done;
2164 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2165 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2166 err = got_error_from_errno("asprintf");
2167 fileindex_path = NULL;
2168 goto done;
2171 index = fopen(fileindex_path, "rb");
2172 if (index == NULL) {
2173 err = got_error_from_errno2("fopen", fileindex_path);
2174 goto done;
2177 err = got_fileindex_read(fileindex, index);
2178 if (err)
2179 goto done;
2181 ie = got_fileindex_entry_get(fileindex, relpath);
2182 if (ie == NULL) {
2183 err = got_error(GOT_ERR_BAD_PATH);
2184 goto done;
2187 /* Construct in-repository path of tree which contains this blob. */
2188 err = got_path_dirname(&parent_path, ie->path);
2189 if (err) {
2190 if (err->code != GOT_ERR_BAD_PATH)
2191 goto done;
2192 parent_path = "/";
2194 if (got_path_is_root_dir(worktree->path_prefix)) {
2195 tree_path = strdup(parent_path);
2196 if (tree_path == NULL) {
2197 err = got_error_from_errno("strdup");
2198 goto done;
2200 } else {
2201 if (got_path_is_root_dir(parent_path)) {
2202 tree_path = strdup(worktree->path_prefix);
2203 if (tree_path == NULL) {
2204 err = got_error_from_errno("strdup");
2205 goto done;
2207 } else {
2208 if (asprintf(&tree_path, "%s/%s",
2209 worktree->path_prefix, parent_path) == -1) {
2210 err = got_error_from_errno("asprintf");
2211 goto done;
2216 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2217 tree_path);
2218 if (err)
2219 goto done;
2221 err = got_object_open_as_tree(&tree, repo, tree_id);
2222 if (err)
2223 goto done;
2225 te_name = basename(ie->path);
2226 if (te_name == NULL) {
2227 err = got_error_from_errno2("basename", ie->path);
2228 goto done;
2231 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2232 if (err)
2233 goto done;
2235 te = got_object_tree_find_entry(tree, te_name);
2236 if (te == NULL && status != GOT_STATUS_ADD) {
2237 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2238 goto done;
2241 switch (status) {
2242 case GOT_STATUS_ADD:
2243 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2244 got_fileindex_entry_remove(fileindex, ie);
2245 break;
2246 case GOT_STATUS_DELETE:
2247 case GOT_STATUS_MODIFY:
2248 case GOT_STATUS_CONFLICT:
2249 case GOT_STATUS_MISSING:
2250 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2251 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2252 if (err)
2253 goto done;
2254 err = install_blob(worktree, ondisk_path, ie->path,
2255 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2256 progress_arg);
2257 if (err)
2258 goto done;
2259 if (status == GOT_STATUS_DELETE) {
2260 err = update_blob_fileindex_entry(worktree,
2261 fileindex, ie, ondisk_path, ie->path, blob, 1);
2262 if (err)
2263 goto done;
2265 break;
2266 default:
2267 goto done;
2270 err = got_opentemp_named(&new_fileindex_path, &new_index,
2271 fileindex_path);
2272 if (err)
2273 goto done;
2275 err = got_fileindex_write(fileindex, new_index);
2276 if (err)
2277 goto done;
2279 if (rename(new_fileindex_path, fileindex_path) != 0) {
2280 err = got_error_from_errno3("rename", new_fileindex_path,
2281 fileindex_path);
2282 goto done;
2285 free(new_fileindex_path);
2286 new_fileindex_path = NULL;
2287 done:
2288 free(relpath);
2289 free(tree_path);
2290 if (blob)
2291 got_object_blob_close(blob);
2292 if (tree)
2293 got_object_tree_close(tree);
2294 free(tree_id);
2295 if (index) {
2296 if (fclose(index) != 0 && err == NULL)
2297 err = got_error_from_errno("fclose");
2299 if (new_fileindex_path) {
2300 if (unlink(new_fileindex_path) != 0 && err == NULL)
2301 err = got_error_from_errno2("unlink",
2302 new_fileindex_path);
2303 free(new_fileindex_path);
2305 if (fileindex)
2306 got_fileindex_free(fileindex);
2307 unlockerr = lock_worktree(worktree, LOCK_SH);
2308 if (unlockerr && err == NULL)
2309 err = unlockerr;
2310 return err;
2313 static void
2314 free_commitable(struct got_commitable *ct)
2316 free(ct->path);
2317 free(ct->in_repo_path);
2318 free(ct->ondisk_path);
2319 free(ct->blob_id);
2320 free(ct->base_blob_id);
2321 free(ct->base_commit_id);
2322 free(ct);
2325 struct collect_commitables_arg {
2326 struct got_pathlist_head *commitable_paths;
2327 struct got_repository *repo;
2328 struct got_worktree *worktree;
2331 static const struct got_error *
2332 collect_commitables(void *arg, unsigned char status, const char *relpath,
2333 struct got_object_id *blob_id, struct got_object_id *commit_id)
2335 struct collect_commitables_arg *a = arg;
2336 const struct got_error *err = NULL;
2337 struct got_commitable *ct = NULL;
2338 struct got_pathlist_entry *new = NULL;
2339 char *parent_path = NULL, *path = NULL;
2340 struct stat sb;
2342 if (status == GOT_STATUS_CONFLICT)
2343 return got_error(GOT_ERR_COMMIT_CONFLICT);
2345 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2346 status != GOT_STATUS_DELETE)
2347 return NULL;
2349 if (asprintf(&path, "/%s", relpath) == -1) {
2350 err = got_error_from_errno("asprintf");
2351 goto done;
2353 if (strcmp(path, "/") == 0) {
2354 parent_path = strdup("");
2355 if (parent_path == NULL)
2356 return got_error_from_errno("strdup");
2357 } else {
2358 err = got_path_dirname(&parent_path, path);
2359 if (err)
2360 return err;
2363 ct = calloc(1, sizeof(*ct));
2364 if (ct == NULL) {
2365 err = got_error_from_errno("calloc");
2366 goto done;
2369 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2370 relpath) == -1) {
2371 err = got_error_from_errno("asprintf");
2372 goto done;
2374 if (status == GOT_STATUS_DELETE) {
2375 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2376 } else {
2377 if (lstat(ct->ondisk_path, &sb) != 0) {
2378 err = got_error_from_errno2("lstat", ct->ondisk_path);
2379 goto done;
2381 ct->mode = sb.st_mode;
2384 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2385 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2386 relpath) == -1) {
2387 err = got_error_from_errno("asprintf");
2388 goto done;
2391 ct->status = status;
2392 ct->blob_id = NULL; /* will be filled in when blob gets created */
2393 if (ct->status != GOT_STATUS_ADD) {
2394 ct->base_blob_id = got_object_id_dup(blob_id);
2395 if (ct->base_blob_id == NULL) {
2396 err = got_error_from_errno("got_object_id_dup");
2397 goto done;
2399 ct->base_commit_id = got_object_id_dup(commit_id);
2400 if (ct->base_commit_id == NULL) {
2401 err = got_error_from_errno("got_object_id_dup");
2402 goto done;
2405 ct->path = strdup(path);
2406 if (ct->path == NULL) {
2407 err = got_error_from_errno("strdup");
2408 goto done;
2410 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2411 done:
2412 if (ct && (err || new == NULL))
2413 free_commitable(ct);
2414 free(parent_path);
2415 free(path);
2416 return err;
2419 static const struct got_error *write_tree(struct got_object_id **,
2420 struct got_tree_object *, const char *, struct got_pathlist_head *,
2421 got_worktree_status_cb status_cb, void *status_arg,
2422 struct got_repository *);
2424 static const struct got_error *
2425 write_subtree(struct got_object_id **new_subtree_id,
2426 struct got_tree_entry *te, const char *parent_path,
2427 struct got_pathlist_head *commitable_paths,
2428 got_worktree_status_cb status_cb, void *status_arg,
2429 struct got_repository *repo)
2431 const struct got_error *err = NULL;
2432 struct got_tree_object *subtree;
2433 char *subpath;
2435 if (asprintf(&subpath, "%s%s%s", parent_path,
2436 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2437 return got_error_from_errno("asprintf");
2439 err = got_object_open_as_tree(&subtree, repo, te->id);
2440 if (err)
2441 return err;
2443 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2444 status_cb, status_arg, repo);
2445 got_object_tree_close(subtree);
2446 free(subpath);
2447 return err;
2450 static const struct got_error *
2451 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2453 const struct got_error *err = NULL;
2454 char *ct_parent_path = NULL;
2456 *match = 0;
2458 if (strchr(ct->path, '/') == NULL) {
2459 *match = got_path_is_root_dir(path);
2460 return NULL;
2463 err = got_path_dirname(&ct_parent_path, ct->path);
2464 if (err)
2465 return err;
2466 *match = (strcmp(path, ct_parent_path) == 0);
2467 free(ct_parent_path);
2468 return err;
2471 static mode_t
2472 get_ct_file_mode(struct got_commitable *ct)
2474 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2477 static const struct got_error *
2478 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2479 struct got_tree_entry *te, struct got_commitable *ct)
2481 const struct got_error *err = NULL;
2483 *new_te = NULL;
2485 err = got_object_tree_entry_dup(new_te, te);
2486 if (err)
2487 goto done;
2489 (*new_te)->mode = get_ct_file_mode(ct);
2491 free((*new_te)->id);
2492 (*new_te)->id = got_object_id_dup(ct->blob_id);
2493 if ((*new_te)->id == NULL) {
2494 err = got_error_from_errno("got_object_id_dup");
2495 goto done;
2497 done:
2498 if (err && *new_te) {
2499 got_object_tree_entry_close(*new_te);
2500 *new_te = NULL;
2502 return err;
2505 static const struct got_error *
2506 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2507 struct got_commitable *ct)
2509 const struct got_error *err = NULL;
2510 char *ct_name;
2512 *new_te = NULL;
2514 *new_te = calloc(1, sizeof(**new_te));
2515 if (*new_te == NULL)
2516 return got_error_from_errno("calloc");
2518 ct_name = basename(ct->path);
2519 if (ct_name == NULL) {
2520 err = got_error_from_errno2("basename", ct->path);
2521 goto done;
2523 (*new_te)->name = strdup(ct_name);
2524 if ((*new_te)->name == NULL) {
2525 err = got_error_from_errno("strdup");
2526 goto done;
2529 (*new_te)->mode = get_ct_file_mode(ct);
2531 (*new_te)->id = got_object_id_dup(ct->blob_id);
2532 if ((*new_te)->id == NULL) {
2533 err = got_error_from_errno("got_object_id_dup");
2534 goto done;
2536 done:
2537 if (err && *new_te) {
2538 got_object_tree_entry_close(*new_te);
2539 *new_te = NULL;
2541 return err;
2544 static const struct got_error *
2545 insert_tree_entry(struct got_tree_entry *new_te,
2546 struct got_pathlist_head *paths)
2548 const struct got_error *err = NULL;
2549 struct got_pathlist_entry *new_pe;
2551 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2552 if (err)
2553 return err;
2554 if (new_pe == NULL)
2555 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2556 return NULL;
2559 static const struct got_error *
2560 report_ct_status(struct got_commitable *ct,
2561 got_worktree_status_cb status_cb, void *status_arg)
2563 const char *ct_path = ct->path;
2564 while (ct_path[0] == '/')
2565 ct_path++;
2566 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2569 static const struct got_error *
2570 match_modified_subtree(int *modified, struct got_tree_entry *te,
2571 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2573 const struct got_error *err = NULL;
2574 struct got_pathlist_entry *pe;
2575 char *te_path;
2577 *modified = 0;
2579 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2580 got_path_is_root_dir(base_tree_path) ? "" : "/",
2581 te->name) == -1)
2582 return got_error_from_errno("asprintf");
2584 TAILQ_FOREACH(pe, commitable_paths, entry) {
2585 struct got_commitable *ct = pe->data;
2586 *modified = got_path_is_child(ct->in_repo_path, te_path,
2587 strlen(te_path));
2588 if (*modified)
2589 break;
2592 free(te_path);
2593 return err;
2596 static const struct got_error *
2597 match_deleted_or_modified_ct(struct got_commitable **ctp,
2598 struct got_tree_entry *te, const char *base_tree_path,
2599 struct got_pathlist_head *commitable_paths)
2601 const struct got_error *err = NULL;
2602 struct got_pathlist_entry *pe;
2604 *ctp = NULL;
2606 TAILQ_FOREACH(pe, commitable_paths, entry) {
2607 struct got_commitable *ct = pe->data;
2608 char *ct_name = NULL;
2609 int path_matches;
2611 if (ct->status != GOT_STATUS_MODIFY &&
2612 ct->status != GOT_STATUS_DELETE)
2613 continue;
2615 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2616 continue;
2618 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2619 if (err)
2620 return err;
2621 if (!path_matches)
2622 continue;
2624 ct_name = basename(pe->path);
2625 if (ct_name == NULL)
2626 return got_error_from_errno2("basename", pe->path);
2628 if (strcmp(te->name, ct_name) != 0)
2629 continue;
2631 *ctp = ct;
2632 break;
2635 return err;
2638 static const struct got_error *
2639 write_tree(struct got_object_id **new_tree_id,
2640 struct got_tree_object *base_tree, const char *path_base_tree,
2641 struct got_pathlist_head *commitable_paths,
2642 got_worktree_status_cb status_cb, void *status_arg,
2643 struct got_repository *repo)
2645 const struct got_error *err = NULL;
2646 const struct got_tree_entries *base_entries = NULL;
2647 struct got_pathlist_head paths;
2648 struct got_tree_entries new_tree_entries;
2649 struct got_tree_entry *te, *new_te = NULL;
2650 struct got_pathlist_entry *pe;
2652 TAILQ_INIT(&paths);
2653 new_tree_entries.nentries = 0;
2654 SIMPLEQ_INIT(&new_tree_entries.head);
2656 /* Insert, and recurse into, newly added entries first. */
2657 TAILQ_FOREACH(pe, commitable_paths, entry) {
2658 struct got_commitable *ct = pe->data;
2659 char *child_path = NULL, *slash;
2661 if (ct->status != GOT_STATUS_ADD ||
2662 (ct->flags & GOT_COMMITABLE_ADDED))
2663 continue;
2665 if (!got_path_is_child(pe->path, path_base_tree,
2666 strlen(path_base_tree)))
2667 continue;
2669 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2670 pe->path);
2671 if (err)
2672 goto done;
2674 slash = strchr(child_path, '/');
2675 if (slash == NULL) {
2676 err = alloc_added_blob_tree_entry(&new_te, ct);
2677 if (err)
2678 goto done;
2679 err = report_ct_status(ct, status_cb, status_arg);
2680 if (err)
2681 goto done;
2682 ct->flags |= GOT_COMMITABLE_ADDED;
2683 } else {
2684 char *subtree_path;
2686 *slash = '\0'; /* trim trailing path components */
2687 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2688 got_path_is_root_dir(path_base_tree) ? "" : "/",
2689 child_path) == -1) {
2690 err = got_error_from_errno("asprintf");
2691 goto done;
2694 new_te = calloc(1, sizeof(*new_te));
2695 new_te->mode = S_IFDIR;
2696 new_te->name = strdup(child_path);
2697 if (new_te->name == NULL) {
2698 err = got_error_from_errno("strdup");
2699 got_object_tree_entry_close(new_te);
2700 new_te = NULL;
2701 goto done;
2703 err = write_tree(&new_te->id, NULL, subtree_path,
2704 commitable_paths, status_cb, status_arg, repo);
2705 free(subtree_path);
2706 if (err) {
2707 got_object_tree_entry_close(new_te);
2708 new_te = NULL;
2709 goto done;
2712 err = insert_tree_entry(new_te, &paths);
2713 if (err)
2714 goto done;
2717 if (base_tree) {
2718 /* Handle modified and deleted entries. */
2719 base_entries = got_object_tree_get_entries(base_tree);
2720 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2721 struct got_commitable *ct = NULL;
2723 if (S_ISDIR(te->mode)) {
2724 int modified;
2725 err = got_object_tree_entry_dup(&new_te, te);
2726 if (err)
2727 goto done;
2728 err = match_modified_subtree(&modified, te,
2729 path_base_tree, commitable_paths);
2730 if (err)
2731 goto done;
2732 /* Avoid recursion into unmodified subtrees. */
2733 if (modified) {
2734 free(new_te->id);
2735 err = write_subtree(&new_te->id, te,
2736 path_base_tree, commitable_paths,
2737 status_cb, status_arg, repo);
2738 if (err)
2739 goto done;
2741 err = insert_tree_entry(new_te, &paths);
2742 if (err)
2743 goto done;
2744 continue;
2747 err = match_deleted_or_modified_ct(&ct, te,
2748 path_base_tree, commitable_paths);
2749 if (ct) {
2750 /* NB: Deleted entries get dropped here. */
2751 if (ct->status == GOT_STATUS_MODIFY) {
2752 err = alloc_modified_blob_tree_entry(
2753 &new_te, te, ct);
2754 if (err)
2755 goto done;
2756 err = insert_tree_entry(new_te, &paths);
2757 if (err)
2758 goto done;
2760 err = report_ct_status(ct, status_cb,
2761 status_arg);
2762 if (err)
2763 goto done;
2764 } else {
2765 /* Entry is unchanged; just copy it. */
2766 err = got_object_tree_entry_dup(&new_te, te);
2767 if (err)
2768 goto done;
2769 err = insert_tree_entry(new_te, &paths);
2770 if (err)
2771 goto done;
2776 /* Write new list of entries; deleted entries have been dropped. */
2777 TAILQ_FOREACH(pe, &paths, entry) {
2778 struct got_tree_entry *te = pe->data;
2779 new_tree_entries.nentries++;
2780 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2782 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2783 done:
2784 got_object_tree_entries_close(&new_tree_entries);
2785 got_pathlist_free(&paths);
2786 return err;
2789 static const struct got_error *
2790 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2791 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2793 const struct got_error *err = NULL;
2794 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2795 struct got_fileindex *fileindex = NULL;
2796 FILE *new_index = NULL;
2797 struct got_pathlist_entry *pe;
2799 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2800 if (err)
2801 return err;
2803 err = got_opentemp_named(&new_fileindex_path, &new_index,
2804 fileindex_path);
2805 if (err)
2806 goto done;
2808 TAILQ_FOREACH(pe, commitable_paths, entry) {
2809 struct got_fileindex_entry *ie;
2810 struct got_commitable *ct = pe->data;
2812 ie = got_fileindex_entry_get(fileindex, pe->path);
2813 if (ie) {
2814 if (ct->status == GOT_STATUS_DELETE) {
2815 got_fileindex_entry_remove(fileindex, ie);
2816 got_fileindex_entry_free(ie);
2817 } else
2818 err = got_fileindex_entry_update(ie,
2819 ct->ondisk_path, ct->blob_id->sha1,
2820 new_base_commit_id->sha1, 1);
2821 } else {
2822 err = got_fileindex_entry_alloc(&ie,
2823 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2824 new_base_commit_id->sha1);
2825 if (err)
2826 goto done;
2827 err = got_fileindex_entry_add(fileindex, ie);
2828 if (err)
2829 goto done;
2833 err = got_fileindex_write(fileindex, new_index);
2834 if (err)
2835 goto done;
2837 if (rename(new_fileindex_path, fileindex_path) != 0) {
2838 err = got_error_from_errno3("rename", new_fileindex_path,
2839 fileindex_path);
2840 unlink(new_fileindex_path);
2841 goto done;
2844 free(new_fileindex_path);
2845 new_fileindex_path = NULL;
2847 done:
2848 if (new_fileindex_path)
2849 unlink(new_fileindex_path);
2850 if (new_index)
2851 fclose(new_index);
2852 free(new_fileindex_path);
2853 free(fileindex_path);
2854 got_fileindex_free(fileindex);
2855 return err;
2858 static const struct got_error *
2859 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2860 struct got_object_id *head_commit_id)
2862 const struct got_error *err = NULL;
2863 struct got_object_id *id_in_head;
2866 * Require that modified/deleted files are based on the branch head.
2867 * This requirement could be relaxed to force less update operations
2868 * on users but, for now, we want to play it safe and see how it goes.
2870 * If this check is relaxed, it must still ensure that no modifications
2871 * were made to files *and their parents* in commits between the file's
2872 * base commit and the branch head. Otherwise, tree conflicts will not
2873 * be detected reliably.
2875 if (ct->status != GOT_STATUS_ADD) {
2876 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
2877 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2878 return NULL;
2881 /* Require that added files don't exist in the branch head. */
2882 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
2883 ct->in_repo_path);
2884 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2885 return err;
2886 if (id_in_head) {
2887 free(id_in_head);
2888 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2891 free(id_in_head);
2892 return NULL;
2895 const struct got_error *
2896 got_worktree_commit(struct got_object_id **new_commit_id,
2897 struct got_worktree *worktree, const char *ondisk_path,
2898 const char *author, const char *committer,
2899 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2900 got_worktree_status_cb status_cb, void *status_arg,
2901 struct got_repository *repo)
2903 const struct got_error *err = NULL, *unlockerr = NULL;
2904 struct collect_commitables_arg cc_arg;
2905 struct got_pathlist_head commitable_paths;
2906 struct got_pathlist_entry *pe;
2907 char *relpath = NULL;
2908 const char *head_ref_name = NULL;
2909 struct got_reference *head_ref = NULL;
2910 struct got_commit_object *head_commit = NULL;
2911 struct got_object_id *head_commit_id = NULL;
2912 struct got_reference *head_ref2 = NULL;
2913 struct got_object_id *head_commit_id2 = NULL;
2914 struct got_tree_object *head_tree = NULL;
2915 struct got_object_id *new_tree_id = NULL;
2916 struct got_object_id_queue parent_ids;
2917 struct got_object_qid *pid = NULL;
2918 char *logmsg = NULL;
2920 *new_commit_id = NULL;
2922 TAILQ_INIT(&commitable_paths);
2923 SIMPLEQ_INIT(&parent_ids);
2925 if (ondisk_path) {
2926 err = got_path_skip_common_ancestor(&relpath,
2927 worktree->root_path, ondisk_path);
2928 if (err)
2929 return err;
2932 err = lock_worktree(worktree, LOCK_EX);
2933 if (err)
2934 goto done;
2936 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2937 if (err)
2938 goto done;
2939 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2940 if (err)
2941 goto done;
2943 cc_arg.commitable_paths = &commitable_paths;
2944 cc_arg.worktree = worktree;
2945 cc_arg.repo = repo;
2946 err = got_worktree_status(worktree, relpath ? relpath : "",
2947 repo, collect_commitables, &cc_arg, NULL, NULL);
2948 if (err)
2949 goto done;
2951 if (TAILQ_EMPTY(&commitable_paths)) {
2952 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2953 goto done;
2956 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2957 if (err)
2958 goto done;
2960 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2961 struct got_commitable *ct = pe->data;
2962 err = check_ct_out_of_date(ct, repo, head_commit_id);
2963 if (err)
2964 goto done;
2967 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2968 if (err)
2969 goto done;
2971 if (commit_msg_cb != NULL) {
2972 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2973 if (err)
2974 goto done;
2977 if (logmsg == NULL || strlen(logmsg) == 0) {
2978 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2979 goto done;
2982 /* Create blobs from added and modified files and record their IDs. */
2983 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2984 struct got_commitable *ct = pe->data;
2985 char *ondisk_path;
2987 if (ct->status != GOT_STATUS_ADD &&
2988 ct->status != GOT_STATUS_MODIFY)
2989 continue;
2991 if (asprintf(&ondisk_path, "%s/%s",
2992 worktree->root_path, pe->path) == -1) {
2993 err = got_error_from_errno("asprintf");
2994 goto done;
2996 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2997 free(ondisk_path);
2998 if (err)
2999 goto done;
3002 /* Recursively write new tree objects. */
3003 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3004 status_cb, status_arg, repo);
3005 if (err)
3006 goto done;
3008 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3009 if (err)
3010 goto done;
3011 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3012 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3013 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3014 got_object_qid_free(pid);
3015 if (logmsg != NULL)
3016 free(logmsg);
3017 if (err)
3018 goto done;
3020 /* Check if a concurrent commit to our branch has occurred. */
3021 head_ref_name = got_worktree_get_head_ref_name(worktree);
3022 if (head_ref_name == NULL) {
3023 err = got_error_from_errno("got_worktree_get_head_ref_name");
3024 goto done;
3026 /* Lock the reference here to prevent concurrent modification. */
3027 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3028 if (err)
3029 goto done;
3030 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3031 if (err)
3032 goto done;
3033 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3034 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3035 goto done;
3037 /* Update branch head in repository. */
3038 err = got_ref_change_ref(head_ref2, *new_commit_id);
3039 if (err)
3040 goto done;
3041 err = got_ref_write(head_ref2, repo);
3042 if (err)
3043 goto done;
3045 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3046 if (err)
3047 goto done;
3049 err = ref_base_commit(worktree, repo);
3050 if (err)
3051 goto done;
3053 err = update_fileindex_after_commit(&commitable_paths,
3054 *new_commit_id, worktree);
3055 if (err)
3056 goto done;
3057 done:
3058 unlockerr = lock_worktree(worktree, LOCK_SH);
3059 if (unlockerr && err == NULL)
3060 err = unlockerr;
3061 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3062 struct got_commitable *ct = pe->data;
3063 free_commitable(ct);
3065 got_pathlist_free(&commitable_paths);
3066 if (head_tree)
3067 got_object_tree_close(head_tree);
3068 if (head_commit)
3069 got_object_commit_close(head_commit);
3070 free(relpath);
3071 free(head_commit_id);
3072 free(head_commit_id2);
3073 if (head_ref)
3074 got_ref_close(head_ref);
3075 if (head_ref2) {
3076 unlockerr = got_ref_unlock(head_ref2);
3077 if (unlockerr && err == NULL)
3078 err = unlockerr;
3079 got_ref_close(head_ref2);
3081 return err;
3084 const char *
3085 got_commitable_get_path(struct got_commitable *ct)
3087 return ct->path;
3090 unsigned int
3091 got_commitable_get_status(struct got_commitable *ct)
3093 return ct->status;