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;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 parent = dirname(ondisk_path);
792 if (parent == NULL)
793 return got_error_from_errno2("dirname", ondisk_path);
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
796 return got_error_from_errno("asprintf");
798 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
799 if (err)
800 goto done;
802 free(base_path);
803 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
804 err = got_error_from_errno("asprintf");
805 base_path = NULL;
806 goto done;
809 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
810 if (err)
811 goto done;
812 if (blob_orig) {
813 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
814 blob_orig);
815 if (err)
816 goto done;
817 } else {
818 /*
819 * If the file has no blob, this is an "add vs add" conflict,
820 * and we simply use an empty ancestor file to make both files
821 * appear in the merged result in their entirety.
822 */
825 /*
826 * In order the run a 3-way merge with a symlink we copy the symlink's
827 * target path into a temporary file and use that file with diff3.
828 */
829 if (S_ISLNK(st_mode)) {
830 char target_path[PATH_MAX];
831 ssize_t target_len;
832 size_t n;
834 free(base_path);
835 if (asprintf(&base_path, "%s/got-symlink-merge",
836 parent) == -1) {
837 err = got_error_from_errno("asprintf");
838 base_path = NULL;
839 goto done;
841 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
842 if (err)
843 goto done;
844 target_len = readlink(ondisk_path, target_path,
845 sizeof(target_path));
846 if (target_len == -1) {
847 err = got_error_from_errno2("readlink", ondisk_path);
848 goto done;
850 n = fwrite(target_path, 1, target_len, symlinkf);
851 if (n != target_len) {
852 err = got_ferror(symlinkf, GOT_ERR_IO);
853 goto done;
855 if (fflush(symlinkf) == EOF) {
856 err = got_error_from_errno2("fflush", symlink_path);
857 goto done;
861 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
862 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
863 label_deriv, label_orig, NULL);
864 if (err)
865 goto done;
867 err = (*progress_cb)(progress_arg,
868 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
869 if (err)
870 goto done;
872 if (fsync(merged_fd) != 0) {
873 err = got_error_from_errno("fsync");
874 goto done;
877 /* Check if a clean merge has subsumed all local changes. */
878 if (overlapcnt == 0) {
879 err = check_files_equal(local_changes_subsumed, deriv_path,
880 merged_path);
881 if (err)
882 goto done;
885 if (fchmod(merged_fd, st_mode) != 0) {
886 err = got_error_from_errno2("fchmod", merged_path);
887 goto done;
890 if (rename(merged_path, ondisk_path) != 0) {
891 err = got_error_from_errno3("rename", merged_path,
892 ondisk_path);
893 goto done;
895 done:
896 if (err) {
897 if (merged_path)
898 unlink(merged_path);
900 if (symlink_path) {
901 if (unlink(symlink_path) == -1 && err == NULL)
902 err = got_error_from_errno2("unlink", symlink_path);
904 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
905 err = got_error_from_errno2("fclose", symlink_path);
906 free(symlink_path);
907 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
908 err = got_error_from_errno("close");
909 if (f_orig && fclose(f_orig) != 0 && err == NULL)
910 err = got_error_from_errno("fclose");
911 free(merged_path);
912 free(base_path);
913 if (blob_orig_path) {
914 unlink(blob_orig_path);
915 free(blob_orig_path);
917 return err;
920 static const struct got_error *
921 update_symlink(const char *ondisk_path, const char *target_path,
922 size_t target_len)
924 /* This is not atomic but matches what 'ln -sf' does. */
925 if (unlink(ondisk_path) == -1)
926 return got_error_from_errno2("unlink", ondisk_path);
927 if (symlink(target_path, ondisk_path) == -1)
928 return got_error_from_errno3("symlink", target_path,
929 ondisk_path);
930 return NULL;
933 /*
934 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
935 * in the work tree with a file that contains conflict markers and the
936 * conflicting target paths of the original version, a "derived version"
937 * of a symlink from an incoming change, and a local version of the symlink.
939 * The original versions's target path can be NULL if it is not available,
940 * such as if both derived versions added a new symlink at the same path.
942 * The incoming derived symlink target is NULL in case the incoming change
943 * has deleted this symlink.
944 */
945 static const struct got_error *
946 install_symlink_conflict(const char *deriv_target,
947 struct got_object_id *deriv_base_commit_id, const char *orig_target,
948 const char *label_orig, const char *local_target, const char *ondisk_path)
950 const struct got_error *err;
951 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
952 FILE *f = NULL;
954 err = got_object_id_str(&id_str, deriv_base_commit_id);
955 if (err)
956 return got_error_from_errno("asprintf");
958 if (asprintf(&label_deriv, "%s: commit %s",
959 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
960 err = got_error_from_errno("asprintf");
961 goto done;
964 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
965 if (err)
966 goto done;
968 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
969 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
970 deriv_target ? deriv_target : "(symlink was deleted)",
971 orig_target ? label_orig : "",
972 orig_target ? "\n" : "",
973 orig_target ? orig_target : "",
974 orig_target ? "\n" : "",
975 GOT_DIFF_CONFLICT_MARKER_SEP,
976 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
977 err = got_error_from_errno2("fprintf", path);
978 goto done;
981 if (unlink(ondisk_path) == -1) {
982 err = got_error_from_errno2("unlink", ondisk_path);
983 goto done;
985 if (rename(path, ondisk_path) == -1) {
986 err = got_error_from_errno3("rename", path, ondisk_path);
987 goto done;
989 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
990 err = got_error_from_errno2("chmod", ondisk_path);
991 goto done;
993 done:
994 if (f != NULL && fclose(f) == EOF && err == NULL)
995 err = got_error_from_errno2("fclose", path);
996 free(path);
997 free(id_str);
998 free(label_deriv);
999 return err;
1002 /* forward declaration */
1003 static const struct got_error *
1004 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1005 const char *, const char *, uint16_t, const char *,
1006 struct got_blob_object *, struct got_object_id *,
1007 struct got_repository *, got_worktree_checkout_cb, void *);
1010 * Merge a symlink into the work tree, where blob_orig acts as the common
1011 * ancestor, deriv_target is the link target of the first derived version,
1012 * and the symlink on disk acts as the second derived version.
1013 * Assume that contents of both blobs represent symlinks.
1015 static const struct got_error *
1016 merge_symlink(struct got_worktree *worktree,
1017 struct got_blob_object *blob_orig, const char *ondisk_path,
1018 const char *path, const char *label_orig, const char *deriv_target,
1019 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1020 got_worktree_checkout_cb progress_cb, void *progress_arg)
1022 const struct got_error *err = NULL;
1023 char *ancestor_target = NULL;
1024 struct stat sb;
1025 ssize_t ondisk_len, deriv_len;
1026 char ondisk_target[PATH_MAX];
1027 int have_local_change = 0;
1028 int have_incoming_change = 0;
1030 if (lstat(ondisk_path, &sb) == -1)
1031 return got_error_from_errno2("lstat", ondisk_path);
1033 ondisk_len = readlink(ondisk_path, ondisk_target,
1034 sizeof(ondisk_target));
1035 if (ondisk_len == -1) {
1036 err = got_error_from_errno2("readlink",
1037 ondisk_path);
1038 goto done;
1040 ondisk_target[ondisk_len] = '\0';
1042 if (blob_orig) {
1043 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1044 if (err)
1045 goto done;
1048 if (ancestor_target == NULL ||
1049 (ondisk_len != strlen(ancestor_target) ||
1050 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1051 have_local_change = 1;
1053 deriv_len = strlen(deriv_target);
1054 if (ancestor_target == NULL ||
1055 (deriv_len != strlen(ancestor_target) ||
1056 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1057 have_incoming_change = 1;
1059 if (!have_local_change && !have_incoming_change) {
1060 if (ancestor_target) {
1061 /* Both sides made the same change. */
1062 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1063 path);
1064 } else if (deriv_len == ondisk_len &&
1065 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1066 /* Both sides added the same symlink. */
1067 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1068 path);
1069 } else {
1070 /* Both sides added symlinks which don't match. */
1071 err = install_symlink_conflict(deriv_target,
1072 deriv_base_commit_id, ancestor_target,
1073 label_orig, ondisk_target, ondisk_path);
1074 if (err)
1075 goto done;
1076 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1077 path);
1079 } else if (!have_local_change && have_incoming_change) {
1080 /* Apply the incoming change. */
1081 err = update_symlink(ondisk_path, deriv_target,
1082 strlen(deriv_target));
1083 if (err)
1084 goto done;
1085 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1086 } else if (have_local_change && have_incoming_change) {
1087 if (deriv_len == ondisk_len &&
1088 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1089 /* Both sides made the same change. */
1090 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1091 path);
1092 } else {
1093 err = install_symlink_conflict(deriv_target,
1094 deriv_base_commit_id, ancestor_target, label_orig,
1095 ondisk_target, ondisk_path);
1096 if (err)
1097 goto done;
1098 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1099 path);
1103 done:
1104 free(ancestor_target);
1105 return err;
1109 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1110 * blob_deriv acts as the first derived version, and the file on disk
1111 * acts as the second derived version.
1113 static const struct got_error *
1114 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1115 struct got_blob_object *blob_orig, const char *ondisk_path,
1116 const char *path, uint16_t st_mode, const char *label_orig,
1117 struct got_blob_object *blob_deriv,
1118 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1119 got_worktree_checkout_cb progress_cb, void *progress_arg)
1121 const struct got_error *err = NULL;
1122 FILE *f_deriv = NULL;
1123 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1124 char *label_deriv = NULL, *parent;
1126 *local_changes_subsumed = 0;
1128 parent = dirname(ondisk_path);
1129 if (parent == NULL)
1130 return got_error_from_errno2("dirname", ondisk_path);
1132 free(base_path);
1133 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1134 err = got_error_from_errno("asprintf");
1135 base_path = NULL;
1136 goto done;
1139 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1140 if (err)
1141 goto done;
1142 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1143 blob_deriv);
1144 if (err)
1145 goto done;
1147 err = got_object_id_str(&id_str, deriv_base_commit_id);
1148 if (err)
1149 goto done;
1150 if (asprintf(&label_deriv, "%s: commit %s",
1151 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1152 err = got_error_from_errno("asprintf");
1153 goto done;
1156 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1157 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1158 label_deriv, repo, progress_cb, progress_arg);
1159 done:
1160 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1161 err = got_error_from_errno("fclose");
1162 free(base_path);
1163 if (blob_deriv_path) {
1164 unlink(blob_deriv_path);
1165 free(blob_deriv_path);
1167 free(id_str);
1168 free(label_deriv);
1169 return err;
1172 static const struct got_error *
1173 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1174 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1175 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1177 const struct got_error *err = NULL;
1178 struct got_fileindex_entry *new_ie;
1180 *new_iep = NULL;
1182 err = got_fileindex_entry_alloc(&new_ie, path);
1183 if (err)
1184 return err;
1186 err = got_fileindex_entry_update(new_ie, ondisk_path,
1187 blob_id->sha1, base_commit_id->sha1, 1);
1188 if (err)
1189 goto done;
1191 err = got_fileindex_entry_add(fileindex, new_ie);
1192 done:
1193 if (err)
1194 got_fileindex_entry_free(new_ie);
1195 else
1196 *new_iep = new_ie;
1197 return err;
1200 static mode_t
1201 get_ondisk_perms(int executable, mode_t st_mode)
1203 mode_t xbits = S_IXUSR;
1205 if (executable) {
1206 /* Map read bits to execute bits. */
1207 if (st_mode & S_IRGRP)
1208 xbits |= S_IXGRP;
1209 if (st_mode & S_IROTH)
1210 xbits |= S_IXOTH;
1211 return st_mode | xbits;
1214 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1217 /* forward declaration */
1218 static const struct got_error *
1219 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1220 const char *path, mode_t te_mode, mode_t st_mode,
1221 struct got_blob_object *blob, int restoring_missing_file,
1222 int reverting_versioned_file, int installing_bad_symlink,
1223 int path_is_unversioned, struct got_repository *repo,
1224 got_worktree_checkout_cb progress_cb, void *progress_arg);
1227 * This function assumes that the provided symlink target points at a
1228 * safe location in the work tree!
1230 static const struct got_error *
1231 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1232 size_t target_len)
1234 const struct got_error *err = NULL;
1235 ssize_t elen;
1236 char etarget[PATH_MAX];
1237 int fd;
1240 * "Bad" symlinks (those pointing outside the work tree or into the
1241 * .got directory) are installed in the work tree as a regular file
1242 * which contains the bad symlink target path.
1243 * The new symlink target has already been checked for safety by our
1244 * caller. If we can successfully open a regular file then we simply
1245 * replace this file with a symlink below.
1247 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1248 if (fd == -1) {
1249 if (errno != ELOOP)
1250 return got_error_from_errno2("open", ondisk_path);
1252 /* We are updating an existing on-disk symlink. */
1253 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1254 if (elen == -1)
1255 return got_error_from_errno2("readlink", ondisk_path);
1257 if (elen == target_len &&
1258 memcmp(etarget, target_path, target_len) == 0)
1259 return NULL; /* nothing to do */
1262 err = update_symlink(ondisk_path, target_path, target_len);
1263 if (fd != -1 && close(fd) == -1 && err == NULL)
1264 err = got_error_from_errno2("close", ondisk_path);
1265 return err;
1268 static const struct got_error *
1269 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1270 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1272 const struct got_error *err = NULL;
1273 char canonpath[PATH_MAX];
1274 char *path_got = NULL;
1276 *is_bad_symlink = 0;
1278 if (target_len >= sizeof(canonpath)) {
1279 *is_bad_symlink = 1;
1280 return NULL;
1284 * We do not use realpath(3) to resolve the symlink's target
1285 * path because we don't want to resolve symlinks recursively.
1286 * Instead we make the path absolute and then canonicalize it.
1287 * Relative symlink target lookup should begin at the directory
1288 * in which the blob object is being installed.
1290 if (!got_path_is_absolute(target_path)) {
1291 char *abspath;
1292 char *parent = dirname(ondisk_path);
1293 if (parent == NULL)
1294 return got_error_from_errno2("dirname", ondisk_path);
1295 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1296 return got_error_from_errno("asprintf");
1297 if (strlen(abspath) >= sizeof(canonpath)) {
1298 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1299 free(abspath);
1300 return err;
1302 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1303 free(abspath);
1304 if (err)
1305 return err;
1306 } else {
1307 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1308 if (err)
1309 return err;
1312 /* Only allow symlinks pointing at paths within the work tree. */
1313 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1314 *is_bad_symlink = 1;
1315 return NULL;
1318 /* Do not allow symlinks pointing into the .got directory. */
1319 if (asprintf(&path_got, "%s/%s", wtroot_path,
1320 GOT_WORKTREE_GOT_DIR) == -1)
1321 return got_error_from_errno("asprintf");
1322 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1323 *is_bad_symlink = 1;
1325 free(path_got);
1326 return NULL;
1329 static const struct got_error *
1330 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1331 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1332 int restoring_missing_file, int reverting_versioned_file,
1333 int path_is_unversioned, struct got_repository *repo,
1334 got_worktree_checkout_cb progress_cb, void *progress_arg)
1336 const struct got_error *err = NULL;
1337 char target_path[PATH_MAX];
1338 size_t len, target_len = 0;
1339 char *path_got = NULL;
1340 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1341 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1343 *is_bad_symlink = 0;
1346 * Blob object content specifies the target path of the link.
1347 * If a symbolic link cannot be installed we instead create
1348 * a regular file which contains the link target path stored
1349 * in the blob object.
1351 do {
1352 err = got_object_blob_read_block(&len, blob);
1353 if (len + target_len >= sizeof(target_path)) {
1354 /* Path too long; install as a regular file. */
1355 *is_bad_symlink = 1;
1356 got_object_blob_rewind(blob);
1357 return install_blob(worktree, ondisk_path, path,
1358 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1359 restoring_missing_file, reverting_versioned_file,
1360 1, path_is_unversioned, repo, progress_cb,
1361 progress_arg);
1363 if (len > 0) {
1364 /* Skip blob object header first time around. */
1365 memcpy(target_path + target_len, buf + hdrlen,
1366 len - hdrlen);
1367 target_len += len - hdrlen;
1368 hdrlen = 0;
1370 } while (len != 0);
1371 target_path[target_len] = '\0';
1373 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1374 ondisk_path, worktree->root_path);
1375 if (err)
1376 return err;
1378 if (*is_bad_symlink) {
1379 /* install as a regular file */
1380 *is_bad_symlink = 1;
1381 got_object_blob_rewind(blob);
1382 err = install_blob(worktree, ondisk_path, path,
1383 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 restoring_missing_file, reverting_versioned_file, 1,
1385 path_is_unversioned, repo, progress_cb, progress_arg);
1386 goto done;
1389 if (symlink(target_path, ondisk_path) == -1) {
1390 if (errno == EEXIST) {
1391 if (path_is_unversioned) {
1392 err = (*progress_cb)(progress_arg,
1393 GOT_STATUS_UNVERSIONED, path);
1394 goto done;
1396 err = replace_existing_symlink(ondisk_path,
1397 target_path, target_len);
1398 if (err)
1399 goto done;
1400 if (progress_cb) {
1401 err = (*progress_cb)(progress_arg,
1402 reverting_versioned_file ?
1403 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1404 path);
1406 goto done; /* Nothing else to do. */
1409 if (errno == ENOENT) {
1410 char *parent = dirname(ondisk_path);
1411 if (parent == NULL) {
1412 err = got_error_from_errno2("dirname",
1413 ondisk_path);
1414 goto done;
1416 err = add_dir_on_disk(worktree, parent);
1417 if (err)
1418 goto done;
1420 * Retry, and fall through to error handling
1421 * below if this second attempt fails.
1423 if (symlink(target_path, ondisk_path) != -1) {
1424 err = NULL; /* success */
1425 goto done;
1429 /* Handle errors from first or second creation attempt. */
1430 if (errno == ENAMETOOLONG) {
1431 /* bad target path; install as a regular file */
1432 *is_bad_symlink = 1;
1433 got_object_blob_rewind(blob);
1434 err = install_blob(worktree, ondisk_path, path,
1435 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1436 restoring_missing_file, reverting_versioned_file, 1,
1437 path_is_unversioned, repo,
1438 progress_cb, progress_arg);
1439 } else if (errno == ENOTDIR) {
1440 err = got_error_path(ondisk_path,
1441 GOT_ERR_FILE_OBSTRUCTED);
1442 } else {
1443 err = got_error_from_errno3("symlink",
1444 target_path, ondisk_path);
1446 } else if (progress_cb)
1447 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1448 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1449 done:
1450 free(path_got);
1451 return err;
1454 static const struct got_error *
1455 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1456 const char *path, mode_t te_mode, mode_t st_mode,
1457 struct got_blob_object *blob, int restoring_missing_file,
1458 int reverting_versioned_file, int installing_bad_symlink,
1459 int path_is_unversioned, struct got_repository *repo,
1460 got_worktree_checkout_cb progress_cb, void *progress_arg)
1462 const struct got_error *err = NULL;
1463 int fd = -1;
1464 size_t len, hdrlen;
1465 int update = 0;
1466 char *tmppath = NULL;
1468 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1469 GOT_DEFAULT_FILE_MODE);
1470 if (fd == -1) {
1471 if (errno == ENOENT) {
1472 char *parent = dirname(path);
1473 if (parent == NULL)
1474 return got_error_from_errno2("dirname", path);
1475 err = add_dir_on_disk(worktree, parent);
1476 if (err)
1477 return err;
1478 fd = open(ondisk_path,
1479 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1480 GOT_DEFAULT_FILE_MODE);
1481 if (fd == -1)
1482 return got_error_from_errno2("open",
1483 ondisk_path);
1484 } else if (errno == EEXIST) {
1485 if (path_is_unversioned) {
1486 err = (*progress_cb)(progress_arg,
1487 GOT_STATUS_UNVERSIONED, path);
1488 goto done;
1490 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1491 /* TODO file is obstructed; do something */
1492 err = got_error_path(ondisk_path,
1493 GOT_ERR_FILE_OBSTRUCTED);
1494 goto done;
1495 } else {
1496 err = got_opentemp_named_fd(&tmppath, &fd,
1497 ondisk_path);
1498 if (err)
1499 goto done;
1500 update = 1;
1502 } else
1503 return got_error_from_errno2("open", ondisk_path);
1506 if (progress_cb) {
1507 if (restoring_missing_file)
1508 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1509 path);
1510 else if (reverting_versioned_file)
1511 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1512 path);
1513 else
1514 err = (*progress_cb)(progress_arg,
1515 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1516 if (err)
1517 goto done;
1520 hdrlen = got_object_blob_get_hdrlen(blob);
1521 do {
1522 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1523 err = got_object_blob_read_block(&len, blob);
1524 if (err)
1525 break;
1526 if (len > 0) {
1527 /* Skip blob object header first time around. */
1528 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1529 if (outlen == -1) {
1530 err = got_error_from_errno("write");
1531 goto done;
1532 } else if (outlen != len - hdrlen) {
1533 err = got_error(GOT_ERR_IO);
1534 goto done;
1536 hdrlen = 0;
1538 } while (len != 0);
1540 if (fsync(fd) != 0) {
1541 err = got_error_from_errno("fsync");
1542 goto done;
1545 if (update) {
1546 if (rename(tmppath, ondisk_path) != 0) {
1547 err = got_error_from_errno3("rename", tmppath,
1548 ondisk_path);
1549 unlink(tmppath);
1550 goto done;
1554 if (chmod(ondisk_path,
1555 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 err = got_error_from_errno2("chmod", ondisk_path);
1557 goto done;
1560 done:
1561 if (fd != -1 && close(fd) != 0 && err == NULL)
1562 err = got_error_from_errno("close");
1563 free(tmppath);
1564 return err;
1567 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1568 static const struct got_error *
1569 get_modified_file_content_status(unsigned char *status, FILE *f)
1571 const struct got_error *err = NULL;
1572 const char *markers[3] = {
1573 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1574 GOT_DIFF_CONFLICT_MARKER_SEP,
1575 GOT_DIFF_CONFLICT_MARKER_END
1577 int i = 0;
1578 char *line;
1579 size_t len;
1580 const char delim[3] = {'\0', '\0', '\0'};
1582 while (*status == GOT_STATUS_MODIFY) {
1583 line = fparseln(f, &len, NULL, delim, 0);
1584 if (line == NULL) {
1585 if (feof(f))
1586 break;
1587 err = got_ferror(f, GOT_ERR_IO);
1588 break;
1591 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1592 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1593 == 0)
1594 *status = GOT_STATUS_CONFLICT;
1595 else
1596 i++;
1600 return err;
1603 static int
1604 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1606 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1607 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1610 static int
1611 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1613 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1614 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1615 ie->mtime_sec == sb->st_mtim.tv_sec &&
1616 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1617 ie->size == (sb->st_size & 0xffffffff) &&
1618 !xbit_differs(ie, sb->st_mode));
1621 static unsigned char
1622 get_staged_status(struct got_fileindex_entry *ie)
1624 switch (got_fileindex_entry_stage_get(ie)) {
1625 case GOT_FILEIDX_STAGE_ADD:
1626 return GOT_STATUS_ADD;
1627 case GOT_FILEIDX_STAGE_DELETE:
1628 return GOT_STATUS_DELETE;
1629 case GOT_FILEIDX_STAGE_MODIFY:
1630 return GOT_STATUS_MODIFY;
1631 default:
1632 return GOT_STATUS_NO_CHANGE;
1636 static const struct got_error *
1637 get_symlink_modification_status(unsigned char *status,
1638 struct got_fileindex_entry *ie, const char *abspath,
1639 int dirfd, const char *de_name, struct got_blob_object *blob)
1641 const struct got_error *err = NULL;
1642 char target_path[PATH_MAX];
1643 char etarget[PATH_MAX];
1644 ssize_t elen;
1645 size_t len, target_len = 0;
1646 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1647 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1649 *status = GOT_STATUS_NO_CHANGE;
1651 /* Blob object content specifies the target path of the link. */
1652 do {
1653 err = got_object_blob_read_block(&len, blob);
1654 if (err)
1655 return err;
1656 if (len + target_len >= sizeof(target_path)) {
1658 * Should not happen. The blob contents were OK
1659 * when this symlink was installed.
1661 return got_error(GOT_ERR_NO_SPACE);
1663 if (len > 0) {
1664 /* Skip blob object header first time around. */
1665 memcpy(target_path + target_len, buf + hdrlen,
1666 len - hdrlen);
1667 target_len += len - hdrlen;
1668 hdrlen = 0;
1670 } while (len != 0);
1671 target_path[target_len] = '\0';
1673 if (dirfd != -1) {
1674 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1675 if (elen == -1)
1676 return got_error_from_errno2("readlinkat", abspath);
1677 } else {
1678 elen = readlink(abspath, etarget, sizeof(etarget));
1679 if (elen == -1)
1680 return got_error_from_errno2("readlink", abspath);
1683 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1684 *status = GOT_STATUS_MODIFY;
1686 return NULL;
1689 static const struct got_error *
1690 get_file_status(unsigned char *status, struct stat *sb,
1691 struct got_fileindex_entry *ie, const char *abspath,
1692 int dirfd, const char *de_name, struct got_repository *repo)
1694 const struct got_error *err = NULL;
1695 struct got_object_id id;
1696 size_t hdrlen;
1697 int fd = -1;
1698 FILE *f = NULL;
1699 uint8_t fbuf[8192];
1700 struct got_blob_object *blob = NULL;
1701 size_t flen, blen;
1702 unsigned char staged_status = get_staged_status(ie);
1704 *status = GOT_STATUS_NO_CHANGE;
1707 * Whenever the caller provides a directory descriptor and a
1708 * directory entry name for the file, use them! This prevents
1709 * race conditions if filesystem paths change beneath our feet.
1711 if (dirfd != -1) {
1712 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1713 if (errno == ENOENT) {
1714 if (got_fileindex_entry_has_file_on_disk(ie))
1715 *status = GOT_STATUS_MISSING;
1716 else
1717 *status = GOT_STATUS_DELETE;
1718 goto done;
1720 err = got_error_from_errno2("fstatat", abspath);
1721 goto done;
1723 } else {
1724 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1725 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1726 return got_error_from_errno2("open", abspath);
1727 else if (fd == -1 && errno == ELOOP) {
1728 if (lstat(abspath, sb) == -1)
1729 return got_error_from_errno2("lstat", abspath);
1730 } else if (fd == -1 || fstat(fd, sb) == -1) {
1731 if (errno == ENOENT) {
1732 if (got_fileindex_entry_has_file_on_disk(ie))
1733 *status = GOT_STATUS_MISSING;
1734 else
1735 *status = GOT_STATUS_DELETE;
1736 goto done;
1738 err = got_error_from_errno2("fstat", abspath);
1739 goto done;
1743 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1744 *status = GOT_STATUS_OBSTRUCTED;
1745 goto done;
1748 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1749 *status = GOT_STATUS_DELETE;
1750 goto done;
1751 } else if (!got_fileindex_entry_has_blob(ie) &&
1752 staged_status != GOT_STATUS_ADD) {
1753 *status = GOT_STATUS_ADD;
1754 goto done;
1757 if (!stat_info_differs(ie, sb))
1758 goto done;
1760 if (S_ISLNK(sb->st_mode) &&
1761 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1762 *status = GOT_STATUS_MODIFY;
1763 goto done;
1766 if (staged_status == GOT_STATUS_MODIFY ||
1767 staged_status == GOT_STATUS_ADD)
1768 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1769 else
1770 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1772 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1773 if (err)
1774 goto done;
1776 if (S_ISLNK(sb->st_mode)) {
1777 err = get_symlink_modification_status(status, ie,
1778 abspath, dirfd, de_name, blob);
1779 goto done;
1782 if (dirfd != -1) {
1783 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1784 if (fd == -1) {
1785 err = got_error_from_errno2("openat", abspath);
1786 goto done;
1790 f = fdopen(fd, "r");
1791 if (f == NULL) {
1792 err = got_error_from_errno2("fdopen", abspath);
1793 goto done;
1795 fd = -1;
1796 hdrlen = got_object_blob_get_hdrlen(blob);
1797 for (;;) {
1798 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1799 err = got_object_blob_read_block(&blen, blob);
1800 if (err)
1801 goto done;
1802 /* Skip length of blob object header first time around. */
1803 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1804 if (flen == 0 && ferror(f)) {
1805 err = got_error_from_errno("fread");
1806 goto done;
1808 if (blen - hdrlen == 0) {
1809 if (flen != 0)
1810 *status = GOT_STATUS_MODIFY;
1811 break;
1812 } else if (flen == 0) {
1813 if (blen - hdrlen != 0)
1814 *status = GOT_STATUS_MODIFY;
1815 break;
1816 } else if (blen - hdrlen == flen) {
1817 /* Skip blob object header first time around. */
1818 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1819 *status = GOT_STATUS_MODIFY;
1820 break;
1822 } else {
1823 *status = GOT_STATUS_MODIFY;
1824 break;
1826 hdrlen = 0;
1829 if (*status == GOT_STATUS_MODIFY) {
1830 rewind(f);
1831 err = get_modified_file_content_status(status, f);
1832 } else if (xbit_differs(ie, sb->st_mode))
1833 *status = GOT_STATUS_MODE_CHANGE;
1834 done:
1835 if (blob)
1836 got_object_blob_close(blob);
1837 if (f != NULL && fclose(f) == EOF && err == NULL)
1838 err = got_error_from_errno2("fclose", abspath);
1839 if (fd != -1 && close(fd) == -1 && err == NULL)
1840 err = got_error_from_errno2("close", abspath);
1841 return err;
1845 * Update timestamps in the file index if a file is unmodified and
1846 * we had to run a full content comparison to find out.
1848 static const struct got_error *
1849 sync_timestamps(char *ondisk_path, unsigned char status,
1850 struct got_fileindex_entry *ie, struct stat *sb)
1852 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1853 return got_fileindex_entry_update(ie, ondisk_path,
1854 ie->blob_sha1, ie->commit_sha1, 1);
1856 return NULL;
1859 static const struct got_error *
1860 update_blob(struct got_worktree *worktree,
1861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1862 struct got_tree_entry *te, const char *path,
1863 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1864 void *progress_arg)
1866 const struct got_error *err = NULL;
1867 struct got_blob_object *blob = NULL;
1868 char *ondisk_path;
1869 unsigned char status = GOT_STATUS_NO_CHANGE;
1870 struct stat sb;
1872 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1873 return got_error_from_errno("asprintf");
1875 if (ie) {
1876 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1877 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1878 goto done;
1880 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1881 repo);
1882 if (err)
1883 goto done;
1884 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1885 sb.st_mode = got_fileindex_perms_to_st(ie);
1886 } else {
1887 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1888 status = GOT_STATUS_UNVERSIONED;
1891 if (status == GOT_STATUS_OBSTRUCTED) {
1892 err = (*progress_cb)(progress_arg, status, path);
1893 goto done;
1895 if (status == GOT_STATUS_CONFLICT) {
1896 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1897 path);
1898 goto done;
1901 if (ie && status != GOT_STATUS_MISSING &&
1902 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1903 if (got_fileindex_entry_has_commit(ie) &&
1904 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1905 SHA1_DIGEST_LENGTH) == 0) {
1906 err = sync_timestamps(ondisk_path, status, ie, &sb);
1907 if (err)
1908 goto done;
1909 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1910 path);
1911 goto done;
1913 if (got_fileindex_entry_has_blob(ie) &&
1914 memcmp(ie->blob_sha1, te->id.sha1,
1915 SHA1_DIGEST_LENGTH) == 0) {
1916 err = sync_timestamps(ondisk_path, status, ie, &sb);
1917 goto done;
1921 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1922 if (err)
1923 goto done;
1925 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1926 int update_timestamps;
1927 struct got_blob_object *blob2 = NULL;
1928 char *label_orig = NULL;
1929 if (got_fileindex_entry_has_blob(ie)) {
1930 struct got_object_id id2;
1931 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1933 if (err)
1934 goto done;
1936 if (got_fileindex_entry_has_commit(ie)) {
1937 char id_str[SHA1_DIGEST_STRING_LENGTH];
1938 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1939 sizeof(id_str)) == NULL) {
1940 err = got_error_path(id_str,
1941 GOT_ERR_BAD_OBJ_ID_STR);
1942 goto done;
1944 if (asprintf(&label_orig, "%s: commit %s",
1945 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1946 err = got_error_from_errno("asprintf");
1947 goto done;
1950 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1951 char *link_target;
1952 err = got_object_blob_read_to_str(&link_target, blob);
1953 if (err)
1954 goto done;
1955 err = merge_symlink(worktree, blob2, ondisk_path, path,
1956 label_orig, link_target, worktree->base_commit_id,
1957 repo, progress_cb, progress_arg);
1958 free(link_target);
1959 } else {
1960 err = merge_blob(&update_timestamps, worktree, blob2,
1961 ondisk_path, path, sb.st_mode, label_orig, blob,
1962 worktree->base_commit_id, repo,
1963 progress_cb, progress_arg);
1965 free(label_orig);
1966 if (blob2)
1967 got_object_blob_close(blob2);
1968 if (err)
1969 goto done;
1971 * Do not update timestamps of files with local changes.
1972 * Otherwise, a future status walk would treat them as
1973 * unmodified files again.
1975 err = got_fileindex_entry_update(ie, ondisk_path,
1976 blob->id.sha1, worktree->base_commit_id->sha1,
1977 update_timestamps);
1978 } else if (status == GOT_STATUS_MODE_CHANGE) {
1979 err = got_fileindex_entry_update(ie, ondisk_path,
1980 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1981 } else if (status == GOT_STATUS_DELETE) {
1982 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1983 if (err)
1984 goto done;
1985 err = got_fileindex_entry_update(ie, ondisk_path,
1986 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1987 if (err)
1988 goto done;
1989 } else {
1990 int is_bad_symlink = 0;
1991 if (S_ISLNK(te->mode)) {
1992 err = install_symlink(&is_bad_symlink, worktree,
1993 ondisk_path, path, blob,
1994 status == GOT_STATUS_MISSING, 0,
1995 status == GOT_STATUS_UNVERSIONED, repo,
1996 progress_cb, progress_arg);
1997 } else {
1998 err = install_blob(worktree, ondisk_path, path,
1999 te->mode, sb.st_mode, blob,
2000 status == GOT_STATUS_MISSING, 0, 0,
2001 status == GOT_STATUS_UNVERSIONED, repo,
2002 progress_cb, progress_arg);
2004 if (err)
2005 goto done;
2007 if (ie) {
2008 err = got_fileindex_entry_update(ie, ondisk_path,
2009 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2010 } else {
2011 err = create_fileindex_entry(&ie, fileindex,
2012 worktree->base_commit_id, ondisk_path, path,
2013 &blob->id);
2015 if (err)
2016 goto done;
2018 if (is_bad_symlink) {
2019 got_fileindex_entry_filetype_set(ie,
2020 GOT_FILEIDX_MODE_BAD_SYMLINK);
2023 got_object_blob_close(blob);
2024 done:
2025 free(ondisk_path);
2026 return err;
2029 static const struct got_error *
2030 remove_ondisk_file(const char *root_path, const char *path)
2032 const struct got_error *err = NULL;
2033 char *ondisk_path = NULL;
2035 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2036 return got_error_from_errno("asprintf");
2038 if (unlink(ondisk_path) == -1) {
2039 if (errno != ENOENT)
2040 err = got_error_from_errno2("unlink", ondisk_path);
2041 } else {
2042 char *parent = dirname(ondisk_path);
2043 while (parent && strcmp(parent, root_path) != 0) {
2044 if (rmdir(parent) == -1) {
2045 if (errno != ENOTEMPTY)
2046 err = got_error_from_errno2("rmdir",
2047 parent);
2048 break;
2050 parent = dirname(parent);
2053 free(ondisk_path);
2054 return err;
2057 static const struct got_error *
2058 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2059 struct got_fileindex_entry *ie, struct got_repository *repo,
2060 got_worktree_checkout_cb progress_cb, void *progress_arg)
2062 const struct got_error *err = NULL;
2063 unsigned char status;
2064 struct stat sb;
2065 char *ondisk_path;
2067 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2068 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2070 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2071 == -1)
2072 return got_error_from_errno("asprintf");
2074 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2075 if (err)
2076 goto done;
2078 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2079 char ondisk_target[PATH_MAX];
2080 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2081 sizeof(ondisk_target));
2082 if (ondisk_len == -1) {
2083 err = got_error_from_errno2("readlink", ondisk_path);
2084 goto done;
2086 ondisk_target[ondisk_len] = '\0';
2087 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2088 NULL, NULL, /* XXX pass common ancestor info? */
2089 ondisk_target, ondisk_path);
2090 if (err)
2091 goto done;
2092 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2093 ie->path);
2094 goto done;
2097 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2098 status == GOT_STATUS_ADD) {
2099 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2100 if (err)
2101 goto done;
2103 * Preserve the working file and change the deleted blob's
2104 * entry into a schedule-add entry.
2106 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2107 0);
2108 } else {
2109 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2110 if (err)
2111 goto done;
2112 if (status == GOT_STATUS_NO_CHANGE) {
2113 err = remove_ondisk_file(worktree->root_path, ie->path);
2114 if (err)
2115 goto done;
2117 got_fileindex_entry_remove(fileindex, ie);
2119 done:
2120 free(ondisk_path);
2121 return err;
2124 struct diff_cb_arg {
2125 struct got_fileindex *fileindex;
2126 struct got_worktree *worktree;
2127 struct got_repository *repo;
2128 got_worktree_checkout_cb progress_cb;
2129 void *progress_arg;
2130 got_cancel_cb cancel_cb;
2131 void *cancel_arg;
2134 static const struct got_error *
2135 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2136 struct got_tree_entry *te, const char *parent_path)
2138 struct diff_cb_arg *a = arg;
2140 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2141 return got_error(GOT_ERR_CANCELLED);
2143 return update_blob(a->worktree, a->fileindex, ie, te,
2144 ie->path, a->repo, a->progress_cb, a->progress_arg);
2147 static const struct got_error *
2148 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2150 struct diff_cb_arg *a = arg;
2152 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2153 return got_error(GOT_ERR_CANCELLED);
2155 return delete_blob(a->worktree, a->fileindex, ie,
2156 a->repo, a->progress_cb, a->progress_arg);
2159 static const struct got_error *
2160 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2162 struct diff_cb_arg *a = arg;
2163 const struct got_error *err;
2164 char *path;
2166 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2167 return got_error(GOT_ERR_CANCELLED);
2169 if (got_object_tree_entry_is_submodule(te))
2170 return NULL;
2172 if (asprintf(&path, "%s%s%s", parent_path,
2173 parent_path[0] ? "/" : "", te->name)
2174 == -1)
2175 return got_error_from_errno("asprintf");
2177 if (S_ISDIR(te->mode))
2178 err = add_dir_on_disk(a->worktree, path);
2179 else
2180 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2181 a->repo, a->progress_cb, a->progress_arg);
2183 free(path);
2184 return err;
2187 const struct got_error *
2188 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2190 uint32_t uuid_status;
2192 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2193 if (uuid_status != uuid_s_ok) {
2194 *uuidstr = NULL;
2195 return got_error_uuid(uuid_status, "uuid_to_string");
2198 return NULL;
2201 static const struct got_error *
2202 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2204 const struct got_error *err = NULL;
2205 char *uuidstr = NULL;
2207 *refname = NULL;
2209 err = got_worktree_get_uuid(&uuidstr, worktree);
2210 if (err)
2211 return err;
2213 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2214 err = got_error_from_errno("asprintf");
2215 *refname = NULL;
2217 free(uuidstr);
2218 return err;
2221 const struct got_error *
2222 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2224 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2227 static const struct got_error *
2228 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2234 static const struct got_error *
2235 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2240 static const struct got_error *
2241 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 return get_ref_name(refname, worktree,
2244 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2247 static const struct got_error *
2248 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 return get_ref_name(refname, worktree,
2251 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2254 static const struct got_error *
2255 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree,
2258 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2261 static const struct got_error *
2262 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2268 static const struct got_error *
2269 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree,
2272 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2275 static const struct got_error *
2276 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2282 const struct got_error *
2283 got_worktree_get_histedit_script_path(char **path,
2284 struct got_worktree *worktree)
2286 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2287 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2288 *path = NULL;
2289 return got_error_from_errno("asprintf");
2291 return NULL;
2295 * Prevent Git's garbage collector from deleting our base commit by
2296 * setting a reference to our base commit's ID.
2298 static const struct got_error *
2299 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2301 const struct got_error *err = NULL;
2302 struct got_reference *ref = NULL;
2303 char *refname;
2305 err = got_worktree_get_base_ref_name(&refname, worktree);
2306 if (err)
2307 return err;
2309 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2310 if (err)
2311 goto done;
2313 err = got_ref_write(ref, repo);
2314 done:
2315 free(refname);
2316 if (ref)
2317 got_ref_close(ref);
2318 return err;
2321 static const struct got_error *
2322 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2324 const struct got_error *err = NULL;
2326 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2327 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2328 err = got_error_from_errno("asprintf");
2329 *fileindex_path = NULL;
2331 return err;
2335 static const struct got_error *
2336 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2337 struct got_worktree *worktree)
2339 const struct got_error *err = NULL;
2340 FILE *index = NULL;
2342 *fileindex_path = NULL;
2343 *fileindex = got_fileindex_alloc();
2344 if (*fileindex == NULL)
2345 return got_error_from_errno("got_fileindex_alloc");
2347 err = get_fileindex_path(fileindex_path, worktree);
2348 if (err)
2349 goto done;
2351 index = fopen(*fileindex_path, "rb");
2352 if (index == NULL) {
2353 if (errno != ENOENT)
2354 err = got_error_from_errno2("fopen", *fileindex_path);
2355 } else {
2356 err = got_fileindex_read(*fileindex, index);
2357 if (fclose(index) != 0 && err == NULL)
2358 err = got_error_from_errno("fclose");
2360 done:
2361 if (err) {
2362 free(*fileindex_path);
2363 *fileindex_path = NULL;
2364 got_fileindex_free(*fileindex);
2365 *fileindex = NULL;
2367 return err;
2370 struct bump_base_commit_id_arg {
2371 struct got_object_id *base_commit_id;
2372 const char *path;
2373 size_t path_len;
2374 const char *entry_name;
2375 got_worktree_checkout_cb progress_cb;
2376 void *progress_arg;
2379 /* Bump base commit ID of all files within an updated part of the work tree. */
2380 static const struct got_error *
2381 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2383 const struct got_error *err;
2384 struct bump_base_commit_id_arg *a = arg;
2386 if (a->entry_name) {
2387 if (strcmp(ie->path, a->path) != 0)
2388 return NULL;
2389 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2390 return NULL;
2392 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2393 SHA1_DIGEST_LENGTH) == 0)
2394 return NULL;
2396 if (a->progress_cb) {
2397 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2398 ie->path);
2399 if (err)
2400 return err;
2402 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2403 return NULL;
2406 static const struct got_error *
2407 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2409 const struct got_error *err = NULL;
2410 char *new_fileindex_path = NULL;
2411 FILE *new_index = NULL;
2412 struct timespec timeout;
2414 err = got_opentemp_named(&new_fileindex_path, &new_index,
2415 fileindex_path);
2416 if (err)
2417 goto done;
2419 err = got_fileindex_write(fileindex, new_index);
2420 if (err)
2421 goto done;
2423 if (rename(new_fileindex_path, fileindex_path) != 0) {
2424 err = got_error_from_errno3("rename", new_fileindex_path,
2425 fileindex_path);
2426 unlink(new_fileindex_path);
2430 * Sleep for a short amount of time to ensure that files modified after
2431 * this program exits have a different time stamp from the one which
2432 * was recorded in the file index.
2434 timeout.tv_sec = 0;
2435 timeout.tv_nsec = 1;
2436 nanosleep(&timeout, NULL);
2437 done:
2438 if (new_index)
2439 fclose(new_index);
2440 free(new_fileindex_path);
2441 return err;
2444 static const struct got_error *
2445 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2446 struct got_object_id **tree_id, const char *wt_relpath,
2447 struct got_worktree *worktree, struct got_repository *repo)
2449 const struct got_error *err = NULL;
2450 struct got_object_id *id = NULL;
2451 char *in_repo_path = NULL;
2452 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2454 *entry_type = GOT_OBJ_TYPE_ANY;
2455 *tree_relpath = NULL;
2456 *tree_id = NULL;
2458 if (wt_relpath[0] == '\0') {
2459 /* Check out all files within the work tree. */
2460 *entry_type = GOT_OBJ_TYPE_TREE;
2461 *tree_relpath = strdup("");
2462 if (*tree_relpath == NULL) {
2463 err = got_error_from_errno("strdup");
2464 goto done;
2466 err = got_object_id_by_path(tree_id, repo,
2467 worktree->base_commit_id, worktree->path_prefix);
2468 if (err)
2469 goto done;
2470 return NULL;
2473 /* Check out a subset of files in the work tree. */
2475 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2476 is_root_wt ? "" : "/", wt_relpath) == -1) {
2477 err = got_error_from_errno("asprintf");
2478 goto done;
2481 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2482 in_repo_path);
2483 if (err)
2484 goto done;
2486 free(in_repo_path);
2487 in_repo_path = NULL;
2489 err = got_object_get_type(entry_type, repo, id);
2490 if (err)
2491 goto done;
2493 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2494 /* Check out a single file. */
2495 if (strchr(wt_relpath, '/') == NULL) {
2496 /* Check out a single file in work tree's root dir. */
2497 in_repo_path = strdup(worktree->path_prefix);
2498 if (in_repo_path == NULL) {
2499 err = got_error_from_errno("strdup");
2500 goto done;
2502 *tree_relpath = strdup("");
2503 if (*tree_relpath == NULL) {
2504 err = got_error_from_errno("strdup");
2505 goto done;
2507 } else {
2508 /* Check out a single file in a subdirectory. */
2509 err = got_path_dirname(tree_relpath, wt_relpath);
2510 if (err)
2511 return err;
2512 if (asprintf(&in_repo_path, "%s%s%s",
2513 worktree->path_prefix, is_root_wt ? "" : "/",
2514 *tree_relpath) == -1) {
2515 err = got_error_from_errno("asprintf");
2516 goto done;
2519 err = got_object_id_by_path(tree_id, repo,
2520 worktree->base_commit_id, in_repo_path);
2521 } else {
2522 /* Check out all files within a subdirectory. */
2523 *tree_id = got_object_id_dup(id);
2524 if (*tree_id == NULL) {
2525 err = got_error_from_errno("got_object_id_dup");
2526 goto done;
2528 *tree_relpath = strdup(wt_relpath);
2529 if (*tree_relpath == NULL) {
2530 err = got_error_from_errno("strdup");
2531 goto done;
2534 done:
2535 free(id);
2536 free(in_repo_path);
2537 if (err) {
2538 *entry_type = GOT_OBJ_TYPE_ANY;
2539 free(*tree_relpath);
2540 *tree_relpath = NULL;
2541 free(*tree_id);
2542 *tree_id = NULL;
2544 return err;
2547 static const struct got_error *
2548 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2549 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2550 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2551 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2553 const struct got_error *err = NULL;
2554 struct got_commit_object *commit = NULL;
2555 struct got_tree_object *tree = NULL;
2556 struct got_fileindex_diff_tree_cb diff_cb;
2557 struct diff_cb_arg arg;
2559 err = ref_base_commit(worktree, repo);
2560 if (err) {
2561 if (!(err->code == GOT_ERR_ERRNO &&
2562 (errno == EACCES || errno == EROFS)))
2563 goto done;
2564 err = (*progress_cb)(progress_arg,
2565 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2566 if (err)
2567 return err;
2570 err = got_object_open_as_commit(&commit, repo,
2571 worktree->base_commit_id);
2572 if (err)
2573 goto done;
2575 err = got_object_open_as_tree(&tree, repo, tree_id);
2576 if (err)
2577 goto done;
2579 if (entry_name &&
2580 got_object_tree_find_entry(tree, entry_name) == NULL) {
2581 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2582 goto done;
2585 diff_cb.diff_old_new = diff_old_new;
2586 diff_cb.diff_old = diff_old;
2587 diff_cb.diff_new = diff_new;
2588 arg.fileindex = fileindex;
2589 arg.worktree = worktree;
2590 arg.repo = repo;
2591 arg.progress_cb = progress_cb;
2592 arg.progress_arg = progress_arg;
2593 arg.cancel_cb = cancel_cb;
2594 arg.cancel_arg = cancel_arg;
2595 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2596 entry_name, repo, &diff_cb, &arg);
2597 done:
2598 if (tree)
2599 got_object_tree_close(tree);
2600 if (commit)
2601 got_object_commit_close(commit);
2602 return err;
2605 const struct got_error *
2606 got_worktree_checkout_files(struct got_worktree *worktree,
2607 struct got_pathlist_head *paths, struct got_repository *repo,
2608 got_worktree_checkout_cb progress_cb, void *progress_arg,
2609 got_cancel_cb cancel_cb, void *cancel_arg)
2611 const struct got_error *err = NULL, *sync_err, *unlockerr;
2612 struct got_commit_object *commit = NULL;
2613 struct got_tree_object *tree = NULL;
2614 struct got_fileindex *fileindex = NULL;
2615 char *fileindex_path = NULL;
2616 struct got_pathlist_entry *pe;
2617 struct tree_path_data {
2618 SIMPLEQ_ENTRY(tree_path_data) entry;
2619 struct got_object_id *tree_id;
2620 int entry_type;
2621 char *relpath;
2622 char *entry_name;
2623 } *tpd = NULL;
2624 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2626 SIMPLEQ_INIT(&tree_paths);
2628 err = lock_worktree(worktree, LOCK_EX);
2629 if (err)
2630 return err;
2632 /* Map all specified paths to in-repository trees. */
2633 TAILQ_FOREACH(pe, paths, entry) {
2634 tpd = malloc(sizeof(*tpd));
2635 if (tpd == NULL) {
2636 err = got_error_from_errno("malloc");
2637 goto done;
2640 err = find_tree_entry_for_checkout(&tpd->entry_type,
2641 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2642 if (err) {
2643 free(tpd);
2644 goto done;
2647 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2648 err = got_path_basename(&tpd->entry_name, pe->path);
2649 if (err) {
2650 free(tpd->relpath);
2651 free(tpd->tree_id);
2652 free(tpd);
2653 goto done;
2655 } else
2656 tpd->entry_name = NULL;
2658 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2662 * Read the file index.
2663 * Checking out files is supposed to be an idempotent operation.
2664 * If the on-disk file index is incomplete we will try to complete it.
2666 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2667 if (err)
2668 goto done;
2670 tpd = SIMPLEQ_FIRST(&tree_paths);
2671 TAILQ_FOREACH(pe, paths, entry) {
2672 struct bump_base_commit_id_arg bbc_arg;
2674 err = checkout_files(worktree, fileindex, tpd->relpath,
2675 tpd->tree_id, tpd->entry_name, repo,
2676 progress_cb, progress_arg, cancel_cb, cancel_arg);
2677 if (err)
2678 break;
2680 bbc_arg.base_commit_id = worktree->base_commit_id;
2681 bbc_arg.entry_name = tpd->entry_name;
2682 bbc_arg.path = pe->path;
2683 bbc_arg.path_len = pe->path_len;
2684 bbc_arg.progress_cb = progress_cb;
2685 bbc_arg.progress_arg = progress_arg;
2686 err = got_fileindex_for_each_entry_safe(fileindex,
2687 bump_base_commit_id, &bbc_arg);
2688 if (err)
2689 break;
2691 tpd = SIMPLEQ_NEXT(tpd, entry);
2693 sync_err = sync_fileindex(fileindex, fileindex_path);
2694 if (sync_err && err == NULL)
2695 err = sync_err;
2696 done:
2697 free(fileindex_path);
2698 if (tree)
2699 got_object_tree_close(tree);
2700 if (commit)
2701 got_object_commit_close(commit);
2702 if (fileindex)
2703 got_fileindex_free(fileindex);
2704 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2705 tpd = SIMPLEQ_FIRST(&tree_paths);
2706 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2707 free(tpd->relpath);
2708 free(tpd->tree_id);
2709 free(tpd);
2711 unlockerr = lock_worktree(worktree, LOCK_SH);
2712 if (unlockerr && err == NULL)
2713 err = unlockerr;
2714 return err;
2717 struct merge_file_cb_arg {
2718 struct got_worktree *worktree;
2719 struct got_fileindex *fileindex;
2720 got_worktree_checkout_cb progress_cb;
2721 void *progress_arg;
2722 got_cancel_cb cancel_cb;
2723 void *cancel_arg;
2724 const char *label_orig;
2725 struct got_object_id *commit_id2;
2728 static const struct got_error *
2729 merge_file_cb(void *arg, struct got_blob_object *blob1,
2730 struct got_blob_object *blob2, struct got_object_id *id1,
2731 struct got_object_id *id2, const char *path1, const char *path2,
2732 mode_t mode1, mode_t mode2, struct got_repository *repo)
2734 static const struct got_error *err = NULL;
2735 struct merge_file_cb_arg *a = arg;
2736 struct got_fileindex_entry *ie;
2737 char *ondisk_path = NULL;
2738 struct stat sb;
2739 unsigned char status;
2740 int local_changes_subsumed;
2742 if (blob1 && blob2) {
2743 ie = got_fileindex_entry_get(a->fileindex, path2,
2744 strlen(path2));
2745 if (ie == NULL)
2746 return (*a->progress_cb)(a->progress_arg,
2747 GOT_STATUS_MISSING, path2);
2749 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2750 path2) == -1)
2751 return got_error_from_errno("asprintf");
2753 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2754 repo);
2755 if (err)
2756 goto done;
2758 if (status == GOT_STATUS_DELETE) {
2759 err = (*a->progress_cb)(a->progress_arg,
2760 GOT_STATUS_MERGE, path2);
2761 goto done;
2763 if (status != GOT_STATUS_NO_CHANGE &&
2764 status != GOT_STATUS_MODIFY &&
2765 status != GOT_STATUS_CONFLICT &&
2766 status != GOT_STATUS_ADD) {
2767 err = (*a->progress_cb)(a->progress_arg, status, path2);
2768 goto done;
2771 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2772 char *link_target2;
2773 err = got_object_blob_read_to_str(&link_target2, blob2);
2774 if (err)
2775 goto done;
2776 err = merge_symlink(a->worktree, blob1, ondisk_path,
2777 path2, a->label_orig, link_target2, a->commit_id2,
2778 repo, a->progress_cb, a->progress_arg);
2779 free(link_target2);
2780 } else {
2781 err = merge_blob(&local_changes_subsumed, a->worktree,
2782 blob1, ondisk_path, path2, sb.st_mode,
2783 a->label_orig, blob2, a->commit_id2, repo,
2784 a->progress_cb, a->progress_arg);
2786 } else if (blob1) {
2787 ie = got_fileindex_entry_get(a->fileindex, path1,
2788 strlen(path1));
2789 if (ie == NULL)
2790 return (*a->progress_cb)(a->progress_arg,
2791 GOT_STATUS_MISSING, path1);
2793 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2794 path1) == -1)
2795 return got_error_from_errno("asprintf");
2797 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2798 repo);
2799 if (err)
2800 goto done;
2802 switch (status) {
2803 case GOT_STATUS_NO_CHANGE:
2804 err = (*a->progress_cb)(a->progress_arg,
2805 GOT_STATUS_DELETE, path1);
2806 if (err)
2807 goto done;
2808 err = remove_ondisk_file(a->worktree->root_path, path1);
2809 if (err)
2810 goto done;
2811 if (ie)
2812 got_fileindex_entry_mark_deleted_from_disk(ie);
2813 break;
2814 case GOT_STATUS_DELETE:
2815 case GOT_STATUS_MISSING:
2816 err = (*a->progress_cb)(a->progress_arg,
2817 GOT_STATUS_DELETE, path1);
2818 if (err)
2819 goto done;
2820 if (ie)
2821 got_fileindex_entry_mark_deleted_from_disk(ie);
2822 break;
2823 case GOT_STATUS_ADD: {
2824 struct got_object_id *id;
2825 FILE *blob1_f;
2827 * Delete the added file only if its content already
2828 * exists in the repository.
2830 err = got_object_blob_file_create(&id, &blob1_f, path1);
2831 if (err)
2832 goto done;
2833 if (got_object_id_cmp(id, id1) == 0) {
2834 err = (*a->progress_cb)(a->progress_arg,
2835 GOT_STATUS_DELETE, path1);
2836 if (err)
2837 goto done;
2838 err = remove_ondisk_file(a->worktree->root_path,
2839 path1);
2840 if (err)
2841 goto done;
2842 if (ie)
2843 got_fileindex_entry_remove(a->fileindex,
2844 ie);
2845 } else {
2846 err = (*a->progress_cb)(a->progress_arg,
2847 GOT_STATUS_CANNOT_DELETE, path1);
2849 if (fclose(blob1_f) == EOF && err == NULL)
2850 err = got_error_from_errno("fclose");
2851 free(id);
2852 if (err)
2853 goto done;
2854 break;
2856 case GOT_STATUS_MODIFY:
2857 case GOT_STATUS_CONFLICT:
2858 err = (*a->progress_cb)(a->progress_arg,
2859 GOT_STATUS_CANNOT_DELETE, path1);
2860 if (err)
2861 goto done;
2862 break;
2863 case GOT_STATUS_OBSTRUCTED:
2864 err = (*a->progress_cb)(a->progress_arg, status, path1);
2865 if (err)
2866 goto done;
2867 break;
2868 default:
2869 break;
2871 } else if (blob2) {
2872 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2873 path2) == -1)
2874 return got_error_from_errno("asprintf");
2875 ie = got_fileindex_entry_get(a->fileindex, path2,
2876 strlen(path2));
2877 if (ie) {
2878 err = get_file_status(&status, &sb, ie, ondisk_path,
2879 -1, NULL, repo);
2880 if (err)
2881 goto done;
2882 if (status != GOT_STATUS_NO_CHANGE &&
2883 status != GOT_STATUS_MODIFY &&
2884 status != GOT_STATUS_CONFLICT &&
2885 status != GOT_STATUS_ADD) {
2886 err = (*a->progress_cb)(a->progress_arg,
2887 status, path2);
2888 goto done;
2890 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2891 char *link_target2;
2892 err = got_object_blob_read_to_str(&link_target2,
2893 blob2);
2894 if (err)
2895 goto done;
2896 err = merge_symlink(a->worktree, NULL,
2897 ondisk_path, path2, a->label_orig,
2898 link_target2, a->commit_id2, repo,
2899 a->progress_cb, a->progress_arg);
2900 free(link_target2);
2901 } else if (S_ISREG(sb.st_mode)) {
2902 err = merge_blob(&local_changes_subsumed,
2903 a->worktree, NULL, ondisk_path, path2,
2904 sb.st_mode, a->label_orig, blob2,
2905 a->commit_id2, repo, a->progress_cb,
2906 a->progress_arg);
2907 } else {
2908 err = got_error_path(ondisk_path,
2909 GOT_ERR_FILE_OBSTRUCTED);
2911 if (err)
2912 goto done;
2913 if (status == GOT_STATUS_DELETE) {
2914 err = got_fileindex_entry_update(ie,
2915 ondisk_path, blob2->id.sha1,
2916 a->worktree->base_commit_id->sha1, 0);
2917 if (err)
2918 goto done;
2920 } else {
2921 int is_bad_symlink = 0;
2922 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2923 if (S_ISLNK(mode2)) {
2924 err = install_symlink(&is_bad_symlink,
2925 a->worktree, ondisk_path, path2, blob2, 0,
2926 0, 1, repo, a->progress_cb, a->progress_arg);
2927 } else {
2928 err = install_blob(a->worktree, ondisk_path, path2,
2929 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2930 a->progress_cb, a->progress_arg);
2932 if (err)
2933 goto done;
2934 err = got_fileindex_entry_alloc(&ie, path2);
2935 if (err)
2936 goto done;
2937 err = got_fileindex_entry_update(ie, ondisk_path,
2938 NULL, NULL, 1);
2939 if (err) {
2940 got_fileindex_entry_free(ie);
2941 goto done;
2943 err = got_fileindex_entry_add(a->fileindex, ie);
2944 if (err) {
2945 got_fileindex_entry_free(ie);
2946 goto done;
2948 if (is_bad_symlink) {
2949 got_fileindex_entry_filetype_set(ie,
2950 GOT_FILEIDX_MODE_BAD_SYMLINK);
2954 done:
2955 free(ondisk_path);
2956 return err;
2959 struct check_merge_ok_arg {
2960 struct got_worktree *worktree;
2961 struct got_repository *repo;
2964 static const struct got_error *
2965 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2967 const struct got_error *err = NULL;
2968 struct check_merge_ok_arg *a = arg;
2969 unsigned char status;
2970 struct stat sb;
2971 char *ondisk_path;
2973 /* Reject merges into a work tree with mixed base commits. */
2974 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2975 SHA1_DIGEST_LENGTH))
2976 return got_error(GOT_ERR_MIXED_COMMITS);
2978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2979 == -1)
2980 return got_error_from_errno("asprintf");
2982 /* Reject merges into a work tree with conflicted files. */
2983 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2984 if (err)
2985 return err;
2986 if (status == GOT_STATUS_CONFLICT)
2987 return got_error(GOT_ERR_CONFLICTS);
2989 return NULL;
2992 static const struct got_error *
2993 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2994 const char *fileindex_path, struct got_object_id *commit_id1,
2995 struct got_object_id *commit_id2, struct got_repository *repo,
2996 got_worktree_checkout_cb progress_cb, void *progress_arg,
2997 got_cancel_cb cancel_cb, void *cancel_arg)
2999 const struct got_error *err = NULL, *sync_err;
3000 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3001 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3002 struct merge_file_cb_arg arg;
3003 char *label_orig = NULL;
3005 if (commit_id1) {
3006 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3007 worktree->path_prefix);
3008 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3009 goto done;
3011 if (tree_id1) {
3012 char *id_str;
3014 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3015 if (err)
3016 goto done;
3018 err = got_object_id_str(&id_str, commit_id1);
3019 if (err)
3020 goto done;
3022 if (asprintf(&label_orig, "%s: commit %s",
3023 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3024 err = got_error_from_errno("asprintf");
3025 free(id_str);
3026 goto done;
3028 free(id_str);
3031 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3032 worktree->path_prefix);
3033 if (err)
3034 goto done;
3036 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3037 if (err)
3038 goto done;
3040 arg.worktree = worktree;
3041 arg.fileindex = fileindex;
3042 arg.progress_cb = progress_cb;
3043 arg.progress_arg = progress_arg;
3044 arg.cancel_cb = cancel_cb;
3045 arg.cancel_arg = cancel_arg;
3046 arg.label_orig = label_orig;
3047 arg.commit_id2 = commit_id2;
3048 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3049 sync_err = sync_fileindex(fileindex, fileindex_path);
3050 if (sync_err && err == NULL)
3051 err = sync_err;
3052 done:
3053 if (tree1)
3054 got_object_tree_close(tree1);
3055 if (tree2)
3056 got_object_tree_close(tree2);
3057 free(label_orig);
3058 return err;
3061 const struct got_error *
3062 got_worktree_merge_files(struct got_worktree *worktree,
3063 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3064 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3065 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3067 const struct got_error *err, *unlockerr;
3068 char *fileindex_path = NULL;
3069 struct got_fileindex *fileindex = NULL;
3070 struct check_merge_ok_arg mok_arg;
3072 err = lock_worktree(worktree, LOCK_EX);
3073 if (err)
3074 return err;
3076 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3077 if (err)
3078 goto done;
3080 mok_arg.worktree = worktree;
3081 mok_arg.repo = repo;
3082 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3083 &mok_arg);
3084 if (err)
3085 goto done;
3087 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3088 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3089 done:
3090 if (fileindex)
3091 got_fileindex_free(fileindex);
3092 free(fileindex_path);
3093 unlockerr = lock_worktree(worktree, LOCK_SH);
3094 if (unlockerr && err == NULL)
3095 err = unlockerr;
3096 return err;
3099 struct diff_dir_cb_arg {
3100 struct got_fileindex *fileindex;
3101 struct got_worktree *worktree;
3102 const char *status_path;
3103 size_t status_path_len;
3104 struct got_repository *repo;
3105 got_worktree_status_cb status_cb;
3106 void *status_arg;
3107 got_cancel_cb cancel_cb;
3108 void *cancel_arg;
3109 /* A pathlist containing per-directory pathlists of ignore patterns. */
3110 struct got_pathlist_head ignores;
3111 int report_unchanged;
3112 int no_ignores;
3115 static const struct got_error *
3116 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3117 int dirfd, const char *de_name,
3118 got_worktree_status_cb status_cb, void *status_arg,
3119 struct got_repository *repo, int report_unchanged)
3121 const struct got_error *err = NULL;
3122 unsigned char status = GOT_STATUS_NO_CHANGE;
3123 unsigned char staged_status = get_staged_status(ie);
3124 struct stat sb;
3125 struct got_object_id blob_id, commit_id, staged_blob_id;
3126 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3127 struct got_object_id *staged_blob_idp = NULL;
3129 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3130 if (err)
3131 return err;
3133 if (status == GOT_STATUS_NO_CHANGE &&
3134 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3135 return NULL;
3137 if (got_fileindex_entry_has_blob(ie)) {
3138 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3139 blob_idp = &blob_id;
3141 if (got_fileindex_entry_has_commit(ie)) {
3142 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3143 commit_idp = &commit_id;
3145 if (staged_status == GOT_STATUS_ADD ||
3146 staged_status == GOT_STATUS_MODIFY) {
3147 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3148 SHA1_DIGEST_LENGTH);
3149 staged_blob_idp = &staged_blob_id;
3152 return (*status_cb)(status_arg, status, staged_status,
3153 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3156 static const struct got_error *
3157 status_old_new(void *arg, struct got_fileindex_entry *ie,
3158 struct dirent *de, const char *parent_path, int dirfd)
3160 const struct got_error *err = NULL;
3161 struct diff_dir_cb_arg *a = arg;
3162 char *abspath;
3164 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3165 return got_error(GOT_ERR_CANCELLED);
3167 if (got_path_cmp(parent_path, a->status_path,
3168 strlen(parent_path), a->status_path_len) != 0 &&
3169 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3170 return NULL;
3172 if (parent_path[0]) {
3173 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3174 parent_path, de->d_name) == -1)
3175 return got_error_from_errno("asprintf");
3176 } else {
3177 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3178 de->d_name) == -1)
3179 return got_error_from_errno("asprintf");
3182 err = report_file_status(ie, abspath, dirfd, de->d_name,
3183 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3184 free(abspath);
3185 return err;
3188 static const struct got_error *
3189 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3191 struct diff_dir_cb_arg *a = arg;
3192 struct got_object_id blob_id, commit_id;
3193 unsigned char status;
3195 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3196 return got_error(GOT_ERR_CANCELLED);
3198 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3199 return NULL;
3201 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3202 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3203 if (got_fileindex_entry_has_file_on_disk(ie))
3204 status = GOT_STATUS_MISSING;
3205 else
3206 status = GOT_STATUS_DELETE;
3207 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3208 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3211 void
3212 free_ignorelist(struct got_pathlist_head *ignorelist)
3214 struct got_pathlist_entry *pe;
3216 TAILQ_FOREACH(pe, ignorelist, entry)
3217 free((char *)pe->path);
3218 got_pathlist_free(ignorelist);
3221 void
3222 free_ignores(struct got_pathlist_head *ignores)
3224 struct got_pathlist_entry *pe;
3226 TAILQ_FOREACH(pe, ignores, entry) {
3227 struct got_pathlist_head *ignorelist = pe->data;
3228 free_ignorelist(ignorelist);
3229 free((char *)pe->path);
3231 got_pathlist_free(ignores);
3234 static const struct got_error *
3235 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3237 const struct got_error *err = NULL;
3238 struct got_pathlist_entry *pe = NULL;
3239 struct got_pathlist_head *ignorelist;
3240 char *line = NULL, *pattern, *dirpath = NULL;
3241 size_t linesize = 0;
3242 ssize_t linelen;
3244 ignorelist = calloc(1, sizeof(*ignorelist));
3245 if (ignorelist == NULL)
3246 return got_error_from_errno("calloc");
3247 TAILQ_INIT(ignorelist);
3249 while ((linelen = getline(&line, &linesize, f)) != -1) {
3250 if (linelen > 0 && line[linelen - 1] == '\n')
3251 line[linelen - 1] = '\0';
3253 /* Git's ignores may contain comments. */
3254 if (line[0] == '#')
3255 continue;
3257 /* Git's negated patterns are not (yet?) supported. */
3258 if (line[0] == '!')
3259 continue;
3261 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3262 line) == -1) {
3263 err = got_error_from_errno("asprintf");
3264 goto done;
3266 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3267 if (err)
3268 goto done;
3270 if (ferror(f)) {
3271 err = got_error_from_errno("getline");
3272 goto done;
3275 dirpath = strdup(path);
3276 if (dirpath == NULL) {
3277 err = got_error_from_errno("strdup");
3278 goto done;
3280 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3281 done:
3282 free(line);
3283 if (err || pe == NULL) {
3284 free(dirpath);
3285 free_ignorelist(ignorelist);
3287 return err;
3290 int
3291 match_ignores(struct got_pathlist_head *ignores, const char *path)
3293 struct got_pathlist_entry *pe;
3295 /* Handle patterns which match in all directories. */
3296 TAILQ_FOREACH(pe, ignores, entry) {
3297 struct got_pathlist_head *ignorelist = pe->data;
3298 struct got_pathlist_entry *pi;
3300 TAILQ_FOREACH(pi, ignorelist, entry) {
3301 const char *p, *pattern = pi->path;
3303 if (strncmp(pattern, "**/", 3) != 0)
3304 continue;
3305 pattern += 3;
3306 p = path;
3307 while (*p) {
3308 if (fnmatch(pattern, p,
3309 FNM_PATHNAME | FNM_LEADING_DIR)) {
3310 /* Retry in next directory. */
3311 while (*p && *p != '/')
3312 p++;
3313 while (*p == '/')
3314 p++;
3315 continue;
3317 return 1;
3323 * The ignores pathlist contains ignore lists from children before
3324 * parents, so we can find the most specific ignorelist by walking
3325 * ignores backwards.
3327 pe = TAILQ_LAST(ignores, got_pathlist_head);
3328 while (pe) {
3329 if (got_path_is_child(path, pe->path, pe->path_len)) {
3330 struct got_pathlist_head *ignorelist = pe->data;
3331 struct got_pathlist_entry *pi;
3332 TAILQ_FOREACH(pi, ignorelist, entry) {
3333 const char *pattern = pi->path;
3334 int flags = FNM_LEADING_DIR;
3335 if (strstr(pattern, "/**/") == NULL)
3336 flags |= FNM_PATHNAME;
3337 if (fnmatch(pattern, path, flags))
3338 continue;
3339 return 1;
3342 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3345 return 0;
3348 static const struct got_error *
3349 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3350 const char *path, int dirfd, const char *ignores_filename)
3352 const struct got_error *err = NULL;
3353 char *ignorespath;
3354 int fd = -1;
3355 FILE *ignoresfile = NULL;
3357 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3358 path[0] ? "/" : "", ignores_filename) == -1)
3359 return got_error_from_errno("asprintf");
3361 if (dirfd != -1) {
3362 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3363 if (fd == -1) {
3364 if (errno != ENOENT && errno != EACCES)
3365 err = got_error_from_errno2("openat",
3366 ignorespath);
3367 } else {
3368 ignoresfile = fdopen(fd, "r");
3369 if (ignoresfile == NULL)
3370 err = got_error_from_errno2("fdopen",
3371 ignorespath);
3372 else {
3373 fd = -1;
3374 err = read_ignores(ignores, path, ignoresfile);
3377 } else {
3378 ignoresfile = fopen(ignorespath, "r");
3379 if (ignoresfile == NULL) {
3380 if (errno != ENOENT && errno != EACCES)
3381 err = got_error_from_errno2("fopen",
3382 ignorespath);
3383 } else
3384 err = read_ignores(ignores, path, ignoresfile);
3387 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3388 err = got_error_from_errno2("fclose", path);
3389 if (fd != -1 && close(fd) == -1 && err == NULL)
3390 err = got_error_from_errno2("close", path);
3391 free(ignorespath);
3392 return err;
3395 static const struct got_error *
3396 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3398 const struct got_error *err = NULL;
3399 struct diff_dir_cb_arg *a = arg;
3400 char *path = NULL;
3402 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3403 return got_error(GOT_ERR_CANCELLED);
3405 if (parent_path[0]) {
3406 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3407 return got_error_from_errno("asprintf");
3408 } else {
3409 path = de->d_name;
3412 if (de->d_type != DT_DIR &&
3413 got_path_is_child(path, a->status_path, a->status_path_len)
3414 && !match_ignores(&a->ignores, path))
3415 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3416 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3417 if (parent_path[0])
3418 free(path);
3419 return err;
3422 static const struct got_error *
3423 status_traverse(void *arg, const char *path, int dirfd)
3425 const struct got_error *err = NULL;
3426 struct diff_dir_cb_arg *a = arg;
3428 if (a->no_ignores)
3429 return NULL;
3431 err = add_ignores(&a->ignores, a->worktree->root_path,
3432 path, dirfd, ".cvsignore");
3433 if (err)
3434 return err;
3436 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3437 dirfd, ".gitignore");
3439 return err;
3442 static const struct got_error *
3443 report_single_file_status(const char *path, const char *ondisk_path,
3444 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3445 void *status_arg, struct got_repository *repo, int report_unchanged)
3447 struct got_fileindex_entry *ie;
3448 struct stat sb;
3450 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3451 if (ie)
3452 return report_file_status(ie, ondisk_path, -1, NULL,
3453 status_cb, status_arg, repo, report_unchanged);
3455 if (lstat(ondisk_path, &sb) == -1) {
3456 if (errno != ENOENT)
3457 return got_error_from_errno2("lstat", ondisk_path);
3458 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3459 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3460 return NULL;
3463 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3464 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3465 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3467 return NULL;
3470 static const struct got_error *
3471 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3472 const char *root_path, const char *path)
3474 const struct got_error *err;
3475 char *parent_path, *next_parent_path = NULL;
3477 err = add_ignores(ignores, root_path, "", -1,
3478 ".cvsignore");
3479 if (err)
3480 return err;
3482 err = add_ignores(ignores, root_path, "", -1,
3483 ".gitignore");
3484 if (err)
3485 return err;
3487 err = got_path_dirname(&parent_path, path);
3488 if (err) {
3489 if (err->code == GOT_ERR_BAD_PATH)
3490 return NULL; /* cannot traverse parent */
3491 return err;
3493 for (;;) {
3494 err = add_ignores(ignores, root_path, parent_path, -1,
3495 ".cvsignore");
3496 if (err)
3497 break;
3498 err = add_ignores(ignores, root_path, parent_path, -1,
3499 ".gitignore");
3500 if (err)
3501 break;
3502 err = got_path_dirname(&next_parent_path, parent_path);
3503 if (err) {
3504 if (err->code == GOT_ERR_BAD_PATH)
3505 err = NULL; /* traversed everything */
3506 break;
3508 free(parent_path);
3509 parent_path = next_parent_path;
3510 next_parent_path = NULL;
3513 free(parent_path);
3514 free(next_parent_path);
3515 return err;
3518 static const struct got_error *
3519 worktree_status(struct got_worktree *worktree, const char *path,
3520 struct got_fileindex *fileindex, struct got_repository *repo,
3521 got_worktree_status_cb status_cb, void *status_arg,
3522 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3523 int report_unchanged)
3525 const struct got_error *err = NULL;
3526 int fd = -1;
3527 struct got_fileindex_diff_dir_cb fdiff_cb;
3528 struct diff_dir_cb_arg arg;
3529 char *ondisk_path = NULL;
3531 TAILQ_INIT(&arg.ignores);
3533 if (asprintf(&ondisk_path, "%s%s%s",
3534 worktree->root_path, path[0] ? "/" : "", path) == -1)
3535 return got_error_from_errno("asprintf");
3537 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3538 if (fd == -1) {
3539 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3540 errno != ELOOP)
3541 err = got_error_from_errno2("open", ondisk_path);
3542 else
3543 err = report_single_file_status(path, ondisk_path,
3544 fileindex, status_cb, status_arg, repo,
3545 report_unchanged);
3546 } else {
3547 fdiff_cb.diff_old_new = status_old_new;
3548 fdiff_cb.diff_old = status_old;
3549 fdiff_cb.diff_new = status_new;
3550 fdiff_cb.diff_traverse = status_traverse;
3551 arg.fileindex = fileindex;
3552 arg.worktree = worktree;
3553 arg.status_path = path;
3554 arg.status_path_len = strlen(path);
3555 arg.repo = repo;
3556 arg.status_cb = status_cb;
3557 arg.status_arg = status_arg;
3558 arg.cancel_cb = cancel_cb;
3559 arg.cancel_arg = cancel_arg;
3560 arg.report_unchanged = report_unchanged;
3561 arg.no_ignores = no_ignores;
3562 if (!no_ignores) {
3563 err = add_ignores_from_parent_paths(&arg.ignores,
3564 worktree->root_path, path);
3565 if (err)
3566 goto done;
3568 err = got_fileindex_diff_dir(fileindex, fd,
3569 worktree->root_path, path, repo, &fdiff_cb, &arg);
3571 done:
3572 free_ignores(&arg.ignores);
3573 if (fd != -1 && close(fd) != 0 && err == NULL)
3574 err = got_error_from_errno("close");
3575 free(ondisk_path);
3576 return err;
3579 const struct got_error *
3580 got_worktree_status(struct got_worktree *worktree,
3581 struct got_pathlist_head *paths, struct got_repository *repo,
3582 got_worktree_status_cb status_cb, void *status_arg,
3583 got_cancel_cb cancel_cb, void *cancel_arg)
3585 const struct got_error *err = NULL;
3586 char *fileindex_path = NULL;
3587 struct got_fileindex *fileindex = NULL;
3588 struct got_pathlist_entry *pe;
3590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3591 if (err)
3592 return err;
3594 TAILQ_FOREACH(pe, paths, entry) {
3595 err = worktree_status(worktree, pe->path, fileindex, repo,
3596 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3597 if (err)
3598 break;
3600 free(fileindex_path);
3601 got_fileindex_free(fileindex);
3602 return err;
3605 const struct got_error *
3606 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3607 const char *arg)
3609 const struct got_error *err = NULL;
3610 char *resolved = NULL, *cwd = NULL, *path = NULL;
3611 size_t len;
3612 struct stat sb;
3614 *wt_path = NULL;
3616 cwd = getcwd(NULL, 0);
3617 if (cwd == NULL)
3618 return got_error_from_errno("getcwd");
3620 if (lstat(arg, &sb) == -1) {
3621 if (errno != ENOENT) {
3622 err = got_error_from_errno2("lstat", arg);
3623 goto done;
3626 if (S_ISLNK(sb.st_mode)) {
3628 * We cannot use realpath(3) with symlinks since we want to
3629 * operate on the symlink itself.
3630 * But we can make the path absolute, assuming it is relative
3631 * to the current working directory, and then canonicalize it.
3633 char *abspath = NULL;
3634 char canonpath[PATH_MAX];
3635 if (!got_path_is_absolute(arg)) {
3636 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3637 err = got_error_from_errno("asprintf");
3638 goto done;
3642 err = got_canonpath(abspath ? abspath : arg, canonpath,
3643 sizeof(canonpath));
3644 if (err)
3645 goto done;
3646 resolved = strdup(canonpath);
3647 if (resolved == NULL) {
3648 err = got_error_from_errno("strdup");
3649 goto done;
3651 } else {
3652 resolved = realpath(arg, NULL);
3653 if (resolved == NULL) {
3654 if (errno != ENOENT) {
3655 err = got_error_from_errno2("realpath", arg);
3656 goto done;
3658 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3659 err = got_error_from_errno("asprintf");
3660 goto done;
3665 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3666 strlen(got_worktree_get_root_path(worktree)))) {
3667 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3668 goto done;
3671 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3672 err = got_path_skip_common_ancestor(&path,
3673 got_worktree_get_root_path(worktree), resolved);
3674 if (err)
3675 goto done;
3676 } else {
3677 path = strdup("");
3678 if (path == NULL) {
3679 err = got_error_from_errno("strdup");
3680 goto done;
3684 /* XXX status walk can't deal with trailing slash! */
3685 len = strlen(path);
3686 while (len > 0 && path[len - 1] == '/') {
3687 path[len - 1] = '\0';
3688 len--;
3690 done:
3691 free(resolved);
3692 free(cwd);
3693 if (err == NULL)
3694 *wt_path = path;
3695 else
3696 free(path);
3697 return err;
3700 struct schedule_addition_args {
3701 struct got_worktree *worktree;
3702 struct got_fileindex *fileindex;
3703 got_worktree_checkout_cb progress_cb;
3704 void *progress_arg;
3705 struct got_repository *repo;
3708 static const struct got_error *
3709 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3710 const char *relpath, struct got_object_id *blob_id,
3711 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3712 int dirfd, const char *de_name)
3714 struct schedule_addition_args *a = arg;
3715 const struct got_error *err = NULL;
3716 struct got_fileindex_entry *ie;
3717 struct stat sb;
3718 char *ondisk_path;
3720 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3721 relpath) == -1)
3722 return got_error_from_errno("asprintf");
3724 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3725 if (ie) {
3726 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3727 de_name, a->repo);
3728 if (err)
3729 goto done;
3730 /* Re-adding an existing entry is a no-op. */
3731 if (status == GOT_STATUS_ADD)
3732 goto done;
3733 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3734 if (err)
3735 goto done;
3738 if (status != GOT_STATUS_UNVERSIONED) {
3739 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3740 goto done;
3743 err = got_fileindex_entry_alloc(&ie, relpath);
3744 if (err)
3745 goto done;
3746 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3747 if (err) {
3748 got_fileindex_entry_free(ie);
3749 goto done;
3751 err = got_fileindex_entry_add(a->fileindex, ie);
3752 if (err) {
3753 got_fileindex_entry_free(ie);
3754 goto done;
3756 done:
3757 free(ondisk_path);
3758 if (err)
3759 return err;
3760 if (status == GOT_STATUS_ADD)
3761 return NULL;
3762 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3765 const struct got_error *
3766 got_worktree_schedule_add(struct got_worktree *worktree,
3767 struct got_pathlist_head *paths,
3768 got_worktree_checkout_cb progress_cb, void *progress_arg,
3769 struct got_repository *repo, int no_ignores)
3771 struct got_fileindex *fileindex = NULL;
3772 char *fileindex_path = NULL;
3773 const struct got_error *err = NULL, *sync_err, *unlockerr;
3774 struct got_pathlist_entry *pe;
3775 struct schedule_addition_args saa;
3777 err = lock_worktree(worktree, LOCK_EX);
3778 if (err)
3779 return err;
3781 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3782 if (err)
3783 goto done;
3785 saa.worktree = worktree;
3786 saa.fileindex = fileindex;
3787 saa.progress_cb = progress_cb;
3788 saa.progress_arg = progress_arg;
3789 saa.repo = repo;
3791 TAILQ_FOREACH(pe, paths, entry) {
3792 err = worktree_status(worktree, pe->path, fileindex, repo,
3793 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3794 if (err)
3795 break;
3797 sync_err = sync_fileindex(fileindex, fileindex_path);
3798 if (sync_err && err == NULL)
3799 err = sync_err;
3800 done:
3801 free(fileindex_path);
3802 if (fileindex)
3803 got_fileindex_free(fileindex);
3804 unlockerr = lock_worktree(worktree, LOCK_SH);
3805 if (unlockerr && err == NULL)
3806 err = unlockerr;
3807 return err;
3810 struct schedule_deletion_args {
3811 struct got_worktree *worktree;
3812 struct got_fileindex *fileindex;
3813 got_worktree_delete_cb progress_cb;
3814 void *progress_arg;
3815 struct got_repository *repo;
3816 int delete_local_mods;
3817 int keep_on_disk;
3818 const char *status_codes;
3821 static const struct got_error *
3822 schedule_for_deletion(void *arg, unsigned char status,
3823 unsigned char staged_status, const char *relpath,
3824 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3825 struct got_object_id *commit_id, int dirfd, const char *de_name)
3827 struct schedule_deletion_args *a = arg;
3828 const struct got_error *err = NULL;
3829 struct got_fileindex_entry *ie = NULL;
3830 struct stat sb;
3831 char *ondisk_path, *parent = NULL;
3833 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3834 if (ie == NULL)
3835 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3837 staged_status = get_staged_status(ie);
3838 if (staged_status != GOT_STATUS_NO_CHANGE) {
3839 if (staged_status == GOT_STATUS_DELETE)
3840 return NULL;
3841 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3844 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3845 relpath) == -1)
3846 return got_error_from_errno("asprintf");
3848 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3849 a->repo);
3850 if (err)
3851 goto done;
3853 if (a->status_codes) {
3854 size_t ncodes = strlen(a->status_codes);
3855 int i;
3856 for (i = 0; i < ncodes ; i++) {
3857 if (status == a->status_codes[i])
3858 break;
3860 if (i == ncodes) {
3861 /* Do not delete files in non-matching status. */
3862 free(ondisk_path);
3863 return NULL;
3865 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3866 a->status_codes[i] != GOT_STATUS_MISSING) {
3867 static char msg[64];
3868 snprintf(msg, sizeof(msg),
3869 "invalid status code '%c'", a->status_codes[i]);
3870 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3871 goto done;
3875 if (status != GOT_STATUS_NO_CHANGE) {
3876 if (status == GOT_STATUS_DELETE)
3877 goto done;
3878 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3879 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3880 goto done;
3882 if (status != GOT_STATUS_MODIFY &&
3883 status != GOT_STATUS_MISSING) {
3884 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3885 goto done;
3889 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3890 if (dirfd != -1) {
3891 if (unlinkat(dirfd, de_name, 0) != 0) {
3892 err = got_error_from_errno2("unlinkat",
3893 ondisk_path);
3894 goto done;
3896 } else if (unlink(ondisk_path) != 0) {
3897 err = got_error_from_errno2("unlink", ondisk_path);
3898 goto done;
3901 parent = dirname(ondisk_path);
3903 if (parent == NULL) {
3904 err = got_error_from_errno2("dirname", ondisk_path);
3905 goto done;
3907 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3908 if (rmdir(parent) == -1) {
3909 if (errno != ENOTEMPTY)
3910 err = got_error_from_errno2("rmdir",
3911 parent);
3912 break;
3914 parent = dirname(parent);
3915 if (parent == NULL) {
3916 err = got_error_from_errno2("dirname", parent);
3917 goto done;
3922 got_fileindex_entry_mark_deleted_from_disk(ie);
3923 done:
3924 free(ondisk_path);
3925 if (err)
3926 return err;
3927 if (status == GOT_STATUS_DELETE)
3928 return NULL;
3929 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3930 staged_status, relpath);
3933 const struct got_error *
3934 got_worktree_schedule_delete(struct got_worktree *worktree,
3935 struct got_pathlist_head *paths, int delete_local_mods,
3936 const char *status_codes,
3937 got_worktree_delete_cb progress_cb, void *progress_arg,
3938 struct got_repository *repo, int keep_on_disk)
3940 struct got_fileindex *fileindex = NULL;
3941 char *fileindex_path = NULL;
3942 const struct got_error *err = NULL, *sync_err, *unlockerr;
3943 struct got_pathlist_entry *pe;
3944 struct schedule_deletion_args sda;
3946 err = lock_worktree(worktree, LOCK_EX);
3947 if (err)
3948 return err;
3950 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3951 if (err)
3952 goto done;
3954 sda.worktree = worktree;
3955 sda.fileindex = fileindex;
3956 sda.progress_cb = progress_cb;
3957 sda.progress_arg = progress_arg;
3958 sda.repo = repo;
3959 sda.delete_local_mods = delete_local_mods;
3960 sda.keep_on_disk = keep_on_disk;
3961 sda.status_codes = status_codes;
3963 TAILQ_FOREACH(pe, paths, entry) {
3964 err = worktree_status(worktree, pe->path, fileindex, repo,
3965 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3966 if (err)
3967 break;
3969 sync_err = sync_fileindex(fileindex, fileindex_path);
3970 if (sync_err && err == NULL)
3971 err = sync_err;
3972 done:
3973 free(fileindex_path);
3974 if (fileindex)
3975 got_fileindex_free(fileindex);
3976 unlockerr = lock_worktree(worktree, LOCK_SH);
3977 if (unlockerr && err == NULL)
3978 err = unlockerr;
3979 return err;
3982 static const struct got_error *
3983 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3985 const struct got_error *err = NULL;
3986 char *line = NULL;
3987 size_t linesize = 0, n;
3988 ssize_t linelen;
3990 linelen = getline(&line, &linesize, infile);
3991 if (linelen == -1) {
3992 if (ferror(infile)) {
3993 err = got_error_from_errno("getline");
3994 goto done;
3996 return NULL;
3998 if (outfile) {
3999 n = fwrite(line, 1, linelen, outfile);
4000 if (n != linelen) {
4001 err = got_ferror(outfile, GOT_ERR_IO);
4002 goto done;
4005 if (rejectfile) {
4006 n = fwrite(line, 1, linelen, rejectfile);
4007 if (n != linelen)
4008 err = got_ferror(outfile, GOT_ERR_IO);
4010 done:
4011 free(line);
4012 return err;
4015 static const struct got_error *
4016 skip_one_line(FILE *f)
4018 char *line = NULL;
4019 size_t linesize = 0;
4020 ssize_t linelen;
4022 linelen = getline(&line, &linesize, f);
4023 if (linelen == -1) {
4024 if (ferror(f))
4025 return got_error_from_errno("getline");
4026 return NULL;
4028 free(line);
4029 return NULL;
4032 static const struct got_error *
4033 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4034 int start_old, int end_old, int start_new, int end_new,
4035 FILE *outfile, FILE *rejectfile)
4037 const struct got_error *err;
4039 /* Copy old file's lines leading up to patch. */
4040 while (!feof(f1) && *line_cur1 < start_old) {
4041 err = copy_one_line(f1, outfile, NULL);
4042 if (err)
4043 return err;
4044 (*line_cur1)++;
4046 /* Skip new file's lines leading up to patch. */
4047 while (!feof(f2) && *line_cur2 < start_new) {
4048 if (rejectfile)
4049 err = copy_one_line(f2, NULL, rejectfile);
4050 else
4051 err = skip_one_line(f2);
4052 if (err)
4053 return err;
4054 (*line_cur2)++;
4056 /* Copy patched lines. */
4057 while (!feof(f2) && *line_cur2 <= end_new) {
4058 err = copy_one_line(f2, outfile, NULL);
4059 if (err)
4060 return err;
4061 (*line_cur2)++;
4063 /* Skip over old file's replaced lines. */
4064 while (!feof(f1) && *line_cur1 <= end_old) {
4065 if (rejectfile)
4066 err = copy_one_line(f1, NULL, rejectfile);
4067 else
4068 err = skip_one_line(f1);
4069 if (err)
4070 return err;
4071 (*line_cur1)++;
4074 return NULL;
4077 static const struct got_error *
4078 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4079 FILE *outfile, FILE *rejectfile)
4081 const struct got_error *err;
4083 if (outfile) {
4084 /* Copy old file's lines until EOF. */
4085 while (!feof(f1)) {
4086 err = copy_one_line(f1, outfile, NULL);
4087 if (err)
4088 return err;
4089 (*line_cur1)++;
4092 if (rejectfile) {
4093 /* Copy new file's lines until EOF. */
4094 while (!feof(f2)) {
4095 err = copy_one_line(f2, NULL, rejectfile);
4096 if (err)
4097 return err;
4098 (*line_cur2)++;
4102 return NULL;
4105 static const struct got_error *
4106 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4107 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4108 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4109 int *line_cur2, FILE *outfile, FILE *rejectfile,
4110 got_worktree_patch_cb patch_cb, void *patch_arg)
4112 const struct got_error *err = NULL;
4113 int start_old = change->cv.a;
4114 int end_old = change->cv.b;
4115 int start_new = change->cv.c;
4116 int end_new = change->cv.d;
4117 long pos1, pos2;
4118 FILE *hunkfile;
4120 *choice = GOT_PATCH_CHOICE_NONE;
4122 hunkfile = got_opentemp();
4123 if (hunkfile == NULL)
4124 return got_error_from_errno("got_opentemp");
4126 pos1 = ftell(f1);
4127 pos2 = ftell(f2);
4129 /* XXX TODO needs error checking */
4130 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4132 if (fseek(f1, pos1, SEEK_SET) == -1) {
4133 err = got_ferror(f1, GOT_ERR_IO);
4134 goto done;
4136 if (fseek(f2, pos2, SEEK_SET) == -1) {
4137 err = got_ferror(f1, GOT_ERR_IO);
4138 goto done;
4140 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4141 err = got_ferror(hunkfile, GOT_ERR_IO);
4142 goto done;
4145 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4146 hunkfile, n, nchanges);
4147 if (err)
4148 goto done;
4150 switch (*choice) {
4151 case GOT_PATCH_CHOICE_YES:
4152 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4153 end_old, start_new, end_new, outfile, rejectfile);
4154 break;
4155 case GOT_PATCH_CHOICE_NO:
4156 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4157 end_old, start_new, end_new, rejectfile, outfile);
4158 break;
4159 case GOT_PATCH_CHOICE_QUIT:
4160 break;
4161 default:
4162 err = got_error(GOT_ERR_PATCH_CHOICE);
4163 break;
4165 done:
4166 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4167 err = got_error_from_errno("fclose");
4168 return err;
4171 struct revert_file_args {
4172 struct got_worktree *worktree;
4173 struct got_fileindex *fileindex;
4174 got_worktree_checkout_cb progress_cb;
4175 void *progress_arg;
4176 got_worktree_patch_cb patch_cb;
4177 void *patch_arg;
4178 struct got_repository *repo;
4181 static const struct got_error *
4182 create_patched_content(char **path_outfile, int reverse_patch,
4183 struct got_object_id *blob_id, const char *path2,
4184 int dirfd2, const char *de_name2,
4185 const char *relpath, struct got_repository *repo,
4186 got_worktree_patch_cb patch_cb, void *patch_arg)
4188 const struct got_error *err;
4189 struct got_blob_object *blob = NULL;
4190 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4191 int fd2 = -1;
4192 char link_target[PATH_MAX];
4193 ssize_t link_len = 0;
4194 char *path1 = NULL, *id_str = NULL;
4195 struct stat sb1, sb2;
4196 struct got_diff_changes *changes = NULL;
4197 struct got_diff_state *ds = NULL;
4198 struct got_diff_args *args = NULL;
4199 struct got_diff_change *change;
4200 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4201 int n = 0;
4203 *path_outfile = NULL;
4205 err = got_object_id_str(&id_str, blob_id);
4206 if (err)
4207 return err;
4209 if (dirfd2 != -1) {
4210 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4211 if (fd2 == -1) {
4212 if (errno != ELOOP) {
4213 err = got_error_from_errno2("openat", path2);
4214 goto done;
4216 link_len = readlinkat(dirfd2, de_name2,
4217 link_target, sizeof(link_target));
4218 if (link_len == -1)
4219 return got_error_from_errno2("readlinkat", path2);
4220 sb2.st_mode = S_IFLNK;
4221 sb2.st_size = link_len;
4223 } else {
4224 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4225 if (fd2 == -1) {
4226 if (errno != ELOOP) {
4227 err = got_error_from_errno2("open", path2);
4228 goto done;
4230 link_len = readlink(path2, link_target,
4231 sizeof(link_target));
4232 if (link_len == -1)
4233 return got_error_from_errno2("readlink", path2);
4234 sb2.st_mode = S_IFLNK;
4235 sb2.st_size = link_len;
4238 if (fd2 != -1) {
4239 if (fstat(fd2, &sb2) == -1) {
4240 err = got_error_from_errno2("fstat", path2);
4241 goto done;
4244 f2 = fdopen(fd2, "r");
4245 if (f2 == NULL) {
4246 err = got_error_from_errno2("fdopen", path2);
4247 goto done;
4249 fd2 = -1;
4250 } else {
4251 size_t n;
4252 f2 = got_opentemp();
4253 if (f2 == NULL) {
4254 err = got_error_from_errno2("got_opentemp", path2);
4255 goto done;
4257 n = fwrite(link_target, 1, link_len, f2);
4258 if (n != link_len) {
4259 err = got_ferror(f2, GOT_ERR_IO);
4260 goto done;
4262 if (fflush(f2) == EOF) {
4263 err = got_error_from_errno("fflush");
4264 goto done;
4266 rewind(f2);
4269 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4270 if (err)
4271 goto done;
4273 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4274 if (err)
4275 goto done;
4277 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4278 if (err)
4279 goto done;
4281 if (stat(path1, &sb1) == -1) {
4282 err = got_error_from_errno2("stat", path1);
4283 goto done;
4286 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4287 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4288 if (err)
4289 goto done;
4291 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4292 if (err)
4293 goto done;
4295 if (fseek(f1, 0L, SEEK_SET) == -1)
4296 return got_ferror(f1, GOT_ERR_IO);
4297 if (fseek(f2, 0L, SEEK_SET) == -1)
4298 return got_ferror(f2, GOT_ERR_IO);
4299 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4300 int choice;
4301 err = apply_or_reject_change(&choice, change, ++n,
4302 changes->nchanges, ds, args, diff_flags, relpath,
4303 f1, f2, &line_cur1, &line_cur2,
4304 reverse_patch ? NULL : outfile,
4305 reverse_patch ? outfile : NULL,
4306 patch_cb, patch_arg);
4307 if (err)
4308 goto done;
4309 if (choice == GOT_PATCH_CHOICE_YES)
4310 have_content = 1;
4311 else if (choice == GOT_PATCH_CHOICE_QUIT)
4312 break;
4314 if (have_content) {
4315 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4316 reverse_patch ? NULL : outfile,
4317 reverse_patch ? outfile : NULL);
4318 if (err)
4319 goto done;
4321 if (!S_ISLNK(sb2.st_mode)) {
4322 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4323 err = got_error_from_errno2("chmod", path2);
4324 goto done;
4328 done:
4329 free(id_str);
4330 if (blob)
4331 got_object_blob_close(blob);
4332 if (f1 && fclose(f1) == EOF && err == NULL)
4333 err = got_error_from_errno2("fclose", path1);
4334 if (f2 && fclose(f2) == EOF && err == NULL)
4335 err = got_error_from_errno2("fclose", path2);
4336 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4337 err = got_error_from_errno2("close", path2);
4338 if (outfile && fclose(outfile) == EOF && err == NULL)
4339 err = got_error_from_errno2("fclose", *path_outfile);
4340 if (path1 && unlink(path1) == -1 && err == NULL)
4341 err = got_error_from_errno2("unlink", path1);
4342 if (err || !have_content) {
4343 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4344 err = got_error_from_errno2("unlink", *path_outfile);
4345 free(*path_outfile);
4346 *path_outfile = NULL;
4348 free(args);
4349 if (ds) {
4350 got_diff_state_free(ds);
4351 free(ds);
4353 if (changes)
4354 got_diff_free_changes(changes);
4355 free(path1);
4356 return err;
4359 static const struct got_error *
4360 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4361 const char *relpath, struct got_object_id *blob_id,
4362 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4363 int dirfd, const char *de_name)
4365 struct revert_file_args *a = arg;
4366 const struct got_error *err = NULL;
4367 char *parent_path = NULL;
4368 struct got_fileindex_entry *ie;
4369 struct got_tree_object *tree = NULL;
4370 struct got_object_id *tree_id = NULL;
4371 const struct got_tree_entry *te = NULL;
4372 char *tree_path = NULL, *te_name;
4373 char *ondisk_path = NULL, *path_content = NULL;
4374 struct got_blob_object *blob = NULL;
4376 /* Reverting a staged deletion is a no-op. */
4377 if (status == GOT_STATUS_DELETE &&
4378 staged_status != GOT_STATUS_NO_CHANGE)
4379 return NULL;
4381 if (status == GOT_STATUS_UNVERSIONED)
4382 return (*a->progress_cb)(a->progress_arg,
4383 GOT_STATUS_UNVERSIONED, relpath);
4385 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4386 if (ie == NULL)
4387 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4389 /* Construct in-repository path of tree which contains this blob. */
4390 err = got_path_dirname(&parent_path, ie->path);
4391 if (err) {
4392 if (err->code != GOT_ERR_BAD_PATH)
4393 goto done;
4394 parent_path = strdup("/");
4395 if (parent_path == NULL) {
4396 err = got_error_from_errno("strdup");
4397 goto done;
4400 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4401 tree_path = strdup(parent_path);
4402 if (tree_path == NULL) {
4403 err = got_error_from_errno("strdup");
4404 goto done;
4406 } else {
4407 if (got_path_is_root_dir(parent_path)) {
4408 tree_path = strdup(a->worktree->path_prefix);
4409 if (tree_path == NULL) {
4410 err = got_error_from_errno("strdup");
4411 goto done;
4413 } else {
4414 if (asprintf(&tree_path, "%s/%s",
4415 a->worktree->path_prefix, parent_path) == -1) {
4416 err = got_error_from_errno("asprintf");
4417 goto done;
4422 err = got_object_id_by_path(&tree_id, a->repo,
4423 a->worktree->base_commit_id, tree_path);
4424 if (err) {
4425 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4426 (status == GOT_STATUS_ADD ||
4427 staged_status == GOT_STATUS_ADD)))
4428 goto done;
4429 } else {
4430 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4431 if (err)
4432 goto done;
4434 err = got_path_basename(&te_name, ie->path);
4435 if (err)
4436 goto done;
4438 te = got_object_tree_find_entry(tree, te_name);
4439 free(te_name);
4440 if (te == NULL && status != GOT_STATUS_ADD &&
4441 staged_status != GOT_STATUS_ADD) {
4442 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4443 goto done;
4447 switch (status) {
4448 case GOT_STATUS_ADD:
4449 if (a->patch_cb) {
4450 int choice = GOT_PATCH_CHOICE_NONE;
4451 err = (*a->patch_cb)(&choice, a->patch_arg,
4452 status, ie->path, NULL, 1, 1);
4453 if (err)
4454 goto done;
4455 if (choice != GOT_PATCH_CHOICE_YES)
4456 break;
4458 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4459 ie->path);
4460 if (err)
4461 goto done;
4462 got_fileindex_entry_remove(a->fileindex, ie);
4463 break;
4464 case GOT_STATUS_DELETE:
4465 if (a->patch_cb) {
4466 int choice = GOT_PATCH_CHOICE_NONE;
4467 err = (*a->patch_cb)(&choice, a->patch_arg,
4468 status, ie->path, NULL, 1, 1);
4469 if (err)
4470 goto done;
4471 if (choice != GOT_PATCH_CHOICE_YES)
4472 break;
4474 /* fall through */
4475 case GOT_STATUS_MODIFY:
4476 case GOT_STATUS_MODE_CHANGE:
4477 case GOT_STATUS_CONFLICT:
4478 case GOT_STATUS_MISSING: {
4479 struct got_object_id id;
4480 if (staged_status == GOT_STATUS_ADD ||
4481 staged_status == GOT_STATUS_MODIFY) {
4482 memcpy(id.sha1, ie->staged_blob_sha1,
4483 SHA1_DIGEST_LENGTH);
4484 } else
4485 memcpy(id.sha1, ie->blob_sha1,
4486 SHA1_DIGEST_LENGTH);
4487 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4488 if (err)
4489 goto done;
4491 if (asprintf(&ondisk_path, "%s/%s",
4492 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4493 err = got_error_from_errno("asprintf");
4494 goto done;
4497 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4498 status == GOT_STATUS_CONFLICT)) {
4499 int is_bad_symlink = 0;
4500 err = create_patched_content(&path_content, 1, &id,
4501 ondisk_path, dirfd, de_name, ie->path, a->repo,
4502 a->patch_cb, a->patch_arg);
4503 if (err || path_content == NULL)
4504 break;
4505 if (te && S_ISLNK(te->mode)) {
4506 if (unlink(path_content) == -1) {
4507 err = got_error_from_errno2("unlink",
4508 path_content);
4509 break;
4511 err = install_symlink(&is_bad_symlink,
4512 a->worktree, ondisk_path, ie->path,
4513 blob, 0, 1, 0, a->repo,
4514 a->progress_cb, a->progress_arg);
4515 } else {
4516 if (rename(path_content, ondisk_path) == -1) {
4517 err = got_error_from_errno3("rename",
4518 path_content, ondisk_path);
4519 goto done;
4522 } else {
4523 int is_bad_symlink = 0;
4524 if (te && S_ISLNK(te->mode)) {
4525 err = install_symlink(&is_bad_symlink,
4526 a->worktree, ondisk_path, ie->path,
4527 blob, 0, 1, 0, a->repo,
4528 a->progress_cb, a->progress_arg);
4529 } else {
4530 err = install_blob(a->worktree, ondisk_path,
4531 ie->path,
4532 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4533 got_fileindex_perms_to_st(ie), blob,
4534 0, 1, 0, 0, a->repo,
4535 a->progress_cb, a->progress_arg);
4537 if (err)
4538 goto done;
4539 if (status == GOT_STATUS_DELETE ||
4540 status == GOT_STATUS_MODE_CHANGE) {
4541 err = got_fileindex_entry_update(ie,
4542 ondisk_path, blob->id.sha1,
4543 a->worktree->base_commit_id->sha1, 1);
4544 if (err)
4545 goto done;
4547 if (is_bad_symlink) {
4548 got_fileindex_entry_filetype_set(ie,
4549 GOT_FILEIDX_MODE_BAD_SYMLINK);
4552 break;
4554 default:
4555 break;
4557 done:
4558 free(ondisk_path);
4559 free(path_content);
4560 free(parent_path);
4561 free(tree_path);
4562 if (blob)
4563 got_object_blob_close(blob);
4564 if (tree)
4565 got_object_tree_close(tree);
4566 free(tree_id);
4567 return err;
4570 const struct got_error *
4571 got_worktree_revert(struct got_worktree *worktree,
4572 struct got_pathlist_head *paths,
4573 got_worktree_checkout_cb progress_cb, void *progress_arg,
4574 got_worktree_patch_cb patch_cb, void *patch_arg,
4575 struct got_repository *repo)
4577 struct got_fileindex *fileindex = NULL;
4578 char *fileindex_path = NULL;
4579 const struct got_error *err = NULL, *unlockerr = NULL;
4580 const struct got_error *sync_err = NULL;
4581 struct got_pathlist_entry *pe;
4582 struct revert_file_args rfa;
4584 err = lock_worktree(worktree, LOCK_EX);
4585 if (err)
4586 return err;
4588 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4589 if (err)
4590 goto done;
4592 rfa.worktree = worktree;
4593 rfa.fileindex = fileindex;
4594 rfa.progress_cb = progress_cb;
4595 rfa.progress_arg = progress_arg;
4596 rfa.patch_cb = patch_cb;
4597 rfa.patch_arg = patch_arg;
4598 rfa.repo = repo;
4599 TAILQ_FOREACH(pe, paths, entry) {
4600 err = worktree_status(worktree, pe->path, fileindex, repo,
4601 revert_file, &rfa, NULL, NULL, 0, 0);
4602 if (err)
4603 break;
4605 sync_err = sync_fileindex(fileindex, fileindex_path);
4606 if (sync_err && err == NULL)
4607 err = sync_err;
4608 done:
4609 free(fileindex_path);
4610 if (fileindex)
4611 got_fileindex_free(fileindex);
4612 unlockerr = lock_worktree(worktree, LOCK_SH);
4613 if (unlockerr && err == NULL)
4614 err = unlockerr;
4615 return err;
4618 static void
4619 free_commitable(struct got_commitable *ct)
4621 free(ct->path);
4622 free(ct->in_repo_path);
4623 free(ct->ondisk_path);
4624 free(ct->blob_id);
4625 free(ct->base_blob_id);
4626 free(ct->staged_blob_id);
4627 free(ct->base_commit_id);
4628 free(ct);
4631 struct collect_commitables_arg {
4632 struct got_pathlist_head *commitable_paths;
4633 struct got_repository *repo;
4634 struct got_worktree *worktree;
4635 struct got_fileindex *fileindex;
4636 int have_staged_files;
4637 int allow_bad_symlinks;
4640 static const struct got_error *
4641 collect_commitables(void *arg, unsigned char status,
4642 unsigned char staged_status, const char *relpath,
4643 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4644 struct got_object_id *commit_id, int dirfd, const char *de_name)
4646 struct collect_commitables_arg *a = arg;
4647 const struct got_error *err = NULL;
4648 struct got_commitable *ct = NULL;
4649 struct got_pathlist_entry *new = NULL;
4650 char *parent_path = NULL, *path = NULL;
4651 struct stat sb;
4653 if (a->have_staged_files) {
4654 if (staged_status != GOT_STATUS_MODIFY &&
4655 staged_status != GOT_STATUS_ADD &&
4656 staged_status != GOT_STATUS_DELETE)
4657 return NULL;
4658 } else {
4659 if (status == GOT_STATUS_CONFLICT)
4660 return got_error(GOT_ERR_COMMIT_CONFLICT);
4662 if (status != GOT_STATUS_MODIFY &&
4663 status != GOT_STATUS_MODE_CHANGE &&
4664 status != GOT_STATUS_ADD &&
4665 status != GOT_STATUS_DELETE)
4666 return NULL;
4669 if (asprintf(&path, "/%s", relpath) == -1) {
4670 err = got_error_from_errno("asprintf");
4671 goto done;
4673 if (strcmp(path, "/") == 0) {
4674 parent_path = strdup("");
4675 if (parent_path == NULL)
4676 return got_error_from_errno("strdup");
4677 } else {
4678 err = got_path_dirname(&parent_path, path);
4679 if (err)
4680 return err;
4683 ct = calloc(1, sizeof(*ct));
4684 if (ct == NULL) {
4685 err = got_error_from_errno("calloc");
4686 goto done;
4689 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4690 relpath) == -1) {
4691 err = got_error_from_errno("asprintf");
4692 goto done;
4695 if (staged_status == GOT_STATUS_ADD ||
4696 staged_status == GOT_STATUS_MODIFY) {
4697 struct got_fileindex_entry *ie;
4698 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4699 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4700 case GOT_FILEIDX_MODE_REGULAR_FILE:
4701 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4702 ct->mode = S_IFREG;
4703 break;
4704 case GOT_FILEIDX_MODE_SYMLINK:
4705 ct->mode = S_IFLNK;
4706 break;
4707 default:
4708 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4709 goto done;
4711 ct->mode |= got_fileindex_entry_perms_get(ie);
4712 } else if (status != GOT_STATUS_DELETE &&
4713 staged_status != GOT_STATUS_DELETE) {
4714 if (dirfd != -1) {
4715 if (fstatat(dirfd, de_name, &sb,
4716 AT_SYMLINK_NOFOLLOW) == -1) {
4717 err = got_error_from_errno2("fstatat",
4718 ct->ondisk_path);
4719 goto done;
4721 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4722 err = got_error_from_errno2("lstat", ct->ondisk_path);
4723 goto done;
4725 ct->mode = sb.st_mode;
4728 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4729 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4730 relpath) == -1) {
4731 err = got_error_from_errno("asprintf");
4732 goto done;
4735 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4736 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4737 int is_bad_symlink;
4738 char target_path[PATH_MAX];
4739 ssize_t target_len;
4740 target_len = readlink(ct->ondisk_path, target_path,
4741 sizeof(target_path));
4742 if (target_len == -1) {
4743 err = got_error_from_errno2("readlink",
4744 ct->ondisk_path);
4745 goto done;
4747 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4748 target_len, ct->ondisk_path, a->worktree->root_path);
4749 if (err)
4750 goto done;
4751 if (is_bad_symlink) {
4752 err = got_error_path(ct->ondisk_path,
4753 GOT_ERR_BAD_SYMLINK);
4754 goto done;
4759 ct->status = status;
4760 ct->staged_status = staged_status;
4761 ct->blob_id = NULL; /* will be filled in when blob gets created */
4762 if (ct->status != GOT_STATUS_ADD &&
4763 ct->staged_status != GOT_STATUS_ADD) {
4764 ct->base_blob_id = got_object_id_dup(blob_id);
4765 if (ct->base_blob_id == NULL) {
4766 err = got_error_from_errno("got_object_id_dup");
4767 goto done;
4769 ct->base_commit_id = got_object_id_dup(commit_id);
4770 if (ct->base_commit_id == NULL) {
4771 err = got_error_from_errno("got_object_id_dup");
4772 goto done;
4775 if (ct->staged_status == GOT_STATUS_ADD ||
4776 ct->staged_status == GOT_STATUS_MODIFY) {
4777 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4778 if (ct->staged_blob_id == NULL) {
4779 err = got_error_from_errno("got_object_id_dup");
4780 goto done;
4783 ct->path = strdup(path);
4784 if (ct->path == NULL) {
4785 err = got_error_from_errno("strdup");
4786 goto done;
4788 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4789 done:
4790 if (ct && (err || new == NULL))
4791 free_commitable(ct);
4792 free(parent_path);
4793 free(path);
4794 return err;
4797 static const struct got_error *write_tree(struct got_object_id **, int *,
4798 struct got_tree_object *, const char *, struct got_pathlist_head *,
4799 got_worktree_status_cb status_cb, void *status_arg,
4800 struct got_repository *);
4802 static const struct got_error *
4803 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4804 struct got_tree_entry *te, const char *parent_path,
4805 struct got_pathlist_head *commitable_paths,
4806 got_worktree_status_cb status_cb, void *status_arg,
4807 struct got_repository *repo)
4809 const struct got_error *err = NULL;
4810 struct got_tree_object *subtree;
4811 char *subpath;
4813 if (asprintf(&subpath, "%s%s%s", parent_path,
4814 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4815 return got_error_from_errno("asprintf");
4817 err = got_object_open_as_tree(&subtree, repo, &te->id);
4818 if (err)
4819 return err;
4821 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4822 commitable_paths, status_cb, status_arg, repo);
4823 got_object_tree_close(subtree);
4824 free(subpath);
4825 return err;
4828 static const struct got_error *
4829 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4831 const struct got_error *err = NULL;
4832 char *ct_parent_path = NULL;
4834 *match = 0;
4836 if (strchr(ct->in_repo_path, '/') == NULL) {
4837 *match = got_path_is_root_dir(path);
4838 return NULL;
4841 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4842 if (err)
4843 return err;
4844 *match = (strcmp(path, ct_parent_path) == 0);
4845 free(ct_parent_path);
4846 return err;
4849 static mode_t
4850 get_ct_file_mode(struct got_commitable *ct)
4852 if (S_ISLNK(ct->mode))
4853 return S_IFLNK;
4855 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4858 static const struct got_error *
4859 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4860 struct got_tree_entry *te, struct got_commitable *ct)
4862 const struct got_error *err = NULL;
4864 *new_te = NULL;
4866 err = got_object_tree_entry_dup(new_te, te);
4867 if (err)
4868 goto done;
4870 (*new_te)->mode = get_ct_file_mode(ct);
4872 if (ct->staged_status == GOT_STATUS_MODIFY)
4873 memcpy(&(*new_te)->id, ct->staged_blob_id,
4874 sizeof((*new_te)->id));
4875 else
4876 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4877 done:
4878 if (err && *new_te) {
4879 free(*new_te);
4880 *new_te = NULL;
4882 return err;
4885 static const struct got_error *
4886 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4887 struct got_commitable *ct)
4889 const struct got_error *err = NULL;
4890 char *ct_name = NULL;
4892 *new_te = NULL;
4894 *new_te = calloc(1, sizeof(**new_te));
4895 if (*new_te == NULL)
4896 return got_error_from_errno("calloc");
4898 err = got_path_basename(&ct_name, ct->path);
4899 if (err)
4900 goto done;
4901 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4902 sizeof((*new_te)->name)) {
4903 err = got_error(GOT_ERR_NO_SPACE);
4904 goto done;
4907 (*new_te)->mode = get_ct_file_mode(ct);
4909 if (ct->staged_status == GOT_STATUS_ADD)
4910 memcpy(&(*new_te)->id, ct->staged_blob_id,
4911 sizeof((*new_te)->id));
4912 else
4913 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4914 done:
4915 free(ct_name);
4916 if (err && *new_te) {
4917 free(*new_te);
4918 *new_te = NULL;
4920 return err;
4923 static const struct got_error *
4924 insert_tree_entry(struct got_tree_entry *new_te,
4925 struct got_pathlist_head *paths)
4927 const struct got_error *err = NULL;
4928 struct got_pathlist_entry *new_pe;
4930 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4931 if (err)
4932 return err;
4933 if (new_pe == NULL)
4934 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4935 return NULL;
4938 static const struct got_error *
4939 report_ct_status(struct got_commitable *ct,
4940 got_worktree_status_cb status_cb, void *status_arg)
4942 const char *ct_path = ct->path;
4943 unsigned char status;
4945 while (ct_path[0] == '/')
4946 ct_path++;
4948 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4949 status = ct->staged_status;
4950 else
4951 status = ct->status;
4953 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4954 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4957 static const struct got_error *
4958 match_modified_subtree(int *modified, struct got_tree_entry *te,
4959 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4961 const struct got_error *err = NULL;
4962 struct got_pathlist_entry *pe;
4963 char *te_path;
4965 *modified = 0;
4967 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4968 got_path_is_root_dir(base_tree_path) ? "" : "/",
4969 te->name) == -1)
4970 return got_error_from_errno("asprintf");
4972 TAILQ_FOREACH(pe, commitable_paths, entry) {
4973 struct got_commitable *ct = pe->data;
4974 *modified = got_path_is_child(ct->in_repo_path, te_path,
4975 strlen(te_path));
4976 if (*modified)
4977 break;
4980 free(te_path);
4981 return err;
4984 static const struct got_error *
4985 match_deleted_or_modified_ct(struct got_commitable **ctp,
4986 struct got_tree_entry *te, const char *base_tree_path,
4987 struct got_pathlist_head *commitable_paths)
4989 const struct got_error *err = NULL;
4990 struct got_pathlist_entry *pe;
4992 *ctp = NULL;
4994 TAILQ_FOREACH(pe, commitable_paths, entry) {
4995 struct got_commitable *ct = pe->data;
4996 char *ct_name = NULL;
4997 int path_matches;
4999 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5000 if (ct->status != GOT_STATUS_MODIFY &&
5001 ct->status != GOT_STATUS_MODE_CHANGE &&
5002 ct->status != GOT_STATUS_DELETE)
5003 continue;
5004 } else {
5005 if (ct->staged_status != GOT_STATUS_MODIFY &&
5006 ct->staged_status != GOT_STATUS_DELETE)
5007 continue;
5010 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5011 continue;
5013 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5014 if (err)
5015 return err;
5016 if (!path_matches)
5017 continue;
5019 err = got_path_basename(&ct_name, pe->path);
5020 if (err)
5021 return err;
5023 if (strcmp(te->name, ct_name) != 0) {
5024 free(ct_name);
5025 continue;
5027 free(ct_name);
5029 *ctp = ct;
5030 break;
5033 return err;
5036 static const struct got_error *
5037 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5038 const char *child_path, const char *path_base_tree,
5039 struct got_pathlist_head *commitable_paths,
5040 got_worktree_status_cb status_cb, void *status_arg,
5041 struct got_repository *repo)
5043 const struct got_error *err = NULL;
5044 struct got_tree_entry *new_te;
5045 char *subtree_path;
5046 struct got_object_id *id = NULL;
5047 int nentries;
5049 *new_tep = NULL;
5051 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5052 got_path_is_root_dir(path_base_tree) ? "" : "/",
5053 child_path) == -1)
5054 return got_error_from_errno("asprintf");
5056 new_te = calloc(1, sizeof(*new_te));
5057 if (new_te == NULL)
5058 return got_error_from_errno("calloc");
5059 new_te->mode = S_IFDIR;
5061 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5062 sizeof(new_te->name)) {
5063 err = got_error(GOT_ERR_NO_SPACE);
5064 goto done;
5066 err = write_tree(&id, &nentries, NULL, subtree_path,
5067 commitable_paths, status_cb, status_arg, repo);
5068 if (err) {
5069 free(new_te);
5070 goto done;
5072 memcpy(&new_te->id, id, sizeof(new_te->id));
5073 done:
5074 free(id);
5075 free(subtree_path);
5076 if (err == NULL)
5077 *new_tep = new_te;
5078 return err;
5081 static const struct got_error *
5082 write_tree(struct got_object_id **new_tree_id, int *nentries,
5083 struct got_tree_object *base_tree, const char *path_base_tree,
5084 struct got_pathlist_head *commitable_paths,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *repo)
5088 const struct got_error *err = NULL;
5089 struct got_pathlist_head paths;
5090 struct got_tree_entry *te, *new_te = NULL;
5091 struct got_pathlist_entry *pe;
5093 TAILQ_INIT(&paths);
5094 *nentries = 0;
5096 /* Insert, and recurse into, newly added entries first. */
5097 TAILQ_FOREACH(pe, commitable_paths, entry) {
5098 struct got_commitable *ct = pe->data;
5099 char *child_path = NULL, *slash;
5101 if ((ct->status != GOT_STATUS_ADD &&
5102 ct->staged_status != GOT_STATUS_ADD) ||
5103 (ct->flags & GOT_COMMITABLE_ADDED))
5104 continue;
5106 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5107 strlen(path_base_tree)))
5108 continue;
5110 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5111 ct->in_repo_path);
5112 if (err)
5113 goto done;
5115 slash = strchr(child_path, '/');
5116 if (slash == NULL) {
5117 err = alloc_added_blob_tree_entry(&new_te, ct);
5118 if (err)
5119 goto done;
5120 err = report_ct_status(ct, status_cb, status_arg);
5121 if (err)
5122 goto done;
5123 ct->flags |= GOT_COMMITABLE_ADDED;
5124 err = insert_tree_entry(new_te, &paths);
5125 if (err)
5126 goto done;
5127 (*nentries)++;
5128 } else {
5129 *slash = '\0'; /* trim trailing path components */
5130 if (base_tree == NULL ||
5131 got_object_tree_find_entry(base_tree, child_path)
5132 == NULL) {
5133 err = make_subtree_for_added_blob(&new_te,
5134 child_path, path_base_tree,
5135 commitable_paths, status_cb, status_arg,
5136 repo);
5137 if (err)
5138 goto done;
5139 err = insert_tree_entry(new_te, &paths);
5140 if (err)
5141 goto done;
5142 (*nentries)++;
5147 if (base_tree) {
5148 int i, nbase_entries;
5149 /* Handle modified and deleted entries. */
5150 nbase_entries = got_object_tree_get_nentries(base_tree);
5151 for (i = 0; i < nbase_entries; i++) {
5152 struct got_commitable *ct = NULL;
5154 te = got_object_tree_get_entry(base_tree, i);
5155 if (got_object_tree_entry_is_submodule(te)) {
5156 /* Entry is a submodule; just copy it. */
5157 err = got_object_tree_entry_dup(&new_te, te);
5158 if (err)
5159 goto done;
5160 err = insert_tree_entry(new_te, &paths);
5161 if (err)
5162 goto done;
5163 (*nentries)++;
5164 continue;
5167 if (S_ISDIR(te->mode)) {
5168 int modified;
5169 err = got_object_tree_entry_dup(&new_te, te);
5170 if (err)
5171 goto done;
5172 err = match_modified_subtree(&modified, te,
5173 path_base_tree, commitable_paths);
5174 if (err)
5175 goto done;
5176 /* Avoid recursion into unmodified subtrees. */
5177 if (modified) {
5178 struct got_object_id *new_id;
5179 int nsubentries;
5180 err = write_subtree(&new_id,
5181 &nsubentries, te,
5182 path_base_tree, commitable_paths,
5183 status_cb, status_arg, repo);
5184 if (err)
5185 goto done;
5186 if (nsubentries == 0) {
5187 /* All entries were deleted. */
5188 free(new_id);
5189 continue;
5191 memcpy(&new_te->id, new_id,
5192 sizeof(new_te->id));
5193 free(new_id);
5195 err = insert_tree_entry(new_te, &paths);
5196 if (err)
5197 goto done;
5198 (*nentries)++;
5199 continue;
5202 err = match_deleted_or_modified_ct(&ct, te,
5203 path_base_tree, commitable_paths);
5204 if (err)
5205 goto done;
5206 if (ct) {
5207 /* NB: Deleted entries get dropped here. */
5208 if (ct->status == GOT_STATUS_MODIFY ||
5209 ct->status == GOT_STATUS_MODE_CHANGE ||
5210 ct->staged_status == GOT_STATUS_MODIFY) {
5211 err = alloc_modified_blob_tree_entry(
5212 &new_te, te, ct);
5213 if (err)
5214 goto done;
5215 err = insert_tree_entry(new_te, &paths);
5216 if (err)
5217 goto done;
5218 (*nentries)++;
5220 err = report_ct_status(ct, status_cb,
5221 status_arg);
5222 if (err)
5223 goto done;
5224 } else {
5225 /* Entry is unchanged; just copy it. */
5226 err = got_object_tree_entry_dup(&new_te, te);
5227 if (err)
5228 goto done;
5229 err = insert_tree_entry(new_te, &paths);
5230 if (err)
5231 goto done;
5232 (*nentries)++;
5237 /* Write new list of entries; deleted entries have been dropped. */
5238 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5239 done:
5240 got_pathlist_free(&paths);
5241 return err;
5244 static const struct got_error *
5245 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5246 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5247 int have_staged_files)
5249 const struct got_error *err = NULL;
5250 struct got_pathlist_entry *pe;
5252 TAILQ_FOREACH(pe, commitable_paths, entry) {
5253 struct got_fileindex_entry *ie;
5254 struct got_commitable *ct = pe->data;
5256 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5257 if (ie) {
5258 if (ct->status == GOT_STATUS_DELETE ||
5259 ct->staged_status == GOT_STATUS_DELETE) {
5260 got_fileindex_entry_remove(fileindex, ie);
5261 } else if (ct->staged_status == GOT_STATUS_ADD ||
5262 ct->staged_status == GOT_STATUS_MODIFY) {
5263 got_fileindex_entry_stage_set(ie,
5264 GOT_FILEIDX_STAGE_NONE);
5265 err = got_fileindex_entry_update(ie,
5266 ct->ondisk_path, ct->staged_blob_id->sha1,
5267 new_base_commit_id->sha1,
5268 !have_staged_files);
5269 } else
5270 err = got_fileindex_entry_update(ie,
5271 ct->ondisk_path, ct->blob_id->sha1,
5272 new_base_commit_id->sha1,
5273 !have_staged_files);
5274 } else {
5275 err = got_fileindex_entry_alloc(&ie, pe->path);
5276 if (err)
5277 break;
5278 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5279 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5280 if (err) {
5281 got_fileindex_entry_free(ie);
5282 break;
5284 err = got_fileindex_entry_add(fileindex, ie);
5285 if (err) {
5286 got_fileindex_entry_free(ie);
5287 break;
5291 return err;
5295 static const struct got_error *
5296 check_out_of_date(const char *in_repo_path, unsigned char status,
5297 unsigned char staged_status, struct got_object_id *base_blob_id,
5298 struct got_object_id *base_commit_id,
5299 struct got_object_id *head_commit_id, struct got_repository *repo,
5300 int ood_errcode)
5302 const struct got_error *err = NULL;
5303 struct got_object_id *id = NULL;
5305 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5306 /* Trivial case: base commit == head commit */
5307 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5308 return NULL;
5310 * Ensure file content which local changes were based
5311 * on matches file content in the branch head.
5313 err = got_object_id_by_path(&id, repo, head_commit_id,
5314 in_repo_path);
5315 if (err) {
5316 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5317 err = got_error(ood_errcode);
5318 goto done;
5319 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5320 err = got_error(ood_errcode);
5321 } else {
5322 /* Require that added files don't exist in the branch head. */
5323 err = got_object_id_by_path(&id, repo, head_commit_id,
5324 in_repo_path);
5325 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5326 goto done;
5327 err = id ? got_error(ood_errcode) : NULL;
5329 done:
5330 free(id);
5331 return err;
5334 const struct got_error *
5335 commit_worktree(struct got_object_id **new_commit_id,
5336 struct got_pathlist_head *commitable_paths,
5337 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5338 const char *author, const char *committer,
5339 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5340 got_worktree_status_cb status_cb, void *status_arg,
5341 struct got_repository *repo)
5343 const struct got_error *err = NULL, *unlockerr = NULL;
5344 struct got_pathlist_entry *pe;
5345 const char *head_ref_name = NULL;
5346 struct got_commit_object *head_commit = NULL;
5347 struct got_reference *head_ref2 = NULL;
5348 struct got_object_id *head_commit_id2 = NULL;
5349 struct got_tree_object *head_tree = NULL;
5350 struct got_object_id *new_tree_id = NULL;
5351 int nentries;
5352 struct got_object_id_queue parent_ids;
5353 struct got_object_qid *pid = NULL;
5354 char *logmsg = NULL;
5356 *new_commit_id = NULL;
5358 SIMPLEQ_INIT(&parent_ids);
5360 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5361 if (err)
5362 goto done;
5364 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5365 if (err)
5366 goto done;
5368 if (commit_msg_cb != NULL) {
5369 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5370 if (err)
5371 goto done;
5374 if (logmsg == NULL || strlen(logmsg) == 0) {
5375 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5376 goto done;
5379 /* Create blobs from added and modified files and record their IDs. */
5380 TAILQ_FOREACH(pe, commitable_paths, entry) {
5381 struct got_commitable *ct = pe->data;
5382 char *ondisk_path;
5384 /* Blobs for staged files already exist. */
5385 if (ct->staged_status == GOT_STATUS_ADD ||
5386 ct->staged_status == GOT_STATUS_MODIFY)
5387 continue;
5389 if (ct->status != GOT_STATUS_ADD &&
5390 ct->status != GOT_STATUS_MODIFY &&
5391 ct->status != GOT_STATUS_MODE_CHANGE)
5392 continue;
5394 if (asprintf(&ondisk_path, "%s/%s",
5395 worktree->root_path, pe->path) == -1) {
5396 err = got_error_from_errno("asprintf");
5397 goto done;
5399 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5400 free(ondisk_path);
5401 if (err)
5402 goto done;
5405 /* Recursively write new tree objects. */
5406 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5407 commitable_paths, status_cb, status_arg, repo);
5408 if (err)
5409 goto done;
5411 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5412 if (err)
5413 goto done;
5414 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5415 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5416 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5417 got_object_qid_free(pid);
5418 if (logmsg != NULL)
5419 free(logmsg);
5420 if (err)
5421 goto done;
5423 /* Check if a concurrent commit to our branch has occurred. */
5424 head_ref_name = got_worktree_get_head_ref_name(worktree);
5425 if (head_ref_name == NULL) {
5426 err = got_error_from_errno("got_worktree_get_head_ref_name");
5427 goto done;
5429 /* Lock the reference here to prevent concurrent modification. */
5430 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5431 if (err)
5432 goto done;
5433 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5434 if (err)
5435 goto done;
5436 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5437 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5438 goto done;
5440 /* Update branch head in repository. */
5441 err = got_ref_change_ref(head_ref2, *new_commit_id);
5442 if (err)
5443 goto done;
5444 err = got_ref_write(head_ref2, repo);
5445 if (err)
5446 goto done;
5448 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5449 if (err)
5450 goto done;
5452 err = ref_base_commit(worktree, repo);
5453 if (err)
5454 goto done;
5455 done:
5456 if (head_tree)
5457 got_object_tree_close(head_tree);
5458 if (head_commit)
5459 got_object_commit_close(head_commit);
5460 free(head_commit_id2);
5461 if (head_ref2) {
5462 unlockerr = got_ref_unlock(head_ref2);
5463 if (unlockerr && err == NULL)
5464 err = unlockerr;
5465 got_ref_close(head_ref2);
5467 return err;
5470 static const struct got_error *
5471 check_path_is_commitable(const char *path,
5472 struct got_pathlist_head *commitable_paths)
5474 struct got_pathlist_entry *cpe = NULL;
5475 size_t path_len = strlen(path);
5477 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5478 struct got_commitable *ct = cpe->data;
5479 const char *ct_path = ct->path;
5481 while (ct_path[0] == '/')
5482 ct_path++;
5484 if (strcmp(path, ct_path) == 0 ||
5485 got_path_is_child(ct_path, path, path_len))
5486 break;
5489 if (cpe == NULL)
5490 return got_error_path(path, GOT_ERR_BAD_PATH);
5492 return NULL;
5495 static const struct got_error *
5496 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5498 int *have_staged_files = arg;
5500 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5501 *have_staged_files = 1;
5502 return got_error(GOT_ERR_CANCELLED);
5505 return NULL;
5508 static const struct got_error *
5509 check_non_staged_files(struct got_fileindex *fileindex,
5510 struct got_pathlist_head *paths)
5512 struct got_pathlist_entry *pe;
5513 struct got_fileindex_entry *ie;
5515 TAILQ_FOREACH(pe, paths, entry) {
5516 if (pe->path[0] == '\0')
5517 continue;
5518 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5519 if (ie == NULL)
5520 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5521 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5522 return got_error_path(pe->path,
5523 GOT_ERR_FILE_NOT_STAGED);
5526 return NULL;
5529 const struct got_error *
5530 got_worktree_commit(struct got_object_id **new_commit_id,
5531 struct got_worktree *worktree, struct got_pathlist_head *paths,
5532 const char *author, const char *committer, int allow_bad_symlinks,
5533 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5534 got_worktree_status_cb status_cb, void *status_arg,
5535 struct got_repository *repo)
5537 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5538 struct got_fileindex *fileindex = NULL;
5539 char *fileindex_path = NULL;
5540 struct got_pathlist_head commitable_paths;
5541 struct collect_commitables_arg cc_arg;
5542 struct got_pathlist_entry *pe;
5543 struct got_reference *head_ref = NULL;
5544 struct got_object_id *head_commit_id = NULL;
5545 int have_staged_files = 0;
5547 *new_commit_id = NULL;
5549 TAILQ_INIT(&commitable_paths);
5551 err = lock_worktree(worktree, LOCK_EX);
5552 if (err)
5553 goto done;
5555 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5556 if (err)
5557 goto done;
5559 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5560 if (err)
5561 goto done;
5563 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5564 if (err)
5565 goto done;
5567 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5568 &have_staged_files);
5569 if (err && err->code != GOT_ERR_CANCELLED)
5570 goto done;
5571 if (have_staged_files) {
5572 err = check_non_staged_files(fileindex, paths);
5573 if (err)
5574 goto done;
5577 cc_arg.commitable_paths = &commitable_paths;
5578 cc_arg.worktree = worktree;
5579 cc_arg.fileindex = fileindex;
5580 cc_arg.repo = repo;
5581 cc_arg.have_staged_files = have_staged_files;
5582 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5583 TAILQ_FOREACH(pe, paths, entry) {
5584 err = worktree_status(worktree, pe->path, fileindex, repo,
5585 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5586 if (err)
5587 goto done;
5590 if (TAILQ_EMPTY(&commitable_paths)) {
5591 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5592 goto done;
5595 TAILQ_FOREACH(pe, paths, entry) {
5596 err = check_path_is_commitable(pe->path, &commitable_paths);
5597 if (err)
5598 goto done;
5601 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5602 struct got_commitable *ct = pe->data;
5603 const char *ct_path = ct->in_repo_path;
5605 while (ct_path[0] == '/')
5606 ct_path++;
5607 err = check_out_of_date(ct_path, ct->status,
5608 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5609 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5610 if (err)
5611 goto done;
5615 err = commit_worktree(new_commit_id, &commitable_paths,
5616 head_commit_id, worktree, author, committer,
5617 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5618 if (err)
5619 goto done;
5621 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5622 fileindex, have_staged_files);
5623 sync_err = sync_fileindex(fileindex, fileindex_path);
5624 if (sync_err && err == NULL)
5625 err = sync_err;
5626 done:
5627 if (fileindex)
5628 got_fileindex_free(fileindex);
5629 free(fileindex_path);
5630 unlockerr = lock_worktree(worktree, LOCK_SH);
5631 if (unlockerr && err == NULL)
5632 err = unlockerr;
5633 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5634 struct got_commitable *ct = pe->data;
5635 free_commitable(ct);
5637 got_pathlist_free(&commitable_paths);
5638 return err;
5641 const char *
5642 got_commitable_get_path(struct got_commitable *ct)
5644 return ct->path;
5647 unsigned int
5648 got_commitable_get_status(struct got_commitable *ct)
5650 return ct->status;
5653 struct check_rebase_ok_arg {
5654 struct got_worktree *worktree;
5655 struct got_repository *repo;
5658 static const struct got_error *
5659 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5661 const struct got_error *err = NULL;
5662 struct check_rebase_ok_arg *a = arg;
5663 unsigned char status;
5664 struct stat sb;
5665 char *ondisk_path;
5667 /* Reject rebase of a work tree with mixed base commits. */
5668 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5669 SHA1_DIGEST_LENGTH))
5670 return got_error(GOT_ERR_MIXED_COMMITS);
5672 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5673 == -1)
5674 return got_error_from_errno("asprintf");
5676 /* Reject rebase of a work tree with modified or staged files. */
5677 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5678 free(ondisk_path);
5679 if (err)
5680 return err;
5682 if (status != GOT_STATUS_NO_CHANGE)
5683 return got_error(GOT_ERR_MODIFIED);
5684 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5685 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5687 return NULL;
5690 const struct got_error *
5691 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5692 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5693 struct got_worktree *worktree, struct got_reference *branch,
5694 struct got_repository *repo)
5696 const struct got_error *err = NULL;
5697 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5698 char *branch_ref_name = NULL;
5699 char *fileindex_path = NULL;
5700 struct check_rebase_ok_arg ok_arg;
5701 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5702 struct got_object_id *wt_branch_tip = NULL;
5704 *new_base_branch_ref = NULL;
5705 *tmp_branch = NULL;
5706 *fileindex = NULL;
5708 err = lock_worktree(worktree, LOCK_EX);
5709 if (err)
5710 return err;
5712 err = open_fileindex(fileindex, &fileindex_path, worktree);
5713 if (err)
5714 goto done;
5716 ok_arg.worktree = worktree;
5717 ok_arg.repo = repo;
5718 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5719 &ok_arg);
5720 if (err)
5721 goto done;
5723 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5724 if (err)
5725 goto done;
5727 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5728 if (err)
5729 goto done;
5731 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5732 if (err)
5733 goto done;
5735 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5736 0);
5737 if (err)
5738 goto done;
5740 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5741 if (err)
5742 goto done;
5743 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5744 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5745 goto done;
5748 err = got_ref_alloc_symref(new_base_branch_ref,
5749 new_base_branch_ref_name, wt_branch);
5750 if (err)
5751 goto done;
5752 err = got_ref_write(*new_base_branch_ref, repo);
5753 if (err)
5754 goto done;
5756 /* TODO Lock original branch's ref while rebasing? */
5758 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5759 if (err)
5760 goto done;
5762 err = got_ref_write(branch_ref, repo);
5763 if (err)
5764 goto done;
5766 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5767 worktree->base_commit_id);
5768 if (err)
5769 goto done;
5770 err = got_ref_write(*tmp_branch, repo);
5771 if (err)
5772 goto done;
5774 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5775 if (err)
5776 goto done;
5777 done:
5778 free(fileindex_path);
5779 free(tmp_branch_name);
5780 free(new_base_branch_ref_name);
5781 free(branch_ref_name);
5782 if (branch_ref)
5783 got_ref_close(branch_ref);
5784 if (wt_branch)
5785 got_ref_close(wt_branch);
5786 free(wt_branch_tip);
5787 if (err) {
5788 if (*new_base_branch_ref) {
5789 got_ref_close(*new_base_branch_ref);
5790 *new_base_branch_ref = NULL;
5792 if (*tmp_branch) {
5793 got_ref_close(*tmp_branch);
5794 *tmp_branch = NULL;
5796 if (*fileindex) {
5797 got_fileindex_free(*fileindex);
5798 *fileindex = NULL;
5800 lock_worktree(worktree, LOCK_SH);
5802 return err;
5805 const struct got_error *
5806 got_worktree_rebase_continue(struct got_object_id **commit_id,
5807 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5808 struct got_reference **branch, struct got_fileindex **fileindex,
5809 struct got_worktree *worktree, struct got_repository *repo)
5811 const struct got_error *err;
5812 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5813 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5814 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5815 char *fileindex_path = NULL;
5816 int have_staged_files = 0;
5818 *commit_id = NULL;
5819 *new_base_branch = NULL;
5820 *tmp_branch = NULL;
5821 *branch = NULL;
5822 *fileindex = NULL;
5824 err = lock_worktree(worktree, LOCK_EX);
5825 if (err)
5826 return err;
5828 err = open_fileindex(fileindex, &fileindex_path, worktree);
5829 if (err)
5830 goto done;
5832 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5833 &have_staged_files);
5834 if (err && err->code != GOT_ERR_CANCELLED)
5835 goto done;
5836 if (have_staged_files) {
5837 err = got_error(GOT_ERR_STAGED_PATHS);
5838 goto done;
5841 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5842 if (err)
5843 goto done;
5845 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5846 if (err)
5847 goto done;
5849 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5850 if (err)
5851 goto done;
5853 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5854 if (err)
5855 goto done;
5857 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5858 if (err)
5859 goto done;
5861 err = got_ref_open(branch, repo,
5862 got_ref_get_symref_target(branch_ref), 0);
5863 if (err)
5864 goto done;
5866 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5867 if (err)
5868 goto done;
5870 err = got_ref_resolve(commit_id, repo, commit_ref);
5871 if (err)
5872 goto done;
5874 err = got_ref_open(new_base_branch, repo,
5875 new_base_branch_ref_name, 0);
5876 if (err)
5877 goto done;
5879 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5880 if (err)
5881 goto done;
5882 done:
5883 free(commit_ref_name);
5884 free(branch_ref_name);
5885 free(fileindex_path);
5886 if (commit_ref)
5887 got_ref_close(commit_ref);
5888 if (branch_ref)
5889 got_ref_close(branch_ref);
5890 if (err) {
5891 free(*commit_id);
5892 *commit_id = NULL;
5893 if (*tmp_branch) {
5894 got_ref_close(*tmp_branch);
5895 *tmp_branch = NULL;
5897 if (*new_base_branch) {
5898 got_ref_close(*new_base_branch);
5899 *new_base_branch = NULL;
5901 if (*branch) {
5902 got_ref_close(*branch);
5903 *branch = NULL;
5905 if (*fileindex) {
5906 got_fileindex_free(*fileindex);
5907 *fileindex = NULL;
5909 lock_worktree(worktree, LOCK_SH);
5911 return err;
5914 const struct got_error *
5915 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5917 const struct got_error *err;
5918 char *tmp_branch_name = NULL;
5920 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5921 if (err)
5922 return err;
5924 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5925 free(tmp_branch_name);
5926 return NULL;
5929 static const struct got_error *
5930 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5931 char **logmsg, void *arg)
5933 *logmsg = arg;
5934 return NULL;
5937 static const struct got_error *
5938 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5939 const char *path, struct got_object_id *blob_id,
5940 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5941 int dirfd, const char *de_name)
5943 return NULL;
5946 struct collect_merged_paths_arg {
5947 got_worktree_checkout_cb progress_cb;
5948 void *progress_arg;
5949 struct got_pathlist_head *merged_paths;
5952 static const struct got_error *
5953 collect_merged_paths(void *arg, unsigned char status, const char *path)
5955 const struct got_error *err;
5956 struct collect_merged_paths_arg *a = arg;
5957 char *p;
5958 struct got_pathlist_entry *new;
5960 err = (*a->progress_cb)(a->progress_arg, status, path);
5961 if (err)
5962 return err;
5964 if (status != GOT_STATUS_MERGE &&
5965 status != GOT_STATUS_ADD &&
5966 status != GOT_STATUS_DELETE &&
5967 status != GOT_STATUS_CONFLICT)
5968 return NULL;
5970 p = strdup(path);
5971 if (p == NULL)
5972 return got_error_from_errno("strdup");
5974 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5975 if (err || new == NULL)
5976 free(p);
5977 return err;
5980 void
5981 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5983 struct got_pathlist_entry *pe;
5985 TAILQ_FOREACH(pe, merged_paths, entry)
5986 free((char *)pe->path);
5988 got_pathlist_free(merged_paths);
5991 static const struct got_error *
5992 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5993 int is_rebase, struct got_repository *repo)
5995 const struct got_error *err;
5996 struct got_reference *commit_ref = NULL;
5998 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5999 if (err) {
6000 if (err->code != GOT_ERR_NOT_REF)
6001 goto done;
6002 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6003 if (err)
6004 goto done;
6005 err = got_ref_write(commit_ref, repo);
6006 if (err)
6007 goto done;
6008 } else if (is_rebase) {
6009 struct got_object_id *stored_id;
6010 int cmp;
6012 err = got_ref_resolve(&stored_id, repo, commit_ref);
6013 if (err)
6014 goto done;
6015 cmp = got_object_id_cmp(commit_id, stored_id);
6016 free(stored_id);
6017 if (cmp != 0) {
6018 err = got_error(GOT_ERR_REBASE_COMMITID);
6019 goto done;
6022 done:
6023 if (commit_ref)
6024 got_ref_close(commit_ref);
6025 return err;
6028 static const struct got_error *
6029 rebase_merge_files(struct got_pathlist_head *merged_paths,
6030 const char *commit_ref_name, struct got_worktree *worktree,
6031 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6032 struct got_object_id *commit_id, struct got_repository *repo,
6033 got_worktree_checkout_cb progress_cb, void *progress_arg,
6034 got_cancel_cb cancel_cb, void *cancel_arg)
6036 const struct got_error *err;
6037 struct got_reference *commit_ref = NULL;
6038 struct collect_merged_paths_arg cmp_arg;
6039 char *fileindex_path;
6041 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6043 err = get_fileindex_path(&fileindex_path, worktree);
6044 if (err)
6045 return err;
6047 cmp_arg.progress_cb = progress_cb;
6048 cmp_arg.progress_arg = progress_arg;
6049 cmp_arg.merged_paths = merged_paths;
6050 err = merge_files(worktree, fileindex, fileindex_path,
6051 parent_commit_id, commit_id, repo, collect_merged_paths,
6052 &cmp_arg, cancel_cb, cancel_arg);
6053 if (commit_ref)
6054 got_ref_close(commit_ref);
6055 return err;
6058 const struct got_error *
6059 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6060 struct got_worktree *worktree, struct got_fileindex *fileindex,
6061 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6062 struct got_repository *repo,
6063 got_worktree_checkout_cb progress_cb, void *progress_arg,
6064 got_cancel_cb cancel_cb, void *cancel_arg)
6066 const struct got_error *err;
6067 char *commit_ref_name;
6069 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6070 if (err)
6071 return err;
6073 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6074 if (err)
6075 goto done;
6077 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6078 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6079 progress_arg, cancel_cb, cancel_arg);
6080 done:
6081 free(commit_ref_name);
6082 return err;
6085 const struct got_error *
6086 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6087 struct got_worktree *worktree, struct got_fileindex *fileindex,
6088 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6089 struct got_repository *repo,
6090 got_worktree_checkout_cb progress_cb, void *progress_arg,
6091 got_cancel_cb cancel_cb, void *cancel_arg)
6093 const struct got_error *err;
6094 char *commit_ref_name;
6096 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6097 if (err)
6098 return err;
6100 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6101 if (err)
6102 goto done;
6104 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6105 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6106 progress_arg, cancel_cb, cancel_arg);
6107 done:
6108 free(commit_ref_name);
6109 return err;
6112 static const struct got_error *
6113 rebase_commit(struct got_object_id **new_commit_id,
6114 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6115 struct got_worktree *worktree, struct got_fileindex *fileindex,
6116 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6117 const char *new_logmsg, struct got_repository *repo)
6119 const struct got_error *err, *sync_err;
6120 struct got_pathlist_head commitable_paths;
6121 struct collect_commitables_arg cc_arg;
6122 char *fileindex_path = NULL;
6123 struct got_reference *head_ref = NULL;
6124 struct got_object_id *head_commit_id = NULL;
6125 char *logmsg = NULL;
6127 TAILQ_INIT(&commitable_paths);
6128 *new_commit_id = NULL;
6130 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6132 err = get_fileindex_path(&fileindex_path, worktree);
6133 if (err)
6134 return err;
6136 cc_arg.commitable_paths = &commitable_paths;
6137 cc_arg.worktree = worktree;
6138 cc_arg.repo = repo;
6139 cc_arg.have_staged_files = 0;
6141 * If possible get the status of individual files directly to
6142 * avoid crawling the entire work tree once per rebased commit.
6143 * TODO: Ideally, merged_paths would contain a list of commitables
6144 * we could use so we could skip worktree_status() entirely.
6146 if (merged_paths) {
6147 struct got_pathlist_entry *pe;
6148 TAILQ_FOREACH(pe, merged_paths, entry) {
6149 err = worktree_status(worktree, pe->path, fileindex,
6150 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6151 0);
6152 if (err)
6153 goto done;
6155 } else {
6156 err = worktree_status(worktree, "", fileindex, repo,
6157 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6158 if (err)
6159 goto done;
6162 if (TAILQ_EMPTY(&commitable_paths)) {
6163 /* No-op change; commit will be elided. */
6164 err = got_ref_delete(commit_ref, repo);
6165 if (err)
6166 goto done;
6167 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6168 goto done;
6171 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6172 if (err)
6173 goto done;
6175 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6176 if (err)
6177 goto done;
6179 if (new_logmsg) {
6180 logmsg = strdup(new_logmsg);
6181 if (logmsg == NULL) {
6182 err = got_error_from_errno("strdup");
6183 goto done;
6185 } else {
6186 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6187 if (err)
6188 goto done;
6191 /* NB: commit_worktree will call free(logmsg) */
6192 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6193 worktree, got_object_commit_get_author(orig_commit),
6194 got_object_commit_get_committer(orig_commit),
6195 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6196 if (err)
6197 goto done;
6199 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6200 if (err)
6201 goto done;
6203 err = got_ref_delete(commit_ref, repo);
6204 if (err)
6205 goto done;
6207 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6208 fileindex, 0);
6209 sync_err = sync_fileindex(fileindex, fileindex_path);
6210 if (sync_err && err == NULL)
6211 err = sync_err;
6212 done:
6213 free(fileindex_path);
6214 free(head_commit_id);
6215 if (head_ref)
6216 got_ref_close(head_ref);
6217 if (err) {
6218 free(*new_commit_id);
6219 *new_commit_id = NULL;
6221 return err;
6224 const struct got_error *
6225 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6226 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6227 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6228 struct got_commit_object *orig_commit,
6229 struct got_object_id *orig_commit_id, struct got_repository *repo)
6231 const struct got_error *err;
6232 char *commit_ref_name;
6233 struct got_reference *commit_ref = NULL;
6234 struct got_object_id *commit_id = NULL;
6236 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6237 if (err)
6238 return err;
6240 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6241 if (err)
6242 goto done;
6243 err = got_ref_resolve(&commit_id, repo, commit_ref);
6244 if (err)
6245 goto done;
6246 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6247 err = got_error(GOT_ERR_REBASE_COMMITID);
6248 goto done;
6251 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6252 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6253 done:
6254 if (commit_ref)
6255 got_ref_close(commit_ref);
6256 free(commit_ref_name);
6257 free(commit_id);
6258 return err;
6261 const struct got_error *
6262 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6263 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6264 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6265 struct got_commit_object *orig_commit,
6266 struct got_object_id *orig_commit_id, const char *new_logmsg,
6267 struct got_repository *repo)
6269 const struct got_error *err;
6270 char *commit_ref_name;
6271 struct got_reference *commit_ref = NULL;
6273 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6274 if (err)
6275 return err;
6277 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6278 if (err)
6279 goto done;
6281 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6282 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6283 done:
6284 if (commit_ref)
6285 got_ref_close(commit_ref);
6286 free(commit_ref_name);
6287 return err;
6290 const struct got_error *
6291 got_worktree_rebase_postpone(struct got_worktree *worktree,
6292 struct got_fileindex *fileindex)
6294 if (fileindex)
6295 got_fileindex_free(fileindex);
6296 return lock_worktree(worktree, LOCK_SH);
6299 static const struct got_error *
6300 delete_ref(const char *name, struct got_repository *repo)
6302 const struct got_error *err;
6303 struct got_reference *ref;
6305 err = got_ref_open(&ref, repo, name, 0);
6306 if (err) {
6307 if (err->code == GOT_ERR_NOT_REF)
6308 return NULL;
6309 return err;
6312 err = got_ref_delete(ref, repo);
6313 got_ref_close(ref);
6314 return err;
6317 static const struct got_error *
6318 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6320 const struct got_error *err;
6321 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6322 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6324 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6325 if (err)
6326 goto done;
6327 err = delete_ref(tmp_branch_name, repo);
6328 if (err)
6329 goto done;
6331 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6332 if (err)
6333 goto done;
6334 err = delete_ref(new_base_branch_ref_name, repo);
6335 if (err)
6336 goto done;
6338 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6339 if (err)
6340 goto done;
6341 err = delete_ref(branch_ref_name, repo);
6342 if (err)
6343 goto done;
6345 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6346 if (err)
6347 goto done;
6348 err = delete_ref(commit_ref_name, repo);
6349 if (err)
6350 goto done;
6352 done:
6353 free(tmp_branch_name);
6354 free(new_base_branch_ref_name);
6355 free(branch_ref_name);
6356 free(commit_ref_name);
6357 return err;
6360 const struct got_error *
6361 got_worktree_rebase_complete(struct got_worktree *worktree,
6362 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6363 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6364 struct got_repository *repo)
6366 const struct got_error *err, *unlockerr;
6367 struct got_object_id *new_head_commit_id = NULL;
6369 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6370 if (err)
6371 return err;
6373 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6374 if (err)
6375 goto done;
6377 err = got_ref_write(rebased_branch, repo);
6378 if (err)
6379 goto done;
6381 err = got_worktree_set_head_ref(worktree, rebased_branch);
6382 if (err)
6383 goto done;
6385 err = delete_rebase_refs(worktree, repo);
6386 done:
6387 if (fileindex)
6388 got_fileindex_free(fileindex);
6389 free(new_head_commit_id);
6390 unlockerr = lock_worktree(worktree, LOCK_SH);
6391 if (unlockerr && err == NULL)
6392 err = unlockerr;
6393 return err;
6396 const struct got_error *
6397 got_worktree_rebase_abort(struct got_worktree *worktree,
6398 struct got_fileindex *fileindex, struct got_repository *repo,
6399 struct got_reference *new_base_branch,
6400 got_worktree_checkout_cb progress_cb, void *progress_arg)
6402 const struct got_error *err, *unlockerr, *sync_err;
6403 struct got_reference *resolved = NULL;
6404 struct got_object_id *commit_id = NULL;
6405 char *fileindex_path = NULL;
6406 struct revert_file_args rfa;
6407 struct got_object_id *tree_id = NULL;
6409 err = lock_worktree(worktree, LOCK_EX);
6410 if (err)
6411 return err;
6413 err = got_ref_open(&resolved, repo,
6414 got_ref_get_symref_target(new_base_branch), 0);
6415 if (err)
6416 goto done;
6418 err = got_worktree_set_head_ref(worktree, resolved);
6419 if (err)
6420 goto done;
6423 * XXX commits to the base branch could have happened while
6424 * we were busy rebasing; should we store the original commit ID
6425 * when rebase begins and read it back here?
6427 err = got_ref_resolve(&commit_id, repo, resolved);
6428 if (err)
6429 goto done;
6431 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6432 if (err)
6433 goto done;
6435 err = got_object_id_by_path(&tree_id, repo,
6436 worktree->base_commit_id, worktree->path_prefix);
6437 if (err)
6438 goto done;
6440 err = delete_rebase_refs(worktree, repo);
6441 if (err)
6442 goto done;
6444 err = get_fileindex_path(&fileindex_path, worktree);
6445 if (err)
6446 goto done;
6448 rfa.worktree = worktree;
6449 rfa.fileindex = fileindex;
6450 rfa.progress_cb = progress_cb;
6451 rfa.progress_arg = progress_arg;
6452 rfa.patch_cb = NULL;
6453 rfa.patch_arg = NULL;
6454 rfa.repo = repo;
6455 err = worktree_status(worktree, "", fileindex, repo,
6456 revert_file, &rfa, NULL, NULL, 0, 0);
6457 if (err)
6458 goto sync;
6460 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6461 repo, progress_cb, progress_arg, NULL, NULL);
6462 sync:
6463 sync_err = sync_fileindex(fileindex, fileindex_path);
6464 if (sync_err && err == NULL)
6465 err = sync_err;
6466 done:
6467 got_ref_close(resolved);
6468 free(tree_id);
6469 free(commit_id);
6470 if (fileindex)
6471 got_fileindex_free(fileindex);
6472 free(fileindex_path);
6474 unlockerr = lock_worktree(worktree, LOCK_SH);
6475 if (unlockerr && err == NULL)
6476 err = unlockerr;
6477 return err;
6480 const struct got_error *
6481 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6482 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6483 struct got_fileindex **fileindex, struct got_worktree *worktree,
6484 struct got_repository *repo)
6486 const struct got_error *err = NULL;
6487 char *tmp_branch_name = NULL;
6488 char *branch_ref_name = NULL;
6489 char *base_commit_ref_name = NULL;
6490 char *fileindex_path = NULL;
6491 struct check_rebase_ok_arg ok_arg;
6492 struct got_reference *wt_branch = NULL;
6493 struct got_reference *base_commit_ref = NULL;
6495 *tmp_branch = NULL;
6496 *branch_ref = NULL;
6497 *base_commit_id = NULL;
6498 *fileindex = NULL;
6500 err = lock_worktree(worktree, LOCK_EX);
6501 if (err)
6502 return err;
6504 err = open_fileindex(fileindex, &fileindex_path, worktree);
6505 if (err)
6506 goto done;
6508 ok_arg.worktree = worktree;
6509 ok_arg.repo = repo;
6510 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6511 &ok_arg);
6512 if (err)
6513 goto done;
6515 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6516 if (err)
6517 goto done;
6519 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6520 if (err)
6521 goto done;
6523 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6524 worktree);
6525 if (err)
6526 goto done;
6528 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6529 0);
6530 if (err)
6531 goto done;
6533 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6534 if (err)
6535 goto done;
6537 err = got_ref_write(*branch_ref, repo);
6538 if (err)
6539 goto done;
6541 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6542 worktree->base_commit_id);
6543 if (err)
6544 goto done;
6545 err = got_ref_write(base_commit_ref, repo);
6546 if (err)
6547 goto done;
6548 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6549 if (*base_commit_id == NULL) {
6550 err = got_error_from_errno("got_object_id_dup");
6551 goto done;
6554 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6555 worktree->base_commit_id);
6556 if (err)
6557 goto done;
6558 err = got_ref_write(*tmp_branch, repo);
6559 if (err)
6560 goto done;
6562 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6563 if (err)
6564 goto done;
6565 done:
6566 free(fileindex_path);
6567 free(tmp_branch_name);
6568 free(branch_ref_name);
6569 free(base_commit_ref_name);
6570 if (wt_branch)
6571 got_ref_close(wt_branch);
6572 if (err) {
6573 if (*branch_ref) {
6574 got_ref_close(*branch_ref);
6575 *branch_ref = NULL;
6577 if (*tmp_branch) {
6578 got_ref_close(*tmp_branch);
6579 *tmp_branch = NULL;
6581 free(*base_commit_id);
6582 if (*fileindex) {
6583 got_fileindex_free(*fileindex);
6584 *fileindex = NULL;
6586 lock_worktree(worktree, LOCK_SH);
6588 return err;
6591 const struct got_error *
6592 got_worktree_histedit_postpone(struct got_worktree *worktree,
6593 struct got_fileindex *fileindex)
6595 if (fileindex)
6596 got_fileindex_free(fileindex);
6597 return lock_worktree(worktree, LOCK_SH);
6600 const struct got_error *
6601 got_worktree_histedit_in_progress(int *in_progress,
6602 struct got_worktree *worktree)
6604 const struct got_error *err;
6605 char *tmp_branch_name = NULL;
6607 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6608 if (err)
6609 return err;
6611 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6612 free(tmp_branch_name);
6613 return NULL;
6616 const struct got_error *
6617 got_worktree_histedit_continue(struct got_object_id **commit_id,
6618 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6619 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6620 struct got_worktree *worktree, struct got_repository *repo)
6622 const struct got_error *err;
6623 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6624 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6625 struct got_reference *commit_ref = NULL;
6626 struct got_reference *base_commit_ref = NULL;
6627 char *fileindex_path = NULL;
6628 int have_staged_files = 0;
6630 *commit_id = NULL;
6631 *tmp_branch = NULL;
6632 *base_commit_id = NULL;
6633 *fileindex = NULL;
6635 err = lock_worktree(worktree, LOCK_EX);
6636 if (err)
6637 return err;
6639 err = open_fileindex(fileindex, &fileindex_path, worktree);
6640 if (err)
6641 goto done;
6643 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6644 &have_staged_files);
6645 if (err && err->code != GOT_ERR_CANCELLED)
6646 goto done;
6647 if (have_staged_files) {
6648 err = got_error(GOT_ERR_STAGED_PATHS);
6649 goto done;
6652 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6653 if (err)
6654 goto done;
6656 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6657 if (err)
6658 goto done;
6660 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6661 if (err)
6662 goto done;
6664 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6665 worktree);
6666 if (err)
6667 goto done;
6669 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6670 if (err)
6671 goto done;
6673 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6674 if (err)
6675 goto done;
6676 err = got_ref_resolve(commit_id, repo, commit_ref);
6677 if (err)
6678 goto done;
6680 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6681 if (err)
6682 goto done;
6683 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6684 if (err)
6685 goto done;
6687 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6688 if (err)
6689 goto done;
6690 done:
6691 free(commit_ref_name);
6692 free(branch_ref_name);
6693 free(fileindex_path);
6694 if (commit_ref)
6695 got_ref_close(commit_ref);
6696 if (base_commit_ref)
6697 got_ref_close(base_commit_ref);
6698 if (err) {
6699 free(*commit_id);
6700 *commit_id = NULL;
6701 free(*base_commit_id);
6702 *base_commit_id = NULL;
6703 if (*tmp_branch) {
6704 got_ref_close(*tmp_branch);
6705 *tmp_branch = NULL;
6707 if (*fileindex) {
6708 got_fileindex_free(*fileindex);
6709 *fileindex = NULL;
6711 lock_worktree(worktree, LOCK_EX);
6713 return err;
6716 static const struct got_error *
6717 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6719 const struct got_error *err;
6720 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6721 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6723 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6724 if (err)
6725 goto done;
6726 err = delete_ref(tmp_branch_name, repo);
6727 if (err)
6728 goto done;
6730 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6731 worktree);
6732 if (err)
6733 goto done;
6734 err = delete_ref(base_commit_ref_name, repo);
6735 if (err)
6736 goto done;
6738 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6739 if (err)
6740 goto done;
6741 err = delete_ref(branch_ref_name, repo);
6742 if (err)
6743 goto done;
6745 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6746 if (err)
6747 goto done;
6748 err = delete_ref(commit_ref_name, repo);
6749 if (err)
6750 goto done;
6751 done:
6752 free(tmp_branch_name);
6753 free(base_commit_ref_name);
6754 free(branch_ref_name);
6755 free(commit_ref_name);
6756 return err;
6759 const struct got_error *
6760 got_worktree_histedit_abort(struct got_worktree *worktree,
6761 struct got_fileindex *fileindex, struct got_repository *repo,
6762 struct got_reference *branch, struct got_object_id *base_commit_id,
6763 got_worktree_checkout_cb progress_cb, void *progress_arg)
6765 const struct got_error *err, *unlockerr, *sync_err;
6766 struct got_reference *resolved = NULL;
6767 char *fileindex_path = NULL;
6768 struct got_object_id *tree_id = NULL;
6769 struct revert_file_args rfa;
6771 err = lock_worktree(worktree, LOCK_EX);
6772 if (err)
6773 return err;
6775 err = got_ref_open(&resolved, repo,
6776 got_ref_get_symref_target(branch), 0);
6777 if (err)
6778 goto done;
6780 err = got_worktree_set_head_ref(worktree, resolved);
6781 if (err)
6782 goto done;
6784 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6785 if (err)
6786 goto done;
6788 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6789 worktree->path_prefix);
6790 if (err)
6791 goto done;
6793 err = delete_histedit_refs(worktree, repo);
6794 if (err)
6795 goto done;
6797 err = get_fileindex_path(&fileindex_path, worktree);
6798 if (err)
6799 goto done;
6801 rfa.worktree = worktree;
6802 rfa.fileindex = fileindex;
6803 rfa.progress_cb = progress_cb;
6804 rfa.progress_arg = progress_arg;
6805 rfa.patch_cb = NULL;
6806 rfa.patch_arg = NULL;
6807 rfa.repo = repo;
6808 err = worktree_status(worktree, "", fileindex, repo,
6809 revert_file, &rfa, NULL, NULL, 0, 0);
6810 if (err)
6811 goto sync;
6813 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6814 repo, progress_cb, progress_arg, NULL, NULL);
6815 sync:
6816 sync_err = sync_fileindex(fileindex, fileindex_path);
6817 if (sync_err && err == NULL)
6818 err = sync_err;
6819 done:
6820 got_ref_close(resolved);
6821 free(tree_id);
6822 free(fileindex_path);
6824 unlockerr = lock_worktree(worktree, LOCK_SH);
6825 if (unlockerr && err == NULL)
6826 err = unlockerr;
6827 return err;
6830 const struct got_error *
6831 got_worktree_histedit_complete(struct got_worktree *worktree,
6832 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6833 struct got_reference *edited_branch, struct got_repository *repo)
6835 const struct got_error *err, *unlockerr;
6836 struct got_object_id *new_head_commit_id = NULL;
6837 struct got_reference *resolved = NULL;
6839 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6840 if (err)
6841 return err;
6843 err = got_ref_open(&resolved, repo,
6844 got_ref_get_symref_target(edited_branch), 0);
6845 if (err)
6846 goto done;
6848 err = got_ref_change_ref(resolved, new_head_commit_id);
6849 if (err)
6850 goto done;
6852 err = got_ref_write(resolved, repo);
6853 if (err)
6854 goto done;
6856 err = got_worktree_set_head_ref(worktree, resolved);
6857 if (err)
6858 goto done;
6860 err = delete_histedit_refs(worktree, repo);
6861 done:
6862 if (fileindex)
6863 got_fileindex_free(fileindex);
6864 free(new_head_commit_id);
6865 unlockerr = lock_worktree(worktree, LOCK_SH);
6866 if (unlockerr && err == NULL)
6867 err = unlockerr;
6868 return err;
6871 const struct got_error *
6872 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6873 struct got_object_id *commit_id, struct got_repository *repo)
6875 const struct got_error *err;
6876 char *commit_ref_name;
6878 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6879 if (err)
6880 return err;
6882 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6883 if (err)
6884 goto done;
6886 err = delete_ref(commit_ref_name, repo);
6887 done:
6888 free(commit_ref_name);
6889 return err;
6892 const struct got_error *
6893 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6894 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6895 struct got_worktree *worktree, const char *refname,
6896 struct got_repository *repo)
6898 const struct got_error *err = NULL;
6899 char *fileindex_path = NULL;
6900 struct check_rebase_ok_arg ok_arg;
6902 *fileindex = NULL;
6903 *branch_ref = NULL;
6904 *base_branch_ref = NULL;
6906 err = lock_worktree(worktree, LOCK_EX);
6907 if (err)
6908 return err;
6910 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6911 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6912 "cannot integrate a branch into itself; "
6913 "update -b or different branch name required");
6914 goto done;
6917 err = open_fileindex(fileindex, &fileindex_path, worktree);
6918 if (err)
6919 goto done;
6921 /* Preconditions are the same as for rebase. */
6922 ok_arg.worktree = worktree;
6923 ok_arg.repo = repo;
6924 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6925 &ok_arg);
6926 if (err)
6927 goto done;
6929 err = got_ref_open(branch_ref, repo, refname, 1);
6930 if (err)
6931 goto done;
6933 err = got_ref_open(base_branch_ref, repo,
6934 got_worktree_get_head_ref_name(worktree), 1);
6935 done:
6936 if (err) {
6937 if (*branch_ref) {
6938 got_ref_close(*branch_ref);
6939 *branch_ref = NULL;
6941 if (*base_branch_ref) {
6942 got_ref_close(*base_branch_ref);
6943 *base_branch_ref = NULL;
6945 if (*fileindex) {
6946 got_fileindex_free(*fileindex);
6947 *fileindex = NULL;
6949 lock_worktree(worktree, LOCK_SH);
6951 return err;
6954 const struct got_error *
6955 got_worktree_integrate_continue(struct got_worktree *worktree,
6956 struct got_fileindex *fileindex, struct got_repository *repo,
6957 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6958 got_worktree_checkout_cb progress_cb, void *progress_arg,
6959 got_cancel_cb cancel_cb, void *cancel_arg)
6961 const struct got_error *err = NULL, *sync_err, *unlockerr;
6962 char *fileindex_path = NULL;
6963 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6965 err = get_fileindex_path(&fileindex_path, worktree);
6966 if (err)
6967 goto done;
6969 err = got_ref_resolve(&commit_id, repo, branch_ref);
6970 if (err)
6971 goto done;
6973 err = got_object_id_by_path(&tree_id, repo, commit_id,
6974 worktree->path_prefix);
6975 if (err)
6976 goto done;
6978 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6979 if (err)
6980 goto done;
6982 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6983 progress_cb, progress_arg, cancel_cb, cancel_arg);
6984 if (err)
6985 goto sync;
6987 err = got_ref_change_ref(base_branch_ref, commit_id);
6988 if (err)
6989 goto sync;
6991 err = got_ref_write(base_branch_ref, repo);
6992 sync:
6993 sync_err = sync_fileindex(fileindex, fileindex_path);
6994 if (sync_err && err == NULL)
6995 err = sync_err;
6997 done:
6998 unlockerr = got_ref_unlock(branch_ref);
6999 if (unlockerr && err == NULL)
7000 err = unlockerr;
7001 got_ref_close(branch_ref);
7003 unlockerr = got_ref_unlock(base_branch_ref);
7004 if (unlockerr && err == NULL)
7005 err = unlockerr;
7006 got_ref_close(base_branch_ref);
7008 got_fileindex_free(fileindex);
7009 free(fileindex_path);
7010 free(tree_id);
7012 unlockerr = lock_worktree(worktree, LOCK_SH);
7013 if (unlockerr && err == NULL)
7014 err = unlockerr;
7015 return err;
7018 const struct got_error *
7019 got_worktree_integrate_abort(struct got_worktree *worktree,
7020 struct got_fileindex *fileindex, struct got_repository *repo,
7021 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7023 const struct got_error *err = NULL, *unlockerr = NULL;
7025 got_fileindex_free(fileindex);
7027 err = lock_worktree(worktree, LOCK_SH);
7029 unlockerr = got_ref_unlock(branch_ref);
7030 if (unlockerr && err == NULL)
7031 err = unlockerr;
7032 got_ref_close(branch_ref);
7034 unlockerr = got_ref_unlock(base_branch_ref);
7035 if (unlockerr && err == NULL)
7036 err = unlockerr;
7037 got_ref_close(base_branch_ref);
7039 return err;
7042 struct check_stage_ok_arg {
7043 struct got_object_id *head_commit_id;
7044 struct got_worktree *worktree;
7045 struct got_fileindex *fileindex;
7046 struct got_repository *repo;
7047 int have_changes;
7050 const struct got_error *
7051 check_stage_ok(void *arg, unsigned char status,
7052 unsigned char staged_status, const char *relpath,
7053 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7054 struct got_object_id *commit_id, int dirfd, const char *de_name)
7056 struct check_stage_ok_arg *a = arg;
7057 const struct got_error *err = NULL;
7058 struct got_fileindex_entry *ie;
7059 struct got_object_id base_commit_id;
7060 struct got_object_id *base_commit_idp = NULL;
7061 char *in_repo_path = NULL, *p;
7063 if (status == GOT_STATUS_UNVERSIONED ||
7064 status == GOT_STATUS_NO_CHANGE)
7065 return NULL;
7066 if (status == GOT_STATUS_NONEXISTENT)
7067 return got_error_set_errno(ENOENT, relpath);
7069 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7070 if (ie == NULL)
7071 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7073 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7074 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7075 relpath) == -1)
7076 return got_error_from_errno("asprintf");
7078 if (got_fileindex_entry_has_commit(ie)) {
7079 memcpy(base_commit_id.sha1, ie->commit_sha1,
7080 SHA1_DIGEST_LENGTH);
7081 base_commit_idp = &base_commit_id;
7084 if (status == GOT_STATUS_CONFLICT) {
7085 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7086 goto done;
7087 } else if (status != GOT_STATUS_ADD &&
7088 status != GOT_STATUS_MODIFY &&
7089 status != GOT_STATUS_DELETE) {
7090 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7091 goto done;
7094 a->have_changes = 1;
7096 p = in_repo_path;
7097 while (p[0] == '/')
7098 p++;
7099 err = check_out_of_date(p, status, staged_status,
7100 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7101 GOT_ERR_STAGE_OUT_OF_DATE);
7102 done:
7103 free(in_repo_path);
7104 return err;
7107 struct stage_path_arg {
7108 struct got_worktree *worktree;
7109 struct got_fileindex *fileindex;
7110 struct got_repository *repo;
7111 got_worktree_status_cb status_cb;
7112 void *status_arg;
7113 got_worktree_patch_cb patch_cb;
7114 void *patch_arg;
7115 int staged_something;
7116 int allow_bad_symlinks;
7119 static const struct got_error *
7120 stage_path(void *arg, unsigned char status,
7121 unsigned char staged_status, const char *relpath,
7122 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7123 struct got_object_id *commit_id, int dirfd, const char *de_name)
7125 struct stage_path_arg *a = arg;
7126 const struct got_error *err = NULL;
7127 struct got_fileindex_entry *ie;
7128 char *ondisk_path = NULL, *path_content = NULL;
7129 uint32_t stage;
7130 struct got_object_id *new_staged_blob_id = NULL;
7131 struct stat sb;
7133 if (status == GOT_STATUS_UNVERSIONED)
7134 return NULL;
7136 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7137 if (ie == NULL)
7138 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7140 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7141 relpath)== -1)
7142 return got_error_from_errno("asprintf");
7144 switch (status) {
7145 case GOT_STATUS_ADD:
7146 case GOT_STATUS_MODIFY:
7147 /* XXX could sb.st_mode be passed in by our caller? */
7148 if (lstat(ondisk_path, &sb) == -1) {
7149 err = got_error_from_errno2("lstat", ondisk_path);
7150 break;
7152 if (a->patch_cb) {
7153 if (status == GOT_STATUS_ADD) {
7154 int choice = GOT_PATCH_CHOICE_NONE;
7155 err = (*a->patch_cb)(&choice, a->patch_arg,
7156 status, ie->path, NULL, 1, 1);
7157 if (err)
7158 break;
7159 if (choice != GOT_PATCH_CHOICE_YES)
7160 break;
7161 } else {
7162 err = create_patched_content(&path_content, 0,
7163 staged_blob_id ? staged_blob_id : blob_id,
7164 ondisk_path, dirfd, de_name, ie->path,
7165 a->repo, a->patch_cb, a->patch_arg);
7166 if (err || path_content == NULL)
7167 break;
7170 err = got_object_blob_create(&new_staged_blob_id,
7171 path_content ? path_content : ondisk_path, a->repo);
7172 if (err)
7173 break;
7174 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7175 SHA1_DIGEST_LENGTH);
7176 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7177 stage = GOT_FILEIDX_STAGE_ADD;
7178 else
7179 stage = GOT_FILEIDX_STAGE_MODIFY;
7180 got_fileindex_entry_stage_set(ie, stage);
7181 if (S_ISLNK(sb.st_mode)) {
7182 int is_bad_symlink = 0;
7183 if (!a->allow_bad_symlinks) {
7184 char target_path[PATH_MAX];
7185 ssize_t target_len;
7186 target_len = readlink(ondisk_path, target_path,
7187 sizeof(target_path));
7188 if (target_len == -1) {
7189 err = got_error_from_errno2("readlink",
7190 ondisk_path);
7191 break;
7193 err = is_bad_symlink_target(&is_bad_symlink,
7194 target_path, target_len, ondisk_path,
7195 a->worktree->root_path);
7196 if (err)
7197 break;
7198 if (is_bad_symlink) {
7199 err = got_error_path(ondisk_path,
7200 GOT_ERR_BAD_SYMLINK);
7201 break;
7204 if (is_bad_symlink)
7205 got_fileindex_entry_staged_filetype_set(ie,
7206 GOT_FILEIDX_MODE_BAD_SYMLINK);
7207 else
7208 got_fileindex_entry_staged_filetype_set(ie,
7209 GOT_FILEIDX_MODE_SYMLINK);
7210 } else {
7211 got_fileindex_entry_staged_filetype_set(ie,
7212 GOT_FILEIDX_MODE_REGULAR_FILE);
7214 a->staged_something = 1;
7215 if (a->status_cb == NULL)
7216 break;
7217 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7218 get_staged_status(ie), relpath, blob_id,
7219 new_staged_blob_id, NULL, dirfd, de_name);
7220 break;
7221 case GOT_STATUS_DELETE:
7222 if (staged_status == GOT_STATUS_DELETE)
7223 break;
7224 if (a->patch_cb) {
7225 int choice = GOT_PATCH_CHOICE_NONE;
7226 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7227 ie->path, NULL, 1, 1);
7228 if (err)
7229 break;
7230 if (choice == GOT_PATCH_CHOICE_NO)
7231 break;
7232 if (choice != GOT_PATCH_CHOICE_YES) {
7233 err = got_error(GOT_ERR_PATCH_CHOICE);
7234 break;
7237 stage = GOT_FILEIDX_STAGE_DELETE;
7238 got_fileindex_entry_stage_set(ie, stage);
7239 a->staged_something = 1;
7240 if (a->status_cb == NULL)
7241 break;
7242 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7243 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7244 de_name);
7245 break;
7246 case GOT_STATUS_NO_CHANGE:
7247 break;
7248 case GOT_STATUS_CONFLICT:
7249 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7250 break;
7251 case GOT_STATUS_NONEXISTENT:
7252 err = got_error_set_errno(ENOENT, relpath);
7253 break;
7254 default:
7255 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7256 break;
7259 if (path_content && unlink(path_content) == -1 && err == NULL)
7260 err = got_error_from_errno2("unlink", path_content);
7261 free(path_content);
7262 free(ondisk_path);
7263 free(new_staged_blob_id);
7264 return err;
7267 const struct got_error *
7268 got_worktree_stage(struct got_worktree *worktree,
7269 struct got_pathlist_head *paths,
7270 got_worktree_status_cb status_cb, void *status_arg,
7271 got_worktree_patch_cb patch_cb, void *patch_arg,
7272 int allow_bad_symlinks, struct got_repository *repo)
7274 const struct got_error *err = NULL, *sync_err, *unlockerr;
7275 struct got_pathlist_entry *pe;
7276 struct got_fileindex *fileindex = NULL;
7277 char *fileindex_path = NULL;
7278 struct got_reference *head_ref = NULL;
7279 struct got_object_id *head_commit_id = NULL;
7280 struct check_stage_ok_arg oka;
7281 struct stage_path_arg spa;
7283 err = lock_worktree(worktree, LOCK_EX);
7284 if (err)
7285 return err;
7287 err = got_ref_open(&head_ref, repo,
7288 got_worktree_get_head_ref_name(worktree), 0);
7289 if (err)
7290 goto done;
7291 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7292 if (err)
7293 goto done;
7294 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7295 if (err)
7296 goto done;
7298 /* Check pre-conditions before staging anything. */
7299 oka.head_commit_id = head_commit_id;
7300 oka.worktree = worktree;
7301 oka.fileindex = fileindex;
7302 oka.repo = repo;
7303 oka.have_changes = 0;
7304 TAILQ_FOREACH(pe, paths, entry) {
7305 err = worktree_status(worktree, pe->path, fileindex, repo,
7306 check_stage_ok, &oka, NULL, NULL, 0, 0);
7307 if (err)
7308 goto done;
7310 if (!oka.have_changes) {
7311 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7312 goto done;
7315 spa.worktree = worktree;
7316 spa.fileindex = fileindex;
7317 spa.repo = repo;
7318 spa.patch_cb = patch_cb;
7319 spa.patch_arg = patch_arg;
7320 spa.status_cb = status_cb;
7321 spa.status_arg = status_arg;
7322 spa.staged_something = 0;
7323 spa.allow_bad_symlinks = allow_bad_symlinks;
7324 TAILQ_FOREACH(pe, paths, entry) {
7325 err = worktree_status(worktree, pe->path, fileindex, repo,
7326 stage_path, &spa, NULL, NULL, 0, 0);
7327 if (err)
7328 goto done;
7330 if (!spa.staged_something) {
7331 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7332 goto done;
7335 sync_err = sync_fileindex(fileindex, fileindex_path);
7336 if (sync_err && err == NULL)
7337 err = sync_err;
7338 done:
7339 if (head_ref)
7340 got_ref_close(head_ref);
7341 free(head_commit_id);
7342 free(fileindex_path);
7343 if (fileindex)
7344 got_fileindex_free(fileindex);
7345 unlockerr = lock_worktree(worktree, LOCK_SH);
7346 if (unlockerr && err == NULL)
7347 err = unlockerr;
7348 return err;
7351 struct unstage_path_arg {
7352 struct got_worktree *worktree;
7353 struct got_fileindex *fileindex;
7354 struct got_repository *repo;
7355 got_worktree_checkout_cb progress_cb;
7356 void *progress_arg;
7357 got_worktree_patch_cb patch_cb;
7358 void *patch_arg;
7361 static const struct got_error *
7362 create_unstaged_content(char **path_unstaged_content,
7363 char **path_new_staged_content, struct got_object_id *blob_id,
7364 struct got_object_id *staged_blob_id, const char *relpath,
7365 struct got_repository *repo,
7366 got_worktree_patch_cb patch_cb, void *patch_arg)
7368 const struct got_error *err;
7369 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7370 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7371 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7372 struct stat sb1, sb2;
7373 struct got_diff_changes *changes = NULL;
7374 struct got_diff_state *ds = NULL;
7375 struct got_diff_args *args = NULL;
7376 struct got_diff_change *change;
7377 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7378 int have_content = 0, have_rejected_content = 0;
7380 *path_unstaged_content = NULL;
7381 *path_new_staged_content = NULL;
7383 err = got_object_id_str(&label1, blob_id);
7384 if (err)
7385 return err;
7386 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7387 if (err)
7388 goto done;
7390 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7391 if (err)
7392 goto done;
7394 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7395 if (err)
7396 goto done;
7398 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7399 if (err)
7400 goto done;
7402 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7403 if (err)
7404 goto done;
7406 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7407 if (err)
7408 goto done;
7410 if (stat(path1, &sb1) == -1) {
7411 err = got_error_from_errno2("stat", path1);
7412 goto done;
7415 if (stat(path2, &sb2) == -1) {
7416 err = got_error_from_errno2("stat", path2);
7417 goto done;
7420 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7421 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7422 if (err)
7423 goto done;
7425 err = got_opentemp_named(path_unstaged_content, &outfile,
7426 "got-unstaged-content");
7427 if (err)
7428 goto done;
7429 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7430 "got-new-staged-content");
7431 if (err)
7432 goto done;
7434 if (fseek(f1, 0L, SEEK_SET) == -1) {
7435 err = got_ferror(f1, GOT_ERR_IO);
7436 goto done;
7438 if (fseek(f2, 0L, SEEK_SET) == -1) {
7439 err = got_ferror(f2, GOT_ERR_IO);
7440 goto done;
7442 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7443 int choice;
7444 err = apply_or_reject_change(&choice, change, ++n,
7445 changes->nchanges, ds, args, diff_flags, relpath,
7446 f1, f2, &line_cur1, &line_cur2,
7447 outfile, rejectfile, patch_cb, patch_arg);
7448 if (err)
7449 goto done;
7450 if (choice == GOT_PATCH_CHOICE_YES)
7451 have_content = 1;
7452 else
7453 have_rejected_content = 1;
7454 if (choice == GOT_PATCH_CHOICE_QUIT)
7455 break;
7457 if (have_content || have_rejected_content)
7458 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7459 outfile, rejectfile);
7460 done:
7461 free(label1);
7462 if (blob)
7463 got_object_blob_close(blob);
7464 if (staged_blob)
7465 got_object_blob_close(staged_blob);
7466 if (f1 && fclose(f1) == EOF && err == NULL)
7467 err = got_error_from_errno2("fclose", path1);
7468 if (f2 && fclose(f2) == EOF && err == NULL)
7469 err = got_error_from_errno2("fclose", path2);
7470 if (outfile && fclose(outfile) == EOF && err == NULL)
7471 err = got_error_from_errno2("fclose", *path_unstaged_content);
7472 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7473 err = got_error_from_errno2("fclose", *path_new_staged_content);
7474 if (path1 && unlink(path1) == -1 && err == NULL)
7475 err = got_error_from_errno2("unlink", path1);
7476 if (path2 && unlink(path2) == -1 && err == NULL)
7477 err = got_error_from_errno2("unlink", path2);
7478 if (err || !have_content) {
7479 if (*path_unstaged_content &&
7480 unlink(*path_unstaged_content) == -1 && err == NULL)
7481 err = got_error_from_errno2("unlink",
7482 *path_unstaged_content);
7483 free(*path_unstaged_content);
7484 *path_unstaged_content = NULL;
7486 if (err || !have_content || !have_rejected_content) {
7487 if (*path_new_staged_content &&
7488 unlink(*path_new_staged_content) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink",
7490 *path_new_staged_content);
7491 free(*path_new_staged_content);
7492 *path_new_staged_content = NULL;
7494 free(args);
7495 if (ds) {
7496 got_diff_state_free(ds);
7497 free(ds);
7499 if (changes)
7500 got_diff_free_changes(changes);
7501 free(path1);
7502 free(path2);
7503 return err;
7506 static const struct got_error *
7507 unstage_hunks(struct got_object_id *staged_blob_id,
7508 struct got_blob_object *blob_base,
7509 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7510 const char *ondisk_path, const char *label_orig,
7511 struct got_worktree *worktree, struct got_repository *repo,
7512 got_worktree_patch_cb patch_cb, void *patch_arg,
7513 got_worktree_checkout_cb progress_cb, void *progress_arg)
7515 const struct got_error *err = NULL;
7516 char *path_unstaged_content = NULL;
7517 char *path_new_staged_content = NULL;
7518 struct got_object_id *new_staged_blob_id = NULL;
7519 FILE *f = NULL;
7520 struct stat sb;
7522 err = create_unstaged_content(&path_unstaged_content,
7523 &path_new_staged_content, blob_id, staged_blob_id,
7524 ie->path, repo, patch_cb, patch_arg);
7525 if (err)
7526 return err;
7528 if (path_unstaged_content == NULL)
7529 return NULL;
7531 if (path_new_staged_content) {
7532 err = got_object_blob_create(&new_staged_blob_id,
7533 path_new_staged_content, repo);
7534 if (err)
7535 goto done;
7538 f = fopen(path_unstaged_content, "r");
7539 if (f == NULL) {
7540 err = got_error_from_errno2("fopen",
7541 path_unstaged_content);
7542 goto done;
7544 if (fstat(fileno(f), &sb) == -1) {
7545 err = got_error_from_errno2("fstat", path_unstaged_content);
7546 goto done;
7548 if (got_fileindex_entry_staged_filetype_get(ie) ==
7549 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7550 char link_target[PATH_MAX];
7551 size_t r;
7552 r = fread(link_target, 1, sizeof(link_target), f);
7553 if (r == 0 && ferror(f)) {
7554 err = got_error_from_errno("fread");
7555 goto done;
7557 if (r >= sizeof(link_target)) { /* should not happen */
7558 err = got_error(GOT_ERR_NO_SPACE);
7559 goto done;
7561 link_target[r] = '\0';
7562 err = merge_symlink(worktree, blob_base,
7563 ondisk_path, ie->path, label_orig, link_target,
7564 worktree->base_commit_id, repo, progress_cb,
7565 progress_arg);
7566 } else {
7567 int local_changes_subsumed;
7568 err = merge_file(&local_changes_subsumed, worktree,
7569 blob_base, ondisk_path, ie->path,
7570 got_fileindex_perms_to_st(ie),
7571 path_unstaged_content, label_orig, "unstaged",
7572 repo, progress_cb, progress_arg);
7574 if (err)
7575 goto done;
7577 if (new_staged_blob_id) {
7578 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7579 SHA1_DIGEST_LENGTH);
7580 } else
7581 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7582 done:
7583 free(new_staged_blob_id);
7584 if (path_unstaged_content &&
7585 unlink(path_unstaged_content) == -1 && err == NULL)
7586 err = got_error_from_errno2("unlink", path_unstaged_content);
7587 if (path_new_staged_content &&
7588 unlink(path_new_staged_content) == -1 && err == NULL)
7589 err = got_error_from_errno2("unlink", path_new_staged_content);
7590 if (f && fclose(f) != 0 && err == NULL)
7591 err = got_error_from_errno2("fclose", path_unstaged_content);
7592 free(path_unstaged_content);
7593 free(path_new_staged_content);
7594 return err;
7597 static const struct got_error *
7598 unstage_path(void *arg, unsigned char status,
7599 unsigned char staged_status, const char *relpath,
7600 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7601 struct got_object_id *commit_id, int dirfd, const char *de_name)
7603 const struct got_error *err = NULL;
7604 struct unstage_path_arg *a = arg;
7605 struct got_fileindex_entry *ie;
7606 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7607 char *ondisk_path = NULL;
7608 char *id_str = NULL, *label_orig = NULL;
7609 int local_changes_subsumed;
7610 struct stat sb;
7612 if (staged_status != GOT_STATUS_ADD &&
7613 staged_status != GOT_STATUS_MODIFY &&
7614 staged_status != GOT_STATUS_DELETE)
7615 return NULL;
7617 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7618 if (ie == NULL)
7619 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7621 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7622 == -1)
7623 return got_error_from_errno("asprintf");
7625 err = got_object_id_str(&id_str,
7626 commit_id ? commit_id : a->worktree->base_commit_id);
7627 if (err)
7628 goto done;
7629 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7630 id_str) == -1) {
7631 err = got_error_from_errno("asprintf");
7632 goto done;
7635 switch (staged_status) {
7636 case GOT_STATUS_MODIFY:
7637 err = got_object_open_as_blob(&blob_base, a->repo,
7638 blob_id, 8192);
7639 if (err)
7640 break;
7641 /* fall through */
7642 case GOT_STATUS_ADD:
7643 if (a->patch_cb) {
7644 if (staged_status == GOT_STATUS_ADD) {
7645 int choice = GOT_PATCH_CHOICE_NONE;
7646 err = (*a->patch_cb)(&choice, a->patch_arg,
7647 staged_status, ie->path, NULL, 1, 1);
7648 if (err)
7649 break;
7650 if (choice != GOT_PATCH_CHOICE_YES)
7651 break;
7652 } else {
7653 err = unstage_hunks(staged_blob_id,
7654 blob_base, blob_id, ie, ondisk_path,
7655 label_orig, a->worktree, a->repo,
7656 a->patch_cb, a->patch_arg,
7657 a->progress_cb, a->progress_arg);
7658 break; /* Done with this file. */
7661 err = got_object_open_as_blob(&blob_staged, a->repo,
7662 staged_blob_id, 8192);
7663 if (err)
7664 break;
7665 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7666 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7667 case GOT_FILEIDX_MODE_REGULAR_FILE:
7668 err = merge_blob(&local_changes_subsumed, a->worktree,
7669 blob_base, ondisk_path, relpath,
7670 got_fileindex_perms_to_st(ie), label_orig,
7671 blob_staged, commit_id ? commit_id :
7672 a->worktree->base_commit_id, a->repo,
7673 a->progress_cb, a->progress_arg);
7674 break;
7675 case GOT_FILEIDX_MODE_SYMLINK:
7676 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7677 char *staged_target;
7678 err = got_object_blob_read_to_str(
7679 &staged_target, blob_staged);
7680 if (err)
7681 goto done;
7682 err = merge_symlink(a->worktree, blob_base,
7683 ondisk_path, relpath, label_orig,
7684 staged_target, commit_id ? commit_id :
7685 a->worktree->base_commit_id,
7686 a->repo, a->progress_cb, a->progress_arg);
7687 free(staged_target);
7688 } else {
7689 err = merge_blob(&local_changes_subsumed,
7690 a->worktree, blob_base, ondisk_path,
7691 relpath, got_fileindex_perms_to_st(ie),
7692 label_orig, blob_staged,
7693 commit_id ? commit_id :
7694 a->worktree->base_commit_id, a->repo,
7695 a->progress_cb, a->progress_arg);
7697 break;
7698 default:
7699 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7700 break;
7702 if (err == NULL)
7703 got_fileindex_entry_stage_set(ie,
7704 GOT_FILEIDX_STAGE_NONE);
7705 break;
7706 case GOT_STATUS_DELETE:
7707 if (a->patch_cb) {
7708 int choice = GOT_PATCH_CHOICE_NONE;
7709 err = (*a->patch_cb)(&choice, a->patch_arg,
7710 staged_status, ie->path, NULL, 1, 1);
7711 if (err)
7712 break;
7713 if (choice == GOT_PATCH_CHOICE_NO)
7714 break;
7715 if (choice != GOT_PATCH_CHOICE_YES) {
7716 err = got_error(GOT_ERR_PATCH_CHOICE);
7717 break;
7720 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7721 err = get_file_status(&status, &sb, ie, ondisk_path,
7722 dirfd, de_name, a->repo);
7723 if (err)
7724 break;
7725 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7726 break;
7728 done:
7729 free(ondisk_path);
7730 if (blob_base)
7731 got_object_blob_close(blob_base);
7732 if (blob_staged)
7733 got_object_blob_close(blob_staged);
7734 free(id_str);
7735 free(label_orig);
7736 return err;
7739 const struct got_error *
7740 got_worktree_unstage(struct got_worktree *worktree,
7741 struct got_pathlist_head *paths,
7742 got_worktree_checkout_cb progress_cb, void *progress_arg,
7743 got_worktree_patch_cb patch_cb, void *patch_arg,
7744 struct got_repository *repo)
7746 const struct got_error *err = NULL, *sync_err, *unlockerr;
7747 struct got_pathlist_entry *pe;
7748 struct got_fileindex *fileindex = NULL;
7749 char *fileindex_path = NULL;
7750 struct unstage_path_arg upa;
7752 err = lock_worktree(worktree, LOCK_EX);
7753 if (err)
7754 return err;
7756 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7757 if (err)
7758 goto done;
7760 upa.worktree = worktree;
7761 upa.fileindex = fileindex;
7762 upa.repo = repo;
7763 upa.progress_cb = progress_cb;
7764 upa.progress_arg = progress_arg;
7765 upa.patch_cb = patch_cb;
7766 upa.patch_arg = patch_arg;
7767 TAILQ_FOREACH(pe, paths, entry) {
7768 err = worktree_status(worktree, pe->path, fileindex, repo,
7769 unstage_path, &upa, NULL, NULL, 0, 0);
7770 if (err)
7771 goto done;
7774 sync_err = sync_fileindex(fileindex, fileindex_path);
7775 if (sync_err && err == NULL)
7776 err = sync_err;
7777 done:
7778 free(fileindex_path);
7779 if (fileindex)
7780 got_fileindex_free(fileindex);
7781 unlockerr = lock_worktree(worktree, LOCK_SH);
7782 if (unlockerr && err == NULL)
7783 err = unlockerr;
7784 return err;
7787 struct report_file_info_arg {
7788 struct got_worktree *worktree;
7789 got_worktree_path_info_cb info_cb;
7790 void *info_arg;
7791 struct got_pathlist_head *paths;
7792 got_cancel_cb cancel_cb;
7793 void *cancel_arg;
7796 static const struct got_error *
7797 report_file_info(void *arg, struct got_fileindex_entry *ie)
7799 struct report_file_info_arg *a = arg;
7800 struct got_pathlist_entry *pe;
7801 struct got_object_id blob_id, staged_blob_id, commit_id;
7802 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7803 struct got_object_id *commit_idp = NULL;
7804 int stage;
7806 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7807 return got_error(GOT_ERR_CANCELLED);
7809 TAILQ_FOREACH(pe, a->paths, entry) {
7810 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7811 got_path_is_child(ie->path, pe->path, pe->path_len))
7812 break;
7814 if (pe == NULL) /* not found */
7815 return NULL;
7817 if (got_fileindex_entry_has_blob(ie)) {
7818 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7819 blob_idp = &blob_id;
7821 stage = got_fileindex_entry_stage_get(ie);
7822 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7823 stage == GOT_FILEIDX_STAGE_ADD) {
7824 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7825 SHA1_DIGEST_LENGTH);
7826 staged_blob_idp = &staged_blob_id;
7829 if (got_fileindex_entry_has_commit(ie)) {
7830 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7831 commit_idp = &commit_id;
7834 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7835 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7838 const struct got_error *
7839 got_worktree_path_info(struct got_worktree *worktree,
7840 struct got_pathlist_head *paths,
7841 got_worktree_path_info_cb info_cb, void *info_arg,
7842 got_cancel_cb cancel_cb, void *cancel_arg)
7845 const struct got_error *err = NULL, *unlockerr;
7846 struct got_fileindex *fileindex = NULL;
7847 char *fileindex_path = NULL;
7848 struct report_file_info_arg arg;
7850 err = lock_worktree(worktree, LOCK_SH);
7851 if (err)
7852 return err;
7854 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7855 if (err)
7856 goto done;
7858 arg.worktree = worktree;
7859 arg.info_cb = info_cb;
7860 arg.info_arg = info_arg;
7861 arg.paths = paths;
7862 arg.cancel_cb = cancel_cb;
7863 arg.cancel_arg = cancel_arg;
7864 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7865 &arg);
7866 done:
7867 free(fileindex_path);
7868 if (fileindex)
7869 got_fileindex_free(fileindex);
7870 unlockerr = lock_worktree(worktree, LOCK_UN);
7871 if (unlockerr && err == NULL)
7872 err = unlockerr;
7873 return err;