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"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 static const struct got_error *
62 create_meta_file(const char *path_got, const char *name, const char *content)
63 {
64 const struct got_error *err = NULL;
65 char *path;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 return got_error_from_errno("asprintf");
70 err = got_path_create_file(path, content);
71 free(path);
72 return err;
73 }
75 static const struct got_error *
76 update_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 FILE *tmpfile = NULL;
80 char *tmppath = NULL;
81 char *path = NULL;
83 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 err = got_error_from_errno("asprintf");
85 path = NULL;
86 goto done;
87 }
89 err = got_opentemp_named(&tmppath, &tmpfile, path);
90 if (err)
91 goto done;
93 if (content) {
94 int len = fprintf(tmpfile, "%s\n", content);
95 if (len != strlen(content) + 1) {
96 err = got_error_from_errno2("fprintf", tmppath);
97 goto done;
98 }
99 }
101 if (rename(tmppath, path) != 0) {
102 err = got_error_from_errno3("rename", tmppath, path);
103 unlink(tmppath);
104 goto done;
107 done:
108 if (fclose(tmpfile) != 0 && err == NULL)
109 err = got_error_from_errno2("fclose", tmppath);
110 free(tmppath);
111 return err;
114 static const struct got_error *
115 read_meta_file(char **content, const char *path_got, const char *name)
117 const struct got_error *err = NULL;
118 char *path;
119 int fd = -1;
120 ssize_t n;
121 struct stat sb;
123 *content = NULL;
125 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 err = got_error_from_errno("asprintf");
127 path = NULL;
128 goto done;
131 fd = open(path, O_RDONLY | O_NOFOLLOW);
132 if (fd == -1) {
133 if (errno == ENOENT)
134 err = got_error_path(path, GOT_ERR_WORKTREE_META);
135 else
136 err = got_error_from_errno2("open", path);
137 goto done;
139 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 : got_error_from_errno2("flock", path));
142 goto done;
145 if (fstat(fd, &sb) != 0) {
146 err = got_error_from_errno2("fstat", path);
147 goto done;
149 *content = calloc(1, sb.st_size);
150 if (*content == NULL) {
151 err = got_error_from_errno("calloc");
152 goto done;
155 n = read(fd, *content, sb.st_size);
156 if (n != sb.st_size) {
157 err = (n == -1 ? got_error_from_errno2("read", path) :
158 got_error_path(path, GOT_ERR_WORKTREE_META));
159 goto done;
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error_path(path, GOT_ERR_WORKTREE_META);
163 goto done;
165 (*content)[sb.st_size - 1] = '\0';
167 done:
168 if (fd != -1 && close(fd) == -1 && err == NULL)
169 err = got_error_from_errno2("close", path_got);
170 free(path);
171 if (err) {
172 free(*content);
173 *content = NULL;
175 return err;
178 static const struct got_error *
179 write_head_ref(const char *path_got, struct got_reference *head_ref)
181 const struct got_error *err = NULL;
182 char *refstr = NULL;
184 if (got_ref_is_symbolic(head_ref)) {
185 refstr = got_ref_to_str(head_ref);
186 if (refstr == NULL)
187 return got_error_from_errno("got_ref_to_str");
188 } else {
189 refstr = strdup(got_ref_get_name(head_ref));
190 if (refstr == NULL)
191 return got_error_from_errno("strdup");
193 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 free(refstr);
195 return err;
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
204 uuid_t uuid;
205 uint32_t uuid_status;
206 int obj_type;
207 char *path_got = NULL;
208 char *formatstr = NULL;
209 char *absprefix = NULL;
210 char *basestr = NULL;
211 char *uuidstr = NULL;
213 if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 err = got_error(GOT_ERR_WORKTREE_REPO);
215 goto done;
218 err = got_ref_resolve(&commit_id, repo, head_ref);
219 if (err)
220 return err;
221 err = got_object_get_type(&obj_type, repo, commit_id);
222 if (err)
223 return err;
224 if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 return got_error(GOT_ERR_OBJ_TYPE);
227 if (!got_path_is_absolute(prefix)) {
228 if (asprintf(&absprefix, "/%s", prefix) == -1)
229 return got_error_from_errno("asprintf");
232 /* Create top-level directory (may already exist). */
233 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 err = got_error_from_errno2("mkdir", path);
235 goto done;
238 /* Create .got directory (may already exist). */
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
243 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 err = got_error_from_errno2("mkdir", path_got);
245 goto done;
248 /* Create an empty lock file. */
249 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 if (err)
251 goto done;
253 /* Create an empty file index. */
254 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 if (err)
256 goto done;
258 /* Write the HEAD reference. */
259 err = write_head_ref(path_got, head_ref);
260 if (err)
261 goto done;
263 /* Record our base commit. */
264 err = got_object_id_str(&basestr, commit_id);
265 if (err)
266 goto done;
267 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
268 if (err)
269 goto done;
271 /* Store path to repository. */
272 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 got_repo_get_path(repo));
274 if (err)
275 goto done;
277 /* Store in-repository path prefix. */
278 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 absprefix ? absprefix : prefix);
280 if (err)
281 goto done;
283 /* Generate UUID. */
284 uuid_create(&uuid, &uuid_status);
285 if (uuid_status != uuid_s_ok) {
286 err = got_error_uuid(uuid_status);
287 goto done;
289 uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status);
292 goto done;
294 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
295 if (err)
296 goto done;
298 /* Stamp work tree with format file. */
299 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 err = got_error_from_errno("asprintf");
301 goto done;
303 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
304 if (err)
305 goto done;
307 done:
308 free(commit_id);
309 free(path_got);
310 free(formatstr);
311 free(absprefix);
312 free(basestr);
313 free(uuidstr);
314 return err;
317 static const struct got_error *
318 open_worktree(struct got_worktree **worktree, const char *path)
320 const struct got_error *err = NULL;
321 char *path_got;
322 char *formatstr = NULL;
323 char *uuidstr = NULL;
324 char *path_lock = NULL;
325 char *base_commit_id_str = NULL;
326 int version, fd = -1;
327 const char *errstr;
328 struct got_repository *repo = NULL;
329 uint32_t uuid_status;
331 *worktree = NULL;
333 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_lock = NULL;
342 goto done;
345 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 if (fd == -1) {
347 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 : got_error_from_errno2("open", path_lock));
349 goto done;
352 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
353 if (err)
354 goto done;
356 version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 if (errstr) {
358 err = got_error_msg(GOT_ERR_WORKTREE_META,
359 "could not parse work tree format version number");
360 goto done;
362 if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 err = got_error(GOT_ERR_WORKTREE_VERS);
364 goto done;
367 *worktree = calloc(1, sizeof(**worktree));
368 if (*worktree == NULL) {
369 err = got_error_from_errno("calloc");
370 goto done;
372 (*worktree)->lockfd = -1;
374 (*worktree)->root_path = strdup(path);
375 if ((*worktree)->root_path == NULL) {
376 err = got_error_from_errno("strdup");
377 goto done;
379 err = read_meta_file(&(*worktree)->repo_path, path_got,
380 GOT_WORKTREE_REPOSITORY);
381 if (err)
382 goto done;
384 err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 GOT_WORKTREE_PATH_PREFIX);
386 if (err)
387 goto done;
389 err = read_meta_file(&base_commit_id_str, path_got,
390 GOT_WORKTREE_BASE_COMMIT);
391 if (err)
392 goto done;
394 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
395 if (err)
396 goto done;
397 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 if (uuid_status != uuid_s_ok) {
399 err = got_error_uuid(uuid_status);
400 goto done;
403 err = got_repo_open(&repo, (*worktree)->repo_path);
404 if (err)
405 goto done;
407 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 base_commit_id_str);
409 if (err)
410 goto done;
412 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 GOT_WORKTREE_HEAD_REF);
414 done:
415 if (repo)
416 got_repo_close(repo);
417 free(path_got);
418 free(path_lock);
419 free(base_commit_id_str);
420 free(uuidstr);
421 free(formatstr);
422 if (err) {
423 if (fd != -1)
424 close(fd);
425 if (*worktree != NULL)
426 got_worktree_close(*worktree);
427 *worktree = NULL;
428 } else
429 (*worktree)->lockfd = fd;
431 return err;
434 const struct got_error *
435 got_worktree_open(struct got_worktree **worktree, const char *path)
437 const struct got_error *err = NULL;
439 do {
440 err = open_worktree(worktree, path);
441 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 return err;
443 if (*worktree)
444 return NULL;
445 path = dirname(path);
446 if (path == NULL)
447 return got_error_from_errno2("dirname", path);
448 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 return got_error(GOT_ERR_NOT_WORKTREE);
453 const struct got_error *
454 got_worktree_close(struct got_worktree *worktree)
456 const struct got_error *err = NULL;
457 free(worktree->root_path);
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
466 free(worktree);
467 return err;
470 const char *
471 got_worktree_get_root_path(struct got_worktree *worktree)
473 return worktree->root_path;
476 const char *
477 got_worktree_get_repo_path(struct got_worktree *worktree)
479 return worktree->repo_path;
482 const char *
483 got_worktree_get_path_prefix(struct got_worktree *worktree)
485 return worktree->path_prefix;
488 const struct got_error *
489 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 const char *path_prefix)
492 char *absprefix = NULL;
494 if (!got_path_is_absolute(path_prefix)) {
495 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 return got_error_from_errno("asprintf");
498 *match = (strcmp(absprefix ? absprefix : path_prefix,
499 worktree->path_prefix) == 0);
500 free(absprefix);
501 return NULL;
504 const char *
505 got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 return worktree->head_ref_name;
510 const struct got_error *
511 got_worktree_set_head_ref(struct got_worktree *worktree,
512 struct got_reference *head_ref)
514 const struct got_error *err = NULL;
515 char *path_got = NULL, *head_ref_name = NULL;
517 if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 GOT_WORKTREE_GOT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
520 path_got = NULL;
521 goto done;
524 head_ref_name = strdup(got_ref_get_name(head_ref));
525 if (head_ref_name == NULL) {
526 err = got_error_from_errno("strdup");
527 goto done;
530 err = write_head_ref(path_got, head_ref);
531 if (err)
532 goto done;
534 free(worktree->head_ref_name);
535 worktree->head_ref_name = head_ref_name;
536 done:
537 free(path_got);
538 if (err)
539 free(head_ref_name);
540 return err;
543 struct got_object_id *
544 got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 return worktree->base_commit_id;
549 const struct got_error *
550 got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 struct got_repository *repo, struct got_object_id *commit_id)
553 const struct got_error *err;
554 struct got_object *obj = NULL;
555 char *id_str = NULL;
556 char *path_got = NULL;
558 if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 GOT_WORKTREE_GOT_DIR) == -1) {
560 err = got_error_from_errno("asprintf");
561 path_got = NULL;
562 goto done;
565 err = got_object_open(&obj, repo, commit_id);
566 if (err)
567 return err;
569 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 err = got_error(GOT_ERR_OBJ_TYPE);
571 goto done;
574 /* Record our base commit. */
575 err = got_object_id_str(&id_str, commit_id);
576 if (err)
577 goto done;
578 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
579 if (err)
580 goto done;
582 free(worktree->base_commit_id);
583 worktree->base_commit_id = got_object_id_dup(commit_id);
584 if (worktree->base_commit_id == NULL) {
585 err = got_error_from_errno("got_object_id_dup");
586 goto done;
588 done:
589 if (obj)
590 got_object_close(obj);
591 free(id_str);
592 free(path_got);
593 return err;
596 static const struct got_error *
597 lock_worktree(struct got_worktree *worktree, int operation)
599 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 : got_error_from_errno2("flock",
602 got_worktree_get_root_path(worktree)));
603 return NULL;
606 static const struct got_error *
607 add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 const struct got_error *err = NULL;
610 char *abspath;
612 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 return got_error_from_errno("asprintf");
615 err = got_path_mkdir(abspath);
616 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 struct stat sb;
618 err = NULL;
619 if (lstat(abspath, &sb) == -1) {
620 err = got_error_from_errno2("lstat", abspath);
621 } else if (!S_ISDIR(sb.st_mode)) {
622 /* TODO directory is obstructed; do something */
623 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
626 free(abspath);
627 return err;
630 static const struct got_error *
631 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 const struct got_error *err = NULL;
634 uint8_t fbuf1[8192];
635 uint8_t fbuf2[8192];
636 size_t flen1 = 0, flen2 = 0;
638 *same = 1;
640 for (;;) {
641 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 if (flen1 == 0 && ferror(f1)) {
643 err = got_error_from_errno("fread");
644 break;
646 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 if (flen2 == 0 && ferror(f2)) {
648 err = got_error_from_errno("fread");
649 break;
651 if (flen1 == 0) {
652 if (flen2 != 0)
653 *same = 0;
654 break;
655 } else if (flen2 == 0) {
656 if (flen1 != 0)
657 *same = 0;
658 break;
659 } else if (flen1 == flen2) {
660 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
661 *same = 0;
662 break;
664 } else {
665 *same = 0;
666 break;
670 return err;
673 static const struct got_error *
674 check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 const struct got_error *err = NULL;
677 struct stat sb;
678 size_t size1, size2;
679 FILE *f1 = NULL, *f2 = NULL;
681 *same = 1;
683 if (lstat(f1_path, &sb) != 0) {
684 err = got_error_from_errno2("lstat", f1_path);
685 goto done;
687 size1 = sb.st_size;
689 if (lstat(f2_path, &sb) != 0) {
690 err = got_error_from_errno2("lstat", f2_path);
691 goto done;
693 size2 = sb.st_size;
695 if (size1 != size2) {
696 *same = 0;
697 return NULL;
700 f1 = fopen(f1_path, "r");
701 if (f1 == NULL)
702 return got_error_from_errno2("open", f1_path);
704 f2 = fopen(f2_path, "r");
705 if (f2 == NULL) {
706 err = got_error_from_errno2("open", f2_path);
707 goto done;
710 err = check_file_contents_equal(same, f1, f2);
711 done:
712 if (f1 && fclose(f1) != 0 && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (f2 && fclose(f2) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
717 return err;
720 /*
721 * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 * blob_deriv acts as the first derived version, and the file on disk
723 * acts as the second derived version.
724 */
725 static const struct got_error *
726 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 struct got_blob_object *blob_orig, const char *ondisk_path,
728 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 struct got_object_id *deriv_base_commit_id,
730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
731 void *progress_arg)
733 const struct got_error *err = NULL;
734 int merged_fd = -1;
735 FILE *f_deriv = NULL, *f_orig = NULL;
736 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
738 char *id_str = NULL;
739 char *label_deriv = NULL;
740 int overlapcnt = 0;
741 char *parent;
743 *local_changes_subsumed = 0;
745 parent = dirname(ondisk_path);
746 if (parent == NULL)
747 return got_error_from_errno2("dirname", ondisk_path);
749 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 return got_error_from_errno("asprintf");
752 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
753 if (err)
754 goto done;
756 free(base_path);
757 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 err = got_error_from_errno("asprintf");
759 base_path = NULL;
760 goto done;
763 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
764 if (err)
765 goto done;
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
767 blob_deriv);
768 if (err)
769 goto done;
771 free(base_path);
772 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 err = got_error_from_errno("asprintf");
774 base_path = NULL;
775 goto done;
778 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
779 if (err)
780 goto done;
781 if (blob_orig) {
782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
783 blob_orig);
784 if (err)
785 goto done;
786 } else {
787 /*
788 * If the file has no blob, this is an "add vs add" conflict,
789 * and we simply use an empty ancestor file to make both files
790 * appear in the merged result in their entirety.
791 */
794 err = got_object_id_str(&id_str, deriv_base_commit_id);
795 if (err)
796 goto done;
797 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 err = got_error_from_errno("asprintf");
799 goto done;
802 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 blob_orig_path, ondisk_path, label_deriv, path);
804 if (err)
805 goto done;
807 err = (*progress_cb)(progress_arg,
808 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 if (err)
810 goto done;
812 if (fsync(merged_fd) != 0) {
813 err = got_error_from_errno("fsync");
814 goto done;
817 /* Check if a clean merge has subsumed all local changes. */
818 if (overlapcnt == 0) {
819 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
820 merged_path);
821 if (err)
822 goto done;
825 if (chmod(merged_path, st_mode) != 0) {
826 err = got_error_from_errno2("chmod", merged_path);
827 goto done;
830 if (rename(merged_path, ondisk_path) != 0) {
831 err = got_error_from_errno3("rename", merged_path,
832 ondisk_path);
833 unlink(merged_path);
834 goto done;
837 done:
838 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 err = got_error_from_errno("close");
840 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 err = got_error_from_errno("fclose");
842 if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 err = got_error_from_errno("fclose");
844 free(merged_path);
845 free(base_path);
846 if (blob_deriv_path) {
847 unlink(blob_deriv_path);
848 free(blob_deriv_path);
850 if (blob_orig_path) {
851 unlink(blob_orig_path);
852 free(blob_orig_path);
854 free(id_str);
855 free(label_deriv);
856 return err;
859 static const struct got_error *
860 update_blob_fileindex_entry(struct got_worktree *worktree,
861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 int update_timestamps)
865 const struct got_error *err = NULL;
867 if (ie == NULL)
868 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
869 if (ie)
870 err = got_fileindex_entry_update(ie, ondisk_path,
871 blob->id.sha1, worktree->base_commit_id->sha1,
872 update_timestamps);
873 else {
874 struct got_fileindex_entry *new_ie;
875 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 path, blob->id.sha1, worktree->base_commit_id->sha1);
877 if (!err)
878 err = got_fileindex_entry_add(fileindex, new_ie);
880 return err;
883 static const struct got_error *
884 install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 const char *path, uint16_t te_mode, uint16_t st_mode,
886 struct got_blob_object *blob, int restoring_missing_file,
887 int reverting_versioned_file, struct got_repository *repo,
888 got_worktree_checkout_cb progress_cb, void *progress_arg)
890 const struct got_error *err = NULL;
891 int fd = -1;
892 size_t len, hdrlen;
893 int update = 0;
894 char *tmppath = NULL;
896 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 GOT_DEFAULT_FILE_MODE);
898 if (fd == -1) {
899 if (errno == ENOENT) {
900 char *parent = dirname(path);
901 if (parent == NULL)
902 return got_error_from_errno2("dirname", path);
903 err = add_dir_on_disk(worktree, parent);
904 if (err)
905 return err;
906 fd = open(ondisk_path,
907 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 GOT_DEFAULT_FILE_MODE);
909 if (fd == -1)
910 return got_error_from_errno2("open",
911 ondisk_path);
912 } else if (errno == EEXIST) {
913 if (!S_ISREG(st_mode)) {
914 /* TODO file is obstructed; do something */
915 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
916 goto done;
917 } else {
918 err = got_opentemp_named_fd(&tmppath, &fd,
919 ondisk_path);
920 if (err)
921 goto done;
922 update = 1;
924 } else
925 return got_error_from_errno2("open", ondisk_path);
928 if (restoring_missing_file)
929 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 else if (reverting_versioned_file)
931 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 else
933 err = (*progress_cb)(progress_arg,
934 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
935 if (err)
936 goto done;
938 hdrlen = got_object_blob_get_hdrlen(blob);
939 do {
940 const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 err = got_object_blob_read_block(&len, blob);
942 if (err)
943 break;
944 if (len > 0) {
945 /* Skip blob object header first time around. */
946 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
947 if (outlen == -1) {
948 err = got_error_from_errno("write");
949 goto done;
950 } else if (outlen != len - hdrlen) {
951 err = got_error(GOT_ERR_IO);
952 goto done;
954 hdrlen = 0;
956 } while (len != 0);
958 if (fsync(fd) != 0) {
959 err = got_error_from_errno("fsync");
960 goto done;
963 if (update) {
964 if (rename(tmppath, ondisk_path) != 0) {
965 err = got_error_from_errno3("rename", tmppath,
966 ondisk_path);
967 unlink(tmppath);
968 goto done;
972 if (te_mode & S_IXUSR) {
973 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
975 goto done;
977 } else {
978 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 err = got_error_from_errno2("chmod", ondisk_path);
980 goto done;
984 done:
985 if (fd != -1 && close(fd) != 0 && err == NULL)
986 err = got_error_from_errno("close");
987 free(tmppath);
988 return err;
991 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 static const struct got_error *
993 get_modified_file_content_status(unsigned char *status, FILE *f)
995 const struct got_error *err = NULL;
996 const char *markers[3] = {
997 GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 GOT_DIFF_CONFLICT_MARKER_SEP,
999 GOT_DIFF_CONFLICT_MARKER_END
1001 int i = 0;
1002 char *line;
1003 size_t len;
1004 const char delim[3] = {'\0', '\0', '\0'};
1006 while (*status == GOT_STATUS_MODIFY) {
1007 line = fparseln(f, &len, NULL, delim, 0);
1008 if (line == NULL) {
1009 if (feof(f))
1010 break;
1011 err = got_ferror(f, GOT_ERR_IO);
1012 break;
1015 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 == 0)
1018 *status = GOT_STATUS_CONFLICT;
1019 else
1020 i++;
1024 return err;
1027 static int
1028 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1030 return !(ie->ctime_sec == sb->st_ctime &&
1031 ie->ctime_nsec == sb->st_ctimensec &&
1032 ie->mtime_sec == sb->st_mtime &&
1033 ie->mtime_nsec == sb->st_mtimensec &&
1034 ie->size == (sb->st_size & 0xffffffff));
1037 static unsigned char
1038 get_staged_status(struct got_fileindex_entry *ie)
1040 switch (got_fileindex_entry_stage_get(ie)) {
1041 case GOT_FILEIDX_STAGE_ADD:
1042 return GOT_STATUS_ADD;
1043 case GOT_FILEIDX_STAGE_DELETE:
1044 return GOT_STATUS_DELETE;
1045 case GOT_FILEIDX_STAGE_MODIFY:
1046 return GOT_STATUS_MODIFY;
1047 default:
1048 return GOT_STATUS_NO_CHANGE;
1052 static const struct got_error *
1053 get_file_status(unsigned char *status, struct stat *sb,
1054 struct got_fileindex_entry *ie, const char *abspath,
1055 struct got_repository *repo)
1057 const struct got_error *err = NULL;
1058 struct got_object_id id;
1059 size_t hdrlen;
1060 FILE *f = NULL;
1061 uint8_t fbuf[8192];
1062 struct got_blob_object *blob = NULL;
1063 size_t flen, blen;
1064 unsigned char staged_status = get_staged_status(ie);
1066 *status = GOT_STATUS_NO_CHANGE;
1068 if (lstat(abspath, sb) == -1) {
1069 if (errno == ENOENT) {
1070 if (got_fileindex_entry_has_file_on_disk(ie))
1071 *status = GOT_STATUS_MISSING;
1072 else
1073 *status = GOT_STATUS_DELETE;
1074 return NULL;
1076 return got_error_from_errno2("lstat", abspath);
1079 if (!S_ISREG(sb->st_mode)) {
1080 *status = GOT_STATUS_OBSTRUCTED;
1081 return NULL;
1084 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1085 *status = GOT_STATUS_DELETE;
1086 return NULL;
1087 } else if (!got_fileindex_entry_has_blob(ie) &&
1088 staged_status != GOT_STATUS_ADD) {
1089 *status = GOT_STATUS_ADD;
1090 return NULL;
1093 if (!stat_info_differs(ie, sb))
1094 return NULL;
1096 if (staged_status == GOT_STATUS_MODIFY ||
1097 staged_status == GOT_STATUS_ADD)
1098 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1099 else
1100 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1102 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1103 if (err)
1104 return err;
1106 f = fopen(abspath, "r");
1107 if (f == NULL) {
1108 err = got_error_from_errno2("fopen", abspath);
1109 goto done;
1111 hdrlen = got_object_blob_get_hdrlen(blob);
1112 for (;;) {
1113 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1114 err = got_object_blob_read_block(&blen, blob);
1115 if (err)
1116 goto done;
1117 /* Skip length of blob object header first time around. */
1118 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1119 if (flen == 0 && ferror(f)) {
1120 err = got_error_from_errno("fread");
1121 goto done;
1123 if (blen == 0) {
1124 if (flen != 0)
1125 *status = GOT_STATUS_MODIFY;
1126 break;
1127 } else if (flen == 0) {
1128 if (blen != 0)
1129 *status = GOT_STATUS_MODIFY;
1130 break;
1131 } else if (blen - hdrlen == flen) {
1132 /* Skip blob object header first time around. */
1133 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1134 *status = GOT_STATUS_MODIFY;
1135 break;
1137 } else {
1138 *status = GOT_STATUS_MODIFY;
1139 break;
1141 hdrlen = 0;
1144 if (*status == GOT_STATUS_MODIFY) {
1145 rewind(f);
1146 err = get_modified_file_content_status(status, f);
1148 done:
1149 if (blob)
1150 got_object_blob_close(blob);
1151 if (f)
1152 fclose(f);
1153 return err;
1157 * Update timestamps in the file index if a file is unmodified and
1158 * we had to run a full content comparison to find out.
1160 static const struct got_error *
1161 sync_timestamps(char *ondisk_path, unsigned char status,
1162 struct got_fileindex_entry *ie, struct stat *sb)
1164 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1165 return got_fileindex_entry_update(ie, ondisk_path,
1166 ie->blob_sha1, ie->commit_sha1, 1);
1168 return NULL;
1171 static const struct got_error *
1172 update_blob(struct got_worktree *worktree,
1173 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1174 struct got_tree_entry *te, const char *path,
1175 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1176 void *progress_arg)
1178 const struct got_error *err = NULL;
1179 struct got_blob_object *blob = NULL;
1180 char *ondisk_path;
1181 unsigned char status = GOT_STATUS_NO_CHANGE;
1182 struct stat sb;
1184 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1185 return got_error_from_errno("asprintf");
1187 if (ie) {
1188 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1189 if (err)
1190 goto done;
1191 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1192 sb.st_mode = got_fileindex_perms_to_st(ie);
1193 } else
1194 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1196 if (status == GOT_STATUS_OBSTRUCTED) {
1197 err = (*progress_cb)(progress_arg, status, path);
1198 goto done;
1201 if (ie && status != GOT_STATUS_MISSING) {
1202 if (got_fileindex_entry_has_commit(ie) &&
1203 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1204 SHA1_DIGEST_LENGTH) == 0) {
1205 err = sync_timestamps(ondisk_path, status, ie, &sb);
1206 if (err)
1207 goto done;
1208 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1209 path);
1210 goto done;
1212 if (got_fileindex_entry_has_blob(ie) &&
1213 memcmp(ie->blob_sha1, te->id->sha1,
1214 SHA1_DIGEST_LENGTH) == 0) {
1215 err = sync_timestamps(ondisk_path, status, ie, &sb);
1216 goto done;
1220 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1221 if (err)
1222 goto done;
1224 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1225 int update_timestamps;
1226 struct got_blob_object *blob2 = NULL;
1227 if (got_fileindex_entry_has_blob(ie)) {
1228 struct got_object_id id2;
1229 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1230 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1231 if (err)
1232 goto done;
1234 err = merge_blob(&update_timestamps, worktree, blob2,
1235 ondisk_path, path, sb.st_mode, blob,
1236 worktree->base_commit_id, repo,
1237 progress_cb, progress_arg);
1238 if (blob2)
1239 got_object_blob_close(blob2);
1241 * Do not update timestamps of files with local changes.
1242 * Otherwise, a future status walk would treat them as
1243 * unmodified files again.
1245 err = got_fileindex_entry_update(ie, ondisk_path,
1246 blob->id.sha1, worktree->base_commit_id->sha1,
1247 update_timestamps);
1248 } else if (status == GOT_STATUS_DELETE) {
1249 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1250 if (err)
1251 goto done;
1252 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1253 ondisk_path, path, blob, 0);
1254 if (err)
1255 goto done;
1256 } else {
1257 err = install_blob(worktree, ondisk_path, path, te->mode,
1258 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1259 repo, progress_cb, progress_arg);
1260 if (err)
1261 goto done;
1262 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1263 ondisk_path, path, blob, 1);
1264 if (err)
1265 goto done;
1267 got_object_blob_close(blob);
1268 done:
1269 free(ondisk_path);
1270 return err;
1273 static const struct got_error *
1274 remove_ondisk_file(const char *root_path, const char *path)
1276 const struct got_error *err = NULL;
1277 char *ondisk_path = NULL;
1279 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1280 return got_error_from_errno("asprintf");
1282 if (unlink(ondisk_path) == -1) {
1283 if (errno != ENOENT)
1284 err = got_error_from_errno2("unlink", ondisk_path);
1285 } else {
1286 char *parent = dirname(ondisk_path);
1287 while (parent && strcmp(parent, root_path) != 0) {
1288 if (rmdir(parent) == -1) {
1289 if (errno != ENOTEMPTY)
1290 err = got_error_from_errno2("rmdir",
1291 parent);
1292 break;
1294 parent = dirname(parent);
1297 free(ondisk_path);
1298 return err;
1301 static const struct got_error *
1302 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1303 struct got_fileindex_entry *ie, struct got_repository *repo,
1304 got_worktree_checkout_cb progress_cb, void *progress_arg)
1306 const struct got_error *err = NULL;
1307 unsigned char status;
1308 struct stat sb;
1309 char *ondisk_path;
1311 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1312 == -1)
1313 return got_error_from_errno("asprintf");
1315 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1316 if (err)
1317 return err;
1319 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1320 status == GOT_STATUS_ADD) {
1321 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1322 if (err)
1323 return err;
1325 * Preserve the working file and change the deleted blob's
1326 * entry into a schedule-add entry.
1328 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1329 0);
1330 if (err)
1331 return err;
1332 } else {
1333 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1334 if (err)
1335 return err;
1336 if (status == GOT_STATUS_NO_CHANGE) {
1337 err = remove_ondisk_file(worktree->root_path, ie->path);
1338 if (err)
1339 return err;
1341 got_fileindex_entry_remove(fileindex, ie);
1344 return err;
1347 struct diff_cb_arg {
1348 struct got_fileindex *fileindex;
1349 struct got_worktree *worktree;
1350 struct got_repository *repo;
1351 got_worktree_checkout_cb progress_cb;
1352 void *progress_arg;
1353 got_worktree_cancel_cb cancel_cb;
1354 void *cancel_arg;
1357 static const struct got_error *
1358 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1359 struct got_tree_entry *te, const char *parent_path)
1361 struct diff_cb_arg *a = arg;
1363 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1364 return got_error(GOT_ERR_CANCELLED);
1366 return update_blob(a->worktree, a->fileindex, ie, te,
1367 ie->path, a->repo, a->progress_cb, a->progress_arg);
1370 static const struct got_error *
1371 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1373 struct diff_cb_arg *a = arg;
1375 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1376 return got_error(GOT_ERR_CANCELLED);
1378 return delete_blob(a->worktree, a->fileindex, ie,
1379 a->repo, a->progress_cb, a->progress_arg);
1382 static const struct got_error *
1383 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1385 struct diff_cb_arg *a = arg;
1386 const struct got_error *err;
1387 char *path;
1389 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1390 return got_error(GOT_ERR_CANCELLED);
1392 if (asprintf(&path, "%s%s%s", parent_path,
1393 parent_path[0] ? "/" : "", te->name)
1394 == -1)
1395 return got_error_from_errno("asprintf");
1397 if (S_ISDIR(te->mode))
1398 err = add_dir_on_disk(a->worktree, path);
1399 else
1400 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1401 a->repo, a->progress_cb, a->progress_arg);
1403 free(path);
1404 return err;
1407 static const struct got_error *
1408 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1410 const struct got_error *err = NULL;
1411 char *uuidstr = NULL;
1412 uint32_t uuid_status;
1414 *refname = NULL;
1416 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1417 if (uuid_status != uuid_s_ok)
1418 return got_error_uuid(uuid_status);
1420 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1421 == -1) {
1422 err = got_error_from_errno("asprintf");
1423 *refname = NULL;
1425 free(uuidstr);
1426 return err;
1429 const struct got_error *
1430 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1432 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1435 static const struct got_error *
1436 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1438 return get_ref_name(refname, worktree,
1439 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1442 static const struct got_error *
1443 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1445 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1448 static const struct got_error *
1449 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1451 return get_ref_name(refname, worktree,
1452 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1455 static const struct got_error *
1456 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1458 return get_ref_name(refname, worktree,
1459 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1462 static const struct got_error *
1463 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1465 return get_ref_name(refname, worktree,
1466 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1469 static const struct got_error *
1470 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1472 return get_ref_name(refname, worktree,
1473 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1476 static const struct got_error *
1477 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1479 return get_ref_name(refname, worktree,
1480 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1483 static const struct got_error *
1484 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1486 return get_ref_name(refname, worktree,
1487 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1490 const struct got_error *
1491 got_worktree_get_histedit_script_path(char **path,
1492 struct got_worktree *worktree)
1494 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1495 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1496 *path = NULL;
1497 return got_error_from_errno("asprintf");
1499 return NULL;
1503 * Prevent Git's garbage collector from deleting our base commit by
1504 * setting a reference to our base commit's ID.
1506 static const struct got_error *
1507 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1509 const struct got_error *err = NULL;
1510 struct got_reference *ref = NULL;
1511 char *refname;
1513 err = got_worktree_get_base_ref_name(&refname, worktree);
1514 if (err)
1515 return err;
1517 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1518 if (err)
1519 goto done;
1521 err = got_ref_write(ref, repo);
1522 done:
1523 free(refname);
1524 if (ref)
1525 got_ref_close(ref);
1526 return err;
1529 static const struct got_error *
1530 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1532 const struct got_error *err = NULL;
1534 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1535 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1536 err = got_error_from_errno("asprintf");
1537 *fileindex_path = NULL;
1539 return err;
1543 static const struct got_error *
1544 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1545 struct got_worktree *worktree)
1547 const struct got_error *err = NULL;
1548 FILE *index = NULL;
1550 *fileindex_path = NULL;
1551 *fileindex = got_fileindex_alloc();
1552 if (*fileindex == NULL)
1553 return got_error_from_errno("got_fileindex_alloc");
1555 err = get_fileindex_path(fileindex_path, worktree);
1556 if (err)
1557 goto done;
1559 index = fopen(*fileindex_path, "rb");
1560 if (index == NULL) {
1561 if (errno != ENOENT)
1562 err = got_error_from_errno2("fopen", *fileindex_path);
1563 } else {
1564 err = got_fileindex_read(*fileindex, index);
1565 if (fclose(index) != 0 && err == NULL)
1566 err = got_error_from_errno("fclose");
1568 done:
1569 if (err) {
1570 free(*fileindex_path);
1571 *fileindex_path = NULL;
1572 got_fileindex_free(*fileindex);
1573 *fileindex = NULL;
1575 return err;
1578 struct bump_base_commit_id_arg {
1579 struct got_object_id *base_commit_id;
1580 const char *path;
1581 size_t path_len;
1582 const char *entry_name;
1583 got_worktree_checkout_cb progress_cb;
1584 void *progress_arg;
1587 /* Bump base commit ID of all files within an updated part of the work tree. */
1588 static const struct got_error *
1589 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1591 const struct got_error *err;
1592 struct bump_base_commit_id_arg *a = arg;
1594 if (a->entry_name) {
1595 if (strcmp(ie->path, a->path) != 0)
1596 return NULL;
1597 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1598 return NULL;
1600 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1601 SHA1_DIGEST_LENGTH) == 0)
1602 return NULL;
1604 if (a->progress_cb) {
1605 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1606 ie->path);
1607 if (err)
1608 return err;
1610 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1611 return NULL;
1614 static const struct got_error *
1615 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1617 const struct got_error *err = NULL;
1618 char *new_fileindex_path = NULL;
1619 FILE *new_index = NULL;
1621 err = got_opentemp_named(&new_fileindex_path, &new_index,
1622 fileindex_path);
1623 if (err)
1624 goto done;
1626 err = got_fileindex_write(fileindex, new_index);
1627 if (err)
1628 goto done;
1630 if (rename(new_fileindex_path, fileindex_path) != 0) {
1631 err = got_error_from_errno3("rename", new_fileindex_path,
1632 fileindex_path);
1633 unlink(new_fileindex_path);
1635 done:
1636 if (new_index)
1637 fclose(new_index);
1638 free(new_fileindex_path);
1639 return err;
1642 static const struct got_error *
1643 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1644 struct got_object_id **tree_id, const char *wt_relpath,
1645 struct got_worktree *worktree, struct got_repository *repo)
1647 const struct got_error *err = NULL;
1648 struct got_object_id *id = NULL;
1649 char *in_repo_path = NULL;
1650 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1652 *entry_type = GOT_OBJ_TYPE_ANY;
1653 *tree_relpath = NULL;
1654 *tree_id = NULL;
1656 if (wt_relpath[0] == '\0') {
1657 /* Check out all files within the work tree. */
1658 *entry_type = GOT_OBJ_TYPE_TREE;
1659 *tree_relpath = strdup("");
1660 if (*tree_relpath == NULL) {
1661 err = got_error_from_errno("strdup");
1662 goto done;
1664 err = got_object_id_by_path(tree_id, repo,
1665 worktree->base_commit_id, worktree->path_prefix);
1666 if (err)
1667 goto done;
1668 return NULL;
1671 /* Check out a subset of files in the work tree. */
1673 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1674 is_root_wt ? "" : "/", wt_relpath) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 goto done;
1679 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1680 in_repo_path);
1681 if (err)
1682 goto done;
1684 free(in_repo_path);
1685 in_repo_path = NULL;
1687 err = got_object_get_type(entry_type, repo, id);
1688 if (err)
1689 goto done;
1691 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1692 /* Check out a single file. */
1693 if (strchr(wt_relpath, '/') == NULL) {
1694 /* Check out a single file in work tree's root dir. */
1695 in_repo_path = strdup(worktree->path_prefix);
1696 if (in_repo_path == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1700 *tree_relpath = strdup("");
1701 if (*tree_relpath == NULL) {
1702 err = got_error_from_errno("strdup");
1703 goto done;
1705 } else {
1706 /* Check out a single file in a subdirectory. */
1707 err = got_path_dirname(tree_relpath, wt_relpath);
1708 if (err)
1709 return err;
1710 if (asprintf(&in_repo_path, "%s%s%s",
1711 worktree->path_prefix, is_root_wt ? "" : "/",
1712 *tree_relpath) == -1) {
1713 err = got_error_from_errno("asprintf");
1714 goto done;
1717 err = got_object_id_by_path(tree_id, repo,
1718 worktree->base_commit_id, in_repo_path);
1719 } else {
1720 /* Check out all files within a subdirectory. */
1721 *tree_id = got_object_id_dup(id);
1722 if (*tree_id == NULL) {
1723 err = got_error_from_errno("got_object_id_dup");
1724 goto done;
1726 *tree_relpath = strdup(wt_relpath);
1727 if (*tree_relpath == NULL) {
1728 err = got_error_from_errno("strdup");
1729 goto done;
1732 done:
1733 free(id);
1734 free(in_repo_path);
1735 if (err) {
1736 *entry_type = GOT_OBJ_TYPE_ANY;
1737 free(*tree_relpath);
1738 *tree_relpath = NULL;
1739 free(*tree_id);
1740 *tree_id = NULL;
1742 return err;
1745 static const struct got_error *
1746 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1747 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1748 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1749 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1751 const struct got_error *err = NULL;
1752 struct got_commit_object *commit = NULL;
1753 struct got_tree_object *tree = NULL;
1754 struct got_fileindex_diff_tree_cb diff_cb;
1755 struct diff_cb_arg arg;
1757 err = ref_base_commit(worktree, repo);
1758 if (err)
1759 goto done;
1761 err = got_object_open_as_commit(&commit, repo,
1762 worktree->base_commit_id);
1763 if (err)
1764 goto done;
1766 err = got_object_open_as_tree(&tree, repo, tree_id);
1767 if (err)
1768 goto done;
1770 if (entry_name &&
1771 got_object_tree_find_entry(tree, entry_name) == NULL) {
1772 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1773 goto done;
1776 diff_cb.diff_old_new = diff_old_new;
1777 diff_cb.diff_old = diff_old;
1778 diff_cb.diff_new = diff_new;
1779 arg.fileindex = fileindex;
1780 arg.worktree = worktree;
1781 arg.repo = repo;
1782 arg.progress_cb = progress_cb;
1783 arg.progress_arg = progress_arg;
1784 arg.cancel_cb = cancel_cb;
1785 arg.cancel_arg = cancel_arg;
1786 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1787 entry_name, repo, &diff_cb, &arg);
1788 done:
1789 if (tree)
1790 got_object_tree_close(tree);
1791 if (commit)
1792 got_object_commit_close(commit);
1793 return err;
1796 const struct got_error *
1797 got_worktree_checkout_files(struct got_worktree *worktree,
1798 struct got_pathlist_head *paths, struct got_repository *repo,
1799 got_worktree_checkout_cb progress_cb, void *progress_arg,
1800 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1802 const struct got_error *err = NULL, *sync_err, *unlockerr;
1803 struct got_commit_object *commit = NULL;
1804 struct got_tree_object *tree = NULL;
1805 struct got_fileindex *fileindex = NULL;
1806 char *fileindex_path = NULL;
1807 struct got_pathlist_entry *pe;
1808 struct tree_path_data {
1809 SIMPLEQ_ENTRY(tree_path_data) entry;
1810 struct got_object_id *tree_id;
1811 int entry_type;
1812 char *relpath;
1813 char *entry_name;
1814 } *tpd = NULL;
1815 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1817 SIMPLEQ_INIT(&tree_paths);
1819 err = lock_worktree(worktree, LOCK_EX);
1820 if (err)
1821 return err;
1823 /* Map all specified paths to in-repository trees. */
1824 TAILQ_FOREACH(pe, paths, entry) {
1825 tpd = malloc(sizeof(*tpd));
1826 if (tpd == NULL) {
1827 err = got_error_from_errno("malloc");
1828 goto done;
1831 err = find_tree_entry_for_checkout(&tpd->entry_type,
1832 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1833 if (err) {
1834 free(tpd);
1835 goto done;
1838 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1839 err = got_path_basename(&tpd->entry_name, pe->path);
1840 if (err) {
1841 free(tpd->relpath);
1842 free(tpd->tree_id);
1843 free(tpd);
1844 goto done;
1846 } else
1847 tpd->entry_name = NULL;
1849 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1853 * Read the file index.
1854 * Checking out files is supposed to be an idempotent operation.
1855 * If the on-disk file index is incomplete we will try to complete it.
1857 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1858 if (err)
1859 goto done;
1861 tpd = SIMPLEQ_FIRST(&tree_paths);
1862 TAILQ_FOREACH(pe, paths, entry) {
1863 struct bump_base_commit_id_arg bbc_arg;
1865 err = checkout_files(worktree, fileindex, tpd->relpath,
1866 tpd->tree_id, tpd->entry_name, repo,
1867 progress_cb, progress_arg, cancel_cb, cancel_arg);
1868 if (err)
1869 break;
1871 bbc_arg.base_commit_id = worktree->base_commit_id;
1872 bbc_arg.entry_name = tpd->entry_name;
1873 bbc_arg.path = pe->path;
1874 bbc_arg.path_len = pe->path_len;
1875 bbc_arg.progress_cb = progress_cb;
1876 bbc_arg.progress_arg = progress_arg;
1877 err = got_fileindex_for_each_entry_safe(fileindex,
1878 bump_base_commit_id, &bbc_arg);
1879 if (err)
1880 break;
1882 tpd = SIMPLEQ_NEXT(tpd, entry);
1884 sync_err = sync_fileindex(fileindex, fileindex_path);
1885 if (sync_err && err == NULL)
1886 err = sync_err;
1887 done:
1888 free(fileindex_path);
1889 if (tree)
1890 got_object_tree_close(tree);
1891 if (commit)
1892 got_object_commit_close(commit);
1893 if (fileindex)
1894 got_fileindex_free(fileindex);
1895 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1896 tpd = SIMPLEQ_FIRST(&tree_paths);
1897 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1898 free(tpd->relpath);
1899 free(tpd->tree_id);
1900 free(tpd);
1902 unlockerr = lock_worktree(worktree, LOCK_SH);
1903 if (unlockerr && err == NULL)
1904 err = unlockerr;
1905 return err;
1908 struct merge_file_cb_arg {
1909 struct got_worktree *worktree;
1910 struct got_fileindex *fileindex;
1911 got_worktree_checkout_cb progress_cb;
1912 void *progress_arg;
1913 got_worktree_cancel_cb cancel_cb;
1914 void *cancel_arg;
1915 struct got_object_id *commit_id2;
1918 static const struct got_error *
1919 merge_file_cb(void *arg, struct got_blob_object *blob1,
1920 struct got_blob_object *blob2, struct got_object_id *id1,
1921 struct got_object_id *id2, const char *path1, const char *path2,
1922 struct got_repository *repo)
1924 static const struct got_error *err = NULL;
1925 struct merge_file_cb_arg *a = arg;
1926 struct got_fileindex_entry *ie;
1927 char *ondisk_path = NULL;
1928 struct stat sb;
1929 unsigned char status;
1930 int local_changes_subsumed;
1932 if (blob1 && blob2) {
1933 ie = got_fileindex_entry_get(a->fileindex, path2,
1934 strlen(path2));
1935 if (ie == NULL)
1936 return (*a->progress_cb)(a->progress_arg,
1937 GOT_STATUS_MISSING, path2);
1939 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1940 path2) == -1)
1941 return got_error_from_errno("asprintf");
1943 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1944 if (err)
1945 goto done;
1947 if (status == GOT_STATUS_DELETE) {
1948 err = (*a->progress_cb)(a->progress_arg,
1949 GOT_STATUS_MERGE, path2);
1950 goto done;
1952 if (status != GOT_STATUS_NO_CHANGE &&
1953 status != GOT_STATUS_MODIFY &&
1954 status != GOT_STATUS_CONFLICT &&
1955 status != GOT_STATUS_ADD) {
1956 err = (*a->progress_cb)(a->progress_arg, status, path2);
1957 goto done;
1960 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1961 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1962 a->progress_cb, a->progress_arg);
1963 } else if (blob1) {
1964 ie = got_fileindex_entry_get(a->fileindex, path1,
1965 strlen(path1));
1966 if (ie == NULL)
1967 return (*a->progress_cb)(a->progress_arg,
1968 GOT_STATUS_MISSING, path2);
1970 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1971 path1) == -1)
1972 return got_error_from_errno("asprintf");
1974 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1975 if (err)
1976 goto done;
1978 switch (status) {
1979 case GOT_STATUS_NO_CHANGE:
1980 err = (*a->progress_cb)(a->progress_arg,
1981 GOT_STATUS_DELETE, path1);
1982 if (err)
1983 goto done;
1984 err = remove_ondisk_file(a->worktree->root_path, path1);
1985 if (err)
1986 goto done;
1987 if (ie)
1988 got_fileindex_entry_mark_deleted_from_disk(ie);
1989 break;
1990 case GOT_STATUS_DELETE:
1991 case GOT_STATUS_MISSING:
1992 err = (*a->progress_cb)(a->progress_arg,
1993 GOT_STATUS_DELETE, path1);
1994 if (err)
1995 goto done;
1996 if (ie)
1997 got_fileindex_entry_mark_deleted_from_disk(ie);
1998 break;
1999 case GOT_STATUS_ADD:
2000 case GOT_STATUS_MODIFY:
2001 case GOT_STATUS_CONFLICT:
2002 err = (*a->progress_cb)(a->progress_arg,
2003 GOT_STATUS_CANNOT_DELETE, path1);
2004 if (err)
2005 goto done;
2006 break;
2007 case GOT_STATUS_OBSTRUCTED:
2008 err = (*a->progress_cb)(a->progress_arg, status, path1);
2009 if (err)
2010 goto done;
2011 break;
2012 default:
2013 break;
2015 } else if (blob2) {
2016 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2017 path2) == -1)
2018 return got_error_from_errno("asprintf");
2019 ie = got_fileindex_entry_get(a->fileindex, path2,
2020 strlen(path2));
2021 if (ie) {
2022 err = get_file_status(&status, &sb, ie, ondisk_path,
2023 repo);
2024 if (err)
2025 goto done;
2026 if (status != GOT_STATUS_NO_CHANGE &&
2027 status != GOT_STATUS_MODIFY &&
2028 status != GOT_STATUS_CONFLICT &&
2029 status != GOT_STATUS_ADD) {
2030 err = (*a->progress_cb)(a->progress_arg,
2031 status, path2);
2032 goto done;
2034 err = merge_blob(&local_changes_subsumed, a->worktree,
2035 NULL, ondisk_path, path2, sb.st_mode, blob2,
2036 a->commit_id2, repo,
2037 a->progress_cb, a->progress_arg);
2038 if (status == GOT_STATUS_DELETE) {
2039 err = update_blob_fileindex_entry(a->worktree,
2040 a->fileindex, ie, ondisk_path, ie->path,
2041 blob2, 0);
2042 if (err)
2043 goto done;
2045 } else {
2046 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2047 err = install_blob(a->worktree, ondisk_path, path2,
2048 /* XXX get this from parent tree! */
2049 GOT_DEFAULT_FILE_MODE,
2050 sb.st_mode, blob2, 0, 0, repo,
2051 a->progress_cb, a->progress_arg);
2052 if (err)
2053 goto done;
2054 err = got_fileindex_entry_alloc(&ie,
2055 ondisk_path, path2, NULL, NULL);
2056 if (err)
2057 goto done;
2058 err = got_fileindex_entry_add(a->fileindex, ie);
2059 if (err) {
2060 got_fileindex_entry_free(ie);
2061 goto done;
2065 done:
2066 free(ondisk_path);
2067 return err;
2070 struct check_merge_ok_arg {
2071 struct got_worktree *worktree;
2072 struct got_repository *repo;
2075 static const struct got_error *
2076 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2078 const struct got_error *err = NULL;
2079 struct check_merge_ok_arg *a = arg;
2080 unsigned char status;
2081 struct stat sb;
2082 char *ondisk_path;
2084 /* Reject merges into a work tree with mixed base commits. */
2085 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2086 SHA1_DIGEST_LENGTH))
2087 return got_error(GOT_ERR_MIXED_COMMITS);
2089 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2090 == -1)
2091 return got_error_from_errno("asprintf");
2093 /* Reject merges into a work tree with conflicted files. */
2094 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2095 if (err)
2096 return err;
2097 if (status == GOT_STATUS_CONFLICT)
2098 return got_error(GOT_ERR_CONFLICTS);
2100 return NULL;
2103 static const struct got_error *
2104 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2105 const char *fileindex_path, struct got_object_id *commit_id1,
2106 struct got_object_id *commit_id2, struct got_repository *repo,
2107 got_worktree_checkout_cb progress_cb, void *progress_arg,
2108 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2110 const struct got_error *err = NULL, *sync_err;
2111 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2112 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2113 struct merge_file_cb_arg arg;
2115 if (commit_id1) {
2116 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2117 worktree->path_prefix);
2118 if (err)
2119 goto done;
2121 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2122 if (err)
2123 goto done;
2126 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2127 worktree->path_prefix);
2128 if (err)
2129 goto done;
2131 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2132 if (err)
2133 goto done;
2135 arg.worktree = worktree;
2136 arg.fileindex = fileindex;
2137 arg.progress_cb = progress_cb;
2138 arg.progress_arg = progress_arg;
2139 arg.cancel_cb = cancel_cb;
2140 arg.cancel_arg = cancel_arg;
2141 arg.commit_id2 = commit_id2;
2142 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2143 sync_err = sync_fileindex(fileindex, fileindex_path);
2144 if (sync_err && err == NULL)
2145 err = sync_err;
2146 done:
2147 if (tree1)
2148 got_object_tree_close(tree1);
2149 if (tree2)
2150 got_object_tree_close(tree2);
2151 return err;
2154 const struct got_error *
2155 got_worktree_merge_files(struct got_worktree *worktree,
2156 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2157 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2158 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2160 const struct got_error *err, *unlockerr;
2161 char *fileindex_path = NULL;
2162 struct got_fileindex *fileindex = NULL;
2163 struct check_merge_ok_arg mok_arg;
2165 err = lock_worktree(worktree, LOCK_EX);
2166 if (err)
2167 return err;
2169 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2170 if (err)
2171 goto done;
2173 mok_arg.worktree = worktree;
2174 mok_arg.repo = repo;
2175 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2176 &mok_arg);
2177 if (err)
2178 goto done;
2180 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2181 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2182 done:
2183 if (fileindex)
2184 got_fileindex_free(fileindex);
2185 free(fileindex_path);
2186 unlockerr = lock_worktree(worktree, LOCK_SH);
2187 if (unlockerr && err == NULL)
2188 err = unlockerr;
2189 return err;
2192 struct diff_dir_cb_arg {
2193 struct got_fileindex *fileindex;
2194 struct got_worktree *worktree;
2195 const char *status_path;
2196 size_t status_path_len;
2197 struct got_repository *repo;
2198 got_worktree_status_cb status_cb;
2199 void *status_arg;
2200 got_worktree_cancel_cb cancel_cb;
2201 void *cancel_arg;
2204 static const struct got_error *
2205 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2206 got_worktree_status_cb status_cb, void *status_arg,
2207 struct got_repository *repo)
2209 const struct got_error *err = NULL;
2210 unsigned char status = GOT_STATUS_NO_CHANGE;
2211 unsigned char staged_status = get_staged_status(ie);
2212 struct stat sb;
2213 struct got_object_id blob_id, commit_id, staged_blob_id;
2214 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2215 struct got_object_id *staged_blob_idp = NULL;
2217 err = get_file_status(&status, &sb, ie, abspath, repo);
2218 if (err)
2219 return err;
2221 if (status == GOT_STATUS_NO_CHANGE &&
2222 staged_status == GOT_STATUS_NO_CHANGE)
2223 return NULL;
2225 if (got_fileindex_entry_has_blob(ie)) {
2226 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2227 blob_idp = &blob_id;
2229 if (got_fileindex_entry_has_commit(ie)) {
2230 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2231 commit_idp = &commit_id;
2233 if (staged_status == GOT_STATUS_ADD ||
2234 staged_status == GOT_STATUS_MODIFY) {
2235 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2236 SHA1_DIGEST_LENGTH);
2237 staged_blob_idp = &staged_blob_id;
2240 return (*status_cb)(status_arg, status, staged_status,
2241 ie->path, blob_idp, staged_blob_idp, commit_idp);
2244 static const struct got_error *
2245 status_old_new(void *arg, struct got_fileindex_entry *ie,
2246 struct dirent *de, const char *parent_path)
2248 const struct got_error *err = NULL;
2249 struct diff_dir_cb_arg *a = arg;
2250 char *abspath;
2252 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2253 return got_error(GOT_ERR_CANCELLED);
2255 if (got_path_cmp(parent_path, a->status_path,
2256 strlen(parent_path), a->status_path_len) != 0 &&
2257 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2258 return NULL;
2260 if (parent_path[0]) {
2261 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2262 parent_path, de->d_name) == -1)
2263 return got_error_from_errno("asprintf");
2264 } else {
2265 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2266 de->d_name) == -1)
2267 return got_error_from_errno("asprintf");
2270 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2271 a->repo);
2272 free(abspath);
2273 return err;
2276 static const struct got_error *
2277 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2279 struct diff_dir_cb_arg *a = arg;
2280 struct got_object_id blob_id, commit_id;
2281 unsigned char status;
2283 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2284 return got_error(GOT_ERR_CANCELLED);
2286 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2287 return NULL;
2289 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2290 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2291 if (got_fileindex_entry_has_file_on_disk(ie))
2292 status = GOT_STATUS_MISSING;
2293 else
2294 status = GOT_STATUS_DELETE;
2295 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2296 ie->path, &blob_id, NULL, &commit_id);
2299 static const struct got_error *
2300 status_new(void *arg, struct dirent *de, const char *parent_path)
2302 const struct got_error *err = NULL;
2303 struct diff_dir_cb_arg *a = arg;
2304 char *path = NULL;
2306 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2307 return got_error(GOT_ERR_CANCELLED);
2309 if (de->d_type == DT_DIR)
2310 return NULL;
2312 /* XXX ignore symlinks for now */
2313 if (de->d_type == DT_LNK)
2314 return NULL;
2316 if (parent_path[0]) {
2317 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2318 return got_error_from_errno("asprintf");
2319 } else {
2320 path = de->d_name;
2323 if (got_path_is_child(path, a->status_path, a->status_path_len))
2324 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2325 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2326 if (parent_path[0])
2327 free(path);
2328 return err;
2331 static const struct got_error *
2332 report_single_file_status(const char *path, const char *ondisk_path,
2333 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2334 void *status_arg, struct got_repository *repo)
2336 struct got_fileindex_entry *ie;
2337 struct stat sb;
2339 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2340 if (ie)
2341 return report_file_status(ie, ondisk_path, status_cb,
2342 status_arg, repo);
2344 if (lstat(ondisk_path, &sb) == -1) {
2345 if (errno != ENOENT)
2346 return got_error_from_errno2("lstat", ondisk_path);
2347 return NULL;
2350 if (S_ISREG(sb.st_mode))
2351 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2352 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2354 return NULL;
2357 static const struct got_error *
2358 worktree_status(struct got_worktree *worktree, const char *path,
2359 struct got_fileindex *fileindex, struct got_repository *repo,
2360 got_worktree_status_cb status_cb, void *status_arg,
2361 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2363 const struct got_error *err = NULL;
2364 DIR *workdir = NULL;
2365 struct got_fileindex_diff_dir_cb fdiff_cb;
2366 struct diff_dir_cb_arg arg;
2367 char *ondisk_path = NULL;
2369 if (asprintf(&ondisk_path, "%s%s%s",
2370 worktree->root_path, path[0] ? "/" : "", path) == -1)
2371 return got_error_from_errno("asprintf");
2373 workdir = opendir(ondisk_path);
2374 if (workdir == NULL) {
2375 if (errno != ENOTDIR && errno != ENOENT)
2376 err = got_error_from_errno2("opendir", ondisk_path);
2377 else
2378 err = report_single_file_status(path, ondisk_path,
2379 fileindex, status_cb, status_arg, repo);
2380 } else {
2381 fdiff_cb.diff_old_new = status_old_new;
2382 fdiff_cb.diff_old = status_old;
2383 fdiff_cb.diff_new = status_new;
2384 arg.fileindex = fileindex;
2385 arg.worktree = worktree;
2386 arg.status_path = path;
2387 arg.status_path_len = strlen(path);
2388 arg.repo = repo;
2389 arg.status_cb = status_cb;
2390 arg.status_arg = status_arg;
2391 arg.cancel_cb = cancel_cb;
2392 arg.cancel_arg = cancel_arg;
2393 err = got_fileindex_diff_dir(fileindex, workdir,
2394 worktree->root_path, path, repo, &fdiff_cb, &arg);
2397 if (workdir)
2398 closedir(workdir);
2399 free(ondisk_path);
2400 return err;
2403 const struct got_error *
2404 got_worktree_status(struct got_worktree *worktree,
2405 struct got_pathlist_head *paths, struct got_repository *repo,
2406 got_worktree_status_cb status_cb, void *status_arg,
2407 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2409 const struct got_error *err = NULL;
2410 char *fileindex_path = NULL;
2411 struct got_fileindex *fileindex = NULL;
2412 struct got_pathlist_entry *pe;
2414 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2415 if (err)
2416 return err;
2418 TAILQ_FOREACH(pe, paths, entry) {
2419 err = worktree_status(worktree, pe->path, fileindex, repo,
2420 status_cb, status_arg, cancel_cb, cancel_arg);
2421 if (err)
2422 break;
2424 free(fileindex_path);
2425 got_fileindex_free(fileindex);
2426 return err;
2429 const struct got_error *
2430 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2431 const char *arg)
2433 const struct got_error *err = NULL;
2434 char *resolved, *cwd = NULL, *path = NULL;
2435 size_t len;
2437 *wt_path = NULL;
2439 resolved = realpath(arg, NULL);
2440 if (resolved == NULL) {
2441 if (errno != ENOENT)
2442 return got_error_from_errno2("realpath", arg);
2443 cwd = getcwd(NULL, 0);
2444 if (cwd == NULL)
2445 return got_error_from_errno("getcwd");
2446 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2447 err = got_error_from_errno("asprintf");
2448 goto done;
2452 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2453 strlen(got_worktree_get_root_path(worktree)))) {
2454 err = got_error(GOT_ERR_BAD_PATH);
2455 goto done;
2458 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2459 err = got_path_skip_common_ancestor(&path,
2460 got_worktree_get_root_path(worktree), resolved);
2461 if (err)
2462 goto done;
2463 } else {
2464 path = strdup("");
2465 if (path == NULL) {
2466 err = got_error_from_errno("strdup");
2467 goto done;
2471 /* XXX status walk can't deal with trailing slash! */
2472 len = strlen(path);
2473 while (len > 0 && path[len - 1] == '/') {
2474 path[len - 1] = '\0';
2475 len--;
2477 done:
2478 free(resolved);
2479 free(cwd);
2480 if (err == NULL)
2481 *wt_path = path;
2482 else
2483 free(path);
2484 return err;
2487 static const struct got_error *
2488 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2489 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2490 struct got_repository *repo)
2492 const struct got_error *err = NULL;
2493 struct got_fileindex_entry *ie;
2495 /* Re-adding an existing entry is a no-op. */
2496 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2497 return NULL;
2499 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2500 if (err)
2501 return err;
2503 err = got_fileindex_entry_add(fileindex, ie);
2504 if (err) {
2505 got_fileindex_entry_free(ie);
2506 return err;
2509 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2512 const struct got_error *
2513 got_worktree_schedule_add(struct got_worktree *worktree,
2514 struct got_pathlist_head *ondisk_paths,
2515 got_worktree_status_cb status_cb, void *status_arg,
2516 struct got_repository *repo)
2518 struct got_fileindex *fileindex = NULL;
2519 char *fileindex_path = NULL;
2520 const struct got_error *err = NULL, *sync_err, *unlockerr;
2521 struct got_pathlist_entry *pe;
2523 err = lock_worktree(worktree, LOCK_EX);
2524 if (err)
2525 return err;
2527 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2528 if (err)
2529 goto done;
2531 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2532 char *relpath;
2533 err = got_path_skip_common_ancestor(&relpath,
2534 got_worktree_get_root_path(worktree), pe->path);
2535 if (err)
2536 break;
2537 err = schedule_addition(pe->path, fileindex, relpath,
2538 status_cb, status_arg, repo);
2539 free(relpath);
2540 if (err)
2541 break;
2543 sync_err = sync_fileindex(fileindex, fileindex_path);
2544 if (sync_err && err == NULL)
2545 err = sync_err;
2546 done:
2547 free(fileindex_path);
2548 if (fileindex)
2549 got_fileindex_free(fileindex);
2550 unlockerr = lock_worktree(worktree, LOCK_SH);
2551 if (unlockerr && err == NULL)
2552 err = unlockerr;
2553 return err;
2556 static const struct got_error *
2557 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2558 const char *relpath, int delete_local_mods,
2559 got_worktree_status_cb status_cb, void *status_arg,
2560 struct got_repository *repo)
2562 const struct got_error *err = NULL;
2563 struct got_fileindex_entry *ie = NULL;
2564 unsigned char status, staged_status;
2565 struct stat sb;
2567 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2568 if (ie == NULL)
2569 return got_error(GOT_ERR_BAD_PATH);
2571 staged_status = get_staged_status(ie);
2572 if (staged_status != GOT_STATUS_NO_CHANGE) {
2573 if (staged_status == GOT_STATUS_DELETE)
2574 return NULL;
2575 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2578 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2579 if (err)
2580 return err;
2582 if (status != GOT_STATUS_NO_CHANGE) {
2583 if (status == GOT_STATUS_DELETE)
2584 return NULL;
2585 if (status != GOT_STATUS_MODIFY)
2586 return got_error(GOT_ERR_FILE_STATUS);
2587 if (!delete_local_mods)
2588 return got_error(GOT_ERR_FILE_MODIFIED);
2591 if (unlink(ondisk_path) != 0)
2592 return got_error_from_errno2("unlink", ondisk_path);
2594 got_fileindex_entry_mark_deleted_from_disk(ie);
2595 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2598 const struct got_error *
2599 got_worktree_schedule_delete(struct got_worktree *worktree,
2600 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2601 got_worktree_status_cb status_cb, void *status_arg,
2602 struct got_repository *repo)
2604 struct got_fileindex *fileindex = NULL;
2605 char *fileindex_path = NULL;
2606 const struct got_error *err = NULL, *sync_err, *unlockerr;
2607 struct got_pathlist_entry *pe;
2609 err = lock_worktree(worktree, LOCK_EX);
2610 if (err)
2611 return err;
2613 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2614 if (err)
2615 goto done;
2617 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2618 char *relpath;
2619 err = got_path_skip_common_ancestor(&relpath,
2620 got_worktree_get_root_path(worktree), pe->path);
2621 if (err)
2622 break;
2623 err = schedule_for_deletion(pe->path, fileindex, relpath,
2624 delete_local_mods, status_cb, status_arg, repo);
2625 free(relpath);
2626 if (err)
2627 break;
2629 sync_err = sync_fileindex(fileindex, fileindex_path);
2630 if (sync_err && err == NULL)
2631 err = sync_err;
2632 done:
2633 free(fileindex_path);
2634 if (fileindex)
2635 got_fileindex_free(fileindex);
2636 unlockerr = lock_worktree(worktree, LOCK_SH);
2637 if (unlockerr && err == NULL)
2638 err = unlockerr;
2639 return err;
2642 static const struct got_error *
2643 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2644 const char *ondisk_path,
2645 got_worktree_checkout_cb progress_cb, void *progress_arg,
2646 struct got_repository *repo)
2648 const struct got_error *err = NULL;
2649 char *relpath = NULL, *parent_path = NULL;
2650 struct got_fileindex_entry *ie;
2651 struct got_tree_object *tree = NULL;
2652 struct got_object_id *tree_id = NULL;
2653 const struct got_tree_entry *te = NULL;
2654 char *tree_path = NULL, *te_name;
2655 struct got_blob_object *blob = NULL;
2656 unsigned char status, staged_status;
2657 struct stat sb;
2659 err = got_path_skip_common_ancestor(&relpath,
2660 got_worktree_get_root_path(worktree), ondisk_path);
2661 if (err)
2662 goto done;
2664 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2665 if (ie == NULL) {
2666 err = got_error(GOT_ERR_BAD_PATH);
2667 goto done;
2670 /* Construct in-repository path of tree which contains this blob. */
2671 err = got_path_dirname(&parent_path, ie->path);
2672 if (err) {
2673 if (err->code != GOT_ERR_BAD_PATH)
2674 goto done;
2675 parent_path = strdup("/");
2676 if (parent_path == NULL) {
2677 err = got_error_from_errno("strdup");
2678 goto done;
2681 if (got_path_is_root_dir(worktree->path_prefix)) {
2682 tree_path = strdup(parent_path);
2683 if (tree_path == NULL) {
2684 err = got_error_from_errno("strdup");
2685 goto done;
2687 } else {
2688 if (got_path_is_root_dir(parent_path)) {
2689 tree_path = strdup(worktree->path_prefix);
2690 if (tree_path == NULL) {
2691 err = got_error_from_errno("strdup");
2692 goto done;
2694 } else {
2695 if (asprintf(&tree_path, "%s/%s",
2696 worktree->path_prefix, parent_path) == -1) {
2697 err = got_error_from_errno("asprintf");
2698 goto done;
2703 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2704 if (err)
2705 goto done;
2706 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2707 sb.st_mode = got_fileindex_perms_to_st(ie);
2709 staged_status = get_staged_status(ie);
2710 if (status == GOT_STATUS_DELETE &&
2711 staged_status != GOT_STATUS_NO_CHANGE) {
2712 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2713 goto done;
2716 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2717 tree_path);
2718 if (err) {
2719 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2720 (status == GOT_STATUS_ADD ||
2721 staged_status == GOT_STATUS_ADD)))
2722 goto done;
2723 } else {
2724 err = got_object_open_as_tree(&tree, repo, tree_id);
2725 if (err)
2726 goto done;
2728 te_name = basename(ie->path);
2729 if (te_name == NULL) {
2730 err = got_error_from_errno2("basename", ie->path);
2731 goto done;
2734 te = got_object_tree_find_entry(tree, te_name);
2735 if (te == NULL && status != GOT_STATUS_ADD &&
2736 staged_status != GOT_STATUS_ADD) {
2737 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2738 goto done;
2742 switch (status) {
2743 case GOT_STATUS_ADD:
2744 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2745 if (err)
2746 goto done;
2747 got_fileindex_entry_remove(fileindex, ie);
2748 break;
2749 case GOT_STATUS_DELETE:
2750 case GOT_STATUS_MODIFY:
2751 case GOT_STATUS_CONFLICT:
2752 case GOT_STATUS_MISSING: {
2753 struct got_object_id id;
2754 if (staged_status == GOT_STATUS_ADD ||
2755 staged_status == GOT_STATUS_MODIFY) {
2756 memcpy(id.sha1, ie->staged_blob_sha1,
2757 SHA1_DIGEST_LENGTH);
2758 } else
2759 memcpy(id.sha1, ie->blob_sha1,
2760 SHA1_DIGEST_LENGTH);
2761 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2762 if (err)
2763 goto done;
2764 err = install_blob(worktree, ondisk_path, ie->path,
2765 te ? te->mode : GOT_DEFAULT_FILE_MODE, sb.st_mode,
2766 blob, 0, 1, repo, progress_cb, progress_arg);
2767 if (err)
2768 goto done;
2769 if (status == GOT_STATUS_DELETE) {
2770 err = update_blob_fileindex_entry(worktree,
2771 fileindex, ie, ondisk_path, ie->path, blob, 1);
2772 if (err)
2773 goto done;
2775 break;
2777 default:
2778 goto done;
2780 done:
2781 free(relpath);
2782 free(parent_path);
2783 free(tree_path);
2784 if (blob)
2785 got_object_blob_close(blob);
2786 if (tree)
2787 got_object_tree_close(tree);
2788 free(tree_id);
2789 return err;
2792 const struct got_error *
2793 got_worktree_revert(struct got_worktree *worktree,
2794 struct got_pathlist_head *ondisk_paths,
2795 got_worktree_checkout_cb progress_cb, void *progress_arg,
2796 struct got_repository *repo)
2798 struct got_fileindex *fileindex = NULL;
2799 char *fileindex_path = NULL;
2800 const struct got_error *err = NULL, *unlockerr = NULL;
2801 const struct got_error *sync_err = NULL;
2802 struct got_pathlist_entry *pe;
2804 err = lock_worktree(worktree, LOCK_EX);
2805 if (err)
2806 return err;
2808 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2809 if (err)
2810 goto done;
2812 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2813 err = revert_file(worktree, fileindex, pe->path,
2814 progress_cb, progress_arg, repo);
2815 if (err)
2816 break;
2818 sync_err = sync_fileindex(fileindex, fileindex_path);
2819 if (sync_err && err == NULL)
2820 err = sync_err;
2821 done:
2822 free(fileindex_path);
2823 if (fileindex)
2824 got_fileindex_free(fileindex);
2825 unlockerr = lock_worktree(worktree, LOCK_SH);
2826 if (unlockerr && err == NULL)
2827 err = unlockerr;
2828 return err;
2831 static void
2832 free_commitable(struct got_commitable *ct)
2834 free(ct->path);
2835 free(ct->in_repo_path);
2836 free(ct->ondisk_path);
2837 free(ct->blob_id);
2838 free(ct->base_blob_id);
2839 free(ct->base_commit_id);
2840 free(ct);
2843 struct collect_commitables_arg {
2844 struct got_pathlist_head *commitable_paths;
2845 struct got_repository *repo;
2846 struct got_worktree *worktree;
2849 static const struct got_error *
2850 collect_commitables(void *arg, unsigned char status,
2851 unsigned char staged_status, const char *relpath,
2852 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2853 struct got_object_id *commit_id)
2855 struct collect_commitables_arg *a = arg;
2856 const struct got_error *err = NULL;
2857 struct got_commitable *ct = NULL;
2858 struct got_pathlist_entry *new = NULL;
2859 char *parent_path = NULL, *path = NULL;
2860 struct stat sb;
2862 if (status == GOT_STATUS_CONFLICT)
2863 return got_error(GOT_ERR_COMMIT_CONFLICT);
2865 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2866 status != GOT_STATUS_DELETE)
2867 return NULL;
2869 if (asprintf(&path, "/%s", relpath) == -1) {
2870 err = got_error_from_errno("asprintf");
2871 goto done;
2873 if (strcmp(path, "/") == 0) {
2874 parent_path = strdup("");
2875 if (parent_path == NULL)
2876 return got_error_from_errno("strdup");
2877 } else {
2878 err = got_path_dirname(&parent_path, path);
2879 if (err)
2880 return err;
2883 ct = calloc(1, sizeof(*ct));
2884 if (ct == NULL) {
2885 err = got_error_from_errno("calloc");
2886 goto done;
2889 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2890 relpath) == -1) {
2891 err = got_error_from_errno("asprintf");
2892 goto done;
2894 if (status == GOT_STATUS_DELETE) {
2895 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2896 } else {
2897 if (lstat(ct->ondisk_path, &sb) != 0) {
2898 err = got_error_from_errno2("lstat", ct->ondisk_path);
2899 goto done;
2901 ct->mode = sb.st_mode;
2904 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2905 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2906 relpath) == -1) {
2907 err = got_error_from_errno("asprintf");
2908 goto done;
2911 ct->status = status;
2912 ct->blob_id = NULL; /* will be filled in when blob gets created */
2913 if (ct->status != GOT_STATUS_ADD) {
2914 ct->base_blob_id = got_object_id_dup(blob_id);
2915 if (ct->base_blob_id == NULL) {
2916 err = got_error_from_errno("got_object_id_dup");
2917 goto done;
2919 ct->base_commit_id = got_object_id_dup(commit_id);
2920 if (ct->base_commit_id == NULL) {
2921 err = got_error_from_errno("got_object_id_dup");
2922 goto done;
2925 ct->path = strdup(path);
2926 if (ct->path == NULL) {
2927 err = got_error_from_errno("strdup");
2928 goto done;
2930 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2931 done:
2932 if (ct && (err || new == NULL))
2933 free_commitable(ct);
2934 free(parent_path);
2935 free(path);
2936 return err;
2939 static const struct got_error *write_tree(struct got_object_id **,
2940 struct got_tree_object *, const char *, struct got_pathlist_head *,
2941 got_worktree_status_cb status_cb, void *status_arg,
2942 struct got_repository *);
2944 static const struct got_error *
2945 write_subtree(struct got_object_id **new_subtree_id,
2946 struct got_tree_entry *te, const char *parent_path,
2947 struct got_pathlist_head *commitable_paths,
2948 got_worktree_status_cb status_cb, void *status_arg,
2949 struct got_repository *repo)
2951 const struct got_error *err = NULL;
2952 struct got_tree_object *subtree;
2953 char *subpath;
2955 if (asprintf(&subpath, "%s%s%s", parent_path,
2956 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2957 return got_error_from_errno("asprintf");
2959 err = got_object_open_as_tree(&subtree, repo, te->id);
2960 if (err)
2961 return err;
2963 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2964 status_cb, status_arg, repo);
2965 got_object_tree_close(subtree);
2966 free(subpath);
2967 return err;
2970 static const struct got_error *
2971 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2973 const struct got_error *err = NULL;
2974 char *ct_parent_path = NULL;
2976 *match = 0;
2978 if (strchr(ct->in_repo_path, '/') == NULL) {
2979 *match = got_path_is_root_dir(path);
2980 return NULL;
2983 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2984 if (err)
2985 return err;
2986 *match = (strcmp(path, ct_parent_path) == 0);
2987 free(ct_parent_path);
2988 return err;
2991 static mode_t
2992 get_ct_file_mode(struct got_commitable *ct)
2994 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2997 static const struct got_error *
2998 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2999 struct got_tree_entry *te, struct got_commitable *ct)
3001 const struct got_error *err = NULL;
3003 *new_te = NULL;
3005 err = got_object_tree_entry_dup(new_te, te);
3006 if (err)
3007 goto done;
3009 (*new_te)->mode = get_ct_file_mode(ct);
3011 free((*new_te)->id);
3012 (*new_te)->id = got_object_id_dup(ct->blob_id);
3013 if ((*new_te)->id == NULL) {
3014 err = got_error_from_errno("got_object_id_dup");
3015 goto done;
3017 done:
3018 if (err && *new_te) {
3019 got_object_tree_entry_close(*new_te);
3020 *new_te = NULL;
3022 return err;
3025 static const struct got_error *
3026 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3027 struct got_commitable *ct)
3029 const struct got_error *err = NULL;
3030 char *ct_name;
3032 *new_te = NULL;
3034 *new_te = calloc(1, sizeof(**new_te));
3035 if (*new_te == NULL)
3036 return got_error_from_errno("calloc");
3038 ct_name = basename(ct->path);
3039 if (ct_name == NULL) {
3040 err = got_error_from_errno2("basename", ct->path);
3041 goto done;
3043 (*new_te)->name = strdup(ct_name);
3044 if ((*new_te)->name == NULL) {
3045 err = got_error_from_errno("strdup");
3046 goto done;
3049 (*new_te)->mode = get_ct_file_mode(ct);
3051 (*new_te)->id = got_object_id_dup(ct->blob_id);
3052 if ((*new_te)->id == NULL) {
3053 err = got_error_from_errno("got_object_id_dup");
3054 goto done;
3056 done:
3057 if (err && *new_te) {
3058 got_object_tree_entry_close(*new_te);
3059 *new_te = NULL;
3061 return err;
3064 static const struct got_error *
3065 insert_tree_entry(struct got_tree_entry *new_te,
3066 struct got_pathlist_head *paths)
3068 const struct got_error *err = NULL;
3069 struct got_pathlist_entry *new_pe;
3071 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3072 if (err)
3073 return err;
3074 if (new_pe == NULL)
3075 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3076 return NULL;
3079 static const struct got_error *
3080 report_ct_status(struct got_commitable *ct,
3081 got_worktree_status_cb status_cb, void *status_arg)
3083 const char *ct_path = ct->path;
3084 while (ct_path[0] == '/')
3085 ct_path++;
3086 return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3087 ct_path, ct->blob_id, NULL, NULL);
3090 static const struct got_error *
3091 match_modified_subtree(int *modified, struct got_tree_entry *te,
3092 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3094 const struct got_error *err = NULL;
3095 struct got_pathlist_entry *pe;
3096 char *te_path;
3098 *modified = 0;
3100 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3101 got_path_is_root_dir(base_tree_path) ? "" : "/",
3102 te->name) == -1)
3103 return got_error_from_errno("asprintf");
3105 TAILQ_FOREACH(pe, commitable_paths, entry) {
3106 struct got_commitable *ct = pe->data;
3107 *modified = got_path_is_child(ct->in_repo_path, te_path,
3108 strlen(te_path));
3109 if (*modified)
3110 break;
3113 free(te_path);
3114 return err;
3117 static const struct got_error *
3118 match_deleted_or_modified_ct(struct got_commitable **ctp,
3119 struct got_tree_entry *te, const char *base_tree_path,
3120 struct got_pathlist_head *commitable_paths)
3122 const struct got_error *err = NULL;
3123 struct got_pathlist_entry *pe;
3125 *ctp = NULL;
3127 TAILQ_FOREACH(pe, commitable_paths, entry) {
3128 struct got_commitable *ct = pe->data;
3129 char *ct_name = NULL;
3130 int path_matches;
3132 if (ct->status != GOT_STATUS_MODIFY &&
3133 ct->status != GOT_STATUS_DELETE)
3134 continue;
3136 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3137 continue;
3139 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3140 if (err)
3141 return err;
3142 if (!path_matches)
3143 continue;
3145 ct_name = basename(pe->path);
3146 if (ct_name == NULL)
3147 return got_error_from_errno2("basename", pe->path);
3149 if (strcmp(te->name, ct_name) != 0)
3150 continue;
3152 *ctp = ct;
3153 break;
3156 return err;
3159 static const struct got_error *
3160 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3161 const char *child_path, const char *path_base_tree,
3162 struct got_pathlist_head *commitable_paths,
3163 got_worktree_status_cb status_cb, void *status_arg,
3164 struct got_repository *repo)
3166 const struct got_error *err = NULL;
3167 struct got_tree_entry *new_te;
3168 char *subtree_path;
3170 *new_tep = NULL;
3172 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3173 got_path_is_root_dir(path_base_tree) ? "" : "/",
3174 child_path) == -1)
3175 return got_error_from_errno("asprintf");
3177 new_te = calloc(1, sizeof(*new_te));
3178 new_te->mode = S_IFDIR;
3179 new_te->name = strdup(child_path);
3180 if (new_te->name == NULL) {
3181 err = got_error_from_errno("strdup");
3182 got_object_tree_entry_close(new_te);
3183 goto done;
3185 err = write_tree(&new_te->id, NULL, subtree_path,
3186 commitable_paths, status_cb, status_arg, repo);
3187 if (err) {
3188 got_object_tree_entry_close(new_te);
3189 goto done;
3191 done:
3192 free(subtree_path);
3193 if (err == NULL)
3194 *new_tep = new_te;
3195 return err;
3198 static const struct got_error *
3199 write_tree(struct got_object_id **new_tree_id,
3200 struct got_tree_object *base_tree, const char *path_base_tree,
3201 struct got_pathlist_head *commitable_paths,
3202 got_worktree_status_cb status_cb, void *status_arg,
3203 struct got_repository *repo)
3205 const struct got_error *err = NULL;
3206 const struct got_tree_entries *base_entries = NULL;
3207 struct got_pathlist_head paths;
3208 struct got_tree_entries new_tree_entries;
3209 struct got_tree_entry *te, *new_te = NULL;
3210 struct got_pathlist_entry *pe;
3212 TAILQ_INIT(&paths);
3213 new_tree_entries.nentries = 0;
3214 SIMPLEQ_INIT(&new_tree_entries.head);
3216 /* Insert, and recurse into, newly added entries first. */
3217 TAILQ_FOREACH(pe, commitable_paths, entry) {
3218 struct got_commitable *ct = pe->data;
3219 char *child_path = NULL, *slash;
3221 if (ct->status != GOT_STATUS_ADD ||
3222 (ct->flags & GOT_COMMITABLE_ADDED))
3223 continue;
3225 if (!got_path_is_child(pe->path, path_base_tree,
3226 strlen(path_base_tree)))
3227 continue;
3229 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3230 pe->path);
3231 if (err)
3232 goto done;
3234 slash = strchr(child_path, '/');
3235 if (slash == NULL) {
3236 err = alloc_added_blob_tree_entry(&new_te, ct);
3237 if (err)
3238 goto done;
3239 err = report_ct_status(ct, status_cb, status_arg);
3240 if (err)
3241 goto done;
3242 ct->flags |= GOT_COMMITABLE_ADDED;
3243 err = insert_tree_entry(new_te, &paths);
3244 if (err)
3245 goto done;
3246 } else {
3247 *slash = '\0'; /* trim trailing path components */
3248 if (base_tree == NULL ||
3249 got_object_tree_find_entry(base_tree, child_path)
3250 == NULL) {
3251 err = make_subtree_for_added_blob(&new_te,
3252 child_path, path_base_tree,
3253 commitable_paths, status_cb, status_arg,
3254 repo);
3255 if (err)
3256 goto done;
3257 err = insert_tree_entry(new_te, &paths);
3258 if (err)
3259 goto done;
3264 if (base_tree) {
3265 /* Handle modified and deleted entries. */
3266 base_entries = got_object_tree_get_entries(base_tree);
3267 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3268 struct got_commitable *ct = NULL;
3270 if (S_ISDIR(te->mode)) {
3271 int modified;
3272 err = got_object_tree_entry_dup(&new_te, te);
3273 if (err)
3274 goto done;
3275 err = match_modified_subtree(&modified, te,
3276 path_base_tree, commitable_paths);
3277 if (err)
3278 goto done;
3279 /* Avoid recursion into unmodified subtrees. */
3280 if (modified) {
3281 free(new_te->id);
3282 err = write_subtree(&new_te->id, te,
3283 path_base_tree, commitable_paths,
3284 status_cb, status_arg, repo);
3285 if (err)
3286 goto done;
3288 err = insert_tree_entry(new_te, &paths);
3289 if (err)
3290 goto done;
3291 continue;
3294 err = match_deleted_or_modified_ct(&ct, te,
3295 path_base_tree, commitable_paths);
3296 if (ct) {
3297 /* NB: Deleted entries get dropped here. */
3298 if (ct->status == GOT_STATUS_MODIFY) {
3299 err = alloc_modified_blob_tree_entry(
3300 &new_te, te, ct);
3301 if (err)
3302 goto done;
3303 err = insert_tree_entry(new_te, &paths);
3304 if (err)
3305 goto done;
3307 err = report_ct_status(ct, status_cb,
3308 status_arg);
3309 if (err)
3310 goto done;
3311 } else {
3312 /* Entry is unchanged; just copy it. */
3313 err = got_object_tree_entry_dup(&new_te, te);
3314 if (err)
3315 goto done;
3316 err = insert_tree_entry(new_te, &paths);
3317 if (err)
3318 goto done;
3323 /* Write new list of entries; deleted entries have been dropped. */
3324 TAILQ_FOREACH(pe, &paths, entry) {
3325 struct got_tree_entry *te = pe->data;
3326 new_tree_entries.nentries++;
3327 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3329 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3330 done:
3331 got_object_tree_entries_close(&new_tree_entries);
3332 got_pathlist_free(&paths);
3333 return err;
3336 static const struct got_error *
3337 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3338 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3340 const struct got_error *err = NULL;
3341 struct got_pathlist_entry *pe;
3343 TAILQ_FOREACH(pe, commitable_paths, entry) {
3344 struct got_fileindex_entry *ie;
3345 struct got_commitable *ct = pe->data;
3347 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3348 if (ie) {
3349 if (ct->status == GOT_STATUS_DELETE) {
3350 got_fileindex_entry_remove(fileindex, ie);
3351 got_fileindex_entry_free(ie);
3352 } else
3353 err = got_fileindex_entry_update(ie,
3354 ct->ondisk_path, ct->blob_id->sha1,
3355 new_base_commit_id->sha1, 1);
3356 } else {
3357 err = got_fileindex_entry_alloc(&ie,
3358 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3359 new_base_commit_id->sha1);
3360 if (err)
3361 break;
3362 err = got_fileindex_entry_add(fileindex, ie);
3363 if (err)
3364 break;
3367 return err;
3370 static const struct got_error *
3371 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3372 struct got_object_id *head_commit_id)
3374 const struct got_error *err = NULL;
3375 struct got_object_id *id = NULL;
3376 struct got_commit_object *commit = NULL;
3377 const char *ct_path = ct->in_repo_path;
3379 while (ct_path[0] == '/')
3380 ct_path++;
3382 if (ct->status != GOT_STATUS_ADD) {
3383 /* Trivial case: base commit == head commit */
3384 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3385 return NULL;
3387 * Ensure file content which local changes were based
3388 * on matches file content in the branch head.
3390 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3391 if (err) {
3392 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3393 goto done;
3394 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3395 goto done;
3396 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3397 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3398 } else {
3399 /* Require that added files don't exist in the branch head. */
3400 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3401 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3402 goto done;
3403 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3405 done:
3406 if (commit)
3407 got_object_commit_close(commit);
3408 free(id);
3409 return err;
3412 const struct got_error *
3413 commit_worktree(struct got_object_id **new_commit_id,
3414 struct got_pathlist_head *commitable_paths,
3415 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3416 const char *author, const char *committer,
3417 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3418 got_worktree_status_cb status_cb, void *status_arg,
3419 struct got_repository *repo)
3421 const struct got_error *err = NULL, *unlockerr = NULL;
3422 struct got_pathlist_entry *pe;
3423 const char *head_ref_name = NULL;
3424 struct got_commit_object *head_commit = NULL;
3425 struct got_reference *head_ref2 = NULL;
3426 struct got_object_id *head_commit_id2 = NULL;
3427 struct got_tree_object *head_tree = NULL;
3428 struct got_object_id *new_tree_id = NULL;
3429 struct got_object_id_queue parent_ids;
3430 struct got_object_qid *pid = NULL;
3431 char *logmsg = NULL;
3433 *new_commit_id = NULL;
3435 SIMPLEQ_INIT(&parent_ids);
3437 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3438 if (err)
3439 goto done;
3441 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3442 if (err)
3443 goto done;
3445 if (commit_msg_cb != NULL) {
3446 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3447 if (err)
3448 goto done;
3451 if (logmsg == NULL || strlen(logmsg) == 0) {
3452 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3453 goto done;
3456 /* Create blobs from added and modified files and record their IDs. */
3457 TAILQ_FOREACH(pe, commitable_paths, entry) {
3458 struct got_commitable *ct = pe->data;
3459 char *ondisk_path;
3461 if (ct->status != GOT_STATUS_ADD &&
3462 ct->status != GOT_STATUS_MODIFY)
3463 continue;
3465 if (asprintf(&ondisk_path, "%s/%s",
3466 worktree->root_path, pe->path) == -1) {
3467 err = got_error_from_errno("asprintf");
3468 goto done;
3470 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3471 free(ondisk_path);
3472 if (err)
3473 goto done;
3476 /* Recursively write new tree objects. */
3477 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3478 status_cb, status_arg, repo);
3479 if (err)
3480 goto done;
3482 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3483 if (err)
3484 goto done;
3485 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3486 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3487 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3488 got_object_qid_free(pid);
3489 if (logmsg != NULL)
3490 free(logmsg);
3491 if (err)
3492 goto done;
3494 /* Check if a concurrent commit to our branch has occurred. */
3495 head_ref_name = got_worktree_get_head_ref_name(worktree);
3496 if (head_ref_name == NULL) {
3497 err = got_error_from_errno("got_worktree_get_head_ref_name");
3498 goto done;
3500 /* Lock the reference here to prevent concurrent modification. */
3501 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3502 if (err)
3503 goto done;
3504 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3505 if (err)
3506 goto done;
3507 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3508 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3509 goto done;
3511 /* Update branch head in repository. */
3512 err = got_ref_change_ref(head_ref2, *new_commit_id);
3513 if (err)
3514 goto done;
3515 err = got_ref_write(head_ref2, repo);
3516 if (err)
3517 goto done;
3519 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3520 if (err)
3521 goto done;
3523 err = ref_base_commit(worktree, repo);
3524 if (err)
3525 goto done;
3526 done:
3527 if (head_tree)
3528 got_object_tree_close(head_tree);
3529 if (head_commit)
3530 got_object_commit_close(head_commit);
3531 free(head_commit_id2);
3532 if (head_ref2) {
3533 unlockerr = got_ref_unlock(head_ref2);
3534 if (unlockerr && err == NULL)
3535 err = unlockerr;
3536 got_ref_close(head_ref2);
3538 return err;
3541 static const struct got_error *
3542 check_path_is_commitable(const char *path,
3543 struct got_pathlist_head *commitable_paths)
3545 struct got_pathlist_entry *cpe = NULL;
3546 size_t path_len = strlen(path);
3548 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3549 struct got_commitable *ct = cpe->data;
3550 const char *ct_path = ct->path;
3552 while (ct_path[0] == '/')
3553 ct_path++;
3555 if (strcmp(path, ct_path) == 0 ||
3556 got_path_is_child(ct_path, path, path_len))
3557 break;
3560 if (cpe == NULL)
3561 return got_error_path(path, GOT_ERR_BAD_PATH);
3563 return NULL;
3566 const struct got_error *
3567 got_worktree_commit(struct got_object_id **new_commit_id,
3568 struct got_worktree *worktree, struct got_pathlist_head *paths,
3569 const char *author, const char *committer,
3570 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3571 got_worktree_status_cb status_cb, void *status_arg,
3572 struct got_repository *repo)
3574 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3575 struct got_fileindex *fileindex = NULL;
3576 char *fileindex_path = NULL;
3577 struct got_pathlist_head commitable_paths;
3578 struct collect_commitables_arg cc_arg;
3579 struct got_pathlist_entry *pe;
3580 struct got_reference *head_ref = NULL;
3581 struct got_object_id *head_commit_id = NULL;
3583 *new_commit_id = NULL;
3585 TAILQ_INIT(&commitable_paths);
3587 err = lock_worktree(worktree, LOCK_EX);
3588 if (err)
3589 goto done;
3591 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3592 if (err)
3593 goto done;
3595 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3596 if (err)
3597 goto done;
3599 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3600 if (err)
3601 goto done;
3603 cc_arg.commitable_paths = &commitable_paths;
3604 cc_arg.worktree = worktree;
3605 cc_arg.repo = repo;
3606 TAILQ_FOREACH(pe, paths, entry) {
3607 err = worktree_status(worktree, pe->path, fileindex, repo,
3608 collect_commitables, &cc_arg, NULL, NULL);
3609 if (err)
3610 goto done;
3613 if (TAILQ_EMPTY(&commitable_paths)) {
3614 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3615 goto done;
3618 TAILQ_FOREACH(pe, paths, entry) {
3619 err = check_path_is_commitable(pe->path, &commitable_paths);
3620 if (err)
3621 goto done;
3624 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3625 struct got_commitable *ct = pe->data;
3626 err = check_ct_out_of_date(ct, repo, head_commit_id);
3627 if (err)
3628 goto done;
3631 err = commit_worktree(new_commit_id, &commitable_paths,
3632 head_commit_id, worktree, author, committer,
3633 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3634 if (err)
3635 goto done;
3637 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3638 fileindex);
3639 sync_err = sync_fileindex(fileindex, fileindex_path);
3640 if (sync_err && err == NULL)
3641 err = sync_err;
3642 done:
3643 if (fileindex)
3644 got_fileindex_free(fileindex);
3645 free(fileindex_path);
3646 unlockerr = lock_worktree(worktree, LOCK_SH);
3647 if (unlockerr && err == NULL)
3648 err = unlockerr;
3649 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3650 struct got_commitable *ct = pe->data;
3651 free_commitable(ct);
3653 got_pathlist_free(&commitable_paths);
3654 return err;
3657 const char *
3658 got_commitable_get_path(struct got_commitable *ct)
3660 return ct->path;
3663 unsigned int
3664 got_commitable_get_status(struct got_commitable *ct)
3666 return ct->status;
3669 struct check_rebase_ok_arg {
3670 struct got_worktree *worktree;
3671 struct got_repository *repo;
3674 static const struct got_error *
3675 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3677 const struct got_error *err = NULL;
3678 struct check_rebase_ok_arg *a = arg;
3679 unsigned char status;
3680 struct stat sb;
3681 char *ondisk_path;
3683 /* Reject rebase of a work tree with mixed base commits. */
3684 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3685 SHA1_DIGEST_LENGTH))
3686 return got_error(GOT_ERR_MIXED_COMMITS);
3688 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3689 == -1)
3690 return got_error_from_errno("asprintf");
3692 /* Reject rebase of a work tree with modified or conflicted files. */
3693 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3694 free(ondisk_path);
3695 if (err)
3696 return err;
3698 if (status != GOT_STATUS_NO_CHANGE)
3699 return got_error(GOT_ERR_MODIFIED);
3701 return NULL;
3704 const struct got_error *
3705 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3706 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3707 struct got_worktree *worktree, struct got_reference *branch,
3708 struct got_repository *repo)
3710 const struct got_error *err = NULL;
3711 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3712 char *branch_ref_name = NULL;
3713 char *fileindex_path = NULL;
3714 struct check_rebase_ok_arg ok_arg;
3715 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3717 *new_base_branch_ref = NULL;
3718 *tmp_branch = NULL;
3719 *fileindex = NULL;
3721 err = lock_worktree(worktree, LOCK_EX);
3722 if (err)
3723 return err;
3725 err = open_fileindex(fileindex, &fileindex_path, worktree);
3726 if (err)
3727 goto done;
3729 ok_arg.worktree = worktree;
3730 ok_arg.repo = repo;
3731 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3732 &ok_arg);
3733 if (err)
3734 goto done;
3736 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3737 if (err)
3738 goto done;
3740 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3741 if (err)
3742 goto done;
3744 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3745 if (err)
3746 goto done;
3748 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3749 0);
3750 if (err)
3751 goto done;
3753 err = got_ref_alloc_symref(new_base_branch_ref,
3754 new_base_branch_ref_name, wt_branch);
3755 if (err)
3756 goto done;
3757 err = got_ref_write(*new_base_branch_ref, repo);
3758 if (err)
3759 goto done;
3761 /* TODO Lock original branch's ref while rebasing? */
3763 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3764 if (err)
3765 goto done;
3767 err = got_ref_write(branch_ref, repo);
3768 if (err)
3769 goto done;
3771 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3772 worktree->base_commit_id);
3773 if (err)
3774 goto done;
3775 err = got_ref_write(*tmp_branch, repo);
3776 if (err)
3777 goto done;
3779 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3780 if (err)
3781 goto done;
3782 done:
3783 free(fileindex_path);
3784 free(tmp_branch_name);
3785 free(new_base_branch_ref_name);
3786 free(branch_ref_name);
3787 if (branch_ref)
3788 got_ref_close(branch_ref);
3789 if (wt_branch)
3790 got_ref_close(wt_branch);
3791 if (err) {
3792 if (*new_base_branch_ref) {
3793 got_ref_close(*new_base_branch_ref);
3794 *new_base_branch_ref = NULL;
3796 if (*tmp_branch) {
3797 got_ref_close(*tmp_branch);
3798 *tmp_branch = NULL;
3800 if (*fileindex) {
3801 got_fileindex_free(*fileindex);
3802 *fileindex = NULL;
3804 lock_worktree(worktree, LOCK_SH);
3806 return err;
3809 const struct got_error *
3810 got_worktree_rebase_continue(struct got_object_id **commit_id,
3811 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3812 struct got_reference **branch, struct got_fileindex **fileindex,
3813 struct got_worktree *worktree, struct got_repository *repo)
3815 const struct got_error *err;
3816 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3817 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3818 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3819 char *fileindex_path = NULL;
3821 *commit_id = NULL;
3822 *new_base_branch = NULL;
3823 *tmp_branch = NULL;
3824 *branch = NULL;
3825 *fileindex = NULL;
3827 err = lock_worktree(worktree, LOCK_EX);
3828 if (err)
3829 return err;
3831 err = open_fileindex(fileindex, &fileindex_path, worktree);
3832 if (err)
3833 goto done;
3835 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3836 if (err)
3837 return err;
3839 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3840 if (err)
3841 goto done;
3843 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3844 if (err)
3845 goto done;
3847 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3848 if (err)
3849 goto done;
3851 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3852 if (err)
3853 goto done;
3855 err = got_ref_open(branch, repo,
3856 got_ref_get_symref_target(branch_ref), 0);
3857 if (err)
3858 goto done;
3860 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3861 if (err)
3862 goto done;
3864 err = got_ref_resolve(commit_id, repo, commit_ref);
3865 if (err)
3866 goto done;
3868 err = got_ref_open(new_base_branch, repo,
3869 new_base_branch_ref_name, 0);
3870 if (err)
3871 goto done;
3873 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3874 if (err)
3875 goto done;
3876 done:
3877 free(commit_ref_name);
3878 free(branch_ref_name);
3879 free(fileindex_path);
3880 if (commit_ref)
3881 got_ref_close(commit_ref);
3882 if (branch_ref)
3883 got_ref_close(branch_ref);
3884 if (err) {
3885 free(*commit_id);
3886 *commit_id = NULL;
3887 if (*tmp_branch) {
3888 got_ref_close(*tmp_branch);
3889 *tmp_branch = NULL;
3891 if (*new_base_branch) {
3892 got_ref_close(*new_base_branch);
3893 *new_base_branch = NULL;
3895 if (*branch) {
3896 got_ref_close(*branch);
3897 *branch = NULL;
3899 if (*fileindex) {
3900 got_fileindex_free(*fileindex);
3901 *fileindex = NULL;
3903 lock_worktree(worktree, LOCK_SH);
3905 return err;
3908 const struct got_error *
3909 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3911 const struct got_error *err;
3912 char *tmp_branch_name = NULL;
3914 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3915 if (err)
3916 return err;
3918 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3919 free(tmp_branch_name);
3920 return NULL;
3923 static const struct got_error *
3924 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3925 char **logmsg, void *arg)
3927 *logmsg = arg;
3928 return NULL;
3931 static const struct got_error *
3932 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3933 const char *path, struct got_object_id *blob_id,
3934 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3936 return NULL;
3939 struct collect_merged_paths_arg {
3940 got_worktree_checkout_cb progress_cb;
3941 void *progress_arg;
3942 struct got_pathlist_head *merged_paths;
3945 static const struct got_error *
3946 collect_merged_paths(void *arg, unsigned char status, const char *path)
3948 const struct got_error *err;
3949 struct collect_merged_paths_arg *a = arg;
3950 char *p;
3951 struct got_pathlist_entry *new;
3953 err = (*a->progress_cb)(a->progress_arg, status, path);
3954 if (err)
3955 return err;
3957 if (status != GOT_STATUS_MERGE &&
3958 status != GOT_STATUS_ADD &&
3959 status != GOT_STATUS_DELETE &&
3960 status != GOT_STATUS_CONFLICT)
3961 return NULL;
3963 p = strdup(path);
3964 if (p == NULL)
3965 return got_error_from_errno("strdup");
3967 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3968 if (err || new == NULL)
3969 free(p);
3970 return err;
3973 void
3974 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3976 struct got_pathlist_entry *pe;
3978 TAILQ_FOREACH(pe, merged_paths, entry)
3979 free((char *)pe->path);
3981 got_pathlist_free(merged_paths);
3984 static const struct got_error *
3985 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3986 struct got_repository *repo)
3988 const struct got_error *err;
3989 struct got_reference *commit_ref = NULL;
3991 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3992 if (err) {
3993 if (err->code != GOT_ERR_NOT_REF)
3994 goto done;
3995 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3996 if (err)
3997 goto done;
3998 err = got_ref_write(commit_ref, repo);
3999 if (err)
4000 goto done;
4001 } else {
4002 struct got_object_id *stored_id;
4003 int cmp;
4005 err = got_ref_resolve(&stored_id, repo, commit_ref);
4006 if (err)
4007 goto done;
4008 cmp = got_object_id_cmp(commit_id, stored_id);
4009 free(stored_id);
4010 if (cmp != 0) {
4011 err = got_error(GOT_ERR_REBASE_COMMITID);
4012 goto done;
4015 done:
4016 if (commit_ref)
4017 got_ref_close(commit_ref);
4018 return err;
4021 static const struct got_error *
4022 rebase_merge_files(struct got_pathlist_head *merged_paths,
4023 const char *commit_ref_name, struct got_worktree *worktree,
4024 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4025 struct got_object_id *commit_id, struct got_repository *repo,
4026 got_worktree_checkout_cb progress_cb, void *progress_arg,
4027 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4029 const struct got_error *err;
4030 struct got_reference *commit_ref = NULL;
4031 struct collect_merged_paths_arg cmp_arg;
4032 char *fileindex_path;
4034 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4036 err = get_fileindex_path(&fileindex_path, worktree);
4037 if (err)
4038 return err;
4040 cmp_arg.progress_cb = progress_cb;
4041 cmp_arg.progress_arg = progress_arg;
4042 cmp_arg.merged_paths = merged_paths;
4043 err = merge_files(worktree, fileindex, fileindex_path,
4044 parent_commit_id, commit_id, repo, collect_merged_paths,
4045 &cmp_arg, cancel_cb, cancel_arg);
4046 if (commit_ref)
4047 got_ref_close(commit_ref);
4048 return err;
4051 const struct got_error *
4052 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4053 struct got_worktree *worktree, struct got_fileindex *fileindex,
4054 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4055 struct got_repository *repo,
4056 got_worktree_checkout_cb progress_cb, void *progress_arg,
4057 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4059 const struct got_error *err;
4060 char *commit_ref_name;
4062 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4063 if (err)
4064 return err;
4066 err = store_commit_id(commit_ref_name, commit_id, repo);
4067 if (err)
4068 goto done;
4070 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4071 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4072 progress_arg, cancel_cb, cancel_arg);
4073 done:
4074 free(commit_ref_name);
4075 return err;
4078 const struct got_error *
4079 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4080 struct got_worktree *worktree, struct got_fileindex *fileindex,
4081 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4082 struct got_repository *repo,
4083 got_worktree_checkout_cb progress_cb, void *progress_arg,
4084 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4086 const struct got_error *err;
4087 char *commit_ref_name;
4089 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4090 if (err)
4091 return err;
4093 err = store_commit_id(commit_ref_name, commit_id, repo);
4094 if (err)
4095 goto done;
4097 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4098 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4099 progress_arg, cancel_cb, cancel_arg);
4100 done:
4101 free(commit_ref_name);
4102 return err;
4105 static const struct got_error *
4106 rebase_commit(struct got_object_id **new_commit_id,
4107 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4108 struct got_worktree *worktree, struct got_fileindex *fileindex,
4109 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4110 const char *new_logmsg, struct got_repository *repo)
4112 const struct got_error *err, *sync_err;
4113 struct got_pathlist_head commitable_paths;
4114 struct collect_commitables_arg cc_arg;
4115 char *fileindex_path = NULL;
4116 struct got_reference *head_ref = NULL;
4117 struct got_object_id *head_commit_id = NULL;
4118 char *logmsg = NULL;
4120 TAILQ_INIT(&commitable_paths);
4121 *new_commit_id = NULL;
4123 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4125 err = get_fileindex_path(&fileindex_path, worktree);
4126 if (err)
4127 return err;
4129 cc_arg.commitable_paths = &commitable_paths;
4130 cc_arg.worktree = worktree;
4131 cc_arg.repo = repo;
4133 * If possible get the status of individual files directly to
4134 * avoid crawling the entire work tree once per rebased commit.
4135 * TODO: Ideally, merged_paths would contain a list of commitables
4136 * we could use so we could skip worktree_status() entirely.
4138 if (merged_paths) {
4139 struct got_pathlist_entry *pe;
4140 if (TAILQ_EMPTY(merged_paths)) {
4141 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4142 goto done;
4144 TAILQ_FOREACH(pe, merged_paths, entry) {
4145 err = worktree_status(worktree, pe->path, fileindex,
4146 repo, collect_commitables, &cc_arg, NULL, NULL);
4147 if (err)
4148 goto done;
4150 } else {
4151 err = worktree_status(worktree, "", fileindex, repo,
4152 collect_commitables, &cc_arg, NULL, NULL);
4153 if (err)
4154 goto done;
4157 if (TAILQ_EMPTY(&commitable_paths)) {
4158 /* No-op change; commit will be elided. */
4159 err = got_ref_delete(commit_ref, repo);
4160 if (err)
4161 goto done;
4162 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4163 goto done;
4166 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4167 if (err)
4168 goto done;
4170 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4171 if (err)
4172 goto done;
4174 if (new_logmsg)
4175 logmsg = strdup(new_logmsg);
4176 else
4177 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4178 if (logmsg == NULL)
4179 return got_error_from_errno("strdup");
4181 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4182 worktree, got_object_commit_get_author(orig_commit),
4183 got_object_commit_get_committer(orig_commit),
4184 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4185 if (err)
4186 goto done;
4188 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4189 if (err)
4190 goto done;
4192 err = got_ref_delete(commit_ref, repo);
4193 if (err)
4194 goto done;
4196 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4197 fileindex);
4198 sync_err = sync_fileindex(fileindex, fileindex_path);
4199 if (sync_err && err == NULL)
4200 err = sync_err;
4201 done:
4202 free(fileindex_path);
4203 free(head_commit_id);
4204 if (head_ref)
4205 got_ref_close(head_ref);
4206 if (err) {
4207 free(*new_commit_id);
4208 *new_commit_id = NULL;
4210 return err;
4213 const struct got_error *
4214 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4215 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4216 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4217 struct got_commit_object *orig_commit,
4218 struct got_object_id *orig_commit_id, struct got_repository *repo)
4220 const struct got_error *err;
4221 char *commit_ref_name;
4222 struct got_reference *commit_ref = NULL;
4223 struct got_object_id *commit_id = NULL;
4225 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4226 if (err)
4227 return err;
4229 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4230 if (err)
4231 goto done;
4232 err = got_ref_resolve(&commit_id, repo, commit_ref);
4233 if (err)
4234 goto done;
4235 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4236 err = got_error(GOT_ERR_REBASE_COMMITID);
4237 goto done;
4240 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4241 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4242 done:
4243 if (commit_ref)
4244 got_ref_close(commit_ref);
4245 free(commit_ref_name);
4246 free(commit_id);
4247 return err;
4250 const struct got_error *
4251 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4252 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4253 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4254 struct got_commit_object *orig_commit,
4255 struct got_object_id *orig_commit_id, const char *new_logmsg,
4256 struct got_repository *repo)
4258 const struct got_error *err;
4259 char *commit_ref_name;
4260 struct got_reference *commit_ref = NULL;
4261 struct got_object_id *commit_id = NULL;
4263 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4264 if (err)
4265 return err;
4267 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4268 if (err)
4269 goto done;
4270 err = got_ref_resolve(&commit_id, repo, commit_ref);
4271 if (err)
4272 goto done;
4273 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4274 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4275 goto done;
4278 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4279 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4280 done:
4281 if (commit_ref)
4282 got_ref_close(commit_ref);
4283 free(commit_ref_name);
4284 free(commit_id);
4285 return err;
4288 const struct got_error *
4289 got_worktree_rebase_postpone(struct got_worktree *worktree,
4290 struct got_fileindex *fileindex)
4292 if (fileindex)
4293 got_fileindex_free(fileindex);
4294 return lock_worktree(worktree, LOCK_SH);
4297 static const struct got_error *
4298 delete_ref(const char *name, struct got_repository *repo)
4300 const struct got_error *err;
4301 struct got_reference *ref;
4303 err = got_ref_open(&ref, repo, name, 0);
4304 if (err) {
4305 if (err->code == GOT_ERR_NOT_REF)
4306 return NULL;
4307 return err;
4310 err = got_ref_delete(ref, repo);
4311 got_ref_close(ref);
4312 return err;
4315 static const struct got_error *
4316 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4318 const struct got_error *err;
4319 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4320 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4322 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4323 if (err)
4324 goto done;
4325 err = delete_ref(tmp_branch_name, repo);
4326 if (err)
4327 goto done;
4329 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4330 if (err)
4331 goto done;
4332 err = delete_ref(new_base_branch_ref_name, repo);
4333 if (err)
4334 goto done;
4336 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4337 if (err)
4338 goto done;
4339 err = delete_ref(branch_ref_name, repo);
4340 if (err)
4341 goto done;
4343 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4344 if (err)
4345 goto done;
4346 err = delete_ref(commit_ref_name, repo);
4347 if (err)
4348 goto done;
4350 done:
4351 free(tmp_branch_name);
4352 free(new_base_branch_ref_name);
4353 free(branch_ref_name);
4354 free(commit_ref_name);
4355 return err;
4358 const struct got_error *
4359 got_worktree_rebase_complete(struct got_worktree *worktree,
4360 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4361 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4362 struct got_repository *repo)
4364 const struct got_error *err, *unlockerr;
4365 struct got_object_id *new_head_commit_id = NULL;
4367 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4368 if (err)
4369 return err;
4371 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4372 if (err)
4373 goto done;
4375 err = got_ref_write(rebased_branch, repo);
4376 if (err)
4377 goto done;
4379 err = got_worktree_set_head_ref(worktree, rebased_branch);
4380 if (err)
4381 goto done;
4383 err = delete_rebase_refs(worktree, repo);
4384 done:
4385 if (fileindex)
4386 got_fileindex_free(fileindex);
4387 free(new_head_commit_id);
4388 unlockerr = lock_worktree(worktree, LOCK_SH);
4389 if (unlockerr && err == NULL)
4390 err = unlockerr;
4391 return err;
4394 struct collect_revertible_paths_arg {
4395 struct got_pathlist_head *revertible_paths;
4396 struct got_worktree *worktree;
4399 static const struct got_error *
4400 collect_revertible_paths(void *arg, unsigned char status,
4401 unsigned char staged_status, const char *relpath,
4402 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4403 struct got_object_id *commit_id)
4405 struct collect_revertible_paths_arg *a = arg;
4406 const struct got_error *err = NULL;
4407 struct got_pathlist_entry *new = NULL;
4408 char *path = NULL;
4410 if (status != GOT_STATUS_ADD &&
4411 status != GOT_STATUS_DELETE &&
4412 status != GOT_STATUS_MODIFY &&
4413 status != GOT_STATUS_CONFLICT &&
4414 status != GOT_STATUS_MISSING)
4415 return NULL;
4417 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4418 return got_error_from_errno("asprintf");
4420 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4421 if (err || new == NULL)
4422 free(path);
4423 return err;
4426 const struct got_error *
4427 got_worktree_rebase_abort(struct got_worktree *worktree,
4428 struct got_fileindex *fileindex, struct got_repository *repo,
4429 struct got_reference *new_base_branch,
4430 got_worktree_checkout_cb progress_cb, void *progress_arg)
4432 const struct got_error *err, *unlockerr, *sync_err;
4433 struct got_reference *resolved = NULL;
4434 struct got_object_id *commit_id = NULL;
4435 char *fileindex_path = NULL;
4436 struct got_pathlist_head revertible_paths;
4437 struct got_pathlist_entry *pe;
4438 struct collect_revertible_paths_arg crp_arg;
4439 struct got_object_id *tree_id = NULL;
4441 TAILQ_INIT(&revertible_paths);
4443 err = lock_worktree(worktree, LOCK_EX);
4444 if (err)
4445 return err;
4447 err = got_ref_open(&resolved, repo,
4448 got_ref_get_symref_target(new_base_branch), 0);
4449 if (err)
4450 goto done;
4452 err = got_worktree_set_head_ref(worktree, resolved);
4453 if (err)
4454 goto done;
4457 * XXX commits to the base branch could have happened while
4458 * we were busy rebasing; should we store the original commit ID
4459 * when rebase begins and read it back here?
4461 err = got_ref_resolve(&commit_id, repo, resolved);
4462 if (err)
4463 goto done;
4465 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4466 if (err)
4467 goto done;
4469 err = got_object_id_by_path(&tree_id, repo,
4470 worktree->base_commit_id, worktree->path_prefix);
4471 if (err)
4472 goto done;
4474 err = delete_rebase_refs(worktree, repo);
4475 if (err)
4476 goto done;
4478 err = get_fileindex_path(&fileindex_path, worktree);
4479 if (err)
4480 goto done;
4482 crp_arg.revertible_paths = &revertible_paths;
4483 crp_arg.worktree = worktree;
4484 err = worktree_status(worktree, "", fileindex, repo,
4485 collect_revertible_paths, &crp_arg, NULL, NULL);
4486 if (err)
4487 goto done;
4489 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4490 err = revert_file(worktree, fileindex, pe->path,
4491 progress_cb, progress_arg, repo);
4492 if (err)
4493 goto sync;
4496 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4497 repo, progress_cb, progress_arg, NULL, NULL);
4498 sync:
4499 sync_err = sync_fileindex(fileindex, fileindex_path);
4500 if (sync_err && err == NULL)
4501 err = sync_err;
4502 done:
4503 got_ref_close(resolved);
4504 free(tree_id);
4505 free(commit_id);
4506 if (fileindex)
4507 got_fileindex_free(fileindex);
4508 free(fileindex_path);
4509 TAILQ_FOREACH(pe, &revertible_paths, entry)
4510 free((char *)pe->path);
4511 got_pathlist_free(&revertible_paths);
4513 unlockerr = lock_worktree(worktree, LOCK_SH);
4514 if (unlockerr && err == NULL)
4515 err = unlockerr;
4516 return err;
4519 const struct got_error *
4520 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4521 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4522 struct got_fileindex **fileindex, struct got_worktree *worktree,
4523 struct got_repository *repo)
4525 const struct got_error *err = NULL;
4526 char *tmp_branch_name = NULL;
4527 char *branch_ref_name = NULL;
4528 char *base_commit_ref_name = NULL;
4529 char *fileindex_path = NULL;
4530 struct check_rebase_ok_arg ok_arg;
4531 struct got_reference *wt_branch = NULL;
4532 struct got_reference *base_commit_ref = NULL;
4534 *tmp_branch = NULL;
4535 *branch_ref = NULL;
4536 *base_commit_id = NULL;
4537 *fileindex = NULL;
4539 err = lock_worktree(worktree, LOCK_EX);
4540 if (err)
4541 return err;
4543 err = open_fileindex(fileindex, &fileindex_path, worktree);
4544 if (err)
4545 goto done;
4547 ok_arg.worktree = worktree;
4548 ok_arg.repo = repo;
4549 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4550 &ok_arg);
4551 if (err)
4552 goto done;
4554 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4555 if (err)
4556 goto done;
4558 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4559 if (err)
4560 goto done;
4562 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4563 worktree);
4564 if (err)
4565 goto done;
4567 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4568 0);
4569 if (err)
4570 goto done;
4572 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4573 if (err)
4574 goto done;
4576 err = got_ref_write(*branch_ref, repo);
4577 if (err)
4578 goto done;
4580 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4581 worktree->base_commit_id);
4582 if (err)
4583 goto done;
4584 err = got_ref_write(base_commit_ref, repo);
4585 if (err)
4586 goto done;
4587 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4588 if (*base_commit_id == NULL) {
4589 err = got_error_from_errno("got_object_id_dup");
4590 goto done;
4593 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4594 worktree->base_commit_id);
4595 if (err)
4596 goto done;
4597 err = got_ref_write(*tmp_branch, repo);
4598 if (err)
4599 goto done;
4601 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4602 if (err)
4603 goto done;
4604 done:
4605 free(fileindex_path);
4606 free(tmp_branch_name);
4607 free(branch_ref_name);
4608 free(base_commit_ref_name);
4609 if (wt_branch)
4610 got_ref_close(wt_branch);
4611 if (err) {
4612 if (*branch_ref) {
4613 got_ref_close(*branch_ref);
4614 *branch_ref = NULL;
4616 if (*tmp_branch) {
4617 got_ref_close(*tmp_branch);
4618 *tmp_branch = NULL;
4620 free(*base_commit_id);
4621 if (*fileindex) {
4622 got_fileindex_free(*fileindex);
4623 *fileindex = NULL;
4625 lock_worktree(worktree, LOCK_SH);
4627 return err;
4630 const struct got_error *
4631 got_worktree_histedit_postpone(struct got_worktree *worktree,
4632 struct got_fileindex *fileindex)
4634 if (fileindex)
4635 got_fileindex_free(fileindex);
4636 return lock_worktree(worktree, LOCK_SH);
4639 const struct got_error *
4640 got_worktree_histedit_in_progress(int *in_progress,
4641 struct got_worktree *worktree)
4643 const struct got_error *err;
4644 char *tmp_branch_name = NULL;
4646 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4647 if (err)
4648 return err;
4650 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4651 free(tmp_branch_name);
4652 return NULL;
4655 const struct got_error *
4656 got_worktree_histedit_continue(struct got_object_id **commit_id,
4657 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4658 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4659 struct got_worktree *worktree, struct got_repository *repo)
4661 const struct got_error *err;
4662 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4663 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4664 struct got_reference *commit_ref = NULL;
4665 struct got_reference *base_commit_ref = NULL;
4666 char *fileindex_path = NULL;
4668 *commit_id = NULL;
4669 *tmp_branch = NULL;
4670 *base_commit_id = NULL;
4671 *fileindex = NULL;
4673 err = lock_worktree(worktree, LOCK_EX);
4674 if (err)
4675 return err;
4677 err = open_fileindex(fileindex, &fileindex_path, worktree);
4678 if (err)
4679 goto done;
4681 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4682 if (err)
4683 return err;
4685 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4686 if (err)
4687 goto done;
4689 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4690 if (err)
4691 goto done;
4693 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4694 worktree);
4695 if (err)
4696 goto done;
4698 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4699 if (err)
4700 goto done;
4702 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4703 if (err)
4704 goto done;
4705 err = got_ref_resolve(commit_id, repo, commit_ref);
4706 if (err)
4707 goto done;
4709 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4710 if (err)
4711 goto done;
4712 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4713 if (err)
4714 goto done;
4716 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4717 if (err)
4718 goto done;
4719 done:
4720 free(commit_ref_name);
4721 free(branch_ref_name);
4722 free(fileindex_path);
4723 if (commit_ref)
4724 got_ref_close(commit_ref);
4725 if (base_commit_ref)
4726 got_ref_close(base_commit_ref);
4727 if (err) {
4728 free(*commit_id);
4729 *commit_id = NULL;
4730 free(*base_commit_id);
4731 *base_commit_id = NULL;
4732 if (*tmp_branch) {
4733 got_ref_close(*tmp_branch);
4734 *tmp_branch = NULL;
4736 if (*fileindex) {
4737 got_fileindex_free(*fileindex);
4738 *fileindex = NULL;
4740 lock_worktree(worktree, LOCK_EX);
4742 return err;
4745 static const struct got_error *
4746 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4748 const struct got_error *err;
4749 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4750 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4752 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4753 if (err)
4754 goto done;
4755 err = delete_ref(tmp_branch_name, repo);
4756 if (err)
4757 goto done;
4759 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4760 worktree);
4761 if (err)
4762 goto done;
4763 err = delete_ref(base_commit_ref_name, repo);
4764 if (err)
4765 goto done;
4767 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4768 if (err)
4769 goto done;
4770 err = delete_ref(branch_ref_name, repo);
4771 if (err)
4772 goto done;
4774 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4775 if (err)
4776 goto done;
4777 err = delete_ref(commit_ref_name, repo);
4778 if (err)
4779 goto done;
4780 done:
4781 free(tmp_branch_name);
4782 free(base_commit_ref_name);
4783 free(branch_ref_name);
4784 free(commit_ref_name);
4785 return err;
4788 const struct got_error *
4789 got_worktree_histedit_abort(struct got_worktree *worktree,
4790 struct got_fileindex *fileindex, struct got_repository *repo,
4791 struct got_reference *branch, struct got_object_id *base_commit_id,
4792 got_worktree_checkout_cb progress_cb, void *progress_arg)
4794 const struct got_error *err, *unlockerr, *sync_err;
4795 struct got_reference *resolved = NULL;
4796 char *fileindex_path = NULL;
4797 struct got_pathlist_head revertible_paths;
4798 struct got_pathlist_entry *pe;
4799 struct collect_revertible_paths_arg crp_arg;
4800 struct got_object_id *tree_id = NULL;
4802 TAILQ_INIT(&revertible_paths);
4804 err = lock_worktree(worktree, LOCK_EX);
4805 if (err)
4806 return err;
4808 err = got_ref_open(&resolved, repo,
4809 got_ref_get_symref_target(branch), 0);
4810 if (err)
4811 goto done;
4813 err = got_worktree_set_head_ref(worktree, resolved);
4814 if (err)
4815 goto done;
4817 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4818 if (err)
4819 goto done;
4821 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4822 worktree->path_prefix);
4823 if (err)
4824 goto done;
4826 err = delete_histedit_refs(worktree, repo);
4827 if (err)
4828 goto done;
4830 err = get_fileindex_path(&fileindex_path, worktree);
4831 if (err)
4832 goto done;
4834 crp_arg.revertible_paths = &revertible_paths;
4835 crp_arg.worktree = worktree;
4836 err = worktree_status(worktree, "", fileindex, repo,
4837 collect_revertible_paths, &crp_arg, NULL, NULL);
4838 if (err)
4839 goto done;
4841 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4842 err = revert_file(worktree, fileindex, pe->path,
4843 progress_cb, progress_arg, repo);
4844 if (err)
4845 goto sync;
4848 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4849 repo, progress_cb, progress_arg, NULL, NULL);
4850 sync:
4851 sync_err = sync_fileindex(fileindex, fileindex_path);
4852 if (sync_err && err == NULL)
4853 err = sync_err;
4854 done:
4855 got_ref_close(resolved);
4856 free(tree_id);
4857 free(fileindex_path);
4858 TAILQ_FOREACH(pe, &revertible_paths, entry)
4859 free((char *)pe->path);
4860 got_pathlist_free(&revertible_paths);
4862 unlockerr = lock_worktree(worktree, LOCK_SH);
4863 if (unlockerr && err == NULL)
4864 err = unlockerr;
4865 return err;
4868 const struct got_error *
4869 got_worktree_histedit_complete(struct got_worktree *worktree,
4870 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4871 struct got_reference *edited_branch, struct got_repository *repo)
4873 const struct got_error *err, *unlockerr;
4874 struct got_object_id *new_head_commit_id = NULL;
4875 struct got_reference *resolved = NULL;
4877 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4878 if (err)
4879 return err;
4881 err = got_ref_open(&resolved, repo,
4882 got_ref_get_symref_target(edited_branch), 0);
4883 if (err)
4884 goto done;
4886 err = got_ref_change_ref(resolved, new_head_commit_id);
4887 if (err)
4888 goto done;
4890 err = got_ref_write(resolved, repo);
4891 if (err)
4892 goto done;
4894 err = got_worktree_set_head_ref(worktree, resolved);
4895 if (err)
4896 goto done;
4898 err = delete_histedit_refs(worktree, repo);
4899 done:
4900 if (fileindex)
4901 got_fileindex_free(fileindex);
4902 free(new_head_commit_id);
4903 unlockerr = lock_worktree(worktree, LOCK_SH);
4904 if (unlockerr && err == NULL)
4905 err = unlockerr;
4906 return err;
4909 const struct got_error *
4910 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4911 struct got_object_id *commit_id, struct got_repository *repo)
4913 const struct got_error *err;
4914 char *commit_ref_name;
4916 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4917 if (err)
4918 return err;
4920 err = store_commit_id(commit_ref_name, commit_id, repo);
4921 if (err)
4922 goto done;
4924 err = delete_ref(commit_ref_name, repo);
4925 done:
4926 free(commit_ref_name);
4927 return err;
4930 static const struct got_error *
4931 stage_path(const char *relpath, const char *ondisk_path,
4932 const char *path_content, struct got_worktree *worktree,
4933 struct got_fileindex *fileindex, struct got_repository *repo,
4934 got_worktree_status_cb status_cb, void *status_arg)
4936 const struct got_error *err = NULL;
4937 struct got_fileindex_entry *ie;
4938 unsigned char status, staged_status;
4939 struct stat sb;
4940 struct got_object_id blob_id, *staged_blob_id = NULL;
4941 uint32_t stage;
4943 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4944 if (ie == NULL)
4945 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4947 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4948 if (err)
4949 return err;
4950 staged_status = get_staged_status(ie);
4952 switch (status) {
4953 case GOT_STATUS_ADD:
4954 case GOT_STATUS_MODIFY:
4955 err = got_object_blob_create(&staged_blob_id,
4956 path_content ? path_content : ondisk_path, repo);
4957 if (err)
4958 goto done;
4959 memcpy(&blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
4960 memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
4961 SHA1_DIGEST_LENGTH);
4962 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
4963 stage = GOT_FILEIDX_STAGE_ADD;
4964 else
4965 stage = GOT_FILEIDX_STAGE_MODIFY;
4966 got_fileindex_entry_stage_set(ie, stage);
4967 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4968 get_staged_status(ie), relpath, &blob_id,
4969 staged_blob_id, NULL);
4970 break;
4971 case GOT_STATUS_DELETE:
4972 if (staged_status == GOT_STATUS_DELETE)
4973 break;
4974 stage = GOT_FILEIDX_STAGE_DELETE;
4975 got_fileindex_entry_stage_set(ie, stage);
4976 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4977 get_staged_status(ie), relpath, NULL, NULL, NULL);
4978 break;
4979 case GOT_STATUS_NO_CHANGE:
4980 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
4981 break;
4982 default:
4983 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4984 break;
4986 done:
4987 free(staged_blob_id);
4988 return err;
4991 const struct got_error *
4992 got_worktree_stage(struct got_worktree *worktree,
4993 struct got_pathlist_head *paths,
4994 got_worktree_status_cb status_cb, void *status_arg,
4995 struct got_repository *repo)
4997 const struct got_error *err = NULL, *sync_err, *unlockerr;
4998 struct got_pathlist_entry *pe;
4999 struct got_fileindex *fileindex = NULL;
5000 char *fileindex_path = NULL;
5002 err = lock_worktree(worktree, LOCK_EX);
5003 if (err)
5004 return err;
5006 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5007 if (err)
5008 goto done;
5010 TAILQ_FOREACH(pe, paths, entry) {
5011 char *relpath;
5012 err = got_path_skip_common_ancestor(&relpath,
5013 got_worktree_get_root_path(worktree), pe->path);
5014 if (err)
5015 break;
5016 err = stage_path(relpath, pe->path,
5017 (const char *)pe->data, worktree, fileindex, repo,
5018 status_cb, status_arg);
5019 free(relpath);
5020 if (err)
5021 break;
5024 sync_err = sync_fileindex(fileindex, fileindex_path);
5025 if (sync_err && err == NULL)
5026 err = sync_err;
5027 done:
5028 free(fileindex_path);
5029 if (fileindex)
5030 got_fileindex_free(fileindex);
5031 unlockerr = lock_worktree(worktree, LOCK_SH);
5032 if (unlockerr && err == NULL)
5033 err = unlockerr;
5034 return err;