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"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
432 done:
433 if (repo)
434 got_repo_close(repo);
435 free(path_got);
436 free(path_lock);
437 free(base_commit_id_str);
438 free(uuidstr);
439 free(formatstr);
440 if (err) {
441 if (fd != -1)
442 close(fd);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
445 *worktree = NULL;
446 } else
447 (*worktree)->lockfd = fd;
449 return err;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
456 char *worktree_path;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
462 for (;;) {
463 char *parent_path;
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 free(worktree_path);
468 return err;
470 if (*worktree) {
471 free(worktree_path);
472 return NULL;
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
475 break;
476 err = got_path_dirname(&parent_path, worktree_path);
477 if (err) {
478 if (err->code != GOT_ERR_BAD_PATH) {
479 free(worktree_path);
480 return err;
482 break;
484 free(worktree_path);
485 worktree_path = parent_path;
488 free(worktree_path);
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
507 free(worktree);
508 return err;
511 const char *
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
517 const char *
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
522 const char *
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
540 free(absprefix);
541 return NULL;
544 const char *
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
567 goto done;
570 err = write_head_ref(path_got, head_ref);
571 if (err)
572 goto done;
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
576 done:
577 free(path_got);
578 if (err)
579 free(head_ref_name);
580 return err;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
595 char *id_str = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
601 path_got = NULL;
602 goto done;
605 err = got_object_open(&obj, repo, commit_id);
606 if (err)
607 return err;
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
616 if (err)
617 goto done;
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
619 if (err)
620 goto done;
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
626 goto done;
628 done:
629 if (obj)
630 got_object_close(obj);
631 free(id_str);
632 free(path_got);
633 return err;
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
649 return NULL;
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
656 char *abspath;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 struct stat sb;
664 err = NULL;
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
672 free(abspath);
673 return err;
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
680 uint8_t fbuf1[8192];
681 uint8_t fbuf2[8192];
682 size_t flen1 = 0, flen2 = 0;
684 *same = 1;
686 for (;;) {
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
690 break;
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
695 break;
697 if (flen1 == 0) {
698 if (flen2 != 0)
699 *same = 0;
700 break;
701 } else if (flen2 == 0) {
702 if (flen1 != 0)
703 *same = 0;
704 break;
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
707 *same = 0;
708 break;
710 } else {
711 *same = 0;
712 break;
716 return err;
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
723 struct stat sb;
724 size_t size1, size2;
725 FILE *f1 = NULL, *f2 = NULL;
727 *same = 1;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
731 goto done;
733 size1 = sb.st_size;
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
737 goto done;
739 size2 = sb.st_size;
741 if (size1 != size2) {
742 *same = 0;
743 return NULL;
746 f1 = fopen(f1_path, "r");
747 if (f1 == NULL)
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
751 if (f2 == NULL) {
752 err = got_error_from_errno2("fopen", f2_path);
753 goto done;
756 err = check_file_contents_equal(same, f1, f2);
757 done:
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
763 return err;
766 /*
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
770 */
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
780 int merged_fd = -1;
781 FILE *f_orig = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
784 int overlapcnt = 0;
785 char *parent = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
792 if (err)
793 return err;
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 goto done;
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
801 if (err)
802 goto done;
804 free(base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
807 base_path = NULL;
808 goto done;
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
812 if (err)
813 goto done;
814 if (blob_orig) {
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
816 blob_orig);
817 if (err)
818 goto done;
819 } else {
820 /*
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
824 */
827 /*
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
830 */
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
833 ssize_t target_len;
834 size_t n;
836 free(base_path);
837 if (asprintf(&base_path, "%s/got-symlink-merge",
838 parent) == -1) {
839 err = got_error_from_errno("asprintf");
840 base_path = NULL;
841 goto done;
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 if (err)
845 goto done;
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
850 goto done;
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
855 goto done;
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
859 goto done;
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
866 if (err)
867 goto done;
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
871 if (err)
872 goto done;
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
876 goto done;
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
882 merged_path);
883 if (err)
884 goto done;
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
889 goto done;
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
894 ondisk_path);
895 goto done;
897 done:
898 if (err) {
899 if (merged_path)
900 unlink(merged_path);
902 if (symlink_path) {
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
908 free(symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
913 free(merged_path);
914 free(base_path);
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
919 free(parent);
920 return err;
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
925 size_t target_len)
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
932 ondisk_path);
933 return NULL;
936 /*
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
947 */
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
955 FILE *f = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
958 if (err)
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
964 goto done;
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 if (err)
969 goto done;
971 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
972 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
973 deriv_target ? deriv_target : "(symlink was deleted)",
974 orig_target ? label_orig : "",
975 orig_target ? "\n" : "",
976 orig_target ? orig_target : "",
977 orig_target ? "\n" : "",
978 GOT_DIFF_CONFLICT_MARKER_SEP,
979 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
980 err = got_error_from_errno2("fprintf", path);
981 goto done;
984 if (unlink(ondisk_path) == -1) {
985 err = got_error_from_errno2("unlink", ondisk_path);
986 goto done;
988 if (rename(path, ondisk_path) == -1) {
989 err = got_error_from_errno3("rename", path, ondisk_path);
990 goto done;
992 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
994 goto done;
996 done:
997 if (f != NULL && fclose(f) == EOF && err == NULL)
998 err = got_error_from_errno2("fclose", path);
999 free(path);
1000 free(id_str);
1001 free(label_deriv);
1002 return err;
1005 /* forward declaration */
1006 static const struct got_error *
1007 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1008 const char *, const char *, uint16_t, const char *,
1009 struct got_blob_object *, struct got_object_id *,
1010 struct got_repository *, got_worktree_checkout_cb, void *);
1013 * Merge a symlink into the work tree, where blob_orig acts as the common
1014 * ancestor, deriv_target is the link target of the first derived version,
1015 * and the symlink on disk acts as the second derived version.
1016 * Assume that contents of both blobs represent symlinks.
1018 static const struct got_error *
1019 merge_symlink(struct got_worktree *worktree,
1020 struct got_blob_object *blob_orig, const char *ondisk_path,
1021 const char *path, const char *label_orig, const char *deriv_target,
1022 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1023 got_worktree_checkout_cb progress_cb, void *progress_arg)
1025 const struct got_error *err = NULL;
1026 char *ancestor_target = NULL;
1027 struct stat sb;
1028 ssize_t ondisk_len, deriv_len;
1029 char ondisk_target[PATH_MAX];
1030 int have_local_change = 0;
1031 int have_incoming_change = 0;
1033 if (lstat(ondisk_path, &sb) == -1)
1034 return got_error_from_errno2("lstat", ondisk_path);
1036 ondisk_len = readlink(ondisk_path, ondisk_target,
1037 sizeof(ondisk_target));
1038 if (ondisk_len == -1) {
1039 err = got_error_from_errno2("readlink",
1040 ondisk_path);
1041 goto done;
1043 ondisk_target[ondisk_len] = '\0';
1045 if (blob_orig) {
1046 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1047 if (err)
1048 goto done;
1051 if (ancestor_target == NULL ||
1052 (ondisk_len != strlen(ancestor_target) ||
1053 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1054 have_local_change = 1;
1056 deriv_len = strlen(deriv_target);
1057 if (ancestor_target == NULL ||
1058 (deriv_len != strlen(ancestor_target) ||
1059 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1060 have_incoming_change = 1;
1062 if (!have_local_change && !have_incoming_change) {
1063 if (ancestor_target) {
1064 /* Both sides made the same change. */
1065 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1066 path);
1067 } else if (deriv_len == ondisk_len &&
1068 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1069 /* Both sides added the same symlink. */
1070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1071 path);
1072 } else {
1073 /* Both sides added symlinks which don't match. */
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target,
1076 label_orig, ondisk_target, ondisk_path);
1077 if (err)
1078 goto done;
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 path);
1082 } else if (!have_local_change && have_incoming_change) {
1083 /* Apply the incoming change. */
1084 err = update_symlink(ondisk_path, deriv_target,
1085 strlen(deriv_target));
1086 if (err)
1087 goto done;
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1089 } else if (have_local_change && have_incoming_change) {
1090 if (deriv_len == ondisk_len &&
1091 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1092 /* Both sides made the same change. */
1093 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1094 path);
1095 } else {
1096 err = install_symlink_conflict(deriv_target,
1097 deriv_base_commit_id, ancestor_target, label_orig,
1098 ondisk_target, ondisk_path);
1099 if (err)
1100 goto done;
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1102 path);
1106 done:
1107 free(ancestor_target);
1108 return err;
1112 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1113 * blob_deriv acts as the first derived version, and the file on disk
1114 * acts as the second derived version.
1116 static const struct got_error *
1117 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1118 struct got_blob_object *blob_orig, const char *ondisk_path,
1119 const char *path, uint16_t st_mode, const char *label_orig,
1120 struct got_blob_object *blob_deriv,
1121 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1122 got_worktree_checkout_cb progress_cb, void *progress_arg)
1124 const struct got_error *err = NULL;
1125 FILE *f_deriv = NULL;
1126 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1127 char *label_deriv = NULL, *parent = NULL;
1129 *local_changes_subsumed = 0;
1131 err = got_path_dirname(&parent, ondisk_path);
1132 if (err)
1133 return err;
1135 free(base_path);
1136 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1137 err = got_error_from_errno("asprintf");
1138 base_path = NULL;
1139 goto done;
1142 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1143 if (err)
1144 goto done;
1145 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1146 blob_deriv);
1147 if (err)
1148 goto done;
1150 err = got_object_id_str(&id_str, deriv_base_commit_id);
1151 if (err)
1152 goto done;
1153 if (asprintf(&label_deriv, "%s: commit %s",
1154 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1155 err = got_error_from_errno("asprintf");
1156 goto done;
1159 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1160 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1161 label_deriv, repo, progress_cb, progress_arg);
1162 done:
1163 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1164 err = got_error_from_errno("fclose");
1165 free(base_path);
1166 if (blob_deriv_path) {
1167 unlink(blob_deriv_path);
1168 free(blob_deriv_path);
1170 free(id_str);
1171 free(label_deriv);
1172 free(parent);
1173 return err;
1176 static const struct got_error *
1177 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1178 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1179 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1181 const struct got_error *err = NULL;
1182 struct got_fileindex_entry *new_ie;
1184 *new_iep = NULL;
1186 err = got_fileindex_entry_alloc(&new_ie, path);
1187 if (err)
1188 return err;
1190 err = got_fileindex_entry_update(new_ie, ondisk_path,
1191 blob_id->sha1, base_commit_id->sha1, 1);
1192 if (err)
1193 goto done;
1195 err = got_fileindex_entry_add(fileindex, new_ie);
1196 done:
1197 if (err)
1198 got_fileindex_entry_free(new_ie);
1199 else
1200 *new_iep = new_ie;
1201 return err;
1204 static mode_t
1205 get_ondisk_perms(int executable, mode_t st_mode)
1207 mode_t xbits = S_IXUSR;
1209 if (executable) {
1210 /* Map read bits to execute bits. */
1211 if (st_mode & S_IRGRP)
1212 xbits |= S_IXGRP;
1213 if (st_mode & S_IROTH)
1214 xbits |= S_IXOTH;
1215 return st_mode | xbits;
1218 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1221 /* forward declaration */
1222 static const struct got_error *
1223 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1224 const char *path, mode_t te_mode, mode_t st_mode,
1225 struct got_blob_object *blob, int restoring_missing_file,
1226 int reverting_versioned_file, int installing_bad_symlink,
1227 int path_is_unversioned, struct got_repository *repo,
1228 got_worktree_checkout_cb progress_cb, void *progress_arg);
1231 * This function assumes that the provided symlink target points at a
1232 * safe location in the work tree!
1234 static const struct got_error *
1235 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1236 size_t target_len)
1238 const struct got_error *err = NULL;
1239 ssize_t elen;
1240 char etarget[PATH_MAX];
1241 int fd;
1244 * "Bad" symlinks (those pointing outside the work tree or into the
1245 * .got directory) are installed in the work tree as a regular file
1246 * which contains the bad symlink target path.
1247 * The new symlink target has already been checked for safety by our
1248 * caller. If we can successfully open a regular file then we simply
1249 * replace this file with a symlink below.
1251 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1252 if (fd == -1) {
1253 if (errno != ELOOP)
1254 return got_error_from_errno2("open", ondisk_path);
1256 /* We are updating an existing on-disk symlink. */
1257 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1258 if (elen == -1)
1259 return got_error_from_errno2("readlink", ondisk_path);
1261 if (elen == target_len &&
1262 memcmp(etarget, target_path, target_len) == 0)
1263 return NULL; /* nothing to do */
1266 err = update_symlink(ondisk_path, target_path, target_len);
1267 if (fd != -1 && close(fd) == -1 && err == NULL)
1268 err = got_error_from_errno2("close", ondisk_path);
1269 return err;
1272 static const struct got_error *
1273 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1274 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1276 const struct got_error *err = NULL;
1277 char canonpath[PATH_MAX];
1278 char *path_got = NULL;
1280 *is_bad_symlink = 0;
1282 if (target_len >= sizeof(canonpath)) {
1283 *is_bad_symlink = 1;
1284 return NULL;
1288 * We do not use realpath(3) to resolve the symlink's target
1289 * path because we don't want to resolve symlinks recursively.
1290 * Instead we make the path absolute and then canonicalize it.
1291 * Relative symlink target lookup should begin at the directory
1292 * in which the blob object is being installed.
1294 if (!got_path_is_absolute(target_path)) {
1295 char *abspath;
1296 char *parent = dirname(ondisk_path);
1297 if (parent == NULL)
1298 return got_error_from_errno2("dirname", ondisk_path);
1299 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1300 return got_error_from_errno("asprintf");
1301 if (strlen(abspath) >= sizeof(canonpath)) {
1302 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1303 free(abspath);
1304 return err;
1306 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1307 free(abspath);
1308 if (err)
1309 return err;
1310 } else {
1311 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1312 if (err)
1313 return err;
1316 /* Only allow symlinks pointing at paths within the work tree. */
1317 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1318 *is_bad_symlink = 1;
1319 return NULL;
1322 /* Do not allow symlinks pointing into the .got directory. */
1323 if (asprintf(&path_got, "%s/%s", wtroot_path,
1324 GOT_WORKTREE_GOT_DIR) == -1)
1325 return got_error_from_errno("asprintf");
1326 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1327 *is_bad_symlink = 1;
1329 free(path_got);
1330 return NULL;
1333 static const struct got_error *
1334 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1335 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1336 int restoring_missing_file, int reverting_versioned_file,
1337 int path_is_unversioned, struct got_repository *repo,
1338 got_worktree_checkout_cb progress_cb, void *progress_arg)
1340 const struct got_error *err = NULL;
1341 char target_path[PATH_MAX];
1342 size_t len, target_len = 0;
1343 char *path_got = NULL;
1344 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1345 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1347 *is_bad_symlink = 0;
1350 * Blob object content specifies the target path of the link.
1351 * If a symbolic link cannot be installed we instead create
1352 * a regular file which contains the link target path stored
1353 * in the blob object.
1355 do {
1356 err = got_object_blob_read_block(&len, blob);
1357 if (len + target_len >= sizeof(target_path)) {
1358 /* Path too long; install as a regular file. */
1359 *is_bad_symlink = 1;
1360 got_object_blob_rewind(blob);
1361 return install_blob(worktree, ondisk_path, path,
1362 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 restoring_missing_file, reverting_versioned_file,
1364 1, path_is_unversioned, repo, progress_cb,
1365 progress_arg);
1367 if (len > 0) {
1368 /* Skip blob object header first time around. */
1369 memcpy(target_path + target_len, buf + hdrlen,
1370 len - hdrlen);
1371 target_len += len - hdrlen;
1372 hdrlen = 0;
1374 } while (len != 0);
1375 target_path[target_len] = '\0';
1377 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1378 ondisk_path, worktree->root_path);
1379 if (err)
1380 return err;
1382 if (*is_bad_symlink) {
1383 /* install as a regular file */
1384 *is_bad_symlink = 1;
1385 got_object_blob_rewind(blob);
1386 err = install_blob(worktree, ondisk_path, path,
1387 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1388 restoring_missing_file, reverting_versioned_file, 1,
1389 path_is_unversioned, repo, progress_cb, progress_arg);
1390 goto done;
1393 if (symlink(target_path, ondisk_path) == -1) {
1394 if (errno == EEXIST) {
1395 if (path_is_unversioned) {
1396 err = (*progress_cb)(progress_arg,
1397 GOT_STATUS_UNVERSIONED, path);
1398 goto done;
1400 err = replace_existing_symlink(ondisk_path,
1401 target_path, target_len);
1402 if (err)
1403 goto done;
1404 if (progress_cb) {
1405 err = (*progress_cb)(progress_arg,
1406 reverting_versioned_file ?
1407 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1408 path);
1410 goto done; /* Nothing else to do. */
1413 if (errno == ENOENT) {
1414 char *parent = dirname(ondisk_path);
1415 if (parent == NULL) {
1416 err = got_error_from_errno2("dirname",
1417 ondisk_path);
1418 goto done;
1420 err = add_dir_on_disk(worktree, parent);
1421 if (err)
1422 goto done;
1424 * Retry, and fall through to error handling
1425 * below if this second attempt fails.
1427 if (symlink(target_path, ondisk_path) != -1) {
1428 err = NULL; /* success */
1429 goto done;
1433 /* Handle errors from first or second creation attempt. */
1434 if (errno == ENAMETOOLONG) {
1435 /* bad target path; install as a regular file */
1436 *is_bad_symlink = 1;
1437 got_object_blob_rewind(blob);
1438 err = install_blob(worktree, ondisk_path, path,
1439 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1440 restoring_missing_file, reverting_versioned_file, 1,
1441 path_is_unversioned, repo,
1442 progress_cb, progress_arg);
1443 } else if (errno == ENOTDIR) {
1444 err = got_error_path(ondisk_path,
1445 GOT_ERR_FILE_OBSTRUCTED);
1446 } else {
1447 err = got_error_from_errno3("symlink",
1448 target_path, ondisk_path);
1450 } else if (progress_cb)
1451 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1452 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1453 done:
1454 free(path_got);
1455 return err;
1458 static const struct got_error *
1459 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1460 const char *path, mode_t te_mode, mode_t st_mode,
1461 struct got_blob_object *blob, int restoring_missing_file,
1462 int reverting_versioned_file, int installing_bad_symlink,
1463 int path_is_unversioned, struct got_repository *repo,
1464 got_worktree_checkout_cb progress_cb, void *progress_arg)
1466 const struct got_error *err = NULL;
1467 int fd = -1;
1468 size_t len, hdrlen;
1469 int update = 0;
1470 char *tmppath = NULL;
1472 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1473 GOT_DEFAULT_FILE_MODE);
1474 if (fd == -1) {
1475 if (errno == ENOENT) {
1476 char *parent = dirname(path);
1477 if (parent == NULL)
1478 return got_error_from_errno2("dirname", path);
1479 err = add_dir_on_disk(worktree, parent);
1480 if (err)
1481 return err;
1482 fd = open(ondisk_path,
1483 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1484 GOT_DEFAULT_FILE_MODE);
1485 if (fd == -1)
1486 return got_error_from_errno2("open",
1487 ondisk_path);
1488 } else if (errno == EEXIST) {
1489 if (path_is_unversioned) {
1490 err = (*progress_cb)(progress_arg,
1491 GOT_STATUS_UNVERSIONED, path);
1492 goto done;
1494 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1495 /* TODO file is obstructed; do something */
1496 err = got_error_path(ondisk_path,
1497 GOT_ERR_FILE_OBSTRUCTED);
1498 goto done;
1499 } else {
1500 err = got_opentemp_named_fd(&tmppath, &fd,
1501 ondisk_path);
1502 if (err)
1503 goto done;
1504 update = 1;
1506 } else
1507 return got_error_from_errno2("open", ondisk_path);
1510 if (progress_cb) {
1511 if (restoring_missing_file)
1512 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1513 path);
1514 else if (reverting_versioned_file)
1515 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1516 path);
1517 else
1518 err = (*progress_cb)(progress_arg,
1519 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1520 if (err)
1521 goto done;
1524 hdrlen = got_object_blob_get_hdrlen(blob);
1525 do {
1526 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1527 err = got_object_blob_read_block(&len, blob);
1528 if (err)
1529 break;
1530 if (len > 0) {
1531 /* Skip blob object header first time around. */
1532 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1533 if (outlen == -1) {
1534 err = got_error_from_errno("write");
1535 goto done;
1536 } else if (outlen != len - hdrlen) {
1537 err = got_error(GOT_ERR_IO);
1538 goto done;
1540 hdrlen = 0;
1542 } while (len != 0);
1544 if (fsync(fd) != 0) {
1545 err = got_error_from_errno("fsync");
1546 goto done;
1549 if (update) {
1550 if (rename(tmppath, ondisk_path) != 0) {
1551 err = got_error_from_errno3("rename", tmppath,
1552 ondisk_path);
1553 unlink(tmppath);
1554 goto done;
1558 if (chmod(ondisk_path,
1559 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1560 err = got_error_from_errno2("chmod", ondisk_path);
1561 goto done;
1564 done:
1565 if (fd != -1 && close(fd) != 0 && err == NULL)
1566 err = got_error_from_errno("close");
1567 free(tmppath);
1568 return err;
1571 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1572 static const struct got_error *
1573 get_modified_file_content_status(unsigned char *status, FILE *f)
1575 const struct got_error *err = NULL;
1576 const char *markers[3] = {
1577 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1578 GOT_DIFF_CONFLICT_MARKER_SEP,
1579 GOT_DIFF_CONFLICT_MARKER_END
1581 int i = 0;
1582 char *line;
1583 size_t len;
1584 const char delim[3] = {'\0', '\0', '\0'};
1586 while (*status == GOT_STATUS_MODIFY) {
1587 line = fparseln(f, &len, NULL, delim, 0);
1588 if (line == NULL) {
1589 if (feof(f))
1590 break;
1591 err = got_ferror(f, GOT_ERR_IO);
1592 break;
1595 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1596 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1597 == 0)
1598 *status = GOT_STATUS_CONFLICT;
1599 else
1600 i++;
1604 return err;
1607 static int
1608 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1610 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1611 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1614 static int
1615 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1617 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1618 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1619 ie->mtime_sec == sb->st_mtim.tv_sec &&
1620 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1621 ie->size == (sb->st_size & 0xffffffff) &&
1622 !xbit_differs(ie, sb->st_mode));
1625 static unsigned char
1626 get_staged_status(struct got_fileindex_entry *ie)
1628 switch (got_fileindex_entry_stage_get(ie)) {
1629 case GOT_FILEIDX_STAGE_ADD:
1630 return GOT_STATUS_ADD;
1631 case GOT_FILEIDX_STAGE_DELETE:
1632 return GOT_STATUS_DELETE;
1633 case GOT_FILEIDX_STAGE_MODIFY:
1634 return GOT_STATUS_MODIFY;
1635 default:
1636 return GOT_STATUS_NO_CHANGE;
1640 static const struct got_error *
1641 get_symlink_modification_status(unsigned char *status,
1642 struct got_fileindex_entry *ie, const char *abspath,
1643 int dirfd, const char *de_name, struct got_blob_object *blob)
1645 const struct got_error *err = NULL;
1646 char target_path[PATH_MAX];
1647 char etarget[PATH_MAX];
1648 ssize_t elen;
1649 size_t len, target_len = 0;
1650 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1651 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1653 *status = GOT_STATUS_NO_CHANGE;
1655 /* Blob object content specifies the target path of the link. */
1656 do {
1657 err = got_object_blob_read_block(&len, blob);
1658 if (err)
1659 return err;
1660 if (len + target_len >= sizeof(target_path)) {
1662 * Should not happen. The blob contents were OK
1663 * when this symlink was installed.
1665 return got_error(GOT_ERR_NO_SPACE);
1667 if (len > 0) {
1668 /* Skip blob object header first time around. */
1669 memcpy(target_path + target_len, buf + hdrlen,
1670 len - hdrlen);
1671 target_len += len - hdrlen;
1672 hdrlen = 0;
1674 } while (len != 0);
1675 target_path[target_len] = '\0';
1677 if (dirfd != -1) {
1678 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1679 if (elen == -1)
1680 return got_error_from_errno2("readlinkat", abspath);
1681 } else {
1682 elen = readlink(abspath, etarget, sizeof(etarget));
1683 if (elen == -1)
1684 return got_error_from_errno2("readlink", abspath);
1687 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1688 *status = GOT_STATUS_MODIFY;
1690 return NULL;
1693 static const struct got_error *
1694 get_file_status(unsigned char *status, struct stat *sb,
1695 struct got_fileindex_entry *ie, const char *abspath,
1696 int dirfd, const char *de_name, struct got_repository *repo)
1698 const struct got_error *err = NULL;
1699 struct got_object_id id;
1700 size_t hdrlen;
1701 int fd = -1;
1702 FILE *f = NULL;
1703 uint8_t fbuf[8192];
1704 struct got_blob_object *blob = NULL;
1705 size_t flen, blen;
1706 unsigned char staged_status = get_staged_status(ie);
1708 *status = GOT_STATUS_NO_CHANGE;
1711 * Whenever the caller provides a directory descriptor and a
1712 * directory entry name for the file, use them! This prevents
1713 * race conditions if filesystem paths change beneath our feet.
1715 if (dirfd != -1) {
1716 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1717 if (errno == ENOENT) {
1718 if (got_fileindex_entry_has_file_on_disk(ie))
1719 *status = GOT_STATUS_MISSING;
1720 else
1721 *status = GOT_STATUS_DELETE;
1722 goto done;
1724 err = got_error_from_errno2("fstatat", abspath);
1725 goto done;
1727 } else {
1728 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1729 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1730 return got_error_from_errno2("open", abspath);
1731 else if (fd == -1 && errno == ELOOP) {
1732 if (lstat(abspath, sb) == -1)
1733 return got_error_from_errno2("lstat", abspath);
1734 } else if (fd == -1 || fstat(fd, sb) == -1) {
1735 if (errno == ENOENT) {
1736 if (got_fileindex_entry_has_file_on_disk(ie))
1737 *status = GOT_STATUS_MISSING;
1738 else
1739 *status = GOT_STATUS_DELETE;
1740 goto done;
1742 err = got_error_from_errno2("fstat", abspath);
1743 goto done;
1747 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1748 *status = GOT_STATUS_OBSTRUCTED;
1749 goto done;
1752 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1753 *status = GOT_STATUS_DELETE;
1754 goto done;
1755 } else if (!got_fileindex_entry_has_blob(ie) &&
1756 staged_status != GOT_STATUS_ADD) {
1757 *status = GOT_STATUS_ADD;
1758 goto done;
1761 if (!stat_info_differs(ie, sb))
1762 goto done;
1764 if (S_ISLNK(sb->st_mode) &&
1765 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1766 *status = GOT_STATUS_MODIFY;
1767 goto done;
1770 if (staged_status == GOT_STATUS_MODIFY ||
1771 staged_status == GOT_STATUS_ADD)
1772 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1773 else
1774 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1776 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1777 if (err)
1778 goto done;
1780 if (S_ISLNK(sb->st_mode)) {
1781 err = get_symlink_modification_status(status, ie,
1782 abspath, dirfd, de_name, blob);
1783 goto done;
1786 if (dirfd != -1) {
1787 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1788 if (fd == -1) {
1789 err = got_error_from_errno2("openat", abspath);
1790 goto done;
1794 f = fdopen(fd, "r");
1795 if (f == NULL) {
1796 err = got_error_from_errno2("fdopen", abspath);
1797 goto done;
1799 fd = -1;
1800 hdrlen = got_object_blob_get_hdrlen(blob);
1801 for (;;) {
1802 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1803 err = got_object_blob_read_block(&blen, blob);
1804 if (err)
1805 goto done;
1806 /* Skip length of blob object header first time around. */
1807 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1808 if (flen == 0 && ferror(f)) {
1809 err = got_error_from_errno("fread");
1810 goto done;
1812 if (blen - hdrlen == 0) {
1813 if (flen != 0)
1814 *status = GOT_STATUS_MODIFY;
1815 break;
1816 } else if (flen == 0) {
1817 if (blen - hdrlen != 0)
1818 *status = GOT_STATUS_MODIFY;
1819 break;
1820 } else if (blen - hdrlen == flen) {
1821 /* Skip blob object header first time around. */
1822 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1823 *status = GOT_STATUS_MODIFY;
1824 break;
1826 } else {
1827 *status = GOT_STATUS_MODIFY;
1828 break;
1830 hdrlen = 0;
1833 if (*status == GOT_STATUS_MODIFY) {
1834 rewind(f);
1835 err = get_modified_file_content_status(status, f);
1836 } else if (xbit_differs(ie, sb->st_mode))
1837 *status = GOT_STATUS_MODE_CHANGE;
1838 done:
1839 if (blob)
1840 got_object_blob_close(blob);
1841 if (f != NULL && fclose(f) == EOF && err == NULL)
1842 err = got_error_from_errno2("fclose", abspath);
1843 if (fd != -1 && close(fd) == -1 && err == NULL)
1844 err = got_error_from_errno2("close", abspath);
1845 return err;
1849 * Update timestamps in the file index if a file is unmodified and
1850 * we had to run a full content comparison to find out.
1852 static const struct got_error *
1853 sync_timestamps(char *ondisk_path, unsigned char status,
1854 struct got_fileindex_entry *ie, struct stat *sb)
1856 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1857 return got_fileindex_entry_update(ie, ondisk_path,
1858 ie->blob_sha1, ie->commit_sha1, 1);
1860 return NULL;
1863 static const struct got_error *
1864 update_blob(struct got_worktree *worktree,
1865 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1866 struct got_tree_entry *te, const char *path,
1867 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1868 void *progress_arg)
1870 const struct got_error *err = NULL;
1871 struct got_blob_object *blob = NULL;
1872 char *ondisk_path;
1873 unsigned char status = GOT_STATUS_NO_CHANGE;
1874 struct stat sb;
1876 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1877 return got_error_from_errno("asprintf");
1879 if (ie) {
1880 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1881 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1882 goto done;
1884 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1885 repo);
1886 if (err)
1887 goto done;
1888 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1889 sb.st_mode = got_fileindex_perms_to_st(ie);
1890 } else {
1891 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1892 status = GOT_STATUS_UNVERSIONED;
1895 if (status == GOT_STATUS_OBSTRUCTED) {
1896 err = (*progress_cb)(progress_arg, status, path);
1897 goto done;
1899 if (status == GOT_STATUS_CONFLICT) {
1900 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1901 path);
1902 goto done;
1905 if (ie && status != GOT_STATUS_MISSING &&
1906 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1907 if (got_fileindex_entry_has_commit(ie) &&
1908 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1909 SHA1_DIGEST_LENGTH) == 0) {
1910 err = sync_timestamps(ondisk_path, status, ie, &sb);
1911 if (err)
1912 goto done;
1913 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1914 path);
1915 goto done;
1917 if (got_fileindex_entry_has_blob(ie) &&
1918 memcmp(ie->blob_sha1, te->id.sha1,
1919 SHA1_DIGEST_LENGTH) == 0) {
1920 err = sync_timestamps(ondisk_path, status, ie, &sb);
1921 goto done;
1925 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1926 if (err)
1927 goto done;
1929 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1930 int update_timestamps;
1931 struct got_blob_object *blob2 = NULL;
1932 char *label_orig = NULL;
1933 if (got_fileindex_entry_has_blob(ie)) {
1934 struct got_object_id id2;
1935 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1936 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1937 if (err)
1938 goto done;
1940 if (got_fileindex_entry_has_commit(ie)) {
1941 char id_str[SHA1_DIGEST_STRING_LENGTH];
1942 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1943 sizeof(id_str)) == NULL) {
1944 err = got_error_path(id_str,
1945 GOT_ERR_BAD_OBJ_ID_STR);
1946 goto done;
1948 if (asprintf(&label_orig, "%s: commit %s",
1949 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1950 err = got_error_from_errno("asprintf");
1951 goto done;
1954 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1955 char *link_target;
1956 err = got_object_blob_read_to_str(&link_target, blob);
1957 if (err)
1958 goto done;
1959 err = merge_symlink(worktree, blob2, ondisk_path, path,
1960 label_orig, link_target, worktree->base_commit_id,
1961 repo, progress_cb, progress_arg);
1962 free(link_target);
1963 } else {
1964 err = merge_blob(&update_timestamps, worktree, blob2,
1965 ondisk_path, path, sb.st_mode, label_orig, blob,
1966 worktree->base_commit_id, repo,
1967 progress_cb, progress_arg);
1969 free(label_orig);
1970 if (blob2)
1971 got_object_blob_close(blob2);
1972 if (err)
1973 goto done;
1975 * Do not update timestamps of files with local changes.
1976 * Otherwise, a future status walk would treat them as
1977 * unmodified files again.
1979 err = got_fileindex_entry_update(ie, ondisk_path,
1980 blob->id.sha1, worktree->base_commit_id->sha1,
1981 update_timestamps);
1982 } else if (status == GOT_STATUS_MODE_CHANGE) {
1983 err = got_fileindex_entry_update(ie, ondisk_path,
1984 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1985 } else if (status == GOT_STATUS_DELETE) {
1986 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1987 if (err)
1988 goto done;
1989 err = got_fileindex_entry_update(ie, ondisk_path,
1990 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1991 if (err)
1992 goto done;
1993 } else {
1994 int is_bad_symlink = 0;
1995 if (S_ISLNK(te->mode)) {
1996 err = install_symlink(&is_bad_symlink, worktree,
1997 ondisk_path, path, blob,
1998 status == GOT_STATUS_MISSING, 0,
1999 status == GOT_STATUS_UNVERSIONED, repo,
2000 progress_cb, progress_arg);
2001 } else {
2002 err = install_blob(worktree, ondisk_path, path,
2003 te->mode, sb.st_mode, blob,
2004 status == GOT_STATUS_MISSING, 0, 0,
2005 status == GOT_STATUS_UNVERSIONED, repo,
2006 progress_cb, progress_arg);
2008 if (err)
2009 goto done;
2011 if (ie) {
2012 err = got_fileindex_entry_update(ie, ondisk_path,
2013 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2014 } else {
2015 err = create_fileindex_entry(&ie, fileindex,
2016 worktree->base_commit_id, ondisk_path, path,
2017 &blob->id);
2019 if (err)
2020 goto done;
2022 if (is_bad_symlink) {
2023 got_fileindex_entry_filetype_set(ie,
2024 GOT_FILEIDX_MODE_BAD_SYMLINK);
2027 got_object_blob_close(blob);
2028 done:
2029 free(ondisk_path);
2030 return err;
2033 static const struct got_error *
2034 remove_ondisk_file(const char *root_path, const char *path)
2036 const struct got_error *err = NULL;
2037 char *ondisk_path = NULL;
2039 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2040 return got_error_from_errno("asprintf");
2042 if (unlink(ondisk_path) == -1) {
2043 if (errno != ENOENT)
2044 err = got_error_from_errno2("unlink", ondisk_path);
2045 } else {
2046 char *parent = dirname(ondisk_path);
2047 while (parent && strcmp(parent, root_path) != 0) {
2048 if (rmdir(parent) == -1) {
2049 if (errno != ENOTEMPTY)
2050 err = got_error_from_errno2("rmdir",
2051 parent);
2052 break;
2054 parent = dirname(parent);
2057 free(ondisk_path);
2058 return err;
2061 static const struct got_error *
2062 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2063 struct got_fileindex_entry *ie, struct got_repository *repo,
2064 got_worktree_checkout_cb progress_cb, void *progress_arg)
2066 const struct got_error *err = NULL;
2067 unsigned char status;
2068 struct stat sb;
2069 char *ondisk_path;
2071 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2072 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2074 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2075 == -1)
2076 return got_error_from_errno("asprintf");
2078 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2079 if (err)
2080 goto done;
2082 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2083 char ondisk_target[PATH_MAX];
2084 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2085 sizeof(ondisk_target));
2086 if (ondisk_len == -1) {
2087 err = got_error_from_errno2("readlink", ondisk_path);
2088 goto done;
2090 ondisk_target[ondisk_len] = '\0';
2091 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2092 NULL, NULL, /* XXX pass common ancestor info? */
2093 ondisk_target, ondisk_path);
2094 if (err)
2095 goto done;
2096 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2097 ie->path);
2098 goto done;
2101 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2102 status == GOT_STATUS_ADD) {
2103 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2104 if (err)
2105 goto done;
2107 * Preserve the working file and change the deleted blob's
2108 * entry into a schedule-add entry.
2110 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2111 0);
2112 } else {
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2114 if (err)
2115 goto done;
2116 if (status == GOT_STATUS_NO_CHANGE) {
2117 err = remove_ondisk_file(worktree->root_path, ie->path);
2118 if (err)
2119 goto done;
2121 got_fileindex_entry_remove(fileindex, ie);
2123 done:
2124 free(ondisk_path);
2125 return err;
2128 struct diff_cb_arg {
2129 struct got_fileindex *fileindex;
2130 struct got_worktree *worktree;
2131 struct got_repository *repo;
2132 got_worktree_checkout_cb progress_cb;
2133 void *progress_arg;
2134 got_cancel_cb cancel_cb;
2135 void *cancel_arg;
2138 static const struct got_error *
2139 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2140 struct got_tree_entry *te, const char *parent_path)
2142 struct diff_cb_arg *a = arg;
2144 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2145 return got_error(GOT_ERR_CANCELLED);
2147 return update_blob(a->worktree, a->fileindex, ie, te,
2148 ie->path, a->repo, a->progress_cb, a->progress_arg);
2151 static const struct got_error *
2152 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2154 struct diff_cb_arg *a = arg;
2156 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2157 return got_error(GOT_ERR_CANCELLED);
2159 return delete_blob(a->worktree, a->fileindex, ie,
2160 a->repo, a->progress_cb, a->progress_arg);
2163 static const struct got_error *
2164 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2166 struct diff_cb_arg *a = arg;
2167 const struct got_error *err;
2168 char *path;
2170 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2171 return got_error(GOT_ERR_CANCELLED);
2173 if (got_object_tree_entry_is_submodule(te))
2174 return NULL;
2176 if (asprintf(&path, "%s%s%s", parent_path,
2177 parent_path[0] ? "/" : "", te->name)
2178 == -1)
2179 return got_error_from_errno("asprintf");
2181 if (S_ISDIR(te->mode))
2182 err = add_dir_on_disk(a->worktree, path);
2183 else
2184 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2185 a->repo, a->progress_cb, a->progress_arg);
2187 free(path);
2188 return err;
2191 const struct got_error *
2192 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2194 uint32_t uuid_status;
2196 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2197 if (uuid_status != uuid_s_ok) {
2198 *uuidstr = NULL;
2199 return got_error_uuid(uuid_status, "uuid_to_string");
2202 return NULL;
2205 static const struct got_error *
2206 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2208 const struct got_error *err = NULL;
2209 char *uuidstr = NULL;
2211 *refname = NULL;
2213 err = got_worktree_get_uuid(&uuidstr, worktree);
2214 if (err)
2215 return err;
2217 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2218 err = got_error_from_errno("asprintf");
2219 *refname = NULL;
2221 free(uuidstr);
2222 return err;
2225 const struct got_error *
2226 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2228 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2231 static const struct got_error *
2232 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 return get_ref_name(refname, worktree,
2235 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2238 static const struct got_error *
2239 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2241 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2244 static const struct got_error *
2245 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2247 return get_ref_name(refname, worktree,
2248 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2251 static const struct got_error *
2252 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2254 return get_ref_name(refname, worktree,
2255 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2258 static const struct got_error *
2259 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2265 static const struct got_error *
2266 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree,
2269 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2272 static const struct got_error *
2273 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2279 static const struct got_error *
2280 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2286 const struct got_error *
2287 got_worktree_get_histedit_script_path(char **path,
2288 struct got_worktree *worktree)
2290 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2291 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2292 *path = NULL;
2293 return got_error_from_errno("asprintf");
2295 return NULL;
2299 * Prevent Git's garbage collector from deleting our base commit by
2300 * setting a reference to our base commit's ID.
2302 static const struct got_error *
2303 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2305 const struct got_error *err = NULL;
2306 struct got_reference *ref = NULL;
2307 char *refname;
2309 err = got_worktree_get_base_ref_name(&refname, worktree);
2310 if (err)
2311 return err;
2313 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2314 if (err)
2315 goto done;
2317 err = got_ref_write(ref, repo);
2318 done:
2319 free(refname);
2320 if (ref)
2321 got_ref_close(ref);
2322 return err;
2325 static const struct got_error *
2326 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2328 const struct got_error *err = NULL;
2330 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2331 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2332 err = got_error_from_errno("asprintf");
2333 *fileindex_path = NULL;
2335 return err;
2339 static const struct got_error *
2340 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2341 struct got_worktree *worktree)
2343 const struct got_error *err = NULL;
2344 FILE *index = NULL;
2346 *fileindex_path = NULL;
2347 *fileindex = got_fileindex_alloc();
2348 if (*fileindex == NULL)
2349 return got_error_from_errno("got_fileindex_alloc");
2351 err = get_fileindex_path(fileindex_path, worktree);
2352 if (err)
2353 goto done;
2355 index = fopen(*fileindex_path, "rb");
2356 if (index == NULL) {
2357 if (errno != ENOENT)
2358 err = got_error_from_errno2("fopen", *fileindex_path);
2359 } else {
2360 err = got_fileindex_read(*fileindex, index);
2361 if (fclose(index) != 0 && err == NULL)
2362 err = got_error_from_errno("fclose");
2364 done:
2365 if (err) {
2366 free(*fileindex_path);
2367 *fileindex_path = NULL;
2368 got_fileindex_free(*fileindex);
2369 *fileindex = NULL;
2371 return err;
2374 struct bump_base_commit_id_arg {
2375 struct got_object_id *base_commit_id;
2376 const char *path;
2377 size_t path_len;
2378 const char *entry_name;
2379 got_worktree_checkout_cb progress_cb;
2380 void *progress_arg;
2383 /* Bump base commit ID of all files within an updated part of the work tree. */
2384 static const struct got_error *
2385 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2387 const struct got_error *err;
2388 struct bump_base_commit_id_arg *a = arg;
2390 if (a->entry_name) {
2391 if (strcmp(ie->path, a->path) != 0)
2392 return NULL;
2393 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2394 return NULL;
2396 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2397 SHA1_DIGEST_LENGTH) == 0)
2398 return NULL;
2400 if (a->progress_cb) {
2401 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2402 ie->path);
2403 if (err)
2404 return err;
2406 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2407 return NULL;
2410 static const struct got_error *
2411 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2413 const struct got_error *err = NULL;
2414 char *new_fileindex_path = NULL;
2415 FILE *new_index = NULL;
2416 struct timespec timeout;
2418 err = got_opentemp_named(&new_fileindex_path, &new_index,
2419 fileindex_path);
2420 if (err)
2421 goto done;
2423 err = got_fileindex_write(fileindex, new_index);
2424 if (err)
2425 goto done;
2427 if (rename(new_fileindex_path, fileindex_path) != 0) {
2428 err = got_error_from_errno3("rename", new_fileindex_path,
2429 fileindex_path);
2430 unlink(new_fileindex_path);
2434 * Sleep for a short amount of time to ensure that files modified after
2435 * this program exits have a different time stamp from the one which
2436 * was recorded in the file index.
2438 timeout.tv_sec = 0;
2439 timeout.tv_nsec = 1;
2440 nanosleep(&timeout, NULL);
2441 done:
2442 if (new_index)
2443 fclose(new_index);
2444 free(new_fileindex_path);
2445 return err;
2448 static const struct got_error *
2449 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2450 struct got_object_id **tree_id, const char *wt_relpath,
2451 struct got_worktree *worktree, struct got_repository *repo)
2453 const struct got_error *err = NULL;
2454 struct got_object_id *id = NULL;
2455 char *in_repo_path = NULL;
2456 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2458 *entry_type = GOT_OBJ_TYPE_ANY;
2459 *tree_relpath = NULL;
2460 *tree_id = NULL;
2462 if (wt_relpath[0] == '\0') {
2463 /* Check out all files within the work tree. */
2464 *entry_type = GOT_OBJ_TYPE_TREE;
2465 *tree_relpath = strdup("");
2466 if (*tree_relpath == NULL) {
2467 err = got_error_from_errno("strdup");
2468 goto done;
2470 err = got_object_id_by_path(tree_id, repo,
2471 worktree->base_commit_id, worktree->path_prefix);
2472 if (err)
2473 goto done;
2474 return NULL;
2477 /* Check out a subset of files in the work tree. */
2479 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2480 is_root_wt ? "" : "/", wt_relpath) == -1) {
2481 err = got_error_from_errno("asprintf");
2482 goto done;
2485 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2486 in_repo_path);
2487 if (err)
2488 goto done;
2490 free(in_repo_path);
2491 in_repo_path = NULL;
2493 err = got_object_get_type(entry_type, repo, id);
2494 if (err)
2495 goto done;
2497 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2498 /* Check out a single file. */
2499 if (strchr(wt_relpath, '/') == NULL) {
2500 /* Check out a single file in work tree's root dir. */
2501 in_repo_path = strdup(worktree->path_prefix);
2502 if (in_repo_path == NULL) {
2503 err = got_error_from_errno("strdup");
2504 goto done;
2506 *tree_relpath = strdup("");
2507 if (*tree_relpath == NULL) {
2508 err = got_error_from_errno("strdup");
2509 goto done;
2511 } else {
2512 /* Check out a single file in a subdirectory. */
2513 err = got_path_dirname(tree_relpath, wt_relpath);
2514 if (err)
2515 return err;
2516 if (asprintf(&in_repo_path, "%s%s%s",
2517 worktree->path_prefix, is_root_wt ? "" : "/",
2518 *tree_relpath) == -1) {
2519 err = got_error_from_errno("asprintf");
2520 goto done;
2523 err = got_object_id_by_path(tree_id, repo,
2524 worktree->base_commit_id, in_repo_path);
2525 } else {
2526 /* Check out all files within a subdirectory. */
2527 *tree_id = got_object_id_dup(id);
2528 if (*tree_id == NULL) {
2529 err = got_error_from_errno("got_object_id_dup");
2530 goto done;
2532 *tree_relpath = strdup(wt_relpath);
2533 if (*tree_relpath == NULL) {
2534 err = got_error_from_errno("strdup");
2535 goto done;
2538 done:
2539 free(id);
2540 free(in_repo_path);
2541 if (err) {
2542 *entry_type = GOT_OBJ_TYPE_ANY;
2543 free(*tree_relpath);
2544 *tree_relpath = NULL;
2545 free(*tree_id);
2546 *tree_id = NULL;
2548 return err;
2551 static const struct got_error *
2552 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2553 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2554 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2555 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2557 const struct got_error *err = NULL;
2558 struct got_commit_object *commit = NULL;
2559 struct got_tree_object *tree = NULL;
2560 struct got_fileindex_diff_tree_cb diff_cb;
2561 struct diff_cb_arg arg;
2563 err = ref_base_commit(worktree, repo);
2564 if (err) {
2565 if (!(err->code == GOT_ERR_ERRNO &&
2566 (errno == EACCES || errno == EROFS)))
2567 goto done;
2568 err = (*progress_cb)(progress_arg,
2569 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2570 if (err)
2571 return err;
2574 err = got_object_open_as_commit(&commit, repo,
2575 worktree->base_commit_id);
2576 if (err)
2577 goto done;
2579 err = got_object_open_as_tree(&tree, repo, tree_id);
2580 if (err)
2581 goto done;
2583 if (entry_name &&
2584 got_object_tree_find_entry(tree, entry_name) == NULL) {
2585 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2586 goto done;
2589 diff_cb.diff_old_new = diff_old_new;
2590 diff_cb.diff_old = diff_old;
2591 diff_cb.diff_new = diff_new;
2592 arg.fileindex = fileindex;
2593 arg.worktree = worktree;
2594 arg.repo = repo;
2595 arg.progress_cb = progress_cb;
2596 arg.progress_arg = progress_arg;
2597 arg.cancel_cb = cancel_cb;
2598 arg.cancel_arg = cancel_arg;
2599 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2600 entry_name, repo, &diff_cb, &arg);
2601 done:
2602 if (tree)
2603 got_object_tree_close(tree);
2604 if (commit)
2605 got_object_commit_close(commit);
2606 return err;
2609 const struct got_error *
2610 got_worktree_checkout_files(struct got_worktree *worktree,
2611 struct got_pathlist_head *paths, struct got_repository *repo,
2612 got_worktree_checkout_cb progress_cb, void *progress_arg,
2613 got_cancel_cb cancel_cb, void *cancel_arg)
2615 const struct got_error *err = NULL, *sync_err, *unlockerr;
2616 struct got_commit_object *commit = NULL;
2617 struct got_tree_object *tree = NULL;
2618 struct got_fileindex *fileindex = NULL;
2619 char *fileindex_path = NULL;
2620 struct got_pathlist_entry *pe;
2621 struct tree_path_data {
2622 SIMPLEQ_ENTRY(tree_path_data) entry;
2623 struct got_object_id *tree_id;
2624 int entry_type;
2625 char *relpath;
2626 char *entry_name;
2627 } *tpd = NULL;
2628 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2630 SIMPLEQ_INIT(&tree_paths);
2632 err = lock_worktree(worktree, LOCK_EX);
2633 if (err)
2634 return err;
2636 /* Map all specified paths to in-repository trees. */
2637 TAILQ_FOREACH(pe, paths, entry) {
2638 tpd = malloc(sizeof(*tpd));
2639 if (tpd == NULL) {
2640 err = got_error_from_errno("malloc");
2641 goto done;
2644 err = find_tree_entry_for_checkout(&tpd->entry_type,
2645 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2646 if (err) {
2647 free(tpd);
2648 goto done;
2651 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2652 err = got_path_basename(&tpd->entry_name, pe->path);
2653 if (err) {
2654 free(tpd->relpath);
2655 free(tpd->tree_id);
2656 free(tpd);
2657 goto done;
2659 } else
2660 tpd->entry_name = NULL;
2662 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2666 * Read the file index.
2667 * Checking out files is supposed to be an idempotent operation.
2668 * If the on-disk file index is incomplete we will try to complete it.
2670 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2671 if (err)
2672 goto done;
2674 tpd = SIMPLEQ_FIRST(&tree_paths);
2675 TAILQ_FOREACH(pe, paths, entry) {
2676 struct bump_base_commit_id_arg bbc_arg;
2678 err = checkout_files(worktree, fileindex, tpd->relpath,
2679 tpd->tree_id, tpd->entry_name, repo,
2680 progress_cb, progress_arg, cancel_cb, cancel_arg);
2681 if (err)
2682 break;
2684 bbc_arg.base_commit_id = worktree->base_commit_id;
2685 bbc_arg.entry_name = tpd->entry_name;
2686 bbc_arg.path = pe->path;
2687 bbc_arg.path_len = pe->path_len;
2688 bbc_arg.progress_cb = progress_cb;
2689 bbc_arg.progress_arg = progress_arg;
2690 err = got_fileindex_for_each_entry_safe(fileindex,
2691 bump_base_commit_id, &bbc_arg);
2692 if (err)
2693 break;
2695 tpd = SIMPLEQ_NEXT(tpd, entry);
2697 sync_err = sync_fileindex(fileindex, fileindex_path);
2698 if (sync_err && err == NULL)
2699 err = sync_err;
2700 done:
2701 free(fileindex_path);
2702 if (tree)
2703 got_object_tree_close(tree);
2704 if (commit)
2705 got_object_commit_close(commit);
2706 if (fileindex)
2707 got_fileindex_free(fileindex);
2708 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2709 tpd = SIMPLEQ_FIRST(&tree_paths);
2710 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2711 free(tpd->relpath);
2712 free(tpd->tree_id);
2713 free(tpd);
2715 unlockerr = lock_worktree(worktree, LOCK_SH);
2716 if (unlockerr && err == NULL)
2717 err = unlockerr;
2718 return err;
2721 struct merge_file_cb_arg {
2722 struct got_worktree *worktree;
2723 struct got_fileindex *fileindex;
2724 got_worktree_checkout_cb progress_cb;
2725 void *progress_arg;
2726 got_cancel_cb cancel_cb;
2727 void *cancel_arg;
2728 const char *label_orig;
2729 struct got_object_id *commit_id2;
2732 static const struct got_error *
2733 merge_file_cb(void *arg, struct got_blob_object *blob1,
2734 struct got_blob_object *blob2, struct got_object_id *id1,
2735 struct got_object_id *id2, const char *path1, const char *path2,
2736 mode_t mode1, mode_t mode2, struct got_repository *repo)
2738 static const struct got_error *err = NULL;
2739 struct merge_file_cb_arg *a = arg;
2740 struct got_fileindex_entry *ie;
2741 char *ondisk_path = NULL;
2742 struct stat sb;
2743 unsigned char status;
2744 int local_changes_subsumed;
2746 if (blob1 && blob2) {
2747 ie = got_fileindex_entry_get(a->fileindex, path2,
2748 strlen(path2));
2749 if (ie == NULL)
2750 return (*a->progress_cb)(a->progress_arg,
2751 GOT_STATUS_MISSING, path2);
2753 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2754 path2) == -1)
2755 return got_error_from_errno("asprintf");
2757 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2758 repo);
2759 if (err)
2760 goto done;
2762 if (status == GOT_STATUS_DELETE) {
2763 err = (*a->progress_cb)(a->progress_arg,
2764 GOT_STATUS_MERGE, path2);
2765 goto done;
2767 if (status != GOT_STATUS_NO_CHANGE &&
2768 status != GOT_STATUS_MODIFY &&
2769 status != GOT_STATUS_CONFLICT &&
2770 status != GOT_STATUS_ADD) {
2771 err = (*a->progress_cb)(a->progress_arg, status, path2);
2772 goto done;
2775 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2776 char *link_target2;
2777 err = got_object_blob_read_to_str(&link_target2, blob2);
2778 if (err)
2779 goto done;
2780 err = merge_symlink(a->worktree, blob1, ondisk_path,
2781 path2, a->label_orig, link_target2, a->commit_id2,
2782 repo, a->progress_cb, a->progress_arg);
2783 free(link_target2);
2784 } else {
2785 err = merge_blob(&local_changes_subsumed, a->worktree,
2786 blob1, ondisk_path, path2, sb.st_mode,
2787 a->label_orig, blob2, a->commit_id2, repo,
2788 a->progress_cb, a->progress_arg);
2790 } else if (blob1) {
2791 ie = got_fileindex_entry_get(a->fileindex, path1,
2792 strlen(path1));
2793 if (ie == NULL)
2794 return (*a->progress_cb)(a->progress_arg,
2795 GOT_STATUS_MISSING, path1);
2797 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2798 path1) == -1)
2799 return got_error_from_errno("asprintf");
2801 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2802 repo);
2803 if (err)
2804 goto done;
2806 switch (status) {
2807 case GOT_STATUS_NO_CHANGE:
2808 err = (*a->progress_cb)(a->progress_arg,
2809 GOT_STATUS_DELETE, path1);
2810 if (err)
2811 goto done;
2812 err = remove_ondisk_file(a->worktree->root_path, path1);
2813 if (err)
2814 goto done;
2815 if (ie)
2816 got_fileindex_entry_mark_deleted_from_disk(ie);
2817 break;
2818 case GOT_STATUS_DELETE:
2819 case GOT_STATUS_MISSING:
2820 err = (*a->progress_cb)(a->progress_arg,
2821 GOT_STATUS_DELETE, path1);
2822 if (err)
2823 goto done;
2824 if (ie)
2825 got_fileindex_entry_mark_deleted_from_disk(ie);
2826 break;
2827 case GOT_STATUS_ADD: {
2828 struct got_object_id *id;
2829 FILE *blob1_f;
2831 * Delete the added file only if its content already
2832 * exists in the repository.
2834 err = got_object_blob_file_create(&id, &blob1_f, path1);
2835 if (err)
2836 goto done;
2837 if (got_object_id_cmp(id, id1) == 0) {
2838 err = (*a->progress_cb)(a->progress_arg,
2839 GOT_STATUS_DELETE, path1);
2840 if (err)
2841 goto done;
2842 err = remove_ondisk_file(a->worktree->root_path,
2843 path1);
2844 if (err)
2845 goto done;
2846 if (ie)
2847 got_fileindex_entry_remove(a->fileindex,
2848 ie);
2849 } else {
2850 err = (*a->progress_cb)(a->progress_arg,
2851 GOT_STATUS_CANNOT_DELETE, path1);
2853 if (fclose(blob1_f) == EOF && err == NULL)
2854 err = got_error_from_errno("fclose");
2855 free(id);
2856 if (err)
2857 goto done;
2858 break;
2860 case GOT_STATUS_MODIFY:
2861 case GOT_STATUS_CONFLICT:
2862 err = (*a->progress_cb)(a->progress_arg,
2863 GOT_STATUS_CANNOT_DELETE, path1);
2864 if (err)
2865 goto done;
2866 break;
2867 case GOT_STATUS_OBSTRUCTED:
2868 err = (*a->progress_cb)(a->progress_arg, status, path1);
2869 if (err)
2870 goto done;
2871 break;
2872 default:
2873 break;
2875 } else if (blob2) {
2876 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2877 path2) == -1)
2878 return got_error_from_errno("asprintf");
2879 ie = got_fileindex_entry_get(a->fileindex, path2,
2880 strlen(path2));
2881 if (ie) {
2882 err = get_file_status(&status, &sb, ie, ondisk_path,
2883 -1, NULL, repo);
2884 if (err)
2885 goto done;
2886 if (status != GOT_STATUS_NO_CHANGE &&
2887 status != GOT_STATUS_MODIFY &&
2888 status != GOT_STATUS_CONFLICT &&
2889 status != GOT_STATUS_ADD) {
2890 err = (*a->progress_cb)(a->progress_arg,
2891 status, path2);
2892 goto done;
2894 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2895 char *link_target2;
2896 err = got_object_blob_read_to_str(&link_target2,
2897 blob2);
2898 if (err)
2899 goto done;
2900 err = merge_symlink(a->worktree, NULL,
2901 ondisk_path, path2, a->label_orig,
2902 link_target2, a->commit_id2, repo,
2903 a->progress_cb, a->progress_arg);
2904 free(link_target2);
2905 } else if (S_ISREG(sb.st_mode)) {
2906 err = merge_blob(&local_changes_subsumed,
2907 a->worktree, NULL, ondisk_path, path2,
2908 sb.st_mode, a->label_orig, blob2,
2909 a->commit_id2, repo, a->progress_cb,
2910 a->progress_arg);
2911 } else {
2912 err = got_error_path(ondisk_path,
2913 GOT_ERR_FILE_OBSTRUCTED);
2915 if (err)
2916 goto done;
2917 if (status == GOT_STATUS_DELETE) {
2918 err = got_fileindex_entry_update(ie,
2919 ondisk_path, blob2->id.sha1,
2920 a->worktree->base_commit_id->sha1, 0);
2921 if (err)
2922 goto done;
2924 } else {
2925 int is_bad_symlink = 0;
2926 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2927 if (S_ISLNK(mode2)) {
2928 err = install_symlink(&is_bad_symlink,
2929 a->worktree, ondisk_path, path2, blob2, 0,
2930 0, 1, repo, a->progress_cb, a->progress_arg);
2931 } else {
2932 err = install_blob(a->worktree, ondisk_path, path2,
2933 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2934 a->progress_cb, a->progress_arg);
2936 if (err)
2937 goto done;
2938 err = got_fileindex_entry_alloc(&ie, path2);
2939 if (err)
2940 goto done;
2941 err = got_fileindex_entry_update(ie, ondisk_path,
2942 NULL, NULL, 1);
2943 if (err) {
2944 got_fileindex_entry_free(ie);
2945 goto done;
2947 err = got_fileindex_entry_add(a->fileindex, ie);
2948 if (err) {
2949 got_fileindex_entry_free(ie);
2950 goto done;
2952 if (is_bad_symlink) {
2953 got_fileindex_entry_filetype_set(ie,
2954 GOT_FILEIDX_MODE_BAD_SYMLINK);
2958 done:
2959 free(ondisk_path);
2960 return err;
2963 struct check_merge_ok_arg {
2964 struct got_worktree *worktree;
2965 struct got_repository *repo;
2968 static const struct got_error *
2969 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2971 const struct got_error *err = NULL;
2972 struct check_merge_ok_arg *a = arg;
2973 unsigned char status;
2974 struct stat sb;
2975 char *ondisk_path;
2977 /* Reject merges into a work tree with mixed base commits. */
2978 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2979 SHA1_DIGEST_LENGTH))
2980 return got_error(GOT_ERR_MIXED_COMMITS);
2982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2983 == -1)
2984 return got_error_from_errno("asprintf");
2986 /* Reject merges into a work tree with conflicted files. */
2987 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2988 if (err)
2989 return err;
2990 if (status == GOT_STATUS_CONFLICT)
2991 return got_error(GOT_ERR_CONFLICTS);
2993 return NULL;
2996 static const struct got_error *
2997 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2998 const char *fileindex_path, struct got_object_id *commit_id1,
2999 struct got_object_id *commit_id2, struct got_repository *repo,
3000 got_worktree_checkout_cb progress_cb, void *progress_arg,
3001 got_cancel_cb cancel_cb, void *cancel_arg)
3003 const struct got_error *err = NULL, *sync_err;
3004 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3005 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3006 struct merge_file_cb_arg arg;
3007 char *label_orig = NULL;
3009 if (commit_id1) {
3010 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3011 worktree->path_prefix);
3012 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3013 goto done;
3015 if (tree_id1) {
3016 char *id_str;
3018 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3019 if (err)
3020 goto done;
3022 err = got_object_id_str(&id_str, commit_id1);
3023 if (err)
3024 goto done;
3026 if (asprintf(&label_orig, "%s: commit %s",
3027 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3028 err = got_error_from_errno("asprintf");
3029 free(id_str);
3030 goto done;
3032 free(id_str);
3035 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3036 worktree->path_prefix);
3037 if (err)
3038 goto done;
3040 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3041 if (err)
3042 goto done;
3044 arg.worktree = worktree;
3045 arg.fileindex = fileindex;
3046 arg.progress_cb = progress_cb;
3047 arg.progress_arg = progress_arg;
3048 arg.cancel_cb = cancel_cb;
3049 arg.cancel_arg = cancel_arg;
3050 arg.label_orig = label_orig;
3051 arg.commit_id2 = commit_id2;
3052 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3053 sync_err = sync_fileindex(fileindex, fileindex_path);
3054 if (sync_err && err == NULL)
3055 err = sync_err;
3056 done:
3057 if (tree1)
3058 got_object_tree_close(tree1);
3059 if (tree2)
3060 got_object_tree_close(tree2);
3061 free(label_orig);
3062 return err;
3065 const struct got_error *
3066 got_worktree_merge_files(struct got_worktree *worktree,
3067 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3068 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3069 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3071 const struct got_error *err, *unlockerr;
3072 char *fileindex_path = NULL;
3073 struct got_fileindex *fileindex = NULL;
3074 struct check_merge_ok_arg mok_arg;
3076 err = lock_worktree(worktree, LOCK_EX);
3077 if (err)
3078 return err;
3080 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3081 if (err)
3082 goto done;
3084 mok_arg.worktree = worktree;
3085 mok_arg.repo = repo;
3086 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3087 &mok_arg);
3088 if (err)
3089 goto done;
3091 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3092 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3093 done:
3094 if (fileindex)
3095 got_fileindex_free(fileindex);
3096 free(fileindex_path);
3097 unlockerr = lock_worktree(worktree, LOCK_SH);
3098 if (unlockerr && err == NULL)
3099 err = unlockerr;
3100 return err;
3103 struct diff_dir_cb_arg {
3104 struct got_fileindex *fileindex;
3105 struct got_worktree *worktree;
3106 const char *status_path;
3107 size_t status_path_len;
3108 struct got_repository *repo;
3109 got_worktree_status_cb status_cb;
3110 void *status_arg;
3111 got_cancel_cb cancel_cb;
3112 void *cancel_arg;
3113 /* A pathlist containing per-directory pathlists of ignore patterns. */
3114 struct got_pathlist_head ignores;
3115 int report_unchanged;
3116 int no_ignores;
3119 static const struct got_error *
3120 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3121 int dirfd, const char *de_name,
3122 got_worktree_status_cb status_cb, void *status_arg,
3123 struct got_repository *repo, int report_unchanged)
3125 const struct got_error *err = NULL;
3126 unsigned char status = GOT_STATUS_NO_CHANGE;
3127 unsigned char staged_status = get_staged_status(ie);
3128 struct stat sb;
3129 struct got_object_id blob_id, commit_id, staged_blob_id;
3130 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3131 struct got_object_id *staged_blob_idp = NULL;
3133 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3134 if (err)
3135 return err;
3137 if (status == GOT_STATUS_NO_CHANGE &&
3138 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3139 return NULL;
3141 if (got_fileindex_entry_has_blob(ie)) {
3142 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3143 blob_idp = &blob_id;
3145 if (got_fileindex_entry_has_commit(ie)) {
3146 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3147 commit_idp = &commit_id;
3149 if (staged_status == GOT_STATUS_ADD ||
3150 staged_status == GOT_STATUS_MODIFY) {
3151 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3152 SHA1_DIGEST_LENGTH);
3153 staged_blob_idp = &staged_blob_id;
3156 return (*status_cb)(status_arg, status, staged_status,
3157 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3160 static const struct got_error *
3161 status_old_new(void *arg, struct got_fileindex_entry *ie,
3162 struct dirent *de, const char *parent_path, int dirfd)
3164 const struct got_error *err = NULL;
3165 struct diff_dir_cb_arg *a = arg;
3166 char *abspath;
3168 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3169 return got_error(GOT_ERR_CANCELLED);
3171 if (got_path_cmp(parent_path, a->status_path,
3172 strlen(parent_path), a->status_path_len) != 0 &&
3173 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3174 return NULL;
3176 if (parent_path[0]) {
3177 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3178 parent_path, de->d_name) == -1)
3179 return got_error_from_errno("asprintf");
3180 } else {
3181 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3182 de->d_name) == -1)
3183 return got_error_from_errno("asprintf");
3186 err = report_file_status(ie, abspath, dirfd, de->d_name,
3187 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3188 free(abspath);
3189 return err;
3192 static const struct got_error *
3193 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3195 struct diff_dir_cb_arg *a = arg;
3196 struct got_object_id blob_id, commit_id;
3197 unsigned char status;
3199 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3200 return got_error(GOT_ERR_CANCELLED);
3202 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3203 return NULL;
3205 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3206 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3207 if (got_fileindex_entry_has_file_on_disk(ie))
3208 status = GOT_STATUS_MISSING;
3209 else
3210 status = GOT_STATUS_DELETE;
3211 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3212 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3215 void
3216 free_ignorelist(struct got_pathlist_head *ignorelist)
3218 struct got_pathlist_entry *pe;
3220 TAILQ_FOREACH(pe, ignorelist, entry)
3221 free((char *)pe->path);
3222 got_pathlist_free(ignorelist);
3225 void
3226 free_ignores(struct got_pathlist_head *ignores)
3228 struct got_pathlist_entry *pe;
3230 TAILQ_FOREACH(pe, ignores, entry) {
3231 struct got_pathlist_head *ignorelist = pe->data;
3232 free_ignorelist(ignorelist);
3233 free((char *)pe->path);
3235 got_pathlist_free(ignores);
3238 static const struct got_error *
3239 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3241 const struct got_error *err = NULL;
3242 struct got_pathlist_entry *pe = NULL;
3243 struct got_pathlist_head *ignorelist;
3244 char *line = NULL, *pattern, *dirpath = NULL;
3245 size_t linesize = 0;
3246 ssize_t linelen;
3248 ignorelist = calloc(1, sizeof(*ignorelist));
3249 if (ignorelist == NULL)
3250 return got_error_from_errno("calloc");
3251 TAILQ_INIT(ignorelist);
3253 while ((linelen = getline(&line, &linesize, f)) != -1) {
3254 if (linelen > 0 && line[linelen - 1] == '\n')
3255 line[linelen - 1] = '\0';
3257 /* Git's ignores may contain comments. */
3258 if (line[0] == '#')
3259 continue;
3261 /* Git's negated patterns are not (yet?) supported. */
3262 if (line[0] == '!')
3263 continue;
3265 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3266 line) == -1) {
3267 err = got_error_from_errno("asprintf");
3268 goto done;
3270 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3271 if (err)
3272 goto done;
3274 if (ferror(f)) {
3275 err = got_error_from_errno("getline");
3276 goto done;
3279 dirpath = strdup(path);
3280 if (dirpath == NULL) {
3281 err = got_error_from_errno("strdup");
3282 goto done;
3284 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3285 done:
3286 free(line);
3287 if (err || pe == NULL) {
3288 free(dirpath);
3289 free_ignorelist(ignorelist);
3291 return err;
3294 int
3295 match_ignores(struct got_pathlist_head *ignores, const char *path)
3297 struct got_pathlist_entry *pe;
3299 /* Handle patterns which match in all directories. */
3300 TAILQ_FOREACH(pe, ignores, entry) {
3301 struct got_pathlist_head *ignorelist = pe->data;
3302 struct got_pathlist_entry *pi;
3304 TAILQ_FOREACH(pi, ignorelist, entry) {
3305 const char *p, *pattern = pi->path;
3307 if (strncmp(pattern, "**/", 3) != 0)
3308 continue;
3309 pattern += 3;
3310 p = path;
3311 while (*p) {
3312 if (fnmatch(pattern, p,
3313 FNM_PATHNAME | FNM_LEADING_DIR)) {
3314 /* Retry in next directory. */
3315 while (*p && *p != '/')
3316 p++;
3317 while (*p == '/')
3318 p++;
3319 continue;
3321 return 1;
3327 * The ignores pathlist contains ignore lists from children before
3328 * parents, so we can find the most specific ignorelist by walking
3329 * ignores backwards.
3331 pe = TAILQ_LAST(ignores, got_pathlist_head);
3332 while (pe) {
3333 if (got_path_is_child(path, pe->path, pe->path_len)) {
3334 struct got_pathlist_head *ignorelist = pe->data;
3335 struct got_pathlist_entry *pi;
3336 TAILQ_FOREACH(pi, ignorelist, entry) {
3337 const char *pattern = pi->path;
3338 int flags = FNM_LEADING_DIR;
3339 if (strstr(pattern, "/**/") == NULL)
3340 flags |= FNM_PATHNAME;
3341 if (fnmatch(pattern, path, flags))
3342 continue;
3343 return 1;
3346 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3349 return 0;
3352 static const struct got_error *
3353 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3354 const char *path, int dirfd, const char *ignores_filename)
3356 const struct got_error *err = NULL;
3357 char *ignorespath;
3358 int fd = -1;
3359 FILE *ignoresfile = NULL;
3361 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3362 path[0] ? "/" : "", ignores_filename) == -1)
3363 return got_error_from_errno("asprintf");
3365 if (dirfd != -1) {
3366 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3367 if (fd == -1) {
3368 if (errno != ENOENT && errno != EACCES)
3369 err = got_error_from_errno2("openat",
3370 ignorespath);
3371 } else {
3372 ignoresfile = fdopen(fd, "r");
3373 if (ignoresfile == NULL)
3374 err = got_error_from_errno2("fdopen",
3375 ignorespath);
3376 else {
3377 fd = -1;
3378 err = read_ignores(ignores, path, ignoresfile);
3381 } else {
3382 ignoresfile = fopen(ignorespath, "r");
3383 if (ignoresfile == NULL) {
3384 if (errno != ENOENT && errno != EACCES)
3385 err = got_error_from_errno2("fopen",
3386 ignorespath);
3387 } else
3388 err = read_ignores(ignores, path, ignoresfile);
3391 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3392 err = got_error_from_errno2("fclose", path);
3393 if (fd != -1 && close(fd) == -1 && err == NULL)
3394 err = got_error_from_errno2("close", path);
3395 free(ignorespath);
3396 return err;
3399 static const struct got_error *
3400 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3402 const struct got_error *err = NULL;
3403 struct diff_dir_cb_arg *a = arg;
3404 char *path = NULL;
3406 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3407 return got_error(GOT_ERR_CANCELLED);
3409 if (parent_path[0]) {
3410 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3411 return got_error_from_errno("asprintf");
3412 } else {
3413 path = de->d_name;
3416 if (de->d_type != DT_DIR &&
3417 got_path_is_child(path, a->status_path, a->status_path_len)
3418 && !match_ignores(&a->ignores, path))
3419 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3420 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3421 if (parent_path[0])
3422 free(path);
3423 return err;
3426 static const struct got_error *
3427 status_traverse(void *arg, const char *path, int dirfd)
3429 const struct got_error *err = NULL;
3430 struct diff_dir_cb_arg *a = arg;
3432 if (a->no_ignores)
3433 return NULL;
3435 err = add_ignores(&a->ignores, a->worktree->root_path,
3436 path, dirfd, ".cvsignore");
3437 if (err)
3438 return err;
3440 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3441 dirfd, ".gitignore");
3443 return err;
3446 static const struct got_error *
3447 report_single_file_status(const char *path, const char *ondisk_path,
3448 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3449 void *status_arg, struct got_repository *repo, int report_unchanged)
3451 struct got_fileindex_entry *ie;
3452 struct stat sb;
3454 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3455 if (ie)
3456 return report_file_status(ie, ondisk_path, -1, NULL,
3457 status_cb, status_arg, repo, report_unchanged);
3459 if (lstat(ondisk_path, &sb) == -1) {
3460 if (errno != ENOENT)
3461 return got_error_from_errno2("lstat", ondisk_path);
3462 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3463 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3464 return NULL;
3467 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3468 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3469 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3471 return NULL;
3474 static const struct got_error *
3475 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3476 const char *root_path, const char *path)
3478 const struct got_error *err;
3479 char *parent_path, *next_parent_path = NULL;
3481 err = add_ignores(ignores, root_path, "", -1,
3482 ".cvsignore");
3483 if (err)
3484 return err;
3486 err = add_ignores(ignores, root_path, "", -1,
3487 ".gitignore");
3488 if (err)
3489 return err;
3491 err = got_path_dirname(&parent_path, path);
3492 if (err) {
3493 if (err->code == GOT_ERR_BAD_PATH)
3494 return NULL; /* cannot traverse parent */
3495 return err;
3497 for (;;) {
3498 err = add_ignores(ignores, root_path, parent_path, -1,
3499 ".cvsignore");
3500 if (err)
3501 break;
3502 err = add_ignores(ignores, root_path, parent_path, -1,
3503 ".gitignore");
3504 if (err)
3505 break;
3506 err = got_path_dirname(&next_parent_path, parent_path);
3507 if (err) {
3508 if (err->code == GOT_ERR_BAD_PATH)
3509 err = NULL; /* traversed everything */
3510 break;
3512 free(parent_path);
3513 parent_path = next_parent_path;
3514 next_parent_path = NULL;
3517 free(parent_path);
3518 free(next_parent_path);
3519 return err;
3522 static const struct got_error *
3523 worktree_status(struct got_worktree *worktree, const char *path,
3524 struct got_fileindex *fileindex, struct got_repository *repo,
3525 got_worktree_status_cb status_cb, void *status_arg,
3526 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3527 int report_unchanged)
3529 const struct got_error *err = NULL;
3530 int fd = -1;
3531 struct got_fileindex_diff_dir_cb fdiff_cb;
3532 struct diff_dir_cb_arg arg;
3533 char *ondisk_path = NULL;
3535 TAILQ_INIT(&arg.ignores);
3537 if (asprintf(&ondisk_path, "%s%s%s",
3538 worktree->root_path, path[0] ? "/" : "", path) == -1)
3539 return got_error_from_errno("asprintf");
3541 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3542 if (fd == -1) {
3543 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3544 errno != ELOOP)
3545 err = got_error_from_errno2("open", ondisk_path);
3546 else
3547 err = report_single_file_status(path, ondisk_path,
3548 fileindex, status_cb, status_arg, repo,
3549 report_unchanged);
3550 } else {
3551 fdiff_cb.diff_old_new = status_old_new;
3552 fdiff_cb.diff_old = status_old;
3553 fdiff_cb.diff_new = status_new;
3554 fdiff_cb.diff_traverse = status_traverse;
3555 arg.fileindex = fileindex;
3556 arg.worktree = worktree;
3557 arg.status_path = path;
3558 arg.status_path_len = strlen(path);
3559 arg.repo = repo;
3560 arg.status_cb = status_cb;
3561 arg.status_arg = status_arg;
3562 arg.cancel_cb = cancel_cb;
3563 arg.cancel_arg = cancel_arg;
3564 arg.report_unchanged = report_unchanged;
3565 arg.no_ignores = no_ignores;
3566 if (!no_ignores) {
3567 err = add_ignores_from_parent_paths(&arg.ignores,
3568 worktree->root_path, path);
3569 if (err)
3570 goto done;
3572 err = got_fileindex_diff_dir(fileindex, fd,
3573 worktree->root_path, path, repo, &fdiff_cb, &arg);
3575 done:
3576 free_ignores(&arg.ignores);
3577 if (fd != -1 && close(fd) != 0 && err == NULL)
3578 err = got_error_from_errno("close");
3579 free(ondisk_path);
3580 return err;
3583 const struct got_error *
3584 got_worktree_status(struct got_worktree *worktree,
3585 struct got_pathlist_head *paths, struct got_repository *repo,
3586 got_worktree_status_cb status_cb, void *status_arg,
3587 got_cancel_cb cancel_cb, void *cancel_arg)
3589 const struct got_error *err = NULL;
3590 char *fileindex_path = NULL;
3591 struct got_fileindex *fileindex = NULL;
3592 struct got_pathlist_entry *pe;
3594 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3595 if (err)
3596 return err;
3598 TAILQ_FOREACH(pe, paths, entry) {
3599 err = worktree_status(worktree, pe->path, fileindex, repo,
3600 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3601 if (err)
3602 break;
3604 free(fileindex_path);
3605 got_fileindex_free(fileindex);
3606 return err;
3609 const struct got_error *
3610 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3611 const char *arg)
3613 const struct got_error *err = NULL;
3614 char *resolved = NULL, *cwd = NULL, *path = NULL;
3615 size_t len;
3616 struct stat sb;
3618 *wt_path = NULL;
3620 cwd = getcwd(NULL, 0);
3621 if (cwd == NULL)
3622 return got_error_from_errno("getcwd");
3624 if (lstat(arg, &sb) == -1) {
3625 if (errno != ENOENT) {
3626 err = got_error_from_errno2("lstat", arg);
3627 goto done;
3630 if (S_ISLNK(sb.st_mode)) {
3632 * We cannot use realpath(3) with symlinks since we want to
3633 * operate on the symlink itself.
3634 * But we can make the path absolute, assuming it is relative
3635 * to the current working directory, and then canonicalize it.
3637 char *abspath = NULL;
3638 char canonpath[PATH_MAX];
3639 if (!got_path_is_absolute(arg)) {
3640 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3641 err = got_error_from_errno("asprintf");
3642 goto done;
3646 err = got_canonpath(abspath ? abspath : arg, canonpath,
3647 sizeof(canonpath));
3648 if (err)
3649 goto done;
3650 resolved = strdup(canonpath);
3651 if (resolved == NULL) {
3652 err = got_error_from_errno("strdup");
3653 goto done;
3655 } else {
3656 resolved = realpath(arg, NULL);
3657 if (resolved == NULL) {
3658 if (errno != ENOENT) {
3659 err = got_error_from_errno2("realpath", arg);
3660 goto done;
3662 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3663 err = got_error_from_errno("asprintf");
3664 goto done;
3669 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3670 strlen(got_worktree_get_root_path(worktree)))) {
3671 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3672 goto done;
3675 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3676 err = got_path_skip_common_ancestor(&path,
3677 got_worktree_get_root_path(worktree), resolved);
3678 if (err)
3679 goto done;
3680 } else {
3681 path = strdup("");
3682 if (path == NULL) {
3683 err = got_error_from_errno("strdup");
3684 goto done;
3688 /* XXX status walk can't deal with trailing slash! */
3689 len = strlen(path);
3690 while (len > 0 && path[len - 1] == '/') {
3691 path[len - 1] = '\0';
3692 len--;
3694 done:
3695 free(resolved);
3696 free(cwd);
3697 if (err == NULL)
3698 *wt_path = path;
3699 else
3700 free(path);
3701 return err;
3704 struct schedule_addition_args {
3705 struct got_worktree *worktree;
3706 struct got_fileindex *fileindex;
3707 got_worktree_checkout_cb progress_cb;
3708 void *progress_arg;
3709 struct got_repository *repo;
3712 static const struct got_error *
3713 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3714 const char *relpath, struct got_object_id *blob_id,
3715 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3716 int dirfd, const char *de_name)
3718 struct schedule_addition_args *a = arg;
3719 const struct got_error *err = NULL;
3720 struct got_fileindex_entry *ie;
3721 struct stat sb;
3722 char *ondisk_path;
3724 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3725 relpath) == -1)
3726 return got_error_from_errno("asprintf");
3728 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3729 if (ie) {
3730 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3731 de_name, a->repo);
3732 if (err)
3733 goto done;
3734 /* Re-adding an existing entry is a no-op. */
3735 if (status == GOT_STATUS_ADD)
3736 goto done;
3737 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3738 if (err)
3739 goto done;
3742 if (status != GOT_STATUS_UNVERSIONED) {
3743 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3744 goto done;
3747 err = got_fileindex_entry_alloc(&ie, relpath);
3748 if (err)
3749 goto done;
3750 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3751 if (err) {
3752 got_fileindex_entry_free(ie);
3753 goto done;
3755 err = got_fileindex_entry_add(a->fileindex, ie);
3756 if (err) {
3757 got_fileindex_entry_free(ie);
3758 goto done;
3760 done:
3761 free(ondisk_path);
3762 if (err)
3763 return err;
3764 if (status == GOT_STATUS_ADD)
3765 return NULL;
3766 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3769 const struct got_error *
3770 got_worktree_schedule_add(struct got_worktree *worktree,
3771 struct got_pathlist_head *paths,
3772 got_worktree_checkout_cb progress_cb, void *progress_arg,
3773 struct got_repository *repo, int no_ignores)
3775 struct got_fileindex *fileindex = NULL;
3776 char *fileindex_path = NULL;
3777 const struct got_error *err = NULL, *sync_err, *unlockerr;
3778 struct got_pathlist_entry *pe;
3779 struct schedule_addition_args saa;
3781 err = lock_worktree(worktree, LOCK_EX);
3782 if (err)
3783 return err;
3785 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3786 if (err)
3787 goto done;
3789 saa.worktree = worktree;
3790 saa.fileindex = fileindex;
3791 saa.progress_cb = progress_cb;
3792 saa.progress_arg = progress_arg;
3793 saa.repo = repo;
3795 TAILQ_FOREACH(pe, paths, entry) {
3796 err = worktree_status(worktree, pe->path, fileindex, repo,
3797 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3798 if (err)
3799 break;
3801 sync_err = sync_fileindex(fileindex, fileindex_path);
3802 if (sync_err && err == NULL)
3803 err = sync_err;
3804 done:
3805 free(fileindex_path);
3806 if (fileindex)
3807 got_fileindex_free(fileindex);
3808 unlockerr = lock_worktree(worktree, LOCK_SH);
3809 if (unlockerr && err == NULL)
3810 err = unlockerr;
3811 return err;
3814 struct schedule_deletion_args {
3815 struct got_worktree *worktree;
3816 struct got_fileindex *fileindex;
3817 got_worktree_delete_cb progress_cb;
3818 void *progress_arg;
3819 struct got_repository *repo;
3820 int delete_local_mods;
3821 int keep_on_disk;
3822 const char *status_codes;
3825 static const struct got_error *
3826 schedule_for_deletion(void *arg, unsigned char status,
3827 unsigned char staged_status, const char *relpath,
3828 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3829 struct got_object_id *commit_id, int dirfd, const char *de_name)
3831 struct schedule_deletion_args *a = arg;
3832 const struct got_error *err = NULL;
3833 struct got_fileindex_entry *ie = NULL;
3834 struct stat sb;
3835 char *ondisk_path, *parent = NULL;
3837 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3838 if (ie == NULL)
3839 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3841 staged_status = get_staged_status(ie);
3842 if (staged_status != GOT_STATUS_NO_CHANGE) {
3843 if (staged_status == GOT_STATUS_DELETE)
3844 return NULL;
3845 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3848 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3849 relpath) == -1)
3850 return got_error_from_errno("asprintf");
3852 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3853 a->repo);
3854 if (err)
3855 goto done;
3857 if (a->status_codes) {
3858 size_t ncodes = strlen(a->status_codes);
3859 int i;
3860 for (i = 0; i < ncodes ; i++) {
3861 if (status == a->status_codes[i])
3862 break;
3864 if (i == ncodes) {
3865 /* Do not delete files in non-matching status. */
3866 free(ondisk_path);
3867 return NULL;
3869 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3870 a->status_codes[i] != GOT_STATUS_MISSING) {
3871 static char msg[64];
3872 snprintf(msg, sizeof(msg),
3873 "invalid status code '%c'", a->status_codes[i]);
3874 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3875 goto done;
3879 if (status != GOT_STATUS_NO_CHANGE) {
3880 if (status == GOT_STATUS_DELETE)
3881 goto done;
3882 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3883 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3884 goto done;
3886 if (status != GOT_STATUS_MODIFY &&
3887 status != GOT_STATUS_MISSING) {
3888 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3889 goto done;
3893 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3894 if (dirfd != -1) {
3895 if (unlinkat(dirfd, de_name, 0) != 0) {
3896 err = got_error_from_errno2("unlinkat",
3897 ondisk_path);
3898 goto done;
3900 } else if (unlink(ondisk_path) != 0) {
3901 err = got_error_from_errno2("unlink", ondisk_path);
3902 goto done;
3905 parent = dirname(ondisk_path);
3907 if (parent == NULL) {
3908 err = got_error_from_errno2("dirname", ondisk_path);
3909 goto done;
3911 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3912 if (rmdir(parent) == -1) {
3913 if (errno != ENOTEMPTY)
3914 err = got_error_from_errno2("rmdir",
3915 parent);
3916 break;
3918 parent = dirname(parent);
3919 if (parent == NULL) {
3920 err = got_error_from_errno2("dirname", parent);
3921 goto done;
3926 got_fileindex_entry_mark_deleted_from_disk(ie);
3927 done:
3928 free(ondisk_path);
3929 if (err)
3930 return err;
3931 if (status == GOT_STATUS_DELETE)
3932 return NULL;
3933 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3934 staged_status, relpath);
3937 const struct got_error *
3938 got_worktree_schedule_delete(struct got_worktree *worktree,
3939 struct got_pathlist_head *paths, int delete_local_mods,
3940 const char *status_codes,
3941 got_worktree_delete_cb progress_cb, void *progress_arg,
3942 struct got_repository *repo, int keep_on_disk)
3944 struct got_fileindex *fileindex = NULL;
3945 char *fileindex_path = NULL;
3946 const struct got_error *err = NULL, *sync_err, *unlockerr;
3947 struct got_pathlist_entry *pe;
3948 struct schedule_deletion_args sda;
3950 err = lock_worktree(worktree, LOCK_EX);
3951 if (err)
3952 return err;
3954 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3955 if (err)
3956 goto done;
3958 sda.worktree = worktree;
3959 sda.fileindex = fileindex;
3960 sda.progress_cb = progress_cb;
3961 sda.progress_arg = progress_arg;
3962 sda.repo = repo;
3963 sda.delete_local_mods = delete_local_mods;
3964 sda.keep_on_disk = keep_on_disk;
3965 sda.status_codes = status_codes;
3967 TAILQ_FOREACH(pe, paths, entry) {
3968 err = worktree_status(worktree, pe->path, fileindex, repo,
3969 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3970 if (err)
3971 break;
3973 sync_err = sync_fileindex(fileindex, fileindex_path);
3974 if (sync_err && err == NULL)
3975 err = sync_err;
3976 done:
3977 free(fileindex_path);
3978 if (fileindex)
3979 got_fileindex_free(fileindex);
3980 unlockerr = lock_worktree(worktree, LOCK_SH);
3981 if (unlockerr && err == NULL)
3982 err = unlockerr;
3983 return err;
3986 static const struct got_error *
3987 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3989 const struct got_error *err = NULL;
3990 char *line = NULL;
3991 size_t linesize = 0, n;
3992 ssize_t linelen;
3994 linelen = getline(&line, &linesize, infile);
3995 if (linelen == -1) {
3996 if (ferror(infile)) {
3997 err = got_error_from_errno("getline");
3998 goto done;
4000 return NULL;
4002 if (outfile) {
4003 n = fwrite(line, 1, linelen, outfile);
4004 if (n != linelen) {
4005 err = got_ferror(outfile, GOT_ERR_IO);
4006 goto done;
4009 if (rejectfile) {
4010 n = fwrite(line, 1, linelen, rejectfile);
4011 if (n != linelen)
4012 err = got_ferror(outfile, GOT_ERR_IO);
4014 done:
4015 free(line);
4016 return err;
4019 static const struct got_error *
4020 skip_one_line(FILE *f)
4022 char *line = NULL;
4023 size_t linesize = 0;
4024 ssize_t linelen;
4026 linelen = getline(&line, &linesize, f);
4027 if (linelen == -1) {
4028 if (ferror(f))
4029 return got_error_from_errno("getline");
4030 return NULL;
4032 free(line);
4033 return NULL;
4036 static const struct got_error *
4037 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4038 int start_old, int end_old, int start_new, int end_new,
4039 FILE *outfile, FILE *rejectfile)
4041 const struct got_error *err;
4043 /* Copy old file's lines leading up to patch. */
4044 while (!feof(f1) && *line_cur1 < start_old) {
4045 err = copy_one_line(f1, outfile, NULL);
4046 if (err)
4047 return err;
4048 (*line_cur1)++;
4050 /* Skip new file's lines leading up to patch. */
4051 while (!feof(f2) && *line_cur2 < start_new) {
4052 if (rejectfile)
4053 err = copy_one_line(f2, NULL, rejectfile);
4054 else
4055 err = skip_one_line(f2);
4056 if (err)
4057 return err;
4058 (*line_cur2)++;
4060 /* Copy patched lines. */
4061 while (!feof(f2) && *line_cur2 <= end_new) {
4062 err = copy_one_line(f2, outfile, NULL);
4063 if (err)
4064 return err;
4065 (*line_cur2)++;
4067 /* Skip over old file's replaced lines. */
4068 while (!feof(f1) && *line_cur1 <= end_old) {
4069 if (rejectfile)
4070 err = copy_one_line(f1, NULL, rejectfile);
4071 else
4072 err = skip_one_line(f1);
4073 if (err)
4074 return err;
4075 (*line_cur1)++;
4078 return NULL;
4081 static const struct got_error *
4082 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4083 FILE *outfile, FILE *rejectfile)
4085 const struct got_error *err;
4087 if (outfile) {
4088 /* Copy old file's lines until EOF. */
4089 while (!feof(f1)) {
4090 err = copy_one_line(f1, outfile, NULL);
4091 if (err)
4092 return err;
4093 (*line_cur1)++;
4096 if (rejectfile) {
4097 /* Copy new file's lines until EOF. */
4098 while (!feof(f2)) {
4099 err = copy_one_line(f2, NULL, rejectfile);
4100 if (err)
4101 return err;
4102 (*line_cur2)++;
4106 return NULL;
4109 static const struct got_error *
4110 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4111 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4112 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4113 int *line_cur2, FILE *outfile, FILE *rejectfile,
4114 got_worktree_patch_cb patch_cb, void *patch_arg)
4116 const struct got_error *err = NULL;
4117 int start_old = change->cv.a;
4118 int end_old = change->cv.b;
4119 int start_new = change->cv.c;
4120 int end_new = change->cv.d;
4121 long pos1, pos2;
4122 FILE *hunkfile;
4124 *choice = GOT_PATCH_CHOICE_NONE;
4126 hunkfile = got_opentemp();
4127 if (hunkfile == NULL)
4128 return got_error_from_errno("got_opentemp");
4130 pos1 = ftell(f1);
4131 pos2 = ftell(f2);
4133 /* XXX TODO needs error checking */
4134 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4136 if (fseek(f1, pos1, SEEK_SET) == -1) {
4137 err = got_ferror(f1, GOT_ERR_IO);
4138 goto done;
4140 if (fseek(f2, pos2, SEEK_SET) == -1) {
4141 err = got_ferror(f1, GOT_ERR_IO);
4142 goto done;
4144 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4145 err = got_ferror(hunkfile, GOT_ERR_IO);
4146 goto done;
4149 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4150 hunkfile, n, nchanges);
4151 if (err)
4152 goto done;
4154 switch (*choice) {
4155 case GOT_PATCH_CHOICE_YES:
4156 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4157 end_old, start_new, end_new, outfile, rejectfile);
4158 break;
4159 case GOT_PATCH_CHOICE_NO:
4160 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4161 end_old, start_new, end_new, rejectfile, outfile);
4162 break;
4163 case GOT_PATCH_CHOICE_QUIT:
4164 break;
4165 default:
4166 err = got_error(GOT_ERR_PATCH_CHOICE);
4167 break;
4169 done:
4170 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4171 err = got_error_from_errno("fclose");
4172 return err;
4175 struct revert_file_args {
4176 struct got_worktree *worktree;
4177 struct got_fileindex *fileindex;
4178 got_worktree_checkout_cb progress_cb;
4179 void *progress_arg;
4180 got_worktree_patch_cb patch_cb;
4181 void *patch_arg;
4182 struct got_repository *repo;
4185 static const struct got_error *
4186 create_patched_content(char **path_outfile, int reverse_patch,
4187 struct got_object_id *blob_id, const char *path2,
4188 int dirfd2, const char *de_name2,
4189 const char *relpath, struct got_repository *repo,
4190 got_worktree_patch_cb patch_cb, void *patch_arg)
4192 const struct got_error *err;
4193 struct got_blob_object *blob = NULL;
4194 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4195 int fd2 = -1;
4196 char link_target[PATH_MAX];
4197 ssize_t link_len = 0;
4198 char *path1 = NULL, *id_str = NULL;
4199 struct stat sb1, sb2;
4200 struct got_diff_changes *changes = NULL;
4201 struct got_diff_state *ds = NULL;
4202 struct got_diff_args *args = NULL;
4203 struct got_diff_change *change;
4204 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4205 int n = 0;
4207 *path_outfile = NULL;
4209 err = got_object_id_str(&id_str, blob_id);
4210 if (err)
4211 return err;
4213 if (dirfd2 != -1) {
4214 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4215 if (fd2 == -1) {
4216 if (errno != ELOOP) {
4217 err = got_error_from_errno2("openat", path2);
4218 goto done;
4220 link_len = readlinkat(dirfd2, de_name2,
4221 link_target, sizeof(link_target));
4222 if (link_len == -1)
4223 return got_error_from_errno2("readlinkat", path2);
4224 sb2.st_mode = S_IFLNK;
4225 sb2.st_size = link_len;
4227 } else {
4228 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4229 if (fd2 == -1) {
4230 if (errno != ELOOP) {
4231 err = got_error_from_errno2("open", path2);
4232 goto done;
4234 link_len = readlink(path2, link_target,
4235 sizeof(link_target));
4236 if (link_len == -1)
4237 return got_error_from_errno2("readlink", path2);
4238 sb2.st_mode = S_IFLNK;
4239 sb2.st_size = link_len;
4242 if (fd2 != -1) {
4243 if (fstat(fd2, &sb2) == -1) {
4244 err = got_error_from_errno2("fstat", path2);
4245 goto done;
4248 f2 = fdopen(fd2, "r");
4249 if (f2 == NULL) {
4250 err = got_error_from_errno2("fdopen", path2);
4251 goto done;
4253 fd2 = -1;
4254 } else {
4255 size_t n;
4256 f2 = got_opentemp();
4257 if (f2 == NULL) {
4258 err = got_error_from_errno2("got_opentemp", path2);
4259 goto done;
4261 n = fwrite(link_target, 1, link_len, f2);
4262 if (n != link_len) {
4263 err = got_ferror(f2, GOT_ERR_IO);
4264 goto done;
4266 if (fflush(f2) == EOF) {
4267 err = got_error_from_errno("fflush");
4268 goto done;
4270 rewind(f2);
4273 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4274 if (err)
4275 goto done;
4277 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4278 if (err)
4279 goto done;
4281 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4282 if (err)
4283 goto done;
4285 if (stat(path1, &sb1) == -1) {
4286 err = got_error_from_errno2("stat", path1);
4287 goto done;
4290 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4291 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4292 if (err)
4293 goto done;
4295 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4296 if (err)
4297 goto done;
4299 if (fseek(f1, 0L, SEEK_SET) == -1)
4300 return got_ferror(f1, GOT_ERR_IO);
4301 if (fseek(f2, 0L, SEEK_SET) == -1)
4302 return got_ferror(f2, GOT_ERR_IO);
4303 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4304 int choice;
4305 err = apply_or_reject_change(&choice, change, ++n,
4306 changes->nchanges, ds, args, diff_flags, relpath,
4307 f1, f2, &line_cur1, &line_cur2,
4308 reverse_patch ? NULL : outfile,
4309 reverse_patch ? outfile : NULL,
4310 patch_cb, patch_arg);
4311 if (err)
4312 goto done;
4313 if (choice == GOT_PATCH_CHOICE_YES)
4314 have_content = 1;
4315 else if (choice == GOT_PATCH_CHOICE_QUIT)
4316 break;
4318 if (have_content) {
4319 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4320 reverse_patch ? NULL : outfile,
4321 reverse_patch ? outfile : NULL);
4322 if (err)
4323 goto done;
4325 if (!S_ISLNK(sb2.st_mode)) {
4326 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4327 err = got_error_from_errno2("chmod", path2);
4328 goto done;
4332 done:
4333 free(id_str);
4334 if (blob)
4335 got_object_blob_close(blob);
4336 if (f1 && fclose(f1) == EOF && err == NULL)
4337 err = got_error_from_errno2("fclose", path1);
4338 if (f2 && fclose(f2) == EOF && err == NULL)
4339 err = got_error_from_errno2("fclose", path2);
4340 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4341 err = got_error_from_errno2("close", path2);
4342 if (outfile && fclose(outfile) == EOF && err == NULL)
4343 err = got_error_from_errno2("fclose", *path_outfile);
4344 if (path1 && unlink(path1) == -1 && err == NULL)
4345 err = got_error_from_errno2("unlink", path1);
4346 if (err || !have_content) {
4347 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4348 err = got_error_from_errno2("unlink", *path_outfile);
4349 free(*path_outfile);
4350 *path_outfile = NULL;
4352 free(args);
4353 if (ds) {
4354 got_diff_state_free(ds);
4355 free(ds);
4357 if (changes)
4358 got_diff_free_changes(changes);
4359 free(path1);
4360 return err;
4363 static const struct got_error *
4364 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4365 const char *relpath, struct got_object_id *blob_id,
4366 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4367 int dirfd, const char *de_name)
4369 struct revert_file_args *a = arg;
4370 const struct got_error *err = NULL;
4371 char *parent_path = NULL;
4372 struct got_fileindex_entry *ie;
4373 struct got_tree_object *tree = NULL;
4374 struct got_object_id *tree_id = NULL;
4375 const struct got_tree_entry *te = NULL;
4376 char *tree_path = NULL, *te_name;
4377 char *ondisk_path = NULL, *path_content = NULL;
4378 struct got_blob_object *blob = NULL;
4380 /* Reverting a staged deletion is a no-op. */
4381 if (status == GOT_STATUS_DELETE &&
4382 staged_status != GOT_STATUS_NO_CHANGE)
4383 return NULL;
4385 if (status == GOT_STATUS_UNVERSIONED)
4386 return (*a->progress_cb)(a->progress_arg,
4387 GOT_STATUS_UNVERSIONED, relpath);
4389 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4390 if (ie == NULL)
4391 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4393 /* Construct in-repository path of tree which contains this blob. */
4394 err = got_path_dirname(&parent_path, ie->path);
4395 if (err) {
4396 if (err->code != GOT_ERR_BAD_PATH)
4397 goto done;
4398 parent_path = strdup("/");
4399 if (parent_path == NULL) {
4400 err = got_error_from_errno("strdup");
4401 goto done;
4404 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4405 tree_path = strdup(parent_path);
4406 if (tree_path == NULL) {
4407 err = got_error_from_errno("strdup");
4408 goto done;
4410 } else {
4411 if (got_path_is_root_dir(parent_path)) {
4412 tree_path = strdup(a->worktree->path_prefix);
4413 if (tree_path == NULL) {
4414 err = got_error_from_errno("strdup");
4415 goto done;
4417 } else {
4418 if (asprintf(&tree_path, "%s/%s",
4419 a->worktree->path_prefix, parent_path) == -1) {
4420 err = got_error_from_errno("asprintf");
4421 goto done;
4426 err = got_object_id_by_path(&tree_id, a->repo,
4427 a->worktree->base_commit_id, tree_path);
4428 if (err) {
4429 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4430 (status == GOT_STATUS_ADD ||
4431 staged_status == GOT_STATUS_ADD)))
4432 goto done;
4433 } else {
4434 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4435 if (err)
4436 goto done;
4438 err = got_path_basename(&te_name, ie->path);
4439 if (err)
4440 goto done;
4442 te = got_object_tree_find_entry(tree, te_name);
4443 free(te_name);
4444 if (te == NULL && status != GOT_STATUS_ADD &&
4445 staged_status != GOT_STATUS_ADD) {
4446 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4447 goto done;
4451 switch (status) {
4452 case GOT_STATUS_ADD:
4453 if (a->patch_cb) {
4454 int choice = GOT_PATCH_CHOICE_NONE;
4455 err = (*a->patch_cb)(&choice, a->patch_arg,
4456 status, ie->path, NULL, 1, 1);
4457 if (err)
4458 goto done;
4459 if (choice != GOT_PATCH_CHOICE_YES)
4460 break;
4462 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4463 ie->path);
4464 if (err)
4465 goto done;
4466 got_fileindex_entry_remove(a->fileindex, ie);
4467 break;
4468 case GOT_STATUS_DELETE:
4469 if (a->patch_cb) {
4470 int choice = GOT_PATCH_CHOICE_NONE;
4471 err = (*a->patch_cb)(&choice, a->patch_arg,
4472 status, ie->path, NULL, 1, 1);
4473 if (err)
4474 goto done;
4475 if (choice != GOT_PATCH_CHOICE_YES)
4476 break;
4478 /* fall through */
4479 case GOT_STATUS_MODIFY:
4480 case GOT_STATUS_MODE_CHANGE:
4481 case GOT_STATUS_CONFLICT:
4482 case GOT_STATUS_MISSING: {
4483 struct got_object_id id;
4484 if (staged_status == GOT_STATUS_ADD ||
4485 staged_status == GOT_STATUS_MODIFY) {
4486 memcpy(id.sha1, ie->staged_blob_sha1,
4487 SHA1_DIGEST_LENGTH);
4488 } else
4489 memcpy(id.sha1, ie->blob_sha1,
4490 SHA1_DIGEST_LENGTH);
4491 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4492 if (err)
4493 goto done;
4495 if (asprintf(&ondisk_path, "%s/%s",
4496 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4497 err = got_error_from_errno("asprintf");
4498 goto done;
4501 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4502 status == GOT_STATUS_CONFLICT)) {
4503 int is_bad_symlink = 0;
4504 err = create_patched_content(&path_content, 1, &id,
4505 ondisk_path, dirfd, de_name, ie->path, a->repo,
4506 a->patch_cb, a->patch_arg);
4507 if (err || path_content == NULL)
4508 break;
4509 if (te && S_ISLNK(te->mode)) {
4510 if (unlink(path_content) == -1) {
4511 err = got_error_from_errno2("unlink",
4512 path_content);
4513 break;
4515 err = install_symlink(&is_bad_symlink,
4516 a->worktree, ondisk_path, ie->path,
4517 blob, 0, 1, 0, a->repo,
4518 a->progress_cb, a->progress_arg);
4519 } else {
4520 if (rename(path_content, ondisk_path) == -1) {
4521 err = got_error_from_errno3("rename",
4522 path_content, ondisk_path);
4523 goto done;
4526 } else {
4527 int is_bad_symlink = 0;
4528 if (te && S_ISLNK(te->mode)) {
4529 err = install_symlink(&is_bad_symlink,
4530 a->worktree, ondisk_path, ie->path,
4531 blob, 0, 1, 0, a->repo,
4532 a->progress_cb, a->progress_arg);
4533 } else {
4534 err = install_blob(a->worktree, ondisk_path,
4535 ie->path,
4536 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4537 got_fileindex_perms_to_st(ie), blob,
4538 0, 1, 0, 0, a->repo,
4539 a->progress_cb, a->progress_arg);
4541 if (err)
4542 goto done;
4543 if (status == GOT_STATUS_DELETE ||
4544 status == GOT_STATUS_MODE_CHANGE) {
4545 err = got_fileindex_entry_update(ie,
4546 ondisk_path, blob->id.sha1,
4547 a->worktree->base_commit_id->sha1, 1);
4548 if (err)
4549 goto done;
4551 if (is_bad_symlink) {
4552 got_fileindex_entry_filetype_set(ie,
4553 GOT_FILEIDX_MODE_BAD_SYMLINK);
4556 break;
4558 default:
4559 break;
4561 done:
4562 free(ondisk_path);
4563 free(path_content);
4564 free(parent_path);
4565 free(tree_path);
4566 if (blob)
4567 got_object_blob_close(blob);
4568 if (tree)
4569 got_object_tree_close(tree);
4570 free(tree_id);
4571 return err;
4574 const struct got_error *
4575 got_worktree_revert(struct got_worktree *worktree,
4576 struct got_pathlist_head *paths,
4577 got_worktree_checkout_cb progress_cb, void *progress_arg,
4578 got_worktree_patch_cb patch_cb, void *patch_arg,
4579 struct got_repository *repo)
4581 struct got_fileindex *fileindex = NULL;
4582 char *fileindex_path = NULL;
4583 const struct got_error *err = NULL, *unlockerr = NULL;
4584 const struct got_error *sync_err = NULL;
4585 struct got_pathlist_entry *pe;
4586 struct revert_file_args rfa;
4588 err = lock_worktree(worktree, LOCK_EX);
4589 if (err)
4590 return err;
4592 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4593 if (err)
4594 goto done;
4596 rfa.worktree = worktree;
4597 rfa.fileindex = fileindex;
4598 rfa.progress_cb = progress_cb;
4599 rfa.progress_arg = progress_arg;
4600 rfa.patch_cb = patch_cb;
4601 rfa.patch_arg = patch_arg;
4602 rfa.repo = repo;
4603 TAILQ_FOREACH(pe, paths, entry) {
4604 err = worktree_status(worktree, pe->path, fileindex, repo,
4605 revert_file, &rfa, NULL, NULL, 0, 0);
4606 if (err)
4607 break;
4609 sync_err = sync_fileindex(fileindex, fileindex_path);
4610 if (sync_err && err == NULL)
4611 err = sync_err;
4612 done:
4613 free(fileindex_path);
4614 if (fileindex)
4615 got_fileindex_free(fileindex);
4616 unlockerr = lock_worktree(worktree, LOCK_SH);
4617 if (unlockerr && err == NULL)
4618 err = unlockerr;
4619 return err;
4622 static void
4623 free_commitable(struct got_commitable *ct)
4625 free(ct->path);
4626 free(ct->in_repo_path);
4627 free(ct->ondisk_path);
4628 free(ct->blob_id);
4629 free(ct->base_blob_id);
4630 free(ct->staged_blob_id);
4631 free(ct->base_commit_id);
4632 free(ct);
4635 struct collect_commitables_arg {
4636 struct got_pathlist_head *commitable_paths;
4637 struct got_repository *repo;
4638 struct got_worktree *worktree;
4639 struct got_fileindex *fileindex;
4640 int have_staged_files;
4641 int allow_bad_symlinks;
4644 static const struct got_error *
4645 collect_commitables(void *arg, unsigned char status,
4646 unsigned char staged_status, const char *relpath,
4647 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4648 struct got_object_id *commit_id, int dirfd, const char *de_name)
4650 struct collect_commitables_arg *a = arg;
4651 const struct got_error *err = NULL;
4652 struct got_commitable *ct = NULL;
4653 struct got_pathlist_entry *new = NULL;
4654 char *parent_path = NULL, *path = NULL;
4655 struct stat sb;
4657 if (a->have_staged_files) {
4658 if (staged_status != GOT_STATUS_MODIFY &&
4659 staged_status != GOT_STATUS_ADD &&
4660 staged_status != GOT_STATUS_DELETE)
4661 return NULL;
4662 } else {
4663 if (status == GOT_STATUS_CONFLICT)
4664 return got_error(GOT_ERR_COMMIT_CONFLICT);
4666 if (status != GOT_STATUS_MODIFY &&
4667 status != GOT_STATUS_MODE_CHANGE &&
4668 status != GOT_STATUS_ADD &&
4669 status != GOT_STATUS_DELETE)
4670 return NULL;
4673 if (asprintf(&path, "/%s", relpath) == -1) {
4674 err = got_error_from_errno("asprintf");
4675 goto done;
4677 if (strcmp(path, "/") == 0) {
4678 parent_path = strdup("");
4679 if (parent_path == NULL)
4680 return got_error_from_errno("strdup");
4681 } else {
4682 err = got_path_dirname(&parent_path, path);
4683 if (err)
4684 return err;
4687 ct = calloc(1, sizeof(*ct));
4688 if (ct == NULL) {
4689 err = got_error_from_errno("calloc");
4690 goto done;
4693 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4694 relpath) == -1) {
4695 err = got_error_from_errno("asprintf");
4696 goto done;
4699 if (staged_status == GOT_STATUS_ADD ||
4700 staged_status == GOT_STATUS_MODIFY) {
4701 struct got_fileindex_entry *ie;
4702 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4703 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4704 case GOT_FILEIDX_MODE_REGULAR_FILE:
4705 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4706 ct->mode = S_IFREG;
4707 break;
4708 case GOT_FILEIDX_MODE_SYMLINK:
4709 ct->mode = S_IFLNK;
4710 break;
4711 default:
4712 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4713 goto done;
4715 ct->mode |= got_fileindex_entry_perms_get(ie);
4716 } else if (status != GOT_STATUS_DELETE &&
4717 staged_status != GOT_STATUS_DELETE) {
4718 if (dirfd != -1) {
4719 if (fstatat(dirfd, de_name, &sb,
4720 AT_SYMLINK_NOFOLLOW) == -1) {
4721 err = got_error_from_errno2("fstatat",
4722 ct->ondisk_path);
4723 goto done;
4725 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4726 err = got_error_from_errno2("lstat", ct->ondisk_path);
4727 goto done;
4729 ct->mode = sb.st_mode;
4732 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4733 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4734 relpath) == -1) {
4735 err = got_error_from_errno("asprintf");
4736 goto done;
4739 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4740 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4741 int is_bad_symlink;
4742 char target_path[PATH_MAX];
4743 ssize_t target_len;
4744 target_len = readlink(ct->ondisk_path, target_path,
4745 sizeof(target_path));
4746 if (target_len == -1) {
4747 err = got_error_from_errno2("readlink",
4748 ct->ondisk_path);
4749 goto done;
4751 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4752 target_len, ct->ondisk_path, a->worktree->root_path);
4753 if (err)
4754 goto done;
4755 if (is_bad_symlink) {
4756 err = got_error_path(ct->ondisk_path,
4757 GOT_ERR_BAD_SYMLINK);
4758 goto done;
4763 ct->status = status;
4764 ct->staged_status = staged_status;
4765 ct->blob_id = NULL; /* will be filled in when blob gets created */
4766 if (ct->status != GOT_STATUS_ADD &&
4767 ct->staged_status != GOT_STATUS_ADD) {
4768 ct->base_blob_id = got_object_id_dup(blob_id);
4769 if (ct->base_blob_id == NULL) {
4770 err = got_error_from_errno("got_object_id_dup");
4771 goto done;
4773 ct->base_commit_id = got_object_id_dup(commit_id);
4774 if (ct->base_commit_id == NULL) {
4775 err = got_error_from_errno("got_object_id_dup");
4776 goto done;
4779 if (ct->staged_status == GOT_STATUS_ADD ||
4780 ct->staged_status == GOT_STATUS_MODIFY) {
4781 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4782 if (ct->staged_blob_id == NULL) {
4783 err = got_error_from_errno("got_object_id_dup");
4784 goto done;
4787 ct->path = strdup(path);
4788 if (ct->path == NULL) {
4789 err = got_error_from_errno("strdup");
4790 goto done;
4792 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4793 done:
4794 if (ct && (err || new == NULL))
4795 free_commitable(ct);
4796 free(parent_path);
4797 free(path);
4798 return err;
4801 static const struct got_error *write_tree(struct got_object_id **, int *,
4802 struct got_tree_object *, const char *, struct got_pathlist_head *,
4803 got_worktree_status_cb status_cb, void *status_arg,
4804 struct got_repository *);
4806 static const struct got_error *
4807 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4808 struct got_tree_entry *te, const char *parent_path,
4809 struct got_pathlist_head *commitable_paths,
4810 got_worktree_status_cb status_cb, void *status_arg,
4811 struct got_repository *repo)
4813 const struct got_error *err = NULL;
4814 struct got_tree_object *subtree;
4815 char *subpath;
4817 if (asprintf(&subpath, "%s%s%s", parent_path,
4818 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4819 return got_error_from_errno("asprintf");
4821 err = got_object_open_as_tree(&subtree, repo, &te->id);
4822 if (err)
4823 return err;
4825 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4826 commitable_paths, status_cb, status_arg, repo);
4827 got_object_tree_close(subtree);
4828 free(subpath);
4829 return err;
4832 static const struct got_error *
4833 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4835 const struct got_error *err = NULL;
4836 char *ct_parent_path = NULL;
4838 *match = 0;
4840 if (strchr(ct->in_repo_path, '/') == NULL) {
4841 *match = got_path_is_root_dir(path);
4842 return NULL;
4845 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4846 if (err)
4847 return err;
4848 *match = (strcmp(path, ct_parent_path) == 0);
4849 free(ct_parent_path);
4850 return err;
4853 static mode_t
4854 get_ct_file_mode(struct got_commitable *ct)
4856 if (S_ISLNK(ct->mode))
4857 return S_IFLNK;
4859 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4862 static const struct got_error *
4863 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4864 struct got_tree_entry *te, struct got_commitable *ct)
4866 const struct got_error *err = NULL;
4868 *new_te = NULL;
4870 err = got_object_tree_entry_dup(new_te, te);
4871 if (err)
4872 goto done;
4874 (*new_te)->mode = get_ct_file_mode(ct);
4876 if (ct->staged_status == GOT_STATUS_MODIFY)
4877 memcpy(&(*new_te)->id, ct->staged_blob_id,
4878 sizeof((*new_te)->id));
4879 else
4880 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4881 done:
4882 if (err && *new_te) {
4883 free(*new_te);
4884 *new_te = NULL;
4886 return err;
4889 static const struct got_error *
4890 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4891 struct got_commitable *ct)
4893 const struct got_error *err = NULL;
4894 char *ct_name = NULL;
4896 *new_te = NULL;
4898 *new_te = calloc(1, sizeof(**new_te));
4899 if (*new_te == NULL)
4900 return got_error_from_errno("calloc");
4902 err = got_path_basename(&ct_name, ct->path);
4903 if (err)
4904 goto done;
4905 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4906 sizeof((*new_te)->name)) {
4907 err = got_error(GOT_ERR_NO_SPACE);
4908 goto done;
4911 (*new_te)->mode = get_ct_file_mode(ct);
4913 if (ct->staged_status == GOT_STATUS_ADD)
4914 memcpy(&(*new_te)->id, ct->staged_blob_id,
4915 sizeof((*new_te)->id));
4916 else
4917 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4918 done:
4919 free(ct_name);
4920 if (err && *new_te) {
4921 free(*new_te);
4922 *new_te = NULL;
4924 return err;
4927 static const struct got_error *
4928 insert_tree_entry(struct got_tree_entry *new_te,
4929 struct got_pathlist_head *paths)
4931 const struct got_error *err = NULL;
4932 struct got_pathlist_entry *new_pe;
4934 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4935 if (err)
4936 return err;
4937 if (new_pe == NULL)
4938 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4939 return NULL;
4942 static const struct got_error *
4943 report_ct_status(struct got_commitable *ct,
4944 got_worktree_status_cb status_cb, void *status_arg)
4946 const char *ct_path = ct->path;
4947 unsigned char status;
4949 while (ct_path[0] == '/')
4950 ct_path++;
4952 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4953 status = ct->staged_status;
4954 else
4955 status = ct->status;
4957 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4958 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4961 static const struct got_error *
4962 match_modified_subtree(int *modified, struct got_tree_entry *te,
4963 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4965 const struct got_error *err = NULL;
4966 struct got_pathlist_entry *pe;
4967 char *te_path;
4969 *modified = 0;
4971 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4972 got_path_is_root_dir(base_tree_path) ? "" : "/",
4973 te->name) == -1)
4974 return got_error_from_errno("asprintf");
4976 TAILQ_FOREACH(pe, commitable_paths, entry) {
4977 struct got_commitable *ct = pe->data;
4978 *modified = got_path_is_child(ct->in_repo_path, te_path,
4979 strlen(te_path));
4980 if (*modified)
4981 break;
4984 free(te_path);
4985 return err;
4988 static const struct got_error *
4989 match_deleted_or_modified_ct(struct got_commitable **ctp,
4990 struct got_tree_entry *te, const char *base_tree_path,
4991 struct got_pathlist_head *commitable_paths)
4993 const struct got_error *err = NULL;
4994 struct got_pathlist_entry *pe;
4996 *ctp = NULL;
4998 TAILQ_FOREACH(pe, commitable_paths, entry) {
4999 struct got_commitable *ct = pe->data;
5000 char *ct_name = NULL;
5001 int path_matches;
5003 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5004 if (ct->status != GOT_STATUS_MODIFY &&
5005 ct->status != GOT_STATUS_MODE_CHANGE &&
5006 ct->status != GOT_STATUS_DELETE)
5007 continue;
5008 } else {
5009 if (ct->staged_status != GOT_STATUS_MODIFY &&
5010 ct->staged_status != GOT_STATUS_DELETE)
5011 continue;
5014 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5015 continue;
5017 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5018 if (err)
5019 return err;
5020 if (!path_matches)
5021 continue;
5023 err = got_path_basename(&ct_name, pe->path);
5024 if (err)
5025 return err;
5027 if (strcmp(te->name, ct_name) != 0) {
5028 free(ct_name);
5029 continue;
5031 free(ct_name);
5033 *ctp = ct;
5034 break;
5037 return err;
5040 static const struct got_error *
5041 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5042 const char *child_path, const char *path_base_tree,
5043 struct got_pathlist_head *commitable_paths,
5044 got_worktree_status_cb status_cb, void *status_arg,
5045 struct got_repository *repo)
5047 const struct got_error *err = NULL;
5048 struct got_tree_entry *new_te;
5049 char *subtree_path;
5050 struct got_object_id *id = NULL;
5051 int nentries;
5053 *new_tep = NULL;
5055 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5056 got_path_is_root_dir(path_base_tree) ? "" : "/",
5057 child_path) == -1)
5058 return got_error_from_errno("asprintf");
5060 new_te = calloc(1, sizeof(*new_te));
5061 if (new_te == NULL)
5062 return got_error_from_errno("calloc");
5063 new_te->mode = S_IFDIR;
5065 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5066 sizeof(new_te->name)) {
5067 err = got_error(GOT_ERR_NO_SPACE);
5068 goto done;
5070 err = write_tree(&id, &nentries, NULL, subtree_path,
5071 commitable_paths, status_cb, status_arg, repo);
5072 if (err) {
5073 free(new_te);
5074 goto done;
5076 memcpy(&new_te->id, id, sizeof(new_te->id));
5077 done:
5078 free(id);
5079 free(subtree_path);
5080 if (err == NULL)
5081 *new_tep = new_te;
5082 return err;
5085 static const struct got_error *
5086 write_tree(struct got_object_id **new_tree_id, int *nentries,
5087 struct got_tree_object *base_tree, const char *path_base_tree,
5088 struct got_pathlist_head *commitable_paths,
5089 got_worktree_status_cb status_cb, void *status_arg,
5090 struct got_repository *repo)
5092 const struct got_error *err = NULL;
5093 struct got_pathlist_head paths;
5094 struct got_tree_entry *te, *new_te = NULL;
5095 struct got_pathlist_entry *pe;
5097 TAILQ_INIT(&paths);
5098 *nentries = 0;
5100 /* Insert, and recurse into, newly added entries first. */
5101 TAILQ_FOREACH(pe, commitable_paths, entry) {
5102 struct got_commitable *ct = pe->data;
5103 char *child_path = NULL, *slash;
5105 if ((ct->status != GOT_STATUS_ADD &&
5106 ct->staged_status != GOT_STATUS_ADD) ||
5107 (ct->flags & GOT_COMMITABLE_ADDED))
5108 continue;
5110 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5111 strlen(path_base_tree)))
5112 continue;
5114 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5115 ct->in_repo_path);
5116 if (err)
5117 goto done;
5119 slash = strchr(child_path, '/');
5120 if (slash == NULL) {
5121 err = alloc_added_blob_tree_entry(&new_te, ct);
5122 if (err)
5123 goto done;
5124 err = report_ct_status(ct, status_cb, status_arg);
5125 if (err)
5126 goto done;
5127 ct->flags |= GOT_COMMITABLE_ADDED;
5128 err = insert_tree_entry(new_te, &paths);
5129 if (err)
5130 goto done;
5131 (*nentries)++;
5132 } else {
5133 *slash = '\0'; /* trim trailing path components */
5134 if (base_tree == NULL ||
5135 got_object_tree_find_entry(base_tree, child_path)
5136 == NULL) {
5137 err = make_subtree_for_added_blob(&new_te,
5138 child_path, path_base_tree,
5139 commitable_paths, status_cb, status_arg,
5140 repo);
5141 if (err)
5142 goto done;
5143 err = insert_tree_entry(new_te, &paths);
5144 if (err)
5145 goto done;
5146 (*nentries)++;
5151 if (base_tree) {
5152 int i, nbase_entries;
5153 /* Handle modified and deleted entries. */
5154 nbase_entries = got_object_tree_get_nentries(base_tree);
5155 for (i = 0; i < nbase_entries; i++) {
5156 struct got_commitable *ct = NULL;
5158 te = got_object_tree_get_entry(base_tree, i);
5159 if (got_object_tree_entry_is_submodule(te)) {
5160 /* Entry is a submodule; just copy it. */
5161 err = got_object_tree_entry_dup(&new_te, te);
5162 if (err)
5163 goto done;
5164 err = insert_tree_entry(new_te, &paths);
5165 if (err)
5166 goto done;
5167 (*nentries)++;
5168 continue;
5171 if (S_ISDIR(te->mode)) {
5172 int modified;
5173 err = got_object_tree_entry_dup(&new_te, te);
5174 if (err)
5175 goto done;
5176 err = match_modified_subtree(&modified, te,
5177 path_base_tree, commitable_paths);
5178 if (err)
5179 goto done;
5180 /* Avoid recursion into unmodified subtrees. */
5181 if (modified) {
5182 struct got_object_id *new_id;
5183 int nsubentries;
5184 err = write_subtree(&new_id,
5185 &nsubentries, te,
5186 path_base_tree, commitable_paths,
5187 status_cb, status_arg, repo);
5188 if (err)
5189 goto done;
5190 if (nsubentries == 0) {
5191 /* All entries were deleted. */
5192 free(new_id);
5193 continue;
5195 memcpy(&new_te->id, new_id,
5196 sizeof(new_te->id));
5197 free(new_id);
5199 err = insert_tree_entry(new_te, &paths);
5200 if (err)
5201 goto done;
5202 (*nentries)++;
5203 continue;
5206 err = match_deleted_or_modified_ct(&ct, te,
5207 path_base_tree, commitable_paths);
5208 if (err)
5209 goto done;
5210 if (ct) {
5211 /* NB: Deleted entries get dropped here. */
5212 if (ct->status == GOT_STATUS_MODIFY ||
5213 ct->status == GOT_STATUS_MODE_CHANGE ||
5214 ct->staged_status == GOT_STATUS_MODIFY) {
5215 err = alloc_modified_blob_tree_entry(
5216 &new_te, te, ct);
5217 if (err)
5218 goto done;
5219 err = insert_tree_entry(new_te, &paths);
5220 if (err)
5221 goto done;
5222 (*nentries)++;
5224 err = report_ct_status(ct, status_cb,
5225 status_arg);
5226 if (err)
5227 goto done;
5228 } else {
5229 /* Entry is unchanged; just copy it. */
5230 err = got_object_tree_entry_dup(&new_te, te);
5231 if (err)
5232 goto done;
5233 err = insert_tree_entry(new_te, &paths);
5234 if (err)
5235 goto done;
5236 (*nentries)++;
5241 /* Write new list of entries; deleted entries have been dropped. */
5242 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5243 done:
5244 got_pathlist_free(&paths);
5245 return err;
5248 static const struct got_error *
5249 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5250 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5251 int have_staged_files)
5253 const struct got_error *err = NULL;
5254 struct got_pathlist_entry *pe;
5256 TAILQ_FOREACH(pe, commitable_paths, entry) {
5257 struct got_fileindex_entry *ie;
5258 struct got_commitable *ct = pe->data;
5260 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5261 if (ie) {
5262 if (ct->status == GOT_STATUS_DELETE ||
5263 ct->staged_status == GOT_STATUS_DELETE) {
5264 got_fileindex_entry_remove(fileindex, ie);
5265 } else if (ct->staged_status == GOT_STATUS_ADD ||
5266 ct->staged_status == GOT_STATUS_MODIFY) {
5267 got_fileindex_entry_stage_set(ie,
5268 GOT_FILEIDX_STAGE_NONE);
5269 err = got_fileindex_entry_update(ie,
5270 ct->ondisk_path, ct->staged_blob_id->sha1,
5271 new_base_commit_id->sha1,
5272 !have_staged_files);
5273 } else
5274 err = got_fileindex_entry_update(ie,
5275 ct->ondisk_path, ct->blob_id->sha1,
5276 new_base_commit_id->sha1,
5277 !have_staged_files);
5278 } else {
5279 err = got_fileindex_entry_alloc(&ie, pe->path);
5280 if (err)
5281 break;
5282 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5283 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5284 if (err) {
5285 got_fileindex_entry_free(ie);
5286 break;
5288 err = got_fileindex_entry_add(fileindex, ie);
5289 if (err) {
5290 got_fileindex_entry_free(ie);
5291 break;
5295 return err;
5299 static const struct got_error *
5300 check_out_of_date(const char *in_repo_path, unsigned char status,
5301 unsigned char staged_status, struct got_object_id *base_blob_id,
5302 struct got_object_id *base_commit_id,
5303 struct got_object_id *head_commit_id, struct got_repository *repo,
5304 int ood_errcode)
5306 const struct got_error *err = NULL;
5307 struct got_object_id *id = NULL;
5309 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5310 /* Trivial case: base commit == head commit */
5311 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5312 return NULL;
5314 * Ensure file content which local changes were based
5315 * on matches file content in the branch head.
5317 err = got_object_id_by_path(&id, repo, head_commit_id,
5318 in_repo_path);
5319 if (err) {
5320 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5321 err = got_error(ood_errcode);
5322 goto done;
5323 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5324 err = got_error(ood_errcode);
5325 } else {
5326 /* Require that added files don't exist in the branch head. */
5327 err = got_object_id_by_path(&id, repo, head_commit_id,
5328 in_repo_path);
5329 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5330 goto done;
5331 err = id ? got_error(ood_errcode) : NULL;
5333 done:
5334 free(id);
5335 return err;
5338 const struct got_error *
5339 commit_worktree(struct got_object_id **new_commit_id,
5340 struct got_pathlist_head *commitable_paths,
5341 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5342 const char *author, const char *committer,
5343 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5344 got_worktree_status_cb status_cb, void *status_arg,
5345 struct got_repository *repo)
5347 const struct got_error *err = NULL, *unlockerr = NULL;
5348 struct got_pathlist_entry *pe;
5349 const char *head_ref_name = NULL;
5350 struct got_commit_object *head_commit = NULL;
5351 struct got_reference *head_ref2 = NULL;
5352 struct got_object_id *head_commit_id2 = NULL;
5353 struct got_tree_object *head_tree = NULL;
5354 struct got_object_id *new_tree_id = NULL;
5355 int nentries;
5356 struct got_object_id_queue parent_ids;
5357 struct got_object_qid *pid = NULL;
5358 char *logmsg = NULL;
5360 *new_commit_id = NULL;
5362 SIMPLEQ_INIT(&parent_ids);
5364 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5365 if (err)
5366 goto done;
5368 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5369 if (err)
5370 goto done;
5372 if (commit_msg_cb != NULL) {
5373 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5374 if (err)
5375 goto done;
5378 if (logmsg == NULL || strlen(logmsg) == 0) {
5379 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5380 goto done;
5383 /* Create blobs from added and modified files and record their IDs. */
5384 TAILQ_FOREACH(pe, commitable_paths, entry) {
5385 struct got_commitable *ct = pe->data;
5386 char *ondisk_path;
5388 /* Blobs for staged files already exist. */
5389 if (ct->staged_status == GOT_STATUS_ADD ||
5390 ct->staged_status == GOT_STATUS_MODIFY)
5391 continue;
5393 if (ct->status != GOT_STATUS_ADD &&
5394 ct->status != GOT_STATUS_MODIFY &&
5395 ct->status != GOT_STATUS_MODE_CHANGE)
5396 continue;
5398 if (asprintf(&ondisk_path, "%s/%s",
5399 worktree->root_path, pe->path) == -1) {
5400 err = got_error_from_errno("asprintf");
5401 goto done;
5403 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5404 free(ondisk_path);
5405 if (err)
5406 goto done;
5409 /* Recursively write new tree objects. */
5410 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5411 commitable_paths, status_cb, status_arg, repo);
5412 if (err)
5413 goto done;
5415 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5416 if (err)
5417 goto done;
5418 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5419 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5420 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5421 got_object_qid_free(pid);
5422 if (logmsg != NULL)
5423 free(logmsg);
5424 if (err)
5425 goto done;
5427 /* Check if a concurrent commit to our branch has occurred. */
5428 head_ref_name = got_worktree_get_head_ref_name(worktree);
5429 if (head_ref_name == NULL) {
5430 err = got_error_from_errno("got_worktree_get_head_ref_name");
5431 goto done;
5433 /* Lock the reference here to prevent concurrent modification. */
5434 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5435 if (err)
5436 goto done;
5437 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5438 if (err)
5439 goto done;
5440 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5441 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5442 goto done;
5444 /* Update branch head in repository. */
5445 err = got_ref_change_ref(head_ref2, *new_commit_id);
5446 if (err)
5447 goto done;
5448 err = got_ref_write(head_ref2, repo);
5449 if (err)
5450 goto done;
5452 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5453 if (err)
5454 goto done;
5456 err = ref_base_commit(worktree, repo);
5457 if (err)
5458 goto done;
5459 done:
5460 if (head_tree)
5461 got_object_tree_close(head_tree);
5462 if (head_commit)
5463 got_object_commit_close(head_commit);
5464 free(head_commit_id2);
5465 if (head_ref2) {
5466 unlockerr = got_ref_unlock(head_ref2);
5467 if (unlockerr && err == NULL)
5468 err = unlockerr;
5469 got_ref_close(head_ref2);
5471 return err;
5474 static const struct got_error *
5475 check_path_is_commitable(const char *path,
5476 struct got_pathlist_head *commitable_paths)
5478 struct got_pathlist_entry *cpe = NULL;
5479 size_t path_len = strlen(path);
5481 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5482 struct got_commitable *ct = cpe->data;
5483 const char *ct_path = ct->path;
5485 while (ct_path[0] == '/')
5486 ct_path++;
5488 if (strcmp(path, ct_path) == 0 ||
5489 got_path_is_child(ct_path, path, path_len))
5490 break;
5493 if (cpe == NULL)
5494 return got_error_path(path, GOT_ERR_BAD_PATH);
5496 return NULL;
5499 static const struct got_error *
5500 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5502 int *have_staged_files = arg;
5504 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5505 *have_staged_files = 1;
5506 return got_error(GOT_ERR_CANCELLED);
5509 return NULL;
5512 static const struct got_error *
5513 check_non_staged_files(struct got_fileindex *fileindex,
5514 struct got_pathlist_head *paths)
5516 struct got_pathlist_entry *pe;
5517 struct got_fileindex_entry *ie;
5519 TAILQ_FOREACH(pe, paths, entry) {
5520 if (pe->path[0] == '\0')
5521 continue;
5522 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5523 if (ie == NULL)
5524 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5525 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5526 return got_error_path(pe->path,
5527 GOT_ERR_FILE_NOT_STAGED);
5530 return NULL;
5533 const struct got_error *
5534 got_worktree_commit(struct got_object_id **new_commit_id,
5535 struct got_worktree *worktree, struct got_pathlist_head *paths,
5536 const char *author, const char *committer, int allow_bad_symlinks,
5537 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5538 got_worktree_status_cb status_cb, void *status_arg,
5539 struct got_repository *repo)
5541 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5542 struct got_fileindex *fileindex = NULL;
5543 char *fileindex_path = NULL;
5544 struct got_pathlist_head commitable_paths;
5545 struct collect_commitables_arg cc_arg;
5546 struct got_pathlist_entry *pe;
5547 struct got_reference *head_ref = NULL;
5548 struct got_object_id *head_commit_id = NULL;
5549 int have_staged_files = 0;
5551 *new_commit_id = NULL;
5553 TAILQ_INIT(&commitable_paths);
5555 err = lock_worktree(worktree, LOCK_EX);
5556 if (err)
5557 goto done;
5559 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5560 if (err)
5561 goto done;
5563 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5564 if (err)
5565 goto done;
5567 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5568 if (err)
5569 goto done;
5571 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5572 &have_staged_files);
5573 if (err && err->code != GOT_ERR_CANCELLED)
5574 goto done;
5575 if (have_staged_files) {
5576 err = check_non_staged_files(fileindex, paths);
5577 if (err)
5578 goto done;
5581 cc_arg.commitable_paths = &commitable_paths;
5582 cc_arg.worktree = worktree;
5583 cc_arg.fileindex = fileindex;
5584 cc_arg.repo = repo;
5585 cc_arg.have_staged_files = have_staged_files;
5586 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5587 TAILQ_FOREACH(pe, paths, entry) {
5588 err = worktree_status(worktree, pe->path, fileindex, repo,
5589 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5590 if (err)
5591 goto done;
5594 if (TAILQ_EMPTY(&commitable_paths)) {
5595 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5596 goto done;
5599 TAILQ_FOREACH(pe, paths, entry) {
5600 err = check_path_is_commitable(pe->path, &commitable_paths);
5601 if (err)
5602 goto done;
5605 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5606 struct got_commitable *ct = pe->data;
5607 const char *ct_path = ct->in_repo_path;
5609 while (ct_path[0] == '/')
5610 ct_path++;
5611 err = check_out_of_date(ct_path, ct->status,
5612 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5613 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5614 if (err)
5615 goto done;
5619 err = commit_worktree(new_commit_id, &commitable_paths,
5620 head_commit_id, worktree, author, committer,
5621 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5622 if (err)
5623 goto done;
5625 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5626 fileindex, have_staged_files);
5627 sync_err = sync_fileindex(fileindex, fileindex_path);
5628 if (sync_err && err == NULL)
5629 err = sync_err;
5630 done:
5631 if (fileindex)
5632 got_fileindex_free(fileindex);
5633 free(fileindex_path);
5634 unlockerr = lock_worktree(worktree, LOCK_SH);
5635 if (unlockerr && err == NULL)
5636 err = unlockerr;
5637 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5638 struct got_commitable *ct = pe->data;
5639 free_commitable(ct);
5641 got_pathlist_free(&commitable_paths);
5642 return err;
5645 const char *
5646 got_commitable_get_path(struct got_commitable *ct)
5648 return ct->path;
5651 unsigned int
5652 got_commitable_get_status(struct got_commitable *ct)
5654 return ct->status;
5657 struct check_rebase_ok_arg {
5658 struct got_worktree *worktree;
5659 struct got_repository *repo;
5662 static const struct got_error *
5663 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5665 const struct got_error *err = NULL;
5666 struct check_rebase_ok_arg *a = arg;
5667 unsigned char status;
5668 struct stat sb;
5669 char *ondisk_path;
5671 /* Reject rebase of a work tree with mixed base commits. */
5672 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5673 SHA1_DIGEST_LENGTH))
5674 return got_error(GOT_ERR_MIXED_COMMITS);
5676 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5677 == -1)
5678 return got_error_from_errno("asprintf");
5680 /* Reject rebase of a work tree with modified or staged files. */
5681 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5682 free(ondisk_path);
5683 if (err)
5684 return err;
5686 if (status != GOT_STATUS_NO_CHANGE)
5687 return got_error(GOT_ERR_MODIFIED);
5688 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5689 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5691 return NULL;
5694 const struct got_error *
5695 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5696 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5697 struct got_worktree *worktree, struct got_reference *branch,
5698 struct got_repository *repo)
5700 const struct got_error *err = NULL;
5701 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5702 char *branch_ref_name = NULL;
5703 char *fileindex_path = NULL;
5704 struct check_rebase_ok_arg ok_arg;
5705 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5706 struct got_object_id *wt_branch_tip = NULL;
5708 *new_base_branch_ref = NULL;
5709 *tmp_branch = NULL;
5710 *fileindex = NULL;
5712 err = lock_worktree(worktree, LOCK_EX);
5713 if (err)
5714 return err;
5716 err = open_fileindex(fileindex, &fileindex_path, worktree);
5717 if (err)
5718 goto done;
5720 ok_arg.worktree = worktree;
5721 ok_arg.repo = repo;
5722 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5723 &ok_arg);
5724 if (err)
5725 goto done;
5727 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5728 if (err)
5729 goto done;
5731 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5732 if (err)
5733 goto done;
5735 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5736 if (err)
5737 goto done;
5739 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5740 0);
5741 if (err)
5742 goto done;
5744 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5745 if (err)
5746 goto done;
5747 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5748 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5749 goto done;
5752 err = got_ref_alloc_symref(new_base_branch_ref,
5753 new_base_branch_ref_name, wt_branch);
5754 if (err)
5755 goto done;
5756 err = got_ref_write(*new_base_branch_ref, repo);
5757 if (err)
5758 goto done;
5760 /* TODO Lock original branch's ref while rebasing? */
5762 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5763 if (err)
5764 goto done;
5766 err = got_ref_write(branch_ref, repo);
5767 if (err)
5768 goto done;
5770 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5771 worktree->base_commit_id);
5772 if (err)
5773 goto done;
5774 err = got_ref_write(*tmp_branch, repo);
5775 if (err)
5776 goto done;
5778 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5779 if (err)
5780 goto done;
5781 done:
5782 free(fileindex_path);
5783 free(tmp_branch_name);
5784 free(new_base_branch_ref_name);
5785 free(branch_ref_name);
5786 if (branch_ref)
5787 got_ref_close(branch_ref);
5788 if (wt_branch)
5789 got_ref_close(wt_branch);
5790 free(wt_branch_tip);
5791 if (err) {
5792 if (*new_base_branch_ref) {
5793 got_ref_close(*new_base_branch_ref);
5794 *new_base_branch_ref = NULL;
5796 if (*tmp_branch) {
5797 got_ref_close(*tmp_branch);
5798 *tmp_branch = NULL;
5800 if (*fileindex) {
5801 got_fileindex_free(*fileindex);
5802 *fileindex = NULL;
5804 lock_worktree(worktree, LOCK_SH);
5806 return err;
5809 const struct got_error *
5810 got_worktree_rebase_continue(struct got_object_id **commit_id,
5811 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5812 struct got_reference **branch, struct got_fileindex **fileindex,
5813 struct got_worktree *worktree, struct got_repository *repo)
5815 const struct got_error *err;
5816 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5817 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5818 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5819 char *fileindex_path = NULL;
5820 int have_staged_files = 0;
5822 *commit_id = NULL;
5823 *new_base_branch = NULL;
5824 *tmp_branch = NULL;
5825 *branch = NULL;
5826 *fileindex = NULL;
5828 err = lock_worktree(worktree, LOCK_EX);
5829 if (err)
5830 return err;
5832 err = open_fileindex(fileindex, &fileindex_path, worktree);
5833 if (err)
5834 goto done;
5836 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5837 &have_staged_files);
5838 if (err && err->code != GOT_ERR_CANCELLED)
5839 goto done;
5840 if (have_staged_files) {
5841 err = got_error(GOT_ERR_STAGED_PATHS);
5842 goto done;
5845 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5846 if (err)
5847 goto done;
5849 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5850 if (err)
5851 goto done;
5853 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5854 if (err)
5855 goto done;
5857 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5858 if (err)
5859 goto done;
5861 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5862 if (err)
5863 goto done;
5865 err = got_ref_open(branch, repo,
5866 got_ref_get_symref_target(branch_ref), 0);
5867 if (err)
5868 goto done;
5870 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5871 if (err)
5872 goto done;
5874 err = got_ref_resolve(commit_id, repo, commit_ref);
5875 if (err)
5876 goto done;
5878 err = got_ref_open(new_base_branch, repo,
5879 new_base_branch_ref_name, 0);
5880 if (err)
5881 goto done;
5883 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5884 if (err)
5885 goto done;
5886 done:
5887 free(commit_ref_name);
5888 free(branch_ref_name);
5889 free(fileindex_path);
5890 if (commit_ref)
5891 got_ref_close(commit_ref);
5892 if (branch_ref)
5893 got_ref_close(branch_ref);
5894 if (err) {
5895 free(*commit_id);
5896 *commit_id = NULL;
5897 if (*tmp_branch) {
5898 got_ref_close(*tmp_branch);
5899 *tmp_branch = NULL;
5901 if (*new_base_branch) {
5902 got_ref_close(*new_base_branch);
5903 *new_base_branch = NULL;
5905 if (*branch) {
5906 got_ref_close(*branch);
5907 *branch = NULL;
5909 if (*fileindex) {
5910 got_fileindex_free(*fileindex);
5911 *fileindex = NULL;
5913 lock_worktree(worktree, LOCK_SH);
5915 return err;
5918 const struct got_error *
5919 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5921 const struct got_error *err;
5922 char *tmp_branch_name = NULL;
5924 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5925 if (err)
5926 return err;
5928 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5929 free(tmp_branch_name);
5930 return NULL;
5933 static const struct got_error *
5934 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5935 char **logmsg, void *arg)
5937 *logmsg = arg;
5938 return NULL;
5941 static const struct got_error *
5942 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5943 const char *path, struct got_object_id *blob_id,
5944 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5945 int dirfd, const char *de_name)
5947 return NULL;
5950 struct collect_merged_paths_arg {
5951 got_worktree_checkout_cb progress_cb;
5952 void *progress_arg;
5953 struct got_pathlist_head *merged_paths;
5956 static const struct got_error *
5957 collect_merged_paths(void *arg, unsigned char status, const char *path)
5959 const struct got_error *err;
5960 struct collect_merged_paths_arg *a = arg;
5961 char *p;
5962 struct got_pathlist_entry *new;
5964 err = (*a->progress_cb)(a->progress_arg, status, path);
5965 if (err)
5966 return err;
5968 if (status != GOT_STATUS_MERGE &&
5969 status != GOT_STATUS_ADD &&
5970 status != GOT_STATUS_DELETE &&
5971 status != GOT_STATUS_CONFLICT)
5972 return NULL;
5974 p = strdup(path);
5975 if (p == NULL)
5976 return got_error_from_errno("strdup");
5978 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5979 if (err || new == NULL)
5980 free(p);
5981 return err;
5984 void
5985 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5987 struct got_pathlist_entry *pe;
5989 TAILQ_FOREACH(pe, merged_paths, entry)
5990 free((char *)pe->path);
5992 got_pathlist_free(merged_paths);
5995 static const struct got_error *
5996 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5997 int is_rebase, struct got_repository *repo)
5999 const struct got_error *err;
6000 struct got_reference *commit_ref = NULL;
6002 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6003 if (err) {
6004 if (err->code != GOT_ERR_NOT_REF)
6005 goto done;
6006 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6007 if (err)
6008 goto done;
6009 err = got_ref_write(commit_ref, repo);
6010 if (err)
6011 goto done;
6012 } else if (is_rebase) {
6013 struct got_object_id *stored_id;
6014 int cmp;
6016 err = got_ref_resolve(&stored_id, repo, commit_ref);
6017 if (err)
6018 goto done;
6019 cmp = got_object_id_cmp(commit_id, stored_id);
6020 free(stored_id);
6021 if (cmp != 0) {
6022 err = got_error(GOT_ERR_REBASE_COMMITID);
6023 goto done;
6026 done:
6027 if (commit_ref)
6028 got_ref_close(commit_ref);
6029 return err;
6032 static const struct got_error *
6033 rebase_merge_files(struct got_pathlist_head *merged_paths,
6034 const char *commit_ref_name, struct got_worktree *worktree,
6035 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6036 struct got_object_id *commit_id, struct got_repository *repo,
6037 got_worktree_checkout_cb progress_cb, void *progress_arg,
6038 got_cancel_cb cancel_cb, void *cancel_arg)
6040 const struct got_error *err;
6041 struct got_reference *commit_ref = NULL;
6042 struct collect_merged_paths_arg cmp_arg;
6043 char *fileindex_path;
6045 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6047 err = get_fileindex_path(&fileindex_path, worktree);
6048 if (err)
6049 return err;
6051 cmp_arg.progress_cb = progress_cb;
6052 cmp_arg.progress_arg = progress_arg;
6053 cmp_arg.merged_paths = merged_paths;
6054 err = merge_files(worktree, fileindex, fileindex_path,
6055 parent_commit_id, commit_id, repo, collect_merged_paths,
6056 &cmp_arg, cancel_cb, cancel_arg);
6057 if (commit_ref)
6058 got_ref_close(commit_ref);
6059 return err;
6062 const struct got_error *
6063 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6064 struct got_worktree *worktree, struct got_fileindex *fileindex,
6065 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6066 struct got_repository *repo,
6067 got_worktree_checkout_cb progress_cb, void *progress_arg,
6068 got_cancel_cb cancel_cb, void *cancel_arg)
6070 const struct got_error *err;
6071 char *commit_ref_name;
6073 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6074 if (err)
6075 return err;
6077 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6078 if (err)
6079 goto done;
6081 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6082 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6083 progress_arg, cancel_cb, cancel_arg);
6084 done:
6085 free(commit_ref_name);
6086 return err;
6089 const struct got_error *
6090 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6091 struct got_worktree *worktree, struct got_fileindex *fileindex,
6092 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6093 struct got_repository *repo,
6094 got_worktree_checkout_cb progress_cb, void *progress_arg,
6095 got_cancel_cb cancel_cb, void *cancel_arg)
6097 const struct got_error *err;
6098 char *commit_ref_name;
6100 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6101 if (err)
6102 return err;
6104 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6105 if (err)
6106 goto done;
6108 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6109 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6110 progress_arg, cancel_cb, cancel_arg);
6111 done:
6112 free(commit_ref_name);
6113 return err;
6116 static const struct got_error *
6117 rebase_commit(struct got_object_id **new_commit_id,
6118 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6119 struct got_worktree *worktree, struct got_fileindex *fileindex,
6120 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6121 const char *new_logmsg, struct got_repository *repo)
6123 const struct got_error *err, *sync_err;
6124 struct got_pathlist_head commitable_paths;
6125 struct collect_commitables_arg cc_arg;
6126 char *fileindex_path = NULL;
6127 struct got_reference *head_ref = NULL;
6128 struct got_object_id *head_commit_id = NULL;
6129 char *logmsg = NULL;
6131 TAILQ_INIT(&commitable_paths);
6132 *new_commit_id = NULL;
6134 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6136 err = get_fileindex_path(&fileindex_path, worktree);
6137 if (err)
6138 return err;
6140 cc_arg.commitable_paths = &commitable_paths;
6141 cc_arg.worktree = worktree;
6142 cc_arg.repo = repo;
6143 cc_arg.have_staged_files = 0;
6145 * If possible get the status of individual files directly to
6146 * avoid crawling the entire work tree once per rebased commit.
6147 * TODO: Ideally, merged_paths would contain a list of commitables
6148 * we could use so we could skip worktree_status() entirely.
6150 if (merged_paths) {
6151 struct got_pathlist_entry *pe;
6152 TAILQ_FOREACH(pe, merged_paths, entry) {
6153 err = worktree_status(worktree, pe->path, fileindex,
6154 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6155 0);
6156 if (err)
6157 goto done;
6159 } else {
6160 err = worktree_status(worktree, "", fileindex, repo,
6161 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6162 if (err)
6163 goto done;
6166 if (TAILQ_EMPTY(&commitable_paths)) {
6167 /* No-op change; commit will be elided. */
6168 err = got_ref_delete(commit_ref, repo);
6169 if (err)
6170 goto done;
6171 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6172 goto done;
6175 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6176 if (err)
6177 goto done;
6179 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6180 if (err)
6181 goto done;
6183 if (new_logmsg) {
6184 logmsg = strdup(new_logmsg);
6185 if (logmsg == NULL) {
6186 err = got_error_from_errno("strdup");
6187 goto done;
6189 } else {
6190 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6191 if (err)
6192 goto done;
6195 /* NB: commit_worktree will call free(logmsg) */
6196 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6197 worktree, got_object_commit_get_author(orig_commit),
6198 got_object_commit_get_committer(orig_commit),
6199 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6200 if (err)
6201 goto done;
6203 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6204 if (err)
6205 goto done;
6207 err = got_ref_delete(commit_ref, repo);
6208 if (err)
6209 goto done;
6211 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6212 fileindex, 0);
6213 sync_err = sync_fileindex(fileindex, fileindex_path);
6214 if (sync_err && err == NULL)
6215 err = sync_err;
6216 done:
6217 free(fileindex_path);
6218 free(head_commit_id);
6219 if (head_ref)
6220 got_ref_close(head_ref);
6221 if (err) {
6222 free(*new_commit_id);
6223 *new_commit_id = NULL;
6225 return err;
6228 const struct got_error *
6229 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6230 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6231 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6232 struct got_commit_object *orig_commit,
6233 struct got_object_id *orig_commit_id, struct got_repository *repo)
6235 const struct got_error *err;
6236 char *commit_ref_name;
6237 struct got_reference *commit_ref = NULL;
6238 struct got_object_id *commit_id = NULL;
6240 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6241 if (err)
6242 return err;
6244 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6245 if (err)
6246 goto done;
6247 err = got_ref_resolve(&commit_id, repo, commit_ref);
6248 if (err)
6249 goto done;
6250 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6251 err = got_error(GOT_ERR_REBASE_COMMITID);
6252 goto done;
6255 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6256 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6257 done:
6258 if (commit_ref)
6259 got_ref_close(commit_ref);
6260 free(commit_ref_name);
6261 free(commit_id);
6262 return err;
6265 const struct got_error *
6266 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6267 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6268 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6269 struct got_commit_object *orig_commit,
6270 struct got_object_id *orig_commit_id, const char *new_logmsg,
6271 struct got_repository *repo)
6273 const struct got_error *err;
6274 char *commit_ref_name;
6275 struct got_reference *commit_ref = NULL;
6277 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6278 if (err)
6279 return err;
6281 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6282 if (err)
6283 goto done;
6285 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6286 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6287 done:
6288 if (commit_ref)
6289 got_ref_close(commit_ref);
6290 free(commit_ref_name);
6291 return err;
6294 const struct got_error *
6295 got_worktree_rebase_postpone(struct got_worktree *worktree,
6296 struct got_fileindex *fileindex)
6298 if (fileindex)
6299 got_fileindex_free(fileindex);
6300 return lock_worktree(worktree, LOCK_SH);
6303 static const struct got_error *
6304 delete_ref(const char *name, struct got_repository *repo)
6306 const struct got_error *err;
6307 struct got_reference *ref;
6309 err = got_ref_open(&ref, repo, name, 0);
6310 if (err) {
6311 if (err->code == GOT_ERR_NOT_REF)
6312 return NULL;
6313 return err;
6316 err = got_ref_delete(ref, repo);
6317 got_ref_close(ref);
6318 return err;
6321 static const struct got_error *
6322 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6324 const struct got_error *err;
6325 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6326 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6328 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6329 if (err)
6330 goto done;
6331 err = delete_ref(tmp_branch_name, repo);
6332 if (err)
6333 goto done;
6335 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6336 if (err)
6337 goto done;
6338 err = delete_ref(new_base_branch_ref_name, repo);
6339 if (err)
6340 goto done;
6342 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6343 if (err)
6344 goto done;
6345 err = delete_ref(branch_ref_name, repo);
6346 if (err)
6347 goto done;
6349 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6350 if (err)
6351 goto done;
6352 err = delete_ref(commit_ref_name, repo);
6353 if (err)
6354 goto done;
6356 done:
6357 free(tmp_branch_name);
6358 free(new_base_branch_ref_name);
6359 free(branch_ref_name);
6360 free(commit_ref_name);
6361 return err;
6364 const struct got_error *
6365 got_worktree_rebase_complete(struct got_worktree *worktree,
6366 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6367 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6368 struct got_repository *repo)
6370 const struct got_error *err, *unlockerr;
6371 struct got_object_id *new_head_commit_id = NULL;
6373 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6374 if (err)
6375 return err;
6377 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6378 if (err)
6379 goto done;
6381 err = got_ref_write(rebased_branch, repo);
6382 if (err)
6383 goto done;
6385 err = got_worktree_set_head_ref(worktree, rebased_branch);
6386 if (err)
6387 goto done;
6389 err = delete_rebase_refs(worktree, repo);
6390 done:
6391 if (fileindex)
6392 got_fileindex_free(fileindex);
6393 free(new_head_commit_id);
6394 unlockerr = lock_worktree(worktree, LOCK_SH);
6395 if (unlockerr && err == NULL)
6396 err = unlockerr;
6397 return err;
6400 const struct got_error *
6401 got_worktree_rebase_abort(struct got_worktree *worktree,
6402 struct got_fileindex *fileindex, struct got_repository *repo,
6403 struct got_reference *new_base_branch,
6404 got_worktree_checkout_cb progress_cb, void *progress_arg)
6406 const struct got_error *err, *unlockerr, *sync_err;
6407 struct got_reference *resolved = NULL;
6408 struct got_object_id *commit_id = NULL;
6409 char *fileindex_path = NULL;
6410 struct revert_file_args rfa;
6411 struct got_object_id *tree_id = NULL;
6413 err = lock_worktree(worktree, LOCK_EX);
6414 if (err)
6415 return err;
6417 err = got_ref_open(&resolved, repo,
6418 got_ref_get_symref_target(new_base_branch), 0);
6419 if (err)
6420 goto done;
6422 err = got_worktree_set_head_ref(worktree, resolved);
6423 if (err)
6424 goto done;
6427 * XXX commits to the base branch could have happened while
6428 * we were busy rebasing; should we store the original commit ID
6429 * when rebase begins and read it back here?
6431 err = got_ref_resolve(&commit_id, repo, resolved);
6432 if (err)
6433 goto done;
6435 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6436 if (err)
6437 goto done;
6439 err = got_object_id_by_path(&tree_id, repo,
6440 worktree->base_commit_id, worktree->path_prefix);
6441 if (err)
6442 goto done;
6444 err = delete_rebase_refs(worktree, repo);
6445 if (err)
6446 goto done;
6448 err = get_fileindex_path(&fileindex_path, worktree);
6449 if (err)
6450 goto done;
6452 rfa.worktree = worktree;
6453 rfa.fileindex = fileindex;
6454 rfa.progress_cb = progress_cb;
6455 rfa.progress_arg = progress_arg;
6456 rfa.patch_cb = NULL;
6457 rfa.patch_arg = NULL;
6458 rfa.repo = repo;
6459 err = worktree_status(worktree, "", fileindex, repo,
6460 revert_file, &rfa, NULL, NULL, 0, 0);
6461 if (err)
6462 goto sync;
6464 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6465 repo, progress_cb, progress_arg, NULL, NULL);
6466 sync:
6467 sync_err = sync_fileindex(fileindex, fileindex_path);
6468 if (sync_err && err == NULL)
6469 err = sync_err;
6470 done:
6471 got_ref_close(resolved);
6472 free(tree_id);
6473 free(commit_id);
6474 if (fileindex)
6475 got_fileindex_free(fileindex);
6476 free(fileindex_path);
6478 unlockerr = lock_worktree(worktree, LOCK_SH);
6479 if (unlockerr && err == NULL)
6480 err = unlockerr;
6481 return err;
6484 const struct got_error *
6485 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6486 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6487 struct got_fileindex **fileindex, struct got_worktree *worktree,
6488 struct got_repository *repo)
6490 const struct got_error *err = NULL;
6491 char *tmp_branch_name = NULL;
6492 char *branch_ref_name = NULL;
6493 char *base_commit_ref_name = NULL;
6494 char *fileindex_path = NULL;
6495 struct check_rebase_ok_arg ok_arg;
6496 struct got_reference *wt_branch = NULL;
6497 struct got_reference *base_commit_ref = NULL;
6499 *tmp_branch = NULL;
6500 *branch_ref = NULL;
6501 *base_commit_id = NULL;
6502 *fileindex = NULL;
6504 err = lock_worktree(worktree, LOCK_EX);
6505 if (err)
6506 return err;
6508 err = open_fileindex(fileindex, &fileindex_path, worktree);
6509 if (err)
6510 goto done;
6512 ok_arg.worktree = worktree;
6513 ok_arg.repo = repo;
6514 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6515 &ok_arg);
6516 if (err)
6517 goto done;
6519 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6520 if (err)
6521 goto done;
6523 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6524 if (err)
6525 goto done;
6527 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6528 worktree);
6529 if (err)
6530 goto done;
6532 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6533 0);
6534 if (err)
6535 goto done;
6537 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6538 if (err)
6539 goto done;
6541 err = got_ref_write(*branch_ref, repo);
6542 if (err)
6543 goto done;
6545 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6546 worktree->base_commit_id);
6547 if (err)
6548 goto done;
6549 err = got_ref_write(base_commit_ref, repo);
6550 if (err)
6551 goto done;
6552 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6553 if (*base_commit_id == NULL) {
6554 err = got_error_from_errno("got_object_id_dup");
6555 goto done;
6558 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6559 worktree->base_commit_id);
6560 if (err)
6561 goto done;
6562 err = got_ref_write(*tmp_branch, repo);
6563 if (err)
6564 goto done;
6566 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6567 if (err)
6568 goto done;
6569 done:
6570 free(fileindex_path);
6571 free(tmp_branch_name);
6572 free(branch_ref_name);
6573 free(base_commit_ref_name);
6574 if (wt_branch)
6575 got_ref_close(wt_branch);
6576 if (err) {
6577 if (*branch_ref) {
6578 got_ref_close(*branch_ref);
6579 *branch_ref = NULL;
6581 if (*tmp_branch) {
6582 got_ref_close(*tmp_branch);
6583 *tmp_branch = NULL;
6585 free(*base_commit_id);
6586 if (*fileindex) {
6587 got_fileindex_free(*fileindex);
6588 *fileindex = NULL;
6590 lock_worktree(worktree, LOCK_SH);
6592 return err;
6595 const struct got_error *
6596 got_worktree_histedit_postpone(struct got_worktree *worktree,
6597 struct got_fileindex *fileindex)
6599 if (fileindex)
6600 got_fileindex_free(fileindex);
6601 return lock_worktree(worktree, LOCK_SH);
6604 const struct got_error *
6605 got_worktree_histedit_in_progress(int *in_progress,
6606 struct got_worktree *worktree)
6608 const struct got_error *err;
6609 char *tmp_branch_name = NULL;
6611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6612 if (err)
6613 return err;
6615 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6616 free(tmp_branch_name);
6617 return NULL;
6620 const struct got_error *
6621 got_worktree_histedit_continue(struct got_object_id **commit_id,
6622 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6623 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6624 struct got_worktree *worktree, struct got_repository *repo)
6626 const struct got_error *err;
6627 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6628 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6629 struct got_reference *commit_ref = NULL;
6630 struct got_reference *base_commit_ref = NULL;
6631 char *fileindex_path = NULL;
6632 int have_staged_files = 0;
6634 *commit_id = NULL;
6635 *tmp_branch = NULL;
6636 *base_commit_id = NULL;
6637 *fileindex = NULL;
6639 err = lock_worktree(worktree, LOCK_EX);
6640 if (err)
6641 return err;
6643 err = open_fileindex(fileindex, &fileindex_path, worktree);
6644 if (err)
6645 goto done;
6647 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6648 &have_staged_files);
6649 if (err && err->code != GOT_ERR_CANCELLED)
6650 goto done;
6651 if (have_staged_files) {
6652 err = got_error(GOT_ERR_STAGED_PATHS);
6653 goto done;
6656 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6657 if (err)
6658 goto done;
6660 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6661 if (err)
6662 goto done;
6664 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6665 if (err)
6666 goto done;
6668 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6669 worktree);
6670 if (err)
6671 goto done;
6673 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6674 if (err)
6675 goto done;
6677 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6678 if (err)
6679 goto done;
6680 err = got_ref_resolve(commit_id, repo, commit_ref);
6681 if (err)
6682 goto done;
6684 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6685 if (err)
6686 goto done;
6687 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6688 if (err)
6689 goto done;
6691 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6692 if (err)
6693 goto done;
6694 done:
6695 free(commit_ref_name);
6696 free(branch_ref_name);
6697 free(fileindex_path);
6698 if (commit_ref)
6699 got_ref_close(commit_ref);
6700 if (base_commit_ref)
6701 got_ref_close(base_commit_ref);
6702 if (err) {
6703 free(*commit_id);
6704 *commit_id = NULL;
6705 free(*base_commit_id);
6706 *base_commit_id = NULL;
6707 if (*tmp_branch) {
6708 got_ref_close(*tmp_branch);
6709 *tmp_branch = NULL;
6711 if (*fileindex) {
6712 got_fileindex_free(*fileindex);
6713 *fileindex = NULL;
6715 lock_worktree(worktree, LOCK_EX);
6717 return err;
6720 static const struct got_error *
6721 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6723 const struct got_error *err;
6724 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6725 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6727 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6728 if (err)
6729 goto done;
6730 err = delete_ref(tmp_branch_name, repo);
6731 if (err)
6732 goto done;
6734 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6735 worktree);
6736 if (err)
6737 goto done;
6738 err = delete_ref(base_commit_ref_name, repo);
6739 if (err)
6740 goto done;
6742 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6743 if (err)
6744 goto done;
6745 err = delete_ref(branch_ref_name, repo);
6746 if (err)
6747 goto done;
6749 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6750 if (err)
6751 goto done;
6752 err = delete_ref(commit_ref_name, repo);
6753 if (err)
6754 goto done;
6755 done:
6756 free(tmp_branch_name);
6757 free(base_commit_ref_name);
6758 free(branch_ref_name);
6759 free(commit_ref_name);
6760 return err;
6763 const struct got_error *
6764 got_worktree_histedit_abort(struct got_worktree *worktree,
6765 struct got_fileindex *fileindex, struct got_repository *repo,
6766 struct got_reference *branch, struct got_object_id *base_commit_id,
6767 got_worktree_checkout_cb progress_cb, void *progress_arg)
6769 const struct got_error *err, *unlockerr, *sync_err;
6770 struct got_reference *resolved = NULL;
6771 char *fileindex_path = NULL;
6772 struct got_object_id *tree_id = NULL;
6773 struct revert_file_args rfa;
6775 err = lock_worktree(worktree, LOCK_EX);
6776 if (err)
6777 return err;
6779 err = got_ref_open(&resolved, repo,
6780 got_ref_get_symref_target(branch), 0);
6781 if (err)
6782 goto done;
6784 err = got_worktree_set_head_ref(worktree, resolved);
6785 if (err)
6786 goto done;
6788 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6789 if (err)
6790 goto done;
6792 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6793 worktree->path_prefix);
6794 if (err)
6795 goto done;
6797 err = delete_histedit_refs(worktree, repo);
6798 if (err)
6799 goto done;
6801 err = get_fileindex_path(&fileindex_path, worktree);
6802 if (err)
6803 goto done;
6805 rfa.worktree = worktree;
6806 rfa.fileindex = fileindex;
6807 rfa.progress_cb = progress_cb;
6808 rfa.progress_arg = progress_arg;
6809 rfa.patch_cb = NULL;
6810 rfa.patch_arg = NULL;
6811 rfa.repo = repo;
6812 err = worktree_status(worktree, "", fileindex, repo,
6813 revert_file, &rfa, NULL, NULL, 0, 0);
6814 if (err)
6815 goto sync;
6817 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6818 repo, progress_cb, progress_arg, NULL, NULL);
6819 sync:
6820 sync_err = sync_fileindex(fileindex, fileindex_path);
6821 if (sync_err && err == NULL)
6822 err = sync_err;
6823 done:
6824 got_ref_close(resolved);
6825 free(tree_id);
6826 free(fileindex_path);
6828 unlockerr = lock_worktree(worktree, LOCK_SH);
6829 if (unlockerr && err == NULL)
6830 err = unlockerr;
6831 return err;
6834 const struct got_error *
6835 got_worktree_histedit_complete(struct got_worktree *worktree,
6836 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6837 struct got_reference *edited_branch, struct got_repository *repo)
6839 const struct got_error *err, *unlockerr;
6840 struct got_object_id *new_head_commit_id = NULL;
6841 struct got_reference *resolved = NULL;
6843 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6844 if (err)
6845 return err;
6847 err = got_ref_open(&resolved, repo,
6848 got_ref_get_symref_target(edited_branch), 0);
6849 if (err)
6850 goto done;
6852 err = got_ref_change_ref(resolved, new_head_commit_id);
6853 if (err)
6854 goto done;
6856 err = got_ref_write(resolved, repo);
6857 if (err)
6858 goto done;
6860 err = got_worktree_set_head_ref(worktree, resolved);
6861 if (err)
6862 goto done;
6864 err = delete_histedit_refs(worktree, repo);
6865 done:
6866 if (fileindex)
6867 got_fileindex_free(fileindex);
6868 free(new_head_commit_id);
6869 unlockerr = lock_worktree(worktree, LOCK_SH);
6870 if (unlockerr && err == NULL)
6871 err = unlockerr;
6872 return err;
6875 const struct got_error *
6876 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6877 struct got_object_id *commit_id, struct got_repository *repo)
6879 const struct got_error *err;
6880 char *commit_ref_name;
6882 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6883 if (err)
6884 return err;
6886 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6887 if (err)
6888 goto done;
6890 err = delete_ref(commit_ref_name, repo);
6891 done:
6892 free(commit_ref_name);
6893 return err;
6896 const struct got_error *
6897 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6898 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6899 struct got_worktree *worktree, const char *refname,
6900 struct got_repository *repo)
6902 const struct got_error *err = NULL;
6903 char *fileindex_path = NULL;
6904 struct check_rebase_ok_arg ok_arg;
6906 *fileindex = NULL;
6907 *branch_ref = NULL;
6908 *base_branch_ref = NULL;
6910 err = lock_worktree(worktree, LOCK_EX);
6911 if (err)
6912 return err;
6914 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6915 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6916 "cannot integrate a branch into itself; "
6917 "update -b or different branch name required");
6918 goto done;
6921 err = open_fileindex(fileindex, &fileindex_path, worktree);
6922 if (err)
6923 goto done;
6925 /* Preconditions are the same as for rebase. */
6926 ok_arg.worktree = worktree;
6927 ok_arg.repo = repo;
6928 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6929 &ok_arg);
6930 if (err)
6931 goto done;
6933 err = got_ref_open(branch_ref, repo, refname, 1);
6934 if (err)
6935 goto done;
6937 err = got_ref_open(base_branch_ref, repo,
6938 got_worktree_get_head_ref_name(worktree), 1);
6939 done:
6940 if (err) {
6941 if (*branch_ref) {
6942 got_ref_close(*branch_ref);
6943 *branch_ref = NULL;
6945 if (*base_branch_ref) {
6946 got_ref_close(*base_branch_ref);
6947 *base_branch_ref = NULL;
6949 if (*fileindex) {
6950 got_fileindex_free(*fileindex);
6951 *fileindex = NULL;
6953 lock_worktree(worktree, LOCK_SH);
6955 return err;
6958 const struct got_error *
6959 got_worktree_integrate_continue(struct got_worktree *worktree,
6960 struct got_fileindex *fileindex, struct got_repository *repo,
6961 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6962 got_worktree_checkout_cb progress_cb, void *progress_arg,
6963 got_cancel_cb cancel_cb, void *cancel_arg)
6965 const struct got_error *err = NULL, *sync_err, *unlockerr;
6966 char *fileindex_path = NULL;
6967 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6969 err = get_fileindex_path(&fileindex_path, worktree);
6970 if (err)
6971 goto done;
6973 err = got_ref_resolve(&commit_id, repo, branch_ref);
6974 if (err)
6975 goto done;
6977 err = got_object_id_by_path(&tree_id, repo, commit_id,
6978 worktree->path_prefix);
6979 if (err)
6980 goto done;
6982 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6983 if (err)
6984 goto done;
6986 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6987 progress_cb, progress_arg, cancel_cb, cancel_arg);
6988 if (err)
6989 goto sync;
6991 err = got_ref_change_ref(base_branch_ref, commit_id);
6992 if (err)
6993 goto sync;
6995 err = got_ref_write(base_branch_ref, repo);
6996 sync:
6997 sync_err = sync_fileindex(fileindex, fileindex_path);
6998 if (sync_err && err == NULL)
6999 err = sync_err;
7001 done:
7002 unlockerr = got_ref_unlock(branch_ref);
7003 if (unlockerr && err == NULL)
7004 err = unlockerr;
7005 got_ref_close(branch_ref);
7007 unlockerr = got_ref_unlock(base_branch_ref);
7008 if (unlockerr && err == NULL)
7009 err = unlockerr;
7010 got_ref_close(base_branch_ref);
7012 got_fileindex_free(fileindex);
7013 free(fileindex_path);
7014 free(tree_id);
7016 unlockerr = lock_worktree(worktree, LOCK_SH);
7017 if (unlockerr && err == NULL)
7018 err = unlockerr;
7019 return err;
7022 const struct got_error *
7023 got_worktree_integrate_abort(struct got_worktree *worktree,
7024 struct got_fileindex *fileindex, struct got_repository *repo,
7025 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7027 const struct got_error *err = NULL, *unlockerr = NULL;
7029 got_fileindex_free(fileindex);
7031 err = lock_worktree(worktree, LOCK_SH);
7033 unlockerr = got_ref_unlock(branch_ref);
7034 if (unlockerr && err == NULL)
7035 err = unlockerr;
7036 got_ref_close(branch_ref);
7038 unlockerr = got_ref_unlock(base_branch_ref);
7039 if (unlockerr && err == NULL)
7040 err = unlockerr;
7041 got_ref_close(base_branch_ref);
7043 return err;
7046 struct check_stage_ok_arg {
7047 struct got_object_id *head_commit_id;
7048 struct got_worktree *worktree;
7049 struct got_fileindex *fileindex;
7050 struct got_repository *repo;
7051 int have_changes;
7054 const struct got_error *
7055 check_stage_ok(void *arg, unsigned char status,
7056 unsigned char staged_status, const char *relpath,
7057 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7058 struct got_object_id *commit_id, int dirfd, const char *de_name)
7060 struct check_stage_ok_arg *a = arg;
7061 const struct got_error *err = NULL;
7062 struct got_fileindex_entry *ie;
7063 struct got_object_id base_commit_id;
7064 struct got_object_id *base_commit_idp = NULL;
7065 char *in_repo_path = NULL, *p;
7067 if (status == GOT_STATUS_UNVERSIONED ||
7068 status == GOT_STATUS_NO_CHANGE)
7069 return NULL;
7070 if (status == GOT_STATUS_NONEXISTENT)
7071 return got_error_set_errno(ENOENT, relpath);
7073 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7074 if (ie == NULL)
7075 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7077 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7078 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7079 relpath) == -1)
7080 return got_error_from_errno("asprintf");
7082 if (got_fileindex_entry_has_commit(ie)) {
7083 memcpy(base_commit_id.sha1, ie->commit_sha1,
7084 SHA1_DIGEST_LENGTH);
7085 base_commit_idp = &base_commit_id;
7088 if (status == GOT_STATUS_CONFLICT) {
7089 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7090 goto done;
7091 } else if (status != GOT_STATUS_ADD &&
7092 status != GOT_STATUS_MODIFY &&
7093 status != GOT_STATUS_DELETE) {
7094 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7095 goto done;
7098 a->have_changes = 1;
7100 p = in_repo_path;
7101 while (p[0] == '/')
7102 p++;
7103 err = check_out_of_date(p, status, staged_status,
7104 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7105 GOT_ERR_STAGE_OUT_OF_DATE);
7106 done:
7107 free(in_repo_path);
7108 return err;
7111 struct stage_path_arg {
7112 struct got_worktree *worktree;
7113 struct got_fileindex *fileindex;
7114 struct got_repository *repo;
7115 got_worktree_status_cb status_cb;
7116 void *status_arg;
7117 got_worktree_patch_cb patch_cb;
7118 void *patch_arg;
7119 int staged_something;
7120 int allow_bad_symlinks;
7123 static const struct got_error *
7124 stage_path(void *arg, unsigned char status,
7125 unsigned char staged_status, const char *relpath,
7126 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7127 struct got_object_id *commit_id, int dirfd, const char *de_name)
7129 struct stage_path_arg *a = arg;
7130 const struct got_error *err = NULL;
7131 struct got_fileindex_entry *ie;
7132 char *ondisk_path = NULL, *path_content = NULL;
7133 uint32_t stage;
7134 struct got_object_id *new_staged_blob_id = NULL;
7135 struct stat sb;
7137 if (status == GOT_STATUS_UNVERSIONED)
7138 return NULL;
7140 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7141 if (ie == NULL)
7142 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7144 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7145 relpath)== -1)
7146 return got_error_from_errno("asprintf");
7148 switch (status) {
7149 case GOT_STATUS_ADD:
7150 case GOT_STATUS_MODIFY:
7151 /* XXX could sb.st_mode be passed in by our caller? */
7152 if (lstat(ondisk_path, &sb) == -1) {
7153 err = got_error_from_errno2("lstat", ondisk_path);
7154 break;
7156 if (a->patch_cb) {
7157 if (status == GOT_STATUS_ADD) {
7158 int choice = GOT_PATCH_CHOICE_NONE;
7159 err = (*a->patch_cb)(&choice, a->patch_arg,
7160 status, ie->path, NULL, 1, 1);
7161 if (err)
7162 break;
7163 if (choice != GOT_PATCH_CHOICE_YES)
7164 break;
7165 } else {
7166 err = create_patched_content(&path_content, 0,
7167 staged_blob_id ? staged_blob_id : blob_id,
7168 ondisk_path, dirfd, de_name, ie->path,
7169 a->repo, a->patch_cb, a->patch_arg);
7170 if (err || path_content == NULL)
7171 break;
7174 err = got_object_blob_create(&new_staged_blob_id,
7175 path_content ? path_content : ondisk_path, a->repo);
7176 if (err)
7177 break;
7178 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7179 SHA1_DIGEST_LENGTH);
7180 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7181 stage = GOT_FILEIDX_STAGE_ADD;
7182 else
7183 stage = GOT_FILEIDX_STAGE_MODIFY;
7184 got_fileindex_entry_stage_set(ie, stage);
7185 if (S_ISLNK(sb.st_mode)) {
7186 int is_bad_symlink = 0;
7187 if (!a->allow_bad_symlinks) {
7188 char target_path[PATH_MAX];
7189 ssize_t target_len;
7190 target_len = readlink(ondisk_path, target_path,
7191 sizeof(target_path));
7192 if (target_len == -1) {
7193 err = got_error_from_errno2("readlink",
7194 ondisk_path);
7195 break;
7197 err = is_bad_symlink_target(&is_bad_symlink,
7198 target_path, target_len, ondisk_path,
7199 a->worktree->root_path);
7200 if (err)
7201 break;
7202 if (is_bad_symlink) {
7203 err = got_error_path(ondisk_path,
7204 GOT_ERR_BAD_SYMLINK);
7205 break;
7208 if (is_bad_symlink)
7209 got_fileindex_entry_staged_filetype_set(ie,
7210 GOT_FILEIDX_MODE_BAD_SYMLINK);
7211 else
7212 got_fileindex_entry_staged_filetype_set(ie,
7213 GOT_FILEIDX_MODE_SYMLINK);
7214 } else {
7215 got_fileindex_entry_staged_filetype_set(ie,
7216 GOT_FILEIDX_MODE_REGULAR_FILE);
7218 a->staged_something = 1;
7219 if (a->status_cb == NULL)
7220 break;
7221 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7222 get_staged_status(ie), relpath, blob_id,
7223 new_staged_blob_id, NULL, dirfd, de_name);
7224 break;
7225 case GOT_STATUS_DELETE:
7226 if (staged_status == GOT_STATUS_DELETE)
7227 break;
7228 if (a->patch_cb) {
7229 int choice = GOT_PATCH_CHOICE_NONE;
7230 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7231 ie->path, NULL, 1, 1);
7232 if (err)
7233 break;
7234 if (choice == GOT_PATCH_CHOICE_NO)
7235 break;
7236 if (choice != GOT_PATCH_CHOICE_YES) {
7237 err = got_error(GOT_ERR_PATCH_CHOICE);
7238 break;
7241 stage = GOT_FILEIDX_STAGE_DELETE;
7242 got_fileindex_entry_stage_set(ie, stage);
7243 a->staged_something = 1;
7244 if (a->status_cb == NULL)
7245 break;
7246 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7247 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7248 de_name);
7249 break;
7250 case GOT_STATUS_NO_CHANGE:
7251 break;
7252 case GOT_STATUS_CONFLICT:
7253 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7254 break;
7255 case GOT_STATUS_NONEXISTENT:
7256 err = got_error_set_errno(ENOENT, relpath);
7257 break;
7258 default:
7259 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7260 break;
7263 if (path_content && unlink(path_content) == -1 && err == NULL)
7264 err = got_error_from_errno2("unlink", path_content);
7265 free(path_content);
7266 free(ondisk_path);
7267 free(new_staged_blob_id);
7268 return err;
7271 const struct got_error *
7272 got_worktree_stage(struct got_worktree *worktree,
7273 struct got_pathlist_head *paths,
7274 got_worktree_status_cb status_cb, void *status_arg,
7275 got_worktree_patch_cb patch_cb, void *patch_arg,
7276 int allow_bad_symlinks, struct got_repository *repo)
7278 const struct got_error *err = NULL, *sync_err, *unlockerr;
7279 struct got_pathlist_entry *pe;
7280 struct got_fileindex *fileindex = NULL;
7281 char *fileindex_path = NULL;
7282 struct got_reference *head_ref = NULL;
7283 struct got_object_id *head_commit_id = NULL;
7284 struct check_stage_ok_arg oka;
7285 struct stage_path_arg spa;
7287 err = lock_worktree(worktree, LOCK_EX);
7288 if (err)
7289 return err;
7291 err = got_ref_open(&head_ref, repo,
7292 got_worktree_get_head_ref_name(worktree), 0);
7293 if (err)
7294 goto done;
7295 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7296 if (err)
7297 goto done;
7298 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7299 if (err)
7300 goto done;
7302 /* Check pre-conditions before staging anything. */
7303 oka.head_commit_id = head_commit_id;
7304 oka.worktree = worktree;
7305 oka.fileindex = fileindex;
7306 oka.repo = repo;
7307 oka.have_changes = 0;
7308 TAILQ_FOREACH(pe, paths, entry) {
7309 err = worktree_status(worktree, pe->path, fileindex, repo,
7310 check_stage_ok, &oka, NULL, NULL, 0, 0);
7311 if (err)
7312 goto done;
7314 if (!oka.have_changes) {
7315 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7316 goto done;
7319 spa.worktree = worktree;
7320 spa.fileindex = fileindex;
7321 spa.repo = repo;
7322 spa.patch_cb = patch_cb;
7323 spa.patch_arg = patch_arg;
7324 spa.status_cb = status_cb;
7325 spa.status_arg = status_arg;
7326 spa.staged_something = 0;
7327 spa.allow_bad_symlinks = allow_bad_symlinks;
7328 TAILQ_FOREACH(pe, paths, entry) {
7329 err = worktree_status(worktree, pe->path, fileindex, repo,
7330 stage_path, &spa, NULL, NULL, 0, 0);
7331 if (err)
7332 goto done;
7334 if (!spa.staged_something) {
7335 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7336 goto done;
7339 sync_err = sync_fileindex(fileindex, fileindex_path);
7340 if (sync_err && err == NULL)
7341 err = sync_err;
7342 done:
7343 if (head_ref)
7344 got_ref_close(head_ref);
7345 free(head_commit_id);
7346 free(fileindex_path);
7347 if (fileindex)
7348 got_fileindex_free(fileindex);
7349 unlockerr = lock_worktree(worktree, LOCK_SH);
7350 if (unlockerr && err == NULL)
7351 err = unlockerr;
7352 return err;
7355 struct unstage_path_arg {
7356 struct got_worktree *worktree;
7357 struct got_fileindex *fileindex;
7358 struct got_repository *repo;
7359 got_worktree_checkout_cb progress_cb;
7360 void *progress_arg;
7361 got_worktree_patch_cb patch_cb;
7362 void *patch_arg;
7365 static const struct got_error *
7366 create_unstaged_content(char **path_unstaged_content,
7367 char **path_new_staged_content, struct got_object_id *blob_id,
7368 struct got_object_id *staged_blob_id, const char *relpath,
7369 struct got_repository *repo,
7370 got_worktree_patch_cb patch_cb, void *patch_arg)
7372 const struct got_error *err;
7373 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7374 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7375 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7376 struct stat sb1, sb2;
7377 struct got_diff_changes *changes = NULL;
7378 struct got_diff_state *ds = NULL;
7379 struct got_diff_args *args = NULL;
7380 struct got_diff_change *change;
7381 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7382 int have_content = 0, have_rejected_content = 0;
7384 *path_unstaged_content = NULL;
7385 *path_new_staged_content = NULL;
7387 err = got_object_id_str(&label1, blob_id);
7388 if (err)
7389 return err;
7390 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7391 if (err)
7392 goto done;
7394 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7395 if (err)
7396 goto done;
7398 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7399 if (err)
7400 goto done;
7402 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7403 if (err)
7404 goto done;
7406 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7407 if (err)
7408 goto done;
7410 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7411 if (err)
7412 goto done;
7414 if (stat(path1, &sb1) == -1) {
7415 err = got_error_from_errno2("stat", path1);
7416 goto done;
7419 if (stat(path2, &sb2) == -1) {
7420 err = got_error_from_errno2("stat", path2);
7421 goto done;
7424 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7425 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7426 if (err)
7427 goto done;
7429 err = got_opentemp_named(path_unstaged_content, &outfile,
7430 "got-unstaged-content");
7431 if (err)
7432 goto done;
7433 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7434 "got-new-staged-content");
7435 if (err)
7436 goto done;
7438 if (fseek(f1, 0L, SEEK_SET) == -1) {
7439 err = got_ferror(f1, GOT_ERR_IO);
7440 goto done;
7442 if (fseek(f2, 0L, SEEK_SET) == -1) {
7443 err = got_ferror(f2, GOT_ERR_IO);
7444 goto done;
7446 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7447 int choice;
7448 err = apply_or_reject_change(&choice, change, ++n,
7449 changes->nchanges, ds, args, diff_flags, relpath,
7450 f1, f2, &line_cur1, &line_cur2,
7451 outfile, rejectfile, patch_cb, patch_arg);
7452 if (err)
7453 goto done;
7454 if (choice == GOT_PATCH_CHOICE_YES)
7455 have_content = 1;
7456 else
7457 have_rejected_content = 1;
7458 if (choice == GOT_PATCH_CHOICE_QUIT)
7459 break;
7461 if (have_content || have_rejected_content)
7462 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7463 outfile, rejectfile);
7464 done:
7465 free(label1);
7466 if (blob)
7467 got_object_blob_close(blob);
7468 if (staged_blob)
7469 got_object_blob_close(staged_blob);
7470 if (f1 && fclose(f1) == EOF && err == NULL)
7471 err = got_error_from_errno2("fclose", path1);
7472 if (f2 && fclose(f2) == EOF && err == NULL)
7473 err = got_error_from_errno2("fclose", path2);
7474 if (outfile && fclose(outfile) == EOF && err == NULL)
7475 err = got_error_from_errno2("fclose", *path_unstaged_content);
7476 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7477 err = got_error_from_errno2("fclose", *path_new_staged_content);
7478 if (path1 && unlink(path1) == -1 && err == NULL)
7479 err = got_error_from_errno2("unlink", path1);
7480 if (path2 && unlink(path2) == -1 && err == NULL)
7481 err = got_error_from_errno2("unlink", path2);
7482 if (err || !have_content) {
7483 if (*path_unstaged_content &&
7484 unlink(*path_unstaged_content) == -1 && err == NULL)
7485 err = got_error_from_errno2("unlink",
7486 *path_unstaged_content);
7487 free(*path_unstaged_content);
7488 *path_unstaged_content = NULL;
7490 if (err || !have_content || !have_rejected_content) {
7491 if (*path_new_staged_content &&
7492 unlink(*path_new_staged_content) == -1 && err == NULL)
7493 err = got_error_from_errno2("unlink",
7494 *path_new_staged_content);
7495 free(*path_new_staged_content);
7496 *path_new_staged_content = NULL;
7498 free(args);
7499 if (ds) {
7500 got_diff_state_free(ds);
7501 free(ds);
7503 if (changes)
7504 got_diff_free_changes(changes);
7505 free(path1);
7506 free(path2);
7507 return err;
7510 static const struct got_error *
7511 unstage_hunks(struct got_object_id *staged_blob_id,
7512 struct got_blob_object *blob_base,
7513 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7514 const char *ondisk_path, const char *label_orig,
7515 struct got_worktree *worktree, struct got_repository *repo,
7516 got_worktree_patch_cb patch_cb, void *patch_arg,
7517 got_worktree_checkout_cb progress_cb, void *progress_arg)
7519 const struct got_error *err = NULL;
7520 char *path_unstaged_content = NULL;
7521 char *path_new_staged_content = NULL;
7522 struct got_object_id *new_staged_blob_id = NULL;
7523 FILE *f = NULL;
7524 struct stat sb;
7526 err = create_unstaged_content(&path_unstaged_content,
7527 &path_new_staged_content, blob_id, staged_blob_id,
7528 ie->path, repo, patch_cb, patch_arg);
7529 if (err)
7530 return err;
7532 if (path_unstaged_content == NULL)
7533 return NULL;
7535 if (path_new_staged_content) {
7536 err = got_object_blob_create(&new_staged_blob_id,
7537 path_new_staged_content, repo);
7538 if (err)
7539 goto done;
7542 f = fopen(path_unstaged_content, "r");
7543 if (f == NULL) {
7544 err = got_error_from_errno2("fopen",
7545 path_unstaged_content);
7546 goto done;
7548 if (fstat(fileno(f), &sb) == -1) {
7549 err = got_error_from_errno2("fstat", path_unstaged_content);
7550 goto done;
7552 if (got_fileindex_entry_staged_filetype_get(ie) ==
7553 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7554 char link_target[PATH_MAX];
7555 size_t r;
7556 r = fread(link_target, 1, sizeof(link_target), f);
7557 if (r == 0 && ferror(f)) {
7558 err = got_error_from_errno("fread");
7559 goto done;
7561 if (r >= sizeof(link_target)) { /* should not happen */
7562 err = got_error(GOT_ERR_NO_SPACE);
7563 goto done;
7565 link_target[r] = '\0';
7566 err = merge_symlink(worktree, blob_base,
7567 ondisk_path, ie->path, label_orig, link_target,
7568 worktree->base_commit_id, repo, progress_cb,
7569 progress_arg);
7570 } else {
7571 int local_changes_subsumed;
7572 err = merge_file(&local_changes_subsumed, worktree,
7573 blob_base, ondisk_path, ie->path,
7574 got_fileindex_perms_to_st(ie),
7575 path_unstaged_content, label_orig, "unstaged",
7576 repo, progress_cb, progress_arg);
7578 if (err)
7579 goto done;
7581 if (new_staged_blob_id) {
7582 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7583 SHA1_DIGEST_LENGTH);
7584 } else
7585 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7586 done:
7587 free(new_staged_blob_id);
7588 if (path_unstaged_content &&
7589 unlink(path_unstaged_content) == -1 && err == NULL)
7590 err = got_error_from_errno2("unlink", path_unstaged_content);
7591 if (path_new_staged_content &&
7592 unlink(path_new_staged_content) == -1 && err == NULL)
7593 err = got_error_from_errno2("unlink", path_new_staged_content);
7594 if (f && fclose(f) != 0 && err == NULL)
7595 err = got_error_from_errno2("fclose", path_unstaged_content);
7596 free(path_unstaged_content);
7597 free(path_new_staged_content);
7598 return err;
7601 static const struct got_error *
7602 unstage_path(void *arg, unsigned char status,
7603 unsigned char staged_status, const char *relpath,
7604 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7605 struct got_object_id *commit_id, int dirfd, const char *de_name)
7607 const struct got_error *err = NULL;
7608 struct unstage_path_arg *a = arg;
7609 struct got_fileindex_entry *ie;
7610 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7611 char *ondisk_path = NULL;
7612 char *id_str = NULL, *label_orig = NULL;
7613 int local_changes_subsumed;
7614 struct stat sb;
7616 if (staged_status != GOT_STATUS_ADD &&
7617 staged_status != GOT_STATUS_MODIFY &&
7618 staged_status != GOT_STATUS_DELETE)
7619 return NULL;
7621 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7622 if (ie == NULL)
7623 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7625 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7626 == -1)
7627 return got_error_from_errno("asprintf");
7629 err = got_object_id_str(&id_str,
7630 commit_id ? commit_id : a->worktree->base_commit_id);
7631 if (err)
7632 goto done;
7633 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7634 id_str) == -1) {
7635 err = got_error_from_errno("asprintf");
7636 goto done;
7639 switch (staged_status) {
7640 case GOT_STATUS_MODIFY:
7641 err = got_object_open_as_blob(&blob_base, a->repo,
7642 blob_id, 8192);
7643 if (err)
7644 break;
7645 /* fall through */
7646 case GOT_STATUS_ADD:
7647 if (a->patch_cb) {
7648 if (staged_status == GOT_STATUS_ADD) {
7649 int choice = GOT_PATCH_CHOICE_NONE;
7650 err = (*a->patch_cb)(&choice, a->patch_arg,
7651 staged_status, ie->path, NULL, 1, 1);
7652 if (err)
7653 break;
7654 if (choice != GOT_PATCH_CHOICE_YES)
7655 break;
7656 } else {
7657 err = unstage_hunks(staged_blob_id,
7658 blob_base, blob_id, ie, ondisk_path,
7659 label_orig, a->worktree, a->repo,
7660 a->patch_cb, a->patch_arg,
7661 a->progress_cb, a->progress_arg);
7662 break; /* Done with this file. */
7665 err = got_object_open_as_blob(&blob_staged, a->repo,
7666 staged_blob_id, 8192);
7667 if (err)
7668 break;
7669 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7670 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7671 case GOT_FILEIDX_MODE_REGULAR_FILE:
7672 err = merge_blob(&local_changes_subsumed, a->worktree,
7673 blob_base, ondisk_path, relpath,
7674 got_fileindex_perms_to_st(ie), label_orig,
7675 blob_staged, commit_id ? commit_id :
7676 a->worktree->base_commit_id, a->repo,
7677 a->progress_cb, a->progress_arg);
7678 break;
7679 case GOT_FILEIDX_MODE_SYMLINK:
7680 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7681 char *staged_target;
7682 err = got_object_blob_read_to_str(
7683 &staged_target, blob_staged);
7684 if (err)
7685 goto done;
7686 err = merge_symlink(a->worktree, blob_base,
7687 ondisk_path, relpath, label_orig,
7688 staged_target, commit_id ? commit_id :
7689 a->worktree->base_commit_id,
7690 a->repo, a->progress_cb, a->progress_arg);
7691 free(staged_target);
7692 } else {
7693 err = merge_blob(&local_changes_subsumed,
7694 a->worktree, blob_base, ondisk_path,
7695 relpath, got_fileindex_perms_to_st(ie),
7696 label_orig, blob_staged,
7697 commit_id ? commit_id :
7698 a->worktree->base_commit_id, a->repo,
7699 a->progress_cb, a->progress_arg);
7701 break;
7702 default:
7703 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7704 break;
7706 if (err == NULL)
7707 got_fileindex_entry_stage_set(ie,
7708 GOT_FILEIDX_STAGE_NONE);
7709 break;
7710 case GOT_STATUS_DELETE:
7711 if (a->patch_cb) {
7712 int choice = GOT_PATCH_CHOICE_NONE;
7713 err = (*a->patch_cb)(&choice, a->patch_arg,
7714 staged_status, ie->path, NULL, 1, 1);
7715 if (err)
7716 break;
7717 if (choice == GOT_PATCH_CHOICE_NO)
7718 break;
7719 if (choice != GOT_PATCH_CHOICE_YES) {
7720 err = got_error(GOT_ERR_PATCH_CHOICE);
7721 break;
7724 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7725 err = get_file_status(&status, &sb, ie, ondisk_path,
7726 dirfd, de_name, a->repo);
7727 if (err)
7728 break;
7729 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7730 break;
7732 done:
7733 free(ondisk_path);
7734 if (blob_base)
7735 got_object_blob_close(blob_base);
7736 if (blob_staged)
7737 got_object_blob_close(blob_staged);
7738 free(id_str);
7739 free(label_orig);
7740 return err;
7743 const struct got_error *
7744 got_worktree_unstage(struct got_worktree *worktree,
7745 struct got_pathlist_head *paths,
7746 got_worktree_checkout_cb progress_cb, void *progress_arg,
7747 got_worktree_patch_cb patch_cb, void *patch_arg,
7748 struct got_repository *repo)
7750 const struct got_error *err = NULL, *sync_err, *unlockerr;
7751 struct got_pathlist_entry *pe;
7752 struct got_fileindex *fileindex = NULL;
7753 char *fileindex_path = NULL;
7754 struct unstage_path_arg upa;
7756 err = lock_worktree(worktree, LOCK_EX);
7757 if (err)
7758 return err;
7760 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7761 if (err)
7762 goto done;
7764 upa.worktree = worktree;
7765 upa.fileindex = fileindex;
7766 upa.repo = repo;
7767 upa.progress_cb = progress_cb;
7768 upa.progress_arg = progress_arg;
7769 upa.patch_cb = patch_cb;
7770 upa.patch_arg = patch_arg;
7771 TAILQ_FOREACH(pe, paths, entry) {
7772 err = worktree_status(worktree, pe->path, fileindex, repo,
7773 unstage_path, &upa, NULL, NULL, 0, 0);
7774 if (err)
7775 goto done;
7778 sync_err = sync_fileindex(fileindex, fileindex_path);
7779 if (sync_err && err == NULL)
7780 err = sync_err;
7781 done:
7782 free(fileindex_path);
7783 if (fileindex)
7784 got_fileindex_free(fileindex);
7785 unlockerr = lock_worktree(worktree, LOCK_SH);
7786 if (unlockerr && err == NULL)
7787 err = unlockerr;
7788 return err;
7791 struct report_file_info_arg {
7792 struct got_worktree *worktree;
7793 got_worktree_path_info_cb info_cb;
7794 void *info_arg;
7795 struct got_pathlist_head *paths;
7796 got_cancel_cb cancel_cb;
7797 void *cancel_arg;
7800 static const struct got_error *
7801 report_file_info(void *arg, struct got_fileindex_entry *ie)
7803 struct report_file_info_arg *a = arg;
7804 struct got_pathlist_entry *pe;
7805 struct got_object_id blob_id, staged_blob_id, commit_id;
7806 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7807 struct got_object_id *commit_idp = NULL;
7808 int stage;
7810 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7811 return got_error(GOT_ERR_CANCELLED);
7813 TAILQ_FOREACH(pe, a->paths, entry) {
7814 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7815 got_path_is_child(ie->path, pe->path, pe->path_len))
7816 break;
7818 if (pe == NULL) /* not found */
7819 return NULL;
7821 if (got_fileindex_entry_has_blob(ie)) {
7822 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7823 blob_idp = &blob_id;
7825 stage = got_fileindex_entry_stage_get(ie);
7826 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7827 stage == GOT_FILEIDX_STAGE_ADD) {
7828 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7829 SHA1_DIGEST_LENGTH);
7830 staged_blob_idp = &staged_blob_id;
7833 if (got_fileindex_entry_has_commit(ie)) {
7834 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7835 commit_idp = &commit_id;
7838 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7839 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7842 const struct got_error *
7843 got_worktree_path_info(struct got_worktree *worktree,
7844 struct got_pathlist_head *paths,
7845 got_worktree_path_info_cb info_cb, void *info_arg,
7846 got_cancel_cb cancel_cb, void *cancel_arg)
7849 const struct got_error *err = NULL, *unlockerr;
7850 struct got_fileindex *fileindex = NULL;
7851 char *fileindex_path = NULL;
7852 struct report_file_info_arg arg;
7854 err = lock_worktree(worktree, LOCK_SH);
7855 if (err)
7856 return err;
7858 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7859 if (err)
7860 goto done;
7862 arg.worktree = worktree;
7863 arg.info_cb = info_cb;
7864 arg.info_arg = info_arg;
7865 arg.paths = paths;
7866 arg.cancel_cb = cancel_cb;
7867 arg.cancel_arg = cancel_arg;
7868 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7869 &arg);
7870 done:
7871 free(fileindex_path);
7872 if (fileindex)
7873 got_fileindex_free(fileindex);
7874 unlockerr = lock_worktree(worktree, LOCK_UN);
7875 if (unlockerr && err == NULL)
7876 err = unlockerr;
7877 return err;