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 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1189 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1190 goto done;
1192 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1193 if (err)
1194 goto done;
1195 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1196 sb.st_mode = got_fileindex_perms_to_st(ie);
1197 } else
1198 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1200 if (status == GOT_STATUS_OBSTRUCTED) {
1201 err = (*progress_cb)(progress_arg, status, path);
1202 goto done;
1205 if (ie && status != GOT_STATUS_MISSING) {
1206 if (got_fileindex_entry_has_commit(ie) &&
1207 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1208 SHA1_DIGEST_LENGTH) == 0) {
1209 err = sync_timestamps(ondisk_path, status, ie, &sb);
1210 if (err)
1211 goto done;
1212 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1213 path);
1214 goto done;
1216 if (got_fileindex_entry_has_blob(ie) &&
1217 memcmp(ie->blob_sha1, te->id->sha1,
1218 SHA1_DIGEST_LENGTH) == 0) {
1219 err = sync_timestamps(ondisk_path, status, ie, &sb);
1220 goto done;
1224 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1225 if (err)
1226 goto done;
1228 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1229 int update_timestamps;
1230 struct got_blob_object *blob2 = NULL;
1231 if (got_fileindex_entry_has_blob(ie)) {
1232 struct got_object_id id2;
1233 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1234 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1235 if (err)
1236 goto done;
1238 err = merge_blob(&update_timestamps, worktree, blob2,
1239 ondisk_path, path, sb.st_mode, blob,
1240 worktree->base_commit_id, repo,
1241 progress_cb, progress_arg);
1242 if (blob2)
1243 got_object_blob_close(blob2);
1245 * Do not update timestamps of files with local changes.
1246 * Otherwise, a future status walk would treat them as
1247 * unmodified files again.
1249 err = got_fileindex_entry_update(ie, ondisk_path,
1250 blob->id.sha1, worktree->base_commit_id->sha1,
1251 update_timestamps);
1252 } else if (status == GOT_STATUS_DELETE) {
1253 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1254 if (err)
1255 goto done;
1256 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1257 ondisk_path, path, blob, 0);
1258 if (err)
1259 goto done;
1260 } else {
1261 err = install_blob(worktree, ondisk_path, path, te->mode,
1262 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1263 repo, progress_cb, progress_arg);
1264 if (err)
1265 goto done;
1266 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1267 ondisk_path, path, blob, 1);
1268 if (err)
1269 goto done;
1271 got_object_blob_close(blob);
1272 done:
1273 free(ondisk_path);
1274 return err;
1277 static const struct got_error *
1278 remove_ondisk_file(const char *root_path, const char *path)
1280 const struct got_error *err = NULL;
1281 char *ondisk_path = NULL;
1283 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1284 return got_error_from_errno("asprintf");
1286 if (unlink(ondisk_path) == -1) {
1287 if (errno != ENOENT)
1288 err = got_error_from_errno2("unlink", ondisk_path);
1289 } else {
1290 char *parent = dirname(ondisk_path);
1291 while (parent && strcmp(parent, root_path) != 0) {
1292 if (rmdir(parent) == -1) {
1293 if (errno != ENOTEMPTY)
1294 err = got_error_from_errno2("rmdir",
1295 parent);
1296 break;
1298 parent = dirname(parent);
1301 free(ondisk_path);
1302 return err;
1305 static const struct got_error *
1306 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1307 struct got_fileindex_entry *ie, struct got_repository *repo,
1308 got_worktree_checkout_cb progress_cb, void *progress_arg)
1310 const struct got_error *err = NULL;
1311 unsigned char status;
1312 struct stat sb;
1313 char *ondisk_path;
1315 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1316 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1318 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1319 == -1)
1320 return got_error_from_errno("asprintf");
1322 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1323 if (err)
1324 return err;
1326 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1327 status == GOT_STATUS_ADD) {
1328 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1329 if (err)
1330 return err;
1332 * Preserve the working file and change the deleted blob's
1333 * entry into a schedule-add entry.
1335 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1336 0);
1337 if (err)
1338 return err;
1339 } else {
1340 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1341 if (err)
1342 return err;
1343 if (status == GOT_STATUS_NO_CHANGE) {
1344 err = remove_ondisk_file(worktree->root_path, ie->path);
1345 if (err)
1346 return err;
1348 got_fileindex_entry_remove(fileindex, ie);
1351 return err;
1354 struct diff_cb_arg {
1355 struct got_fileindex *fileindex;
1356 struct got_worktree *worktree;
1357 struct got_repository *repo;
1358 got_worktree_checkout_cb progress_cb;
1359 void *progress_arg;
1360 got_worktree_cancel_cb cancel_cb;
1361 void *cancel_arg;
1364 static const struct got_error *
1365 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1366 struct got_tree_entry *te, const char *parent_path)
1368 struct diff_cb_arg *a = arg;
1370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 return got_error(GOT_ERR_CANCELLED);
1373 return update_blob(a->worktree, a->fileindex, ie, te,
1374 ie->path, a->repo, a->progress_cb, a->progress_arg);
1377 static const struct got_error *
1378 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1380 struct diff_cb_arg *a = arg;
1382 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1383 return got_error(GOT_ERR_CANCELLED);
1385 return delete_blob(a->worktree, a->fileindex, ie,
1386 a->repo, a->progress_cb, a->progress_arg);
1389 static const struct got_error *
1390 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1392 struct diff_cb_arg *a = arg;
1393 const struct got_error *err;
1394 char *path;
1396 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1397 return got_error(GOT_ERR_CANCELLED);
1399 if (asprintf(&path, "%s%s%s", parent_path,
1400 parent_path[0] ? "/" : "", te->name)
1401 == -1)
1402 return got_error_from_errno("asprintf");
1404 if (S_ISDIR(te->mode))
1405 err = add_dir_on_disk(a->worktree, path);
1406 else
1407 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1408 a->repo, a->progress_cb, a->progress_arg);
1410 free(path);
1411 return err;
1414 static const struct got_error *
1415 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1417 const struct got_error *err = NULL;
1418 char *uuidstr = NULL;
1419 uint32_t uuid_status;
1421 *refname = NULL;
1423 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1424 if (uuid_status != uuid_s_ok)
1425 return got_error_uuid(uuid_status);
1427 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1428 == -1) {
1429 err = got_error_from_errno("asprintf");
1430 *refname = NULL;
1432 free(uuidstr);
1433 return err;
1436 const struct got_error *
1437 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1439 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1442 static const struct got_error *
1443 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1445 return get_ref_name(refname, worktree,
1446 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1449 static const struct got_error *
1450 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1452 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1455 static const struct got_error *
1456 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1458 return get_ref_name(refname, worktree,
1459 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1462 static const struct got_error *
1463 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1465 return get_ref_name(refname, worktree,
1466 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1469 static const struct got_error *
1470 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1472 return get_ref_name(refname, worktree,
1473 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1476 static const struct got_error *
1477 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1479 return get_ref_name(refname, worktree,
1480 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1483 static const struct got_error *
1484 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1486 return get_ref_name(refname, worktree,
1487 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1490 static const struct got_error *
1491 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1493 return get_ref_name(refname, worktree,
1494 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1497 const struct got_error *
1498 got_worktree_get_histedit_script_path(char **path,
1499 struct got_worktree *worktree)
1501 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1502 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1503 *path = NULL;
1504 return got_error_from_errno("asprintf");
1506 return NULL;
1510 * Prevent Git's garbage collector from deleting our base commit by
1511 * setting a reference to our base commit's ID.
1513 static const struct got_error *
1514 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1516 const struct got_error *err = NULL;
1517 struct got_reference *ref = NULL;
1518 char *refname;
1520 err = got_worktree_get_base_ref_name(&refname, worktree);
1521 if (err)
1522 return err;
1524 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1525 if (err)
1526 goto done;
1528 err = got_ref_write(ref, repo);
1529 done:
1530 free(refname);
1531 if (ref)
1532 got_ref_close(ref);
1533 return err;
1536 static const struct got_error *
1537 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1539 const struct got_error *err = NULL;
1541 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1542 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1543 err = got_error_from_errno("asprintf");
1544 *fileindex_path = NULL;
1546 return err;
1550 static const struct got_error *
1551 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1552 struct got_worktree *worktree)
1554 const struct got_error *err = NULL;
1555 FILE *index = NULL;
1557 *fileindex_path = NULL;
1558 *fileindex = got_fileindex_alloc();
1559 if (*fileindex == NULL)
1560 return got_error_from_errno("got_fileindex_alloc");
1562 err = get_fileindex_path(fileindex_path, worktree);
1563 if (err)
1564 goto done;
1566 index = fopen(*fileindex_path, "rb");
1567 if (index == NULL) {
1568 if (errno != ENOENT)
1569 err = got_error_from_errno2("fopen", *fileindex_path);
1570 } else {
1571 err = got_fileindex_read(*fileindex, index);
1572 if (fclose(index) != 0 && err == NULL)
1573 err = got_error_from_errno("fclose");
1575 done:
1576 if (err) {
1577 free(*fileindex_path);
1578 *fileindex_path = NULL;
1579 got_fileindex_free(*fileindex);
1580 *fileindex = NULL;
1582 return err;
1585 struct bump_base_commit_id_arg {
1586 struct got_object_id *base_commit_id;
1587 const char *path;
1588 size_t path_len;
1589 const char *entry_name;
1590 got_worktree_checkout_cb progress_cb;
1591 void *progress_arg;
1594 /* Bump base commit ID of all files within an updated part of the work tree. */
1595 static const struct got_error *
1596 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1598 const struct got_error *err;
1599 struct bump_base_commit_id_arg *a = arg;
1601 if (a->entry_name) {
1602 if (strcmp(ie->path, a->path) != 0)
1603 return NULL;
1604 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1605 return NULL;
1607 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1608 SHA1_DIGEST_LENGTH) == 0)
1609 return NULL;
1611 if (a->progress_cb) {
1612 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1613 ie->path);
1614 if (err)
1615 return err;
1617 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1618 return NULL;
1621 static const struct got_error *
1622 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1624 const struct got_error *err = NULL;
1625 char *new_fileindex_path = NULL;
1626 FILE *new_index = NULL;
1628 err = got_opentemp_named(&new_fileindex_path, &new_index,
1629 fileindex_path);
1630 if (err)
1631 goto done;
1633 err = got_fileindex_write(fileindex, new_index);
1634 if (err)
1635 goto done;
1637 if (rename(new_fileindex_path, fileindex_path) != 0) {
1638 err = got_error_from_errno3("rename", new_fileindex_path,
1639 fileindex_path);
1640 unlink(new_fileindex_path);
1642 done:
1643 if (new_index)
1644 fclose(new_index);
1645 free(new_fileindex_path);
1646 return err;
1649 static const struct got_error *
1650 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1651 struct got_object_id **tree_id, const char *wt_relpath,
1652 struct got_worktree *worktree, struct got_repository *repo)
1654 const struct got_error *err = NULL;
1655 struct got_object_id *id = NULL;
1656 char *in_repo_path = NULL;
1657 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1659 *entry_type = GOT_OBJ_TYPE_ANY;
1660 *tree_relpath = NULL;
1661 *tree_id = NULL;
1663 if (wt_relpath[0] == '\0') {
1664 /* Check out all files within the work tree. */
1665 *entry_type = GOT_OBJ_TYPE_TREE;
1666 *tree_relpath = strdup("");
1667 if (*tree_relpath == NULL) {
1668 err = got_error_from_errno("strdup");
1669 goto done;
1671 err = got_object_id_by_path(tree_id, repo,
1672 worktree->base_commit_id, worktree->path_prefix);
1673 if (err)
1674 goto done;
1675 return NULL;
1678 /* Check out a subset of files in the work tree. */
1680 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1681 is_root_wt ? "" : "/", wt_relpath) == -1) {
1682 err = got_error_from_errno("asprintf");
1683 goto done;
1686 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1687 in_repo_path);
1688 if (err)
1689 goto done;
1691 free(in_repo_path);
1692 in_repo_path = NULL;
1694 err = got_object_get_type(entry_type, repo, id);
1695 if (err)
1696 goto done;
1698 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1699 /* Check out a single file. */
1700 if (strchr(wt_relpath, '/') == NULL) {
1701 /* Check out a single file in work tree's root dir. */
1702 in_repo_path = strdup(worktree->path_prefix);
1703 if (in_repo_path == NULL) {
1704 err = got_error_from_errno("strdup");
1705 goto done;
1707 *tree_relpath = strdup("");
1708 if (*tree_relpath == NULL) {
1709 err = got_error_from_errno("strdup");
1710 goto done;
1712 } else {
1713 /* Check out a single file in a subdirectory. */
1714 err = got_path_dirname(tree_relpath, wt_relpath);
1715 if (err)
1716 return err;
1717 if (asprintf(&in_repo_path, "%s%s%s",
1718 worktree->path_prefix, is_root_wt ? "" : "/",
1719 *tree_relpath) == -1) {
1720 err = got_error_from_errno("asprintf");
1721 goto done;
1724 err = got_object_id_by_path(tree_id, repo,
1725 worktree->base_commit_id, in_repo_path);
1726 } else {
1727 /* Check out all files within a subdirectory. */
1728 *tree_id = got_object_id_dup(id);
1729 if (*tree_id == NULL) {
1730 err = got_error_from_errno("got_object_id_dup");
1731 goto done;
1733 *tree_relpath = strdup(wt_relpath);
1734 if (*tree_relpath == NULL) {
1735 err = got_error_from_errno("strdup");
1736 goto done;
1739 done:
1740 free(id);
1741 free(in_repo_path);
1742 if (err) {
1743 *entry_type = GOT_OBJ_TYPE_ANY;
1744 free(*tree_relpath);
1745 *tree_relpath = NULL;
1746 free(*tree_id);
1747 *tree_id = NULL;
1749 return err;
1752 static const struct got_error *
1753 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1754 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1755 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1756 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1758 const struct got_error *err = NULL;
1759 struct got_commit_object *commit = NULL;
1760 struct got_tree_object *tree = NULL;
1761 struct got_fileindex_diff_tree_cb diff_cb;
1762 struct diff_cb_arg arg;
1764 err = ref_base_commit(worktree, repo);
1765 if (err)
1766 goto done;
1768 err = got_object_open_as_commit(&commit, repo,
1769 worktree->base_commit_id);
1770 if (err)
1771 goto done;
1773 err = got_object_open_as_tree(&tree, repo, tree_id);
1774 if (err)
1775 goto done;
1777 if (entry_name &&
1778 got_object_tree_find_entry(tree, entry_name) == NULL) {
1779 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1780 goto done;
1783 diff_cb.diff_old_new = diff_old_new;
1784 diff_cb.diff_old = diff_old;
1785 diff_cb.diff_new = diff_new;
1786 arg.fileindex = fileindex;
1787 arg.worktree = worktree;
1788 arg.repo = repo;
1789 arg.progress_cb = progress_cb;
1790 arg.progress_arg = progress_arg;
1791 arg.cancel_cb = cancel_cb;
1792 arg.cancel_arg = cancel_arg;
1793 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1794 entry_name, repo, &diff_cb, &arg);
1795 done:
1796 if (tree)
1797 got_object_tree_close(tree);
1798 if (commit)
1799 got_object_commit_close(commit);
1800 return err;
1803 const struct got_error *
1804 got_worktree_checkout_files(struct got_worktree *worktree,
1805 struct got_pathlist_head *paths, struct got_repository *repo,
1806 got_worktree_checkout_cb progress_cb, void *progress_arg,
1807 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1809 const struct got_error *err = NULL, *sync_err, *unlockerr;
1810 struct got_commit_object *commit = NULL;
1811 struct got_tree_object *tree = NULL;
1812 struct got_fileindex *fileindex = NULL;
1813 char *fileindex_path = NULL;
1814 struct got_pathlist_entry *pe;
1815 struct tree_path_data {
1816 SIMPLEQ_ENTRY(tree_path_data) entry;
1817 struct got_object_id *tree_id;
1818 int entry_type;
1819 char *relpath;
1820 char *entry_name;
1821 } *tpd = NULL;
1822 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1824 SIMPLEQ_INIT(&tree_paths);
1826 err = lock_worktree(worktree, LOCK_EX);
1827 if (err)
1828 return err;
1830 /* Map all specified paths to in-repository trees. */
1831 TAILQ_FOREACH(pe, paths, entry) {
1832 tpd = malloc(sizeof(*tpd));
1833 if (tpd == NULL) {
1834 err = got_error_from_errno("malloc");
1835 goto done;
1838 err = find_tree_entry_for_checkout(&tpd->entry_type,
1839 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1840 if (err) {
1841 free(tpd);
1842 goto done;
1845 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1846 err = got_path_basename(&tpd->entry_name, pe->path);
1847 if (err) {
1848 free(tpd->relpath);
1849 free(tpd->tree_id);
1850 free(tpd);
1851 goto done;
1853 } else
1854 tpd->entry_name = NULL;
1856 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1860 * Read the file index.
1861 * Checking out files is supposed to be an idempotent operation.
1862 * If the on-disk file index is incomplete we will try to complete it.
1864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1865 if (err)
1866 goto done;
1868 tpd = SIMPLEQ_FIRST(&tree_paths);
1869 TAILQ_FOREACH(pe, paths, entry) {
1870 struct bump_base_commit_id_arg bbc_arg;
1872 err = checkout_files(worktree, fileindex, tpd->relpath,
1873 tpd->tree_id, tpd->entry_name, repo,
1874 progress_cb, progress_arg, cancel_cb, cancel_arg);
1875 if (err)
1876 break;
1878 bbc_arg.base_commit_id = worktree->base_commit_id;
1879 bbc_arg.entry_name = tpd->entry_name;
1880 bbc_arg.path = pe->path;
1881 bbc_arg.path_len = pe->path_len;
1882 bbc_arg.progress_cb = progress_cb;
1883 bbc_arg.progress_arg = progress_arg;
1884 err = got_fileindex_for_each_entry_safe(fileindex,
1885 bump_base_commit_id, &bbc_arg);
1886 if (err)
1887 break;
1889 tpd = SIMPLEQ_NEXT(tpd, entry);
1891 sync_err = sync_fileindex(fileindex, fileindex_path);
1892 if (sync_err && err == NULL)
1893 err = sync_err;
1894 done:
1895 free(fileindex_path);
1896 if (tree)
1897 got_object_tree_close(tree);
1898 if (commit)
1899 got_object_commit_close(commit);
1900 if (fileindex)
1901 got_fileindex_free(fileindex);
1902 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1903 tpd = SIMPLEQ_FIRST(&tree_paths);
1904 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1905 free(tpd->relpath);
1906 free(tpd->tree_id);
1907 free(tpd);
1909 unlockerr = lock_worktree(worktree, LOCK_SH);
1910 if (unlockerr && err == NULL)
1911 err = unlockerr;
1912 return err;
1915 struct merge_file_cb_arg {
1916 struct got_worktree *worktree;
1917 struct got_fileindex *fileindex;
1918 got_worktree_checkout_cb progress_cb;
1919 void *progress_arg;
1920 got_worktree_cancel_cb cancel_cb;
1921 void *cancel_arg;
1922 struct got_object_id *commit_id2;
1925 static const struct got_error *
1926 merge_file_cb(void *arg, struct got_blob_object *blob1,
1927 struct got_blob_object *blob2, struct got_object_id *id1,
1928 struct got_object_id *id2, const char *path1, const char *path2,
1929 struct got_repository *repo)
1931 static const struct got_error *err = NULL;
1932 struct merge_file_cb_arg *a = arg;
1933 struct got_fileindex_entry *ie;
1934 char *ondisk_path = NULL;
1935 struct stat sb;
1936 unsigned char status;
1937 int local_changes_subsumed;
1939 if (blob1 && blob2) {
1940 ie = got_fileindex_entry_get(a->fileindex, path2,
1941 strlen(path2));
1942 if (ie == NULL)
1943 return (*a->progress_cb)(a->progress_arg,
1944 GOT_STATUS_MISSING, path2);
1946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1947 path2) == -1)
1948 return got_error_from_errno("asprintf");
1950 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1951 if (err)
1952 goto done;
1954 if (status == GOT_STATUS_DELETE) {
1955 err = (*a->progress_cb)(a->progress_arg,
1956 GOT_STATUS_MERGE, path2);
1957 goto done;
1959 if (status != GOT_STATUS_NO_CHANGE &&
1960 status != GOT_STATUS_MODIFY &&
1961 status != GOT_STATUS_CONFLICT &&
1962 status != GOT_STATUS_ADD) {
1963 err = (*a->progress_cb)(a->progress_arg, status, path2);
1964 goto done;
1967 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1968 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1969 a->progress_cb, a->progress_arg);
1970 } else if (blob1) {
1971 ie = got_fileindex_entry_get(a->fileindex, path1,
1972 strlen(path1));
1973 if (ie == NULL)
1974 return (*a->progress_cb)(a->progress_arg,
1975 GOT_STATUS_MISSING, path2);
1977 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1978 path1) == -1)
1979 return got_error_from_errno("asprintf");
1981 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1982 if (err)
1983 goto done;
1985 switch (status) {
1986 case GOT_STATUS_NO_CHANGE:
1987 err = (*a->progress_cb)(a->progress_arg,
1988 GOT_STATUS_DELETE, path1);
1989 if (err)
1990 goto done;
1991 err = remove_ondisk_file(a->worktree->root_path, path1);
1992 if (err)
1993 goto done;
1994 if (ie)
1995 got_fileindex_entry_mark_deleted_from_disk(ie);
1996 break;
1997 case GOT_STATUS_DELETE:
1998 case GOT_STATUS_MISSING:
1999 err = (*a->progress_cb)(a->progress_arg,
2000 GOT_STATUS_DELETE, path1);
2001 if (err)
2002 goto done;
2003 if (ie)
2004 got_fileindex_entry_mark_deleted_from_disk(ie);
2005 break;
2006 case GOT_STATUS_ADD:
2007 case GOT_STATUS_MODIFY:
2008 case GOT_STATUS_CONFLICT:
2009 err = (*a->progress_cb)(a->progress_arg,
2010 GOT_STATUS_CANNOT_DELETE, path1);
2011 if (err)
2012 goto done;
2013 break;
2014 case GOT_STATUS_OBSTRUCTED:
2015 err = (*a->progress_cb)(a->progress_arg, status, path1);
2016 if (err)
2017 goto done;
2018 break;
2019 default:
2020 break;
2022 } else if (blob2) {
2023 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2024 path2) == -1)
2025 return got_error_from_errno("asprintf");
2026 ie = got_fileindex_entry_get(a->fileindex, path2,
2027 strlen(path2));
2028 if (ie) {
2029 err = get_file_status(&status, &sb, ie, ondisk_path,
2030 repo);
2031 if (err)
2032 goto done;
2033 if (status != GOT_STATUS_NO_CHANGE &&
2034 status != GOT_STATUS_MODIFY &&
2035 status != GOT_STATUS_CONFLICT &&
2036 status != GOT_STATUS_ADD) {
2037 err = (*a->progress_cb)(a->progress_arg,
2038 status, path2);
2039 goto done;
2041 err = merge_blob(&local_changes_subsumed, a->worktree,
2042 NULL, ondisk_path, path2, sb.st_mode, blob2,
2043 a->commit_id2, repo,
2044 a->progress_cb, a->progress_arg);
2045 if (status == GOT_STATUS_DELETE) {
2046 err = update_blob_fileindex_entry(a->worktree,
2047 a->fileindex, ie, ondisk_path, ie->path,
2048 blob2, 0);
2049 if (err)
2050 goto done;
2052 } else {
2053 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2054 err = install_blob(a->worktree, ondisk_path, path2,
2055 /* XXX get this from parent tree! */
2056 GOT_DEFAULT_FILE_MODE,
2057 sb.st_mode, blob2, 0, 0, repo,
2058 a->progress_cb, a->progress_arg);
2059 if (err)
2060 goto done;
2061 err = got_fileindex_entry_alloc(&ie,
2062 ondisk_path, path2, NULL, NULL);
2063 if (err)
2064 goto done;
2065 err = got_fileindex_entry_add(a->fileindex, ie);
2066 if (err) {
2067 got_fileindex_entry_free(ie);
2068 goto done;
2072 done:
2073 free(ondisk_path);
2074 return err;
2077 struct check_merge_ok_arg {
2078 struct got_worktree *worktree;
2079 struct got_repository *repo;
2082 static const struct got_error *
2083 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2085 const struct got_error *err = NULL;
2086 struct check_merge_ok_arg *a = arg;
2087 unsigned char status;
2088 struct stat sb;
2089 char *ondisk_path;
2091 /* Reject merges into a work tree with mixed base commits. */
2092 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2093 SHA1_DIGEST_LENGTH))
2094 return got_error(GOT_ERR_MIXED_COMMITS);
2096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2097 == -1)
2098 return got_error_from_errno("asprintf");
2100 /* Reject merges into a work tree with conflicted files. */
2101 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2102 if (err)
2103 return err;
2104 if (status == GOT_STATUS_CONFLICT)
2105 return got_error(GOT_ERR_CONFLICTS);
2107 return NULL;
2110 static const struct got_error *
2111 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2112 const char *fileindex_path, struct got_object_id *commit_id1,
2113 struct got_object_id *commit_id2, struct got_repository *repo,
2114 got_worktree_checkout_cb progress_cb, void *progress_arg,
2115 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2117 const struct got_error *err = NULL, *sync_err;
2118 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2119 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2120 struct merge_file_cb_arg arg;
2122 if (commit_id1) {
2123 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2124 worktree->path_prefix);
2125 if (err)
2126 goto done;
2128 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2129 if (err)
2130 goto done;
2133 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2134 worktree->path_prefix);
2135 if (err)
2136 goto done;
2138 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2139 if (err)
2140 goto done;
2142 arg.worktree = worktree;
2143 arg.fileindex = fileindex;
2144 arg.progress_cb = progress_cb;
2145 arg.progress_arg = progress_arg;
2146 arg.cancel_cb = cancel_cb;
2147 arg.cancel_arg = cancel_arg;
2148 arg.commit_id2 = commit_id2;
2149 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2150 sync_err = sync_fileindex(fileindex, fileindex_path);
2151 if (sync_err && err == NULL)
2152 err = sync_err;
2153 done:
2154 if (tree1)
2155 got_object_tree_close(tree1);
2156 if (tree2)
2157 got_object_tree_close(tree2);
2158 return err;
2161 const struct got_error *
2162 got_worktree_merge_files(struct got_worktree *worktree,
2163 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2164 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2165 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2167 const struct got_error *err, *unlockerr;
2168 char *fileindex_path = NULL;
2169 struct got_fileindex *fileindex = NULL;
2170 struct check_merge_ok_arg mok_arg;
2172 err = lock_worktree(worktree, LOCK_EX);
2173 if (err)
2174 return err;
2176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2177 if (err)
2178 goto done;
2180 mok_arg.worktree = worktree;
2181 mok_arg.repo = repo;
2182 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2183 &mok_arg);
2184 if (err)
2185 goto done;
2187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2188 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2189 done:
2190 if (fileindex)
2191 got_fileindex_free(fileindex);
2192 free(fileindex_path);
2193 unlockerr = lock_worktree(worktree, LOCK_SH);
2194 if (unlockerr && err == NULL)
2195 err = unlockerr;
2196 return err;
2199 struct diff_dir_cb_arg {
2200 struct got_fileindex *fileindex;
2201 struct got_worktree *worktree;
2202 const char *status_path;
2203 size_t status_path_len;
2204 struct got_repository *repo;
2205 got_worktree_status_cb status_cb;
2206 void *status_arg;
2207 got_worktree_cancel_cb cancel_cb;
2208 void *cancel_arg;
2211 static const struct got_error *
2212 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2213 got_worktree_status_cb status_cb, void *status_arg,
2214 struct got_repository *repo)
2216 const struct got_error *err = NULL;
2217 unsigned char status = GOT_STATUS_NO_CHANGE;
2218 unsigned char staged_status = get_staged_status(ie);
2219 struct stat sb;
2220 struct got_object_id blob_id, commit_id, staged_blob_id;
2221 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2222 struct got_object_id *staged_blob_idp = NULL;
2224 err = get_file_status(&status, &sb, ie, abspath, repo);
2225 if (err)
2226 return err;
2228 if (status == GOT_STATUS_NO_CHANGE &&
2229 staged_status == GOT_STATUS_NO_CHANGE)
2230 return NULL;
2232 if (got_fileindex_entry_has_blob(ie)) {
2233 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2234 blob_idp = &blob_id;
2236 if (got_fileindex_entry_has_commit(ie)) {
2237 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2238 commit_idp = &commit_id;
2240 if (staged_status == GOT_STATUS_ADD ||
2241 staged_status == GOT_STATUS_MODIFY) {
2242 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2243 SHA1_DIGEST_LENGTH);
2244 staged_blob_idp = &staged_blob_id;
2247 return (*status_cb)(status_arg, status, staged_status,
2248 ie->path, blob_idp, staged_blob_idp, commit_idp);
2251 static const struct got_error *
2252 status_old_new(void *arg, struct got_fileindex_entry *ie,
2253 struct dirent *de, const char *parent_path)
2255 const struct got_error *err = NULL;
2256 struct diff_dir_cb_arg *a = arg;
2257 char *abspath;
2259 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2260 return got_error(GOT_ERR_CANCELLED);
2262 if (got_path_cmp(parent_path, a->status_path,
2263 strlen(parent_path), a->status_path_len) != 0 &&
2264 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2265 return NULL;
2267 if (parent_path[0]) {
2268 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2269 parent_path, de->d_name) == -1)
2270 return got_error_from_errno("asprintf");
2271 } else {
2272 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2273 de->d_name) == -1)
2274 return got_error_from_errno("asprintf");
2277 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2278 a->repo);
2279 free(abspath);
2280 return err;
2283 static const struct got_error *
2284 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2286 struct diff_dir_cb_arg *a = arg;
2287 struct got_object_id blob_id, commit_id;
2288 unsigned char status;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2294 return NULL;
2296 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2297 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2298 if (got_fileindex_entry_has_file_on_disk(ie))
2299 status = GOT_STATUS_MISSING;
2300 else
2301 status = GOT_STATUS_DELETE;
2302 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2303 ie->path, &blob_id, NULL, &commit_id);
2306 static const struct got_error *
2307 status_new(void *arg, struct dirent *de, const char *parent_path)
2309 const struct got_error *err = NULL;
2310 struct diff_dir_cb_arg *a = arg;
2311 char *path = NULL;
2313 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2314 return got_error(GOT_ERR_CANCELLED);
2316 if (de->d_type == DT_DIR)
2317 return NULL;
2319 /* XXX ignore symlinks for now */
2320 if (de->d_type == DT_LNK)
2321 return NULL;
2323 if (parent_path[0]) {
2324 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2325 return got_error_from_errno("asprintf");
2326 } else {
2327 path = de->d_name;
2330 if (got_path_is_child(path, a->status_path, a->status_path_len))
2331 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2332 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2333 if (parent_path[0])
2334 free(path);
2335 return err;
2338 static const struct got_error *
2339 report_single_file_status(const char *path, const char *ondisk_path,
2340 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2341 void *status_arg, struct got_repository *repo)
2343 struct got_fileindex_entry *ie;
2344 struct stat sb;
2346 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2347 if (ie)
2348 return report_file_status(ie, ondisk_path, status_cb,
2349 status_arg, repo);
2351 if (lstat(ondisk_path, &sb) == -1) {
2352 if (errno != ENOENT)
2353 return got_error_from_errno2("lstat", ondisk_path);
2354 return NULL;
2357 if (S_ISREG(sb.st_mode))
2358 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2359 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2361 return NULL;
2364 static const struct got_error *
2365 worktree_status(struct got_worktree *worktree, const char *path,
2366 struct got_fileindex *fileindex, struct got_repository *repo,
2367 got_worktree_status_cb status_cb, void *status_arg,
2368 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2370 const struct got_error *err = NULL;
2371 DIR *workdir = NULL;
2372 struct got_fileindex_diff_dir_cb fdiff_cb;
2373 struct diff_dir_cb_arg arg;
2374 char *ondisk_path = NULL;
2376 if (asprintf(&ondisk_path, "%s%s%s",
2377 worktree->root_path, path[0] ? "/" : "", path) == -1)
2378 return got_error_from_errno("asprintf");
2380 workdir = opendir(ondisk_path);
2381 if (workdir == NULL) {
2382 if (errno != ENOTDIR && errno != ENOENT)
2383 err = got_error_from_errno2("opendir", ondisk_path);
2384 else
2385 err = report_single_file_status(path, ondisk_path,
2386 fileindex, status_cb, status_arg, repo);
2387 } else {
2388 fdiff_cb.diff_old_new = status_old_new;
2389 fdiff_cb.diff_old = status_old;
2390 fdiff_cb.diff_new = status_new;
2391 arg.fileindex = fileindex;
2392 arg.worktree = worktree;
2393 arg.status_path = path;
2394 arg.status_path_len = strlen(path);
2395 arg.repo = repo;
2396 arg.status_cb = status_cb;
2397 arg.status_arg = status_arg;
2398 arg.cancel_cb = cancel_cb;
2399 arg.cancel_arg = cancel_arg;
2400 err = got_fileindex_diff_dir(fileindex, workdir,
2401 worktree->root_path, path, repo, &fdiff_cb, &arg);
2404 if (workdir)
2405 closedir(workdir);
2406 free(ondisk_path);
2407 return err;
2410 const struct got_error *
2411 got_worktree_status(struct got_worktree *worktree,
2412 struct got_pathlist_head *paths, struct got_repository *repo,
2413 got_worktree_status_cb status_cb, void *status_arg,
2414 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2416 const struct got_error *err = NULL;
2417 char *fileindex_path = NULL;
2418 struct got_fileindex *fileindex = NULL;
2419 struct got_pathlist_entry *pe;
2421 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2422 if (err)
2423 return err;
2425 TAILQ_FOREACH(pe, paths, entry) {
2426 err = worktree_status(worktree, pe->path, fileindex, repo,
2427 status_cb, status_arg, cancel_cb, cancel_arg);
2428 if (err)
2429 break;
2431 free(fileindex_path);
2432 got_fileindex_free(fileindex);
2433 return err;
2436 const struct got_error *
2437 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2438 const char *arg)
2440 const struct got_error *err = NULL;
2441 char *resolved, *cwd = NULL, *path = NULL;
2442 size_t len;
2444 *wt_path = NULL;
2446 resolved = realpath(arg, NULL);
2447 if (resolved == NULL) {
2448 if (errno != ENOENT)
2449 return got_error_from_errno2("realpath", arg);
2450 cwd = getcwd(NULL, 0);
2451 if (cwd == NULL)
2452 return got_error_from_errno("getcwd");
2453 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2454 err = got_error_from_errno("asprintf");
2455 goto done;
2459 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2460 strlen(got_worktree_get_root_path(worktree)))) {
2461 err = got_error(GOT_ERR_BAD_PATH);
2462 goto done;
2465 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2466 err = got_path_skip_common_ancestor(&path,
2467 got_worktree_get_root_path(worktree), resolved);
2468 if (err)
2469 goto done;
2470 } else {
2471 path = strdup("");
2472 if (path == NULL) {
2473 err = got_error_from_errno("strdup");
2474 goto done;
2478 /* XXX status walk can't deal with trailing slash! */
2479 len = strlen(path);
2480 while (len > 0 && path[len - 1] == '/') {
2481 path[len - 1] = '\0';
2482 len--;
2484 done:
2485 free(resolved);
2486 free(cwd);
2487 if (err == NULL)
2488 *wt_path = path;
2489 else
2490 free(path);
2491 return err;
2494 static const struct got_error *
2495 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2496 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2497 struct got_repository *repo)
2499 const struct got_error *err = NULL;
2500 struct got_fileindex_entry *ie;
2501 unsigned char status;
2502 struct stat sb;
2504 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2505 if (ie) {
2506 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2507 if (err)
2508 return err;
2509 /* Re-adding an existing entry is a no-op. */
2510 if (status == GOT_STATUS_ADD)
2511 return NULL;
2512 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2515 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2516 if (err)
2517 return err;
2519 err = got_fileindex_entry_add(fileindex, ie);
2520 if (err) {
2521 got_fileindex_entry_free(ie);
2522 return err;
2525 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2528 const struct got_error *
2529 got_worktree_schedule_add(struct got_worktree *worktree,
2530 struct got_pathlist_head *paths,
2531 got_worktree_status_cb status_cb, void *status_arg,
2532 struct got_repository *repo)
2534 struct got_fileindex *fileindex = NULL;
2535 char *fileindex_path = NULL;
2536 const struct got_error *err = NULL, *sync_err, *unlockerr;
2537 struct got_pathlist_entry *pe;
2539 err = lock_worktree(worktree, LOCK_EX);
2540 if (err)
2541 return err;
2543 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2544 if (err)
2545 goto done;
2547 TAILQ_FOREACH(pe, paths, entry) {
2548 char *ondisk_path;
2549 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2550 pe->path) == -1)
2551 return got_error_from_errno("asprintf");
2552 err = schedule_addition(ondisk_path, fileindex, pe->path,
2553 status_cb, status_arg, repo);
2554 free(ondisk_path);
2555 if (err)
2556 break;
2558 sync_err = sync_fileindex(fileindex, fileindex_path);
2559 if (sync_err && err == NULL)
2560 err = sync_err;
2561 done:
2562 free(fileindex_path);
2563 if (fileindex)
2564 got_fileindex_free(fileindex);
2565 unlockerr = lock_worktree(worktree, LOCK_SH);
2566 if (unlockerr && err == NULL)
2567 err = unlockerr;
2568 return err;
2571 static const struct got_error *
2572 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2573 const char *relpath, int delete_local_mods,
2574 got_worktree_status_cb status_cb, void *status_arg,
2575 struct got_repository *repo)
2577 const struct got_error *err = NULL;
2578 struct got_fileindex_entry *ie = NULL;
2579 unsigned char status, staged_status;
2580 struct stat sb;
2582 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2583 if (ie == NULL)
2584 return got_error(GOT_ERR_BAD_PATH);
2586 staged_status = get_staged_status(ie);
2587 if (staged_status != GOT_STATUS_NO_CHANGE) {
2588 if (staged_status == GOT_STATUS_DELETE)
2589 return NULL;
2590 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2593 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2594 if (err)
2595 return err;
2597 if (status != GOT_STATUS_NO_CHANGE) {
2598 if (status == GOT_STATUS_DELETE)
2599 return NULL;
2600 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2601 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2602 if (status != GOT_STATUS_MODIFY &&
2603 status != GOT_STATUS_MISSING)
2604 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2607 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2608 return got_error_from_errno2("unlink", ondisk_path);
2610 got_fileindex_entry_mark_deleted_from_disk(ie);
2611 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2614 const struct got_error *
2615 got_worktree_schedule_delete(struct got_worktree *worktree,
2616 struct got_pathlist_head *paths, int delete_local_mods,
2617 got_worktree_status_cb status_cb, void *status_arg,
2618 struct got_repository *repo)
2620 struct got_fileindex *fileindex = NULL;
2621 char *fileindex_path = NULL;
2622 const struct got_error *err = NULL, *sync_err, *unlockerr;
2623 struct got_pathlist_entry *pe;
2625 err = lock_worktree(worktree, LOCK_EX);
2626 if (err)
2627 return err;
2629 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2630 if (err)
2631 goto done;
2633 TAILQ_FOREACH(pe, paths, entry) {
2634 char *ondisk_path;
2635 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2636 pe->path) == -1)
2637 return got_error_from_errno("asprintf");
2638 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2639 delete_local_mods, status_cb, status_arg, repo);
2640 free(ondisk_path);
2641 if (err)
2642 break;
2644 sync_err = sync_fileindex(fileindex, fileindex_path);
2645 if (sync_err && err == NULL)
2646 err = sync_err;
2647 done:
2648 free(fileindex_path);
2649 if (fileindex)
2650 got_fileindex_free(fileindex);
2651 unlockerr = lock_worktree(worktree, LOCK_SH);
2652 if (unlockerr && err == NULL)
2653 err = unlockerr;
2654 return err;
2657 static const struct got_error *
2658 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2659 const char *ondisk_path,
2660 got_worktree_checkout_cb progress_cb, void *progress_arg,
2661 struct got_repository *repo)
2663 const struct got_error *err = NULL;
2664 char *relpath = NULL, *parent_path = NULL;
2665 struct got_fileindex_entry *ie;
2666 struct got_tree_object *tree = NULL;
2667 struct got_object_id *tree_id = NULL;
2668 const struct got_tree_entry *te = NULL;
2669 char *tree_path = NULL, *te_name;
2670 struct got_blob_object *blob = NULL;
2671 unsigned char status, staged_status;
2672 struct stat sb;
2674 err = got_path_skip_common_ancestor(&relpath,
2675 got_worktree_get_root_path(worktree), ondisk_path);
2676 if (err)
2677 goto done;
2679 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2680 if (ie == NULL) {
2681 err = got_error(GOT_ERR_BAD_PATH);
2682 goto done;
2685 /* Construct in-repository path of tree which contains this blob. */
2686 err = got_path_dirname(&parent_path, ie->path);
2687 if (err) {
2688 if (err->code != GOT_ERR_BAD_PATH)
2689 goto done;
2690 parent_path = strdup("/");
2691 if (parent_path == NULL) {
2692 err = got_error_from_errno("strdup");
2693 goto done;
2696 if (got_path_is_root_dir(worktree->path_prefix)) {
2697 tree_path = strdup(parent_path);
2698 if (tree_path == NULL) {
2699 err = got_error_from_errno("strdup");
2700 goto done;
2702 } else {
2703 if (got_path_is_root_dir(parent_path)) {
2704 tree_path = strdup(worktree->path_prefix);
2705 if (tree_path == NULL) {
2706 err = got_error_from_errno("strdup");
2707 goto done;
2709 } else {
2710 if (asprintf(&tree_path, "%s/%s",
2711 worktree->path_prefix, parent_path) == -1) {
2712 err = got_error_from_errno("asprintf");
2713 goto done;
2718 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2719 if (err)
2720 goto done;
2721 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2722 sb.st_mode = got_fileindex_perms_to_st(ie);
2724 staged_status = get_staged_status(ie);
2725 if (status == GOT_STATUS_DELETE &&
2726 staged_status != GOT_STATUS_NO_CHANGE) {
2727 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2728 goto done;
2731 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2732 tree_path);
2733 if (err) {
2734 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2735 (status == GOT_STATUS_ADD ||
2736 staged_status == GOT_STATUS_ADD)))
2737 goto done;
2738 } else {
2739 err = got_object_open_as_tree(&tree, repo, tree_id);
2740 if (err)
2741 goto done;
2743 te_name = basename(ie->path);
2744 if (te_name == NULL) {
2745 err = got_error_from_errno2("basename", ie->path);
2746 goto done;
2749 te = got_object_tree_find_entry(tree, te_name);
2750 if (te == NULL && status != GOT_STATUS_ADD &&
2751 staged_status != GOT_STATUS_ADD) {
2752 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2753 goto done;
2757 switch (status) {
2758 case GOT_STATUS_ADD:
2759 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2760 if (err)
2761 goto done;
2762 got_fileindex_entry_remove(fileindex, ie);
2763 break;
2764 case GOT_STATUS_DELETE:
2765 case GOT_STATUS_MODIFY:
2766 case GOT_STATUS_CONFLICT:
2767 case GOT_STATUS_MISSING: {
2768 struct got_object_id id;
2769 if (staged_status == GOT_STATUS_ADD ||
2770 staged_status == GOT_STATUS_MODIFY) {
2771 memcpy(id.sha1, ie->staged_blob_sha1,
2772 SHA1_DIGEST_LENGTH);
2773 } else
2774 memcpy(id.sha1, ie->blob_sha1,
2775 SHA1_DIGEST_LENGTH);
2776 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2777 if (err)
2778 goto done;
2779 err = install_blob(worktree, ondisk_path, ie->path,
2780 te ? te->mode : GOT_DEFAULT_FILE_MODE, sb.st_mode,
2781 blob, 0, 1, repo, progress_cb, progress_arg);
2782 if (err)
2783 goto done;
2784 if (status == GOT_STATUS_DELETE) {
2785 err = update_blob_fileindex_entry(worktree,
2786 fileindex, ie, ondisk_path, ie->path, blob, 1);
2787 if (err)
2788 goto done;
2790 break;
2792 default:
2793 goto done;
2795 done:
2796 free(relpath);
2797 free(parent_path);
2798 free(tree_path);
2799 if (blob)
2800 got_object_blob_close(blob);
2801 if (tree)
2802 got_object_tree_close(tree);
2803 free(tree_id);
2804 return err;
2807 const struct got_error *
2808 got_worktree_revert(struct got_worktree *worktree,
2809 struct got_pathlist_head *ondisk_paths,
2810 got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 struct got_repository *repo)
2813 struct got_fileindex *fileindex = NULL;
2814 char *fileindex_path = NULL;
2815 const struct got_error *err = NULL, *unlockerr = NULL;
2816 const struct got_error *sync_err = NULL;
2817 struct got_pathlist_entry *pe;
2819 err = lock_worktree(worktree, LOCK_EX);
2820 if (err)
2821 return err;
2823 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2824 if (err)
2825 goto done;
2827 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2828 char *ondisk_path;
2829 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2830 pe->path) == -1)
2831 return got_error_from_errno("asprintf");
2832 err = revert_file(worktree, fileindex, ondisk_path,
2833 progress_cb, progress_arg, repo);
2834 free(ondisk_path);
2835 if (err)
2836 break;
2838 sync_err = sync_fileindex(fileindex, fileindex_path);
2839 if (sync_err && err == NULL)
2840 err = sync_err;
2841 done:
2842 free(fileindex_path);
2843 if (fileindex)
2844 got_fileindex_free(fileindex);
2845 unlockerr = lock_worktree(worktree, LOCK_SH);
2846 if (unlockerr && err == NULL)
2847 err = unlockerr;
2848 return err;
2851 static void
2852 free_commitable(struct got_commitable *ct)
2854 free(ct->path);
2855 free(ct->in_repo_path);
2856 free(ct->ondisk_path);
2857 free(ct->blob_id);
2858 free(ct->base_blob_id);
2859 free(ct->staged_blob_id);
2860 free(ct->base_commit_id);
2861 free(ct);
2864 struct collect_commitables_arg {
2865 struct got_pathlist_head *commitable_paths;
2866 struct got_repository *repo;
2867 struct got_worktree *worktree;
2868 int have_staged_files;
2871 static const struct got_error *
2872 collect_commitables(void *arg, unsigned char status,
2873 unsigned char staged_status, const char *relpath,
2874 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2875 struct got_object_id *commit_id)
2877 struct collect_commitables_arg *a = arg;
2878 const struct got_error *err = NULL;
2879 struct got_commitable *ct = NULL;
2880 struct got_pathlist_entry *new = NULL;
2881 char *parent_path = NULL, *path = NULL;
2882 struct stat sb;
2884 if (a->have_staged_files) {
2885 if (staged_status != GOT_STATUS_MODIFY &&
2886 staged_status != GOT_STATUS_ADD &&
2887 staged_status != GOT_STATUS_DELETE)
2888 return NULL;
2889 } else {
2890 if (status == GOT_STATUS_CONFLICT)
2891 return got_error(GOT_ERR_COMMIT_CONFLICT);
2893 if (status != GOT_STATUS_MODIFY &&
2894 status != GOT_STATUS_ADD &&
2895 status != GOT_STATUS_DELETE)
2896 return NULL;
2899 if (asprintf(&path, "/%s", relpath) == -1) {
2900 err = got_error_from_errno("asprintf");
2901 goto done;
2903 if (strcmp(path, "/") == 0) {
2904 parent_path = strdup("");
2905 if (parent_path == NULL)
2906 return got_error_from_errno("strdup");
2907 } else {
2908 err = got_path_dirname(&parent_path, path);
2909 if (err)
2910 return err;
2913 ct = calloc(1, sizeof(*ct));
2914 if (ct == NULL) {
2915 err = got_error_from_errno("calloc");
2916 goto done;
2919 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2920 relpath) == -1) {
2921 err = got_error_from_errno("asprintf");
2922 goto done;
2924 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
2925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2926 } else {
2927 if (lstat(ct->ondisk_path, &sb) != 0) {
2928 err = got_error_from_errno2("lstat", ct->ondisk_path);
2929 goto done;
2931 ct->mode = sb.st_mode;
2934 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2935 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2936 relpath) == -1) {
2937 err = got_error_from_errno("asprintf");
2938 goto done;
2941 ct->status = status;
2942 ct->staged_status = staged_status;
2943 ct->blob_id = NULL; /* will be filled in when blob gets created */
2944 if (ct->status != GOT_STATUS_ADD &&
2945 ct->staged_status != GOT_STATUS_ADD) {
2946 ct->base_blob_id = got_object_id_dup(blob_id);
2947 if (ct->base_blob_id == NULL) {
2948 err = got_error_from_errno("got_object_id_dup");
2949 goto done;
2951 ct->base_commit_id = got_object_id_dup(commit_id);
2952 if (ct->base_commit_id == NULL) {
2953 err = got_error_from_errno("got_object_id_dup");
2954 goto done;
2957 if (ct->staged_status == GOT_STATUS_ADD ||
2958 ct->staged_status == GOT_STATUS_MODIFY) {
2959 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
2960 if (ct->staged_blob_id == NULL) {
2961 err = got_error_from_errno("got_object_id_dup");
2962 goto done;
2965 ct->path = strdup(path);
2966 if (ct->path == NULL) {
2967 err = got_error_from_errno("strdup");
2968 goto done;
2970 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2971 done:
2972 if (ct && (err || new == NULL))
2973 free_commitable(ct);
2974 free(parent_path);
2975 free(path);
2976 return err;
2979 static const struct got_error *write_tree(struct got_object_id **,
2980 struct got_tree_object *, const char *, struct got_pathlist_head *,
2981 got_worktree_status_cb status_cb, void *status_arg,
2982 struct got_repository *);
2984 static const struct got_error *
2985 write_subtree(struct got_object_id **new_subtree_id,
2986 struct got_tree_entry *te, const char *parent_path,
2987 struct got_pathlist_head *commitable_paths,
2988 got_worktree_status_cb status_cb, void *status_arg,
2989 struct got_repository *repo)
2991 const struct got_error *err = NULL;
2992 struct got_tree_object *subtree;
2993 char *subpath;
2995 if (asprintf(&subpath, "%s%s%s", parent_path,
2996 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2997 return got_error_from_errno("asprintf");
2999 err = got_object_open_as_tree(&subtree, repo, te->id);
3000 if (err)
3001 return err;
3003 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3004 status_cb, status_arg, repo);
3005 got_object_tree_close(subtree);
3006 free(subpath);
3007 return err;
3010 static const struct got_error *
3011 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3013 const struct got_error *err = NULL;
3014 char *ct_parent_path = NULL;
3016 *match = 0;
3018 if (strchr(ct->in_repo_path, '/') == NULL) {
3019 *match = got_path_is_root_dir(path);
3020 return NULL;
3023 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3024 if (err)
3025 return err;
3026 *match = (strcmp(path, ct_parent_path) == 0);
3027 free(ct_parent_path);
3028 return err;
3031 static mode_t
3032 get_ct_file_mode(struct got_commitable *ct)
3034 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3037 static const struct got_error *
3038 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3039 struct got_tree_entry *te, struct got_commitable *ct)
3041 const struct got_error *err = NULL;
3043 *new_te = NULL;
3045 err = got_object_tree_entry_dup(new_te, te);
3046 if (err)
3047 goto done;
3049 (*new_te)->mode = get_ct_file_mode(ct);
3051 free((*new_te)->id);
3052 if (ct->staged_status == GOT_STATUS_MODIFY)
3053 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3054 else
3055 (*new_te)->id = got_object_id_dup(ct->blob_id);
3056 if ((*new_te)->id == NULL) {
3057 err = got_error_from_errno("got_object_id_dup");
3058 goto done;
3060 done:
3061 if (err && *new_te) {
3062 got_object_tree_entry_close(*new_te);
3063 *new_te = NULL;
3065 return err;
3068 static const struct got_error *
3069 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3070 struct got_commitable *ct)
3072 const struct got_error *err = NULL;
3073 char *ct_name;
3075 *new_te = NULL;
3077 *new_te = calloc(1, sizeof(**new_te));
3078 if (*new_te == NULL)
3079 return got_error_from_errno("calloc");
3081 ct_name = basename(ct->path);
3082 if (ct_name == NULL) {
3083 err = got_error_from_errno2("basename", ct->path);
3084 goto done;
3086 (*new_te)->name = strdup(ct_name);
3087 if ((*new_te)->name == NULL) {
3088 err = got_error_from_errno("strdup");
3089 goto done;
3092 (*new_te)->mode = get_ct_file_mode(ct);
3094 if (ct->staged_status == GOT_STATUS_ADD)
3095 (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
3096 else
3097 (*new_te)->id = got_object_id_dup(ct->blob_id);
3098 if ((*new_te)->id == NULL) {
3099 err = got_error_from_errno("got_object_id_dup");
3100 goto done;
3102 done:
3103 if (err && *new_te) {
3104 got_object_tree_entry_close(*new_te);
3105 *new_te = NULL;
3107 return err;
3110 static const struct got_error *
3111 insert_tree_entry(struct got_tree_entry *new_te,
3112 struct got_pathlist_head *paths)
3114 const struct got_error *err = NULL;
3115 struct got_pathlist_entry *new_pe;
3117 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3118 if (err)
3119 return err;
3120 if (new_pe == NULL)
3121 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3122 return NULL;
3125 static const struct got_error *
3126 report_ct_status(struct got_commitable *ct,
3127 got_worktree_status_cb status_cb, void *status_arg)
3129 const char *ct_path = ct->path;
3130 unsigned char status;
3132 while (ct_path[0] == '/')
3133 ct_path++;
3135 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3136 status = ct->staged_status;
3137 else
3138 status = ct->status;
3140 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3141 ct_path, ct->blob_id, NULL, NULL);
3144 static const struct got_error *
3145 match_modified_subtree(int *modified, struct got_tree_entry *te,
3146 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3148 const struct got_error *err = NULL;
3149 struct got_pathlist_entry *pe;
3150 char *te_path;
3152 *modified = 0;
3154 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3155 got_path_is_root_dir(base_tree_path) ? "" : "/",
3156 te->name) == -1)
3157 return got_error_from_errno("asprintf");
3159 TAILQ_FOREACH(pe, commitable_paths, entry) {
3160 struct got_commitable *ct = pe->data;
3161 *modified = got_path_is_child(ct->in_repo_path, te_path,
3162 strlen(te_path));
3163 if (*modified)
3164 break;
3167 free(te_path);
3168 return err;
3171 static const struct got_error *
3172 match_deleted_or_modified_ct(struct got_commitable **ctp,
3173 struct got_tree_entry *te, const char *base_tree_path,
3174 struct got_pathlist_head *commitable_paths)
3176 const struct got_error *err = NULL;
3177 struct got_pathlist_entry *pe;
3179 *ctp = NULL;
3181 TAILQ_FOREACH(pe, commitable_paths, entry) {
3182 struct got_commitable *ct = pe->data;
3183 char *ct_name = NULL;
3184 int path_matches;
3186 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3187 if (ct->status != GOT_STATUS_MODIFY &&
3188 ct->status != GOT_STATUS_DELETE)
3189 continue;
3190 } else {
3191 if (ct->staged_status != GOT_STATUS_MODIFY &&
3192 ct->staged_status != GOT_STATUS_DELETE)
3193 continue;
3196 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3197 continue;
3199 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3200 if (err)
3201 return err;
3202 if (!path_matches)
3203 continue;
3205 ct_name = basename(pe->path);
3206 if (ct_name == NULL)
3207 return got_error_from_errno2("basename", pe->path);
3209 if (strcmp(te->name, ct_name) != 0)
3210 continue;
3212 *ctp = ct;
3213 break;
3216 return err;
3219 static const struct got_error *
3220 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3221 const char *child_path, const char *path_base_tree,
3222 struct got_pathlist_head *commitable_paths,
3223 got_worktree_status_cb status_cb, void *status_arg,
3224 struct got_repository *repo)
3226 const struct got_error *err = NULL;
3227 struct got_tree_entry *new_te;
3228 char *subtree_path;
3230 *new_tep = NULL;
3232 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3233 got_path_is_root_dir(path_base_tree) ? "" : "/",
3234 child_path) == -1)
3235 return got_error_from_errno("asprintf");
3237 new_te = calloc(1, sizeof(*new_te));
3238 new_te->mode = S_IFDIR;
3239 new_te->name = strdup(child_path);
3240 if (new_te->name == NULL) {
3241 err = got_error_from_errno("strdup");
3242 got_object_tree_entry_close(new_te);
3243 goto done;
3245 err = write_tree(&new_te->id, NULL, subtree_path,
3246 commitable_paths, status_cb, status_arg, repo);
3247 if (err) {
3248 got_object_tree_entry_close(new_te);
3249 goto done;
3251 done:
3252 free(subtree_path);
3253 if (err == NULL)
3254 *new_tep = new_te;
3255 return err;
3258 static const struct got_error *
3259 write_tree(struct got_object_id **new_tree_id,
3260 struct got_tree_object *base_tree, const char *path_base_tree,
3261 struct got_pathlist_head *commitable_paths,
3262 got_worktree_status_cb status_cb, void *status_arg,
3263 struct got_repository *repo)
3265 const struct got_error *err = NULL;
3266 const struct got_tree_entries *base_entries = NULL;
3267 struct got_pathlist_head paths;
3268 struct got_tree_entries new_tree_entries;
3269 struct got_tree_entry *te, *new_te = NULL;
3270 struct got_pathlist_entry *pe;
3272 TAILQ_INIT(&paths);
3273 new_tree_entries.nentries = 0;
3274 SIMPLEQ_INIT(&new_tree_entries.head);
3276 /* Insert, and recurse into, newly added entries first. */
3277 TAILQ_FOREACH(pe, commitable_paths, entry) {
3278 struct got_commitable *ct = pe->data;
3279 char *child_path = NULL, *slash;
3281 if ((ct->status != GOT_STATUS_ADD &&
3282 ct->staged_status != GOT_STATUS_ADD) ||
3283 (ct->flags & GOT_COMMITABLE_ADDED))
3284 continue;
3286 if (!got_path_is_child(pe->path, path_base_tree,
3287 strlen(path_base_tree)))
3288 continue;
3290 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3291 pe->path);
3292 if (err)
3293 goto done;
3295 slash = strchr(child_path, '/');
3296 if (slash == NULL) {
3297 err = alloc_added_blob_tree_entry(&new_te, ct);
3298 if (err)
3299 goto done;
3300 err = report_ct_status(ct, status_cb, status_arg);
3301 if (err)
3302 goto done;
3303 ct->flags |= GOT_COMMITABLE_ADDED;
3304 err = insert_tree_entry(new_te, &paths);
3305 if (err)
3306 goto done;
3307 } else {
3308 *slash = '\0'; /* trim trailing path components */
3309 if (base_tree == NULL ||
3310 got_object_tree_find_entry(base_tree, child_path)
3311 == NULL) {
3312 err = make_subtree_for_added_blob(&new_te,
3313 child_path, path_base_tree,
3314 commitable_paths, status_cb, status_arg,
3315 repo);
3316 if (err)
3317 goto done;
3318 err = insert_tree_entry(new_te, &paths);
3319 if (err)
3320 goto done;
3325 if (base_tree) {
3326 /* Handle modified and deleted entries. */
3327 base_entries = got_object_tree_get_entries(base_tree);
3328 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3329 struct got_commitable *ct = NULL;
3331 if (S_ISDIR(te->mode)) {
3332 int modified;
3333 err = got_object_tree_entry_dup(&new_te, te);
3334 if (err)
3335 goto done;
3336 err = match_modified_subtree(&modified, te,
3337 path_base_tree, commitable_paths);
3338 if (err)
3339 goto done;
3340 /* Avoid recursion into unmodified subtrees. */
3341 if (modified) {
3342 free(new_te->id);
3343 err = write_subtree(&new_te->id, te,
3344 path_base_tree, commitable_paths,
3345 status_cb, status_arg, repo);
3346 if (err)
3347 goto done;
3349 err = insert_tree_entry(new_te, &paths);
3350 if (err)
3351 goto done;
3352 continue;
3355 err = match_deleted_or_modified_ct(&ct, te,
3356 path_base_tree, commitable_paths);
3357 if (ct) {
3358 /* NB: Deleted entries get dropped here. */
3359 if (ct->status == GOT_STATUS_MODIFY ||
3360 ct->staged_status == GOT_STATUS_MODIFY) {
3361 err = alloc_modified_blob_tree_entry(
3362 &new_te, te, ct);
3363 if (err)
3364 goto done;
3365 err = insert_tree_entry(new_te, &paths);
3366 if (err)
3367 goto done;
3369 err = report_ct_status(ct, status_cb,
3370 status_arg);
3371 if (err)
3372 goto done;
3373 } else {
3374 /* Entry is unchanged; just copy it. */
3375 err = got_object_tree_entry_dup(&new_te, te);
3376 if (err)
3377 goto done;
3378 err = insert_tree_entry(new_te, &paths);
3379 if (err)
3380 goto done;
3385 /* Write new list of entries; deleted entries have been dropped. */
3386 TAILQ_FOREACH(pe, &paths, entry) {
3387 struct got_tree_entry *te = pe->data;
3388 new_tree_entries.nentries++;
3389 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3391 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3392 done:
3393 got_object_tree_entries_close(&new_tree_entries);
3394 got_pathlist_free(&paths);
3395 return err;
3398 static const struct got_error *
3399 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3400 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3402 const struct got_error *err = NULL;
3403 struct got_pathlist_entry *pe;
3405 TAILQ_FOREACH(pe, commitable_paths, entry) {
3406 struct got_fileindex_entry *ie;
3407 struct got_commitable *ct = pe->data;
3409 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3410 if (ie) {
3411 if (ct->status == GOT_STATUS_DELETE ||
3412 ct->staged_status == GOT_STATUS_DELETE) {
3413 got_fileindex_entry_remove(fileindex, ie);
3414 got_fileindex_entry_free(ie);
3415 } else if (ct->staged_status == GOT_STATUS_ADD ||
3416 ct->staged_status == GOT_STATUS_MODIFY) {
3417 got_fileindex_entry_stage_set(ie,
3418 GOT_FILEIDX_STAGE_NONE);
3419 err = got_fileindex_entry_update(ie,
3420 ct->ondisk_path, ct->staged_blob_id->sha1,
3421 new_base_commit_id->sha1, 1);
3422 } else
3423 err = got_fileindex_entry_update(ie,
3424 ct->ondisk_path, ct->blob_id->sha1,
3425 new_base_commit_id->sha1, 1);
3426 } else {
3427 err = got_fileindex_entry_alloc(&ie,
3428 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3429 new_base_commit_id->sha1);
3430 if (err)
3431 break;
3432 err = got_fileindex_entry_add(fileindex, ie);
3433 if (err)
3434 break;
3437 return err;
3441 static const struct got_error *
3442 check_out_of_date(const char *in_repo_path, unsigned char status,
3443 unsigned char staged_status, struct got_object_id *base_blob_id,
3444 struct got_object_id *base_commit_id,
3445 struct got_object_id *head_commit_id, struct got_repository *repo,
3446 int ood_errcode)
3448 const struct got_error *err = NULL;
3449 struct got_object_id *id = NULL;
3451 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
3452 /* Trivial case: base commit == head commit */
3453 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
3454 return NULL;
3456 * Ensure file content which local changes were based
3457 * on matches file content in the branch head.
3459 err = got_object_id_by_path(&id, repo, head_commit_id,
3460 in_repo_path);
3461 if (err) {
3462 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3463 goto done;
3464 err = got_error(ood_errcode);
3465 goto done;
3466 } else if (got_object_id_cmp(id, base_blob_id) != 0)
3467 err = got_error(ood_errcode);
3468 } else {
3469 /* Require that added files don't exist in the branch head. */
3470 err = got_object_id_by_path(&id, repo, head_commit_id,
3471 in_repo_path);
3472 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3473 goto done;
3474 err = id ? got_error(ood_errcode) : NULL;
3476 done:
3477 free(id);
3478 return err;
3481 const struct got_error *
3482 commit_worktree(struct got_object_id **new_commit_id,
3483 struct got_pathlist_head *commitable_paths,
3484 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3485 const char *author, const char *committer,
3486 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3487 got_worktree_status_cb status_cb, void *status_arg,
3488 struct got_repository *repo)
3490 const struct got_error *err = NULL, *unlockerr = NULL;
3491 struct got_pathlist_entry *pe;
3492 const char *head_ref_name = NULL;
3493 struct got_commit_object *head_commit = NULL;
3494 struct got_reference *head_ref2 = NULL;
3495 struct got_object_id *head_commit_id2 = NULL;
3496 struct got_tree_object *head_tree = NULL;
3497 struct got_object_id *new_tree_id = NULL;
3498 struct got_object_id_queue parent_ids;
3499 struct got_object_qid *pid = NULL;
3500 char *logmsg = NULL;
3502 *new_commit_id = NULL;
3504 SIMPLEQ_INIT(&parent_ids);
3506 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3507 if (err)
3508 goto done;
3510 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3511 if (err)
3512 goto done;
3514 if (commit_msg_cb != NULL) {
3515 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3516 if (err)
3517 goto done;
3520 if (logmsg == NULL || strlen(logmsg) == 0) {
3521 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3522 goto done;
3525 /* Create blobs from added and modified files and record their IDs. */
3526 TAILQ_FOREACH(pe, commitable_paths, entry) {
3527 struct got_commitable *ct = pe->data;
3528 char *ondisk_path;
3530 /* Blobs for staged files already exist. */
3531 if (ct->staged_status == GOT_STATUS_ADD ||
3532 ct->staged_status == GOT_STATUS_MODIFY)
3533 continue;
3535 if (ct->status != GOT_STATUS_ADD &&
3536 ct->status != GOT_STATUS_MODIFY)
3537 continue;
3539 if (asprintf(&ondisk_path, "%s/%s",
3540 worktree->root_path, pe->path) == -1) {
3541 err = got_error_from_errno("asprintf");
3542 goto done;
3544 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3545 free(ondisk_path);
3546 if (err)
3547 goto done;
3550 /* Recursively write new tree objects. */
3551 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3552 status_cb, status_arg, repo);
3553 if (err)
3554 goto done;
3556 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3557 if (err)
3558 goto done;
3559 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3560 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3561 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3562 got_object_qid_free(pid);
3563 if (logmsg != NULL)
3564 free(logmsg);
3565 if (err)
3566 goto done;
3568 /* Check if a concurrent commit to our branch has occurred. */
3569 head_ref_name = got_worktree_get_head_ref_name(worktree);
3570 if (head_ref_name == NULL) {
3571 err = got_error_from_errno("got_worktree_get_head_ref_name");
3572 goto done;
3574 /* Lock the reference here to prevent concurrent modification. */
3575 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3576 if (err)
3577 goto done;
3578 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3579 if (err)
3580 goto done;
3581 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3582 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3583 goto done;
3585 /* Update branch head in repository. */
3586 err = got_ref_change_ref(head_ref2, *new_commit_id);
3587 if (err)
3588 goto done;
3589 err = got_ref_write(head_ref2, repo);
3590 if (err)
3591 goto done;
3593 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3594 if (err)
3595 goto done;
3597 err = ref_base_commit(worktree, repo);
3598 if (err)
3599 goto done;
3600 done:
3601 if (head_tree)
3602 got_object_tree_close(head_tree);
3603 if (head_commit)
3604 got_object_commit_close(head_commit);
3605 free(head_commit_id2);
3606 if (head_ref2) {
3607 unlockerr = got_ref_unlock(head_ref2);
3608 if (unlockerr && err == NULL)
3609 err = unlockerr;
3610 got_ref_close(head_ref2);
3612 return err;
3615 static const struct got_error *
3616 check_path_is_commitable(const char *path,
3617 struct got_pathlist_head *commitable_paths)
3619 struct got_pathlist_entry *cpe = NULL;
3620 size_t path_len = strlen(path);
3622 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3623 struct got_commitable *ct = cpe->data;
3624 const char *ct_path = ct->path;
3626 while (ct_path[0] == '/')
3627 ct_path++;
3629 if (strcmp(path, ct_path) == 0 ||
3630 got_path_is_child(ct_path, path, path_len))
3631 break;
3634 if (cpe == NULL)
3635 return got_error_path(path, GOT_ERR_BAD_PATH);
3637 return NULL;
3640 static const struct got_error *
3641 check_staged_file(void *arg, struct got_fileindex_entry *ie)
3643 int *have_staged_files = arg;
3645 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
3646 *have_staged_files = 1;
3647 return got_error(GOT_ERR_CANCELLED);
3650 return NULL;
3653 static const struct got_error *
3654 check_non_staged_files(struct got_fileindex *fileindex,
3655 struct got_pathlist_head *paths)
3657 struct got_pathlist_entry *pe;
3658 struct got_fileindex_entry *ie;
3660 TAILQ_FOREACH(pe, paths, entry) {
3661 if (pe->path[0] == '\0')
3662 continue;
3663 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3664 if (ie == NULL)
3665 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
3666 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
3667 return got_error_path(pe->path,
3668 GOT_ERR_FILE_NOT_STAGED);
3671 return NULL;
3674 const struct got_error *
3675 got_worktree_commit(struct got_object_id **new_commit_id,
3676 struct got_worktree *worktree, struct got_pathlist_head *paths,
3677 const char *author, const char *committer,
3678 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3679 got_worktree_status_cb status_cb, void *status_arg,
3680 struct got_repository *repo)
3682 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3683 struct got_fileindex *fileindex = NULL;
3684 char *fileindex_path = NULL;
3685 struct got_pathlist_head commitable_paths;
3686 struct collect_commitables_arg cc_arg;
3687 struct got_pathlist_entry *pe;
3688 struct got_reference *head_ref = NULL;
3689 struct got_object_id *head_commit_id = NULL;
3690 int have_staged_files = 0;
3692 *new_commit_id = NULL;
3694 TAILQ_INIT(&commitable_paths);
3696 err = lock_worktree(worktree, LOCK_EX);
3697 if (err)
3698 goto done;
3700 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3701 if (err)
3702 goto done;
3704 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3705 if (err)
3706 goto done;
3708 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3709 if (err)
3710 goto done;
3712 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
3713 &have_staged_files);
3714 if (err && err->code != GOT_ERR_CANCELLED)
3715 goto done;
3716 if (have_staged_files) {
3717 err = check_non_staged_files(fileindex, paths);
3718 if (err)
3719 goto done;
3722 cc_arg.commitable_paths = &commitable_paths;
3723 cc_arg.worktree = worktree;
3724 cc_arg.repo = repo;
3725 cc_arg.have_staged_files = have_staged_files;
3726 TAILQ_FOREACH(pe, paths, entry) {
3727 err = worktree_status(worktree, pe->path, fileindex, repo,
3728 collect_commitables, &cc_arg, NULL, NULL);
3729 if (err)
3730 goto done;
3733 if (TAILQ_EMPTY(&commitable_paths)) {
3734 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3735 goto done;
3738 TAILQ_FOREACH(pe, paths, entry) {
3739 err = check_path_is_commitable(pe->path, &commitable_paths);
3740 if (err)
3741 goto done;
3744 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3745 struct got_commitable *ct = pe->data;
3746 const char *ct_path = ct->in_repo_path;
3748 while (ct_path[0] == '/')
3749 ct_path++;
3750 err = check_out_of_date(ct_path, ct->status,
3751 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
3752 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
3753 if (err)
3754 goto done;
3758 err = commit_worktree(new_commit_id, &commitable_paths,
3759 head_commit_id, worktree, author, committer,
3760 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3761 if (err)
3762 goto done;
3764 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3765 fileindex);
3766 sync_err = sync_fileindex(fileindex, fileindex_path);
3767 if (sync_err && err == NULL)
3768 err = sync_err;
3769 done:
3770 if (fileindex)
3771 got_fileindex_free(fileindex);
3772 free(fileindex_path);
3773 unlockerr = lock_worktree(worktree, LOCK_SH);
3774 if (unlockerr && err == NULL)
3775 err = unlockerr;
3776 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3777 struct got_commitable *ct = pe->data;
3778 free_commitable(ct);
3780 got_pathlist_free(&commitable_paths);
3781 return err;
3784 const char *
3785 got_commitable_get_path(struct got_commitable *ct)
3787 return ct->path;
3790 unsigned int
3791 got_commitable_get_status(struct got_commitable *ct)
3793 return ct->status;
3796 struct check_rebase_ok_arg {
3797 struct got_worktree *worktree;
3798 struct got_repository *repo;
3801 static const struct got_error *
3802 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3804 const struct got_error *err = NULL;
3805 struct check_rebase_ok_arg *a = arg;
3806 unsigned char status;
3807 struct stat sb;
3808 char *ondisk_path;
3810 /* Reject rebase of a work tree with mixed base commits. */
3811 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3812 SHA1_DIGEST_LENGTH))
3813 return got_error(GOT_ERR_MIXED_COMMITS);
3815 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3816 == -1)
3817 return got_error_from_errno("asprintf");
3819 /* Reject rebase of a work tree with modified or staged files. */
3820 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3821 free(ondisk_path);
3822 if (err)
3823 return err;
3825 if (status != GOT_STATUS_NO_CHANGE)
3826 return got_error(GOT_ERR_MODIFIED);
3827 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
3828 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
3830 return NULL;
3833 const struct got_error *
3834 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3835 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3836 struct got_worktree *worktree, struct got_reference *branch,
3837 struct got_repository *repo)
3839 const struct got_error *err = NULL;
3840 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3841 char *branch_ref_name = NULL;
3842 char *fileindex_path = NULL;
3843 struct check_rebase_ok_arg ok_arg;
3844 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3846 *new_base_branch_ref = NULL;
3847 *tmp_branch = NULL;
3848 *fileindex = NULL;
3850 err = lock_worktree(worktree, LOCK_EX);
3851 if (err)
3852 return err;
3854 err = open_fileindex(fileindex, &fileindex_path, worktree);
3855 if (err)
3856 goto done;
3858 ok_arg.worktree = worktree;
3859 ok_arg.repo = repo;
3860 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3861 &ok_arg);
3862 if (err)
3863 goto done;
3865 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3866 if (err)
3867 goto done;
3869 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3870 if (err)
3871 goto done;
3873 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3874 if (err)
3875 goto done;
3877 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3878 0);
3879 if (err)
3880 goto done;
3882 err = got_ref_alloc_symref(new_base_branch_ref,
3883 new_base_branch_ref_name, wt_branch);
3884 if (err)
3885 goto done;
3886 err = got_ref_write(*new_base_branch_ref, repo);
3887 if (err)
3888 goto done;
3890 /* TODO Lock original branch's ref while rebasing? */
3892 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3893 if (err)
3894 goto done;
3896 err = got_ref_write(branch_ref, repo);
3897 if (err)
3898 goto done;
3900 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3901 worktree->base_commit_id);
3902 if (err)
3903 goto done;
3904 err = got_ref_write(*tmp_branch, repo);
3905 if (err)
3906 goto done;
3908 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3909 if (err)
3910 goto done;
3911 done:
3912 free(fileindex_path);
3913 free(tmp_branch_name);
3914 free(new_base_branch_ref_name);
3915 free(branch_ref_name);
3916 if (branch_ref)
3917 got_ref_close(branch_ref);
3918 if (wt_branch)
3919 got_ref_close(wt_branch);
3920 if (err) {
3921 if (*new_base_branch_ref) {
3922 got_ref_close(*new_base_branch_ref);
3923 *new_base_branch_ref = NULL;
3925 if (*tmp_branch) {
3926 got_ref_close(*tmp_branch);
3927 *tmp_branch = NULL;
3929 if (*fileindex) {
3930 got_fileindex_free(*fileindex);
3931 *fileindex = NULL;
3933 lock_worktree(worktree, LOCK_SH);
3935 return err;
3938 const struct got_error *
3939 got_worktree_rebase_continue(struct got_object_id **commit_id,
3940 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3941 struct got_reference **branch, struct got_fileindex **fileindex,
3942 struct got_worktree *worktree, struct got_repository *repo)
3944 const struct got_error *err;
3945 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3946 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3947 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3948 char *fileindex_path = NULL;
3949 int have_staged_files = 0;
3951 *commit_id = NULL;
3952 *new_base_branch = NULL;
3953 *tmp_branch = NULL;
3954 *branch = NULL;
3955 *fileindex = NULL;
3957 err = lock_worktree(worktree, LOCK_EX);
3958 if (err)
3959 return err;
3961 err = open_fileindex(fileindex, &fileindex_path, worktree);
3962 if (err)
3963 goto done;
3965 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
3966 &have_staged_files);
3967 if (err && err->code != GOT_ERR_CANCELLED)
3968 goto done;
3969 if (have_staged_files) {
3970 err = got_error(GOT_ERR_STAGED_PATHS);
3971 goto done;
3974 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3975 if (err)
3976 goto done;
3978 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3979 if (err)
3980 goto done;
3982 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3983 if (err)
3984 goto done;
3986 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3987 if (err)
3988 goto done;
3990 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3991 if (err)
3992 goto done;
3994 err = got_ref_open(branch, repo,
3995 got_ref_get_symref_target(branch_ref), 0);
3996 if (err)
3997 goto done;
3999 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4000 if (err)
4001 goto done;
4003 err = got_ref_resolve(commit_id, repo, commit_ref);
4004 if (err)
4005 goto done;
4007 err = got_ref_open(new_base_branch, repo,
4008 new_base_branch_ref_name, 0);
4009 if (err)
4010 goto done;
4012 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4013 if (err)
4014 goto done;
4015 done:
4016 free(commit_ref_name);
4017 free(branch_ref_name);
4018 free(fileindex_path);
4019 if (commit_ref)
4020 got_ref_close(commit_ref);
4021 if (branch_ref)
4022 got_ref_close(branch_ref);
4023 if (err) {
4024 free(*commit_id);
4025 *commit_id = NULL;
4026 if (*tmp_branch) {
4027 got_ref_close(*tmp_branch);
4028 *tmp_branch = NULL;
4030 if (*new_base_branch) {
4031 got_ref_close(*new_base_branch);
4032 *new_base_branch = NULL;
4034 if (*branch) {
4035 got_ref_close(*branch);
4036 *branch = NULL;
4038 if (*fileindex) {
4039 got_fileindex_free(*fileindex);
4040 *fileindex = NULL;
4042 lock_worktree(worktree, LOCK_SH);
4044 return err;
4047 const struct got_error *
4048 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4050 const struct got_error *err;
4051 char *tmp_branch_name = NULL;
4053 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4054 if (err)
4055 return err;
4057 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4058 free(tmp_branch_name);
4059 return NULL;
4062 static const struct got_error *
4063 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4064 char **logmsg, void *arg)
4066 *logmsg = arg;
4067 return NULL;
4070 static const struct got_error *
4071 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4072 const char *path, struct got_object_id *blob_id,
4073 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4075 return NULL;
4078 struct collect_merged_paths_arg {
4079 got_worktree_checkout_cb progress_cb;
4080 void *progress_arg;
4081 struct got_pathlist_head *merged_paths;
4084 static const struct got_error *
4085 collect_merged_paths(void *arg, unsigned char status, const char *path)
4087 const struct got_error *err;
4088 struct collect_merged_paths_arg *a = arg;
4089 char *p;
4090 struct got_pathlist_entry *new;
4092 err = (*a->progress_cb)(a->progress_arg, status, path);
4093 if (err)
4094 return err;
4096 if (status != GOT_STATUS_MERGE &&
4097 status != GOT_STATUS_ADD &&
4098 status != GOT_STATUS_DELETE &&
4099 status != GOT_STATUS_CONFLICT)
4100 return NULL;
4102 p = strdup(path);
4103 if (p == NULL)
4104 return got_error_from_errno("strdup");
4106 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4107 if (err || new == NULL)
4108 free(p);
4109 return err;
4112 void
4113 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4115 struct got_pathlist_entry *pe;
4117 TAILQ_FOREACH(pe, merged_paths, entry)
4118 free((char *)pe->path);
4120 got_pathlist_free(merged_paths);
4123 static const struct got_error *
4124 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4125 struct got_repository *repo)
4127 const struct got_error *err;
4128 struct got_reference *commit_ref = NULL;
4130 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4131 if (err) {
4132 if (err->code != GOT_ERR_NOT_REF)
4133 goto done;
4134 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4135 if (err)
4136 goto done;
4137 err = got_ref_write(commit_ref, repo);
4138 if (err)
4139 goto done;
4140 } else {
4141 struct got_object_id *stored_id;
4142 int cmp;
4144 err = got_ref_resolve(&stored_id, repo, commit_ref);
4145 if (err)
4146 goto done;
4147 cmp = got_object_id_cmp(commit_id, stored_id);
4148 free(stored_id);
4149 if (cmp != 0) {
4150 err = got_error(GOT_ERR_REBASE_COMMITID);
4151 goto done;
4154 done:
4155 if (commit_ref)
4156 got_ref_close(commit_ref);
4157 return err;
4160 static const struct got_error *
4161 rebase_merge_files(struct got_pathlist_head *merged_paths,
4162 const char *commit_ref_name, struct got_worktree *worktree,
4163 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4164 struct got_object_id *commit_id, struct got_repository *repo,
4165 got_worktree_checkout_cb progress_cb, void *progress_arg,
4166 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4168 const struct got_error *err;
4169 struct got_reference *commit_ref = NULL;
4170 struct collect_merged_paths_arg cmp_arg;
4171 char *fileindex_path;
4173 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4175 err = get_fileindex_path(&fileindex_path, worktree);
4176 if (err)
4177 return err;
4179 cmp_arg.progress_cb = progress_cb;
4180 cmp_arg.progress_arg = progress_arg;
4181 cmp_arg.merged_paths = merged_paths;
4182 err = merge_files(worktree, fileindex, fileindex_path,
4183 parent_commit_id, commit_id, repo, collect_merged_paths,
4184 &cmp_arg, cancel_cb, cancel_arg);
4185 if (commit_ref)
4186 got_ref_close(commit_ref);
4187 return err;
4190 const struct got_error *
4191 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4192 struct got_worktree *worktree, struct got_fileindex *fileindex,
4193 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4194 struct got_repository *repo,
4195 got_worktree_checkout_cb progress_cb, void *progress_arg,
4196 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4198 const struct got_error *err;
4199 char *commit_ref_name;
4201 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4202 if (err)
4203 return err;
4205 err = store_commit_id(commit_ref_name, commit_id, repo);
4206 if (err)
4207 goto done;
4209 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4210 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4211 progress_arg, cancel_cb, cancel_arg);
4212 done:
4213 free(commit_ref_name);
4214 return err;
4217 const struct got_error *
4218 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4219 struct got_worktree *worktree, struct got_fileindex *fileindex,
4220 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4221 struct got_repository *repo,
4222 got_worktree_checkout_cb progress_cb, void *progress_arg,
4223 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4225 const struct got_error *err;
4226 char *commit_ref_name;
4228 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4229 if (err)
4230 return err;
4232 err = store_commit_id(commit_ref_name, commit_id, repo);
4233 if (err)
4234 goto done;
4236 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4237 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4238 progress_arg, cancel_cb, cancel_arg);
4239 done:
4240 free(commit_ref_name);
4241 return err;
4244 static const struct got_error *
4245 rebase_commit(struct got_object_id **new_commit_id,
4246 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4247 struct got_worktree *worktree, struct got_fileindex *fileindex,
4248 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4249 const char *new_logmsg, struct got_repository *repo)
4251 const struct got_error *err, *sync_err;
4252 struct got_pathlist_head commitable_paths;
4253 struct collect_commitables_arg cc_arg;
4254 char *fileindex_path = NULL;
4255 struct got_reference *head_ref = NULL;
4256 struct got_object_id *head_commit_id = NULL;
4257 char *logmsg = NULL;
4259 TAILQ_INIT(&commitable_paths);
4260 *new_commit_id = NULL;
4262 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4264 err = get_fileindex_path(&fileindex_path, worktree);
4265 if (err)
4266 return err;
4268 cc_arg.commitable_paths = &commitable_paths;
4269 cc_arg.worktree = worktree;
4270 cc_arg.repo = repo;
4271 cc_arg.have_staged_files = 0;
4273 * If possible get the status of individual files directly to
4274 * avoid crawling the entire work tree once per rebased commit.
4275 * TODO: Ideally, merged_paths would contain a list of commitables
4276 * we could use so we could skip worktree_status() entirely.
4278 if (merged_paths) {
4279 struct got_pathlist_entry *pe;
4280 if (TAILQ_EMPTY(merged_paths)) {
4281 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4282 goto done;
4284 TAILQ_FOREACH(pe, merged_paths, entry) {
4285 err = worktree_status(worktree, pe->path, fileindex,
4286 repo, collect_commitables, &cc_arg, NULL, NULL);
4287 if (err)
4288 goto done;
4290 } else {
4291 err = worktree_status(worktree, "", fileindex, repo,
4292 collect_commitables, &cc_arg, NULL, NULL);
4293 if (err)
4294 goto done;
4297 if (TAILQ_EMPTY(&commitable_paths)) {
4298 /* No-op change; commit will be elided. */
4299 err = got_ref_delete(commit_ref, repo);
4300 if (err)
4301 goto done;
4302 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4303 goto done;
4306 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4307 if (err)
4308 goto done;
4310 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4311 if (err)
4312 goto done;
4314 if (new_logmsg)
4315 logmsg = strdup(new_logmsg);
4316 else
4317 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4318 if (logmsg == NULL)
4319 return got_error_from_errno("strdup");
4321 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4322 worktree, got_object_commit_get_author(orig_commit),
4323 got_object_commit_get_committer(orig_commit),
4324 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4325 if (err)
4326 goto done;
4328 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4329 if (err)
4330 goto done;
4332 err = got_ref_delete(commit_ref, repo);
4333 if (err)
4334 goto done;
4336 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4337 fileindex);
4338 sync_err = sync_fileindex(fileindex, fileindex_path);
4339 if (sync_err && err == NULL)
4340 err = sync_err;
4341 done:
4342 free(fileindex_path);
4343 free(head_commit_id);
4344 if (head_ref)
4345 got_ref_close(head_ref);
4346 if (err) {
4347 free(*new_commit_id);
4348 *new_commit_id = NULL;
4350 return err;
4353 const struct got_error *
4354 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4355 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4356 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4357 struct got_commit_object *orig_commit,
4358 struct got_object_id *orig_commit_id, struct got_repository *repo)
4360 const struct got_error *err;
4361 char *commit_ref_name;
4362 struct got_reference *commit_ref = NULL;
4363 struct got_object_id *commit_id = NULL;
4365 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4366 if (err)
4367 return err;
4369 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4370 if (err)
4371 goto done;
4372 err = got_ref_resolve(&commit_id, repo, commit_ref);
4373 if (err)
4374 goto done;
4375 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4376 err = got_error(GOT_ERR_REBASE_COMMITID);
4377 goto done;
4380 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4381 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4382 done:
4383 if (commit_ref)
4384 got_ref_close(commit_ref);
4385 free(commit_ref_name);
4386 free(commit_id);
4387 return err;
4390 const struct got_error *
4391 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4392 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4393 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4394 struct got_commit_object *orig_commit,
4395 struct got_object_id *orig_commit_id, const char *new_logmsg,
4396 struct got_repository *repo)
4398 const struct got_error *err;
4399 char *commit_ref_name;
4400 struct got_reference *commit_ref = NULL;
4401 struct got_object_id *commit_id = NULL;
4403 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4404 if (err)
4405 return err;
4407 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4408 if (err)
4409 goto done;
4410 err = got_ref_resolve(&commit_id, repo, commit_ref);
4411 if (err)
4412 goto done;
4413 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4414 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4415 goto done;
4418 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4419 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4420 done:
4421 if (commit_ref)
4422 got_ref_close(commit_ref);
4423 free(commit_ref_name);
4424 free(commit_id);
4425 return err;
4428 const struct got_error *
4429 got_worktree_rebase_postpone(struct got_worktree *worktree,
4430 struct got_fileindex *fileindex)
4432 if (fileindex)
4433 got_fileindex_free(fileindex);
4434 return lock_worktree(worktree, LOCK_SH);
4437 static const struct got_error *
4438 delete_ref(const char *name, struct got_repository *repo)
4440 const struct got_error *err;
4441 struct got_reference *ref;
4443 err = got_ref_open(&ref, repo, name, 0);
4444 if (err) {
4445 if (err->code == GOT_ERR_NOT_REF)
4446 return NULL;
4447 return err;
4450 err = got_ref_delete(ref, repo);
4451 got_ref_close(ref);
4452 return err;
4455 static const struct got_error *
4456 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4458 const struct got_error *err;
4459 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4460 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4462 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4463 if (err)
4464 goto done;
4465 err = delete_ref(tmp_branch_name, repo);
4466 if (err)
4467 goto done;
4469 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4470 if (err)
4471 goto done;
4472 err = delete_ref(new_base_branch_ref_name, repo);
4473 if (err)
4474 goto done;
4476 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4477 if (err)
4478 goto done;
4479 err = delete_ref(branch_ref_name, repo);
4480 if (err)
4481 goto done;
4483 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4484 if (err)
4485 goto done;
4486 err = delete_ref(commit_ref_name, repo);
4487 if (err)
4488 goto done;
4490 done:
4491 free(tmp_branch_name);
4492 free(new_base_branch_ref_name);
4493 free(branch_ref_name);
4494 free(commit_ref_name);
4495 return err;
4498 const struct got_error *
4499 got_worktree_rebase_complete(struct got_worktree *worktree,
4500 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4501 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4502 struct got_repository *repo)
4504 const struct got_error *err, *unlockerr;
4505 struct got_object_id *new_head_commit_id = NULL;
4507 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4508 if (err)
4509 return err;
4511 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4512 if (err)
4513 goto done;
4515 err = got_ref_write(rebased_branch, repo);
4516 if (err)
4517 goto done;
4519 err = got_worktree_set_head_ref(worktree, rebased_branch);
4520 if (err)
4521 goto done;
4523 err = delete_rebase_refs(worktree, repo);
4524 done:
4525 if (fileindex)
4526 got_fileindex_free(fileindex);
4527 free(new_head_commit_id);
4528 unlockerr = lock_worktree(worktree, LOCK_SH);
4529 if (unlockerr && err == NULL)
4530 err = unlockerr;
4531 return err;
4534 struct collect_revertible_paths_arg {
4535 struct got_pathlist_head *revertible_paths;
4536 struct got_worktree *worktree;
4539 static const struct got_error *
4540 collect_revertible_paths(void *arg, unsigned char status,
4541 unsigned char staged_status, const char *relpath,
4542 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4543 struct got_object_id *commit_id)
4545 struct collect_revertible_paths_arg *a = arg;
4546 const struct got_error *err = NULL;
4547 struct got_pathlist_entry *new = NULL;
4548 char *path = NULL;
4550 if (status != GOT_STATUS_ADD &&
4551 status != GOT_STATUS_DELETE &&
4552 status != GOT_STATUS_MODIFY &&
4553 status != GOT_STATUS_CONFLICT &&
4554 status != GOT_STATUS_MISSING)
4555 return NULL;
4557 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4558 return got_error_from_errno("asprintf");
4560 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4561 if (err || new == NULL)
4562 free(path);
4563 return err;
4566 const struct got_error *
4567 got_worktree_rebase_abort(struct got_worktree *worktree,
4568 struct got_fileindex *fileindex, struct got_repository *repo,
4569 struct got_reference *new_base_branch,
4570 got_worktree_checkout_cb progress_cb, void *progress_arg)
4572 const struct got_error *err, *unlockerr, *sync_err;
4573 struct got_reference *resolved = NULL;
4574 struct got_object_id *commit_id = NULL;
4575 char *fileindex_path = NULL;
4576 struct got_pathlist_head revertible_paths;
4577 struct got_pathlist_entry *pe;
4578 struct collect_revertible_paths_arg crp_arg;
4579 struct got_object_id *tree_id = NULL;
4581 TAILQ_INIT(&revertible_paths);
4583 err = lock_worktree(worktree, LOCK_EX);
4584 if (err)
4585 return err;
4587 err = got_ref_open(&resolved, repo,
4588 got_ref_get_symref_target(new_base_branch), 0);
4589 if (err)
4590 goto done;
4592 err = got_worktree_set_head_ref(worktree, resolved);
4593 if (err)
4594 goto done;
4597 * XXX commits to the base branch could have happened while
4598 * we were busy rebasing; should we store the original commit ID
4599 * when rebase begins and read it back here?
4601 err = got_ref_resolve(&commit_id, repo, resolved);
4602 if (err)
4603 goto done;
4605 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4606 if (err)
4607 goto done;
4609 err = got_object_id_by_path(&tree_id, repo,
4610 worktree->base_commit_id, worktree->path_prefix);
4611 if (err)
4612 goto done;
4614 err = delete_rebase_refs(worktree, repo);
4615 if (err)
4616 goto done;
4618 err = get_fileindex_path(&fileindex_path, worktree);
4619 if (err)
4620 goto done;
4622 crp_arg.revertible_paths = &revertible_paths;
4623 crp_arg.worktree = worktree;
4624 err = worktree_status(worktree, "", fileindex, repo,
4625 collect_revertible_paths, &crp_arg, NULL, NULL);
4626 if (err)
4627 goto done;
4629 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4630 err = revert_file(worktree, fileindex, pe->path,
4631 progress_cb, progress_arg, repo);
4632 if (err)
4633 goto sync;
4636 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4637 repo, progress_cb, progress_arg, NULL, NULL);
4638 sync:
4639 sync_err = sync_fileindex(fileindex, fileindex_path);
4640 if (sync_err && err == NULL)
4641 err = sync_err;
4642 done:
4643 got_ref_close(resolved);
4644 free(tree_id);
4645 free(commit_id);
4646 if (fileindex)
4647 got_fileindex_free(fileindex);
4648 free(fileindex_path);
4649 TAILQ_FOREACH(pe, &revertible_paths, entry)
4650 free((char *)pe->path);
4651 got_pathlist_free(&revertible_paths);
4653 unlockerr = lock_worktree(worktree, LOCK_SH);
4654 if (unlockerr && err == NULL)
4655 err = unlockerr;
4656 return err;
4659 const struct got_error *
4660 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4661 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4662 struct got_fileindex **fileindex, struct got_worktree *worktree,
4663 struct got_repository *repo)
4665 const struct got_error *err = NULL;
4666 char *tmp_branch_name = NULL;
4667 char *branch_ref_name = NULL;
4668 char *base_commit_ref_name = NULL;
4669 char *fileindex_path = NULL;
4670 struct check_rebase_ok_arg ok_arg;
4671 struct got_reference *wt_branch = NULL;
4672 struct got_reference *base_commit_ref = NULL;
4674 *tmp_branch = NULL;
4675 *branch_ref = NULL;
4676 *base_commit_id = NULL;
4677 *fileindex = NULL;
4679 err = lock_worktree(worktree, LOCK_EX);
4680 if (err)
4681 return err;
4683 err = open_fileindex(fileindex, &fileindex_path, worktree);
4684 if (err)
4685 goto done;
4687 ok_arg.worktree = worktree;
4688 ok_arg.repo = repo;
4689 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4690 &ok_arg);
4691 if (err)
4692 goto done;
4694 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4695 if (err)
4696 goto done;
4698 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4699 if (err)
4700 goto done;
4702 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4703 worktree);
4704 if (err)
4705 goto done;
4707 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4708 0);
4709 if (err)
4710 goto done;
4712 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4713 if (err)
4714 goto done;
4716 err = got_ref_write(*branch_ref, repo);
4717 if (err)
4718 goto done;
4720 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4721 worktree->base_commit_id);
4722 if (err)
4723 goto done;
4724 err = got_ref_write(base_commit_ref, repo);
4725 if (err)
4726 goto done;
4727 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4728 if (*base_commit_id == NULL) {
4729 err = got_error_from_errno("got_object_id_dup");
4730 goto done;
4733 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4734 worktree->base_commit_id);
4735 if (err)
4736 goto done;
4737 err = got_ref_write(*tmp_branch, repo);
4738 if (err)
4739 goto done;
4741 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4742 if (err)
4743 goto done;
4744 done:
4745 free(fileindex_path);
4746 free(tmp_branch_name);
4747 free(branch_ref_name);
4748 free(base_commit_ref_name);
4749 if (wt_branch)
4750 got_ref_close(wt_branch);
4751 if (err) {
4752 if (*branch_ref) {
4753 got_ref_close(*branch_ref);
4754 *branch_ref = NULL;
4756 if (*tmp_branch) {
4757 got_ref_close(*tmp_branch);
4758 *tmp_branch = NULL;
4760 free(*base_commit_id);
4761 if (*fileindex) {
4762 got_fileindex_free(*fileindex);
4763 *fileindex = NULL;
4765 lock_worktree(worktree, LOCK_SH);
4767 return err;
4770 const struct got_error *
4771 got_worktree_histedit_postpone(struct got_worktree *worktree,
4772 struct got_fileindex *fileindex)
4774 if (fileindex)
4775 got_fileindex_free(fileindex);
4776 return lock_worktree(worktree, LOCK_SH);
4779 const struct got_error *
4780 got_worktree_histedit_in_progress(int *in_progress,
4781 struct got_worktree *worktree)
4783 const struct got_error *err;
4784 char *tmp_branch_name = NULL;
4786 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4787 if (err)
4788 return err;
4790 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4791 free(tmp_branch_name);
4792 return NULL;
4795 const struct got_error *
4796 got_worktree_histedit_continue(struct got_object_id **commit_id,
4797 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4798 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4799 struct got_worktree *worktree, struct got_repository *repo)
4801 const struct got_error *err;
4802 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4803 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4804 struct got_reference *commit_ref = NULL;
4805 struct got_reference *base_commit_ref = NULL;
4806 char *fileindex_path = NULL;
4807 int have_staged_files = 0;
4809 *commit_id = NULL;
4810 *tmp_branch = NULL;
4811 *base_commit_id = NULL;
4812 *fileindex = NULL;
4814 err = lock_worktree(worktree, LOCK_EX);
4815 if (err)
4816 return err;
4818 err = open_fileindex(fileindex, &fileindex_path, worktree);
4819 if (err)
4820 goto done;
4822 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4823 &have_staged_files);
4824 if (err && err->code != GOT_ERR_CANCELLED)
4825 goto done;
4826 if (have_staged_files) {
4827 err = got_error(GOT_ERR_STAGED_PATHS);
4828 goto done;
4831 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4832 if (err)
4833 goto done;
4835 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4836 if (err)
4837 goto done;
4839 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4840 if (err)
4841 goto done;
4843 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4844 worktree);
4845 if (err)
4846 goto done;
4848 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4849 if (err)
4850 goto done;
4852 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4853 if (err)
4854 goto done;
4855 err = got_ref_resolve(commit_id, repo, commit_ref);
4856 if (err)
4857 goto done;
4859 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4860 if (err)
4861 goto done;
4862 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4863 if (err)
4864 goto done;
4866 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4867 if (err)
4868 goto done;
4869 done:
4870 free(commit_ref_name);
4871 free(branch_ref_name);
4872 free(fileindex_path);
4873 if (commit_ref)
4874 got_ref_close(commit_ref);
4875 if (base_commit_ref)
4876 got_ref_close(base_commit_ref);
4877 if (err) {
4878 free(*commit_id);
4879 *commit_id = NULL;
4880 free(*base_commit_id);
4881 *base_commit_id = NULL;
4882 if (*tmp_branch) {
4883 got_ref_close(*tmp_branch);
4884 *tmp_branch = NULL;
4886 if (*fileindex) {
4887 got_fileindex_free(*fileindex);
4888 *fileindex = NULL;
4890 lock_worktree(worktree, LOCK_EX);
4892 return err;
4895 static const struct got_error *
4896 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4898 const struct got_error *err;
4899 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4900 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4902 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4903 if (err)
4904 goto done;
4905 err = delete_ref(tmp_branch_name, repo);
4906 if (err)
4907 goto done;
4909 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4910 worktree);
4911 if (err)
4912 goto done;
4913 err = delete_ref(base_commit_ref_name, repo);
4914 if (err)
4915 goto done;
4917 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4918 if (err)
4919 goto done;
4920 err = delete_ref(branch_ref_name, repo);
4921 if (err)
4922 goto done;
4924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4925 if (err)
4926 goto done;
4927 err = delete_ref(commit_ref_name, repo);
4928 if (err)
4929 goto done;
4930 done:
4931 free(tmp_branch_name);
4932 free(base_commit_ref_name);
4933 free(branch_ref_name);
4934 free(commit_ref_name);
4935 return err;
4938 const struct got_error *
4939 got_worktree_histedit_abort(struct got_worktree *worktree,
4940 struct got_fileindex *fileindex, struct got_repository *repo,
4941 struct got_reference *branch, struct got_object_id *base_commit_id,
4942 got_worktree_checkout_cb progress_cb, void *progress_arg)
4944 const struct got_error *err, *unlockerr, *sync_err;
4945 struct got_reference *resolved = NULL;
4946 char *fileindex_path = NULL;
4947 struct got_pathlist_head revertible_paths;
4948 struct got_pathlist_entry *pe;
4949 struct collect_revertible_paths_arg crp_arg;
4950 struct got_object_id *tree_id = NULL;
4952 TAILQ_INIT(&revertible_paths);
4954 err = lock_worktree(worktree, LOCK_EX);
4955 if (err)
4956 return err;
4958 err = got_ref_open(&resolved, repo,
4959 got_ref_get_symref_target(branch), 0);
4960 if (err)
4961 goto done;
4963 err = got_worktree_set_head_ref(worktree, resolved);
4964 if (err)
4965 goto done;
4967 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4968 if (err)
4969 goto done;
4971 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4972 worktree->path_prefix);
4973 if (err)
4974 goto done;
4976 err = delete_histedit_refs(worktree, repo);
4977 if (err)
4978 goto done;
4980 err = get_fileindex_path(&fileindex_path, worktree);
4981 if (err)
4982 goto done;
4984 crp_arg.revertible_paths = &revertible_paths;
4985 crp_arg.worktree = worktree;
4986 err = worktree_status(worktree, "", fileindex, repo,
4987 collect_revertible_paths, &crp_arg, NULL, NULL);
4988 if (err)
4989 goto done;
4991 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4992 err = revert_file(worktree, fileindex, pe->path,
4993 progress_cb, progress_arg, repo);
4994 if (err)
4995 goto sync;
4998 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4999 repo, progress_cb, progress_arg, NULL, NULL);
5000 sync:
5001 sync_err = sync_fileindex(fileindex, fileindex_path);
5002 if (sync_err && err == NULL)
5003 err = sync_err;
5004 done:
5005 got_ref_close(resolved);
5006 free(tree_id);
5007 free(fileindex_path);
5008 TAILQ_FOREACH(pe, &revertible_paths, entry)
5009 free((char *)pe->path);
5010 got_pathlist_free(&revertible_paths);
5012 unlockerr = lock_worktree(worktree, LOCK_SH);
5013 if (unlockerr && err == NULL)
5014 err = unlockerr;
5015 return err;
5018 const struct got_error *
5019 got_worktree_histedit_complete(struct got_worktree *worktree,
5020 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5021 struct got_reference *edited_branch, struct got_repository *repo)
5023 const struct got_error *err, *unlockerr;
5024 struct got_object_id *new_head_commit_id = NULL;
5025 struct got_reference *resolved = NULL;
5027 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5028 if (err)
5029 return err;
5031 err = got_ref_open(&resolved, repo,
5032 got_ref_get_symref_target(edited_branch), 0);
5033 if (err)
5034 goto done;
5036 err = got_ref_change_ref(resolved, new_head_commit_id);
5037 if (err)
5038 goto done;
5040 err = got_ref_write(resolved, repo);
5041 if (err)
5042 goto done;
5044 err = got_worktree_set_head_ref(worktree, resolved);
5045 if (err)
5046 goto done;
5048 err = delete_histedit_refs(worktree, repo);
5049 done:
5050 if (fileindex)
5051 got_fileindex_free(fileindex);
5052 free(new_head_commit_id);
5053 unlockerr = lock_worktree(worktree, LOCK_SH);
5054 if (unlockerr && err == NULL)
5055 err = unlockerr;
5056 return err;
5059 const struct got_error *
5060 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5061 struct got_object_id *commit_id, struct got_repository *repo)
5063 const struct got_error *err;
5064 char *commit_ref_name;
5066 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5067 if (err)
5068 return err;
5070 err = store_commit_id(commit_ref_name, commit_id, repo);
5071 if (err)
5072 goto done;
5074 err = delete_ref(commit_ref_name, repo);
5075 done:
5076 free(commit_ref_name);
5077 return err;
5080 static const struct got_error *
5081 stage_check_out_of_date(const char *relpath, const char *ondisk_path,
5082 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5083 struct got_fileindex *fileindex, struct got_repository *repo)
5085 const struct got_error *err = NULL;
5086 struct got_fileindex_entry *ie;
5087 unsigned char status;
5088 struct stat sb;
5089 struct got_object_id blob_id, base_commit_id;
5090 struct got_object_id *blob_idp = NULL, *base_commit_idp = NULL;
5091 char *in_repo_path = NULL, *p;
5093 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
5094 if (ie == NULL)
5095 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5097 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5098 return NULL;
5100 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
5101 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
5102 relpath) == -1)
5103 return got_error_from_errno("asprintf");
5105 if (got_fileindex_entry_has_blob(ie)) {
5106 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
5107 blob_idp = &blob_id;
5109 if (got_fileindex_entry_has_commit(ie)) {
5110 memcpy(base_commit_id.sha1, ie->commit_sha1,
5111 SHA1_DIGEST_LENGTH);
5112 base_commit_idp = &base_commit_id;
5115 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
5116 if (err)
5117 goto done;
5119 p = in_repo_path;
5120 while (p[0] == '/')
5121 p++;
5122 err = check_out_of_date(p, status, GOT_STATUS_NO_CHANGE,
5123 blob_idp, base_commit_idp, head_commit_id, repo,
5124 GOT_ERR_STAGE_OUT_OF_DATE);
5125 done:
5126 free(in_repo_path);
5127 return err;
5130 static const struct got_error *
5131 stage_path(const char *relpath, const char *ondisk_path,
5132 const char *path_content, struct got_worktree *worktree,
5133 struct got_fileindex *fileindex, struct got_repository *repo,
5134 got_worktree_status_cb status_cb, void *status_arg)
5136 const struct got_error *err = NULL;
5137 struct got_fileindex_entry *ie;
5138 unsigned char status, staged_status;
5139 struct stat sb;
5140 struct got_object_id blob_id, *staged_blob_id = NULL;
5141 uint32_t stage;
5143 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
5144 if (ie == NULL)
5145 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5147 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
5148 if (err)
5149 return err;
5150 staged_status = get_staged_status(ie);
5152 switch (status) {
5153 case GOT_STATUS_ADD:
5154 case GOT_STATUS_MODIFY:
5155 err = got_object_blob_create(&staged_blob_id,
5156 path_content ? path_content : ondisk_path, repo);
5157 if (err)
5158 goto done;
5159 memcpy(&blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
5160 memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
5161 SHA1_DIGEST_LENGTH);
5162 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
5163 stage = GOT_FILEIDX_STAGE_ADD;
5164 else
5165 stage = GOT_FILEIDX_STAGE_MODIFY;
5166 got_fileindex_entry_stage_set(ie, stage);
5167 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
5168 get_staged_status(ie), relpath, &blob_id,
5169 staged_blob_id, NULL);
5170 break;
5171 case GOT_STATUS_DELETE:
5172 if (staged_status == GOT_STATUS_DELETE)
5173 break;
5174 stage = GOT_FILEIDX_STAGE_DELETE;
5175 got_fileindex_entry_stage_set(ie, stage);
5176 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
5177 get_staged_status(ie), relpath, NULL, NULL, NULL);
5178 break;
5179 case GOT_STATUS_NO_CHANGE:
5180 err = got_error_path(relpath, GOT_ERR_STAGE_NO_CHANGE);
5181 break;
5182 case GOT_STATUS_CONFLICT:
5183 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
5184 break;
5185 default:
5186 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
5187 break;
5189 done:
5190 free(staged_blob_id);
5191 return err;
5194 const struct got_error *
5195 got_worktree_stage(struct got_worktree *worktree,
5196 struct got_pathlist_head *paths,
5197 got_worktree_status_cb status_cb, void *status_arg,
5198 struct got_repository *repo)
5200 const struct got_error *err = NULL, *sync_err, *unlockerr;
5201 struct got_pathlist_entry *pe;
5202 struct got_fileindex *fileindex = NULL;
5203 char *fileindex_path = NULL;
5204 struct got_reference *head_ref = NULL;
5205 struct got_object_id *head_commit_id = NULL;
5207 err = lock_worktree(worktree, LOCK_EX);
5208 if (err)
5209 return err;
5211 err = got_ref_open(&head_ref, repo,
5212 got_worktree_get_head_ref_name(worktree), 0);
5213 if (err)
5214 goto done;
5215 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5216 if (err)
5217 goto done;
5218 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5219 if (err)
5220 goto done;
5222 /* Check out-of-dateness before staging anything. */
5223 TAILQ_FOREACH(pe, paths, entry) {
5224 char *ondisk_path;
5225 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
5226 pe->path) == -1)
5227 return got_error_from_errno("asprintf");
5228 err = stage_check_out_of_date(pe->path, ondisk_path,
5229 head_commit_id, worktree, fileindex, repo);
5230 free(ondisk_path);
5231 if (err)
5232 goto done;
5235 TAILQ_FOREACH(pe, paths, entry) {
5236 char *ondisk_path;
5237 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
5238 pe->path) == -1)
5239 return got_error_from_errno("asprintf");
5240 err = stage_path(pe->path, ondisk_path,
5241 (const char *)pe->data, worktree, fileindex, repo,
5242 status_cb, status_arg);
5243 free(ondisk_path);
5244 if (err)
5245 break;
5248 sync_err = sync_fileindex(fileindex, fileindex_path);
5249 if (sync_err && err == NULL)
5250 err = sync_err;
5251 done:
5252 if (head_ref)
5253 got_ref_close(head_ref);
5254 free(head_commit_id);
5255 free(fileindex_path);
5256 if (fileindex)
5257 got_fileindex_free(fileindex);
5258 unlockerr = lock_worktree(worktree, LOCK_SH);
5259 if (unlockerr && err == NULL)
5260 err = unlockerr;
5261 return err;
5264 struct unstage_path_arg {
5265 struct got_worktree *worktree;
5266 struct got_fileindex *fileindex;
5267 struct got_repository *repo;
5268 got_worktree_checkout_cb progress_cb;
5269 void *progress_arg;
5272 static const struct got_error *
5273 unstage_path(void *arg, unsigned char status,
5274 unsigned char staged_status, const char *relpath,
5275 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5276 struct got_object_id *commit_id)
5278 const struct got_error *err = NULL;
5279 struct unstage_path_arg *a = arg;
5280 struct got_fileindex_entry *ie;
5281 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
5282 char *ondisk_path = NULL;
5283 int local_changes_subsumed;
5284 struct stat sb;
5286 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5287 if (ie == NULL)
5288 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5290 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
5291 == -1)
5292 return got_error_from_errno("asprintf");
5294 switch (staged_status) {
5295 case GOT_STATUS_MODIFY:
5296 err = got_object_open_as_blob(&blob_base, a->repo,
5297 blob_id, 8192);
5298 if (err)
5299 break;
5300 /* fall through */
5301 case GOT_STATUS_ADD:
5302 err = got_object_open_as_blob(&blob_staged, a->repo,
5303 staged_blob_id, 8192);
5304 if (err)
5305 break;
5308 err = merge_blob(&local_changes_subsumed, a->worktree,
5309 blob_base, ondisk_path, relpath,
5310 got_fileindex_perms_to_st(ie), blob_staged,
5311 commit_id ? commit_id : a->worktree->base_commit_id,
5312 a->repo, a->progress_cb, a->progress_arg);
5313 if (err == NULL)
5314 got_fileindex_entry_stage_set(ie,
5315 GOT_FILEIDX_STAGE_NONE);
5316 break;
5317 case GOT_STATUS_DELETE:
5318 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
5319 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
5320 if (err)
5321 break;
5322 err = (*a->progress_cb)(a->progress_arg, status, relpath);
5323 break;
5326 free(ondisk_path);
5327 if (blob_base)
5328 got_object_blob_close(blob_base);
5329 if (blob_staged)
5330 got_object_blob_close(blob_staged);
5331 return err;
5334 const struct got_error *
5335 got_worktree_unstage(struct got_worktree *worktree,
5336 struct got_pathlist_head *paths,
5337 got_worktree_checkout_cb progress_cb, void *progress_arg,
5338 struct got_repository *repo)
5340 const struct got_error *err = NULL, *sync_err, *unlockerr;
5341 struct got_pathlist_entry *pe;
5342 struct got_fileindex *fileindex = NULL;
5343 char *fileindex_path = NULL;
5344 struct unstage_path_arg upa;
5346 err = lock_worktree(worktree, LOCK_EX);
5347 if (err)
5348 return err;
5350 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5351 if (err)
5352 goto done;
5354 upa.worktree = worktree;
5355 upa.fileindex = fileindex;
5356 upa.repo = repo;
5357 upa.progress_cb = progress_cb;
5358 upa.progress_arg = progress_arg;
5359 TAILQ_FOREACH(pe, paths, entry) {
5360 err = worktree_status(worktree, pe->path, fileindex, repo,
5361 unstage_path, &upa, NULL, NULL);
5362 if (err)
5363 goto done;
5366 sync_err = sync_fileindex(fileindex, fileindex_path);
5367 if (sync_err && err == NULL)
5368 err = sync_err;
5369 done:
5370 free(fileindex_path);
5371 if (fileindex)
5372 got_fileindex_free(fileindex);
5373 unlockerr = lock_worktree(worktree, LOCK_SH);
5374 if (unlockerr && err == NULL)
5375 err = unlockerr;
5376 return err;