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 (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
972 err = got_error_from_errno2("fchmod", path);
973 goto done;
976 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
977 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
978 deriv_target ? deriv_target : "(symlink was deleted)",
979 orig_target ? label_orig : "",
980 orig_target ? "\n" : "",
981 orig_target ? orig_target : "",
982 orig_target ? "\n" : "",
983 GOT_DIFF_CONFLICT_MARKER_SEP,
984 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
985 err = got_error_from_errno2("fprintf", path);
986 goto done;
989 if (unlink(ondisk_path) == -1) {
990 err = got_error_from_errno2("unlink", ondisk_path);
991 goto done;
993 if (rename(path, ondisk_path) == -1) {
994 err = got_error_from_errno3("rename", path, ondisk_path);
995 goto done;
997 done:
998 if (f != NULL && fclose(f) == EOF && err == NULL)
999 err = got_error_from_errno2("fclose", path);
1000 free(path);
1001 free(id_str);
1002 free(label_deriv);
1003 return err;
1006 /* forward declaration */
1007 static const struct got_error *
1008 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1009 const char *, const char *, uint16_t, const char *,
1010 struct got_blob_object *, struct got_object_id *,
1011 struct got_repository *, got_worktree_checkout_cb, void *);
1014 * Merge a symlink into the work tree, where blob_orig acts as the common
1015 * ancestor, deriv_target is the link target of the first derived version,
1016 * and the symlink on disk acts as the second derived version.
1017 * Assume that contents of both blobs represent symlinks.
1019 static const struct got_error *
1020 merge_symlink(struct got_worktree *worktree,
1021 struct got_blob_object *blob_orig, const char *ondisk_path,
1022 const char *path, const char *label_orig, const char *deriv_target,
1023 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1024 got_worktree_checkout_cb progress_cb, void *progress_arg)
1026 const struct got_error *err = NULL;
1027 char *ancestor_target = NULL;
1028 struct stat sb;
1029 ssize_t ondisk_len, deriv_len;
1030 char ondisk_target[PATH_MAX];
1031 int have_local_change = 0;
1032 int have_incoming_change = 0;
1034 if (lstat(ondisk_path, &sb) == -1)
1035 return got_error_from_errno2("lstat", ondisk_path);
1037 ondisk_len = readlink(ondisk_path, ondisk_target,
1038 sizeof(ondisk_target));
1039 if (ondisk_len == -1) {
1040 err = got_error_from_errno2("readlink",
1041 ondisk_path);
1042 goto done;
1044 ondisk_target[ondisk_len] = '\0';
1046 if (blob_orig) {
1047 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1048 if (err)
1049 goto done;
1052 if (ancestor_target == NULL ||
1053 (ondisk_len != strlen(ancestor_target) ||
1054 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1055 have_local_change = 1;
1057 deriv_len = strlen(deriv_target);
1058 if (ancestor_target == NULL ||
1059 (deriv_len != strlen(ancestor_target) ||
1060 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1061 have_incoming_change = 1;
1063 if (!have_local_change && !have_incoming_change) {
1064 if (ancestor_target) {
1065 /* Both sides made the same change. */
1066 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1067 path);
1068 } else if (deriv_len == ondisk_len &&
1069 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1070 /* Both sides added the same symlink. */
1071 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1072 path);
1073 } else {
1074 /* Both sides added symlinks which don't match. */
1075 err = install_symlink_conflict(deriv_target,
1076 deriv_base_commit_id, ancestor_target,
1077 label_orig, ondisk_target, ondisk_path);
1078 if (err)
1079 goto done;
1080 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1081 path);
1083 } else if (!have_local_change && have_incoming_change) {
1084 /* Apply the incoming change. */
1085 err = update_symlink(ondisk_path, deriv_target,
1086 strlen(deriv_target));
1087 if (err)
1088 goto done;
1089 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1090 } else if (have_local_change && have_incoming_change) {
1091 if (deriv_len == ondisk_len &&
1092 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1093 /* Both sides made the same change. */
1094 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1095 path);
1096 } else {
1097 err = install_symlink_conflict(deriv_target,
1098 deriv_base_commit_id, ancestor_target, label_orig,
1099 ondisk_target, ondisk_path);
1100 if (err)
1101 goto done;
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1103 path);
1107 done:
1108 free(ancestor_target);
1109 return err;
1113 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1114 * blob_deriv acts as the first derived version, and the file on disk
1115 * acts as the second derived version.
1117 static const struct got_error *
1118 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1119 struct got_blob_object *blob_orig, const char *ondisk_path,
1120 const char *path, uint16_t st_mode, const char *label_orig,
1121 struct got_blob_object *blob_deriv,
1122 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1123 got_worktree_checkout_cb progress_cb, void *progress_arg)
1125 const struct got_error *err = NULL;
1126 FILE *f_deriv = NULL;
1127 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1128 char *label_deriv = NULL, *parent = NULL;
1130 *local_changes_subsumed = 0;
1132 err = got_path_dirname(&parent, ondisk_path);
1133 if (err)
1134 return err;
1136 free(base_path);
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 base_path = NULL;
1140 goto done;
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 if (err)
1145 goto done;
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 blob_deriv);
1148 if (err)
1149 goto done;
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (err)
1153 goto done;
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1157 goto done;
1160 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1161 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1162 label_deriv, repo, progress_cb, progress_arg);
1163 done:
1164 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1165 err = got_error_from_errno("fclose");
1166 free(base_path);
1167 if (blob_deriv_path) {
1168 unlink(blob_deriv_path);
1169 free(blob_deriv_path);
1171 free(id_str);
1172 free(label_deriv);
1173 free(parent);
1174 return err;
1177 static const struct got_error *
1178 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1179 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1180 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1182 const struct got_error *err = NULL;
1183 struct got_fileindex_entry *new_ie;
1185 *new_iep = NULL;
1187 err = got_fileindex_entry_alloc(&new_ie, path);
1188 if (err)
1189 return err;
1191 err = got_fileindex_entry_update(new_ie, ondisk_path,
1192 blob_id->sha1, base_commit_id->sha1, 1);
1193 if (err)
1194 goto done;
1196 err = got_fileindex_entry_add(fileindex, new_ie);
1197 done:
1198 if (err)
1199 got_fileindex_entry_free(new_ie);
1200 else
1201 *new_iep = new_ie;
1202 return err;
1205 static mode_t
1206 get_ondisk_perms(int executable, mode_t st_mode)
1208 mode_t xbits = S_IXUSR;
1210 if (executable) {
1211 /* Map read bits to execute bits. */
1212 if (st_mode & S_IRGRP)
1213 xbits |= S_IXGRP;
1214 if (st_mode & S_IROTH)
1215 xbits |= S_IXOTH;
1216 return st_mode | xbits;
1219 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1222 /* forward declaration */
1223 static const struct got_error *
1224 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1225 const char *path, mode_t te_mode, mode_t st_mode,
1226 struct got_blob_object *blob, int restoring_missing_file,
1227 int reverting_versioned_file, int installing_bad_symlink,
1228 int path_is_unversioned, struct got_repository *repo,
1229 got_worktree_checkout_cb progress_cb, void *progress_arg);
1232 * This function assumes that the provided symlink target points at a
1233 * safe location in the work tree!
1235 static const struct got_error *
1236 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1237 size_t target_len)
1239 const struct got_error *err = NULL;
1240 ssize_t elen;
1241 char etarget[PATH_MAX];
1242 int fd;
1245 * "Bad" symlinks (those pointing outside the work tree or into the
1246 * .got directory) are installed in the work tree as a regular file
1247 * which contains the bad symlink target path.
1248 * The new symlink target has already been checked for safety by our
1249 * caller. If we can successfully open a regular file then we simply
1250 * replace this file with a symlink below.
1252 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1253 if (fd == -1) {
1254 if (errno != ELOOP)
1255 return got_error_from_errno2("open", ondisk_path);
1257 /* We are updating an existing on-disk symlink. */
1258 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 if (elen == -1)
1260 return got_error_from_errno2("readlink", ondisk_path);
1262 if (elen == target_len &&
1263 memcmp(etarget, target_path, target_len) == 0)
1264 return NULL; /* nothing to do */
1267 err = update_symlink(ondisk_path, target_path, target_len);
1268 if (fd != -1 && close(fd) == -1 && err == NULL)
1269 err = got_error_from_errno2("close", ondisk_path);
1270 return err;
1273 static const struct got_error *
1274 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1275 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1277 const struct got_error *err = NULL;
1278 char canonpath[PATH_MAX];
1279 char *path_got = NULL;
1281 *is_bad_symlink = 0;
1283 if (target_len >= sizeof(canonpath)) {
1284 *is_bad_symlink = 1;
1285 return NULL;
1289 * We do not use realpath(3) to resolve the symlink's target
1290 * path because we don't want to resolve symlinks recursively.
1291 * Instead we make the path absolute and then canonicalize it.
1292 * Relative symlink target lookup should begin at the directory
1293 * in which the blob object is being installed.
1295 if (!got_path_is_absolute(target_path)) {
1296 char *abspath, *parent;
1297 err = got_path_dirname(&parent, ondisk_path);
1298 if (err)
1299 return err;
1300 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1301 free(parent);
1302 return got_error_from_errno("asprintf");
1304 free(parent);
1305 if (strlen(abspath) >= sizeof(canonpath)) {
1306 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1307 free(abspath);
1308 return err;
1310 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1311 free(abspath);
1312 if (err)
1313 return err;
1314 } else {
1315 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1316 if (err)
1317 return err;
1320 /* Only allow symlinks pointing at paths within the work tree. */
1321 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1322 *is_bad_symlink = 1;
1323 return NULL;
1326 /* Do not allow symlinks pointing into the .got directory. */
1327 if (asprintf(&path_got, "%s/%s", wtroot_path,
1328 GOT_WORKTREE_GOT_DIR) == -1)
1329 return got_error_from_errno("asprintf");
1330 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1331 *is_bad_symlink = 1;
1333 free(path_got);
1334 return NULL;
1337 static const struct got_error *
1338 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1339 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1340 int restoring_missing_file, int reverting_versioned_file,
1341 int path_is_unversioned, struct got_repository *repo,
1342 got_worktree_checkout_cb progress_cb, void *progress_arg)
1344 const struct got_error *err = NULL;
1345 char target_path[PATH_MAX];
1346 size_t len, target_len = 0;
1347 char *path_got = NULL;
1348 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1349 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1351 *is_bad_symlink = 0;
1354 * Blob object content specifies the target path of the link.
1355 * If a symbolic link cannot be installed we instead create
1356 * a regular file which contains the link target path stored
1357 * in the blob object.
1359 do {
1360 err = got_object_blob_read_block(&len, blob);
1361 if (len + target_len >= sizeof(target_path)) {
1362 /* Path too long; install as a regular file. */
1363 *is_bad_symlink = 1;
1364 got_object_blob_rewind(blob);
1365 return install_blob(worktree, ondisk_path, path,
1366 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1367 restoring_missing_file, reverting_versioned_file,
1368 1, path_is_unversioned, repo, progress_cb,
1369 progress_arg);
1371 if (len > 0) {
1372 /* Skip blob object header first time around. */
1373 memcpy(target_path + target_len, buf + hdrlen,
1374 len - hdrlen);
1375 target_len += len - hdrlen;
1376 hdrlen = 0;
1378 } while (len != 0);
1379 target_path[target_len] = '\0';
1381 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1382 ondisk_path, worktree->root_path);
1383 if (err)
1384 return err;
1386 if (*is_bad_symlink) {
1387 /* install as a regular file */
1388 *is_bad_symlink = 1;
1389 got_object_blob_rewind(blob);
1390 err = install_blob(worktree, ondisk_path, path,
1391 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1392 restoring_missing_file, reverting_versioned_file, 1,
1393 path_is_unversioned, repo, progress_cb, progress_arg);
1394 goto done;
1397 if (symlink(target_path, ondisk_path) == -1) {
1398 if (errno == EEXIST) {
1399 if (path_is_unversioned) {
1400 err = (*progress_cb)(progress_arg,
1401 GOT_STATUS_UNVERSIONED, path);
1402 goto done;
1404 err = replace_existing_symlink(ondisk_path,
1405 target_path, target_len);
1406 if (err)
1407 goto done;
1408 if (progress_cb) {
1409 err = (*progress_cb)(progress_arg,
1410 reverting_versioned_file ?
1411 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1412 path);
1414 goto done; /* Nothing else to do. */
1417 if (errno == ENOENT) {
1418 char *parent;
1419 err = got_path_dirname(&parent, ondisk_path);
1420 if (err)
1421 goto done;
1422 err = add_dir_on_disk(worktree, parent);
1423 free(parent);
1424 if (err)
1425 goto done;
1427 * Retry, and fall through to error handling
1428 * below if this second attempt fails.
1430 if (symlink(target_path, ondisk_path) != -1) {
1431 err = NULL; /* success */
1432 goto done;
1436 /* Handle errors from first or second creation attempt. */
1437 if (errno == ENAMETOOLONG) {
1438 /* bad target path; install as a regular file */
1439 *is_bad_symlink = 1;
1440 got_object_blob_rewind(blob);
1441 err = install_blob(worktree, ondisk_path, path,
1442 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1443 restoring_missing_file, reverting_versioned_file, 1,
1444 path_is_unversioned, repo,
1445 progress_cb, progress_arg);
1446 } else if (errno == ENOTDIR) {
1447 err = got_error_path(ondisk_path,
1448 GOT_ERR_FILE_OBSTRUCTED);
1449 } else {
1450 err = got_error_from_errno3("symlink",
1451 target_path, ondisk_path);
1453 } else if (progress_cb)
1454 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1455 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1456 done:
1457 free(path_got);
1458 return err;
1461 static const struct got_error *
1462 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1463 const char *path, mode_t te_mode, mode_t st_mode,
1464 struct got_blob_object *blob, int restoring_missing_file,
1465 int reverting_versioned_file, int installing_bad_symlink,
1466 int path_is_unversioned, struct got_repository *repo,
1467 got_worktree_checkout_cb progress_cb, void *progress_arg)
1469 const struct got_error *err = NULL;
1470 int fd = -1;
1471 size_t len, hdrlen;
1472 int update = 0;
1473 char *tmppath = NULL;
1475 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1476 GOT_DEFAULT_FILE_MODE);
1477 if (fd == -1) {
1478 if (errno == ENOENT) {
1479 char *parent;
1480 err = got_path_dirname(&parent, path);
1481 if (err)
1482 return err;
1483 err = add_dir_on_disk(worktree, parent);
1484 free(parent);
1485 if (err)
1486 return err;
1487 fd = open(ondisk_path,
1488 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1489 GOT_DEFAULT_FILE_MODE);
1490 if (fd == -1)
1491 return got_error_from_errno2("open",
1492 ondisk_path);
1493 } else if (errno == EEXIST) {
1494 if (path_is_unversioned) {
1495 err = (*progress_cb)(progress_arg,
1496 GOT_STATUS_UNVERSIONED, path);
1497 goto done;
1499 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1500 /* TODO file is obstructed; do something */
1501 err = got_error_path(ondisk_path,
1502 GOT_ERR_FILE_OBSTRUCTED);
1503 goto done;
1504 } else {
1505 err = got_opentemp_named_fd(&tmppath, &fd,
1506 ondisk_path);
1507 if (err)
1508 goto done;
1509 update = 1;
1511 } else
1512 return got_error_from_errno2("open", ondisk_path);
1515 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1516 err = got_error_from_errno2("fchmod",
1517 update ? tmppath : ondisk_path);
1518 goto done;
1521 if (progress_cb) {
1522 if (restoring_missing_file)
1523 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1524 path);
1525 else if (reverting_versioned_file)
1526 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1527 path);
1528 else
1529 err = (*progress_cb)(progress_arg,
1530 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1531 if (err)
1532 goto done;
1535 hdrlen = got_object_blob_get_hdrlen(blob);
1536 do {
1537 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1538 err = got_object_blob_read_block(&len, blob);
1539 if (err)
1540 break;
1541 if (len > 0) {
1542 /* Skip blob object header first time around. */
1543 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1544 if (outlen == -1) {
1545 err = got_error_from_errno("write");
1546 goto done;
1547 } else if (outlen != len - hdrlen) {
1548 err = got_error(GOT_ERR_IO);
1549 goto done;
1551 hdrlen = 0;
1553 } while (len != 0);
1555 if (fsync(fd) != 0) {
1556 err = got_error_from_errno("fsync");
1557 goto done;
1560 if (update) {
1561 if (rename(tmppath, ondisk_path) != 0) {
1562 err = got_error_from_errno3("rename", tmppath,
1563 ondisk_path);
1564 unlink(tmppath);
1565 goto done;
1569 done:
1570 if (fd != -1 && close(fd) != 0 && err == NULL)
1571 err = got_error_from_errno("close");
1572 free(tmppath);
1573 return err;
1576 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1577 static const struct got_error *
1578 get_modified_file_content_status(unsigned char *status, FILE *f)
1580 const struct got_error *err = NULL;
1581 const char *markers[3] = {
1582 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1583 GOT_DIFF_CONFLICT_MARKER_SEP,
1584 GOT_DIFF_CONFLICT_MARKER_END
1586 int i = 0;
1587 char *line;
1588 size_t len;
1589 const char delim[3] = {'\0', '\0', '\0'};
1591 while (*status == GOT_STATUS_MODIFY) {
1592 line = fparseln(f, &len, NULL, delim, 0);
1593 if (line == NULL) {
1594 if (feof(f))
1595 break;
1596 err = got_ferror(f, GOT_ERR_IO);
1597 break;
1600 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1601 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1602 == 0)
1603 *status = GOT_STATUS_CONFLICT;
1604 else
1605 i++;
1609 return err;
1612 static int
1613 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1615 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1616 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1619 static int
1620 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1622 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1623 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1624 ie->mtime_sec == sb->st_mtim.tv_sec &&
1625 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1626 ie->size == (sb->st_size & 0xffffffff) &&
1627 !xbit_differs(ie, sb->st_mode));
1630 static unsigned char
1631 get_staged_status(struct got_fileindex_entry *ie)
1633 switch (got_fileindex_entry_stage_get(ie)) {
1634 case GOT_FILEIDX_STAGE_ADD:
1635 return GOT_STATUS_ADD;
1636 case GOT_FILEIDX_STAGE_DELETE:
1637 return GOT_STATUS_DELETE;
1638 case GOT_FILEIDX_STAGE_MODIFY:
1639 return GOT_STATUS_MODIFY;
1640 default:
1641 return GOT_STATUS_NO_CHANGE;
1645 static const struct got_error *
1646 get_symlink_modification_status(unsigned char *status,
1647 struct got_fileindex_entry *ie, const char *abspath,
1648 int dirfd, const char *de_name, struct got_blob_object *blob)
1650 const struct got_error *err = NULL;
1651 char target_path[PATH_MAX];
1652 char etarget[PATH_MAX];
1653 ssize_t elen;
1654 size_t len, target_len = 0;
1655 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1656 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1658 *status = GOT_STATUS_NO_CHANGE;
1660 /* Blob object content specifies the target path of the link. */
1661 do {
1662 err = got_object_blob_read_block(&len, blob);
1663 if (err)
1664 return err;
1665 if (len + target_len >= sizeof(target_path)) {
1667 * Should not happen. The blob contents were OK
1668 * when this symlink was installed.
1670 return got_error(GOT_ERR_NO_SPACE);
1672 if (len > 0) {
1673 /* Skip blob object header first time around. */
1674 memcpy(target_path + target_len, buf + hdrlen,
1675 len - hdrlen);
1676 target_len += len - hdrlen;
1677 hdrlen = 0;
1679 } while (len != 0);
1680 target_path[target_len] = '\0';
1682 if (dirfd != -1) {
1683 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1684 if (elen == -1)
1685 return got_error_from_errno2("readlinkat", abspath);
1686 } else {
1687 elen = readlink(abspath, etarget, sizeof(etarget));
1688 if (elen == -1)
1689 return got_error_from_errno2("readlink", abspath);
1692 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1693 *status = GOT_STATUS_MODIFY;
1695 return NULL;
1698 static const struct got_error *
1699 get_file_status(unsigned char *status, struct stat *sb,
1700 struct got_fileindex_entry *ie, const char *abspath,
1701 int dirfd, const char *de_name, struct got_repository *repo)
1703 const struct got_error *err = NULL;
1704 struct got_object_id id;
1705 size_t hdrlen;
1706 int fd = -1;
1707 FILE *f = NULL;
1708 uint8_t fbuf[8192];
1709 struct got_blob_object *blob = NULL;
1710 size_t flen, blen;
1711 unsigned char staged_status = get_staged_status(ie);
1713 *status = GOT_STATUS_NO_CHANGE;
1716 * Whenever the caller provides a directory descriptor and a
1717 * directory entry name for the file, use them! This prevents
1718 * race conditions if filesystem paths change beneath our feet.
1720 if (dirfd != -1) {
1721 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1722 if (errno == ENOENT) {
1723 if (got_fileindex_entry_has_file_on_disk(ie))
1724 *status = GOT_STATUS_MISSING;
1725 else
1726 *status = GOT_STATUS_DELETE;
1727 goto done;
1729 err = got_error_from_errno2("fstatat", abspath);
1730 goto done;
1732 } else {
1733 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1734 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1735 return got_error_from_errno2("open", abspath);
1736 else if (fd == -1 && errno == ELOOP) {
1737 if (lstat(abspath, sb) == -1)
1738 return got_error_from_errno2("lstat", abspath);
1739 } else if (fd == -1 || fstat(fd, sb) == -1) {
1740 if (errno == ENOENT) {
1741 if (got_fileindex_entry_has_file_on_disk(ie))
1742 *status = GOT_STATUS_MISSING;
1743 else
1744 *status = GOT_STATUS_DELETE;
1745 goto done;
1747 err = got_error_from_errno2("fstat", abspath);
1748 goto done;
1752 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1753 *status = GOT_STATUS_OBSTRUCTED;
1754 goto done;
1757 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1758 *status = GOT_STATUS_DELETE;
1759 goto done;
1760 } else if (!got_fileindex_entry_has_blob(ie) &&
1761 staged_status != GOT_STATUS_ADD) {
1762 *status = GOT_STATUS_ADD;
1763 goto done;
1766 if (!stat_info_differs(ie, sb))
1767 goto done;
1769 if (S_ISLNK(sb->st_mode) &&
1770 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1771 *status = GOT_STATUS_MODIFY;
1772 goto done;
1775 if (staged_status == GOT_STATUS_MODIFY ||
1776 staged_status == GOT_STATUS_ADD)
1777 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1778 else
1779 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1781 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1782 if (err)
1783 goto done;
1785 if (S_ISLNK(sb->st_mode)) {
1786 err = get_symlink_modification_status(status, ie,
1787 abspath, dirfd, de_name, blob);
1788 goto done;
1791 if (dirfd != -1) {
1792 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1793 if (fd == -1) {
1794 err = got_error_from_errno2("openat", abspath);
1795 goto done;
1799 f = fdopen(fd, "r");
1800 if (f == NULL) {
1801 err = got_error_from_errno2("fdopen", abspath);
1802 goto done;
1804 fd = -1;
1805 hdrlen = got_object_blob_get_hdrlen(blob);
1806 for (;;) {
1807 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1808 err = got_object_blob_read_block(&blen, blob);
1809 if (err)
1810 goto done;
1811 /* Skip length of blob object header first time around. */
1812 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1813 if (flen == 0 && ferror(f)) {
1814 err = got_error_from_errno("fread");
1815 goto done;
1817 if (blen - hdrlen == 0) {
1818 if (flen != 0)
1819 *status = GOT_STATUS_MODIFY;
1820 break;
1821 } else if (flen == 0) {
1822 if (blen - hdrlen != 0)
1823 *status = GOT_STATUS_MODIFY;
1824 break;
1825 } else if (blen - hdrlen == flen) {
1826 /* Skip blob object header first time around. */
1827 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1828 *status = GOT_STATUS_MODIFY;
1829 break;
1831 } else {
1832 *status = GOT_STATUS_MODIFY;
1833 break;
1835 hdrlen = 0;
1838 if (*status == GOT_STATUS_MODIFY) {
1839 rewind(f);
1840 err = get_modified_file_content_status(status, f);
1841 } else if (xbit_differs(ie, sb->st_mode))
1842 *status = GOT_STATUS_MODE_CHANGE;
1843 done:
1844 if (blob)
1845 got_object_blob_close(blob);
1846 if (f != NULL && fclose(f) == EOF && err == NULL)
1847 err = got_error_from_errno2("fclose", abspath);
1848 if (fd != -1 && close(fd) == -1 && err == NULL)
1849 err = got_error_from_errno2("close", abspath);
1850 return err;
1854 * Update timestamps in the file index if a file is unmodified and
1855 * we had to run a full content comparison to find out.
1857 static const struct got_error *
1858 sync_timestamps(char *ondisk_path, unsigned char status,
1859 struct got_fileindex_entry *ie, struct stat *sb)
1861 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1862 return got_fileindex_entry_update(ie, ondisk_path,
1863 ie->blob_sha1, ie->commit_sha1, 1);
1865 return NULL;
1868 static const struct got_error *
1869 update_blob(struct got_worktree *worktree,
1870 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1871 struct got_tree_entry *te, const char *path,
1872 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1873 void *progress_arg)
1875 const struct got_error *err = NULL;
1876 struct got_blob_object *blob = NULL;
1877 char *ondisk_path;
1878 unsigned char status = GOT_STATUS_NO_CHANGE;
1879 struct stat sb;
1881 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1882 return got_error_from_errno("asprintf");
1884 if (ie) {
1885 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1886 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1887 goto done;
1889 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1890 repo);
1891 if (err)
1892 goto done;
1893 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1894 sb.st_mode = got_fileindex_perms_to_st(ie);
1895 } else {
1896 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1897 status = GOT_STATUS_UNVERSIONED;
1900 if (status == GOT_STATUS_OBSTRUCTED) {
1901 err = (*progress_cb)(progress_arg, status, path);
1902 goto done;
1904 if (status == GOT_STATUS_CONFLICT) {
1905 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1906 path);
1907 goto done;
1910 if (ie && status != GOT_STATUS_MISSING &&
1911 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1912 if (got_fileindex_entry_has_commit(ie) &&
1913 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1914 SHA1_DIGEST_LENGTH) == 0) {
1915 err = sync_timestamps(ondisk_path, status, ie, &sb);
1916 if (err)
1917 goto done;
1918 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1919 path);
1920 goto done;
1922 if (got_fileindex_entry_has_blob(ie) &&
1923 memcmp(ie->blob_sha1, te->id.sha1,
1924 SHA1_DIGEST_LENGTH) == 0) {
1925 err = sync_timestamps(ondisk_path, status, ie, &sb);
1926 goto done;
1930 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1931 if (err)
1932 goto done;
1934 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1935 int update_timestamps;
1936 struct got_blob_object *blob2 = NULL;
1937 char *label_orig = NULL;
1938 if (got_fileindex_entry_has_blob(ie)) {
1939 struct got_object_id id2;
1940 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1941 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1942 if (err)
1943 goto done;
1945 if (got_fileindex_entry_has_commit(ie)) {
1946 char id_str[SHA1_DIGEST_STRING_LENGTH];
1947 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1948 sizeof(id_str)) == NULL) {
1949 err = got_error_path(id_str,
1950 GOT_ERR_BAD_OBJ_ID_STR);
1951 goto done;
1953 if (asprintf(&label_orig, "%s: commit %s",
1954 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1955 err = got_error_from_errno("asprintf");
1956 goto done;
1959 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1960 char *link_target;
1961 err = got_object_blob_read_to_str(&link_target, blob);
1962 if (err)
1963 goto done;
1964 err = merge_symlink(worktree, blob2, ondisk_path, path,
1965 label_orig, link_target, worktree->base_commit_id,
1966 repo, progress_cb, progress_arg);
1967 free(link_target);
1968 } else {
1969 err = merge_blob(&update_timestamps, worktree, blob2,
1970 ondisk_path, path, sb.st_mode, label_orig, blob,
1971 worktree->base_commit_id, repo,
1972 progress_cb, progress_arg);
1974 free(label_orig);
1975 if (blob2)
1976 got_object_blob_close(blob2);
1977 if (err)
1978 goto done;
1980 * Do not update timestamps of files with local changes.
1981 * Otherwise, a future status walk would treat them as
1982 * unmodified files again.
1984 err = got_fileindex_entry_update(ie, ondisk_path,
1985 blob->id.sha1, worktree->base_commit_id->sha1,
1986 update_timestamps);
1987 } else if (status == GOT_STATUS_MODE_CHANGE) {
1988 err = got_fileindex_entry_update(ie, ondisk_path,
1989 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1990 } else if (status == GOT_STATUS_DELETE) {
1991 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1992 if (err)
1993 goto done;
1994 err = got_fileindex_entry_update(ie, ondisk_path,
1995 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1996 if (err)
1997 goto done;
1998 } else {
1999 int is_bad_symlink = 0;
2000 if (S_ISLNK(te->mode)) {
2001 err = install_symlink(&is_bad_symlink, worktree,
2002 ondisk_path, path, blob,
2003 status == GOT_STATUS_MISSING, 0,
2004 status == GOT_STATUS_UNVERSIONED, repo,
2005 progress_cb, progress_arg);
2006 } else {
2007 err = install_blob(worktree, ondisk_path, path,
2008 te->mode, sb.st_mode, blob,
2009 status == GOT_STATUS_MISSING, 0, 0,
2010 status == GOT_STATUS_UNVERSIONED, repo,
2011 progress_cb, progress_arg);
2013 if (err)
2014 goto done;
2016 if (ie) {
2017 err = got_fileindex_entry_update(ie, ondisk_path,
2018 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2019 } else {
2020 err = create_fileindex_entry(&ie, fileindex,
2021 worktree->base_commit_id, ondisk_path, path,
2022 &blob->id);
2024 if (err)
2025 goto done;
2027 if (is_bad_symlink) {
2028 got_fileindex_entry_filetype_set(ie,
2029 GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 got_object_blob_close(blob);
2033 done:
2034 free(ondisk_path);
2035 return err;
2038 static const struct got_error *
2039 remove_ondisk_file(const char *root_path, const char *path)
2041 const struct got_error *err = NULL;
2042 char *ondisk_path = NULL;
2044 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2045 return got_error_from_errno("asprintf");
2047 if (unlink(ondisk_path) == -1) {
2048 if (errno != ENOENT)
2049 err = got_error_from_errno2("unlink", ondisk_path);
2050 } else {
2051 size_t root_len = strlen(root_path);
2052 do {
2053 char *parent;
2054 err = got_path_dirname(&parent, ondisk_path);
2055 if (err)
2056 break;
2057 free(ondisk_path);
2058 ondisk_path = parent;
2059 if (rmdir(ondisk_path) == -1) {
2060 if (errno != ENOTEMPTY)
2061 err = got_error_from_errno2("rmdir",
2062 ondisk_path);
2063 break;
2065 } while (got_path_cmp(ondisk_path, root_path,
2066 strlen(ondisk_path), root_len) != 0);
2068 free(ondisk_path);
2069 return err;
2072 static const struct got_error *
2073 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2074 struct got_fileindex_entry *ie, struct got_repository *repo,
2075 got_worktree_checkout_cb progress_cb, void *progress_arg)
2077 const struct got_error *err = NULL;
2078 unsigned char status;
2079 struct stat sb;
2080 char *ondisk_path;
2082 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2083 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2085 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2086 == -1)
2087 return got_error_from_errno("asprintf");
2089 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2090 if (err)
2091 goto done;
2093 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2094 char ondisk_target[PATH_MAX];
2095 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2096 sizeof(ondisk_target));
2097 if (ondisk_len == -1) {
2098 err = got_error_from_errno2("readlink", ondisk_path);
2099 goto done;
2101 ondisk_target[ondisk_len] = '\0';
2102 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2103 NULL, NULL, /* XXX pass common ancestor info? */
2104 ondisk_target, ondisk_path);
2105 if (err)
2106 goto done;
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2108 ie->path);
2109 goto done;
2112 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2113 status == GOT_STATUS_ADD) {
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2115 if (err)
2116 goto done;
2118 * Preserve the working file and change the deleted blob's
2119 * entry into a schedule-add entry.
2121 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2122 0);
2123 } else {
2124 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2125 if (err)
2126 goto done;
2127 if (status == GOT_STATUS_NO_CHANGE) {
2128 err = remove_ondisk_file(worktree->root_path, ie->path);
2129 if (err)
2130 goto done;
2132 got_fileindex_entry_remove(fileindex, ie);
2134 done:
2135 free(ondisk_path);
2136 return err;
2139 struct diff_cb_arg {
2140 struct got_fileindex *fileindex;
2141 struct got_worktree *worktree;
2142 struct got_repository *repo;
2143 got_worktree_checkout_cb progress_cb;
2144 void *progress_arg;
2145 got_cancel_cb cancel_cb;
2146 void *cancel_arg;
2149 static const struct got_error *
2150 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2151 struct got_tree_entry *te, const char *parent_path)
2153 struct diff_cb_arg *a = arg;
2155 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2156 return got_error(GOT_ERR_CANCELLED);
2158 return update_blob(a->worktree, a->fileindex, ie, te,
2159 ie->path, a->repo, a->progress_cb, a->progress_arg);
2162 static const struct got_error *
2163 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2165 struct diff_cb_arg *a = arg;
2167 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2168 return got_error(GOT_ERR_CANCELLED);
2170 return delete_blob(a->worktree, a->fileindex, ie,
2171 a->repo, a->progress_cb, a->progress_arg);
2174 static const struct got_error *
2175 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2177 struct diff_cb_arg *a = arg;
2178 const struct got_error *err;
2179 char *path;
2181 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2182 return got_error(GOT_ERR_CANCELLED);
2184 if (got_object_tree_entry_is_submodule(te))
2185 return NULL;
2187 if (asprintf(&path, "%s%s%s", parent_path,
2188 parent_path[0] ? "/" : "", te->name)
2189 == -1)
2190 return got_error_from_errno("asprintf");
2192 if (S_ISDIR(te->mode))
2193 err = add_dir_on_disk(a->worktree, path);
2194 else
2195 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2196 a->repo, a->progress_cb, a->progress_arg);
2198 free(path);
2199 return err;
2202 const struct got_error *
2203 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2205 uint32_t uuid_status;
2207 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2208 if (uuid_status != uuid_s_ok) {
2209 *uuidstr = NULL;
2210 return got_error_uuid(uuid_status, "uuid_to_string");
2213 return NULL;
2216 static const struct got_error *
2217 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2219 const struct got_error *err = NULL;
2220 char *uuidstr = NULL;
2222 *refname = NULL;
2224 err = got_worktree_get_uuid(&uuidstr, worktree);
2225 if (err)
2226 return err;
2228 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2229 err = got_error_from_errno("asprintf");
2230 *refname = NULL;
2232 free(uuidstr);
2233 return err;
2236 const struct got_error *
2237 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2239 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2242 static const struct got_error *
2243 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree,
2246 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2249 static const struct got_error *
2250 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2252 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2290 static const struct got_error *
2291 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 return get_ref_name(refname, worktree,
2294 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2297 const struct got_error *
2298 got_worktree_get_histedit_script_path(char **path,
2299 struct got_worktree *worktree)
2301 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2302 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2303 *path = NULL;
2304 return got_error_from_errno("asprintf");
2306 return NULL;
2310 * Prevent Git's garbage collector from deleting our base commit by
2311 * setting a reference to our base commit's ID.
2313 static const struct got_error *
2314 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2316 const struct got_error *err = NULL;
2317 struct got_reference *ref = NULL;
2318 char *refname;
2320 err = got_worktree_get_base_ref_name(&refname, worktree);
2321 if (err)
2322 return err;
2324 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2325 if (err)
2326 goto done;
2328 err = got_ref_write(ref, repo);
2329 done:
2330 free(refname);
2331 if (ref)
2332 got_ref_close(ref);
2333 return err;
2336 static const struct got_error *
2337 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2339 const struct got_error *err = NULL;
2341 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2342 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2343 err = got_error_from_errno("asprintf");
2344 *fileindex_path = NULL;
2346 return err;
2350 static const struct got_error *
2351 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2352 struct got_worktree *worktree)
2354 const struct got_error *err = NULL;
2355 FILE *index = NULL;
2357 *fileindex_path = NULL;
2358 *fileindex = got_fileindex_alloc();
2359 if (*fileindex == NULL)
2360 return got_error_from_errno("got_fileindex_alloc");
2362 err = get_fileindex_path(fileindex_path, worktree);
2363 if (err)
2364 goto done;
2366 index = fopen(*fileindex_path, "rb");
2367 if (index == NULL) {
2368 if (errno != ENOENT)
2369 err = got_error_from_errno2("fopen", *fileindex_path);
2370 } else {
2371 err = got_fileindex_read(*fileindex, index);
2372 if (fclose(index) != 0 && err == NULL)
2373 err = got_error_from_errno("fclose");
2375 done:
2376 if (err) {
2377 free(*fileindex_path);
2378 *fileindex_path = NULL;
2379 got_fileindex_free(*fileindex);
2380 *fileindex = NULL;
2382 return err;
2385 struct bump_base_commit_id_arg {
2386 struct got_object_id *base_commit_id;
2387 const char *path;
2388 size_t path_len;
2389 const char *entry_name;
2390 got_worktree_checkout_cb progress_cb;
2391 void *progress_arg;
2394 /* Bump base commit ID of all files within an updated part of the work tree. */
2395 static const struct got_error *
2396 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2398 const struct got_error *err;
2399 struct bump_base_commit_id_arg *a = arg;
2401 if (a->entry_name) {
2402 if (strcmp(ie->path, a->path) != 0)
2403 return NULL;
2404 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2405 return NULL;
2407 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2408 SHA1_DIGEST_LENGTH) == 0)
2409 return NULL;
2411 if (a->progress_cb) {
2412 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2413 ie->path);
2414 if (err)
2415 return err;
2417 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2418 return NULL;
2421 static const struct got_error *
2422 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 const struct got_error *err = NULL;
2425 char *new_fileindex_path = NULL;
2426 FILE *new_index = NULL;
2427 struct timespec timeout;
2429 err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 fileindex_path);
2431 if (err)
2432 goto done;
2434 err = got_fileindex_write(fileindex, new_index);
2435 if (err)
2436 goto done;
2438 if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 err = got_error_from_errno3("rename", new_fileindex_path,
2440 fileindex_path);
2441 unlink(new_fileindex_path);
2445 * Sleep for a short amount of time to ensure that files modified after
2446 * this program exits have a different time stamp from the one which
2447 * was recorded in the file index.
2449 timeout.tv_sec = 0;
2450 timeout.tv_nsec = 1;
2451 nanosleep(&timeout, NULL);
2452 done:
2453 if (new_index)
2454 fclose(new_index);
2455 free(new_fileindex_path);
2456 return err;
2459 static const struct got_error *
2460 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 struct got_object_id **tree_id, const char *wt_relpath,
2462 struct got_worktree *worktree, struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct got_object_id *id = NULL;
2466 char *in_repo_path = NULL;
2467 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 *entry_type = GOT_OBJ_TYPE_ANY;
2470 *tree_relpath = NULL;
2471 *tree_id = NULL;
2473 if (wt_relpath[0] == '\0') {
2474 /* Check out all files within the work tree. */
2475 *entry_type = GOT_OBJ_TYPE_TREE;
2476 *tree_relpath = strdup("");
2477 if (*tree_relpath == NULL) {
2478 err = got_error_from_errno("strdup");
2479 goto done;
2481 err = got_object_id_by_path(tree_id, repo,
2482 worktree->base_commit_id, worktree->path_prefix);
2483 if (err)
2484 goto done;
2485 return NULL;
2488 /* Check out a subset of files in the work tree. */
2490 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 err = got_error_from_errno("asprintf");
2493 goto done;
2496 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2497 in_repo_path);
2498 if (err)
2499 goto done;
2501 free(in_repo_path);
2502 in_repo_path = NULL;
2504 err = got_object_get_type(entry_type, repo, id);
2505 if (err)
2506 goto done;
2508 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 /* Check out a single file. */
2510 if (strchr(wt_relpath, '/') == NULL) {
2511 /* Check out a single file in work tree's root dir. */
2512 in_repo_path = strdup(worktree->path_prefix);
2513 if (in_repo_path == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 goto done;
2522 } else {
2523 /* Check out a single file in a subdirectory. */
2524 err = got_path_dirname(tree_relpath, wt_relpath);
2525 if (err)
2526 return err;
2527 if (asprintf(&in_repo_path, "%s%s%s",
2528 worktree->path_prefix, is_root_wt ? "" : "/",
2529 *tree_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2531 goto done;
2534 err = got_object_id_by_path(tree_id, repo,
2535 worktree->base_commit_id, in_repo_path);
2536 } else {
2537 /* Check out all files within a subdirectory. */
2538 *tree_id = got_object_id_dup(id);
2539 if (*tree_id == NULL) {
2540 err = got_error_from_errno("got_object_id_dup");
2541 goto done;
2543 *tree_relpath = strdup(wt_relpath);
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2546 goto done;
2549 done:
2550 free(id);
2551 free(in_repo_path);
2552 if (err) {
2553 *entry_type = GOT_OBJ_TYPE_ANY;
2554 free(*tree_relpath);
2555 *tree_relpath = NULL;
2556 free(*tree_id);
2557 *tree_id = NULL;
2559 return err;
2562 static const struct got_error *
2563 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 const struct got_error *err = NULL;
2569 struct got_commit_object *commit = NULL;
2570 struct got_tree_object *tree = NULL;
2571 struct got_fileindex_diff_tree_cb diff_cb;
2572 struct diff_cb_arg arg;
2574 err = ref_base_commit(worktree, repo);
2575 if (err) {
2576 if (!(err->code == GOT_ERR_ERRNO &&
2577 (errno == EACCES || errno == EROFS)))
2578 goto done;
2579 err = (*progress_cb)(progress_arg,
2580 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2581 if (err)
2582 return err;
2585 err = got_object_open_as_commit(&commit, repo,
2586 worktree->base_commit_id);
2587 if (err)
2588 goto done;
2590 err = got_object_open_as_tree(&tree, repo, tree_id);
2591 if (err)
2592 goto done;
2594 if (entry_name &&
2595 got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 goto done;
2600 diff_cb.diff_old_new = diff_old_new;
2601 diff_cb.diff_old = diff_old;
2602 diff_cb.diff_new = diff_new;
2603 arg.fileindex = fileindex;
2604 arg.worktree = worktree;
2605 arg.repo = repo;
2606 arg.progress_cb = progress_cb;
2607 arg.progress_arg = progress_arg;
2608 arg.cancel_cb = cancel_cb;
2609 arg.cancel_arg = cancel_arg;
2610 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 entry_name, repo, &diff_cb, &arg);
2612 done:
2613 if (tree)
2614 got_object_tree_close(tree);
2615 if (commit)
2616 got_object_commit_close(commit);
2617 return err;
2620 const struct got_error *
2621 got_worktree_checkout_files(struct got_worktree *worktree,
2622 struct got_pathlist_head *paths, struct got_repository *repo,
2623 got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 got_cancel_cb cancel_cb, void *cancel_arg)
2626 const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 struct got_commit_object *commit = NULL;
2628 struct got_tree_object *tree = NULL;
2629 struct got_fileindex *fileindex = NULL;
2630 char *fileindex_path = NULL;
2631 struct got_pathlist_entry *pe;
2632 struct tree_path_data {
2633 SIMPLEQ_ENTRY(tree_path_data) entry;
2634 struct got_object_id *tree_id;
2635 int entry_type;
2636 char *relpath;
2637 char *entry_name;
2638 } *tpd = NULL;
2639 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 SIMPLEQ_INIT(&tree_paths);
2643 err = lock_worktree(worktree, LOCK_EX);
2644 if (err)
2645 return err;
2647 /* Map all specified paths to in-repository trees. */
2648 TAILQ_FOREACH(pe, paths, entry) {
2649 tpd = malloc(sizeof(*tpd));
2650 if (tpd == NULL) {
2651 err = got_error_from_errno("malloc");
2652 goto done;
2655 err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2657 if (err) {
2658 free(tpd);
2659 goto done;
2662 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 err = got_path_basename(&tpd->entry_name, pe->path);
2664 if (err) {
2665 free(tpd->relpath);
2666 free(tpd->tree_id);
2667 free(tpd);
2668 goto done;
2670 } else
2671 tpd->entry_name = NULL;
2673 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 * Read the file index.
2678 * Checking out files is supposed to be an idempotent operation.
2679 * If the on-disk file index is incomplete we will try to complete it.
2681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2682 if (err)
2683 goto done;
2685 tpd = SIMPLEQ_FIRST(&tree_paths);
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 struct bump_base_commit_id_arg bbc_arg;
2689 err = checkout_files(worktree, fileindex, tpd->relpath,
2690 tpd->tree_id, tpd->entry_name, repo,
2691 progress_cb, progress_arg, cancel_cb, cancel_arg);
2692 if (err)
2693 break;
2695 bbc_arg.base_commit_id = worktree->base_commit_id;
2696 bbc_arg.entry_name = tpd->entry_name;
2697 bbc_arg.path = pe->path;
2698 bbc_arg.path_len = pe->path_len;
2699 bbc_arg.progress_cb = progress_cb;
2700 bbc_arg.progress_arg = progress_arg;
2701 err = got_fileindex_for_each_entry_safe(fileindex,
2702 bump_base_commit_id, &bbc_arg);
2703 if (err)
2704 break;
2706 tpd = SIMPLEQ_NEXT(tpd, entry);
2708 sync_err = sync_fileindex(fileindex, fileindex_path);
2709 if (sync_err && err == NULL)
2710 err = sync_err;
2711 done:
2712 free(fileindex_path);
2713 if (tree)
2714 got_object_tree_close(tree);
2715 if (commit)
2716 got_object_commit_close(commit);
2717 if (fileindex)
2718 got_fileindex_free(fileindex);
2719 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2720 tpd = SIMPLEQ_FIRST(&tree_paths);
2721 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2722 free(tpd->relpath);
2723 free(tpd->tree_id);
2724 free(tpd);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2728 err = unlockerr;
2729 return err;
2732 struct merge_file_cb_arg {
2733 struct got_worktree *worktree;
2734 struct got_fileindex *fileindex;
2735 got_worktree_checkout_cb progress_cb;
2736 void *progress_arg;
2737 got_cancel_cb cancel_cb;
2738 void *cancel_arg;
2739 const char *label_orig;
2740 struct got_object_id *commit_id2;
2743 static const struct got_error *
2744 merge_file_cb(void *arg, struct got_blob_object *blob1,
2745 struct got_blob_object *blob2, struct got_object_id *id1,
2746 struct got_object_id *id2, const char *path1, const char *path2,
2747 mode_t mode1, mode_t mode2, struct got_repository *repo)
2749 static const struct got_error *err = NULL;
2750 struct merge_file_cb_arg *a = arg;
2751 struct got_fileindex_entry *ie;
2752 char *ondisk_path = NULL;
2753 struct stat sb;
2754 unsigned char status;
2755 int local_changes_subsumed;
2757 if (blob1 && blob2) {
2758 ie = got_fileindex_entry_get(a->fileindex, path2,
2759 strlen(path2));
2760 if (ie == NULL)
2761 return (*a->progress_cb)(a->progress_arg,
2762 GOT_STATUS_MISSING, path2);
2764 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2765 path2) == -1)
2766 return got_error_from_errno("asprintf");
2768 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2769 repo);
2770 if (err)
2771 goto done;
2773 if (status == GOT_STATUS_DELETE) {
2774 err = (*a->progress_cb)(a->progress_arg,
2775 GOT_STATUS_MERGE, path2);
2776 goto done;
2778 if (status != GOT_STATUS_NO_CHANGE &&
2779 status != GOT_STATUS_MODIFY &&
2780 status != GOT_STATUS_CONFLICT &&
2781 status != GOT_STATUS_ADD) {
2782 err = (*a->progress_cb)(a->progress_arg, status, path2);
2783 goto done;
2786 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2787 char *link_target2;
2788 err = got_object_blob_read_to_str(&link_target2, blob2);
2789 if (err)
2790 goto done;
2791 err = merge_symlink(a->worktree, blob1, ondisk_path,
2792 path2, a->label_orig, link_target2, a->commit_id2,
2793 repo, a->progress_cb, a->progress_arg);
2794 free(link_target2);
2795 } else {
2796 err = merge_blob(&local_changes_subsumed, a->worktree,
2797 blob1, ondisk_path, path2, sb.st_mode,
2798 a->label_orig, blob2, a->commit_id2, repo,
2799 a->progress_cb, a->progress_arg);
2801 } else if (blob1) {
2802 ie = got_fileindex_entry_get(a->fileindex, path1,
2803 strlen(path1));
2804 if (ie == NULL)
2805 return (*a->progress_cb)(a->progress_arg,
2806 GOT_STATUS_MISSING, path1);
2808 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2809 path1) == -1)
2810 return got_error_from_errno("asprintf");
2812 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2813 repo);
2814 if (err)
2815 goto done;
2817 switch (status) {
2818 case GOT_STATUS_NO_CHANGE:
2819 err = (*a->progress_cb)(a->progress_arg,
2820 GOT_STATUS_DELETE, path1);
2821 if (err)
2822 goto done;
2823 err = remove_ondisk_file(a->worktree->root_path, path1);
2824 if (err)
2825 goto done;
2826 if (ie)
2827 got_fileindex_entry_mark_deleted_from_disk(ie);
2828 break;
2829 case GOT_STATUS_DELETE:
2830 case GOT_STATUS_MISSING:
2831 err = (*a->progress_cb)(a->progress_arg,
2832 GOT_STATUS_DELETE, path1);
2833 if (err)
2834 goto done;
2835 if (ie)
2836 got_fileindex_entry_mark_deleted_from_disk(ie);
2837 break;
2838 case GOT_STATUS_ADD: {
2839 struct got_object_id *id;
2840 FILE *blob1_f;
2842 * Delete the added file only if its content already
2843 * exists in the repository.
2845 err = got_object_blob_file_create(&id, &blob1_f, path1);
2846 if (err)
2847 goto done;
2848 if (got_object_id_cmp(id, id1) == 0) {
2849 err = (*a->progress_cb)(a->progress_arg,
2850 GOT_STATUS_DELETE, path1);
2851 if (err)
2852 goto done;
2853 err = remove_ondisk_file(a->worktree->root_path,
2854 path1);
2855 if (err)
2856 goto done;
2857 if (ie)
2858 got_fileindex_entry_remove(a->fileindex,
2859 ie);
2860 } else {
2861 err = (*a->progress_cb)(a->progress_arg,
2862 GOT_STATUS_CANNOT_DELETE, path1);
2864 if (fclose(blob1_f) == EOF && err == NULL)
2865 err = got_error_from_errno("fclose");
2866 free(id);
2867 if (err)
2868 goto done;
2869 break;
2871 case GOT_STATUS_MODIFY:
2872 case GOT_STATUS_CONFLICT:
2873 err = (*a->progress_cb)(a->progress_arg,
2874 GOT_STATUS_CANNOT_DELETE, path1);
2875 if (err)
2876 goto done;
2877 break;
2878 case GOT_STATUS_OBSTRUCTED:
2879 err = (*a->progress_cb)(a->progress_arg, status, path1);
2880 if (err)
2881 goto done;
2882 break;
2883 default:
2884 break;
2886 } else if (blob2) {
2887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 path2) == -1)
2889 return got_error_from_errno("asprintf");
2890 ie = got_fileindex_entry_get(a->fileindex, path2,
2891 strlen(path2));
2892 if (ie) {
2893 err = get_file_status(&status, &sb, ie, ondisk_path,
2894 -1, NULL, repo);
2895 if (err)
2896 goto done;
2897 if (status != GOT_STATUS_NO_CHANGE &&
2898 status != GOT_STATUS_MODIFY &&
2899 status != GOT_STATUS_CONFLICT &&
2900 status != GOT_STATUS_ADD) {
2901 err = (*a->progress_cb)(a->progress_arg,
2902 status, path2);
2903 goto done;
2905 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2906 char *link_target2;
2907 err = got_object_blob_read_to_str(&link_target2,
2908 blob2);
2909 if (err)
2910 goto done;
2911 err = merge_symlink(a->worktree, NULL,
2912 ondisk_path, path2, a->label_orig,
2913 link_target2, a->commit_id2, repo,
2914 a->progress_cb, a->progress_arg);
2915 free(link_target2);
2916 } else if (S_ISREG(sb.st_mode)) {
2917 err = merge_blob(&local_changes_subsumed,
2918 a->worktree, NULL, ondisk_path, path2,
2919 sb.st_mode, a->label_orig, blob2,
2920 a->commit_id2, repo, a->progress_cb,
2921 a->progress_arg);
2922 } else {
2923 err = got_error_path(ondisk_path,
2924 GOT_ERR_FILE_OBSTRUCTED);
2926 if (err)
2927 goto done;
2928 if (status == GOT_STATUS_DELETE) {
2929 err = got_fileindex_entry_update(ie,
2930 ondisk_path, blob2->id.sha1,
2931 a->worktree->base_commit_id->sha1, 0);
2932 if (err)
2933 goto done;
2935 } else {
2936 int is_bad_symlink = 0;
2937 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2938 if (S_ISLNK(mode2)) {
2939 err = install_symlink(&is_bad_symlink,
2940 a->worktree, ondisk_path, path2, blob2, 0,
2941 0, 1, repo, a->progress_cb, a->progress_arg);
2942 } else {
2943 err = install_blob(a->worktree, ondisk_path, path2,
2944 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2945 a->progress_cb, a->progress_arg);
2947 if (err)
2948 goto done;
2949 err = got_fileindex_entry_alloc(&ie, path2);
2950 if (err)
2951 goto done;
2952 err = got_fileindex_entry_update(ie, ondisk_path,
2953 NULL, NULL, 1);
2954 if (err) {
2955 got_fileindex_entry_free(ie);
2956 goto done;
2958 err = got_fileindex_entry_add(a->fileindex, ie);
2959 if (err) {
2960 got_fileindex_entry_free(ie);
2961 goto done;
2963 if (is_bad_symlink) {
2964 got_fileindex_entry_filetype_set(ie,
2965 GOT_FILEIDX_MODE_BAD_SYMLINK);
2969 done:
2970 free(ondisk_path);
2971 return err;
2974 struct check_merge_ok_arg {
2975 struct got_worktree *worktree;
2976 struct got_repository *repo;
2979 static const struct got_error *
2980 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2982 const struct got_error *err = NULL;
2983 struct check_merge_ok_arg *a = arg;
2984 unsigned char status;
2985 struct stat sb;
2986 char *ondisk_path;
2988 /* Reject merges into a work tree with mixed base commits. */
2989 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2990 SHA1_DIGEST_LENGTH))
2991 return got_error(GOT_ERR_MIXED_COMMITS);
2993 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2994 == -1)
2995 return got_error_from_errno("asprintf");
2997 /* Reject merges into a work tree with conflicted files. */
2998 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2999 if (err)
3000 return err;
3001 if (status == GOT_STATUS_CONFLICT)
3002 return got_error(GOT_ERR_CONFLICTS);
3004 return NULL;
3007 static const struct got_error *
3008 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3009 const char *fileindex_path, struct got_object_id *commit_id1,
3010 struct got_object_id *commit_id2, struct got_repository *repo,
3011 got_worktree_checkout_cb progress_cb, void *progress_arg,
3012 got_cancel_cb cancel_cb, void *cancel_arg)
3014 const struct got_error *err = NULL, *sync_err;
3015 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3016 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3017 struct merge_file_cb_arg arg;
3018 char *label_orig = NULL;
3020 if (commit_id1) {
3021 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3022 worktree->path_prefix);
3023 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3024 goto done;
3026 if (tree_id1) {
3027 char *id_str;
3029 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3030 if (err)
3031 goto done;
3033 err = got_object_id_str(&id_str, commit_id1);
3034 if (err)
3035 goto done;
3037 if (asprintf(&label_orig, "%s: commit %s",
3038 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3039 err = got_error_from_errno("asprintf");
3040 free(id_str);
3041 goto done;
3043 free(id_str);
3046 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3047 worktree->path_prefix);
3048 if (err)
3049 goto done;
3051 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3052 if (err)
3053 goto done;
3055 arg.worktree = worktree;
3056 arg.fileindex = fileindex;
3057 arg.progress_cb = progress_cb;
3058 arg.progress_arg = progress_arg;
3059 arg.cancel_cb = cancel_cb;
3060 arg.cancel_arg = cancel_arg;
3061 arg.label_orig = label_orig;
3062 arg.commit_id2 = commit_id2;
3063 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3064 sync_err = sync_fileindex(fileindex, fileindex_path);
3065 if (sync_err && err == NULL)
3066 err = sync_err;
3067 done:
3068 if (tree1)
3069 got_object_tree_close(tree1);
3070 if (tree2)
3071 got_object_tree_close(tree2);
3072 free(label_orig);
3073 return err;
3076 const struct got_error *
3077 got_worktree_merge_files(struct got_worktree *worktree,
3078 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3079 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3080 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3082 const struct got_error *err, *unlockerr;
3083 char *fileindex_path = NULL;
3084 struct got_fileindex *fileindex = NULL;
3085 struct check_merge_ok_arg mok_arg;
3087 err = lock_worktree(worktree, LOCK_EX);
3088 if (err)
3089 return err;
3091 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3092 if (err)
3093 goto done;
3095 mok_arg.worktree = worktree;
3096 mok_arg.repo = repo;
3097 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3098 &mok_arg);
3099 if (err)
3100 goto done;
3102 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3103 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3104 done:
3105 if (fileindex)
3106 got_fileindex_free(fileindex);
3107 free(fileindex_path);
3108 unlockerr = lock_worktree(worktree, LOCK_SH);
3109 if (unlockerr && err == NULL)
3110 err = unlockerr;
3111 return err;
3114 struct diff_dir_cb_arg {
3115 struct got_fileindex *fileindex;
3116 struct got_worktree *worktree;
3117 const char *status_path;
3118 size_t status_path_len;
3119 struct got_repository *repo;
3120 got_worktree_status_cb status_cb;
3121 void *status_arg;
3122 got_cancel_cb cancel_cb;
3123 void *cancel_arg;
3124 /* A pathlist containing per-directory pathlists of ignore patterns. */
3125 struct got_pathlist_head ignores;
3126 int report_unchanged;
3127 int no_ignores;
3130 static const struct got_error *
3131 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3132 int dirfd, const char *de_name,
3133 got_worktree_status_cb status_cb, void *status_arg,
3134 struct got_repository *repo, int report_unchanged)
3136 const struct got_error *err = NULL;
3137 unsigned char status = GOT_STATUS_NO_CHANGE;
3138 unsigned char staged_status = get_staged_status(ie);
3139 struct stat sb;
3140 struct got_object_id blob_id, commit_id, staged_blob_id;
3141 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3142 struct got_object_id *staged_blob_idp = NULL;
3144 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3145 if (err)
3146 return err;
3148 if (status == GOT_STATUS_NO_CHANGE &&
3149 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3150 return NULL;
3152 if (got_fileindex_entry_has_blob(ie)) {
3153 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3154 blob_idp = &blob_id;
3156 if (got_fileindex_entry_has_commit(ie)) {
3157 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3158 commit_idp = &commit_id;
3160 if (staged_status == GOT_STATUS_ADD ||
3161 staged_status == GOT_STATUS_MODIFY) {
3162 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3163 SHA1_DIGEST_LENGTH);
3164 staged_blob_idp = &staged_blob_id;
3167 return (*status_cb)(status_arg, status, staged_status,
3168 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3171 static const struct got_error *
3172 status_old_new(void *arg, struct got_fileindex_entry *ie,
3173 struct dirent *de, const char *parent_path, int dirfd)
3175 const struct got_error *err = NULL;
3176 struct diff_dir_cb_arg *a = arg;
3177 char *abspath;
3179 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3180 return got_error(GOT_ERR_CANCELLED);
3182 if (got_path_cmp(parent_path, a->status_path,
3183 strlen(parent_path), a->status_path_len) != 0 &&
3184 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3185 return NULL;
3187 if (parent_path[0]) {
3188 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3189 parent_path, de->d_name) == -1)
3190 return got_error_from_errno("asprintf");
3191 } else {
3192 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3193 de->d_name) == -1)
3194 return got_error_from_errno("asprintf");
3197 err = report_file_status(ie, abspath, dirfd, de->d_name,
3198 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3199 free(abspath);
3200 return err;
3203 static const struct got_error *
3204 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3206 struct diff_dir_cb_arg *a = arg;
3207 struct got_object_id blob_id, commit_id;
3208 unsigned char status;
3210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3211 return got_error(GOT_ERR_CANCELLED);
3213 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3214 return NULL;
3216 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3217 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3218 if (got_fileindex_entry_has_file_on_disk(ie))
3219 status = GOT_STATUS_MISSING;
3220 else
3221 status = GOT_STATUS_DELETE;
3222 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3223 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3226 void
3227 free_ignorelist(struct got_pathlist_head *ignorelist)
3229 struct got_pathlist_entry *pe;
3231 TAILQ_FOREACH(pe, ignorelist, entry)
3232 free((char *)pe->path);
3233 got_pathlist_free(ignorelist);
3236 void
3237 free_ignores(struct got_pathlist_head *ignores)
3239 struct got_pathlist_entry *pe;
3241 TAILQ_FOREACH(pe, ignores, entry) {
3242 struct got_pathlist_head *ignorelist = pe->data;
3243 free_ignorelist(ignorelist);
3244 free((char *)pe->path);
3246 got_pathlist_free(ignores);
3249 static const struct got_error *
3250 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3252 const struct got_error *err = NULL;
3253 struct got_pathlist_entry *pe = NULL;
3254 struct got_pathlist_head *ignorelist;
3255 char *line = NULL, *pattern, *dirpath = NULL;
3256 size_t linesize = 0;
3257 ssize_t linelen;
3259 ignorelist = calloc(1, sizeof(*ignorelist));
3260 if (ignorelist == NULL)
3261 return got_error_from_errno("calloc");
3262 TAILQ_INIT(ignorelist);
3264 while ((linelen = getline(&line, &linesize, f)) != -1) {
3265 if (linelen > 0 && line[linelen - 1] == '\n')
3266 line[linelen - 1] = '\0';
3268 /* Git's ignores may contain comments. */
3269 if (line[0] == '#')
3270 continue;
3272 /* Git's negated patterns are not (yet?) supported. */
3273 if (line[0] == '!')
3274 continue;
3276 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3277 line) == -1) {
3278 err = got_error_from_errno("asprintf");
3279 goto done;
3281 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3282 if (err)
3283 goto done;
3285 if (ferror(f)) {
3286 err = got_error_from_errno("getline");
3287 goto done;
3290 dirpath = strdup(path);
3291 if (dirpath == NULL) {
3292 err = got_error_from_errno("strdup");
3293 goto done;
3295 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3296 done:
3297 free(line);
3298 if (err || pe == NULL) {
3299 free(dirpath);
3300 free_ignorelist(ignorelist);
3302 return err;
3305 int
3306 match_ignores(struct got_pathlist_head *ignores, const char *path)
3308 struct got_pathlist_entry *pe;
3310 /* Handle patterns which match in all directories. */
3311 TAILQ_FOREACH(pe, ignores, entry) {
3312 struct got_pathlist_head *ignorelist = pe->data;
3313 struct got_pathlist_entry *pi;
3315 TAILQ_FOREACH(pi, ignorelist, entry) {
3316 const char *p, *pattern = pi->path;
3318 if (strncmp(pattern, "**/", 3) != 0)
3319 continue;
3320 pattern += 3;
3321 p = path;
3322 while (*p) {
3323 if (fnmatch(pattern, p,
3324 FNM_PATHNAME | FNM_LEADING_DIR)) {
3325 /* Retry in next directory. */
3326 while (*p && *p != '/')
3327 p++;
3328 while (*p == '/')
3329 p++;
3330 continue;
3332 return 1;
3338 * The ignores pathlist contains ignore lists from children before
3339 * parents, so we can find the most specific ignorelist by walking
3340 * ignores backwards.
3342 pe = TAILQ_LAST(ignores, got_pathlist_head);
3343 while (pe) {
3344 if (got_path_is_child(path, pe->path, pe->path_len)) {
3345 struct got_pathlist_head *ignorelist = pe->data;
3346 struct got_pathlist_entry *pi;
3347 TAILQ_FOREACH(pi, ignorelist, entry) {
3348 const char *pattern = pi->path;
3349 int flags = FNM_LEADING_DIR;
3350 if (strstr(pattern, "/**/") == NULL)
3351 flags |= FNM_PATHNAME;
3352 if (fnmatch(pattern, path, flags))
3353 continue;
3354 return 1;
3357 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3360 return 0;
3363 static const struct got_error *
3364 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3365 const char *path, int dirfd, const char *ignores_filename)
3367 const struct got_error *err = NULL;
3368 char *ignorespath;
3369 int fd = -1;
3370 FILE *ignoresfile = NULL;
3372 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3373 path[0] ? "/" : "", ignores_filename) == -1)
3374 return got_error_from_errno("asprintf");
3376 if (dirfd != -1) {
3377 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3378 if (fd == -1) {
3379 if (errno != ENOENT && errno != EACCES)
3380 err = got_error_from_errno2("openat",
3381 ignorespath);
3382 } else {
3383 ignoresfile = fdopen(fd, "r");
3384 if (ignoresfile == NULL)
3385 err = got_error_from_errno2("fdopen",
3386 ignorespath);
3387 else {
3388 fd = -1;
3389 err = read_ignores(ignores, path, ignoresfile);
3392 } else {
3393 ignoresfile = fopen(ignorespath, "r");
3394 if (ignoresfile == NULL) {
3395 if (errno != ENOENT && errno != EACCES)
3396 err = got_error_from_errno2("fopen",
3397 ignorespath);
3398 } else
3399 err = read_ignores(ignores, path, ignoresfile);
3402 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3403 err = got_error_from_errno2("fclose", path);
3404 if (fd != -1 && close(fd) == -1 && err == NULL)
3405 err = got_error_from_errno2("close", path);
3406 free(ignorespath);
3407 return err;
3410 static const struct got_error *
3411 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3413 const struct got_error *err = NULL;
3414 struct diff_dir_cb_arg *a = arg;
3415 char *path = NULL;
3417 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3418 return got_error(GOT_ERR_CANCELLED);
3420 if (parent_path[0]) {
3421 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3422 return got_error_from_errno("asprintf");
3423 } else {
3424 path = de->d_name;
3427 if (de->d_type != DT_DIR &&
3428 got_path_is_child(path, a->status_path, a->status_path_len)
3429 && !match_ignores(&a->ignores, path))
3430 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3431 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3432 if (parent_path[0])
3433 free(path);
3434 return err;
3437 static const struct got_error *
3438 status_traverse(void *arg, const char *path, int dirfd)
3440 const struct got_error *err = NULL;
3441 struct diff_dir_cb_arg *a = arg;
3443 if (a->no_ignores)
3444 return NULL;
3446 err = add_ignores(&a->ignores, a->worktree->root_path,
3447 path, dirfd, ".cvsignore");
3448 if (err)
3449 return err;
3451 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3452 dirfd, ".gitignore");
3454 return err;
3457 static const struct got_error *
3458 report_single_file_status(const char *path, const char *ondisk_path,
3459 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3460 void *status_arg, struct got_repository *repo, int report_unchanged)
3462 struct got_fileindex_entry *ie;
3463 struct stat sb;
3465 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3466 if (ie)
3467 return report_file_status(ie, ondisk_path, -1, NULL,
3468 status_cb, status_arg, repo, report_unchanged);
3470 if (lstat(ondisk_path, &sb) == -1) {
3471 if (errno != ENOENT)
3472 return got_error_from_errno2("lstat", ondisk_path);
3473 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3474 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3475 return NULL;
3478 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3479 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3480 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3482 return NULL;
3485 static const struct got_error *
3486 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3487 const char *root_path, const char *path)
3489 const struct got_error *err;
3490 char *parent_path, *next_parent_path = NULL;
3492 err = add_ignores(ignores, root_path, "", -1,
3493 ".cvsignore");
3494 if (err)
3495 return err;
3497 err = add_ignores(ignores, root_path, "", -1,
3498 ".gitignore");
3499 if (err)
3500 return err;
3502 err = got_path_dirname(&parent_path, path);
3503 if (err) {
3504 if (err->code == GOT_ERR_BAD_PATH)
3505 return NULL; /* cannot traverse parent */
3506 return err;
3508 for (;;) {
3509 err = add_ignores(ignores, root_path, parent_path, -1,
3510 ".cvsignore");
3511 if (err)
3512 break;
3513 err = add_ignores(ignores, root_path, parent_path, -1,
3514 ".gitignore");
3515 if (err)
3516 break;
3517 err = got_path_dirname(&next_parent_path, parent_path);
3518 if (err) {
3519 if (err->code == GOT_ERR_BAD_PATH)
3520 err = NULL; /* traversed everything */
3521 break;
3523 free(parent_path);
3524 parent_path = next_parent_path;
3525 next_parent_path = NULL;
3528 free(parent_path);
3529 free(next_parent_path);
3530 return err;
3533 static const struct got_error *
3534 worktree_status(struct got_worktree *worktree, const char *path,
3535 struct got_fileindex *fileindex, struct got_repository *repo,
3536 got_worktree_status_cb status_cb, void *status_arg,
3537 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3538 int report_unchanged)
3540 const struct got_error *err = NULL;
3541 int fd = -1;
3542 struct got_fileindex_diff_dir_cb fdiff_cb;
3543 struct diff_dir_cb_arg arg;
3544 char *ondisk_path = NULL;
3546 TAILQ_INIT(&arg.ignores);
3548 if (asprintf(&ondisk_path, "%s%s%s",
3549 worktree->root_path, path[0] ? "/" : "", path) == -1)
3550 return got_error_from_errno("asprintf");
3552 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3553 if (fd == -1) {
3554 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3555 errno != ELOOP)
3556 err = got_error_from_errno2("open", ondisk_path);
3557 else
3558 err = report_single_file_status(path, ondisk_path,
3559 fileindex, status_cb, status_arg, repo,
3560 report_unchanged);
3561 } else {
3562 fdiff_cb.diff_old_new = status_old_new;
3563 fdiff_cb.diff_old = status_old;
3564 fdiff_cb.diff_new = status_new;
3565 fdiff_cb.diff_traverse = status_traverse;
3566 arg.fileindex = fileindex;
3567 arg.worktree = worktree;
3568 arg.status_path = path;
3569 arg.status_path_len = strlen(path);
3570 arg.repo = repo;
3571 arg.status_cb = status_cb;
3572 arg.status_arg = status_arg;
3573 arg.cancel_cb = cancel_cb;
3574 arg.cancel_arg = cancel_arg;
3575 arg.report_unchanged = report_unchanged;
3576 arg.no_ignores = no_ignores;
3577 if (!no_ignores) {
3578 err = add_ignores_from_parent_paths(&arg.ignores,
3579 worktree->root_path, path);
3580 if (err)
3581 goto done;
3583 err = got_fileindex_diff_dir(fileindex, fd,
3584 worktree->root_path, path, repo, &fdiff_cb, &arg);
3586 done:
3587 free_ignores(&arg.ignores);
3588 if (fd != -1 && close(fd) != 0 && err == NULL)
3589 err = got_error_from_errno("close");
3590 free(ondisk_path);
3591 return err;
3594 const struct got_error *
3595 got_worktree_status(struct got_worktree *worktree,
3596 struct got_pathlist_head *paths, struct got_repository *repo,
3597 got_worktree_status_cb status_cb, void *status_arg,
3598 got_cancel_cb cancel_cb, void *cancel_arg)
3600 const struct got_error *err = NULL;
3601 char *fileindex_path = NULL;
3602 struct got_fileindex *fileindex = NULL;
3603 struct got_pathlist_entry *pe;
3605 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3606 if (err)
3607 return err;
3609 TAILQ_FOREACH(pe, paths, entry) {
3610 err = worktree_status(worktree, pe->path, fileindex, repo,
3611 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3612 if (err)
3613 break;
3615 free(fileindex_path);
3616 got_fileindex_free(fileindex);
3617 return err;
3620 const struct got_error *
3621 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3622 const char *arg)
3624 const struct got_error *err = NULL;
3625 char *resolved = NULL, *cwd = NULL, *path = NULL;
3626 size_t len;
3627 struct stat sb;
3628 char *abspath = NULL;
3629 char canonpath[PATH_MAX];
3631 *wt_path = NULL;
3633 cwd = getcwd(NULL, 0);
3634 if (cwd == NULL)
3635 return got_error_from_errno("getcwd");
3637 if (lstat(arg, &sb) == -1) {
3638 if (errno != ENOENT) {
3639 err = got_error_from_errno2("lstat", arg);
3640 goto done;
3643 if (S_ISLNK(sb.st_mode)) {
3645 * We cannot use realpath(3) with symlinks since we want to
3646 * operate on the symlink itself.
3647 * But we can make the path absolute, assuming it is relative
3648 * to the current working directory, and then canonicalize it.
3650 if (!got_path_is_absolute(arg)) {
3651 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3652 err = got_error_from_errno("asprintf");
3653 goto done;
3657 err = got_canonpath(abspath ? abspath : arg, canonpath,
3658 sizeof(canonpath));
3659 if (err)
3660 goto done;
3661 resolved = strdup(canonpath);
3662 if (resolved == NULL) {
3663 err = got_error_from_errno("strdup");
3664 goto done;
3666 } else {
3667 resolved = realpath(arg, NULL);
3668 if (resolved == NULL) {
3669 if (errno != ENOENT) {
3670 err = got_error_from_errno2("realpath", arg);
3671 goto done;
3673 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3674 err = got_error_from_errno("asprintf");
3675 goto done;
3677 err = got_canonpath(abspath, canonpath,
3678 sizeof(canonpath));
3679 if (err)
3680 goto done;
3681 resolved = strdup(canonpath);
3682 if (resolved == NULL) {
3683 err = got_error_from_errno("strdup");
3684 goto done;
3689 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3690 strlen(got_worktree_get_root_path(worktree)))) {
3691 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3692 goto done;
3695 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3696 err = got_path_skip_common_ancestor(&path,
3697 got_worktree_get_root_path(worktree), resolved);
3698 if (err)
3699 goto done;
3700 } else {
3701 path = strdup("");
3702 if (path == NULL) {
3703 err = got_error_from_errno("strdup");
3704 goto done;
3708 /* XXX status walk can't deal with trailing slash! */
3709 len = strlen(path);
3710 while (len > 0 && path[len - 1] == '/') {
3711 path[len - 1] = '\0';
3712 len--;
3714 done:
3715 free(abspath);
3716 free(resolved);
3717 free(cwd);
3718 if (err == NULL)
3719 *wt_path = path;
3720 else
3721 free(path);
3722 return err;
3725 struct schedule_addition_args {
3726 struct got_worktree *worktree;
3727 struct got_fileindex *fileindex;
3728 got_worktree_checkout_cb progress_cb;
3729 void *progress_arg;
3730 struct got_repository *repo;
3733 static const struct got_error *
3734 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3735 const char *relpath, struct got_object_id *blob_id,
3736 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3737 int dirfd, const char *de_name)
3739 struct schedule_addition_args *a = arg;
3740 const struct got_error *err = NULL;
3741 struct got_fileindex_entry *ie;
3742 struct stat sb;
3743 char *ondisk_path;
3745 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3746 relpath) == -1)
3747 return got_error_from_errno("asprintf");
3749 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3750 if (ie) {
3751 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3752 de_name, a->repo);
3753 if (err)
3754 goto done;
3755 /* Re-adding an existing entry is a no-op. */
3756 if (status == GOT_STATUS_ADD)
3757 goto done;
3758 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3759 if (err)
3760 goto done;
3763 if (status != GOT_STATUS_UNVERSIONED) {
3764 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3765 goto done;
3768 err = got_fileindex_entry_alloc(&ie, relpath);
3769 if (err)
3770 goto done;
3771 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3772 if (err) {
3773 got_fileindex_entry_free(ie);
3774 goto done;
3776 err = got_fileindex_entry_add(a->fileindex, ie);
3777 if (err) {
3778 got_fileindex_entry_free(ie);
3779 goto done;
3781 done:
3782 free(ondisk_path);
3783 if (err)
3784 return err;
3785 if (status == GOT_STATUS_ADD)
3786 return NULL;
3787 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3790 const struct got_error *
3791 got_worktree_schedule_add(struct got_worktree *worktree,
3792 struct got_pathlist_head *paths,
3793 got_worktree_checkout_cb progress_cb, void *progress_arg,
3794 struct got_repository *repo, int no_ignores)
3796 struct got_fileindex *fileindex = NULL;
3797 char *fileindex_path = NULL;
3798 const struct got_error *err = NULL, *sync_err, *unlockerr;
3799 struct got_pathlist_entry *pe;
3800 struct schedule_addition_args saa;
3802 err = lock_worktree(worktree, LOCK_EX);
3803 if (err)
3804 return err;
3806 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3807 if (err)
3808 goto done;
3810 saa.worktree = worktree;
3811 saa.fileindex = fileindex;
3812 saa.progress_cb = progress_cb;
3813 saa.progress_arg = progress_arg;
3814 saa.repo = repo;
3816 TAILQ_FOREACH(pe, paths, entry) {
3817 err = worktree_status(worktree, pe->path, fileindex, repo,
3818 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3819 if (err)
3820 break;
3822 sync_err = sync_fileindex(fileindex, fileindex_path);
3823 if (sync_err && err == NULL)
3824 err = sync_err;
3825 done:
3826 free(fileindex_path);
3827 if (fileindex)
3828 got_fileindex_free(fileindex);
3829 unlockerr = lock_worktree(worktree, LOCK_SH);
3830 if (unlockerr && err == NULL)
3831 err = unlockerr;
3832 return err;
3835 struct schedule_deletion_args {
3836 struct got_worktree *worktree;
3837 struct got_fileindex *fileindex;
3838 got_worktree_delete_cb progress_cb;
3839 void *progress_arg;
3840 struct got_repository *repo;
3841 int delete_local_mods;
3842 int keep_on_disk;
3843 const char *status_codes;
3846 static const struct got_error *
3847 schedule_for_deletion(void *arg, unsigned char status,
3848 unsigned char staged_status, const char *relpath,
3849 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3850 struct got_object_id *commit_id, int dirfd, const char *de_name)
3852 struct schedule_deletion_args *a = arg;
3853 const struct got_error *err = NULL;
3854 struct got_fileindex_entry *ie = NULL;
3855 struct stat sb;
3856 char *ondisk_path;
3858 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3859 if (ie == NULL)
3860 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3862 staged_status = get_staged_status(ie);
3863 if (staged_status != GOT_STATUS_NO_CHANGE) {
3864 if (staged_status == GOT_STATUS_DELETE)
3865 return NULL;
3866 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3869 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3870 relpath) == -1)
3871 return got_error_from_errno("asprintf");
3873 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3874 a->repo);
3875 if (err)
3876 goto done;
3878 if (a->status_codes) {
3879 size_t ncodes = strlen(a->status_codes);
3880 int i;
3881 for (i = 0; i < ncodes ; i++) {
3882 if (status == a->status_codes[i])
3883 break;
3885 if (i == ncodes) {
3886 /* Do not delete files in non-matching status. */
3887 free(ondisk_path);
3888 return NULL;
3890 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3891 a->status_codes[i] != GOT_STATUS_MISSING) {
3892 static char msg[64];
3893 snprintf(msg, sizeof(msg),
3894 "invalid status code '%c'", a->status_codes[i]);
3895 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3896 goto done;
3900 if (status != GOT_STATUS_NO_CHANGE) {
3901 if (status == GOT_STATUS_DELETE)
3902 goto done;
3903 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3904 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3905 goto done;
3907 if (status != GOT_STATUS_MODIFY &&
3908 status != GOT_STATUS_MISSING) {
3909 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3910 goto done;
3914 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3915 size_t root_len;
3917 if (dirfd != -1) {
3918 if (unlinkat(dirfd, de_name, 0) != 0) {
3919 err = got_error_from_errno2("unlinkat",
3920 ondisk_path);
3921 goto done;
3923 } else if (unlink(ondisk_path) != 0) {
3924 err = got_error_from_errno2("unlink", ondisk_path);
3925 goto done;
3928 root_len = strlen(a->worktree->root_path);
3929 do {
3930 char *parent;
3931 err = got_path_dirname(&parent, ondisk_path);
3932 if (err)
3933 goto done;
3934 free(ondisk_path);
3935 ondisk_path = parent;
3936 if (rmdir(ondisk_path) == -1) {
3937 if (errno != ENOTEMPTY)
3938 err = got_error_from_errno2("rmdir",
3939 ondisk_path);
3940 break;
3942 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3943 strlen(ondisk_path), root_len) != 0);
3946 got_fileindex_entry_mark_deleted_from_disk(ie);
3947 done:
3948 free(ondisk_path);
3949 if (err)
3950 return err;
3951 if (status == GOT_STATUS_DELETE)
3952 return NULL;
3953 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3954 staged_status, relpath);
3957 const struct got_error *
3958 got_worktree_schedule_delete(struct got_worktree *worktree,
3959 struct got_pathlist_head *paths, int delete_local_mods,
3960 const char *status_codes,
3961 got_worktree_delete_cb progress_cb, void *progress_arg,
3962 struct got_repository *repo, int keep_on_disk)
3964 struct got_fileindex *fileindex = NULL;
3965 char *fileindex_path = NULL;
3966 const struct got_error *err = NULL, *sync_err, *unlockerr;
3967 struct got_pathlist_entry *pe;
3968 struct schedule_deletion_args sda;
3970 err = lock_worktree(worktree, LOCK_EX);
3971 if (err)
3972 return err;
3974 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3975 if (err)
3976 goto done;
3978 sda.worktree = worktree;
3979 sda.fileindex = fileindex;
3980 sda.progress_cb = progress_cb;
3981 sda.progress_arg = progress_arg;
3982 sda.repo = repo;
3983 sda.delete_local_mods = delete_local_mods;
3984 sda.keep_on_disk = keep_on_disk;
3985 sda.status_codes = status_codes;
3987 TAILQ_FOREACH(pe, paths, entry) {
3988 err = worktree_status(worktree, pe->path, fileindex, repo,
3989 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3990 if (err)
3991 break;
3993 sync_err = sync_fileindex(fileindex, fileindex_path);
3994 if (sync_err && err == NULL)
3995 err = sync_err;
3996 done:
3997 free(fileindex_path);
3998 if (fileindex)
3999 got_fileindex_free(fileindex);
4000 unlockerr = lock_worktree(worktree, LOCK_SH);
4001 if (unlockerr && err == NULL)
4002 err = unlockerr;
4003 return err;
4006 static const struct got_error *
4007 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4009 const struct got_error *err = NULL;
4010 char *line = NULL;
4011 size_t linesize = 0, n;
4012 ssize_t linelen;
4014 linelen = getline(&line, &linesize, infile);
4015 if (linelen == -1) {
4016 if (ferror(infile)) {
4017 err = got_error_from_errno("getline");
4018 goto done;
4020 return NULL;
4022 if (outfile) {
4023 n = fwrite(line, 1, linelen, outfile);
4024 if (n != linelen) {
4025 err = got_ferror(outfile, GOT_ERR_IO);
4026 goto done;
4029 if (rejectfile) {
4030 n = fwrite(line, 1, linelen, rejectfile);
4031 if (n != linelen)
4032 err = got_ferror(outfile, GOT_ERR_IO);
4034 done:
4035 free(line);
4036 return err;
4039 static const struct got_error *
4040 skip_one_line(FILE *f)
4042 char *line = NULL;
4043 size_t linesize = 0;
4044 ssize_t linelen;
4046 linelen = getline(&line, &linesize, f);
4047 if (linelen == -1) {
4048 if (ferror(f))
4049 return got_error_from_errno("getline");
4050 return NULL;
4052 free(line);
4053 return NULL;
4056 static const struct got_error *
4057 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4058 int start_old, int end_old, int start_new, int end_new,
4059 FILE *outfile, FILE *rejectfile)
4061 const struct got_error *err;
4063 /* Copy old file's lines leading up to patch. */
4064 while (!feof(f1) && *line_cur1 < start_old) {
4065 err = copy_one_line(f1, outfile, NULL);
4066 if (err)
4067 return err;
4068 (*line_cur1)++;
4070 /* Skip new file's lines leading up to patch. */
4071 while (!feof(f2) && *line_cur2 < start_new) {
4072 if (rejectfile)
4073 err = copy_one_line(f2, NULL, rejectfile);
4074 else
4075 err = skip_one_line(f2);
4076 if (err)
4077 return err;
4078 (*line_cur2)++;
4080 /* Copy patched lines. */
4081 while (!feof(f2) && *line_cur2 <= end_new) {
4082 err = copy_one_line(f2, outfile, NULL);
4083 if (err)
4084 return err;
4085 (*line_cur2)++;
4087 /* Skip over old file's replaced lines. */
4088 while (!feof(f1) && *line_cur1 <= end_old) {
4089 if (rejectfile)
4090 err = copy_one_line(f1, NULL, rejectfile);
4091 else
4092 err = skip_one_line(f1);
4093 if (err)
4094 return err;
4095 (*line_cur1)++;
4098 return NULL;
4101 static const struct got_error *
4102 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4103 FILE *outfile, FILE *rejectfile)
4105 const struct got_error *err;
4107 if (outfile) {
4108 /* Copy old file's lines until EOF. */
4109 while (!feof(f1)) {
4110 err = copy_one_line(f1, outfile, NULL);
4111 if (err)
4112 return err;
4113 (*line_cur1)++;
4116 if (rejectfile) {
4117 /* Copy new file's lines until EOF. */
4118 while (!feof(f2)) {
4119 err = copy_one_line(f2, NULL, rejectfile);
4120 if (err)
4121 return err;
4122 (*line_cur2)++;
4126 return NULL;
4129 static const struct got_error *
4130 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4131 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4132 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4133 int *line_cur2, FILE *outfile, FILE *rejectfile,
4134 got_worktree_patch_cb patch_cb, void *patch_arg)
4136 const struct got_error *err = NULL;
4137 int start_old = change->cv.a;
4138 int end_old = change->cv.b;
4139 int start_new = change->cv.c;
4140 int end_new = change->cv.d;
4141 long pos1, pos2;
4142 FILE *hunkfile;
4144 *choice = GOT_PATCH_CHOICE_NONE;
4146 hunkfile = got_opentemp();
4147 if (hunkfile == NULL)
4148 return got_error_from_errno("got_opentemp");
4150 pos1 = ftell(f1);
4151 pos2 = ftell(f2);
4153 /* XXX TODO needs error checking */
4154 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4156 if (fseek(f1, pos1, SEEK_SET) == -1) {
4157 err = got_ferror(f1, GOT_ERR_IO);
4158 goto done;
4160 if (fseek(f2, pos2, SEEK_SET) == -1) {
4161 err = got_ferror(f1, GOT_ERR_IO);
4162 goto done;
4164 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4165 err = got_ferror(hunkfile, GOT_ERR_IO);
4166 goto done;
4169 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4170 hunkfile, n, nchanges);
4171 if (err)
4172 goto done;
4174 switch (*choice) {
4175 case GOT_PATCH_CHOICE_YES:
4176 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4177 end_old, start_new, end_new, outfile, rejectfile);
4178 break;
4179 case GOT_PATCH_CHOICE_NO:
4180 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4181 end_old, start_new, end_new, rejectfile, outfile);
4182 break;
4183 case GOT_PATCH_CHOICE_QUIT:
4184 break;
4185 default:
4186 err = got_error(GOT_ERR_PATCH_CHOICE);
4187 break;
4189 done:
4190 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4191 err = got_error_from_errno("fclose");
4192 return err;
4195 struct revert_file_args {
4196 struct got_worktree *worktree;
4197 struct got_fileindex *fileindex;
4198 got_worktree_checkout_cb progress_cb;
4199 void *progress_arg;
4200 got_worktree_patch_cb patch_cb;
4201 void *patch_arg;
4202 struct got_repository *repo;
4205 static const struct got_error *
4206 create_patched_content(char **path_outfile, int reverse_patch,
4207 struct got_object_id *blob_id, const char *path2,
4208 int dirfd2, const char *de_name2,
4209 const char *relpath, struct got_repository *repo,
4210 got_worktree_patch_cb patch_cb, void *patch_arg)
4212 const struct got_error *err;
4213 struct got_blob_object *blob = NULL;
4214 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4215 int fd2 = -1;
4216 char link_target[PATH_MAX];
4217 ssize_t link_len = 0;
4218 char *path1 = NULL, *id_str = NULL;
4219 struct stat sb1, sb2;
4220 struct got_diff_changes *changes = NULL;
4221 struct got_diff_state *ds = NULL;
4222 struct got_diff_args *args = NULL;
4223 struct got_diff_change *change;
4224 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4225 int n = 0;
4227 *path_outfile = NULL;
4229 err = got_object_id_str(&id_str, blob_id);
4230 if (err)
4231 return err;
4233 if (dirfd2 != -1) {
4234 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4235 if (fd2 == -1) {
4236 if (errno != ELOOP) {
4237 err = got_error_from_errno2("openat", path2);
4238 goto done;
4240 link_len = readlinkat(dirfd2, de_name2,
4241 link_target, sizeof(link_target));
4242 if (link_len == -1)
4243 return got_error_from_errno2("readlinkat", path2);
4244 sb2.st_mode = S_IFLNK;
4245 sb2.st_size = link_len;
4247 } else {
4248 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4249 if (fd2 == -1) {
4250 if (errno != ELOOP) {
4251 err = got_error_from_errno2("open", path2);
4252 goto done;
4254 link_len = readlink(path2, link_target,
4255 sizeof(link_target));
4256 if (link_len == -1)
4257 return got_error_from_errno2("readlink", path2);
4258 sb2.st_mode = S_IFLNK;
4259 sb2.st_size = link_len;
4262 if (fd2 != -1) {
4263 if (fstat(fd2, &sb2) == -1) {
4264 err = got_error_from_errno2("fstat", path2);
4265 goto done;
4268 f2 = fdopen(fd2, "r");
4269 if (f2 == NULL) {
4270 err = got_error_from_errno2("fdopen", path2);
4271 goto done;
4273 fd2 = -1;
4274 } else {
4275 size_t n;
4276 f2 = got_opentemp();
4277 if (f2 == NULL) {
4278 err = got_error_from_errno2("got_opentemp", path2);
4279 goto done;
4281 n = fwrite(link_target, 1, link_len, f2);
4282 if (n != link_len) {
4283 err = got_ferror(f2, GOT_ERR_IO);
4284 goto done;
4286 if (fflush(f2) == EOF) {
4287 err = got_error_from_errno("fflush");
4288 goto done;
4290 rewind(f2);
4293 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4294 if (err)
4295 goto done;
4297 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4298 if (err)
4299 goto done;
4301 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4302 if (err)
4303 goto done;
4305 if (stat(path1, &sb1) == -1) {
4306 err = got_error_from_errno2("stat", path1);
4307 goto done;
4310 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4311 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4312 if (err)
4313 goto done;
4315 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4316 if (err)
4317 goto done;
4319 if (fseek(f1, 0L, SEEK_SET) == -1)
4320 return got_ferror(f1, GOT_ERR_IO);
4321 if (fseek(f2, 0L, SEEK_SET) == -1)
4322 return got_ferror(f2, GOT_ERR_IO);
4323 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4324 int choice;
4325 err = apply_or_reject_change(&choice, change, ++n,
4326 changes->nchanges, ds, args, diff_flags, relpath,
4327 f1, f2, &line_cur1, &line_cur2,
4328 reverse_patch ? NULL : outfile,
4329 reverse_patch ? outfile : NULL,
4330 patch_cb, patch_arg);
4331 if (err)
4332 goto done;
4333 if (choice == GOT_PATCH_CHOICE_YES)
4334 have_content = 1;
4335 else if (choice == GOT_PATCH_CHOICE_QUIT)
4336 break;
4338 if (have_content) {
4339 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4340 reverse_patch ? NULL : outfile,
4341 reverse_patch ? outfile : NULL);
4342 if (err)
4343 goto done;
4345 if (!S_ISLNK(sb2.st_mode)) {
4346 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4347 err = got_error_from_errno2("fchmod", path2);
4348 goto done;
4352 done:
4353 free(id_str);
4354 if (blob)
4355 got_object_blob_close(blob);
4356 if (f1 && fclose(f1) == EOF && err == NULL)
4357 err = got_error_from_errno2("fclose", path1);
4358 if (f2 && fclose(f2) == EOF && err == NULL)
4359 err = got_error_from_errno2("fclose", path2);
4360 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4361 err = got_error_from_errno2("close", path2);
4362 if (outfile && fclose(outfile) == EOF && err == NULL)
4363 err = got_error_from_errno2("fclose", *path_outfile);
4364 if (path1 && unlink(path1) == -1 && err == NULL)
4365 err = got_error_from_errno2("unlink", path1);
4366 if (err || !have_content) {
4367 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4368 err = got_error_from_errno2("unlink", *path_outfile);
4369 free(*path_outfile);
4370 *path_outfile = NULL;
4372 free(args);
4373 if (ds) {
4374 got_diff_state_free(ds);
4375 free(ds);
4377 if (changes)
4378 got_diff_free_changes(changes);
4379 free(path1);
4380 return err;
4383 static const struct got_error *
4384 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4385 const char *relpath, struct got_object_id *blob_id,
4386 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4387 int dirfd, const char *de_name)
4389 struct revert_file_args *a = arg;
4390 const struct got_error *err = NULL;
4391 char *parent_path = NULL;
4392 struct got_fileindex_entry *ie;
4393 struct got_tree_object *tree = NULL;
4394 struct got_object_id *tree_id = NULL;
4395 const struct got_tree_entry *te = NULL;
4396 char *tree_path = NULL, *te_name;
4397 char *ondisk_path = NULL, *path_content = NULL;
4398 struct got_blob_object *blob = NULL;
4400 /* Reverting a staged deletion is a no-op. */
4401 if (status == GOT_STATUS_DELETE &&
4402 staged_status != GOT_STATUS_NO_CHANGE)
4403 return NULL;
4405 if (status == GOT_STATUS_UNVERSIONED)
4406 return (*a->progress_cb)(a->progress_arg,
4407 GOT_STATUS_UNVERSIONED, relpath);
4409 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4410 if (ie == NULL)
4411 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4413 /* Construct in-repository path of tree which contains this blob. */
4414 err = got_path_dirname(&parent_path, ie->path);
4415 if (err) {
4416 if (err->code != GOT_ERR_BAD_PATH)
4417 goto done;
4418 parent_path = strdup("/");
4419 if (parent_path == NULL) {
4420 err = got_error_from_errno("strdup");
4421 goto done;
4424 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4425 tree_path = strdup(parent_path);
4426 if (tree_path == NULL) {
4427 err = got_error_from_errno("strdup");
4428 goto done;
4430 } else {
4431 if (got_path_is_root_dir(parent_path)) {
4432 tree_path = strdup(a->worktree->path_prefix);
4433 if (tree_path == NULL) {
4434 err = got_error_from_errno("strdup");
4435 goto done;
4437 } else {
4438 if (asprintf(&tree_path, "%s/%s",
4439 a->worktree->path_prefix, parent_path) == -1) {
4440 err = got_error_from_errno("asprintf");
4441 goto done;
4446 err = got_object_id_by_path(&tree_id, a->repo,
4447 a->worktree->base_commit_id, tree_path);
4448 if (err) {
4449 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4450 (status == GOT_STATUS_ADD ||
4451 staged_status == GOT_STATUS_ADD)))
4452 goto done;
4453 } else {
4454 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4455 if (err)
4456 goto done;
4458 err = got_path_basename(&te_name, ie->path);
4459 if (err)
4460 goto done;
4462 te = got_object_tree_find_entry(tree, te_name);
4463 free(te_name);
4464 if (te == NULL && status != GOT_STATUS_ADD &&
4465 staged_status != GOT_STATUS_ADD) {
4466 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4467 goto done;
4471 switch (status) {
4472 case GOT_STATUS_ADD:
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 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4483 ie->path);
4484 if (err)
4485 goto done;
4486 got_fileindex_entry_remove(a->fileindex, ie);
4487 break;
4488 case GOT_STATUS_DELETE:
4489 if (a->patch_cb) {
4490 int choice = GOT_PATCH_CHOICE_NONE;
4491 err = (*a->patch_cb)(&choice, a->patch_arg,
4492 status, ie->path, NULL, 1, 1);
4493 if (err)
4494 goto done;
4495 if (choice != GOT_PATCH_CHOICE_YES)
4496 break;
4498 /* fall through */
4499 case GOT_STATUS_MODIFY:
4500 case GOT_STATUS_MODE_CHANGE:
4501 case GOT_STATUS_CONFLICT:
4502 case GOT_STATUS_MISSING: {
4503 struct got_object_id id;
4504 if (staged_status == GOT_STATUS_ADD ||
4505 staged_status == GOT_STATUS_MODIFY) {
4506 memcpy(id.sha1, ie->staged_blob_sha1,
4507 SHA1_DIGEST_LENGTH);
4508 } else
4509 memcpy(id.sha1, ie->blob_sha1,
4510 SHA1_DIGEST_LENGTH);
4511 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4512 if (err)
4513 goto done;
4515 if (asprintf(&ondisk_path, "%s/%s",
4516 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4517 err = got_error_from_errno("asprintf");
4518 goto done;
4521 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4522 status == GOT_STATUS_CONFLICT)) {
4523 int is_bad_symlink = 0;
4524 err = create_patched_content(&path_content, 1, &id,
4525 ondisk_path, dirfd, de_name, ie->path, a->repo,
4526 a->patch_cb, a->patch_arg);
4527 if (err || path_content == NULL)
4528 break;
4529 if (te && S_ISLNK(te->mode)) {
4530 if (unlink(path_content) == -1) {
4531 err = got_error_from_errno2("unlink",
4532 path_content);
4533 break;
4535 err = install_symlink(&is_bad_symlink,
4536 a->worktree, ondisk_path, ie->path,
4537 blob, 0, 1, 0, a->repo,
4538 a->progress_cb, a->progress_arg);
4539 } else {
4540 if (rename(path_content, ondisk_path) == -1) {
4541 err = got_error_from_errno3("rename",
4542 path_content, ondisk_path);
4543 goto done;
4546 } else {
4547 int is_bad_symlink = 0;
4548 if (te && S_ISLNK(te->mode)) {
4549 err = install_symlink(&is_bad_symlink,
4550 a->worktree, ondisk_path, ie->path,
4551 blob, 0, 1, 0, a->repo,
4552 a->progress_cb, a->progress_arg);
4553 } else {
4554 err = install_blob(a->worktree, ondisk_path,
4555 ie->path,
4556 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4557 got_fileindex_perms_to_st(ie), blob,
4558 0, 1, 0, 0, a->repo,
4559 a->progress_cb, a->progress_arg);
4561 if (err)
4562 goto done;
4563 if (status == GOT_STATUS_DELETE ||
4564 status == GOT_STATUS_MODE_CHANGE) {
4565 err = got_fileindex_entry_update(ie,
4566 ondisk_path, blob->id.sha1,
4567 a->worktree->base_commit_id->sha1, 1);
4568 if (err)
4569 goto done;
4571 if (is_bad_symlink) {
4572 got_fileindex_entry_filetype_set(ie,
4573 GOT_FILEIDX_MODE_BAD_SYMLINK);
4576 break;
4578 default:
4579 break;
4581 done:
4582 free(ondisk_path);
4583 free(path_content);
4584 free(parent_path);
4585 free(tree_path);
4586 if (blob)
4587 got_object_blob_close(blob);
4588 if (tree)
4589 got_object_tree_close(tree);
4590 free(tree_id);
4591 return err;
4594 const struct got_error *
4595 got_worktree_revert(struct got_worktree *worktree,
4596 struct got_pathlist_head *paths,
4597 got_worktree_checkout_cb progress_cb, void *progress_arg,
4598 got_worktree_patch_cb patch_cb, void *patch_arg,
4599 struct got_repository *repo)
4601 struct got_fileindex *fileindex = NULL;
4602 char *fileindex_path = NULL;
4603 const struct got_error *err = NULL, *unlockerr = NULL;
4604 const struct got_error *sync_err = NULL;
4605 struct got_pathlist_entry *pe;
4606 struct revert_file_args rfa;
4608 err = lock_worktree(worktree, LOCK_EX);
4609 if (err)
4610 return err;
4612 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4613 if (err)
4614 goto done;
4616 rfa.worktree = worktree;
4617 rfa.fileindex = fileindex;
4618 rfa.progress_cb = progress_cb;
4619 rfa.progress_arg = progress_arg;
4620 rfa.patch_cb = patch_cb;
4621 rfa.patch_arg = patch_arg;
4622 rfa.repo = repo;
4623 TAILQ_FOREACH(pe, paths, entry) {
4624 err = worktree_status(worktree, pe->path, fileindex, repo,
4625 revert_file, &rfa, NULL, NULL, 0, 0);
4626 if (err)
4627 break;
4629 sync_err = sync_fileindex(fileindex, fileindex_path);
4630 if (sync_err && err == NULL)
4631 err = sync_err;
4632 done:
4633 free(fileindex_path);
4634 if (fileindex)
4635 got_fileindex_free(fileindex);
4636 unlockerr = lock_worktree(worktree, LOCK_SH);
4637 if (unlockerr && err == NULL)
4638 err = unlockerr;
4639 return err;
4642 static void
4643 free_commitable(struct got_commitable *ct)
4645 free(ct->path);
4646 free(ct->in_repo_path);
4647 free(ct->ondisk_path);
4648 free(ct->blob_id);
4649 free(ct->base_blob_id);
4650 free(ct->staged_blob_id);
4651 free(ct->base_commit_id);
4652 free(ct);
4655 struct collect_commitables_arg {
4656 struct got_pathlist_head *commitable_paths;
4657 struct got_repository *repo;
4658 struct got_worktree *worktree;
4659 struct got_fileindex *fileindex;
4660 int have_staged_files;
4661 int allow_bad_symlinks;
4664 static const struct got_error *
4665 collect_commitables(void *arg, unsigned char status,
4666 unsigned char staged_status, const char *relpath,
4667 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4668 struct got_object_id *commit_id, int dirfd, const char *de_name)
4670 struct collect_commitables_arg *a = arg;
4671 const struct got_error *err = NULL;
4672 struct got_commitable *ct = NULL;
4673 struct got_pathlist_entry *new = NULL;
4674 char *parent_path = NULL, *path = NULL;
4675 struct stat sb;
4677 if (a->have_staged_files) {
4678 if (staged_status != GOT_STATUS_MODIFY &&
4679 staged_status != GOT_STATUS_ADD &&
4680 staged_status != GOT_STATUS_DELETE)
4681 return NULL;
4682 } else {
4683 if (status == GOT_STATUS_CONFLICT)
4684 return got_error(GOT_ERR_COMMIT_CONFLICT);
4686 if (status != GOT_STATUS_MODIFY &&
4687 status != GOT_STATUS_MODE_CHANGE &&
4688 status != GOT_STATUS_ADD &&
4689 status != GOT_STATUS_DELETE)
4690 return NULL;
4693 if (asprintf(&path, "/%s", relpath) == -1) {
4694 err = got_error_from_errno("asprintf");
4695 goto done;
4697 if (strcmp(path, "/") == 0) {
4698 parent_path = strdup("");
4699 if (parent_path == NULL)
4700 return got_error_from_errno("strdup");
4701 } else {
4702 err = got_path_dirname(&parent_path, path);
4703 if (err)
4704 return err;
4707 ct = calloc(1, sizeof(*ct));
4708 if (ct == NULL) {
4709 err = got_error_from_errno("calloc");
4710 goto done;
4713 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4714 relpath) == -1) {
4715 err = got_error_from_errno("asprintf");
4716 goto done;
4719 if (staged_status == GOT_STATUS_ADD ||
4720 staged_status == GOT_STATUS_MODIFY) {
4721 struct got_fileindex_entry *ie;
4722 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4723 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4724 case GOT_FILEIDX_MODE_REGULAR_FILE:
4725 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4726 ct->mode = S_IFREG;
4727 break;
4728 case GOT_FILEIDX_MODE_SYMLINK:
4729 ct->mode = S_IFLNK;
4730 break;
4731 default:
4732 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4733 goto done;
4735 ct->mode |= got_fileindex_entry_perms_get(ie);
4736 } else if (status != GOT_STATUS_DELETE &&
4737 staged_status != GOT_STATUS_DELETE) {
4738 if (dirfd != -1) {
4739 if (fstatat(dirfd, de_name, &sb,
4740 AT_SYMLINK_NOFOLLOW) == -1) {
4741 err = got_error_from_errno2("fstatat",
4742 ct->ondisk_path);
4743 goto done;
4745 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4746 err = got_error_from_errno2("lstat", ct->ondisk_path);
4747 goto done;
4749 ct->mode = sb.st_mode;
4752 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4753 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4754 relpath) == -1) {
4755 err = got_error_from_errno("asprintf");
4756 goto done;
4759 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4760 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4761 int is_bad_symlink;
4762 char target_path[PATH_MAX];
4763 ssize_t target_len;
4764 target_len = readlink(ct->ondisk_path, target_path,
4765 sizeof(target_path));
4766 if (target_len == -1) {
4767 err = got_error_from_errno2("readlink",
4768 ct->ondisk_path);
4769 goto done;
4771 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4772 target_len, ct->ondisk_path, a->worktree->root_path);
4773 if (err)
4774 goto done;
4775 if (is_bad_symlink) {
4776 err = got_error_path(ct->ondisk_path,
4777 GOT_ERR_BAD_SYMLINK);
4778 goto done;
4783 ct->status = status;
4784 ct->staged_status = staged_status;
4785 ct->blob_id = NULL; /* will be filled in when blob gets created */
4786 if (ct->status != GOT_STATUS_ADD &&
4787 ct->staged_status != GOT_STATUS_ADD) {
4788 ct->base_blob_id = got_object_id_dup(blob_id);
4789 if (ct->base_blob_id == NULL) {
4790 err = got_error_from_errno("got_object_id_dup");
4791 goto done;
4793 ct->base_commit_id = got_object_id_dup(commit_id);
4794 if (ct->base_commit_id == NULL) {
4795 err = got_error_from_errno("got_object_id_dup");
4796 goto done;
4799 if (ct->staged_status == GOT_STATUS_ADD ||
4800 ct->staged_status == GOT_STATUS_MODIFY) {
4801 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4802 if (ct->staged_blob_id == NULL) {
4803 err = got_error_from_errno("got_object_id_dup");
4804 goto done;
4807 ct->path = strdup(path);
4808 if (ct->path == NULL) {
4809 err = got_error_from_errno("strdup");
4810 goto done;
4812 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4813 done:
4814 if (ct && (err || new == NULL))
4815 free_commitable(ct);
4816 free(parent_path);
4817 free(path);
4818 return err;
4821 static const struct got_error *write_tree(struct got_object_id **, int *,
4822 struct got_tree_object *, const char *, struct got_pathlist_head *,
4823 got_worktree_status_cb status_cb, void *status_arg,
4824 struct got_repository *);
4826 static const struct got_error *
4827 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4828 struct got_tree_entry *te, const char *parent_path,
4829 struct got_pathlist_head *commitable_paths,
4830 got_worktree_status_cb status_cb, void *status_arg,
4831 struct got_repository *repo)
4833 const struct got_error *err = NULL;
4834 struct got_tree_object *subtree;
4835 char *subpath;
4837 if (asprintf(&subpath, "%s%s%s", parent_path,
4838 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4839 return got_error_from_errno("asprintf");
4841 err = got_object_open_as_tree(&subtree, repo, &te->id);
4842 if (err)
4843 return err;
4845 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4846 commitable_paths, status_cb, status_arg, repo);
4847 got_object_tree_close(subtree);
4848 free(subpath);
4849 return err;
4852 static const struct got_error *
4853 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4855 const struct got_error *err = NULL;
4856 char *ct_parent_path = NULL;
4858 *match = 0;
4860 if (strchr(ct->in_repo_path, '/') == NULL) {
4861 *match = got_path_is_root_dir(path);
4862 return NULL;
4865 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4866 if (err)
4867 return err;
4868 *match = (strcmp(path, ct_parent_path) == 0);
4869 free(ct_parent_path);
4870 return err;
4873 static mode_t
4874 get_ct_file_mode(struct got_commitable *ct)
4876 if (S_ISLNK(ct->mode))
4877 return S_IFLNK;
4879 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4882 static const struct got_error *
4883 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4884 struct got_tree_entry *te, struct got_commitable *ct)
4886 const struct got_error *err = NULL;
4888 *new_te = NULL;
4890 err = got_object_tree_entry_dup(new_te, te);
4891 if (err)
4892 goto done;
4894 (*new_te)->mode = get_ct_file_mode(ct);
4896 if (ct->staged_status == GOT_STATUS_MODIFY)
4897 memcpy(&(*new_te)->id, ct->staged_blob_id,
4898 sizeof((*new_te)->id));
4899 else
4900 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4901 done:
4902 if (err && *new_te) {
4903 free(*new_te);
4904 *new_te = NULL;
4906 return err;
4909 static const struct got_error *
4910 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4911 struct got_commitable *ct)
4913 const struct got_error *err = NULL;
4914 char *ct_name = NULL;
4916 *new_te = NULL;
4918 *new_te = calloc(1, sizeof(**new_te));
4919 if (*new_te == NULL)
4920 return got_error_from_errno("calloc");
4922 err = got_path_basename(&ct_name, ct->path);
4923 if (err)
4924 goto done;
4925 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4926 sizeof((*new_te)->name)) {
4927 err = got_error(GOT_ERR_NO_SPACE);
4928 goto done;
4931 (*new_te)->mode = get_ct_file_mode(ct);
4933 if (ct->staged_status == GOT_STATUS_ADD)
4934 memcpy(&(*new_te)->id, ct->staged_blob_id,
4935 sizeof((*new_te)->id));
4936 else
4937 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4938 done:
4939 free(ct_name);
4940 if (err && *new_te) {
4941 free(*new_te);
4942 *new_te = NULL;
4944 return err;
4947 static const struct got_error *
4948 insert_tree_entry(struct got_tree_entry *new_te,
4949 struct got_pathlist_head *paths)
4951 const struct got_error *err = NULL;
4952 struct got_pathlist_entry *new_pe;
4954 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4955 if (err)
4956 return err;
4957 if (new_pe == NULL)
4958 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4959 return NULL;
4962 static const struct got_error *
4963 report_ct_status(struct got_commitable *ct,
4964 got_worktree_status_cb status_cb, void *status_arg)
4966 const char *ct_path = ct->path;
4967 unsigned char status;
4969 while (ct_path[0] == '/')
4970 ct_path++;
4972 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4973 status = ct->staged_status;
4974 else
4975 status = ct->status;
4977 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4978 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4981 static const struct got_error *
4982 match_modified_subtree(int *modified, struct got_tree_entry *te,
4983 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4985 const struct got_error *err = NULL;
4986 struct got_pathlist_entry *pe;
4987 char *te_path;
4989 *modified = 0;
4991 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4992 got_path_is_root_dir(base_tree_path) ? "" : "/",
4993 te->name) == -1)
4994 return got_error_from_errno("asprintf");
4996 TAILQ_FOREACH(pe, commitable_paths, entry) {
4997 struct got_commitable *ct = pe->data;
4998 *modified = got_path_is_child(ct->in_repo_path, te_path,
4999 strlen(te_path));
5000 if (*modified)
5001 break;
5004 free(te_path);
5005 return err;
5008 static const struct got_error *
5009 match_deleted_or_modified_ct(struct got_commitable **ctp,
5010 struct got_tree_entry *te, const char *base_tree_path,
5011 struct got_pathlist_head *commitable_paths)
5013 const struct got_error *err = NULL;
5014 struct got_pathlist_entry *pe;
5016 *ctp = NULL;
5018 TAILQ_FOREACH(pe, commitable_paths, entry) {
5019 struct got_commitable *ct = pe->data;
5020 char *ct_name = NULL;
5021 int path_matches;
5023 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5024 if (ct->status != GOT_STATUS_MODIFY &&
5025 ct->status != GOT_STATUS_MODE_CHANGE &&
5026 ct->status != GOT_STATUS_DELETE)
5027 continue;
5028 } else {
5029 if (ct->staged_status != GOT_STATUS_MODIFY &&
5030 ct->staged_status != GOT_STATUS_DELETE)
5031 continue;
5034 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5035 continue;
5037 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5038 if (err)
5039 return err;
5040 if (!path_matches)
5041 continue;
5043 err = got_path_basename(&ct_name, pe->path);
5044 if (err)
5045 return err;
5047 if (strcmp(te->name, ct_name) != 0) {
5048 free(ct_name);
5049 continue;
5051 free(ct_name);
5053 *ctp = ct;
5054 break;
5057 return err;
5060 static const struct got_error *
5061 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5062 const char *child_path, const char *path_base_tree,
5063 struct got_pathlist_head *commitable_paths,
5064 got_worktree_status_cb status_cb, void *status_arg,
5065 struct got_repository *repo)
5067 const struct got_error *err = NULL;
5068 struct got_tree_entry *new_te;
5069 char *subtree_path;
5070 struct got_object_id *id = NULL;
5071 int nentries;
5073 *new_tep = NULL;
5075 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5076 got_path_is_root_dir(path_base_tree) ? "" : "/",
5077 child_path) == -1)
5078 return got_error_from_errno("asprintf");
5080 new_te = calloc(1, sizeof(*new_te));
5081 if (new_te == NULL)
5082 return got_error_from_errno("calloc");
5083 new_te->mode = S_IFDIR;
5085 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5086 sizeof(new_te->name)) {
5087 err = got_error(GOT_ERR_NO_SPACE);
5088 goto done;
5090 err = write_tree(&id, &nentries, NULL, subtree_path,
5091 commitable_paths, status_cb, status_arg, repo);
5092 if (err) {
5093 free(new_te);
5094 goto done;
5096 memcpy(&new_te->id, id, sizeof(new_te->id));
5097 done:
5098 free(id);
5099 free(subtree_path);
5100 if (err == NULL)
5101 *new_tep = new_te;
5102 return err;
5105 static const struct got_error *
5106 write_tree(struct got_object_id **new_tree_id, int *nentries,
5107 struct got_tree_object *base_tree, const char *path_base_tree,
5108 struct got_pathlist_head *commitable_paths,
5109 got_worktree_status_cb status_cb, void *status_arg,
5110 struct got_repository *repo)
5112 const struct got_error *err = NULL;
5113 struct got_pathlist_head paths;
5114 struct got_tree_entry *te, *new_te = NULL;
5115 struct got_pathlist_entry *pe;
5117 TAILQ_INIT(&paths);
5118 *nentries = 0;
5120 /* Insert, and recurse into, newly added entries first. */
5121 TAILQ_FOREACH(pe, commitable_paths, entry) {
5122 struct got_commitable *ct = pe->data;
5123 char *child_path = NULL, *slash;
5125 if ((ct->status != GOT_STATUS_ADD &&
5126 ct->staged_status != GOT_STATUS_ADD) ||
5127 (ct->flags & GOT_COMMITABLE_ADDED))
5128 continue;
5130 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5131 strlen(path_base_tree)))
5132 continue;
5134 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5135 ct->in_repo_path);
5136 if (err)
5137 goto done;
5139 slash = strchr(child_path, '/');
5140 if (slash == NULL) {
5141 err = alloc_added_blob_tree_entry(&new_te, ct);
5142 if (err)
5143 goto done;
5144 err = report_ct_status(ct, status_cb, status_arg);
5145 if (err)
5146 goto done;
5147 ct->flags |= GOT_COMMITABLE_ADDED;
5148 err = insert_tree_entry(new_te, &paths);
5149 if (err)
5150 goto done;
5151 (*nentries)++;
5152 } else {
5153 *slash = '\0'; /* trim trailing path components */
5154 if (base_tree == NULL ||
5155 got_object_tree_find_entry(base_tree, child_path)
5156 == NULL) {
5157 err = make_subtree_for_added_blob(&new_te,
5158 child_path, path_base_tree,
5159 commitable_paths, status_cb, status_arg,
5160 repo);
5161 if (err)
5162 goto done;
5163 err = insert_tree_entry(new_te, &paths);
5164 if (err)
5165 goto done;
5166 (*nentries)++;
5171 if (base_tree) {
5172 int i, nbase_entries;
5173 /* Handle modified and deleted entries. */
5174 nbase_entries = got_object_tree_get_nentries(base_tree);
5175 for (i = 0; i < nbase_entries; i++) {
5176 struct got_commitable *ct = NULL;
5178 te = got_object_tree_get_entry(base_tree, i);
5179 if (got_object_tree_entry_is_submodule(te)) {
5180 /* Entry is a submodule; just copy it. */
5181 err = got_object_tree_entry_dup(&new_te, te);
5182 if (err)
5183 goto done;
5184 err = insert_tree_entry(new_te, &paths);
5185 if (err)
5186 goto done;
5187 (*nentries)++;
5188 continue;
5191 if (S_ISDIR(te->mode)) {
5192 int modified;
5193 err = got_object_tree_entry_dup(&new_te, te);
5194 if (err)
5195 goto done;
5196 err = match_modified_subtree(&modified, te,
5197 path_base_tree, commitable_paths);
5198 if (err)
5199 goto done;
5200 /* Avoid recursion into unmodified subtrees. */
5201 if (modified) {
5202 struct got_object_id *new_id;
5203 int nsubentries;
5204 err = write_subtree(&new_id,
5205 &nsubentries, te,
5206 path_base_tree, commitable_paths,
5207 status_cb, status_arg, repo);
5208 if (err)
5209 goto done;
5210 if (nsubentries == 0) {
5211 /* All entries were deleted. */
5212 free(new_id);
5213 continue;
5215 memcpy(&new_te->id, new_id,
5216 sizeof(new_te->id));
5217 free(new_id);
5219 err = insert_tree_entry(new_te, &paths);
5220 if (err)
5221 goto done;
5222 (*nentries)++;
5223 continue;
5226 err = match_deleted_or_modified_ct(&ct, te,
5227 path_base_tree, commitable_paths);
5228 if (err)
5229 goto done;
5230 if (ct) {
5231 /* NB: Deleted entries get dropped here. */
5232 if (ct->status == GOT_STATUS_MODIFY ||
5233 ct->status == GOT_STATUS_MODE_CHANGE ||
5234 ct->staged_status == GOT_STATUS_MODIFY) {
5235 err = alloc_modified_blob_tree_entry(
5236 &new_te, te, ct);
5237 if (err)
5238 goto done;
5239 err = insert_tree_entry(new_te, &paths);
5240 if (err)
5241 goto done;
5242 (*nentries)++;
5244 err = report_ct_status(ct, status_cb,
5245 status_arg);
5246 if (err)
5247 goto done;
5248 } else {
5249 /* Entry is unchanged; just copy it. */
5250 err = got_object_tree_entry_dup(&new_te, te);
5251 if (err)
5252 goto done;
5253 err = insert_tree_entry(new_te, &paths);
5254 if (err)
5255 goto done;
5256 (*nentries)++;
5261 /* Write new list of entries; deleted entries have been dropped. */
5262 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5263 done:
5264 got_pathlist_free(&paths);
5265 return err;
5268 static const struct got_error *
5269 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5270 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5271 int have_staged_files)
5273 const struct got_error *err = NULL;
5274 struct got_pathlist_entry *pe;
5276 TAILQ_FOREACH(pe, commitable_paths, entry) {
5277 struct got_fileindex_entry *ie;
5278 struct got_commitable *ct = pe->data;
5280 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5281 if (ie) {
5282 if (ct->status == GOT_STATUS_DELETE ||
5283 ct->staged_status == GOT_STATUS_DELETE) {
5284 got_fileindex_entry_remove(fileindex, ie);
5285 } else if (ct->staged_status == GOT_STATUS_ADD ||
5286 ct->staged_status == GOT_STATUS_MODIFY) {
5287 got_fileindex_entry_stage_set(ie,
5288 GOT_FILEIDX_STAGE_NONE);
5289 err = got_fileindex_entry_update(ie,
5290 ct->ondisk_path, ct->staged_blob_id->sha1,
5291 new_base_commit_id->sha1,
5292 !have_staged_files);
5293 } else
5294 err = got_fileindex_entry_update(ie,
5295 ct->ondisk_path, ct->blob_id->sha1,
5296 new_base_commit_id->sha1,
5297 !have_staged_files);
5298 } else {
5299 err = got_fileindex_entry_alloc(&ie, pe->path);
5300 if (err)
5301 break;
5302 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5303 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5304 if (err) {
5305 got_fileindex_entry_free(ie);
5306 break;
5308 err = got_fileindex_entry_add(fileindex, ie);
5309 if (err) {
5310 got_fileindex_entry_free(ie);
5311 break;
5315 return err;
5319 static const struct got_error *
5320 check_out_of_date(const char *in_repo_path, unsigned char status,
5321 unsigned char staged_status, struct got_object_id *base_blob_id,
5322 struct got_object_id *base_commit_id,
5323 struct got_object_id *head_commit_id, struct got_repository *repo,
5324 int ood_errcode)
5326 const struct got_error *err = NULL;
5327 struct got_object_id *id = NULL;
5329 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5330 /* Trivial case: base commit == head commit */
5331 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5332 return NULL;
5334 * Ensure file content which local changes were based
5335 * on matches file content in the branch head.
5337 err = got_object_id_by_path(&id, repo, head_commit_id,
5338 in_repo_path);
5339 if (err) {
5340 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5341 err = got_error(ood_errcode);
5342 goto done;
5343 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5344 err = got_error(ood_errcode);
5345 } else {
5346 /* Require that added files don't exist in the branch head. */
5347 err = got_object_id_by_path(&id, repo, head_commit_id,
5348 in_repo_path);
5349 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5350 goto done;
5351 err = id ? got_error(ood_errcode) : NULL;
5353 done:
5354 free(id);
5355 return err;
5358 const struct got_error *
5359 commit_worktree(struct got_object_id **new_commit_id,
5360 struct got_pathlist_head *commitable_paths,
5361 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5362 const char *author, const char *committer,
5363 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5364 got_worktree_status_cb status_cb, void *status_arg,
5365 struct got_repository *repo)
5367 const struct got_error *err = NULL, *unlockerr = NULL;
5368 struct got_pathlist_entry *pe;
5369 const char *head_ref_name = NULL;
5370 struct got_commit_object *head_commit = NULL;
5371 struct got_reference *head_ref2 = NULL;
5372 struct got_object_id *head_commit_id2 = NULL;
5373 struct got_tree_object *head_tree = NULL;
5374 struct got_object_id *new_tree_id = NULL;
5375 int nentries;
5376 struct got_object_id_queue parent_ids;
5377 struct got_object_qid *pid = NULL;
5378 char *logmsg = NULL;
5380 *new_commit_id = NULL;
5382 SIMPLEQ_INIT(&parent_ids);
5384 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5385 if (err)
5386 goto done;
5388 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5389 if (err)
5390 goto done;
5392 if (commit_msg_cb != NULL) {
5393 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5394 if (err)
5395 goto done;
5398 if (logmsg == NULL || strlen(logmsg) == 0) {
5399 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5400 goto done;
5403 /* Create blobs from added and modified files and record their IDs. */
5404 TAILQ_FOREACH(pe, commitable_paths, entry) {
5405 struct got_commitable *ct = pe->data;
5406 char *ondisk_path;
5408 /* Blobs for staged files already exist. */
5409 if (ct->staged_status == GOT_STATUS_ADD ||
5410 ct->staged_status == GOT_STATUS_MODIFY)
5411 continue;
5413 if (ct->status != GOT_STATUS_ADD &&
5414 ct->status != GOT_STATUS_MODIFY &&
5415 ct->status != GOT_STATUS_MODE_CHANGE)
5416 continue;
5418 if (asprintf(&ondisk_path, "%s/%s",
5419 worktree->root_path, pe->path) == -1) {
5420 err = got_error_from_errno("asprintf");
5421 goto done;
5423 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5424 free(ondisk_path);
5425 if (err)
5426 goto done;
5429 /* Recursively write new tree objects. */
5430 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5431 commitable_paths, status_cb, status_arg, repo);
5432 if (err)
5433 goto done;
5435 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5436 if (err)
5437 goto done;
5438 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5439 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5440 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5441 got_object_qid_free(pid);
5442 if (logmsg != NULL)
5443 free(logmsg);
5444 if (err)
5445 goto done;
5447 /* Check if a concurrent commit to our branch has occurred. */
5448 head_ref_name = got_worktree_get_head_ref_name(worktree);
5449 if (head_ref_name == NULL) {
5450 err = got_error_from_errno("got_worktree_get_head_ref_name");
5451 goto done;
5453 /* Lock the reference here to prevent concurrent modification. */
5454 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5455 if (err)
5456 goto done;
5457 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5458 if (err)
5459 goto done;
5460 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5461 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5462 goto done;
5464 /* Update branch head in repository. */
5465 err = got_ref_change_ref(head_ref2, *new_commit_id);
5466 if (err)
5467 goto done;
5468 err = got_ref_write(head_ref2, repo);
5469 if (err)
5470 goto done;
5472 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5473 if (err)
5474 goto done;
5476 err = ref_base_commit(worktree, repo);
5477 if (err)
5478 goto done;
5479 done:
5480 if (head_tree)
5481 got_object_tree_close(head_tree);
5482 if (head_commit)
5483 got_object_commit_close(head_commit);
5484 free(head_commit_id2);
5485 if (head_ref2) {
5486 unlockerr = got_ref_unlock(head_ref2);
5487 if (unlockerr && err == NULL)
5488 err = unlockerr;
5489 got_ref_close(head_ref2);
5491 return err;
5494 static const struct got_error *
5495 check_path_is_commitable(const char *path,
5496 struct got_pathlist_head *commitable_paths)
5498 struct got_pathlist_entry *cpe = NULL;
5499 size_t path_len = strlen(path);
5501 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5502 struct got_commitable *ct = cpe->data;
5503 const char *ct_path = ct->path;
5505 while (ct_path[0] == '/')
5506 ct_path++;
5508 if (strcmp(path, ct_path) == 0 ||
5509 got_path_is_child(ct_path, path, path_len))
5510 break;
5513 if (cpe == NULL)
5514 return got_error_path(path, GOT_ERR_BAD_PATH);
5516 return NULL;
5519 static const struct got_error *
5520 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5522 int *have_staged_files = arg;
5524 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5525 *have_staged_files = 1;
5526 return got_error(GOT_ERR_CANCELLED);
5529 return NULL;
5532 static const struct got_error *
5533 check_non_staged_files(struct got_fileindex *fileindex,
5534 struct got_pathlist_head *paths)
5536 struct got_pathlist_entry *pe;
5537 struct got_fileindex_entry *ie;
5539 TAILQ_FOREACH(pe, paths, entry) {
5540 if (pe->path[0] == '\0')
5541 continue;
5542 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5543 if (ie == NULL)
5544 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5545 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5546 return got_error_path(pe->path,
5547 GOT_ERR_FILE_NOT_STAGED);
5550 return NULL;
5553 const struct got_error *
5554 got_worktree_commit(struct got_object_id **new_commit_id,
5555 struct got_worktree *worktree, struct got_pathlist_head *paths,
5556 const char *author, const char *committer, int allow_bad_symlinks,
5557 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5558 got_worktree_status_cb status_cb, void *status_arg,
5559 struct got_repository *repo)
5561 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5562 struct got_fileindex *fileindex = NULL;
5563 char *fileindex_path = NULL;
5564 struct got_pathlist_head commitable_paths;
5565 struct collect_commitables_arg cc_arg;
5566 struct got_pathlist_entry *pe;
5567 struct got_reference *head_ref = NULL;
5568 struct got_object_id *head_commit_id = NULL;
5569 int have_staged_files = 0;
5571 *new_commit_id = NULL;
5573 TAILQ_INIT(&commitable_paths);
5575 err = lock_worktree(worktree, LOCK_EX);
5576 if (err)
5577 goto done;
5579 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5580 if (err)
5581 goto done;
5583 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5584 if (err)
5585 goto done;
5587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5588 if (err)
5589 goto done;
5591 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5592 &have_staged_files);
5593 if (err && err->code != GOT_ERR_CANCELLED)
5594 goto done;
5595 if (have_staged_files) {
5596 err = check_non_staged_files(fileindex, paths);
5597 if (err)
5598 goto done;
5601 cc_arg.commitable_paths = &commitable_paths;
5602 cc_arg.worktree = worktree;
5603 cc_arg.fileindex = fileindex;
5604 cc_arg.repo = repo;
5605 cc_arg.have_staged_files = have_staged_files;
5606 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5607 TAILQ_FOREACH(pe, paths, entry) {
5608 err = worktree_status(worktree, pe->path, fileindex, repo,
5609 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5610 if (err)
5611 goto done;
5614 if (TAILQ_EMPTY(&commitable_paths)) {
5615 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5616 goto done;
5619 TAILQ_FOREACH(pe, paths, entry) {
5620 err = check_path_is_commitable(pe->path, &commitable_paths);
5621 if (err)
5622 goto done;
5625 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5626 struct got_commitable *ct = pe->data;
5627 const char *ct_path = ct->in_repo_path;
5629 while (ct_path[0] == '/')
5630 ct_path++;
5631 err = check_out_of_date(ct_path, ct->status,
5632 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5633 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5634 if (err)
5635 goto done;
5639 err = commit_worktree(new_commit_id, &commitable_paths,
5640 head_commit_id, worktree, author, committer,
5641 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5642 if (err)
5643 goto done;
5645 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5646 fileindex, have_staged_files);
5647 sync_err = sync_fileindex(fileindex, fileindex_path);
5648 if (sync_err && err == NULL)
5649 err = sync_err;
5650 done:
5651 if (fileindex)
5652 got_fileindex_free(fileindex);
5653 free(fileindex_path);
5654 unlockerr = lock_worktree(worktree, LOCK_SH);
5655 if (unlockerr && err == NULL)
5656 err = unlockerr;
5657 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5658 struct got_commitable *ct = pe->data;
5659 free_commitable(ct);
5661 got_pathlist_free(&commitable_paths);
5662 return err;
5665 const char *
5666 got_commitable_get_path(struct got_commitable *ct)
5668 return ct->path;
5671 unsigned int
5672 got_commitable_get_status(struct got_commitable *ct)
5674 return ct->status;
5677 struct check_rebase_ok_arg {
5678 struct got_worktree *worktree;
5679 struct got_repository *repo;
5682 static const struct got_error *
5683 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5685 const struct got_error *err = NULL;
5686 struct check_rebase_ok_arg *a = arg;
5687 unsigned char status;
5688 struct stat sb;
5689 char *ondisk_path;
5691 /* Reject rebase of a work tree with mixed base commits. */
5692 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5693 SHA1_DIGEST_LENGTH))
5694 return got_error(GOT_ERR_MIXED_COMMITS);
5696 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5697 == -1)
5698 return got_error_from_errno("asprintf");
5700 /* Reject rebase of a work tree with modified or staged files. */
5701 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5702 free(ondisk_path);
5703 if (err)
5704 return err;
5706 if (status != GOT_STATUS_NO_CHANGE)
5707 return got_error(GOT_ERR_MODIFIED);
5708 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5709 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5711 return NULL;
5714 const struct got_error *
5715 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5716 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5717 struct got_worktree *worktree, struct got_reference *branch,
5718 struct got_repository *repo)
5720 const struct got_error *err = NULL;
5721 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5722 char *branch_ref_name = NULL;
5723 char *fileindex_path = NULL;
5724 struct check_rebase_ok_arg ok_arg;
5725 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5726 struct got_object_id *wt_branch_tip = NULL;
5728 *new_base_branch_ref = NULL;
5729 *tmp_branch = NULL;
5730 *fileindex = NULL;
5732 err = lock_worktree(worktree, LOCK_EX);
5733 if (err)
5734 return err;
5736 err = open_fileindex(fileindex, &fileindex_path, worktree);
5737 if (err)
5738 goto done;
5740 ok_arg.worktree = worktree;
5741 ok_arg.repo = repo;
5742 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5743 &ok_arg);
5744 if (err)
5745 goto done;
5747 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5748 if (err)
5749 goto done;
5751 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5752 if (err)
5753 goto done;
5755 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5756 if (err)
5757 goto done;
5759 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5760 0);
5761 if (err)
5762 goto done;
5764 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5765 if (err)
5766 goto done;
5767 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5768 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5769 goto done;
5772 err = got_ref_alloc_symref(new_base_branch_ref,
5773 new_base_branch_ref_name, wt_branch);
5774 if (err)
5775 goto done;
5776 err = got_ref_write(*new_base_branch_ref, repo);
5777 if (err)
5778 goto done;
5780 /* TODO Lock original branch's ref while rebasing? */
5782 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5783 if (err)
5784 goto done;
5786 err = got_ref_write(branch_ref, repo);
5787 if (err)
5788 goto done;
5790 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5791 worktree->base_commit_id);
5792 if (err)
5793 goto done;
5794 err = got_ref_write(*tmp_branch, repo);
5795 if (err)
5796 goto done;
5798 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5799 if (err)
5800 goto done;
5801 done:
5802 free(fileindex_path);
5803 free(tmp_branch_name);
5804 free(new_base_branch_ref_name);
5805 free(branch_ref_name);
5806 if (branch_ref)
5807 got_ref_close(branch_ref);
5808 if (wt_branch)
5809 got_ref_close(wt_branch);
5810 free(wt_branch_tip);
5811 if (err) {
5812 if (*new_base_branch_ref) {
5813 got_ref_close(*new_base_branch_ref);
5814 *new_base_branch_ref = NULL;
5816 if (*tmp_branch) {
5817 got_ref_close(*tmp_branch);
5818 *tmp_branch = NULL;
5820 if (*fileindex) {
5821 got_fileindex_free(*fileindex);
5822 *fileindex = NULL;
5824 lock_worktree(worktree, LOCK_SH);
5826 return err;
5829 const struct got_error *
5830 got_worktree_rebase_continue(struct got_object_id **commit_id,
5831 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5832 struct got_reference **branch, struct got_fileindex **fileindex,
5833 struct got_worktree *worktree, struct got_repository *repo)
5835 const struct got_error *err;
5836 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5837 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5838 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5839 char *fileindex_path = NULL;
5840 int have_staged_files = 0;
5842 *commit_id = NULL;
5843 *new_base_branch = NULL;
5844 *tmp_branch = NULL;
5845 *branch = NULL;
5846 *fileindex = NULL;
5848 err = lock_worktree(worktree, LOCK_EX);
5849 if (err)
5850 return err;
5852 err = open_fileindex(fileindex, &fileindex_path, worktree);
5853 if (err)
5854 goto done;
5856 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5857 &have_staged_files);
5858 if (err && err->code != GOT_ERR_CANCELLED)
5859 goto done;
5860 if (have_staged_files) {
5861 err = got_error(GOT_ERR_STAGED_PATHS);
5862 goto done;
5865 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5866 if (err)
5867 goto done;
5869 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5870 if (err)
5871 goto done;
5873 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5874 if (err)
5875 goto done;
5877 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5878 if (err)
5879 goto done;
5881 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5882 if (err)
5883 goto done;
5885 err = got_ref_open(branch, repo,
5886 got_ref_get_symref_target(branch_ref), 0);
5887 if (err)
5888 goto done;
5890 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5891 if (err)
5892 goto done;
5894 err = got_ref_resolve(commit_id, repo, commit_ref);
5895 if (err)
5896 goto done;
5898 err = got_ref_open(new_base_branch, repo,
5899 new_base_branch_ref_name, 0);
5900 if (err)
5901 goto done;
5903 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5904 if (err)
5905 goto done;
5906 done:
5907 free(commit_ref_name);
5908 free(branch_ref_name);
5909 free(fileindex_path);
5910 if (commit_ref)
5911 got_ref_close(commit_ref);
5912 if (branch_ref)
5913 got_ref_close(branch_ref);
5914 if (err) {
5915 free(*commit_id);
5916 *commit_id = NULL;
5917 if (*tmp_branch) {
5918 got_ref_close(*tmp_branch);
5919 *tmp_branch = NULL;
5921 if (*new_base_branch) {
5922 got_ref_close(*new_base_branch);
5923 *new_base_branch = NULL;
5925 if (*branch) {
5926 got_ref_close(*branch);
5927 *branch = NULL;
5929 if (*fileindex) {
5930 got_fileindex_free(*fileindex);
5931 *fileindex = NULL;
5933 lock_worktree(worktree, LOCK_SH);
5935 return err;
5938 const struct got_error *
5939 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5941 const struct got_error *err;
5942 char *tmp_branch_name = NULL;
5944 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5945 if (err)
5946 return err;
5948 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5949 free(tmp_branch_name);
5950 return NULL;
5953 static const struct got_error *
5954 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5955 char **logmsg, void *arg)
5957 *logmsg = arg;
5958 return NULL;
5961 static const struct got_error *
5962 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5963 const char *path, struct got_object_id *blob_id,
5964 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5965 int dirfd, const char *de_name)
5967 return NULL;
5970 struct collect_merged_paths_arg {
5971 got_worktree_checkout_cb progress_cb;
5972 void *progress_arg;
5973 struct got_pathlist_head *merged_paths;
5976 static const struct got_error *
5977 collect_merged_paths(void *arg, unsigned char status, const char *path)
5979 const struct got_error *err;
5980 struct collect_merged_paths_arg *a = arg;
5981 char *p;
5982 struct got_pathlist_entry *new;
5984 err = (*a->progress_cb)(a->progress_arg, status, path);
5985 if (err)
5986 return err;
5988 if (status != GOT_STATUS_MERGE &&
5989 status != GOT_STATUS_ADD &&
5990 status != GOT_STATUS_DELETE &&
5991 status != GOT_STATUS_CONFLICT)
5992 return NULL;
5994 p = strdup(path);
5995 if (p == NULL)
5996 return got_error_from_errno("strdup");
5998 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5999 if (err || new == NULL)
6000 free(p);
6001 return err;
6004 void
6005 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6007 struct got_pathlist_entry *pe;
6009 TAILQ_FOREACH(pe, merged_paths, entry)
6010 free((char *)pe->path);
6012 got_pathlist_free(merged_paths);
6015 static const struct got_error *
6016 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6017 int is_rebase, struct got_repository *repo)
6019 const struct got_error *err;
6020 struct got_reference *commit_ref = NULL;
6022 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6023 if (err) {
6024 if (err->code != GOT_ERR_NOT_REF)
6025 goto done;
6026 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6027 if (err)
6028 goto done;
6029 err = got_ref_write(commit_ref, repo);
6030 if (err)
6031 goto done;
6032 } else if (is_rebase) {
6033 struct got_object_id *stored_id;
6034 int cmp;
6036 err = got_ref_resolve(&stored_id, repo, commit_ref);
6037 if (err)
6038 goto done;
6039 cmp = got_object_id_cmp(commit_id, stored_id);
6040 free(stored_id);
6041 if (cmp != 0) {
6042 err = got_error(GOT_ERR_REBASE_COMMITID);
6043 goto done;
6046 done:
6047 if (commit_ref)
6048 got_ref_close(commit_ref);
6049 return err;
6052 static const struct got_error *
6053 rebase_merge_files(struct got_pathlist_head *merged_paths,
6054 const char *commit_ref_name, struct got_worktree *worktree,
6055 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6056 struct got_object_id *commit_id, struct got_repository *repo,
6057 got_worktree_checkout_cb progress_cb, void *progress_arg,
6058 got_cancel_cb cancel_cb, void *cancel_arg)
6060 const struct got_error *err;
6061 struct got_reference *commit_ref = NULL;
6062 struct collect_merged_paths_arg cmp_arg;
6063 char *fileindex_path;
6065 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6067 err = get_fileindex_path(&fileindex_path, worktree);
6068 if (err)
6069 return err;
6071 cmp_arg.progress_cb = progress_cb;
6072 cmp_arg.progress_arg = progress_arg;
6073 cmp_arg.merged_paths = merged_paths;
6074 err = merge_files(worktree, fileindex, fileindex_path,
6075 parent_commit_id, commit_id, repo, collect_merged_paths,
6076 &cmp_arg, cancel_cb, cancel_arg);
6077 if (commit_ref)
6078 got_ref_close(commit_ref);
6079 return err;
6082 const struct got_error *
6083 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6084 struct got_worktree *worktree, struct got_fileindex *fileindex,
6085 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6086 struct got_repository *repo,
6087 got_worktree_checkout_cb progress_cb, void *progress_arg,
6088 got_cancel_cb cancel_cb, void *cancel_arg)
6090 const struct got_error *err;
6091 char *commit_ref_name;
6093 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6094 if (err)
6095 return err;
6097 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6098 if (err)
6099 goto done;
6101 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6102 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6103 progress_arg, cancel_cb, cancel_arg);
6104 done:
6105 free(commit_ref_name);
6106 return err;
6109 const struct got_error *
6110 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6111 struct got_worktree *worktree, struct got_fileindex *fileindex,
6112 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6113 struct got_repository *repo,
6114 got_worktree_checkout_cb progress_cb, void *progress_arg,
6115 got_cancel_cb cancel_cb, void *cancel_arg)
6117 const struct got_error *err;
6118 char *commit_ref_name;
6120 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6121 if (err)
6122 return err;
6124 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6125 if (err)
6126 goto done;
6128 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6129 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6130 progress_arg, cancel_cb, cancel_arg);
6131 done:
6132 free(commit_ref_name);
6133 return err;
6136 static const struct got_error *
6137 rebase_commit(struct got_object_id **new_commit_id,
6138 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6139 struct got_worktree *worktree, struct got_fileindex *fileindex,
6140 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6141 const char *new_logmsg, struct got_repository *repo)
6143 const struct got_error *err, *sync_err;
6144 struct got_pathlist_head commitable_paths;
6145 struct collect_commitables_arg cc_arg;
6146 char *fileindex_path = NULL;
6147 struct got_reference *head_ref = NULL;
6148 struct got_object_id *head_commit_id = NULL;
6149 char *logmsg = NULL;
6151 TAILQ_INIT(&commitable_paths);
6152 *new_commit_id = NULL;
6154 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6156 err = get_fileindex_path(&fileindex_path, worktree);
6157 if (err)
6158 return err;
6160 cc_arg.commitable_paths = &commitable_paths;
6161 cc_arg.worktree = worktree;
6162 cc_arg.repo = repo;
6163 cc_arg.have_staged_files = 0;
6165 * If possible get the status of individual files directly to
6166 * avoid crawling the entire work tree once per rebased commit.
6167 * TODO: Ideally, merged_paths would contain a list of commitables
6168 * we could use so we could skip worktree_status() entirely.
6170 if (merged_paths) {
6171 struct got_pathlist_entry *pe;
6172 TAILQ_FOREACH(pe, merged_paths, entry) {
6173 err = worktree_status(worktree, pe->path, fileindex,
6174 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6175 0);
6176 if (err)
6177 goto done;
6179 } else {
6180 err = worktree_status(worktree, "", fileindex, repo,
6181 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6182 if (err)
6183 goto done;
6186 if (TAILQ_EMPTY(&commitable_paths)) {
6187 /* No-op change; commit will be elided. */
6188 err = got_ref_delete(commit_ref, repo);
6189 if (err)
6190 goto done;
6191 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6192 goto done;
6195 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6196 if (err)
6197 goto done;
6199 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6200 if (err)
6201 goto done;
6203 if (new_logmsg) {
6204 logmsg = strdup(new_logmsg);
6205 if (logmsg == NULL) {
6206 err = got_error_from_errno("strdup");
6207 goto done;
6209 } else {
6210 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6211 if (err)
6212 goto done;
6215 /* NB: commit_worktree will call free(logmsg) */
6216 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6217 worktree, got_object_commit_get_author(orig_commit),
6218 got_object_commit_get_committer(orig_commit),
6219 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6220 if (err)
6221 goto done;
6223 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6224 if (err)
6225 goto done;
6227 err = got_ref_delete(commit_ref, repo);
6228 if (err)
6229 goto done;
6231 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6232 fileindex, 0);
6233 sync_err = sync_fileindex(fileindex, fileindex_path);
6234 if (sync_err && err == NULL)
6235 err = sync_err;
6236 done:
6237 free(fileindex_path);
6238 free(head_commit_id);
6239 if (head_ref)
6240 got_ref_close(head_ref);
6241 if (err) {
6242 free(*new_commit_id);
6243 *new_commit_id = NULL;
6245 return err;
6248 const struct got_error *
6249 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6250 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6251 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6252 struct got_commit_object *orig_commit,
6253 struct got_object_id *orig_commit_id, struct got_repository *repo)
6255 const struct got_error *err;
6256 char *commit_ref_name;
6257 struct got_reference *commit_ref = NULL;
6258 struct got_object_id *commit_id = NULL;
6260 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6261 if (err)
6262 return err;
6264 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6265 if (err)
6266 goto done;
6267 err = got_ref_resolve(&commit_id, repo, commit_ref);
6268 if (err)
6269 goto done;
6270 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6271 err = got_error(GOT_ERR_REBASE_COMMITID);
6272 goto done;
6275 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6276 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6277 done:
6278 if (commit_ref)
6279 got_ref_close(commit_ref);
6280 free(commit_ref_name);
6281 free(commit_id);
6282 return err;
6285 const struct got_error *
6286 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6287 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6288 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6289 struct got_commit_object *orig_commit,
6290 struct got_object_id *orig_commit_id, const char *new_logmsg,
6291 struct got_repository *repo)
6293 const struct got_error *err;
6294 char *commit_ref_name;
6295 struct got_reference *commit_ref = NULL;
6297 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6298 if (err)
6299 return err;
6301 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6302 if (err)
6303 goto done;
6305 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6306 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6307 done:
6308 if (commit_ref)
6309 got_ref_close(commit_ref);
6310 free(commit_ref_name);
6311 return err;
6314 const struct got_error *
6315 got_worktree_rebase_postpone(struct got_worktree *worktree,
6316 struct got_fileindex *fileindex)
6318 if (fileindex)
6319 got_fileindex_free(fileindex);
6320 return lock_worktree(worktree, LOCK_SH);
6323 static const struct got_error *
6324 delete_ref(const char *name, struct got_repository *repo)
6326 const struct got_error *err;
6327 struct got_reference *ref;
6329 err = got_ref_open(&ref, repo, name, 0);
6330 if (err) {
6331 if (err->code == GOT_ERR_NOT_REF)
6332 return NULL;
6333 return err;
6336 err = got_ref_delete(ref, repo);
6337 got_ref_close(ref);
6338 return err;
6341 static const struct got_error *
6342 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6344 const struct got_error *err;
6345 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6346 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6348 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6349 if (err)
6350 goto done;
6351 err = delete_ref(tmp_branch_name, repo);
6352 if (err)
6353 goto done;
6355 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6356 if (err)
6357 goto done;
6358 err = delete_ref(new_base_branch_ref_name, repo);
6359 if (err)
6360 goto done;
6362 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6363 if (err)
6364 goto done;
6365 err = delete_ref(branch_ref_name, repo);
6366 if (err)
6367 goto done;
6369 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6370 if (err)
6371 goto done;
6372 err = delete_ref(commit_ref_name, repo);
6373 if (err)
6374 goto done;
6376 done:
6377 free(tmp_branch_name);
6378 free(new_base_branch_ref_name);
6379 free(branch_ref_name);
6380 free(commit_ref_name);
6381 return err;
6384 const struct got_error *
6385 got_worktree_rebase_complete(struct got_worktree *worktree,
6386 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6387 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6388 struct got_repository *repo)
6390 const struct got_error *err, *unlockerr;
6391 struct got_object_id *new_head_commit_id = NULL;
6393 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6394 if (err)
6395 return err;
6397 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6398 if (err)
6399 goto done;
6401 err = got_ref_write(rebased_branch, repo);
6402 if (err)
6403 goto done;
6405 err = got_worktree_set_head_ref(worktree, rebased_branch);
6406 if (err)
6407 goto done;
6409 err = delete_rebase_refs(worktree, repo);
6410 done:
6411 if (fileindex)
6412 got_fileindex_free(fileindex);
6413 free(new_head_commit_id);
6414 unlockerr = lock_worktree(worktree, LOCK_SH);
6415 if (unlockerr && err == NULL)
6416 err = unlockerr;
6417 return err;
6420 const struct got_error *
6421 got_worktree_rebase_abort(struct got_worktree *worktree,
6422 struct got_fileindex *fileindex, struct got_repository *repo,
6423 struct got_reference *new_base_branch,
6424 got_worktree_checkout_cb progress_cb, void *progress_arg)
6426 const struct got_error *err, *unlockerr, *sync_err;
6427 struct got_reference *resolved = NULL;
6428 struct got_object_id *commit_id = NULL;
6429 char *fileindex_path = NULL;
6430 struct revert_file_args rfa;
6431 struct got_object_id *tree_id = NULL;
6433 err = lock_worktree(worktree, LOCK_EX);
6434 if (err)
6435 return err;
6437 err = got_ref_open(&resolved, repo,
6438 got_ref_get_symref_target(new_base_branch), 0);
6439 if (err)
6440 goto done;
6442 err = got_worktree_set_head_ref(worktree, resolved);
6443 if (err)
6444 goto done;
6447 * XXX commits to the base branch could have happened while
6448 * we were busy rebasing; should we store the original commit ID
6449 * when rebase begins and read it back here?
6451 err = got_ref_resolve(&commit_id, repo, resolved);
6452 if (err)
6453 goto done;
6455 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6456 if (err)
6457 goto done;
6459 err = got_object_id_by_path(&tree_id, repo,
6460 worktree->base_commit_id, worktree->path_prefix);
6461 if (err)
6462 goto done;
6464 err = delete_rebase_refs(worktree, repo);
6465 if (err)
6466 goto done;
6468 err = get_fileindex_path(&fileindex_path, worktree);
6469 if (err)
6470 goto done;
6472 rfa.worktree = worktree;
6473 rfa.fileindex = fileindex;
6474 rfa.progress_cb = progress_cb;
6475 rfa.progress_arg = progress_arg;
6476 rfa.patch_cb = NULL;
6477 rfa.patch_arg = NULL;
6478 rfa.repo = repo;
6479 err = worktree_status(worktree, "", fileindex, repo,
6480 revert_file, &rfa, NULL, NULL, 0, 0);
6481 if (err)
6482 goto sync;
6484 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6485 repo, progress_cb, progress_arg, NULL, NULL);
6486 sync:
6487 sync_err = sync_fileindex(fileindex, fileindex_path);
6488 if (sync_err && err == NULL)
6489 err = sync_err;
6490 done:
6491 got_ref_close(resolved);
6492 free(tree_id);
6493 free(commit_id);
6494 if (fileindex)
6495 got_fileindex_free(fileindex);
6496 free(fileindex_path);
6498 unlockerr = lock_worktree(worktree, LOCK_SH);
6499 if (unlockerr && err == NULL)
6500 err = unlockerr;
6501 return err;
6504 const struct got_error *
6505 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6506 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6507 struct got_fileindex **fileindex, struct got_worktree *worktree,
6508 struct got_repository *repo)
6510 const struct got_error *err = NULL;
6511 char *tmp_branch_name = NULL;
6512 char *branch_ref_name = NULL;
6513 char *base_commit_ref_name = NULL;
6514 char *fileindex_path = NULL;
6515 struct check_rebase_ok_arg ok_arg;
6516 struct got_reference *wt_branch = NULL;
6517 struct got_reference *base_commit_ref = NULL;
6519 *tmp_branch = NULL;
6520 *branch_ref = NULL;
6521 *base_commit_id = NULL;
6522 *fileindex = NULL;
6524 err = lock_worktree(worktree, LOCK_EX);
6525 if (err)
6526 return err;
6528 err = open_fileindex(fileindex, &fileindex_path, worktree);
6529 if (err)
6530 goto done;
6532 ok_arg.worktree = worktree;
6533 ok_arg.repo = repo;
6534 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6535 &ok_arg);
6536 if (err)
6537 goto done;
6539 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6540 if (err)
6541 goto done;
6543 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6544 if (err)
6545 goto done;
6547 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6548 worktree);
6549 if (err)
6550 goto done;
6552 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6553 0);
6554 if (err)
6555 goto done;
6557 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6558 if (err)
6559 goto done;
6561 err = got_ref_write(*branch_ref, repo);
6562 if (err)
6563 goto done;
6565 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6566 worktree->base_commit_id);
6567 if (err)
6568 goto done;
6569 err = got_ref_write(base_commit_ref, repo);
6570 if (err)
6571 goto done;
6572 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6573 if (*base_commit_id == NULL) {
6574 err = got_error_from_errno("got_object_id_dup");
6575 goto done;
6578 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6579 worktree->base_commit_id);
6580 if (err)
6581 goto done;
6582 err = got_ref_write(*tmp_branch, repo);
6583 if (err)
6584 goto done;
6586 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6587 if (err)
6588 goto done;
6589 done:
6590 free(fileindex_path);
6591 free(tmp_branch_name);
6592 free(branch_ref_name);
6593 free(base_commit_ref_name);
6594 if (wt_branch)
6595 got_ref_close(wt_branch);
6596 if (err) {
6597 if (*branch_ref) {
6598 got_ref_close(*branch_ref);
6599 *branch_ref = NULL;
6601 if (*tmp_branch) {
6602 got_ref_close(*tmp_branch);
6603 *tmp_branch = NULL;
6605 free(*base_commit_id);
6606 if (*fileindex) {
6607 got_fileindex_free(*fileindex);
6608 *fileindex = NULL;
6610 lock_worktree(worktree, LOCK_SH);
6612 return err;
6615 const struct got_error *
6616 got_worktree_histedit_postpone(struct got_worktree *worktree,
6617 struct got_fileindex *fileindex)
6619 if (fileindex)
6620 got_fileindex_free(fileindex);
6621 return lock_worktree(worktree, LOCK_SH);
6624 const struct got_error *
6625 got_worktree_histedit_in_progress(int *in_progress,
6626 struct got_worktree *worktree)
6628 const struct got_error *err;
6629 char *tmp_branch_name = NULL;
6631 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6632 if (err)
6633 return err;
6635 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6636 free(tmp_branch_name);
6637 return NULL;
6640 const struct got_error *
6641 got_worktree_histedit_continue(struct got_object_id **commit_id,
6642 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6643 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6644 struct got_worktree *worktree, struct got_repository *repo)
6646 const struct got_error *err;
6647 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6648 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6649 struct got_reference *commit_ref = NULL;
6650 struct got_reference *base_commit_ref = NULL;
6651 char *fileindex_path = NULL;
6652 int have_staged_files = 0;
6654 *commit_id = NULL;
6655 *tmp_branch = NULL;
6656 *base_commit_id = NULL;
6657 *fileindex = NULL;
6659 err = lock_worktree(worktree, LOCK_EX);
6660 if (err)
6661 return err;
6663 err = open_fileindex(fileindex, &fileindex_path, worktree);
6664 if (err)
6665 goto done;
6667 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6668 &have_staged_files);
6669 if (err && err->code != GOT_ERR_CANCELLED)
6670 goto done;
6671 if (have_staged_files) {
6672 err = got_error(GOT_ERR_STAGED_PATHS);
6673 goto done;
6676 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6677 if (err)
6678 goto done;
6680 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6681 if (err)
6682 goto done;
6684 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6685 if (err)
6686 goto done;
6688 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6689 worktree);
6690 if (err)
6691 goto done;
6693 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6694 if (err)
6695 goto done;
6697 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6698 if (err)
6699 goto done;
6700 err = got_ref_resolve(commit_id, repo, commit_ref);
6701 if (err)
6702 goto done;
6704 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6705 if (err)
6706 goto done;
6707 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6708 if (err)
6709 goto done;
6711 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6712 if (err)
6713 goto done;
6714 done:
6715 free(commit_ref_name);
6716 free(branch_ref_name);
6717 free(fileindex_path);
6718 if (commit_ref)
6719 got_ref_close(commit_ref);
6720 if (base_commit_ref)
6721 got_ref_close(base_commit_ref);
6722 if (err) {
6723 free(*commit_id);
6724 *commit_id = NULL;
6725 free(*base_commit_id);
6726 *base_commit_id = NULL;
6727 if (*tmp_branch) {
6728 got_ref_close(*tmp_branch);
6729 *tmp_branch = NULL;
6731 if (*fileindex) {
6732 got_fileindex_free(*fileindex);
6733 *fileindex = NULL;
6735 lock_worktree(worktree, LOCK_EX);
6737 return err;
6740 static const struct got_error *
6741 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6743 const struct got_error *err;
6744 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6745 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6747 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6748 if (err)
6749 goto done;
6750 err = delete_ref(tmp_branch_name, repo);
6751 if (err)
6752 goto done;
6754 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6755 worktree);
6756 if (err)
6757 goto done;
6758 err = delete_ref(base_commit_ref_name, repo);
6759 if (err)
6760 goto done;
6762 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6763 if (err)
6764 goto done;
6765 err = delete_ref(branch_ref_name, repo);
6766 if (err)
6767 goto done;
6769 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6770 if (err)
6771 goto done;
6772 err = delete_ref(commit_ref_name, repo);
6773 if (err)
6774 goto done;
6775 done:
6776 free(tmp_branch_name);
6777 free(base_commit_ref_name);
6778 free(branch_ref_name);
6779 free(commit_ref_name);
6780 return err;
6783 const struct got_error *
6784 got_worktree_histedit_abort(struct got_worktree *worktree,
6785 struct got_fileindex *fileindex, struct got_repository *repo,
6786 struct got_reference *branch, struct got_object_id *base_commit_id,
6787 got_worktree_checkout_cb progress_cb, void *progress_arg)
6789 const struct got_error *err, *unlockerr, *sync_err;
6790 struct got_reference *resolved = NULL;
6791 char *fileindex_path = NULL;
6792 struct got_object_id *tree_id = NULL;
6793 struct revert_file_args rfa;
6795 err = lock_worktree(worktree, LOCK_EX);
6796 if (err)
6797 return err;
6799 err = got_ref_open(&resolved, repo,
6800 got_ref_get_symref_target(branch), 0);
6801 if (err)
6802 goto done;
6804 err = got_worktree_set_head_ref(worktree, resolved);
6805 if (err)
6806 goto done;
6808 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6809 if (err)
6810 goto done;
6812 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6813 worktree->path_prefix);
6814 if (err)
6815 goto done;
6817 err = delete_histedit_refs(worktree, repo);
6818 if (err)
6819 goto done;
6821 err = get_fileindex_path(&fileindex_path, worktree);
6822 if (err)
6823 goto done;
6825 rfa.worktree = worktree;
6826 rfa.fileindex = fileindex;
6827 rfa.progress_cb = progress_cb;
6828 rfa.progress_arg = progress_arg;
6829 rfa.patch_cb = NULL;
6830 rfa.patch_arg = NULL;
6831 rfa.repo = repo;
6832 err = worktree_status(worktree, "", fileindex, repo,
6833 revert_file, &rfa, NULL, NULL, 0, 0);
6834 if (err)
6835 goto sync;
6837 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6838 repo, progress_cb, progress_arg, NULL, NULL);
6839 sync:
6840 sync_err = sync_fileindex(fileindex, fileindex_path);
6841 if (sync_err && err == NULL)
6842 err = sync_err;
6843 done:
6844 got_ref_close(resolved);
6845 free(tree_id);
6846 free(fileindex_path);
6848 unlockerr = lock_worktree(worktree, LOCK_SH);
6849 if (unlockerr && err == NULL)
6850 err = unlockerr;
6851 return err;
6854 const struct got_error *
6855 got_worktree_histedit_complete(struct got_worktree *worktree,
6856 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6857 struct got_reference *edited_branch, struct got_repository *repo)
6859 const struct got_error *err, *unlockerr;
6860 struct got_object_id *new_head_commit_id = NULL;
6861 struct got_reference *resolved = NULL;
6863 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6864 if (err)
6865 return err;
6867 err = got_ref_open(&resolved, repo,
6868 got_ref_get_symref_target(edited_branch), 0);
6869 if (err)
6870 goto done;
6872 err = got_ref_change_ref(resolved, new_head_commit_id);
6873 if (err)
6874 goto done;
6876 err = got_ref_write(resolved, repo);
6877 if (err)
6878 goto done;
6880 err = got_worktree_set_head_ref(worktree, resolved);
6881 if (err)
6882 goto done;
6884 err = delete_histedit_refs(worktree, repo);
6885 done:
6886 if (fileindex)
6887 got_fileindex_free(fileindex);
6888 free(new_head_commit_id);
6889 unlockerr = lock_worktree(worktree, LOCK_SH);
6890 if (unlockerr && err == NULL)
6891 err = unlockerr;
6892 return err;
6895 const struct got_error *
6896 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6897 struct got_object_id *commit_id, struct got_repository *repo)
6899 const struct got_error *err;
6900 char *commit_ref_name;
6902 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6903 if (err)
6904 return err;
6906 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6907 if (err)
6908 goto done;
6910 err = delete_ref(commit_ref_name, repo);
6911 done:
6912 free(commit_ref_name);
6913 return err;
6916 const struct got_error *
6917 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6918 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6919 struct got_worktree *worktree, const char *refname,
6920 struct got_repository *repo)
6922 const struct got_error *err = NULL;
6923 char *fileindex_path = NULL;
6924 struct check_rebase_ok_arg ok_arg;
6926 *fileindex = NULL;
6927 *branch_ref = NULL;
6928 *base_branch_ref = NULL;
6930 err = lock_worktree(worktree, LOCK_EX);
6931 if (err)
6932 return err;
6934 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6935 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6936 "cannot integrate a branch into itself; "
6937 "update -b or different branch name required");
6938 goto done;
6941 err = open_fileindex(fileindex, &fileindex_path, worktree);
6942 if (err)
6943 goto done;
6945 /* Preconditions are the same as for rebase. */
6946 ok_arg.worktree = worktree;
6947 ok_arg.repo = repo;
6948 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6949 &ok_arg);
6950 if (err)
6951 goto done;
6953 err = got_ref_open(branch_ref, repo, refname, 1);
6954 if (err)
6955 goto done;
6957 err = got_ref_open(base_branch_ref, repo,
6958 got_worktree_get_head_ref_name(worktree), 1);
6959 done:
6960 if (err) {
6961 if (*branch_ref) {
6962 got_ref_close(*branch_ref);
6963 *branch_ref = NULL;
6965 if (*base_branch_ref) {
6966 got_ref_close(*base_branch_ref);
6967 *base_branch_ref = NULL;
6969 if (*fileindex) {
6970 got_fileindex_free(*fileindex);
6971 *fileindex = NULL;
6973 lock_worktree(worktree, LOCK_SH);
6975 return err;
6978 const struct got_error *
6979 got_worktree_integrate_continue(struct got_worktree *worktree,
6980 struct got_fileindex *fileindex, struct got_repository *repo,
6981 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6982 got_worktree_checkout_cb progress_cb, void *progress_arg,
6983 got_cancel_cb cancel_cb, void *cancel_arg)
6985 const struct got_error *err = NULL, *sync_err, *unlockerr;
6986 char *fileindex_path = NULL;
6987 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6989 err = get_fileindex_path(&fileindex_path, worktree);
6990 if (err)
6991 goto done;
6993 err = got_ref_resolve(&commit_id, repo, branch_ref);
6994 if (err)
6995 goto done;
6997 err = got_object_id_by_path(&tree_id, repo, commit_id,
6998 worktree->path_prefix);
6999 if (err)
7000 goto done;
7002 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7003 if (err)
7004 goto done;
7006 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7007 progress_cb, progress_arg, cancel_cb, cancel_arg);
7008 if (err)
7009 goto sync;
7011 err = got_ref_change_ref(base_branch_ref, commit_id);
7012 if (err)
7013 goto sync;
7015 err = got_ref_write(base_branch_ref, repo);
7016 sync:
7017 sync_err = sync_fileindex(fileindex, fileindex_path);
7018 if (sync_err && err == NULL)
7019 err = sync_err;
7021 done:
7022 unlockerr = got_ref_unlock(branch_ref);
7023 if (unlockerr && err == NULL)
7024 err = unlockerr;
7025 got_ref_close(branch_ref);
7027 unlockerr = got_ref_unlock(base_branch_ref);
7028 if (unlockerr && err == NULL)
7029 err = unlockerr;
7030 got_ref_close(base_branch_ref);
7032 got_fileindex_free(fileindex);
7033 free(fileindex_path);
7034 free(tree_id);
7036 unlockerr = lock_worktree(worktree, LOCK_SH);
7037 if (unlockerr && err == NULL)
7038 err = unlockerr;
7039 return err;
7042 const struct got_error *
7043 got_worktree_integrate_abort(struct got_worktree *worktree,
7044 struct got_fileindex *fileindex, struct got_repository *repo,
7045 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7047 const struct got_error *err = NULL, *unlockerr = NULL;
7049 got_fileindex_free(fileindex);
7051 err = lock_worktree(worktree, LOCK_SH);
7053 unlockerr = got_ref_unlock(branch_ref);
7054 if (unlockerr && err == NULL)
7055 err = unlockerr;
7056 got_ref_close(branch_ref);
7058 unlockerr = got_ref_unlock(base_branch_ref);
7059 if (unlockerr && err == NULL)
7060 err = unlockerr;
7061 got_ref_close(base_branch_ref);
7063 return err;
7066 struct check_stage_ok_arg {
7067 struct got_object_id *head_commit_id;
7068 struct got_worktree *worktree;
7069 struct got_fileindex *fileindex;
7070 struct got_repository *repo;
7071 int have_changes;
7074 const struct got_error *
7075 check_stage_ok(void *arg, unsigned char status,
7076 unsigned char staged_status, const char *relpath,
7077 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7078 struct got_object_id *commit_id, int dirfd, const char *de_name)
7080 struct check_stage_ok_arg *a = arg;
7081 const struct got_error *err = NULL;
7082 struct got_fileindex_entry *ie;
7083 struct got_object_id base_commit_id;
7084 struct got_object_id *base_commit_idp = NULL;
7085 char *in_repo_path = NULL, *p;
7087 if (status == GOT_STATUS_UNVERSIONED ||
7088 status == GOT_STATUS_NO_CHANGE)
7089 return NULL;
7090 if (status == GOT_STATUS_NONEXISTENT)
7091 return got_error_set_errno(ENOENT, relpath);
7093 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7094 if (ie == NULL)
7095 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7097 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7098 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7099 relpath) == -1)
7100 return got_error_from_errno("asprintf");
7102 if (got_fileindex_entry_has_commit(ie)) {
7103 memcpy(base_commit_id.sha1, ie->commit_sha1,
7104 SHA1_DIGEST_LENGTH);
7105 base_commit_idp = &base_commit_id;
7108 if (status == GOT_STATUS_CONFLICT) {
7109 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7110 goto done;
7111 } else if (status != GOT_STATUS_ADD &&
7112 status != GOT_STATUS_MODIFY &&
7113 status != GOT_STATUS_DELETE) {
7114 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7115 goto done;
7118 a->have_changes = 1;
7120 p = in_repo_path;
7121 while (p[0] == '/')
7122 p++;
7123 err = check_out_of_date(p, status, staged_status,
7124 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7125 GOT_ERR_STAGE_OUT_OF_DATE);
7126 done:
7127 free(in_repo_path);
7128 return err;
7131 struct stage_path_arg {
7132 struct got_worktree *worktree;
7133 struct got_fileindex *fileindex;
7134 struct got_repository *repo;
7135 got_worktree_status_cb status_cb;
7136 void *status_arg;
7137 got_worktree_patch_cb patch_cb;
7138 void *patch_arg;
7139 int staged_something;
7140 int allow_bad_symlinks;
7143 static const struct got_error *
7144 stage_path(void *arg, unsigned char status,
7145 unsigned char staged_status, const char *relpath,
7146 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7147 struct got_object_id *commit_id, int dirfd, const char *de_name)
7149 struct stage_path_arg *a = arg;
7150 const struct got_error *err = NULL;
7151 struct got_fileindex_entry *ie;
7152 char *ondisk_path = NULL, *path_content = NULL;
7153 uint32_t stage;
7154 struct got_object_id *new_staged_blob_id = NULL;
7155 struct stat sb;
7157 if (status == GOT_STATUS_UNVERSIONED)
7158 return NULL;
7160 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7161 if (ie == NULL)
7162 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7164 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7165 relpath)== -1)
7166 return got_error_from_errno("asprintf");
7168 switch (status) {
7169 case GOT_STATUS_ADD:
7170 case GOT_STATUS_MODIFY:
7171 /* XXX could sb.st_mode be passed in by our caller? */
7172 if (lstat(ondisk_path, &sb) == -1) {
7173 err = got_error_from_errno2("lstat", ondisk_path);
7174 break;
7176 if (a->patch_cb) {
7177 if (status == GOT_STATUS_ADD) {
7178 int choice = GOT_PATCH_CHOICE_NONE;
7179 err = (*a->patch_cb)(&choice, a->patch_arg,
7180 status, ie->path, NULL, 1, 1);
7181 if (err)
7182 break;
7183 if (choice != GOT_PATCH_CHOICE_YES)
7184 break;
7185 } else {
7186 err = create_patched_content(&path_content, 0,
7187 staged_blob_id ? staged_blob_id : blob_id,
7188 ondisk_path, dirfd, de_name, ie->path,
7189 a->repo, a->patch_cb, a->patch_arg);
7190 if (err || path_content == NULL)
7191 break;
7194 err = got_object_blob_create(&new_staged_blob_id,
7195 path_content ? path_content : ondisk_path, a->repo);
7196 if (err)
7197 break;
7198 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7199 SHA1_DIGEST_LENGTH);
7200 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7201 stage = GOT_FILEIDX_STAGE_ADD;
7202 else
7203 stage = GOT_FILEIDX_STAGE_MODIFY;
7204 got_fileindex_entry_stage_set(ie, stage);
7205 if (S_ISLNK(sb.st_mode)) {
7206 int is_bad_symlink = 0;
7207 if (!a->allow_bad_symlinks) {
7208 char target_path[PATH_MAX];
7209 ssize_t target_len;
7210 target_len = readlink(ondisk_path, target_path,
7211 sizeof(target_path));
7212 if (target_len == -1) {
7213 err = got_error_from_errno2("readlink",
7214 ondisk_path);
7215 break;
7217 err = is_bad_symlink_target(&is_bad_symlink,
7218 target_path, target_len, ondisk_path,
7219 a->worktree->root_path);
7220 if (err)
7221 break;
7222 if (is_bad_symlink) {
7223 err = got_error_path(ondisk_path,
7224 GOT_ERR_BAD_SYMLINK);
7225 break;
7228 if (is_bad_symlink)
7229 got_fileindex_entry_staged_filetype_set(ie,
7230 GOT_FILEIDX_MODE_BAD_SYMLINK);
7231 else
7232 got_fileindex_entry_staged_filetype_set(ie,
7233 GOT_FILEIDX_MODE_SYMLINK);
7234 } else {
7235 got_fileindex_entry_staged_filetype_set(ie,
7236 GOT_FILEIDX_MODE_REGULAR_FILE);
7238 a->staged_something = 1;
7239 if (a->status_cb == NULL)
7240 break;
7241 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7242 get_staged_status(ie), relpath, blob_id,
7243 new_staged_blob_id, NULL, dirfd, de_name);
7244 break;
7245 case GOT_STATUS_DELETE:
7246 if (staged_status == GOT_STATUS_DELETE)
7247 break;
7248 if (a->patch_cb) {
7249 int choice = GOT_PATCH_CHOICE_NONE;
7250 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7251 ie->path, NULL, 1, 1);
7252 if (err)
7253 break;
7254 if (choice == GOT_PATCH_CHOICE_NO)
7255 break;
7256 if (choice != GOT_PATCH_CHOICE_YES) {
7257 err = got_error(GOT_ERR_PATCH_CHOICE);
7258 break;
7261 stage = GOT_FILEIDX_STAGE_DELETE;
7262 got_fileindex_entry_stage_set(ie, stage);
7263 a->staged_something = 1;
7264 if (a->status_cb == NULL)
7265 break;
7266 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7267 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7268 de_name);
7269 break;
7270 case GOT_STATUS_NO_CHANGE:
7271 break;
7272 case GOT_STATUS_CONFLICT:
7273 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7274 break;
7275 case GOT_STATUS_NONEXISTENT:
7276 err = got_error_set_errno(ENOENT, relpath);
7277 break;
7278 default:
7279 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7280 break;
7283 if (path_content && unlink(path_content) == -1 && err == NULL)
7284 err = got_error_from_errno2("unlink", path_content);
7285 free(path_content);
7286 free(ondisk_path);
7287 free(new_staged_blob_id);
7288 return err;
7291 const struct got_error *
7292 got_worktree_stage(struct got_worktree *worktree,
7293 struct got_pathlist_head *paths,
7294 got_worktree_status_cb status_cb, void *status_arg,
7295 got_worktree_patch_cb patch_cb, void *patch_arg,
7296 int allow_bad_symlinks, struct got_repository *repo)
7298 const struct got_error *err = NULL, *sync_err, *unlockerr;
7299 struct got_pathlist_entry *pe;
7300 struct got_fileindex *fileindex = NULL;
7301 char *fileindex_path = NULL;
7302 struct got_reference *head_ref = NULL;
7303 struct got_object_id *head_commit_id = NULL;
7304 struct check_stage_ok_arg oka;
7305 struct stage_path_arg spa;
7307 err = lock_worktree(worktree, LOCK_EX);
7308 if (err)
7309 return err;
7311 err = got_ref_open(&head_ref, repo,
7312 got_worktree_get_head_ref_name(worktree), 0);
7313 if (err)
7314 goto done;
7315 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7316 if (err)
7317 goto done;
7318 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7319 if (err)
7320 goto done;
7322 /* Check pre-conditions before staging anything. */
7323 oka.head_commit_id = head_commit_id;
7324 oka.worktree = worktree;
7325 oka.fileindex = fileindex;
7326 oka.repo = repo;
7327 oka.have_changes = 0;
7328 TAILQ_FOREACH(pe, paths, entry) {
7329 err = worktree_status(worktree, pe->path, fileindex, repo,
7330 check_stage_ok, &oka, NULL, NULL, 0, 0);
7331 if (err)
7332 goto done;
7334 if (!oka.have_changes) {
7335 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7336 goto done;
7339 spa.worktree = worktree;
7340 spa.fileindex = fileindex;
7341 spa.repo = repo;
7342 spa.patch_cb = patch_cb;
7343 spa.patch_arg = patch_arg;
7344 spa.status_cb = status_cb;
7345 spa.status_arg = status_arg;
7346 spa.staged_something = 0;
7347 spa.allow_bad_symlinks = allow_bad_symlinks;
7348 TAILQ_FOREACH(pe, paths, entry) {
7349 err = worktree_status(worktree, pe->path, fileindex, repo,
7350 stage_path, &spa, NULL, NULL, 0, 0);
7351 if (err)
7352 goto done;
7354 if (!spa.staged_something) {
7355 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7356 goto done;
7359 sync_err = sync_fileindex(fileindex, fileindex_path);
7360 if (sync_err && err == NULL)
7361 err = sync_err;
7362 done:
7363 if (head_ref)
7364 got_ref_close(head_ref);
7365 free(head_commit_id);
7366 free(fileindex_path);
7367 if (fileindex)
7368 got_fileindex_free(fileindex);
7369 unlockerr = lock_worktree(worktree, LOCK_SH);
7370 if (unlockerr && err == NULL)
7371 err = unlockerr;
7372 return err;
7375 struct unstage_path_arg {
7376 struct got_worktree *worktree;
7377 struct got_fileindex *fileindex;
7378 struct got_repository *repo;
7379 got_worktree_checkout_cb progress_cb;
7380 void *progress_arg;
7381 got_worktree_patch_cb patch_cb;
7382 void *patch_arg;
7385 static const struct got_error *
7386 create_unstaged_content(char **path_unstaged_content,
7387 char **path_new_staged_content, struct got_object_id *blob_id,
7388 struct got_object_id *staged_blob_id, const char *relpath,
7389 struct got_repository *repo,
7390 got_worktree_patch_cb patch_cb, void *patch_arg)
7392 const struct got_error *err;
7393 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7394 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7395 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7396 struct stat sb1, sb2;
7397 struct got_diff_changes *changes = NULL;
7398 struct got_diff_state *ds = NULL;
7399 struct got_diff_args *args = NULL;
7400 struct got_diff_change *change;
7401 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7402 int have_content = 0, have_rejected_content = 0;
7404 *path_unstaged_content = NULL;
7405 *path_new_staged_content = NULL;
7407 err = got_object_id_str(&label1, blob_id);
7408 if (err)
7409 return err;
7410 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7411 if (err)
7412 goto done;
7414 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7415 if (err)
7416 goto done;
7418 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7419 if (err)
7420 goto done;
7422 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7423 if (err)
7424 goto done;
7426 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7427 if (err)
7428 goto done;
7430 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7431 if (err)
7432 goto done;
7434 if (stat(path1, &sb1) == -1) {
7435 err = got_error_from_errno2("stat", path1);
7436 goto done;
7439 if (stat(path2, &sb2) == -1) {
7440 err = got_error_from_errno2("stat", path2);
7441 goto done;
7444 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7445 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7446 if (err)
7447 goto done;
7449 err = got_opentemp_named(path_unstaged_content, &outfile,
7450 "got-unstaged-content");
7451 if (err)
7452 goto done;
7453 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7454 "got-new-staged-content");
7455 if (err)
7456 goto done;
7458 if (fseek(f1, 0L, SEEK_SET) == -1) {
7459 err = got_ferror(f1, GOT_ERR_IO);
7460 goto done;
7462 if (fseek(f2, 0L, SEEK_SET) == -1) {
7463 err = got_ferror(f2, GOT_ERR_IO);
7464 goto done;
7466 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7467 int choice;
7468 err = apply_or_reject_change(&choice, change, ++n,
7469 changes->nchanges, ds, args, diff_flags, relpath,
7470 f1, f2, &line_cur1, &line_cur2,
7471 outfile, rejectfile, patch_cb, patch_arg);
7472 if (err)
7473 goto done;
7474 if (choice == GOT_PATCH_CHOICE_YES)
7475 have_content = 1;
7476 else
7477 have_rejected_content = 1;
7478 if (choice == GOT_PATCH_CHOICE_QUIT)
7479 break;
7481 if (have_content || have_rejected_content)
7482 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7483 outfile, rejectfile);
7484 done:
7485 free(label1);
7486 if (blob)
7487 got_object_blob_close(blob);
7488 if (staged_blob)
7489 got_object_blob_close(staged_blob);
7490 if (f1 && fclose(f1) == EOF && err == NULL)
7491 err = got_error_from_errno2("fclose", path1);
7492 if (f2 && fclose(f2) == EOF && err == NULL)
7493 err = got_error_from_errno2("fclose", path2);
7494 if (outfile && fclose(outfile) == EOF && err == NULL)
7495 err = got_error_from_errno2("fclose", *path_unstaged_content);
7496 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7497 err = got_error_from_errno2("fclose", *path_new_staged_content);
7498 if (path1 && unlink(path1) == -1 && err == NULL)
7499 err = got_error_from_errno2("unlink", path1);
7500 if (path2 && unlink(path2) == -1 && err == NULL)
7501 err = got_error_from_errno2("unlink", path2);
7502 if (err || !have_content) {
7503 if (*path_unstaged_content &&
7504 unlink(*path_unstaged_content) == -1 && err == NULL)
7505 err = got_error_from_errno2("unlink",
7506 *path_unstaged_content);
7507 free(*path_unstaged_content);
7508 *path_unstaged_content = NULL;
7510 if (err || !have_content || !have_rejected_content) {
7511 if (*path_new_staged_content &&
7512 unlink(*path_new_staged_content) == -1 && err == NULL)
7513 err = got_error_from_errno2("unlink",
7514 *path_new_staged_content);
7515 free(*path_new_staged_content);
7516 *path_new_staged_content = NULL;
7518 free(args);
7519 if (ds) {
7520 got_diff_state_free(ds);
7521 free(ds);
7523 if (changes)
7524 got_diff_free_changes(changes);
7525 free(path1);
7526 free(path2);
7527 return err;
7530 static const struct got_error *
7531 unstage_hunks(struct got_object_id *staged_blob_id,
7532 struct got_blob_object *blob_base,
7533 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7534 const char *ondisk_path, const char *label_orig,
7535 struct got_worktree *worktree, struct got_repository *repo,
7536 got_worktree_patch_cb patch_cb, void *patch_arg,
7537 got_worktree_checkout_cb progress_cb, void *progress_arg)
7539 const struct got_error *err = NULL;
7540 char *path_unstaged_content = NULL;
7541 char *path_new_staged_content = NULL;
7542 struct got_object_id *new_staged_blob_id = NULL;
7543 FILE *f = NULL;
7544 struct stat sb;
7546 err = create_unstaged_content(&path_unstaged_content,
7547 &path_new_staged_content, blob_id, staged_blob_id,
7548 ie->path, repo, patch_cb, patch_arg);
7549 if (err)
7550 return err;
7552 if (path_unstaged_content == NULL)
7553 return NULL;
7555 if (path_new_staged_content) {
7556 err = got_object_blob_create(&new_staged_blob_id,
7557 path_new_staged_content, repo);
7558 if (err)
7559 goto done;
7562 f = fopen(path_unstaged_content, "r");
7563 if (f == NULL) {
7564 err = got_error_from_errno2("fopen",
7565 path_unstaged_content);
7566 goto done;
7568 if (fstat(fileno(f), &sb) == -1) {
7569 err = got_error_from_errno2("fstat", path_unstaged_content);
7570 goto done;
7572 if (got_fileindex_entry_staged_filetype_get(ie) ==
7573 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7574 char link_target[PATH_MAX];
7575 size_t r;
7576 r = fread(link_target, 1, sizeof(link_target), f);
7577 if (r == 0 && ferror(f)) {
7578 err = got_error_from_errno("fread");
7579 goto done;
7581 if (r >= sizeof(link_target)) { /* should not happen */
7582 err = got_error(GOT_ERR_NO_SPACE);
7583 goto done;
7585 link_target[r] = '\0';
7586 err = merge_symlink(worktree, blob_base,
7587 ondisk_path, ie->path, label_orig, link_target,
7588 worktree->base_commit_id, repo, progress_cb,
7589 progress_arg);
7590 } else {
7591 int local_changes_subsumed;
7592 err = merge_file(&local_changes_subsumed, worktree,
7593 blob_base, ondisk_path, ie->path,
7594 got_fileindex_perms_to_st(ie),
7595 path_unstaged_content, label_orig, "unstaged",
7596 repo, progress_cb, progress_arg);
7598 if (err)
7599 goto done;
7601 if (new_staged_blob_id) {
7602 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7603 SHA1_DIGEST_LENGTH);
7604 } else
7605 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7606 done:
7607 free(new_staged_blob_id);
7608 if (path_unstaged_content &&
7609 unlink(path_unstaged_content) == -1 && err == NULL)
7610 err = got_error_from_errno2("unlink", path_unstaged_content);
7611 if (path_new_staged_content &&
7612 unlink(path_new_staged_content) == -1 && err == NULL)
7613 err = got_error_from_errno2("unlink", path_new_staged_content);
7614 if (f && fclose(f) != 0 && err == NULL)
7615 err = got_error_from_errno2("fclose", path_unstaged_content);
7616 free(path_unstaged_content);
7617 free(path_new_staged_content);
7618 return err;
7621 static const struct got_error *
7622 unstage_path(void *arg, unsigned char status,
7623 unsigned char staged_status, const char *relpath,
7624 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7625 struct got_object_id *commit_id, int dirfd, const char *de_name)
7627 const struct got_error *err = NULL;
7628 struct unstage_path_arg *a = arg;
7629 struct got_fileindex_entry *ie;
7630 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7631 char *ondisk_path = NULL;
7632 char *id_str = NULL, *label_orig = NULL;
7633 int local_changes_subsumed;
7634 struct stat sb;
7636 if (staged_status != GOT_STATUS_ADD &&
7637 staged_status != GOT_STATUS_MODIFY &&
7638 staged_status != GOT_STATUS_DELETE)
7639 return NULL;
7641 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7642 if (ie == NULL)
7643 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7645 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7646 == -1)
7647 return got_error_from_errno("asprintf");
7649 err = got_object_id_str(&id_str,
7650 commit_id ? commit_id : a->worktree->base_commit_id);
7651 if (err)
7652 goto done;
7653 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7654 id_str) == -1) {
7655 err = got_error_from_errno("asprintf");
7656 goto done;
7659 switch (staged_status) {
7660 case GOT_STATUS_MODIFY:
7661 err = got_object_open_as_blob(&blob_base, a->repo,
7662 blob_id, 8192);
7663 if (err)
7664 break;
7665 /* fall through */
7666 case GOT_STATUS_ADD:
7667 if (a->patch_cb) {
7668 if (staged_status == GOT_STATUS_ADD) {
7669 int choice = GOT_PATCH_CHOICE_NONE;
7670 err = (*a->patch_cb)(&choice, a->patch_arg,
7671 staged_status, ie->path, NULL, 1, 1);
7672 if (err)
7673 break;
7674 if (choice != GOT_PATCH_CHOICE_YES)
7675 break;
7676 } else {
7677 err = unstage_hunks(staged_blob_id,
7678 blob_base, blob_id, ie, ondisk_path,
7679 label_orig, a->worktree, a->repo,
7680 a->patch_cb, a->patch_arg,
7681 a->progress_cb, a->progress_arg);
7682 break; /* Done with this file. */
7685 err = got_object_open_as_blob(&blob_staged, a->repo,
7686 staged_blob_id, 8192);
7687 if (err)
7688 break;
7689 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7690 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7691 case GOT_FILEIDX_MODE_REGULAR_FILE:
7692 err = merge_blob(&local_changes_subsumed, a->worktree,
7693 blob_base, ondisk_path, relpath,
7694 got_fileindex_perms_to_st(ie), label_orig,
7695 blob_staged, commit_id ? commit_id :
7696 a->worktree->base_commit_id, a->repo,
7697 a->progress_cb, a->progress_arg);
7698 break;
7699 case GOT_FILEIDX_MODE_SYMLINK:
7700 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7701 char *staged_target;
7702 err = got_object_blob_read_to_str(
7703 &staged_target, blob_staged);
7704 if (err)
7705 goto done;
7706 err = merge_symlink(a->worktree, blob_base,
7707 ondisk_path, relpath, label_orig,
7708 staged_target, commit_id ? commit_id :
7709 a->worktree->base_commit_id,
7710 a->repo, a->progress_cb, a->progress_arg);
7711 free(staged_target);
7712 } else {
7713 err = merge_blob(&local_changes_subsumed,
7714 a->worktree, blob_base, ondisk_path,
7715 relpath, got_fileindex_perms_to_st(ie),
7716 label_orig, blob_staged,
7717 commit_id ? commit_id :
7718 a->worktree->base_commit_id, a->repo,
7719 a->progress_cb, a->progress_arg);
7721 break;
7722 default:
7723 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7724 break;
7726 if (err == NULL)
7727 got_fileindex_entry_stage_set(ie,
7728 GOT_FILEIDX_STAGE_NONE);
7729 break;
7730 case GOT_STATUS_DELETE:
7731 if (a->patch_cb) {
7732 int choice = GOT_PATCH_CHOICE_NONE;
7733 err = (*a->patch_cb)(&choice, a->patch_arg,
7734 staged_status, ie->path, NULL, 1, 1);
7735 if (err)
7736 break;
7737 if (choice == GOT_PATCH_CHOICE_NO)
7738 break;
7739 if (choice != GOT_PATCH_CHOICE_YES) {
7740 err = got_error(GOT_ERR_PATCH_CHOICE);
7741 break;
7744 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7745 err = get_file_status(&status, &sb, ie, ondisk_path,
7746 dirfd, de_name, a->repo);
7747 if (err)
7748 break;
7749 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7750 break;
7752 done:
7753 free(ondisk_path);
7754 if (blob_base)
7755 got_object_blob_close(blob_base);
7756 if (blob_staged)
7757 got_object_blob_close(blob_staged);
7758 free(id_str);
7759 free(label_orig);
7760 return err;
7763 const struct got_error *
7764 got_worktree_unstage(struct got_worktree *worktree,
7765 struct got_pathlist_head *paths,
7766 got_worktree_checkout_cb progress_cb, void *progress_arg,
7767 got_worktree_patch_cb patch_cb, void *patch_arg,
7768 struct got_repository *repo)
7770 const struct got_error *err = NULL, *sync_err, *unlockerr;
7771 struct got_pathlist_entry *pe;
7772 struct got_fileindex *fileindex = NULL;
7773 char *fileindex_path = NULL;
7774 struct unstage_path_arg upa;
7776 err = lock_worktree(worktree, LOCK_EX);
7777 if (err)
7778 return err;
7780 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7781 if (err)
7782 goto done;
7784 upa.worktree = worktree;
7785 upa.fileindex = fileindex;
7786 upa.repo = repo;
7787 upa.progress_cb = progress_cb;
7788 upa.progress_arg = progress_arg;
7789 upa.patch_cb = patch_cb;
7790 upa.patch_arg = patch_arg;
7791 TAILQ_FOREACH(pe, paths, entry) {
7792 err = worktree_status(worktree, pe->path, fileindex, repo,
7793 unstage_path, &upa, NULL, NULL, 0, 0);
7794 if (err)
7795 goto done;
7798 sync_err = sync_fileindex(fileindex, fileindex_path);
7799 if (sync_err && err == NULL)
7800 err = sync_err;
7801 done:
7802 free(fileindex_path);
7803 if (fileindex)
7804 got_fileindex_free(fileindex);
7805 unlockerr = lock_worktree(worktree, LOCK_SH);
7806 if (unlockerr && err == NULL)
7807 err = unlockerr;
7808 return err;
7811 struct report_file_info_arg {
7812 struct got_worktree *worktree;
7813 got_worktree_path_info_cb info_cb;
7814 void *info_arg;
7815 struct got_pathlist_head *paths;
7816 got_cancel_cb cancel_cb;
7817 void *cancel_arg;
7820 static const struct got_error *
7821 report_file_info(void *arg, struct got_fileindex_entry *ie)
7823 struct report_file_info_arg *a = arg;
7824 struct got_pathlist_entry *pe;
7825 struct got_object_id blob_id, staged_blob_id, commit_id;
7826 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7827 struct got_object_id *commit_idp = NULL;
7828 int stage;
7830 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7831 return got_error(GOT_ERR_CANCELLED);
7833 TAILQ_FOREACH(pe, a->paths, entry) {
7834 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7835 got_path_is_child(ie->path, pe->path, pe->path_len))
7836 break;
7838 if (pe == NULL) /* not found */
7839 return NULL;
7841 if (got_fileindex_entry_has_blob(ie)) {
7842 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7843 blob_idp = &blob_id;
7845 stage = got_fileindex_entry_stage_get(ie);
7846 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7847 stage == GOT_FILEIDX_STAGE_ADD) {
7848 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7849 SHA1_DIGEST_LENGTH);
7850 staged_blob_idp = &staged_blob_id;
7853 if (got_fileindex_entry_has_commit(ie)) {
7854 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7855 commit_idp = &commit_id;
7858 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7859 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7862 const struct got_error *
7863 got_worktree_path_info(struct got_worktree *worktree,
7864 struct got_pathlist_head *paths,
7865 got_worktree_path_info_cb info_cb, void *info_arg,
7866 got_cancel_cb cancel_cb, void *cancel_arg)
7869 const struct got_error *err = NULL, *unlockerr;
7870 struct got_fileindex *fileindex = NULL;
7871 char *fileindex_path = NULL;
7872 struct report_file_info_arg arg;
7874 err = lock_worktree(worktree, LOCK_SH);
7875 if (err)
7876 return err;
7878 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7879 if (err)
7880 goto done;
7882 arg.worktree = worktree;
7883 arg.info_cb = info_cb;
7884 arg.info_arg = info_arg;
7885 arg.paths = paths;
7886 arg.cancel_cb = cancel_cb;
7887 arg.cancel_arg = cancel_arg;
7888 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7889 &arg);
7890 done:
7891 free(fileindex_path);
7892 if (fileindex)
7893 got_fileindex_free(fileindex);
7894 unlockerr = lock_worktree(worktree, LOCK_UN);
7895 if (unlockerr && err == NULL)
7896 err = unlockerr;
7897 return err;