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 int fd = -1;
1116 FILE *f = NULL;
1117 uint8_t fbuf[8192];
1118 struct got_blob_object *blob = NULL;
1119 size_t flen, blen;
1120 unsigned char staged_status = get_staged_status(ie);
1122 *status = GOT_STATUS_NO_CHANGE;
1124 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1125 if (fd == -1 && errno != ENOENT)
1126 return got_error_from_errno2("open", abspath);
1127 if (fd == -1 || fstat(fd, sb) == -1) {
1128 if (errno == ENOENT) {
1129 if (got_fileindex_entry_has_file_on_disk(ie))
1130 *status = GOT_STATUS_MISSING;
1131 else
1132 *status = GOT_STATUS_DELETE;
1133 goto done;
1135 err = got_error_from_errno2("fstat", abspath);
1136 goto done;
1139 if (!S_ISREG(sb->st_mode)) {
1140 *status = GOT_STATUS_OBSTRUCTED;
1141 goto done;
1144 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1145 *status = GOT_STATUS_DELETE;
1146 goto done;
1147 } else if (!got_fileindex_entry_has_blob(ie) &&
1148 staged_status != GOT_STATUS_ADD) {
1149 *status = GOT_STATUS_ADD;
1150 goto done;
1153 if (!stat_info_differs(ie, sb))
1154 goto done;
1156 if (staged_status == GOT_STATUS_MODIFY ||
1157 staged_status == GOT_STATUS_ADD)
1158 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1159 else
1160 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1162 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1163 if (err)
1164 goto done;
1166 f = fdopen(fd, "r");
1167 if (f == NULL) {
1168 err = got_error_from_errno2("fopen", abspath);
1169 goto done;
1171 fd = -1;
1172 hdrlen = got_object_blob_get_hdrlen(blob);
1173 for (;;) {
1174 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1175 err = got_object_blob_read_block(&blen, blob);
1176 if (err)
1177 goto done;
1178 /* Skip length of blob object header first time around. */
1179 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1180 if (flen == 0 && ferror(f)) {
1181 err = got_error_from_errno("fread");
1182 goto done;
1184 if (blen == 0) {
1185 if (flen != 0)
1186 *status = GOT_STATUS_MODIFY;
1187 break;
1188 } else if (flen == 0) {
1189 if (blen != 0)
1190 *status = GOT_STATUS_MODIFY;
1191 break;
1192 } else if (blen - hdrlen == flen) {
1193 /* Skip blob object header first time around. */
1194 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1195 *status = GOT_STATUS_MODIFY;
1196 break;
1198 } else {
1199 *status = GOT_STATUS_MODIFY;
1200 break;
1202 hdrlen = 0;
1205 if (*status == GOT_STATUS_MODIFY) {
1206 rewind(f);
1207 err = get_modified_file_content_status(status, f);
1208 } else if (xbit_differs(ie, sb->st_mode))
1209 *status = GOT_STATUS_MODE_CHANGE;
1210 done:
1211 if (blob)
1212 got_object_blob_close(blob);
1213 if (f != NULL && fclose(f) == EOF && err == NULL)
1214 err = got_error_from_errno2("fclose", abspath);
1215 if (fd != -1 && close(fd) == -1 && err == NULL)
1216 err = got_error_from_errno2("close", abspath);
1217 return err;
1221 * Update timestamps in the file index if a file is unmodified and
1222 * we had to run a full content comparison to find out.
1224 static const struct got_error *
1225 sync_timestamps(char *ondisk_path, unsigned char status,
1226 struct got_fileindex_entry *ie, struct stat *sb)
1228 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1229 return got_fileindex_entry_update(ie, ondisk_path,
1230 ie->blob_sha1, ie->commit_sha1, 1);
1232 return NULL;
1235 static const struct got_error *
1236 update_blob(struct got_worktree *worktree,
1237 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1238 struct got_tree_entry *te, const char *path,
1239 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1240 void *progress_arg)
1242 const struct got_error *err = NULL;
1243 struct got_blob_object *blob = NULL;
1244 char *ondisk_path;
1245 unsigned char status = GOT_STATUS_NO_CHANGE;
1246 struct stat sb;
1248 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1249 return got_error_from_errno("asprintf");
1251 if (ie) {
1252 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1253 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1254 goto done;
1256 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1257 if (err)
1258 goto done;
1259 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1260 sb.st_mode = got_fileindex_perms_to_st(ie);
1261 } else
1262 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1264 if (status == GOT_STATUS_OBSTRUCTED) {
1265 err = (*progress_cb)(progress_arg, status, path);
1266 goto done;
1269 if (ie && status != GOT_STATUS_MISSING &&
1270 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1271 if (got_fileindex_entry_has_commit(ie) &&
1272 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1273 SHA1_DIGEST_LENGTH) == 0) {
1274 err = sync_timestamps(ondisk_path, status, ie, &sb);
1275 if (err)
1276 goto done;
1277 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1278 path);
1279 goto done;
1281 if (got_fileindex_entry_has_blob(ie) &&
1282 memcmp(ie->blob_sha1, te->id.sha1,
1283 SHA1_DIGEST_LENGTH) == 0) {
1284 err = sync_timestamps(ondisk_path, status, ie, &sb);
1285 goto done;
1289 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1290 if (err)
1291 goto done;
1293 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1294 int update_timestamps;
1295 struct got_blob_object *blob2 = NULL;
1296 char *label_orig = NULL;
1297 if (got_fileindex_entry_has_blob(ie)) {
1298 struct got_object_id id2;
1299 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1300 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1301 if (err)
1302 goto done;
1304 if (got_fileindex_entry_has_commit(ie)) {
1305 char id_str[SHA1_DIGEST_STRING_LENGTH];
1306 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1307 sizeof(id_str)) == NULL) {
1308 err = got_error_path(id_str,
1309 GOT_ERR_BAD_OBJ_ID_STR);
1310 goto done;
1312 if (asprintf(&label_orig, "%s: commit %s",
1313 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1314 err = got_error_from_errno("asprintf");
1315 goto done;
1318 err = merge_blob(&update_timestamps, worktree, blob2,
1319 ondisk_path, path, sb.st_mode, label_orig, blob,
1320 worktree->base_commit_id, repo,
1321 progress_cb, progress_arg);
1322 free(label_orig);
1323 if (blob2)
1324 got_object_blob_close(blob2);
1325 if (err)
1326 goto done;
1328 * Do not update timestamps of files with local changes.
1329 * Otherwise, a future status walk would treat them as
1330 * unmodified files again.
1332 err = got_fileindex_entry_update(ie, ondisk_path,
1333 blob->id.sha1, worktree->base_commit_id->sha1,
1334 update_timestamps);
1335 } else if (status == GOT_STATUS_MODE_CHANGE) {
1336 err = got_fileindex_entry_update(ie, ondisk_path,
1337 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1338 } else if (status == GOT_STATUS_DELETE) {
1339 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1340 if (err)
1341 goto done;
1342 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1343 ondisk_path, path, blob, 0);
1344 if (err)
1345 goto done;
1346 } else {
1347 err = install_blob(worktree, ondisk_path, path, te->mode,
1348 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1349 repo, progress_cb, progress_arg);
1350 if (err)
1351 goto done;
1352 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1353 ondisk_path, path, blob, 1);
1354 if (err)
1355 goto done;
1357 got_object_blob_close(blob);
1358 done:
1359 free(ondisk_path);
1360 return err;
1363 static const struct got_error *
1364 remove_ondisk_file(const char *root_path, const char *path)
1366 const struct got_error *err = NULL;
1367 char *ondisk_path = NULL;
1369 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1370 return got_error_from_errno("asprintf");
1372 if (unlink(ondisk_path) == -1) {
1373 if (errno != ENOENT)
1374 err = got_error_from_errno2("unlink", ondisk_path);
1375 } else {
1376 char *parent = dirname(ondisk_path);
1377 while (parent && strcmp(parent, root_path) != 0) {
1378 if (rmdir(parent) == -1) {
1379 if (errno != ENOTEMPTY)
1380 err = got_error_from_errno2("rmdir",
1381 parent);
1382 break;
1384 parent = dirname(parent);
1387 free(ondisk_path);
1388 return err;
1391 static const struct got_error *
1392 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1393 struct got_fileindex_entry *ie, struct got_repository *repo,
1394 got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 const struct got_error *err = NULL;
1397 unsigned char status;
1398 struct stat sb;
1399 char *ondisk_path;
1401 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1402 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1404 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1405 == -1)
1406 return got_error_from_errno("asprintf");
1408 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1409 if (err)
1410 return err;
1412 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1413 status == GOT_STATUS_ADD) {
1414 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1415 if (err)
1416 return err;
1418 * Preserve the working file and change the deleted blob's
1419 * entry into a schedule-add entry.
1421 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1422 0);
1423 if (err)
1424 return err;
1425 } else {
1426 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1427 if (err)
1428 return err;
1429 if (status == GOT_STATUS_NO_CHANGE) {
1430 err = remove_ondisk_file(worktree->root_path, ie->path);
1431 if (err)
1432 return err;
1434 got_fileindex_entry_remove(fileindex, ie);
1437 return err;
1440 struct diff_cb_arg {
1441 struct got_fileindex *fileindex;
1442 struct got_worktree *worktree;
1443 struct got_repository *repo;
1444 got_worktree_checkout_cb progress_cb;
1445 void *progress_arg;
1446 got_cancel_cb cancel_cb;
1447 void *cancel_arg;
1450 static const struct got_error *
1451 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1452 struct got_tree_entry *te, const char *parent_path)
1454 struct diff_cb_arg *a = arg;
1456 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1457 return got_error(GOT_ERR_CANCELLED);
1459 return update_blob(a->worktree, a->fileindex, ie, te,
1460 ie->path, a->repo, a->progress_cb, a->progress_arg);
1463 static const struct got_error *
1464 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1466 struct diff_cb_arg *a = arg;
1468 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1469 return got_error(GOT_ERR_CANCELLED);
1471 return delete_blob(a->worktree, a->fileindex, ie,
1472 a->repo, a->progress_cb, a->progress_arg);
1475 static const struct got_error *
1476 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1478 struct diff_cb_arg *a = arg;
1479 const struct got_error *err;
1480 char *path;
1482 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1483 return got_error(GOT_ERR_CANCELLED);
1485 if (got_object_tree_entry_is_submodule(te))
1486 return NULL;
1488 if (asprintf(&path, "%s%s%s", parent_path,
1489 parent_path[0] ? "/" : "", te->name)
1490 == -1)
1491 return got_error_from_errno("asprintf");
1493 if (S_ISDIR(te->mode))
1494 err = add_dir_on_disk(a->worktree, path);
1495 else
1496 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1497 a->repo, a->progress_cb, a->progress_arg);
1499 free(path);
1500 return err;
1503 static const struct got_error *
1504 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1506 const struct got_error *err = NULL;
1507 char *uuidstr = NULL;
1508 uint32_t uuid_status;
1510 *refname = NULL;
1512 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1513 if (uuid_status != uuid_s_ok)
1514 return got_error_uuid(uuid_status, "uuid_to_string");
1516 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1517 == -1) {
1518 err = got_error_from_errno("asprintf");
1519 *refname = NULL;
1521 free(uuidstr);
1522 return err;
1525 const struct got_error *
1526 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1528 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1531 static const struct got_error *
1532 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1534 return get_ref_name(refname, worktree,
1535 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1538 static const struct got_error *
1539 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1541 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1544 static const struct got_error *
1545 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1547 return get_ref_name(refname, worktree,
1548 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1551 static const struct got_error *
1552 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1554 return get_ref_name(refname, worktree,
1555 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1558 static const struct got_error *
1559 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1561 return get_ref_name(refname, worktree,
1562 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1565 static const struct got_error *
1566 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1568 return get_ref_name(refname, worktree,
1569 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1572 static const struct got_error *
1573 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1575 return get_ref_name(refname, worktree,
1576 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1579 static const struct got_error *
1580 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1582 return get_ref_name(refname, worktree,
1583 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1586 const struct got_error *
1587 got_worktree_get_histedit_script_path(char **path,
1588 struct got_worktree *worktree)
1590 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1591 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1592 *path = NULL;
1593 return got_error_from_errno("asprintf");
1595 return NULL;
1599 * Prevent Git's garbage collector from deleting our base commit by
1600 * setting a reference to our base commit's ID.
1602 static const struct got_error *
1603 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1605 const struct got_error *err = NULL;
1606 struct got_reference *ref = NULL;
1607 char *refname;
1609 err = got_worktree_get_base_ref_name(&refname, worktree);
1610 if (err)
1611 return err;
1613 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1614 if (err)
1615 goto done;
1617 err = got_ref_write(ref, repo);
1618 done:
1619 free(refname);
1620 if (ref)
1621 got_ref_close(ref);
1622 return err;
1625 static const struct got_error *
1626 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1628 const struct got_error *err = NULL;
1630 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1631 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1632 err = got_error_from_errno("asprintf");
1633 *fileindex_path = NULL;
1635 return err;
1639 static const struct got_error *
1640 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1641 struct got_worktree *worktree)
1643 const struct got_error *err = NULL;
1644 FILE *index = NULL;
1646 *fileindex_path = NULL;
1647 *fileindex = got_fileindex_alloc();
1648 if (*fileindex == NULL)
1649 return got_error_from_errno("got_fileindex_alloc");
1651 err = get_fileindex_path(fileindex_path, worktree);
1652 if (err)
1653 goto done;
1655 index = fopen(*fileindex_path, "rb");
1656 if (index == NULL) {
1657 if (errno != ENOENT)
1658 err = got_error_from_errno2("fopen", *fileindex_path);
1659 } else {
1660 err = got_fileindex_read(*fileindex, index);
1661 if (fclose(index) != 0 && err == NULL)
1662 err = got_error_from_errno("fclose");
1664 done:
1665 if (err) {
1666 free(*fileindex_path);
1667 *fileindex_path = NULL;
1668 got_fileindex_free(*fileindex);
1669 *fileindex = NULL;
1671 return err;
1674 struct bump_base_commit_id_arg {
1675 struct got_object_id *base_commit_id;
1676 const char *path;
1677 size_t path_len;
1678 const char *entry_name;
1679 got_worktree_checkout_cb progress_cb;
1680 void *progress_arg;
1683 /* Bump base commit ID of all files within an updated part of the work tree. */
1684 static const struct got_error *
1685 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1687 const struct got_error *err;
1688 struct bump_base_commit_id_arg *a = arg;
1690 if (a->entry_name) {
1691 if (strcmp(ie->path, a->path) != 0)
1692 return NULL;
1693 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1694 return NULL;
1696 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1697 SHA1_DIGEST_LENGTH) == 0)
1698 return NULL;
1700 if (a->progress_cb) {
1701 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1702 ie->path);
1703 if (err)
1704 return err;
1706 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1707 return NULL;
1710 static const struct got_error *
1711 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1713 const struct got_error *err = NULL;
1714 char *new_fileindex_path = NULL;
1715 FILE *new_index = NULL;
1717 err = got_opentemp_named(&new_fileindex_path, &new_index,
1718 fileindex_path);
1719 if (err)
1720 goto done;
1722 err = got_fileindex_write(fileindex, new_index);
1723 if (err)
1724 goto done;
1726 if (rename(new_fileindex_path, fileindex_path) != 0) {
1727 err = got_error_from_errno3("rename", new_fileindex_path,
1728 fileindex_path);
1729 unlink(new_fileindex_path);
1731 done:
1732 if (new_index)
1733 fclose(new_index);
1734 free(new_fileindex_path);
1735 return err;
1738 static const struct got_error *
1739 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1740 struct got_object_id **tree_id, const char *wt_relpath,
1741 struct got_worktree *worktree, struct got_repository *repo)
1743 const struct got_error *err = NULL;
1744 struct got_object_id *id = NULL;
1745 char *in_repo_path = NULL;
1746 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1748 *entry_type = GOT_OBJ_TYPE_ANY;
1749 *tree_relpath = NULL;
1750 *tree_id = NULL;
1752 if (wt_relpath[0] == '\0') {
1753 /* Check out all files within the work tree. */
1754 *entry_type = GOT_OBJ_TYPE_TREE;
1755 *tree_relpath = strdup("");
1756 if (*tree_relpath == NULL) {
1757 err = got_error_from_errno("strdup");
1758 goto done;
1760 err = got_object_id_by_path(tree_id, repo,
1761 worktree->base_commit_id, worktree->path_prefix);
1762 if (err)
1763 goto done;
1764 return NULL;
1767 /* Check out a subset of files in the work tree. */
1769 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1770 is_root_wt ? "" : "/", wt_relpath) == -1) {
1771 err = got_error_from_errno("asprintf");
1772 goto done;
1775 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1776 in_repo_path);
1777 if (err)
1778 goto done;
1780 free(in_repo_path);
1781 in_repo_path = NULL;
1783 err = got_object_get_type(entry_type, repo, id);
1784 if (err)
1785 goto done;
1787 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1788 /* Check out a single file. */
1789 if (strchr(wt_relpath, '/') == NULL) {
1790 /* Check out a single file in work tree's root dir. */
1791 in_repo_path = strdup(worktree->path_prefix);
1792 if (in_repo_path == NULL) {
1793 err = got_error_from_errno("strdup");
1794 goto done;
1796 *tree_relpath = strdup("");
1797 if (*tree_relpath == NULL) {
1798 err = got_error_from_errno("strdup");
1799 goto done;
1801 } else {
1802 /* Check out a single file in a subdirectory. */
1803 err = got_path_dirname(tree_relpath, wt_relpath);
1804 if (err)
1805 return err;
1806 if (asprintf(&in_repo_path, "%s%s%s",
1807 worktree->path_prefix, is_root_wt ? "" : "/",
1808 *tree_relpath) == -1) {
1809 err = got_error_from_errno("asprintf");
1810 goto done;
1813 err = got_object_id_by_path(tree_id, repo,
1814 worktree->base_commit_id, in_repo_path);
1815 } else {
1816 /* Check out all files within a subdirectory. */
1817 *tree_id = got_object_id_dup(id);
1818 if (*tree_id == NULL) {
1819 err = got_error_from_errno("got_object_id_dup");
1820 goto done;
1822 *tree_relpath = strdup(wt_relpath);
1823 if (*tree_relpath == NULL) {
1824 err = got_error_from_errno("strdup");
1825 goto done;
1828 done:
1829 free(id);
1830 free(in_repo_path);
1831 if (err) {
1832 *entry_type = GOT_OBJ_TYPE_ANY;
1833 free(*tree_relpath);
1834 *tree_relpath = NULL;
1835 free(*tree_id);
1836 *tree_id = NULL;
1838 return err;
1841 static const struct got_error *
1842 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1843 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1844 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1845 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1847 const struct got_error *err = NULL;
1848 struct got_commit_object *commit = NULL;
1849 struct got_tree_object *tree = NULL;
1850 struct got_fileindex_diff_tree_cb diff_cb;
1851 struct diff_cb_arg arg;
1853 err = ref_base_commit(worktree, repo);
1854 if (err)
1855 goto done;
1857 err = got_object_open_as_commit(&commit, repo,
1858 worktree->base_commit_id);
1859 if (err)
1860 goto done;
1862 err = got_object_open_as_tree(&tree, repo, tree_id);
1863 if (err)
1864 goto done;
1866 if (entry_name &&
1867 got_object_tree_find_entry(tree, entry_name) == NULL) {
1868 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1869 goto done;
1872 diff_cb.diff_old_new = diff_old_new;
1873 diff_cb.diff_old = diff_old;
1874 diff_cb.diff_new = diff_new;
1875 arg.fileindex = fileindex;
1876 arg.worktree = worktree;
1877 arg.repo = repo;
1878 arg.progress_cb = progress_cb;
1879 arg.progress_arg = progress_arg;
1880 arg.cancel_cb = cancel_cb;
1881 arg.cancel_arg = cancel_arg;
1882 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1883 entry_name, repo, &diff_cb, &arg);
1884 done:
1885 if (tree)
1886 got_object_tree_close(tree);
1887 if (commit)
1888 got_object_commit_close(commit);
1889 return err;
1892 const struct got_error *
1893 got_worktree_checkout_files(struct got_worktree *worktree,
1894 struct got_pathlist_head *paths, struct got_repository *repo,
1895 got_worktree_checkout_cb progress_cb, void *progress_arg,
1896 got_cancel_cb cancel_cb, void *cancel_arg)
1898 const struct got_error *err = NULL, *sync_err, *unlockerr;
1899 struct got_commit_object *commit = NULL;
1900 struct got_tree_object *tree = NULL;
1901 struct got_fileindex *fileindex = NULL;
1902 char *fileindex_path = NULL;
1903 struct got_pathlist_entry *pe;
1904 struct tree_path_data {
1905 SIMPLEQ_ENTRY(tree_path_data) entry;
1906 struct got_object_id *tree_id;
1907 int entry_type;
1908 char *relpath;
1909 char *entry_name;
1910 } *tpd = NULL;
1911 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1913 SIMPLEQ_INIT(&tree_paths);
1915 err = lock_worktree(worktree, LOCK_EX);
1916 if (err)
1917 return err;
1919 /* Map all specified paths to in-repository trees. */
1920 TAILQ_FOREACH(pe, paths, entry) {
1921 tpd = malloc(sizeof(*tpd));
1922 if (tpd == NULL) {
1923 err = got_error_from_errno("malloc");
1924 goto done;
1927 err = find_tree_entry_for_checkout(&tpd->entry_type,
1928 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1929 if (err) {
1930 free(tpd);
1931 goto done;
1934 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1935 err = got_path_basename(&tpd->entry_name, pe->path);
1936 if (err) {
1937 free(tpd->relpath);
1938 free(tpd->tree_id);
1939 free(tpd);
1940 goto done;
1942 } else
1943 tpd->entry_name = NULL;
1945 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1949 * Read the file index.
1950 * Checking out files is supposed to be an idempotent operation.
1951 * If the on-disk file index is incomplete we will try to complete it.
1953 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1954 if (err)
1955 goto done;
1957 tpd = SIMPLEQ_FIRST(&tree_paths);
1958 TAILQ_FOREACH(pe, paths, entry) {
1959 struct bump_base_commit_id_arg bbc_arg;
1961 err = checkout_files(worktree, fileindex, tpd->relpath,
1962 tpd->tree_id, tpd->entry_name, repo,
1963 progress_cb, progress_arg, cancel_cb, cancel_arg);
1964 if (err)
1965 break;
1967 bbc_arg.base_commit_id = worktree->base_commit_id;
1968 bbc_arg.entry_name = tpd->entry_name;
1969 bbc_arg.path = pe->path;
1970 bbc_arg.path_len = pe->path_len;
1971 bbc_arg.progress_cb = progress_cb;
1972 bbc_arg.progress_arg = progress_arg;
1973 err = got_fileindex_for_each_entry_safe(fileindex,
1974 bump_base_commit_id, &bbc_arg);
1975 if (err)
1976 break;
1978 tpd = SIMPLEQ_NEXT(tpd, entry);
1980 sync_err = sync_fileindex(fileindex, fileindex_path);
1981 if (sync_err && err == NULL)
1982 err = sync_err;
1983 done:
1984 free(fileindex_path);
1985 if (tree)
1986 got_object_tree_close(tree);
1987 if (commit)
1988 got_object_commit_close(commit);
1989 if (fileindex)
1990 got_fileindex_free(fileindex);
1991 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1992 tpd = SIMPLEQ_FIRST(&tree_paths);
1993 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1994 free(tpd->relpath);
1995 free(tpd->tree_id);
1996 free(tpd);
1998 unlockerr = lock_worktree(worktree, LOCK_SH);
1999 if (unlockerr && err == NULL)
2000 err = unlockerr;
2001 return err;
2004 struct merge_file_cb_arg {
2005 struct got_worktree *worktree;
2006 struct got_fileindex *fileindex;
2007 got_worktree_checkout_cb progress_cb;
2008 void *progress_arg;
2009 got_cancel_cb cancel_cb;
2010 void *cancel_arg;
2011 const char *label_orig;
2012 struct got_object_id *commit_id2;
2015 static const struct got_error *
2016 merge_file_cb(void *arg, struct got_blob_object *blob1,
2017 struct got_blob_object *blob2, struct got_object_id *id1,
2018 struct got_object_id *id2, const char *path1, const char *path2,
2019 mode_t mode1, mode_t mode2, struct got_repository *repo)
2021 static const struct got_error *err = NULL;
2022 struct merge_file_cb_arg *a = arg;
2023 struct got_fileindex_entry *ie;
2024 char *ondisk_path = NULL;
2025 struct stat sb;
2026 unsigned char status;
2027 int local_changes_subsumed;
2029 if (blob1 && blob2) {
2030 ie = got_fileindex_entry_get(a->fileindex, path2,
2031 strlen(path2));
2032 if (ie == NULL)
2033 return (*a->progress_cb)(a->progress_arg,
2034 GOT_STATUS_MISSING, path2);
2036 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2037 path2) == -1)
2038 return got_error_from_errno("asprintf");
2040 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2041 if (err)
2042 goto done;
2044 if (status == GOT_STATUS_DELETE) {
2045 err = (*a->progress_cb)(a->progress_arg,
2046 GOT_STATUS_MERGE, path2);
2047 goto done;
2049 if (status != GOT_STATUS_NO_CHANGE &&
2050 status != GOT_STATUS_MODIFY &&
2051 status != GOT_STATUS_CONFLICT &&
2052 status != GOT_STATUS_ADD) {
2053 err = (*a->progress_cb)(a->progress_arg, status, path2);
2054 goto done;
2057 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2058 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2059 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2060 } else if (blob1) {
2061 ie = got_fileindex_entry_get(a->fileindex, path1,
2062 strlen(path1));
2063 if (ie == NULL)
2064 return (*a->progress_cb)(a->progress_arg,
2065 GOT_STATUS_MISSING, path2);
2067 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2068 path1) == -1)
2069 return got_error_from_errno("asprintf");
2071 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2072 if (err)
2073 goto done;
2075 switch (status) {
2076 case GOT_STATUS_NO_CHANGE:
2077 err = (*a->progress_cb)(a->progress_arg,
2078 GOT_STATUS_DELETE, path1);
2079 if (err)
2080 goto done;
2081 err = remove_ondisk_file(a->worktree->root_path, path1);
2082 if (err)
2083 goto done;
2084 if (ie)
2085 got_fileindex_entry_mark_deleted_from_disk(ie);
2086 break;
2087 case GOT_STATUS_DELETE:
2088 case GOT_STATUS_MISSING:
2089 err = (*a->progress_cb)(a->progress_arg,
2090 GOT_STATUS_DELETE, path1);
2091 if (err)
2092 goto done;
2093 if (ie)
2094 got_fileindex_entry_mark_deleted_from_disk(ie);
2095 break;
2096 case GOT_STATUS_ADD:
2097 case GOT_STATUS_MODIFY:
2098 case GOT_STATUS_CONFLICT:
2099 err = (*a->progress_cb)(a->progress_arg,
2100 GOT_STATUS_CANNOT_DELETE, path1);
2101 if (err)
2102 goto done;
2103 break;
2104 case GOT_STATUS_OBSTRUCTED:
2105 err = (*a->progress_cb)(a->progress_arg, status, path1);
2106 if (err)
2107 goto done;
2108 break;
2109 default:
2110 break;
2112 } else if (blob2) {
2113 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2114 path2) == -1)
2115 return got_error_from_errno("asprintf");
2116 ie = got_fileindex_entry_get(a->fileindex, path2,
2117 strlen(path2));
2118 if (ie) {
2119 err = get_file_status(&status, &sb, ie, ondisk_path,
2120 repo);
2121 if (err)
2122 goto done;
2123 if (status != GOT_STATUS_NO_CHANGE &&
2124 status != GOT_STATUS_MODIFY &&
2125 status != GOT_STATUS_CONFLICT &&
2126 status != GOT_STATUS_ADD) {
2127 err = (*a->progress_cb)(a->progress_arg,
2128 status, path2);
2129 goto done;
2131 err = merge_blob(&local_changes_subsumed, a->worktree,
2132 NULL, ondisk_path, path2, sb.st_mode,
2133 a->label_orig, blob2, a->commit_id2, repo,
2134 a->progress_cb,
2135 a->progress_arg);
2136 if (status == GOT_STATUS_DELETE) {
2137 err = update_blob_fileindex_entry(a->worktree,
2138 a->fileindex, ie, ondisk_path, ie->path,
2139 blob2, 0);
2140 if (err)
2141 goto done;
2143 } else {
2144 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2145 err = install_blob(a->worktree, ondisk_path, path2,
2146 /* XXX get this from parent tree! */
2147 GOT_DEFAULT_FILE_MODE,
2148 sb.st_mode, blob2, 0, 0, repo,
2149 a->progress_cb, a->progress_arg);
2150 if (err)
2151 goto done;
2152 err = got_fileindex_entry_alloc(&ie,
2153 ondisk_path, path2, NULL, NULL);
2154 if (err)
2155 goto done;
2156 err = got_fileindex_entry_add(a->fileindex, ie);
2157 if (err) {
2158 got_fileindex_entry_free(ie);
2159 goto done;
2163 done:
2164 free(ondisk_path);
2165 return err;
2168 struct check_merge_ok_arg {
2169 struct got_worktree *worktree;
2170 struct got_repository *repo;
2173 static const struct got_error *
2174 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2176 const struct got_error *err = NULL;
2177 struct check_merge_ok_arg *a = arg;
2178 unsigned char status;
2179 struct stat sb;
2180 char *ondisk_path;
2182 /* Reject merges into a work tree with mixed base commits. */
2183 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2184 SHA1_DIGEST_LENGTH))
2185 return got_error(GOT_ERR_MIXED_COMMITS);
2187 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2188 == -1)
2189 return got_error_from_errno("asprintf");
2191 /* Reject merges into a work tree with conflicted files. */
2192 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2193 if (err)
2194 return err;
2195 if (status == GOT_STATUS_CONFLICT)
2196 return got_error(GOT_ERR_CONFLICTS);
2198 return NULL;
2201 static const struct got_error *
2202 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2203 const char *fileindex_path, struct got_object_id *commit_id1,
2204 struct got_object_id *commit_id2, struct got_repository *repo,
2205 got_worktree_checkout_cb progress_cb, void *progress_arg,
2206 got_cancel_cb cancel_cb, void *cancel_arg)
2208 const struct got_error *err = NULL, *sync_err;
2209 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2210 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2211 struct merge_file_cb_arg arg;
2212 char *label_orig = NULL;
2214 if (commit_id1) {
2215 char *id_str;
2217 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2218 worktree->path_prefix);
2219 if (err)
2220 goto done;
2222 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2223 if (err)
2224 goto done;
2226 err = got_object_id_str(&id_str, commit_id1);
2227 if (err)
2228 goto done;
2230 if (asprintf(&label_orig, "%s: commit %s",
2231 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2232 err = got_error_from_errno("asprintf");
2233 free(id_str);
2234 goto done;
2236 free(id_str);
2239 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2240 worktree->path_prefix);
2241 if (err)
2242 goto done;
2244 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2245 if (err)
2246 goto done;
2248 arg.worktree = worktree;
2249 arg.fileindex = fileindex;
2250 arg.progress_cb = progress_cb;
2251 arg.progress_arg = progress_arg;
2252 arg.cancel_cb = cancel_cb;
2253 arg.cancel_arg = cancel_arg;
2254 arg.label_orig = label_orig;
2255 arg.commit_id2 = commit_id2;
2256 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2257 sync_err = sync_fileindex(fileindex, fileindex_path);
2258 if (sync_err && err == NULL)
2259 err = sync_err;
2260 done:
2261 if (tree1)
2262 got_object_tree_close(tree1);
2263 if (tree2)
2264 got_object_tree_close(tree2);
2265 free(label_orig);
2266 return err;
2269 const struct got_error *
2270 got_worktree_merge_files(struct got_worktree *worktree,
2271 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2272 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2273 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2275 const struct got_error *err, *unlockerr;
2276 char *fileindex_path = NULL;
2277 struct got_fileindex *fileindex = NULL;
2278 struct check_merge_ok_arg mok_arg;
2280 err = lock_worktree(worktree, LOCK_EX);
2281 if (err)
2282 return err;
2284 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2285 if (err)
2286 goto done;
2288 mok_arg.worktree = worktree;
2289 mok_arg.repo = repo;
2290 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2291 &mok_arg);
2292 if (err)
2293 goto done;
2295 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2296 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2297 done:
2298 if (fileindex)
2299 got_fileindex_free(fileindex);
2300 free(fileindex_path);
2301 unlockerr = lock_worktree(worktree, LOCK_SH);
2302 if (unlockerr && err == NULL)
2303 err = unlockerr;
2304 return err;
2307 struct diff_dir_cb_arg {
2308 struct got_fileindex *fileindex;
2309 struct got_worktree *worktree;
2310 const char *status_path;
2311 size_t status_path_len;
2312 struct got_repository *repo;
2313 got_worktree_status_cb status_cb;
2314 void *status_arg;
2315 got_cancel_cb cancel_cb;
2316 void *cancel_arg;
2317 /* A pathlist containing per-directory pathlists of ignore patterns. */
2318 struct got_pathlist_head ignores;
2319 int report_unchanged;
2322 static const struct got_error *
2323 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2324 got_worktree_status_cb status_cb, void *status_arg,
2325 struct got_repository *repo, int report_unchanged)
2327 const struct got_error *err = NULL;
2328 unsigned char status = GOT_STATUS_NO_CHANGE;
2329 unsigned char staged_status = get_staged_status(ie);
2330 struct stat sb;
2331 struct got_object_id blob_id, commit_id, staged_blob_id;
2332 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2333 struct got_object_id *staged_blob_idp = NULL;
2335 err = get_file_status(&status, &sb, ie, abspath, repo);
2336 if (err)
2337 return err;
2339 if (status == GOT_STATUS_NO_CHANGE &&
2340 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2341 return NULL;
2343 if (got_fileindex_entry_has_blob(ie)) {
2344 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2345 blob_idp = &blob_id;
2347 if (got_fileindex_entry_has_commit(ie)) {
2348 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2349 commit_idp = &commit_id;
2351 if (staged_status == GOT_STATUS_ADD ||
2352 staged_status == GOT_STATUS_MODIFY) {
2353 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2354 SHA1_DIGEST_LENGTH);
2355 staged_blob_idp = &staged_blob_id;
2358 return (*status_cb)(status_arg, status, staged_status,
2359 ie->path, blob_idp, staged_blob_idp, commit_idp);
2362 static const struct got_error *
2363 status_old_new(void *arg, struct got_fileindex_entry *ie,
2364 struct dirent *de, const char *parent_path)
2366 const struct got_error *err = NULL;
2367 struct diff_dir_cb_arg *a = arg;
2368 char *abspath;
2370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2371 return got_error(GOT_ERR_CANCELLED);
2373 if (got_path_cmp(parent_path, a->status_path,
2374 strlen(parent_path), a->status_path_len) != 0 &&
2375 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2376 return NULL;
2378 if (parent_path[0]) {
2379 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2380 parent_path, de->d_name) == -1)
2381 return got_error_from_errno("asprintf");
2382 } else {
2383 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2384 de->d_name) == -1)
2385 return got_error_from_errno("asprintf");
2388 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2389 a->repo, a->report_unchanged);
2390 free(abspath);
2391 return err;
2394 static const struct got_error *
2395 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2397 struct diff_dir_cb_arg *a = arg;
2398 struct got_object_id blob_id, commit_id;
2399 unsigned char status;
2401 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2402 return got_error(GOT_ERR_CANCELLED);
2404 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2405 return NULL;
2407 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2408 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2409 if (got_fileindex_entry_has_file_on_disk(ie))
2410 status = GOT_STATUS_MISSING;
2411 else
2412 status = GOT_STATUS_DELETE;
2413 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2414 ie->path, &blob_id, NULL, &commit_id);
2417 void
2418 free_ignorelist(struct got_pathlist_head *ignorelist)
2420 struct got_pathlist_entry *pe;
2422 TAILQ_FOREACH(pe, ignorelist, entry)
2423 free((char *)pe->path);
2424 got_pathlist_free(ignorelist);
2427 void
2428 free_ignores(struct got_pathlist_head *ignores)
2430 struct got_pathlist_entry *pe;
2432 TAILQ_FOREACH(pe, ignores, entry) {
2433 struct got_pathlist_head *ignorelist = pe->data;
2434 free_ignorelist(ignorelist);
2435 free((char *)pe->path);
2437 got_pathlist_free(ignores);
2440 static const struct got_error *
2441 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2443 const struct got_error *err = NULL;
2444 struct got_pathlist_entry *pe = NULL;
2445 struct got_pathlist_head *ignorelist;
2446 char *line = NULL, *pattern, *dirpath = NULL;
2447 size_t linesize = 0;
2448 ssize_t linelen;
2450 ignorelist = calloc(1, sizeof(*ignorelist));
2451 if (ignorelist == NULL)
2452 return got_error_from_errno("calloc");
2453 TAILQ_INIT(ignorelist);
2455 while ((linelen = getline(&line, &linesize, f)) != -1) {
2456 if (linelen > 0 && line[linelen - 1] == '\n')
2457 line[linelen - 1] = '\0';
2459 /* Git's ignores may contain comments. */
2460 if (line[0] == '#')
2461 continue;
2463 /* Git's negated patterns are not (yet?) supported. */
2464 if (line[0] == '!')
2465 continue;
2467 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2468 line) == -1) {
2469 err = got_error_from_errno("asprintf");
2470 goto done;
2472 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2473 if (err)
2474 goto done;
2476 if (ferror(f)) {
2477 err = got_error_from_errno("getline");
2478 goto done;
2481 dirpath = strdup(path);
2482 if (dirpath == NULL) {
2483 err = got_error_from_errno("strdup");
2484 goto done;
2486 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2487 done:
2488 free(line);
2489 if (err || pe == NULL) {
2490 free(dirpath);
2491 free_ignorelist(ignorelist);
2493 return err;
2496 int
2497 match_ignores(struct got_pathlist_head *ignores, const char *path)
2499 struct got_pathlist_entry *pe;
2501 /* Handle patterns which match in all directories. */
2502 TAILQ_FOREACH(pe, ignores, entry) {
2503 struct got_pathlist_head *ignorelist = pe->data;
2504 struct got_pathlist_entry *pi;
2506 TAILQ_FOREACH(pi, ignorelist, entry) {
2507 const char *p, *pattern = pi->path;
2509 if (strncmp(pattern, "**/", 3) != 0)
2510 continue;
2511 pattern += 3;
2512 p = path;
2513 while (*p) {
2514 if (fnmatch(pattern, p,
2515 FNM_PATHNAME | FNM_LEADING_DIR)) {
2516 /* Retry in next directory. */
2517 while (*p && *p != '/')
2518 p++;
2519 while (*p == '/')
2520 p++;
2521 continue;
2523 return 1;
2529 * The ignores pathlist contains ignore lists from children before
2530 * parents, so we can find the most specific ignorelist by walking
2531 * ignores backwards.
2533 pe = TAILQ_LAST(ignores, got_pathlist_head);
2534 while (pe) {
2535 if (got_path_is_child(path, pe->path, pe->path_len)) {
2536 struct got_pathlist_head *ignorelist = pe->data;
2537 struct got_pathlist_entry *pi;
2538 TAILQ_FOREACH(pi, ignorelist, entry) {
2539 const char *pattern = pi->path;
2540 int flags = FNM_LEADING_DIR;
2541 if (strstr(pattern, "/**/") == NULL)
2542 flags |= FNM_PATHNAME;
2543 if (fnmatch(pattern, path, flags))
2544 continue;
2545 return 1;
2548 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2551 return 0;
2554 static const struct got_error *
2555 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2556 const char *path, const char *ignores_filename)
2558 const struct got_error *err = NULL;
2559 char *ignorespath;
2560 FILE *ignoresfile = NULL;
2562 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2563 path[0] ? "/" : "", ignores_filename) == -1)
2564 return got_error_from_errno("asprintf");
2566 ignoresfile = fopen(ignorespath, "r");
2567 if (ignoresfile == NULL) {
2568 if (errno != ENOENT && errno != EACCES)
2569 err = got_error_from_errno2("fopen",
2570 ignorespath);
2571 } else
2572 err = read_ignores(ignores, path, ignoresfile);
2574 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2575 err = got_error_from_errno2("fclose", path);
2576 free(ignorespath);
2577 return err;
2580 static const struct got_error *
2581 status_new(void *arg, struct dirent *de, const char *parent_path)
2583 const struct got_error *err = NULL;
2584 struct diff_dir_cb_arg *a = arg;
2585 char *path = NULL;
2587 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2588 return got_error(GOT_ERR_CANCELLED);
2590 /* XXX ignore symlinks for now */
2591 if (de->d_type == DT_LNK)
2592 return NULL;
2594 if (parent_path[0]) {
2595 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2596 return got_error_from_errno("asprintf");
2597 } else {
2598 path = de->d_name;
2601 if (de->d_type == DT_DIR) {
2602 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2603 ".cvsignore");
2604 if (err == NULL)
2605 err = add_ignores(&a->ignores, a->worktree->root_path,
2606 path, ".gitignore");
2608 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2609 && !match_ignores(&a->ignores, path))
2610 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2611 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2612 if (parent_path[0])
2613 free(path);
2614 return err;
2617 static const struct got_error *
2618 report_single_file_status(const char *path, const char *ondisk_path,
2619 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2620 void *status_arg, struct got_repository *repo, int report_unchanged)
2622 struct got_fileindex_entry *ie;
2623 struct stat sb;
2625 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2626 if (ie)
2627 return report_file_status(ie, ondisk_path, status_cb,
2628 status_arg, repo, report_unchanged);
2630 if (lstat(ondisk_path, &sb) == -1) {
2631 if (errno != ENOENT)
2632 return got_error_from_errno2("lstat", ondisk_path);
2633 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2634 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2635 return NULL;
2638 if (S_ISREG(sb.st_mode))
2639 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2640 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2642 return NULL;
2645 static const struct got_error *
2646 worktree_status(struct got_worktree *worktree, const char *path,
2647 struct got_fileindex *fileindex, struct got_repository *repo,
2648 got_worktree_status_cb status_cb, void *status_arg,
2649 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2650 int report_unchanged)
2652 const struct got_error *err = NULL;
2653 DIR *workdir = NULL;
2654 struct got_fileindex_diff_dir_cb fdiff_cb;
2655 struct diff_dir_cb_arg arg;
2656 char *ondisk_path = NULL;
2658 if (asprintf(&ondisk_path, "%s%s%s",
2659 worktree->root_path, path[0] ? "/" : "", path) == -1)
2660 return got_error_from_errno("asprintf");
2662 workdir = opendir(ondisk_path);
2663 if (workdir == NULL) {
2664 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2665 err = got_error_from_errno2("opendir", ondisk_path);
2666 else
2667 err = report_single_file_status(path, ondisk_path,
2668 fileindex, status_cb, status_arg, repo,
2669 report_unchanged);
2670 } else {
2671 fdiff_cb.diff_old_new = status_old_new;
2672 fdiff_cb.diff_old = status_old;
2673 fdiff_cb.diff_new = status_new;
2674 arg.fileindex = fileindex;
2675 arg.worktree = worktree;
2676 arg.status_path = path;
2677 arg.status_path_len = strlen(path);
2678 arg.repo = repo;
2679 arg.status_cb = status_cb;
2680 arg.status_arg = status_arg;
2681 arg.cancel_cb = cancel_cb;
2682 arg.cancel_arg = cancel_arg;
2683 arg.report_unchanged = report_unchanged;
2684 TAILQ_INIT(&arg.ignores);
2685 if (!no_ignores) {
2686 err = add_ignores(&arg.ignores, worktree->root_path,
2687 path, ".cvsignore");
2688 if (err == NULL)
2689 err = add_ignores(&arg.ignores,
2690 worktree->root_path, path, ".gitignore");
2692 if (err == NULL)
2693 err = got_fileindex_diff_dir(fileindex, workdir,
2694 worktree->root_path, path, repo, &fdiff_cb, &arg);
2695 free_ignores(&arg.ignores);
2698 if (workdir)
2699 closedir(workdir);
2700 free(ondisk_path);
2701 return err;
2704 const struct got_error *
2705 got_worktree_status(struct got_worktree *worktree,
2706 struct got_pathlist_head *paths, struct got_repository *repo,
2707 got_worktree_status_cb status_cb, void *status_arg,
2708 got_cancel_cb cancel_cb, void *cancel_arg)
2710 const struct got_error *err = NULL;
2711 char *fileindex_path = NULL;
2712 struct got_fileindex *fileindex = NULL;
2713 struct got_pathlist_entry *pe;
2715 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2716 if (err)
2717 return err;
2719 TAILQ_FOREACH(pe, paths, entry) {
2720 err = worktree_status(worktree, pe->path, fileindex, repo,
2721 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2722 if (err)
2723 break;
2725 free(fileindex_path);
2726 got_fileindex_free(fileindex);
2727 return err;
2730 const struct got_error *
2731 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2732 const char *arg)
2734 const struct got_error *err = NULL;
2735 char *resolved, *cwd = NULL, *path = NULL;
2736 size_t len;
2738 *wt_path = NULL;
2740 resolved = realpath(arg, NULL);
2741 if (resolved == NULL) {
2742 if (errno != ENOENT)
2743 return got_error_from_errno2("realpath", arg);
2744 cwd = getcwd(NULL, 0);
2745 if (cwd == NULL)
2746 return got_error_from_errno("getcwd");
2747 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2748 err = got_error_from_errno("asprintf");
2749 goto done;
2753 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2754 strlen(got_worktree_get_root_path(worktree)))) {
2755 err = got_error(GOT_ERR_BAD_PATH);
2756 goto done;
2759 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2760 err = got_path_skip_common_ancestor(&path,
2761 got_worktree_get_root_path(worktree), resolved);
2762 if (err)
2763 goto done;
2764 } else {
2765 path = strdup("");
2766 if (path == NULL) {
2767 err = got_error_from_errno("strdup");
2768 goto done;
2772 /* XXX status walk can't deal with trailing slash! */
2773 len = strlen(path);
2774 while (len > 0 && path[len - 1] == '/') {
2775 path[len - 1] = '\0';
2776 len--;
2778 done:
2779 free(resolved);
2780 free(cwd);
2781 if (err == NULL)
2782 *wt_path = path;
2783 else
2784 free(path);
2785 return err;
2788 struct schedule_addition_args {
2789 struct got_worktree *worktree;
2790 struct got_fileindex *fileindex;
2791 got_worktree_checkout_cb progress_cb;
2792 void *progress_arg;
2793 struct got_repository *repo;
2796 static const struct got_error *
2797 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2798 const char *relpath, struct got_object_id *blob_id,
2799 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2801 struct schedule_addition_args *a = arg;
2802 const struct got_error *err = NULL;
2803 struct got_fileindex_entry *ie;
2804 struct stat sb;
2805 char *ondisk_path;
2807 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2808 relpath) == -1)
2809 return got_error_from_errno("asprintf");
2811 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2812 if (ie) {
2813 err = get_file_status(&status, &sb, ie, ondisk_path,
2814 a->repo);
2815 if (err)
2816 goto done;
2817 /* Re-adding an existing entry is a no-op. */
2818 if (status == GOT_STATUS_ADD)
2819 goto done;
2820 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2821 if (err)
2822 goto done;
2825 if (status != GOT_STATUS_UNVERSIONED) {
2826 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2827 goto done;
2830 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2831 if (err)
2832 goto done;
2834 err = got_fileindex_entry_add(a->fileindex, ie);
2835 if (err) {
2836 got_fileindex_entry_free(ie);
2837 goto done;
2839 done:
2840 free(ondisk_path);
2841 if (err)
2842 return err;
2843 if (status == GOT_STATUS_ADD)
2844 return NULL;
2845 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2848 const struct got_error *
2849 got_worktree_schedule_add(struct got_worktree *worktree,
2850 struct got_pathlist_head *paths,
2851 got_worktree_checkout_cb progress_cb, void *progress_arg,
2852 struct got_repository *repo, int no_ignores)
2854 struct got_fileindex *fileindex = NULL;
2855 char *fileindex_path = NULL;
2856 const struct got_error *err = NULL, *sync_err, *unlockerr;
2857 struct got_pathlist_entry *pe;
2858 struct schedule_addition_args saa;
2860 err = lock_worktree(worktree, LOCK_EX);
2861 if (err)
2862 return err;
2864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2865 if (err)
2866 goto done;
2868 saa.worktree = worktree;
2869 saa.fileindex = fileindex;
2870 saa.progress_cb = progress_cb;
2871 saa.progress_arg = progress_arg;
2872 saa.repo = repo;
2874 TAILQ_FOREACH(pe, paths, entry) {
2875 err = worktree_status(worktree, pe->path, fileindex, repo,
2876 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2877 if (err)
2878 break;
2880 sync_err = sync_fileindex(fileindex, fileindex_path);
2881 if (sync_err && err == NULL)
2882 err = sync_err;
2883 done:
2884 free(fileindex_path);
2885 if (fileindex)
2886 got_fileindex_free(fileindex);
2887 unlockerr = lock_worktree(worktree, LOCK_SH);
2888 if (unlockerr && err == NULL)
2889 err = unlockerr;
2890 return err;
2893 struct schedule_deletion_args {
2894 struct got_worktree *worktree;
2895 struct got_fileindex *fileindex;
2896 got_worktree_delete_cb progress_cb;
2897 void *progress_arg;
2898 struct got_repository *repo;
2899 int delete_local_mods;
2902 static const struct got_error *
2903 schedule_for_deletion(void *arg, unsigned char status,
2904 unsigned char staged_status, const char *relpath,
2905 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2906 struct got_object_id *commit_id)
2908 struct schedule_deletion_args *a = arg;
2909 const struct got_error *err = NULL;
2910 struct got_fileindex_entry *ie = NULL;
2911 struct stat sb;
2912 char *ondisk_path;
2914 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2915 if (ie == NULL)
2916 return got_error(GOT_ERR_BAD_PATH);
2918 staged_status = get_staged_status(ie);
2919 if (staged_status != GOT_STATUS_NO_CHANGE) {
2920 if (staged_status == GOT_STATUS_DELETE)
2921 return NULL;
2922 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2925 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2926 relpath) == -1)
2927 return got_error_from_errno("asprintf");
2929 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2930 if (err)
2931 goto done;
2933 if (status != GOT_STATUS_NO_CHANGE) {
2934 if (status == GOT_STATUS_DELETE)
2935 goto done;
2936 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2937 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2938 goto done;
2940 if (status != GOT_STATUS_MODIFY &&
2941 status != GOT_STATUS_MISSING) {
2942 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2943 goto done;
2947 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0) {
2948 err = got_error_from_errno2("unlink", ondisk_path);
2949 goto done;
2952 got_fileindex_entry_mark_deleted_from_disk(ie);
2953 done:
2954 free(ondisk_path);
2955 if (err)
2956 return err;
2957 if (status == GOT_STATUS_DELETE)
2958 return NULL;
2959 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
2960 staged_status, relpath);
2963 const struct got_error *
2964 got_worktree_schedule_delete(struct got_worktree *worktree,
2965 struct got_pathlist_head *paths, int delete_local_mods,
2966 got_worktree_delete_cb progress_cb, void *progress_arg,
2967 struct got_repository *repo)
2969 struct got_fileindex *fileindex = NULL;
2970 char *fileindex_path = NULL;
2971 const struct got_error *err = NULL, *sync_err, *unlockerr;
2972 struct got_pathlist_entry *pe;
2973 struct schedule_deletion_args sda;
2975 err = lock_worktree(worktree, LOCK_EX);
2976 if (err)
2977 return err;
2979 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2980 if (err)
2981 goto done;
2983 sda.worktree = worktree;
2984 sda.fileindex = fileindex;
2985 sda.progress_cb = progress_cb;
2986 sda.progress_arg = progress_arg;
2987 sda.repo = repo;
2988 sda.delete_local_mods = delete_local_mods;
2990 TAILQ_FOREACH(pe, paths, entry) {
2991 err = worktree_status(worktree, pe->path, fileindex, repo,
2992 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
2993 if (err)
2994 break;
2996 sync_err = sync_fileindex(fileindex, fileindex_path);
2997 if (sync_err && err == NULL)
2998 err = sync_err;
2999 done:
3000 free(fileindex_path);
3001 if (fileindex)
3002 got_fileindex_free(fileindex);
3003 unlockerr = lock_worktree(worktree, LOCK_SH);
3004 if (unlockerr && err == NULL)
3005 err = unlockerr;
3006 return err;
3009 static const struct got_error *
3010 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3012 const struct got_error *err = NULL;
3013 char *line = NULL;
3014 size_t linesize = 0, n;
3015 ssize_t linelen;
3017 linelen = getline(&line, &linesize, infile);
3018 if (linelen == -1) {
3019 if (ferror(infile)) {
3020 err = got_error_from_errno("getline");
3021 goto done;
3023 return NULL;
3025 if (outfile) {
3026 n = fwrite(line, 1, linelen, outfile);
3027 if (n != linelen) {
3028 err = got_ferror(outfile, GOT_ERR_IO);
3029 goto done;
3032 if (rejectfile) {
3033 n = fwrite(line, 1, linelen, rejectfile);
3034 if (n != linelen)
3035 err = got_ferror(outfile, GOT_ERR_IO);
3037 done:
3038 free(line);
3039 return err;
3042 static const struct got_error *
3043 skip_one_line(FILE *f)
3045 char *line = NULL;
3046 size_t linesize = 0;
3047 ssize_t linelen;
3049 linelen = getline(&line, &linesize, f);
3050 if (linelen == -1) {
3051 if (ferror(f))
3052 return got_error_from_errno("getline");
3053 return NULL;
3055 free(line);
3056 return NULL;
3059 static const struct got_error *
3060 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3061 int start_old, int end_old, int start_new, int end_new,
3062 FILE *outfile, FILE *rejectfile)
3064 const struct got_error *err;
3066 /* Copy old file's lines leading up to patch. */
3067 while (!feof(f1) && *line_cur1 < start_old) {
3068 err = copy_one_line(f1, outfile, NULL);
3069 if (err)
3070 return err;
3071 (*line_cur1)++;
3073 /* Skip new file's lines leading up to patch. */
3074 while (!feof(f2) && *line_cur2 < start_new) {
3075 if (rejectfile)
3076 err = copy_one_line(f2, NULL, rejectfile);
3077 else
3078 err = skip_one_line(f2);
3079 if (err)
3080 return err;
3081 (*line_cur2)++;
3083 /* Copy patched lines. */
3084 while (!feof(f2) && *line_cur2 <= end_new) {
3085 err = copy_one_line(f2, outfile, NULL);
3086 if (err)
3087 return err;
3088 (*line_cur2)++;
3090 /* Skip over old file's replaced lines. */
3091 while (!feof(f1) && *line_cur1 <= end_old) {
3092 if (rejectfile)
3093 err = copy_one_line(f1, NULL, rejectfile);
3094 else
3095 err = skip_one_line(f1);
3096 if (err)
3097 return err;
3098 (*line_cur1)++;
3101 return NULL;
3104 static const struct got_error *
3105 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3106 FILE *outfile, FILE *rejectfile)
3108 const struct got_error *err;
3110 if (outfile) {
3111 /* Copy old file's lines until EOF. */
3112 while (!feof(f1)) {
3113 err = copy_one_line(f1, outfile, NULL);
3114 if (err)
3115 return err;
3116 (*line_cur1)++;
3119 if (rejectfile) {
3120 /* Copy new file's lines until EOF. */
3121 while (!feof(f2)) {
3122 err = copy_one_line(f2, NULL, rejectfile);
3123 if (err)
3124 return err;
3125 (*line_cur2)++;
3129 return NULL;
3132 static const struct got_error *
3133 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3134 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3135 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3136 int *line_cur2, FILE *outfile, FILE *rejectfile,
3137 got_worktree_patch_cb patch_cb, void *patch_arg)
3139 const struct got_error *err = NULL;
3140 int start_old = change->cv.a;
3141 int end_old = change->cv.b;
3142 int start_new = change->cv.c;
3143 int end_new = change->cv.d;
3144 long pos1, pos2;
3145 FILE *hunkfile;
3147 *choice = GOT_PATCH_CHOICE_NONE;
3149 hunkfile = got_opentemp();
3150 if (hunkfile == NULL)
3151 return got_error_from_errno("got_opentemp");
3153 pos1 = ftell(f1);
3154 pos2 = ftell(f2);
3156 /* XXX TODO needs error checking */
3157 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3159 if (fseek(f1, pos1, SEEK_SET) == -1) {
3160 err = got_ferror(f1, GOT_ERR_IO);
3161 goto done;
3163 if (fseek(f2, pos2, SEEK_SET) == -1) {
3164 err = got_ferror(f1, GOT_ERR_IO);
3165 goto done;
3167 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3168 err = got_ferror(hunkfile, GOT_ERR_IO);
3169 goto done;
3172 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3173 hunkfile, n, nchanges);
3174 if (err)
3175 goto done;
3177 switch (*choice) {
3178 case GOT_PATCH_CHOICE_YES:
3179 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3180 end_old, start_new, end_new, outfile, rejectfile);
3181 break;
3182 case GOT_PATCH_CHOICE_NO:
3183 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3184 end_old, start_new, end_new, rejectfile, outfile);
3185 break;
3186 case GOT_PATCH_CHOICE_QUIT:
3187 break;
3188 default:
3189 err = got_error(GOT_ERR_PATCH_CHOICE);
3190 break;
3192 done:
3193 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3194 err = got_error_from_errno("fclose");
3195 return err;
3198 struct revert_file_args {
3199 struct got_worktree *worktree;
3200 struct got_fileindex *fileindex;
3201 got_worktree_checkout_cb progress_cb;
3202 void *progress_arg;
3203 got_worktree_patch_cb patch_cb;
3204 void *patch_arg;
3205 struct got_repository *repo;
3208 static const struct got_error *
3209 create_patched_content(char **path_outfile, int reverse_patch,
3210 struct got_object_id *blob_id, const char *path2,
3211 const char *relpath, struct got_repository *repo,
3212 got_worktree_patch_cb patch_cb, void *patch_arg)
3214 const struct got_error *err;
3215 struct got_blob_object *blob = NULL;
3216 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3217 int fd2 = -1;
3218 char *path1 = NULL, *id_str = NULL;
3219 struct stat sb1, sb2;
3220 struct got_diff_changes *changes = NULL;
3221 struct got_diff_state *ds = NULL;
3222 struct got_diff_args *args = NULL;
3223 struct got_diff_change *change;
3224 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3225 int n = 0;
3227 *path_outfile = NULL;
3229 err = got_object_id_str(&id_str, blob_id);
3230 if (err)
3231 return err;
3233 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3234 if (fd2 == -1) {
3235 err = got_error_from_errno2("open", path2);
3236 goto done;
3238 if (fstat(fd2, &sb2) == -1) {
3239 err = got_error_from_errno2("fstat", path2);
3240 goto done;
3243 f2 = fdopen(fd2, "r");
3244 if (f2 == NULL) {
3245 err = got_error_from_errno2("fopen", path2);
3246 goto done;
3248 fd2 = -1;
3250 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3251 if (err)
3252 goto done;
3254 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3255 if (err)
3256 goto done;
3258 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3259 if (err)
3260 goto done;
3262 if (stat(path1, &sb1) == -1) {
3263 err = got_error_from_errno2("stat", path1);
3264 goto done;
3267 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3268 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3269 if (err)
3270 goto done;
3272 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3273 if (err)
3274 goto done;
3276 if (fseek(f1, 0L, SEEK_SET) == -1)
3277 return got_ferror(f1, GOT_ERR_IO);
3278 if (fseek(f2, 0L, SEEK_SET) == -1)
3279 return got_ferror(f2, GOT_ERR_IO);
3280 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3281 int choice;
3282 err = apply_or_reject_change(&choice, change, ++n,
3283 changes->nchanges, ds, args, diff_flags, relpath,
3284 f1, f2, &line_cur1, &line_cur2,
3285 reverse_patch ? NULL : outfile,
3286 reverse_patch ? outfile : NULL,
3287 patch_cb, patch_arg);
3288 if (err)
3289 goto done;
3290 if (choice == GOT_PATCH_CHOICE_YES)
3291 have_content = 1;
3292 else if (choice == GOT_PATCH_CHOICE_QUIT)
3293 break;
3295 if (have_content) {
3296 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3297 reverse_patch ? NULL : outfile,
3298 reverse_patch ? outfile : NULL);
3299 if (err)
3300 goto done;
3302 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3303 err = got_error_from_errno2("chmod", path2);
3304 goto done;
3307 done:
3308 free(id_str);
3309 if (blob)
3310 got_object_blob_close(blob);
3311 if (f1 && fclose(f1) == EOF && err == NULL)
3312 err = got_error_from_errno2("fclose", path1);
3313 if (f2 && fclose(f2) == EOF && err == NULL)
3314 err = got_error_from_errno2("fclose", path2);
3315 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3316 err = got_error_from_errno2("close", path2);
3317 if (outfile && fclose(outfile) == EOF && err == NULL)
3318 err = got_error_from_errno2("fclose", *path_outfile);
3319 if (path1 && unlink(path1) == -1 && err == NULL)
3320 err = got_error_from_errno2("unlink", path1);
3321 if (err || !have_content) {
3322 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3323 err = got_error_from_errno2("unlink", *path_outfile);
3324 free(*path_outfile);
3325 *path_outfile = NULL;
3327 free(args);
3328 if (ds) {
3329 got_diff_state_free(ds);
3330 free(ds);
3332 if (changes)
3333 got_diff_free_changes(changes);
3334 free(path1);
3335 return err;
3338 static const struct got_error *
3339 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3340 const char *relpath, struct got_object_id *blob_id,
3341 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3343 struct revert_file_args *a = arg;
3344 const struct got_error *err = NULL;
3345 char *parent_path = NULL;
3346 struct got_fileindex_entry *ie;
3347 struct got_tree_object *tree = NULL;
3348 struct got_object_id *tree_id = NULL;
3349 const struct got_tree_entry *te = NULL;
3350 char *tree_path = NULL, *te_name;
3351 char *ondisk_path = NULL, *path_content = NULL;
3352 struct got_blob_object *blob = NULL;
3354 /* Reverting a staged deletion is a no-op. */
3355 if (status == GOT_STATUS_DELETE &&
3356 staged_status != GOT_STATUS_NO_CHANGE)
3357 return NULL;
3359 if (status == GOT_STATUS_UNVERSIONED)
3360 return (*a->progress_cb)(a->progress_arg,
3361 GOT_STATUS_UNVERSIONED, relpath);
3363 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3364 if (ie == NULL)
3365 return got_error(GOT_ERR_BAD_PATH);
3367 /* Construct in-repository path of tree which contains this blob. */
3368 err = got_path_dirname(&parent_path, ie->path);
3369 if (err) {
3370 if (err->code != GOT_ERR_BAD_PATH)
3371 goto done;
3372 parent_path = strdup("/");
3373 if (parent_path == NULL) {
3374 err = got_error_from_errno("strdup");
3375 goto done;
3378 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3379 tree_path = strdup(parent_path);
3380 if (tree_path == NULL) {
3381 err = got_error_from_errno("strdup");
3382 goto done;
3384 } else {
3385 if (got_path_is_root_dir(parent_path)) {
3386 tree_path = strdup(a->worktree->path_prefix);
3387 if (tree_path == NULL) {
3388 err = got_error_from_errno("strdup");
3389 goto done;
3391 } else {
3392 if (asprintf(&tree_path, "%s/%s",
3393 a->worktree->path_prefix, parent_path) == -1) {
3394 err = got_error_from_errno("asprintf");
3395 goto done;
3400 err = got_object_id_by_path(&tree_id, a->repo,
3401 a->worktree->base_commit_id, tree_path);
3402 if (err) {
3403 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3404 (status == GOT_STATUS_ADD ||
3405 staged_status == GOT_STATUS_ADD)))
3406 goto done;
3407 } else {
3408 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3409 if (err)
3410 goto done;
3412 te_name = basename(ie->path);
3413 if (te_name == NULL) {
3414 err = got_error_from_errno2("basename", ie->path);
3415 goto done;
3418 te = got_object_tree_find_entry(tree, te_name);
3419 if (te == NULL && status != GOT_STATUS_ADD &&
3420 staged_status != GOT_STATUS_ADD) {
3421 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3422 goto done;
3426 switch (status) {
3427 case GOT_STATUS_ADD:
3428 if (a->patch_cb) {
3429 int choice = GOT_PATCH_CHOICE_NONE;
3430 err = (*a->patch_cb)(&choice, a->patch_arg,
3431 status, ie->path, NULL, 1, 1);
3432 if (err)
3433 goto done;
3434 if (choice != GOT_PATCH_CHOICE_YES)
3435 break;
3437 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3438 ie->path);
3439 if (err)
3440 goto done;
3441 got_fileindex_entry_remove(a->fileindex, ie);
3442 break;
3443 case GOT_STATUS_DELETE:
3444 if (a->patch_cb) {
3445 int choice = GOT_PATCH_CHOICE_NONE;
3446 err = (*a->patch_cb)(&choice, a->patch_arg,
3447 status, ie->path, NULL, 1, 1);
3448 if (err)
3449 goto done;
3450 if (choice != GOT_PATCH_CHOICE_YES)
3451 break;
3453 /* fall through */
3454 case GOT_STATUS_MODIFY:
3455 case GOT_STATUS_MODE_CHANGE:
3456 case GOT_STATUS_CONFLICT:
3457 case GOT_STATUS_MISSING: {
3458 struct got_object_id id;
3459 if (staged_status == GOT_STATUS_ADD ||
3460 staged_status == GOT_STATUS_MODIFY) {
3461 memcpy(id.sha1, ie->staged_blob_sha1,
3462 SHA1_DIGEST_LENGTH);
3463 } else
3464 memcpy(id.sha1, ie->blob_sha1,
3465 SHA1_DIGEST_LENGTH);
3466 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3467 if (err)
3468 goto done;
3470 if (asprintf(&ondisk_path, "%s/%s",
3471 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3472 err = got_error_from_errno("asprintf");
3473 goto done;
3476 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3477 status == GOT_STATUS_CONFLICT)) {
3478 err = create_patched_content(&path_content, 1, &id,
3479 ondisk_path, ie->path, a->repo,
3480 a->patch_cb, a->patch_arg);
3481 if (err || path_content == NULL)
3482 break;
3483 if (rename(path_content, ondisk_path) == -1) {
3484 err = got_error_from_errno3("rename",
3485 path_content, ondisk_path);
3486 goto done;
3488 } else {
3489 err = install_blob(a->worktree, ondisk_path, ie->path,
3490 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3491 got_fileindex_perms_to_st(ie), blob, 0, 1,
3492 a->repo, a->progress_cb, a->progress_arg);
3493 if (err)
3494 goto done;
3495 if (status == GOT_STATUS_DELETE ||
3496 status == GOT_STATUS_MODE_CHANGE) {
3497 err = update_blob_fileindex_entry(a->worktree,
3498 a->fileindex, ie, ondisk_path, ie->path,
3499 blob, 1);
3500 if (err)
3501 goto done;
3504 break;
3506 default:
3507 break;
3509 done:
3510 free(ondisk_path);
3511 free(path_content);
3512 free(parent_path);
3513 free(tree_path);
3514 if (blob)
3515 got_object_blob_close(blob);
3516 if (tree)
3517 got_object_tree_close(tree);
3518 free(tree_id);
3519 return err;
3522 const struct got_error *
3523 got_worktree_revert(struct got_worktree *worktree,
3524 struct got_pathlist_head *paths,
3525 got_worktree_checkout_cb progress_cb, void *progress_arg,
3526 got_worktree_patch_cb patch_cb, void *patch_arg,
3527 struct got_repository *repo)
3529 struct got_fileindex *fileindex = NULL;
3530 char *fileindex_path = NULL;
3531 const struct got_error *err = NULL, *unlockerr = NULL;
3532 const struct got_error *sync_err = NULL;
3533 struct got_pathlist_entry *pe;
3534 struct revert_file_args rfa;
3536 err = lock_worktree(worktree, LOCK_EX);
3537 if (err)
3538 return err;
3540 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3541 if (err)
3542 goto done;
3544 rfa.worktree = worktree;
3545 rfa.fileindex = fileindex;
3546 rfa.progress_cb = progress_cb;
3547 rfa.progress_arg = progress_arg;
3548 rfa.patch_cb = patch_cb;
3549 rfa.patch_arg = patch_arg;
3550 rfa.repo = repo;
3551 TAILQ_FOREACH(pe, paths, entry) {
3552 err = worktree_status(worktree, pe->path, fileindex, repo,
3553 revert_file, &rfa, NULL, NULL, 0, 0);
3554 if (err)
3555 break;
3557 sync_err = sync_fileindex(fileindex, fileindex_path);
3558 if (sync_err && err == NULL)
3559 err = sync_err;
3560 done:
3561 free(fileindex_path);
3562 if (fileindex)
3563 got_fileindex_free(fileindex);
3564 unlockerr = lock_worktree(worktree, LOCK_SH);
3565 if (unlockerr && err == NULL)
3566 err = unlockerr;
3567 return err;
3570 static void
3571 free_commitable(struct got_commitable *ct)
3573 free(ct->path);
3574 free(ct->in_repo_path);
3575 free(ct->ondisk_path);
3576 free(ct->blob_id);
3577 free(ct->base_blob_id);
3578 free(ct->staged_blob_id);
3579 free(ct->base_commit_id);
3580 free(ct);
3583 struct collect_commitables_arg {
3584 struct got_pathlist_head *commitable_paths;
3585 struct got_repository *repo;
3586 struct got_worktree *worktree;
3587 int have_staged_files;
3590 static const struct got_error *
3591 collect_commitables(void *arg, unsigned char status,
3592 unsigned char staged_status, const char *relpath,
3593 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3594 struct got_object_id *commit_id)
3596 struct collect_commitables_arg *a = arg;
3597 const struct got_error *err = NULL;
3598 struct got_commitable *ct = NULL;
3599 struct got_pathlist_entry *new = NULL;
3600 char *parent_path = NULL, *path = NULL;
3601 struct stat sb;
3603 if (a->have_staged_files) {
3604 if (staged_status != GOT_STATUS_MODIFY &&
3605 staged_status != GOT_STATUS_ADD &&
3606 staged_status != GOT_STATUS_DELETE)
3607 return NULL;
3608 } else {
3609 if (status == GOT_STATUS_CONFLICT)
3610 return got_error(GOT_ERR_COMMIT_CONFLICT);
3612 if (status != GOT_STATUS_MODIFY &&
3613 status != GOT_STATUS_MODE_CHANGE &&
3614 status != GOT_STATUS_ADD &&
3615 status != GOT_STATUS_DELETE)
3616 return NULL;
3619 if (asprintf(&path, "/%s", relpath) == -1) {
3620 err = got_error_from_errno("asprintf");
3621 goto done;
3623 if (strcmp(path, "/") == 0) {
3624 parent_path = strdup("");
3625 if (parent_path == NULL)
3626 return got_error_from_errno("strdup");
3627 } else {
3628 err = got_path_dirname(&parent_path, path);
3629 if (err)
3630 return err;
3633 ct = calloc(1, sizeof(*ct));
3634 if (ct == NULL) {
3635 err = got_error_from_errno("calloc");
3636 goto done;
3639 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3640 relpath) == -1) {
3641 err = got_error_from_errno("asprintf");
3642 goto done;
3644 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3645 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3646 } else {
3647 if (lstat(ct->ondisk_path, &sb) != 0) {
3648 err = got_error_from_errno2("lstat", ct->ondisk_path);
3649 goto done;
3651 ct->mode = sb.st_mode;
3654 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3655 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3656 relpath) == -1) {
3657 err = got_error_from_errno("asprintf");
3658 goto done;
3661 ct->status = status;
3662 ct->staged_status = staged_status;
3663 ct->blob_id = NULL; /* will be filled in when blob gets created */
3664 if (ct->status != GOT_STATUS_ADD &&
3665 ct->staged_status != GOT_STATUS_ADD) {
3666 ct->base_blob_id = got_object_id_dup(blob_id);
3667 if (ct->base_blob_id == NULL) {
3668 err = got_error_from_errno("got_object_id_dup");
3669 goto done;
3671 ct->base_commit_id = got_object_id_dup(commit_id);
3672 if (ct->base_commit_id == NULL) {
3673 err = got_error_from_errno("got_object_id_dup");
3674 goto done;
3677 if (ct->staged_status == GOT_STATUS_ADD ||
3678 ct->staged_status == GOT_STATUS_MODIFY) {
3679 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3680 if (ct->staged_blob_id == NULL) {
3681 err = got_error_from_errno("got_object_id_dup");
3682 goto done;
3685 ct->path = strdup(path);
3686 if (ct->path == NULL) {
3687 err = got_error_from_errno("strdup");
3688 goto done;
3690 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3691 done:
3692 if (ct && (err || new == NULL))
3693 free_commitable(ct);
3694 free(parent_path);
3695 free(path);
3696 return err;
3699 static const struct got_error *write_tree(struct got_object_id **,
3700 struct got_tree_object *, const char *, struct got_pathlist_head *,
3701 got_worktree_status_cb status_cb, void *status_arg,
3702 struct got_repository *);
3704 static const struct got_error *
3705 write_subtree(struct got_object_id **new_subtree_id,
3706 struct got_tree_entry *te, const char *parent_path,
3707 struct got_pathlist_head *commitable_paths,
3708 got_worktree_status_cb status_cb, void *status_arg,
3709 struct got_repository *repo)
3711 const struct got_error *err = NULL;
3712 struct got_tree_object *subtree;
3713 char *subpath;
3715 if (asprintf(&subpath, "%s%s%s", parent_path,
3716 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3717 return got_error_from_errno("asprintf");
3719 err = got_object_open_as_tree(&subtree, repo, &te->id);
3720 if (err)
3721 return err;
3723 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3724 status_cb, status_arg, repo);
3725 got_object_tree_close(subtree);
3726 free(subpath);
3727 return err;
3730 static const struct got_error *
3731 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3733 const struct got_error *err = NULL;
3734 char *ct_parent_path = NULL;
3736 *match = 0;
3738 if (strchr(ct->in_repo_path, '/') == NULL) {
3739 *match = got_path_is_root_dir(path);
3740 return NULL;
3743 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3744 if (err)
3745 return err;
3746 *match = (strcmp(path, ct_parent_path) == 0);
3747 free(ct_parent_path);
3748 return err;
3751 static mode_t
3752 get_ct_file_mode(struct got_commitable *ct)
3754 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3757 static const struct got_error *
3758 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3759 struct got_tree_entry *te, struct got_commitable *ct)
3761 const struct got_error *err = NULL;
3763 *new_te = NULL;
3765 err = got_object_tree_entry_dup(new_te, te);
3766 if (err)
3767 goto done;
3769 (*new_te)->mode = get_ct_file_mode(ct);
3771 if (ct->staged_status == GOT_STATUS_MODIFY)
3772 memcpy(&(*new_te)->id, ct->staged_blob_id,
3773 sizeof((*new_te)->id));
3774 else
3775 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3776 done:
3777 if (err && *new_te) {
3778 free(*new_te);
3779 *new_te = NULL;
3781 return err;
3784 static const struct got_error *
3785 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3786 struct got_commitable *ct)
3788 const struct got_error *err = NULL;
3789 char *ct_name;
3791 *new_te = NULL;
3793 *new_te = calloc(1, sizeof(**new_te));
3794 if (*new_te == NULL)
3795 return got_error_from_errno("calloc");
3797 ct_name = basename(ct->path);
3798 if (ct_name == NULL) {
3799 err = got_error_from_errno2("basename", ct->path);
3800 goto done;
3802 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3803 sizeof((*new_te)->name)) {
3804 err = got_error(GOT_ERR_NO_SPACE);
3805 goto done;
3808 (*new_te)->mode = get_ct_file_mode(ct);
3810 if (ct->staged_status == GOT_STATUS_ADD)
3811 memcpy(&(*new_te)->id, ct->staged_blob_id,
3812 sizeof((*new_te)->id));
3813 else
3814 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3815 done:
3816 if (err && *new_te) {
3817 free(*new_te);
3818 *new_te = NULL;
3820 return err;
3823 static const struct got_error *
3824 insert_tree_entry(struct got_tree_entry *new_te,
3825 struct got_pathlist_head *paths)
3827 const struct got_error *err = NULL;
3828 struct got_pathlist_entry *new_pe;
3830 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3831 if (err)
3832 return err;
3833 if (new_pe == NULL)
3834 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3835 return NULL;
3838 static const struct got_error *
3839 report_ct_status(struct got_commitable *ct,
3840 got_worktree_status_cb status_cb, void *status_arg)
3842 const char *ct_path = ct->path;
3843 unsigned char status;
3845 while (ct_path[0] == '/')
3846 ct_path++;
3848 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3849 status = ct->staged_status;
3850 else
3851 status = ct->status;
3853 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3854 ct_path, ct->blob_id, NULL, NULL);
3857 static const struct got_error *
3858 match_modified_subtree(int *modified, struct got_tree_entry *te,
3859 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3861 const struct got_error *err = NULL;
3862 struct got_pathlist_entry *pe;
3863 char *te_path;
3865 *modified = 0;
3867 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3868 got_path_is_root_dir(base_tree_path) ? "" : "/",
3869 te->name) == -1)
3870 return got_error_from_errno("asprintf");
3872 TAILQ_FOREACH(pe, commitable_paths, entry) {
3873 struct got_commitable *ct = pe->data;
3874 *modified = got_path_is_child(ct->in_repo_path, te_path,
3875 strlen(te_path));
3876 if (*modified)
3877 break;
3880 free(te_path);
3881 return err;
3884 static const struct got_error *
3885 match_deleted_or_modified_ct(struct got_commitable **ctp,
3886 struct got_tree_entry *te, const char *base_tree_path,
3887 struct got_pathlist_head *commitable_paths)
3889 const struct got_error *err = NULL;
3890 struct got_pathlist_entry *pe;
3892 *ctp = NULL;
3894 TAILQ_FOREACH(pe, commitable_paths, entry) {
3895 struct got_commitable *ct = pe->data;
3896 char *ct_name = NULL;
3897 int path_matches;
3899 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3900 if (ct->status != GOT_STATUS_MODIFY &&
3901 ct->status != GOT_STATUS_MODE_CHANGE &&
3902 ct->status != GOT_STATUS_DELETE)
3903 continue;
3904 } else {
3905 if (ct->staged_status != GOT_STATUS_MODIFY &&
3906 ct->staged_status != GOT_STATUS_DELETE)
3907 continue;
3910 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3911 continue;
3913 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3914 if (err)
3915 return err;
3916 if (!path_matches)
3917 continue;
3919 ct_name = basename(pe->path);
3920 if (ct_name == NULL)
3921 return got_error_from_errno2("basename", pe->path);
3923 if (strcmp(te->name, ct_name) != 0)
3924 continue;
3926 *ctp = ct;
3927 break;
3930 return err;
3933 static const struct got_error *
3934 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3935 const char *child_path, const char *path_base_tree,
3936 struct got_pathlist_head *commitable_paths,
3937 got_worktree_status_cb status_cb, void *status_arg,
3938 struct got_repository *repo)
3940 const struct got_error *err = NULL;
3941 struct got_tree_entry *new_te;
3942 char *subtree_path;
3943 struct got_object_id *id = NULL;
3945 *new_tep = NULL;
3947 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3948 got_path_is_root_dir(path_base_tree) ? "" : "/",
3949 child_path) == -1)
3950 return got_error_from_errno("asprintf");
3952 new_te = calloc(1, sizeof(*new_te));
3953 if (new_te == NULL)
3954 return got_error_from_errno("calloc");
3955 new_te->mode = S_IFDIR;
3957 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
3958 sizeof(new_te->name)) {
3959 err = got_error(GOT_ERR_NO_SPACE);
3960 goto done;
3962 err = write_tree(&id, NULL, subtree_path,
3963 commitable_paths, status_cb, status_arg, repo);
3964 if (err) {
3965 free(new_te);
3966 goto done;
3968 memcpy(&new_te->id, id, sizeof(new_te->id));
3969 done:
3970 free(id);
3971 free(subtree_path);
3972 if (err == NULL)
3973 *new_tep = new_te;
3974 return err;
3977 static const struct got_error *
3978 write_tree(struct got_object_id **new_tree_id,
3979 struct got_tree_object *base_tree, const char *path_base_tree,
3980 struct got_pathlist_head *commitable_paths,
3981 got_worktree_status_cb status_cb, void *status_arg,
3982 struct got_repository *repo)
3984 const struct got_error *err = NULL;
3985 struct got_pathlist_head paths;
3986 struct got_tree_entry *te, *new_te = NULL;
3987 struct got_pathlist_entry *pe;
3988 int nentries = 0;
3990 TAILQ_INIT(&paths);
3992 /* Insert, and recurse into, newly added entries first. */
3993 TAILQ_FOREACH(pe, commitable_paths, entry) {
3994 struct got_commitable *ct = pe->data;
3995 char *child_path = NULL, *slash;
3997 if ((ct->status != GOT_STATUS_ADD &&
3998 ct->staged_status != GOT_STATUS_ADD) ||
3999 (ct->flags & GOT_COMMITABLE_ADDED))
4000 continue;
4002 if (!got_path_is_child(pe->path, path_base_tree,
4003 strlen(path_base_tree)))
4004 continue;
4006 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4007 pe->path);
4008 if (err)
4009 goto done;
4011 slash = strchr(child_path, '/');
4012 if (slash == NULL) {
4013 err = alloc_added_blob_tree_entry(&new_te, ct);
4014 if (err)
4015 goto done;
4016 err = report_ct_status(ct, status_cb, status_arg);
4017 if (err)
4018 goto done;
4019 ct->flags |= GOT_COMMITABLE_ADDED;
4020 err = insert_tree_entry(new_te, &paths);
4021 if (err)
4022 goto done;
4023 nentries++;
4024 } else {
4025 *slash = '\0'; /* trim trailing path components */
4026 if (base_tree == NULL ||
4027 got_object_tree_find_entry(base_tree, child_path)
4028 == NULL) {
4029 err = make_subtree_for_added_blob(&new_te,
4030 child_path, path_base_tree,
4031 commitable_paths, status_cb, status_arg,
4032 repo);
4033 if (err)
4034 goto done;
4035 err = insert_tree_entry(new_te, &paths);
4036 if (err)
4037 goto done;
4038 nentries++;
4043 if (base_tree) {
4044 int i, nbase_entries;
4045 /* Handle modified and deleted entries. */
4046 nbase_entries = got_object_tree_get_nentries(base_tree);
4047 for (i = 0; i < nbase_entries; i++) {
4048 struct got_commitable *ct = NULL;
4050 te = got_object_tree_get_entry(base_tree, i);
4051 if (got_object_tree_entry_is_submodule(te)) {
4052 /* Entry is a submodule; just copy it. */
4053 err = got_object_tree_entry_dup(&new_te, te);
4054 if (err)
4055 goto done;
4056 err = insert_tree_entry(new_te, &paths);
4057 if (err)
4058 goto done;
4059 nentries++;
4060 continue;
4063 if (S_ISDIR(te->mode)) {
4064 int modified;
4065 err = got_object_tree_entry_dup(&new_te, te);
4066 if (err)
4067 goto done;
4068 err = match_modified_subtree(&modified, te,
4069 path_base_tree, commitable_paths);
4070 if (err)
4071 goto done;
4072 /* Avoid recursion into unmodified subtrees. */
4073 if (modified) {
4074 struct got_object_id *new_id;
4075 err = write_subtree(&new_id, te,
4076 path_base_tree, commitable_paths,
4077 status_cb, status_arg, repo);
4078 if (err)
4079 goto done;
4080 memcpy(&new_te->id, new_id,
4081 sizeof(new_te->id));
4082 free(new_id);
4084 err = insert_tree_entry(new_te, &paths);
4085 if (err)
4086 goto done;
4087 nentries++;
4088 continue;
4091 err = match_deleted_or_modified_ct(&ct, te,
4092 path_base_tree, commitable_paths);
4093 if (err)
4094 goto done;
4095 if (ct) {
4096 /* NB: Deleted entries get dropped here. */
4097 if (ct->status == GOT_STATUS_MODIFY ||
4098 ct->status == GOT_STATUS_MODE_CHANGE ||
4099 ct->staged_status == GOT_STATUS_MODIFY) {
4100 err = alloc_modified_blob_tree_entry(
4101 &new_te, te, ct);
4102 if (err)
4103 goto done;
4104 err = insert_tree_entry(new_te, &paths);
4105 if (err)
4106 goto done;
4107 nentries++;
4109 err = report_ct_status(ct, status_cb,
4110 status_arg);
4111 if (err)
4112 goto done;
4113 } else {
4114 /* Entry is unchanged; just copy it. */
4115 err = got_object_tree_entry_dup(&new_te, te);
4116 if (err)
4117 goto done;
4118 err = insert_tree_entry(new_te, &paths);
4119 if (err)
4120 goto done;
4121 nentries++;
4126 /* Write new list of entries; deleted entries have been dropped. */
4127 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4128 done:
4129 got_pathlist_free(&paths);
4130 return err;
4133 static const struct got_error *
4134 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4135 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4136 int have_staged_files)
4138 const struct got_error *err = NULL;
4139 struct got_pathlist_entry *pe;
4141 TAILQ_FOREACH(pe, commitable_paths, entry) {
4142 struct got_fileindex_entry *ie;
4143 struct got_commitable *ct = pe->data;
4145 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4146 if (ie) {
4147 if (ct->status == GOT_STATUS_DELETE ||
4148 ct->staged_status == GOT_STATUS_DELETE) {
4149 got_fileindex_entry_remove(fileindex, ie);
4150 got_fileindex_entry_free(ie);
4151 } else if (ct->staged_status == GOT_STATUS_ADD ||
4152 ct->staged_status == GOT_STATUS_MODIFY) {
4153 got_fileindex_entry_stage_set(ie,
4154 GOT_FILEIDX_STAGE_NONE);
4155 err = got_fileindex_entry_update(ie,
4156 ct->ondisk_path, ct->staged_blob_id->sha1,
4157 new_base_commit_id->sha1,
4158 !have_staged_files);
4159 } else
4160 err = got_fileindex_entry_update(ie,
4161 ct->ondisk_path, ct->blob_id->sha1,
4162 new_base_commit_id->sha1,
4163 !have_staged_files);
4164 } else {
4165 err = got_fileindex_entry_alloc(&ie,
4166 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4167 new_base_commit_id->sha1);
4168 if (err)
4169 break;
4170 err = got_fileindex_entry_add(fileindex, ie);
4171 if (err)
4172 break;
4175 return err;
4179 static const struct got_error *
4180 check_out_of_date(const char *in_repo_path, unsigned char status,
4181 unsigned char staged_status, struct got_object_id *base_blob_id,
4182 struct got_object_id *base_commit_id,
4183 struct got_object_id *head_commit_id, struct got_repository *repo,
4184 int ood_errcode)
4186 const struct got_error *err = NULL;
4187 struct got_object_id *id = NULL;
4189 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4190 /* Trivial case: base commit == head commit */
4191 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4192 return NULL;
4194 * Ensure file content which local changes were based
4195 * on matches file content in the branch head.
4197 err = got_object_id_by_path(&id, repo, head_commit_id,
4198 in_repo_path);
4199 if (err) {
4200 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4201 err = got_error(ood_errcode);
4202 goto done;
4203 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4204 err = got_error(ood_errcode);
4205 } else {
4206 /* Require that added files don't exist in the branch head. */
4207 err = got_object_id_by_path(&id, repo, head_commit_id,
4208 in_repo_path);
4209 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4210 goto done;
4211 err = id ? got_error(ood_errcode) : NULL;
4213 done:
4214 free(id);
4215 return err;
4218 const struct got_error *
4219 commit_worktree(struct got_object_id **new_commit_id,
4220 struct got_pathlist_head *commitable_paths,
4221 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4222 const char *author, const char *committer,
4223 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4224 got_worktree_status_cb status_cb, void *status_arg,
4225 struct got_repository *repo)
4227 const struct got_error *err = NULL, *unlockerr = NULL;
4228 struct got_pathlist_entry *pe;
4229 const char *head_ref_name = NULL;
4230 struct got_commit_object *head_commit = NULL;
4231 struct got_reference *head_ref2 = NULL;
4232 struct got_object_id *head_commit_id2 = NULL;
4233 struct got_tree_object *head_tree = NULL;
4234 struct got_object_id *new_tree_id = NULL;
4235 struct got_object_id_queue parent_ids;
4236 struct got_object_qid *pid = NULL;
4237 char *logmsg = NULL;
4239 *new_commit_id = NULL;
4241 SIMPLEQ_INIT(&parent_ids);
4243 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4244 if (err)
4245 goto done;
4247 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4248 if (err)
4249 goto done;
4251 if (commit_msg_cb != NULL) {
4252 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4253 if (err)
4254 goto done;
4257 if (logmsg == NULL || strlen(logmsg) == 0) {
4258 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4259 goto done;
4262 /* Create blobs from added and modified files and record their IDs. */
4263 TAILQ_FOREACH(pe, commitable_paths, entry) {
4264 struct got_commitable *ct = pe->data;
4265 char *ondisk_path;
4267 /* Blobs for staged files already exist. */
4268 if (ct->staged_status == GOT_STATUS_ADD ||
4269 ct->staged_status == GOT_STATUS_MODIFY)
4270 continue;
4272 if (ct->status != GOT_STATUS_ADD &&
4273 ct->status != GOT_STATUS_MODIFY &&
4274 ct->status != GOT_STATUS_MODE_CHANGE)
4275 continue;
4277 if (asprintf(&ondisk_path, "%s/%s",
4278 worktree->root_path, pe->path) == -1) {
4279 err = got_error_from_errno("asprintf");
4280 goto done;
4282 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4283 free(ondisk_path);
4284 if (err)
4285 goto done;
4288 /* Recursively write new tree objects. */
4289 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4290 status_cb, status_arg, repo);
4291 if (err)
4292 goto done;
4294 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4295 if (err)
4296 goto done;
4297 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4298 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4299 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4300 got_object_qid_free(pid);
4301 if (logmsg != NULL)
4302 free(logmsg);
4303 if (err)
4304 goto done;
4306 /* Check if a concurrent commit to our branch has occurred. */
4307 head_ref_name = got_worktree_get_head_ref_name(worktree);
4308 if (head_ref_name == NULL) {
4309 err = got_error_from_errno("got_worktree_get_head_ref_name");
4310 goto done;
4312 /* Lock the reference here to prevent concurrent modification. */
4313 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4314 if (err)
4315 goto done;
4316 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4317 if (err)
4318 goto done;
4319 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4320 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4321 goto done;
4323 /* Update branch head in repository. */
4324 err = got_ref_change_ref(head_ref2, *new_commit_id);
4325 if (err)
4326 goto done;
4327 err = got_ref_write(head_ref2, repo);
4328 if (err)
4329 goto done;
4331 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4332 if (err)
4333 goto done;
4335 err = ref_base_commit(worktree, repo);
4336 if (err)
4337 goto done;
4338 done:
4339 if (head_tree)
4340 got_object_tree_close(head_tree);
4341 if (head_commit)
4342 got_object_commit_close(head_commit);
4343 free(head_commit_id2);
4344 if (head_ref2) {
4345 unlockerr = got_ref_unlock(head_ref2);
4346 if (unlockerr && err == NULL)
4347 err = unlockerr;
4348 got_ref_close(head_ref2);
4350 return err;
4353 static const struct got_error *
4354 check_path_is_commitable(const char *path,
4355 struct got_pathlist_head *commitable_paths)
4357 struct got_pathlist_entry *cpe = NULL;
4358 size_t path_len = strlen(path);
4360 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4361 struct got_commitable *ct = cpe->data;
4362 const char *ct_path = ct->path;
4364 while (ct_path[0] == '/')
4365 ct_path++;
4367 if (strcmp(path, ct_path) == 0 ||
4368 got_path_is_child(ct_path, path, path_len))
4369 break;
4372 if (cpe == NULL)
4373 return got_error_path(path, GOT_ERR_BAD_PATH);
4375 return NULL;
4378 static const struct got_error *
4379 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4381 int *have_staged_files = arg;
4383 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4384 *have_staged_files = 1;
4385 return got_error(GOT_ERR_CANCELLED);
4388 return NULL;
4391 static const struct got_error *
4392 check_non_staged_files(struct got_fileindex *fileindex,
4393 struct got_pathlist_head *paths)
4395 struct got_pathlist_entry *pe;
4396 struct got_fileindex_entry *ie;
4398 TAILQ_FOREACH(pe, paths, entry) {
4399 if (pe->path[0] == '\0')
4400 continue;
4401 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4402 if (ie == NULL)
4403 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4404 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4405 return got_error_path(pe->path,
4406 GOT_ERR_FILE_NOT_STAGED);
4409 return NULL;
4412 const struct got_error *
4413 got_worktree_commit(struct got_object_id **new_commit_id,
4414 struct got_worktree *worktree, struct got_pathlist_head *paths,
4415 const char *author, const char *committer,
4416 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4417 got_worktree_status_cb status_cb, void *status_arg,
4418 struct got_repository *repo)
4420 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4421 struct got_fileindex *fileindex = NULL;
4422 char *fileindex_path = NULL;
4423 struct got_pathlist_head commitable_paths;
4424 struct collect_commitables_arg cc_arg;
4425 struct got_pathlist_entry *pe;
4426 struct got_reference *head_ref = NULL;
4427 struct got_object_id *head_commit_id = NULL;
4428 int have_staged_files = 0;
4430 *new_commit_id = NULL;
4432 TAILQ_INIT(&commitable_paths);
4434 err = lock_worktree(worktree, LOCK_EX);
4435 if (err)
4436 goto done;
4438 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4439 if (err)
4440 goto done;
4442 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4443 if (err)
4444 goto done;
4446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4447 if (err)
4448 goto done;
4450 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4451 &have_staged_files);
4452 if (err && err->code != GOT_ERR_CANCELLED)
4453 goto done;
4454 if (have_staged_files) {
4455 err = check_non_staged_files(fileindex, paths);
4456 if (err)
4457 goto done;
4460 cc_arg.commitable_paths = &commitable_paths;
4461 cc_arg.worktree = worktree;
4462 cc_arg.repo = repo;
4463 cc_arg.have_staged_files = have_staged_files;
4464 TAILQ_FOREACH(pe, paths, entry) {
4465 err = worktree_status(worktree, pe->path, fileindex, repo,
4466 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4467 if (err)
4468 goto done;
4471 if (TAILQ_EMPTY(&commitable_paths)) {
4472 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4473 goto done;
4476 TAILQ_FOREACH(pe, paths, entry) {
4477 err = check_path_is_commitable(pe->path, &commitable_paths);
4478 if (err)
4479 goto done;
4482 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4483 struct got_commitable *ct = pe->data;
4484 const char *ct_path = ct->in_repo_path;
4486 while (ct_path[0] == '/')
4487 ct_path++;
4488 err = check_out_of_date(ct_path, ct->status,
4489 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4490 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4491 if (err)
4492 goto done;
4496 err = commit_worktree(new_commit_id, &commitable_paths,
4497 head_commit_id, worktree, author, committer,
4498 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4499 if (err)
4500 goto done;
4502 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4503 fileindex, have_staged_files);
4504 sync_err = sync_fileindex(fileindex, fileindex_path);
4505 if (sync_err && err == NULL)
4506 err = sync_err;
4507 done:
4508 if (fileindex)
4509 got_fileindex_free(fileindex);
4510 free(fileindex_path);
4511 unlockerr = lock_worktree(worktree, LOCK_SH);
4512 if (unlockerr && err == NULL)
4513 err = unlockerr;
4514 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4515 struct got_commitable *ct = pe->data;
4516 free_commitable(ct);
4518 got_pathlist_free(&commitable_paths);
4519 return err;
4522 const char *
4523 got_commitable_get_path(struct got_commitable *ct)
4525 return ct->path;
4528 unsigned int
4529 got_commitable_get_status(struct got_commitable *ct)
4531 return ct->status;
4534 struct check_rebase_ok_arg {
4535 struct got_worktree *worktree;
4536 struct got_repository *repo;
4539 static const struct got_error *
4540 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4542 const struct got_error *err = NULL;
4543 struct check_rebase_ok_arg *a = arg;
4544 unsigned char status;
4545 struct stat sb;
4546 char *ondisk_path;
4548 /* Reject rebase of a work tree with mixed base commits. */
4549 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4550 SHA1_DIGEST_LENGTH))
4551 return got_error(GOT_ERR_MIXED_COMMITS);
4553 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4554 == -1)
4555 return got_error_from_errno("asprintf");
4557 /* Reject rebase of a work tree with modified or staged files. */
4558 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4559 free(ondisk_path);
4560 if (err)
4561 return err;
4563 if (status != GOT_STATUS_NO_CHANGE)
4564 return got_error(GOT_ERR_MODIFIED);
4565 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4566 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4568 return NULL;
4571 const struct got_error *
4572 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4573 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4574 struct got_worktree *worktree, struct got_reference *branch,
4575 struct got_repository *repo)
4577 const struct got_error *err = NULL;
4578 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4579 char *branch_ref_name = NULL;
4580 char *fileindex_path = NULL;
4581 struct check_rebase_ok_arg ok_arg;
4582 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4584 *new_base_branch_ref = NULL;
4585 *tmp_branch = NULL;
4586 *fileindex = NULL;
4588 err = lock_worktree(worktree, LOCK_EX);
4589 if (err)
4590 return err;
4592 err = open_fileindex(fileindex, &fileindex_path, worktree);
4593 if (err)
4594 goto done;
4596 ok_arg.worktree = worktree;
4597 ok_arg.repo = repo;
4598 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4599 &ok_arg);
4600 if (err)
4601 goto done;
4603 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4604 if (err)
4605 goto done;
4607 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4608 if (err)
4609 goto done;
4611 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4612 if (err)
4613 goto done;
4615 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4616 0);
4617 if (err)
4618 goto done;
4620 err = got_ref_alloc_symref(new_base_branch_ref,
4621 new_base_branch_ref_name, wt_branch);
4622 if (err)
4623 goto done;
4624 err = got_ref_write(*new_base_branch_ref, repo);
4625 if (err)
4626 goto done;
4628 /* TODO Lock original branch's ref while rebasing? */
4630 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4631 if (err)
4632 goto done;
4634 err = got_ref_write(branch_ref, repo);
4635 if (err)
4636 goto done;
4638 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4639 worktree->base_commit_id);
4640 if (err)
4641 goto done;
4642 err = got_ref_write(*tmp_branch, repo);
4643 if (err)
4644 goto done;
4646 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4647 if (err)
4648 goto done;
4649 done:
4650 free(fileindex_path);
4651 free(tmp_branch_name);
4652 free(new_base_branch_ref_name);
4653 free(branch_ref_name);
4654 if (branch_ref)
4655 got_ref_close(branch_ref);
4656 if (wt_branch)
4657 got_ref_close(wt_branch);
4658 if (err) {
4659 if (*new_base_branch_ref) {
4660 got_ref_close(*new_base_branch_ref);
4661 *new_base_branch_ref = NULL;
4663 if (*tmp_branch) {
4664 got_ref_close(*tmp_branch);
4665 *tmp_branch = NULL;
4667 if (*fileindex) {
4668 got_fileindex_free(*fileindex);
4669 *fileindex = NULL;
4671 lock_worktree(worktree, LOCK_SH);
4673 return err;
4676 const struct got_error *
4677 got_worktree_rebase_continue(struct got_object_id **commit_id,
4678 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4679 struct got_reference **branch, struct got_fileindex **fileindex,
4680 struct got_worktree *worktree, struct got_repository *repo)
4682 const struct got_error *err;
4683 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4684 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4685 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4686 char *fileindex_path = NULL;
4687 int have_staged_files = 0;
4689 *commit_id = NULL;
4690 *new_base_branch = NULL;
4691 *tmp_branch = NULL;
4692 *branch = NULL;
4693 *fileindex = NULL;
4695 err = lock_worktree(worktree, LOCK_EX);
4696 if (err)
4697 return err;
4699 err = open_fileindex(fileindex, &fileindex_path, worktree);
4700 if (err)
4701 goto done;
4703 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4704 &have_staged_files);
4705 if (err && err->code != GOT_ERR_CANCELLED)
4706 goto done;
4707 if (have_staged_files) {
4708 err = got_error(GOT_ERR_STAGED_PATHS);
4709 goto done;
4712 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4713 if (err)
4714 goto done;
4716 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4717 if (err)
4718 goto done;
4720 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4721 if (err)
4722 goto done;
4724 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4725 if (err)
4726 goto done;
4728 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4729 if (err)
4730 goto done;
4732 err = got_ref_open(branch, repo,
4733 got_ref_get_symref_target(branch_ref), 0);
4734 if (err)
4735 goto done;
4737 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4738 if (err)
4739 goto done;
4741 err = got_ref_resolve(commit_id, repo, commit_ref);
4742 if (err)
4743 goto done;
4745 err = got_ref_open(new_base_branch, repo,
4746 new_base_branch_ref_name, 0);
4747 if (err)
4748 goto done;
4750 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4751 if (err)
4752 goto done;
4753 done:
4754 free(commit_ref_name);
4755 free(branch_ref_name);
4756 free(fileindex_path);
4757 if (commit_ref)
4758 got_ref_close(commit_ref);
4759 if (branch_ref)
4760 got_ref_close(branch_ref);
4761 if (err) {
4762 free(*commit_id);
4763 *commit_id = NULL;
4764 if (*tmp_branch) {
4765 got_ref_close(*tmp_branch);
4766 *tmp_branch = NULL;
4768 if (*new_base_branch) {
4769 got_ref_close(*new_base_branch);
4770 *new_base_branch = NULL;
4772 if (*branch) {
4773 got_ref_close(*branch);
4774 *branch = NULL;
4776 if (*fileindex) {
4777 got_fileindex_free(*fileindex);
4778 *fileindex = NULL;
4780 lock_worktree(worktree, LOCK_SH);
4782 return err;
4785 const struct got_error *
4786 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4788 const struct got_error *err;
4789 char *tmp_branch_name = NULL;
4791 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4792 if (err)
4793 return err;
4795 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4796 free(tmp_branch_name);
4797 return NULL;
4800 static const struct got_error *
4801 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4802 char **logmsg, void *arg)
4804 *logmsg = arg;
4805 return NULL;
4808 static const struct got_error *
4809 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4810 const char *path, struct got_object_id *blob_id,
4811 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4813 return NULL;
4816 struct collect_merged_paths_arg {
4817 got_worktree_checkout_cb progress_cb;
4818 void *progress_arg;
4819 struct got_pathlist_head *merged_paths;
4822 static const struct got_error *
4823 collect_merged_paths(void *arg, unsigned char status, const char *path)
4825 const struct got_error *err;
4826 struct collect_merged_paths_arg *a = arg;
4827 char *p;
4828 struct got_pathlist_entry *new;
4830 err = (*a->progress_cb)(a->progress_arg, status, path);
4831 if (err)
4832 return err;
4834 if (status != GOT_STATUS_MERGE &&
4835 status != GOT_STATUS_ADD &&
4836 status != GOT_STATUS_DELETE &&
4837 status != GOT_STATUS_CONFLICT)
4838 return NULL;
4840 p = strdup(path);
4841 if (p == NULL)
4842 return got_error_from_errno("strdup");
4844 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4845 if (err || new == NULL)
4846 free(p);
4847 return err;
4850 void
4851 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4853 struct got_pathlist_entry *pe;
4855 TAILQ_FOREACH(pe, merged_paths, entry)
4856 free((char *)pe->path);
4858 got_pathlist_free(merged_paths);
4861 static const struct got_error *
4862 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4863 struct got_repository *repo)
4865 const struct got_error *err;
4866 struct got_reference *commit_ref = NULL;
4868 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4869 if (err) {
4870 if (err->code != GOT_ERR_NOT_REF)
4871 goto done;
4872 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4873 if (err)
4874 goto done;
4875 err = got_ref_write(commit_ref, repo);
4876 if (err)
4877 goto done;
4878 } else {
4879 struct got_object_id *stored_id;
4880 int cmp;
4882 err = got_ref_resolve(&stored_id, repo, commit_ref);
4883 if (err)
4884 goto done;
4885 cmp = got_object_id_cmp(commit_id, stored_id);
4886 free(stored_id);
4887 if (cmp != 0) {
4888 err = got_error(GOT_ERR_REBASE_COMMITID);
4889 goto done;
4892 done:
4893 if (commit_ref)
4894 got_ref_close(commit_ref);
4895 return err;
4898 static const struct got_error *
4899 rebase_merge_files(struct got_pathlist_head *merged_paths,
4900 const char *commit_ref_name, struct got_worktree *worktree,
4901 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4902 struct got_object_id *commit_id, struct got_repository *repo,
4903 got_worktree_checkout_cb progress_cb, void *progress_arg,
4904 got_cancel_cb cancel_cb, void *cancel_arg)
4906 const struct got_error *err;
4907 struct got_reference *commit_ref = NULL;
4908 struct collect_merged_paths_arg cmp_arg;
4909 char *fileindex_path;
4911 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4913 err = get_fileindex_path(&fileindex_path, worktree);
4914 if (err)
4915 return err;
4917 cmp_arg.progress_cb = progress_cb;
4918 cmp_arg.progress_arg = progress_arg;
4919 cmp_arg.merged_paths = merged_paths;
4920 err = merge_files(worktree, fileindex, fileindex_path,
4921 parent_commit_id, commit_id, repo, collect_merged_paths,
4922 &cmp_arg, cancel_cb, cancel_arg);
4923 if (commit_ref)
4924 got_ref_close(commit_ref);
4925 return err;
4928 const struct got_error *
4929 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4930 struct got_worktree *worktree, struct got_fileindex *fileindex,
4931 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4932 struct got_repository *repo,
4933 got_worktree_checkout_cb progress_cb, void *progress_arg,
4934 got_cancel_cb cancel_cb, void *cancel_arg)
4936 const struct got_error *err;
4937 char *commit_ref_name;
4939 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4940 if (err)
4941 return err;
4943 err = store_commit_id(commit_ref_name, commit_id, repo);
4944 if (err)
4945 goto done;
4947 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4948 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4949 progress_arg, cancel_cb, cancel_arg);
4950 done:
4951 free(commit_ref_name);
4952 return err;
4955 const struct got_error *
4956 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4957 struct got_worktree *worktree, struct got_fileindex *fileindex,
4958 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4959 struct got_repository *repo,
4960 got_worktree_checkout_cb progress_cb, void *progress_arg,
4961 got_cancel_cb cancel_cb, void *cancel_arg)
4963 const struct got_error *err;
4964 char *commit_ref_name;
4966 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4967 if (err)
4968 return err;
4970 err = store_commit_id(commit_ref_name, commit_id, repo);
4971 if (err)
4972 goto done;
4974 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4975 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4976 progress_arg, cancel_cb, cancel_arg);
4977 done:
4978 free(commit_ref_name);
4979 return err;
4982 static const struct got_error *
4983 rebase_commit(struct got_object_id **new_commit_id,
4984 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4985 struct got_worktree *worktree, struct got_fileindex *fileindex,
4986 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4987 const char *new_logmsg, struct got_repository *repo)
4989 const struct got_error *err, *sync_err;
4990 struct got_pathlist_head commitable_paths;
4991 struct collect_commitables_arg cc_arg;
4992 char *fileindex_path = NULL;
4993 struct got_reference *head_ref = NULL;
4994 struct got_object_id *head_commit_id = NULL;
4995 char *logmsg = NULL;
4997 TAILQ_INIT(&commitable_paths);
4998 *new_commit_id = NULL;
5000 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5002 err = get_fileindex_path(&fileindex_path, worktree);
5003 if (err)
5004 return err;
5006 cc_arg.commitable_paths = &commitable_paths;
5007 cc_arg.worktree = worktree;
5008 cc_arg.repo = repo;
5009 cc_arg.have_staged_files = 0;
5011 * If possible get the status of individual files directly to
5012 * avoid crawling the entire work tree once per rebased commit.
5013 * TODO: Ideally, merged_paths would contain a list of commitables
5014 * we could use so we could skip worktree_status() entirely.
5016 if (merged_paths) {
5017 struct got_pathlist_entry *pe;
5018 if (TAILQ_EMPTY(merged_paths)) {
5019 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5020 goto done;
5022 TAILQ_FOREACH(pe, merged_paths, entry) {
5023 err = worktree_status(worktree, pe->path, fileindex,
5024 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5025 0);
5026 if (err)
5027 goto done;
5029 } else {
5030 err = worktree_status(worktree, "", fileindex, repo,
5031 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5032 if (err)
5033 goto done;
5036 if (TAILQ_EMPTY(&commitable_paths)) {
5037 /* No-op change; commit will be elided. */
5038 err = got_ref_delete(commit_ref, repo);
5039 if (err)
5040 goto done;
5041 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5042 goto done;
5045 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5046 if (err)
5047 goto done;
5049 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5050 if (err)
5051 goto done;
5053 if (new_logmsg) {
5054 logmsg = strdup(new_logmsg);
5055 if (logmsg == NULL) {
5056 err = got_error_from_errno("strdup");
5057 goto done;
5059 } else {
5060 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5061 if (err)
5062 goto done;
5065 /* NB: commit_worktree will call free(logmsg) */
5066 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5067 worktree, got_object_commit_get_author(orig_commit),
5068 got_object_commit_get_committer(orig_commit),
5069 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5070 if (err)
5071 goto done;
5073 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5074 if (err)
5075 goto done;
5077 err = got_ref_delete(commit_ref, repo);
5078 if (err)
5079 goto done;
5081 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5082 fileindex, 0);
5083 sync_err = sync_fileindex(fileindex, fileindex_path);
5084 if (sync_err && err == NULL)
5085 err = sync_err;
5086 done:
5087 free(fileindex_path);
5088 free(head_commit_id);
5089 if (head_ref)
5090 got_ref_close(head_ref);
5091 if (err) {
5092 free(*new_commit_id);
5093 *new_commit_id = NULL;
5095 return err;
5098 const struct got_error *
5099 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5100 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5101 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5102 struct got_commit_object *orig_commit,
5103 struct got_object_id *orig_commit_id, struct got_repository *repo)
5105 const struct got_error *err;
5106 char *commit_ref_name;
5107 struct got_reference *commit_ref = NULL;
5108 struct got_object_id *commit_id = NULL;
5110 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5111 if (err)
5112 return err;
5114 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5115 if (err)
5116 goto done;
5117 err = got_ref_resolve(&commit_id, repo, commit_ref);
5118 if (err)
5119 goto done;
5120 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5121 err = got_error(GOT_ERR_REBASE_COMMITID);
5122 goto done;
5125 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5126 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5127 done:
5128 if (commit_ref)
5129 got_ref_close(commit_ref);
5130 free(commit_ref_name);
5131 free(commit_id);
5132 return err;
5135 const struct got_error *
5136 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5137 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5138 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5139 struct got_commit_object *orig_commit,
5140 struct got_object_id *orig_commit_id, const char *new_logmsg,
5141 struct got_repository *repo)
5143 const struct got_error *err;
5144 char *commit_ref_name;
5145 struct got_reference *commit_ref = NULL;
5146 struct got_object_id *commit_id = NULL;
5148 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5149 if (err)
5150 return err;
5152 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5153 if (err)
5154 goto done;
5155 err = got_ref_resolve(&commit_id, repo, commit_ref);
5156 if (err)
5157 goto done;
5158 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5159 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5160 goto done;
5163 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5164 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5165 done:
5166 if (commit_ref)
5167 got_ref_close(commit_ref);
5168 free(commit_ref_name);
5169 free(commit_id);
5170 return err;
5173 const struct got_error *
5174 got_worktree_rebase_postpone(struct got_worktree *worktree,
5175 struct got_fileindex *fileindex)
5177 if (fileindex)
5178 got_fileindex_free(fileindex);
5179 return lock_worktree(worktree, LOCK_SH);
5182 static const struct got_error *
5183 delete_ref(const char *name, struct got_repository *repo)
5185 const struct got_error *err;
5186 struct got_reference *ref;
5188 err = got_ref_open(&ref, repo, name, 0);
5189 if (err) {
5190 if (err->code == GOT_ERR_NOT_REF)
5191 return NULL;
5192 return err;
5195 err = got_ref_delete(ref, repo);
5196 got_ref_close(ref);
5197 return err;
5200 static const struct got_error *
5201 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5203 const struct got_error *err;
5204 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5205 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5207 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5208 if (err)
5209 goto done;
5210 err = delete_ref(tmp_branch_name, repo);
5211 if (err)
5212 goto done;
5214 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5215 if (err)
5216 goto done;
5217 err = delete_ref(new_base_branch_ref_name, repo);
5218 if (err)
5219 goto done;
5221 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5222 if (err)
5223 goto done;
5224 err = delete_ref(branch_ref_name, repo);
5225 if (err)
5226 goto done;
5228 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5229 if (err)
5230 goto done;
5231 err = delete_ref(commit_ref_name, repo);
5232 if (err)
5233 goto done;
5235 done:
5236 free(tmp_branch_name);
5237 free(new_base_branch_ref_name);
5238 free(branch_ref_name);
5239 free(commit_ref_name);
5240 return err;
5243 const struct got_error *
5244 got_worktree_rebase_complete(struct got_worktree *worktree,
5245 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5246 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5247 struct got_repository *repo)
5249 const struct got_error *err, *unlockerr;
5250 struct got_object_id *new_head_commit_id = NULL;
5252 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5253 if (err)
5254 return err;
5256 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5257 if (err)
5258 goto done;
5260 err = got_ref_write(rebased_branch, repo);
5261 if (err)
5262 goto done;
5264 err = got_worktree_set_head_ref(worktree, rebased_branch);
5265 if (err)
5266 goto done;
5268 err = delete_rebase_refs(worktree, repo);
5269 done:
5270 if (fileindex)
5271 got_fileindex_free(fileindex);
5272 free(new_head_commit_id);
5273 unlockerr = lock_worktree(worktree, LOCK_SH);
5274 if (unlockerr && err == NULL)
5275 err = unlockerr;
5276 return err;
5279 const struct got_error *
5280 got_worktree_rebase_abort(struct got_worktree *worktree,
5281 struct got_fileindex *fileindex, struct got_repository *repo,
5282 struct got_reference *new_base_branch,
5283 got_worktree_checkout_cb progress_cb, void *progress_arg)
5285 const struct got_error *err, *unlockerr, *sync_err;
5286 struct got_reference *resolved = NULL;
5287 struct got_object_id *commit_id = NULL;
5288 char *fileindex_path = NULL;
5289 struct revert_file_args rfa;
5290 struct got_object_id *tree_id = NULL;
5292 err = lock_worktree(worktree, LOCK_EX);
5293 if (err)
5294 return err;
5296 err = got_ref_open(&resolved, repo,
5297 got_ref_get_symref_target(new_base_branch), 0);
5298 if (err)
5299 goto done;
5301 err = got_worktree_set_head_ref(worktree, resolved);
5302 if (err)
5303 goto done;
5306 * XXX commits to the base branch could have happened while
5307 * we were busy rebasing; should we store the original commit ID
5308 * when rebase begins and read it back here?
5310 err = got_ref_resolve(&commit_id, repo, resolved);
5311 if (err)
5312 goto done;
5314 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5315 if (err)
5316 goto done;
5318 err = got_object_id_by_path(&tree_id, repo,
5319 worktree->base_commit_id, worktree->path_prefix);
5320 if (err)
5321 goto done;
5323 err = delete_rebase_refs(worktree, repo);
5324 if (err)
5325 goto done;
5327 err = get_fileindex_path(&fileindex_path, worktree);
5328 if (err)
5329 goto done;
5331 rfa.worktree = worktree;
5332 rfa.fileindex = fileindex;
5333 rfa.progress_cb = progress_cb;
5334 rfa.progress_arg = progress_arg;
5335 rfa.patch_cb = NULL;
5336 rfa.patch_arg = NULL;
5337 rfa.repo = repo;
5338 err = worktree_status(worktree, "", fileindex, repo,
5339 revert_file, &rfa, NULL, NULL, 0, 0);
5340 if (err)
5341 goto sync;
5343 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5344 repo, progress_cb, progress_arg, NULL, NULL);
5345 sync:
5346 sync_err = sync_fileindex(fileindex, fileindex_path);
5347 if (sync_err && err == NULL)
5348 err = sync_err;
5349 done:
5350 got_ref_close(resolved);
5351 free(tree_id);
5352 free(commit_id);
5353 if (fileindex)
5354 got_fileindex_free(fileindex);
5355 free(fileindex_path);
5357 unlockerr = lock_worktree(worktree, LOCK_SH);
5358 if (unlockerr && err == NULL)
5359 err = unlockerr;
5360 return err;
5363 const struct got_error *
5364 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5365 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5366 struct got_fileindex **fileindex, struct got_worktree *worktree,
5367 struct got_repository *repo)
5369 const struct got_error *err = NULL;
5370 char *tmp_branch_name = NULL;
5371 char *branch_ref_name = NULL;
5372 char *base_commit_ref_name = NULL;
5373 char *fileindex_path = NULL;
5374 struct check_rebase_ok_arg ok_arg;
5375 struct got_reference *wt_branch = NULL;
5376 struct got_reference *base_commit_ref = NULL;
5378 *tmp_branch = NULL;
5379 *branch_ref = NULL;
5380 *base_commit_id = NULL;
5381 *fileindex = NULL;
5383 err = lock_worktree(worktree, LOCK_EX);
5384 if (err)
5385 return err;
5387 err = open_fileindex(fileindex, &fileindex_path, worktree);
5388 if (err)
5389 goto done;
5391 ok_arg.worktree = worktree;
5392 ok_arg.repo = repo;
5393 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5394 &ok_arg);
5395 if (err)
5396 goto done;
5398 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5399 if (err)
5400 goto done;
5402 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5403 if (err)
5404 goto done;
5406 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5407 worktree);
5408 if (err)
5409 goto done;
5411 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5412 0);
5413 if (err)
5414 goto done;
5416 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5417 if (err)
5418 goto done;
5420 err = got_ref_write(*branch_ref, repo);
5421 if (err)
5422 goto done;
5424 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5425 worktree->base_commit_id);
5426 if (err)
5427 goto done;
5428 err = got_ref_write(base_commit_ref, repo);
5429 if (err)
5430 goto done;
5431 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5432 if (*base_commit_id == NULL) {
5433 err = got_error_from_errno("got_object_id_dup");
5434 goto done;
5437 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5438 worktree->base_commit_id);
5439 if (err)
5440 goto done;
5441 err = got_ref_write(*tmp_branch, repo);
5442 if (err)
5443 goto done;
5445 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5446 if (err)
5447 goto done;
5448 done:
5449 free(fileindex_path);
5450 free(tmp_branch_name);
5451 free(branch_ref_name);
5452 free(base_commit_ref_name);
5453 if (wt_branch)
5454 got_ref_close(wt_branch);
5455 if (err) {
5456 if (*branch_ref) {
5457 got_ref_close(*branch_ref);
5458 *branch_ref = NULL;
5460 if (*tmp_branch) {
5461 got_ref_close(*tmp_branch);
5462 *tmp_branch = NULL;
5464 free(*base_commit_id);
5465 if (*fileindex) {
5466 got_fileindex_free(*fileindex);
5467 *fileindex = NULL;
5469 lock_worktree(worktree, LOCK_SH);
5471 return err;
5474 const struct got_error *
5475 got_worktree_histedit_postpone(struct got_worktree *worktree,
5476 struct got_fileindex *fileindex)
5478 if (fileindex)
5479 got_fileindex_free(fileindex);
5480 return lock_worktree(worktree, LOCK_SH);
5483 const struct got_error *
5484 got_worktree_histedit_in_progress(int *in_progress,
5485 struct got_worktree *worktree)
5487 const struct got_error *err;
5488 char *tmp_branch_name = NULL;
5490 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5491 if (err)
5492 return err;
5494 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5495 free(tmp_branch_name);
5496 return NULL;
5499 const struct got_error *
5500 got_worktree_histedit_continue(struct got_object_id **commit_id,
5501 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5502 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5503 struct got_worktree *worktree, struct got_repository *repo)
5505 const struct got_error *err;
5506 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5507 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5508 struct got_reference *commit_ref = NULL;
5509 struct got_reference *base_commit_ref = NULL;
5510 char *fileindex_path = NULL;
5511 int have_staged_files = 0;
5513 *commit_id = NULL;
5514 *tmp_branch = NULL;
5515 *base_commit_id = NULL;
5516 *fileindex = NULL;
5518 err = lock_worktree(worktree, LOCK_EX);
5519 if (err)
5520 return err;
5522 err = open_fileindex(fileindex, &fileindex_path, worktree);
5523 if (err)
5524 goto done;
5526 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5527 &have_staged_files);
5528 if (err && err->code != GOT_ERR_CANCELLED)
5529 goto done;
5530 if (have_staged_files) {
5531 err = got_error(GOT_ERR_STAGED_PATHS);
5532 goto done;
5535 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5536 if (err)
5537 goto done;
5539 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5540 if (err)
5541 goto done;
5543 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5544 if (err)
5545 goto done;
5547 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5548 worktree);
5549 if (err)
5550 goto done;
5552 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5553 if (err)
5554 goto done;
5556 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5557 if (err)
5558 goto done;
5559 err = got_ref_resolve(commit_id, repo, commit_ref);
5560 if (err)
5561 goto done;
5563 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5564 if (err)
5565 goto done;
5566 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5567 if (err)
5568 goto done;
5570 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5571 if (err)
5572 goto done;
5573 done:
5574 free(commit_ref_name);
5575 free(branch_ref_name);
5576 free(fileindex_path);
5577 if (commit_ref)
5578 got_ref_close(commit_ref);
5579 if (base_commit_ref)
5580 got_ref_close(base_commit_ref);
5581 if (err) {
5582 free(*commit_id);
5583 *commit_id = NULL;
5584 free(*base_commit_id);
5585 *base_commit_id = NULL;
5586 if (*tmp_branch) {
5587 got_ref_close(*tmp_branch);
5588 *tmp_branch = NULL;
5590 if (*fileindex) {
5591 got_fileindex_free(*fileindex);
5592 *fileindex = NULL;
5594 lock_worktree(worktree, LOCK_EX);
5596 return err;
5599 static const struct got_error *
5600 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5602 const struct got_error *err;
5603 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5604 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5606 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5607 if (err)
5608 goto done;
5609 err = delete_ref(tmp_branch_name, repo);
5610 if (err)
5611 goto done;
5613 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5614 worktree);
5615 if (err)
5616 goto done;
5617 err = delete_ref(base_commit_ref_name, repo);
5618 if (err)
5619 goto done;
5621 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5622 if (err)
5623 goto done;
5624 err = delete_ref(branch_ref_name, repo);
5625 if (err)
5626 goto done;
5628 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5629 if (err)
5630 goto done;
5631 err = delete_ref(commit_ref_name, repo);
5632 if (err)
5633 goto done;
5634 done:
5635 free(tmp_branch_name);
5636 free(base_commit_ref_name);
5637 free(branch_ref_name);
5638 free(commit_ref_name);
5639 return err;
5642 const struct got_error *
5643 got_worktree_histedit_abort(struct got_worktree *worktree,
5644 struct got_fileindex *fileindex, struct got_repository *repo,
5645 struct got_reference *branch, struct got_object_id *base_commit_id,
5646 got_worktree_checkout_cb progress_cb, void *progress_arg)
5648 const struct got_error *err, *unlockerr, *sync_err;
5649 struct got_reference *resolved = NULL;
5650 char *fileindex_path = NULL;
5651 struct got_object_id *tree_id = NULL;
5652 struct revert_file_args rfa;
5654 err = lock_worktree(worktree, LOCK_EX);
5655 if (err)
5656 return err;
5658 err = got_ref_open(&resolved, repo,
5659 got_ref_get_symref_target(branch), 0);
5660 if (err)
5661 goto done;
5663 err = got_worktree_set_head_ref(worktree, resolved);
5664 if (err)
5665 goto done;
5667 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5668 if (err)
5669 goto done;
5671 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5672 worktree->path_prefix);
5673 if (err)
5674 goto done;
5676 err = delete_histedit_refs(worktree, repo);
5677 if (err)
5678 goto done;
5680 err = get_fileindex_path(&fileindex_path, worktree);
5681 if (err)
5682 goto done;
5684 rfa.worktree = worktree;
5685 rfa.fileindex = fileindex;
5686 rfa.progress_cb = progress_cb;
5687 rfa.progress_arg = progress_arg;
5688 rfa.patch_cb = NULL;
5689 rfa.patch_arg = NULL;
5690 rfa.repo = repo;
5691 err = worktree_status(worktree, "", fileindex, repo,
5692 revert_file, &rfa, NULL, NULL, 0, 0);
5693 if (err)
5694 goto sync;
5696 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5697 repo, progress_cb, progress_arg, NULL, NULL);
5698 sync:
5699 sync_err = sync_fileindex(fileindex, fileindex_path);
5700 if (sync_err && err == NULL)
5701 err = sync_err;
5702 done:
5703 got_ref_close(resolved);
5704 free(tree_id);
5705 free(fileindex_path);
5707 unlockerr = lock_worktree(worktree, LOCK_SH);
5708 if (unlockerr && err == NULL)
5709 err = unlockerr;
5710 return err;
5713 const struct got_error *
5714 got_worktree_histedit_complete(struct got_worktree *worktree,
5715 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5716 struct got_reference *edited_branch, struct got_repository *repo)
5718 const struct got_error *err, *unlockerr;
5719 struct got_object_id *new_head_commit_id = NULL;
5720 struct got_reference *resolved = NULL;
5722 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5723 if (err)
5724 return err;
5726 err = got_ref_open(&resolved, repo,
5727 got_ref_get_symref_target(edited_branch), 0);
5728 if (err)
5729 goto done;
5731 err = got_ref_change_ref(resolved, new_head_commit_id);
5732 if (err)
5733 goto done;
5735 err = got_ref_write(resolved, repo);
5736 if (err)
5737 goto done;
5739 err = got_worktree_set_head_ref(worktree, resolved);
5740 if (err)
5741 goto done;
5743 err = delete_histedit_refs(worktree, repo);
5744 done:
5745 if (fileindex)
5746 got_fileindex_free(fileindex);
5747 free(new_head_commit_id);
5748 unlockerr = lock_worktree(worktree, LOCK_SH);
5749 if (unlockerr && err == NULL)
5750 err = unlockerr;
5751 return err;
5754 const struct got_error *
5755 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5756 struct got_object_id *commit_id, struct got_repository *repo)
5758 const struct got_error *err;
5759 char *commit_ref_name;
5761 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5762 if (err)
5763 return err;
5765 err = store_commit_id(commit_ref_name, commit_id, repo);
5766 if (err)
5767 goto done;
5769 err = delete_ref(commit_ref_name, repo);
5770 done:
5771 free(commit_ref_name);
5772 return err;
5775 const struct got_error *
5776 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5777 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5778 struct got_worktree *worktree, const char *refname,
5779 struct got_repository *repo)
5781 const struct got_error *err = NULL;
5782 char *fileindex_path = NULL;
5783 struct check_rebase_ok_arg ok_arg;
5785 *fileindex = NULL;
5786 *branch_ref = NULL;
5787 *base_branch_ref = NULL;
5789 err = lock_worktree(worktree, LOCK_EX);
5790 if (err)
5791 return err;
5793 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5794 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5795 "cannot integrate a branch into itself; "
5796 "update -b or different branch name required");
5797 goto done;
5800 err = open_fileindex(fileindex, &fileindex_path, worktree);
5801 if (err)
5802 goto done;
5804 /* Preconditions are the same as for rebase. */
5805 ok_arg.worktree = worktree;
5806 ok_arg.repo = repo;
5807 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5808 &ok_arg);
5809 if (err)
5810 goto done;
5812 err = got_ref_open(branch_ref, repo, refname, 1);
5813 if (err)
5814 goto done;
5816 err = got_ref_open(base_branch_ref, repo,
5817 got_worktree_get_head_ref_name(worktree), 1);
5818 done:
5819 if (err) {
5820 if (*branch_ref) {
5821 got_ref_close(*branch_ref);
5822 *branch_ref = NULL;
5824 if (*base_branch_ref) {
5825 got_ref_close(*base_branch_ref);
5826 *base_branch_ref = NULL;
5828 if (*fileindex) {
5829 got_fileindex_free(*fileindex);
5830 *fileindex = NULL;
5832 lock_worktree(worktree, LOCK_SH);
5834 return err;
5837 const struct got_error *
5838 got_worktree_integrate_continue(struct got_worktree *worktree,
5839 struct got_fileindex *fileindex, struct got_repository *repo,
5840 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5841 got_worktree_checkout_cb progress_cb, void *progress_arg,
5842 got_cancel_cb cancel_cb, void *cancel_arg)
5844 const struct got_error *err = NULL, *sync_err, *unlockerr;
5845 char *fileindex_path = NULL;
5846 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5848 err = get_fileindex_path(&fileindex_path, worktree);
5849 if (err)
5850 goto done;
5852 err = got_ref_resolve(&commit_id, repo, branch_ref);
5853 if (err)
5854 goto done;
5856 err = got_object_id_by_path(&tree_id, repo, commit_id,
5857 worktree->path_prefix);
5858 if (err)
5859 goto done;
5861 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5862 if (err)
5863 goto done;
5865 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5866 progress_cb, progress_arg, cancel_cb, cancel_arg);
5867 if (err)
5868 goto sync;
5870 err = got_ref_change_ref(base_branch_ref, commit_id);
5871 if (err)
5872 goto sync;
5874 err = got_ref_write(base_branch_ref, repo);
5875 sync:
5876 sync_err = sync_fileindex(fileindex, fileindex_path);
5877 if (sync_err && err == NULL)
5878 err = sync_err;
5880 done:
5881 unlockerr = got_ref_unlock(branch_ref);
5882 if (unlockerr && err == NULL)
5883 err = unlockerr;
5884 got_ref_close(branch_ref);
5886 unlockerr = got_ref_unlock(base_branch_ref);
5887 if (unlockerr && err == NULL)
5888 err = unlockerr;
5889 got_ref_close(base_branch_ref);
5891 got_fileindex_free(fileindex);
5892 free(fileindex_path);
5893 free(tree_id);
5895 unlockerr = lock_worktree(worktree, LOCK_SH);
5896 if (unlockerr && err == NULL)
5897 err = unlockerr;
5898 return err;
5901 const struct got_error *
5902 got_worktree_integrate_abort(struct got_worktree *worktree,
5903 struct got_fileindex *fileindex, struct got_repository *repo,
5904 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5906 const struct got_error *err = NULL, *unlockerr = NULL;
5908 got_fileindex_free(fileindex);
5910 err = lock_worktree(worktree, LOCK_SH);
5912 unlockerr = got_ref_unlock(branch_ref);
5913 if (unlockerr && err == NULL)
5914 err = unlockerr;
5915 got_ref_close(branch_ref);
5917 unlockerr = got_ref_unlock(base_branch_ref);
5918 if (unlockerr && err == NULL)
5919 err = unlockerr;
5920 got_ref_close(base_branch_ref);
5922 return err;
5925 struct check_stage_ok_arg {
5926 struct got_object_id *head_commit_id;
5927 struct got_worktree *worktree;
5928 struct got_fileindex *fileindex;
5929 struct got_repository *repo;
5930 int have_changes;
5933 const struct got_error *
5934 check_stage_ok(void *arg, unsigned char status,
5935 unsigned char staged_status, const char *relpath,
5936 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5937 struct got_object_id *commit_id)
5939 struct check_stage_ok_arg *a = arg;
5940 const struct got_error *err = NULL;
5941 struct got_fileindex_entry *ie;
5942 struct got_object_id base_commit_id;
5943 struct got_object_id *base_commit_idp = NULL;
5944 char *in_repo_path = NULL, *p;
5946 if (status == GOT_STATUS_UNVERSIONED ||
5947 status == GOT_STATUS_NO_CHANGE)
5948 return NULL;
5949 if (status == GOT_STATUS_NONEXISTENT)
5950 return got_error_set_errno(ENOENT, relpath);
5952 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5953 if (ie == NULL)
5954 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5956 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5957 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5958 relpath) == -1)
5959 return got_error_from_errno("asprintf");
5961 if (got_fileindex_entry_has_commit(ie)) {
5962 memcpy(base_commit_id.sha1, ie->commit_sha1,
5963 SHA1_DIGEST_LENGTH);
5964 base_commit_idp = &base_commit_id;
5967 if (status == GOT_STATUS_CONFLICT) {
5968 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5969 goto done;
5970 } else if (status != GOT_STATUS_ADD &&
5971 status != GOT_STATUS_MODIFY &&
5972 status != GOT_STATUS_DELETE) {
5973 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5974 goto done;
5977 a->have_changes = 1;
5979 p = in_repo_path;
5980 while (p[0] == '/')
5981 p++;
5982 err = check_out_of_date(p, status, staged_status,
5983 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5984 GOT_ERR_STAGE_OUT_OF_DATE);
5985 done:
5986 free(in_repo_path);
5987 return err;
5990 struct stage_path_arg {
5991 struct got_worktree *worktree;
5992 struct got_fileindex *fileindex;
5993 struct got_repository *repo;
5994 got_worktree_status_cb status_cb;
5995 void *status_arg;
5996 got_worktree_patch_cb patch_cb;
5997 void *patch_arg;
5998 int staged_something;
6001 static const struct got_error *
6002 stage_path(void *arg, unsigned char status,
6003 unsigned char staged_status, const char *relpath,
6004 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6005 struct got_object_id *commit_id)
6007 struct stage_path_arg *a = arg;
6008 const struct got_error *err = NULL;
6009 struct got_fileindex_entry *ie;
6010 char *ondisk_path = NULL, *path_content = NULL;
6011 uint32_t stage;
6012 struct got_object_id *new_staged_blob_id = NULL;
6014 if (status == GOT_STATUS_UNVERSIONED)
6015 return NULL;
6017 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6018 if (ie == NULL)
6019 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6021 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6022 relpath)== -1)
6023 return got_error_from_errno("asprintf");
6025 switch (status) {
6026 case GOT_STATUS_ADD:
6027 case GOT_STATUS_MODIFY:
6028 if (a->patch_cb) {
6029 if (status == GOT_STATUS_ADD) {
6030 int choice = GOT_PATCH_CHOICE_NONE;
6031 err = (*a->patch_cb)(&choice, a->patch_arg,
6032 status, ie->path, NULL, 1, 1);
6033 if (err)
6034 break;
6035 if (choice != GOT_PATCH_CHOICE_YES)
6036 break;
6037 } else {
6038 err = create_patched_content(&path_content, 0,
6039 staged_blob_id ? staged_blob_id : blob_id,
6040 ondisk_path, ie->path, a->repo,
6041 a->patch_cb, a->patch_arg);
6042 if (err || path_content == NULL)
6043 break;
6046 err = got_object_blob_create(&new_staged_blob_id,
6047 path_content ? path_content : ondisk_path, a->repo);
6048 if (err)
6049 break;
6050 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6051 SHA1_DIGEST_LENGTH);
6052 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6053 stage = GOT_FILEIDX_STAGE_ADD;
6054 else
6055 stage = GOT_FILEIDX_STAGE_MODIFY;
6056 got_fileindex_entry_stage_set(ie, stage);
6057 a->staged_something = 1;
6058 if (a->status_cb == NULL)
6059 break;
6060 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6061 get_staged_status(ie), relpath, blob_id,
6062 new_staged_blob_id, NULL);
6063 break;
6064 case GOT_STATUS_DELETE:
6065 if (staged_status == GOT_STATUS_DELETE)
6066 break;
6067 if (a->patch_cb) {
6068 int choice = GOT_PATCH_CHOICE_NONE;
6069 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6070 ie->path, NULL, 1, 1);
6071 if (err)
6072 break;
6073 if (choice == GOT_PATCH_CHOICE_NO)
6074 break;
6075 if (choice != GOT_PATCH_CHOICE_YES) {
6076 err = got_error(GOT_ERR_PATCH_CHOICE);
6077 break;
6080 stage = GOT_FILEIDX_STAGE_DELETE;
6081 got_fileindex_entry_stage_set(ie, stage);
6082 a->staged_something = 1;
6083 if (a->status_cb == NULL)
6084 break;
6085 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6086 get_staged_status(ie), relpath, NULL, NULL, NULL);
6087 break;
6088 case GOT_STATUS_NO_CHANGE:
6089 break;
6090 case GOT_STATUS_CONFLICT:
6091 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6092 break;
6093 case GOT_STATUS_NONEXISTENT:
6094 err = got_error_set_errno(ENOENT, relpath);
6095 break;
6096 default:
6097 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6098 break;
6101 if (path_content && unlink(path_content) == -1 && err == NULL)
6102 err = got_error_from_errno2("unlink", path_content);
6103 free(path_content);
6104 free(ondisk_path);
6105 free(new_staged_blob_id);
6106 return err;
6109 const struct got_error *
6110 got_worktree_stage(struct got_worktree *worktree,
6111 struct got_pathlist_head *paths,
6112 got_worktree_status_cb status_cb, void *status_arg,
6113 got_worktree_patch_cb patch_cb, void *patch_arg,
6114 struct got_repository *repo)
6116 const struct got_error *err = NULL, *sync_err, *unlockerr;
6117 struct got_pathlist_entry *pe;
6118 struct got_fileindex *fileindex = NULL;
6119 char *fileindex_path = NULL;
6120 struct got_reference *head_ref = NULL;
6121 struct got_object_id *head_commit_id = NULL;
6122 struct check_stage_ok_arg oka;
6123 struct stage_path_arg spa;
6125 err = lock_worktree(worktree, LOCK_EX);
6126 if (err)
6127 return err;
6129 err = got_ref_open(&head_ref, repo,
6130 got_worktree_get_head_ref_name(worktree), 0);
6131 if (err)
6132 goto done;
6133 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6134 if (err)
6135 goto done;
6136 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6137 if (err)
6138 goto done;
6140 /* Check pre-conditions before staging anything. */
6141 oka.head_commit_id = head_commit_id;
6142 oka.worktree = worktree;
6143 oka.fileindex = fileindex;
6144 oka.repo = repo;
6145 oka.have_changes = 0;
6146 TAILQ_FOREACH(pe, paths, entry) {
6147 err = worktree_status(worktree, pe->path, fileindex, repo,
6148 check_stage_ok, &oka, NULL, NULL, 0, 0);
6149 if (err)
6150 goto done;
6152 if (!oka.have_changes) {
6153 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6154 goto done;
6157 spa.worktree = worktree;
6158 spa.fileindex = fileindex;
6159 spa.repo = repo;
6160 spa.patch_cb = patch_cb;
6161 spa.patch_arg = patch_arg;
6162 spa.status_cb = status_cb;
6163 spa.status_arg = status_arg;
6164 spa.staged_something = 0;
6165 TAILQ_FOREACH(pe, paths, entry) {
6166 err = worktree_status(worktree, pe->path, fileindex, repo,
6167 stage_path, &spa, NULL, NULL, 0, 0);
6168 if (err)
6169 goto done;
6171 if (!spa.staged_something) {
6172 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6173 goto done;
6176 sync_err = sync_fileindex(fileindex, fileindex_path);
6177 if (sync_err && err == NULL)
6178 err = sync_err;
6179 done:
6180 if (head_ref)
6181 got_ref_close(head_ref);
6182 free(head_commit_id);
6183 free(fileindex_path);
6184 if (fileindex)
6185 got_fileindex_free(fileindex);
6186 unlockerr = lock_worktree(worktree, LOCK_SH);
6187 if (unlockerr && err == NULL)
6188 err = unlockerr;
6189 return err;
6192 struct unstage_path_arg {
6193 struct got_worktree *worktree;
6194 struct got_fileindex *fileindex;
6195 struct got_repository *repo;
6196 got_worktree_checkout_cb progress_cb;
6197 void *progress_arg;
6198 got_worktree_patch_cb patch_cb;
6199 void *patch_arg;
6202 static const struct got_error *
6203 create_unstaged_content(char **path_unstaged_content,
6204 char **path_new_staged_content, struct got_object_id *blob_id,
6205 struct got_object_id *staged_blob_id, const char *relpath,
6206 struct got_repository *repo,
6207 got_worktree_patch_cb patch_cb, void *patch_arg)
6209 const struct got_error *err;
6210 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6211 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6212 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6213 struct stat sb1, sb2;
6214 struct got_diff_changes *changes = NULL;
6215 struct got_diff_state *ds = NULL;
6216 struct got_diff_args *args = NULL;
6217 struct got_diff_change *change;
6218 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6219 int have_content = 0, have_rejected_content = 0;
6221 *path_unstaged_content = NULL;
6222 *path_new_staged_content = NULL;
6224 err = got_object_id_str(&label1, blob_id);
6225 if (err)
6226 return err;
6227 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6228 if (err)
6229 goto done;
6231 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6232 if (err)
6233 goto done;
6235 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6236 if (err)
6237 goto done;
6239 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6240 if (err)
6241 goto done;
6243 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6244 if (err)
6245 goto done;
6247 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6248 if (err)
6249 goto done;
6251 if (stat(path1, &sb1) == -1) {
6252 err = got_error_from_errno2("stat", path1);
6253 goto done;
6256 if (stat(path2, &sb2) == -1) {
6257 err = got_error_from_errno2("stat", path2);
6258 goto done;
6261 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6262 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6263 if (err)
6264 goto done;
6266 err = got_opentemp_named(path_unstaged_content, &outfile,
6267 "got-unstaged-content");
6268 if (err)
6269 goto done;
6270 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6271 "got-new-staged-content");
6272 if (err)
6273 goto done;
6275 if (fseek(f1, 0L, SEEK_SET) == -1) {
6276 err = got_ferror(f1, GOT_ERR_IO);
6277 goto done;
6279 if (fseek(f2, 0L, SEEK_SET) == -1) {
6280 err = got_ferror(f2, GOT_ERR_IO);
6281 goto done;
6283 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6284 int choice;
6285 err = apply_or_reject_change(&choice, change, ++n,
6286 changes->nchanges, ds, args, diff_flags, relpath,
6287 f1, f2, &line_cur1, &line_cur2,
6288 outfile, rejectfile, patch_cb, patch_arg);
6289 if (err)
6290 goto done;
6291 if (choice == GOT_PATCH_CHOICE_YES)
6292 have_content = 1;
6293 else
6294 have_rejected_content = 1;
6295 if (choice == GOT_PATCH_CHOICE_QUIT)
6296 break;
6298 if (have_content || have_rejected_content)
6299 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6300 outfile, rejectfile);
6301 done:
6302 free(label1);
6303 if (blob)
6304 got_object_blob_close(blob);
6305 if (staged_blob)
6306 got_object_blob_close(staged_blob);
6307 if (f1 && fclose(f1) == EOF && err == NULL)
6308 err = got_error_from_errno2("fclose", path1);
6309 if (f2 && fclose(f2) == EOF && err == NULL)
6310 err = got_error_from_errno2("fclose", path2);
6311 if (outfile && fclose(outfile) == EOF && err == NULL)
6312 err = got_error_from_errno2("fclose", *path_unstaged_content);
6313 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6314 err = got_error_from_errno2("fclose", *path_new_staged_content);
6315 if (path1 && unlink(path1) == -1 && err == NULL)
6316 err = got_error_from_errno2("unlink", path1);
6317 if (path2 && unlink(path2) == -1 && err == NULL)
6318 err = got_error_from_errno2("unlink", path2);
6319 if (err || !have_content) {
6320 if (*path_unstaged_content &&
6321 unlink(*path_unstaged_content) == -1 && err == NULL)
6322 err = got_error_from_errno2("unlink",
6323 *path_unstaged_content);
6324 free(*path_unstaged_content);
6325 *path_unstaged_content = NULL;
6327 if (err || !have_rejected_content) {
6328 if (*path_new_staged_content &&
6329 unlink(*path_new_staged_content) == -1 && err == NULL)
6330 err = got_error_from_errno2("unlink",
6331 *path_new_staged_content);
6332 free(*path_new_staged_content);
6333 *path_new_staged_content = NULL;
6335 free(args);
6336 if (ds) {
6337 got_diff_state_free(ds);
6338 free(ds);
6340 if (changes)
6341 got_diff_free_changes(changes);
6342 free(path1);
6343 free(path2);
6344 return err;
6347 static const struct got_error *
6348 unstage_path(void *arg, unsigned char status,
6349 unsigned char staged_status, const char *relpath,
6350 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6351 struct got_object_id *commit_id)
6353 const struct got_error *err = NULL;
6354 struct unstage_path_arg *a = arg;
6355 struct got_fileindex_entry *ie;
6356 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6357 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6358 char *path_new_staged_content = NULL;
6359 char *id_str = NULL, *label_orig = NULL;
6360 int local_changes_subsumed;
6361 struct stat sb;
6363 if (staged_status != GOT_STATUS_ADD &&
6364 staged_status != GOT_STATUS_MODIFY &&
6365 staged_status != GOT_STATUS_DELETE)
6366 return NULL;
6368 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6369 if (ie == NULL)
6370 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6372 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6373 == -1)
6374 return got_error_from_errno("asprintf");
6376 err = got_object_id_str(&id_str,
6377 commit_id ? commit_id : a->worktree->base_commit_id);
6378 if (err)
6379 goto done;
6380 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6381 id_str) == -1) {
6382 err = got_error_from_errno("asprintf");
6383 goto done;
6386 switch (staged_status) {
6387 case GOT_STATUS_MODIFY:
6388 err = got_object_open_as_blob(&blob_base, a->repo,
6389 blob_id, 8192);
6390 if (err)
6391 break;
6392 /* fall through */
6393 case GOT_STATUS_ADD:
6394 if (a->patch_cb) {
6395 if (staged_status == GOT_STATUS_ADD) {
6396 int choice = GOT_PATCH_CHOICE_NONE;
6397 err = (*a->patch_cb)(&choice, a->patch_arg,
6398 staged_status, ie->path, NULL, 1, 1);
6399 if (err)
6400 break;
6401 if (choice != GOT_PATCH_CHOICE_YES)
6402 break;
6403 } else {
6404 err = create_unstaged_content(
6405 &path_unstaged_content,
6406 &path_new_staged_content, blob_id,
6407 staged_blob_id, ie->path, a->repo,
6408 a->patch_cb, a->patch_arg);
6409 if (err || path_unstaged_content == NULL)
6410 break;
6411 if (path_new_staged_content) {
6412 err = got_object_blob_create(
6413 &staged_blob_id,
6414 path_new_staged_content,
6415 a->repo);
6416 if (err)
6417 break;
6418 memcpy(ie->staged_blob_sha1,
6419 staged_blob_id->sha1,
6420 SHA1_DIGEST_LENGTH);
6422 err = merge_file(&local_changes_subsumed,
6423 a->worktree, blob_base, ondisk_path,
6424 relpath, got_fileindex_perms_to_st(ie),
6425 path_unstaged_content, label_orig,
6426 "unstaged", a->repo, a->progress_cb,
6427 a->progress_arg);
6428 if (err == NULL &&
6429 path_new_staged_content == NULL)
6430 got_fileindex_entry_stage_set(ie,
6431 GOT_FILEIDX_STAGE_NONE);
6432 break; /* Done with this file. */
6435 err = got_object_open_as_blob(&blob_staged, a->repo,
6436 staged_blob_id, 8192);
6437 if (err)
6438 break;
6439 err = merge_blob(&local_changes_subsumed, a->worktree,
6440 blob_base, ondisk_path, relpath,
6441 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6442 commit_id ? commit_id : a->worktree->base_commit_id,
6443 a->repo, a->progress_cb, a->progress_arg);
6444 if (err == NULL)
6445 got_fileindex_entry_stage_set(ie,
6446 GOT_FILEIDX_STAGE_NONE);
6447 break;
6448 case GOT_STATUS_DELETE:
6449 if (a->patch_cb) {
6450 int choice = GOT_PATCH_CHOICE_NONE;
6451 err = (*a->patch_cb)(&choice, a->patch_arg,
6452 staged_status, ie->path, NULL, 1, 1);
6453 if (err)
6454 break;
6455 if (choice == GOT_PATCH_CHOICE_NO)
6456 break;
6457 if (choice != GOT_PATCH_CHOICE_YES) {
6458 err = got_error(GOT_ERR_PATCH_CHOICE);
6459 break;
6462 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6463 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6464 if (err)
6465 break;
6466 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6467 break;
6469 done:
6470 free(ondisk_path);
6471 if (path_unstaged_content &&
6472 unlink(path_unstaged_content) == -1 && err == NULL)
6473 err = got_error_from_errno2("unlink", path_unstaged_content);
6474 if (path_new_staged_content &&
6475 unlink(path_new_staged_content) == -1 && err == NULL)
6476 err = got_error_from_errno2("unlink", path_new_staged_content);
6477 free(path_unstaged_content);
6478 free(path_new_staged_content);
6479 if (blob_base)
6480 got_object_blob_close(blob_base);
6481 if (blob_staged)
6482 got_object_blob_close(blob_staged);
6483 free(id_str);
6484 free(label_orig);
6485 return err;
6488 const struct got_error *
6489 got_worktree_unstage(struct got_worktree *worktree,
6490 struct got_pathlist_head *paths,
6491 got_worktree_checkout_cb progress_cb, void *progress_arg,
6492 got_worktree_patch_cb patch_cb, void *patch_arg,
6493 struct got_repository *repo)
6495 const struct got_error *err = NULL, *sync_err, *unlockerr;
6496 struct got_pathlist_entry *pe;
6497 struct got_fileindex *fileindex = NULL;
6498 char *fileindex_path = NULL;
6499 struct unstage_path_arg upa;
6501 err = lock_worktree(worktree, LOCK_EX);
6502 if (err)
6503 return err;
6505 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6506 if (err)
6507 goto done;
6509 upa.worktree = worktree;
6510 upa.fileindex = fileindex;
6511 upa.repo = repo;
6512 upa.progress_cb = progress_cb;
6513 upa.progress_arg = progress_arg;
6514 upa.patch_cb = patch_cb;
6515 upa.patch_arg = patch_arg;
6516 TAILQ_FOREACH(pe, paths, entry) {
6517 err = worktree_status(worktree, pe->path, fileindex, repo,
6518 unstage_path, &upa, NULL, NULL, 0, 0);
6519 if (err)
6520 goto done;
6523 sync_err = sync_fileindex(fileindex, fileindex_path);
6524 if (sync_err && err == NULL)
6525 err = sync_err;
6526 done:
6527 free(fileindex_path);
6528 if (fileindex)
6529 got_fileindex_free(fileindex);
6530 unlockerr = lock_worktree(worktree, LOCK_SH);
6531 if (unlockerr && err == NULL)
6532 err = unlockerr;
6533 return err;