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/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("open", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("open", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 unlink(merged_path);
814 goto done;
817 done:
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 free(merged_path);
823 free(base_path);
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
828 return err;
831 /*
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
835 */
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
852 if (parent == NULL)
853 return got_error_from_errno2("dirname", ondisk_path);
855 free(base_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
858 base_path = NULL;
859 goto done;
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 if (err)
864 goto done;
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 blob_deriv);
867 if (err)
868 goto done;
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (err)
872 goto done;
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
882 done:
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(base_path);
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
890 free(id_str);
891 free(label_deriv);
892 return err;
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
903 if (ie == NULL)
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
905 if (ie)
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
908 update_timestamps);
909 else {
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
913 if (!err)
914 err = got_fileindex_entry_add(fileindex, new_ie);
916 return err;
919 static mode_t
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
924 if (executable) {
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
927 xbits |= S_IXGRP;
928 if (st_mode & S_IROTH)
929 xbits |= S_IXOTH;
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
944 int fd = -1;
945 size_t len, hdrlen;
946 int update = 0;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
951 if (fd == -1) {
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
954 if (parent == NULL)
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
957 if (err)
958 return err;
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
962 if (fd == -1)
963 return got_error_from_errno2("open",
964 ondisk_path);
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
969 goto done;
970 } else {
971 err = got_opentemp_named_fd(&tmppath, &fd,
972 ondisk_path);
973 if (err)
974 goto done;
975 update = 1;
977 } else
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
985 else
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
988 if (err)
989 goto done;
991 hdrlen = got_object_blob_get_hdrlen(blob);
992 do {
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
995 if (err)
996 break;
997 if (len > 0) {
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1000 if (outlen == -1) {
1001 err = got_error_from_errno("write");
1002 goto done;
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1005 goto done;
1007 hdrlen = 0;
1009 } while (len != 0);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1016 if (update) {
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1019 ondisk_path);
1020 unlink(tmppath);
1021 goto done;
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1028 goto done;
1031 done:
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 free(tmppath);
1035 return err;
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1048 int i = 0;
1049 char *line;
1050 size_t len;
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1055 if (line == NULL) {
1056 if (feof(f))
1057 break;
1058 err = got_ferror(f, GOT_ERR_IO);
1059 break;
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1064 == 0)
1065 *status = GOT_STATUS_CONFLICT;
1066 else
1067 i++;
1071 return err;
1074 static int
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1081 static int
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1102 default:
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1114 size_t hdrlen;
1115 FILE *f = NULL;
1116 uint8_t fbuf[8192];
1117 struct got_blob_object *blob = NULL;
1118 size_t flen, blen;
1119 unsigned char staged_status = get_staged_status(ie);
1121 *status = GOT_STATUS_NO_CHANGE;
1123 if (lstat(abspath, sb) == -1) {
1124 if (errno == ENOENT) {
1125 if (got_fileindex_entry_has_file_on_disk(ie))
1126 *status = GOT_STATUS_MISSING;
1127 else
1128 *status = GOT_STATUS_DELETE;
1129 return NULL;
1131 return got_error_from_errno2("lstat", abspath);
1134 if (!S_ISREG(sb->st_mode)) {
1135 *status = GOT_STATUS_OBSTRUCTED;
1136 return NULL;
1139 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1140 *status = GOT_STATUS_DELETE;
1141 return NULL;
1142 } else if (!got_fileindex_entry_has_blob(ie) &&
1143 staged_status != GOT_STATUS_ADD) {
1144 *status = GOT_STATUS_ADD;
1145 return NULL;
1148 if (!stat_info_differs(ie, sb))
1149 return NULL;
1151 if (staged_status == GOT_STATUS_MODIFY ||
1152 staged_status == GOT_STATUS_ADD)
1153 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1154 else
1155 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1157 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1158 if (err)
1159 return err;
1161 f = fopen(abspath, "r");
1162 if (f == NULL) {
1163 err = got_error_from_errno2("fopen", abspath);
1164 goto done;
1166 hdrlen = got_object_blob_get_hdrlen(blob);
1167 for (;;) {
1168 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1169 err = got_object_blob_read_block(&blen, blob);
1170 if (err)
1171 goto done;
1172 /* Skip length of blob object header first time around. */
1173 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1174 if (flen == 0 && ferror(f)) {
1175 err = got_error_from_errno("fread");
1176 goto done;
1178 if (blen == 0) {
1179 if (flen != 0)
1180 *status = GOT_STATUS_MODIFY;
1181 break;
1182 } else if (flen == 0) {
1183 if (blen != 0)
1184 *status = GOT_STATUS_MODIFY;
1185 break;
1186 } else if (blen - hdrlen == flen) {
1187 /* Skip blob object header first time around. */
1188 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1189 *status = GOT_STATUS_MODIFY;
1190 break;
1192 } else {
1193 *status = GOT_STATUS_MODIFY;
1194 break;
1196 hdrlen = 0;
1199 if (*status == GOT_STATUS_MODIFY) {
1200 rewind(f);
1201 err = get_modified_file_content_status(status, f);
1202 } else if (xbit_differs(ie, sb->st_mode))
1203 *status = GOT_STATUS_MODE_CHANGE;
1204 done:
1205 if (blob)
1206 got_object_blob_close(blob);
1207 if (f)
1208 fclose(f);
1209 return err;
1213 * Update timestamps in the file index if a file is unmodified and
1214 * we had to run a full content comparison to find out.
1216 static const struct got_error *
1217 sync_timestamps(char *ondisk_path, unsigned char status,
1218 struct got_fileindex_entry *ie, struct stat *sb)
1220 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1221 return got_fileindex_entry_update(ie, ondisk_path,
1222 ie->blob_sha1, ie->commit_sha1, 1);
1224 return NULL;
1227 static const struct got_error *
1228 update_blob(struct got_worktree *worktree,
1229 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1230 struct got_tree_entry *te, const char *path,
1231 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1232 void *progress_arg)
1234 const struct got_error *err = NULL;
1235 struct got_blob_object *blob = NULL;
1236 char *ondisk_path;
1237 unsigned char status = GOT_STATUS_NO_CHANGE;
1238 struct stat sb;
1240 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1241 return got_error_from_errno("asprintf");
1243 if (ie) {
1244 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1245 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1246 goto done;
1248 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1249 if (err)
1250 goto done;
1251 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1252 sb.st_mode = got_fileindex_perms_to_st(ie);
1253 } else
1254 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1256 if (status == GOT_STATUS_OBSTRUCTED) {
1257 err = (*progress_cb)(progress_arg, status, path);
1258 goto done;
1261 if (ie && status != GOT_STATUS_MISSING &&
1262 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1263 if (got_fileindex_entry_has_commit(ie) &&
1264 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1265 SHA1_DIGEST_LENGTH) == 0) {
1266 err = sync_timestamps(ondisk_path, status, ie, &sb);
1267 if (err)
1268 goto done;
1269 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1270 path);
1271 goto done;
1273 if (got_fileindex_entry_has_blob(ie) &&
1274 memcmp(ie->blob_sha1, te->id.sha1,
1275 SHA1_DIGEST_LENGTH) == 0) {
1276 err = sync_timestamps(ondisk_path, status, ie, &sb);
1277 goto done;
1281 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1282 if (err)
1283 goto done;
1285 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1286 int update_timestamps;
1287 struct got_blob_object *blob2 = NULL;
1288 char *label_orig = NULL;
1289 if (got_fileindex_entry_has_blob(ie)) {
1290 struct got_object_id id2;
1291 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1292 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1293 if (err)
1294 goto done;
1296 if (got_fileindex_entry_has_commit(ie)) {
1297 char id_str[SHA1_DIGEST_STRING_LENGTH];
1298 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1299 sizeof(id_str)) == NULL) {
1300 err = got_error_path(id_str,
1301 GOT_ERR_BAD_OBJ_ID_STR);
1302 goto done;
1304 if (asprintf(&label_orig, "%s: commit %s",
1305 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1306 err = got_error_from_errno("asprintf");
1307 goto done;
1310 err = merge_blob(&update_timestamps, worktree, blob2,
1311 ondisk_path, path, sb.st_mode, label_orig, blob,
1312 worktree->base_commit_id, repo,
1313 progress_cb, progress_arg);
1314 free(label_orig);
1315 if (blob2)
1316 got_object_blob_close(blob2);
1317 if (err)
1318 goto done;
1320 * Do not update timestamps of files with local changes.
1321 * Otherwise, a future status walk would treat them as
1322 * unmodified files again.
1324 err = got_fileindex_entry_update(ie, ondisk_path,
1325 blob->id.sha1, worktree->base_commit_id->sha1,
1326 update_timestamps);
1327 } else if (status == GOT_STATUS_MODE_CHANGE) {
1328 err = got_fileindex_entry_update(ie, ondisk_path,
1329 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1330 } else if (status == GOT_STATUS_DELETE) {
1331 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1332 if (err)
1333 goto done;
1334 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1335 ondisk_path, path, blob, 0);
1336 if (err)
1337 goto done;
1338 } else {
1339 err = install_blob(worktree, ondisk_path, path, te->mode,
1340 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1341 repo, progress_cb, progress_arg);
1342 if (err)
1343 goto done;
1344 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1345 ondisk_path, path, blob, 1);
1346 if (err)
1347 goto done;
1349 got_object_blob_close(blob);
1350 done:
1351 free(ondisk_path);
1352 return err;
1355 static const struct got_error *
1356 remove_ondisk_file(const char *root_path, const char *path)
1358 const struct got_error *err = NULL;
1359 char *ondisk_path = NULL;
1361 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1362 return got_error_from_errno("asprintf");
1364 if (unlink(ondisk_path) == -1) {
1365 if (errno != ENOENT)
1366 err = got_error_from_errno2("unlink", ondisk_path);
1367 } else {
1368 char *parent = dirname(ondisk_path);
1369 while (parent && strcmp(parent, root_path) != 0) {
1370 if (rmdir(parent) == -1) {
1371 if (errno != ENOTEMPTY)
1372 err = got_error_from_errno2("rmdir",
1373 parent);
1374 break;
1376 parent = dirname(parent);
1379 free(ondisk_path);
1380 return err;
1383 static const struct got_error *
1384 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1385 struct got_fileindex_entry *ie, struct got_repository *repo,
1386 got_worktree_checkout_cb progress_cb, void *progress_arg)
1388 const struct got_error *err = NULL;
1389 unsigned char status;
1390 struct stat sb;
1391 char *ondisk_path;
1393 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1394 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1396 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1397 == -1)
1398 return got_error_from_errno("asprintf");
1400 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1401 if (err)
1402 return err;
1404 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1405 status == GOT_STATUS_ADD) {
1406 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1407 if (err)
1408 return err;
1410 * Preserve the working file and change the deleted blob's
1411 * entry into a schedule-add entry.
1413 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1414 0);
1415 if (err)
1416 return err;
1417 } else {
1418 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1419 if (err)
1420 return err;
1421 if (status == GOT_STATUS_NO_CHANGE) {
1422 err = remove_ondisk_file(worktree->root_path, ie->path);
1423 if (err)
1424 return err;
1426 got_fileindex_entry_remove(fileindex, ie);
1429 return err;
1432 struct diff_cb_arg {
1433 struct got_fileindex *fileindex;
1434 struct got_worktree *worktree;
1435 struct got_repository *repo;
1436 got_worktree_checkout_cb progress_cb;
1437 void *progress_arg;
1438 got_cancel_cb cancel_cb;
1439 void *cancel_arg;
1442 static const struct got_error *
1443 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1444 struct got_tree_entry *te, const char *parent_path)
1446 struct diff_cb_arg *a = arg;
1448 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1449 return got_error(GOT_ERR_CANCELLED);
1451 return update_blob(a->worktree, a->fileindex, ie, te,
1452 ie->path, a->repo, a->progress_cb, a->progress_arg);
1455 static const struct got_error *
1456 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1458 struct diff_cb_arg *a = arg;
1460 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1461 return got_error(GOT_ERR_CANCELLED);
1463 return delete_blob(a->worktree, a->fileindex, ie,
1464 a->repo, a->progress_cb, a->progress_arg);
1467 static const struct got_error *
1468 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1470 struct diff_cb_arg *a = arg;
1471 const struct got_error *err;
1472 char *path;
1474 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1475 return got_error(GOT_ERR_CANCELLED);
1477 if (got_object_tree_entry_is_submodule(te))
1478 return NULL;
1480 if (asprintf(&path, "%s%s%s", parent_path,
1481 parent_path[0] ? "/" : "", te->name)
1482 == -1)
1483 return got_error_from_errno("asprintf");
1485 if (S_ISDIR(te->mode))
1486 err = add_dir_on_disk(a->worktree, path);
1487 else
1488 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1489 a->repo, a->progress_cb, a->progress_arg);
1491 free(path);
1492 return err;
1495 static const struct got_error *
1496 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1498 const struct got_error *err = NULL;
1499 char *uuidstr = NULL;
1500 uint32_t uuid_status;
1502 *refname = NULL;
1504 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1505 if (uuid_status != uuid_s_ok)
1506 return got_error_uuid(uuid_status, "uuid_to_string");
1508 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1509 == -1) {
1510 err = got_error_from_errno("asprintf");
1511 *refname = NULL;
1513 free(uuidstr);
1514 return err;
1517 const struct got_error *
1518 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1520 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1523 static const struct got_error *
1524 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1526 return get_ref_name(refname, worktree,
1527 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1530 static const struct got_error *
1531 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1533 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1536 static const struct got_error *
1537 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1539 return get_ref_name(refname, worktree,
1540 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1543 static const struct got_error *
1544 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1546 return get_ref_name(refname, worktree,
1547 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1550 static const struct got_error *
1551 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree,
1554 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1557 static const struct got_error *
1558 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1560 return get_ref_name(refname, worktree,
1561 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1564 static const struct got_error *
1565 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1567 return get_ref_name(refname, worktree,
1568 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1571 static const struct got_error *
1572 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1574 return get_ref_name(refname, worktree,
1575 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1578 const struct got_error *
1579 got_worktree_get_histedit_script_path(char **path,
1580 struct got_worktree *worktree)
1582 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1583 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1584 *path = NULL;
1585 return got_error_from_errno("asprintf");
1587 return NULL;
1591 * Prevent Git's garbage collector from deleting our base commit by
1592 * setting a reference to our base commit's ID.
1594 static const struct got_error *
1595 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 struct got_reference *ref = NULL;
1599 char *refname;
1601 err = got_worktree_get_base_ref_name(&refname, worktree);
1602 if (err)
1603 return err;
1605 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1606 if (err)
1607 goto done;
1609 err = got_ref_write(ref, repo);
1610 done:
1611 free(refname);
1612 if (ref)
1613 got_ref_close(ref);
1614 return err;
1617 static const struct got_error *
1618 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1620 const struct got_error *err = NULL;
1622 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1623 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1624 err = got_error_from_errno("asprintf");
1625 *fileindex_path = NULL;
1627 return err;
1631 static const struct got_error *
1632 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1633 struct got_worktree *worktree)
1635 const struct got_error *err = NULL;
1636 FILE *index = NULL;
1638 *fileindex_path = NULL;
1639 *fileindex = got_fileindex_alloc();
1640 if (*fileindex == NULL)
1641 return got_error_from_errno("got_fileindex_alloc");
1643 err = get_fileindex_path(fileindex_path, worktree);
1644 if (err)
1645 goto done;
1647 index = fopen(*fileindex_path, "rb");
1648 if (index == NULL) {
1649 if (errno != ENOENT)
1650 err = got_error_from_errno2("fopen", *fileindex_path);
1651 } else {
1652 err = got_fileindex_read(*fileindex, index);
1653 if (fclose(index) != 0 && err == NULL)
1654 err = got_error_from_errno("fclose");
1656 done:
1657 if (err) {
1658 free(*fileindex_path);
1659 *fileindex_path = NULL;
1660 got_fileindex_free(*fileindex);
1661 *fileindex = NULL;
1663 return err;
1666 struct bump_base_commit_id_arg {
1667 struct got_object_id *base_commit_id;
1668 const char *path;
1669 size_t path_len;
1670 const char *entry_name;
1671 got_worktree_checkout_cb progress_cb;
1672 void *progress_arg;
1675 /* Bump base commit ID of all files within an updated part of the work tree. */
1676 static const struct got_error *
1677 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1679 const struct got_error *err;
1680 struct bump_base_commit_id_arg *a = arg;
1682 if (a->entry_name) {
1683 if (strcmp(ie->path, a->path) != 0)
1684 return NULL;
1685 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1686 return NULL;
1688 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1689 SHA1_DIGEST_LENGTH) == 0)
1690 return NULL;
1692 if (a->progress_cb) {
1693 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1694 ie->path);
1695 if (err)
1696 return err;
1698 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1699 return NULL;
1702 static const struct got_error *
1703 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1705 const struct got_error *err = NULL;
1706 char *new_fileindex_path = NULL;
1707 FILE *new_index = NULL;
1709 err = got_opentemp_named(&new_fileindex_path, &new_index,
1710 fileindex_path);
1711 if (err)
1712 goto done;
1714 err = got_fileindex_write(fileindex, new_index);
1715 if (err)
1716 goto done;
1718 if (rename(new_fileindex_path, fileindex_path) != 0) {
1719 err = got_error_from_errno3("rename", new_fileindex_path,
1720 fileindex_path);
1721 unlink(new_fileindex_path);
1723 done:
1724 if (new_index)
1725 fclose(new_index);
1726 free(new_fileindex_path);
1727 return err;
1730 static const struct got_error *
1731 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1732 struct got_object_id **tree_id, const char *wt_relpath,
1733 struct got_worktree *worktree, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_object_id *id = NULL;
1737 char *in_repo_path = NULL;
1738 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1740 *entry_type = GOT_OBJ_TYPE_ANY;
1741 *tree_relpath = NULL;
1742 *tree_id = NULL;
1744 if (wt_relpath[0] == '\0') {
1745 /* Check out all files within the work tree. */
1746 *entry_type = GOT_OBJ_TYPE_TREE;
1747 *tree_relpath = strdup("");
1748 if (*tree_relpath == NULL) {
1749 err = got_error_from_errno("strdup");
1750 goto done;
1752 err = got_object_id_by_path(tree_id, repo,
1753 worktree->base_commit_id, worktree->path_prefix);
1754 if (err)
1755 goto done;
1756 return NULL;
1759 /* Check out a subset of files in the work tree. */
1761 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1762 is_root_wt ? "" : "/", wt_relpath) == -1) {
1763 err = got_error_from_errno("asprintf");
1764 goto done;
1767 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1768 in_repo_path);
1769 if (err)
1770 goto done;
1772 free(in_repo_path);
1773 in_repo_path = NULL;
1775 err = got_object_get_type(entry_type, repo, id);
1776 if (err)
1777 goto done;
1779 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1780 /* Check out a single file. */
1781 if (strchr(wt_relpath, '/') == NULL) {
1782 /* Check out a single file in work tree's root dir. */
1783 in_repo_path = strdup(worktree->path_prefix);
1784 if (in_repo_path == NULL) {
1785 err = got_error_from_errno("strdup");
1786 goto done;
1788 *tree_relpath = strdup("");
1789 if (*tree_relpath == NULL) {
1790 err = got_error_from_errno("strdup");
1791 goto done;
1793 } else {
1794 /* Check out a single file in a subdirectory. */
1795 err = got_path_dirname(tree_relpath, wt_relpath);
1796 if (err)
1797 return err;
1798 if (asprintf(&in_repo_path, "%s%s%s",
1799 worktree->path_prefix, is_root_wt ? "" : "/",
1800 *tree_relpath) == -1) {
1801 err = got_error_from_errno("asprintf");
1802 goto done;
1805 err = got_object_id_by_path(tree_id, repo,
1806 worktree->base_commit_id, in_repo_path);
1807 } else {
1808 /* Check out all files within a subdirectory. */
1809 *tree_id = got_object_id_dup(id);
1810 if (*tree_id == NULL) {
1811 err = got_error_from_errno("got_object_id_dup");
1812 goto done;
1814 *tree_relpath = strdup(wt_relpath);
1815 if (*tree_relpath == NULL) {
1816 err = got_error_from_errno("strdup");
1817 goto done;
1820 done:
1821 free(id);
1822 free(in_repo_path);
1823 if (err) {
1824 *entry_type = GOT_OBJ_TYPE_ANY;
1825 free(*tree_relpath);
1826 *tree_relpath = NULL;
1827 free(*tree_id);
1828 *tree_id = NULL;
1830 return err;
1833 static const struct got_error *
1834 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1835 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1837 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1839 const struct got_error *err = NULL;
1840 struct got_commit_object *commit = NULL;
1841 struct got_tree_object *tree = NULL;
1842 struct got_fileindex_diff_tree_cb diff_cb;
1843 struct diff_cb_arg arg;
1845 err = ref_base_commit(worktree, repo);
1846 if (err)
1847 goto done;
1849 err = got_object_open_as_commit(&commit, repo,
1850 worktree->base_commit_id);
1851 if (err)
1852 goto done;
1854 err = got_object_open_as_tree(&tree, repo, tree_id);
1855 if (err)
1856 goto done;
1858 if (entry_name &&
1859 got_object_tree_find_entry(tree, entry_name) == NULL) {
1860 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1861 goto done;
1864 diff_cb.diff_old_new = diff_old_new;
1865 diff_cb.diff_old = diff_old;
1866 diff_cb.diff_new = diff_new;
1867 arg.fileindex = fileindex;
1868 arg.worktree = worktree;
1869 arg.repo = repo;
1870 arg.progress_cb = progress_cb;
1871 arg.progress_arg = progress_arg;
1872 arg.cancel_cb = cancel_cb;
1873 arg.cancel_arg = cancel_arg;
1874 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1875 entry_name, repo, &diff_cb, &arg);
1876 done:
1877 if (tree)
1878 got_object_tree_close(tree);
1879 if (commit)
1880 got_object_commit_close(commit);
1881 return err;
1884 const struct got_error *
1885 got_worktree_checkout_files(struct got_worktree *worktree,
1886 struct got_pathlist_head *paths, struct got_repository *repo,
1887 got_worktree_checkout_cb progress_cb, void *progress_arg,
1888 got_cancel_cb cancel_cb, void *cancel_arg)
1890 const struct got_error *err = NULL, *sync_err, *unlockerr;
1891 struct got_commit_object *commit = NULL;
1892 struct got_tree_object *tree = NULL;
1893 struct got_fileindex *fileindex = NULL;
1894 char *fileindex_path = NULL;
1895 struct got_pathlist_entry *pe;
1896 struct tree_path_data {
1897 SIMPLEQ_ENTRY(tree_path_data) entry;
1898 struct got_object_id *tree_id;
1899 int entry_type;
1900 char *relpath;
1901 char *entry_name;
1902 } *tpd = NULL;
1903 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1905 SIMPLEQ_INIT(&tree_paths);
1907 err = lock_worktree(worktree, LOCK_EX);
1908 if (err)
1909 return err;
1911 /* Map all specified paths to in-repository trees. */
1912 TAILQ_FOREACH(pe, paths, entry) {
1913 tpd = malloc(sizeof(*tpd));
1914 if (tpd == NULL) {
1915 err = got_error_from_errno("malloc");
1916 goto done;
1919 err = find_tree_entry_for_checkout(&tpd->entry_type,
1920 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1921 if (err) {
1922 free(tpd);
1923 goto done;
1926 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1927 err = got_path_basename(&tpd->entry_name, pe->path);
1928 if (err) {
1929 free(tpd->relpath);
1930 free(tpd->tree_id);
1931 free(tpd);
1932 goto done;
1934 } else
1935 tpd->entry_name = NULL;
1937 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1941 * Read the file index.
1942 * Checking out files is supposed to be an idempotent operation.
1943 * If the on-disk file index is incomplete we will try to complete it.
1945 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1946 if (err)
1947 goto done;
1949 tpd = SIMPLEQ_FIRST(&tree_paths);
1950 TAILQ_FOREACH(pe, paths, entry) {
1951 struct bump_base_commit_id_arg bbc_arg;
1953 err = checkout_files(worktree, fileindex, tpd->relpath,
1954 tpd->tree_id, tpd->entry_name, repo,
1955 progress_cb, progress_arg, cancel_cb, cancel_arg);
1956 if (err)
1957 break;
1959 bbc_arg.base_commit_id = worktree->base_commit_id;
1960 bbc_arg.entry_name = tpd->entry_name;
1961 bbc_arg.path = pe->path;
1962 bbc_arg.path_len = pe->path_len;
1963 bbc_arg.progress_cb = progress_cb;
1964 bbc_arg.progress_arg = progress_arg;
1965 err = got_fileindex_for_each_entry_safe(fileindex,
1966 bump_base_commit_id, &bbc_arg);
1967 if (err)
1968 break;
1970 tpd = SIMPLEQ_NEXT(tpd, entry);
1972 sync_err = sync_fileindex(fileindex, fileindex_path);
1973 if (sync_err && err == NULL)
1974 err = sync_err;
1975 done:
1976 free(fileindex_path);
1977 if (tree)
1978 got_object_tree_close(tree);
1979 if (commit)
1980 got_object_commit_close(commit);
1981 if (fileindex)
1982 got_fileindex_free(fileindex);
1983 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1984 tpd = SIMPLEQ_FIRST(&tree_paths);
1985 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1986 free(tpd->relpath);
1987 free(tpd->tree_id);
1988 free(tpd);
1990 unlockerr = lock_worktree(worktree, LOCK_SH);
1991 if (unlockerr && err == NULL)
1992 err = unlockerr;
1993 return err;
1996 struct merge_file_cb_arg {
1997 struct got_worktree *worktree;
1998 struct got_fileindex *fileindex;
1999 got_worktree_checkout_cb progress_cb;
2000 void *progress_arg;
2001 got_cancel_cb cancel_cb;
2002 void *cancel_arg;
2003 const char *label_orig;
2004 struct got_object_id *commit_id2;
2007 static const struct got_error *
2008 merge_file_cb(void *arg, struct got_blob_object *blob1,
2009 struct got_blob_object *blob2, struct got_object_id *id1,
2010 struct got_object_id *id2, const char *path1, const char *path2,
2011 mode_t mode1, mode_t mode2, struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct merge_file_cb_arg *a = arg;
2015 struct got_fileindex_entry *ie;
2016 char *ondisk_path = NULL;
2017 struct stat sb;
2018 unsigned char status;
2019 int local_changes_subsumed;
2021 if (blob1 && blob2) {
2022 ie = got_fileindex_entry_get(a->fileindex, path2,
2023 strlen(path2));
2024 if (ie == NULL)
2025 return (*a->progress_cb)(a->progress_arg,
2026 GOT_STATUS_MISSING, path2);
2028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2029 path2) == -1)
2030 return got_error_from_errno("asprintf");
2032 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2033 if (err)
2034 goto done;
2036 if (status == GOT_STATUS_DELETE) {
2037 err = (*a->progress_cb)(a->progress_arg,
2038 GOT_STATUS_MERGE, path2);
2039 goto done;
2041 if (status != GOT_STATUS_NO_CHANGE &&
2042 status != GOT_STATUS_MODIFY &&
2043 status != GOT_STATUS_CONFLICT &&
2044 status != GOT_STATUS_ADD) {
2045 err = (*a->progress_cb)(a->progress_arg, status, path2);
2046 goto done;
2049 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2050 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2051 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2052 } else if (blob1) {
2053 ie = got_fileindex_entry_get(a->fileindex, path1,
2054 strlen(path1));
2055 if (ie == NULL)
2056 return (*a->progress_cb)(a->progress_arg,
2057 GOT_STATUS_MISSING, path2);
2059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2060 path1) == -1)
2061 return got_error_from_errno("asprintf");
2063 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2064 if (err)
2065 goto done;
2067 switch (status) {
2068 case GOT_STATUS_NO_CHANGE:
2069 err = (*a->progress_cb)(a->progress_arg,
2070 GOT_STATUS_DELETE, path1);
2071 if (err)
2072 goto done;
2073 err = remove_ondisk_file(a->worktree->root_path, path1);
2074 if (err)
2075 goto done;
2076 if (ie)
2077 got_fileindex_entry_mark_deleted_from_disk(ie);
2078 break;
2079 case GOT_STATUS_DELETE:
2080 case GOT_STATUS_MISSING:
2081 err = (*a->progress_cb)(a->progress_arg,
2082 GOT_STATUS_DELETE, path1);
2083 if (err)
2084 goto done;
2085 if (ie)
2086 got_fileindex_entry_mark_deleted_from_disk(ie);
2087 break;
2088 case GOT_STATUS_ADD:
2089 case GOT_STATUS_MODIFY:
2090 case GOT_STATUS_CONFLICT:
2091 err = (*a->progress_cb)(a->progress_arg,
2092 GOT_STATUS_CANNOT_DELETE, path1);
2093 if (err)
2094 goto done;
2095 break;
2096 case GOT_STATUS_OBSTRUCTED:
2097 err = (*a->progress_cb)(a->progress_arg, status, path1);
2098 if (err)
2099 goto done;
2100 break;
2101 default:
2102 break;
2104 } else if (blob2) {
2105 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2106 path2) == -1)
2107 return got_error_from_errno("asprintf");
2108 ie = got_fileindex_entry_get(a->fileindex, path2,
2109 strlen(path2));
2110 if (ie) {
2111 err = get_file_status(&status, &sb, ie, ondisk_path,
2112 repo);
2113 if (err)
2114 goto done;
2115 if (status != GOT_STATUS_NO_CHANGE &&
2116 status != GOT_STATUS_MODIFY &&
2117 status != GOT_STATUS_CONFLICT &&
2118 status != GOT_STATUS_ADD) {
2119 err = (*a->progress_cb)(a->progress_arg,
2120 status, path2);
2121 goto done;
2123 err = merge_blob(&local_changes_subsumed, a->worktree,
2124 NULL, ondisk_path, path2, sb.st_mode,
2125 a->label_orig, blob2, a->commit_id2, repo,
2126 a->progress_cb,
2127 a->progress_arg);
2128 if (status == GOT_STATUS_DELETE) {
2129 err = update_blob_fileindex_entry(a->worktree,
2130 a->fileindex, ie, ondisk_path, ie->path,
2131 blob2, 0);
2132 if (err)
2133 goto done;
2135 } else {
2136 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2137 err = install_blob(a->worktree, ondisk_path, path2,
2138 /* XXX get this from parent tree! */
2139 GOT_DEFAULT_FILE_MODE,
2140 sb.st_mode, blob2, 0, 0, repo,
2141 a->progress_cb, a->progress_arg);
2142 if (err)
2143 goto done;
2144 err = got_fileindex_entry_alloc(&ie,
2145 ondisk_path, path2, NULL, NULL);
2146 if (err)
2147 goto done;
2148 err = got_fileindex_entry_add(a->fileindex, ie);
2149 if (err) {
2150 got_fileindex_entry_free(ie);
2151 goto done;
2155 done:
2156 free(ondisk_path);
2157 return err;
2160 struct check_merge_ok_arg {
2161 struct got_worktree *worktree;
2162 struct got_repository *repo;
2165 static const struct got_error *
2166 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2168 const struct got_error *err = NULL;
2169 struct check_merge_ok_arg *a = arg;
2170 unsigned char status;
2171 struct stat sb;
2172 char *ondisk_path;
2174 /* Reject merges into a work tree with mixed base commits. */
2175 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2176 SHA1_DIGEST_LENGTH))
2177 return got_error(GOT_ERR_MIXED_COMMITS);
2179 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2180 == -1)
2181 return got_error_from_errno("asprintf");
2183 /* Reject merges into a work tree with conflicted files. */
2184 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2185 if (err)
2186 return err;
2187 if (status == GOT_STATUS_CONFLICT)
2188 return got_error(GOT_ERR_CONFLICTS);
2190 return NULL;
2193 static const struct got_error *
2194 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2195 const char *fileindex_path, struct got_object_id *commit_id1,
2196 struct got_object_id *commit_id2, struct got_repository *repo,
2197 got_worktree_checkout_cb progress_cb, void *progress_arg,
2198 got_cancel_cb cancel_cb, void *cancel_arg)
2200 const struct got_error *err = NULL, *sync_err;
2201 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2202 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2203 struct merge_file_cb_arg arg;
2204 char *label_orig = NULL;
2206 if (commit_id1) {
2207 char *id_str;
2209 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2210 worktree->path_prefix);
2211 if (err)
2212 goto done;
2214 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2215 if (err)
2216 goto done;
2218 err = got_object_id_str(&id_str, commit_id1);
2219 if (err)
2220 goto done;
2222 if (asprintf(&label_orig, "%s: commit %s",
2223 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2224 err = got_error_from_errno("asprintf");
2225 free(id_str);
2226 goto done;
2228 free(id_str);
2231 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2232 worktree->path_prefix);
2233 if (err)
2234 goto done;
2236 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2237 if (err)
2238 goto done;
2240 arg.worktree = worktree;
2241 arg.fileindex = fileindex;
2242 arg.progress_cb = progress_cb;
2243 arg.progress_arg = progress_arg;
2244 arg.cancel_cb = cancel_cb;
2245 arg.cancel_arg = cancel_arg;
2246 arg.label_orig = label_orig;
2247 arg.commit_id2 = commit_id2;
2248 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2249 sync_err = sync_fileindex(fileindex, fileindex_path);
2250 if (sync_err && err == NULL)
2251 err = sync_err;
2252 done:
2253 if (tree1)
2254 got_object_tree_close(tree1);
2255 if (tree2)
2256 got_object_tree_close(tree2);
2257 free(label_orig);
2258 return err;
2261 const struct got_error *
2262 got_worktree_merge_files(struct got_worktree *worktree,
2263 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2264 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2265 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2267 const struct got_error *err, *unlockerr;
2268 char *fileindex_path = NULL;
2269 struct got_fileindex *fileindex = NULL;
2270 struct check_merge_ok_arg mok_arg;
2272 err = lock_worktree(worktree, LOCK_EX);
2273 if (err)
2274 return err;
2276 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2277 if (err)
2278 goto done;
2280 mok_arg.worktree = worktree;
2281 mok_arg.repo = repo;
2282 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2283 &mok_arg);
2284 if (err)
2285 goto done;
2287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2288 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2289 done:
2290 if (fileindex)
2291 got_fileindex_free(fileindex);
2292 free(fileindex_path);
2293 unlockerr = lock_worktree(worktree, LOCK_SH);
2294 if (unlockerr && err == NULL)
2295 err = unlockerr;
2296 return err;
2299 struct diff_dir_cb_arg {
2300 struct got_fileindex *fileindex;
2301 struct got_worktree *worktree;
2302 const char *status_path;
2303 size_t status_path_len;
2304 struct got_repository *repo;
2305 got_worktree_status_cb status_cb;
2306 void *status_arg;
2307 got_cancel_cb cancel_cb;
2308 void *cancel_arg;
2309 /* A pathlist containing per-directory pathlists of ignore patterns. */
2310 struct got_pathlist_head ignores;
2311 int report_unchanged;
2314 static const struct got_error *
2315 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2316 got_worktree_status_cb status_cb, void *status_arg,
2317 struct got_repository *repo, int report_unchanged)
2319 const struct got_error *err = NULL;
2320 unsigned char status = GOT_STATUS_NO_CHANGE;
2321 unsigned char staged_status = get_staged_status(ie);
2322 struct stat sb;
2323 struct got_object_id blob_id, commit_id, staged_blob_id;
2324 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2325 struct got_object_id *staged_blob_idp = NULL;
2327 err = get_file_status(&status, &sb, ie, abspath, repo);
2328 if (err)
2329 return err;
2331 if (status == GOT_STATUS_NO_CHANGE &&
2332 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2333 return NULL;
2335 if (got_fileindex_entry_has_blob(ie)) {
2336 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2337 blob_idp = &blob_id;
2339 if (got_fileindex_entry_has_commit(ie)) {
2340 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2341 commit_idp = &commit_id;
2343 if (staged_status == GOT_STATUS_ADD ||
2344 staged_status == GOT_STATUS_MODIFY) {
2345 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2346 SHA1_DIGEST_LENGTH);
2347 staged_blob_idp = &staged_blob_id;
2350 return (*status_cb)(status_arg, status, staged_status,
2351 ie->path, blob_idp, staged_blob_idp, commit_idp);
2354 static const struct got_error *
2355 status_old_new(void *arg, struct got_fileindex_entry *ie,
2356 struct dirent *de, const char *parent_path)
2358 const struct got_error *err = NULL;
2359 struct diff_dir_cb_arg *a = arg;
2360 char *abspath;
2362 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2363 return got_error(GOT_ERR_CANCELLED);
2365 if (got_path_cmp(parent_path, a->status_path,
2366 strlen(parent_path), a->status_path_len) != 0 &&
2367 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2368 return NULL;
2370 if (parent_path[0]) {
2371 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2372 parent_path, de->d_name) == -1)
2373 return got_error_from_errno("asprintf");
2374 } else {
2375 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2376 de->d_name) == -1)
2377 return got_error_from_errno("asprintf");
2380 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2381 a->repo, a->report_unchanged);
2382 free(abspath);
2383 return err;
2386 static const struct got_error *
2387 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2389 struct diff_dir_cb_arg *a = arg;
2390 struct got_object_id blob_id, commit_id;
2391 unsigned char status;
2393 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2394 return got_error(GOT_ERR_CANCELLED);
2396 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2397 return NULL;
2399 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2400 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2401 if (got_fileindex_entry_has_file_on_disk(ie))
2402 status = GOT_STATUS_MISSING;
2403 else
2404 status = GOT_STATUS_DELETE;
2405 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2406 ie->path, &blob_id, NULL, &commit_id);
2409 void
2410 free_ignorelist(struct got_pathlist_head *ignorelist)
2412 struct got_pathlist_entry *pe;
2414 TAILQ_FOREACH(pe, ignorelist, entry)
2415 free((char *)pe->path);
2416 got_pathlist_free(ignorelist);
2419 void
2420 free_ignores(struct got_pathlist_head *ignores)
2422 struct got_pathlist_entry *pe;
2424 TAILQ_FOREACH(pe, ignores, entry) {
2425 struct got_pathlist_head *ignorelist = pe->data;
2426 free_ignorelist(ignorelist);
2427 free((char *)pe->path);
2429 got_pathlist_free(ignores);
2432 static const struct got_error *
2433 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2435 const struct got_error *err = NULL;
2436 struct got_pathlist_entry *pe = NULL;
2437 struct got_pathlist_head *ignorelist;
2438 char *line = NULL, *pattern, *dirpath = NULL;
2439 size_t linesize = 0;
2440 ssize_t linelen;
2442 ignorelist = calloc(1, sizeof(*ignorelist));
2443 if (ignorelist == NULL)
2444 return got_error_from_errno("calloc");
2445 TAILQ_INIT(ignorelist);
2447 while ((linelen = getline(&line, &linesize, f)) != -1) {
2448 if (linelen > 0 && line[linelen - 1] == '\n')
2449 line[linelen - 1] = '\0';
2451 /* Git's ignores may contain comments. */
2452 if (line[0] == '#')
2453 continue;
2455 /* Git's negated patterns are not (yet?) supported. */
2456 if (line[0] == '!')
2457 continue;
2459 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2460 line) == -1) {
2461 err = got_error_from_errno("asprintf");
2462 goto done;
2464 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2465 if (err)
2466 goto done;
2468 if (ferror(f)) {
2469 err = got_error_from_errno("getline");
2470 goto done;
2473 dirpath = strdup(path);
2474 if (dirpath == NULL) {
2475 err = got_error_from_errno("strdup");
2476 goto done;
2478 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2479 done:
2480 free(line);
2481 if (err || pe == NULL) {
2482 free(dirpath);
2483 free_ignorelist(ignorelist);
2485 return err;
2488 int
2489 match_ignores(struct got_pathlist_head *ignores, const char *path)
2491 struct got_pathlist_entry *pe;
2493 /* Handle patterns which match in all directories. */
2494 TAILQ_FOREACH(pe, ignores, entry) {
2495 struct got_pathlist_head *ignorelist = pe->data;
2496 struct got_pathlist_entry *pi;
2498 TAILQ_FOREACH(pi, ignorelist, entry) {
2499 const char *p, *pattern = pi->path;
2501 if (strncmp(pattern, "**/", 3) != 0)
2502 continue;
2503 pattern += 3;
2504 p = path;
2505 while (*p) {
2506 if (fnmatch(pattern, p,
2507 FNM_PATHNAME | FNM_LEADING_DIR)) {
2508 /* Retry in next directory. */
2509 while (*p && *p != '/')
2510 p++;
2511 while (*p == '/')
2512 p++;
2513 continue;
2515 return 1;
2521 * The ignores pathlist contains ignore lists from children before
2522 * parents, so we can find the most specific ignorelist by walking
2523 * ignores backwards.
2525 pe = TAILQ_LAST(ignores, got_pathlist_head);
2526 while (pe) {
2527 if (got_path_is_child(path, pe->path, pe->path_len)) {
2528 struct got_pathlist_head *ignorelist = pe->data;
2529 struct got_pathlist_entry *pi;
2530 TAILQ_FOREACH(pi, ignorelist, entry) {
2531 const char *pattern = pi->path;
2532 int flags = FNM_LEADING_DIR;
2533 if (strstr(pattern, "/**/") == NULL)
2534 flags |= FNM_PATHNAME;
2535 if (fnmatch(pattern, path, flags))
2536 continue;
2537 return 1;
2540 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2543 return 0;
2546 static const struct got_error *
2547 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2548 const char *path, const char *ignores_filename)
2550 const struct got_error *err = NULL;
2551 char *ignorespath;
2552 FILE *ignoresfile = NULL;
2554 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2555 path[0] ? "/" : "", ignores_filename) == -1)
2556 return got_error_from_errno("asprintf");
2558 ignoresfile = fopen(ignorespath, "r");
2559 if (ignoresfile == NULL) {
2560 if (errno != ENOENT && errno != EACCES)
2561 err = got_error_from_errno2("fopen",
2562 ignorespath);
2563 } else
2564 err = read_ignores(ignores, path, ignoresfile);
2566 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2567 err = got_error_from_errno2("fclose", path);
2568 free(ignorespath);
2569 return err;
2572 static const struct got_error *
2573 status_new(void *arg, struct dirent *de, const char *parent_path)
2575 const struct got_error *err = NULL;
2576 struct diff_dir_cb_arg *a = arg;
2577 char *path = NULL;
2579 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2580 return got_error(GOT_ERR_CANCELLED);
2582 /* XXX ignore symlinks for now */
2583 if (de->d_type == DT_LNK)
2584 return NULL;
2586 if (parent_path[0]) {
2587 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2588 return got_error_from_errno("asprintf");
2589 } else {
2590 path = de->d_name;
2593 if (de->d_type == DT_DIR) {
2594 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2595 ".cvsignore");
2596 if (err == NULL)
2597 err = add_ignores(&a->ignores, a->worktree->root_path,
2598 path, ".gitignore");
2600 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2601 && !match_ignores(&a->ignores, path))
2602 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2603 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2604 if (parent_path[0])
2605 free(path);
2606 return err;
2609 static const struct got_error *
2610 report_single_file_status(const char *path, const char *ondisk_path,
2611 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2612 void *status_arg, struct got_repository *repo, int report_unchanged)
2614 struct got_fileindex_entry *ie;
2615 struct stat sb;
2617 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2618 if (ie)
2619 return report_file_status(ie, ondisk_path, status_cb,
2620 status_arg, repo, report_unchanged);
2622 if (lstat(ondisk_path, &sb) == -1) {
2623 if (errno != ENOENT)
2624 return got_error_from_errno2("lstat", ondisk_path);
2625 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2626 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2627 return NULL;
2630 if (S_ISREG(sb.st_mode))
2631 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2632 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2634 return NULL;
2637 static const struct got_error *
2638 worktree_status(struct got_worktree *worktree, const char *path,
2639 struct got_fileindex *fileindex, struct got_repository *repo,
2640 got_worktree_status_cb status_cb, void *status_arg,
2641 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2642 int report_unchanged)
2644 const struct got_error *err = NULL;
2645 DIR *workdir = NULL;
2646 struct got_fileindex_diff_dir_cb fdiff_cb;
2647 struct diff_dir_cb_arg arg;
2648 char *ondisk_path = NULL;
2650 if (asprintf(&ondisk_path, "%s%s%s",
2651 worktree->root_path, path[0] ? "/" : "", path) == -1)
2652 return got_error_from_errno("asprintf");
2654 workdir = opendir(ondisk_path);
2655 if (workdir == NULL) {
2656 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2657 err = got_error_from_errno2("opendir", ondisk_path);
2658 else
2659 err = report_single_file_status(path, ondisk_path,
2660 fileindex, status_cb, status_arg, repo,
2661 report_unchanged);
2662 } else {
2663 fdiff_cb.diff_old_new = status_old_new;
2664 fdiff_cb.diff_old = status_old;
2665 fdiff_cb.diff_new = status_new;
2666 arg.fileindex = fileindex;
2667 arg.worktree = worktree;
2668 arg.status_path = path;
2669 arg.status_path_len = strlen(path);
2670 arg.repo = repo;
2671 arg.status_cb = status_cb;
2672 arg.status_arg = status_arg;
2673 arg.cancel_cb = cancel_cb;
2674 arg.cancel_arg = cancel_arg;
2675 arg.report_unchanged = report_unchanged;
2676 TAILQ_INIT(&arg.ignores);
2677 if (!no_ignores) {
2678 err = add_ignores(&arg.ignores, worktree->root_path,
2679 path, ".cvsignore");
2680 if (err == NULL)
2681 err = add_ignores(&arg.ignores,
2682 worktree->root_path, path, ".gitignore");
2684 if (err == NULL)
2685 err = got_fileindex_diff_dir(fileindex, workdir,
2686 worktree->root_path, path, repo, &fdiff_cb, &arg);
2687 free_ignores(&arg.ignores);
2690 if (workdir)
2691 closedir(workdir);
2692 free(ondisk_path);
2693 return err;
2696 const struct got_error *
2697 got_worktree_status(struct got_worktree *worktree,
2698 struct got_pathlist_head *paths, struct got_repository *repo,
2699 got_worktree_status_cb status_cb, void *status_arg,
2700 got_cancel_cb cancel_cb, void *cancel_arg)
2702 const struct got_error *err = NULL;
2703 char *fileindex_path = NULL;
2704 struct got_fileindex *fileindex = NULL;
2705 struct got_pathlist_entry *pe;
2707 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2708 if (err)
2709 return err;
2711 TAILQ_FOREACH(pe, paths, entry) {
2712 err = worktree_status(worktree, pe->path, fileindex, repo,
2713 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2714 if (err)
2715 break;
2717 free(fileindex_path);
2718 got_fileindex_free(fileindex);
2719 return err;
2722 const struct got_error *
2723 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2724 const char *arg)
2726 const struct got_error *err = NULL;
2727 char *resolved, *cwd = NULL, *path = NULL;
2728 size_t len;
2730 *wt_path = NULL;
2732 resolved = realpath(arg, NULL);
2733 if (resolved == NULL) {
2734 if (errno != ENOENT)
2735 return got_error_from_errno2("realpath", arg);
2736 cwd = getcwd(NULL, 0);
2737 if (cwd == NULL)
2738 return got_error_from_errno("getcwd");
2739 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2740 err = got_error_from_errno("asprintf");
2741 goto done;
2745 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2746 strlen(got_worktree_get_root_path(worktree)))) {
2747 err = got_error(GOT_ERR_BAD_PATH);
2748 goto done;
2751 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2752 err = got_path_skip_common_ancestor(&path,
2753 got_worktree_get_root_path(worktree), resolved);
2754 if (err)
2755 goto done;
2756 } else {
2757 path = strdup("");
2758 if (path == NULL) {
2759 err = got_error_from_errno("strdup");
2760 goto done;
2764 /* XXX status walk can't deal with trailing slash! */
2765 len = strlen(path);
2766 while (len > 0 && path[len - 1] == '/') {
2767 path[len - 1] = '\0';
2768 len--;
2770 done:
2771 free(resolved);
2772 free(cwd);
2773 if (err == NULL)
2774 *wt_path = path;
2775 else
2776 free(path);
2777 return err;
2780 struct schedule_addition_args {
2781 struct got_worktree *worktree;
2782 struct got_fileindex *fileindex;
2783 got_worktree_checkout_cb progress_cb;
2784 void *progress_arg;
2785 struct got_repository *repo;
2788 static const struct got_error *
2789 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2790 const char *relpath, struct got_object_id *blob_id,
2791 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2793 struct schedule_addition_args *a = arg;
2794 const struct got_error *err = NULL;
2795 struct got_fileindex_entry *ie;
2796 struct stat sb;
2797 char *ondisk_path;
2799 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2800 relpath) == -1)
2801 return got_error_from_errno("asprintf");
2803 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2804 if (ie) {
2805 err = get_file_status(&status, &sb, ie, ondisk_path,
2806 a->repo);
2807 if (err)
2808 goto done;
2809 /* Re-adding an existing entry is a no-op. */
2810 if (status == GOT_STATUS_ADD)
2811 goto done;
2812 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2813 if (err)
2814 goto done;
2817 if (status != GOT_STATUS_UNVERSIONED) {
2818 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2819 goto done;
2822 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2823 if (err)
2824 goto done;
2826 err = got_fileindex_entry_add(a->fileindex, ie);
2827 if (err) {
2828 got_fileindex_entry_free(ie);
2829 goto done;
2831 done:
2832 free(ondisk_path);
2833 if (err)
2834 return err;
2835 if (status == GOT_STATUS_ADD)
2836 return NULL;
2837 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2840 const struct got_error *
2841 got_worktree_schedule_add(struct got_worktree *worktree,
2842 struct got_pathlist_head *paths,
2843 got_worktree_checkout_cb progress_cb, void *progress_arg,
2844 struct got_repository *repo, int no_ignores)
2846 struct got_fileindex *fileindex = NULL;
2847 char *fileindex_path = NULL;
2848 const struct got_error *err = NULL, *sync_err, *unlockerr;
2849 struct got_pathlist_entry *pe;
2850 struct schedule_addition_args saa;
2852 err = lock_worktree(worktree, LOCK_EX);
2853 if (err)
2854 return err;
2856 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2857 if (err)
2858 goto done;
2860 saa.worktree = worktree;
2861 saa.fileindex = fileindex;
2862 saa.progress_cb = progress_cb;
2863 saa.progress_arg = progress_arg;
2864 saa.repo = repo;
2866 TAILQ_FOREACH(pe, paths, entry) {
2867 err = worktree_status(worktree, pe->path, fileindex, repo,
2868 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2869 if (err)
2870 break;
2872 sync_err = sync_fileindex(fileindex, fileindex_path);
2873 if (sync_err && err == NULL)
2874 err = sync_err;
2875 done:
2876 free(fileindex_path);
2877 if (fileindex)
2878 got_fileindex_free(fileindex);
2879 unlockerr = lock_worktree(worktree, LOCK_SH);
2880 if (unlockerr && err == NULL)
2881 err = unlockerr;
2882 return err;
2885 struct schedule_deletion_args {
2886 struct got_worktree *worktree;
2887 struct got_fileindex *fileindex;
2888 got_worktree_delete_cb progress_cb;
2889 void *progress_arg;
2890 struct got_repository *repo;
2891 int delete_local_mods;
2894 static const struct got_error *
2895 schedule_for_deletion(void *arg, unsigned char status,
2896 unsigned char staged_status, const char *relpath,
2897 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2898 struct got_object_id *commit_id)
2900 struct schedule_deletion_args *a = arg;
2901 const struct got_error *err = NULL;
2902 struct got_fileindex_entry *ie = NULL;
2903 struct stat sb;
2904 char *ondisk_path;
2906 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2907 if (ie == NULL)
2908 return got_error(GOT_ERR_BAD_PATH);
2910 staged_status = get_staged_status(ie);
2911 if (staged_status != GOT_STATUS_NO_CHANGE) {
2912 if (staged_status == GOT_STATUS_DELETE)
2913 return NULL;
2914 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2917 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2918 relpath) == -1)
2919 return got_error_from_errno("asprintf");
2921 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2922 if (err)
2923 goto done;
2925 if (status != GOT_STATUS_NO_CHANGE) {
2926 if (status == GOT_STATUS_DELETE)
2927 goto done;
2928 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2929 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2930 goto done;
2932 if (status != GOT_STATUS_MODIFY &&
2933 status != GOT_STATUS_MISSING) {
2934 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2935 goto done;
2939 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0) {
2940 err = got_error_from_errno2("unlink", ondisk_path);
2941 goto done;
2944 got_fileindex_entry_mark_deleted_from_disk(ie);
2945 done:
2946 free(ondisk_path);
2947 if (err)
2948 return err;
2949 if (status == GOT_STATUS_DELETE)
2950 return NULL;
2951 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
2952 staged_status, relpath);
2955 const struct got_error *
2956 got_worktree_schedule_delete(struct got_worktree *worktree,
2957 struct got_pathlist_head *paths, int delete_local_mods,
2958 got_worktree_delete_cb progress_cb, void *progress_arg,
2959 struct got_repository *repo)
2961 struct got_fileindex *fileindex = NULL;
2962 char *fileindex_path = NULL;
2963 const struct got_error *err = NULL, *sync_err, *unlockerr;
2964 struct got_pathlist_entry *pe;
2965 struct schedule_deletion_args sda;
2967 err = lock_worktree(worktree, LOCK_EX);
2968 if (err)
2969 return err;
2971 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2972 if (err)
2973 goto done;
2975 sda.worktree = worktree;
2976 sda.fileindex = fileindex;
2977 sda.progress_cb = progress_cb;
2978 sda.progress_arg = progress_arg;
2979 sda.repo = repo;
2980 sda.delete_local_mods = delete_local_mods;
2982 TAILQ_FOREACH(pe, paths, entry) {
2983 err = worktree_status(worktree, pe->path, fileindex, repo,
2984 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
2985 if (err)
2986 break;
2988 sync_err = sync_fileindex(fileindex, fileindex_path);
2989 if (sync_err && err == NULL)
2990 err = sync_err;
2991 done:
2992 free(fileindex_path);
2993 if (fileindex)
2994 got_fileindex_free(fileindex);
2995 unlockerr = lock_worktree(worktree, LOCK_SH);
2996 if (unlockerr && err == NULL)
2997 err = unlockerr;
2998 return err;
3001 static const struct got_error *
3002 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3004 const struct got_error *err = NULL;
3005 char *line = NULL;
3006 size_t linesize = 0, n;
3007 ssize_t linelen;
3009 linelen = getline(&line, &linesize, infile);
3010 if (linelen == -1) {
3011 if (ferror(infile)) {
3012 err = got_error_from_errno("getline");
3013 goto done;
3015 return NULL;
3017 if (outfile) {
3018 n = fwrite(line, 1, linelen, outfile);
3019 if (n != linelen) {
3020 err = got_ferror(outfile, GOT_ERR_IO);
3021 goto done;
3024 if (rejectfile) {
3025 n = fwrite(line, 1, linelen, rejectfile);
3026 if (n != linelen)
3027 err = got_ferror(outfile, GOT_ERR_IO);
3029 done:
3030 free(line);
3031 return err;
3034 static const struct got_error *
3035 skip_one_line(FILE *f)
3037 char *line = NULL;
3038 size_t linesize = 0;
3039 ssize_t linelen;
3041 linelen = getline(&line, &linesize, f);
3042 if (linelen == -1) {
3043 if (ferror(f))
3044 return got_error_from_errno("getline");
3045 return NULL;
3047 free(line);
3048 return NULL;
3051 static const struct got_error *
3052 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3053 int start_old, int end_old, int start_new, int end_new,
3054 FILE *outfile, FILE *rejectfile)
3056 const struct got_error *err;
3058 /* Copy old file's lines leading up to patch. */
3059 while (!feof(f1) && *line_cur1 < start_old) {
3060 err = copy_one_line(f1, outfile, NULL);
3061 if (err)
3062 return err;
3063 (*line_cur1)++;
3065 /* Skip new file's lines leading up to patch. */
3066 while (!feof(f2) && *line_cur2 < start_new) {
3067 if (rejectfile)
3068 err = copy_one_line(f2, NULL, rejectfile);
3069 else
3070 err = skip_one_line(f2);
3071 if (err)
3072 return err;
3073 (*line_cur2)++;
3075 /* Copy patched lines. */
3076 while (!feof(f2) && *line_cur2 <= end_new) {
3077 err = copy_one_line(f2, outfile, NULL);
3078 if (err)
3079 return err;
3080 (*line_cur2)++;
3082 /* Skip over old file's replaced lines. */
3083 while (!feof(f1) && *line_cur1 <= end_old) {
3084 if (rejectfile)
3085 err = copy_one_line(f1, NULL, rejectfile);
3086 else
3087 err = skip_one_line(f1);
3088 if (err)
3089 return err;
3090 (*line_cur1)++;
3093 return NULL;
3096 static const struct got_error *
3097 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3098 FILE *outfile, FILE *rejectfile)
3100 const struct got_error *err;
3102 if (outfile) {
3103 /* Copy old file's lines until EOF. */
3104 while (!feof(f1)) {
3105 err = copy_one_line(f1, outfile, NULL);
3106 if (err)
3107 return err;
3108 (*line_cur1)++;
3111 if (rejectfile) {
3112 /* Copy new file's lines until EOF. */
3113 while (!feof(f2)) {
3114 err = copy_one_line(f2, NULL, rejectfile);
3115 if (err)
3116 return err;
3117 (*line_cur2)++;
3121 return NULL;
3124 static const struct got_error *
3125 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3126 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3127 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3128 int *line_cur2, FILE *outfile, FILE *rejectfile,
3129 got_worktree_patch_cb patch_cb, void *patch_arg)
3131 const struct got_error *err = NULL;
3132 int start_old = change->cv.a;
3133 int end_old = change->cv.b;
3134 int start_new = change->cv.c;
3135 int end_new = change->cv.d;
3136 long pos1, pos2;
3137 FILE *hunkfile;
3139 *choice = GOT_PATCH_CHOICE_NONE;
3141 hunkfile = got_opentemp();
3142 if (hunkfile == NULL)
3143 return got_error_from_errno("got_opentemp");
3145 pos1 = ftell(f1);
3146 pos2 = ftell(f2);
3148 /* XXX TODO needs error checking */
3149 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3151 if (fseek(f1, pos1, SEEK_SET) == -1) {
3152 err = got_ferror(f1, GOT_ERR_IO);
3153 goto done;
3155 if (fseek(f2, pos2, SEEK_SET) == -1) {
3156 err = got_ferror(f1, GOT_ERR_IO);
3157 goto done;
3159 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3160 err = got_ferror(hunkfile, GOT_ERR_IO);
3161 goto done;
3164 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3165 hunkfile, n, nchanges);
3166 if (err)
3167 goto done;
3169 switch (*choice) {
3170 case GOT_PATCH_CHOICE_YES:
3171 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3172 end_old, start_new, end_new, outfile, rejectfile);
3173 break;
3174 case GOT_PATCH_CHOICE_NO:
3175 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3176 end_old, start_new, end_new, rejectfile, outfile);
3177 break;
3178 case GOT_PATCH_CHOICE_QUIT:
3179 break;
3180 default:
3181 err = got_error(GOT_ERR_PATCH_CHOICE);
3182 break;
3184 done:
3185 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3186 err = got_error_from_errno("fclose");
3187 return err;
3190 struct revert_file_args {
3191 struct got_worktree *worktree;
3192 struct got_fileindex *fileindex;
3193 got_worktree_checkout_cb progress_cb;
3194 void *progress_arg;
3195 got_worktree_patch_cb patch_cb;
3196 void *patch_arg;
3197 struct got_repository *repo;
3200 static const struct got_error *
3201 create_patched_content(char **path_outfile, int reverse_patch,
3202 struct got_object_id *blob_id, const char *path2,
3203 const char *relpath, struct got_repository *repo,
3204 got_worktree_patch_cb patch_cb, void *patch_arg)
3206 const struct got_error *err;
3207 struct got_blob_object *blob = NULL;
3208 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3209 int fd2 = -1;
3210 char *path1 = NULL, *id_str = NULL;
3211 struct stat sb1, sb2;
3212 struct got_diff_changes *changes = NULL;
3213 struct got_diff_state *ds = NULL;
3214 struct got_diff_args *args = NULL;
3215 struct got_diff_change *change;
3216 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3217 int n = 0;
3219 *path_outfile = NULL;
3221 err = got_object_id_str(&id_str, blob_id);
3222 if (err)
3223 return err;
3225 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3226 if (fd2 == -1) {
3227 err = got_error_from_errno2("open", path2);
3228 goto done;
3230 if (fstat(fd2, &sb2) == -1) {
3231 err = got_error_from_errno2("fstat", path2);
3232 goto done;
3235 f2 = fdopen(fd2, "r");
3236 if (f2 == NULL) {
3237 err = got_error_from_errno2("fopen", path2);
3238 goto done;
3240 fd2 = -1;
3242 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3243 if (err)
3244 goto done;
3246 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3247 if (err)
3248 goto done;
3250 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3251 if (err)
3252 goto done;
3254 if (stat(path1, &sb1) == -1) {
3255 err = got_error_from_errno2("stat", path1);
3256 goto done;
3259 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3260 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3261 if (err)
3262 goto done;
3264 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3265 if (err)
3266 goto done;
3268 if (fseek(f1, 0L, SEEK_SET) == -1)
3269 return got_ferror(f1, GOT_ERR_IO);
3270 if (fseek(f2, 0L, SEEK_SET) == -1)
3271 return got_ferror(f2, GOT_ERR_IO);
3272 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3273 int choice;
3274 err = apply_or_reject_change(&choice, change, ++n,
3275 changes->nchanges, ds, args, diff_flags, relpath,
3276 f1, f2, &line_cur1, &line_cur2,
3277 reverse_patch ? NULL : outfile,
3278 reverse_patch ? outfile : NULL,
3279 patch_cb, patch_arg);
3280 if (err)
3281 goto done;
3282 if (choice == GOT_PATCH_CHOICE_YES)
3283 have_content = 1;
3284 else if (choice == GOT_PATCH_CHOICE_QUIT)
3285 break;
3287 if (have_content) {
3288 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3289 reverse_patch ? NULL : outfile,
3290 reverse_patch ? outfile : NULL);
3291 if (err)
3292 goto done;
3294 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3295 err = got_error_from_errno2("chmod", path2);
3296 goto done;
3299 done:
3300 free(id_str);
3301 if (blob)
3302 got_object_blob_close(blob);
3303 if (f1 && fclose(f1) == EOF && err == NULL)
3304 err = got_error_from_errno2("fclose", path1);
3305 if (f2 && fclose(f2) == EOF && err == NULL)
3306 err = got_error_from_errno2("fclose", path2);
3307 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3308 err = got_error_from_errno2("close", path2);
3309 if (outfile && fclose(outfile) == EOF && err == NULL)
3310 err = got_error_from_errno2("fclose", *path_outfile);
3311 if (path1 && unlink(path1) == -1 && err == NULL)
3312 err = got_error_from_errno2("unlink", path1);
3313 if (err || !have_content) {
3314 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3315 err = got_error_from_errno2("unlink", *path_outfile);
3316 free(*path_outfile);
3317 *path_outfile = NULL;
3319 free(args);
3320 if (ds) {
3321 got_diff_state_free(ds);
3322 free(ds);
3324 if (changes)
3325 got_diff_free_changes(changes);
3326 free(path1);
3327 return err;
3330 static const struct got_error *
3331 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3332 const char *relpath, struct got_object_id *blob_id,
3333 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3335 struct revert_file_args *a = arg;
3336 const struct got_error *err = NULL;
3337 char *parent_path = NULL;
3338 struct got_fileindex_entry *ie;
3339 struct got_tree_object *tree = NULL;
3340 struct got_object_id *tree_id = NULL;
3341 const struct got_tree_entry *te = NULL;
3342 char *tree_path = NULL, *te_name;
3343 char *ondisk_path = NULL, *path_content = NULL;
3344 struct got_blob_object *blob = NULL;
3346 /* Reverting a staged deletion is a no-op. */
3347 if (status == GOT_STATUS_DELETE &&
3348 staged_status != GOT_STATUS_NO_CHANGE)
3349 return NULL;
3351 if (status == GOT_STATUS_UNVERSIONED)
3352 return (*a->progress_cb)(a->progress_arg,
3353 GOT_STATUS_UNVERSIONED, relpath);
3355 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3356 if (ie == NULL)
3357 return got_error(GOT_ERR_BAD_PATH);
3359 /* Construct in-repository path of tree which contains this blob. */
3360 err = got_path_dirname(&parent_path, ie->path);
3361 if (err) {
3362 if (err->code != GOT_ERR_BAD_PATH)
3363 goto done;
3364 parent_path = strdup("/");
3365 if (parent_path == NULL) {
3366 err = got_error_from_errno("strdup");
3367 goto done;
3370 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3371 tree_path = strdup(parent_path);
3372 if (tree_path == NULL) {
3373 err = got_error_from_errno("strdup");
3374 goto done;
3376 } else {
3377 if (got_path_is_root_dir(parent_path)) {
3378 tree_path = strdup(a->worktree->path_prefix);
3379 if (tree_path == NULL) {
3380 err = got_error_from_errno("strdup");
3381 goto done;
3383 } else {
3384 if (asprintf(&tree_path, "%s/%s",
3385 a->worktree->path_prefix, parent_path) == -1) {
3386 err = got_error_from_errno("asprintf");
3387 goto done;
3392 err = got_object_id_by_path(&tree_id, a->repo,
3393 a->worktree->base_commit_id, tree_path);
3394 if (err) {
3395 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3396 (status == GOT_STATUS_ADD ||
3397 staged_status == GOT_STATUS_ADD)))
3398 goto done;
3399 } else {
3400 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3401 if (err)
3402 goto done;
3404 te_name = basename(ie->path);
3405 if (te_name == NULL) {
3406 err = got_error_from_errno2("basename", ie->path);
3407 goto done;
3410 te = got_object_tree_find_entry(tree, te_name);
3411 if (te == NULL && status != GOT_STATUS_ADD &&
3412 staged_status != GOT_STATUS_ADD) {
3413 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3414 goto done;
3418 switch (status) {
3419 case GOT_STATUS_ADD:
3420 if (a->patch_cb) {
3421 int choice = GOT_PATCH_CHOICE_NONE;
3422 err = (*a->patch_cb)(&choice, a->patch_arg,
3423 status, ie->path, NULL, 1, 1);
3424 if (err)
3425 goto done;
3426 if (choice != GOT_PATCH_CHOICE_YES)
3427 break;
3429 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3430 ie->path);
3431 if (err)
3432 goto done;
3433 got_fileindex_entry_remove(a->fileindex, ie);
3434 break;
3435 case GOT_STATUS_DELETE:
3436 if (a->patch_cb) {
3437 int choice = GOT_PATCH_CHOICE_NONE;
3438 err = (*a->patch_cb)(&choice, a->patch_arg,
3439 status, ie->path, NULL, 1, 1);
3440 if (err)
3441 goto done;
3442 if (choice != GOT_PATCH_CHOICE_YES)
3443 break;
3445 /* fall through */
3446 case GOT_STATUS_MODIFY:
3447 case GOT_STATUS_MODE_CHANGE:
3448 case GOT_STATUS_CONFLICT:
3449 case GOT_STATUS_MISSING: {
3450 struct got_object_id id;
3451 if (staged_status == GOT_STATUS_ADD ||
3452 staged_status == GOT_STATUS_MODIFY) {
3453 memcpy(id.sha1, ie->staged_blob_sha1,
3454 SHA1_DIGEST_LENGTH);
3455 } else
3456 memcpy(id.sha1, ie->blob_sha1,
3457 SHA1_DIGEST_LENGTH);
3458 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3459 if (err)
3460 goto done;
3462 if (asprintf(&ondisk_path, "%s/%s",
3463 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3464 err = got_error_from_errno("asprintf");
3465 goto done;
3468 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3469 status == GOT_STATUS_CONFLICT)) {
3470 err = create_patched_content(&path_content, 1, &id,
3471 ondisk_path, ie->path, a->repo,
3472 a->patch_cb, a->patch_arg);
3473 if (err || path_content == NULL)
3474 break;
3475 if (rename(path_content, ondisk_path) == -1) {
3476 err = got_error_from_errno3("rename",
3477 path_content, ondisk_path);
3478 goto done;
3480 } else {
3481 err = install_blob(a->worktree, ondisk_path, ie->path,
3482 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3483 got_fileindex_perms_to_st(ie), blob, 0, 1,
3484 a->repo, a->progress_cb, a->progress_arg);
3485 if (err)
3486 goto done;
3487 if (status == GOT_STATUS_DELETE ||
3488 status == GOT_STATUS_MODE_CHANGE) {
3489 err = update_blob_fileindex_entry(a->worktree,
3490 a->fileindex, ie, ondisk_path, ie->path,
3491 blob, 1);
3492 if (err)
3493 goto done;
3496 break;
3498 default:
3499 break;
3501 done:
3502 free(ondisk_path);
3503 free(path_content);
3504 free(parent_path);
3505 free(tree_path);
3506 if (blob)
3507 got_object_blob_close(blob);
3508 if (tree)
3509 got_object_tree_close(tree);
3510 free(tree_id);
3511 return err;
3514 const struct got_error *
3515 got_worktree_revert(struct got_worktree *worktree,
3516 struct got_pathlist_head *paths,
3517 got_worktree_checkout_cb progress_cb, void *progress_arg,
3518 got_worktree_patch_cb patch_cb, void *patch_arg,
3519 struct got_repository *repo)
3521 struct got_fileindex *fileindex = NULL;
3522 char *fileindex_path = NULL;
3523 const struct got_error *err = NULL, *unlockerr = NULL;
3524 const struct got_error *sync_err = NULL;
3525 struct got_pathlist_entry *pe;
3526 struct revert_file_args rfa;
3528 err = lock_worktree(worktree, LOCK_EX);
3529 if (err)
3530 return err;
3532 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3533 if (err)
3534 goto done;
3536 rfa.worktree = worktree;
3537 rfa.fileindex = fileindex;
3538 rfa.progress_cb = progress_cb;
3539 rfa.progress_arg = progress_arg;
3540 rfa.patch_cb = patch_cb;
3541 rfa.patch_arg = patch_arg;
3542 rfa.repo = repo;
3543 TAILQ_FOREACH(pe, paths, entry) {
3544 err = worktree_status(worktree, pe->path, fileindex, repo,
3545 revert_file, &rfa, NULL, NULL, 0, 0);
3546 if (err)
3547 break;
3549 sync_err = sync_fileindex(fileindex, fileindex_path);
3550 if (sync_err && err == NULL)
3551 err = sync_err;
3552 done:
3553 free(fileindex_path);
3554 if (fileindex)
3555 got_fileindex_free(fileindex);
3556 unlockerr = lock_worktree(worktree, LOCK_SH);
3557 if (unlockerr && err == NULL)
3558 err = unlockerr;
3559 return err;
3562 static void
3563 free_commitable(struct got_commitable *ct)
3565 free(ct->path);
3566 free(ct->in_repo_path);
3567 free(ct->ondisk_path);
3568 free(ct->blob_id);
3569 free(ct->base_blob_id);
3570 free(ct->staged_blob_id);
3571 free(ct->base_commit_id);
3572 free(ct);
3575 struct collect_commitables_arg {
3576 struct got_pathlist_head *commitable_paths;
3577 struct got_repository *repo;
3578 struct got_worktree *worktree;
3579 int have_staged_files;
3582 static const struct got_error *
3583 collect_commitables(void *arg, unsigned char status,
3584 unsigned char staged_status, const char *relpath,
3585 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3586 struct got_object_id *commit_id)
3588 struct collect_commitables_arg *a = arg;
3589 const struct got_error *err = NULL;
3590 struct got_commitable *ct = NULL;
3591 struct got_pathlist_entry *new = NULL;
3592 char *parent_path = NULL, *path = NULL;
3593 struct stat sb;
3595 if (a->have_staged_files) {
3596 if (staged_status != GOT_STATUS_MODIFY &&
3597 staged_status != GOT_STATUS_ADD &&
3598 staged_status != GOT_STATUS_DELETE)
3599 return NULL;
3600 } else {
3601 if (status == GOT_STATUS_CONFLICT)
3602 return got_error(GOT_ERR_COMMIT_CONFLICT);
3604 if (status != GOT_STATUS_MODIFY &&
3605 status != GOT_STATUS_MODE_CHANGE &&
3606 status != GOT_STATUS_ADD &&
3607 status != GOT_STATUS_DELETE)
3608 return NULL;
3611 if (asprintf(&path, "/%s", relpath) == -1) {
3612 err = got_error_from_errno("asprintf");
3613 goto done;
3615 if (strcmp(path, "/") == 0) {
3616 parent_path = strdup("");
3617 if (parent_path == NULL)
3618 return got_error_from_errno("strdup");
3619 } else {
3620 err = got_path_dirname(&parent_path, path);
3621 if (err)
3622 return err;
3625 ct = calloc(1, sizeof(*ct));
3626 if (ct == NULL) {
3627 err = got_error_from_errno("calloc");
3628 goto done;
3631 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3632 relpath) == -1) {
3633 err = got_error_from_errno("asprintf");
3634 goto done;
3636 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3637 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3638 } else {
3639 if (lstat(ct->ondisk_path, &sb) != 0) {
3640 err = got_error_from_errno2("lstat", ct->ondisk_path);
3641 goto done;
3643 ct->mode = sb.st_mode;
3646 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3647 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3648 relpath) == -1) {
3649 err = got_error_from_errno("asprintf");
3650 goto done;
3653 ct->status = status;
3654 ct->staged_status = staged_status;
3655 ct->blob_id = NULL; /* will be filled in when blob gets created */
3656 if (ct->status != GOT_STATUS_ADD &&
3657 ct->staged_status != GOT_STATUS_ADD) {
3658 ct->base_blob_id = got_object_id_dup(blob_id);
3659 if (ct->base_blob_id == NULL) {
3660 err = got_error_from_errno("got_object_id_dup");
3661 goto done;
3663 ct->base_commit_id = got_object_id_dup(commit_id);
3664 if (ct->base_commit_id == NULL) {
3665 err = got_error_from_errno("got_object_id_dup");
3666 goto done;
3669 if (ct->staged_status == GOT_STATUS_ADD ||
3670 ct->staged_status == GOT_STATUS_MODIFY) {
3671 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3672 if (ct->staged_blob_id == NULL) {
3673 err = got_error_from_errno("got_object_id_dup");
3674 goto done;
3677 ct->path = strdup(path);
3678 if (ct->path == NULL) {
3679 err = got_error_from_errno("strdup");
3680 goto done;
3682 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3683 done:
3684 if (ct && (err || new == NULL))
3685 free_commitable(ct);
3686 free(parent_path);
3687 free(path);
3688 return err;
3691 static const struct got_error *write_tree(struct got_object_id **,
3692 struct got_tree_object *, const char *, struct got_pathlist_head *,
3693 got_worktree_status_cb status_cb, void *status_arg,
3694 struct got_repository *);
3696 static const struct got_error *
3697 write_subtree(struct got_object_id **new_subtree_id,
3698 struct got_tree_entry *te, const char *parent_path,
3699 struct got_pathlist_head *commitable_paths,
3700 got_worktree_status_cb status_cb, void *status_arg,
3701 struct got_repository *repo)
3703 const struct got_error *err = NULL;
3704 struct got_tree_object *subtree;
3705 char *subpath;
3707 if (asprintf(&subpath, "%s%s%s", parent_path,
3708 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3709 return got_error_from_errno("asprintf");
3711 err = got_object_open_as_tree(&subtree, repo, &te->id);
3712 if (err)
3713 return err;
3715 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3716 status_cb, status_arg, repo);
3717 got_object_tree_close(subtree);
3718 free(subpath);
3719 return err;
3722 static const struct got_error *
3723 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3725 const struct got_error *err = NULL;
3726 char *ct_parent_path = NULL;
3728 *match = 0;
3730 if (strchr(ct->in_repo_path, '/') == NULL) {
3731 *match = got_path_is_root_dir(path);
3732 return NULL;
3735 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3736 if (err)
3737 return err;
3738 *match = (strcmp(path, ct_parent_path) == 0);
3739 free(ct_parent_path);
3740 return err;
3743 static mode_t
3744 get_ct_file_mode(struct got_commitable *ct)
3746 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3749 static const struct got_error *
3750 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3751 struct got_tree_entry *te, struct got_commitable *ct)
3753 const struct got_error *err = NULL;
3755 *new_te = NULL;
3757 err = got_object_tree_entry_dup(new_te, te);
3758 if (err)
3759 goto done;
3761 (*new_te)->mode = get_ct_file_mode(ct);
3763 if (ct->staged_status == GOT_STATUS_MODIFY)
3764 memcpy(&(*new_te)->id, ct->staged_blob_id,
3765 sizeof((*new_te)->id));
3766 else
3767 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3768 done:
3769 if (err && *new_te) {
3770 free(*new_te);
3771 *new_te = NULL;
3773 return err;
3776 static const struct got_error *
3777 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3778 struct got_commitable *ct)
3780 const struct got_error *err = NULL;
3781 char *ct_name;
3783 *new_te = NULL;
3785 *new_te = calloc(1, sizeof(**new_te));
3786 if (*new_te == NULL)
3787 return got_error_from_errno("calloc");
3789 ct_name = basename(ct->path);
3790 if (ct_name == NULL) {
3791 err = got_error_from_errno2("basename", ct->path);
3792 goto done;
3794 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3795 sizeof((*new_te)->name)) {
3796 err = got_error(GOT_ERR_NO_SPACE);
3797 goto done;
3800 (*new_te)->mode = get_ct_file_mode(ct);
3802 if (ct->staged_status == GOT_STATUS_ADD)
3803 memcpy(&(*new_te)->id, ct->staged_blob_id,
3804 sizeof((*new_te)->id));
3805 else
3806 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3807 done:
3808 if (err && *new_te) {
3809 free(*new_te);
3810 *new_te = NULL;
3812 return err;
3815 static const struct got_error *
3816 insert_tree_entry(struct got_tree_entry *new_te,
3817 struct got_pathlist_head *paths)
3819 const struct got_error *err = NULL;
3820 struct got_pathlist_entry *new_pe;
3822 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3823 if (err)
3824 return err;
3825 if (new_pe == NULL)
3826 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3827 return NULL;
3830 static const struct got_error *
3831 report_ct_status(struct got_commitable *ct,
3832 got_worktree_status_cb status_cb, void *status_arg)
3834 const char *ct_path = ct->path;
3835 unsigned char status;
3837 while (ct_path[0] == '/')
3838 ct_path++;
3840 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3841 status = ct->staged_status;
3842 else
3843 status = ct->status;
3845 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3846 ct_path, ct->blob_id, NULL, NULL);
3849 static const struct got_error *
3850 match_modified_subtree(int *modified, struct got_tree_entry *te,
3851 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3853 const struct got_error *err = NULL;
3854 struct got_pathlist_entry *pe;
3855 char *te_path;
3857 *modified = 0;
3859 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3860 got_path_is_root_dir(base_tree_path) ? "" : "/",
3861 te->name) == -1)
3862 return got_error_from_errno("asprintf");
3864 TAILQ_FOREACH(pe, commitable_paths, entry) {
3865 struct got_commitable *ct = pe->data;
3866 *modified = got_path_is_child(ct->in_repo_path, te_path,
3867 strlen(te_path));
3868 if (*modified)
3869 break;
3872 free(te_path);
3873 return err;
3876 static const struct got_error *
3877 match_deleted_or_modified_ct(struct got_commitable **ctp,
3878 struct got_tree_entry *te, const char *base_tree_path,
3879 struct got_pathlist_head *commitable_paths)
3881 const struct got_error *err = NULL;
3882 struct got_pathlist_entry *pe;
3884 *ctp = NULL;
3886 TAILQ_FOREACH(pe, commitable_paths, entry) {
3887 struct got_commitable *ct = pe->data;
3888 char *ct_name = NULL;
3889 int path_matches;
3891 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3892 if (ct->status != GOT_STATUS_MODIFY &&
3893 ct->status != GOT_STATUS_MODE_CHANGE &&
3894 ct->status != GOT_STATUS_DELETE)
3895 continue;
3896 } else {
3897 if (ct->staged_status != GOT_STATUS_MODIFY &&
3898 ct->staged_status != GOT_STATUS_DELETE)
3899 continue;
3902 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3903 continue;
3905 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3906 if (err)
3907 return err;
3908 if (!path_matches)
3909 continue;
3911 ct_name = basename(pe->path);
3912 if (ct_name == NULL)
3913 return got_error_from_errno2("basename", pe->path);
3915 if (strcmp(te->name, ct_name) != 0)
3916 continue;
3918 *ctp = ct;
3919 break;
3922 return err;
3925 static const struct got_error *
3926 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3927 const char *child_path, const char *path_base_tree,
3928 struct got_pathlist_head *commitable_paths,
3929 got_worktree_status_cb status_cb, void *status_arg,
3930 struct got_repository *repo)
3932 const struct got_error *err = NULL;
3933 struct got_tree_entry *new_te;
3934 char *subtree_path;
3935 struct got_object_id *id = NULL;
3937 *new_tep = NULL;
3939 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3940 got_path_is_root_dir(path_base_tree) ? "" : "/",
3941 child_path) == -1)
3942 return got_error_from_errno("asprintf");
3944 new_te = calloc(1, sizeof(*new_te));
3945 if (new_te == NULL)
3946 return got_error_from_errno("calloc");
3947 new_te->mode = S_IFDIR;
3949 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
3950 sizeof(new_te->name)) {
3951 err = got_error(GOT_ERR_NO_SPACE);
3952 goto done;
3954 err = write_tree(&id, NULL, subtree_path,
3955 commitable_paths, status_cb, status_arg, repo);
3956 if (err) {
3957 free(new_te);
3958 goto done;
3960 memcpy(&new_te->id, id, sizeof(new_te->id));
3961 done:
3962 free(id);
3963 free(subtree_path);
3964 if (err == NULL)
3965 *new_tep = new_te;
3966 return err;
3969 static const struct got_error *
3970 write_tree(struct got_object_id **new_tree_id,
3971 struct got_tree_object *base_tree, const char *path_base_tree,
3972 struct got_pathlist_head *commitable_paths,
3973 got_worktree_status_cb status_cb, void *status_arg,
3974 struct got_repository *repo)
3976 const struct got_error *err = NULL;
3977 struct got_pathlist_head paths;
3978 struct got_tree_entry *te, *new_te = NULL;
3979 struct got_pathlist_entry *pe;
3980 int nentries = 0;
3982 TAILQ_INIT(&paths);
3984 /* Insert, and recurse into, newly added entries first. */
3985 TAILQ_FOREACH(pe, commitable_paths, entry) {
3986 struct got_commitable *ct = pe->data;
3987 char *child_path = NULL, *slash;
3989 if ((ct->status != GOT_STATUS_ADD &&
3990 ct->staged_status != GOT_STATUS_ADD) ||
3991 (ct->flags & GOT_COMMITABLE_ADDED))
3992 continue;
3994 if (!got_path_is_child(pe->path, path_base_tree,
3995 strlen(path_base_tree)))
3996 continue;
3998 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3999 pe->path);
4000 if (err)
4001 goto done;
4003 slash = strchr(child_path, '/');
4004 if (slash == NULL) {
4005 err = alloc_added_blob_tree_entry(&new_te, ct);
4006 if (err)
4007 goto done;
4008 err = report_ct_status(ct, status_cb, status_arg);
4009 if (err)
4010 goto done;
4011 ct->flags |= GOT_COMMITABLE_ADDED;
4012 err = insert_tree_entry(new_te, &paths);
4013 if (err)
4014 goto done;
4015 nentries++;
4016 } else {
4017 *slash = '\0'; /* trim trailing path components */
4018 if (base_tree == NULL ||
4019 got_object_tree_find_entry(base_tree, child_path)
4020 == NULL) {
4021 err = make_subtree_for_added_blob(&new_te,
4022 child_path, path_base_tree,
4023 commitable_paths, status_cb, status_arg,
4024 repo);
4025 if (err)
4026 goto done;
4027 err = insert_tree_entry(new_te, &paths);
4028 if (err)
4029 goto done;
4030 nentries++;
4035 if (base_tree) {
4036 int i, nbase_entries;
4037 /* Handle modified and deleted entries. */
4038 nbase_entries = got_object_tree_get_nentries(base_tree);
4039 for (i = 0; i < nbase_entries; i++) {
4040 struct got_commitable *ct = NULL;
4042 te = got_object_tree_get_entry(base_tree, i);
4043 if (got_object_tree_entry_is_submodule(te)) {
4044 /* Entry is a submodule; just copy it. */
4045 err = got_object_tree_entry_dup(&new_te, te);
4046 if (err)
4047 goto done;
4048 err = insert_tree_entry(new_te, &paths);
4049 if (err)
4050 goto done;
4051 nentries++;
4052 continue;
4055 if (S_ISDIR(te->mode)) {
4056 int modified;
4057 err = got_object_tree_entry_dup(&new_te, te);
4058 if (err)
4059 goto done;
4060 err = match_modified_subtree(&modified, te,
4061 path_base_tree, commitable_paths);
4062 if (err)
4063 goto done;
4064 /* Avoid recursion into unmodified subtrees. */
4065 if (modified) {
4066 struct got_object_id *new_id;
4067 err = write_subtree(&new_id, te,
4068 path_base_tree, commitable_paths,
4069 status_cb, status_arg, repo);
4070 if (err)
4071 goto done;
4072 memcpy(&new_te->id, new_id,
4073 sizeof(new_te->id));
4074 free(new_id);
4076 err = insert_tree_entry(new_te, &paths);
4077 if (err)
4078 goto done;
4079 nentries++;
4080 continue;
4083 err = match_deleted_or_modified_ct(&ct, te,
4084 path_base_tree, commitable_paths);
4085 if (err)
4086 goto done;
4087 if (ct) {
4088 /* NB: Deleted entries get dropped here. */
4089 if (ct->status == GOT_STATUS_MODIFY ||
4090 ct->status == GOT_STATUS_MODE_CHANGE ||
4091 ct->staged_status == GOT_STATUS_MODIFY) {
4092 err = alloc_modified_blob_tree_entry(
4093 &new_te, te, ct);
4094 if (err)
4095 goto done;
4096 err = insert_tree_entry(new_te, &paths);
4097 if (err)
4098 goto done;
4099 nentries++;
4101 err = report_ct_status(ct, status_cb,
4102 status_arg);
4103 if (err)
4104 goto done;
4105 } else {
4106 /* Entry is unchanged; just copy it. */
4107 err = got_object_tree_entry_dup(&new_te, te);
4108 if (err)
4109 goto done;
4110 err = insert_tree_entry(new_te, &paths);
4111 if (err)
4112 goto done;
4113 nentries++;
4118 /* Write new list of entries; deleted entries have been dropped. */
4119 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4120 done:
4121 got_pathlist_free(&paths);
4122 return err;
4125 static const struct got_error *
4126 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4127 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4128 int have_staged_files)
4130 const struct got_error *err = NULL;
4131 struct got_pathlist_entry *pe;
4133 TAILQ_FOREACH(pe, commitable_paths, entry) {
4134 struct got_fileindex_entry *ie;
4135 struct got_commitable *ct = pe->data;
4137 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4138 if (ie) {
4139 if (ct->status == GOT_STATUS_DELETE ||
4140 ct->staged_status == GOT_STATUS_DELETE) {
4141 got_fileindex_entry_remove(fileindex, ie);
4142 got_fileindex_entry_free(ie);
4143 } else if (ct->staged_status == GOT_STATUS_ADD ||
4144 ct->staged_status == GOT_STATUS_MODIFY) {
4145 got_fileindex_entry_stage_set(ie,
4146 GOT_FILEIDX_STAGE_NONE);
4147 err = got_fileindex_entry_update(ie,
4148 ct->ondisk_path, ct->staged_blob_id->sha1,
4149 new_base_commit_id->sha1,
4150 !have_staged_files);
4151 } else
4152 err = got_fileindex_entry_update(ie,
4153 ct->ondisk_path, ct->blob_id->sha1,
4154 new_base_commit_id->sha1,
4155 !have_staged_files);
4156 } else {
4157 err = got_fileindex_entry_alloc(&ie,
4158 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4159 new_base_commit_id->sha1);
4160 if (err)
4161 break;
4162 err = got_fileindex_entry_add(fileindex, ie);
4163 if (err)
4164 break;
4167 return err;
4171 static const struct got_error *
4172 check_out_of_date(const char *in_repo_path, unsigned char status,
4173 unsigned char staged_status, struct got_object_id *base_blob_id,
4174 struct got_object_id *base_commit_id,
4175 struct got_object_id *head_commit_id, struct got_repository *repo,
4176 int ood_errcode)
4178 const struct got_error *err = NULL;
4179 struct got_object_id *id = NULL;
4181 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4182 /* Trivial case: base commit == head commit */
4183 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4184 return NULL;
4186 * Ensure file content which local changes were based
4187 * on matches file content in the branch head.
4189 err = got_object_id_by_path(&id, repo, head_commit_id,
4190 in_repo_path);
4191 if (err) {
4192 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4193 err = got_error(ood_errcode);
4194 goto done;
4195 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4196 err = got_error(ood_errcode);
4197 } else {
4198 /* Require that added files don't exist in the branch head. */
4199 err = got_object_id_by_path(&id, repo, head_commit_id,
4200 in_repo_path);
4201 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4202 goto done;
4203 err = id ? got_error(ood_errcode) : NULL;
4205 done:
4206 free(id);
4207 return err;
4210 const struct got_error *
4211 commit_worktree(struct got_object_id **new_commit_id,
4212 struct got_pathlist_head *commitable_paths,
4213 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4214 const char *author, const char *committer,
4215 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4216 got_worktree_status_cb status_cb, void *status_arg,
4217 struct got_repository *repo)
4219 const struct got_error *err = NULL, *unlockerr = NULL;
4220 struct got_pathlist_entry *pe;
4221 const char *head_ref_name = NULL;
4222 struct got_commit_object *head_commit = NULL;
4223 struct got_reference *head_ref2 = NULL;
4224 struct got_object_id *head_commit_id2 = NULL;
4225 struct got_tree_object *head_tree = NULL;
4226 struct got_object_id *new_tree_id = NULL;
4227 struct got_object_id_queue parent_ids;
4228 struct got_object_qid *pid = NULL;
4229 char *logmsg = NULL;
4231 *new_commit_id = NULL;
4233 SIMPLEQ_INIT(&parent_ids);
4235 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4236 if (err)
4237 goto done;
4239 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4240 if (err)
4241 goto done;
4243 if (commit_msg_cb != NULL) {
4244 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4245 if (err)
4246 goto done;
4249 if (logmsg == NULL || strlen(logmsg) == 0) {
4250 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4251 goto done;
4254 /* Create blobs from added and modified files and record their IDs. */
4255 TAILQ_FOREACH(pe, commitable_paths, entry) {
4256 struct got_commitable *ct = pe->data;
4257 char *ondisk_path;
4259 /* Blobs for staged files already exist. */
4260 if (ct->staged_status == GOT_STATUS_ADD ||
4261 ct->staged_status == GOT_STATUS_MODIFY)
4262 continue;
4264 if (ct->status != GOT_STATUS_ADD &&
4265 ct->status != GOT_STATUS_MODIFY &&
4266 ct->status != GOT_STATUS_MODE_CHANGE)
4267 continue;
4269 if (asprintf(&ondisk_path, "%s/%s",
4270 worktree->root_path, pe->path) == -1) {
4271 err = got_error_from_errno("asprintf");
4272 goto done;
4274 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4275 free(ondisk_path);
4276 if (err)
4277 goto done;
4280 /* Recursively write new tree objects. */
4281 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4282 status_cb, status_arg, repo);
4283 if (err)
4284 goto done;
4286 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4287 if (err)
4288 goto done;
4289 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4290 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4291 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4292 got_object_qid_free(pid);
4293 if (logmsg != NULL)
4294 free(logmsg);
4295 if (err)
4296 goto done;
4298 /* Check if a concurrent commit to our branch has occurred. */
4299 head_ref_name = got_worktree_get_head_ref_name(worktree);
4300 if (head_ref_name == NULL) {
4301 err = got_error_from_errno("got_worktree_get_head_ref_name");
4302 goto done;
4304 /* Lock the reference here to prevent concurrent modification. */
4305 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4306 if (err)
4307 goto done;
4308 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4309 if (err)
4310 goto done;
4311 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4312 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4313 goto done;
4315 /* Update branch head in repository. */
4316 err = got_ref_change_ref(head_ref2, *new_commit_id);
4317 if (err)
4318 goto done;
4319 err = got_ref_write(head_ref2, repo);
4320 if (err)
4321 goto done;
4323 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4324 if (err)
4325 goto done;
4327 err = ref_base_commit(worktree, repo);
4328 if (err)
4329 goto done;
4330 done:
4331 if (head_tree)
4332 got_object_tree_close(head_tree);
4333 if (head_commit)
4334 got_object_commit_close(head_commit);
4335 free(head_commit_id2);
4336 if (head_ref2) {
4337 unlockerr = got_ref_unlock(head_ref2);
4338 if (unlockerr && err == NULL)
4339 err = unlockerr;
4340 got_ref_close(head_ref2);
4342 return err;
4345 static const struct got_error *
4346 check_path_is_commitable(const char *path,
4347 struct got_pathlist_head *commitable_paths)
4349 struct got_pathlist_entry *cpe = NULL;
4350 size_t path_len = strlen(path);
4352 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4353 struct got_commitable *ct = cpe->data;
4354 const char *ct_path = ct->path;
4356 while (ct_path[0] == '/')
4357 ct_path++;
4359 if (strcmp(path, ct_path) == 0 ||
4360 got_path_is_child(ct_path, path, path_len))
4361 break;
4364 if (cpe == NULL)
4365 return got_error_path(path, GOT_ERR_BAD_PATH);
4367 return NULL;
4370 static const struct got_error *
4371 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4373 int *have_staged_files = arg;
4375 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4376 *have_staged_files = 1;
4377 return got_error(GOT_ERR_CANCELLED);
4380 return NULL;
4383 static const struct got_error *
4384 check_non_staged_files(struct got_fileindex *fileindex,
4385 struct got_pathlist_head *paths)
4387 struct got_pathlist_entry *pe;
4388 struct got_fileindex_entry *ie;
4390 TAILQ_FOREACH(pe, paths, entry) {
4391 if (pe->path[0] == '\0')
4392 continue;
4393 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4394 if (ie == NULL)
4395 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4396 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4397 return got_error_path(pe->path,
4398 GOT_ERR_FILE_NOT_STAGED);
4401 return NULL;
4404 const struct got_error *
4405 got_worktree_commit(struct got_object_id **new_commit_id,
4406 struct got_worktree *worktree, struct got_pathlist_head *paths,
4407 const char *author, const char *committer,
4408 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4409 got_worktree_status_cb status_cb, void *status_arg,
4410 struct got_repository *repo)
4412 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4413 struct got_fileindex *fileindex = NULL;
4414 char *fileindex_path = NULL;
4415 struct got_pathlist_head commitable_paths;
4416 struct collect_commitables_arg cc_arg;
4417 struct got_pathlist_entry *pe;
4418 struct got_reference *head_ref = NULL;
4419 struct got_object_id *head_commit_id = NULL;
4420 int have_staged_files = 0;
4422 *new_commit_id = NULL;
4424 TAILQ_INIT(&commitable_paths);
4426 err = lock_worktree(worktree, LOCK_EX);
4427 if (err)
4428 goto done;
4430 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4431 if (err)
4432 goto done;
4434 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4435 if (err)
4436 goto done;
4438 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4439 if (err)
4440 goto done;
4442 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4443 &have_staged_files);
4444 if (err && err->code != GOT_ERR_CANCELLED)
4445 goto done;
4446 if (have_staged_files) {
4447 err = check_non_staged_files(fileindex, paths);
4448 if (err)
4449 goto done;
4452 cc_arg.commitable_paths = &commitable_paths;
4453 cc_arg.worktree = worktree;
4454 cc_arg.repo = repo;
4455 cc_arg.have_staged_files = have_staged_files;
4456 TAILQ_FOREACH(pe, paths, entry) {
4457 err = worktree_status(worktree, pe->path, fileindex, repo,
4458 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4459 if (err)
4460 goto done;
4463 if (TAILQ_EMPTY(&commitable_paths)) {
4464 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4465 goto done;
4468 TAILQ_FOREACH(pe, paths, entry) {
4469 err = check_path_is_commitable(pe->path, &commitable_paths);
4470 if (err)
4471 goto done;
4474 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4475 struct got_commitable *ct = pe->data;
4476 const char *ct_path = ct->in_repo_path;
4478 while (ct_path[0] == '/')
4479 ct_path++;
4480 err = check_out_of_date(ct_path, ct->status,
4481 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4482 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4483 if (err)
4484 goto done;
4488 err = commit_worktree(new_commit_id, &commitable_paths,
4489 head_commit_id, worktree, author, committer,
4490 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4491 if (err)
4492 goto done;
4494 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4495 fileindex, have_staged_files);
4496 sync_err = sync_fileindex(fileindex, fileindex_path);
4497 if (sync_err && err == NULL)
4498 err = sync_err;
4499 done:
4500 if (fileindex)
4501 got_fileindex_free(fileindex);
4502 free(fileindex_path);
4503 unlockerr = lock_worktree(worktree, LOCK_SH);
4504 if (unlockerr && err == NULL)
4505 err = unlockerr;
4506 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4507 struct got_commitable *ct = pe->data;
4508 free_commitable(ct);
4510 got_pathlist_free(&commitable_paths);
4511 return err;
4514 const char *
4515 got_commitable_get_path(struct got_commitable *ct)
4517 return ct->path;
4520 unsigned int
4521 got_commitable_get_status(struct got_commitable *ct)
4523 return ct->status;
4526 struct check_rebase_ok_arg {
4527 struct got_worktree *worktree;
4528 struct got_repository *repo;
4531 static const struct got_error *
4532 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4534 const struct got_error *err = NULL;
4535 struct check_rebase_ok_arg *a = arg;
4536 unsigned char status;
4537 struct stat sb;
4538 char *ondisk_path;
4540 /* Reject rebase of a work tree with mixed base commits. */
4541 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4542 SHA1_DIGEST_LENGTH))
4543 return got_error(GOT_ERR_MIXED_COMMITS);
4545 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4546 == -1)
4547 return got_error_from_errno("asprintf");
4549 /* Reject rebase of a work tree with modified or staged files. */
4550 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4551 free(ondisk_path);
4552 if (err)
4553 return err;
4555 if (status != GOT_STATUS_NO_CHANGE)
4556 return got_error(GOT_ERR_MODIFIED);
4557 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4558 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4560 return NULL;
4563 const struct got_error *
4564 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4565 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4566 struct got_worktree *worktree, struct got_reference *branch,
4567 struct got_repository *repo)
4569 const struct got_error *err = NULL;
4570 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4571 char *branch_ref_name = NULL;
4572 char *fileindex_path = NULL;
4573 struct check_rebase_ok_arg ok_arg;
4574 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4576 *new_base_branch_ref = NULL;
4577 *tmp_branch = NULL;
4578 *fileindex = NULL;
4580 err = lock_worktree(worktree, LOCK_EX);
4581 if (err)
4582 return err;
4584 err = open_fileindex(fileindex, &fileindex_path, worktree);
4585 if (err)
4586 goto done;
4588 ok_arg.worktree = worktree;
4589 ok_arg.repo = repo;
4590 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4591 &ok_arg);
4592 if (err)
4593 goto done;
4595 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4596 if (err)
4597 goto done;
4599 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4600 if (err)
4601 goto done;
4603 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4604 if (err)
4605 goto done;
4607 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4608 0);
4609 if (err)
4610 goto done;
4612 err = got_ref_alloc_symref(new_base_branch_ref,
4613 new_base_branch_ref_name, wt_branch);
4614 if (err)
4615 goto done;
4616 err = got_ref_write(*new_base_branch_ref, repo);
4617 if (err)
4618 goto done;
4620 /* TODO Lock original branch's ref while rebasing? */
4622 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4623 if (err)
4624 goto done;
4626 err = got_ref_write(branch_ref, repo);
4627 if (err)
4628 goto done;
4630 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4631 worktree->base_commit_id);
4632 if (err)
4633 goto done;
4634 err = got_ref_write(*tmp_branch, repo);
4635 if (err)
4636 goto done;
4638 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4639 if (err)
4640 goto done;
4641 done:
4642 free(fileindex_path);
4643 free(tmp_branch_name);
4644 free(new_base_branch_ref_name);
4645 free(branch_ref_name);
4646 if (branch_ref)
4647 got_ref_close(branch_ref);
4648 if (wt_branch)
4649 got_ref_close(wt_branch);
4650 if (err) {
4651 if (*new_base_branch_ref) {
4652 got_ref_close(*new_base_branch_ref);
4653 *new_base_branch_ref = NULL;
4655 if (*tmp_branch) {
4656 got_ref_close(*tmp_branch);
4657 *tmp_branch = NULL;
4659 if (*fileindex) {
4660 got_fileindex_free(*fileindex);
4661 *fileindex = NULL;
4663 lock_worktree(worktree, LOCK_SH);
4665 return err;
4668 const struct got_error *
4669 got_worktree_rebase_continue(struct got_object_id **commit_id,
4670 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4671 struct got_reference **branch, struct got_fileindex **fileindex,
4672 struct got_worktree *worktree, struct got_repository *repo)
4674 const struct got_error *err;
4675 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4676 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4677 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4678 char *fileindex_path = NULL;
4679 int have_staged_files = 0;
4681 *commit_id = NULL;
4682 *new_base_branch = NULL;
4683 *tmp_branch = NULL;
4684 *branch = NULL;
4685 *fileindex = NULL;
4687 err = lock_worktree(worktree, LOCK_EX);
4688 if (err)
4689 return err;
4691 err = open_fileindex(fileindex, &fileindex_path, worktree);
4692 if (err)
4693 goto done;
4695 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4696 &have_staged_files);
4697 if (err && err->code != GOT_ERR_CANCELLED)
4698 goto done;
4699 if (have_staged_files) {
4700 err = got_error(GOT_ERR_STAGED_PATHS);
4701 goto done;
4704 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4705 if (err)
4706 goto done;
4708 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4709 if (err)
4710 goto done;
4712 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4713 if (err)
4714 goto done;
4716 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4717 if (err)
4718 goto done;
4720 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4721 if (err)
4722 goto done;
4724 err = got_ref_open(branch, repo,
4725 got_ref_get_symref_target(branch_ref), 0);
4726 if (err)
4727 goto done;
4729 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4730 if (err)
4731 goto done;
4733 err = got_ref_resolve(commit_id, repo, commit_ref);
4734 if (err)
4735 goto done;
4737 err = got_ref_open(new_base_branch, repo,
4738 new_base_branch_ref_name, 0);
4739 if (err)
4740 goto done;
4742 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4743 if (err)
4744 goto done;
4745 done:
4746 free(commit_ref_name);
4747 free(branch_ref_name);
4748 free(fileindex_path);
4749 if (commit_ref)
4750 got_ref_close(commit_ref);
4751 if (branch_ref)
4752 got_ref_close(branch_ref);
4753 if (err) {
4754 free(*commit_id);
4755 *commit_id = NULL;
4756 if (*tmp_branch) {
4757 got_ref_close(*tmp_branch);
4758 *tmp_branch = NULL;
4760 if (*new_base_branch) {
4761 got_ref_close(*new_base_branch);
4762 *new_base_branch = NULL;
4764 if (*branch) {
4765 got_ref_close(*branch);
4766 *branch = NULL;
4768 if (*fileindex) {
4769 got_fileindex_free(*fileindex);
4770 *fileindex = NULL;
4772 lock_worktree(worktree, LOCK_SH);
4774 return err;
4777 const struct got_error *
4778 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4780 const struct got_error *err;
4781 char *tmp_branch_name = NULL;
4783 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4784 if (err)
4785 return err;
4787 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4788 free(tmp_branch_name);
4789 return NULL;
4792 static const struct got_error *
4793 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4794 char **logmsg, void *arg)
4796 *logmsg = arg;
4797 return NULL;
4800 static const struct got_error *
4801 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4802 const char *path, struct got_object_id *blob_id,
4803 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4805 return NULL;
4808 struct collect_merged_paths_arg {
4809 got_worktree_checkout_cb progress_cb;
4810 void *progress_arg;
4811 struct got_pathlist_head *merged_paths;
4814 static const struct got_error *
4815 collect_merged_paths(void *arg, unsigned char status, const char *path)
4817 const struct got_error *err;
4818 struct collect_merged_paths_arg *a = arg;
4819 char *p;
4820 struct got_pathlist_entry *new;
4822 err = (*a->progress_cb)(a->progress_arg, status, path);
4823 if (err)
4824 return err;
4826 if (status != GOT_STATUS_MERGE &&
4827 status != GOT_STATUS_ADD &&
4828 status != GOT_STATUS_DELETE &&
4829 status != GOT_STATUS_CONFLICT)
4830 return NULL;
4832 p = strdup(path);
4833 if (p == NULL)
4834 return got_error_from_errno("strdup");
4836 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4837 if (err || new == NULL)
4838 free(p);
4839 return err;
4842 void
4843 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4845 struct got_pathlist_entry *pe;
4847 TAILQ_FOREACH(pe, merged_paths, entry)
4848 free((char *)pe->path);
4850 got_pathlist_free(merged_paths);
4853 static const struct got_error *
4854 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4855 struct got_repository *repo)
4857 const struct got_error *err;
4858 struct got_reference *commit_ref = NULL;
4860 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4861 if (err) {
4862 if (err->code != GOT_ERR_NOT_REF)
4863 goto done;
4864 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4865 if (err)
4866 goto done;
4867 err = got_ref_write(commit_ref, repo);
4868 if (err)
4869 goto done;
4870 } else {
4871 struct got_object_id *stored_id;
4872 int cmp;
4874 err = got_ref_resolve(&stored_id, repo, commit_ref);
4875 if (err)
4876 goto done;
4877 cmp = got_object_id_cmp(commit_id, stored_id);
4878 free(stored_id);
4879 if (cmp != 0) {
4880 err = got_error(GOT_ERR_REBASE_COMMITID);
4881 goto done;
4884 done:
4885 if (commit_ref)
4886 got_ref_close(commit_ref);
4887 return err;
4890 static const struct got_error *
4891 rebase_merge_files(struct got_pathlist_head *merged_paths,
4892 const char *commit_ref_name, struct got_worktree *worktree,
4893 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4894 struct got_object_id *commit_id, struct got_repository *repo,
4895 got_worktree_checkout_cb progress_cb, void *progress_arg,
4896 got_cancel_cb cancel_cb, void *cancel_arg)
4898 const struct got_error *err;
4899 struct got_reference *commit_ref = NULL;
4900 struct collect_merged_paths_arg cmp_arg;
4901 char *fileindex_path;
4903 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4905 err = get_fileindex_path(&fileindex_path, worktree);
4906 if (err)
4907 return err;
4909 cmp_arg.progress_cb = progress_cb;
4910 cmp_arg.progress_arg = progress_arg;
4911 cmp_arg.merged_paths = merged_paths;
4912 err = merge_files(worktree, fileindex, fileindex_path,
4913 parent_commit_id, commit_id, repo, collect_merged_paths,
4914 &cmp_arg, cancel_cb, cancel_arg);
4915 if (commit_ref)
4916 got_ref_close(commit_ref);
4917 return err;
4920 const struct got_error *
4921 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4922 struct got_worktree *worktree, struct got_fileindex *fileindex,
4923 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4924 struct got_repository *repo,
4925 got_worktree_checkout_cb progress_cb, void *progress_arg,
4926 got_cancel_cb cancel_cb, void *cancel_arg)
4928 const struct got_error *err;
4929 char *commit_ref_name;
4931 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4932 if (err)
4933 return err;
4935 err = store_commit_id(commit_ref_name, commit_id, repo);
4936 if (err)
4937 goto done;
4939 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4940 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4941 progress_arg, cancel_cb, cancel_arg);
4942 done:
4943 free(commit_ref_name);
4944 return err;
4947 const struct got_error *
4948 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4949 struct got_worktree *worktree, struct got_fileindex *fileindex,
4950 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4951 struct got_repository *repo,
4952 got_worktree_checkout_cb progress_cb, void *progress_arg,
4953 got_cancel_cb cancel_cb, void *cancel_arg)
4955 const struct got_error *err;
4956 char *commit_ref_name;
4958 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4959 if (err)
4960 return err;
4962 err = store_commit_id(commit_ref_name, commit_id, repo);
4963 if (err)
4964 goto done;
4966 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4967 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4968 progress_arg, cancel_cb, cancel_arg);
4969 done:
4970 free(commit_ref_name);
4971 return err;
4974 static const struct got_error *
4975 rebase_commit(struct got_object_id **new_commit_id,
4976 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4977 struct got_worktree *worktree, struct got_fileindex *fileindex,
4978 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4979 const char *new_logmsg, struct got_repository *repo)
4981 const struct got_error *err, *sync_err;
4982 struct got_pathlist_head commitable_paths;
4983 struct collect_commitables_arg cc_arg;
4984 char *fileindex_path = NULL;
4985 struct got_reference *head_ref = NULL;
4986 struct got_object_id *head_commit_id = NULL;
4987 char *logmsg = NULL;
4989 TAILQ_INIT(&commitable_paths);
4990 *new_commit_id = NULL;
4992 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4994 err = get_fileindex_path(&fileindex_path, worktree);
4995 if (err)
4996 return err;
4998 cc_arg.commitable_paths = &commitable_paths;
4999 cc_arg.worktree = worktree;
5000 cc_arg.repo = repo;
5001 cc_arg.have_staged_files = 0;
5003 * If possible get the status of individual files directly to
5004 * avoid crawling the entire work tree once per rebased commit.
5005 * TODO: Ideally, merged_paths would contain a list of commitables
5006 * we could use so we could skip worktree_status() entirely.
5008 if (merged_paths) {
5009 struct got_pathlist_entry *pe;
5010 if (TAILQ_EMPTY(merged_paths)) {
5011 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5012 goto done;
5014 TAILQ_FOREACH(pe, merged_paths, entry) {
5015 err = worktree_status(worktree, pe->path, fileindex,
5016 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5017 0);
5018 if (err)
5019 goto done;
5021 } else {
5022 err = worktree_status(worktree, "", fileindex, repo,
5023 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5024 if (err)
5025 goto done;
5028 if (TAILQ_EMPTY(&commitable_paths)) {
5029 /* No-op change; commit will be elided. */
5030 err = got_ref_delete(commit_ref, repo);
5031 if (err)
5032 goto done;
5033 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5034 goto done;
5037 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5038 if (err)
5039 goto done;
5041 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5042 if (err)
5043 goto done;
5045 if (new_logmsg) {
5046 logmsg = strdup(new_logmsg);
5047 if (logmsg == NULL) {
5048 err = got_error_from_errno("strdup");
5049 goto done;
5051 } else {
5052 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5053 if (err)
5054 goto done;
5057 /* NB: commit_worktree will call free(logmsg) */
5058 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5059 worktree, got_object_commit_get_author(orig_commit),
5060 got_object_commit_get_committer(orig_commit),
5061 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5062 if (err)
5063 goto done;
5065 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5066 if (err)
5067 goto done;
5069 err = got_ref_delete(commit_ref, repo);
5070 if (err)
5071 goto done;
5073 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5074 fileindex, 0);
5075 sync_err = sync_fileindex(fileindex, fileindex_path);
5076 if (sync_err && err == NULL)
5077 err = sync_err;
5078 done:
5079 free(fileindex_path);
5080 free(head_commit_id);
5081 if (head_ref)
5082 got_ref_close(head_ref);
5083 if (err) {
5084 free(*new_commit_id);
5085 *new_commit_id = NULL;
5087 return err;
5090 const struct got_error *
5091 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5092 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5093 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5094 struct got_commit_object *orig_commit,
5095 struct got_object_id *orig_commit_id, struct got_repository *repo)
5097 const struct got_error *err;
5098 char *commit_ref_name;
5099 struct got_reference *commit_ref = NULL;
5100 struct got_object_id *commit_id = NULL;
5102 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5103 if (err)
5104 return err;
5106 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5107 if (err)
5108 goto done;
5109 err = got_ref_resolve(&commit_id, repo, commit_ref);
5110 if (err)
5111 goto done;
5112 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5113 err = got_error(GOT_ERR_REBASE_COMMITID);
5114 goto done;
5117 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5118 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5119 done:
5120 if (commit_ref)
5121 got_ref_close(commit_ref);
5122 free(commit_ref_name);
5123 free(commit_id);
5124 return err;
5127 const struct got_error *
5128 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5129 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5130 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5131 struct got_commit_object *orig_commit,
5132 struct got_object_id *orig_commit_id, const char *new_logmsg,
5133 struct got_repository *repo)
5135 const struct got_error *err;
5136 char *commit_ref_name;
5137 struct got_reference *commit_ref = NULL;
5138 struct got_object_id *commit_id = NULL;
5140 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5141 if (err)
5142 return err;
5144 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5145 if (err)
5146 goto done;
5147 err = got_ref_resolve(&commit_id, repo, commit_ref);
5148 if (err)
5149 goto done;
5150 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5151 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5152 goto done;
5155 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5156 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5157 done:
5158 if (commit_ref)
5159 got_ref_close(commit_ref);
5160 free(commit_ref_name);
5161 free(commit_id);
5162 return err;
5165 const struct got_error *
5166 got_worktree_rebase_postpone(struct got_worktree *worktree,
5167 struct got_fileindex *fileindex)
5169 if (fileindex)
5170 got_fileindex_free(fileindex);
5171 return lock_worktree(worktree, LOCK_SH);
5174 static const struct got_error *
5175 delete_ref(const char *name, struct got_repository *repo)
5177 const struct got_error *err;
5178 struct got_reference *ref;
5180 err = got_ref_open(&ref, repo, name, 0);
5181 if (err) {
5182 if (err->code == GOT_ERR_NOT_REF)
5183 return NULL;
5184 return err;
5187 err = got_ref_delete(ref, repo);
5188 got_ref_close(ref);
5189 return err;
5192 static const struct got_error *
5193 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5195 const struct got_error *err;
5196 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5197 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5199 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5200 if (err)
5201 goto done;
5202 err = delete_ref(tmp_branch_name, repo);
5203 if (err)
5204 goto done;
5206 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5207 if (err)
5208 goto done;
5209 err = delete_ref(new_base_branch_ref_name, repo);
5210 if (err)
5211 goto done;
5213 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5214 if (err)
5215 goto done;
5216 err = delete_ref(branch_ref_name, repo);
5217 if (err)
5218 goto done;
5220 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5221 if (err)
5222 goto done;
5223 err = delete_ref(commit_ref_name, repo);
5224 if (err)
5225 goto done;
5227 done:
5228 free(tmp_branch_name);
5229 free(new_base_branch_ref_name);
5230 free(branch_ref_name);
5231 free(commit_ref_name);
5232 return err;
5235 const struct got_error *
5236 got_worktree_rebase_complete(struct got_worktree *worktree,
5237 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5238 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5239 struct got_repository *repo)
5241 const struct got_error *err, *unlockerr;
5242 struct got_object_id *new_head_commit_id = NULL;
5244 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5245 if (err)
5246 return err;
5248 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5249 if (err)
5250 goto done;
5252 err = got_ref_write(rebased_branch, repo);
5253 if (err)
5254 goto done;
5256 err = got_worktree_set_head_ref(worktree, rebased_branch);
5257 if (err)
5258 goto done;
5260 err = delete_rebase_refs(worktree, repo);
5261 done:
5262 if (fileindex)
5263 got_fileindex_free(fileindex);
5264 free(new_head_commit_id);
5265 unlockerr = lock_worktree(worktree, LOCK_SH);
5266 if (unlockerr && err == NULL)
5267 err = unlockerr;
5268 return err;
5271 const struct got_error *
5272 got_worktree_rebase_abort(struct got_worktree *worktree,
5273 struct got_fileindex *fileindex, struct got_repository *repo,
5274 struct got_reference *new_base_branch,
5275 got_worktree_checkout_cb progress_cb, void *progress_arg)
5277 const struct got_error *err, *unlockerr, *sync_err;
5278 struct got_reference *resolved = NULL;
5279 struct got_object_id *commit_id = NULL;
5280 char *fileindex_path = NULL;
5281 struct revert_file_args rfa;
5282 struct got_object_id *tree_id = NULL;
5284 err = lock_worktree(worktree, LOCK_EX);
5285 if (err)
5286 return err;
5288 err = got_ref_open(&resolved, repo,
5289 got_ref_get_symref_target(new_base_branch), 0);
5290 if (err)
5291 goto done;
5293 err = got_worktree_set_head_ref(worktree, resolved);
5294 if (err)
5295 goto done;
5298 * XXX commits to the base branch could have happened while
5299 * we were busy rebasing; should we store the original commit ID
5300 * when rebase begins and read it back here?
5302 err = got_ref_resolve(&commit_id, repo, resolved);
5303 if (err)
5304 goto done;
5306 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5307 if (err)
5308 goto done;
5310 err = got_object_id_by_path(&tree_id, repo,
5311 worktree->base_commit_id, worktree->path_prefix);
5312 if (err)
5313 goto done;
5315 err = delete_rebase_refs(worktree, repo);
5316 if (err)
5317 goto done;
5319 err = get_fileindex_path(&fileindex_path, worktree);
5320 if (err)
5321 goto done;
5323 rfa.worktree = worktree;
5324 rfa.fileindex = fileindex;
5325 rfa.progress_cb = progress_cb;
5326 rfa.progress_arg = progress_arg;
5327 rfa.patch_cb = NULL;
5328 rfa.patch_arg = NULL;
5329 rfa.repo = repo;
5330 err = worktree_status(worktree, "", fileindex, repo,
5331 revert_file, &rfa, NULL, NULL, 0, 0);
5332 if (err)
5333 goto sync;
5335 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5336 repo, progress_cb, progress_arg, NULL, NULL);
5337 sync:
5338 sync_err = sync_fileindex(fileindex, fileindex_path);
5339 if (sync_err && err == NULL)
5340 err = sync_err;
5341 done:
5342 got_ref_close(resolved);
5343 free(tree_id);
5344 free(commit_id);
5345 if (fileindex)
5346 got_fileindex_free(fileindex);
5347 free(fileindex_path);
5349 unlockerr = lock_worktree(worktree, LOCK_SH);
5350 if (unlockerr && err == NULL)
5351 err = unlockerr;
5352 return err;
5355 const struct got_error *
5356 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5357 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5358 struct got_fileindex **fileindex, struct got_worktree *worktree,
5359 struct got_repository *repo)
5361 const struct got_error *err = NULL;
5362 char *tmp_branch_name = NULL;
5363 char *branch_ref_name = NULL;
5364 char *base_commit_ref_name = NULL;
5365 char *fileindex_path = NULL;
5366 struct check_rebase_ok_arg ok_arg;
5367 struct got_reference *wt_branch = NULL;
5368 struct got_reference *base_commit_ref = NULL;
5370 *tmp_branch = NULL;
5371 *branch_ref = NULL;
5372 *base_commit_id = NULL;
5373 *fileindex = NULL;
5375 err = lock_worktree(worktree, LOCK_EX);
5376 if (err)
5377 return err;
5379 err = open_fileindex(fileindex, &fileindex_path, worktree);
5380 if (err)
5381 goto done;
5383 ok_arg.worktree = worktree;
5384 ok_arg.repo = repo;
5385 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5386 &ok_arg);
5387 if (err)
5388 goto done;
5390 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5391 if (err)
5392 goto done;
5394 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5395 if (err)
5396 goto done;
5398 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5399 worktree);
5400 if (err)
5401 goto done;
5403 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5404 0);
5405 if (err)
5406 goto done;
5408 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5409 if (err)
5410 goto done;
5412 err = got_ref_write(*branch_ref, repo);
5413 if (err)
5414 goto done;
5416 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5417 worktree->base_commit_id);
5418 if (err)
5419 goto done;
5420 err = got_ref_write(base_commit_ref, repo);
5421 if (err)
5422 goto done;
5423 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5424 if (*base_commit_id == NULL) {
5425 err = got_error_from_errno("got_object_id_dup");
5426 goto done;
5429 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5430 worktree->base_commit_id);
5431 if (err)
5432 goto done;
5433 err = got_ref_write(*tmp_branch, repo);
5434 if (err)
5435 goto done;
5437 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5438 if (err)
5439 goto done;
5440 done:
5441 free(fileindex_path);
5442 free(tmp_branch_name);
5443 free(branch_ref_name);
5444 free(base_commit_ref_name);
5445 if (wt_branch)
5446 got_ref_close(wt_branch);
5447 if (err) {
5448 if (*branch_ref) {
5449 got_ref_close(*branch_ref);
5450 *branch_ref = NULL;
5452 if (*tmp_branch) {
5453 got_ref_close(*tmp_branch);
5454 *tmp_branch = NULL;
5456 free(*base_commit_id);
5457 if (*fileindex) {
5458 got_fileindex_free(*fileindex);
5459 *fileindex = NULL;
5461 lock_worktree(worktree, LOCK_SH);
5463 return err;
5466 const struct got_error *
5467 got_worktree_histedit_postpone(struct got_worktree *worktree,
5468 struct got_fileindex *fileindex)
5470 if (fileindex)
5471 got_fileindex_free(fileindex);
5472 return lock_worktree(worktree, LOCK_SH);
5475 const struct got_error *
5476 got_worktree_histedit_in_progress(int *in_progress,
5477 struct got_worktree *worktree)
5479 const struct got_error *err;
5480 char *tmp_branch_name = NULL;
5482 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5483 if (err)
5484 return err;
5486 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5487 free(tmp_branch_name);
5488 return NULL;
5491 const struct got_error *
5492 got_worktree_histedit_continue(struct got_object_id **commit_id,
5493 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5494 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5495 struct got_worktree *worktree, struct got_repository *repo)
5497 const struct got_error *err;
5498 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5499 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5500 struct got_reference *commit_ref = NULL;
5501 struct got_reference *base_commit_ref = NULL;
5502 char *fileindex_path = NULL;
5503 int have_staged_files = 0;
5505 *commit_id = NULL;
5506 *tmp_branch = NULL;
5507 *base_commit_id = NULL;
5508 *fileindex = NULL;
5510 err = lock_worktree(worktree, LOCK_EX);
5511 if (err)
5512 return err;
5514 err = open_fileindex(fileindex, &fileindex_path, worktree);
5515 if (err)
5516 goto done;
5518 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5519 &have_staged_files);
5520 if (err && err->code != GOT_ERR_CANCELLED)
5521 goto done;
5522 if (have_staged_files) {
5523 err = got_error(GOT_ERR_STAGED_PATHS);
5524 goto done;
5527 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5528 if (err)
5529 goto done;
5531 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5532 if (err)
5533 goto done;
5535 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5536 if (err)
5537 goto done;
5539 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5540 worktree);
5541 if (err)
5542 goto done;
5544 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5545 if (err)
5546 goto done;
5548 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5549 if (err)
5550 goto done;
5551 err = got_ref_resolve(commit_id, repo, commit_ref);
5552 if (err)
5553 goto done;
5555 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5556 if (err)
5557 goto done;
5558 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5559 if (err)
5560 goto done;
5562 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5563 if (err)
5564 goto done;
5565 done:
5566 free(commit_ref_name);
5567 free(branch_ref_name);
5568 free(fileindex_path);
5569 if (commit_ref)
5570 got_ref_close(commit_ref);
5571 if (base_commit_ref)
5572 got_ref_close(base_commit_ref);
5573 if (err) {
5574 free(*commit_id);
5575 *commit_id = NULL;
5576 free(*base_commit_id);
5577 *base_commit_id = NULL;
5578 if (*tmp_branch) {
5579 got_ref_close(*tmp_branch);
5580 *tmp_branch = NULL;
5582 if (*fileindex) {
5583 got_fileindex_free(*fileindex);
5584 *fileindex = NULL;
5586 lock_worktree(worktree, LOCK_EX);
5588 return err;
5591 static const struct got_error *
5592 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5594 const struct got_error *err;
5595 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5596 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5598 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5599 if (err)
5600 goto done;
5601 err = delete_ref(tmp_branch_name, repo);
5602 if (err)
5603 goto done;
5605 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5606 worktree);
5607 if (err)
5608 goto done;
5609 err = delete_ref(base_commit_ref_name, repo);
5610 if (err)
5611 goto done;
5613 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5614 if (err)
5615 goto done;
5616 err = delete_ref(branch_ref_name, repo);
5617 if (err)
5618 goto done;
5620 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5621 if (err)
5622 goto done;
5623 err = delete_ref(commit_ref_name, repo);
5624 if (err)
5625 goto done;
5626 done:
5627 free(tmp_branch_name);
5628 free(base_commit_ref_name);
5629 free(branch_ref_name);
5630 free(commit_ref_name);
5631 return err;
5634 const struct got_error *
5635 got_worktree_histedit_abort(struct got_worktree *worktree,
5636 struct got_fileindex *fileindex, struct got_repository *repo,
5637 struct got_reference *branch, struct got_object_id *base_commit_id,
5638 got_worktree_checkout_cb progress_cb, void *progress_arg)
5640 const struct got_error *err, *unlockerr, *sync_err;
5641 struct got_reference *resolved = NULL;
5642 char *fileindex_path = NULL;
5643 struct got_object_id *tree_id = NULL;
5644 struct revert_file_args rfa;
5646 err = lock_worktree(worktree, LOCK_EX);
5647 if (err)
5648 return err;
5650 err = got_ref_open(&resolved, repo,
5651 got_ref_get_symref_target(branch), 0);
5652 if (err)
5653 goto done;
5655 err = got_worktree_set_head_ref(worktree, resolved);
5656 if (err)
5657 goto done;
5659 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5660 if (err)
5661 goto done;
5663 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5664 worktree->path_prefix);
5665 if (err)
5666 goto done;
5668 err = delete_histedit_refs(worktree, repo);
5669 if (err)
5670 goto done;
5672 err = get_fileindex_path(&fileindex_path, worktree);
5673 if (err)
5674 goto done;
5676 rfa.worktree = worktree;
5677 rfa.fileindex = fileindex;
5678 rfa.progress_cb = progress_cb;
5679 rfa.progress_arg = progress_arg;
5680 rfa.patch_cb = NULL;
5681 rfa.patch_arg = NULL;
5682 rfa.repo = repo;
5683 err = worktree_status(worktree, "", fileindex, repo,
5684 revert_file, &rfa, NULL, NULL, 0, 0);
5685 if (err)
5686 goto sync;
5688 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5689 repo, progress_cb, progress_arg, NULL, NULL);
5690 sync:
5691 sync_err = sync_fileindex(fileindex, fileindex_path);
5692 if (sync_err && err == NULL)
5693 err = sync_err;
5694 done:
5695 got_ref_close(resolved);
5696 free(tree_id);
5697 free(fileindex_path);
5699 unlockerr = lock_worktree(worktree, LOCK_SH);
5700 if (unlockerr && err == NULL)
5701 err = unlockerr;
5702 return err;
5705 const struct got_error *
5706 got_worktree_histedit_complete(struct got_worktree *worktree,
5707 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5708 struct got_reference *edited_branch, struct got_repository *repo)
5710 const struct got_error *err, *unlockerr;
5711 struct got_object_id *new_head_commit_id = NULL;
5712 struct got_reference *resolved = NULL;
5714 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5715 if (err)
5716 return err;
5718 err = got_ref_open(&resolved, repo,
5719 got_ref_get_symref_target(edited_branch), 0);
5720 if (err)
5721 goto done;
5723 err = got_ref_change_ref(resolved, new_head_commit_id);
5724 if (err)
5725 goto done;
5727 err = got_ref_write(resolved, repo);
5728 if (err)
5729 goto done;
5731 err = got_worktree_set_head_ref(worktree, resolved);
5732 if (err)
5733 goto done;
5735 err = delete_histedit_refs(worktree, repo);
5736 done:
5737 if (fileindex)
5738 got_fileindex_free(fileindex);
5739 free(new_head_commit_id);
5740 unlockerr = lock_worktree(worktree, LOCK_SH);
5741 if (unlockerr && err == NULL)
5742 err = unlockerr;
5743 return err;
5746 const struct got_error *
5747 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5748 struct got_object_id *commit_id, struct got_repository *repo)
5750 const struct got_error *err;
5751 char *commit_ref_name;
5753 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5754 if (err)
5755 return err;
5757 err = store_commit_id(commit_ref_name, commit_id, repo);
5758 if (err)
5759 goto done;
5761 err = delete_ref(commit_ref_name, repo);
5762 done:
5763 free(commit_ref_name);
5764 return err;
5767 const struct got_error *
5768 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5769 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5770 struct got_worktree *worktree, const char *refname,
5771 struct got_repository *repo)
5773 const struct got_error *err = NULL;
5774 char *fileindex_path = NULL;
5775 struct check_rebase_ok_arg ok_arg;
5777 *fileindex = NULL;
5778 *branch_ref = NULL;
5779 *base_branch_ref = NULL;
5781 err = lock_worktree(worktree, LOCK_EX);
5782 if (err)
5783 return err;
5785 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5786 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5787 "cannot integrate a branch into itself; "
5788 "update -b or different branch name required");
5789 goto done;
5792 err = open_fileindex(fileindex, &fileindex_path, worktree);
5793 if (err)
5794 goto done;
5796 /* Preconditions are the same as for rebase. */
5797 ok_arg.worktree = worktree;
5798 ok_arg.repo = repo;
5799 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5800 &ok_arg);
5801 if (err)
5802 goto done;
5804 err = got_ref_open(branch_ref, repo, refname, 1);
5805 if (err)
5806 goto done;
5808 err = got_ref_open(base_branch_ref, repo,
5809 got_worktree_get_head_ref_name(worktree), 1);
5810 done:
5811 if (err) {
5812 if (*branch_ref) {
5813 got_ref_close(*branch_ref);
5814 *branch_ref = NULL;
5816 if (*base_branch_ref) {
5817 got_ref_close(*base_branch_ref);
5818 *base_branch_ref = NULL;
5820 if (*fileindex) {
5821 got_fileindex_free(*fileindex);
5822 *fileindex = NULL;
5824 lock_worktree(worktree, LOCK_SH);
5826 return err;
5829 const struct got_error *
5830 got_worktree_integrate_continue(struct got_worktree *worktree,
5831 struct got_fileindex *fileindex, struct got_repository *repo,
5832 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5833 got_worktree_checkout_cb progress_cb, void *progress_arg,
5834 got_cancel_cb cancel_cb, void *cancel_arg)
5836 const struct got_error *err = NULL, *sync_err, *unlockerr;
5837 char *fileindex_path = NULL;
5838 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5840 err = get_fileindex_path(&fileindex_path, worktree);
5841 if (err)
5842 goto done;
5844 err = got_ref_resolve(&commit_id, repo, branch_ref);
5845 if (err)
5846 goto done;
5848 err = got_object_id_by_path(&tree_id, repo, commit_id,
5849 worktree->path_prefix);
5850 if (err)
5851 goto done;
5853 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5854 if (err)
5855 goto done;
5857 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5858 progress_cb, progress_arg, cancel_cb, cancel_arg);
5859 if (err)
5860 goto sync;
5862 err = got_ref_change_ref(base_branch_ref, commit_id);
5863 if (err)
5864 goto sync;
5866 err = got_ref_write(base_branch_ref, repo);
5867 sync:
5868 sync_err = sync_fileindex(fileindex, fileindex_path);
5869 if (sync_err && err == NULL)
5870 err = sync_err;
5872 done:
5873 unlockerr = got_ref_unlock(branch_ref);
5874 if (unlockerr && err == NULL)
5875 err = unlockerr;
5876 got_ref_close(branch_ref);
5878 unlockerr = got_ref_unlock(base_branch_ref);
5879 if (unlockerr && err == NULL)
5880 err = unlockerr;
5881 got_ref_close(base_branch_ref);
5883 got_fileindex_free(fileindex);
5884 free(fileindex_path);
5885 free(tree_id);
5887 unlockerr = lock_worktree(worktree, LOCK_SH);
5888 if (unlockerr && err == NULL)
5889 err = unlockerr;
5890 return err;
5893 const struct got_error *
5894 got_worktree_integrate_abort(struct got_worktree *worktree,
5895 struct got_fileindex *fileindex, struct got_repository *repo,
5896 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5898 const struct got_error *err = NULL, *unlockerr = NULL;
5900 got_fileindex_free(fileindex);
5902 err = lock_worktree(worktree, LOCK_SH);
5904 unlockerr = got_ref_unlock(branch_ref);
5905 if (unlockerr && err == NULL)
5906 err = unlockerr;
5907 got_ref_close(branch_ref);
5909 unlockerr = got_ref_unlock(base_branch_ref);
5910 if (unlockerr && err == NULL)
5911 err = unlockerr;
5912 got_ref_close(base_branch_ref);
5914 return err;
5917 struct check_stage_ok_arg {
5918 struct got_object_id *head_commit_id;
5919 struct got_worktree *worktree;
5920 struct got_fileindex *fileindex;
5921 struct got_repository *repo;
5922 int have_changes;
5925 const struct got_error *
5926 check_stage_ok(void *arg, unsigned char status,
5927 unsigned char staged_status, const char *relpath,
5928 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5929 struct got_object_id *commit_id)
5931 struct check_stage_ok_arg *a = arg;
5932 const struct got_error *err = NULL;
5933 struct got_fileindex_entry *ie;
5934 struct got_object_id base_commit_id;
5935 struct got_object_id *base_commit_idp = NULL;
5936 char *in_repo_path = NULL, *p;
5938 if (status == GOT_STATUS_UNVERSIONED ||
5939 status == GOT_STATUS_NO_CHANGE)
5940 return NULL;
5941 if (status == GOT_STATUS_NONEXISTENT)
5942 return got_error_set_errno(ENOENT, relpath);
5944 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5945 if (ie == NULL)
5946 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5948 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5949 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5950 relpath) == -1)
5951 return got_error_from_errno("asprintf");
5953 if (got_fileindex_entry_has_commit(ie)) {
5954 memcpy(base_commit_id.sha1, ie->commit_sha1,
5955 SHA1_DIGEST_LENGTH);
5956 base_commit_idp = &base_commit_id;
5959 if (status == GOT_STATUS_CONFLICT) {
5960 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5961 goto done;
5962 } else if (status != GOT_STATUS_ADD &&
5963 status != GOT_STATUS_MODIFY &&
5964 status != GOT_STATUS_DELETE) {
5965 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5966 goto done;
5969 a->have_changes = 1;
5971 p = in_repo_path;
5972 while (p[0] == '/')
5973 p++;
5974 err = check_out_of_date(p, status, staged_status,
5975 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5976 GOT_ERR_STAGE_OUT_OF_DATE);
5977 done:
5978 free(in_repo_path);
5979 return err;
5982 struct stage_path_arg {
5983 struct got_worktree *worktree;
5984 struct got_fileindex *fileindex;
5985 struct got_repository *repo;
5986 got_worktree_status_cb status_cb;
5987 void *status_arg;
5988 got_worktree_patch_cb patch_cb;
5989 void *patch_arg;
5990 int staged_something;
5993 static const struct got_error *
5994 stage_path(void *arg, unsigned char status,
5995 unsigned char staged_status, const char *relpath,
5996 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5997 struct got_object_id *commit_id)
5999 struct stage_path_arg *a = arg;
6000 const struct got_error *err = NULL;
6001 struct got_fileindex_entry *ie;
6002 char *ondisk_path = NULL, *path_content = NULL;
6003 uint32_t stage;
6004 struct got_object_id *new_staged_blob_id = NULL;
6006 if (status == GOT_STATUS_UNVERSIONED)
6007 return NULL;
6009 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6010 if (ie == NULL)
6011 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6013 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6014 relpath)== -1)
6015 return got_error_from_errno("asprintf");
6017 switch (status) {
6018 case GOT_STATUS_ADD:
6019 case GOT_STATUS_MODIFY:
6020 if (a->patch_cb) {
6021 if (status == GOT_STATUS_ADD) {
6022 int choice = GOT_PATCH_CHOICE_NONE;
6023 err = (*a->patch_cb)(&choice, a->patch_arg,
6024 status, ie->path, NULL, 1, 1);
6025 if (err)
6026 break;
6027 if (choice != GOT_PATCH_CHOICE_YES)
6028 break;
6029 } else {
6030 err = create_patched_content(&path_content, 0,
6031 staged_blob_id ? staged_blob_id : blob_id,
6032 ondisk_path, ie->path, a->repo,
6033 a->patch_cb, a->patch_arg);
6034 if (err || path_content == NULL)
6035 break;
6038 err = got_object_blob_create(&new_staged_blob_id,
6039 path_content ? path_content : ondisk_path, a->repo);
6040 if (err)
6041 break;
6042 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6043 SHA1_DIGEST_LENGTH);
6044 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6045 stage = GOT_FILEIDX_STAGE_ADD;
6046 else
6047 stage = GOT_FILEIDX_STAGE_MODIFY;
6048 got_fileindex_entry_stage_set(ie, stage);
6049 a->staged_something = 1;
6050 if (a->status_cb == NULL)
6051 break;
6052 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6053 get_staged_status(ie), relpath, blob_id,
6054 new_staged_blob_id, NULL);
6055 break;
6056 case GOT_STATUS_DELETE:
6057 if (staged_status == GOT_STATUS_DELETE)
6058 break;
6059 if (a->patch_cb) {
6060 int choice = GOT_PATCH_CHOICE_NONE;
6061 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6062 ie->path, NULL, 1, 1);
6063 if (err)
6064 break;
6065 if (choice == GOT_PATCH_CHOICE_NO)
6066 break;
6067 if (choice != GOT_PATCH_CHOICE_YES) {
6068 err = got_error(GOT_ERR_PATCH_CHOICE);
6069 break;
6072 stage = GOT_FILEIDX_STAGE_DELETE;
6073 got_fileindex_entry_stage_set(ie, stage);
6074 a->staged_something = 1;
6075 if (a->status_cb == NULL)
6076 break;
6077 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6078 get_staged_status(ie), relpath, NULL, NULL, NULL);
6079 break;
6080 case GOT_STATUS_NO_CHANGE:
6081 break;
6082 case GOT_STATUS_CONFLICT:
6083 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6084 break;
6085 case GOT_STATUS_NONEXISTENT:
6086 err = got_error_set_errno(ENOENT, relpath);
6087 break;
6088 default:
6089 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6090 break;
6093 if (path_content && unlink(path_content) == -1 && err == NULL)
6094 err = got_error_from_errno2("unlink", path_content);
6095 free(path_content);
6096 free(ondisk_path);
6097 free(new_staged_blob_id);
6098 return err;
6101 const struct got_error *
6102 got_worktree_stage(struct got_worktree *worktree,
6103 struct got_pathlist_head *paths,
6104 got_worktree_status_cb status_cb, void *status_arg,
6105 got_worktree_patch_cb patch_cb, void *patch_arg,
6106 struct got_repository *repo)
6108 const struct got_error *err = NULL, *sync_err, *unlockerr;
6109 struct got_pathlist_entry *pe;
6110 struct got_fileindex *fileindex = NULL;
6111 char *fileindex_path = NULL;
6112 struct got_reference *head_ref = NULL;
6113 struct got_object_id *head_commit_id = NULL;
6114 struct check_stage_ok_arg oka;
6115 struct stage_path_arg spa;
6117 err = lock_worktree(worktree, LOCK_EX);
6118 if (err)
6119 return err;
6121 err = got_ref_open(&head_ref, repo,
6122 got_worktree_get_head_ref_name(worktree), 0);
6123 if (err)
6124 goto done;
6125 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6126 if (err)
6127 goto done;
6128 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6129 if (err)
6130 goto done;
6132 /* Check pre-conditions before staging anything. */
6133 oka.head_commit_id = head_commit_id;
6134 oka.worktree = worktree;
6135 oka.fileindex = fileindex;
6136 oka.repo = repo;
6137 oka.have_changes = 0;
6138 TAILQ_FOREACH(pe, paths, entry) {
6139 err = worktree_status(worktree, pe->path, fileindex, repo,
6140 check_stage_ok, &oka, NULL, NULL, 0, 0);
6141 if (err)
6142 goto done;
6144 if (!oka.have_changes) {
6145 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6146 goto done;
6149 spa.worktree = worktree;
6150 spa.fileindex = fileindex;
6151 spa.repo = repo;
6152 spa.patch_cb = patch_cb;
6153 spa.patch_arg = patch_arg;
6154 spa.status_cb = status_cb;
6155 spa.status_arg = status_arg;
6156 spa.staged_something = 0;
6157 TAILQ_FOREACH(pe, paths, entry) {
6158 err = worktree_status(worktree, pe->path, fileindex, repo,
6159 stage_path, &spa, NULL, NULL, 0, 0);
6160 if (err)
6161 goto done;
6163 if (!spa.staged_something) {
6164 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6165 goto done;
6168 sync_err = sync_fileindex(fileindex, fileindex_path);
6169 if (sync_err && err == NULL)
6170 err = sync_err;
6171 done:
6172 if (head_ref)
6173 got_ref_close(head_ref);
6174 free(head_commit_id);
6175 free(fileindex_path);
6176 if (fileindex)
6177 got_fileindex_free(fileindex);
6178 unlockerr = lock_worktree(worktree, LOCK_SH);
6179 if (unlockerr && err == NULL)
6180 err = unlockerr;
6181 return err;
6184 struct unstage_path_arg {
6185 struct got_worktree *worktree;
6186 struct got_fileindex *fileindex;
6187 struct got_repository *repo;
6188 got_worktree_checkout_cb progress_cb;
6189 void *progress_arg;
6190 got_worktree_patch_cb patch_cb;
6191 void *patch_arg;
6194 static const struct got_error *
6195 create_unstaged_content(char **path_unstaged_content,
6196 char **path_new_staged_content, struct got_object_id *blob_id,
6197 struct got_object_id *staged_blob_id, const char *relpath,
6198 struct got_repository *repo,
6199 got_worktree_patch_cb patch_cb, void *patch_arg)
6201 const struct got_error *err;
6202 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6203 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6204 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6205 struct stat sb1, sb2;
6206 struct got_diff_changes *changes = NULL;
6207 struct got_diff_state *ds = NULL;
6208 struct got_diff_args *args = NULL;
6209 struct got_diff_change *change;
6210 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6211 int have_content = 0, have_rejected_content = 0;
6213 *path_unstaged_content = NULL;
6214 *path_new_staged_content = NULL;
6216 err = got_object_id_str(&label1, blob_id);
6217 if (err)
6218 return err;
6219 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6220 if (err)
6221 goto done;
6223 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6224 if (err)
6225 goto done;
6227 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6228 if (err)
6229 goto done;
6231 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6232 if (err)
6233 goto done;
6235 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6236 if (err)
6237 goto done;
6239 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6240 if (err)
6241 goto done;
6243 if (stat(path1, &sb1) == -1) {
6244 err = got_error_from_errno2("stat", path1);
6245 goto done;
6248 if (stat(path2, &sb2) == -1) {
6249 err = got_error_from_errno2("stat", path2);
6250 goto done;
6253 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6254 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6255 if (err)
6256 goto done;
6258 err = got_opentemp_named(path_unstaged_content, &outfile,
6259 "got-unstaged-content");
6260 if (err)
6261 goto done;
6262 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6263 "got-new-staged-content");
6264 if (err)
6265 goto done;
6267 if (fseek(f1, 0L, SEEK_SET) == -1) {
6268 err = got_ferror(f1, GOT_ERR_IO);
6269 goto done;
6271 if (fseek(f2, 0L, SEEK_SET) == -1) {
6272 err = got_ferror(f2, GOT_ERR_IO);
6273 goto done;
6275 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6276 int choice;
6277 err = apply_or_reject_change(&choice, change, ++n,
6278 changes->nchanges, ds, args, diff_flags, relpath,
6279 f1, f2, &line_cur1, &line_cur2,
6280 outfile, rejectfile, patch_cb, patch_arg);
6281 if (err)
6282 goto done;
6283 if (choice == GOT_PATCH_CHOICE_YES)
6284 have_content = 1;
6285 else
6286 have_rejected_content = 1;
6287 if (choice == GOT_PATCH_CHOICE_QUIT)
6288 break;
6290 if (have_content || have_rejected_content)
6291 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6292 outfile, rejectfile);
6293 done:
6294 free(label1);
6295 if (blob)
6296 got_object_blob_close(blob);
6297 if (staged_blob)
6298 got_object_blob_close(staged_blob);
6299 if (f1 && fclose(f1) == EOF && err == NULL)
6300 err = got_error_from_errno2("fclose", path1);
6301 if (f2 && fclose(f2) == EOF && err == NULL)
6302 err = got_error_from_errno2("fclose", path2);
6303 if (outfile && fclose(outfile) == EOF && err == NULL)
6304 err = got_error_from_errno2("fclose", *path_unstaged_content);
6305 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6306 err = got_error_from_errno2("fclose", *path_new_staged_content);
6307 if (path1 && unlink(path1) == -1 && err == NULL)
6308 err = got_error_from_errno2("unlink", path1);
6309 if (path2 && unlink(path2) == -1 && err == NULL)
6310 err = got_error_from_errno2("unlink", path2);
6311 if (err || !have_content) {
6312 if (*path_unstaged_content &&
6313 unlink(*path_unstaged_content) == -1 && err == NULL)
6314 err = got_error_from_errno2("unlink",
6315 *path_unstaged_content);
6316 free(*path_unstaged_content);
6317 *path_unstaged_content = NULL;
6319 if (err || !have_rejected_content) {
6320 if (*path_new_staged_content &&
6321 unlink(*path_new_staged_content) == -1 && err == NULL)
6322 err = got_error_from_errno2("unlink",
6323 *path_new_staged_content);
6324 free(*path_new_staged_content);
6325 *path_new_staged_content = NULL;
6327 free(args);
6328 if (ds) {
6329 got_diff_state_free(ds);
6330 free(ds);
6332 if (changes)
6333 got_diff_free_changes(changes);
6334 free(path1);
6335 free(path2);
6336 return err;
6339 static const struct got_error *
6340 unstage_path(void *arg, unsigned char status,
6341 unsigned char staged_status, const char *relpath,
6342 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6343 struct got_object_id *commit_id)
6345 const struct got_error *err = NULL;
6346 struct unstage_path_arg *a = arg;
6347 struct got_fileindex_entry *ie;
6348 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6349 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6350 char *path_new_staged_content = NULL;
6351 char *id_str = NULL, *label_orig = NULL;
6352 int local_changes_subsumed;
6353 struct stat sb;
6355 if (staged_status != GOT_STATUS_ADD &&
6356 staged_status != GOT_STATUS_MODIFY &&
6357 staged_status != GOT_STATUS_DELETE)
6358 return NULL;
6360 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6361 if (ie == NULL)
6362 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6364 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6365 == -1)
6366 return got_error_from_errno("asprintf");
6368 err = got_object_id_str(&id_str,
6369 commit_id ? commit_id : a->worktree->base_commit_id);
6370 if (err)
6371 goto done;
6372 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6373 id_str) == -1) {
6374 err = got_error_from_errno("asprintf");
6375 goto done;
6378 switch (staged_status) {
6379 case GOT_STATUS_MODIFY:
6380 err = got_object_open_as_blob(&blob_base, a->repo,
6381 blob_id, 8192);
6382 if (err)
6383 break;
6384 /* fall through */
6385 case GOT_STATUS_ADD:
6386 if (a->patch_cb) {
6387 if (staged_status == GOT_STATUS_ADD) {
6388 int choice = GOT_PATCH_CHOICE_NONE;
6389 err = (*a->patch_cb)(&choice, a->patch_arg,
6390 staged_status, ie->path, NULL, 1, 1);
6391 if (err)
6392 break;
6393 if (choice != GOT_PATCH_CHOICE_YES)
6394 break;
6395 } else {
6396 err = create_unstaged_content(
6397 &path_unstaged_content,
6398 &path_new_staged_content, blob_id,
6399 staged_blob_id, ie->path, a->repo,
6400 a->patch_cb, a->patch_arg);
6401 if (err || path_unstaged_content == NULL)
6402 break;
6403 if (path_new_staged_content) {
6404 err = got_object_blob_create(
6405 &staged_blob_id,
6406 path_new_staged_content,
6407 a->repo);
6408 if (err)
6409 break;
6410 memcpy(ie->staged_blob_sha1,
6411 staged_blob_id->sha1,
6412 SHA1_DIGEST_LENGTH);
6414 err = merge_file(&local_changes_subsumed,
6415 a->worktree, blob_base, ondisk_path,
6416 relpath, got_fileindex_perms_to_st(ie),
6417 path_unstaged_content, label_orig,
6418 "unstaged", a->repo, a->progress_cb,
6419 a->progress_arg);
6420 if (err == NULL &&
6421 path_new_staged_content == NULL)
6422 got_fileindex_entry_stage_set(ie,
6423 GOT_FILEIDX_STAGE_NONE);
6424 break; /* Done with this file. */
6427 err = got_object_open_as_blob(&blob_staged, a->repo,
6428 staged_blob_id, 8192);
6429 if (err)
6430 break;
6431 err = merge_blob(&local_changes_subsumed, a->worktree,
6432 blob_base, ondisk_path, relpath,
6433 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6434 commit_id ? commit_id : a->worktree->base_commit_id,
6435 a->repo, a->progress_cb, a->progress_arg);
6436 if (err == NULL)
6437 got_fileindex_entry_stage_set(ie,
6438 GOT_FILEIDX_STAGE_NONE);
6439 break;
6440 case GOT_STATUS_DELETE:
6441 if (a->patch_cb) {
6442 int choice = GOT_PATCH_CHOICE_NONE;
6443 err = (*a->patch_cb)(&choice, a->patch_arg,
6444 staged_status, ie->path, NULL, 1, 1);
6445 if (err)
6446 break;
6447 if (choice == GOT_PATCH_CHOICE_NO)
6448 break;
6449 if (choice != GOT_PATCH_CHOICE_YES) {
6450 err = got_error(GOT_ERR_PATCH_CHOICE);
6451 break;
6454 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6455 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6456 if (err)
6457 break;
6458 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6459 break;
6461 done:
6462 free(ondisk_path);
6463 if (path_unstaged_content &&
6464 unlink(path_unstaged_content) == -1 && err == NULL)
6465 err = got_error_from_errno2("unlink", path_unstaged_content);
6466 if (path_new_staged_content &&
6467 unlink(path_new_staged_content) == -1 && err == NULL)
6468 err = got_error_from_errno2("unlink", path_new_staged_content);
6469 free(path_unstaged_content);
6470 free(path_new_staged_content);
6471 if (blob_base)
6472 got_object_blob_close(blob_base);
6473 if (blob_staged)
6474 got_object_blob_close(blob_staged);
6475 free(id_str);
6476 free(label_orig);
6477 return err;
6480 const struct got_error *
6481 got_worktree_unstage(struct got_worktree *worktree,
6482 struct got_pathlist_head *paths,
6483 got_worktree_checkout_cb progress_cb, void *progress_arg,
6484 got_worktree_patch_cb patch_cb, void *patch_arg,
6485 struct got_repository *repo)
6487 const struct got_error *err = NULL, *sync_err, *unlockerr;
6488 struct got_pathlist_entry *pe;
6489 struct got_fileindex *fileindex = NULL;
6490 char *fileindex_path = NULL;
6491 struct unstage_path_arg upa;
6493 err = lock_worktree(worktree, LOCK_EX);
6494 if (err)
6495 return err;
6497 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6498 if (err)
6499 goto done;
6501 upa.worktree = worktree;
6502 upa.fileindex = fileindex;
6503 upa.repo = repo;
6504 upa.progress_cb = progress_cb;
6505 upa.progress_arg = progress_arg;
6506 upa.patch_cb = patch_cb;
6507 upa.patch_arg = patch_arg;
6508 TAILQ_FOREACH(pe, paths, entry) {
6509 err = worktree_status(worktree, pe->path, fileindex, repo,
6510 unstage_path, &upa, NULL, NULL, 0, 0);
6511 if (err)
6512 goto done;
6515 sync_err = sync_fileindex(fileindex, fileindex_path);
6516 if (sync_err && err == NULL)
6517 err = sync_err;
6518 done:
6519 free(fileindex_path);
6520 if (fileindex)
6521 got_fileindex_free(fileindex);
6522 unlockerr = lock_worktree(worktree, LOCK_SH);
6523 if (unlockerr && err == NULL)
6524 err = unlockerr;
6525 return err;