Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
432 done:
433 if (repo)
434 got_repo_close(repo);
435 free(path_got);
436 free(path_lock);
437 free(base_commit_id_str);
438 free(uuidstr);
439 free(formatstr);
440 if (err) {
441 if (fd != -1)
442 close(fd);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
445 *worktree = NULL;
446 } else
447 (*worktree)->lockfd = fd;
449 return err;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
456 char *worktree_path;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
462 for (;;) {
463 char *parent_path;
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 free(worktree_path);
468 return err;
470 if (*worktree) {
471 free(worktree_path);
472 return NULL;
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
475 break;
476 err = got_path_dirname(&parent_path, worktree_path);
477 if (err) {
478 if (err->code != GOT_ERR_BAD_PATH) {
479 free(worktree_path);
480 return err;
482 break;
484 free(worktree_path);
485 worktree_path = parent_path;
488 free(worktree_path);
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
507 free(worktree);
508 return err;
511 const char *
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
517 const char *
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
522 const char *
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
540 free(absprefix);
541 return NULL;
544 const char *
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
567 goto done;
570 err = write_head_ref(path_got, head_ref);
571 if (err)
572 goto done;
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
576 done:
577 free(path_got);
578 if (err)
579 free(head_ref_name);
580 return err;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
595 char *id_str = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
601 path_got = NULL;
602 goto done;
605 err = got_object_open(&obj, repo, commit_id);
606 if (err)
607 return err;
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
616 if (err)
617 goto done;
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
619 if (err)
620 goto done;
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
626 goto done;
628 done:
629 if (obj)
630 got_object_close(obj);
631 free(id_str);
632 free(path_got);
633 return err;
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
649 return NULL;
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
656 char *abspath;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 struct stat sb;
664 err = NULL;
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
672 free(abspath);
673 return err;
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
680 uint8_t fbuf1[8192];
681 uint8_t fbuf2[8192];
682 size_t flen1 = 0, flen2 = 0;
684 *same = 1;
686 for (;;) {
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
690 break;
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
695 break;
697 if (flen1 == 0) {
698 if (flen2 != 0)
699 *same = 0;
700 break;
701 } else if (flen2 == 0) {
702 if (flen1 != 0)
703 *same = 0;
704 break;
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
707 *same = 0;
708 break;
710 } else {
711 *same = 0;
712 break;
716 return err;
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
723 struct stat sb;
724 size_t size1, size2;
725 FILE *f1 = NULL, *f2 = NULL;
727 *same = 1;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
731 goto done;
733 size1 = sb.st_size;
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
737 goto done;
739 size2 = sb.st_size;
741 if (size1 != size2) {
742 *same = 0;
743 return NULL;
746 f1 = fopen(f1_path, "r");
747 if (f1 == NULL)
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
751 if (f2 == NULL) {
752 err = got_error_from_errno2("fopen", f2_path);
753 goto done;
756 err = check_file_contents_equal(same, f1, f2);
757 done:
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
763 return err;
766 /*
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
770 */
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
780 int merged_fd = -1;
781 FILE *f_orig = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
784 int overlapcnt = 0;
785 char *parent = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
792 if (err)
793 return err;
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 goto done;
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
801 if (err)
802 goto done;
804 free(base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
807 base_path = NULL;
808 goto done;
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
812 if (err)
813 goto done;
814 if (blob_orig) {
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
816 blob_orig);
817 if (err)
818 goto done;
819 } else {
820 /*
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
824 */
827 /*
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
830 */
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
833 ssize_t target_len;
834 size_t n;
836 free(base_path);
837 if (asprintf(&base_path, "%s/got-symlink-merge",
838 parent) == -1) {
839 err = got_error_from_errno("asprintf");
840 base_path = NULL;
841 goto done;
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 if (err)
845 goto done;
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
850 goto done;
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
855 goto done;
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
859 goto done;
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
866 if (err)
867 goto done;
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
871 if (err)
872 goto done;
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
876 goto done;
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
882 merged_path);
883 if (err)
884 goto done;
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
889 goto done;
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
894 ondisk_path);
895 goto done;
897 done:
898 if (err) {
899 if (merged_path)
900 unlink(merged_path);
902 if (symlink_path) {
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
908 free(symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
913 free(merged_path);
914 free(base_path);
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
919 free(parent);
920 return err;
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
925 size_t target_len)
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
932 ondisk_path);
933 return NULL;
936 /*
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
947 */
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
955 FILE *f = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
958 if (err)
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
964 goto done;
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 if (err)
969 goto done;
971 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
972 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
973 deriv_target ? deriv_target : "(symlink was deleted)",
974 orig_target ? label_orig : "",
975 orig_target ? "\n" : "",
976 orig_target ? orig_target : "",
977 orig_target ? "\n" : "",
978 GOT_DIFF_CONFLICT_MARKER_SEP,
979 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
980 err = got_error_from_errno2("fprintf", path);
981 goto done;
984 if (unlink(ondisk_path) == -1) {
985 err = got_error_from_errno2("unlink", ondisk_path);
986 goto done;
988 if (rename(path, ondisk_path) == -1) {
989 err = got_error_from_errno3("rename", path, ondisk_path);
990 goto done;
992 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
994 goto done;
996 done:
997 if (f != NULL && fclose(f) == EOF && err == NULL)
998 err = got_error_from_errno2("fclose", path);
999 free(path);
1000 free(id_str);
1001 free(label_deriv);
1002 return err;
1005 /* forward declaration */
1006 static const struct got_error *
1007 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1008 const char *, const char *, uint16_t, const char *,
1009 struct got_blob_object *, struct got_object_id *,
1010 struct got_repository *, got_worktree_checkout_cb, void *);
1013 * Merge a symlink into the work tree, where blob_orig acts as the common
1014 * ancestor, deriv_target is the link target of the first derived version,
1015 * and the symlink on disk acts as the second derived version.
1016 * Assume that contents of both blobs represent symlinks.
1018 static const struct got_error *
1019 merge_symlink(struct got_worktree *worktree,
1020 struct got_blob_object *blob_orig, const char *ondisk_path,
1021 const char *path, const char *label_orig, const char *deriv_target,
1022 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1023 got_worktree_checkout_cb progress_cb, void *progress_arg)
1025 const struct got_error *err = NULL;
1026 char *ancestor_target = NULL;
1027 struct stat sb;
1028 ssize_t ondisk_len, deriv_len;
1029 char ondisk_target[PATH_MAX];
1030 int have_local_change = 0;
1031 int have_incoming_change = 0;
1033 if (lstat(ondisk_path, &sb) == -1)
1034 return got_error_from_errno2("lstat", ondisk_path);
1036 ondisk_len = readlink(ondisk_path, ondisk_target,
1037 sizeof(ondisk_target));
1038 if (ondisk_len == -1) {
1039 err = got_error_from_errno2("readlink",
1040 ondisk_path);
1041 goto done;
1043 ondisk_target[ondisk_len] = '\0';
1045 if (blob_orig) {
1046 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1047 if (err)
1048 goto done;
1051 if (ancestor_target == NULL ||
1052 (ondisk_len != strlen(ancestor_target) ||
1053 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1054 have_local_change = 1;
1056 deriv_len = strlen(deriv_target);
1057 if (ancestor_target == NULL ||
1058 (deriv_len != strlen(ancestor_target) ||
1059 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1060 have_incoming_change = 1;
1062 if (!have_local_change && !have_incoming_change) {
1063 if (ancestor_target) {
1064 /* Both sides made the same change. */
1065 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1066 path);
1067 } else if (deriv_len == ondisk_len &&
1068 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1069 /* Both sides added the same symlink. */
1070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1071 path);
1072 } else {
1073 /* Both sides added symlinks which don't match. */
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target,
1076 label_orig, ondisk_target, ondisk_path);
1077 if (err)
1078 goto done;
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 path);
1082 } else if (!have_local_change && have_incoming_change) {
1083 /* Apply the incoming change. */
1084 err = update_symlink(ondisk_path, deriv_target,
1085 strlen(deriv_target));
1086 if (err)
1087 goto done;
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1089 } else if (have_local_change && have_incoming_change) {
1090 if (deriv_len == ondisk_len &&
1091 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1092 /* Both sides made the same change. */
1093 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1094 path);
1095 } else {
1096 err = install_symlink_conflict(deriv_target,
1097 deriv_base_commit_id, ancestor_target, label_orig,
1098 ondisk_target, ondisk_path);
1099 if (err)
1100 goto done;
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1102 path);
1106 done:
1107 free(ancestor_target);
1108 return err;
1112 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1113 * blob_deriv acts as the first derived version, and the file on disk
1114 * acts as the second derived version.
1116 static const struct got_error *
1117 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1118 struct got_blob_object *blob_orig, const char *ondisk_path,
1119 const char *path, uint16_t st_mode, const char *label_orig,
1120 struct got_blob_object *blob_deriv,
1121 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1122 got_worktree_checkout_cb progress_cb, void *progress_arg)
1124 const struct got_error *err = NULL;
1125 FILE *f_deriv = NULL;
1126 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1127 char *label_deriv = NULL, *parent = NULL;
1129 *local_changes_subsumed = 0;
1131 err = got_path_dirname(&parent, ondisk_path);
1132 if (err)
1133 return err;
1135 free(base_path);
1136 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1137 err = got_error_from_errno("asprintf");
1138 base_path = NULL;
1139 goto done;
1142 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1143 if (err)
1144 goto done;
1145 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1146 blob_deriv);
1147 if (err)
1148 goto done;
1150 err = got_object_id_str(&id_str, deriv_base_commit_id);
1151 if (err)
1152 goto done;
1153 if (asprintf(&label_deriv, "%s: commit %s",
1154 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1155 err = got_error_from_errno("asprintf");
1156 goto done;
1159 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1160 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1161 label_deriv, repo, progress_cb, progress_arg);
1162 done:
1163 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1164 err = got_error_from_errno("fclose");
1165 free(base_path);
1166 if (blob_deriv_path) {
1167 unlink(blob_deriv_path);
1168 free(blob_deriv_path);
1170 free(id_str);
1171 free(label_deriv);
1172 free(parent);
1173 return err;
1176 static const struct got_error *
1177 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1178 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1179 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1181 const struct got_error *err = NULL;
1182 struct got_fileindex_entry *new_ie;
1184 *new_iep = NULL;
1186 err = got_fileindex_entry_alloc(&new_ie, path);
1187 if (err)
1188 return err;
1190 err = got_fileindex_entry_update(new_ie, ondisk_path,
1191 blob_id->sha1, base_commit_id->sha1, 1);
1192 if (err)
1193 goto done;
1195 err = got_fileindex_entry_add(fileindex, new_ie);
1196 done:
1197 if (err)
1198 got_fileindex_entry_free(new_ie);
1199 else
1200 *new_iep = new_ie;
1201 return err;
1204 static mode_t
1205 get_ondisk_perms(int executable, mode_t st_mode)
1207 mode_t xbits = S_IXUSR;
1209 if (executable) {
1210 /* Map read bits to execute bits. */
1211 if (st_mode & S_IRGRP)
1212 xbits |= S_IXGRP;
1213 if (st_mode & S_IROTH)
1214 xbits |= S_IXOTH;
1215 return st_mode | xbits;
1218 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1221 /* forward declaration */
1222 static const struct got_error *
1223 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1224 const char *path, mode_t te_mode, mode_t st_mode,
1225 struct got_blob_object *blob, int restoring_missing_file,
1226 int reverting_versioned_file, int installing_bad_symlink,
1227 int path_is_unversioned, struct got_repository *repo,
1228 got_worktree_checkout_cb progress_cb, void *progress_arg);
1231 * This function assumes that the provided symlink target points at a
1232 * safe location in the work tree!
1234 static const struct got_error *
1235 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1236 size_t target_len)
1238 const struct got_error *err = NULL;
1239 ssize_t elen;
1240 char etarget[PATH_MAX];
1241 int fd;
1244 * "Bad" symlinks (those pointing outside the work tree or into the
1245 * .got directory) are installed in the work tree as a regular file
1246 * which contains the bad symlink target path.
1247 * The new symlink target has already been checked for safety by our
1248 * caller. If we can successfully open a regular file then we simply
1249 * replace this file with a symlink below.
1251 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1252 if (fd == -1) {
1253 if (errno != ELOOP)
1254 return got_error_from_errno2("open", ondisk_path);
1256 /* We are updating an existing on-disk symlink. */
1257 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1258 if (elen == -1)
1259 return got_error_from_errno2("readlink", ondisk_path);
1261 if (elen == target_len &&
1262 memcmp(etarget, target_path, target_len) == 0)
1263 return NULL; /* nothing to do */
1266 err = update_symlink(ondisk_path, target_path, target_len);
1267 if (fd != -1 && close(fd) == -1 && err == NULL)
1268 err = got_error_from_errno2("close", ondisk_path);
1269 return err;
1272 static const struct got_error *
1273 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1274 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1276 const struct got_error *err = NULL;
1277 char canonpath[PATH_MAX];
1278 char *path_got = NULL;
1280 *is_bad_symlink = 0;
1282 if (target_len >= sizeof(canonpath)) {
1283 *is_bad_symlink = 1;
1284 return NULL;
1288 * We do not use realpath(3) to resolve the symlink's target
1289 * path because we don't want to resolve symlinks recursively.
1290 * Instead we make the path absolute and then canonicalize it.
1291 * Relative symlink target lookup should begin at the directory
1292 * in which the blob object is being installed.
1294 if (!got_path_is_absolute(target_path)) {
1295 char *abspath, *parent;
1296 err = got_path_dirname(&parent, ondisk_path);
1297 if (err)
1298 return err;
1299 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1300 free(parent);
1301 return got_error_from_errno("asprintf");
1303 free(parent);
1304 if (strlen(abspath) >= sizeof(canonpath)) {
1305 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1306 free(abspath);
1307 return err;
1309 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1310 free(abspath);
1311 if (err)
1312 return err;
1313 } else {
1314 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1315 if (err)
1316 return err;
1319 /* Only allow symlinks pointing at paths within the work tree. */
1320 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1321 *is_bad_symlink = 1;
1322 return NULL;
1325 /* Do not allow symlinks pointing into the .got directory. */
1326 if (asprintf(&path_got, "%s/%s", wtroot_path,
1327 GOT_WORKTREE_GOT_DIR) == -1)
1328 return got_error_from_errno("asprintf");
1329 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1330 *is_bad_symlink = 1;
1332 free(path_got);
1333 return NULL;
1336 static const struct got_error *
1337 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1338 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1339 int restoring_missing_file, int reverting_versioned_file,
1340 int path_is_unversioned, struct got_repository *repo,
1341 got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 const struct got_error *err = NULL;
1344 char target_path[PATH_MAX];
1345 size_t len, target_len = 0;
1346 char *path_got = NULL;
1347 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1348 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1350 *is_bad_symlink = 0;
1353 * Blob object content specifies the target path of the link.
1354 * If a symbolic link cannot be installed we instead create
1355 * a regular file which contains the link target path stored
1356 * in the blob object.
1358 do {
1359 err = got_object_blob_read_block(&len, blob);
1360 if (len + target_len >= sizeof(target_path)) {
1361 /* Path too long; install as a regular file. */
1362 *is_bad_symlink = 1;
1363 got_object_blob_rewind(blob);
1364 return install_blob(worktree, ondisk_path, path,
1365 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1366 restoring_missing_file, reverting_versioned_file,
1367 1, path_is_unversioned, repo, progress_cb,
1368 progress_arg);
1370 if (len > 0) {
1371 /* Skip blob object header first time around. */
1372 memcpy(target_path + target_len, buf + hdrlen,
1373 len - hdrlen);
1374 target_len += len - hdrlen;
1375 hdrlen = 0;
1377 } while (len != 0);
1378 target_path[target_len] = '\0';
1380 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1381 ondisk_path, worktree->root_path);
1382 if (err)
1383 return err;
1385 if (*is_bad_symlink) {
1386 /* install as a regular file */
1387 *is_bad_symlink = 1;
1388 got_object_blob_rewind(blob);
1389 err = install_blob(worktree, ondisk_path, path,
1390 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 restoring_missing_file, reverting_versioned_file, 1,
1392 path_is_unversioned, repo, progress_cb, progress_arg);
1393 goto done;
1396 if (symlink(target_path, ondisk_path) == -1) {
1397 if (errno == EEXIST) {
1398 if (path_is_unversioned) {
1399 err = (*progress_cb)(progress_arg,
1400 GOT_STATUS_UNVERSIONED, path);
1401 goto done;
1403 err = replace_existing_symlink(ondisk_path,
1404 target_path, target_len);
1405 if (err)
1406 goto done;
1407 if (progress_cb) {
1408 err = (*progress_cb)(progress_arg,
1409 reverting_versioned_file ?
1410 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1411 path);
1413 goto done; /* Nothing else to do. */
1416 if (errno == ENOENT) {
1417 char *parent;
1418 err = got_path_dirname(&parent, ondisk_path);
1419 if (err)
1420 goto done;
1421 err = add_dir_on_disk(worktree, parent);
1422 free(parent);
1423 if (err)
1424 goto done;
1426 * Retry, and fall through to error handling
1427 * below if this second attempt fails.
1429 if (symlink(target_path, ondisk_path) != -1) {
1430 err = NULL; /* success */
1431 goto done;
1435 /* Handle errors from first or second creation attempt. */
1436 if (errno == ENAMETOOLONG) {
1437 /* bad target path; install as a regular file */
1438 *is_bad_symlink = 1;
1439 got_object_blob_rewind(blob);
1440 err = install_blob(worktree, ondisk_path, path,
1441 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1442 restoring_missing_file, reverting_versioned_file, 1,
1443 path_is_unversioned, repo,
1444 progress_cb, progress_arg);
1445 } else if (errno == ENOTDIR) {
1446 err = got_error_path(ondisk_path,
1447 GOT_ERR_FILE_OBSTRUCTED);
1448 } else {
1449 err = got_error_from_errno3("symlink",
1450 target_path, ondisk_path);
1452 } else if (progress_cb)
1453 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1454 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1455 done:
1456 free(path_got);
1457 return err;
1460 static const struct got_error *
1461 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1462 const char *path, mode_t te_mode, mode_t st_mode,
1463 struct got_blob_object *blob, int restoring_missing_file,
1464 int reverting_versioned_file, int installing_bad_symlink,
1465 int path_is_unversioned, struct got_repository *repo,
1466 got_worktree_checkout_cb progress_cb, void *progress_arg)
1468 const struct got_error *err = NULL;
1469 int fd = -1;
1470 size_t len, hdrlen;
1471 int update = 0;
1472 char *tmppath = NULL;
1474 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1475 GOT_DEFAULT_FILE_MODE);
1476 if (fd == -1) {
1477 if (errno == ENOENT) {
1478 char *parent;
1479 err = got_path_dirname(&parent, path);
1480 if (err)
1481 return err;
1482 err = add_dir_on_disk(worktree, parent);
1483 free(parent);
1484 if (err)
1485 return err;
1486 fd = open(ondisk_path,
1487 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1488 GOT_DEFAULT_FILE_MODE);
1489 if (fd == -1)
1490 return got_error_from_errno2("open",
1491 ondisk_path);
1492 } else if (errno == EEXIST) {
1493 if (path_is_unversioned) {
1494 err = (*progress_cb)(progress_arg,
1495 GOT_STATUS_UNVERSIONED, path);
1496 goto done;
1498 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1499 /* TODO file is obstructed; do something */
1500 err = got_error_path(ondisk_path,
1501 GOT_ERR_FILE_OBSTRUCTED);
1502 goto done;
1503 } else {
1504 err = got_opentemp_named_fd(&tmppath, &fd,
1505 ondisk_path);
1506 if (err)
1507 goto done;
1508 update = 1;
1510 } else
1511 return got_error_from_errno2("open", ondisk_path);
1514 if (progress_cb) {
1515 if (restoring_missing_file)
1516 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1517 path);
1518 else if (reverting_versioned_file)
1519 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1520 path);
1521 else
1522 err = (*progress_cb)(progress_arg,
1523 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1524 if (err)
1525 goto done;
1528 hdrlen = got_object_blob_get_hdrlen(blob);
1529 do {
1530 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1531 err = got_object_blob_read_block(&len, blob);
1532 if (err)
1533 break;
1534 if (len > 0) {
1535 /* Skip blob object header first time around. */
1536 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1537 if (outlen == -1) {
1538 err = got_error_from_errno("write");
1539 goto done;
1540 } else if (outlen != len - hdrlen) {
1541 err = got_error(GOT_ERR_IO);
1542 goto done;
1544 hdrlen = 0;
1546 } while (len != 0);
1548 if (fsync(fd) != 0) {
1549 err = got_error_from_errno("fsync");
1550 goto done;
1553 if (update) {
1554 if (rename(tmppath, ondisk_path) != 0) {
1555 err = got_error_from_errno3("rename", tmppath,
1556 ondisk_path);
1557 unlink(tmppath);
1558 goto done;
1562 if (chmod(ondisk_path,
1563 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1564 err = got_error_from_errno2("chmod", ondisk_path);
1565 goto done;
1568 done:
1569 if (fd != -1 && close(fd) != 0 && err == NULL)
1570 err = got_error_from_errno("close");
1571 free(tmppath);
1572 return err;
1575 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1576 static const struct got_error *
1577 get_modified_file_content_status(unsigned char *status, FILE *f)
1579 const struct got_error *err = NULL;
1580 const char *markers[3] = {
1581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1582 GOT_DIFF_CONFLICT_MARKER_SEP,
1583 GOT_DIFF_CONFLICT_MARKER_END
1585 int i = 0;
1586 char *line;
1587 size_t len;
1588 const char delim[3] = {'\0', '\0', '\0'};
1590 while (*status == GOT_STATUS_MODIFY) {
1591 line = fparseln(f, &len, NULL, delim, 0);
1592 if (line == NULL) {
1593 if (feof(f))
1594 break;
1595 err = got_ferror(f, GOT_ERR_IO);
1596 break;
1599 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1600 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1601 == 0)
1602 *status = GOT_STATUS_CONFLICT;
1603 else
1604 i++;
1608 return err;
1611 static int
1612 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1614 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1615 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1618 static int
1619 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1621 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1622 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1623 ie->mtime_sec == sb->st_mtim.tv_sec &&
1624 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1625 ie->size == (sb->st_size & 0xffffffff) &&
1626 !xbit_differs(ie, sb->st_mode));
1629 static unsigned char
1630 get_staged_status(struct got_fileindex_entry *ie)
1632 switch (got_fileindex_entry_stage_get(ie)) {
1633 case GOT_FILEIDX_STAGE_ADD:
1634 return GOT_STATUS_ADD;
1635 case GOT_FILEIDX_STAGE_DELETE:
1636 return GOT_STATUS_DELETE;
1637 case GOT_FILEIDX_STAGE_MODIFY:
1638 return GOT_STATUS_MODIFY;
1639 default:
1640 return GOT_STATUS_NO_CHANGE;
1644 static const struct got_error *
1645 get_symlink_modification_status(unsigned char *status,
1646 struct got_fileindex_entry *ie, const char *abspath,
1647 int dirfd, const char *de_name, struct got_blob_object *blob)
1649 const struct got_error *err = NULL;
1650 char target_path[PATH_MAX];
1651 char etarget[PATH_MAX];
1652 ssize_t elen;
1653 size_t len, target_len = 0;
1654 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1655 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1657 *status = GOT_STATUS_NO_CHANGE;
1659 /* Blob object content specifies the target path of the link. */
1660 do {
1661 err = got_object_blob_read_block(&len, blob);
1662 if (err)
1663 return err;
1664 if (len + target_len >= sizeof(target_path)) {
1666 * Should not happen. The blob contents were OK
1667 * when this symlink was installed.
1669 return got_error(GOT_ERR_NO_SPACE);
1671 if (len > 0) {
1672 /* Skip blob object header first time around. */
1673 memcpy(target_path + target_len, buf + hdrlen,
1674 len - hdrlen);
1675 target_len += len - hdrlen;
1676 hdrlen = 0;
1678 } while (len != 0);
1679 target_path[target_len] = '\0';
1681 if (dirfd != -1) {
1682 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1683 if (elen == -1)
1684 return got_error_from_errno2("readlinkat", abspath);
1685 } else {
1686 elen = readlink(abspath, etarget, sizeof(etarget));
1687 if (elen == -1)
1688 return got_error_from_errno2("readlink", abspath);
1691 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1692 *status = GOT_STATUS_MODIFY;
1694 return NULL;
1697 static const struct got_error *
1698 get_file_status(unsigned char *status, struct stat *sb,
1699 struct got_fileindex_entry *ie, const char *abspath,
1700 int dirfd, const char *de_name, struct got_repository *repo)
1702 const struct got_error *err = NULL;
1703 struct got_object_id id;
1704 size_t hdrlen;
1705 int fd = -1;
1706 FILE *f = NULL;
1707 uint8_t fbuf[8192];
1708 struct got_blob_object *blob = NULL;
1709 size_t flen, blen;
1710 unsigned char staged_status = get_staged_status(ie);
1712 *status = GOT_STATUS_NO_CHANGE;
1715 * Whenever the caller provides a directory descriptor and a
1716 * directory entry name for the file, use them! This prevents
1717 * race conditions if filesystem paths change beneath our feet.
1719 if (dirfd != -1) {
1720 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1721 if (errno == ENOENT) {
1722 if (got_fileindex_entry_has_file_on_disk(ie))
1723 *status = GOT_STATUS_MISSING;
1724 else
1725 *status = GOT_STATUS_DELETE;
1726 goto done;
1728 err = got_error_from_errno2("fstatat", abspath);
1729 goto done;
1731 } else {
1732 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1733 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1734 return got_error_from_errno2("open", abspath);
1735 else if (fd == -1 && errno == ELOOP) {
1736 if (lstat(abspath, sb) == -1)
1737 return got_error_from_errno2("lstat", abspath);
1738 } else if (fd == -1 || fstat(fd, sb) == -1) {
1739 if (errno == ENOENT) {
1740 if (got_fileindex_entry_has_file_on_disk(ie))
1741 *status = GOT_STATUS_MISSING;
1742 else
1743 *status = GOT_STATUS_DELETE;
1744 goto done;
1746 err = got_error_from_errno2("fstat", abspath);
1747 goto done;
1751 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1752 *status = GOT_STATUS_OBSTRUCTED;
1753 goto done;
1756 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1757 *status = GOT_STATUS_DELETE;
1758 goto done;
1759 } else if (!got_fileindex_entry_has_blob(ie) &&
1760 staged_status != GOT_STATUS_ADD) {
1761 *status = GOT_STATUS_ADD;
1762 goto done;
1765 if (!stat_info_differs(ie, sb))
1766 goto done;
1768 if (S_ISLNK(sb->st_mode) &&
1769 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1770 *status = GOT_STATUS_MODIFY;
1771 goto done;
1774 if (staged_status == GOT_STATUS_MODIFY ||
1775 staged_status == GOT_STATUS_ADD)
1776 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1777 else
1778 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1780 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1781 if (err)
1782 goto done;
1784 if (S_ISLNK(sb->st_mode)) {
1785 err = get_symlink_modification_status(status, ie,
1786 abspath, dirfd, de_name, blob);
1787 goto done;
1790 if (dirfd != -1) {
1791 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1792 if (fd == -1) {
1793 err = got_error_from_errno2("openat", abspath);
1794 goto done;
1798 f = fdopen(fd, "r");
1799 if (f == NULL) {
1800 err = got_error_from_errno2("fdopen", abspath);
1801 goto done;
1803 fd = -1;
1804 hdrlen = got_object_blob_get_hdrlen(blob);
1805 for (;;) {
1806 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1807 err = got_object_blob_read_block(&blen, blob);
1808 if (err)
1809 goto done;
1810 /* Skip length of blob object header first time around. */
1811 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1812 if (flen == 0 && ferror(f)) {
1813 err = got_error_from_errno("fread");
1814 goto done;
1816 if (blen - hdrlen == 0) {
1817 if (flen != 0)
1818 *status = GOT_STATUS_MODIFY;
1819 break;
1820 } else if (flen == 0) {
1821 if (blen - hdrlen != 0)
1822 *status = GOT_STATUS_MODIFY;
1823 break;
1824 } else if (blen - hdrlen == flen) {
1825 /* Skip blob object header first time around. */
1826 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1827 *status = GOT_STATUS_MODIFY;
1828 break;
1830 } else {
1831 *status = GOT_STATUS_MODIFY;
1832 break;
1834 hdrlen = 0;
1837 if (*status == GOT_STATUS_MODIFY) {
1838 rewind(f);
1839 err = get_modified_file_content_status(status, f);
1840 } else if (xbit_differs(ie, sb->st_mode))
1841 *status = GOT_STATUS_MODE_CHANGE;
1842 done:
1843 if (blob)
1844 got_object_blob_close(blob);
1845 if (f != NULL && fclose(f) == EOF && err == NULL)
1846 err = got_error_from_errno2("fclose", abspath);
1847 if (fd != -1 && close(fd) == -1 && err == NULL)
1848 err = got_error_from_errno2("close", abspath);
1849 return err;
1853 * Update timestamps in the file index if a file is unmodified and
1854 * we had to run a full content comparison to find out.
1856 static const struct got_error *
1857 sync_timestamps(char *ondisk_path, unsigned char status,
1858 struct got_fileindex_entry *ie, struct stat *sb)
1860 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1861 return got_fileindex_entry_update(ie, ondisk_path,
1862 ie->blob_sha1, ie->commit_sha1, 1);
1864 return NULL;
1867 static const struct got_error *
1868 update_blob(struct got_worktree *worktree,
1869 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1870 struct got_tree_entry *te, const char *path,
1871 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1872 void *progress_arg)
1874 const struct got_error *err = NULL;
1875 struct got_blob_object *blob = NULL;
1876 char *ondisk_path;
1877 unsigned char status = GOT_STATUS_NO_CHANGE;
1878 struct stat sb;
1880 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1881 return got_error_from_errno("asprintf");
1883 if (ie) {
1884 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1885 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1886 goto done;
1888 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1889 repo);
1890 if (err)
1891 goto done;
1892 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1893 sb.st_mode = got_fileindex_perms_to_st(ie);
1894 } else {
1895 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1896 status = GOT_STATUS_UNVERSIONED;
1899 if (status == GOT_STATUS_OBSTRUCTED) {
1900 err = (*progress_cb)(progress_arg, status, path);
1901 goto done;
1903 if (status == GOT_STATUS_CONFLICT) {
1904 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1905 path);
1906 goto done;
1909 if (ie && status != GOT_STATUS_MISSING &&
1910 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1911 if (got_fileindex_entry_has_commit(ie) &&
1912 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1913 SHA1_DIGEST_LENGTH) == 0) {
1914 err = sync_timestamps(ondisk_path, status, ie, &sb);
1915 if (err)
1916 goto done;
1917 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1918 path);
1919 goto done;
1921 if (got_fileindex_entry_has_blob(ie) &&
1922 memcmp(ie->blob_sha1, te->id.sha1,
1923 SHA1_DIGEST_LENGTH) == 0) {
1924 err = sync_timestamps(ondisk_path, status, ie, &sb);
1925 goto done;
1929 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1930 if (err)
1931 goto done;
1933 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1934 int update_timestamps;
1935 struct got_blob_object *blob2 = NULL;
1936 char *label_orig = NULL;
1937 if (got_fileindex_entry_has_blob(ie)) {
1938 struct got_object_id id2;
1939 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1940 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1941 if (err)
1942 goto done;
1944 if (got_fileindex_entry_has_commit(ie)) {
1945 char id_str[SHA1_DIGEST_STRING_LENGTH];
1946 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1947 sizeof(id_str)) == NULL) {
1948 err = got_error_path(id_str,
1949 GOT_ERR_BAD_OBJ_ID_STR);
1950 goto done;
1952 if (asprintf(&label_orig, "%s: commit %s",
1953 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1954 err = got_error_from_errno("asprintf");
1955 goto done;
1958 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1959 char *link_target;
1960 err = got_object_blob_read_to_str(&link_target, blob);
1961 if (err)
1962 goto done;
1963 err = merge_symlink(worktree, blob2, ondisk_path, path,
1964 label_orig, link_target, worktree->base_commit_id,
1965 repo, progress_cb, progress_arg);
1966 free(link_target);
1967 } else {
1968 err = merge_blob(&update_timestamps, worktree, blob2,
1969 ondisk_path, path, sb.st_mode, label_orig, blob,
1970 worktree->base_commit_id, repo,
1971 progress_cb, progress_arg);
1973 free(label_orig);
1974 if (blob2)
1975 got_object_blob_close(blob2);
1976 if (err)
1977 goto done;
1979 * Do not update timestamps of files with local changes.
1980 * Otherwise, a future status walk would treat them as
1981 * unmodified files again.
1983 err = got_fileindex_entry_update(ie, ondisk_path,
1984 blob->id.sha1, worktree->base_commit_id->sha1,
1985 update_timestamps);
1986 } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1989 } else if (status == GOT_STATUS_DELETE) {
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1991 if (err)
1992 goto done;
1993 err = got_fileindex_entry_update(ie, ondisk_path,
1994 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1995 if (err)
1996 goto done;
1997 } else {
1998 int is_bad_symlink = 0;
1999 if (S_ISLNK(te->mode)) {
2000 err = install_symlink(&is_bad_symlink, worktree,
2001 ondisk_path, path, blob,
2002 status == GOT_STATUS_MISSING, 0,
2003 status == GOT_STATUS_UNVERSIONED, repo,
2004 progress_cb, progress_arg);
2005 } else {
2006 err = install_blob(worktree, ondisk_path, path,
2007 te->mode, sb.st_mode, blob,
2008 status == GOT_STATUS_MISSING, 0, 0,
2009 status == GOT_STATUS_UNVERSIONED, repo,
2010 progress_cb, progress_arg);
2012 if (err)
2013 goto done;
2015 if (ie) {
2016 err = got_fileindex_entry_update(ie, ondisk_path,
2017 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2018 } else {
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, ondisk_path, path,
2021 &blob->id);
2023 if (err)
2024 goto done;
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2031 got_object_blob_close(blob);
2032 done:
2033 free(ondisk_path);
2034 return err;
2037 static const struct got_error *
2038 remove_ondisk_file(const char *root_path, const char *path)
2040 const struct got_error *err = NULL;
2041 char *ondisk_path = NULL;
2043 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2044 return got_error_from_errno("asprintf");
2046 if (unlink(ondisk_path) == -1) {
2047 if (errno != ENOENT)
2048 err = got_error_from_errno2("unlink", ondisk_path);
2049 } else {
2050 char *parent = dirname(ondisk_path);
2051 while (parent && strcmp(parent, root_path) != 0) {
2052 if (rmdir(parent) == -1) {
2053 if (errno != ENOTEMPTY)
2054 err = got_error_from_errno2("rmdir",
2055 parent);
2056 break;
2058 parent = dirname(parent);
2061 free(ondisk_path);
2062 return err;
2065 static const struct got_error *
2066 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 struct got_fileindex_entry *ie, struct got_repository *repo,
2068 got_worktree_checkout_cb progress_cb, void *progress_arg)
2070 const struct got_error *err = NULL;
2071 unsigned char status;
2072 struct stat sb;
2073 char *ondisk_path;
2075 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2078 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2079 == -1)
2080 return got_error_from_errno("asprintf");
2082 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2083 if (err)
2084 goto done;
2086 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 char ondisk_target[PATH_MAX];
2088 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 sizeof(ondisk_target));
2090 if (ondisk_len == -1) {
2091 err = got_error_from_errno2("readlink", ondisk_path);
2092 goto done;
2094 ondisk_target[ondisk_len] = '\0';
2095 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 NULL, NULL, /* XXX pass common ancestor info? */
2097 ondisk_target, ondisk_path);
2098 if (err)
2099 goto done;
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2101 ie->path);
2102 goto done;
2105 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 status == GOT_STATUS_ADD) {
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2108 if (err)
2109 goto done;
2111 * Preserve the working file and change the deleted blob's
2112 * entry into a schedule-add entry.
2114 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2115 0);
2116 } else {
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2118 if (err)
2119 goto done;
2120 if (status == GOT_STATUS_NO_CHANGE) {
2121 err = remove_ondisk_file(worktree->root_path, ie->path);
2122 if (err)
2123 goto done;
2125 got_fileindex_entry_remove(fileindex, ie);
2127 done:
2128 free(ondisk_path);
2129 return err;
2132 struct diff_cb_arg {
2133 struct got_fileindex *fileindex;
2134 struct got_worktree *worktree;
2135 struct got_repository *repo;
2136 got_worktree_checkout_cb progress_cb;
2137 void *progress_arg;
2138 got_cancel_cb cancel_cb;
2139 void *cancel_arg;
2142 static const struct got_error *
2143 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 struct got_tree_entry *te, const char *parent_path)
2146 struct diff_cb_arg *a = arg;
2148 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 return got_error(GOT_ERR_CANCELLED);
2151 return update_blob(a->worktree, a->fileindex, ie, te,
2152 ie->path, a->repo, a->progress_cb, a->progress_arg);
2155 static const struct got_error *
2156 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2158 struct diff_cb_arg *a = arg;
2160 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 return got_error(GOT_ERR_CANCELLED);
2163 return delete_blob(a->worktree, a->fileindex, ie,
2164 a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2171 const struct got_error *err;
2172 char *path;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 if (got_object_tree_entry_is_submodule(te))
2178 return NULL;
2180 if (asprintf(&path, "%s%s%s", parent_path,
2181 parent_path[0] ? "/" : "", te->name)
2182 == -1)
2183 return got_error_from_errno("asprintf");
2185 if (S_ISDIR(te->mode))
2186 err = add_dir_on_disk(a->worktree, path);
2187 else
2188 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 a->repo, a->progress_cb, a->progress_arg);
2191 free(path);
2192 return err;
2195 const struct got_error *
2196 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2198 uint32_t uuid_status;
2200 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 if (uuid_status != uuid_s_ok) {
2202 *uuidstr = NULL;
2203 return got_error_uuid(uuid_status, "uuid_to_string");
2206 return NULL;
2209 static const struct got_error *
2210 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2212 const struct got_error *err = NULL;
2213 char *uuidstr = NULL;
2215 *refname = NULL;
2217 err = got_worktree_get_uuid(&uuidstr, worktree);
2218 if (err)
2219 return err;
2221 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 err = got_error_from_errno("asprintf");
2223 *refname = NULL;
2225 free(uuidstr);
2226 return err;
2229 const struct got_error *
2230 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2232 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2235 static const struct got_error *
2236 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree,
2239 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2242 static const struct got_error *
2243 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2262 static const struct got_error *
2263 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2290 const struct got_error *
2291 got_worktree_get_histedit_script_path(char **path,
2292 struct got_worktree *worktree)
2294 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2296 *path = NULL;
2297 return got_error_from_errno("asprintf");
2299 return NULL;
2303 * Prevent Git's garbage collector from deleting our base commit by
2304 * setting a reference to our base commit's ID.
2306 static const struct got_error *
2307 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2309 const struct got_error *err = NULL;
2310 struct got_reference *ref = NULL;
2311 char *refname;
2313 err = got_worktree_get_base_ref_name(&refname, worktree);
2314 if (err)
2315 return err;
2317 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2318 if (err)
2319 goto done;
2321 err = got_ref_write(ref, repo);
2322 done:
2323 free(refname);
2324 if (ref)
2325 got_ref_close(ref);
2326 return err;
2329 static const struct got_error *
2330 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2332 const struct got_error *err = NULL;
2334 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2335 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2336 err = got_error_from_errno("asprintf");
2337 *fileindex_path = NULL;
2339 return err;
2343 static const struct got_error *
2344 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2345 struct got_worktree *worktree)
2347 const struct got_error *err = NULL;
2348 FILE *index = NULL;
2350 *fileindex_path = NULL;
2351 *fileindex = got_fileindex_alloc();
2352 if (*fileindex == NULL)
2353 return got_error_from_errno("got_fileindex_alloc");
2355 err = get_fileindex_path(fileindex_path, worktree);
2356 if (err)
2357 goto done;
2359 index = fopen(*fileindex_path, "rb");
2360 if (index == NULL) {
2361 if (errno != ENOENT)
2362 err = got_error_from_errno2("fopen", *fileindex_path);
2363 } else {
2364 err = got_fileindex_read(*fileindex, index);
2365 if (fclose(index) != 0 && err == NULL)
2366 err = got_error_from_errno("fclose");
2368 done:
2369 if (err) {
2370 free(*fileindex_path);
2371 *fileindex_path = NULL;
2372 got_fileindex_free(*fileindex);
2373 *fileindex = NULL;
2375 return err;
2378 struct bump_base_commit_id_arg {
2379 struct got_object_id *base_commit_id;
2380 const char *path;
2381 size_t path_len;
2382 const char *entry_name;
2383 got_worktree_checkout_cb progress_cb;
2384 void *progress_arg;
2387 /* Bump base commit ID of all files within an updated part of the work tree. */
2388 static const struct got_error *
2389 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2391 const struct got_error *err;
2392 struct bump_base_commit_id_arg *a = arg;
2394 if (a->entry_name) {
2395 if (strcmp(ie->path, a->path) != 0)
2396 return NULL;
2397 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2398 return NULL;
2400 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2401 SHA1_DIGEST_LENGTH) == 0)
2402 return NULL;
2404 if (a->progress_cb) {
2405 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2406 ie->path);
2407 if (err)
2408 return err;
2410 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2411 return NULL;
2414 static const struct got_error *
2415 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2417 const struct got_error *err = NULL;
2418 char *new_fileindex_path = NULL;
2419 FILE *new_index = NULL;
2420 struct timespec timeout;
2422 err = got_opentemp_named(&new_fileindex_path, &new_index,
2423 fileindex_path);
2424 if (err)
2425 goto done;
2427 err = got_fileindex_write(fileindex, new_index);
2428 if (err)
2429 goto done;
2431 if (rename(new_fileindex_path, fileindex_path) != 0) {
2432 err = got_error_from_errno3("rename", new_fileindex_path,
2433 fileindex_path);
2434 unlink(new_fileindex_path);
2438 * Sleep for a short amount of time to ensure that files modified after
2439 * this program exits have a different time stamp from the one which
2440 * was recorded in the file index.
2442 timeout.tv_sec = 0;
2443 timeout.tv_nsec = 1;
2444 nanosleep(&timeout, NULL);
2445 done:
2446 if (new_index)
2447 fclose(new_index);
2448 free(new_fileindex_path);
2449 return err;
2452 static const struct got_error *
2453 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2454 struct got_object_id **tree_id, const char *wt_relpath,
2455 struct got_worktree *worktree, struct got_repository *repo)
2457 const struct got_error *err = NULL;
2458 struct got_object_id *id = NULL;
2459 char *in_repo_path = NULL;
2460 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2462 *entry_type = GOT_OBJ_TYPE_ANY;
2463 *tree_relpath = NULL;
2464 *tree_id = NULL;
2466 if (wt_relpath[0] == '\0') {
2467 /* Check out all files within the work tree. */
2468 *entry_type = GOT_OBJ_TYPE_TREE;
2469 *tree_relpath = strdup("");
2470 if (*tree_relpath == NULL) {
2471 err = got_error_from_errno("strdup");
2472 goto done;
2474 err = got_object_id_by_path(tree_id, repo,
2475 worktree->base_commit_id, worktree->path_prefix);
2476 if (err)
2477 goto done;
2478 return NULL;
2481 /* Check out a subset of files in the work tree. */
2483 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2484 is_root_wt ? "" : "/", wt_relpath) == -1) {
2485 err = got_error_from_errno("asprintf");
2486 goto done;
2489 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2490 in_repo_path);
2491 if (err)
2492 goto done;
2494 free(in_repo_path);
2495 in_repo_path = NULL;
2497 err = got_object_get_type(entry_type, repo, id);
2498 if (err)
2499 goto done;
2501 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2502 /* Check out a single file. */
2503 if (strchr(wt_relpath, '/') == NULL) {
2504 /* Check out a single file in work tree's root dir. */
2505 in_repo_path = strdup(worktree->path_prefix);
2506 if (in_repo_path == NULL) {
2507 err = got_error_from_errno("strdup");
2508 goto done;
2510 *tree_relpath = strdup("");
2511 if (*tree_relpath == NULL) {
2512 err = got_error_from_errno("strdup");
2513 goto done;
2515 } else {
2516 /* Check out a single file in a subdirectory. */
2517 err = got_path_dirname(tree_relpath, wt_relpath);
2518 if (err)
2519 return err;
2520 if (asprintf(&in_repo_path, "%s%s%s",
2521 worktree->path_prefix, is_root_wt ? "" : "/",
2522 *tree_relpath) == -1) {
2523 err = got_error_from_errno("asprintf");
2524 goto done;
2527 err = got_object_id_by_path(tree_id, repo,
2528 worktree->base_commit_id, in_repo_path);
2529 } else {
2530 /* Check out all files within a subdirectory. */
2531 *tree_id = got_object_id_dup(id);
2532 if (*tree_id == NULL) {
2533 err = got_error_from_errno("got_object_id_dup");
2534 goto done;
2536 *tree_relpath = strdup(wt_relpath);
2537 if (*tree_relpath == NULL) {
2538 err = got_error_from_errno("strdup");
2539 goto done;
2542 done:
2543 free(id);
2544 free(in_repo_path);
2545 if (err) {
2546 *entry_type = GOT_OBJ_TYPE_ANY;
2547 free(*tree_relpath);
2548 *tree_relpath = NULL;
2549 free(*tree_id);
2550 *tree_id = NULL;
2552 return err;
2555 static const struct got_error *
2556 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2557 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2558 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2559 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2561 const struct got_error *err = NULL;
2562 struct got_commit_object *commit = NULL;
2563 struct got_tree_object *tree = NULL;
2564 struct got_fileindex_diff_tree_cb diff_cb;
2565 struct diff_cb_arg arg;
2567 err = ref_base_commit(worktree, repo);
2568 if (err) {
2569 if (!(err->code == GOT_ERR_ERRNO &&
2570 (errno == EACCES || errno == EROFS)))
2571 goto done;
2572 err = (*progress_cb)(progress_arg,
2573 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2574 if (err)
2575 return err;
2578 err = got_object_open_as_commit(&commit, repo,
2579 worktree->base_commit_id);
2580 if (err)
2581 goto done;
2583 err = got_object_open_as_tree(&tree, repo, tree_id);
2584 if (err)
2585 goto done;
2587 if (entry_name &&
2588 got_object_tree_find_entry(tree, entry_name) == NULL) {
2589 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2590 goto done;
2593 diff_cb.diff_old_new = diff_old_new;
2594 diff_cb.diff_old = diff_old;
2595 diff_cb.diff_new = diff_new;
2596 arg.fileindex = fileindex;
2597 arg.worktree = worktree;
2598 arg.repo = repo;
2599 arg.progress_cb = progress_cb;
2600 arg.progress_arg = progress_arg;
2601 arg.cancel_cb = cancel_cb;
2602 arg.cancel_arg = cancel_arg;
2603 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2604 entry_name, repo, &diff_cb, &arg);
2605 done:
2606 if (tree)
2607 got_object_tree_close(tree);
2608 if (commit)
2609 got_object_commit_close(commit);
2610 return err;
2613 const struct got_error *
2614 got_worktree_checkout_files(struct got_worktree *worktree,
2615 struct got_pathlist_head *paths, struct got_repository *repo,
2616 got_worktree_checkout_cb progress_cb, void *progress_arg,
2617 got_cancel_cb cancel_cb, void *cancel_arg)
2619 const struct got_error *err = NULL, *sync_err, *unlockerr;
2620 struct got_commit_object *commit = NULL;
2621 struct got_tree_object *tree = NULL;
2622 struct got_fileindex *fileindex = NULL;
2623 char *fileindex_path = NULL;
2624 struct got_pathlist_entry *pe;
2625 struct tree_path_data {
2626 SIMPLEQ_ENTRY(tree_path_data) entry;
2627 struct got_object_id *tree_id;
2628 int entry_type;
2629 char *relpath;
2630 char *entry_name;
2631 } *tpd = NULL;
2632 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2634 SIMPLEQ_INIT(&tree_paths);
2636 err = lock_worktree(worktree, LOCK_EX);
2637 if (err)
2638 return err;
2640 /* Map all specified paths to in-repository trees. */
2641 TAILQ_FOREACH(pe, paths, entry) {
2642 tpd = malloc(sizeof(*tpd));
2643 if (tpd == NULL) {
2644 err = got_error_from_errno("malloc");
2645 goto done;
2648 err = find_tree_entry_for_checkout(&tpd->entry_type,
2649 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2650 if (err) {
2651 free(tpd);
2652 goto done;
2655 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2656 err = got_path_basename(&tpd->entry_name, pe->path);
2657 if (err) {
2658 free(tpd->relpath);
2659 free(tpd->tree_id);
2660 free(tpd);
2661 goto done;
2663 } else
2664 tpd->entry_name = NULL;
2666 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2670 * Read the file index.
2671 * Checking out files is supposed to be an idempotent operation.
2672 * If the on-disk file index is incomplete we will try to complete it.
2674 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2675 if (err)
2676 goto done;
2678 tpd = SIMPLEQ_FIRST(&tree_paths);
2679 TAILQ_FOREACH(pe, paths, entry) {
2680 struct bump_base_commit_id_arg bbc_arg;
2682 err = checkout_files(worktree, fileindex, tpd->relpath,
2683 tpd->tree_id, tpd->entry_name, repo,
2684 progress_cb, progress_arg, cancel_cb, cancel_arg);
2685 if (err)
2686 break;
2688 bbc_arg.base_commit_id = worktree->base_commit_id;
2689 bbc_arg.entry_name = tpd->entry_name;
2690 bbc_arg.path = pe->path;
2691 bbc_arg.path_len = pe->path_len;
2692 bbc_arg.progress_cb = progress_cb;
2693 bbc_arg.progress_arg = progress_arg;
2694 err = got_fileindex_for_each_entry_safe(fileindex,
2695 bump_base_commit_id, &bbc_arg);
2696 if (err)
2697 break;
2699 tpd = SIMPLEQ_NEXT(tpd, entry);
2701 sync_err = sync_fileindex(fileindex, fileindex_path);
2702 if (sync_err && err == NULL)
2703 err = sync_err;
2704 done:
2705 free(fileindex_path);
2706 if (tree)
2707 got_object_tree_close(tree);
2708 if (commit)
2709 got_object_commit_close(commit);
2710 if (fileindex)
2711 got_fileindex_free(fileindex);
2712 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2713 tpd = SIMPLEQ_FIRST(&tree_paths);
2714 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2715 free(tpd->relpath);
2716 free(tpd->tree_id);
2717 free(tpd);
2719 unlockerr = lock_worktree(worktree, LOCK_SH);
2720 if (unlockerr && err == NULL)
2721 err = unlockerr;
2722 return err;
2725 struct merge_file_cb_arg {
2726 struct got_worktree *worktree;
2727 struct got_fileindex *fileindex;
2728 got_worktree_checkout_cb progress_cb;
2729 void *progress_arg;
2730 got_cancel_cb cancel_cb;
2731 void *cancel_arg;
2732 const char *label_orig;
2733 struct got_object_id *commit_id2;
2736 static const struct got_error *
2737 merge_file_cb(void *arg, struct got_blob_object *blob1,
2738 struct got_blob_object *blob2, struct got_object_id *id1,
2739 struct got_object_id *id2, const char *path1, const char *path2,
2740 mode_t mode1, mode_t mode2, struct got_repository *repo)
2742 static const struct got_error *err = NULL;
2743 struct merge_file_cb_arg *a = arg;
2744 struct got_fileindex_entry *ie;
2745 char *ondisk_path = NULL;
2746 struct stat sb;
2747 unsigned char status;
2748 int local_changes_subsumed;
2750 if (blob1 && blob2) {
2751 ie = got_fileindex_entry_get(a->fileindex, path2,
2752 strlen(path2));
2753 if (ie == NULL)
2754 return (*a->progress_cb)(a->progress_arg,
2755 GOT_STATUS_MISSING, path2);
2757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2758 path2) == -1)
2759 return got_error_from_errno("asprintf");
2761 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2762 repo);
2763 if (err)
2764 goto done;
2766 if (status == GOT_STATUS_DELETE) {
2767 err = (*a->progress_cb)(a->progress_arg,
2768 GOT_STATUS_MERGE, path2);
2769 goto done;
2771 if (status != GOT_STATUS_NO_CHANGE &&
2772 status != GOT_STATUS_MODIFY &&
2773 status != GOT_STATUS_CONFLICT &&
2774 status != GOT_STATUS_ADD) {
2775 err = (*a->progress_cb)(a->progress_arg, status, path2);
2776 goto done;
2779 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2780 char *link_target2;
2781 err = got_object_blob_read_to_str(&link_target2, blob2);
2782 if (err)
2783 goto done;
2784 err = merge_symlink(a->worktree, blob1, ondisk_path,
2785 path2, a->label_orig, link_target2, a->commit_id2,
2786 repo, a->progress_cb, a->progress_arg);
2787 free(link_target2);
2788 } else {
2789 err = merge_blob(&local_changes_subsumed, a->worktree,
2790 blob1, ondisk_path, path2, sb.st_mode,
2791 a->label_orig, blob2, a->commit_id2, repo,
2792 a->progress_cb, a->progress_arg);
2794 } else if (blob1) {
2795 ie = got_fileindex_entry_get(a->fileindex, path1,
2796 strlen(path1));
2797 if (ie == NULL)
2798 return (*a->progress_cb)(a->progress_arg,
2799 GOT_STATUS_MISSING, path1);
2801 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2802 path1) == -1)
2803 return got_error_from_errno("asprintf");
2805 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2806 repo);
2807 if (err)
2808 goto done;
2810 switch (status) {
2811 case GOT_STATUS_NO_CHANGE:
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_DELETE, path1);
2814 if (err)
2815 goto done;
2816 err = remove_ondisk_file(a->worktree->root_path, path1);
2817 if (err)
2818 goto done;
2819 if (ie)
2820 got_fileindex_entry_mark_deleted_from_disk(ie);
2821 break;
2822 case GOT_STATUS_DELETE:
2823 case GOT_STATUS_MISSING:
2824 err = (*a->progress_cb)(a->progress_arg,
2825 GOT_STATUS_DELETE, path1);
2826 if (err)
2827 goto done;
2828 if (ie)
2829 got_fileindex_entry_mark_deleted_from_disk(ie);
2830 break;
2831 case GOT_STATUS_ADD: {
2832 struct got_object_id *id;
2833 FILE *blob1_f;
2835 * Delete the added file only if its content already
2836 * exists in the repository.
2838 err = got_object_blob_file_create(&id, &blob1_f, path1);
2839 if (err)
2840 goto done;
2841 if (got_object_id_cmp(id, id1) == 0) {
2842 err = (*a->progress_cb)(a->progress_arg,
2843 GOT_STATUS_DELETE, path1);
2844 if (err)
2845 goto done;
2846 err = remove_ondisk_file(a->worktree->root_path,
2847 path1);
2848 if (err)
2849 goto done;
2850 if (ie)
2851 got_fileindex_entry_remove(a->fileindex,
2852 ie);
2853 } else {
2854 err = (*a->progress_cb)(a->progress_arg,
2855 GOT_STATUS_CANNOT_DELETE, path1);
2857 if (fclose(blob1_f) == EOF && err == NULL)
2858 err = got_error_from_errno("fclose");
2859 free(id);
2860 if (err)
2861 goto done;
2862 break;
2864 case GOT_STATUS_MODIFY:
2865 case GOT_STATUS_CONFLICT:
2866 err = (*a->progress_cb)(a->progress_arg,
2867 GOT_STATUS_CANNOT_DELETE, path1);
2868 if (err)
2869 goto done;
2870 break;
2871 case GOT_STATUS_OBSTRUCTED:
2872 err = (*a->progress_cb)(a->progress_arg, status, path1);
2873 if (err)
2874 goto done;
2875 break;
2876 default:
2877 break;
2879 } else if (blob2) {
2880 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2881 path2) == -1)
2882 return got_error_from_errno("asprintf");
2883 ie = got_fileindex_entry_get(a->fileindex, path2,
2884 strlen(path2));
2885 if (ie) {
2886 err = get_file_status(&status, &sb, ie, ondisk_path,
2887 -1, NULL, repo);
2888 if (err)
2889 goto done;
2890 if (status != GOT_STATUS_NO_CHANGE &&
2891 status != GOT_STATUS_MODIFY &&
2892 status != GOT_STATUS_CONFLICT &&
2893 status != GOT_STATUS_ADD) {
2894 err = (*a->progress_cb)(a->progress_arg,
2895 status, path2);
2896 goto done;
2898 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2899 char *link_target2;
2900 err = got_object_blob_read_to_str(&link_target2,
2901 blob2);
2902 if (err)
2903 goto done;
2904 err = merge_symlink(a->worktree, NULL,
2905 ondisk_path, path2, a->label_orig,
2906 link_target2, a->commit_id2, repo,
2907 a->progress_cb, a->progress_arg);
2908 free(link_target2);
2909 } else if (S_ISREG(sb.st_mode)) {
2910 err = merge_blob(&local_changes_subsumed,
2911 a->worktree, NULL, ondisk_path, path2,
2912 sb.st_mode, a->label_orig, blob2,
2913 a->commit_id2, repo, a->progress_cb,
2914 a->progress_arg);
2915 } else {
2916 err = got_error_path(ondisk_path,
2917 GOT_ERR_FILE_OBSTRUCTED);
2919 if (err)
2920 goto done;
2921 if (status == GOT_STATUS_DELETE) {
2922 err = got_fileindex_entry_update(ie,
2923 ondisk_path, blob2->id.sha1,
2924 a->worktree->base_commit_id->sha1, 0);
2925 if (err)
2926 goto done;
2928 } else {
2929 int is_bad_symlink = 0;
2930 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2931 if (S_ISLNK(mode2)) {
2932 err = install_symlink(&is_bad_symlink,
2933 a->worktree, ondisk_path, path2, blob2, 0,
2934 0, 1, repo, a->progress_cb, a->progress_arg);
2935 } else {
2936 err = install_blob(a->worktree, ondisk_path, path2,
2937 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2938 a->progress_cb, a->progress_arg);
2940 if (err)
2941 goto done;
2942 err = got_fileindex_entry_alloc(&ie, path2);
2943 if (err)
2944 goto done;
2945 err = got_fileindex_entry_update(ie, ondisk_path,
2946 NULL, NULL, 1);
2947 if (err) {
2948 got_fileindex_entry_free(ie);
2949 goto done;
2951 err = got_fileindex_entry_add(a->fileindex, ie);
2952 if (err) {
2953 got_fileindex_entry_free(ie);
2954 goto done;
2956 if (is_bad_symlink) {
2957 got_fileindex_entry_filetype_set(ie,
2958 GOT_FILEIDX_MODE_BAD_SYMLINK);
2962 done:
2963 free(ondisk_path);
2964 return err;
2967 struct check_merge_ok_arg {
2968 struct got_worktree *worktree;
2969 struct got_repository *repo;
2972 static const struct got_error *
2973 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2975 const struct got_error *err = NULL;
2976 struct check_merge_ok_arg *a = arg;
2977 unsigned char status;
2978 struct stat sb;
2979 char *ondisk_path;
2981 /* Reject merges into a work tree with mixed base commits. */
2982 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2983 SHA1_DIGEST_LENGTH))
2984 return got_error(GOT_ERR_MIXED_COMMITS);
2986 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2987 == -1)
2988 return got_error_from_errno("asprintf");
2990 /* Reject merges into a work tree with conflicted files. */
2991 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2992 if (err)
2993 return err;
2994 if (status == GOT_STATUS_CONFLICT)
2995 return got_error(GOT_ERR_CONFLICTS);
2997 return NULL;
3000 static const struct got_error *
3001 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3002 const char *fileindex_path, struct got_object_id *commit_id1,
3003 struct got_object_id *commit_id2, struct got_repository *repo,
3004 got_worktree_checkout_cb progress_cb, void *progress_arg,
3005 got_cancel_cb cancel_cb, void *cancel_arg)
3007 const struct got_error *err = NULL, *sync_err;
3008 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3009 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3010 struct merge_file_cb_arg arg;
3011 char *label_orig = NULL;
3013 if (commit_id1) {
3014 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3015 worktree->path_prefix);
3016 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3017 goto done;
3019 if (tree_id1) {
3020 char *id_str;
3022 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3023 if (err)
3024 goto done;
3026 err = got_object_id_str(&id_str, commit_id1);
3027 if (err)
3028 goto done;
3030 if (asprintf(&label_orig, "%s: commit %s",
3031 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3032 err = got_error_from_errno("asprintf");
3033 free(id_str);
3034 goto done;
3036 free(id_str);
3039 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3040 worktree->path_prefix);
3041 if (err)
3042 goto done;
3044 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3045 if (err)
3046 goto done;
3048 arg.worktree = worktree;
3049 arg.fileindex = fileindex;
3050 arg.progress_cb = progress_cb;
3051 arg.progress_arg = progress_arg;
3052 arg.cancel_cb = cancel_cb;
3053 arg.cancel_arg = cancel_arg;
3054 arg.label_orig = label_orig;
3055 arg.commit_id2 = commit_id2;
3056 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3057 sync_err = sync_fileindex(fileindex, fileindex_path);
3058 if (sync_err && err == NULL)
3059 err = sync_err;
3060 done:
3061 if (tree1)
3062 got_object_tree_close(tree1);
3063 if (tree2)
3064 got_object_tree_close(tree2);
3065 free(label_orig);
3066 return err;
3069 const struct got_error *
3070 got_worktree_merge_files(struct got_worktree *worktree,
3071 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3072 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3073 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3075 const struct got_error *err, *unlockerr;
3076 char *fileindex_path = NULL;
3077 struct got_fileindex *fileindex = NULL;
3078 struct check_merge_ok_arg mok_arg;
3080 err = lock_worktree(worktree, LOCK_EX);
3081 if (err)
3082 return err;
3084 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3085 if (err)
3086 goto done;
3088 mok_arg.worktree = worktree;
3089 mok_arg.repo = repo;
3090 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3091 &mok_arg);
3092 if (err)
3093 goto done;
3095 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3096 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3097 done:
3098 if (fileindex)
3099 got_fileindex_free(fileindex);
3100 free(fileindex_path);
3101 unlockerr = lock_worktree(worktree, LOCK_SH);
3102 if (unlockerr && err == NULL)
3103 err = unlockerr;
3104 return err;
3107 struct diff_dir_cb_arg {
3108 struct got_fileindex *fileindex;
3109 struct got_worktree *worktree;
3110 const char *status_path;
3111 size_t status_path_len;
3112 struct got_repository *repo;
3113 got_worktree_status_cb status_cb;
3114 void *status_arg;
3115 got_cancel_cb cancel_cb;
3116 void *cancel_arg;
3117 /* A pathlist containing per-directory pathlists of ignore patterns. */
3118 struct got_pathlist_head ignores;
3119 int report_unchanged;
3120 int no_ignores;
3123 static const struct got_error *
3124 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3125 int dirfd, const char *de_name,
3126 got_worktree_status_cb status_cb, void *status_arg,
3127 struct got_repository *repo, int report_unchanged)
3129 const struct got_error *err = NULL;
3130 unsigned char status = GOT_STATUS_NO_CHANGE;
3131 unsigned char staged_status = get_staged_status(ie);
3132 struct stat sb;
3133 struct got_object_id blob_id, commit_id, staged_blob_id;
3134 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3135 struct got_object_id *staged_blob_idp = NULL;
3137 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3138 if (err)
3139 return err;
3141 if (status == GOT_STATUS_NO_CHANGE &&
3142 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3143 return NULL;
3145 if (got_fileindex_entry_has_blob(ie)) {
3146 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3147 blob_idp = &blob_id;
3149 if (got_fileindex_entry_has_commit(ie)) {
3150 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3151 commit_idp = &commit_id;
3153 if (staged_status == GOT_STATUS_ADD ||
3154 staged_status == GOT_STATUS_MODIFY) {
3155 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3156 SHA1_DIGEST_LENGTH);
3157 staged_blob_idp = &staged_blob_id;
3160 return (*status_cb)(status_arg, status, staged_status,
3161 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3164 static const struct got_error *
3165 status_old_new(void *arg, struct got_fileindex_entry *ie,
3166 struct dirent *de, const char *parent_path, int dirfd)
3168 const struct got_error *err = NULL;
3169 struct diff_dir_cb_arg *a = arg;
3170 char *abspath;
3172 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3173 return got_error(GOT_ERR_CANCELLED);
3175 if (got_path_cmp(parent_path, a->status_path,
3176 strlen(parent_path), a->status_path_len) != 0 &&
3177 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3178 return NULL;
3180 if (parent_path[0]) {
3181 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3182 parent_path, de->d_name) == -1)
3183 return got_error_from_errno("asprintf");
3184 } else {
3185 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3186 de->d_name) == -1)
3187 return got_error_from_errno("asprintf");
3190 err = report_file_status(ie, abspath, dirfd, de->d_name,
3191 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3192 free(abspath);
3193 return err;
3196 static const struct got_error *
3197 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3199 struct diff_dir_cb_arg *a = arg;
3200 struct got_object_id blob_id, commit_id;
3201 unsigned char status;
3203 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3204 return got_error(GOT_ERR_CANCELLED);
3206 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3207 return NULL;
3209 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3210 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3211 if (got_fileindex_entry_has_file_on_disk(ie))
3212 status = GOT_STATUS_MISSING;
3213 else
3214 status = GOT_STATUS_DELETE;
3215 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3216 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3219 void
3220 free_ignorelist(struct got_pathlist_head *ignorelist)
3222 struct got_pathlist_entry *pe;
3224 TAILQ_FOREACH(pe, ignorelist, entry)
3225 free((char *)pe->path);
3226 got_pathlist_free(ignorelist);
3229 void
3230 free_ignores(struct got_pathlist_head *ignores)
3232 struct got_pathlist_entry *pe;
3234 TAILQ_FOREACH(pe, ignores, entry) {
3235 struct got_pathlist_head *ignorelist = pe->data;
3236 free_ignorelist(ignorelist);
3237 free((char *)pe->path);
3239 got_pathlist_free(ignores);
3242 static const struct got_error *
3243 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3245 const struct got_error *err = NULL;
3246 struct got_pathlist_entry *pe = NULL;
3247 struct got_pathlist_head *ignorelist;
3248 char *line = NULL, *pattern, *dirpath = NULL;
3249 size_t linesize = 0;
3250 ssize_t linelen;
3252 ignorelist = calloc(1, sizeof(*ignorelist));
3253 if (ignorelist == NULL)
3254 return got_error_from_errno("calloc");
3255 TAILQ_INIT(ignorelist);
3257 while ((linelen = getline(&line, &linesize, f)) != -1) {
3258 if (linelen > 0 && line[linelen - 1] == '\n')
3259 line[linelen - 1] = '\0';
3261 /* Git's ignores may contain comments. */
3262 if (line[0] == '#')
3263 continue;
3265 /* Git's negated patterns are not (yet?) supported. */
3266 if (line[0] == '!')
3267 continue;
3269 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3270 line) == -1) {
3271 err = got_error_from_errno("asprintf");
3272 goto done;
3274 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3275 if (err)
3276 goto done;
3278 if (ferror(f)) {
3279 err = got_error_from_errno("getline");
3280 goto done;
3283 dirpath = strdup(path);
3284 if (dirpath == NULL) {
3285 err = got_error_from_errno("strdup");
3286 goto done;
3288 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3289 done:
3290 free(line);
3291 if (err || pe == NULL) {
3292 free(dirpath);
3293 free_ignorelist(ignorelist);
3295 return err;
3298 int
3299 match_ignores(struct got_pathlist_head *ignores, const char *path)
3301 struct got_pathlist_entry *pe;
3303 /* Handle patterns which match in all directories. */
3304 TAILQ_FOREACH(pe, ignores, entry) {
3305 struct got_pathlist_head *ignorelist = pe->data;
3306 struct got_pathlist_entry *pi;
3308 TAILQ_FOREACH(pi, ignorelist, entry) {
3309 const char *p, *pattern = pi->path;
3311 if (strncmp(pattern, "**/", 3) != 0)
3312 continue;
3313 pattern += 3;
3314 p = path;
3315 while (*p) {
3316 if (fnmatch(pattern, p,
3317 FNM_PATHNAME | FNM_LEADING_DIR)) {
3318 /* Retry in next directory. */
3319 while (*p && *p != '/')
3320 p++;
3321 while (*p == '/')
3322 p++;
3323 continue;
3325 return 1;
3331 * The ignores pathlist contains ignore lists from children before
3332 * parents, so we can find the most specific ignorelist by walking
3333 * ignores backwards.
3335 pe = TAILQ_LAST(ignores, got_pathlist_head);
3336 while (pe) {
3337 if (got_path_is_child(path, pe->path, pe->path_len)) {
3338 struct got_pathlist_head *ignorelist = pe->data;
3339 struct got_pathlist_entry *pi;
3340 TAILQ_FOREACH(pi, ignorelist, entry) {
3341 const char *pattern = pi->path;
3342 int flags = FNM_LEADING_DIR;
3343 if (strstr(pattern, "/**/") == NULL)
3344 flags |= FNM_PATHNAME;
3345 if (fnmatch(pattern, path, flags))
3346 continue;
3347 return 1;
3350 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3353 return 0;
3356 static const struct got_error *
3357 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3358 const char *path, int dirfd, const char *ignores_filename)
3360 const struct got_error *err = NULL;
3361 char *ignorespath;
3362 int fd = -1;
3363 FILE *ignoresfile = NULL;
3365 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3366 path[0] ? "/" : "", ignores_filename) == -1)
3367 return got_error_from_errno("asprintf");
3369 if (dirfd != -1) {
3370 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3371 if (fd == -1) {
3372 if (errno != ENOENT && errno != EACCES)
3373 err = got_error_from_errno2("openat",
3374 ignorespath);
3375 } else {
3376 ignoresfile = fdopen(fd, "r");
3377 if (ignoresfile == NULL)
3378 err = got_error_from_errno2("fdopen",
3379 ignorespath);
3380 else {
3381 fd = -1;
3382 err = read_ignores(ignores, path, ignoresfile);
3385 } else {
3386 ignoresfile = fopen(ignorespath, "r");
3387 if (ignoresfile == NULL) {
3388 if (errno != ENOENT && errno != EACCES)
3389 err = got_error_from_errno2("fopen",
3390 ignorespath);
3391 } else
3392 err = read_ignores(ignores, path, ignoresfile);
3395 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3396 err = got_error_from_errno2("fclose", path);
3397 if (fd != -1 && close(fd) == -1 && err == NULL)
3398 err = got_error_from_errno2("close", path);
3399 free(ignorespath);
3400 return err;
3403 static const struct got_error *
3404 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3406 const struct got_error *err = NULL;
3407 struct diff_dir_cb_arg *a = arg;
3408 char *path = NULL;
3410 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3411 return got_error(GOT_ERR_CANCELLED);
3413 if (parent_path[0]) {
3414 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3415 return got_error_from_errno("asprintf");
3416 } else {
3417 path = de->d_name;
3420 if (de->d_type != DT_DIR &&
3421 got_path_is_child(path, a->status_path, a->status_path_len)
3422 && !match_ignores(&a->ignores, path))
3423 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3424 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3425 if (parent_path[0])
3426 free(path);
3427 return err;
3430 static const struct got_error *
3431 status_traverse(void *arg, const char *path, int dirfd)
3433 const struct got_error *err = NULL;
3434 struct diff_dir_cb_arg *a = arg;
3436 if (a->no_ignores)
3437 return NULL;
3439 err = add_ignores(&a->ignores, a->worktree->root_path,
3440 path, dirfd, ".cvsignore");
3441 if (err)
3442 return err;
3444 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3445 dirfd, ".gitignore");
3447 return err;
3450 static const struct got_error *
3451 report_single_file_status(const char *path, const char *ondisk_path,
3452 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3453 void *status_arg, struct got_repository *repo, int report_unchanged)
3455 struct got_fileindex_entry *ie;
3456 struct stat sb;
3458 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3459 if (ie)
3460 return report_file_status(ie, ondisk_path, -1, NULL,
3461 status_cb, status_arg, repo, report_unchanged);
3463 if (lstat(ondisk_path, &sb) == -1) {
3464 if (errno != ENOENT)
3465 return got_error_from_errno2("lstat", ondisk_path);
3466 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3467 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3468 return NULL;
3471 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3472 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3473 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3475 return NULL;
3478 static const struct got_error *
3479 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3480 const char *root_path, const char *path)
3482 const struct got_error *err;
3483 char *parent_path, *next_parent_path = NULL;
3485 err = add_ignores(ignores, root_path, "", -1,
3486 ".cvsignore");
3487 if (err)
3488 return err;
3490 err = add_ignores(ignores, root_path, "", -1,
3491 ".gitignore");
3492 if (err)
3493 return err;
3495 err = got_path_dirname(&parent_path, path);
3496 if (err) {
3497 if (err->code == GOT_ERR_BAD_PATH)
3498 return NULL; /* cannot traverse parent */
3499 return err;
3501 for (;;) {
3502 err = add_ignores(ignores, root_path, parent_path, -1,
3503 ".cvsignore");
3504 if (err)
3505 break;
3506 err = add_ignores(ignores, root_path, parent_path, -1,
3507 ".gitignore");
3508 if (err)
3509 break;
3510 err = got_path_dirname(&next_parent_path, parent_path);
3511 if (err) {
3512 if (err->code == GOT_ERR_BAD_PATH)
3513 err = NULL; /* traversed everything */
3514 break;
3516 free(parent_path);
3517 parent_path = next_parent_path;
3518 next_parent_path = NULL;
3521 free(parent_path);
3522 free(next_parent_path);
3523 return err;
3526 static const struct got_error *
3527 worktree_status(struct got_worktree *worktree, const char *path,
3528 struct got_fileindex *fileindex, struct got_repository *repo,
3529 got_worktree_status_cb status_cb, void *status_arg,
3530 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3531 int report_unchanged)
3533 const struct got_error *err = NULL;
3534 int fd = -1;
3535 struct got_fileindex_diff_dir_cb fdiff_cb;
3536 struct diff_dir_cb_arg arg;
3537 char *ondisk_path = NULL;
3539 TAILQ_INIT(&arg.ignores);
3541 if (asprintf(&ondisk_path, "%s%s%s",
3542 worktree->root_path, path[0] ? "/" : "", path) == -1)
3543 return got_error_from_errno("asprintf");
3545 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3546 if (fd == -1) {
3547 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3548 errno != ELOOP)
3549 err = got_error_from_errno2("open", ondisk_path);
3550 else
3551 err = report_single_file_status(path, ondisk_path,
3552 fileindex, status_cb, status_arg, repo,
3553 report_unchanged);
3554 } else {
3555 fdiff_cb.diff_old_new = status_old_new;
3556 fdiff_cb.diff_old = status_old;
3557 fdiff_cb.diff_new = status_new;
3558 fdiff_cb.diff_traverse = status_traverse;
3559 arg.fileindex = fileindex;
3560 arg.worktree = worktree;
3561 arg.status_path = path;
3562 arg.status_path_len = strlen(path);
3563 arg.repo = repo;
3564 arg.status_cb = status_cb;
3565 arg.status_arg = status_arg;
3566 arg.cancel_cb = cancel_cb;
3567 arg.cancel_arg = cancel_arg;
3568 arg.report_unchanged = report_unchanged;
3569 arg.no_ignores = no_ignores;
3570 if (!no_ignores) {
3571 err = add_ignores_from_parent_paths(&arg.ignores,
3572 worktree->root_path, path);
3573 if (err)
3574 goto done;
3576 err = got_fileindex_diff_dir(fileindex, fd,
3577 worktree->root_path, path, repo, &fdiff_cb, &arg);
3579 done:
3580 free_ignores(&arg.ignores);
3581 if (fd != -1 && close(fd) != 0 && err == NULL)
3582 err = got_error_from_errno("close");
3583 free(ondisk_path);
3584 return err;
3587 const struct got_error *
3588 got_worktree_status(struct got_worktree *worktree,
3589 struct got_pathlist_head *paths, struct got_repository *repo,
3590 got_worktree_status_cb status_cb, void *status_arg,
3591 got_cancel_cb cancel_cb, void *cancel_arg)
3593 const struct got_error *err = NULL;
3594 char *fileindex_path = NULL;
3595 struct got_fileindex *fileindex = NULL;
3596 struct got_pathlist_entry *pe;
3598 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3599 if (err)
3600 return err;
3602 TAILQ_FOREACH(pe, paths, entry) {
3603 err = worktree_status(worktree, pe->path, fileindex, repo,
3604 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3605 if (err)
3606 break;
3608 free(fileindex_path);
3609 got_fileindex_free(fileindex);
3610 return err;
3613 const struct got_error *
3614 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3615 const char *arg)
3617 const struct got_error *err = NULL;
3618 char *resolved = NULL, *cwd = NULL, *path = NULL;
3619 size_t len;
3620 struct stat sb;
3622 *wt_path = NULL;
3624 cwd = getcwd(NULL, 0);
3625 if (cwd == NULL)
3626 return got_error_from_errno("getcwd");
3628 if (lstat(arg, &sb) == -1) {
3629 if (errno != ENOENT) {
3630 err = got_error_from_errno2("lstat", arg);
3631 goto done;
3634 if (S_ISLNK(sb.st_mode)) {
3636 * We cannot use realpath(3) with symlinks since we want to
3637 * operate on the symlink itself.
3638 * But we can make the path absolute, assuming it is relative
3639 * to the current working directory, and then canonicalize it.
3641 char *abspath = NULL;
3642 char canonpath[PATH_MAX];
3643 if (!got_path_is_absolute(arg)) {
3644 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3645 err = got_error_from_errno("asprintf");
3646 goto done;
3650 err = got_canonpath(abspath ? abspath : arg, canonpath,
3651 sizeof(canonpath));
3652 if (err)
3653 goto done;
3654 resolved = strdup(canonpath);
3655 if (resolved == NULL) {
3656 err = got_error_from_errno("strdup");
3657 goto done;
3659 } else {
3660 resolved = realpath(arg, NULL);
3661 if (resolved == NULL) {
3662 if (errno != ENOENT) {
3663 err = got_error_from_errno2("realpath", arg);
3664 goto done;
3666 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3667 err = got_error_from_errno("asprintf");
3668 goto done;
3673 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3674 strlen(got_worktree_get_root_path(worktree)))) {
3675 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3676 goto done;
3679 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3680 err = got_path_skip_common_ancestor(&path,
3681 got_worktree_get_root_path(worktree), resolved);
3682 if (err)
3683 goto done;
3684 } else {
3685 path = strdup("");
3686 if (path == NULL) {
3687 err = got_error_from_errno("strdup");
3688 goto done;
3692 /* XXX status walk can't deal with trailing slash! */
3693 len = strlen(path);
3694 while (len > 0 && path[len - 1] == '/') {
3695 path[len - 1] = '\0';
3696 len--;
3698 done:
3699 free(resolved);
3700 free(cwd);
3701 if (err == NULL)
3702 *wt_path = path;
3703 else
3704 free(path);
3705 return err;
3708 struct schedule_addition_args {
3709 struct got_worktree *worktree;
3710 struct got_fileindex *fileindex;
3711 got_worktree_checkout_cb progress_cb;
3712 void *progress_arg;
3713 struct got_repository *repo;
3716 static const struct got_error *
3717 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3718 const char *relpath, struct got_object_id *blob_id,
3719 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3720 int dirfd, const char *de_name)
3722 struct schedule_addition_args *a = arg;
3723 const struct got_error *err = NULL;
3724 struct got_fileindex_entry *ie;
3725 struct stat sb;
3726 char *ondisk_path;
3728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3729 relpath) == -1)
3730 return got_error_from_errno("asprintf");
3732 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3733 if (ie) {
3734 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3735 de_name, a->repo);
3736 if (err)
3737 goto done;
3738 /* Re-adding an existing entry is a no-op. */
3739 if (status == GOT_STATUS_ADD)
3740 goto done;
3741 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3742 if (err)
3743 goto done;
3746 if (status != GOT_STATUS_UNVERSIONED) {
3747 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3748 goto done;
3751 err = got_fileindex_entry_alloc(&ie, relpath);
3752 if (err)
3753 goto done;
3754 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3755 if (err) {
3756 got_fileindex_entry_free(ie);
3757 goto done;
3759 err = got_fileindex_entry_add(a->fileindex, ie);
3760 if (err) {
3761 got_fileindex_entry_free(ie);
3762 goto done;
3764 done:
3765 free(ondisk_path);
3766 if (err)
3767 return err;
3768 if (status == GOT_STATUS_ADD)
3769 return NULL;
3770 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3773 const struct got_error *
3774 got_worktree_schedule_add(struct got_worktree *worktree,
3775 struct got_pathlist_head *paths,
3776 got_worktree_checkout_cb progress_cb, void *progress_arg,
3777 struct got_repository *repo, int no_ignores)
3779 struct got_fileindex *fileindex = NULL;
3780 char *fileindex_path = NULL;
3781 const struct got_error *err = NULL, *sync_err, *unlockerr;
3782 struct got_pathlist_entry *pe;
3783 struct schedule_addition_args saa;
3785 err = lock_worktree(worktree, LOCK_EX);
3786 if (err)
3787 return err;
3789 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3790 if (err)
3791 goto done;
3793 saa.worktree = worktree;
3794 saa.fileindex = fileindex;
3795 saa.progress_cb = progress_cb;
3796 saa.progress_arg = progress_arg;
3797 saa.repo = repo;
3799 TAILQ_FOREACH(pe, paths, entry) {
3800 err = worktree_status(worktree, pe->path, fileindex, repo,
3801 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3802 if (err)
3803 break;
3805 sync_err = sync_fileindex(fileindex, fileindex_path);
3806 if (sync_err && err == NULL)
3807 err = sync_err;
3808 done:
3809 free(fileindex_path);
3810 if (fileindex)
3811 got_fileindex_free(fileindex);
3812 unlockerr = lock_worktree(worktree, LOCK_SH);
3813 if (unlockerr && err == NULL)
3814 err = unlockerr;
3815 return err;
3818 struct schedule_deletion_args {
3819 struct got_worktree *worktree;
3820 struct got_fileindex *fileindex;
3821 got_worktree_delete_cb progress_cb;
3822 void *progress_arg;
3823 struct got_repository *repo;
3824 int delete_local_mods;
3825 int keep_on_disk;
3826 const char *status_codes;
3829 static const struct got_error *
3830 schedule_for_deletion(void *arg, unsigned char status,
3831 unsigned char staged_status, const char *relpath,
3832 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3833 struct got_object_id *commit_id, int dirfd, const char *de_name)
3835 struct schedule_deletion_args *a = arg;
3836 const struct got_error *err = NULL;
3837 struct got_fileindex_entry *ie = NULL;
3838 struct stat sb;
3839 char *ondisk_path, *parent = NULL;
3841 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3842 if (ie == NULL)
3843 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3845 staged_status = get_staged_status(ie);
3846 if (staged_status != GOT_STATUS_NO_CHANGE) {
3847 if (staged_status == GOT_STATUS_DELETE)
3848 return NULL;
3849 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3853 relpath) == -1)
3854 return got_error_from_errno("asprintf");
3856 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3857 a->repo);
3858 if (err)
3859 goto done;
3861 if (a->status_codes) {
3862 size_t ncodes = strlen(a->status_codes);
3863 int i;
3864 for (i = 0; i < ncodes ; i++) {
3865 if (status == a->status_codes[i])
3866 break;
3868 if (i == ncodes) {
3869 /* Do not delete files in non-matching status. */
3870 free(ondisk_path);
3871 return NULL;
3873 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3874 a->status_codes[i] != GOT_STATUS_MISSING) {
3875 static char msg[64];
3876 snprintf(msg, sizeof(msg),
3877 "invalid status code '%c'", a->status_codes[i]);
3878 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3879 goto done;
3883 if (status != GOT_STATUS_NO_CHANGE) {
3884 if (status == GOT_STATUS_DELETE)
3885 goto done;
3886 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3887 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3888 goto done;
3890 if (status != GOT_STATUS_MODIFY &&
3891 status != GOT_STATUS_MISSING) {
3892 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3893 goto done;
3897 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3898 if (dirfd != -1) {
3899 if (unlinkat(dirfd, de_name, 0) != 0) {
3900 err = got_error_from_errno2("unlinkat",
3901 ondisk_path);
3902 goto done;
3904 } else if (unlink(ondisk_path) != 0) {
3905 err = got_error_from_errno2("unlink", ondisk_path);
3906 goto done;
3909 parent = dirname(ondisk_path);
3911 if (parent == NULL) {
3912 err = got_error_from_errno2("dirname", ondisk_path);
3913 goto done;
3915 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3916 if (rmdir(parent) == -1) {
3917 if (errno != ENOTEMPTY)
3918 err = got_error_from_errno2("rmdir",
3919 parent);
3920 break;
3922 parent = dirname(parent);
3923 if (parent == NULL) {
3924 err = got_error_from_errno2("dirname", parent);
3925 goto done;
3930 got_fileindex_entry_mark_deleted_from_disk(ie);
3931 done:
3932 free(ondisk_path);
3933 if (err)
3934 return err;
3935 if (status == GOT_STATUS_DELETE)
3936 return NULL;
3937 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3938 staged_status, relpath);
3941 const struct got_error *
3942 got_worktree_schedule_delete(struct got_worktree *worktree,
3943 struct got_pathlist_head *paths, int delete_local_mods,
3944 const char *status_codes,
3945 got_worktree_delete_cb progress_cb, void *progress_arg,
3946 struct got_repository *repo, int keep_on_disk)
3948 struct got_fileindex *fileindex = NULL;
3949 char *fileindex_path = NULL;
3950 const struct got_error *err = NULL, *sync_err, *unlockerr;
3951 struct got_pathlist_entry *pe;
3952 struct schedule_deletion_args sda;
3954 err = lock_worktree(worktree, LOCK_EX);
3955 if (err)
3956 return err;
3958 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3959 if (err)
3960 goto done;
3962 sda.worktree = worktree;
3963 sda.fileindex = fileindex;
3964 sda.progress_cb = progress_cb;
3965 sda.progress_arg = progress_arg;
3966 sda.repo = repo;
3967 sda.delete_local_mods = delete_local_mods;
3968 sda.keep_on_disk = keep_on_disk;
3969 sda.status_codes = status_codes;
3971 TAILQ_FOREACH(pe, paths, entry) {
3972 err = worktree_status(worktree, pe->path, fileindex, repo,
3973 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3974 if (err)
3975 break;
3977 sync_err = sync_fileindex(fileindex, fileindex_path);
3978 if (sync_err && err == NULL)
3979 err = sync_err;
3980 done:
3981 free(fileindex_path);
3982 if (fileindex)
3983 got_fileindex_free(fileindex);
3984 unlockerr = lock_worktree(worktree, LOCK_SH);
3985 if (unlockerr && err == NULL)
3986 err = unlockerr;
3987 return err;
3990 static const struct got_error *
3991 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3993 const struct got_error *err = NULL;
3994 char *line = NULL;
3995 size_t linesize = 0, n;
3996 ssize_t linelen;
3998 linelen = getline(&line, &linesize, infile);
3999 if (linelen == -1) {
4000 if (ferror(infile)) {
4001 err = got_error_from_errno("getline");
4002 goto done;
4004 return NULL;
4006 if (outfile) {
4007 n = fwrite(line, 1, linelen, outfile);
4008 if (n != linelen) {
4009 err = got_ferror(outfile, GOT_ERR_IO);
4010 goto done;
4013 if (rejectfile) {
4014 n = fwrite(line, 1, linelen, rejectfile);
4015 if (n != linelen)
4016 err = got_ferror(outfile, GOT_ERR_IO);
4018 done:
4019 free(line);
4020 return err;
4023 static const struct got_error *
4024 skip_one_line(FILE *f)
4026 char *line = NULL;
4027 size_t linesize = 0;
4028 ssize_t linelen;
4030 linelen = getline(&line, &linesize, f);
4031 if (linelen == -1) {
4032 if (ferror(f))
4033 return got_error_from_errno("getline");
4034 return NULL;
4036 free(line);
4037 return NULL;
4040 static const struct got_error *
4041 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4042 int start_old, int end_old, int start_new, int end_new,
4043 FILE *outfile, FILE *rejectfile)
4045 const struct got_error *err;
4047 /* Copy old file's lines leading up to patch. */
4048 while (!feof(f1) && *line_cur1 < start_old) {
4049 err = copy_one_line(f1, outfile, NULL);
4050 if (err)
4051 return err;
4052 (*line_cur1)++;
4054 /* Skip new file's lines leading up to patch. */
4055 while (!feof(f2) && *line_cur2 < start_new) {
4056 if (rejectfile)
4057 err = copy_one_line(f2, NULL, rejectfile);
4058 else
4059 err = skip_one_line(f2);
4060 if (err)
4061 return err;
4062 (*line_cur2)++;
4064 /* Copy patched lines. */
4065 while (!feof(f2) && *line_cur2 <= end_new) {
4066 err = copy_one_line(f2, outfile, NULL);
4067 if (err)
4068 return err;
4069 (*line_cur2)++;
4071 /* Skip over old file's replaced lines. */
4072 while (!feof(f1) && *line_cur1 <= end_old) {
4073 if (rejectfile)
4074 err = copy_one_line(f1, NULL, rejectfile);
4075 else
4076 err = skip_one_line(f1);
4077 if (err)
4078 return err;
4079 (*line_cur1)++;
4082 return NULL;
4085 static const struct got_error *
4086 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4087 FILE *outfile, FILE *rejectfile)
4089 const struct got_error *err;
4091 if (outfile) {
4092 /* Copy old file's lines until EOF. */
4093 while (!feof(f1)) {
4094 err = copy_one_line(f1, outfile, NULL);
4095 if (err)
4096 return err;
4097 (*line_cur1)++;
4100 if (rejectfile) {
4101 /* Copy new file's lines until EOF. */
4102 while (!feof(f2)) {
4103 err = copy_one_line(f2, NULL, rejectfile);
4104 if (err)
4105 return err;
4106 (*line_cur2)++;
4110 return NULL;
4113 static const struct got_error *
4114 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4115 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4116 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4117 int *line_cur2, FILE *outfile, FILE *rejectfile,
4118 got_worktree_patch_cb patch_cb, void *patch_arg)
4120 const struct got_error *err = NULL;
4121 int start_old = change->cv.a;
4122 int end_old = change->cv.b;
4123 int start_new = change->cv.c;
4124 int end_new = change->cv.d;
4125 long pos1, pos2;
4126 FILE *hunkfile;
4128 *choice = GOT_PATCH_CHOICE_NONE;
4130 hunkfile = got_opentemp();
4131 if (hunkfile == NULL)
4132 return got_error_from_errno("got_opentemp");
4134 pos1 = ftell(f1);
4135 pos2 = ftell(f2);
4137 /* XXX TODO needs error checking */
4138 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4140 if (fseek(f1, pos1, SEEK_SET) == -1) {
4141 err = got_ferror(f1, GOT_ERR_IO);
4142 goto done;
4144 if (fseek(f2, pos2, SEEK_SET) == -1) {
4145 err = got_ferror(f1, GOT_ERR_IO);
4146 goto done;
4148 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4149 err = got_ferror(hunkfile, GOT_ERR_IO);
4150 goto done;
4153 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4154 hunkfile, n, nchanges);
4155 if (err)
4156 goto done;
4158 switch (*choice) {
4159 case GOT_PATCH_CHOICE_YES:
4160 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4161 end_old, start_new, end_new, outfile, rejectfile);
4162 break;
4163 case GOT_PATCH_CHOICE_NO:
4164 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4165 end_old, start_new, end_new, rejectfile, outfile);
4166 break;
4167 case GOT_PATCH_CHOICE_QUIT:
4168 break;
4169 default:
4170 err = got_error(GOT_ERR_PATCH_CHOICE);
4171 break;
4173 done:
4174 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4175 err = got_error_from_errno("fclose");
4176 return err;
4179 struct revert_file_args {
4180 struct got_worktree *worktree;
4181 struct got_fileindex *fileindex;
4182 got_worktree_checkout_cb progress_cb;
4183 void *progress_arg;
4184 got_worktree_patch_cb patch_cb;
4185 void *patch_arg;
4186 struct got_repository *repo;
4189 static const struct got_error *
4190 create_patched_content(char **path_outfile, int reverse_patch,
4191 struct got_object_id *blob_id, const char *path2,
4192 int dirfd2, const char *de_name2,
4193 const char *relpath, struct got_repository *repo,
4194 got_worktree_patch_cb patch_cb, void *patch_arg)
4196 const struct got_error *err;
4197 struct got_blob_object *blob = NULL;
4198 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4199 int fd2 = -1;
4200 char link_target[PATH_MAX];
4201 ssize_t link_len = 0;
4202 char *path1 = NULL, *id_str = NULL;
4203 struct stat sb1, sb2;
4204 struct got_diff_changes *changes = NULL;
4205 struct got_diff_state *ds = NULL;
4206 struct got_diff_args *args = NULL;
4207 struct got_diff_change *change;
4208 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4209 int n = 0;
4211 *path_outfile = NULL;
4213 err = got_object_id_str(&id_str, blob_id);
4214 if (err)
4215 return err;
4217 if (dirfd2 != -1) {
4218 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4219 if (fd2 == -1) {
4220 if (errno != ELOOP) {
4221 err = got_error_from_errno2("openat", path2);
4222 goto done;
4224 link_len = readlinkat(dirfd2, de_name2,
4225 link_target, sizeof(link_target));
4226 if (link_len == -1)
4227 return got_error_from_errno2("readlinkat", path2);
4228 sb2.st_mode = S_IFLNK;
4229 sb2.st_size = link_len;
4231 } else {
4232 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4233 if (fd2 == -1) {
4234 if (errno != ELOOP) {
4235 err = got_error_from_errno2("open", path2);
4236 goto done;
4238 link_len = readlink(path2, link_target,
4239 sizeof(link_target));
4240 if (link_len == -1)
4241 return got_error_from_errno2("readlink", path2);
4242 sb2.st_mode = S_IFLNK;
4243 sb2.st_size = link_len;
4246 if (fd2 != -1) {
4247 if (fstat(fd2, &sb2) == -1) {
4248 err = got_error_from_errno2("fstat", path2);
4249 goto done;
4252 f2 = fdopen(fd2, "r");
4253 if (f2 == NULL) {
4254 err = got_error_from_errno2("fdopen", path2);
4255 goto done;
4257 fd2 = -1;
4258 } else {
4259 size_t n;
4260 f2 = got_opentemp();
4261 if (f2 == NULL) {
4262 err = got_error_from_errno2("got_opentemp", path2);
4263 goto done;
4265 n = fwrite(link_target, 1, link_len, f2);
4266 if (n != link_len) {
4267 err = got_ferror(f2, GOT_ERR_IO);
4268 goto done;
4270 if (fflush(f2) == EOF) {
4271 err = got_error_from_errno("fflush");
4272 goto done;
4274 rewind(f2);
4277 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4278 if (err)
4279 goto done;
4281 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4282 if (err)
4283 goto done;
4285 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4286 if (err)
4287 goto done;
4289 if (stat(path1, &sb1) == -1) {
4290 err = got_error_from_errno2("stat", path1);
4291 goto done;
4294 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4295 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4296 if (err)
4297 goto done;
4299 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4300 if (err)
4301 goto done;
4303 if (fseek(f1, 0L, SEEK_SET) == -1)
4304 return got_ferror(f1, GOT_ERR_IO);
4305 if (fseek(f2, 0L, SEEK_SET) == -1)
4306 return got_ferror(f2, GOT_ERR_IO);
4307 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4308 int choice;
4309 err = apply_or_reject_change(&choice, change, ++n,
4310 changes->nchanges, ds, args, diff_flags, relpath,
4311 f1, f2, &line_cur1, &line_cur2,
4312 reverse_patch ? NULL : outfile,
4313 reverse_patch ? outfile : NULL,
4314 patch_cb, patch_arg);
4315 if (err)
4316 goto done;
4317 if (choice == GOT_PATCH_CHOICE_YES)
4318 have_content = 1;
4319 else if (choice == GOT_PATCH_CHOICE_QUIT)
4320 break;
4322 if (have_content) {
4323 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4324 reverse_patch ? NULL : outfile,
4325 reverse_patch ? outfile : NULL);
4326 if (err)
4327 goto done;
4329 if (!S_ISLNK(sb2.st_mode)) {
4330 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4331 err = got_error_from_errno2("chmod", path2);
4332 goto done;
4336 done:
4337 free(id_str);
4338 if (blob)
4339 got_object_blob_close(blob);
4340 if (f1 && fclose(f1) == EOF && err == NULL)
4341 err = got_error_from_errno2("fclose", path1);
4342 if (f2 && fclose(f2) == EOF && err == NULL)
4343 err = got_error_from_errno2("fclose", path2);
4344 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4345 err = got_error_from_errno2("close", path2);
4346 if (outfile && fclose(outfile) == EOF && err == NULL)
4347 err = got_error_from_errno2("fclose", *path_outfile);
4348 if (path1 && unlink(path1) == -1 && err == NULL)
4349 err = got_error_from_errno2("unlink", path1);
4350 if (err || !have_content) {
4351 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4352 err = got_error_from_errno2("unlink", *path_outfile);
4353 free(*path_outfile);
4354 *path_outfile = NULL;
4356 free(args);
4357 if (ds) {
4358 got_diff_state_free(ds);
4359 free(ds);
4361 if (changes)
4362 got_diff_free_changes(changes);
4363 free(path1);
4364 return err;
4367 static const struct got_error *
4368 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4369 const char *relpath, struct got_object_id *blob_id,
4370 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4371 int dirfd, const char *de_name)
4373 struct revert_file_args *a = arg;
4374 const struct got_error *err = NULL;
4375 char *parent_path = NULL;
4376 struct got_fileindex_entry *ie;
4377 struct got_tree_object *tree = NULL;
4378 struct got_object_id *tree_id = NULL;
4379 const struct got_tree_entry *te = NULL;
4380 char *tree_path = NULL, *te_name;
4381 char *ondisk_path = NULL, *path_content = NULL;
4382 struct got_blob_object *blob = NULL;
4384 /* Reverting a staged deletion is a no-op. */
4385 if (status == GOT_STATUS_DELETE &&
4386 staged_status != GOT_STATUS_NO_CHANGE)
4387 return NULL;
4389 if (status == GOT_STATUS_UNVERSIONED)
4390 return (*a->progress_cb)(a->progress_arg,
4391 GOT_STATUS_UNVERSIONED, relpath);
4393 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4394 if (ie == NULL)
4395 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4397 /* Construct in-repository path of tree which contains this blob. */
4398 err = got_path_dirname(&parent_path, ie->path);
4399 if (err) {
4400 if (err->code != GOT_ERR_BAD_PATH)
4401 goto done;
4402 parent_path = strdup("/");
4403 if (parent_path == NULL) {
4404 err = got_error_from_errno("strdup");
4405 goto done;
4408 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4409 tree_path = strdup(parent_path);
4410 if (tree_path == NULL) {
4411 err = got_error_from_errno("strdup");
4412 goto done;
4414 } else {
4415 if (got_path_is_root_dir(parent_path)) {
4416 tree_path = strdup(a->worktree->path_prefix);
4417 if (tree_path == NULL) {
4418 err = got_error_from_errno("strdup");
4419 goto done;
4421 } else {
4422 if (asprintf(&tree_path, "%s/%s",
4423 a->worktree->path_prefix, parent_path) == -1) {
4424 err = got_error_from_errno("asprintf");
4425 goto done;
4430 err = got_object_id_by_path(&tree_id, a->repo,
4431 a->worktree->base_commit_id, tree_path);
4432 if (err) {
4433 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4434 (status == GOT_STATUS_ADD ||
4435 staged_status == GOT_STATUS_ADD)))
4436 goto done;
4437 } else {
4438 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4439 if (err)
4440 goto done;
4442 err = got_path_basename(&te_name, ie->path);
4443 if (err)
4444 goto done;
4446 te = got_object_tree_find_entry(tree, te_name);
4447 free(te_name);
4448 if (te == NULL && status != GOT_STATUS_ADD &&
4449 staged_status != GOT_STATUS_ADD) {
4450 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4451 goto done;
4455 switch (status) {
4456 case GOT_STATUS_ADD:
4457 if (a->patch_cb) {
4458 int choice = GOT_PATCH_CHOICE_NONE;
4459 err = (*a->patch_cb)(&choice, a->patch_arg,
4460 status, ie->path, NULL, 1, 1);
4461 if (err)
4462 goto done;
4463 if (choice != GOT_PATCH_CHOICE_YES)
4464 break;
4466 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4467 ie->path);
4468 if (err)
4469 goto done;
4470 got_fileindex_entry_remove(a->fileindex, ie);
4471 break;
4472 case GOT_STATUS_DELETE:
4473 if (a->patch_cb) {
4474 int choice = GOT_PATCH_CHOICE_NONE;
4475 err = (*a->patch_cb)(&choice, a->patch_arg,
4476 status, ie->path, NULL, 1, 1);
4477 if (err)
4478 goto done;
4479 if (choice != GOT_PATCH_CHOICE_YES)
4480 break;
4482 /* fall through */
4483 case GOT_STATUS_MODIFY:
4484 case GOT_STATUS_MODE_CHANGE:
4485 case GOT_STATUS_CONFLICT:
4486 case GOT_STATUS_MISSING: {
4487 struct got_object_id id;
4488 if (staged_status == GOT_STATUS_ADD ||
4489 staged_status == GOT_STATUS_MODIFY) {
4490 memcpy(id.sha1, ie->staged_blob_sha1,
4491 SHA1_DIGEST_LENGTH);
4492 } else
4493 memcpy(id.sha1, ie->blob_sha1,
4494 SHA1_DIGEST_LENGTH);
4495 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4496 if (err)
4497 goto done;
4499 if (asprintf(&ondisk_path, "%s/%s",
4500 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4501 err = got_error_from_errno("asprintf");
4502 goto done;
4505 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4506 status == GOT_STATUS_CONFLICT)) {
4507 int is_bad_symlink = 0;
4508 err = create_patched_content(&path_content, 1, &id,
4509 ondisk_path, dirfd, de_name, ie->path, a->repo,
4510 a->patch_cb, a->patch_arg);
4511 if (err || path_content == NULL)
4512 break;
4513 if (te && S_ISLNK(te->mode)) {
4514 if (unlink(path_content) == -1) {
4515 err = got_error_from_errno2("unlink",
4516 path_content);
4517 break;
4519 err = install_symlink(&is_bad_symlink,
4520 a->worktree, ondisk_path, ie->path,
4521 blob, 0, 1, 0, a->repo,
4522 a->progress_cb, a->progress_arg);
4523 } else {
4524 if (rename(path_content, ondisk_path) == -1) {
4525 err = got_error_from_errno3("rename",
4526 path_content, ondisk_path);
4527 goto done;
4530 } else {
4531 int is_bad_symlink = 0;
4532 if (te && S_ISLNK(te->mode)) {
4533 err = install_symlink(&is_bad_symlink,
4534 a->worktree, ondisk_path, ie->path,
4535 blob, 0, 1, 0, a->repo,
4536 a->progress_cb, a->progress_arg);
4537 } else {
4538 err = install_blob(a->worktree, ondisk_path,
4539 ie->path,
4540 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4541 got_fileindex_perms_to_st(ie), blob,
4542 0, 1, 0, 0, a->repo,
4543 a->progress_cb, a->progress_arg);
4545 if (err)
4546 goto done;
4547 if (status == GOT_STATUS_DELETE ||
4548 status == GOT_STATUS_MODE_CHANGE) {
4549 err = got_fileindex_entry_update(ie,
4550 ondisk_path, blob->id.sha1,
4551 a->worktree->base_commit_id->sha1, 1);
4552 if (err)
4553 goto done;
4555 if (is_bad_symlink) {
4556 got_fileindex_entry_filetype_set(ie,
4557 GOT_FILEIDX_MODE_BAD_SYMLINK);
4560 break;
4562 default:
4563 break;
4565 done:
4566 free(ondisk_path);
4567 free(path_content);
4568 free(parent_path);
4569 free(tree_path);
4570 if (blob)
4571 got_object_blob_close(blob);
4572 if (tree)
4573 got_object_tree_close(tree);
4574 free(tree_id);
4575 return err;
4578 const struct got_error *
4579 got_worktree_revert(struct got_worktree *worktree,
4580 struct got_pathlist_head *paths,
4581 got_worktree_checkout_cb progress_cb, void *progress_arg,
4582 got_worktree_patch_cb patch_cb, void *patch_arg,
4583 struct got_repository *repo)
4585 struct got_fileindex *fileindex = NULL;
4586 char *fileindex_path = NULL;
4587 const struct got_error *err = NULL, *unlockerr = NULL;
4588 const struct got_error *sync_err = NULL;
4589 struct got_pathlist_entry *pe;
4590 struct revert_file_args rfa;
4592 err = lock_worktree(worktree, LOCK_EX);
4593 if (err)
4594 return err;
4596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4597 if (err)
4598 goto done;
4600 rfa.worktree = worktree;
4601 rfa.fileindex = fileindex;
4602 rfa.progress_cb = progress_cb;
4603 rfa.progress_arg = progress_arg;
4604 rfa.patch_cb = patch_cb;
4605 rfa.patch_arg = patch_arg;
4606 rfa.repo = repo;
4607 TAILQ_FOREACH(pe, paths, entry) {
4608 err = worktree_status(worktree, pe->path, fileindex, repo,
4609 revert_file, &rfa, NULL, NULL, 0, 0);
4610 if (err)
4611 break;
4613 sync_err = sync_fileindex(fileindex, fileindex_path);
4614 if (sync_err && err == NULL)
4615 err = sync_err;
4616 done:
4617 free(fileindex_path);
4618 if (fileindex)
4619 got_fileindex_free(fileindex);
4620 unlockerr = lock_worktree(worktree, LOCK_SH);
4621 if (unlockerr && err == NULL)
4622 err = unlockerr;
4623 return err;
4626 static void
4627 free_commitable(struct got_commitable *ct)
4629 free(ct->path);
4630 free(ct->in_repo_path);
4631 free(ct->ondisk_path);
4632 free(ct->blob_id);
4633 free(ct->base_blob_id);
4634 free(ct->staged_blob_id);
4635 free(ct->base_commit_id);
4636 free(ct);
4639 struct collect_commitables_arg {
4640 struct got_pathlist_head *commitable_paths;
4641 struct got_repository *repo;
4642 struct got_worktree *worktree;
4643 struct got_fileindex *fileindex;
4644 int have_staged_files;
4645 int allow_bad_symlinks;
4648 static const struct got_error *
4649 collect_commitables(void *arg, unsigned char status,
4650 unsigned char staged_status, const char *relpath,
4651 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4652 struct got_object_id *commit_id, int dirfd, const char *de_name)
4654 struct collect_commitables_arg *a = arg;
4655 const struct got_error *err = NULL;
4656 struct got_commitable *ct = NULL;
4657 struct got_pathlist_entry *new = NULL;
4658 char *parent_path = NULL, *path = NULL;
4659 struct stat sb;
4661 if (a->have_staged_files) {
4662 if (staged_status != GOT_STATUS_MODIFY &&
4663 staged_status != GOT_STATUS_ADD &&
4664 staged_status != GOT_STATUS_DELETE)
4665 return NULL;
4666 } else {
4667 if (status == GOT_STATUS_CONFLICT)
4668 return got_error(GOT_ERR_COMMIT_CONFLICT);
4670 if (status != GOT_STATUS_MODIFY &&
4671 status != GOT_STATUS_MODE_CHANGE &&
4672 status != GOT_STATUS_ADD &&
4673 status != GOT_STATUS_DELETE)
4674 return NULL;
4677 if (asprintf(&path, "/%s", relpath) == -1) {
4678 err = got_error_from_errno("asprintf");
4679 goto done;
4681 if (strcmp(path, "/") == 0) {
4682 parent_path = strdup("");
4683 if (parent_path == NULL)
4684 return got_error_from_errno("strdup");
4685 } else {
4686 err = got_path_dirname(&parent_path, path);
4687 if (err)
4688 return err;
4691 ct = calloc(1, sizeof(*ct));
4692 if (ct == NULL) {
4693 err = got_error_from_errno("calloc");
4694 goto done;
4697 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4698 relpath) == -1) {
4699 err = got_error_from_errno("asprintf");
4700 goto done;
4703 if (staged_status == GOT_STATUS_ADD ||
4704 staged_status == GOT_STATUS_MODIFY) {
4705 struct got_fileindex_entry *ie;
4706 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4707 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4708 case GOT_FILEIDX_MODE_REGULAR_FILE:
4709 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4710 ct->mode = S_IFREG;
4711 break;
4712 case GOT_FILEIDX_MODE_SYMLINK:
4713 ct->mode = S_IFLNK;
4714 break;
4715 default:
4716 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4717 goto done;
4719 ct->mode |= got_fileindex_entry_perms_get(ie);
4720 } else if (status != GOT_STATUS_DELETE &&
4721 staged_status != GOT_STATUS_DELETE) {
4722 if (dirfd != -1) {
4723 if (fstatat(dirfd, de_name, &sb,
4724 AT_SYMLINK_NOFOLLOW) == -1) {
4725 err = got_error_from_errno2("fstatat",
4726 ct->ondisk_path);
4727 goto done;
4729 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4730 err = got_error_from_errno2("lstat", ct->ondisk_path);
4731 goto done;
4733 ct->mode = sb.st_mode;
4736 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4737 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4738 relpath) == -1) {
4739 err = got_error_from_errno("asprintf");
4740 goto done;
4743 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4744 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4745 int is_bad_symlink;
4746 char target_path[PATH_MAX];
4747 ssize_t target_len;
4748 target_len = readlink(ct->ondisk_path, target_path,
4749 sizeof(target_path));
4750 if (target_len == -1) {
4751 err = got_error_from_errno2("readlink",
4752 ct->ondisk_path);
4753 goto done;
4755 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4756 target_len, ct->ondisk_path, a->worktree->root_path);
4757 if (err)
4758 goto done;
4759 if (is_bad_symlink) {
4760 err = got_error_path(ct->ondisk_path,
4761 GOT_ERR_BAD_SYMLINK);
4762 goto done;
4767 ct->status = status;
4768 ct->staged_status = staged_status;
4769 ct->blob_id = NULL; /* will be filled in when blob gets created */
4770 if (ct->status != GOT_STATUS_ADD &&
4771 ct->staged_status != GOT_STATUS_ADD) {
4772 ct->base_blob_id = got_object_id_dup(blob_id);
4773 if (ct->base_blob_id == NULL) {
4774 err = got_error_from_errno("got_object_id_dup");
4775 goto done;
4777 ct->base_commit_id = got_object_id_dup(commit_id);
4778 if (ct->base_commit_id == NULL) {
4779 err = got_error_from_errno("got_object_id_dup");
4780 goto done;
4783 if (ct->staged_status == GOT_STATUS_ADD ||
4784 ct->staged_status == GOT_STATUS_MODIFY) {
4785 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4786 if (ct->staged_blob_id == NULL) {
4787 err = got_error_from_errno("got_object_id_dup");
4788 goto done;
4791 ct->path = strdup(path);
4792 if (ct->path == NULL) {
4793 err = got_error_from_errno("strdup");
4794 goto done;
4796 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4797 done:
4798 if (ct && (err || new == NULL))
4799 free_commitable(ct);
4800 free(parent_path);
4801 free(path);
4802 return err;
4805 static const struct got_error *write_tree(struct got_object_id **, int *,
4806 struct got_tree_object *, const char *, struct got_pathlist_head *,
4807 got_worktree_status_cb status_cb, void *status_arg,
4808 struct got_repository *);
4810 static const struct got_error *
4811 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4812 struct got_tree_entry *te, const char *parent_path,
4813 struct got_pathlist_head *commitable_paths,
4814 got_worktree_status_cb status_cb, void *status_arg,
4815 struct got_repository *repo)
4817 const struct got_error *err = NULL;
4818 struct got_tree_object *subtree;
4819 char *subpath;
4821 if (asprintf(&subpath, "%s%s%s", parent_path,
4822 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4823 return got_error_from_errno("asprintf");
4825 err = got_object_open_as_tree(&subtree, repo, &te->id);
4826 if (err)
4827 return err;
4829 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4830 commitable_paths, status_cb, status_arg, repo);
4831 got_object_tree_close(subtree);
4832 free(subpath);
4833 return err;
4836 static const struct got_error *
4837 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4839 const struct got_error *err = NULL;
4840 char *ct_parent_path = NULL;
4842 *match = 0;
4844 if (strchr(ct->in_repo_path, '/') == NULL) {
4845 *match = got_path_is_root_dir(path);
4846 return NULL;
4849 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4850 if (err)
4851 return err;
4852 *match = (strcmp(path, ct_parent_path) == 0);
4853 free(ct_parent_path);
4854 return err;
4857 static mode_t
4858 get_ct_file_mode(struct got_commitable *ct)
4860 if (S_ISLNK(ct->mode))
4861 return S_IFLNK;
4863 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4866 static const struct got_error *
4867 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4868 struct got_tree_entry *te, struct got_commitable *ct)
4870 const struct got_error *err = NULL;
4872 *new_te = NULL;
4874 err = got_object_tree_entry_dup(new_te, te);
4875 if (err)
4876 goto done;
4878 (*new_te)->mode = get_ct_file_mode(ct);
4880 if (ct->staged_status == GOT_STATUS_MODIFY)
4881 memcpy(&(*new_te)->id, ct->staged_blob_id,
4882 sizeof((*new_te)->id));
4883 else
4884 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4885 done:
4886 if (err && *new_te) {
4887 free(*new_te);
4888 *new_te = NULL;
4890 return err;
4893 static const struct got_error *
4894 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4895 struct got_commitable *ct)
4897 const struct got_error *err = NULL;
4898 char *ct_name = NULL;
4900 *new_te = NULL;
4902 *new_te = calloc(1, sizeof(**new_te));
4903 if (*new_te == NULL)
4904 return got_error_from_errno("calloc");
4906 err = got_path_basename(&ct_name, ct->path);
4907 if (err)
4908 goto done;
4909 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4910 sizeof((*new_te)->name)) {
4911 err = got_error(GOT_ERR_NO_SPACE);
4912 goto done;
4915 (*new_te)->mode = get_ct_file_mode(ct);
4917 if (ct->staged_status == GOT_STATUS_ADD)
4918 memcpy(&(*new_te)->id, ct->staged_blob_id,
4919 sizeof((*new_te)->id));
4920 else
4921 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4922 done:
4923 free(ct_name);
4924 if (err && *new_te) {
4925 free(*new_te);
4926 *new_te = NULL;
4928 return err;
4931 static const struct got_error *
4932 insert_tree_entry(struct got_tree_entry *new_te,
4933 struct got_pathlist_head *paths)
4935 const struct got_error *err = NULL;
4936 struct got_pathlist_entry *new_pe;
4938 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4939 if (err)
4940 return err;
4941 if (new_pe == NULL)
4942 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4943 return NULL;
4946 static const struct got_error *
4947 report_ct_status(struct got_commitable *ct,
4948 got_worktree_status_cb status_cb, void *status_arg)
4950 const char *ct_path = ct->path;
4951 unsigned char status;
4953 while (ct_path[0] == '/')
4954 ct_path++;
4956 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4957 status = ct->staged_status;
4958 else
4959 status = ct->status;
4961 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4962 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4965 static const struct got_error *
4966 match_modified_subtree(int *modified, struct got_tree_entry *te,
4967 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4969 const struct got_error *err = NULL;
4970 struct got_pathlist_entry *pe;
4971 char *te_path;
4973 *modified = 0;
4975 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4976 got_path_is_root_dir(base_tree_path) ? "" : "/",
4977 te->name) == -1)
4978 return got_error_from_errno("asprintf");
4980 TAILQ_FOREACH(pe, commitable_paths, entry) {
4981 struct got_commitable *ct = pe->data;
4982 *modified = got_path_is_child(ct->in_repo_path, te_path,
4983 strlen(te_path));
4984 if (*modified)
4985 break;
4988 free(te_path);
4989 return err;
4992 static const struct got_error *
4993 match_deleted_or_modified_ct(struct got_commitable **ctp,
4994 struct got_tree_entry *te, const char *base_tree_path,
4995 struct got_pathlist_head *commitable_paths)
4997 const struct got_error *err = NULL;
4998 struct got_pathlist_entry *pe;
5000 *ctp = NULL;
5002 TAILQ_FOREACH(pe, commitable_paths, entry) {
5003 struct got_commitable *ct = pe->data;
5004 char *ct_name = NULL;
5005 int path_matches;
5007 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5008 if (ct->status != GOT_STATUS_MODIFY &&
5009 ct->status != GOT_STATUS_MODE_CHANGE &&
5010 ct->status != GOT_STATUS_DELETE)
5011 continue;
5012 } else {
5013 if (ct->staged_status != GOT_STATUS_MODIFY &&
5014 ct->staged_status != GOT_STATUS_DELETE)
5015 continue;
5018 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5019 continue;
5021 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5022 if (err)
5023 return err;
5024 if (!path_matches)
5025 continue;
5027 err = got_path_basename(&ct_name, pe->path);
5028 if (err)
5029 return err;
5031 if (strcmp(te->name, ct_name) != 0) {
5032 free(ct_name);
5033 continue;
5035 free(ct_name);
5037 *ctp = ct;
5038 break;
5041 return err;
5044 static const struct got_error *
5045 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5046 const char *child_path, const char *path_base_tree,
5047 struct got_pathlist_head *commitable_paths,
5048 got_worktree_status_cb status_cb, void *status_arg,
5049 struct got_repository *repo)
5051 const struct got_error *err = NULL;
5052 struct got_tree_entry *new_te;
5053 char *subtree_path;
5054 struct got_object_id *id = NULL;
5055 int nentries;
5057 *new_tep = NULL;
5059 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5060 got_path_is_root_dir(path_base_tree) ? "" : "/",
5061 child_path) == -1)
5062 return got_error_from_errno("asprintf");
5064 new_te = calloc(1, sizeof(*new_te));
5065 if (new_te == NULL)
5066 return got_error_from_errno("calloc");
5067 new_te->mode = S_IFDIR;
5069 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5070 sizeof(new_te->name)) {
5071 err = got_error(GOT_ERR_NO_SPACE);
5072 goto done;
5074 err = write_tree(&id, &nentries, NULL, subtree_path,
5075 commitable_paths, status_cb, status_arg, repo);
5076 if (err) {
5077 free(new_te);
5078 goto done;
5080 memcpy(&new_te->id, id, sizeof(new_te->id));
5081 done:
5082 free(id);
5083 free(subtree_path);
5084 if (err == NULL)
5085 *new_tep = new_te;
5086 return err;
5089 static const struct got_error *
5090 write_tree(struct got_object_id **new_tree_id, int *nentries,
5091 struct got_tree_object *base_tree, const char *path_base_tree,
5092 struct got_pathlist_head *commitable_paths,
5093 got_worktree_status_cb status_cb, void *status_arg,
5094 struct got_repository *repo)
5096 const struct got_error *err = NULL;
5097 struct got_pathlist_head paths;
5098 struct got_tree_entry *te, *new_te = NULL;
5099 struct got_pathlist_entry *pe;
5101 TAILQ_INIT(&paths);
5102 *nentries = 0;
5104 /* Insert, and recurse into, newly added entries first. */
5105 TAILQ_FOREACH(pe, commitable_paths, entry) {
5106 struct got_commitable *ct = pe->data;
5107 char *child_path = NULL, *slash;
5109 if ((ct->status != GOT_STATUS_ADD &&
5110 ct->staged_status != GOT_STATUS_ADD) ||
5111 (ct->flags & GOT_COMMITABLE_ADDED))
5112 continue;
5114 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5115 strlen(path_base_tree)))
5116 continue;
5118 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5119 ct->in_repo_path);
5120 if (err)
5121 goto done;
5123 slash = strchr(child_path, '/');
5124 if (slash == NULL) {
5125 err = alloc_added_blob_tree_entry(&new_te, ct);
5126 if (err)
5127 goto done;
5128 err = report_ct_status(ct, status_cb, status_arg);
5129 if (err)
5130 goto done;
5131 ct->flags |= GOT_COMMITABLE_ADDED;
5132 err = insert_tree_entry(new_te, &paths);
5133 if (err)
5134 goto done;
5135 (*nentries)++;
5136 } else {
5137 *slash = '\0'; /* trim trailing path components */
5138 if (base_tree == NULL ||
5139 got_object_tree_find_entry(base_tree, child_path)
5140 == NULL) {
5141 err = make_subtree_for_added_blob(&new_te,
5142 child_path, path_base_tree,
5143 commitable_paths, status_cb, status_arg,
5144 repo);
5145 if (err)
5146 goto done;
5147 err = insert_tree_entry(new_te, &paths);
5148 if (err)
5149 goto done;
5150 (*nentries)++;
5155 if (base_tree) {
5156 int i, nbase_entries;
5157 /* Handle modified and deleted entries. */
5158 nbase_entries = got_object_tree_get_nentries(base_tree);
5159 for (i = 0; i < nbase_entries; i++) {
5160 struct got_commitable *ct = NULL;
5162 te = got_object_tree_get_entry(base_tree, i);
5163 if (got_object_tree_entry_is_submodule(te)) {
5164 /* Entry is a submodule; just copy it. */
5165 err = got_object_tree_entry_dup(&new_te, te);
5166 if (err)
5167 goto done;
5168 err = insert_tree_entry(new_te, &paths);
5169 if (err)
5170 goto done;
5171 (*nentries)++;
5172 continue;
5175 if (S_ISDIR(te->mode)) {
5176 int modified;
5177 err = got_object_tree_entry_dup(&new_te, te);
5178 if (err)
5179 goto done;
5180 err = match_modified_subtree(&modified, te,
5181 path_base_tree, commitable_paths);
5182 if (err)
5183 goto done;
5184 /* Avoid recursion into unmodified subtrees. */
5185 if (modified) {
5186 struct got_object_id *new_id;
5187 int nsubentries;
5188 err = write_subtree(&new_id,
5189 &nsubentries, te,
5190 path_base_tree, commitable_paths,
5191 status_cb, status_arg, repo);
5192 if (err)
5193 goto done;
5194 if (nsubentries == 0) {
5195 /* All entries were deleted. */
5196 free(new_id);
5197 continue;
5199 memcpy(&new_te->id, new_id,
5200 sizeof(new_te->id));
5201 free(new_id);
5203 err = insert_tree_entry(new_te, &paths);
5204 if (err)
5205 goto done;
5206 (*nentries)++;
5207 continue;
5210 err = match_deleted_or_modified_ct(&ct, te,
5211 path_base_tree, commitable_paths);
5212 if (err)
5213 goto done;
5214 if (ct) {
5215 /* NB: Deleted entries get dropped here. */
5216 if (ct->status == GOT_STATUS_MODIFY ||
5217 ct->status == GOT_STATUS_MODE_CHANGE ||
5218 ct->staged_status == GOT_STATUS_MODIFY) {
5219 err = alloc_modified_blob_tree_entry(
5220 &new_te, te, ct);
5221 if (err)
5222 goto done;
5223 err = insert_tree_entry(new_te, &paths);
5224 if (err)
5225 goto done;
5226 (*nentries)++;
5228 err = report_ct_status(ct, status_cb,
5229 status_arg);
5230 if (err)
5231 goto done;
5232 } else {
5233 /* Entry is unchanged; just copy it. */
5234 err = got_object_tree_entry_dup(&new_te, te);
5235 if (err)
5236 goto done;
5237 err = insert_tree_entry(new_te, &paths);
5238 if (err)
5239 goto done;
5240 (*nentries)++;
5245 /* Write new list of entries; deleted entries have been dropped. */
5246 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5247 done:
5248 got_pathlist_free(&paths);
5249 return err;
5252 static const struct got_error *
5253 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5254 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5255 int have_staged_files)
5257 const struct got_error *err = NULL;
5258 struct got_pathlist_entry *pe;
5260 TAILQ_FOREACH(pe, commitable_paths, entry) {
5261 struct got_fileindex_entry *ie;
5262 struct got_commitable *ct = pe->data;
5264 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5265 if (ie) {
5266 if (ct->status == GOT_STATUS_DELETE ||
5267 ct->staged_status == GOT_STATUS_DELETE) {
5268 got_fileindex_entry_remove(fileindex, ie);
5269 } else if (ct->staged_status == GOT_STATUS_ADD ||
5270 ct->staged_status == GOT_STATUS_MODIFY) {
5271 got_fileindex_entry_stage_set(ie,
5272 GOT_FILEIDX_STAGE_NONE);
5273 err = got_fileindex_entry_update(ie,
5274 ct->ondisk_path, ct->staged_blob_id->sha1,
5275 new_base_commit_id->sha1,
5276 !have_staged_files);
5277 } else
5278 err = got_fileindex_entry_update(ie,
5279 ct->ondisk_path, ct->blob_id->sha1,
5280 new_base_commit_id->sha1,
5281 !have_staged_files);
5282 } else {
5283 err = got_fileindex_entry_alloc(&ie, pe->path);
5284 if (err)
5285 break;
5286 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5287 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5288 if (err) {
5289 got_fileindex_entry_free(ie);
5290 break;
5292 err = got_fileindex_entry_add(fileindex, ie);
5293 if (err) {
5294 got_fileindex_entry_free(ie);
5295 break;
5299 return err;
5303 static const struct got_error *
5304 check_out_of_date(const char *in_repo_path, unsigned char status,
5305 unsigned char staged_status, struct got_object_id *base_blob_id,
5306 struct got_object_id *base_commit_id,
5307 struct got_object_id *head_commit_id, struct got_repository *repo,
5308 int ood_errcode)
5310 const struct got_error *err = NULL;
5311 struct got_object_id *id = NULL;
5313 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5314 /* Trivial case: base commit == head commit */
5315 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5316 return NULL;
5318 * Ensure file content which local changes were based
5319 * on matches file content in the branch head.
5321 err = got_object_id_by_path(&id, repo, head_commit_id,
5322 in_repo_path);
5323 if (err) {
5324 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5325 err = got_error(ood_errcode);
5326 goto done;
5327 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5328 err = got_error(ood_errcode);
5329 } else {
5330 /* Require that added files don't exist in the branch head. */
5331 err = got_object_id_by_path(&id, repo, head_commit_id,
5332 in_repo_path);
5333 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5334 goto done;
5335 err = id ? got_error(ood_errcode) : NULL;
5337 done:
5338 free(id);
5339 return err;
5342 const struct got_error *
5343 commit_worktree(struct got_object_id **new_commit_id,
5344 struct got_pathlist_head *commitable_paths,
5345 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5346 const char *author, const char *committer,
5347 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5348 got_worktree_status_cb status_cb, void *status_arg,
5349 struct got_repository *repo)
5351 const struct got_error *err = NULL, *unlockerr = NULL;
5352 struct got_pathlist_entry *pe;
5353 const char *head_ref_name = NULL;
5354 struct got_commit_object *head_commit = NULL;
5355 struct got_reference *head_ref2 = NULL;
5356 struct got_object_id *head_commit_id2 = NULL;
5357 struct got_tree_object *head_tree = NULL;
5358 struct got_object_id *new_tree_id = NULL;
5359 int nentries;
5360 struct got_object_id_queue parent_ids;
5361 struct got_object_qid *pid = NULL;
5362 char *logmsg = NULL;
5364 *new_commit_id = NULL;
5366 SIMPLEQ_INIT(&parent_ids);
5368 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5369 if (err)
5370 goto done;
5372 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5373 if (err)
5374 goto done;
5376 if (commit_msg_cb != NULL) {
5377 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5378 if (err)
5379 goto done;
5382 if (logmsg == NULL || strlen(logmsg) == 0) {
5383 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5384 goto done;
5387 /* Create blobs from added and modified files and record their IDs. */
5388 TAILQ_FOREACH(pe, commitable_paths, entry) {
5389 struct got_commitable *ct = pe->data;
5390 char *ondisk_path;
5392 /* Blobs for staged files already exist. */
5393 if (ct->staged_status == GOT_STATUS_ADD ||
5394 ct->staged_status == GOT_STATUS_MODIFY)
5395 continue;
5397 if (ct->status != GOT_STATUS_ADD &&
5398 ct->status != GOT_STATUS_MODIFY &&
5399 ct->status != GOT_STATUS_MODE_CHANGE)
5400 continue;
5402 if (asprintf(&ondisk_path, "%s/%s",
5403 worktree->root_path, pe->path) == -1) {
5404 err = got_error_from_errno("asprintf");
5405 goto done;
5407 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5408 free(ondisk_path);
5409 if (err)
5410 goto done;
5413 /* Recursively write new tree objects. */
5414 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5415 commitable_paths, status_cb, status_arg, repo);
5416 if (err)
5417 goto done;
5419 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5420 if (err)
5421 goto done;
5422 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5423 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5424 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5425 got_object_qid_free(pid);
5426 if (logmsg != NULL)
5427 free(logmsg);
5428 if (err)
5429 goto done;
5431 /* Check if a concurrent commit to our branch has occurred. */
5432 head_ref_name = got_worktree_get_head_ref_name(worktree);
5433 if (head_ref_name == NULL) {
5434 err = got_error_from_errno("got_worktree_get_head_ref_name");
5435 goto done;
5437 /* Lock the reference here to prevent concurrent modification. */
5438 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5439 if (err)
5440 goto done;
5441 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5442 if (err)
5443 goto done;
5444 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5445 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5446 goto done;
5448 /* Update branch head in repository. */
5449 err = got_ref_change_ref(head_ref2, *new_commit_id);
5450 if (err)
5451 goto done;
5452 err = got_ref_write(head_ref2, repo);
5453 if (err)
5454 goto done;
5456 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5457 if (err)
5458 goto done;
5460 err = ref_base_commit(worktree, repo);
5461 if (err)
5462 goto done;
5463 done:
5464 if (head_tree)
5465 got_object_tree_close(head_tree);
5466 if (head_commit)
5467 got_object_commit_close(head_commit);
5468 free(head_commit_id2);
5469 if (head_ref2) {
5470 unlockerr = got_ref_unlock(head_ref2);
5471 if (unlockerr && err == NULL)
5472 err = unlockerr;
5473 got_ref_close(head_ref2);
5475 return err;
5478 static const struct got_error *
5479 check_path_is_commitable(const char *path,
5480 struct got_pathlist_head *commitable_paths)
5482 struct got_pathlist_entry *cpe = NULL;
5483 size_t path_len = strlen(path);
5485 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5486 struct got_commitable *ct = cpe->data;
5487 const char *ct_path = ct->path;
5489 while (ct_path[0] == '/')
5490 ct_path++;
5492 if (strcmp(path, ct_path) == 0 ||
5493 got_path_is_child(ct_path, path, path_len))
5494 break;
5497 if (cpe == NULL)
5498 return got_error_path(path, GOT_ERR_BAD_PATH);
5500 return NULL;
5503 static const struct got_error *
5504 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5506 int *have_staged_files = arg;
5508 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5509 *have_staged_files = 1;
5510 return got_error(GOT_ERR_CANCELLED);
5513 return NULL;
5516 static const struct got_error *
5517 check_non_staged_files(struct got_fileindex *fileindex,
5518 struct got_pathlist_head *paths)
5520 struct got_pathlist_entry *pe;
5521 struct got_fileindex_entry *ie;
5523 TAILQ_FOREACH(pe, paths, entry) {
5524 if (pe->path[0] == '\0')
5525 continue;
5526 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5527 if (ie == NULL)
5528 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5529 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5530 return got_error_path(pe->path,
5531 GOT_ERR_FILE_NOT_STAGED);
5534 return NULL;
5537 const struct got_error *
5538 got_worktree_commit(struct got_object_id **new_commit_id,
5539 struct got_worktree *worktree, struct got_pathlist_head *paths,
5540 const char *author, const char *committer, int allow_bad_symlinks,
5541 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5542 got_worktree_status_cb status_cb, void *status_arg,
5543 struct got_repository *repo)
5545 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5546 struct got_fileindex *fileindex = NULL;
5547 char *fileindex_path = NULL;
5548 struct got_pathlist_head commitable_paths;
5549 struct collect_commitables_arg cc_arg;
5550 struct got_pathlist_entry *pe;
5551 struct got_reference *head_ref = NULL;
5552 struct got_object_id *head_commit_id = NULL;
5553 int have_staged_files = 0;
5555 *new_commit_id = NULL;
5557 TAILQ_INIT(&commitable_paths);
5559 err = lock_worktree(worktree, LOCK_EX);
5560 if (err)
5561 goto done;
5563 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5564 if (err)
5565 goto done;
5567 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5568 if (err)
5569 goto done;
5571 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5572 if (err)
5573 goto done;
5575 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5576 &have_staged_files);
5577 if (err && err->code != GOT_ERR_CANCELLED)
5578 goto done;
5579 if (have_staged_files) {
5580 err = check_non_staged_files(fileindex, paths);
5581 if (err)
5582 goto done;
5585 cc_arg.commitable_paths = &commitable_paths;
5586 cc_arg.worktree = worktree;
5587 cc_arg.fileindex = fileindex;
5588 cc_arg.repo = repo;
5589 cc_arg.have_staged_files = have_staged_files;
5590 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5591 TAILQ_FOREACH(pe, paths, entry) {
5592 err = worktree_status(worktree, pe->path, fileindex, repo,
5593 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5594 if (err)
5595 goto done;
5598 if (TAILQ_EMPTY(&commitable_paths)) {
5599 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5600 goto done;
5603 TAILQ_FOREACH(pe, paths, entry) {
5604 err = check_path_is_commitable(pe->path, &commitable_paths);
5605 if (err)
5606 goto done;
5609 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5610 struct got_commitable *ct = pe->data;
5611 const char *ct_path = ct->in_repo_path;
5613 while (ct_path[0] == '/')
5614 ct_path++;
5615 err = check_out_of_date(ct_path, ct->status,
5616 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5617 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5618 if (err)
5619 goto done;
5623 err = commit_worktree(new_commit_id, &commitable_paths,
5624 head_commit_id, worktree, author, committer,
5625 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5626 if (err)
5627 goto done;
5629 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5630 fileindex, have_staged_files);
5631 sync_err = sync_fileindex(fileindex, fileindex_path);
5632 if (sync_err && err == NULL)
5633 err = sync_err;
5634 done:
5635 if (fileindex)
5636 got_fileindex_free(fileindex);
5637 free(fileindex_path);
5638 unlockerr = lock_worktree(worktree, LOCK_SH);
5639 if (unlockerr && err == NULL)
5640 err = unlockerr;
5641 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5642 struct got_commitable *ct = pe->data;
5643 free_commitable(ct);
5645 got_pathlist_free(&commitable_paths);
5646 return err;
5649 const char *
5650 got_commitable_get_path(struct got_commitable *ct)
5652 return ct->path;
5655 unsigned int
5656 got_commitable_get_status(struct got_commitable *ct)
5658 return ct->status;
5661 struct check_rebase_ok_arg {
5662 struct got_worktree *worktree;
5663 struct got_repository *repo;
5666 static const struct got_error *
5667 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5669 const struct got_error *err = NULL;
5670 struct check_rebase_ok_arg *a = arg;
5671 unsigned char status;
5672 struct stat sb;
5673 char *ondisk_path;
5675 /* Reject rebase of a work tree with mixed base commits. */
5676 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5677 SHA1_DIGEST_LENGTH))
5678 return got_error(GOT_ERR_MIXED_COMMITS);
5680 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5681 == -1)
5682 return got_error_from_errno("asprintf");
5684 /* Reject rebase of a work tree with modified or staged files. */
5685 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5686 free(ondisk_path);
5687 if (err)
5688 return err;
5690 if (status != GOT_STATUS_NO_CHANGE)
5691 return got_error(GOT_ERR_MODIFIED);
5692 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5693 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5695 return NULL;
5698 const struct got_error *
5699 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5700 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5701 struct got_worktree *worktree, struct got_reference *branch,
5702 struct got_repository *repo)
5704 const struct got_error *err = NULL;
5705 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5706 char *branch_ref_name = NULL;
5707 char *fileindex_path = NULL;
5708 struct check_rebase_ok_arg ok_arg;
5709 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5710 struct got_object_id *wt_branch_tip = NULL;
5712 *new_base_branch_ref = NULL;
5713 *tmp_branch = NULL;
5714 *fileindex = NULL;
5716 err = lock_worktree(worktree, LOCK_EX);
5717 if (err)
5718 return err;
5720 err = open_fileindex(fileindex, &fileindex_path, worktree);
5721 if (err)
5722 goto done;
5724 ok_arg.worktree = worktree;
5725 ok_arg.repo = repo;
5726 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5727 &ok_arg);
5728 if (err)
5729 goto done;
5731 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5732 if (err)
5733 goto done;
5735 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5736 if (err)
5737 goto done;
5739 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5740 if (err)
5741 goto done;
5743 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5744 0);
5745 if (err)
5746 goto done;
5748 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5749 if (err)
5750 goto done;
5751 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5752 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5753 goto done;
5756 err = got_ref_alloc_symref(new_base_branch_ref,
5757 new_base_branch_ref_name, wt_branch);
5758 if (err)
5759 goto done;
5760 err = got_ref_write(*new_base_branch_ref, repo);
5761 if (err)
5762 goto done;
5764 /* TODO Lock original branch's ref while rebasing? */
5766 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5767 if (err)
5768 goto done;
5770 err = got_ref_write(branch_ref, repo);
5771 if (err)
5772 goto done;
5774 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5775 worktree->base_commit_id);
5776 if (err)
5777 goto done;
5778 err = got_ref_write(*tmp_branch, repo);
5779 if (err)
5780 goto done;
5782 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5783 if (err)
5784 goto done;
5785 done:
5786 free(fileindex_path);
5787 free(tmp_branch_name);
5788 free(new_base_branch_ref_name);
5789 free(branch_ref_name);
5790 if (branch_ref)
5791 got_ref_close(branch_ref);
5792 if (wt_branch)
5793 got_ref_close(wt_branch);
5794 free(wt_branch_tip);
5795 if (err) {
5796 if (*new_base_branch_ref) {
5797 got_ref_close(*new_base_branch_ref);
5798 *new_base_branch_ref = NULL;
5800 if (*tmp_branch) {
5801 got_ref_close(*tmp_branch);
5802 *tmp_branch = NULL;
5804 if (*fileindex) {
5805 got_fileindex_free(*fileindex);
5806 *fileindex = NULL;
5808 lock_worktree(worktree, LOCK_SH);
5810 return err;
5813 const struct got_error *
5814 got_worktree_rebase_continue(struct got_object_id **commit_id,
5815 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5816 struct got_reference **branch, struct got_fileindex **fileindex,
5817 struct got_worktree *worktree, struct got_repository *repo)
5819 const struct got_error *err;
5820 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5821 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5822 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5823 char *fileindex_path = NULL;
5824 int have_staged_files = 0;
5826 *commit_id = NULL;
5827 *new_base_branch = NULL;
5828 *tmp_branch = NULL;
5829 *branch = NULL;
5830 *fileindex = NULL;
5832 err = lock_worktree(worktree, LOCK_EX);
5833 if (err)
5834 return err;
5836 err = open_fileindex(fileindex, &fileindex_path, worktree);
5837 if (err)
5838 goto done;
5840 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5841 &have_staged_files);
5842 if (err && err->code != GOT_ERR_CANCELLED)
5843 goto done;
5844 if (have_staged_files) {
5845 err = got_error(GOT_ERR_STAGED_PATHS);
5846 goto done;
5849 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5850 if (err)
5851 goto done;
5853 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5854 if (err)
5855 goto done;
5857 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5858 if (err)
5859 goto done;
5861 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5862 if (err)
5863 goto done;
5865 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5866 if (err)
5867 goto done;
5869 err = got_ref_open(branch, repo,
5870 got_ref_get_symref_target(branch_ref), 0);
5871 if (err)
5872 goto done;
5874 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5875 if (err)
5876 goto done;
5878 err = got_ref_resolve(commit_id, repo, commit_ref);
5879 if (err)
5880 goto done;
5882 err = got_ref_open(new_base_branch, repo,
5883 new_base_branch_ref_name, 0);
5884 if (err)
5885 goto done;
5887 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5888 if (err)
5889 goto done;
5890 done:
5891 free(commit_ref_name);
5892 free(branch_ref_name);
5893 free(fileindex_path);
5894 if (commit_ref)
5895 got_ref_close(commit_ref);
5896 if (branch_ref)
5897 got_ref_close(branch_ref);
5898 if (err) {
5899 free(*commit_id);
5900 *commit_id = NULL;
5901 if (*tmp_branch) {
5902 got_ref_close(*tmp_branch);
5903 *tmp_branch = NULL;
5905 if (*new_base_branch) {
5906 got_ref_close(*new_base_branch);
5907 *new_base_branch = NULL;
5909 if (*branch) {
5910 got_ref_close(*branch);
5911 *branch = NULL;
5913 if (*fileindex) {
5914 got_fileindex_free(*fileindex);
5915 *fileindex = NULL;
5917 lock_worktree(worktree, LOCK_SH);
5919 return err;
5922 const struct got_error *
5923 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5925 const struct got_error *err;
5926 char *tmp_branch_name = NULL;
5928 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5929 if (err)
5930 return err;
5932 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5933 free(tmp_branch_name);
5934 return NULL;
5937 static const struct got_error *
5938 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5939 char **logmsg, void *arg)
5941 *logmsg = arg;
5942 return NULL;
5945 static const struct got_error *
5946 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5947 const char *path, struct got_object_id *blob_id,
5948 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5949 int dirfd, const char *de_name)
5951 return NULL;
5954 struct collect_merged_paths_arg {
5955 got_worktree_checkout_cb progress_cb;
5956 void *progress_arg;
5957 struct got_pathlist_head *merged_paths;
5960 static const struct got_error *
5961 collect_merged_paths(void *arg, unsigned char status, const char *path)
5963 const struct got_error *err;
5964 struct collect_merged_paths_arg *a = arg;
5965 char *p;
5966 struct got_pathlist_entry *new;
5968 err = (*a->progress_cb)(a->progress_arg, status, path);
5969 if (err)
5970 return err;
5972 if (status != GOT_STATUS_MERGE &&
5973 status != GOT_STATUS_ADD &&
5974 status != GOT_STATUS_DELETE &&
5975 status != GOT_STATUS_CONFLICT)
5976 return NULL;
5978 p = strdup(path);
5979 if (p == NULL)
5980 return got_error_from_errno("strdup");
5982 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5983 if (err || new == NULL)
5984 free(p);
5985 return err;
5988 void
5989 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5991 struct got_pathlist_entry *pe;
5993 TAILQ_FOREACH(pe, merged_paths, entry)
5994 free((char *)pe->path);
5996 got_pathlist_free(merged_paths);
5999 static const struct got_error *
6000 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6001 int is_rebase, struct got_repository *repo)
6003 const struct got_error *err;
6004 struct got_reference *commit_ref = NULL;
6006 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6007 if (err) {
6008 if (err->code != GOT_ERR_NOT_REF)
6009 goto done;
6010 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6011 if (err)
6012 goto done;
6013 err = got_ref_write(commit_ref, repo);
6014 if (err)
6015 goto done;
6016 } else if (is_rebase) {
6017 struct got_object_id *stored_id;
6018 int cmp;
6020 err = got_ref_resolve(&stored_id, repo, commit_ref);
6021 if (err)
6022 goto done;
6023 cmp = got_object_id_cmp(commit_id, stored_id);
6024 free(stored_id);
6025 if (cmp != 0) {
6026 err = got_error(GOT_ERR_REBASE_COMMITID);
6027 goto done;
6030 done:
6031 if (commit_ref)
6032 got_ref_close(commit_ref);
6033 return err;
6036 static const struct got_error *
6037 rebase_merge_files(struct got_pathlist_head *merged_paths,
6038 const char *commit_ref_name, struct got_worktree *worktree,
6039 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6040 struct got_object_id *commit_id, struct got_repository *repo,
6041 got_worktree_checkout_cb progress_cb, void *progress_arg,
6042 got_cancel_cb cancel_cb, void *cancel_arg)
6044 const struct got_error *err;
6045 struct got_reference *commit_ref = NULL;
6046 struct collect_merged_paths_arg cmp_arg;
6047 char *fileindex_path;
6049 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6051 err = get_fileindex_path(&fileindex_path, worktree);
6052 if (err)
6053 return err;
6055 cmp_arg.progress_cb = progress_cb;
6056 cmp_arg.progress_arg = progress_arg;
6057 cmp_arg.merged_paths = merged_paths;
6058 err = merge_files(worktree, fileindex, fileindex_path,
6059 parent_commit_id, commit_id, repo, collect_merged_paths,
6060 &cmp_arg, cancel_cb, cancel_arg);
6061 if (commit_ref)
6062 got_ref_close(commit_ref);
6063 return err;
6066 const struct got_error *
6067 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6068 struct got_worktree *worktree, struct got_fileindex *fileindex,
6069 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6070 struct got_repository *repo,
6071 got_worktree_checkout_cb progress_cb, void *progress_arg,
6072 got_cancel_cb cancel_cb, void *cancel_arg)
6074 const struct got_error *err;
6075 char *commit_ref_name;
6077 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6078 if (err)
6079 return err;
6081 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6082 if (err)
6083 goto done;
6085 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6086 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6087 progress_arg, cancel_cb, cancel_arg);
6088 done:
6089 free(commit_ref_name);
6090 return err;
6093 const struct got_error *
6094 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6095 struct got_worktree *worktree, struct got_fileindex *fileindex,
6096 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6097 struct got_repository *repo,
6098 got_worktree_checkout_cb progress_cb, void *progress_arg,
6099 got_cancel_cb cancel_cb, void *cancel_arg)
6101 const struct got_error *err;
6102 char *commit_ref_name;
6104 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6105 if (err)
6106 return err;
6108 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6109 if (err)
6110 goto done;
6112 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6113 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6114 progress_arg, cancel_cb, cancel_arg);
6115 done:
6116 free(commit_ref_name);
6117 return err;
6120 static const struct got_error *
6121 rebase_commit(struct got_object_id **new_commit_id,
6122 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6123 struct got_worktree *worktree, struct got_fileindex *fileindex,
6124 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6125 const char *new_logmsg, struct got_repository *repo)
6127 const struct got_error *err, *sync_err;
6128 struct got_pathlist_head commitable_paths;
6129 struct collect_commitables_arg cc_arg;
6130 char *fileindex_path = NULL;
6131 struct got_reference *head_ref = NULL;
6132 struct got_object_id *head_commit_id = NULL;
6133 char *logmsg = NULL;
6135 TAILQ_INIT(&commitable_paths);
6136 *new_commit_id = NULL;
6138 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6140 err = get_fileindex_path(&fileindex_path, worktree);
6141 if (err)
6142 return err;
6144 cc_arg.commitable_paths = &commitable_paths;
6145 cc_arg.worktree = worktree;
6146 cc_arg.repo = repo;
6147 cc_arg.have_staged_files = 0;
6149 * If possible get the status of individual files directly to
6150 * avoid crawling the entire work tree once per rebased commit.
6151 * TODO: Ideally, merged_paths would contain a list of commitables
6152 * we could use so we could skip worktree_status() entirely.
6154 if (merged_paths) {
6155 struct got_pathlist_entry *pe;
6156 TAILQ_FOREACH(pe, merged_paths, entry) {
6157 err = worktree_status(worktree, pe->path, fileindex,
6158 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6159 0);
6160 if (err)
6161 goto done;
6163 } else {
6164 err = worktree_status(worktree, "", fileindex, repo,
6165 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6166 if (err)
6167 goto done;
6170 if (TAILQ_EMPTY(&commitable_paths)) {
6171 /* No-op change; commit will be elided. */
6172 err = got_ref_delete(commit_ref, repo);
6173 if (err)
6174 goto done;
6175 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6176 goto done;
6179 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6180 if (err)
6181 goto done;
6183 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6184 if (err)
6185 goto done;
6187 if (new_logmsg) {
6188 logmsg = strdup(new_logmsg);
6189 if (logmsg == NULL) {
6190 err = got_error_from_errno("strdup");
6191 goto done;
6193 } else {
6194 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6195 if (err)
6196 goto done;
6199 /* NB: commit_worktree will call free(logmsg) */
6200 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6201 worktree, got_object_commit_get_author(orig_commit),
6202 got_object_commit_get_committer(orig_commit),
6203 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6204 if (err)
6205 goto done;
6207 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6208 if (err)
6209 goto done;
6211 err = got_ref_delete(commit_ref, repo);
6212 if (err)
6213 goto done;
6215 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6216 fileindex, 0);
6217 sync_err = sync_fileindex(fileindex, fileindex_path);
6218 if (sync_err && err == NULL)
6219 err = sync_err;
6220 done:
6221 free(fileindex_path);
6222 free(head_commit_id);
6223 if (head_ref)
6224 got_ref_close(head_ref);
6225 if (err) {
6226 free(*new_commit_id);
6227 *new_commit_id = NULL;
6229 return err;
6232 const struct got_error *
6233 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6234 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6235 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6236 struct got_commit_object *orig_commit,
6237 struct got_object_id *orig_commit_id, struct got_repository *repo)
6239 const struct got_error *err;
6240 char *commit_ref_name;
6241 struct got_reference *commit_ref = NULL;
6242 struct got_object_id *commit_id = NULL;
6244 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6245 if (err)
6246 return err;
6248 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6249 if (err)
6250 goto done;
6251 err = got_ref_resolve(&commit_id, repo, commit_ref);
6252 if (err)
6253 goto done;
6254 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6255 err = got_error(GOT_ERR_REBASE_COMMITID);
6256 goto done;
6259 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6260 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6261 done:
6262 if (commit_ref)
6263 got_ref_close(commit_ref);
6264 free(commit_ref_name);
6265 free(commit_id);
6266 return err;
6269 const struct got_error *
6270 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6271 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6272 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6273 struct got_commit_object *orig_commit,
6274 struct got_object_id *orig_commit_id, const char *new_logmsg,
6275 struct got_repository *repo)
6277 const struct got_error *err;
6278 char *commit_ref_name;
6279 struct got_reference *commit_ref = NULL;
6281 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6282 if (err)
6283 return err;
6285 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6286 if (err)
6287 goto done;
6289 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6290 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6291 done:
6292 if (commit_ref)
6293 got_ref_close(commit_ref);
6294 free(commit_ref_name);
6295 return err;
6298 const struct got_error *
6299 got_worktree_rebase_postpone(struct got_worktree *worktree,
6300 struct got_fileindex *fileindex)
6302 if (fileindex)
6303 got_fileindex_free(fileindex);
6304 return lock_worktree(worktree, LOCK_SH);
6307 static const struct got_error *
6308 delete_ref(const char *name, struct got_repository *repo)
6310 const struct got_error *err;
6311 struct got_reference *ref;
6313 err = got_ref_open(&ref, repo, name, 0);
6314 if (err) {
6315 if (err->code == GOT_ERR_NOT_REF)
6316 return NULL;
6317 return err;
6320 err = got_ref_delete(ref, repo);
6321 got_ref_close(ref);
6322 return err;
6325 static const struct got_error *
6326 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6328 const struct got_error *err;
6329 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6330 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6332 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6333 if (err)
6334 goto done;
6335 err = delete_ref(tmp_branch_name, repo);
6336 if (err)
6337 goto done;
6339 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6340 if (err)
6341 goto done;
6342 err = delete_ref(new_base_branch_ref_name, repo);
6343 if (err)
6344 goto done;
6346 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6347 if (err)
6348 goto done;
6349 err = delete_ref(branch_ref_name, repo);
6350 if (err)
6351 goto done;
6353 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6354 if (err)
6355 goto done;
6356 err = delete_ref(commit_ref_name, repo);
6357 if (err)
6358 goto done;
6360 done:
6361 free(tmp_branch_name);
6362 free(new_base_branch_ref_name);
6363 free(branch_ref_name);
6364 free(commit_ref_name);
6365 return err;
6368 const struct got_error *
6369 got_worktree_rebase_complete(struct got_worktree *worktree,
6370 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6371 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6372 struct got_repository *repo)
6374 const struct got_error *err, *unlockerr;
6375 struct got_object_id *new_head_commit_id = NULL;
6377 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6378 if (err)
6379 return err;
6381 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6382 if (err)
6383 goto done;
6385 err = got_ref_write(rebased_branch, repo);
6386 if (err)
6387 goto done;
6389 err = got_worktree_set_head_ref(worktree, rebased_branch);
6390 if (err)
6391 goto done;
6393 err = delete_rebase_refs(worktree, repo);
6394 done:
6395 if (fileindex)
6396 got_fileindex_free(fileindex);
6397 free(new_head_commit_id);
6398 unlockerr = lock_worktree(worktree, LOCK_SH);
6399 if (unlockerr && err == NULL)
6400 err = unlockerr;
6401 return err;
6404 const struct got_error *
6405 got_worktree_rebase_abort(struct got_worktree *worktree,
6406 struct got_fileindex *fileindex, struct got_repository *repo,
6407 struct got_reference *new_base_branch,
6408 got_worktree_checkout_cb progress_cb, void *progress_arg)
6410 const struct got_error *err, *unlockerr, *sync_err;
6411 struct got_reference *resolved = NULL;
6412 struct got_object_id *commit_id = NULL;
6413 char *fileindex_path = NULL;
6414 struct revert_file_args rfa;
6415 struct got_object_id *tree_id = NULL;
6417 err = lock_worktree(worktree, LOCK_EX);
6418 if (err)
6419 return err;
6421 err = got_ref_open(&resolved, repo,
6422 got_ref_get_symref_target(new_base_branch), 0);
6423 if (err)
6424 goto done;
6426 err = got_worktree_set_head_ref(worktree, resolved);
6427 if (err)
6428 goto done;
6431 * XXX commits to the base branch could have happened while
6432 * we were busy rebasing; should we store the original commit ID
6433 * when rebase begins and read it back here?
6435 err = got_ref_resolve(&commit_id, repo, resolved);
6436 if (err)
6437 goto done;
6439 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6440 if (err)
6441 goto done;
6443 err = got_object_id_by_path(&tree_id, repo,
6444 worktree->base_commit_id, worktree->path_prefix);
6445 if (err)
6446 goto done;
6448 err = delete_rebase_refs(worktree, repo);
6449 if (err)
6450 goto done;
6452 err = get_fileindex_path(&fileindex_path, worktree);
6453 if (err)
6454 goto done;
6456 rfa.worktree = worktree;
6457 rfa.fileindex = fileindex;
6458 rfa.progress_cb = progress_cb;
6459 rfa.progress_arg = progress_arg;
6460 rfa.patch_cb = NULL;
6461 rfa.patch_arg = NULL;
6462 rfa.repo = repo;
6463 err = worktree_status(worktree, "", fileindex, repo,
6464 revert_file, &rfa, NULL, NULL, 0, 0);
6465 if (err)
6466 goto sync;
6468 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6469 repo, progress_cb, progress_arg, NULL, NULL);
6470 sync:
6471 sync_err = sync_fileindex(fileindex, fileindex_path);
6472 if (sync_err && err == NULL)
6473 err = sync_err;
6474 done:
6475 got_ref_close(resolved);
6476 free(tree_id);
6477 free(commit_id);
6478 if (fileindex)
6479 got_fileindex_free(fileindex);
6480 free(fileindex_path);
6482 unlockerr = lock_worktree(worktree, LOCK_SH);
6483 if (unlockerr && err == NULL)
6484 err = unlockerr;
6485 return err;
6488 const struct got_error *
6489 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6490 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6491 struct got_fileindex **fileindex, struct got_worktree *worktree,
6492 struct got_repository *repo)
6494 const struct got_error *err = NULL;
6495 char *tmp_branch_name = NULL;
6496 char *branch_ref_name = NULL;
6497 char *base_commit_ref_name = NULL;
6498 char *fileindex_path = NULL;
6499 struct check_rebase_ok_arg ok_arg;
6500 struct got_reference *wt_branch = NULL;
6501 struct got_reference *base_commit_ref = NULL;
6503 *tmp_branch = NULL;
6504 *branch_ref = NULL;
6505 *base_commit_id = NULL;
6506 *fileindex = NULL;
6508 err = lock_worktree(worktree, LOCK_EX);
6509 if (err)
6510 return err;
6512 err = open_fileindex(fileindex, &fileindex_path, worktree);
6513 if (err)
6514 goto done;
6516 ok_arg.worktree = worktree;
6517 ok_arg.repo = repo;
6518 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6519 &ok_arg);
6520 if (err)
6521 goto done;
6523 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6524 if (err)
6525 goto done;
6527 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6528 if (err)
6529 goto done;
6531 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6532 worktree);
6533 if (err)
6534 goto done;
6536 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6537 0);
6538 if (err)
6539 goto done;
6541 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6542 if (err)
6543 goto done;
6545 err = got_ref_write(*branch_ref, repo);
6546 if (err)
6547 goto done;
6549 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6550 worktree->base_commit_id);
6551 if (err)
6552 goto done;
6553 err = got_ref_write(base_commit_ref, repo);
6554 if (err)
6555 goto done;
6556 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6557 if (*base_commit_id == NULL) {
6558 err = got_error_from_errno("got_object_id_dup");
6559 goto done;
6562 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6563 worktree->base_commit_id);
6564 if (err)
6565 goto done;
6566 err = got_ref_write(*tmp_branch, repo);
6567 if (err)
6568 goto done;
6570 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6571 if (err)
6572 goto done;
6573 done:
6574 free(fileindex_path);
6575 free(tmp_branch_name);
6576 free(branch_ref_name);
6577 free(base_commit_ref_name);
6578 if (wt_branch)
6579 got_ref_close(wt_branch);
6580 if (err) {
6581 if (*branch_ref) {
6582 got_ref_close(*branch_ref);
6583 *branch_ref = NULL;
6585 if (*tmp_branch) {
6586 got_ref_close(*tmp_branch);
6587 *tmp_branch = NULL;
6589 free(*base_commit_id);
6590 if (*fileindex) {
6591 got_fileindex_free(*fileindex);
6592 *fileindex = NULL;
6594 lock_worktree(worktree, LOCK_SH);
6596 return err;
6599 const struct got_error *
6600 got_worktree_histedit_postpone(struct got_worktree *worktree,
6601 struct got_fileindex *fileindex)
6603 if (fileindex)
6604 got_fileindex_free(fileindex);
6605 return lock_worktree(worktree, LOCK_SH);
6608 const struct got_error *
6609 got_worktree_histedit_in_progress(int *in_progress,
6610 struct got_worktree *worktree)
6612 const struct got_error *err;
6613 char *tmp_branch_name = NULL;
6615 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6616 if (err)
6617 return err;
6619 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6620 free(tmp_branch_name);
6621 return NULL;
6624 const struct got_error *
6625 got_worktree_histedit_continue(struct got_object_id **commit_id,
6626 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6627 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6628 struct got_worktree *worktree, struct got_repository *repo)
6630 const struct got_error *err;
6631 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6632 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6633 struct got_reference *commit_ref = NULL;
6634 struct got_reference *base_commit_ref = NULL;
6635 char *fileindex_path = NULL;
6636 int have_staged_files = 0;
6638 *commit_id = NULL;
6639 *tmp_branch = NULL;
6640 *base_commit_id = NULL;
6641 *fileindex = NULL;
6643 err = lock_worktree(worktree, LOCK_EX);
6644 if (err)
6645 return err;
6647 err = open_fileindex(fileindex, &fileindex_path, worktree);
6648 if (err)
6649 goto done;
6651 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6652 &have_staged_files);
6653 if (err && err->code != GOT_ERR_CANCELLED)
6654 goto done;
6655 if (have_staged_files) {
6656 err = got_error(GOT_ERR_STAGED_PATHS);
6657 goto done;
6660 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6661 if (err)
6662 goto done;
6664 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6665 if (err)
6666 goto done;
6668 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6669 if (err)
6670 goto done;
6672 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6673 worktree);
6674 if (err)
6675 goto done;
6677 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6678 if (err)
6679 goto done;
6681 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6682 if (err)
6683 goto done;
6684 err = got_ref_resolve(commit_id, repo, commit_ref);
6685 if (err)
6686 goto done;
6688 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6689 if (err)
6690 goto done;
6691 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6692 if (err)
6693 goto done;
6695 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6696 if (err)
6697 goto done;
6698 done:
6699 free(commit_ref_name);
6700 free(branch_ref_name);
6701 free(fileindex_path);
6702 if (commit_ref)
6703 got_ref_close(commit_ref);
6704 if (base_commit_ref)
6705 got_ref_close(base_commit_ref);
6706 if (err) {
6707 free(*commit_id);
6708 *commit_id = NULL;
6709 free(*base_commit_id);
6710 *base_commit_id = NULL;
6711 if (*tmp_branch) {
6712 got_ref_close(*tmp_branch);
6713 *tmp_branch = NULL;
6715 if (*fileindex) {
6716 got_fileindex_free(*fileindex);
6717 *fileindex = NULL;
6719 lock_worktree(worktree, LOCK_EX);
6721 return err;
6724 static const struct got_error *
6725 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6727 const struct got_error *err;
6728 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6729 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6731 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6732 if (err)
6733 goto done;
6734 err = delete_ref(tmp_branch_name, repo);
6735 if (err)
6736 goto done;
6738 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6739 worktree);
6740 if (err)
6741 goto done;
6742 err = delete_ref(base_commit_ref_name, repo);
6743 if (err)
6744 goto done;
6746 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6747 if (err)
6748 goto done;
6749 err = delete_ref(branch_ref_name, repo);
6750 if (err)
6751 goto done;
6753 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6754 if (err)
6755 goto done;
6756 err = delete_ref(commit_ref_name, repo);
6757 if (err)
6758 goto done;
6759 done:
6760 free(tmp_branch_name);
6761 free(base_commit_ref_name);
6762 free(branch_ref_name);
6763 free(commit_ref_name);
6764 return err;
6767 const struct got_error *
6768 got_worktree_histedit_abort(struct got_worktree *worktree,
6769 struct got_fileindex *fileindex, struct got_repository *repo,
6770 struct got_reference *branch, struct got_object_id *base_commit_id,
6771 got_worktree_checkout_cb progress_cb, void *progress_arg)
6773 const struct got_error *err, *unlockerr, *sync_err;
6774 struct got_reference *resolved = NULL;
6775 char *fileindex_path = NULL;
6776 struct got_object_id *tree_id = NULL;
6777 struct revert_file_args rfa;
6779 err = lock_worktree(worktree, LOCK_EX);
6780 if (err)
6781 return err;
6783 err = got_ref_open(&resolved, repo,
6784 got_ref_get_symref_target(branch), 0);
6785 if (err)
6786 goto done;
6788 err = got_worktree_set_head_ref(worktree, resolved);
6789 if (err)
6790 goto done;
6792 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6793 if (err)
6794 goto done;
6796 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6797 worktree->path_prefix);
6798 if (err)
6799 goto done;
6801 err = delete_histedit_refs(worktree, repo);
6802 if (err)
6803 goto done;
6805 err = get_fileindex_path(&fileindex_path, worktree);
6806 if (err)
6807 goto done;
6809 rfa.worktree = worktree;
6810 rfa.fileindex = fileindex;
6811 rfa.progress_cb = progress_cb;
6812 rfa.progress_arg = progress_arg;
6813 rfa.patch_cb = NULL;
6814 rfa.patch_arg = NULL;
6815 rfa.repo = repo;
6816 err = worktree_status(worktree, "", fileindex, repo,
6817 revert_file, &rfa, NULL, NULL, 0, 0);
6818 if (err)
6819 goto sync;
6821 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6822 repo, progress_cb, progress_arg, NULL, NULL);
6823 sync:
6824 sync_err = sync_fileindex(fileindex, fileindex_path);
6825 if (sync_err && err == NULL)
6826 err = sync_err;
6827 done:
6828 got_ref_close(resolved);
6829 free(tree_id);
6830 free(fileindex_path);
6832 unlockerr = lock_worktree(worktree, LOCK_SH);
6833 if (unlockerr && err == NULL)
6834 err = unlockerr;
6835 return err;
6838 const struct got_error *
6839 got_worktree_histedit_complete(struct got_worktree *worktree,
6840 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6841 struct got_reference *edited_branch, struct got_repository *repo)
6843 const struct got_error *err, *unlockerr;
6844 struct got_object_id *new_head_commit_id = NULL;
6845 struct got_reference *resolved = NULL;
6847 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6848 if (err)
6849 return err;
6851 err = got_ref_open(&resolved, repo,
6852 got_ref_get_symref_target(edited_branch), 0);
6853 if (err)
6854 goto done;
6856 err = got_ref_change_ref(resolved, new_head_commit_id);
6857 if (err)
6858 goto done;
6860 err = got_ref_write(resolved, repo);
6861 if (err)
6862 goto done;
6864 err = got_worktree_set_head_ref(worktree, resolved);
6865 if (err)
6866 goto done;
6868 err = delete_histedit_refs(worktree, repo);
6869 done:
6870 if (fileindex)
6871 got_fileindex_free(fileindex);
6872 free(new_head_commit_id);
6873 unlockerr = lock_worktree(worktree, LOCK_SH);
6874 if (unlockerr && err == NULL)
6875 err = unlockerr;
6876 return err;
6879 const struct got_error *
6880 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6881 struct got_object_id *commit_id, struct got_repository *repo)
6883 const struct got_error *err;
6884 char *commit_ref_name;
6886 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6887 if (err)
6888 return err;
6890 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6891 if (err)
6892 goto done;
6894 err = delete_ref(commit_ref_name, repo);
6895 done:
6896 free(commit_ref_name);
6897 return err;
6900 const struct got_error *
6901 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6902 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6903 struct got_worktree *worktree, const char *refname,
6904 struct got_repository *repo)
6906 const struct got_error *err = NULL;
6907 char *fileindex_path = NULL;
6908 struct check_rebase_ok_arg ok_arg;
6910 *fileindex = NULL;
6911 *branch_ref = NULL;
6912 *base_branch_ref = NULL;
6914 err = lock_worktree(worktree, LOCK_EX);
6915 if (err)
6916 return err;
6918 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6919 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6920 "cannot integrate a branch into itself; "
6921 "update -b or different branch name required");
6922 goto done;
6925 err = open_fileindex(fileindex, &fileindex_path, worktree);
6926 if (err)
6927 goto done;
6929 /* Preconditions are the same as for rebase. */
6930 ok_arg.worktree = worktree;
6931 ok_arg.repo = repo;
6932 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6933 &ok_arg);
6934 if (err)
6935 goto done;
6937 err = got_ref_open(branch_ref, repo, refname, 1);
6938 if (err)
6939 goto done;
6941 err = got_ref_open(base_branch_ref, repo,
6942 got_worktree_get_head_ref_name(worktree), 1);
6943 done:
6944 if (err) {
6945 if (*branch_ref) {
6946 got_ref_close(*branch_ref);
6947 *branch_ref = NULL;
6949 if (*base_branch_ref) {
6950 got_ref_close(*base_branch_ref);
6951 *base_branch_ref = NULL;
6953 if (*fileindex) {
6954 got_fileindex_free(*fileindex);
6955 *fileindex = NULL;
6957 lock_worktree(worktree, LOCK_SH);
6959 return err;
6962 const struct got_error *
6963 got_worktree_integrate_continue(struct got_worktree *worktree,
6964 struct got_fileindex *fileindex, struct got_repository *repo,
6965 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6966 got_worktree_checkout_cb progress_cb, void *progress_arg,
6967 got_cancel_cb cancel_cb, void *cancel_arg)
6969 const struct got_error *err = NULL, *sync_err, *unlockerr;
6970 char *fileindex_path = NULL;
6971 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6973 err = get_fileindex_path(&fileindex_path, worktree);
6974 if (err)
6975 goto done;
6977 err = got_ref_resolve(&commit_id, repo, branch_ref);
6978 if (err)
6979 goto done;
6981 err = got_object_id_by_path(&tree_id, repo, commit_id,
6982 worktree->path_prefix);
6983 if (err)
6984 goto done;
6986 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6987 if (err)
6988 goto done;
6990 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6991 progress_cb, progress_arg, cancel_cb, cancel_arg);
6992 if (err)
6993 goto sync;
6995 err = got_ref_change_ref(base_branch_ref, commit_id);
6996 if (err)
6997 goto sync;
6999 err = got_ref_write(base_branch_ref, repo);
7000 sync:
7001 sync_err = sync_fileindex(fileindex, fileindex_path);
7002 if (sync_err && err == NULL)
7003 err = sync_err;
7005 done:
7006 unlockerr = got_ref_unlock(branch_ref);
7007 if (unlockerr && err == NULL)
7008 err = unlockerr;
7009 got_ref_close(branch_ref);
7011 unlockerr = got_ref_unlock(base_branch_ref);
7012 if (unlockerr && err == NULL)
7013 err = unlockerr;
7014 got_ref_close(base_branch_ref);
7016 got_fileindex_free(fileindex);
7017 free(fileindex_path);
7018 free(tree_id);
7020 unlockerr = lock_worktree(worktree, LOCK_SH);
7021 if (unlockerr && err == NULL)
7022 err = unlockerr;
7023 return err;
7026 const struct got_error *
7027 got_worktree_integrate_abort(struct got_worktree *worktree,
7028 struct got_fileindex *fileindex, struct got_repository *repo,
7029 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7031 const struct got_error *err = NULL, *unlockerr = NULL;
7033 got_fileindex_free(fileindex);
7035 err = lock_worktree(worktree, LOCK_SH);
7037 unlockerr = got_ref_unlock(branch_ref);
7038 if (unlockerr && err == NULL)
7039 err = unlockerr;
7040 got_ref_close(branch_ref);
7042 unlockerr = got_ref_unlock(base_branch_ref);
7043 if (unlockerr && err == NULL)
7044 err = unlockerr;
7045 got_ref_close(base_branch_ref);
7047 return err;
7050 struct check_stage_ok_arg {
7051 struct got_object_id *head_commit_id;
7052 struct got_worktree *worktree;
7053 struct got_fileindex *fileindex;
7054 struct got_repository *repo;
7055 int have_changes;
7058 const struct got_error *
7059 check_stage_ok(void *arg, unsigned char status,
7060 unsigned char staged_status, const char *relpath,
7061 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7062 struct got_object_id *commit_id, int dirfd, const char *de_name)
7064 struct check_stage_ok_arg *a = arg;
7065 const struct got_error *err = NULL;
7066 struct got_fileindex_entry *ie;
7067 struct got_object_id base_commit_id;
7068 struct got_object_id *base_commit_idp = NULL;
7069 char *in_repo_path = NULL, *p;
7071 if (status == GOT_STATUS_UNVERSIONED ||
7072 status == GOT_STATUS_NO_CHANGE)
7073 return NULL;
7074 if (status == GOT_STATUS_NONEXISTENT)
7075 return got_error_set_errno(ENOENT, relpath);
7077 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7078 if (ie == NULL)
7079 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7081 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7082 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7083 relpath) == -1)
7084 return got_error_from_errno("asprintf");
7086 if (got_fileindex_entry_has_commit(ie)) {
7087 memcpy(base_commit_id.sha1, ie->commit_sha1,
7088 SHA1_DIGEST_LENGTH);
7089 base_commit_idp = &base_commit_id;
7092 if (status == GOT_STATUS_CONFLICT) {
7093 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7094 goto done;
7095 } else if (status != GOT_STATUS_ADD &&
7096 status != GOT_STATUS_MODIFY &&
7097 status != GOT_STATUS_DELETE) {
7098 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7099 goto done;
7102 a->have_changes = 1;
7104 p = in_repo_path;
7105 while (p[0] == '/')
7106 p++;
7107 err = check_out_of_date(p, status, staged_status,
7108 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7109 GOT_ERR_STAGE_OUT_OF_DATE);
7110 done:
7111 free(in_repo_path);
7112 return err;
7115 struct stage_path_arg {
7116 struct got_worktree *worktree;
7117 struct got_fileindex *fileindex;
7118 struct got_repository *repo;
7119 got_worktree_status_cb status_cb;
7120 void *status_arg;
7121 got_worktree_patch_cb patch_cb;
7122 void *patch_arg;
7123 int staged_something;
7124 int allow_bad_symlinks;
7127 static const struct got_error *
7128 stage_path(void *arg, unsigned char status,
7129 unsigned char staged_status, const char *relpath,
7130 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7131 struct got_object_id *commit_id, int dirfd, const char *de_name)
7133 struct stage_path_arg *a = arg;
7134 const struct got_error *err = NULL;
7135 struct got_fileindex_entry *ie;
7136 char *ondisk_path = NULL, *path_content = NULL;
7137 uint32_t stage;
7138 struct got_object_id *new_staged_blob_id = NULL;
7139 struct stat sb;
7141 if (status == GOT_STATUS_UNVERSIONED)
7142 return NULL;
7144 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7145 if (ie == NULL)
7146 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7148 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7149 relpath)== -1)
7150 return got_error_from_errno("asprintf");
7152 switch (status) {
7153 case GOT_STATUS_ADD:
7154 case GOT_STATUS_MODIFY:
7155 /* XXX could sb.st_mode be passed in by our caller? */
7156 if (lstat(ondisk_path, &sb) == -1) {
7157 err = got_error_from_errno2("lstat", ondisk_path);
7158 break;
7160 if (a->patch_cb) {
7161 if (status == GOT_STATUS_ADD) {
7162 int choice = GOT_PATCH_CHOICE_NONE;
7163 err = (*a->patch_cb)(&choice, a->patch_arg,
7164 status, ie->path, NULL, 1, 1);
7165 if (err)
7166 break;
7167 if (choice != GOT_PATCH_CHOICE_YES)
7168 break;
7169 } else {
7170 err = create_patched_content(&path_content, 0,
7171 staged_blob_id ? staged_blob_id : blob_id,
7172 ondisk_path, dirfd, de_name, ie->path,
7173 a->repo, a->patch_cb, a->patch_arg);
7174 if (err || path_content == NULL)
7175 break;
7178 err = got_object_blob_create(&new_staged_blob_id,
7179 path_content ? path_content : ondisk_path, a->repo);
7180 if (err)
7181 break;
7182 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7183 SHA1_DIGEST_LENGTH);
7184 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7185 stage = GOT_FILEIDX_STAGE_ADD;
7186 else
7187 stage = GOT_FILEIDX_STAGE_MODIFY;
7188 got_fileindex_entry_stage_set(ie, stage);
7189 if (S_ISLNK(sb.st_mode)) {
7190 int is_bad_symlink = 0;
7191 if (!a->allow_bad_symlinks) {
7192 char target_path[PATH_MAX];
7193 ssize_t target_len;
7194 target_len = readlink(ondisk_path, target_path,
7195 sizeof(target_path));
7196 if (target_len == -1) {
7197 err = got_error_from_errno2("readlink",
7198 ondisk_path);
7199 break;
7201 err = is_bad_symlink_target(&is_bad_symlink,
7202 target_path, target_len, ondisk_path,
7203 a->worktree->root_path);
7204 if (err)
7205 break;
7206 if (is_bad_symlink) {
7207 err = got_error_path(ondisk_path,
7208 GOT_ERR_BAD_SYMLINK);
7209 break;
7212 if (is_bad_symlink)
7213 got_fileindex_entry_staged_filetype_set(ie,
7214 GOT_FILEIDX_MODE_BAD_SYMLINK);
7215 else
7216 got_fileindex_entry_staged_filetype_set(ie,
7217 GOT_FILEIDX_MODE_SYMLINK);
7218 } else {
7219 got_fileindex_entry_staged_filetype_set(ie,
7220 GOT_FILEIDX_MODE_REGULAR_FILE);
7222 a->staged_something = 1;
7223 if (a->status_cb == NULL)
7224 break;
7225 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7226 get_staged_status(ie), relpath, blob_id,
7227 new_staged_blob_id, NULL, dirfd, de_name);
7228 break;
7229 case GOT_STATUS_DELETE:
7230 if (staged_status == GOT_STATUS_DELETE)
7231 break;
7232 if (a->patch_cb) {
7233 int choice = GOT_PATCH_CHOICE_NONE;
7234 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7235 ie->path, NULL, 1, 1);
7236 if (err)
7237 break;
7238 if (choice == GOT_PATCH_CHOICE_NO)
7239 break;
7240 if (choice != GOT_PATCH_CHOICE_YES) {
7241 err = got_error(GOT_ERR_PATCH_CHOICE);
7242 break;
7245 stage = GOT_FILEIDX_STAGE_DELETE;
7246 got_fileindex_entry_stage_set(ie, stage);
7247 a->staged_something = 1;
7248 if (a->status_cb == NULL)
7249 break;
7250 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7251 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7252 de_name);
7253 break;
7254 case GOT_STATUS_NO_CHANGE:
7255 break;
7256 case GOT_STATUS_CONFLICT:
7257 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7258 break;
7259 case GOT_STATUS_NONEXISTENT:
7260 err = got_error_set_errno(ENOENT, relpath);
7261 break;
7262 default:
7263 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7264 break;
7267 if (path_content && unlink(path_content) == -1 && err == NULL)
7268 err = got_error_from_errno2("unlink", path_content);
7269 free(path_content);
7270 free(ondisk_path);
7271 free(new_staged_blob_id);
7272 return err;
7275 const struct got_error *
7276 got_worktree_stage(struct got_worktree *worktree,
7277 struct got_pathlist_head *paths,
7278 got_worktree_status_cb status_cb, void *status_arg,
7279 got_worktree_patch_cb patch_cb, void *patch_arg,
7280 int allow_bad_symlinks, struct got_repository *repo)
7282 const struct got_error *err = NULL, *sync_err, *unlockerr;
7283 struct got_pathlist_entry *pe;
7284 struct got_fileindex *fileindex = NULL;
7285 char *fileindex_path = NULL;
7286 struct got_reference *head_ref = NULL;
7287 struct got_object_id *head_commit_id = NULL;
7288 struct check_stage_ok_arg oka;
7289 struct stage_path_arg spa;
7291 err = lock_worktree(worktree, LOCK_EX);
7292 if (err)
7293 return err;
7295 err = got_ref_open(&head_ref, repo,
7296 got_worktree_get_head_ref_name(worktree), 0);
7297 if (err)
7298 goto done;
7299 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7300 if (err)
7301 goto done;
7302 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7303 if (err)
7304 goto done;
7306 /* Check pre-conditions before staging anything. */
7307 oka.head_commit_id = head_commit_id;
7308 oka.worktree = worktree;
7309 oka.fileindex = fileindex;
7310 oka.repo = repo;
7311 oka.have_changes = 0;
7312 TAILQ_FOREACH(pe, paths, entry) {
7313 err = worktree_status(worktree, pe->path, fileindex, repo,
7314 check_stage_ok, &oka, NULL, NULL, 0, 0);
7315 if (err)
7316 goto done;
7318 if (!oka.have_changes) {
7319 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7320 goto done;
7323 spa.worktree = worktree;
7324 spa.fileindex = fileindex;
7325 spa.repo = repo;
7326 spa.patch_cb = patch_cb;
7327 spa.patch_arg = patch_arg;
7328 spa.status_cb = status_cb;
7329 spa.status_arg = status_arg;
7330 spa.staged_something = 0;
7331 spa.allow_bad_symlinks = allow_bad_symlinks;
7332 TAILQ_FOREACH(pe, paths, entry) {
7333 err = worktree_status(worktree, pe->path, fileindex, repo,
7334 stage_path, &spa, NULL, NULL, 0, 0);
7335 if (err)
7336 goto done;
7338 if (!spa.staged_something) {
7339 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7340 goto done;
7343 sync_err = sync_fileindex(fileindex, fileindex_path);
7344 if (sync_err && err == NULL)
7345 err = sync_err;
7346 done:
7347 if (head_ref)
7348 got_ref_close(head_ref);
7349 free(head_commit_id);
7350 free(fileindex_path);
7351 if (fileindex)
7352 got_fileindex_free(fileindex);
7353 unlockerr = lock_worktree(worktree, LOCK_SH);
7354 if (unlockerr && err == NULL)
7355 err = unlockerr;
7356 return err;
7359 struct unstage_path_arg {
7360 struct got_worktree *worktree;
7361 struct got_fileindex *fileindex;
7362 struct got_repository *repo;
7363 got_worktree_checkout_cb progress_cb;
7364 void *progress_arg;
7365 got_worktree_patch_cb patch_cb;
7366 void *patch_arg;
7369 static const struct got_error *
7370 create_unstaged_content(char **path_unstaged_content,
7371 char **path_new_staged_content, struct got_object_id *blob_id,
7372 struct got_object_id *staged_blob_id, const char *relpath,
7373 struct got_repository *repo,
7374 got_worktree_patch_cb patch_cb, void *patch_arg)
7376 const struct got_error *err;
7377 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7378 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7379 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7380 struct stat sb1, sb2;
7381 struct got_diff_changes *changes = NULL;
7382 struct got_diff_state *ds = NULL;
7383 struct got_diff_args *args = NULL;
7384 struct got_diff_change *change;
7385 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7386 int have_content = 0, have_rejected_content = 0;
7388 *path_unstaged_content = NULL;
7389 *path_new_staged_content = NULL;
7391 err = got_object_id_str(&label1, blob_id);
7392 if (err)
7393 return err;
7394 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7395 if (err)
7396 goto done;
7398 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7399 if (err)
7400 goto done;
7402 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7403 if (err)
7404 goto done;
7406 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7407 if (err)
7408 goto done;
7410 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7411 if (err)
7412 goto done;
7414 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7415 if (err)
7416 goto done;
7418 if (stat(path1, &sb1) == -1) {
7419 err = got_error_from_errno2("stat", path1);
7420 goto done;
7423 if (stat(path2, &sb2) == -1) {
7424 err = got_error_from_errno2("stat", path2);
7425 goto done;
7428 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7429 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7430 if (err)
7431 goto done;
7433 err = got_opentemp_named(path_unstaged_content, &outfile,
7434 "got-unstaged-content");
7435 if (err)
7436 goto done;
7437 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7438 "got-new-staged-content");
7439 if (err)
7440 goto done;
7442 if (fseek(f1, 0L, SEEK_SET) == -1) {
7443 err = got_ferror(f1, GOT_ERR_IO);
7444 goto done;
7446 if (fseek(f2, 0L, SEEK_SET) == -1) {
7447 err = got_ferror(f2, GOT_ERR_IO);
7448 goto done;
7450 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7451 int choice;
7452 err = apply_or_reject_change(&choice, change, ++n,
7453 changes->nchanges, ds, args, diff_flags, relpath,
7454 f1, f2, &line_cur1, &line_cur2,
7455 outfile, rejectfile, patch_cb, patch_arg);
7456 if (err)
7457 goto done;
7458 if (choice == GOT_PATCH_CHOICE_YES)
7459 have_content = 1;
7460 else
7461 have_rejected_content = 1;
7462 if (choice == GOT_PATCH_CHOICE_QUIT)
7463 break;
7465 if (have_content || have_rejected_content)
7466 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7467 outfile, rejectfile);
7468 done:
7469 free(label1);
7470 if (blob)
7471 got_object_blob_close(blob);
7472 if (staged_blob)
7473 got_object_blob_close(staged_blob);
7474 if (f1 && fclose(f1) == EOF && err == NULL)
7475 err = got_error_from_errno2("fclose", path1);
7476 if (f2 && fclose(f2) == EOF && err == NULL)
7477 err = got_error_from_errno2("fclose", path2);
7478 if (outfile && fclose(outfile) == EOF && err == NULL)
7479 err = got_error_from_errno2("fclose", *path_unstaged_content);
7480 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7481 err = got_error_from_errno2("fclose", *path_new_staged_content);
7482 if (path1 && unlink(path1) == -1 && err == NULL)
7483 err = got_error_from_errno2("unlink", path1);
7484 if (path2 && unlink(path2) == -1 && err == NULL)
7485 err = got_error_from_errno2("unlink", path2);
7486 if (err || !have_content) {
7487 if (*path_unstaged_content &&
7488 unlink(*path_unstaged_content) == -1 && err == NULL)
7489 err = got_error_from_errno2("unlink",
7490 *path_unstaged_content);
7491 free(*path_unstaged_content);
7492 *path_unstaged_content = NULL;
7494 if (err || !have_content || !have_rejected_content) {
7495 if (*path_new_staged_content &&
7496 unlink(*path_new_staged_content) == -1 && err == NULL)
7497 err = got_error_from_errno2("unlink",
7498 *path_new_staged_content);
7499 free(*path_new_staged_content);
7500 *path_new_staged_content = NULL;
7502 free(args);
7503 if (ds) {
7504 got_diff_state_free(ds);
7505 free(ds);
7507 if (changes)
7508 got_diff_free_changes(changes);
7509 free(path1);
7510 free(path2);
7511 return err;
7514 static const struct got_error *
7515 unstage_hunks(struct got_object_id *staged_blob_id,
7516 struct got_blob_object *blob_base,
7517 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7518 const char *ondisk_path, const char *label_orig,
7519 struct got_worktree *worktree, struct got_repository *repo,
7520 got_worktree_patch_cb patch_cb, void *patch_arg,
7521 got_worktree_checkout_cb progress_cb, void *progress_arg)
7523 const struct got_error *err = NULL;
7524 char *path_unstaged_content = NULL;
7525 char *path_new_staged_content = NULL;
7526 struct got_object_id *new_staged_blob_id = NULL;
7527 FILE *f = NULL;
7528 struct stat sb;
7530 err = create_unstaged_content(&path_unstaged_content,
7531 &path_new_staged_content, blob_id, staged_blob_id,
7532 ie->path, repo, patch_cb, patch_arg);
7533 if (err)
7534 return err;
7536 if (path_unstaged_content == NULL)
7537 return NULL;
7539 if (path_new_staged_content) {
7540 err = got_object_blob_create(&new_staged_blob_id,
7541 path_new_staged_content, repo);
7542 if (err)
7543 goto done;
7546 f = fopen(path_unstaged_content, "r");
7547 if (f == NULL) {
7548 err = got_error_from_errno2("fopen",
7549 path_unstaged_content);
7550 goto done;
7552 if (fstat(fileno(f), &sb) == -1) {
7553 err = got_error_from_errno2("fstat", path_unstaged_content);
7554 goto done;
7556 if (got_fileindex_entry_staged_filetype_get(ie) ==
7557 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7558 char link_target[PATH_MAX];
7559 size_t r;
7560 r = fread(link_target, 1, sizeof(link_target), f);
7561 if (r == 0 && ferror(f)) {
7562 err = got_error_from_errno("fread");
7563 goto done;
7565 if (r >= sizeof(link_target)) { /* should not happen */
7566 err = got_error(GOT_ERR_NO_SPACE);
7567 goto done;
7569 link_target[r] = '\0';
7570 err = merge_symlink(worktree, blob_base,
7571 ondisk_path, ie->path, label_orig, link_target,
7572 worktree->base_commit_id, repo, progress_cb,
7573 progress_arg);
7574 } else {
7575 int local_changes_subsumed;
7576 err = merge_file(&local_changes_subsumed, worktree,
7577 blob_base, ondisk_path, ie->path,
7578 got_fileindex_perms_to_st(ie),
7579 path_unstaged_content, label_orig, "unstaged",
7580 repo, progress_cb, progress_arg);
7582 if (err)
7583 goto done;
7585 if (new_staged_blob_id) {
7586 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7587 SHA1_DIGEST_LENGTH);
7588 } else
7589 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7590 done:
7591 free(new_staged_blob_id);
7592 if (path_unstaged_content &&
7593 unlink(path_unstaged_content) == -1 && err == NULL)
7594 err = got_error_from_errno2("unlink", path_unstaged_content);
7595 if (path_new_staged_content &&
7596 unlink(path_new_staged_content) == -1 && err == NULL)
7597 err = got_error_from_errno2("unlink", path_new_staged_content);
7598 if (f && fclose(f) != 0 && err == NULL)
7599 err = got_error_from_errno2("fclose", path_unstaged_content);
7600 free(path_unstaged_content);
7601 free(path_new_staged_content);
7602 return err;
7605 static const struct got_error *
7606 unstage_path(void *arg, unsigned char status,
7607 unsigned char staged_status, const char *relpath,
7608 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7609 struct got_object_id *commit_id, int dirfd, const char *de_name)
7611 const struct got_error *err = NULL;
7612 struct unstage_path_arg *a = arg;
7613 struct got_fileindex_entry *ie;
7614 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7615 char *ondisk_path = NULL;
7616 char *id_str = NULL, *label_orig = NULL;
7617 int local_changes_subsumed;
7618 struct stat sb;
7620 if (staged_status != GOT_STATUS_ADD &&
7621 staged_status != GOT_STATUS_MODIFY &&
7622 staged_status != GOT_STATUS_DELETE)
7623 return NULL;
7625 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7626 if (ie == NULL)
7627 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7629 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7630 == -1)
7631 return got_error_from_errno("asprintf");
7633 err = got_object_id_str(&id_str,
7634 commit_id ? commit_id : a->worktree->base_commit_id);
7635 if (err)
7636 goto done;
7637 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7638 id_str) == -1) {
7639 err = got_error_from_errno("asprintf");
7640 goto done;
7643 switch (staged_status) {
7644 case GOT_STATUS_MODIFY:
7645 err = got_object_open_as_blob(&blob_base, a->repo,
7646 blob_id, 8192);
7647 if (err)
7648 break;
7649 /* fall through */
7650 case GOT_STATUS_ADD:
7651 if (a->patch_cb) {
7652 if (staged_status == GOT_STATUS_ADD) {
7653 int choice = GOT_PATCH_CHOICE_NONE;
7654 err = (*a->patch_cb)(&choice, a->patch_arg,
7655 staged_status, ie->path, NULL, 1, 1);
7656 if (err)
7657 break;
7658 if (choice != GOT_PATCH_CHOICE_YES)
7659 break;
7660 } else {
7661 err = unstage_hunks(staged_blob_id,
7662 blob_base, blob_id, ie, ondisk_path,
7663 label_orig, a->worktree, a->repo,
7664 a->patch_cb, a->patch_arg,
7665 a->progress_cb, a->progress_arg);
7666 break; /* Done with this file. */
7669 err = got_object_open_as_blob(&blob_staged, a->repo,
7670 staged_blob_id, 8192);
7671 if (err)
7672 break;
7673 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7674 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7675 case GOT_FILEIDX_MODE_REGULAR_FILE:
7676 err = merge_blob(&local_changes_subsumed, a->worktree,
7677 blob_base, ondisk_path, relpath,
7678 got_fileindex_perms_to_st(ie), label_orig,
7679 blob_staged, commit_id ? commit_id :
7680 a->worktree->base_commit_id, a->repo,
7681 a->progress_cb, a->progress_arg);
7682 break;
7683 case GOT_FILEIDX_MODE_SYMLINK:
7684 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7685 char *staged_target;
7686 err = got_object_blob_read_to_str(
7687 &staged_target, blob_staged);
7688 if (err)
7689 goto done;
7690 err = merge_symlink(a->worktree, blob_base,
7691 ondisk_path, relpath, label_orig,
7692 staged_target, commit_id ? commit_id :
7693 a->worktree->base_commit_id,
7694 a->repo, a->progress_cb, a->progress_arg);
7695 free(staged_target);
7696 } else {
7697 err = merge_blob(&local_changes_subsumed,
7698 a->worktree, blob_base, ondisk_path,
7699 relpath, got_fileindex_perms_to_st(ie),
7700 label_orig, blob_staged,
7701 commit_id ? commit_id :
7702 a->worktree->base_commit_id, a->repo,
7703 a->progress_cb, a->progress_arg);
7705 break;
7706 default:
7707 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7708 break;
7710 if (err == NULL)
7711 got_fileindex_entry_stage_set(ie,
7712 GOT_FILEIDX_STAGE_NONE);
7713 break;
7714 case GOT_STATUS_DELETE:
7715 if (a->patch_cb) {
7716 int choice = GOT_PATCH_CHOICE_NONE;
7717 err = (*a->patch_cb)(&choice, a->patch_arg,
7718 staged_status, ie->path, NULL, 1, 1);
7719 if (err)
7720 break;
7721 if (choice == GOT_PATCH_CHOICE_NO)
7722 break;
7723 if (choice != GOT_PATCH_CHOICE_YES) {
7724 err = got_error(GOT_ERR_PATCH_CHOICE);
7725 break;
7728 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7729 err = get_file_status(&status, &sb, ie, ondisk_path,
7730 dirfd, de_name, a->repo);
7731 if (err)
7732 break;
7733 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7734 break;
7736 done:
7737 free(ondisk_path);
7738 if (blob_base)
7739 got_object_blob_close(blob_base);
7740 if (blob_staged)
7741 got_object_blob_close(blob_staged);
7742 free(id_str);
7743 free(label_orig);
7744 return err;
7747 const struct got_error *
7748 got_worktree_unstage(struct got_worktree *worktree,
7749 struct got_pathlist_head *paths,
7750 got_worktree_checkout_cb progress_cb, void *progress_arg,
7751 got_worktree_patch_cb patch_cb, void *patch_arg,
7752 struct got_repository *repo)
7754 const struct got_error *err = NULL, *sync_err, *unlockerr;
7755 struct got_pathlist_entry *pe;
7756 struct got_fileindex *fileindex = NULL;
7757 char *fileindex_path = NULL;
7758 struct unstage_path_arg upa;
7760 err = lock_worktree(worktree, LOCK_EX);
7761 if (err)
7762 return err;
7764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7765 if (err)
7766 goto done;
7768 upa.worktree = worktree;
7769 upa.fileindex = fileindex;
7770 upa.repo = repo;
7771 upa.progress_cb = progress_cb;
7772 upa.progress_arg = progress_arg;
7773 upa.patch_cb = patch_cb;
7774 upa.patch_arg = patch_arg;
7775 TAILQ_FOREACH(pe, paths, entry) {
7776 err = worktree_status(worktree, pe->path, fileindex, repo,
7777 unstage_path, &upa, NULL, NULL, 0, 0);
7778 if (err)
7779 goto done;
7782 sync_err = sync_fileindex(fileindex, fileindex_path);
7783 if (sync_err && err == NULL)
7784 err = sync_err;
7785 done:
7786 free(fileindex_path);
7787 if (fileindex)
7788 got_fileindex_free(fileindex);
7789 unlockerr = lock_worktree(worktree, LOCK_SH);
7790 if (unlockerr && err == NULL)
7791 err = unlockerr;
7792 return err;
7795 struct report_file_info_arg {
7796 struct got_worktree *worktree;
7797 got_worktree_path_info_cb info_cb;
7798 void *info_arg;
7799 struct got_pathlist_head *paths;
7800 got_cancel_cb cancel_cb;
7801 void *cancel_arg;
7804 static const struct got_error *
7805 report_file_info(void *arg, struct got_fileindex_entry *ie)
7807 struct report_file_info_arg *a = arg;
7808 struct got_pathlist_entry *pe;
7809 struct got_object_id blob_id, staged_blob_id, commit_id;
7810 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7811 struct got_object_id *commit_idp = NULL;
7812 int stage;
7814 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7815 return got_error(GOT_ERR_CANCELLED);
7817 TAILQ_FOREACH(pe, a->paths, entry) {
7818 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7819 got_path_is_child(ie->path, pe->path, pe->path_len))
7820 break;
7822 if (pe == NULL) /* not found */
7823 return NULL;
7825 if (got_fileindex_entry_has_blob(ie)) {
7826 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7827 blob_idp = &blob_id;
7829 stage = got_fileindex_entry_stage_get(ie);
7830 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7831 stage == GOT_FILEIDX_STAGE_ADD) {
7832 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7833 SHA1_DIGEST_LENGTH);
7834 staged_blob_idp = &staged_blob_id;
7837 if (got_fileindex_entry_has_commit(ie)) {
7838 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7839 commit_idp = &commit_id;
7842 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7843 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7846 const struct got_error *
7847 got_worktree_path_info(struct got_worktree *worktree,
7848 struct got_pathlist_head *paths,
7849 got_worktree_path_info_cb info_cb, void *info_arg,
7850 got_cancel_cb cancel_cb, void *cancel_arg)
7853 const struct got_error *err = NULL, *unlockerr;
7854 struct got_fileindex *fileindex = NULL;
7855 char *fileindex_path = NULL;
7856 struct report_file_info_arg arg;
7858 err = lock_worktree(worktree, LOCK_SH);
7859 if (err)
7860 return err;
7862 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7863 if (err)
7864 goto done;
7866 arg.worktree = worktree;
7867 arg.info_cb = info_cb;
7868 arg.info_arg = info_arg;
7869 arg.paths = paths;
7870 arg.cancel_cb = cancel_cb;
7871 arg.cancel_arg = cancel_arg;
7872 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7873 &arg);
7874 done:
7875 free(fileindex_path);
7876 if (fileindex)
7877 got_fileindex_free(fileindex);
7878 unlockerr = lock_worktree(worktree, LOCK_UN);
7879 if (unlockerr && err == NULL)
7880 err = unlockerr;
7881 return err;