Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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 = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
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_path(abspath, 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("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", 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 (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", 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 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 static const struct got_error *
834 update_symlink(const char *ondisk_path, const char *target_path,
835 size_t target_len)
837 /* This is not atomic but matches what 'ln -sf' does. */
838 if (unlink(ondisk_path) == -1)
839 return got_error_from_errno2("unlink", ondisk_path);
840 if (symlink(target_path, ondisk_path) == -1)
841 return got_error_from_errno3("symlink", target_path,
842 ondisk_path);
843 return NULL;
846 /*
847 * Merge a symlink into the work tree, where blob_orig acts as the common
848 * ancestor, blob_deriv acts as the first derived version, and the symlink
849 * on disk acts as the second derived version.
850 * Assume that contents of both blobs represent symlinks.
851 */
852 static const struct got_error *
853 merge_symlink(struct got_worktree *worktree,
854 struct got_blob_object *blob_orig, const char *ondisk_path,
855 const char *path, uint16_t st_mode, const char *label_orig,
856 struct got_blob_object *blob_deriv,
857 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
858 got_worktree_checkout_cb progress_cb, void *progress_arg)
860 const struct got_error *err = NULL;
861 char *ancestor_target = NULL, *deriv_target = NULL;
862 struct stat sb;
863 ssize_t ondisk_len;
864 char ondisk_target[PATH_MAX];
866 if (lstat(ondisk_path, &sb) == -1)
867 return got_error_from_errno2("lstat", ondisk_path);
869 if (!S_ISLNK(sb.st_mode)) {
870 /* TODO symlink is obstructed; do something */
871 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
874 ondisk_len = readlink(ondisk_path, ondisk_target,
875 sizeof(ondisk_target));
876 if (ondisk_len == -1) {
877 err = got_error_from_errno2("readlink",
878 ondisk_path);
879 goto done;
882 if (blob_orig) {
883 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
884 if (err)
885 goto done;
888 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
889 if (err)
890 goto done;
892 if (ancestor_target && (ondisk_len != strlen(ancestor_target) ||
893 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0)) {
894 /*
895 * The symlink has changed on-disk (second derived version).
896 * Keep that change and discard the incoming change (first
897 * derived version).
898 * TODO: Need tree-conflict resolution to handle this.
899 */
900 err = (*progress_cb)(progress_arg, GOT_STATUS_OBSTRUCTED,
901 path);
902 } else if (ondisk_len == strlen(deriv_target) &&
903 memcmp(ondisk_target, deriv_target, ondisk_len) == 0) {
904 /* Both versions made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
906 } else {
907 /* Apply the incoming change. */
908 err = update_symlink(ondisk_path, deriv_target,
909 strlen(deriv_target));
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
914 done:
915 free(ancestor_target);
916 free(deriv_target);
917 return err;
920 /*
921 * Perform a 3-way merge where blob_orig acts as the common ancestor,
922 * blob_deriv acts as the first derived version, and the file on disk
923 * acts as the second derived version.
924 */
925 static const struct got_error *
926 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
927 struct got_blob_object *blob_orig, const char *ondisk_path,
928 const char *path, uint16_t st_mode, const char *label_orig,
929 struct got_blob_object *blob_deriv,
930 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
931 got_worktree_checkout_cb progress_cb, void *progress_arg)
933 const struct got_error *err = NULL;
934 FILE *f_deriv = NULL;
935 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
936 char *label_deriv = NULL, *parent;
938 *local_changes_subsumed = 0;
940 parent = dirname(ondisk_path);
941 if (parent == NULL)
942 return got_error_from_errno2("dirname", ondisk_path);
944 free(base_path);
945 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
946 err = got_error_from_errno("asprintf");
947 base_path = NULL;
948 goto done;
951 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
952 if (err)
953 goto done;
954 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
955 blob_deriv);
956 if (err)
957 goto done;
959 err = got_object_id_str(&id_str, deriv_base_commit_id);
960 if (err)
961 goto done;
962 if (asprintf(&label_deriv, "%s: commit %s",
963 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
964 err = got_error_from_errno("asprintf");
965 goto done;
968 err = merge_file(local_changes_subsumed, worktree, blob_orig,
969 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
970 label_deriv, repo, progress_cb, progress_arg);
971 done:
972 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
973 err = got_error_from_errno("fclose");
974 free(base_path);
975 if (blob_deriv_path) {
976 unlink(blob_deriv_path);
977 free(blob_deriv_path);
979 free(id_str);
980 free(label_deriv);
981 return err;
984 static const struct got_error *
985 create_fileindex_entry(struct got_fileindex *fileindex,
986 struct got_object_id *base_commit_id, const char *ondisk_path,
987 const char *path, struct got_object_id *blob_id)
989 const struct got_error *err = NULL;
990 struct got_fileindex_entry *new_ie;
992 err = got_fileindex_entry_alloc(&new_ie, path);
993 if (err)
994 return err;
996 err = got_fileindex_entry_update(new_ie, ondisk_path,
997 blob_id->sha1, base_commit_id->sha1, 1);
998 if (err)
999 goto done;
1001 err = got_fileindex_entry_add(fileindex, new_ie);
1002 done:
1003 if (err)
1004 got_fileindex_entry_free(new_ie);
1005 return err;
1008 static mode_t
1009 get_ondisk_perms(int executable, mode_t st_mode)
1011 mode_t xbits = S_IXUSR;
1013 if (executable) {
1014 /* Map read bits to execute bits. */
1015 if (st_mode & S_IRGRP)
1016 xbits |= S_IXGRP;
1017 if (st_mode & S_IROTH)
1018 xbits |= S_IXOTH;
1019 return st_mode | xbits;
1022 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1025 /* forward declaration */
1026 static const struct got_error *
1027 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1028 const char *path, mode_t te_mode, mode_t st_mode,
1029 struct got_blob_object *blob, int restoring_missing_file,
1030 int reverting_versioned_file, int installing_bad_symlink,
1031 struct got_repository *repo,
1032 got_worktree_checkout_cb progress_cb, void *progress_arg);
1035 * This function assumes that the provided symlink target points at a
1036 * safe location in the work tree!
1038 static const struct got_error *
1039 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1040 size_t target_len)
1042 const struct got_error *err = NULL;
1043 ssize_t elen;
1044 char etarget[PATH_MAX];
1045 int fd;
1048 * "Bad" symlinks (those pointing outside the work tree or into the
1049 * .got directory) are installed in the work tree as a regular file
1050 * which contains the bad symlink target path.
1051 * The new symlink target has already been checked for safety by our
1052 * caller. If we can successfully open a regular file then we simply
1053 * replace this file with a symlink below.
1055 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1056 if (fd == -1) {
1057 if (errno != ELOOP)
1058 return got_error_from_errno2("open", ondisk_path);
1060 /* We are updating an existing on-disk symlink. */
1061 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1062 if (elen == -1)
1063 return got_error_from_errno2("readlink", ondisk_path);
1065 if (elen == target_len &&
1066 memcmp(etarget, target_path, target_len) == 0)
1067 return NULL; /* nothing to do */
1070 err = update_symlink(ondisk_path, target_path, target_len);
1071 if (fd != -1 && close(fd) == -1 && err == NULL)
1072 err = got_error_from_errno2("close", ondisk_path);
1073 return err;
1076 static const struct got_error *
1077 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1078 const char *path, mode_t te_mode, mode_t st_mode,
1079 struct got_blob_object *blob, int restoring_missing_file,
1080 int reverting_versioned_file, struct got_repository *repo,
1081 got_worktree_checkout_cb progress_cb, void *progress_arg)
1083 const struct got_error *err = NULL;
1084 char target_path[PATH_MAX];
1085 size_t len, target_len = 0;
1086 char *resolved_path = NULL, *abspath = NULL;
1087 char *path_got = NULL;
1088 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1089 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1092 * Blob object content specifies the target path of the link.
1093 * If a symbolic link cannot be installed we instead create
1094 * a regular file which contains the link target path stored
1095 * in the blob object.
1097 do {
1098 err = got_object_blob_read_block(&len, blob);
1099 if (len + target_len >= sizeof(target_path)) {
1100 /* Path too long; install as a regular file. */
1101 got_object_blob_rewind(blob);
1102 return install_blob(worktree, ondisk_path, path,
1103 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1104 restoring_missing_file, reverting_versioned_file,
1105 1, repo, progress_cb, progress_arg);
1107 if (len > 0) {
1108 /* Skip blob object header first time around. */
1109 memcpy(target_path + target_len, buf + hdrlen,
1110 len - hdrlen);
1111 target_len += len - hdrlen;
1112 hdrlen = 0;
1114 } while (len != 0);
1115 target_path[target_len] = '\0';
1118 * Relative symlink target lookup should begin at the directory
1119 * in which the blob object is being installed.
1121 if (!got_path_is_absolute(target_path)) {
1122 char *parent = dirname(ondisk_path);
1123 if (parent == NULL) {
1124 err = got_error_from_errno2("dirname", ondisk_path);
1125 goto done;
1127 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1128 err = got_error_from_errno("asprintf");
1129 goto done;
1134 * unveil(2) restricts our view of paths in the filesystem.
1135 * ENOENT will occur if a link target path does not exist or
1136 * if it points outside our unveiled path space.
1138 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1139 if (resolved_path == NULL) {
1140 if (errno != ENOENT) {
1141 err = got_error_from_errno2("realpath", target_path);
1142 goto done;
1146 /* Only allow symlinks pointing at paths within the work tree. */
1147 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1148 abspath : target_path), worktree->root_path,
1149 strlen(worktree->root_path))) {
1150 /* install as a regular file */
1151 got_object_blob_rewind(blob);
1152 err = install_blob(worktree, ondisk_path, path,
1153 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1154 restoring_missing_file, reverting_versioned_file, 1,
1155 repo, progress_cb, progress_arg);
1156 goto done;
1159 /* Do not allow symlinks pointing into the .got directory. */
1160 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1161 GOT_WORKTREE_GOT_DIR) == -1) {
1162 err = got_error_from_errno("asprintf");
1163 goto done;
1165 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1166 abspath : target_path), path_got, strlen(path_got))) {
1167 /* install as a regular file */
1168 got_object_blob_rewind(blob);
1169 err = install_blob(worktree, ondisk_path, path,
1170 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1171 restoring_missing_file, reverting_versioned_file, 1,
1172 repo, progress_cb, progress_arg);
1173 goto done;
1176 if (symlink(target_path, ondisk_path) == -1) {
1177 if (errno == EEXIST) {
1178 err = replace_existing_symlink(ondisk_path,
1179 target_path, target_len);
1180 if (err)
1181 goto done;
1182 if (progress_cb) {
1183 err = (*progress_cb)(progress_arg,
1184 GOT_STATUS_UPDATE, path);
1186 goto done; /* Nothing else to do. */
1189 if (errno == ENOENT) {
1190 char *parent = dirname(ondisk_path);
1191 if (parent == NULL) {
1192 err = got_error_from_errno2("dirname",
1193 ondisk_path);
1194 goto done;
1196 err = add_dir_on_disk(worktree, parent);
1197 if (err)
1198 goto done;
1200 * Retry, and fall through to error handling
1201 * below if this second attempt fails.
1203 if (symlink(target_path, ondisk_path) != -1) {
1204 err = NULL; /* success */
1205 goto done;
1209 /* Handle errors from first or second creation attempt. */
1210 if (errno == ENAMETOOLONG) {
1211 /* bad target path; install as a regular file */
1212 got_object_blob_rewind(blob);
1213 err = install_blob(worktree, ondisk_path, path,
1214 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1215 restoring_missing_file, reverting_versioned_file, 1,
1216 repo, progress_cb, progress_arg);
1217 } else if (errno == ENOTDIR) {
1218 err = got_error_path(ondisk_path,
1219 GOT_ERR_FILE_OBSTRUCTED);
1220 } else {
1221 err = got_error_from_errno3("symlink",
1222 target_path, ondisk_path);
1224 } else if (progress_cb)
1225 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1226 done:
1227 free(resolved_path);
1228 free(abspath);
1229 free(path_got);
1230 return err;
1233 static const struct got_error *
1234 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1235 const char *path, mode_t te_mode, mode_t st_mode,
1236 struct got_blob_object *blob, int restoring_missing_file,
1237 int reverting_versioned_file, int installing_bad_symlink,
1238 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1239 void *progress_arg)
1241 const struct got_error *err = NULL;
1242 int fd = -1;
1243 size_t len, hdrlen;
1244 int update = 0;
1245 char *tmppath = NULL;
1247 if (S_ISLNK(te_mode))
1248 return install_symlink(worktree, ondisk_path, path, te_mode,
1249 st_mode, blob, restoring_missing_file,
1250 reverting_versioned_file, repo, progress_cb, progress_arg);
1252 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1253 GOT_DEFAULT_FILE_MODE);
1254 if (fd == -1) {
1255 if (errno == ENOENT) {
1256 char *parent = dirname(path);
1257 if (parent == NULL)
1258 return got_error_from_errno2("dirname", path);
1259 err = add_dir_on_disk(worktree, parent);
1260 if (err)
1261 return err;
1262 fd = open(ondisk_path,
1263 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1264 GOT_DEFAULT_FILE_MODE);
1265 if (fd == -1)
1266 return got_error_from_errno2("open",
1267 ondisk_path);
1268 } else if (errno == EEXIST) {
1269 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1270 /* TODO file is obstructed; do something */
1271 err = got_error_path(ondisk_path,
1272 GOT_ERR_FILE_OBSTRUCTED);
1273 goto done;
1274 } else {
1275 err = got_opentemp_named_fd(&tmppath, &fd,
1276 ondisk_path);
1277 if (err)
1278 goto done;
1279 update = 1;
1281 } else
1282 return got_error_from_errno2("open", ondisk_path);
1285 if (progress_cb) {
1286 if (restoring_missing_file)
1287 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1288 path);
1289 else if (reverting_versioned_file)
1290 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1291 path);
1292 else
1293 err = (*progress_cb)(progress_arg,
1294 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1295 if (err)
1296 goto done;
1299 hdrlen = got_object_blob_get_hdrlen(blob);
1300 do {
1301 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1302 err = got_object_blob_read_block(&len, blob);
1303 if (err)
1304 break;
1305 if (len > 0) {
1306 /* Skip blob object header first time around. */
1307 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1308 if (outlen == -1) {
1309 err = got_error_from_errno("write");
1310 goto done;
1311 } else if (outlen != len - hdrlen) {
1312 err = got_error(GOT_ERR_IO);
1313 goto done;
1315 hdrlen = 0;
1317 } while (len != 0);
1319 if (fsync(fd) != 0) {
1320 err = got_error_from_errno("fsync");
1321 goto done;
1324 if (update) {
1325 if (rename(tmppath, ondisk_path) != 0) {
1326 err = got_error_from_errno3("rename", tmppath,
1327 ondisk_path);
1328 unlink(tmppath);
1329 goto done;
1333 if (chmod(ondisk_path,
1334 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1335 err = got_error_from_errno2("chmod", ondisk_path);
1336 goto done;
1339 done:
1340 if (fd != -1 && close(fd) != 0 && err == NULL)
1341 err = got_error_from_errno("close");
1342 free(tmppath);
1343 return err;
1346 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1347 static const struct got_error *
1348 get_modified_file_content_status(unsigned char *status, FILE *f)
1350 const struct got_error *err = NULL;
1351 const char *markers[3] = {
1352 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1353 GOT_DIFF_CONFLICT_MARKER_SEP,
1354 GOT_DIFF_CONFLICT_MARKER_END
1356 int i = 0;
1357 char *line;
1358 size_t len;
1359 const char delim[3] = {'\0', '\0', '\0'};
1361 while (*status == GOT_STATUS_MODIFY) {
1362 line = fparseln(f, &len, NULL, delim, 0);
1363 if (line == NULL) {
1364 if (feof(f))
1365 break;
1366 err = got_ferror(f, GOT_ERR_IO);
1367 break;
1370 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1371 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1372 == 0)
1373 *status = GOT_STATUS_CONFLICT;
1374 else
1375 i++;
1379 return err;
1382 static int
1383 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1385 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1386 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1389 static int
1390 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1392 return !(ie->ctime_sec == sb->st_ctime &&
1393 ie->ctime_nsec == sb->st_ctimensec &&
1394 ie->mtime_sec == sb->st_mtime &&
1395 ie->mtime_nsec == sb->st_mtimensec &&
1396 ie->size == (sb->st_size & 0xffffffff) &&
1397 !xbit_differs(ie, sb->st_mode));
1400 static unsigned char
1401 get_staged_status(struct got_fileindex_entry *ie)
1403 switch (got_fileindex_entry_stage_get(ie)) {
1404 case GOT_FILEIDX_STAGE_ADD:
1405 return GOT_STATUS_ADD;
1406 case GOT_FILEIDX_STAGE_DELETE:
1407 return GOT_STATUS_DELETE;
1408 case GOT_FILEIDX_STAGE_MODIFY:
1409 return GOT_STATUS_MODIFY;
1410 default:
1411 return GOT_STATUS_NO_CHANGE;
1415 static const struct got_error *
1416 get_symlink_status(unsigned char *status, struct stat *sb,
1417 struct got_fileindex_entry *ie, const char *abspath,
1418 int dirfd, const char *de_name, struct got_blob_object *blob)
1420 const struct got_error *err = NULL;
1421 char target_path[PATH_MAX];
1422 char etarget[PATH_MAX];
1423 ssize_t elen;
1424 size_t len, target_len = 0;
1425 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1426 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1428 *status = GOT_STATUS_NO_CHANGE;
1430 /* Blob object content specifies the target path of the link. */
1431 do {
1432 err = got_object_blob_read_block(&len, blob);
1433 if (err)
1434 return err;
1435 if (len + target_len >= sizeof(target_path)) {
1437 * Should not happen. The blob contents were OK
1438 * when this symlink was installed.
1440 return got_error(GOT_ERR_NO_SPACE);
1442 if (len > 0) {
1443 /* Skip blob object header first time around. */
1444 memcpy(target_path + target_len, buf + hdrlen,
1445 len - hdrlen);
1446 target_len += len - hdrlen;
1447 hdrlen = 0;
1449 } while (len != 0);
1450 target_path[target_len] = '\0';
1452 if (dirfd != -1) {
1453 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1454 if (elen == -1)
1455 return got_error_from_errno2("readlinkat", abspath);
1456 } else {
1457 elen = readlink(abspath, etarget, sizeof(etarget));
1458 if (elen == -1)
1459 return got_error_from_errno2("readlinkat", abspath);
1462 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1463 *status = GOT_STATUS_MODIFY;
1465 return NULL;
1468 static const struct got_error *
1469 get_file_status(unsigned char *status, struct stat *sb,
1470 struct got_fileindex_entry *ie, const char *abspath,
1471 int dirfd, const char *de_name, struct got_repository *repo)
1473 const struct got_error *err = NULL;
1474 struct got_object_id id;
1475 size_t hdrlen;
1476 int fd = -1;
1477 FILE *f = NULL;
1478 uint8_t fbuf[8192];
1479 struct got_blob_object *blob = NULL;
1480 size_t flen, blen;
1481 unsigned char staged_status = get_staged_status(ie);
1483 *status = GOT_STATUS_NO_CHANGE;
1486 * Whenever the caller provides a directory descriptor and a
1487 * directory entry name for the file, use them! This prevents
1488 * race conditions if filesystem paths change beneath our feet.
1490 if (dirfd != -1) {
1491 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1492 if (errno == ENOENT) {
1493 if (got_fileindex_entry_has_file_on_disk(ie))
1494 *status = GOT_STATUS_MISSING;
1495 else
1496 *status = GOT_STATUS_DELETE;
1497 goto done;
1499 err = got_error_from_errno2("fstatat", abspath);
1500 goto done;
1502 } else {
1503 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1504 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1505 return got_error_from_errno2("open", abspath);
1506 else if (fd == -1 && errno == ELOOP) {
1507 if (lstat(abspath, sb) == -1)
1508 return got_error_from_errno2("lstat", abspath);
1509 } else if (fd == -1 || fstat(fd, sb) == -1) {
1510 if (errno == ENOENT) {
1511 if (got_fileindex_entry_has_file_on_disk(ie))
1512 *status = GOT_STATUS_MISSING;
1513 else
1514 *status = GOT_STATUS_DELETE;
1515 goto done;
1517 err = got_error_from_errno2("fstat", abspath);
1518 goto done;
1522 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1523 *status = GOT_STATUS_OBSTRUCTED;
1524 goto done;
1527 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1528 *status = GOT_STATUS_DELETE;
1529 goto done;
1530 } else if (!got_fileindex_entry_has_blob(ie) &&
1531 staged_status != GOT_STATUS_ADD) {
1532 *status = GOT_STATUS_ADD;
1533 goto done;
1536 if (!stat_info_differs(ie, sb))
1537 goto done;
1539 if (staged_status == GOT_STATUS_MODIFY ||
1540 staged_status == GOT_STATUS_ADD)
1541 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1542 else
1543 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1545 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1546 if (err)
1547 goto done;
1549 if (S_ISLNK(sb->st_mode)) {
1550 /* Staging changes to symlinks is not yet(?) supported. */
1551 if (staged_status != GOT_STATUS_NO_CHANGE) {
1552 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1553 goto done;
1555 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1556 de_name, blob);
1557 goto done;
1561 if (dirfd != -1) {
1562 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1563 if (fd == -1) {
1564 err = got_error_from_errno2("openat", abspath);
1565 goto done;
1569 f = fdopen(fd, "r");
1570 if (f == NULL) {
1571 err = got_error_from_errno2("fdopen", abspath);
1572 goto done;
1574 fd = -1;
1575 hdrlen = got_object_blob_get_hdrlen(blob);
1576 for (;;) {
1577 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1578 err = got_object_blob_read_block(&blen, blob);
1579 if (err)
1580 goto done;
1581 /* Skip length of blob object header first time around. */
1582 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1583 if (flen == 0 && ferror(f)) {
1584 err = got_error_from_errno("fread");
1585 goto done;
1587 if (blen == 0) {
1588 if (flen != 0)
1589 *status = GOT_STATUS_MODIFY;
1590 break;
1591 } else if (flen == 0) {
1592 if (blen != 0)
1593 *status = GOT_STATUS_MODIFY;
1594 break;
1595 } else if (blen - hdrlen == flen) {
1596 /* Skip blob object header first time around. */
1597 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1598 *status = GOT_STATUS_MODIFY;
1599 break;
1601 } else {
1602 *status = GOT_STATUS_MODIFY;
1603 break;
1605 hdrlen = 0;
1608 if (*status == GOT_STATUS_MODIFY) {
1609 rewind(f);
1610 err = get_modified_file_content_status(status, f);
1611 } else if (xbit_differs(ie, sb->st_mode))
1612 *status = GOT_STATUS_MODE_CHANGE;
1613 done:
1614 if (blob)
1615 got_object_blob_close(blob);
1616 if (f != NULL && fclose(f) == EOF && err == NULL)
1617 err = got_error_from_errno2("fclose", abspath);
1618 if (fd != -1 && close(fd) == -1 && err == NULL)
1619 err = got_error_from_errno2("close", abspath);
1620 return err;
1624 * Update timestamps in the file index if a file is unmodified and
1625 * we had to run a full content comparison to find out.
1627 static const struct got_error *
1628 sync_timestamps(char *ondisk_path, unsigned char status,
1629 struct got_fileindex_entry *ie, struct stat *sb)
1631 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1632 return got_fileindex_entry_update(ie, ondisk_path,
1633 ie->blob_sha1, ie->commit_sha1, 1);
1635 return NULL;
1638 static const struct got_error *
1639 update_blob(struct got_worktree *worktree,
1640 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1641 struct got_tree_entry *te, const char *path,
1642 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1643 void *progress_arg)
1645 const struct got_error *err = NULL;
1646 struct got_blob_object *blob = NULL;
1647 char *ondisk_path;
1648 unsigned char status = GOT_STATUS_NO_CHANGE;
1649 struct stat sb;
1651 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1652 return got_error_from_errno("asprintf");
1654 if (ie) {
1655 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1656 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1657 goto done;
1659 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1660 repo);
1661 if (err)
1662 goto done;
1663 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1664 sb.st_mode = got_fileindex_perms_to_st(ie);
1665 } else
1666 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1668 if (status == GOT_STATUS_OBSTRUCTED) {
1669 err = (*progress_cb)(progress_arg, status, path);
1670 goto done;
1672 if (status == GOT_STATUS_CONFLICT) {
1673 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1674 path);
1675 goto done;
1678 if (ie && status != GOT_STATUS_MISSING &&
1679 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1680 if (got_fileindex_entry_has_commit(ie) &&
1681 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1682 SHA1_DIGEST_LENGTH) == 0) {
1683 err = sync_timestamps(ondisk_path, status, ie, &sb);
1684 if (err)
1685 goto done;
1686 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1687 path);
1688 goto done;
1690 if (got_fileindex_entry_has_blob(ie) &&
1691 memcmp(ie->blob_sha1, te->id.sha1,
1692 SHA1_DIGEST_LENGTH) == 0) {
1693 err = sync_timestamps(ondisk_path, status, ie, &sb);
1694 goto done;
1698 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1699 if (err)
1700 goto done;
1702 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1703 int update_timestamps;
1704 struct got_blob_object *blob2 = NULL;
1705 char *label_orig = NULL;
1706 if (got_fileindex_entry_has_blob(ie)) {
1707 struct got_object_id id2;
1708 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1709 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1710 if (err)
1711 goto done;
1713 if (got_fileindex_entry_has_commit(ie)) {
1714 char id_str[SHA1_DIGEST_STRING_LENGTH];
1715 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1716 sizeof(id_str)) == NULL) {
1717 err = got_error_path(id_str,
1718 GOT_ERR_BAD_OBJ_ID_STR);
1719 goto done;
1721 if (asprintf(&label_orig, "%s: commit %s",
1722 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1723 err = got_error_from_errno("asprintf");
1724 goto done;
1727 err = merge_blob(&update_timestamps, worktree, blob2,
1728 ondisk_path, path, sb.st_mode, label_orig, blob,
1729 worktree->base_commit_id, repo,
1730 progress_cb, progress_arg);
1731 free(label_orig);
1732 if (blob2)
1733 got_object_blob_close(blob2);
1734 if (err)
1735 goto done;
1737 * Do not update timestamps of files with local changes.
1738 * Otherwise, a future status walk would treat them as
1739 * unmodified files again.
1741 err = got_fileindex_entry_update(ie, ondisk_path,
1742 blob->id.sha1, worktree->base_commit_id->sha1,
1743 update_timestamps);
1744 } else if (status == GOT_STATUS_MODE_CHANGE) {
1745 err = got_fileindex_entry_update(ie, ondisk_path,
1746 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1747 } else if (status == GOT_STATUS_DELETE) {
1748 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1749 if (err)
1750 goto done;
1751 err = got_fileindex_entry_update(ie, ondisk_path,
1752 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1753 if (err)
1754 goto done;
1755 } else {
1756 err = install_blob(worktree, ondisk_path, path, te->mode,
1757 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1758 repo, progress_cb, progress_arg);
1759 if (err)
1760 goto done;
1761 if (ie) {
1762 err = got_fileindex_entry_update(ie, ondisk_path,
1763 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1764 } else {
1765 err = create_fileindex_entry(fileindex,
1766 worktree->base_commit_id, ondisk_path, path,
1767 &blob->id);
1769 if (err)
1770 goto done;
1772 got_object_blob_close(blob);
1773 done:
1774 free(ondisk_path);
1775 return err;
1778 static const struct got_error *
1779 remove_ondisk_file(const char *root_path, const char *path)
1781 const struct got_error *err = NULL;
1782 char *ondisk_path = NULL;
1784 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1785 return got_error_from_errno("asprintf");
1787 if (unlink(ondisk_path) == -1) {
1788 if (errno != ENOENT)
1789 err = got_error_from_errno2("unlink", ondisk_path);
1790 } else {
1791 char *parent = dirname(ondisk_path);
1792 while (parent && strcmp(parent, root_path) != 0) {
1793 if (rmdir(parent) == -1) {
1794 if (errno != ENOTEMPTY)
1795 err = got_error_from_errno2("rmdir",
1796 parent);
1797 break;
1799 parent = dirname(parent);
1802 free(ondisk_path);
1803 return err;
1806 static const struct got_error *
1807 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1808 struct got_fileindex_entry *ie, struct got_repository *repo,
1809 got_worktree_checkout_cb progress_cb, void *progress_arg)
1811 const struct got_error *err = NULL;
1812 unsigned char status;
1813 struct stat sb;
1814 char *ondisk_path;
1816 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1817 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1819 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1820 == -1)
1821 return got_error_from_errno("asprintf");
1823 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1824 if (err)
1825 goto done;
1827 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1828 status == GOT_STATUS_ADD) {
1829 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1830 if (err)
1831 goto done;
1833 * Preserve the working file and change the deleted blob's
1834 * entry into a schedule-add entry.
1836 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1837 0);
1838 } else {
1839 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1840 if (err)
1841 goto done;
1842 if (status == GOT_STATUS_NO_CHANGE) {
1843 err = remove_ondisk_file(worktree->root_path, ie->path);
1844 if (err)
1845 goto done;
1847 got_fileindex_entry_remove(fileindex, ie);
1849 done:
1850 free(ondisk_path);
1851 return err;
1854 struct diff_cb_arg {
1855 struct got_fileindex *fileindex;
1856 struct got_worktree *worktree;
1857 struct got_repository *repo;
1858 got_worktree_checkout_cb progress_cb;
1859 void *progress_arg;
1860 got_cancel_cb cancel_cb;
1861 void *cancel_arg;
1864 static const struct got_error *
1865 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1866 struct got_tree_entry *te, const char *parent_path)
1868 struct diff_cb_arg *a = arg;
1870 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1871 return got_error(GOT_ERR_CANCELLED);
1873 return update_blob(a->worktree, a->fileindex, ie, te,
1874 ie->path, a->repo, a->progress_cb, a->progress_arg);
1877 static const struct got_error *
1878 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1880 struct diff_cb_arg *a = arg;
1882 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1883 return got_error(GOT_ERR_CANCELLED);
1885 return delete_blob(a->worktree, a->fileindex, ie,
1886 a->repo, a->progress_cb, a->progress_arg);
1889 static const struct got_error *
1890 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1892 struct diff_cb_arg *a = arg;
1893 const struct got_error *err;
1894 char *path;
1896 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1897 return got_error(GOT_ERR_CANCELLED);
1899 if (got_object_tree_entry_is_submodule(te))
1900 return NULL;
1902 if (asprintf(&path, "%s%s%s", parent_path,
1903 parent_path[0] ? "/" : "", te->name)
1904 == -1)
1905 return got_error_from_errno("asprintf");
1907 if (S_ISDIR(te->mode))
1908 err = add_dir_on_disk(a->worktree, path);
1909 else
1910 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1911 a->repo, a->progress_cb, a->progress_arg);
1913 free(path);
1914 return err;
1917 static const struct got_error *
1918 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1920 const struct got_error *err = NULL;
1921 char *uuidstr = NULL;
1922 uint32_t uuid_status;
1924 *refname = NULL;
1926 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1927 if (uuid_status != uuid_s_ok)
1928 return got_error_uuid(uuid_status, "uuid_to_string");
1930 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1931 == -1) {
1932 err = got_error_from_errno("asprintf");
1933 *refname = NULL;
1935 free(uuidstr);
1936 return err;
1939 const struct got_error *
1940 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1942 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1945 static const struct got_error *
1946 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1948 return get_ref_name(refname, worktree,
1949 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1952 static const struct got_error *
1953 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1955 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1958 static const struct got_error *
1959 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1961 return get_ref_name(refname, worktree,
1962 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1965 static const struct got_error *
1966 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1968 return get_ref_name(refname, worktree,
1969 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1972 static const struct got_error *
1973 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1975 return get_ref_name(refname, worktree,
1976 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1979 static const struct got_error *
1980 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1982 return get_ref_name(refname, worktree,
1983 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1986 static const struct got_error *
1987 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1989 return get_ref_name(refname, worktree,
1990 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1993 static const struct got_error *
1994 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1996 return get_ref_name(refname, worktree,
1997 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2000 const struct got_error *
2001 got_worktree_get_histedit_script_path(char **path,
2002 struct got_worktree *worktree)
2004 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2005 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2006 *path = NULL;
2007 return got_error_from_errno("asprintf");
2009 return NULL;
2013 * Prevent Git's garbage collector from deleting our base commit by
2014 * setting a reference to our base commit's ID.
2016 static const struct got_error *
2017 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2019 const struct got_error *err = NULL;
2020 struct got_reference *ref = NULL;
2021 char *refname;
2023 err = got_worktree_get_base_ref_name(&refname, worktree);
2024 if (err)
2025 return err;
2027 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2028 if (err)
2029 goto done;
2031 err = got_ref_write(ref, repo);
2032 done:
2033 free(refname);
2034 if (ref)
2035 got_ref_close(ref);
2036 return err;
2039 static const struct got_error *
2040 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2042 const struct got_error *err = NULL;
2044 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2045 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2046 err = got_error_from_errno("asprintf");
2047 *fileindex_path = NULL;
2049 return err;
2053 static const struct got_error *
2054 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2055 struct got_worktree *worktree)
2057 const struct got_error *err = NULL;
2058 FILE *index = NULL;
2060 *fileindex_path = NULL;
2061 *fileindex = got_fileindex_alloc();
2062 if (*fileindex == NULL)
2063 return got_error_from_errno("got_fileindex_alloc");
2065 err = get_fileindex_path(fileindex_path, worktree);
2066 if (err)
2067 goto done;
2069 index = fopen(*fileindex_path, "rb");
2070 if (index == NULL) {
2071 if (errno != ENOENT)
2072 err = got_error_from_errno2("fopen", *fileindex_path);
2073 } else {
2074 err = got_fileindex_read(*fileindex, index);
2075 if (fclose(index) != 0 && err == NULL)
2076 err = got_error_from_errno("fclose");
2078 done:
2079 if (err) {
2080 free(*fileindex_path);
2081 *fileindex_path = NULL;
2082 got_fileindex_free(*fileindex);
2083 *fileindex = NULL;
2085 return err;
2088 struct bump_base_commit_id_arg {
2089 struct got_object_id *base_commit_id;
2090 const char *path;
2091 size_t path_len;
2092 const char *entry_name;
2093 got_worktree_checkout_cb progress_cb;
2094 void *progress_arg;
2097 /* Bump base commit ID of all files within an updated part of the work tree. */
2098 static const struct got_error *
2099 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2101 const struct got_error *err;
2102 struct bump_base_commit_id_arg *a = arg;
2104 if (a->entry_name) {
2105 if (strcmp(ie->path, a->path) != 0)
2106 return NULL;
2107 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2108 return NULL;
2110 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2111 SHA1_DIGEST_LENGTH) == 0)
2112 return NULL;
2114 if (a->progress_cb) {
2115 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2116 ie->path);
2117 if (err)
2118 return err;
2120 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2121 return NULL;
2124 static const struct got_error *
2125 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2127 const struct got_error *err = NULL;
2128 char *new_fileindex_path = NULL;
2129 FILE *new_index = NULL;
2130 struct timespec timeout;
2132 err = got_opentemp_named(&new_fileindex_path, &new_index,
2133 fileindex_path);
2134 if (err)
2135 goto done;
2137 err = got_fileindex_write(fileindex, new_index);
2138 if (err)
2139 goto done;
2141 if (rename(new_fileindex_path, fileindex_path) != 0) {
2142 err = got_error_from_errno3("rename", new_fileindex_path,
2143 fileindex_path);
2144 unlink(new_fileindex_path);
2148 * Sleep for a short amount of time to ensure that files modified after
2149 * this program exits have a different time stamp from the one which
2150 * was recorded in the file index.
2152 timeout.tv_sec = 0;
2153 timeout.tv_nsec = 1;
2154 nanosleep(&timeout, NULL);
2155 done:
2156 if (new_index)
2157 fclose(new_index);
2158 free(new_fileindex_path);
2159 return err;
2162 static const struct got_error *
2163 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2164 struct got_object_id **tree_id, const char *wt_relpath,
2165 struct got_worktree *worktree, struct got_repository *repo)
2167 const struct got_error *err = NULL;
2168 struct got_object_id *id = NULL;
2169 char *in_repo_path = NULL;
2170 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2172 *entry_type = GOT_OBJ_TYPE_ANY;
2173 *tree_relpath = NULL;
2174 *tree_id = NULL;
2176 if (wt_relpath[0] == '\0') {
2177 /* Check out all files within the work tree. */
2178 *entry_type = GOT_OBJ_TYPE_TREE;
2179 *tree_relpath = strdup("");
2180 if (*tree_relpath == NULL) {
2181 err = got_error_from_errno("strdup");
2182 goto done;
2184 err = got_object_id_by_path(tree_id, repo,
2185 worktree->base_commit_id, worktree->path_prefix);
2186 if (err)
2187 goto done;
2188 return NULL;
2191 /* Check out a subset of files in the work tree. */
2193 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2194 is_root_wt ? "" : "/", wt_relpath) == -1) {
2195 err = got_error_from_errno("asprintf");
2196 goto done;
2199 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2200 in_repo_path);
2201 if (err)
2202 goto done;
2204 free(in_repo_path);
2205 in_repo_path = NULL;
2207 err = got_object_get_type(entry_type, repo, id);
2208 if (err)
2209 goto done;
2211 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2212 /* Check out a single file. */
2213 if (strchr(wt_relpath, '/') == NULL) {
2214 /* Check out a single file in work tree's root dir. */
2215 in_repo_path = strdup(worktree->path_prefix);
2216 if (in_repo_path == NULL) {
2217 err = got_error_from_errno("strdup");
2218 goto done;
2220 *tree_relpath = strdup("");
2221 if (*tree_relpath == NULL) {
2222 err = got_error_from_errno("strdup");
2223 goto done;
2225 } else {
2226 /* Check out a single file in a subdirectory. */
2227 err = got_path_dirname(tree_relpath, wt_relpath);
2228 if (err)
2229 return err;
2230 if (asprintf(&in_repo_path, "%s%s%s",
2231 worktree->path_prefix, is_root_wt ? "" : "/",
2232 *tree_relpath) == -1) {
2233 err = got_error_from_errno("asprintf");
2234 goto done;
2237 err = got_object_id_by_path(tree_id, repo,
2238 worktree->base_commit_id, in_repo_path);
2239 } else {
2240 /* Check out all files within a subdirectory. */
2241 *tree_id = got_object_id_dup(id);
2242 if (*tree_id == NULL) {
2243 err = got_error_from_errno("got_object_id_dup");
2244 goto done;
2246 *tree_relpath = strdup(wt_relpath);
2247 if (*tree_relpath == NULL) {
2248 err = got_error_from_errno("strdup");
2249 goto done;
2252 done:
2253 free(id);
2254 free(in_repo_path);
2255 if (err) {
2256 *entry_type = GOT_OBJ_TYPE_ANY;
2257 free(*tree_relpath);
2258 *tree_relpath = NULL;
2259 free(*tree_id);
2260 *tree_id = NULL;
2262 return err;
2265 static const struct got_error *
2266 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2267 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2268 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2269 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2271 const struct got_error *err = NULL;
2272 struct got_commit_object *commit = NULL;
2273 struct got_tree_object *tree = NULL;
2274 struct got_fileindex_diff_tree_cb diff_cb;
2275 struct diff_cb_arg arg;
2277 err = ref_base_commit(worktree, repo);
2278 if (err) {
2279 if (!(err->code == GOT_ERR_ERRNO &&
2280 (errno == EACCES || errno == EROFS)))
2281 goto done;
2282 err = (*progress_cb)(progress_arg,
2283 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2284 if (err)
2285 return err;
2288 err = got_object_open_as_commit(&commit, repo,
2289 worktree->base_commit_id);
2290 if (err)
2291 goto done;
2293 err = got_object_open_as_tree(&tree, repo, tree_id);
2294 if (err)
2295 goto done;
2297 if (entry_name &&
2298 got_object_tree_find_entry(tree, entry_name) == NULL) {
2299 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2300 goto done;
2303 diff_cb.diff_old_new = diff_old_new;
2304 diff_cb.diff_old = diff_old;
2305 diff_cb.diff_new = diff_new;
2306 arg.fileindex = fileindex;
2307 arg.worktree = worktree;
2308 arg.repo = repo;
2309 arg.progress_cb = progress_cb;
2310 arg.progress_arg = progress_arg;
2311 arg.cancel_cb = cancel_cb;
2312 arg.cancel_arg = cancel_arg;
2313 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2314 entry_name, repo, &diff_cb, &arg);
2315 done:
2316 if (tree)
2317 got_object_tree_close(tree);
2318 if (commit)
2319 got_object_commit_close(commit);
2320 return err;
2323 const struct got_error *
2324 got_worktree_checkout_files(struct got_worktree *worktree,
2325 struct got_pathlist_head *paths, struct got_repository *repo,
2326 got_worktree_checkout_cb progress_cb, void *progress_arg,
2327 got_cancel_cb cancel_cb, void *cancel_arg)
2329 const struct got_error *err = NULL, *sync_err, *unlockerr;
2330 struct got_commit_object *commit = NULL;
2331 struct got_tree_object *tree = NULL;
2332 struct got_fileindex *fileindex = NULL;
2333 char *fileindex_path = NULL;
2334 struct got_pathlist_entry *pe;
2335 struct tree_path_data {
2336 SIMPLEQ_ENTRY(tree_path_data) entry;
2337 struct got_object_id *tree_id;
2338 int entry_type;
2339 char *relpath;
2340 char *entry_name;
2341 } *tpd = NULL;
2342 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2344 SIMPLEQ_INIT(&tree_paths);
2346 err = lock_worktree(worktree, LOCK_EX);
2347 if (err)
2348 return err;
2350 /* Map all specified paths to in-repository trees. */
2351 TAILQ_FOREACH(pe, paths, entry) {
2352 tpd = malloc(sizeof(*tpd));
2353 if (tpd == NULL) {
2354 err = got_error_from_errno("malloc");
2355 goto done;
2358 err = find_tree_entry_for_checkout(&tpd->entry_type,
2359 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2360 if (err) {
2361 free(tpd);
2362 goto done;
2365 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2366 err = got_path_basename(&tpd->entry_name, pe->path);
2367 if (err) {
2368 free(tpd->relpath);
2369 free(tpd->tree_id);
2370 free(tpd);
2371 goto done;
2373 } else
2374 tpd->entry_name = NULL;
2376 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2380 * Read the file index.
2381 * Checking out files is supposed to be an idempotent operation.
2382 * If the on-disk file index is incomplete we will try to complete it.
2384 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2385 if (err)
2386 goto done;
2388 tpd = SIMPLEQ_FIRST(&tree_paths);
2389 TAILQ_FOREACH(pe, paths, entry) {
2390 struct bump_base_commit_id_arg bbc_arg;
2392 err = checkout_files(worktree, fileindex, tpd->relpath,
2393 tpd->tree_id, tpd->entry_name, repo,
2394 progress_cb, progress_arg, cancel_cb, cancel_arg);
2395 if (err)
2396 break;
2398 bbc_arg.base_commit_id = worktree->base_commit_id;
2399 bbc_arg.entry_name = tpd->entry_name;
2400 bbc_arg.path = pe->path;
2401 bbc_arg.path_len = pe->path_len;
2402 bbc_arg.progress_cb = progress_cb;
2403 bbc_arg.progress_arg = progress_arg;
2404 err = got_fileindex_for_each_entry_safe(fileindex,
2405 bump_base_commit_id, &bbc_arg);
2406 if (err)
2407 break;
2409 tpd = SIMPLEQ_NEXT(tpd, entry);
2411 sync_err = sync_fileindex(fileindex, fileindex_path);
2412 if (sync_err && err == NULL)
2413 err = sync_err;
2414 done:
2415 free(fileindex_path);
2416 if (tree)
2417 got_object_tree_close(tree);
2418 if (commit)
2419 got_object_commit_close(commit);
2420 if (fileindex)
2421 got_fileindex_free(fileindex);
2422 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2423 tpd = SIMPLEQ_FIRST(&tree_paths);
2424 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2425 free(tpd->relpath);
2426 free(tpd->tree_id);
2427 free(tpd);
2429 unlockerr = lock_worktree(worktree, LOCK_SH);
2430 if (unlockerr && err == NULL)
2431 err = unlockerr;
2432 return err;
2435 struct merge_file_cb_arg {
2436 struct got_worktree *worktree;
2437 struct got_fileindex *fileindex;
2438 got_worktree_checkout_cb progress_cb;
2439 void *progress_arg;
2440 got_cancel_cb cancel_cb;
2441 void *cancel_arg;
2442 const char *label_orig;
2443 struct got_object_id *commit_id2;
2446 static const struct got_error *
2447 merge_file_cb(void *arg, struct got_blob_object *blob1,
2448 struct got_blob_object *blob2, struct got_object_id *id1,
2449 struct got_object_id *id2, const char *path1, const char *path2,
2450 mode_t mode1, mode_t mode2, struct got_repository *repo)
2452 static const struct got_error *err = NULL;
2453 struct merge_file_cb_arg *a = arg;
2454 struct got_fileindex_entry *ie;
2455 char *ondisk_path = NULL;
2456 struct stat sb;
2457 unsigned char status;
2458 int local_changes_subsumed;
2460 if (blob1 && blob2) {
2461 ie = got_fileindex_entry_get(a->fileindex, path2,
2462 strlen(path2));
2463 if (ie == NULL)
2464 return (*a->progress_cb)(a->progress_arg,
2465 GOT_STATUS_MISSING, path2);
2467 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2468 path2) == -1)
2469 return got_error_from_errno("asprintf");
2471 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2472 repo);
2473 if (err)
2474 goto done;
2476 if (status == GOT_STATUS_DELETE) {
2477 err = (*a->progress_cb)(a->progress_arg,
2478 GOT_STATUS_MERGE, path2);
2479 goto done;
2481 if (status != GOT_STATUS_NO_CHANGE &&
2482 status != GOT_STATUS_MODIFY &&
2483 status != GOT_STATUS_CONFLICT &&
2484 status != GOT_STATUS_ADD) {
2485 err = (*a->progress_cb)(a->progress_arg, status, path2);
2486 goto done;
2489 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2490 err = merge_symlink(a->worktree, blob1,
2491 ondisk_path, path2, sb.st_mode, a->label_orig,
2492 blob2, a->commit_id2, repo, a->progress_cb,
2493 a->progress_arg);
2494 } else {
2495 err = merge_blob(&local_changes_subsumed, a->worktree,
2496 blob1, ondisk_path, path2, sb.st_mode,
2497 a->label_orig, blob2, a->commit_id2, repo,
2498 a->progress_cb, a->progress_arg);
2500 } else if (blob1) {
2501 ie = got_fileindex_entry_get(a->fileindex, path1,
2502 strlen(path1));
2503 if (ie == NULL)
2504 return (*a->progress_cb)(a->progress_arg,
2505 GOT_STATUS_MISSING, path1);
2507 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2508 path1) == -1)
2509 return got_error_from_errno("asprintf");
2511 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2512 repo);
2513 if (err)
2514 goto done;
2516 switch (status) {
2517 case GOT_STATUS_NO_CHANGE:
2518 err = (*a->progress_cb)(a->progress_arg,
2519 GOT_STATUS_DELETE, path1);
2520 if (err)
2521 goto done;
2522 err = remove_ondisk_file(a->worktree->root_path, path1);
2523 if (err)
2524 goto done;
2525 if (ie)
2526 got_fileindex_entry_mark_deleted_from_disk(ie);
2527 break;
2528 case GOT_STATUS_DELETE:
2529 case GOT_STATUS_MISSING:
2530 err = (*a->progress_cb)(a->progress_arg,
2531 GOT_STATUS_DELETE, path1);
2532 if (err)
2533 goto done;
2534 if (ie)
2535 got_fileindex_entry_mark_deleted_from_disk(ie);
2536 break;
2537 case GOT_STATUS_ADD:
2538 case GOT_STATUS_MODIFY:
2539 case GOT_STATUS_CONFLICT:
2540 err = (*a->progress_cb)(a->progress_arg,
2541 GOT_STATUS_CANNOT_DELETE, path1);
2542 if (err)
2543 goto done;
2544 break;
2545 case GOT_STATUS_OBSTRUCTED:
2546 err = (*a->progress_cb)(a->progress_arg, status, path1);
2547 if (err)
2548 goto done;
2549 break;
2550 default:
2551 break;
2553 } else if (blob2) {
2554 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2555 path2) == -1)
2556 return got_error_from_errno("asprintf");
2557 ie = got_fileindex_entry_get(a->fileindex, path2,
2558 strlen(path2));
2559 if (ie) {
2560 err = get_file_status(&status, &sb, ie, ondisk_path,
2561 -1, NULL, repo);
2562 if (err)
2563 goto done;
2564 if (status != GOT_STATUS_NO_CHANGE &&
2565 status != GOT_STATUS_MODIFY &&
2566 status != GOT_STATUS_CONFLICT &&
2567 status != GOT_STATUS_ADD) {
2568 err = (*a->progress_cb)(a->progress_arg,
2569 status, path2);
2570 goto done;
2572 if (S_ISLNK(mode2)) {
2573 err = merge_symlink(a->worktree, NULL,
2574 ondisk_path, path2, sb.st_mode,
2575 a->label_orig, blob2, a->commit_id2,
2576 repo, a->progress_cb, a->progress_arg);
2577 if (err)
2578 goto done;
2579 } else {
2580 err = merge_blob(&local_changes_subsumed,
2581 a->worktree, NULL, ondisk_path, path2,
2582 sb.st_mode, a->label_orig, blob2,
2583 a->commit_id2, repo, a->progress_cb,
2584 a->progress_arg);
2585 if (err)
2586 goto done;
2588 if (status == GOT_STATUS_DELETE) {
2589 err = got_fileindex_entry_update(ie,
2590 ondisk_path, blob2->id.sha1,
2591 a->worktree->base_commit_id->sha1, 0);
2592 if (err)
2593 goto done;
2595 } else {
2596 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2597 err = install_blob(a->worktree, ondisk_path, path2,
2598 /* XXX get this from parent tree! */
2599 GOT_DEFAULT_FILE_MODE,
2600 sb.st_mode, blob2, 0, 0, 0, repo,
2601 a->progress_cb, a->progress_arg);
2602 if (err)
2603 goto done;
2604 err = got_fileindex_entry_alloc(&ie, path2);
2605 if (err)
2606 goto done;
2607 err = got_fileindex_entry_update(ie, ondisk_path,
2608 NULL, NULL, 1);
2609 if (err) {
2610 got_fileindex_entry_free(ie);
2611 goto done;
2613 err = got_fileindex_entry_add(a->fileindex, ie);
2614 if (err) {
2615 got_fileindex_entry_free(ie);
2616 goto done;
2620 done:
2621 free(ondisk_path);
2622 return err;
2625 struct check_merge_ok_arg {
2626 struct got_worktree *worktree;
2627 struct got_repository *repo;
2630 static const struct got_error *
2631 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2633 const struct got_error *err = NULL;
2634 struct check_merge_ok_arg *a = arg;
2635 unsigned char status;
2636 struct stat sb;
2637 char *ondisk_path;
2639 /* Reject merges into a work tree with mixed base commits. */
2640 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2641 SHA1_DIGEST_LENGTH))
2642 return got_error(GOT_ERR_MIXED_COMMITS);
2644 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2645 == -1)
2646 return got_error_from_errno("asprintf");
2648 /* Reject merges into a work tree with conflicted files. */
2649 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2650 if (err)
2651 return err;
2652 if (status == GOT_STATUS_CONFLICT)
2653 return got_error(GOT_ERR_CONFLICTS);
2655 return NULL;
2658 static const struct got_error *
2659 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2660 const char *fileindex_path, struct got_object_id *commit_id1,
2661 struct got_object_id *commit_id2, struct got_repository *repo,
2662 got_worktree_checkout_cb progress_cb, void *progress_arg,
2663 got_cancel_cb cancel_cb, void *cancel_arg)
2665 const struct got_error *err = NULL, *sync_err;
2666 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2667 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2668 struct merge_file_cb_arg arg;
2669 char *label_orig = NULL;
2671 if (commit_id1) {
2672 char *id_str;
2674 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2675 worktree->path_prefix);
2676 if (err)
2677 goto done;
2679 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2680 if (err)
2681 goto done;
2683 err = got_object_id_str(&id_str, commit_id1);
2684 if (err)
2685 goto done;
2687 if (asprintf(&label_orig, "%s: commit %s",
2688 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2689 err = got_error_from_errno("asprintf");
2690 free(id_str);
2691 goto done;
2693 free(id_str);
2696 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2697 worktree->path_prefix);
2698 if (err)
2699 goto done;
2701 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2702 if (err)
2703 goto done;
2705 arg.worktree = worktree;
2706 arg.fileindex = fileindex;
2707 arg.progress_cb = progress_cb;
2708 arg.progress_arg = progress_arg;
2709 arg.cancel_cb = cancel_cb;
2710 arg.cancel_arg = cancel_arg;
2711 arg.label_orig = label_orig;
2712 arg.commit_id2 = commit_id2;
2713 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2714 sync_err = sync_fileindex(fileindex, fileindex_path);
2715 if (sync_err && err == NULL)
2716 err = sync_err;
2717 done:
2718 if (tree1)
2719 got_object_tree_close(tree1);
2720 if (tree2)
2721 got_object_tree_close(tree2);
2722 free(label_orig);
2723 return err;
2726 const struct got_error *
2727 got_worktree_merge_files(struct got_worktree *worktree,
2728 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2729 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2730 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2732 const struct got_error *err, *unlockerr;
2733 char *fileindex_path = NULL;
2734 struct got_fileindex *fileindex = NULL;
2735 struct check_merge_ok_arg mok_arg;
2737 err = lock_worktree(worktree, LOCK_EX);
2738 if (err)
2739 return err;
2741 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2742 if (err)
2743 goto done;
2745 mok_arg.worktree = worktree;
2746 mok_arg.repo = repo;
2747 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2748 &mok_arg);
2749 if (err)
2750 goto done;
2752 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2753 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2754 done:
2755 if (fileindex)
2756 got_fileindex_free(fileindex);
2757 free(fileindex_path);
2758 unlockerr = lock_worktree(worktree, LOCK_SH);
2759 if (unlockerr && err == NULL)
2760 err = unlockerr;
2761 return err;
2764 struct diff_dir_cb_arg {
2765 struct got_fileindex *fileindex;
2766 struct got_worktree *worktree;
2767 const char *status_path;
2768 size_t status_path_len;
2769 struct got_repository *repo;
2770 got_worktree_status_cb status_cb;
2771 void *status_arg;
2772 got_cancel_cb cancel_cb;
2773 void *cancel_arg;
2774 /* A pathlist containing per-directory pathlists of ignore patterns. */
2775 struct got_pathlist_head ignores;
2776 int report_unchanged;
2777 int no_ignores;
2780 static const struct got_error *
2781 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2782 int dirfd, const char *de_name,
2783 got_worktree_status_cb status_cb, void *status_arg,
2784 struct got_repository *repo, int report_unchanged)
2786 const struct got_error *err = NULL;
2787 unsigned char status = GOT_STATUS_NO_CHANGE;
2788 unsigned char staged_status = get_staged_status(ie);
2789 struct stat sb;
2790 struct got_object_id blob_id, commit_id, staged_blob_id;
2791 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2792 struct got_object_id *staged_blob_idp = NULL;
2794 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2795 if (err)
2796 return err;
2798 if (status == GOT_STATUS_NO_CHANGE &&
2799 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2800 return NULL;
2802 if (got_fileindex_entry_has_blob(ie)) {
2803 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2804 blob_idp = &blob_id;
2806 if (got_fileindex_entry_has_commit(ie)) {
2807 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2808 commit_idp = &commit_id;
2810 if (staged_status == GOT_STATUS_ADD ||
2811 staged_status == GOT_STATUS_MODIFY) {
2812 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2813 SHA1_DIGEST_LENGTH);
2814 staged_blob_idp = &staged_blob_id;
2817 return (*status_cb)(status_arg, status, staged_status,
2818 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2821 static const struct got_error *
2822 status_old_new(void *arg, struct got_fileindex_entry *ie,
2823 struct dirent *de, const char *parent_path, int dirfd)
2825 const struct got_error *err = NULL;
2826 struct diff_dir_cb_arg *a = arg;
2827 char *abspath;
2829 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2830 return got_error(GOT_ERR_CANCELLED);
2832 if (got_path_cmp(parent_path, a->status_path,
2833 strlen(parent_path), a->status_path_len) != 0 &&
2834 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2835 return NULL;
2837 if (parent_path[0]) {
2838 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2839 parent_path, de->d_name) == -1)
2840 return got_error_from_errno("asprintf");
2841 } else {
2842 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2843 de->d_name) == -1)
2844 return got_error_from_errno("asprintf");
2847 err = report_file_status(ie, abspath, dirfd, de->d_name,
2848 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2849 free(abspath);
2850 return err;
2853 static const struct got_error *
2854 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2856 struct diff_dir_cb_arg *a = arg;
2857 struct got_object_id blob_id, commit_id;
2858 unsigned char status;
2860 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2861 return got_error(GOT_ERR_CANCELLED);
2863 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2864 return NULL;
2866 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2867 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2868 if (got_fileindex_entry_has_file_on_disk(ie))
2869 status = GOT_STATUS_MISSING;
2870 else
2871 status = GOT_STATUS_DELETE;
2872 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2873 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2876 void
2877 free_ignorelist(struct got_pathlist_head *ignorelist)
2879 struct got_pathlist_entry *pe;
2881 TAILQ_FOREACH(pe, ignorelist, entry)
2882 free((char *)pe->path);
2883 got_pathlist_free(ignorelist);
2886 void
2887 free_ignores(struct got_pathlist_head *ignores)
2889 struct got_pathlist_entry *pe;
2891 TAILQ_FOREACH(pe, ignores, entry) {
2892 struct got_pathlist_head *ignorelist = pe->data;
2893 free_ignorelist(ignorelist);
2894 free((char *)pe->path);
2896 got_pathlist_free(ignores);
2899 static const struct got_error *
2900 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2902 const struct got_error *err = NULL;
2903 struct got_pathlist_entry *pe = NULL;
2904 struct got_pathlist_head *ignorelist;
2905 char *line = NULL, *pattern, *dirpath = NULL;
2906 size_t linesize = 0;
2907 ssize_t linelen;
2909 ignorelist = calloc(1, sizeof(*ignorelist));
2910 if (ignorelist == NULL)
2911 return got_error_from_errno("calloc");
2912 TAILQ_INIT(ignorelist);
2914 while ((linelen = getline(&line, &linesize, f)) != -1) {
2915 if (linelen > 0 && line[linelen - 1] == '\n')
2916 line[linelen - 1] = '\0';
2918 /* Git's ignores may contain comments. */
2919 if (line[0] == '#')
2920 continue;
2922 /* Git's negated patterns are not (yet?) supported. */
2923 if (line[0] == '!')
2924 continue;
2926 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2927 line) == -1) {
2928 err = got_error_from_errno("asprintf");
2929 goto done;
2931 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2932 if (err)
2933 goto done;
2935 if (ferror(f)) {
2936 err = got_error_from_errno("getline");
2937 goto done;
2940 dirpath = strdup(path);
2941 if (dirpath == NULL) {
2942 err = got_error_from_errno("strdup");
2943 goto done;
2945 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2946 done:
2947 free(line);
2948 if (err || pe == NULL) {
2949 free(dirpath);
2950 free_ignorelist(ignorelist);
2952 return err;
2955 int
2956 match_ignores(struct got_pathlist_head *ignores, const char *path)
2958 struct got_pathlist_entry *pe;
2960 /* Handle patterns which match in all directories. */
2961 TAILQ_FOREACH(pe, ignores, entry) {
2962 struct got_pathlist_head *ignorelist = pe->data;
2963 struct got_pathlist_entry *pi;
2965 TAILQ_FOREACH(pi, ignorelist, entry) {
2966 const char *p, *pattern = pi->path;
2968 if (strncmp(pattern, "**/", 3) != 0)
2969 continue;
2970 pattern += 3;
2971 p = path;
2972 while (*p) {
2973 if (fnmatch(pattern, p,
2974 FNM_PATHNAME | FNM_LEADING_DIR)) {
2975 /* Retry in next directory. */
2976 while (*p && *p != '/')
2977 p++;
2978 while (*p == '/')
2979 p++;
2980 continue;
2982 return 1;
2988 * The ignores pathlist contains ignore lists from children before
2989 * parents, so we can find the most specific ignorelist by walking
2990 * ignores backwards.
2992 pe = TAILQ_LAST(ignores, got_pathlist_head);
2993 while (pe) {
2994 if (got_path_is_child(path, pe->path, pe->path_len)) {
2995 struct got_pathlist_head *ignorelist = pe->data;
2996 struct got_pathlist_entry *pi;
2997 TAILQ_FOREACH(pi, ignorelist, entry) {
2998 const char *pattern = pi->path;
2999 int flags = FNM_LEADING_DIR;
3000 if (strstr(pattern, "/**/") == NULL)
3001 flags |= FNM_PATHNAME;
3002 if (fnmatch(pattern, path, flags))
3003 continue;
3004 return 1;
3007 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3010 return 0;
3013 static const struct got_error *
3014 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3015 const char *path, int dirfd, const char *ignores_filename)
3017 const struct got_error *err = NULL;
3018 char *ignorespath;
3019 int fd = -1;
3020 FILE *ignoresfile = NULL;
3022 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3023 path[0] ? "/" : "", ignores_filename) == -1)
3024 return got_error_from_errno("asprintf");
3026 if (dirfd != -1) {
3027 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3028 if (fd == -1) {
3029 if (errno != ENOENT && errno != EACCES)
3030 err = got_error_from_errno2("openat",
3031 ignorespath);
3032 } else {
3033 ignoresfile = fdopen(fd, "r");
3034 if (ignoresfile == NULL)
3035 err = got_error_from_errno2("fdopen",
3036 ignorespath);
3037 else {
3038 fd = -1;
3039 err = read_ignores(ignores, path, ignoresfile);
3042 } else {
3043 ignoresfile = fopen(ignorespath, "r");
3044 if (ignoresfile == NULL) {
3045 if (errno != ENOENT && errno != EACCES)
3046 err = got_error_from_errno2("fopen",
3047 ignorespath);
3048 } else
3049 err = read_ignores(ignores, path, ignoresfile);
3052 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3053 err = got_error_from_errno2("fclose", path);
3054 if (fd != -1 && close(fd) == -1 && err == NULL)
3055 err = got_error_from_errno2("close", path);
3056 free(ignorespath);
3057 return err;
3060 static const struct got_error *
3061 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3063 const struct got_error *err = NULL;
3064 struct diff_dir_cb_arg *a = arg;
3065 char *path = NULL;
3067 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3068 return got_error(GOT_ERR_CANCELLED);
3070 if (parent_path[0]) {
3071 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3072 return got_error_from_errno("asprintf");
3073 } else {
3074 path = de->d_name;
3077 if (de->d_type != DT_DIR &&
3078 got_path_is_child(path, a->status_path, a->status_path_len)
3079 && !match_ignores(&a->ignores, path))
3080 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3081 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3082 if (parent_path[0])
3083 free(path);
3084 return err;
3087 static const struct got_error *
3088 status_traverse(void *arg, const char *path, int dirfd)
3090 const struct got_error *err = NULL;
3091 struct diff_dir_cb_arg *a = arg;
3093 if (a->no_ignores)
3094 return NULL;
3096 err = add_ignores(&a->ignores, a->worktree->root_path,
3097 path, dirfd, ".cvsignore");
3098 if (err)
3099 return err;
3101 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3102 dirfd, ".gitignore");
3104 return err;
3107 static const struct got_error *
3108 report_single_file_status(const char *path, const char *ondisk_path,
3109 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3110 void *status_arg, struct got_repository *repo, int report_unchanged)
3112 struct got_fileindex_entry *ie;
3113 struct stat sb;
3115 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3116 if (ie)
3117 return report_file_status(ie, ondisk_path, -1, NULL,
3118 status_cb, status_arg, repo, report_unchanged);
3120 if (lstat(ondisk_path, &sb) == -1) {
3121 if (errno != ENOENT)
3122 return got_error_from_errno2("lstat", ondisk_path);
3123 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3124 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3125 return NULL;
3128 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3129 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3130 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3132 return NULL;
3135 static const struct got_error *
3136 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3137 const char *root_path, const char *path)
3139 const struct got_error *err;
3140 char *parent_path, *next_parent_path = NULL;
3142 err = add_ignores(ignores, root_path, "", -1,
3143 ".cvsignore");
3144 if (err)
3145 return err;
3147 err = add_ignores(ignores, root_path, "", -1,
3148 ".gitignore");
3149 if (err)
3150 return err;
3152 err = got_path_dirname(&parent_path, path);
3153 if (err) {
3154 if (err->code == GOT_ERR_BAD_PATH)
3155 return NULL; /* cannot traverse parent */
3156 return err;
3158 for (;;) {
3159 err = add_ignores(ignores, root_path, parent_path, -1,
3160 ".cvsignore");
3161 if (err)
3162 break;
3163 err = add_ignores(ignores, root_path, parent_path, -1,
3164 ".gitignore");
3165 if (err)
3166 break;
3167 err = got_path_dirname(&next_parent_path, parent_path);
3168 if (err) {
3169 if (err->code == GOT_ERR_BAD_PATH)
3170 err = NULL; /* traversed everything */
3171 break;
3173 free(parent_path);
3174 parent_path = next_parent_path;
3175 next_parent_path = NULL;
3178 free(parent_path);
3179 free(next_parent_path);
3180 return err;
3183 static const struct got_error *
3184 worktree_status(struct got_worktree *worktree, const char *path,
3185 struct got_fileindex *fileindex, struct got_repository *repo,
3186 got_worktree_status_cb status_cb, void *status_arg,
3187 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3188 int report_unchanged)
3190 const struct got_error *err = NULL;
3191 int fd = -1;
3192 struct got_fileindex_diff_dir_cb fdiff_cb;
3193 struct diff_dir_cb_arg arg;
3194 char *ondisk_path = NULL;
3196 TAILQ_INIT(&arg.ignores);
3198 if (asprintf(&ondisk_path, "%s%s%s",
3199 worktree->root_path, path[0] ? "/" : "", path) == -1)
3200 return got_error_from_errno("asprintf");
3202 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3203 if (fd == -1) {
3204 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3205 errno != ELOOP)
3206 err = got_error_from_errno2("open", ondisk_path);
3207 else
3208 err = report_single_file_status(path, ondisk_path,
3209 fileindex, status_cb, status_arg, repo,
3210 report_unchanged);
3211 } else {
3212 fdiff_cb.diff_old_new = status_old_new;
3213 fdiff_cb.diff_old = status_old;
3214 fdiff_cb.diff_new = status_new;
3215 fdiff_cb.diff_traverse = status_traverse;
3216 arg.fileindex = fileindex;
3217 arg.worktree = worktree;
3218 arg.status_path = path;
3219 arg.status_path_len = strlen(path);
3220 arg.repo = repo;
3221 arg.status_cb = status_cb;
3222 arg.status_arg = status_arg;
3223 arg.cancel_cb = cancel_cb;
3224 arg.cancel_arg = cancel_arg;
3225 arg.report_unchanged = report_unchanged;
3226 arg.no_ignores = no_ignores;
3227 if (!no_ignores) {
3228 err = add_ignores_from_parent_paths(&arg.ignores,
3229 worktree->root_path, path);
3230 if (err)
3231 goto done;
3233 err = got_fileindex_diff_dir(fileindex, fd,
3234 worktree->root_path, path, repo, &fdiff_cb, &arg);
3236 done:
3237 free_ignores(&arg.ignores);
3238 if (fd != -1 && close(fd) != 0 && err == NULL)
3239 err = got_error_from_errno("close");
3240 free(ondisk_path);
3241 return err;
3244 const struct got_error *
3245 got_worktree_status(struct got_worktree *worktree,
3246 struct got_pathlist_head *paths, struct got_repository *repo,
3247 got_worktree_status_cb status_cb, void *status_arg,
3248 got_cancel_cb cancel_cb, void *cancel_arg)
3250 const struct got_error *err = NULL;
3251 char *fileindex_path = NULL;
3252 struct got_fileindex *fileindex = NULL;
3253 struct got_pathlist_entry *pe;
3255 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3256 if (err)
3257 return err;
3259 TAILQ_FOREACH(pe, paths, entry) {
3260 err = worktree_status(worktree, pe->path, fileindex, repo,
3261 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3262 if (err)
3263 break;
3265 free(fileindex_path);
3266 got_fileindex_free(fileindex);
3267 return err;
3270 const struct got_error *
3271 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3272 const char *arg)
3274 const struct got_error *err = NULL;
3275 char *resolved = NULL, *cwd = NULL, *path = NULL;
3276 size_t len;
3277 struct stat sb;
3279 *wt_path = NULL;
3281 cwd = getcwd(NULL, 0);
3282 if (cwd == NULL)
3283 return got_error_from_errno("getcwd");
3285 if (lstat(arg, &sb) == -1) {
3286 if (errno != ENOENT) {
3287 err = got_error_from_errno2("lstat", arg);
3288 goto done;
3291 if (S_ISLNK(sb.st_mode)) {
3293 * We cannot use realpath(3) with symlinks since we want to
3294 * operate on the symlink itself.
3295 * But we can make the path absolute, assuming it is relative
3296 * to the current working directory, and then canonicalize it.
3298 char *abspath = NULL;
3299 char canonpath[PATH_MAX];
3300 if (!got_path_is_absolute(arg)) {
3301 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3302 err = got_error_from_errno("asprintf");
3303 goto done;
3307 err = got_canonpath(abspath ? abspath : arg, canonpath,
3308 sizeof(canonpath));
3309 if (err)
3310 goto done;
3311 resolved = strdup(canonpath);
3312 if (resolved == NULL) {
3313 err = got_error_from_errno("strdup");
3314 goto done;
3316 } else {
3317 resolved = realpath(arg, NULL);
3318 if (resolved == NULL) {
3319 if (errno != ENOENT) {
3320 err = got_error_from_errno2("realpath", arg);
3321 goto done;
3323 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3324 err = got_error_from_errno("asprintf");
3325 goto done;
3330 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3331 strlen(got_worktree_get_root_path(worktree)))) {
3332 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3333 goto done;
3336 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3337 err = got_path_skip_common_ancestor(&path,
3338 got_worktree_get_root_path(worktree), resolved);
3339 if (err)
3340 goto done;
3341 } else {
3342 path = strdup("");
3343 if (path == NULL) {
3344 err = got_error_from_errno("strdup");
3345 goto done;
3349 /* XXX status walk can't deal with trailing slash! */
3350 len = strlen(path);
3351 while (len > 0 && path[len - 1] == '/') {
3352 path[len - 1] = '\0';
3353 len--;
3355 done:
3356 free(resolved);
3357 free(cwd);
3358 if (err == NULL)
3359 *wt_path = path;
3360 else
3361 free(path);
3362 return err;
3365 struct schedule_addition_args {
3366 struct got_worktree *worktree;
3367 struct got_fileindex *fileindex;
3368 got_worktree_checkout_cb progress_cb;
3369 void *progress_arg;
3370 struct got_repository *repo;
3373 static const struct got_error *
3374 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3375 const char *relpath, struct got_object_id *blob_id,
3376 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3377 int dirfd, const char *de_name)
3379 struct schedule_addition_args *a = arg;
3380 const struct got_error *err = NULL;
3381 struct got_fileindex_entry *ie;
3382 struct stat sb;
3383 char *ondisk_path;
3385 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3386 relpath) == -1)
3387 return got_error_from_errno("asprintf");
3389 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3390 if (ie) {
3391 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3392 de_name, a->repo);
3393 if (err)
3394 goto done;
3395 /* Re-adding an existing entry is a no-op. */
3396 if (status == GOT_STATUS_ADD)
3397 goto done;
3398 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3399 if (err)
3400 goto done;
3403 if (status != GOT_STATUS_UNVERSIONED) {
3404 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3405 goto done;
3408 err = got_fileindex_entry_alloc(&ie, relpath);
3409 if (err)
3410 goto done;
3411 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3412 if (err) {
3413 got_fileindex_entry_free(ie);
3414 goto done;
3416 err = got_fileindex_entry_add(a->fileindex, ie);
3417 if (err) {
3418 got_fileindex_entry_free(ie);
3419 goto done;
3421 done:
3422 free(ondisk_path);
3423 if (err)
3424 return err;
3425 if (status == GOT_STATUS_ADD)
3426 return NULL;
3427 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3430 const struct got_error *
3431 got_worktree_schedule_add(struct got_worktree *worktree,
3432 struct got_pathlist_head *paths,
3433 got_worktree_checkout_cb progress_cb, void *progress_arg,
3434 struct got_repository *repo, int no_ignores)
3436 struct got_fileindex *fileindex = NULL;
3437 char *fileindex_path = NULL;
3438 const struct got_error *err = NULL, *sync_err, *unlockerr;
3439 struct got_pathlist_entry *pe;
3440 struct schedule_addition_args saa;
3442 err = lock_worktree(worktree, LOCK_EX);
3443 if (err)
3444 return err;
3446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3447 if (err)
3448 goto done;
3450 saa.worktree = worktree;
3451 saa.fileindex = fileindex;
3452 saa.progress_cb = progress_cb;
3453 saa.progress_arg = progress_arg;
3454 saa.repo = repo;
3456 TAILQ_FOREACH(pe, paths, entry) {
3457 err = worktree_status(worktree, pe->path, fileindex, repo,
3458 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3459 if (err)
3460 break;
3462 sync_err = sync_fileindex(fileindex, fileindex_path);
3463 if (sync_err && err == NULL)
3464 err = sync_err;
3465 done:
3466 free(fileindex_path);
3467 if (fileindex)
3468 got_fileindex_free(fileindex);
3469 unlockerr = lock_worktree(worktree, LOCK_SH);
3470 if (unlockerr && err == NULL)
3471 err = unlockerr;
3472 return err;
3475 struct schedule_deletion_args {
3476 struct got_worktree *worktree;
3477 struct got_fileindex *fileindex;
3478 got_worktree_delete_cb progress_cb;
3479 void *progress_arg;
3480 struct got_repository *repo;
3481 int delete_local_mods;
3482 int keep_on_disk;
3485 static const struct got_error *
3486 schedule_for_deletion(void *arg, unsigned char status,
3487 unsigned char staged_status, const char *relpath,
3488 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3489 struct got_object_id *commit_id, int dirfd, const char *de_name)
3491 struct schedule_deletion_args *a = arg;
3492 const struct got_error *err = NULL;
3493 struct got_fileindex_entry *ie = NULL;
3494 struct stat sb;
3495 char *ondisk_path, *parent = NULL;
3497 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3498 if (ie == NULL)
3499 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3501 staged_status = get_staged_status(ie);
3502 if (staged_status != GOT_STATUS_NO_CHANGE) {
3503 if (staged_status == GOT_STATUS_DELETE)
3504 return NULL;
3505 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3508 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3509 relpath) == -1)
3510 return got_error_from_errno("asprintf");
3512 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3513 a->repo);
3514 if (err)
3515 goto done;
3517 if (status != GOT_STATUS_NO_CHANGE) {
3518 if (status == GOT_STATUS_DELETE)
3519 goto done;
3520 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3521 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3522 goto done;
3524 if (status != GOT_STATUS_MODIFY &&
3525 status != GOT_STATUS_MISSING) {
3526 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3527 goto done;
3531 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3532 if (dirfd != -1) {
3533 if (unlinkat(dirfd, de_name, 0) != 0) {
3534 err = got_error_from_errno2("unlinkat",
3535 ondisk_path);
3536 goto done;
3538 } else if (unlink(ondisk_path) != 0) {
3539 err = got_error_from_errno2("unlink", ondisk_path);
3540 goto done;
3543 parent = dirname(ondisk_path);
3545 if (parent == NULL) {
3546 err = got_error_from_errno2("dirname", ondisk_path);
3547 goto done;
3549 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3550 if (rmdir(parent) == -1) {
3551 if (errno != ENOTEMPTY)
3552 err = got_error_from_errno2("rmdir",
3553 parent);
3554 break;
3556 parent = dirname(parent);
3557 if (parent == NULL) {
3558 err = got_error_from_errno2("dirname", parent);
3559 goto done;
3564 got_fileindex_entry_mark_deleted_from_disk(ie);
3565 done:
3566 free(ondisk_path);
3567 if (err)
3568 return err;
3569 if (status == GOT_STATUS_DELETE)
3570 return NULL;
3571 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3572 staged_status, relpath);
3575 const struct got_error *
3576 got_worktree_schedule_delete(struct got_worktree *worktree,
3577 struct got_pathlist_head *paths, int delete_local_mods,
3578 got_worktree_delete_cb progress_cb, void *progress_arg,
3579 struct got_repository *repo, int keep_on_disk)
3581 struct got_fileindex *fileindex = NULL;
3582 char *fileindex_path = NULL;
3583 const struct got_error *err = NULL, *sync_err, *unlockerr;
3584 struct got_pathlist_entry *pe;
3585 struct schedule_deletion_args sda;
3587 err = lock_worktree(worktree, LOCK_EX);
3588 if (err)
3589 return err;
3591 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3592 if (err)
3593 goto done;
3595 sda.worktree = worktree;
3596 sda.fileindex = fileindex;
3597 sda.progress_cb = progress_cb;
3598 sda.progress_arg = progress_arg;
3599 sda.repo = repo;
3600 sda.delete_local_mods = delete_local_mods;
3601 sda.keep_on_disk = keep_on_disk;
3603 TAILQ_FOREACH(pe, paths, entry) {
3604 err = worktree_status(worktree, pe->path, fileindex, repo,
3605 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3606 if (err)
3607 break;
3609 sync_err = sync_fileindex(fileindex, fileindex_path);
3610 if (sync_err && err == NULL)
3611 err = sync_err;
3612 done:
3613 free(fileindex_path);
3614 if (fileindex)
3615 got_fileindex_free(fileindex);
3616 unlockerr = lock_worktree(worktree, LOCK_SH);
3617 if (unlockerr && err == NULL)
3618 err = unlockerr;
3619 return err;
3622 static const struct got_error *
3623 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3625 const struct got_error *err = NULL;
3626 char *line = NULL;
3627 size_t linesize = 0, n;
3628 ssize_t linelen;
3630 linelen = getline(&line, &linesize, infile);
3631 if (linelen == -1) {
3632 if (ferror(infile)) {
3633 err = got_error_from_errno("getline");
3634 goto done;
3636 return NULL;
3638 if (outfile) {
3639 n = fwrite(line, 1, linelen, outfile);
3640 if (n != linelen) {
3641 err = got_ferror(outfile, GOT_ERR_IO);
3642 goto done;
3645 if (rejectfile) {
3646 n = fwrite(line, 1, linelen, rejectfile);
3647 if (n != linelen)
3648 err = got_ferror(outfile, GOT_ERR_IO);
3650 done:
3651 free(line);
3652 return err;
3655 static const struct got_error *
3656 skip_one_line(FILE *f)
3658 char *line = NULL;
3659 size_t linesize = 0;
3660 ssize_t linelen;
3662 linelen = getline(&line, &linesize, f);
3663 if (linelen == -1) {
3664 if (ferror(f))
3665 return got_error_from_errno("getline");
3666 return NULL;
3668 free(line);
3669 return NULL;
3672 static const struct got_error *
3673 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3674 int start_old, int end_old, int start_new, int end_new,
3675 FILE *outfile, FILE *rejectfile)
3677 const struct got_error *err;
3679 /* Copy old file's lines leading up to patch. */
3680 while (!feof(f1) && *line_cur1 < start_old) {
3681 err = copy_one_line(f1, outfile, NULL);
3682 if (err)
3683 return err;
3684 (*line_cur1)++;
3686 /* Skip new file's lines leading up to patch. */
3687 while (!feof(f2) && *line_cur2 < start_new) {
3688 if (rejectfile)
3689 err = copy_one_line(f2, NULL, rejectfile);
3690 else
3691 err = skip_one_line(f2);
3692 if (err)
3693 return err;
3694 (*line_cur2)++;
3696 /* Copy patched lines. */
3697 while (!feof(f2) && *line_cur2 <= end_new) {
3698 err = copy_one_line(f2, outfile, NULL);
3699 if (err)
3700 return err;
3701 (*line_cur2)++;
3703 /* Skip over old file's replaced lines. */
3704 while (!feof(f1) && *line_cur1 <= end_old) {
3705 if (rejectfile)
3706 err = copy_one_line(f1, NULL, rejectfile);
3707 else
3708 err = skip_one_line(f1);
3709 if (err)
3710 return err;
3711 (*line_cur1)++;
3714 return NULL;
3717 static const struct got_error *
3718 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3719 FILE *outfile, FILE *rejectfile)
3721 const struct got_error *err;
3723 if (outfile) {
3724 /* Copy old file's lines until EOF. */
3725 while (!feof(f1)) {
3726 err = copy_one_line(f1, outfile, NULL);
3727 if (err)
3728 return err;
3729 (*line_cur1)++;
3732 if (rejectfile) {
3733 /* Copy new file's lines until EOF. */
3734 while (!feof(f2)) {
3735 err = copy_one_line(f2, NULL, rejectfile);
3736 if (err)
3737 return err;
3738 (*line_cur2)++;
3742 return NULL;
3745 static const struct got_error *
3746 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3747 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3748 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3749 int *line_cur2, FILE *outfile, FILE *rejectfile,
3750 got_worktree_patch_cb patch_cb, void *patch_arg)
3752 const struct got_error *err = NULL;
3753 int start_old = change->cv.a;
3754 int end_old = change->cv.b;
3755 int start_new = change->cv.c;
3756 int end_new = change->cv.d;
3757 long pos1, pos2;
3758 FILE *hunkfile;
3760 *choice = GOT_PATCH_CHOICE_NONE;
3762 hunkfile = got_opentemp();
3763 if (hunkfile == NULL)
3764 return got_error_from_errno("got_opentemp");
3766 pos1 = ftell(f1);
3767 pos2 = ftell(f2);
3769 /* XXX TODO needs error checking */
3770 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3772 if (fseek(f1, pos1, SEEK_SET) == -1) {
3773 err = got_ferror(f1, GOT_ERR_IO);
3774 goto done;
3776 if (fseek(f2, pos2, SEEK_SET) == -1) {
3777 err = got_ferror(f1, GOT_ERR_IO);
3778 goto done;
3780 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3781 err = got_ferror(hunkfile, GOT_ERR_IO);
3782 goto done;
3785 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3786 hunkfile, n, nchanges);
3787 if (err)
3788 goto done;
3790 switch (*choice) {
3791 case GOT_PATCH_CHOICE_YES:
3792 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3793 end_old, start_new, end_new, outfile, rejectfile);
3794 break;
3795 case GOT_PATCH_CHOICE_NO:
3796 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3797 end_old, start_new, end_new, rejectfile, outfile);
3798 break;
3799 case GOT_PATCH_CHOICE_QUIT:
3800 break;
3801 default:
3802 err = got_error(GOT_ERR_PATCH_CHOICE);
3803 break;
3805 done:
3806 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3807 err = got_error_from_errno("fclose");
3808 return err;
3811 struct revert_file_args {
3812 struct got_worktree *worktree;
3813 struct got_fileindex *fileindex;
3814 got_worktree_checkout_cb progress_cb;
3815 void *progress_arg;
3816 got_worktree_patch_cb patch_cb;
3817 void *patch_arg;
3818 struct got_repository *repo;
3821 static const struct got_error *
3822 create_patched_content(char **path_outfile, int reverse_patch,
3823 struct got_object_id *blob_id, const char *path2,
3824 int dirfd2, const char *de_name2,
3825 const char *relpath, struct got_repository *repo,
3826 got_worktree_patch_cb patch_cb, void *patch_arg)
3828 const struct got_error *err;
3829 struct got_blob_object *blob = NULL;
3830 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3831 int fd2 = -1;
3832 char *path1 = NULL, *id_str = NULL;
3833 struct stat sb1, sb2;
3834 struct got_diff_changes *changes = NULL;
3835 struct got_diff_state *ds = NULL;
3836 struct got_diff_args *args = NULL;
3837 struct got_diff_change *change;
3838 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3839 int n = 0;
3841 *path_outfile = NULL;
3843 err = got_object_id_str(&id_str, blob_id);
3844 if (err)
3845 return err;
3847 if (dirfd2 != -1) {
3848 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3849 if (fd2 == -1) {
3850 err = got_error_from_errno2("openat", path2);
3851 goto done;
3853 } else {
3854 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3855 if (fd2 == -1) {
3856 err = got_error_from_errno2("open", path2);
3857 goto done;
3860 if (fstat(fd2, &sb2) == -1) {
3861 err = got_error_from_errno2("fstat", path2);
3862 goto done;
3865 f2 = fdopen(fd2, "r");
3866 if (f2 == NULL) {
3867 err = got_error_from_errno2("fdopen", path2);
3868 goto done;
3870 fd2 = -1;
3872 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3873 if (err)
3874 goto done;
3876 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3877 if (err)
3878 goto done;
3880 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3881 if (err)
3882 goto done;
3884 if (stat(path1, &sb1) == -1) {
3885 err = got_error_from_errno2("stat", path1);
3886 goto done;
3889 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3890 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3891 if (err)
3892 goto done;
3894 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3895 if (err)
3896 goto done;
3898 if (fseek(f1, 0L, SEEK_SET) == -1)
3899 return got_ferror(f1, GOT_ERR_IO);
3900 if (fseek(f2, 0L, SEEK_SET) == -1)
3901 return got_ferror(f2, GOT_ERR_IO);
3902 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3903 int choice;
3904 err = apply_or_reject_change(&choice, change, ++n,
3905 changes->nchanges, ds, args, diff_flags, relpath,
3906 f1, f2, &line_cur1, &line_cur2,
3907 reverse_patch ? NULL : outfile,
3908 reverse_patch ? outfile : NULL,
3909 patch_cb, patch_arg);
3910 if (err)
3911 goto done;
3912 if (choice == GOT_PATCH_CHOICE_YES)
3913 have_content = 1;
3914 else if (choice == GOT_PATCH_CHOICE_QUIT)
3915 break;
3917 if (have_content) {
3918 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3919 reverse_patch ? NULL : outfile,
3920 reverse_patch ? outfile : NULL);
3921 if (err)
3922 goto done;
3924 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3925 err = got_error_from_errno2("chmod", path2);
3926 goto done;
3929 done:
3930 free(id_str);
3931 if (blob)
3932 got_object_blob_close(blob);
3933 if (f1 && fclose(f1) == EOF && err == NULL)
3934 err = got_error_from_errno2("fclose", path1);
3935 if (f2 && fclose(f2) == EOF && err == NULL)
3936 err = got_error_from_errno2("fclose", path2);
3937 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3938 err = got_error_from_errno2("close", path2);
3939 if (outfile && fclose(outfile) == EOF && err == NULL)
3940 err = got_error_from_errno2("fclose", *path_outfile);
3941 if (path1 && unlink(path1) == -1 && err == NULL)
3942 err = got_error_from_errno2("unlink", path1);
3943 if (err || !have_content) {
3944 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3945 err = got_error_from_errno2("unlink", *path_outfile);
3946 free(*path_outfile);
3947 *path_outfile = NULL;
3949 free(args);
3950 if (ds) {
3951 got_diff_state_free(ds);
3952 free(ds);
3954 if (changes)
3955 got_diff_free_changes(changes);
3956 free(path1);
3957 return err;
3960 static const struct got_error *
3961 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3962 const char *relpath, struct got_object_id *blob_id,
3963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3964 int dirfd, const char *de_name)
3966 struct revert_file_args *a = arg;
3967 const struct got_error *err = NULL;
3968 char *parent_path = NULL;
3969 struct got_fileindex_entry *ie;
3970 struct got_tree_object *tree = NULL;
3971 struct got_object_id *tree_id = NULL;
3972 const struct got_tree_entry *te = NULL;
3973 char *tree_path = NULL, *te_name;
3974 char *ondisk_path = NULL, *path_content = NULL;
3975 struct got_blob_object *blob = NULL;
3977 /* Reverting a staged deletion is a no-op. */
3978 if (status == GOT_STATUS_DELETE &&
3979 staged_status != GOT_STATUS_NO_CHANGE)
3980 return NULL;
3982 if (status == GOT_STATUS_UNVERSIONED)
3983 return (*a->progress_cb)(a->progress_arg,
3984 GOT_STATUS_UNVERSIONED, relpath);
3986 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3987 if (ie == NULL)
3988 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3990 /* Construct in-repository path of tree which contains this blob. */
3991 err = got_path_dirname(&parent_path, ie->path);
3992 if (err) {
3993 if (err->code != GOT_ERR_BAD_PATH)
3994 goto done;
3995 parent_path = strdup("/");
3996 if (parent_path == NULL) {
3997 err = got_error_from_errno("strdup");
3998 goto done;
4001 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4002 tree_path = strdup(parent_path);
4003 if (tree_path == NULL) {
4004 err = got_error_from_errno("strdup");
4005 goto done;
4007 } else {
4008 if (got_path_is_root_dir(parent_path)) {
4009 tree_path = strdup(a->worktree->path_prefix);
4010 if (tree_path == NULL) {
4011 err = got_error_from_errno("strdup");
4012 goto done;
4014 } else {
4015 if (asprintf(&tree_path, "%s/%s",
4016 a->worktree->path_prefix, parent_path) == -1) {
4017 err = got_error_from_errno("asprintf");
4018 goto done;
4023 err = got_object_id_by_path(&tree_id, a->repo,
4024 a->worktree->base_commit_id, tree_path);
4025 if (err) {
4026 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4027 (status == GOT_STATUS_ADD ||
4028 staged_status == GOT_STATUS_ADD)))
4029 goto done;
4030 } else {
4031 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4032 if (err)
4033 goto done;
4035 te_name = basename(ie->path);
4036 if (te_name == NULL) {
4037 err = got_error_from_errno2("basename", ie->path);
4038 goto done;
4041 te = got_object_tree_find_entry(tree, te_name);
4042 if (te == NULL && status != GOT_STATUS_ADD &&
4043 staged_status != GOT_STATUS_ADD) {
4044 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4045 goto done;
4049 switch (status) {
4050 case GOT_STATUS_ADD:
4051 if (a->patch_cb) {
4052 int choice = GOT_PATCH_CHOICE_NONE;
4053 err = (*a->patch_cb)(&choice, a->patch_arg,
4054 status, ie->path, NULL, 1, 1);
4055 if (err)
4056 goto done;
4057 if (choice != GOT_PATCH_CHOICE_YES)
4058 break;
4060 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4061 ie->path);
4062 if (err)
4063 goto done;
4064 got_fileindex_entry_remove(a->fileindex, ie);
4065 break;
4066 case GOT_STATUS_DELETE:
4067 if (a->patch_cb) {
4068 int choice = GOT_PATCH_CHOICE_NONE;
4069 err = (*a->patch_cb)(&choice, a->patch_arg,
4070 status, ie->path, NULL, 1, 1);
4071 if (err)
4072 goto done;
4073 if (choice != GOT_PATCH_CHOICE_YES)
4074 break;
4076 /* fall through */
4077 case GOT_STATUS_MODIFY:
4078 case GOT_STATUS_MODE_CHANGE:
4079 case GOT_STATUS_CONFLICT:
4080 case GOT_STATUS_MISSING: {
4081 struct got_object_id id;
4082 if (staged_status == GOT_STATUS_ADD ||
4083 staged_status == GOT_STATUS_MODIFY) {
4084 memcpy(id.sha1, ie->staged_blob_sha1,
4085 SHA1_DIGEST_LENGTH);
4086 } else
4087 memcpy(id.sha1, ie->blob_sha1,
4088 SHA1_DIGEST_LENGTH);
4089 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4090 if (err)
4091 goto done;
4093 if (asprintf(&ondisk_path, "%s/%s",
4094 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4095 err = got_error_from_errno("asprintf");
4096 goto done;
4099 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4100 status == GOT_STATUS_CONFLICT)) {
4101 err = create_patched_content(&path_content, 1, &id,
4102 ondisk_path, dirfd, de_name, ie->path, a->repo,
4103 a->patch_cb, a->patch_arg);
4104 if (err || path_content == NULL)
4105 break;
4106 if (rename(path_content, ondisk_path) == -1) {
4107 err = got_error_from_errno3("rename",
4108 path_content, ondisk_path);
4109 goto done;
4111 } else {
4112 err = install_blob(a->worktree, ondisk_path, ie->path,
4113 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4114 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4115 a->repo, a->progress_cb, a->progress_arg);
4116 if (err)
4117 goto done;
4118 if (status == GOT_STATUS_DELETE ||
4119 status == GOT_STATUS_MODE_CHANGE) {
4120 err = got_fileindex_entry_update(ie,
4121 ondisk_path, blob->id.sha1,
4122 a->worktree->base_commit_id->sha1, 1);
4123 if (err)
4124 goto done;
4127 break;
4129 default:
4130 break;
4132 done:
4133 free(ondisk_path);
4134 free(path_content);
4135 free(parent_path);
4136 free(tree_path);
4137 if (blob)
4138 got_object_blob_close(blob);
4139 if (tree)
4140 got_object_tree_close(tree);
4141 free(tree_id);
4142 return err;
4145 const struct got_error *
4146 got_worktree_revert(struct got_worktree *worktree,
4147 struct got_pathlist_head *paths,
4148 got_worktree_checkout_cb progress_cb, void *progress_arg,
4149 got_worktree_patch_cb patch_cb, void *patch_arg,
4150 struct got_repository *repo)
4152 struct got_fileindex *fileindex = NULL;
4153 char *fileindex_path = NULL;
4154 const struct got_error *err = NULL, *unlockerr = NULL;
4155 const struct got_error *sync_err = NULL;
4156 struct got_pathlist_entry *pe;
4157 struct revert_file_args rfa;
4159 err = lock_worktree(worktree, LOCK_EX);
4160 if (err)
4161 return err;
4163 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4164 if (err)
4165 goto done;
4167 rfa.worktree = worktree;
4168 rfa.fileindex = fileindex;
4169 rfa.progress_cb = progress_cb;
4170 rfa.progress_arg = progress_arg;
4171 rfa.patch_cb = patch_cb;
4172 rfa.patch_arg = patch_arg;
4173 rfa.repo = repo;
4174 TAILQ_FOREACH(pe, paths, entry) {
4175 err = worktree_status(worktree, pe->path, fileindex, repo,
4176 revert_file, &rfa, NULL, NULL, 0, 0);
4177 if (err)
4178 break;
4180 sync_err = sync_fileindex(fileindex, fileindex_path);
4181 if (sync_err && err == NULL)
4182 err = sync_err;
4183 done:
4184 free(fileindex_path);
4185 if (fileindex)
4186 got_fileindex_free(fileindex);
4187 unlockerr = lock_worktree(worktree, LOCK_SH);
4188 if (unlockerr && err == NULL)
4189 err = unlockerr;
4190 return err;
4193 static void
4194 free_commitable(struct got_commitable *ct)
4196 free(ct->path);
4197 free(ct->in_repo_path);
4198 free(ct->ondisk_path);
4199 free(ct->blob_id);
4200 free(ct->base_blob_id);
4201 free(ct->staged_blob_id);
4202 free(ct->base_commit_id);
4203 free(ct);
4206 struct collect_commitables_arg {
4207 struct got_pathlist_head *commitable_paths;
4208 struct got_repository *repo;
4209 struct got_worktree *worktree;
4210 int have_staged_files;
4213 static const struct got_error *
4214 collect_commitables(void *arg, unsigned char status,
4215 unsigned char staged_status, const char *relpath,
4216 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4217 struct got_object_id *commit_id, int dirfd, const char *de_name)
4219 struct collect_commitables_arg *a = arg;
4220 const struct got_error *err = NULL;
4221 struct got_commitable *ct = NULL;
4222 struct got_pathlist_entry *new = NULL;
4223 char *parent_path = NULL, *path = NULL;
4224 struct stat sb;
4226 if (a->have_staged_files) {
4227 if (staged_status != GOT_STATUS_MODIFY &&
4228 staged_status != GOT_STATUS_ADD &&
4229 staged_status != GOT_STATUS_DELETE)
4230 return NULL;
4231 } else {
4232 if (status == GOT_STATUS_CONFLICT)
4233 return got_error(GOT_ERR_COMMIT_CONFLICT);
4235 if (status != GOT_STATUS_MODIFY &&
4236 status != GOT_STATUS_MODE_CHANGE &&
4237 status != GOT_STATUS_ADD &&
4238 status != GOT_STATUS_DELETE)
4239 return NULL;
4242 if (asprintf(&path, "/%s", relpath) == -1) {
4243 err = got_error_from_errno("asprintf");
4244 goto done;
4246 if (strcmp(path, "/") == 0) {
4247 parent_path = strdup("");
4248 if (parent_path == NULL)
4249 return got_error_from_errno("strdup");
4250 } else {
4251 err = got_path_dirname(&parent_path, path);
4252 if (err)
4253 return err;
4256 ct = calloc(1, sizeof(*ct));
4257 if (ct == NULL) {
4258 err = got_error_from_errno("calloc");
4259 goto done;
4262 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4263 relpath) == -1) {
4264 err = got_error_from_errno("asprintf");
4265 goto done;
4267 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4268 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4269 } else {
4270 if (dirfd != -1) {
4271 if (fstatat(dirfd, de_name, &sb,
4272 AT_SYMLINK_NOFOLLOW) == -1) {
4273 err = got_error_from_errno2("fstatat",
4274 ct->ondisk_path);
4275 goto done;
4277 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4278 err = got_error_from_errno2("lstat", ct->ondisk_path);
4279 goto done;
4281 ct->mode = sb.st_mode;
4284 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4285 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4286 relpath) == -1) {
4287 err = got_error_from_errno("asprintf");
4288 goto done;
4291 ct->status = status;
4292 ct->staged_status = staged_status;
4293 ct->blob_id = NULL; /* will be filled in when blob gets created */
4294 if (ct->status != GOT_STATUS_ADD &&
4295 ct->staged_status != GOT_STATUS_ADD) {
4296 ct->base_blob_id = got_object_id_dup(blob_id);
4297 if (ct->base_blob_id == NULL) {
4298 err = got_error_from_errno("got_object_id_dup");
4299 goto done;
4301 ct->base_commit_id = got_object_id_dup(commit_id);
4302 if (ct->base_commit_id == NULL) {
4303 err = got_error_from_errno("got_object_id_dup");
4304 goto done;
4307 if (ct->staged_status == GOT_STATUS_ADD ||
4308 ct->staged_status == GOT_STATUS_MODIFY) {
4309 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4310 if (ct->staged_blob_id == NULL) {
4311 err = got_error_from_errno("got_object_id_dup");
4312 goto done;
4315 ct->path = strdup(path);
4316 if (ct->path == NULL) {
4317 err = got_error_from_errno("strdup");
4318 goto done;
4320 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4321 done:
4322 if (ct && (err || new == NULL))
4323 free_commitable(ct);
4324 free(parent_path);
4325 free(path);
4326 return err;
4329 static const struct got_error *write_tree(struct got_object_id **, int *,
4330 struct got_tree_object *, const char *, struct got_pathlist_head *,
4331 got_worktree_status_cb status_cb, void *status_arg,
4332 struct got_repository *);
4334 static const struct got_error *
4335 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4336 struct got_tree_entry *te, const char *parent_path,
4337 struct got_pathlist_head *commitable_paths,
4338 got_worktree_status_cb status_cb, void *status_arg,
4339 struct got_repository *repo)
4341 const struct got_error *err = NULL;
4342 struct got_tree_object *subtree;
4343 char *subpath;
4345 if (asprintf(&subpath, "%s%s%s", parent_path,
4346 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4347 return got_error_from_errno("asprintf");
4349 err = got_object_open_as_tree(&subtree, repo, &te->id);
4350 if (err)
4351 return err;
4353 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4354 commitable_paths, status_cb, status_arg, repo);
4355 got_object_tree_close(subtree);
4356 free(subpath);
4357 return err;
4360 static const struct got_error *
4361 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4363 const struct got_error *err = NULL;
4364 char *ct_parent_path = NULL;
4366 *match = 0;
4368 if (strchr(ct->in_repo_path, '/') == NULL) {
4369 *match = got_path_is_root_dir(path);
4370 return NULL;
4373 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4374 if (err)
4375 return err;
4376 *match = (strcmp(path, ct_parent_path) == 0);
4377 free(ct_parent_path);
4378 return err;
4381 static mode_t
4382 get_ct_file_mode(struct got_commitable *ct)
4384 if (S_ISLNK(ct->mode))
4385 return S_IFLNK;
4387 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4390 static const struct got_error *
4391 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4392 struct got_tree_entry *te, struct got_commitable *ct)
4394 const struct got_error *err = NULL;
4396 *new_te = NULL;
4398 err = got_object_tree_entry_dup(new_te, te);
4399 if (err)
4400 goto done;
4402 (*new_te)->mode = get_ct_file_mode(ct);
4404 if (ct->staged_status == GOT_STATUS_MODIFY)
4405 memcpy(&(*new_te)->id, ct->staged_blob_id,
4406 sizeof((*new_te)->id));
4407 else
4408 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4409 done:
4410 if (err && *new_te) {
4411 free(*new_te);
4412 *new_te = NULL;
4414 return err;
4417 static const struct got_error *
4418 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4419 struct got_commitable *ct)
4421 const struct got_error *err = NULL;
4422 char *ct_name;
4424 *new_te = NULL;
4426 *new_te = calloc(1, sizeof(**new_te));
4427 if (*new_te == NULL)
4428 return got_error_from_errno("calloc");
4430 ct_name = basename(ct->path);
4431 if (ct_name == NULL) {
4432 err = got_error_from_errno2("basename", ct->path);
4433 goto done;
4435 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4436 sizeof((*new_te)->name)) {
4437 err = got_error(GOT_ERR_NO_SPACE);
4438 goto done;
4441 (*new_te)->mode = get_ct_file_mode(ct);
4443 if (ct->staged_status == GOT_STATUS_ADD)
4444 memcpy(&(*new_te)->id, ct->staged_blob_id,
4445 sizeof((*new_te)->id));
4446 else
4447 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4448 done:
4449 if (err && *new_te) {
4450 free(*new_te);
4451 *new_te = NULL;
4453 return err;
4456 static const struct got_error *
4457 insert_tree_entry(struct got_tree_entry *new_te,
4458 struct got_pathlist_head *paths)
4460 const struct got_error *err = NULL;
4461 struct got_pathlist_entry *new_pe;
4463 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4464 if (err)
4465 return err;
4466 if (new_pe == NULL)
4467 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4468 return NULL;
4471 static const struct got_error *
4472 report_ct_status(struct got_commitable *ct,
4473 got_worktree_status_cb status_cb, void *status_arg)
4475 const char *ct_path = ct->path;
4476 unsigned char status;
4478 while (ct_path[0] == '/')
4479 ct_path++;
4481 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4482 status = ct->staged_status;
4483 else
4484 status = ct->status;
4486 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4487 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4490 static const struct got_error *
4491 match_modified_subtree(int *modified, struct got_tree_entry *te,
4492 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4494 const struct got_error *err = NULL;
4495 struct got_pathlist_entry *pe;
4496 char *te_path;
4498 *modified = 0;
4500 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4501 got_path_is_root_dir(base_tree_path) ? "" : "/",
4502 te->name) == -1)
4503 return got_error_from_errno("asprintf");
4505 TAILQ_FOREACH(pe, commitable_paths, entry) {
4506 struct got_commitable *ct = pe->data;
4507 *modified = got_path_is_child(ct->in_repo_path, te_path,
4508 strlen(te_path));
4509 if (*modified)
4510 break;
4513 free(te_path);
4514 return err;
4517 static const struct got_error *
4518 match_deleted_or_modified_ct(struct got_commitable **ctp,
4519 struct got_tree_entry *te, const char *base_tree_path,
4520 struct got_pathlist_head *commitable_paths)
4522 const struct got_error *err = NULL;
4523 struct got_pathlist_entry *pe;
4525 *ctp = NULL;
4527 TAILQ_FOREACH(pe, commitable_paths, entry) {
4528 struct got_commitable *ct = pe->data;
4529 char *ct_name = NULL;
4530 int path_matches;
4532 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4533 if (ct->status != GOT_STATUS_MODIFY &&
4534 ct->status != GOT_STATUS_MODE_CHANGE &&
4535 ct->status != GOT_STATUS_DELETE)
4536 continue;
4537 } else {
4538 if (ct->staged_status != GOT_STATUS_MODIFY &&
4539 ct->staged_status != GOT_STATUS_DELETE)
4540 continue;
4543 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4544 continue;
4546 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4547 if (err)
4548 return err;
4549 if (!path_matches)
4550 continue;
4552 ct_name = basename(pe->path);
4553 if (ct_name == NULL)
4554 return got_error_from_errno2("basename", pe->path);
4556 if (strcmp(te->name, ct_name) != 0)
4557 continue;
4559 *ctp = ct;
4560 break;
4563 return err;
4566 static const struct got_error *
4567 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4568 const char *child_path, const char *path_base_tree,
4569 struct got_pathlist_head *commitable_paths,
4570 got_worktree_status_cb status_cb, void *status_arg,
4571 struct got_repository *repo)
4573 const struct got_error *err = NULL;
4574 struct got_tree_entry *new_te;
4575 char *subtree_path;
4576 struct got_object_id *id = NULL;
4577 int nentries;
4579 *new_tep = NULL;
4581 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4582 got_path_is_root_dir(path_base_tree) ? "" : "/",
4583 child_path) == -1)
4584 return got_error_from_errno("asprintf");
4586 new_te = calloc(1, sizeof(*new_te));
4587 if (new_te == NULL)
4588 return got_error_from_errno("calloc");
4589 new_te->mode = S_IFDIR;
4591 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4592 sizeof(new_te->name)) {
4593 err = got_error(GOT_ERR_NO_SPACE);
4594 goto done;
4596 err = write_tree(&id, &nentries, NULL, subtree_path,
4597 commitable_paths, status_cb, status_arg, repo);
4598 if (err) {
4599 free(new_te);
4600 goto done;
4602 memcpy(&new_te->id, id, sizeof(new_te->id));
4603 done:
4604 free(id);
4605 free(subtree_path);
4606 if (err == NULL)
4607 *new_tep = new_te;
4608 return err;
4611 static const struct got_error *
4612 write_tree(struct got_object_id **new_tree_id, int *nentries,
4613 struct got_tree_object *base_tree, const char *path_base_tree,
4614 struct got_pathlist_head *commitable_paths,
4615 got_worktree_status_cb status_cb, void *status_arg,
4616 struct got_repository *repo)
4618 const struct got_error *err = NULL;
4619 struct got_pathlist_head paths;
4620 struct got_tree_entry *te, *new_te = NULL;
4621 struct got_pathlist_entry *pe;
4623 TAILQ_INIT(&paths);
4624 *nentries = 0;
4626 /* Insert, and recurse into, newly added entries first. */
4627 TAILQ_FOREACH(pe, commitable_paths, entry) {
4628 struct got_commitable *ct = pe->data;
4629 char *child_path = NULL, *slash;
4631 if ((ct->status != GOT_STATUS_ADD &&
4632 ct->staged_status != GOT_STATUS_ADD) ||
4633 (ct->flags & GOT_COMMITABLE_ADDED))
4634 continue;
4636 if (!got_path_is_child(pe->path, path_base_tree,
4637 strlen(path_base_tree)))
4638 continue;
4640 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4641 pe->path);
4642 if (err)
4643 goto done;
4645 slash = strchr(child_path, '/');
4646 if (slash == NULL) {
4647 err = alloc_added_blob_tree_entry(&new_te, ct);
4648 if (err)
4649 goto done;
4650 err = report_ct_status(ct, status_cb, status_arg);
4651 if (err)
4652 goto done;
4653 ct->flags |= GOT_COMMITABLE_ADDED;
4654 err = insert_tree_entry(new_te, &paths);
4655 if (err)
4656 goto done;
4657 (*nentries)++;
4658 } else {
4659 *slash = '\0'; /* trim trailing path components */
4660 if (base_tree == NULL ||
4661 got_object_tree_find_entry(base_tree, child_path)
4662 == NULL) {
4663 err = make_subtree_for_added_blob(&new_te,
4664 child_path, path_base_tree,
4665 commitable_paths, status_cb, status_arg,
4666 repo);
4667 if (err)
4668 goto done;
4669 err = insert_tree_entry(new_te, &paths);
4670 if (err)
4671 goto done;
4672 (*nentries)++;
4677 if (base_tree) {
4678 int i, nbase_entries;
4679 /* Handle modified and deleted entries. */
4680 nbase_entries = got_object_tree_get_nentries(base_tree);
4681 for (i = 0; i < nbase_entries; i++) {
4682 struct got_commitable *ct = NULL;
4684 te = got_object_tree_get_entry(base_tree, i);
4685 if (got_object_tree_entry_is_submodule(te)) {
4686 /* Entry is a submodule; just copy it. */
4687 err = got_object_tree_entry_dup(&new_te, te);
4688 if (err)
4689 goto done;
4690 err = insert_tree_entry(new_te, &paths);
4691 if (err)
4692 goto done;
4693 (*nentries)++;
4694 continue;
4697 if (S_ISDIR(te->mode)) {
4698 int modified;
4699 err = got_object_tree_entry_dup(&new_te, te);
4700 if (err)
4701 goto done;
4702 err = match_modified_subtree(&modified, te,
4703 path_base_tree, commitable_paths);
4704 if (err)
4705 goto done;
4706 /* Avoid recursion into unmodified subtrees. */
4707 if (modified) {
4708 struct got_object_id *new_id;
4709 int nsubentries;
4710 err = write_subtree(&new_id,
4711 &nsubentries, te,
4712 path_base_tree, commitable_paths,
4713 status_cb, status_arg, repo);
4714 if (err)
4715 goto done;
4716 if (nsubentries == 0) {
4717 /* All entries were deleted. */
4718 free(new_id);
4719 continue;
4721 memcpy(&new_te->id, new_id,
4722 sizeof(new_te->id));
4723 free(new_id);
4725 err = insert_tree_entry(new_te, &paths);
4726 if (err)
4727 goto done;
4728 (*nentries)++;
4729 continue;
4732 err = match_deleted_or_modified_ct(&ct, te,
4733 path_base_tree, commitable_paths);
4734 if (err)
4735 goto done;
4736 if (ct) {
4737 /* NB: Deleted entries get dropped here. */
4738 if (ct->status == GOT_STATUS_MODIFY ||
4739 ct->status == GOT_STATUS_MODE_CHANGE ||
4740 ct->staged_status == GOT_STATUS_MODIFY) {
4741 err = alloc_modified_blob_tree_entry(
4742 &new_te, te, ct);
4743 if (err)
4744 goto done;
4745 err = insert_tree_entry(new_te, &paths);
4746 if (err)
4747 goto done;
4748 (*nentries)++;
4750 err = report_ct_status(ct, status_cb,
4751 status_arg);
4752 if (err)
4753 goto done;
4754 } else {
4755 /* Entry is unchanged; just copy it. */
4756 err = got_object_tree_entry_dup(&new_te, te);
4757 if (err)
4758 goto done;
4759 err = insert_tree_entry(new_te, &paths);
4760 if (err)
4761 goto done;
4762 (*nentries)++;
4767 /* Write new list of entries; deleted entries have been dropped. */
4768 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4769 done:
4770 got_pathlist_free(&paths);
4771 return err;
4774 static const struct got_error *
4775 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4776 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4777 int have_staged_files)
4779 const struct got_error *err = NULL;
4780 struct got_pathlist_entry *pe;
4782 TAILQ_FOREACH(pe, commitable_paths, entry) {
4783 struct got_fileindex_entry *ie;
4784 struct got_commitable *ct = pe->data;
4786 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4787 if (ie) {
4788 if (ct->status == GOT_STATUS_DELETE ||
4789 ct->staged_status == GOT_STATUS_DELETE) {
4790 got_fileindex_entry_remove(fileindex, ie);
4791 } else if (ct->staged_status == GOT_STATUS_ADD ||
4792 ct->staged_status == GOT_STATUS_MODIFY) {
4793 got_fileindex_entry_stage_set(ie,
4794 GOT_FILEIDX_STAGE_NONE);
4795 err = got_fileindex_entry_update(ie,
4796 ct->ondisk_path, ct->staged_blob_id->sha1,
4797 new_base_commit_id->sha1,
4798 !have_staged_files);
4799 } else
4800 err = got_fileindex_entry_update(ie,
4801 ct->ondisk_path, ct->blob_id->sha1,
4802 new_base_commit_id->sha1,
4803 !have_staged_files);
4804 } else {
4805 err = got_fileindex_entry_alloc(&ie, pe->path);
4806 if (err)
4807 break;
4808 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4809 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4810 if (err) {
4811 got_fileindex_entry_free(ie);
4812 break;
4814 err = got_fileindex_entry_add(fileindex, ie);
4815 if (err) {
4816 got_fileindex_entry_free(ie);
4817 break;
4821 return err;
4825 static const struct got_error *
4826 check_out_of_date(const char *in_repo_path, unsigned char status,
4827 unsigned char staged_status, struct got_object_id *base_blob_id,
4828 struct got_object_id *base_commit_id,
4829 struct got_object_id *head_commit_id, struct got_repository *repo,
4830 int ood_errcode)
4832 const struct got_error *err = NULL;
4833 struct got_object_id *id = NULL;
4835 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4836 /* Trivial case: base commit == head commit */
4837 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4838 return NULL;
4840 * Ensure file content which local changes were based
4841 * on matches file content in the branch head.
4843 err = got_object_id_by_path(&id, repo, head_commit_id,
4844 in_repo_path);
4845 if (err) {
4846 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4847 err = got_error(ood_errcode);
4848 goto done;
4849 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4850 err = got_error(ood_errcode);
4851 } else {
4852 /* Require that added files don't exist in the branch head. */
4853 err = got_object_id_by_path(&id, repo, head_commit_id,
4854 in_repo_path);
4855 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4856 goto done;
4857 err = id ? got_error(ood_errcode) : NULL;
4859 done:
4860 free(id);
4861 return err;
4864 const struct got_error *
4865 commit_worktree(struct got_object_id **new_commit_id,
4866 struct got_pathlist_head *commitable_paths,
4867 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4868 const char *author, const char *committer,
4869 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4870 got_worktree_status_cb status_cb, void *status_arg,
4871 struct got_repository *repo)
4873 const struct got_error *err = NULL, *unlockerr = NULL;
4874 struct got_pathlist_entry *pe;
4875 const char *head_ref_name = NULL;
4876 struct got_commit_object *head_commit = NULL;
4877 struct got_reference *head_ref2 = NULL;
4878 struct got_object_id *head_commit_id2 = NULL;
4879 struct got_tree_object *head_tree = NULL;
4880 struct got_object_id *new_tree_id = NULL;
4881 int nentries;
4882 struct got_object_id_queue parent_ids;
4883 struct got_object_qid *pid = NULL;
4884 char *logmsg = NULL;
4886 *new_commit_id = NULL;
4888 SIMPLEQ_INIT(&parent_ids);
4890 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4891 if (err)
4892 goto done;
4894 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4895 if (err)
4896 goto done;
4898 if (commit_msg_cb != NULL) {
4899 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4900 if (err)
4901 goto done;
4904 if (logmsg == NULL || strlen(logmsg) == 0) {
4905 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4906 goto done;
4909 /* Create blobs from added and modified files and record their IDs. */
4910 TAILQ_FOREACH(pe, commitable_paths, entry) {
4911 struct got_commitable *ct = pe->data;
4912 char *ondisk_path;
4914 /* Blobs for staged files already exist. */
4915 if (ct->staged_status == GOT_STATUS_ADD ||
4916 ct->staged_status == GOT_STATUS_MODIFY)
4917 continue;
4919 if (ct->status != GOT_STATUS_ADD &&
4920 ct->status != GOT_STATUS_MODIFY &&
4921 ct->status != GOT_STATUS_MODE_CHANGE)
4922 continue;
4924 if (asprintf(&ondisk_path, "%s/%s",
4925 worktree->root_path, pe->path) == -1) {
4926 err = got_error_from_errno("asprintf");
4927 goto done;
4929 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4930 if (err) {
4931 free(ondisk_path);
4932 goto done;
4936 * When comitting a symlink we convert "bad" symlinks (those
4937 * which point outside the work tree or into .got) to regular
4938 * files. This way, the post-commit work tree state matches
4939 * a fresh checkout of the tree which was committed.
4941 if (S_ISLNK(get_ct_file_mode(ct))) {
4942 struct got_blob_object *blob;
4943 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
4944 PATH_MAX);
4945 if (err) {
4946 free(ondisk_path);
4947 goto done;
4949 err = install_symlink(worktree, ondisk_path, ct->path,
4950 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
4951 0, 0, repo, NULL, NULL);
4952 got_object_blob_close(blob);
4953 if (err) {
4954 free(ondisk_path);
4955 goto done;
4958 free(ondisk_path);
4961 /* Recursively write new tree objects. */
4962 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4963 commitable_paths, status_cb, status_arg, repo);
4964 if (err)
4965 goto done;
4967 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4968 if (err)
4969 goto done;
4970 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4971 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4972 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4973 got_object_qid_free(pid);
4974 if (logmsg != NULL)
4975 free(logmsg);
4976 if (err)
4977 goto done;
4979 /* Check if a concurrent commit to our branch has occurred. */
4980 head_ref_name = got_worktree_get_head_ref_name(worktree);
4981 if (head_ref_name == NULL) {
4982 err = got_error_from_errno("got_worktree_get_head_ref_name");
4983 goto done;
4985 /* Lock the reference here to prevent concurrent modification. */
4986 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4987 if (err)
4988 goto done;
4989 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4990 if (err)
4991 goto done;
4992 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4993 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4994 goto done;
4996 /* Update branch head in repository. */
4997 err = got_ref_change_ref(head_ref2, *new_commit_id);
4998 if (err)
4999 goto done;
5000 err = got_ref_write(head_ref2, repo);
5001 if (err)
5002 goto done;
5004 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5005 if (err)
5006 goto done;
5008 err = ref_base_commit(worktree, repo);
5009 if (err)
5010 goto done;
5011 done:
5012 if (head_tree)
5013 got_object_tree_close(head_tree);
5014 if (head_commit)
5015 got_object_commit_close(head_commit);
5016 free(head_commit_id2);
5017 if (head_ref2) {
5018 unlockerr = got_ref_unlock(head_ref2);
5019 if (unlockerr && err == NULL)
5020 err = unlockerr;
5021 got_ref_close(head_ref2);
5023 return err;
5026 static const struct got_error *
5027 check_path_is_commitable(const char *path,
5028 struct got_pathlist_head *commitable_paths)
5030 struct got_pathlist_entry *cpe = NULL;
5031 size_t path_len = strlen(path);
5033 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5034 struct got_commitable *ct = cpe->data;
5035 const char *ct_path = ct->path;
5037 while (ct_path[0] == '/')
5038 ct_path++;
5040 if (strcmp(path, ct_path) == 0 ||
5041 got_path_is_child(ct_path, path, path_len))
5042 break;
5045 if (cpe == NULL)
5046 return got_error_path(path, GOT_ERR_BAD_PATH);
5048 return NULL;
5051 static const struct got_error *
5052 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5054 int *have_staged_files = arg;
5056 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5057 *have_staged_files = 1;
5058 return got_error(GOT_ERR_CANCELLED);
5061 return NULL;
5064 static const struct got_error *
5065 check_non_staged_files(struct got_fileindex *fileindex,
5066 struct got_pathlist_head *paths)
5068 struct got_pathlist_entry *pe;
5069 struct got_fileindex_entry *ie;
5071 TAILQ_FOREACH(pe, paths, entry) {
5072 if (pe->path[0] == '\0')
5073 continue;
5074 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5075 if (ie == NULL)
5076 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5077 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5078 return got_error_path(pe->path,
5079 GOT_ERR_FILE_NOT_STAGED);
5082 return NULL;
5085 const struct got_error *
5086 got_worktree_commit(struct got_object_id **new_commit_id,
5087 struct got_worktree *worktree, struct got_pathlist_head *paths,
5088 const char *author, const char *committer,
5089 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5090 got_worktree_status_cb status_cb, void *status_arg,
5091 struct got_repository *repo)
5093 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5094 struct got_fileindex *fileindex = NULL;
5095 char *fileindex_path = NULL;
5096 struct got_pathlist_head commitable_paths;
5097 struct collect_commitables_arg cc_arg;
5098 struct got_pathlist_entry *pe;
5099 struct got_reference *head_ref = NULL;
5100 struct got_object_id *head_commit_id = NULL;
5101 int have_staged_files = 0;
5103 *new_commit_id = NULL;
5105 TAILQ_INIT(&commitable_paths);
5107 err = lock_worktree(worktree, LOCK_EX);
5108 if (err)
5109 goto done;
5111 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5112 if (err)
5113 goto done;
5115 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5116 if (err)
5117 goto done;
5119 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5120 if (err)
5121 goto done;
5123 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5124 &have_staged_files);
5125 if (err && err->code != GOT_ERR_CANCELLED)
5126 goto done;
5127 if (have_staged_files) {
5128 err = check_non_staged_files(fileindex, paths);
5129 if (err)
5130 goto done;
5133 cc_arg.commitable_paths = &commitable_paths;
5134 cc_arg.worktree = worktree;
5135 cc_arg.repo = repo;
5136 cc_arg.have_staged_files = have_staged_files;
5137 TAILQ_FOREACH(pe, paths, entry) {
5138 err = worktree_status(worktree, pe->path, fileindex, repo,
5139 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5140 if (err)
5141 goto done;
5144 if (TAILQ_EMPTY(&commitable_paths)) {
5145 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5146 goto done;
5149 TAILQ_FOREACH(pe, paths, entry) {
5150 err = check_path_is_commitable(pe->path, &commitable_paths);
5151 if (err)
5152 goto done;
5155 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5156 struct got_commitable *ct = pe->data;
5157 const char *ct_path = ct->in_repo_path;
5159 while (ct_path[0] == '/')
5160 ct_path++;
5161 err = check_out_of_date(ct_path, ct->status,
5162 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5163 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5164 if (err)
5165 goto done;
5169 err = commit_worktree(new_commit_id, &commitable_paths,
5170 head_commit_id, worktree, author, committer,
5171 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5172 if (err)
5173 goto done;
5175 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5176 fileindex, have_staged_files);
5177 sync_err = sync_fileindex(fileindex, fileindex_path);
5178 if (sync_err && err == NULL)
5179 err = sync_err;
5180 done:
5181 if (fileindex)
5182 got_fileindex_free(fileindex);
5183 free(fileindex_path);
5184 unlockerr = lock_worktree(worktree, LOCK_SH);
5185 if (unlockerr && err == NULL)
5186 err = unlockerr;
5187 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5188 struct got_commitable *ct = pe->data;
5189 free_commitable(ct);
5191 got_pathlist_free(&commitable_paths);
5192 return err;
5195 const char *
5196 got_commitable_get_path(struct got_commitable *ct)
5198 return ct->path;
5201 unsigned int
5202 got_commitable_get_status(struct got_commitable *ct)
5204 return ct->status;
5207 struct check_rebase_ok_arg {
5208 struct got_worktree *worktree;
5209 struct got_repository *repo;
5212 static const struct got_error *
5213 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5215 const struct got_error *err = NULL;
5216 struct check_rebase_ok_arg *a = arg;
5217 unsigned char status;
5218 struct stat sb;
5219 char *ondisk_path;
5221 /* Reject rebase of a work tree with mixed base commits. */
5222 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5223 SHA1_DIGEST_LENGTH))
5224 return got_error(GOT_ERR_MIXED_COMMITS);
5226 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5227 == -1)
5228 return got_error_from_errno("asprintf");
5230 /* Reject rebase of a work tree with modified or staged files. */
5231 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5232 free(ondisk_path);
5233 if (err)
5234 return err;
5236 if (status != GOT_STATUS_NO_CHANGE)
5237 return got_error(GOT_ERR_MODIFIED);
5238 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5239 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5241 return NULL;
5244 const struct got_error *
5245 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5246 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5247 struct got_worktree *worktree, struct got_reference *branch,
5248 struct got_repository *repo)
5250 const struct got_error *err = NULL;
5251 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5252 char *branch_ref_name = NULL;
5253 char *fileindex_path = NULL;
5254 struct check_rebase_ok_arg ok_arg;
5255 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5256 struct got_object_id *wt_branch_tip = NULL;
5258 *new_base_branch_ref = NULL;
5259 *tmp_branch = NULL;
5260 *fileindex = NULL;
5262 err = lock_worktree(worktree, LOCK_EX);
5263 if (err)
5264 return err;
5266 err = open_fileindex(fileindex, &fileindex_path, worktree);
5267 if (err)
5268 goto done;
5270 ok_arg.worktree = worktree;
5271 ok_arg.repo = repo;
5272 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5273 &ok_arg);
5274 if (err)
5275 goto done;
5277 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5278 if (err)
5279 goto done;
5281 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5282 if (err)
5283 goto done;
5285 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5286 if (err)
5287 goto done;
5289 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5290 0);
5291 if (err)
5292 goto done;
5294 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5295 if (err)
5296 goto done;
5297 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5298 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5299 goto done;
5302 err = got_ref_alloc_symref(new_base_branch_ref,
5303 new_base_branch_ref_name, wt_branch);
5304 if (err)
5305 goto done;
5306 err = got_ref_write(*new_base_branch_ref, repo);
5307 if (err)
5308 goto done;
5310 /* TODO Lock original branch's ref while rebasing? */
5312 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5313 if (err)
5314 goto done;
5316 err = got_ref_write(branch_ref, repo);
5317 if (err)
5318 goto done;
5320 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5321 worktree->base_commit_id);
5322 if (err)
5323 goto done;
5324 err = got_ref_write(*tmp_branch, repo);
5325 if (err)
5326 goto done;
5328 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5329 if (err)
5330 goto done;
5331 done:
5332 free(fileindex_path);
5333 free(tmp_branch_name);
5334 free(new_base_branch_ref_name);
5335 free(branch_ref_name);
5336 if (branch_ref)
5337 got_ref_close(branch_ref);
5338 if (wt_branch)
5339 got_ref_close(wt_branch);
5340 free(wt_branch_tip);
5341 if (err) {
5342 if (*new_base_branch_ref) {
5343 got_ref_close(*new_base_branch_ref);
5344 *new_base_branch_ref = NULL;
5346 if (*tmp_branch) {
5347 got_ref_close(*tmp_branch);
5348 *tmp_branch = NULL;
5350 if (*fileindex) {
5351 got_fileindex_free(*fileindex);
5352 *fileindex = NULL;
5354 lock_worktree(worktree, LOCK_SH);
5356 return err;
5359 const struct got_error *
5360 got_worktree_rebase_continue(struct got_object_id **commit_id,
5361 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5362 struct got_reference **branch, struct got_fileindex **fileindex,
5363 struct got_worktree *worktree, struct got_repository *repo)
5365 const struct got_error *err;
5366 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5367 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5368 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5369 char *fileindex_path = NULL;
5370 int have_staged_files = 0;
5372 *commit_id = NULL;
5373 *new_base_branch = NULL;
5374 *tmp_branch = NULL;
5375 *branch = NULL;
5376 *fileindex = NULL;
5378 err = lock_worktree(worktree, LOCK_EX);
5379 if (err)
5380 return err;
5382 err = open_fileindex(fileindex, &fileindex_path, worktree);
5383 if (err)
5384 goto done;
5386 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5387 &have_staged_files);
5388 if (err && err->code != GOT_ERR_CANCELLED)
5389 goto done;
5390 if (have_staged_files) {
5391 err = got_error(GOT_ERR_STAGED_PATHS);
5392 goto done;
5395 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5396 if (err)
5397 goto done;
5399 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5400 if (err)
5401 goto done;
5403 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5404 if (err)
5405 goto done;
5407 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5408 if (err)
5409 goto done;
5411 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5412 if (err)
5413 goto done;
5415 err = got_ref_open(branch, repo,
5416 got_ref_get_symref_target(branch_ref), 0);
5417 if (err)
5418 goto done;
5420 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5421 if (err)
5422 goto done;
5424 err = got_ref_resolve(commit_id, repo, commit_ref);
5425 if (err)
5426 goto done;
5428 err = got_ref_open(new_base_branch, repo,
5429 new_base_branch_ref_name, 0);
5430 if (err)
5431 goto done;
5433 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5434 if (err)
5435 goto done;
5436 done:
5437 free(commit_ref_name);
5438 free(branch_ref_name);
5439 free(fileindex_path);
5440 if (commit_ref)
5441 got_ref_close(commit_ref);
5442 if (branch_ref)
5443 got_ref_close(branch_ref);
5444 if (err) {
5445 free(*commit_id);
5446 *commit_id = NULL;
5447 if (*tmp_branch) {
5448 got_ref_close(*tmp_branch);
5449 *tmp_branch = NULL;
5451 if (*new_base_branch) {
5452 got_ref_close(*new_base_branch);
5453 *new_base_branch = NULL;
5455 if (*branch) {
5456 got_ref_close(*branch);
5457 *branch = NULL;
5459 if (*fileindex) {
5460 got_fileindex_free(*fileindex);
5461 *fileindex = NULL;
5463 lock_worktree(worktree, LOCK_SH);
5465 return err;
5468 const struct got_error *
5469 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5471 const struct got_error *err;
5472 char *tmp_branch_name = NULL;
5474 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5475 if (err)
5476 return err;
5478 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5479 free(tmp_branch_name);
5480 return NULL;
5483 static const struct got_error *
5484 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5485 char **logmsg, void *arg)
5487 *logmsg = arg;
5488 return NULL;
5491 static const struct got_error *
5492 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5493 const char *path, struct got_object_id *blob_id,
5494 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5495 int dirfd, const char *de_name)
5497 return NULL;
5500 struct collect_merged_paths_arg {
5501 got_worktree_checkout_cb progress_cb;
5502 void *progress_arg;
5503 struct got_pathlist_head *merged_paths;
5506 static const struct got_error *
5507 collect_merged_paths(void *arg, unsigned char status, const char *path)
5509 const struct got_error *err;
5510 struct collect_merged_paths_arg *a = arg;
5511 char *p;
5512 struct got_pathlist_entry *new;
5514 err = (*a->progress_cb)(a->progress_arg, status, path);
5515 if (err)
5516 return err;
5518 if (status != GOT_STATUS_MERGE &&
5519 status != GOT_STATUS_ADD &&
5520 status != GOT_STATUS_DELETE &&
5521 status != GOT_STATUS_CONFLICT)
5522 return NULL;
5524 p = strdup(path);
5525 if (p == NULL)
5526 return got_error_from_errno("strdup");
5528 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5529 if (err || new == NULL)
5530 free(p);
5531 return err;
5534 void
5535 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5537 struct got_pathlist_entry *pe;
5539 TAILQ_FOREACH(pe, merged_paths, entry)
5540 free((char *)pe->path);
5542 got_pathlist_free(merged_paths);
5545 static const struct got_error *
5546 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5547 int is_rebase, struct got_repository *repo)
5549 const struct got_error *err;
5550 struct got_reference *commit_ref = NULL;
5552 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5553 if (err) {
5554 if (err->code != GOT_ERR_NOT_REF)
5555 goto done;
5556 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5557 if (err)
5558 goto done;
5559 err = got_ref_write(commit_ref, repo);
5560 if (err)
5561 goto done;
5562 } else if (is_rebase) {
5563 struct got_object_id *stored_id;
5564 int cmp;
5566 err = got_ref_resolve(&stored_id, repo, commit_ref);
5567 if (err)
5568 goto done;
5569 cmp = got_object_id_cmp(commit_id, stored_id);
5570 free(stored_id);
5571 if (cmp != 0) {
5572 err = got_error(GOT_ERR_REBASE_COMMITID);
5573 goto done;
5576 done:
5577 if (commit_ref)
5578 got_ref_close(commit_ref);
5579 return err;
5582 static const struct got_error *
5583 rebase_merge_files(struct got_pathlist_head *merged_paths,
5584 const char *commit_ref_name, struct got_worktree *worktree,
5585 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5586 struct got_object_id *commit_id, struct got_repository *repo,
5587 got_worktree_checkout_cb progress_cb, void *progress_arg,
5588 got_cancel_cb cancel_cb, void *cancel_arg)
5590 const struct got_error *err;
5591 struct got_reference *commit_ref = NULL;
5592 struct collect_merged_paths_arg cmp_arg;
5593 char *fileindex_path;
5595 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5597 err = get_fileindex_path(&fileindex_path, worktree);
5598 if (err)
5599 return err;
5601 cmp_arg.progress_cb = progress_cb;
5602 cmp_arg.progress_arg = progress_arg;
5603 cmp_arg.merged_paths = merged_paths;
5604 err = merge_files(worktree, fileindex, fileindex_path,
5605 parent_commit_id, commit_id, repo, collect_merged_paths,
5606 &cmp_arg, cancel_cb, cancel_arg);
5607 if (commit_ref)
5608 got_ref_close(commit_ref);
5609 return err;
5612 const struct got_error *
5613 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5614 struct got_worktree *worktree, struct got_fileindex *fileindex,
5615 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5616 struct got_repository *repo,
5617 got_worktree_checkout_cb progress_cb, void *progress_arg,
5618 got_cancel_cb cancel_cb, void *cancel_arg)
5620 const struct got_error *err;
5621 char *commit_ref_name;
5623 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5624 if (err)
5625 return err;
5627 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5628 if (err)
5629 goto done;
5631 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5632 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5633 progress_arg, cancel_cb, cancel_arg);
5634 done:
5635 free(commit_ref_name);
5636 return err;
5639 const struct got_error *
5640 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5641 struct got_worktree *worktree, struct got_fileindex *fileindex,
5642 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5643 struct got_repository *repo,
5644 got_worktree_checkout_cb progress_cb, void *progress_arg,
5645 got_cancel_cb cancel_cb, void *cancel_arg)
5647 const struct got_error *err;
5648 char *commit_ref_name;
5650 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5651 if (err)
5652 return err;
5654 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5655 if (err)
5656 goto done;
5658 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5659 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5660 progress_arg, cancel_cb, cancel_arg);
5661 done:
5662 free(commit_ref_name);
5663 return err;
5666 static const struct got_error *
5667 rebase_commit(struct got_object_id **new_commit_id,
5668 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5669 struct got_worktree *worktree, struct got_fileindex *fileindex,
5670 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5671 const char *new_logmsg, struct got_repository *repo)
5673 const struct got_error *err, *sync_err;
5674 struct got_pathlist_head commitable_paths;
5675 struct collect_commitables_arg cc_arg;
5676 char *fileindex_path = NULL;
5677 struct got_reference *head_ref = NULL;
5678 struct got_object_id *head_commit_id = NULL;
5679 char *logmsg = NULL;
5681 TAILQ_INIT(&commitable_paths);
5682 *new_commit_id = NULL;
5684 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5686 err = get_fileindex_path(&fileindex_path, worktree);
5687 if (err)
5688 return err;
5690 cc_arg.commitable_paths = &commitable_paths;
5691 cc_arg.worktree = worktree;
5692 cc_arg.repo = repo;
5693 cc_arg.have_staged_files = 0;
5695 * If possible get the status of individual files directly to
5696 * avoid crawling the entire work tree once per rebased commit.
5697 * TODO: Ideally, merged_paths would contain a list of commitables
5698 * we could use so we could skip worktree_status() entirely.
5700 if (merged_paths) {
5701 struct got_pathlist_entry *pe;
5702 TAILQ_FOREACH(pe, merged_paths, entry) {
5703 err = worktree_status(worktree, pe->path, fileindex,
5704 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5705 0);
5706 if (err)
5707 goto done;
5709 } else {
5710 err = worktree_status(worktree, "", fileindex, repo,
5711 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5712 if (err)
5713 goto done;
5716 if (TAILQ_EMPTY(&commitable_paths)) {
5717 /* No-op change; commit will be elided. */
5718 err = got_ref_delete(commit_ref, repo);
5719 if (err)
5720 goto done;
5721 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5722 goto done;
5725 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5726 if (err)
5727 goto done;
5729 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5730 if (err)
5731 goto done;
5733 if (new_logmsg) {
5734 logmsg = strdup(new_logmsg);
5735 if (logmsg == NULL) {
5736 err = got_error_from_errno("strdup");
5737 goto done;
5739 } else {
5740 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5741 if (err)
5742 goto done;
5745 /* NB: commit_worktree will call free(logmsg) */
5746 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5747 worktree, got_object_commit_get_author(orig_commit),
5748 got_object_commit_get_committer(orig_commit),
5749 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5750 if (err)
5751 goto done;
5753 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5754 if (err)
5755 goto done;
5757 err = got_ref_delete(commit_ref, repo);
5758 if (err)
5759 goto done;
5761 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5762 fileindex, 0);
5763 sync_err = sync_fileindex(fileindex, fileindex_path);
5764 if (sync_err && err == NULL)
5765 err = sync_err;
5766 done:
5767 free(fileindex_path);
5768 free(head_commit_id);
5769 if (head_ref)
5770 got_ref_close(head_ref);
5771 if (err) {
5772 free(*new_commit_id);
5773 *new_commit_id = NULL;
5775 return err;
5778 const struct got_error *
5779 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5780 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5781 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5782 struct got_commit_object *orig_commit,
5783 struct got_object_id *orig_commit_id, struct got_repository *repo)
5785 const struct got_error *err;
5786 char *commit_ref_name;
5787 struct got_reference *commit_ref = NULL;
5788 struct got_object_id *commit_id = NULL;
5790 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5791 if (err)
5792 return err;
5794 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5795 if (err)
5796 goto done;
5797 err = got_ref_resolve(&commit_id, repo, commit_ref);
5798 if (err)
5799 goto done;
5800 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5801 err = got_error(GOT_ERR_REBASE_COMMITID);
5802 goto done;
5805 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5806 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5807 done:
5808 if (commit_ref)
5809 got_ref_close(commit_ref);
5810 free(commit_ref_name);
5811 free(commit_id);
5812 return err;
5815 const struct got_error *
5816 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5817 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5818 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5819 struct got_commit_object *orig_commit,
5820 struct got_object_id *orig_commit_id, const char *new_logmsg,
5821 struct got_repository *repo)
5823 const struct got_error *err;
5824 char *commit_ref_name;
5825 struct got_reference *commit_ref = NULL;
5827 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5828 if (err)
5829 return err;
5831 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5832 if (err)
5833 goto done;
5835 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5836 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5837 done:
5838 if (commit_ref)
5839 got_ref_close(commit_ref);
5840 free(commit_ref_name);
5841 return err;
5844 const struct got_error *
5845 got_worktree_rebase_postpone(struct got_worktree *worktree,
5846 struct got_fileindex *fileindex)
5848 if (fileindex)
5849 got_fileindex_free(fileindex);
5850 return lock_worktree(worktree, LOCK_SH);
5853 static const struct got_error *
5854 delete_ref(const char *name, struct got_repository *repo)
5856 const struct got_error *err;
5857 struct got_reference *ref;
5859 err = got_ref_open(&ref, repo, name, 0);
5860 if (err) {
5861 if (err->code == GOT_ERR_NOT_REF)
5862 return NULL;
5863 return err;
5866 err = got_ref_delete(ref, repo);
5867 got_ref_close(ref);
5868 return err;
5871 static const struct got_error *
5872 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5874 const struct got_error *err;
5875 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5876 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5878 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5879 if (err)
5880 goto done;
5881 err = delete_ref(tmp_branch_name, repo);
5882 if (err)
5883 goto done;
5885 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5886 if (err)
5887 goto done;
5888 err = delete_ref(new_base_branch_ref_name, repo);
5889 if (err)
5890 goto done;
5892 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5893 if (err)
5894 goto done;
5895 err = delete_ref(branch_ref_name, repo);
5896 if (err)
5897 goto done;
5899 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5900 if (err)
5901 goto done;
5902 err = delete_ref(commit_ref_name, repo);
5903 if (err)
5904 goto done;
5906 done:
5907 free(tmp_branch_name);
5908 free(new_base_branch_ref_name);
5909 free(branch_ref_name);
5910 free(commit_ref_name);
5911 return err;
5914 const struct got_error *
5915 got_worktree_rebase_complete(struct got_worktree *worktree,
5916 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5917 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5918 struct got_repository *repo)
5920 const struct got_error *err, *unlockerr;
5921 struct got_object_id *new_head_commit_id = NULL;
5923 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5924 if (err)
5925 return err;
5927 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5928 if (err)
5929 goto done;
5931 err = got_ref_write(rebased_branch, repo);
5932 if (err)
5933 goto done;
5935 err = got_worktree_set_head_ref(worktree, rebased_branch);
5936 if (err)
5937 goto done;
5939 err = delete_rebase_refs(worktree, repo);
5940 done:
5941 if (fileindex)
5942 got_fileindex_free(fileindex);
5943 free(new_head_commit_id);
5944 unlockerr = lock_worktree(worktree, LOCK_SH);
5945 if (unlockerr && err == NULL)
5946 err = unlockerr;
5947 return err;
5950 const struct got_error *
5951 got_worktree_rebase_abort(struct got_worktree *worktree,
5952 struct got_fileindex *fileindex, struct got_repository *repo,
5953 struct got_reference *new_base_branch,
5954 got_worktree_checkout_cb progress_cb, void *progress_arg)
5956 const struct got_error *err, *unlockerr, *sync_err;
5957 struct got_reference *resolved = NULL;
5958 struct got_object_id *commit_id = NULL;
5959 char *fileindex_path = NULL;
5960 struct revert_file_args rfa;
5961 struct got_object_id *tree_id = NULL;
5963 err = lock_worktree(worktree, LOCK_EX);
5964 if (err)
5965 return err;
5967 err = got_ref_open(&resolved, repo,
5968 got_ref_get_symref_target(new_base_branch), 0);
5969 if (err)
5970 goto done;
5972 err = got_worktree_set_head_ref(worktree, resolved);
5973 if (err)
5974 goto done;
5977 * XXX commits to the base branch could have happened while
5978 * we were busy rebasing; should we store the original commit ID
5979 * when rebase begins and read it back here?
5981 err = got_ref_resolve(&commit_id, repo, resolved);
5982 if (err)
5983 goto done;
5985 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5986 if (err)
5987 goto done;
5989 err = got_object_id_by_path(&tree_id, repo,
5990 worktree->base_commit_id, worktree->path_prefix);
5991 if (err)
5992 goto done;
5994 err = delete_rebase_refs(worktree, repo);
5995 if (err)
5996 goto done;
5998 err = get_fileindex_path(&fileindex_path, worktree);
5999 if (err)
6000 goto done;
6002 rfa.worktree = worktree;
6003 rfa.fileindex = fileindex;
6004 rfa.progress_cb = progress_cb;
6005 rfa.progress_arg = progress_arg;
6006 rfa.patch_cb = NULL;
6007 rfa.patch_arg = NULL;
6008 rfa.repo = repo;
6009 err = worktree_status(worktree, "", fileindex, repo,
6010 revert_file, &rfa, NULL, NULL, 0, 0);
6011 if (err)
6012 goto sync;
6014 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6015 repo, progress_cb, progress_arg, NULL, NULL);
6016 sync:
6017 sync_err = sync_fileindex(fileindex, fileindex_path);
6018 if (sync_err && err == NULL)
6019 err = sync_err;
6020 done:
6021 got_ref_close(resolved);
6022 free(tree_id);
6023 free(commit_id);
6024 if (fileindex)
6025 got_fileindex_free(fileindex);
6026 free(fileindex_path);
6028 unlockerr = lock_worktree(worktree, LOCK_SH);
6029 if (unlockerr && err == NULL)
6030 err = unlockerr;
6031 return err;
6034 const struct got_error *
6035 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6036 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6037 struct got_fileindex **fileindex, struct got_worktree *worktree,
6038 struct got_repository *repo)
6040 const struct got_error *err = NULL;
6041 char *tmp_branch_name = NULL;
6042 char *branch_ref_name = NULL;
6043 char *base_commit_ref_name = NULL;
6044 char *fileindex_path = NULL;
6045 struct check_rebase_ok_arg ok_arg;
6046 struct got_reference *wt_branch = NULL;
6047 struct got_reference *base_commit_ref = NULL;
6049 *tmp_branch = NULL;
6050 *branch_ref = NULL;
6051 *base_commit_id = NULL;
6052 *fileindex = NULL;
6054 err = lock_worktree(worktree, LOCK_EX);
6055 if (err)
6056 return err;
6058 err = open_fileindex(fileindex, &fileindex_path, worktree);
6059 if (err)
6060 goto done;
6062 ok_arg.worktree = worktree;
6063 ok_arg.repo = repo;
6064 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6065 &ok_arg);
6066 if (err)
6067 goto done;
6069 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6070 if (err)
6071 goto done;
6073 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6074 if (err)
6075 goto done;
6077 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6078 worktree);
6079 if (err)
6080 goto done;
6082 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6083 0);
6084 if (err)
6085 goto done;
6087 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6088 if (err)
6089 goto done;
6091 err = got_ref_write(*branch_ref, repo);
6092 if (err)
6093 goto done;
6095 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6096 worktree->base_commit_id);
6097 if (err)
6098 goto done;
6099 err = got_ref_write(base_commit_ref, repo);
6100 if (err)
6101 goto done;
6102 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6103 if (*base_commit_id == NULL) {
6104 err = got_error_from_errno("got_object_id_dup");
6105 goto done;
6108 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6109 worktree->base_commit_id);
6110 if (err)
6111 goto done;
6112 err = got_ref_write(*tmp_branch, repo);
6113 if (err)
6114 goto done;
6116 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6117 if (err)
6118 goto done;
6119 done:
6120 free(fileindex_path);
6121 free(tmp_branch_name);
6122 free(branch_ref_name);
6123 free(base_commit_ref_name);
6124 if (wt_branch)
6125 got_ref_close(wt_branch);
6126 if (err) {
6127 if (*branch_ref) {
6128 got_ref_close(*branch_ref);
6129 *branch_ref = NULL;
6131 if (*tmp_branch) {
6132 got_ref_close(*tmp_branch);
6133 *tmp_branch = NULL;
6135 free(*base_commit_id);
6136 if (*fileindex) {
6137 got_fileindex_free(*fileindex);
6138 *fileindex = NULL;
6140 lock_worktree(worktree, LOCK_SH);
6142 return err;
6145 const struct got_error *
6146 got_worktree_histedit_postpone(struct got_worktree *worktree,
6147 struct got_fileindex *fileindex)
6149 if (fileindex)
6150 got_fileindex_free(fileindex);
6151 return lock_worktree(worktree, LOCK_SH);
6154 const struct got_error *
6155 got_worktree_histedit_in_progress(int *in_progress,
6156 struct got_worktree *worktree)
6158 const struct got_error *err;
6159 char *tmp_branch_name = NULL;
6161 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6162 if (err)
6163 return err;
6165 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6166 free(tmp_branch_name);
6167 return NULL;
6170 const struct got_error *
6171 got_worktree_histedit_continue(struct got_object_id **commit_id,
6172 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6173 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6174 struct got_worktree *worktree, struct got_repository *repo)
6176 const struct got_error *err;
6177 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6178 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6179 struct got_reference *commit_ref = NULL;
6180 struct got_reference *base_commit_ref = NULL;
6181 char *fileindex_path = NULL;
6182 int have_staged_files = 0;
6184 *commit_id = NULL;
6185 *tmp_branch = NULL;
6186 *base_commit_id = NULL;
6187 *fileindex = NULL;
6189 err = lock_worktree(worktree, LOCK_EX);
6190 if (err)
6191 return err;
6193 err = open_fileindex(fileindex, &fileindex_path, worktree);
6194 if (err)
6195 goto done;
6197 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6198 &have_staged_files);
6199 if (err && err->code != GOT_ERR_CANCELLED)
6200 goto done;
6201 if (have_staged_files) {
6202 err = got_error(GOT_ERR_STAGED_PATHS);
6203 goto done;
6206 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6207 if (err)
6208 goto done;
6210 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6211 if (err)
6212 goto done;
6214 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6215 if (err)
6216 goto done;
6218 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6219 worktree);
6220 if (err)
6221 goto done;
6223 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6224 if (err)
6225 goto done;
6227 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6228 if (err)
6229 goto done;
6230 err = got_ref_resolve(commit_id, repo, commit_ref);
6231 if (err)
6232 goto done;
6234 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6235 if (err)
6236 goto done;
6237 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6238 if (err)
6239 goto done;
6241 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6242 if (err)
6243 goto done;
6244 done:
6245 free(commit_ref_name);
6246 free(branch_ref_name);
6247 free(fileindex_path);
6248 if (commit_ref)
6249 got_ref_close(commit_ref);
6250 if (base_commit_ref)
6251 got_ref_close(base_commit_ref);
6252 if (err) {
6253 free(*commit_id);
6254 *commit_id = NULL;
6255 free(*base_commit_id);
6256 *base_commit_id = NULL;
6257 if (*tmp_branch) {
6258 got_ref_close(*tmp_branch);
6259 *tmp_branch = NULL;
6261 if (*fileindex) {
6262 got_fileindex_free(*fileindex);
6263 *fileindex = NULL;
6265 lock_worktree(worktree, LOCK_EX);
6267 return err;
6270 static const struct got_error *
6271 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6273 const struct got_error *err;
6274 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6275 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6277 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6278 if (err)
6279 goto done;
6280 err = delete_ref(tmp_branch_name, repo);
6281 if (err)
6282 goto done;
6284 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6285 worktree);
6286 if (err)
6287 goto done;
6288 err = delete_ref(base_commit_ref_name, repo);
6289 if (err)
6290 goto done;
6292 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6293 if (err)
6294 goto done;
6295 err = delete_ref(branch_ref_name, repo);
6296 if (err)
6297 goto done;
6299 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6300 if (err)
6301 goto done;
6302 err = delete_ref(commit_ref_name, repo);
6303 if (err)
6304 goto done;
6305 done:
6306 free(tmp_branch_name);
6307 free(base_commit_ref_name);
6308 free(branch_ref_name);
6309 free(commit_ref_name);
6310 return err;
6313 const struct got_error *
6314 got_worktree_histedit_abort(struct got_worktree *worktree,
6315 struct got_fileindex *fileindex, struct got_repository *repo,
6316 struct got_reference *branch, struct got_object_id *base_commit_id,
6317 got_worktree_checkout_cb progress_cb, void *progress_arg)
6319 const struct got_error *err, *unlockerr, *sync_err;
6320 struct got_reference *resolved = NULL;
6321 char *fileindex_path = NULL;
6322 struct got_object_id *tree_id = NULL;
6323 struct revert_file_args rfa;
6325 err = lock_worktree(worktree, LOCK_EX);
6326 if (err)
6327 return err;
6329 err = got_ref_open(&resolved, repo,
6330 got_ref_get_symref_target(branch), 0);
6331 if (err)
6332 goto done;
6334 err = got_worktree_set_head_ref(worktree, resolved);
6335 if (err)
6336 goto done;
6338 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6339 if (err)
6340 goto done;
6342 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6343 worktree->path_prefix);
6344 if (err)
6345 goto done;
6347 err = delete_histedit_refs(worktree, repo);
6348 if (err)
6349 goto done;
6351 err = get_fileindex_path(&fileindex_path, worktree);
6352 if (err)
6353 goto done;
6355 rfa.worktree = worktree;
6356 rfa.fileindex = fileindex;
6357 rfa.progress_cb = progress_cb;
6358 rfa.progress_arg = progress_arg;
6359 rfa.patch_cb = NULL;
6360 rfa.patch_arg = NULL;
6361 rfa.repo = repo;
6362 err = worktree_status(worktree, "", fileindex, repo,
6363 revert_file, &rfa, NULL, NULL, 0, 0);
6364 if (err)
6365 goto sync;
6367 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6368 repo, progress_cb, progress_arg, NULL, NULL);
6369 sync:
6370 sync_err = sync_fileindex(fileindex, fileindex_path);
6371 if (sync_err && err == NULL)
6372 err = sync_err;
6373 done:
6374 got_ref_close(resolved);
6375 free(tree_id);
6376 free(fileindex_path);
6378 unlockerr = lock_worktree(worktree, LOCK_SH);
6379 if (unlockerr && err == NULL)
6380 err = unlockerr;
6381 return err;
6384 const struct got_error *
6385 got_worktree_histedit_complete(struct got_worktree *worktree,
6386 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6387 struct got_reference *edited_branch, struct got_repository *repo)
6389 const struct got_error *err, *unlockerr;
6390 struct got_object_id *new_head_commit_id = NULL;
6391 struct got_reference *resolved = NULL;
6393 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6394 if (err)
6395 return err;
6397 err = got_ref_open(&resolved, repo,
6398 got_ref_get_symref_target(edited_branch), 0);
6399 if (err)
6400 goto done;
6402 err = got_ref_change_ref(resolved, new_head_commit_id);
6403 if (err)
6404 goto done;
6406 err = got_ref_write(resolved, repo);
6407 if (err)
6408 goto done;
6410 err = got_worktree_set_head_ref(worktree, resolved);
6411 if (err)
6412 goto done;
6414 err = delete_histedit_refs(worktree, repo);
6415 done:
6416 if (fileindex)
6417 got_fileindex_free(fileindex);
6418 free(new_head_commit_id);
6419 unlockerr = lock_worktree(worktree, LOCK_SH);
6420 if (unlockerr && err == NULL)
6421 err = unlockerr;
6422 return err;
6425 const struct got_error *
6426 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6427 struct got_object_id *commit_id, struct got_repository *repo)
6429 const struct got_error *err;
6430 char *commit_ref_name;
6432 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6433 if (err)
6434 return err;
6436 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6437 if (err)
6438 goto done;
6440 err = delete_ref(commit_ref_name, repo);
6441 done:
6442 free(commit_ref_name);
6443 return err;
6446 const struct got_error *
6447 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6448 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6449 struct got_worktree *worktree, const char *refname,
6450 struct got_repository *repo)
6452 const struct got_error *err = NULL;
6453 char *fileindex_path = NULL;
6454 struct check_rebase_ok_arg ok_arg;
6456 *fileindex = NULL;
6457 *branch_ref = NULL;
6458 *base_branch_ref = NULL;
6460 err = lock_worktree(worktree, LOCK_EX);
6461 if (err)
6462 return err;
6464 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6465 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6466 "cannot integrate a branch into itself; "
6467 "update -b or different branch name required");
6468 goto done;
6471 err = open_fileindex(fileindex, &fileindex_path, worktree);
6472 if (err)
6473 goto done;
6475 /* Preconditions are the same as for rebase. */
6476 ok_arg.worktree = worktree;
6477 ok_arg.repo = repo;
6478 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6479 &ok_arg);
6480 if (err)
6481 goto done;
6483 err = got_ref_open(branch_ref, repo, refname, 1);
6484 if (err)
6485 goto done;
6487 err = got_ref_open(base_branch_ref, repo,
6488 got_worktree_get_head_ref_name(worktree), 1);
6489 done:
6490 if (err) {
6491 if (*branch_ref) {
6492 got_ref_close(*branch_ref);
6493 *branch_ref = NULL;
6495 if (*base_branch_ref) {
6496 got_ref_close(*base_branch_ref);
6497 *base_branch_ref = NULL;
6499 if (*fileindex) {
6500 got_fileindex_free(*fileindex);
6501 *fileindex = NULL;
6503 lock_worktree(worktree, LOCK_SH);
6505 return err;
6508 const struct got_error *
6509 got_worktree_integrate_continue(struct got_worktree *worktree,
6510 struct got_fileindex *fileindex, struct got_repository *repo,
6511 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6512 got_worktree_checkout_cb progress_cb, void *progress_arg,
6513 got_cancel_cb cancel_cb, void *cancel_arg)
6515 const struct got_error *err = NULL, *sync_err, *unlockerr;
6516 char *fileindex_path = NULL;
6517 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6519 err = get_fileindex_path(&fileindex_path, worktree);
6520 if (err)
6521 goto done;
6523 err = got_ref_resolve(&commit_id, repo, branch_ref);
6524 if (err)
6525 goto done;
6527 err = got_object_id_by_path(&tree_id, repo, commit_id,
6528 worktree->path_prefix);
6529 if (err)
6530 goto done;
6532 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6533 if (err)
6534 goto done;
6536 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6537 progress_cb, progress_arg, cancel_cb, cancel_arg);
6538 if (err)
6539 goto sync;
6541 err = got_ref_change_ref(base_branch_ref, commit_id);
6542 if (err)
6543 goto sync;
6545 err = got_ref_write(base_branch_ref, repo);
6546 sync:
6547 sync_err = sync_fileindex(fileindex, fileindex_path);
6548 if (sync_err && err == NULL)
6549 err = sync_err;
6551 done:
6552 unlockerr = got_ref_unlock(branch_ref);
6553 if (unlockerr && err == NULL)
6554 err = unlockerr;
6555 got_ref_close(branch_ref);
6557 unlockerr = got_ref_unlock(base_branch_ref);
6558 if (unlockerr && err == NULL)
6559 err = unlockerr;
6560 got_ref_close(base_branch_ref);
6562 got_fileindex_free(fileindex);
6563 free(fileindex_path);
6564 free(tree_id);
6566 unlockerr = lock_worktree(worktree, LOCK_SH);
6567 if (unlockerr && err == NULL)
6568 err = unlockerr;
6569 return err;
6572 const struct got_error *
6573 got_worktree_integrate_abort(struct got_worktree *worktree,
6574 struct got_fileindex *fileindex, struct got_repository *repo,
6575 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6577 const struct got_error *err = NULL, *unlockerr = NULL;
6579 got_fileindex_free(fileindex);
6581 err = lock_worktree(worktree, LOCK_SH);
6583 unlockerr = got_ref_unlock(branch_ref);
6584 if (unlockerr && err == NULL)
6585 err = unlockerr;
6586 got_ref_close(branch_ref);
6588 unlockerr = got_ref_unlock(base_branch_ref);
6589 if (unlockerr && err == NULL)
6590 err = unlockerr;
6591 got_ref_close(base_branch_ref);
6593 return err;
6596 struct check_stage_ok_arg {
6597 struct got_object_id *head_commit_id;
6598 struct got_worktree *worktree;
6599 struct got_fileindex *fileindex;
6600 struct got_repository *repo;
6601 int have_changes;
6604 const struct got_error *
6605 check_stage_ok(void *arg, unsigned char status,
6606 unsigned char staged_status, const char *relpath,
6607 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6608 struct got_object_id *commit_id, int dirfd, const char *de_name)
6610 struct check_stage_ok_arg *a = arg;
6611 const struct got_error *err = NULL;
6612 struct got_fileindex_entry *ie;
6613 struct got_object_id base_commit_id;
6614 struct got_object_id *base_commit_idp = NULL;
6615 char *in_repo_path = NULL, *p;
6617 if (status == GOT_STATUS_UNVERSIONED ||
6618 status == GOT_STATUS_NO_CHANGE)
6619 return NULL;
6620 if (status == GOT_STATUS_NONEXISTENT)
6621 return got_error_set_errno(ENOENT, relpath);
6623 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6624 if (ie == NULL)
6625 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6627 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6628 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6629 relpath) == -1)
6630 return got_error_from_errno("asprintf");
6632 if (got_fileindex_entry_has_commit(ie)) {
6633 memcpy(base_commit_id.sha1, ie->commit_sha1,
6634 SHA1_DIGEST_LENGTH);
6635 base_commit_idp = &base_commit_id;
6638 if (status == GOT_STATUS_CONFLICT) {
6639 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6640 goto done;
6641 } else if (status != GOT_STATUS_ADD &&
6642 status != GOT_STATUS_MODIFY &&
6643 status != GOT_STATUS_DELETE) {
6644 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6645 goto done;
6648 a->have_changes = 1;
6650 p = in_repo_path;
6651 while (p[0] == '/')
6652 p++;
6653 err = check_out_of_date(p, status, staged_status,
6654 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6655 GOT_ERR_STAGE_OUT_OF_DATE);
6656 done:
6657 free(in_repo_path);
6658 return err;
6661 struct stage_path_arg {
6662 struct got_worktree *worktree;
6663 struct got_fileindex *fileindex;
6664 struct got_repository *repo;
6665 got_worktree_status_cb status_cb;
6666 void *status_arg;
6667 got_worktree_patch_cb patch_cb;
6668 void *patch_arg;
6669 int staged_something;
6672 static const struct got_error *
6673 stage_path(void *arg, unsigned char status,
6674 unsigned char staged_status, const char *relpath,
6675 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6676 struct got_object_id *commit_id, int dirfd, const char *de_name)
6678 struct stage_path_arg *a = arg;
6679 const struct got_error *err = NULL;
6680 struct got_fileindex_entry *ie;
6681 char *ondisk_path = NULL, *path_content = NULL;
6682 uint32_t stage;
6683 struct got_object_id *new_staged_blob_id = NULL;
6685 if (status == GOT_STATUS_UNVERSIONED)
6686 return NULL;
6688 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6689 if (ie == NULL)
6690 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6692 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6693 relpath)== -1)
6694 return got_error_from_errno("asprintf");
6696 switch (status) {
6697 case GOT_STATUS_ADD:
6698 case GOT_STATUS_MODIFY:
6699 if (a->patch_cb) {
6700 if (status == GOT_STATUS_ADD) {
6701 int choice = GOT_PATCH_CHOICE_NONE;
6702 err = (*a->patch_cb)(&choice, a->patch_arg,
6703 status, ie->path, NULL, 1, 1);
6704 if (err)
6705 break;
6706 if (choice != GOT_PATCH_CHOICE_YES)
6707 break;
6708 } else {
6709 err = create_patched_content(&path_content, 0,
6710 staged_blob_id ? staged_blob_id : blob_id,
6711 ondisk_path, dirfd, de_name, ie->path,
6712 a->repo, a->patch_cb, a->patch_arg);
6713 if (err || path_content == NULL)
6714 break;
6717 err = got_object_blob_create(&new_staged_blob_id,
6718 path_content ? path_content : ondisk_path, a->repo);
6719 if (err)
6720 break;
6721 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6722 SHA1_DIGEST_LENGTH);
6723 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6724 stage = GOT_FILEIDX_STAGE_ADD;
6725 else
6726 stage = GOT_FILEIDX_STAGE_MODIFY;
6727 got_fileindex_entry_stage_set(ie, stage);
6728 a->staged_something = 1;
6729 if (a->status_cb == NULL)
6730 break;
6731 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6732 get_staged_status(ie), relpath, blob_id,
6733 new_staged_blob_id, NULL, dirfd, de_name);
6734 break;
6735 case GOT_STATUS_DELETE:
6736 if (staged_status == GOT_STATUS_DELETE)
6737 break;
6738 if (a->patch_cb) {
6739 int choice = GOT_PATCH_CHOICE_NONE;
6740 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6741 ie->path, NULL, 1, 1);
6742 if (err)
6743 break;
6744 if (choice == GOT_PATCH_CHOICE_NO)
6745 break;
6746 if (choice != GOT_PATCH_CHOICE_YES) {
6747 err = got_error(GOT_ERR_PATCH_CHOICE);
6748 break;
6751 stage = GOT_FILEIDX_STAGE_DELETE;
6752 got_fileindex_entry_stage_set(ie, stage);
6753 a->staged_something = 1;
6754 if (a->status_cb == NULL)
6755 break;
6756 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6757 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6758 de_name);
6759 break;
6760 case GOT_STATUS_NO_CHANGE:
6761 break;
6762 case GOT_STATUS_CONFLICT:
6763 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6764 break;
6765 case GOT_STATUS_NONEXISTENT:
6766 err = got_error_set_errno(ENOENT, relpath);
6767 break;
6768 default:
6769 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6770 break;
6773 if (path_content && unlink(path_content) == -1 && err == NULL)
6774 err = got_error_from_errno2("unlink", path_content);
6775 free(path_content);
6776 free(ondisk_path);
6777 free(new_staged_blob_id);
6778 return err;
6781 const struct got_error *
6782 got_worktree_stage(struct got_worktree *worktree,
6783 struct got_pathlist_head *paths,
6784 got_worktree_status_cb status_cb, void *status_arg,
6785 got_worktree_patch_cb patch_cb, void *patch_arg,
6786 struct got_repository *repo)
6788 const struct got_error *err = NULL, *sync_err, *unlockerr;
6789 struct got_pathlist_entry *pe;
6790 struct got_fileindex *fileindex = NULL;
6791 char *fileindex_path = NULL;
6792 struct got_reference *head_ref = NULL;
6793 struct got_object_id *head_commit_id = NULL;
6794 struct check_stage_ok_arg oka;
6795 struct stage_path_arg spa;
6797 err = lock_worktree(worktree, LOCK_EX);
6798 if (err)
6799 return err;
6801 err = got_ref_open(&head_ref, repo,
6802 got_worktree_get_head_ref_name(worktree), 0);
6803 if (err)
6804 goto done;
6805 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6806 if (err)
6807 goto done;
6808 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6809 if (err)
6810 goto done;
6812 /* Check pre-conditions before staging anything. */
6813 oka.head_commit_id = head_commit_id;
6814 oka.worktree = worktree;
6815 oka.fileindex = fileindex;
6816 oka.repo = repo;
6817 oka.have_changes = 0;
6818 TAILQ_FOREACH(pe, paths, entry) {
6819 err = worktree_status(worktree, pe->path, fileindex, repo,
6820 check_stage_ok, &oka, NULL, NULL, 0, 0);
6821 if (err)
6822 goto done;
6824 if (!oka.have_changes) {
6825 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6826 goto done;
6829 spa.worktree = worktree;
6830 spa.fileindex = fileindex;
6831 spa.repo = repo;
6832 spa.patch_cb = patch_cb;
6833 spa.patch_arg = patch_arg;
6834 spa.status_cb = status_cb;
6835 spa.status_arg = status_arg;
6836 spa.staged_something = 0;
6837 TAILQ_FOREACH(pe, paths, entry) {
6838 err = worktree_status(worktree, pe->path, fileindex, repo,
6839 stage_path, &spa, NULL, NULL, 0, 0);
6840 if (err)
6841 goto done;
6843 if (!spa.staged_something) {
6844 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6845 goto done;
6848 sync_err = sync_fileindex(fileindex, fileindex_path);
6849 if (sync_err && err == NULL)
6850 err = sync_err;
6851 done:
6852 if (head_ref)
6853 got_ref_close(head_ref);
6854 free(head_commit_id);
6855 free(fileindex_path);
6856 if (fileindex)
6857 got_fileindex_free(fileindex);
6858 unlockerr = lock_worktree(worktree, LOCK_SH);
6859 if (unlockerr && err == NULL)
6860 err = unlockerr;
6861 return err;
6864 struct unstage_path_arg {
6865 struct got_worktree *worktree;
6866 struct got_fileindex *fileindex;
6867 struct got_repository *repo;
6868 got_worktree_checkout_cb progress_cb;
6869 void *progress_arg;
6870 got_worktree_patch_cb patch_cb;
6871 void *patch_arg;
6874 static const struct got_error *
6875 create_unstaged_content(char **path_unstaged_content,
6876 char **path_new_staged_content, struct got_object_id *blob_id,
6877 struct got_object_id *staged_blob_id, const char *relpath,
6878 struct got_repository *repo,
6879 got_worktree_patch_cb patch_cb, void *patch_arg)
6881 const struct got_error *err;
6882 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6883 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6884 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6885 struct stat sb1, sb2;
6886 struct got_diff_changes *changes = NULL;
6887 struct got_diff_state *ds = NULL;
6888 struct got_diff_args *args = NULL;
6889 struct got_diff_change *change;
6890 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6891 int have_content = 0, have_rejected_content = 0;
6893 *path_unstaged_content = NULL;
6894 *path_new_staged_content = NULL;
6896 err = got_object_id_str(&label1, blob_id);
6897 if (err)
6898 return err;
6899 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6900 if (err)
6901 goto done;
6903 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6904 if (err)
6905 goto done;
6907 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6908 if (err)
6909 goto done;
6911 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6912 if (err)
6913 goto done;
6915 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6916 if (err)
6917 goto done;
6919 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6920 if (err)
6921 goto done;
6923 if (stat(path1, &sb1) == -1) {
6924 err = got_error_from_errno2("stat", path1);
6925 goto done;
6928 if (stat(path2, &sb2) == -1) {
6929 err = got_error_from_errno2("stat", path2);
6930 goto done;
6933 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6934 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6935 if (err)
6936 goto done;
6938 err = got_opentemp_named(path_unstaged_content, &outfile,
6939 "got-unstaged-content");
6940 if (err)
6941 goto done;
6942 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6943 "got-new-staged-content");
6944 if (err)
6945 goto done;
6947 if (fseek(f1, 0L, SEEK_SET) == -1) {
6948 err = got_ferror(f1, GOT_ERR_IO);
6949 goto done;
6951 if (fseek(f2, 0L, SEEK_SET) == -1) {
6952 err = got_ferror(f2, GOT_ERR_IO);
6953 goto done;
6955 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6956 int choice;
6957 err = apply_or_reject_change(&choice, change, ++n,
6958 changes->nchanges, ds, args, diff_flags, relpath,
6959 f1, f2, &line_cur1, &line_cur2,
6960 outfile, rejectfile, patch_cb, patch_arg);
6961 if (err)
6962 goto done;
6963 if (choice == GOT_PATCH_CHOICE_YES)
6964 have_content = 1;
6965 else
6966 have_rejected_content = 1;
6967 if (choice == GOT_PATCH_CHOICE_QUIT)
6968 break;
6970 if (have_content || have_rejected_content)
6971 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6972 outfile, rejectfile);
6973 done:
6974 free(label1);
6975 if (blob)
6976 got_object_blob_close(blob);
6977 if (staged_blob)
6978 got_object_blob_close(staged_blob);
6979 if (f1 && fclose(f1) == EOF && err == NULL)
6980 err = got_error_from_errno2("fclose", path1);
6981 if (f2 && fclose(f2) == EOF && err == NULL)
6982 err = got_error_from_errno2("fclose", path2);
6983 if (outfile && fclose(outfile) == EOF && err == NULL)
6984 err = got_error_from_errno2("fclose", *path_unstaged_content);
6985 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6986 err = got_error_from_errno2("fclose", *path_new_staged_content);
6987 if (path1 && unlink(path1) == -1 && err == NULL)
6988 err = got_error_from_errno2("unlink", path1);
6989 if (path2 && unlink(path2) == -1 && err == NULL)
6990 err = got_error_from_errno2("unlink", path2);
6991 if (err || !have_content) {
6992 if (*path_unstaged_content &&
6993 unlink(*path_unstaged_content) == -1 && err == NULL)
6994 err = got_error_from_errno2("unlink",
6995 *path_unstaged_content);
6996 free(*path_unstaged_content);
6997 *path_unstaged_content = NULL;
6999 if (err || !have_rejected_content) {
7000 if (*path_new_staged_content &&
7001 unlink(*path_new_staged_content) == -1 && err == NULL)
7002 err = got_error_from_errno2("unlink",
7003 *path_new_staged_content);
7004 free(*path_new_staged_content);
7005 *path_new_staged_content = NULL;
7007 free(args);
7008 if (ds) {
7009 got_diff_state_free(ds);
7010 free(ds);
7012 if (changes)
7013 got_diff_free_changes(changes);
7014 free(path1);
7015 free(path2);
7016 return err;
7019 static const struct got_error *
7020 unstage_path(void *arg, unsigned char status,
7021 unsigned char staged_status, const char *relpath,
7022 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7023 struct got_object_id *commit_id, int dirfd, const char *de_name)
7025 const struct got_error *err = NULL;
7026 struct unstage_path_arg *a = arg;
7027 struct got_fileindex_entry *ie;
7028 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7029 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7030 char *path_new_staged_content = NULL;
7031 char *id_str = NULL, *label_orig = NULL;
7032 int local_changes_subsumed;
7033 struct stat sb;
7035 if (staged_status != GOT_STATUS_ADD &&
7036 staged_status != GOT_STATUS_MODIFY &&
7037 staged_status != GOT_STATUS_DELETE)
7038 return NULL;
7040 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7041 if (ie == NULL)
7042 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7044 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7045 == -1)
7046 return got_error_from_errno("asprintf");
7048 err = got_object_id_str(&id_str,
7049 commit_id ? commit_id : a->worktree->base_commit_id);
7050 if (err)
7051 goto done;
7052 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7053 id_str) == -1) {
7054 err = got_error_from_errno("asprintf");
7055 goto done;
7058 switch (staged_status) {
7059 case GOT_STATUS_MODIFY:
7060 err = got_object_open_as_blob(&blob_base, a->repo,
7061 blob_id, 8192);
7062 if (err)
7063 break;
7064 /* fall through */
7065 case GOT_STATUS_ADD:
7066 if (a->patch_cb) {
7067 if (staged_status == GOT_STATUS_ADD) {
7068 int choice = GOT_PATCH_CHOICE_NONE;
7069 err = (*a->patch_cb)(&choice, a->patch_arg,
7070 staged_status, ie->path, NULL, 1, 1);
7071 if (err)
7072 break;
7073 if (choice != GOT_PATCH_CHOICE_YES)
7074 break;
7075 } else {
7076 err = create_unstaged_content(
7077 &path_unstaged_content,
7078 &path_new_staged_content, blob_id,
7079 staged_blob_id, ie->path, a->repo,
7080 a->patch_cb, a->patch_arg);
7081 if (err || path_unstaged_content == NULL)
7082 break;
7083 if (path_new_staged_content) {
7084 err = got_object_blob_create(
7085 &staged_blob_id,
7086 path_new_staged_content,
7087 a->repo);
7088 if (err)
7089 break;
7090 memcpy(ie->staged_blob_sha1,
7091 staged_blob_id->sha1,
7092 SHA1_DIGEST_LENGTH);
7094 err = merge_file(&local_changes_subsumed,
7095 a->worktree, blob_base, ondisk_path,
7096 relpath, got_fileindex_perms_to_st(ie),
7097 path_unstaged_content, label_orig,
7098 "unstaged", a->repo, a->progress_cb,
7099 a->progress_arg);
7100 if (err == NULL &&
7101 path_new_staged_content == NULL)
7102 got_fileindex_entry_stage_set(ie,
7103 GOT_FILEIDX_STAGE_NONE);
7104 break; /* Done with this file. */
7107 err = got_object_open_as_blob(&blob_staged, a->repo,
7108 staged_blob_id, 8192);
7109 if (err)
7110 break;
7111 err = merge_blob(&local_changes_subsumed, a->worktree,
7112 blob_base, ondisk_path, relpath,
7113 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7114 commit_id ? commit_id : a->worktree->base_commit_id,
7115 a->repo, a->progress_cb, a->progress_arg);
7116 if (err == NULL)
7117 got_fileindex_entry_stage_set(ie,
7118 GOT_FILEIDX_STAGE_NONE);
7119 break;
7120 case GOT_STATUS_DELETE:
7121 if (a->patch_cb) {
7122 int choice = GOT_PATCH_CHOICE_NONE;
7123 err = (*a->patch_cb)(&choice, a->patch_arg,
7124 staged_status, ie->path, NULL, 1, 1);
7125 if (err)
7126 break;
7127 if (choice == GOT_PATCH_CHOICE_NO)
7128 break;
7129 if (choice != GOT_PATCH_CHOICE_YES) {
7130 err = got_error(GOT_ERR_PATCH_CHOICE);
7131 break;
7134 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7135 err = get_file_status(&status, &sb, ie, ondisk_path,
7136 dirfd, de_name, a->repo);
7137 if (err)
7138 break;
7139 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7140 break;
7142 done:
7143 free(ondisk_path);
7144 if (path_unstaged_content &&
7145 unlink(path_unstaged_content) == -1 && err == NULL)
7146 err = got_error_from_errno2("unlink", path_unstaged_content);
7147 if (path_new_staged_content &&
7148 unlink(path_new_staged_content) == -1 && err == NULL)
7149 err = got_error_from_errno2("unlink", path_new_staged_content);
7150 free(path_unstaged_content);
7151 free(path_new_staged_content);
7152 if (blob_base)
7153 got_object_blob_close(blob_base);
7154 if (blob_staged)
7155 got_object_blob_close(blob_staged);
7156 free(id_str);
7157 free(label_orig);
7158 return err;
7161 const struct got_error *
7162 got_worktree_unstage(struct got_worktree *worktree,
7163 struct got_pathlist_head *paths,
7164 got_worktree_checkout_cb progress_cb, void *progress_arg,
7165 got_worktree_patch_cb patch_cb, void *patch_arg,
7166 struct got_repository *repo)
7168 const struct got_error *err = NULL, *sync_err, *unlockerr;
7169 struct got_pathlist_entry *pe;
7170 struct got_fileindex *fileindex = NULL;
7171 char *fileindex_path = NULL;
7172 struct unstage_path_arg upa;
7174 err = lock_worktree(worktree, LOCK_EX);
7175 if (err)
7176 return err;
7178 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7179 if (err)
7180 goto done;
7182 upa.worktree = worktree;
7183 upa.fileindex = fileindex;
7184 upa.repo = repo;
7185 upa.progress_cb = progress_cb;
7186 upa.progress_arg = progress_arg;
7187 upa.patch_cb = patch_cb;
7188 upa.patch_arg = patch_arg;
7189 TAILQ_FOREACH(pe, paths, entry) {
7190 err = worktree_status(worktree, pe->path, fileindex, repo,
7191 unstage_path, &upa, NULL, NULL, 0, 0);
7192 if (err)
7193 goto done;
7196 sync_err = sync_fileindex(fileindex, fileindex_path);
7197 if (sync_err && err == NULL)
7198 err = sync_err;
7199 done:
7200 free(fileindex_path);
7201 if (fileindex)
7202 got_fileindex_free(fileindex);
7203 unlockerr = lock_worktree(worktree, LOCK_SH);
7204 if (unlockerr && err == NULL)
7205 err = unlockerr;
7206 return err;