Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
432 done:
433 if (repo)
434 got_repo_close(repo);
435 free(path_got);
436 free(path_lock);
437 free(base_commit_id_str);
438 free(uuidstr);
439 free(formatstr);
440 if (err) {
441 if (fd != -1)
442 close(fd);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
445 *worktree = NULL;
446 } else
447 (*worktree)->lockfd = fd;
449 return err;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
456 char *worktree_path;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
462 for (;;) {
463 char *parent_path;
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 free(worktree_path);
468 return err;
470 if (*worktree) {
471 free(worktree_path);
472 return NULL;
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
475 break;
476 err = got_path_dirname(&parent_path, worktree_path);
477 if (err) {
478 if (err->code != GOT_ERR_BAD_PATH) {
479 free(worktree_path);
480 return err;
482 break;
484 free(worktree_path);
485 worktree_path = parent_path;
488 free(worktree_path);
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
507 free(worktree);
508 return err;
511 const char *
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
517 const char *
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
522 const char *
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
540 free(absprefix);
541 return NULL;
544 const char *
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
567 goto done;
570 err = write_head_ref(path_got, head_ref);
571 if (err)
572 goto done;
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
576 done:
577 free(path_got);
578 if (err)
579 free(head_ref_name);
580 return err;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
595 char *id_str = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
601 path_got = NULL;
602 goto done;
605 err = got_object_open(&obj, repo, commit_id);
606 if (err)
607 return err;
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
616 if (err)
617 goto done;
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
619 if (err)
620 goto done;
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
626 goto done;
628 done:
629 if (obj)
630 got_object_close(obj);
631 free(id_str);
632 free(path_got);
633 return err;
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
649 return NULL;
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
656 char *abspath;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 struct stat sb;
664 err = NULL;
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
672 free(abspath);
673 return err;
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
680 uint8_t fbuf1[8192];
681 uint8_t fbuf2[8192];
682 size_t flen1 = 0, flen2 = 0;
684 *same = 1;
686 for (;;) {
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
690 break;
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
695 break;
697 if (flen1 == 0) {
698 if (flen2 != 0)
699 *same = 0;
700 break;
701 } else if (flen2 == 0) {
702 if (flen1 != 0)
703 *same = 0;
704 break;
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
707 *same = 0;
708 break;
710 } else {
711 *same = 0;
712 break;
716 return err;
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
723 struct stat sb;
724 size_t size1, size2;
725 FILE *f1 = NULL, *f2 = NULL;
727 *same = 1;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
731 goto done;
733 size1 = sb.st_size;
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
737 goto done;
739 size2 = sb.st_size;
741 if (size1 != size2) {
742 *same = 0;
743 return NULL;
746 f1 = fopen(f1_path, "r");
747 if (f1 == NULL)
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
751 if (f2 == NULL) {
752 err = got_error_from_errno2("fopen", f2_path);
753 goto done;
756 err = check_file_contents_equal(same, f1, f2);
757 done:
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
763 return err;
766 /*
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
770 */
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
780 int merged_fd = -1;
781 FILE *f_orig = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
784 int overlapcnt = 0;
785 char *parent = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
792 if (err)
793 return err;
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 goto done;
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
801 if (err)
802 goto done;
804 free(base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
807 base_path = NULL;
808 goto done;
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
812 if (err)
813 goto done;
814 if (blob_orig) {
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
816 blob_orig);
817 if (err)
818 goto done;
819 } else {
820 /*
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
824 */
827 /*
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
830 */
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
833 ssize_t target_len;
834 size_t n;
836 free(base_path);
837 if (asprintf(&base_path, "%s/got-symlink-merge",
838 parent) == -1) {
839 err = got_error_from_errno("asprintf");
840 base_path = NULL;
841 goto done;
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 if (err)
845 goto done;
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
850 goto done;
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
855 goto done;
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
859 goto done;
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
866 if (err)
867 goto done;
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
871 if (err)
872 goto done;
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
876 goto done;
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
882 merged_path);
883 if (err)
884 goto done;
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
889 goto done;
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
894 ondisk_path);
895 goto done;
897 done:
898 if (err) {
899 if (merged_path)
900 unlink(merged_path);
902 if (symlink_path) {
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
908 free(symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
913 free(merged_path);
914 free(base_path);
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
919 free(parent);
920 return err;
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
925 size_t target_len)
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
932 ondisk_path);
933 return NULL;
936 /*
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
947 */
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
955 FILE *f = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
958 if (err)
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
964 goto done;
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 if (err)
969 goto done;
971 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
972 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
973 deriv_target ? deriv_target : "(symlink was deleted)",
974 orig_target ? label_orig : "",
975 orig_target ? "\n" : "",
976 orig_target ? orig_target : "",
977 orig_target ? "\n" : "",
978 GOT_DIFF_CONFLICT_MARKER_SEP,
979 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
980 err = got_error_from_errno2("fprintf", path);
981 goto done;
984 if (unlink(ondisk_path) == -1) {
985 err = got_error_from_errno2("unlink", ondisk_path);
986 goto done;
988 if (rename(path, ondisk_path) == -1) {
989 err = got_error_from_errno3("rename", path, ondisk_path);
990 goto done;
992 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
994 goto done;
996 done:
997 if (f != NULL && fclose(f) == EOF && err == NULL)
998 err = got_error_from_errno2("fclose", path);
999 free(path);
1000 free(id_str);
1001 free(label_deriv);
1002 return err;
1005 /* forward declaration */
1006 static const struct got_error *
1007 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1008 const char *, const char *, uint16_t, const char *,
1009 struct got_blob_object *, struct got_object_id *,
1010 struct got_repository *, got_worktree_checkout_cb, void *);
1013 * Merge a symlink into the work tree, where blob_orig acts as the common
1014 * ancestor, deriv_target is the link target of the first derived version,
1015 * and the symlink on disk acts as the second derived version.
1016 * Assume that contents of both blobs represent symlinks.
1018 static const struct got_error *
1019 merge_symlink(struct got_worktree *worktree,
1020 struct got_blob_object *blob_orig, const char *ondisk_path,
1021 const char *path, const char *label_orig, const char *deriv_target,
1022 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1023 got_worktree_checkout_cb progress_cb, void *progress_arg)
1025 const struct got_error *err = NULL;
1026 char *ancestor_target = NULL;
1027 struct stat sb;
1028 ssize_t ondisk_len, deriv_len;
1029 char ondisk_target[PATH_MAX];
1030 int have_local_change = 0;
1031 int have_incoming_change = 0;
1033 if (lstat(ondisk_path, &sb) == -1)
1034 return got_error_from_errno2("lstat", ondisk_path);
1036 ondisk_len = readlink(ondisk_path, ondisk_target,
1037 sizeof(ondisk_target));
1038 if (ondisk_len == -1) {
1039 err = got_error_from_errno2("readlink",
1040 ondisk_path);
1041 goto done;
1043 ondisk_target[ondisk_len] = '\0';
1045 if (blob_orig) {
1046 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1047 if (err)
1048 goto done;
1051 if (ancestor_target == NULL ||
1052 (ondisk_len != strlen(ancestor_target) ||
1053 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1054 have_local_change = 1;
1056 deriv_len = strlen(deriv_target);
1057 if (ancestor_target == NULL ||
1058 (deriv_len != strlen(ancestor_target) ||
1059 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1060 have_incoming_change = 1;
1062 if (!have_local_change && !have_incoming_change) {
1063 if (ancestor_target) {
1064 /* Both sides made the same change. */
1065 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1066 path);
1067 } else if (deriv_len == ondisk_len &&
1068 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1069 /* Both sides added the same symlink. */
1070 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1071 path);
1072 } else {
1073 /* Both sides added symlinks which don't match. */
1074 err = install_symlink_conflict(deriv_target,
1075 deriv_base_commit_id, ancestor_target,
1076 label_orig, ondisk_target, ondisk_path);
1077 if (err)
1078 goto done;
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 path);
1082 } else if (!have_local_change && have_incoming_change) {
1083 /* Apply the incoming change. */
1084 err = update_symlink(ondisk_path, deriv_target,
1085 strlen(deriv_target));
1086 if (err)
1087 goto done;
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1089 } else if (have_local_change && have_incoming_change) {
1090 if (deriv_len == ondisk_len &&
1091 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1092 /* Both sides made the same change. */
1093 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1094 path);
1095 } else {
1096 err = install_symlink_conflict(deriv_target,
1097 deriv_base_commit_id, ancestor_target, label_orig,
1098 ondisk_target, ondisk_path);
1099 if (err)
1100 goto done;
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1102 path);
1106 done:
1107 free(ancestor_target);
1108 return err;
1112 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1113 * blob_deriv acts as the first derived version, and the file on disk
1114 * acts as the second derived version.
1116 static const struct got_error *
1117 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1118 struct got_blob_object *blob_orig, const char *ondisk_path,
1119 const char *path, uint16_t st_mode, const char *label_orig,
1120 struct got_blob_object *blob_deriv,
1121 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1122 got_worktree_checkout_cb progress_cb, void *progress_arg)
1124 const struct got_error *err = NULL;
1125 FILE *f_deriv = NULL;
1126 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1127 char *label_deriv = NULL, *parent = NULL;
1129 *local_changes_subsumed = 0;
1131 err = got_path_dirname(&parent, ondisk_path);
1132 if (err)
1133 return err;
1135 free(base_path);
1136 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1137 err = got_error_from_errno("asprintf");
1138 base_path = NULL;
1139 goto done;
1142 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1143 if (err)
1144 goto done;
1145 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1146 blob_deriv);
1147 if (err)
1148 goto done;
1150 err = got_object_id_str(&id_str, deriv_base_commit_id);
1151 if (err)
1152 goto done;
1153 if (asprintf(&label_deriv, "%s: commit %s",
1154 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1155 err = got_error_from_errno("asprintf");
1156 goto done;
1159 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1160 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1161 label_deriv, repo, progress_cb, progress_arg);
1162 done:
1163 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1164 err = got_error_from_errno("fclose");
1165 free(base_path);
1166 if (blob_deriv_path) {
1167 unlink(blob_deriv_path);
1168 free(blob_deriv_path);
1170 free(id_str);
1171 free(label_deriv);
1172 free(parent);
1173 return err;
1176 static const struct got_error *
1177 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1178 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1179 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1181 const struct got_error *err = NULL;
1182 struct got_fileindex_entry *new_ie;
1184 *new_iep = NULL;
1186 err = got_fileindex_entry_alloc(&new_ie, path);
1187 if (err)
1188 return err;
1190 err = got_fileindex_entry_update(new_ie, ondisk_path,
1191 blob_id->sha1, base_commit_id->sha1, 1);
1192 if (err)
1193 goto done;
1195 err = got_fileindex_entry_add(fileindex, new_ie);
1196 done:
1197 if (err)
1198 got_fileindex_entry_free(new_ie);
1199 else
1200 *new_iep = new_ie;
1201 return err;
1204 static mode_t
1205 get_ondisk_perms(int executable, mode_t st_mode)
1207 mode_t xbits = S_IXUSR;
1209 if (executable) {
1210 /* Map read bits to execute bits. */
1211 if (st_mode & S_IRGRP)
1212 xbits |= S_IXGRP;
1213 if (st_mode & S_IROTH)
1214 xbits |= S_IXOTH;
1215 return st_mode | xbits;
1218 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1221 /* forward declaration */
1222 static const struct got_error *
1223 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1224 const char *path, mode_t te_mode, mode_t st_mode,
1225 struct got_blob_object *blob, int restoring_missing_file,
1226 int reverting_versioned_file, int installing_bad_symlink,
1227 int path_is_unversioned, struct got_repository *repo,
1228 got_worktree_checkout_cb progress_cb, void *progress_arg);
1231 * This function assumes that the provided symlink target points at a
1232 * safe location in the work tree!
1234 static const struct got_error *
1235 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1236 size_t target_len)
1238 const struct got_error *err = NULL;
1239 ssize_t elen;
1240 char etarget[PATH_MAX];
1241 int fd;
1244 * "Bad" symlinks (those pointing outside the work tree or into the
1245 * .got directory) are installed in the work tree as a regular file
1246 * which contains the bad symlink target path.
1247 * The new symlink target has already been checked for safety by our
1248 * caller. If we can successfully open a regular file then we simply
1249 * replace this file with a symlink below.
1251 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1252 if (fd == -1) {
1253 if (errno != ELOOP)
1254 return got_error_from_errno2("open", ondisk_path);
1256 /* We are updating an existing on-disk symlink. */
1257 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1258 if (elen == -1)
1259 return got_error_from_errno2("readlink", ondisk_path);
1261 if (elen == target_len &&
1262 memcmp(etarget, target_path, target_len) == 0)
1263 return NULL; /* nothing to do */
1266 err = update_symlink(ondisk_path, target_path, target_len);
1267 if (fd != -1 && close(fd) == -1 && err == NULL)
1268 err = got_error_from_errno2("close", ondisk_path);
1269 return err;
1272 static const struct got_error *
1273 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1274 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1276 const struct got_error *err = NULL;
1277 char canonpath[PATH_MAX];
1278 char *path_got = NULL;
1280 *is_bad_symlink = 0;
1282 if (target_len >= sizeof(canonpath)) {
1283 *is_bad_symlink = 1;
1284 return NULL;
1288 * We do not use realpath(3) to resolve the symlink's target
1289 * path because we don't want to resolve symlinks recursively.
1290 * Instead we make the path absolute and then canonicalize it.
1291 * Relative symlink target lookup should begin at the directory
1292 * in which the blob object is being installed.
1294 if (!got_path_is_absolute(target_path)) {
1295 char *abspath, *parent;
1296 err = got_path_dirname(&parent, ondisk_path);
1297 if (err)
1298 return err;
1299 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1300 free(parent);
1301 return got_error_from_errno("asprintf");
1303 free(parent);
1304 if (strlen(abspath) >= sizeof(canonpath)) {
1305 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1306 free(abspath);
1307 return err;
1309 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1310 free(abspath);
1311 if (err)
1312 return err;
1313 } else {
1314 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1315 if (err)
1316 return err;
1319 /* Only allow symlinks pointing at paths within the work tree. */
1320 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1321 *is_bad_symlink = 1;
1322 return NULL;
1325 /* Do not allow symlinks pointing into the .got directory. */
1326 if (asprintf(&path_got, "%s/%s", wtroot_path,
1327 GOT_WORKTREE_GOT_DIR) == -1)
1328 return got_error_from_errno("asprintf");
1329 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1330 *is_bad_symlink = 1;
1332 free(path_got);
1333 return NULL;
1336 static const struct got_error *
1337 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1338 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1339 int restoring_missing_file, int reverting_versioned_file,
1340 int path_is_unversioned, struct got_repository *repo,
1341 got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 const struct got_error *err = NULL;
1344 char target_path[PATH_MAX];
1345 size_t len, target_len = 0;
1346 char *path_got = NULL;
1347 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1348 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1350 *is_bad_symlink = 0;
1353 * Blob object content specifies the target path of the link.
1354 * If a symbolic link cannot be installed we instead create
1355 * a regular file which contains the link target path stored
1356 * in the blob object.
1358 do {
1359 err = got_object_blob_read_block(&len, blob);
1360 if (len + target_len >= sizeof(target_path)) {
1361 /* Path too long; install as a regular file. */
1362 *is_bad_symlink = 1;
1363 got_object_blob_rewind(blob);
1364 return install_blob(worktree, ondisk_path, path,
1365 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1366 restoring_missing_file, reverting_versioned_file,
1367 1, path_is_unversioned, repo, progress_cb,
1368 progress_arg);
1370 if (len > 0) {
1371 /* Skip blob object header first time around. */
1372 memcpy(target_path + target_len, buf + hdrlen,
1373 len - hdrlen);
1374 target_len += len - hdrlen;
1375 hdrlen = 0;
1377 } while (len != 0);
1378 target_path[target_len] = '\0';
1380 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1381 ondisk_path, worktree->root_path);
1382 if (err)
1383 return err;
1385 if (*is_bad_symlink) {
1386 /* install as a regular file */
1387 *is_bad_symlink = 1;
1388 got_object_blob_rewind(blob);
1389 err = install_blob(worktree, ondisk_path, path,
1390 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 restoring_missing_file, reverting_versioned_file, 1,
1392 path_is_unversioned, repo, progress_cb, progress_arg);
1393 goto done;
1396 if (symlink(target_path, ondisk_path) == -1) {
1397 if (errno == EEXIST) {
1398 if (path_is_unversioned) {
1399 err = (*progress_cb)(progress_arg,
1400 GOT_STATUS_UNVERSIONED, path);
1401 goto done;
1403 err = replace_existing_symlink(ondisk_path,
1404 target_path, target_len);
1405 if (err)
1406 goto done;
1407 if (progress_cb) {
1408 err = (*progress_cb)(progress_arg,
1409 reverting_versioned_file ?
1410 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1411 path);
1413 goto done; /* Nothing else to do. */
1416 if (errno == ENOENT) {
1417 char *parent;
1418 err = got_path_dirname(&parent, ondisk_path);
1419 if (err)
1420 goto done;
1421 err = add_dir_on_disk(worktree, parent);
1422 free(parent);
1423 if (err)
1424 goto done;
1426 * Retry, and fall through to error handling
1427 * below if this second attempt fails.
1429 if (symlink(target_path, ondisk_path) != -1) {
1430 err = NULL; /* success */
1431 goto done;
1435 /* Handle errors from first or second creation attempt. */
1436 if (errno == ENAMETOOLONG) {
1437 /* bad target path; install as a regular file */
1438 *is_bad_symlink = 1;
1439 got_object_blob_rewind(blob);
1440 err = install_blob(worktree, ondisk_path, path,
1441 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1442 restoring_missing_file, reverting_versioned_file, 1,
1443 path_is_unversioned, repo,
1444 progress_cb, progress_arg);
1445 } else if (errno == ENOTDIR) {
1446 err = got_error_path(ondisk_path,
1447 GOT_ERR_FILE_OBSTRUCTED);
1448 } else {
1449 err = got_error_from_errno3("symlink",
1450 target_path, ondisk_path);
1452 } else if (progress_cb)
1453 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1454 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1455 done:
1456 free(path_got);
1457 return err;
1460 static const struct got_error *
1461 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1462 const char *path, mode_t te_mode, mode_t st_mode,
1463 struct got_blob_object *blob, int restoring_missing_file,
1464 int reverting_versioned_file, int installing_bad_symlink,
1465 int path_is_unversioned, struct got_repository *repo,
1466 got_worktree_checkout_cb progress_cb, void *progress_arg)
1468 const struct got_error *err = NULL;
1469 int fd = -1;
1470 size_t len, hdrlen;
1471 int update = 0;
1472 char *tmppath = NULL;
1474 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1475 GOT_DEFAULT_FILE_MODE);
1476 if (fd == -1) {
1477 if (errno == ENOENT) {
1478 char *parent;
1479 err = got_path_dirname(&parent, path);
1480 if (err)
1481 return err;
1482 err = add_dir_on_disk(worktree, parent);
1483 free(parent);
1484 if (err)
1485 return err;
1486 fd = open(ondisk_path,
1487 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1488 GOT_DEFAULT_FILE_MODE);
1489 if (fd == -1)
1490 return got_error_from_errno2("open",
1491 ondisk_path);
1492 } else if (errno == EEXIST) {
1493 if (path_is_unversioned) {
1494 err = (*progress_cb)(progress_arg,
1495 GOT_STATUS_UNVERSIONED, path);
1496 goto done;
1498 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1499 /* TODO file is obstructed; do something */
1500 err = got_error_path(ondisk_path,
1501 GOT_ERR_FILE_OBSTRUCTED);
1502 goto done;
1503 } else {
1504 err = got_opentemp_named_fd(&tmppath, &fd,
1505 ondisk_path);
1506 if (err)
1507 goto done;
1508 update = 1;
1510 } else
1511 return got_error_from_errno2("open", ondisk_path);
1514 if (progress_cb) {
1515 if (restoring_missing_file)
1516 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1517 path);
1518 else if (reverting_versioned_file)
1519 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1520 path);
1521 else
1522 err = (*progress_cb)(progress_arg,
1523 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1524 if (err)
1525 goto done;
1528 hdrlen = got_object_blob_get_hdrlen(blob);
1529 do {
1530 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1531 err = got_object_blob_read_block(&len, blob);
1532 if (err)
1533 break;
1534 if (len > 0) {
1535 /* Skip blob object header first time around. */
1536 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1537 if (outlen == -1) {
1538 err = got_error_from_errno("write");
1539 goto done;
1540 } else if (outlen != len - hdrlen) {
1541 err = got_error(GOT_ERR_IO);
1542 goto done;
1544 hdrlen = 0;
1546 } while (len != 0);
1548 if (fsync(fd) != 0) {
1549 err = got_error_from_errno("fsync");
1550 goto done;
1553 if (update) {
1554 if (rename(tmppath, ondisk_path) != 0) {
1555 err = got_error_from_errno3("rename", tmppath,
1556 ondisk_path);
1557 unlink(tmppath);
1558 goto done;
1562 if (chmod(ondisk_path,
1563 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1564 err = got_error_from_errno2("chmod", ondisk_path);
1565 goto done;
1568 done:
1569 if (fd != -1 && close(fd) != 0 && err == NULL)
1570 err = got_error_from_errno("close");
1571 free(tmppath);
1572 return err;
1575 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1576 static const struct got_error *
1577 get_modified_file_content_status(unsigned char *status, FILE *f)
1579 const struct got_error *err = NULL;
1580 const char *markers[3] = {
1581 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1582 GOT_DIFF_CONFLICT_MARKER_SEP,
1583 GOT_DIFF_CONFLICT_MARKER_END
1585 int i = 0;
1586 char *line;
1587 size_t len;
1588 const char delim[3] = {'\0', '\0', '\0'};
1590 while (*status == GOT_STATUS_MODIFY) {
1591 line = fparseln(f, &len, NULL, delim, 0);
1592 if (line == NULL) {
1593 if (feof(f))
1594 break;
1595 err = got_ferror(f, GOT_ERR_IO);
1596 break;
1599 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1600 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1601 == 0)
1602 *status = GOT_STATUS_CONFLICT;
1603 else
1604 i++;
1608 return err;
1611 static int
1612 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1614 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1615 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1618 static int
1619 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1621 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1622 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1623 ie->mtime_sec == sb->st_mtim.tv_sec &&
1624 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1625 ie->size == (sb->st_size & 0xffffffff) &&
1626 !xbit_differs(ie, sb->st_mode));
1629 static unsigned char
1630 get_staged_status(struct got_fileindex_entry *ie)
1632 switch (got_fileindex_entry_stage_get(ie)) {
1633 case GOT_FILEIDX_STAGE_ADD:
1634 return GOT_STATUS_ADD;
1635 case GOT_FILEIDX_STAGE_DELETE:
1636 return GOT_STATUS_DELETE;
1637 case GOT_FILEIDX_STAGE_MODIFY:
1638 return GOT_STATUS_MODIFY;
1639 default:
1640 return GOT_STATUS_NO_CHANGE;
1644 static const struct got_error *
1645 get_symlink_modification_status(unsigned char *status,
1646 struct got_fileindex_entry *ie, const char *abspath,
1647 int dirfd, const char *de_name, struct got_blob_object *blob)
1649 const struct got_error *err = NULL;
1650 char target_path[PATH_MAX];
1651 char etarget[PATH_MAX];
1652 ssize_t elen;
1653 size_t len, target_len = 0;
1654 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1655 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1657 *status = GOT_STATUS_NO_CHANGE;
1659 /* Blob object content specifies the target path of the link. */
1660 do {
1661 err = got_object_blob_read_block(&len, blob);
1662 if (err)
1663 return err;
1664 if (len + target_len >= sizeof(target_path)) {
1666 * Should not happen. The blob contents were OK
1667 * when this symlink was installed.
1669 return got_error(GOT_ERR_NO_SPACE);
1671 if (len > 0) {
1672 /* Skip blob object header first time around. */
1673 memcpy(target_path + target_len, buf + hdrlen,
1674 len - hdrlen);
1675 target_len += len - hdrlen;
1676 hdrlen = 0;
1678 } while (len != 0);
1679 target_path[target_len] = '\0';
1681 if (dirfd != -1) {
1682 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1683 if (elen == -1)
1684 return got_error_from_errno2("readlinkat", abspath);
1685 } else {
1686 elen = readlink(abspath, etarget, sizeof(etarget));
1687 if (elen == -1)
1688 return got_error_from_errno2("readlink", abspath);
1691 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1692 *status = GOT_STATUS_MODIFY;
1694 return NULL;
1697 static const struct got_error *
1698 get_file_status(unsigned char *status, struct stat *sb,
1699 struct got_fileindex_entry *ie, const char *abspath,
1700 int dirfd, const char *de_name, struct got_repository *repo)
1702 const struct got_error *err = NULL;
1703 struct got_object_id id;
1704 size_t hdrlen;
1705 int fd = -1;
1706 FILE *f = NULL;
1707 uint8_t fbuf[8192];
1708 struct got_blob_object *blob = NULL;
1709 size_t flen, blen;
1710 unsigned char staged_status = get_staged_status(ie);
1712 *status = GOT_STATUS_NO_CHANGE;
1715 * Whenever the caller provides a directory descriptor and a
1716 * directory entry name for the file, use them! This prevents
1717 * race conditions if filesystem paths change beneath our feet.
1719 if (dirfd != -1) {
1720 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1721 if (errno == ENOENT) {
1722 if (got_fileindex_entry_has_file_on_disk(ie))
1723 *status = GOT_STATUS_MISSING;
1724 else
1725 *status = GOT_STATUS_DELETE;
1726 goto done;
1728 err = got_error_from_errno2("fstatat", abspath);
1729 goto done;
1731 } else {
1732 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1733 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1734 return got_error_from_errno2("open", abspath);
1735 else if (fd == -1 && errno == ELOOP) {
1736 if (lstat(abspath, sb) == -1)
1737 return got_error_from_errno2("lstat", abspath);
1738 } else if (fd == -1 || fstat(fd, sb) == -1) {
1739 if (errno == ENOENT) {
1740 if (got_fileindex_entry_has_file_on_disk(ie))
1741 *status = GOT_STATUS_MISSING;
1742 else
1743 *status = GOT_STATUS_DELETE;
1744 goto done;
1746 err = got_error_from_errno2("fstat", abspath);
1747 goto done;
1751 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1752 *status = GOT_STATUS_OBSTRUCTED;
1753 goto done;
1756 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1757 *status = GOT_STATUS_DELETE;
1758 goto done;
1759 } else if (!got_fileindex_entry_has_blob(ie) &&
1760 staged_status != GOT_STATUS_ADD) {
1761 *status = GOT_STATUS_ADD;
1762 goto done;
1765 if (!stat_info_differs(ie, sb))
1766 goto done;
1768 if (S_ISLNK(sb->st_mode) &&
1769 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1770 *status = GOT_STATUS_MODIFY;
1771 goto done;
1774 if (staged_status == GOT_STATUS_MODIFY ||
1775 staged_status == GOT_STATUS_ADD)
1776 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1777 else
1778 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1780 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1781 if (err)
1782 goto done;
1784 if (S_ISLNK(sb->st_mode)) {
1785 err = get_symlink_modification_status(status, ie,
1786 abspath, dirfd, de_name, blob);
1787 goto done;
1790 if (dirfd != -1) {
1791 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1792 if (fd == -1) {
1793 err = got_error_from_errno2("openat", abspath);
1794 goto done;
1798 f = fdopen(fd, "r");
1799 if (f == NULL) {
1800 err = got_error_from_errno2("fdopen", abspath);
1801 goto done;
1803 fd = -1;
1804 hdrlen = got_object_blob_get_hdrlen(blob);
1805 for (;;) {
1806 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1807 err = got_object_blob_read_block(&blen, blob);
1808 if (err)
1809 goto done;
1810 /* Skip length of blob object header first time around. */
1811 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1812 if (flen == 0 && ferror(f)) {
1813 err = got_error_from_errno("fread");
1814 goto done;
1816 if (blen - hdrlen == 0) {
1817 if (flen != 0)
1818 *status = GOT_STATUS_MODIFY;
1819 break;
1820 } else if (flen == 0) {
1821 if (blen - hdrlen != 0)
1822 *status = GOT_STATUS_MODIFY;
1823 break;
1824 } else if (blen - hdrlen == flen) {
1825 /* Skip blob object header first time around. */
1826 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1827 *status = GOT_STATUS_MODIFY;
1828 break;
1830 } else {
1831 *status = GOT_STATUS_MODIFY;
1832 break;
1834 hdrlen = 0;
1837 if (*status == GOT_STATUS_MODIFY) {
1838 rewind(f);
1839 err = get_modified_file_content_status(status, f);
1840 } else if (xbit_differs(ie, sb->st_mode))
1841 *status = GOT_STATUS_MODE_CHANGE;
1842 done:
1843 if (blob)
1844 got_object_blob_close(blob);
1845 if (f != NULL && fclose(f) == EOF && err == NULL)
1846 err = got_error_from_errno2("fclose", abspath);
1847 if (fd != -1 && close(fd) == -1 && err == NULL)
1848 err = got_error_from_errno2("close", abspath);
1849 return err;
1853 * Update timestamps in the file index if a file is unmodified and
1854 * we had to run a full content comparison to find out.
1856 static const struct got_error *
1857 sync_timestamps(char *ondisk_path, unsigned char status,
1858 struct got_fileindex_entry *ie, struct stat *sb)
1860 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1861 return got_fileindex_entry_update(ie, ondisk_path,
1862 ie->blob_sha1, ie->commit_sha1, 1);
1864 return NULL;
1867 static const struct got_error *
1868 update_blob(struct got_worktree *worktree,
1869 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1870 struct got_tree_entry *te, const char *path,
1871 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1872 void *progress_arg)
1874 const struct got_error *err = NULL;
1875 struct got_blob_object *blob = NULL;
1876 char *ondisk_path;
1877 unsigned char status = GOT_STATUS_NO_CHANGE;
1878 struct stat sb;
1880 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1881 return got_error_from_errno("asprintf");
1883 if (ie) {
1884 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1885 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1886 goto done;
1888 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1889 repo);
1890 if (err)
1891 goto done;
1892 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1893 sb.st_mode = got_fileindex_perms_to_st(ie);
1894 } else {
1895 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1896 status = GOT_STATUS_UNVERSIONED;
1899 if (status == GOT_STATUS_OBSTRUCTED) {
1900 err = (*progress_cb)(progress_arg, status, path);
1901 goto done;
1903 if (status == GOT_STATUS_CONFLICT) {
1904 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1905 path);
1906 goto done;
1909 if (ie && status != GOT_STATUS_MISSING &&
1910 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1911 if (got_fileindex_entry_has_commit(ie) &&
1912 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1913 SHA1_DIGEST_LENGTH) == 0) {
1914 err = sync_timestamps(ondisk_path, status, ie, &sb);
1915 if (err)
1916 goto done;
1917 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1918 path);
1919 goto done;
1921 if (got_fileindex_entry_has_blob(ie) &&
1922 memcmp(ie->blob_sha1, te->id.sha1,
1923 SHA1_DIGEST_LENGTH) == 0) {
1924 err = sync_timestamps(ondisk_path, status, ie, &sb);
1925 goto done;
1929 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1930 if (err)
1931 goto done;
1933 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1934 int update_timestamps;
1935 struct got_blob_object *blob2 = NULL;
1936 char *label_orig = NULL;
1937 if (got_fileindex_entry_has_blob(ie)) {
1938 struct got_object_id id2;
1939 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1940 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1941 if (err)
1942 goto done;
1944 if (got_fileindex_entry_has_commit(ie)) {
1945 char id_str[SHA1_DIGEST_STRING_LENGTH];
1946 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1947 sizeof(id_str)) == NULL) {
1948 err = got_error_path(id_str,
1949 GOT_ERR_BAD_OBJ_ID_STR);
1950 goto done;
1952 if (asprintf(&label_orig, "%s: commit %s",
1953 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1954 err = got_error_from_errno("asprintf");
1955 goto done;
1958 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1959 char *link_target;
1960 err = got_object_blob_read_to_str(&link_target, blob);
1961 if (err)
1962 goto done;
1963 err = merge_symlink(worktree, blob2, ondisk_path, path,
1964 label_orig, link_target, worktree->base_commit_id,
1965 repo, progress_cb, progress_arg);
1966 free(link_target);
1967 } else {
1968 err = merge_blob(&update_timestamps, worktree, blob2,
1969 ondisk_path, path, sb.st_mode, label_orig, blob,
1970 worktree->base_commit_id, repo,
1971 progress_cb, progress_arg);
1973 free(label_orig);
1974 if (blob2)
1975 got_object_blob_close(blob2);
1976 if (err)
1977 goto done;
1979 * Do not update timestamps of files with local changes.
1980 * Otherwise, a future status walk would treat them as
1981 * unmodified files again.
1983 err = got_fileindex_entry_update(ie, ondisk_path,
1984 blob->id.sha1, worktree->base_commit_id->sha1,
1985 update_timestamps);
1986 } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1989 } else if (status == GOT_STATUS_DELETE) {
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1991 if (err)
1992 goto done;
1993 err = got_fileindex_entry_update(ie, ondisk_path,
1994 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1995 if (err)
1996 goto done;
1997 } else {
1998 int is_bad_symlink = 0;
1999 if (S_ISLNK(te->mode)) {
2000 err = install_symlink(&is_bad_symlink, worktree,
2001 ondisk_path, path, blob,
2002 status == GOT_STATUS_MISSING, 0,
2003 status == GOT_STATUS_UNVERSIONED, repo,
2004 progress_cb, progress_arg);
2005 } else {
2006 err = install_blob(worktree, ondisk_path, path,
2007 te->mode, sb.st_mode, blob,
2008 status == GOT_STATUS_MISSING, 0, 0,
2009 status == GOT_STATUS_UNVERSIONED, repo,
2010 progress_cb, progress_arg);
2012 if (err)
2013 goto done;
2015 if (ie) {
2016 err = got_fileindex_entry_update(ie, ondisk_path,
2017 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2018 } else {
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, ondisk_path, path,
2021 &blob->id);
2023 if (err)
2024 goto done;
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2031 got_object_blob_close(blob);
2032 done:
2033 free(ondisk_path);
2034 return err;
2037 static const struct got_error *
2038 remove_ondisk_file(const char *root_path, const char *path)
2040 const struct got_error *err = NULL;
2041 char *ondisk_path = NULL;
2043 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2044 return got_error_from_errno("asprintf");
2046 if (unlink(ondisk_path) == -1) {
2047 if (errno != ENOENT)
2048 err = got_error_from_errno2("unlink", ondisk_path);
2049 } else {
2050 size_t root_len = strlen(root_path);
2051 do {
2052 char *parent;
2053 err = got_path_dirname(&parent, ondisk_path);
2054 if (err)
2055 break;
2056 free(ondisk_path);
2057 ondisk_path = parent;
2058 if (rmdir(ondisk_path) == -1) {
2059 if (errno != ENOTEMPTY)
2060 err = got_error_from_errno2("rmdir",
2061 ondisk_path);
2062 break;
2064 } while (got_path_cmp(ondisk_path, root_path,
2065 strlen(ondisk_path), root_len) != 0);
2067 free(ondisk_path);
2068 return err;
2071 static const struct got_error *
2072 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2073 struct got_fileindex_entry *ie, struct got_repository *repo,
2074 got_worktree_checkout_cb progress_cb, void *progress_arg)
2076 const struct got_error *err = NULL;
2077 unsigned char status;
2078 struct stat sb;
2079 char *ondisk_path;
2081 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2082 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2084 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2085 == -1)
2086 return got_error_from_errno("asprintf");
2088 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2089 if (err)
2090 goto done;
2092 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2093 char ondisk_target[PATH_MAX];
2094 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2095 sizeof(ondisk_target));
2096 if (ondisk_len == -1) {
2097 err = got_error_from_errno2("readlink", ondisk_path);
2098 goto done;
2100 ondisk_target[ondisk_len] = '\0';
2101 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2102 NULL, NULL, /* XXX pass common ancestor info? */
2103 ondisk_target, ondisk_path);
2104 if (err)
2105 goto done;
2106 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2107 ie->path);
2108 goto done;
2111 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2112 status == GOT_STATUS_ADD) {
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2114 if (err)
2115 goto done;
2117 * Preserve the working file and change the deleted blob's
2118 * entry into a schedule-add entry.
2120 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2121 0);
2122 } else {
2123 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2124 if (err)
2125 goto done;
2126 if (status == GOT_STATUS_NO_CHANGE) {
2127 err = remove_ondisk_file(worktree->root_path, ie->path);
2128 if (err)
2129 goto done;
2131 got_fileindex_entry_remove(fileindex, ie);
2133 done:
2134 free(ondisk_path);
2135 return err;
2138 struct diff_cb_arg {
2139 struct got_fileindex *fileindex;
2140 struct got_worktree *worktree;
2141 struct got_repository *repo;
2142 got_worktree_checkout_cb progress_cb;
2143 void *progress_arg;
2144 got_cancel_cb cancel_cb;
2145 void *cancel_arg;
2148 static const struct got_error *
2149 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2150 struct got_tree_entry *te, const char *parent_path)
2152 struct diff_cb_arg *a = arg;
2154 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2155 return got_error(GOT_ERR_CANCELLED);
2157 return update_blob(a->worktree, a->fileindex, ie, te,
2158 ie->path, a->repo, a->progress_cb, a->progress_arg);
2161 static const struct got_error *
2162 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2164 struct diff_cb_arg *a = arg;
2166 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2167 return got_error(GOT_ERR_CANCELLED);
2169 return delete_blob(a->worktree, a->fileindex, ie,
2170 a->repo, a->progress_cb, a->progress_arg);
2173 static const struct got_error *
2174 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2176 struct diff_cb_arg *a = arg;
2177 const struct got_error *err;
2178 char *path;
2180 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2181 return got_error(GOT_ERR_CANCELLED);
2183 if (got_object_tree_entry_is_submodule(te))
2184 return NULL;
2186 if (asprintf(&path, "%s%s%s", parent_path,
2187 parent_path[0] ? "/" : "", te->name)
2188 == -1)
2189 return got_error_from_errno("asprintf");
2191 if (S_ISDIR(te->mode))
2192 err = add_dir_on_disk(a->worktree, path);
2193 else
2194 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2195 a->repo, a->progress_cb, a->progress_arg);
2197 free(path);
2198 return err;
2201 const struct got_error *
2202 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2204 uint32_t uuid_status;
2206 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2207 if (uuid_status != uuid_s_ok) {
2208 *uuidstr = NULL;
2209 return got_error_uuid(uuid_status, "uuid_to_string");
2212 return NULL;
2215 static const struct got_error *
2216 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2218 const struct got_error *err = NULL;
2219 char *uuidstr = NULL;
2221 *refname = NULL;
2223 err = got_worktree_get_uuid(&uuidstr, worktree);
2224 if (err)
2225 return err;
2227 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2228 err = got_error_from_errno("asprintf");
2229 *refname = NULL;
2231 free(uuidstr);
2232 return err;
2235 const struct got_error *
2236 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2241 static const struct got_error *
2242 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree,
2245 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2248 static const struct got_error *
2249 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2254 static const struct got_error *
2255 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree,
2258 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2261 static const struct got_error *
2262 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2268 static const struct got_error *
2269 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree,
2272 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2275 static const struct got_error *
2276 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2282 static const struct got_error *
2283 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2296 const struct got_error *
2297 got_worktree_get_histedit_script_path(char **path,
2298 struct got_worktree *worktree)
2300 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2301 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2302 *path = NULL;
2303 return got_error_from_errno("asprintf");
2305 return NULL;
2309 * Prevent Git's garbage collector from deleting our base commit by
2310 * setting a reference to our base commit's ID.
2312 static const struct got_error *
2313 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2315 const struct got_error *err = NULL;
2316 struct got_reference *ref = NULL;
2317 char *refname;
2319 err = got_worktree_get_base_ref_name(&refname, worktree);
2320 if (err)
2321 return err;
2323 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2324 if (err)
2325 goto done;
2327 err = got_ref_write(ref, repo);
2328 done:
2329 free(refname);
2330 if (ref)
2331 got_ref_close(ref);
2332 return err;
2335 static const struct got_error *
2336 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2338 const struct got_error *err = NULL;
2340 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2341 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2342 err = got_error_from_errno("asprintf");
2343 *fileindex_path = NULL;
2345 return err;
2349 static const struct got_error *
2350 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2351 struct got_worktree *worktree)
2353 const struct got_error *err = NULL;
2354 FILE *index = NULL;
2356 *fileindex_path = NULL;
2357 *fileindex = got_fileindex_alloc();
2358 if (*fileindex == NULL)
2359 return got_error_from_errno("got_fileindex_alloc");
2361 err = get_fileindex_path(fileindex_path, worktree);
2362 if (err)
2363 goto done;
2365 index = fopen(*fileindex_path, "rb");
2366 if (index == NULL) {
2367 if (errno != ENOENT)
2368 err = got_error_from_errno2("fopen", *fileindex_path);
2369 } else {
2370 err = got_fileindex_read(*fileindex, index);
2371 if (fclose(index) != 0 && err == NULL)
2372 err = got_error_from_errno("fclose");
2374 done:
2375 if (err) {
2376 free(*fileindex_path);
2377 *fileindex_path = NULL;
2378 got_fileindex_free(*fileindex);
2379 *fileindex = NULL;
2381 return err;
2384 struct bump_base_commit_id_arg {
2385 struct got_object_id *base_commit_id;
2386 const char *path;
2387 size_t path_len;
2388 const char *entry_name;
2389 got_worktree_checkout_cb progress_cb;
2390 void *progress_arg;
2393 /* Bump base commit ID of all files within an updated part of the work tree. */
2394 static const struct got_error *
2395 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2397 const struct got_error *err;
2398 struct bump_base_commit_id_arg *a = arg;
2400 if (a->entry_name) {
2401 if (strcmp(ie->path, a->path) != 0)
2402 return NULL;
2403 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2404 return NULL;
2406 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2407 SHA1_DIGEST_LENGTH) == 0)
2408 return NULL;
2410 if (a->progress_cb) {
2411 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2412 ie->path);
2413 if (err)
2414 return err;
2416 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2417 return NULL;
2420 static const struct got_error *
2421 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2423 const struct got_error *err = NULL;
2424 char *new_fileindex_path = NULL;
2425 FILE *new_index = NULL;
2426 struct timespec timeout;
2428 err = got_opentemp_named(&new_fileindex_path, &new_index,
2429 fileindex_path);
2430 if (err)
2431 goto done;
2433 err = got_fileindex_write(fileindex, new_index);
2434 if (err)
2435 goto done;
2437 if (rename(new_fileindex_path, fileindex_path) != 0) {
2438 err = got_error_from_errno3("rename", new_fileindex_path,
2439 fileindex_path);
2440 unlink(new_fileindex_path);
2444 * Sleep for a short amount of time to ensure that files modified after
2445 * this program exits have a different time stamp from the one which
2446 * was recorded in the file index.
2448 timeout.tv_sec = 0;
2449 timeout.tv_nsec = 1;
2450 nanosleep(&timeout, NULL);
2451 done:
2452 if (new_index)
2453 fclose(new_index);
2454 free(new_fileindex_path);
2455 return err;
2458 static const struct got_error *
2459 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2460 struct got_object_id **tree_id, const char *wt_relpath,
2461 struct got_worktree *worktree, struct got_repository *repo)
2463 const struct got_error *err = NULL;
2464 struct got_object_id *id = NULL;
2465 char *in_repo_path = NULL;
2466 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2468 *entry_type = GOT_OBJ_TYPE_ANY;
2469 *tree_relpath = NULL;
2470 *tree_id = NULL;
2472 if (wt_relpath[0] == '\0') {
2473 /* Check out all files within the work tree. */
2474 *entry_type = GOT_OBJ_TYPE_TREE;
2475 *tree_relpath = strdup("");
2476 if (*tree_relpath == NULL) {
2477 err = got_error_from_errno("strdup");
2478 goto done;
2480 err = got_object_id_by_path(tree_id, repo,
2481 worktree->base_commit_id, worktree->path_prefix);
2482 if (err)
2483 goto done;
2484 return NULL;
2487 /* Check out a subset of files in the work tree. */
2489 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2490 is_root_wt ? "" : "/", wt_relpath) == -1) {
2491 err = got_error_from_errno("asprintf");
2492 goto done;
2495 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2496 in_repo_path);
2497 if (err)
2498 goto done;
2500 free(in_repo_path);
2501 in_repo_path = NULL;
2503 err = got_object_get_type(entry_type, repo, id);
2504 if (err)
2505 goto done;
2507 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2508 /* Check out a single file. */
2509 if (strchr(wt_relpath, '/') == NULL) {
2510 /* Check out a single file in work tree's root dir. */
2511 in_repo_path = strdup(worktree->path_prefix);
2512 if (in_repo_path == NULL) {
2513 err = got_error_from_errno("strdup");
2514 goto done;
2516 *tree_relpath = strdup("");
2517 if (*tree_relpath == NULL) {
2518 err = got_error_from_errno("strdup");
2519 goto done;
2521 } else {
2522 /* Check out a single file in a subdirectory. */
2523 err = got_path_dirname(tree_relpath, wt_relpath);
2524 if (err)
2525 return err;
2526 if (asprintf(&in_repo_path, "%s%s%s",
2527 worktree->path_prefix, is_root_wt ? "" : "/",
2528 *tree_relpath) == -1) {
2529 err = got_error_from_errno("asprintf");
2530 goto done;
2533 err = got_object_id_by_path(tree_id, repo,
2534 worktree->base_commit_id, in_repo_path);
2535 } else {
2536 /* Check out all files within a subdirectory. */
2537 *tree_id = got_object_id_dup(id);
2538 if (*tree_id == NULL) {
2539 err = got_error_from_errno("got_object_id_dup");
2540 goto done;
2542 *tree_relpath = strdup(wt_relpath);
2543 if (*tree_relpath == NULL) {
2544 err = got_error_from_errno("strdup");
2545 goto done;
2548 done:
2549 free(id);
2550 free(in_repo_path);
2551 if (err) {
2552 *entry_type = GOT_OBJ_TYPE_ANY;
2553 free(*tree_relpath);
2554 *tree_relpath = NULL;
2555 free(*tree_id);
2556 *tree_id = NULL;
2558 return err;
2561 static const struct got_error *
2562 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2563 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2564 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2565 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2567 const struct got_error *err = NULL;
2568 struct got_commit_object *commit = NULL;
2569 struct got_tree_object *tree = NULL;
2570 struct got_fileindex_diff_tree_cb diff_cb;
2571 struct diff_cb_arg arg;
2573 err = ref_base_commit(worktree, repo);
2574 if (err) {
2575 if (!(err->code == GOT_ERR_ERRNO &&
2576 (errno == EACCES || errno == EROFS)))
2577 goto done;
2578 err = (*progress_cb)(progress_arg,
2579 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2580 if (err)
2581 return err;
2584 err = got_object_open_as_commit(&commit, repo,
2585 worktree->base_commit_id);
2586 if (err)
2587 goto done;
2589 err = got_object_open_as_tree(&tree, repo, tree_id);
2590 if (err)
2591 goto done;
2593 if (entry_name &&
2594 got_object_tree_find_entry(tree, entry_name) == NULL) {
2595 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2596 goto done;
2599 diff_cb.diff_old_new = diff_old_new;
2600 diff_cb.diff_old = diff_old;
2601 diff_cb.diff_new = diff_new;
2602 arg.fileindex = fileindex;
2603 arg.worktree = worktree;
2604 arg.repo = repo;
2605 arg.progress_cb = progress_cb;
2606 arg.progress_arg = progress_arg;
2607 arg.cancel_cb = cancel_cb;
2608 arg.cancel_arg = cancel_arg;
2609 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2610 entry_name, repo, &diff_cb, &arg);
2611 done:
2612 if (tree)
2613 got_object_tree_close(tree);
2614 if (commit)
2615 got_object_commit_close(commit);
2616 return err;
2619 const struct got_error *
2620 got_worktree_checkout_files(struct got_worktree *worktree,
2621 struct got_pathlist_head *paths, struct got_repository *repo,
2622 got_worktree_checkout_cb progress_cb, void *progress_arg,
2623 got_cancel_cb cancel_cb, void *cancel_arg)
2625 const struct got_error *err = NULL, *sync_err, *unlockerr;
2626 struct got_commit_object *commit = NULL;
2627 struct got_tree_object *tree = NULL;
2628 struct got_fileindex *fileindex = NULL;
2629 char *fileindex_path = NULL;
2630 struct got_pathlist_entry *pe;
2631 struct tree_path_data {
2632 SIMPLEQ_ENTRY(tree_path_data) entry;
2633 struct got_object_id *tree_id;
2634 int entry_type;
2635 char *relpath;
2636 char *entry_name;
2637 } *tpd = NULL;
2638 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2640 SIMPLEQ_INIT(&tree_paths);
2642 err = lock_worktree(worktree, LOCK_EX);
2643 if (err)
2644 return err;
2646 /* Map all specified paths to in-repository trees. */
2647 TAILQ_FOREACH(pe, paths, entry) {
2648 tpd = malloc(sizeof(*tpd));
2649 if (tpd == NULL) {
2650 err = got_error_from_errno("malloc");
2651 goto done;
2654 err = find_tree_entry_for_checkout(&tpd->entry_type,
2655 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2656 if (err) {
2657 free(tpd);
2658 goto done;
2661 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2662 err = got_path_basename(&tpd->entry_name, pe->path);
2663 if (err) {
2664 free(tpd->relpath);
2665 free(tpd->tree_id);
2666 free(tpd);
2667 goto done;
2669 } else
2670 tpd->entry_name = NULL;
2672 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2676 * Read the file index.
2677 * Checking out files is supposed to be an idempotent operation.
2678 * If the on-disk file index is incomplete we will try to complete it.
2680 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2681 if (err)
2682 goto done;
2684 tpd = SIMPLEQ_FIRST(&tree_paths);
2685 TAILQ_FOREACH(pe, paths, entry) {
2686 struct bump_base_commit_id_arg bbc_arg;
2688 err = checkout_files(worktree, fileindex, tpd->relpath,
2689 tpd->tree_id, tpd->entry_name, repo,
2690 progress_cb, progress_arg, cancel_cb, cancel_arg);
2691 if (err)
2692 break;
2694 bbc_arg.base_commit_id = worktree->base_commit_id;
2695 bbc_arg.entry_name = tpd->entry_name;
2696 bbc_arg.path = pe->path;
2697 bbc_arg.path_len = pe->path_len;
2698 bbc_arg.progress_cb = progress_cb;
2699 bbc_arg.progress_arg = progress_arg;
2700 err = got_fileindex_for_each_entry_safe(fileindex,
2701 bump_base_commit_id, &bbc_arg);
2702 if (err)
2703 break;
2705 tpd = SIMPLEQ_NEXT(tpd, entry);
2707 sync_err = sync_fileindex(fileindex, fileindex_path);
2708 if (sync_err && err == NULL)
2709 err = sync_err;
2710 done:
2711 free(fileindex_path);
2712 if (tree)
2713 got_object_tree_close(tree);
2714 if (commit)
2715 got_object_commit_close(commit);
2716 if (fileindex)
2717 got_fileindex_free(fileindex);
2718 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2719 tpd = SIMPLEQ_FIRST(&tree_paths);
2720 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2721 free(tpd->relpath);
2722 free(tpd->tree_id);
2723 free(tpd);
2725 unlockerr = lock_worktree(worktree, LOCK_SH);
2726 if (unlockerr && err == NULL)
2727 err = unlockerr;
2728 return err;
2731 struct merge_file_cb_arg {
2732 struct got_worktree *worktree;
2733 struct got_fileindex *fileindex;
2734 got_worktree_checkout_cb progress_cb;
2735 void *progress_arg;
2736 got_cancel_cb cancel_cb;
2737 void *cancel_arg;
2738 const char *label_orig;
2739 struct got_object_id *commit_id2;
2742 static const struct got_error *
2743 merge_file_cb(void *arg, struct got_blob_object *blob1,
2744 struct got_blob_object *blob2, struct got_object_id *id1,
2745 struct got_object_id *id2, const char *path1, const char *path2,
2746 mode_t mode1, mode_t mode2, struct got_repository *repo)
2748 static const struct got_error *err = NULL;
2749 struct merge_file_cb_arg *a = arg;
2750 struct got_fileindex_entry *ie;
2751 char *ondisk_path = NULL;
2752 struct stat sb;
2753 unsigned char status;
2754 int local_changes_subsumed;
2756 if (blob1 && blob2) {
2757 ie = got_fileindex_entry_get(a->fileindex, path2,
2758 strlen(path2));
2759 if (ie == NULL)
2760 return (*a->progress_cb)(a->progress_arg,
2761 GOT_STATUS_MISSING, path2);
2763 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2764 path2) == -1)
2765 return got_error_from_errno("asprintf");
2767 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2768 repo);
2769 if (err)
2770 goto done;
2772 if (status == GOT_STATUS_DELETE) {
2773 err = (*a->progress_cb)(a->progress_arg,
2774 GOT_STATUS_MERGE, path2);
2775 goto done;
2777 if (status != GOT_STATUS_NO_CHANGE &&
2778 status != GOT_STATUS_MODIFY &&
2779 status != GOT_STATUS_CONFLICT &&
2780 status != GOT_STATUS_ADD) {
2781 err = (*a->progress_cb)(a->progress_arg, status, path2);
2782 goto done;
2785 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2786 char *link_target2;
2787 err = got_object_blob_read_to_str(&link_target2, blob2);
2788 if (err)
2789 goto done;
2790 err = merge_symlink(a->worktree, blob1, ondisk_path,
2791 path2, a->label_orig, link_target2, a->commit_id2,
2792 repo, a->progress_cb, a->progress_arg);
2793 free(link_target2);
2794 } else {
2795 err = merge_blob(&local_changes_subsumed, a->worktree,
2796 blob1, ondisk_path, path2, sb.st_mode,
2797 a->label_orig, blob2, a->commit_id2, repo,
2798 a->progress_cb, a->progress_arg);
2800 } else if (blob1) {
2801 ie = got_fileindex_entry_get(a->fileindex, path1,
2802 strlen(path1));
2803 if (ie == NULL)
2804 return (*a->progress_cb)(a->progress_arg,
2805 GOT_STATUS_MISSING, path1);
2807 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2808 path1) == -1)
2809 return got_error_from_errno("asprintf");
2811 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2812 repo);
2813 if (err)
2814 goto done;
2816 switch (status) {
2817 case GOT_STATUS_NO_CHANGE:
2818 err = (*a->progress_cb)(a->progress_arg,
2819 GOT_STATUS_DELETE, path1);
2820 if (err)
2821 goto done;
2822 err = remove_ondisk_file(a->worktree->root_path, path1);
2823 if (err)
2824 goto done;
2825 if (ie)
2826 got_fileindex_entry_mark_deleted_from_disk(ie);
2827 break;
2828 case GOT_STATUS_DELETE:
2829 case GOT_STATUS_MISSING:
2830 err = (*a->progress_cb)(a->progress_arg,
2831 GOT_STATUS_DELETE, path1);
2832 if (err)
2833 goto done;
2834 if (ie)
2835 got_fileindex_entry_mark_deleted_from_disk(ie);
2836 break;
2837 case GOT_STATUS_ADD: {
2838 struct got_object_id *id;
2839 FILE *blob1_f;
2841 * Delete the added file only if its content already
2842 * exists in the repository.
2844 err = got_object_blob_file_create(&id, &blob1_f, path1);
2845 if (err)
2846 goto done;
2847 if (got_object_id_cmp(id, id1) == 0) {
2848 err = (*a->progress_cb)(a->progress_arg,
2849 GOT_STATUS_DELETE, path1);
2850 if (err)
2851 goto done;
2852 err = remove_ondisk_file(a->worktree->root_path,
2853 path1);
2854 if (err)
2855 goto done;
2856 if (ie)
2857 got_fileindex_entry_remove(a->fileindex,
2858 ie);
2859 } else {
2860 err = (*a->progress_cb)(a->progress_arg,
2861 GOT_STATUS_CANNOT_DELETE, path1);
2863 if (fclose(blob1_f) == EOF && err == NULL)
2864 err = got_error_from_errno("fclose");
2865 free(id);
2866 if (err)
2867 goto done;
2868 break;
2870 case GOT_STATUS_MODIFY:
2871 case GOT_STATUS_CONFLICT:
2872 err = (*a->progress_cb)(a->progress_arg,
2873 GOT_STATUS_CANNOT_DELETE, path1);
2874 if (err)
2875 goto done;
2876 break;
2877 case GOT_STATUS_OBSTRUCTED:
2878 err = (*a->progress_cb)(a->progress_arg, status, path1);
2879 if (err)
2880 goto done;
2881 break;
2882 default:
2883 break;
2885 } else if (blob2) {
2886 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2887 path2) == -1)
2888 return got_error_from_errno("asprintf");
2889 ie = got_fileindex_entry_get(a->fileindex, path2,
2890 strlen(path2));
2891 if (ie) {
2892 err = get_file_status(&status, &sb, ie, ondisk_path,
2893 -1, NULL, repo);
2894 if (err)
2895 goto done;
2896 if (status != GOT_STATUS_NO_CHANGE &&
2897 status != GOT_STATUS_MODIFY &&
2898 status != GOT_STATUS_CONFLICT &&
2899 status != GOT_STATUS_ADD) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 status, path2);
2902 goto done;
2904 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2905 char *link_target2;
2906 err = got_object_blob_read_to_str(&link_target2,
2907 blob2);
2908 if (err)
2909 goto done;
2910 err = merge_symlink(a->worktree, NULL,
2911 ondisk_path, path2, a->label_orig,
2912 link_target2, a->commit_id2, repo,
2913 a->progress_cb, a->progress_arg);
2914 free(link_target2);
2915 } else if (S_ISREG(sb.st_mode)) {
2916 err = merge_blob(&local_changes_subsumed,
2917 a->worktree, NULL, ondisk_path, path2,
2918 sb.st_mode, a->label_orig, blob2,
2919 a->commit_id2, repo, a->progress_cb,
2920 a->progress_arg);
2921 } else {
2922 err = got_error_path(ondisk_path,
2923 GOT_ERR_FILE_OBSTRUCTED);
2925 if (err)
2926 goto done;
2927 if (status == GOT_STATUS_DELETE) {
2928 err = got_fileindex_entry_update(ie,
2929 ondisk_path, blob2->id.sha1,
2930 a->worktree->base_commit_id->sha1, 0);
2931 if (err)
2932 goto done;
2934 } else {
2935 int is_bad_symlink = 0;
2936 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2937 if (S_ISLNK(mode2)) {
2938 err = install_symlink(&is_bad_symlink,
2939 a->worktree, ondisk_path, path2, blob2, 0,
2940 0, 1, repo, a->progress_cb, a->progress_arg);
2941 } else {
2942 err = install_blob(a->worktree, ondisk_path, path2,
2943 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2944 a->progress_cb, a->progress_arg);
2946 if (err)
2947 goto done;
2948 err = got_fileindex_entry_alloc(&ie, path2);
2949 if (err)
2950 goto done;
2951 err = got_fileindex_entry_update(ie, ondisk_path,
2952 NULL, NULL, 1);
2953 if (err) {
2954 got_fileindex_entry_free(ie);
2955 goto done;
2957 err = got_fileindex_entry_add(a->fileindex, ie);
2958 if (err) {
2959 got_fileindex_entry_free(ie);
2960 goto done;
2962 if (is_bad_symlink) {
2963 got_fileindex_entry_filetype_set(ie,
2964 GOT_FILEIDX_MODE_BAD_SYMLINK);
2968 done:
2969 free(ondisk_path);
2970 return err;
2973 struct check_merge_ok_arg {
2974 struct got_worktree *worktree;
2975 struct got_repository *repo;
2978 static const struct got_error *
2979 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2981 const struct got_error *err = NULL;
2982 struct check_merge_ok_arg *a = arg;
2983 unsigned char status;
2984 struct stat sb;
2985 char *ondisk_path;
2987 /* Reject merges into a work tree with mixed base commits. */
2988 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2989 SHA1_DIGEST_LENGTH))
2990 return got_error(GOT_ERR_MIXED_COMMITS);
2992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2993 == -1)
2994 return got_error_from_errno("asprintf");
2996 /* Reject merges into a work tree with conflicted files. */
2997 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2998 if (err)
2999 return err;
3000 if (status == GOT_STATUS_CONFLICT)
3001 return got_error(GOT_ERR_CONFLICTS);
3003 return NULL;
3006 static const struct got_error *
3007 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3008 const char *fileindex_path, struct got_object_id *commit_id1,
3009 struct got_object_id *commit_id2, struct got_repository *repo,
3010 got_worktree_checkout_cb progress_cb, void *progress_arg,
3011 got_cancel_cb cancel_cb, void *cancel_arg)
3013 const struct got_error *err = NULL, *sync_err;
3014 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3015 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3016 struct merge_file_cb_arg arg;
3017 char *label_orig = NULL;
3019 if (commit_id1) {
3020 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3021 worktree->path_prefix);
3022 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3023 goto done;
3025 if (tree_id1) {
3026 char *id_str;
3028 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3029 if (err)
3030 goto done;
3032 err = got_object_id_str(&id_str, commit_id1);
3033 if (err)
3034 goto done;
3036 if (asprintf(&label_orig, "%s: commit %s",
3037 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3038 err = got_error_from_errno("asprintf");
3039 free(id_str);
3040 goto done;
3042 free(id_str);
3045 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3046 worktree->path_prefix);
3047 if (err)
3048 goto done;
3050 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3051 if (err)
3052 goto done;
3054 arg.worktree = worktree;
3055 arg.fileindex = fileindex;
3056 arg.progress_cb = progress_cb;
3057 arg.progress_arg = progress_arg;
3058 arg.cancel_cb = cancel_cb;
3059 arg.cancel_arg = cancel_arg;
3060 arg.label_orig = label_orig;
3061 arg.commit_id2 = commit_id2;
3062 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3063 sync_err = sync_fileindex(fileindex, fileindex_path);
3064 if (sync_err && err == NULL)
3065 err = sync_err;
3066 done:
3067 if (tree1)
3068 got_object_tree_close(tree1);
3069 if (tree2)
3070 got_object_tree_close(tree2);
3071 free(label_orig);
3072 return err;
3075 const struct got_error *
3076 got_worktree_merge_files(struct got_worktree *worktree,
3077 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3078 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3079 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3081 const struct got_error *err, *unlockerr;
3082 char *fileindex_path = NULL;
3083 struct got_fileindex *fileindex = NULL;
3084 struct check_merge_ok_arg mok_arg;
3086 err = lock_worktree(worktree, LOCK_EX);
3087 if (err)
3088 return err;
3090 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3091 if (err)
3092 goto done;
3094 mok_arg.worktree = worktree;
3095 mok_arg.repo = repo;
3096 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3097 &mok_arg);
3098 if (err)
3099 goto done;
3101 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3102 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3103 done:
3104 if (fileindex)
3105 got_fileindex_free(fileindex);
3106 free(fileindex_path);
3107 unlockerr = lock_worktree(worktree, LOCK_SH);
3108 if (unlockerr && err == NULL)
3109 err = unlockerr;
3110 return err;
3113 struct diff_dir_cb_arg {
3114 struct got_fileindex *fileindex;
3115 struct got_worktree *worktree;
3116 const char *status_path;
3117 size_t status_path_len;
3118 struct got_repository *repo;
3119 got_worktree_status_cb status_cb;
3120 void *status_arg;
3121 got_cancel_cb cancel_cb;
3122 void *cancel_arg;
3123 /* A pathlist containing per-directory pathlists of ignore patterns. */
3124 struct got_pathlist_head ignores;
3125 int report_unchanged;
3126 int no_ignores;
3129 static const struct got_error *
3130 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3131 int dirfd, const char *de_name,
3132 got_worktree_status_cb status_cb, void *status_arg,
3133 struct got_repository *repo, int report_unchanged)
3135 const struct got_error *err = NULL;
3136 unsigned char status = GOT_STATUS_NO_CHANGE;
3137 unsigned char staged_status = get_staged_status(ie);
3138 struct stat sb;
3139 struct got_object_id blob_id, commit_id, staged_blob_id;
3140 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3141 struct got_object_id *staged_blob_idp = NULL;
3143 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3144 if (err)
3145 return err;
3147 if (status == GOT_STATUS_NO_CHANGE &&
3148 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3149 return NULL;
3151 if (got_fileindex_entry_has_blob(ie)) {
3152 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3153 blob_idp = &blob_id;
3155 if (got_fileindex_entry_has_commit(ie)) {
3156 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3157 commit_idp = &commit_id;
3159 if (staged_status == GOT_STATUS_ADD ||
3160 staged_status == GOT_STATUS_MODIFY) {
3161 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3162 SHA1_DIGEST_LENGTH);
3163 staged_blob_idp = &staged_blob_id;
3166 return (*status_cb)(status_arg, status, staged_status,
3167 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3170 static const struct got_error *
3171 status_old_new(void *arg, struct got_fileindex_entry *ie,
3172 struct dirent *de, const char *parent_path, int dirfd)
3174 const struct got_error *err = NULL;
3175 struct diff_dir_cb_arg *a = arg;
3176 char *abspath;
3178 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3179 return got_error(GOT_ERR_CANCELLED);
3181 if (got_path_cmp(parent_path, a->status_path,
3182 strlen(parent_path), a->status_path_len) != 0 &&
3183 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3184 return NULL;
3186 if (parent_path[0]) {
3187 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3188 parent_path, de->d_name) == -1)
3189 return got_error_from_errno("asprintf");
3190 } else {
3191 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3192 de->d_name) == -1)
3193 return got_error_from_errno("asprintf");
3196 err = report_file_status(ie, abspath, dirfd, de->d_name,
3197 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3198 free(abspath);
3199 return err;
3202 static const struct got_error *
3203 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3205 struct diff_dir_cb_arg *a = arg;
3206 struct got_object_id blob_id, commit_id;
3207 unsigned char status;
3209 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3210 return got_error(GOT_ERR_CANCELLED);
3212 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3213 return NULL;
3215 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3216 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3217 if (got_fileindex_entry_has_file_on_disk(ie))
3218 status = GOT_STATUS_MISSING;
3219 else
3220 status = GOT_STATUS_DELETE;
3221 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3222 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3225 void
3226 free_ignorelist(struct got_pathlist_head *ignorelist)
3228 struct got_pathlist_entry *pe;
3230 TAILQ_FOREACH(pe, ignorelist, entry)
3231 free((char *)pe->path);
3232 got_pathlist_free(ignorelist);
3235 void
3236 free_ignores(struct got_pathlist_head *ignores)
3238 struct got_pathlist_entry *pe;
3240 TAILQ_FOREACH(pe, ignores, entry) {
3241 struct got_pathlist_head *ignorelist = pe->data;
3242 free_ignorelist(ignorelist);
3243 free((char *)pe->path);
3245 got_pathlist_free(ignores);
3248 static const struct got_error *
3249 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3251 const struct got_error *err = NULL;
3252 struct got_pathlist_entry *pe = NULL;
3253 struct got_pathlist_head *ignorelist;
3254 char *line = NULL, *pattern, *dirpath = NULL;
3255 size_t linesize = 0;
3256 ssize_t linelen;
3258 ignorelist = calloc(1, sizeof(*ignorelist));
3259 if (ignorelist == NULL)
3260 return got_error_from_errno("calloc");
3261 TAILQ_INIT(ignorelist);
3263 while ((linelen = getline(&line, &linesize, f)) != -1) {
3264 if (linelen > 0 && line[linelen - 1] == '\n')
3265 line[linelen - 1] = '\0';
3267 /* Git's ignores may contain comments. */
3268 if (line[0] == '#')
3269 continue;
3271 /* Git's negated patterns are not (yet?) supported. */
3272 if (line[0] == '!')
3273 continue;
3275 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3276 line) == -1) {
3277 err = got_error_from_errno("asprintf");
3278 goto done;
3280 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3281 if (err)
3282 goto done;
3284 if (ferror(f)) {
3285 err = got_error_from_errno("getline");
3286 goto done;
3289 dirpath = strdup(path);
3290 if (dirpath == NULL) {
3291 err = got_error_from_errno("strdup");
3292 goto done;
3294 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3295 done:
3296 free(line);
3297 if (err || pe == NULL) {
3298 free(dirpath);
3299 free_ignorelist(ignorelist);
3301 return err;
3304 int
3305 match_ignores(struct got_pathlist_head *ignores, const char *path)
3307 struct got_pathlist_entry *pe;
3309 /* Handle patterns which match in all directories. */
3310 TAILQ_FOREACH(pe, ignores, entry) {
3311 struct got_pathlist_head *ignorelist = pe->data;
3312 struct got_pathlist_entry *pi;
3314 TAILQ_FOREACH(pi, ignorelist, entry) {
3315 const char *p, *pattern = pi->path;
3317 if (strncmp(pattern, "**/", 3) != 0)
3318 continue;
3319 pattern += 3;
3320 p = path;
3321 while (*p) {
3322 if (fnmatch(pattern, p,
3323 FNM_PATHNAME | FNM_LEADING_DIR)) {
3324 /* Retry in next directory. */
3325 while (*p && *p != '/')
3326 p++;
3327 while (*p == '/')
3328 p++;
3329 continue;
3331 return 1;
3337 * The ignores pathlist contains ignore lists from children before
3338 * parents, so we can find the most specific ignorelist by walking
3339 * ignores backwards.
3341 pe = TAILQ_LAST(ignores, got_pathlist_head);
3342 while (pe) {
3343 if (got_path_is_child(path, pe->path, pe->path_len)) {
3344 struct got_pathlist_head *ignorelist = pe->data;
3345 struct got_pathlist_entry *pi;
3346 TAILQ_FOREACH(pi, ignorelist, entry) {
3347 const char *pattern = pi->path;
3348 int flags = FNM_LEADING_DIR;
3349 if (strstr(pattern, "/**/") == NULL)
3350 flags |= FNM_PATHNAME;
3351 if (fnmatch(pattern, path, flags))
3352 continue;
3353 return 1;
3356 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3359 return 0;
3362 static const struct got_error *
3363 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3364 const char *path, int dirfd, const char *ignores_filename)
3366 const struct got_error *err = NULL;
3367 char *ignorespath;
3368 int fd = -1;
3369 FILE *ignoresfile = NULL;
3371 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3372 path[0] ? "/" : "", ignores_filename) == -1)
3373 return got_error_from_errno("asprintf");
3375 if (dirfd != -1) {
3376 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3377 if (fd == -1) {
3378 if (errno != ENOENT && errno != EACCES)
3379 err = got_error_from_errno2("openat",
3380 ignorespath);
3381 } else {
3382 ignoresfile = fdopen(fd, "r");
3383 if (ignoresfile == NULL)
3384 err = got_error_from_errno2("fdopen",
3385 ignorespath);
3386 else {
3387 fd = -1;
3388 err = read_ignores(ignores, path, ignoresfile);
3391 } else {
3392 ignoresfile = fopen(ignorespath, "r");
3393 if (ignoresfile == NULL) {
3394 if (errno != ENOENT && errno != EACCES)
3395 err = got_error_from_errno2("fopen",
3396 ignorespath);
3397 } else
3398 err = read_ignores(ignores, path, ignoresfile);
3401 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3402 err = got_error_from_errno2("fclose", path);
3403 if (fd != -1 && close(fd) == -1 && err == NULL)
3404 err = got_error_from_errno2("close", path);
3405 free(ignorespath);
3406 return err;
3409 static const struct got_error *
3410 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3412 const struct got_error *err = NULL;
3413 struct diff_dir_cb_arg *a = arg;
3414 char *path = NULL;
3416 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3417 return got_error(GOT_ERR_CANCELLED);
3419 if (parent_path[0]) {
3420 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3421 return got_error_from_errno("asprintf");
3422 } else {
3423 path = de->d_name;
3426 if (de->d_type != DT_DIR &&
3427 got_path_is_child(path, a->status_path, a->status_path_len)
3428 && !match_ignores(&a->ignores, path))
3429 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3430 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3431 if (parent_path[0])
3432 free(path);
3433 return err;
3436 static const struct got_error *
3437 status_traverse(void *arg, const char *path, int dirfd)
3439 const struct got_error *err = NULL;
3440 struct diff_dir_cb_arg *a = arg;
3442 if (a->no_ignores)
3443 return NULL;
3445 err = add_ignores(&a->ignores, a->worktree->root_path,
3446 path, dirfd, ".cvsignore");
3447 if (err)
3448 return err;
3450 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3451 dirfd, ".gitignore");
3453 return err;
3456 static const struct got_error *
3457 report_single_file_status(const char *path, const char *ondisk_path,
3458 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3459 void *status_arg, struct got_repository *repo, int report_unchanged)
3461 struct got_fileindex_entry *ie;
3462 struct stat sb;
3464 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3465 if (ie)
3466 return report_file_status(ie, ondisk_path, -1, NULL,
3467 status_cb, status_arg, repo, report_unchanged);
3469 if (lstat(ondisk_path, &sb) == -1) {
3470 if (errno != ENOENT)
3471 return got_error_from_errno2("lstat", ondisk_path);
3472 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3473 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3474 return NULL;
3477 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3478 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3479 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3481 return NULL;
3484 static const struct got_error *
3485 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3486 const char *root_path, const char *path)
3488 const struct got_error *err;
3489 char *parent_path, *next_parent_path = NULL;
3491 err = add_ignores(ignores, root_path, "", -1,
3492 ".cvsignore");
3493 if (err)
3494 return err;
3496 err = add_ignores(ignores, root_path, "", -1,
3497 ".gitignore");
3498 if (err)
3499 return err;
3501 err = got_path_dirname(&parent_path, path);
3502 if (err) {
3503 if (err->code == GOT_ERR_BAD_PATH)
3504 return NULL; /* cannot traverse parent */
3505 return err;
3507 for (;;) {
3508 err = add_ignores(ignores, root_path, parent_path, -1,
3509 ".cvsignore");
3510 if (err)
3511 break;
3512 err = add_ignores(ignores, root_path, parent_path, -1,
3513 ".gitignore");
3514 if (err)
3515 break;
3516 err = got_path_dirname(&next_parent_path, parent_path);
3517 if (err) {
3518 if (err->code == GOT_ERR_BAD_PATH)
3519 err = NULL; /* traversed everything */
3520 break;
3522 free(parent_path);
3523 parent_path = next_parent_path;
3524 next_parent_path = NULL;
3527 free(parent_path);
3528 free(next_parent_path);
3529 return err;
3532 static const struct got_error *
3533 worktree_status(struct got_worktree *worktree, const char *path,
3534 struct got_fileindex *fileindex, struct got_repository *repo,
3535 got_worktree_status_cb status_cb, void *status_arg,
3536 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3537 int report_unchanged)
3539 const struct got_error *err = NULL;
3540 int fd = -1;
3541 struct got_fileindex_diff_dir_cb fdiff_cb;
3542 struct diff_dir_cb_arg arg;
3543 char *ondisk_path = NULL;
3545 TAILQ_INIT(&arg.ignores);
3547 if (asprintf(&ondisk_path, "%s%s%s",
3548 worktree->root_path, path[0] ? "/" : "", path) == -1)
3549 return got_error_from_errno("asprintf");
3551 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3552 if (fd == -1) {
3553 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3554 errno != ELOOP)
3555 err = got_error_from_errno2("open", ondisk_path);
3556 else
3557 err = report_single_file_status(path, ondisk_path,
3558 fileindex, status_cb, status_arg, repo,
3559 report_unchanged);
3560 } else {
3561 fdiff_cb.diff_old_new = status_old_new;
3562 fdiff_cb.diff_old = status_old;
3563 fdiff_cb.diff_new = status_new;
3564 fdiff_cb.diff_traverse = status_traverse;
3565 arg.fileindex = fileindex;
3566 arg.worktree = worktree;
3567 arg.status_path = path;
3568 arg.status_path_len = strlen(path);
3569 arg.repo = repo;
3570 arg.status_cb = status_cb;
3571 arg.status_arg = status_arg;
3572 arg.cancel_cb = cancel_cb;
3573 arg.cancel_arg = cancel_arg;
3574 arg.report_unchanged = report_unchanged;
3575 arg.no_ignores = no_ignores;
3576 if (!no_ignores) {
3577 err = add_ignores_from_parent_paths(&arg.ignores,
3578 worktree->root_path, path);
3579 if (err)
3580 goto done;
3582 err = got_fileindex_diff_dir(fileindex, fd,
3583 worktree->root_path, path, repo, &fdiff_cb, &arg);
3585 done:
3586 free_ignores(&arg.ignores);
3587 if (fd != -1 && close(fd) != 0 && err == NULL)
3588 err = got_error_from_errno("close");
3589 free(ondisk_path);
3590 return err;
3593 const struct got_error *
3594 got_worktree_status(struct got_worktree *worktree,
3595 struct got_pathlist_head *paths, struct got_repository *repo,
3596 got_worktree_status_cb status_cb, void *status_arg,
3597 got_cancel_cb cancel_cb, void *cancel_arg)
3599 const struct got_error *err = NULL;
3600 char *fileindex_path = NULL;
3601 struct got_fileindex *fileindex = NULL;
3602 struct got_pathlist_entry *pe;
3604 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3605 if (err)
3606 return err;
3608 TAILQ_FOREACH(pe, paths, entry) {
3609 err = worktree_status(worktree, pe->path, fileindex, repo,
3610 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3611 if (err)
3612 break;
3614 free(fileindex_path);
3615 got_fileindex_free(fileindex);
3616 return err;
3619 const struct got_error *
3620 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3621 const char *arg)
3623 const struct got_error *err = NULL;
3624 char *resolved = NULL, *cwd = NULL, *path = NULL;
3625 size_t len;
3626 struct stat sb;
3628 *wt_path = NULL;
3630 cwd = getcwd(NULL, 0);
3631 if (cwd == NULL)
3632 return got_error_from_errno("getcwd");
3634 if (lstat(arg, &sb) == -1) {
3635 if (errno != ENOENT) {
3636 err = got_error_from_errno2("lstat", arg);
3637 goto done;
3640 if (S_ISLNK(sb.st_mode)) {
3642 * We cannot use realpath(3) with symlinks since we want to
3643 * operate on the symlink itself.
3644 * But we can make the path absolute, assuming it is relative
3645 * to the current working directory, and then canonicalize it.
3647 char *abspath = NULL;
3648 char canonpath[PATH_MAX];
3649 if (!got_path_is_absolute(arg)) {
3650 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3651 err = got_error_from_errno("asprintf");
3652 goto done;
3656 err = got_canonpath(abspath ? abspath : arg, canonpath,
3657 sizeof(canonpath));
3658 if (err)
3659 goto done;
3660 resolved = strdup(canonpath);
3661 if (resolved == NULL) {
3662 err = got_error_from_errno("strdup");
3663 goto done;
3665 } else {
3666 resolved = realpath(arg, NULL);
3667 if (resolved == NULL) {
3668 if (errno != ENOENT) {
3669 err = got_error_from_errno2("realpath", arg);
3670 goto done;
3672 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3673 err = got_error_from_errno("asprintf");
3674 goto done;
3679 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3680 strlen(got_worktree_get_root_path(worktree)))) {
3681 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3682 goto done;
3685 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3686 err = got_path_skip_common_ancestor(&path,
3687 got_worktree_get_root_path(worktree), resolved);
3688 if (err)
3689 goto done;
3690 } else {
3691 path = strdup("");
3692 if (path == NULL) {
3693 err = got_error_from_errno("strdup");
3694 goto done;
3698 /* XXX status walk can't deal with trailing slash! */
3699 len = strlen(path);
3700 while (len > 0 && path[len - 1] == '/') {
3701 path[len - 1] = '\0';
3702 len--;
3704 done:
3705 free(resolved);
3706 free(cwd);
3707 if (err == NULL)
3708 *wt_path = path;
3709 else
3710 free(path);
3711 return err;
3714 struct schedule_addition_args {
3715 struct got_worktree *worktree;
3716 struct got_fileindex *fileindex;
3717 got_worktree_checkout_cb progress_cb;
3718 void *progress_arg;
3719 struct got_repository *repo;
3722 static const struct got_error *
3723 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3724 const char *relpath, struct got_object_id *blob_id,
3725 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3726 int dirfd, const char *de_name)
3728 struct schedule_addition_args *a = arg;
3729 const struct got_error *err = NULL;
3730 struct got_fileindex_entry *ie;
3731 struct stat sb;
3732 char *ondisk_path;
3734 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3735 relpath) == -1)
3736 return got_error_from_errno("asprintf");
3738 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3739 if (ie) {
3740 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3741 de_name, a->repo);
3742 if (err)
3743 goto done;
3744 /* Re-adding an existing entry is a no-op. */
3745 if (status == GOT_STATUS_ADD)
3746 goto done;
3747 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3748 if (err)
3749 goto done;
3752 if (status != GOT_STATUS_UNVERSIONED) {
3753 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3754 goto done;
3757 err = got_fileindex_entry_alloc(&ie, relpath);
3758 if (err)
3759 goto done;
3760 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3761 if (err) {
3762 got_fileindex_entry_free(ie);
3763 goto done;
3765 err = got_fileindex_entry_add(a->fileindex, ie);
3766 if (err) {
3767 got_fileindex_entry_free(ie);
3768 goto done;
3770 done:
3771 free(ondisk_path);
3772 if (err)
3773 return err;
3774 if (status == GOT_STATUS_ADD)
3775 return NULL;
3776 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3779 const struct got_error *
3780 got_worktree_schedule_add(struct got_worktree *worktree,
3781 struct got_pathlist_head *paths,
3782 got_worktree_checkout_cb progress_cb, void *progress_arg,
3783 struct got_repository *repo, int no_ignores)
3785 struct got_fileindex *fileindex = NULL;
3786 char *fileindex_path = NULL;
3787 const struct got_error *err = NULL, *sync_err, *unlockerr;
3788 struct got_pathlist_entry *pe;
3789 struct schedule_addition_args saa;
3791 err = lock_worktree(worktree, LOCK_EX);
3792 if (err)
3793 return err;
3795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3796 if (err)
3797 goto done;
3799 saa.worktree = worktree;
3800 saa.fileindex = fileindex;
3801 saa.progress_cb = progress_cb;
3802 saa.progress_arg = progress_arg;
3803 saa.repo = repo;
3805 TAILQ_FOREACH(pe, paths, entry) {
3806 err = worktree_status(worktree, pe->path, fileindex, repo,
3807 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3808 if (err)
3809 break;
3811 sync_err = sync_fileindex(fileindex, fileindex_path);
3812 if (sync_err && err == NULL)
3813 err = sync_err;
3814 done:
3815 free(fileindex_path);
3816 if (fileindex)
3817 got_fileindex_free(fileindex);
3818 unlockerr = lock_worktree(worktree, LOCK_SH);
3819 if (unlockerr && err == NULL)
3820 err = unlockerr;
3821 return err;
3824 struct schedule_deletion_args {
3825 struct got_worktree *worktree;
3826 struct got_fileindex *fileindex;
3827 got_worktree_delete_cb progress_cb;
3828 void *progress_arg;
3829 struct got_repository *repo;
3830 int delete_local_mods;
3831 int keep_on_disk;
3832 const char *status_codes;
3835 static const struct got_error *
3836 schedule_for_deletion(void *arg, unsigned char status,
3837 unsigned char staged_status, const char *relpath,
3838 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3839 struct got_object_id *commit_id, int dirfd, const char *de_name)
3841 struct schedule_deletion_args *a = arg;
3842 const struct got_error *err = NULL;
3843 struct got_fileindex_entry *ie = NULL;
3844 struct stat sb;
3845 char *ondisk_path;
3847 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3848 if (ie == NULL)
3849 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3851 staged_status = get_staged_status(ie);
3852 if (staged_status != GOT_STATUS_NO_CHANGE) {
3853 if (staged_status == GOT_STATUS_DELETE)
3854 return NULL;
3855 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3858 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3859 relpath) == -1)
3860 return got_error_from_errno("asprintf");
3862 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3863 a->repo);
3864 if (err)
3865 goto done;
3867 if (a->status_codes) {
3868 size_t ncodes = strlen(a->status_codes);
3869 int i;
3870 for (i = 0; i < ncodes ; i++) {
3871 if (status == a->status_codes[i])
3872 break;
3874 if (i == ncodes) {
3875 /* Do not delete files in non-matching status. */
3876 free(ondisk_path);
3877 return NULL;
3879 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3880 a->status_codes[i] != GOT_STATUS_MISSING) {
3881 static char msg[64];
3882 snprintf(msg, sizeof(msg),
3883 "invalid status code '%c'", a->status_codes[i]);
3884 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3885 goto done;
3889 if (status != GOT_STATUS_NO_CHANGE) {
3890 if (status == GOT_STATUS_DELETE)
3891 goto done;
3892 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3893 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3894 goto done;
3896 if (status != GOT_STATUS_MODIFY &&
3897 status != GOT_STATUS_MISSING) {
3898 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3899 goto done;
3903 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3904 size_t root_len;
3906 if (dirfd != -1) {
3907 if (unlinkat(dirfd, de_name, 0) != 0) {
3908 err = got_error_from_errno2("unlinkat",
3909 ondisk_path);
3910 goto done;
3912 } else if (unlink(ondisk_path) != 0) {
3913 err = got_error_from_errno2("unlink", ondisk_path);
3914 goto done;
3917 root_len = strlen(a->worktree->root_path);
3918 do {
3919 char *parent;
3920 err = got_path_dirname(&parent, ondisk_path);
3921 if (err)
3922 goto done;
3923 free(ondisk_path);
3924 ondisk_path = parent;
3925 if (rmdir(ondisk_path) == -1) {
3926 if (errno != ENOTEMPTY)
3927 err = got_error_from_errno2("rmdir",
3928 ondisk_path);
3929 break;
3931 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3932 strlen(ondisk_path), root_len) != 0);
3935 got_fileindex_entry_mark_deleted_from_disk(ie);
3936 done:
3937 free(ondisk_path);
3938 if (err)
3939 return err;
3940 if (status == GOT_STATUS_DELETE)
3941 return NULL;
3942 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3943 staged_status, relpath);
3946 const struct got_error *
3947 got_worktree_schedule_delete(struct got_worktree *worktree,
3948 struct got_pathlist_head *paths, int delete_local_mods,
3949 const char *status_codes,
3950 got_worktree_delete_cb progress_cb, void *progress_arg,
3951 struct got_repository *repo, int keep_on_disk)
3953 struct got_fileindex *fileindex = NULL;
3954 char *fileindex_path = NULL;
3955 const struct got_error *err = NULL, *sync_err, *unlockerr;
3956 struct got_pathlist_entry *pe;
3957 struct schedule_deletion_args sda;
3959 err = lock_worktree(worktree, LOCK_EX);
3960 if (err)
3961 return err;
3963 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3964 if (err)
3965 goto done;
3967 sda.worktree = worktree;
3968 sda.fileindex = fileindex;
3969 sda.progress_cb = progress_cb;
3970 sda.progress_arg = progress_arg;
3971 sda.repo = repo;
3972 sda.delete_local_mods = delete_local_mods;
3973 sda.keep_on_disk = keep_on_disk;
3974 sda.status_codes = status_codes;
3976 TAILQ_FOREACH(pe, paths, entry) {
3977 err = worktree_status(worktree, pe->path, fileindex, repo,
3978 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3979 if (err)
3980 break;
3982 sync_err = sync_fileindex(fileindex, fileindex_path);
3983 if (sync_err && err == NULL)
3984 err = sync_err;
3985 done:
3986 free(fileindex_path);
3987 if (fileindex)
3988 got_fileindex_free(fileindex);
3989 unlockerr = lock_worktree(worktree, LOCK_SH);
3990 if (unlockerr && err == NULL)
3991 err = unlockerr;
3992 return err;
3995 static const struct got_error *
3996 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3998 const struct got_error *err = NULL;
3999 char *line = NULL;
4000 size_t linesize = 0, n;
4001 ssize_t linelen;
4003 linelen = getline(&line, &linesize, infile);
4004 if (linelen == -1) {
4005 if (ferror(infile)) {
4006 err = got_error_from_errno("getline");
4007 goto done;
4009 return NULL;
4011 if (outfile) {
4012 n = fwrite(line, 1, linelen, outfile);
4013 if (n != linelen) {
4014 err = got_ferror(outfile, GOT_ERR_IO);
4015 goto done;
4018 if (rejectfile) {
4019 n = fwrite(line, 1, linelen, rejectfile);
4020 if (n != linelen)
4021 err = got_ferror(outfile, GOT_ERR_IO);
4023 done:
4024 free(line);
4025 return err;
4028 static const struct got_error *
4029 skip_one_line(FILE *f)
4031 char *line = NULL;
4032 size_t linesize = 0;
4033 ssize_t linelen;
4035 linelen = getline(&line, &linesize, f);
4036 if (linelen == -1) {
4037 if (ferror(f))
4038 return got_error_from_errno("getline");
4039 return NULL;
4041 free(line);
4042 return NULL;
4045 static const struct got_error *
4046 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4047 int start_old, int end_old, int start_new, int end_new,
4048 FILE *outfile, FILE *rejectfile)
4050 const struct got_error *err;
4052 /* Copy old file's lines leading up to patch. */
4053 while (!feof(f1) && *line_cur1 < start_old) {
4054 err = copy_one_line(f1, outfile, NULL);
4055 if (err)
4056 return err;
4057 (*line_cur1)++;
4059 /* Skip new file's lines leading up to patch. */
4060 while (!feof(f2) && *line_cur2 < start_new) {
4061 if (rejectfile)
4062 err = copy_one_line(f2, NULL, rejectfile);
4063 else
4064 err = skip_one_line(f2);
4065 if (err)
4066 return err;
4067 (*line_cur2)++;
4069 /* Copy patched lines. */
4070 while (!feof(f2) && *line_cur2 <= end_new) {
4071 err = copy_one_line(f2, outfile, NULL);
4072 if (err)
4073 return err;
4074 (*line_cur2)++;
4076 /* Skip over old file's replaced lines. */
4077 while (!feof(f1) && *line_cur1 <= end_old) {
4078 if (rejectfile)
4079 err = copy_one_line(f1, NULL, rejectfile);
4080 else
4081 err = skip_one_line(f1);
4082 if (err)
4083 return err;
4084 (*line_cur1)++;
4087 return NULL;
4090 static const struct got_error *
4091 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4092 FILE *outfile, FILE *rejectfile)
4094 const struct got_error *err;
4096 if (outfile) {
4097 /* Copy old file's lines until EOF. */
4098 while (!feof(f1)) {
4099 err = copy_one_line(f1, outfile, NULL);
4100 if (err)
4101 return err;
4102 (*line_cur1)++;
4105 if (rejectfile) {
4106 /* Copy new file's lines until EOF. */
4107 while (!feof(f2)) {
4108 err = copy_one_line(f2, NULL, rejectfile);
4109 if (err)
4110 return err;
4111 (*line_cur2)++;
4115 return NULL;
4118 static const struct got_error *
4119 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4120 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4121 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4122 int *line_cur2, FILE *outfile, FILE *rejectfile,
4123 got_worktree_patch_cb patch_cb, void *patch_arg)
4125 const struct got_error *err = NULL;
4126 int start_old = change->cv.a;
4127 int end_old = change->cv.b;
4128 int start_new = change->cv.c;
4129 int end_new = change->cv.d;
4130 long pos1, pos2;
4131 FILE *hunkfile;
4133 *choice = GOT_PATCH_CHOICE_NONE;
4135 hunkfile = got_opentemp();
4136 if (hunkfile == NULL)
4137 return got_error_from_errno("got_opentemp");
4139 pos1 = ftell(f1);
4140 pos2 = ftell(f2);
4142 /* XXX TODO needs error checking */
4143 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4145 if (fseek(f1, pos1, SEEK_SET) == -1) {
4146 err = got_ferror(f1, GOT_ERR_IO);
4147 goto done;
4149 if (fseek(f2, pos2, SEEK_SET) == -1) {
4150 err = got_ferror(f1, GOT_ERR_IO);
4151 goto done;
4153 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4154 err = got_ferror(hunkfile, GOT_ERR_IO);
4155 goto done;
4158 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4159 hunkfile, n, nchanges);
4160 if (err)
4161 goto done;
4163 switch (*choice) {
4164 case GOT_PATCH_CHOICE_YES:
4165 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4166 end_old, start_new, end_new, outfile, rejectfile);
4167 break;
4168 case GOT_PATCH_CHOICE_NO:
4169 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4170 end_old, start_new, end_new, rejectfile, outfile);
4171 break;
4172 case GOT_PATCH_CHOICE_QUIT:
4173 break;
4174 default:
4175 err = got_error(GOT_ERR_PATCH_CHOICE);
4176 break;
4178 done:
4179 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4180 err = got_error_from_errno("fclose");
4181 return err;
4184 struct revert_file_args {
4185 struct got_worktree *worktree;
4186 struct got_fileindex *fileindex;
4187 got_worktree_checkout_cb progress_cb;
4188 void *progress_arg;
4189 got_worktree_patch_cb patch_cb;
4190 void *patch_arg;
4191 struct got_repository *repo;
4194 static const struct got_error *
4195 create_patched_content(char **path_outfile, int reverse_patch,
4196 struct got_object_id *blob_id, const char *path2,
4197 int dirfd2, const char *de_name2,
4198 const char *relpath, struct got_repository *repo,
4199 got_worktree_patch_cb patch_cb, void *patch_arg)
4201 const struct got_error *err;
4202 struct got_blob_object *blob = NULL;
4203 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4204 int fd2 = -1;
4205 char link_target[PATH_MAX];
4206 ssize_t link_len = 0;
4207 char *path1 = NULL, *id_str = NULL;
4208 struct stat sb1, sb2;
4209 struct got_diff_changes *changes = NULL;
4210 struct got_diff_state *ds = NULL;
4211 struct got_diff_args *args = NULL;
4212 struct got_diff_change *change;
4213 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4214 int n = 0;
4216 *path_outfile = NULL;
4218 err = got_object_id_str(&id_str, blob_id);
4219 if (err)
4220 return err;
4222 if (dirfd2 != -1) {
4223 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4224 if (fd2 == -1) {
4225 if (errno != ELOOP) {
4226 err = got_error_from_errno2("openat", path2);
4227 goto done;
4229 link_len = readlinkat(dirfd2, de_name2,
4230 link_target, sizeof(link_target));
4231 if (link_len == -1)
4232 return got_error_from_errno2("readlinkat", path2);
4233 sb2.st_mode = S_IFLNK;
4234 sb2.st_size = link_len;
4236 } else {
4237 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4238 if (fd2 == -1) {
4239 if (errno != ELOOP) {
4240 err = got_error_from_errno2("open", path2);
4241 goto done;
4243 link_len = readlink(path2, link_target,
4244 sizeof(link_target));
4245 if (link_len == -1)
4246 return got_error_from_errno2("readlink", path2);
4247 sb2.st_mode = S_IFLNK;
4248 sb2.st_size = link_len;
4251 if (fd2 != -1) {
4252 if (fstat(fd2, &sb2) == -1) {
4253 err = got_error_from_errno2("fstat", path2);
4254 goto done;
4257 f2 = fdopen(fd2, "r");
4258 if (f2 == NULL) {
4259 err = got_error_from_errno2("fdopen", path2);
4260 goto done;
4262 fd2 = -1;
4263 } else {
4264 size_t n;
4265 f2 = got_opentemp();
4266 if (f2 == NULL) {
4267 err = got_error_from_errno2("got_opentemp", path2);
4268 goto done;
4270 n = fwrite(link_target, 1, link_len, f2);
4271 if (n != link_len) {
4272 err = got_ferror(f2, GOT_ERR_IO);
4273 goto done;
4275 if (fflush(f2) == EOF) {
4276 err = got_error_from_errno("fflush");
4277 goto done;
4279 rewind(f2);
4282 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4283 if (err)
4284 goto done;
4286 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4287 if (err)
4288 goto done;
4290 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4291 if (err)
4292 goto done;
4294 if (stat(path1, &sb1) == -1) {
4295 err = got_error_from_errno2("stat", path1);
4296 goto done;
4299 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4300 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4301 if (err)
4302 goto done;
4304 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4305 if (err)
4306 goto done;
4308 if (fseek(f1, 0L, SEEK_SET) == -1)
4309 return got_ferror(f1, GOT_ERR_IO);
4310 if (fseek(f2, 0L, SEEK_SET) == -1)
4311 return got_ferror(f2, GOT_ERR_IO);
4312 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4313 int choice;
4314 err = apply_or_reject_change(&choice, change, ++n,
4315 changes->nchanges, ds, args, diff_flags, relpath,
4316 f1, f2, &line_cur1, &line_cur2,
4317 reverse_patch ? NULL : outfile,
4318 reverse_patch ? outfile : NULL,
4319 patch_cb, patch_arg);
4320 if (err)
4321 goto done;
4322 if (choice == GOT_PATCH_CHOICE_YES)
4323 have_content = 1;
4324 else if (choice == GOT_PATCH_CHOICE_QUIT)
4325 break;
4327 if (have_content) {
4328 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4329 reverse_patch ? NULL : outfile,
4330 reverse_patch ? outfile : NULL);
4331 if (err)
4332 goto done;
4334 if (!S_ISLNK(sb2.st_mode)) {
4335 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4336 err = got_error_from_errno2("chmod", path2);
4337 goto done;
4341 done:
4342 free(id_str);
4343 if (blob)
4344 got_object_blob_close(blob);
4345 if (f1 && fclose(f1) == EOF && err == NULL)
4346 err = got_error_from_errno2("fclose", path1);
4347 if (f2 && fclose(f2) == EOF && err == NULL)
4348 err = got_error_from_errno2("fclose", path2);
4349 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4350 err = got_error_from_errno2("close", path2);
4351 if (outfile && fclose(outfile) == EOF && err == NULL)
4352 err = got_error_from_errno2("fclose", *path_outfile);
4353 if (path1 && unlink(path1) == -1 && err == NULL)
4354 err = got_error_from_errno2("unlink", path1);
4355 if (err || !have_content) {
4356 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4357 err = got_error_from_errno2("unlink", *path_outfile);
4358 free(*path_outfile);
4359 *path_outfile = NULL;
4361 free(args);
4362 if (ds) {
4363 got_diff_state_free(ds);
4364 free(ds);
4366 if (changes)
4367 got_diff_free_changes(changes);
4368 free(path1);
4369 return err;
4372 static const struct got_error *
4373 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4374 const char *relpath, struct got_object_id *blob_id,
4375 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4376 int dirfd, const char *de_name)
4378 struct revert_file_args *a = arg;
4379 const struct got_error *err = NULL;
4380 char *parent_path = NULL;
4381 struct got_fileindex_entry *ie;
4382 struct got_tree_object *tree = NULL;
4383 struct got_object_id *tree_id = NULL;
4384 const struct got_tree_entry *te = NULL;
4385 char *tree_path = NULL, *te_name;
4386 char *ondisk_path = NULL, *path_content = NULL;
4387 struct got_blob_object *blob = NULL;
4389 /* Reverting a staged deletion is a no-op. */
4390 if (status == GOT_STATUS_DELETE &&
4391 staged_status != GOT_STATUS_NO_CHANGE)
4392 return NULL;
4394 if (status == GOT_STATUS_UNVERSIONED)
4395 return (*a->progress_cb)(a->progress_arg,
4396 GOT_STATUS_UNVERSIONED, relpath);
4398 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4399 if (ie == NULL)
4400 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4402 /* Construct in-repository path of tree which contains this blob. */
4403 err = got_path_dirname(&parent_path, ie->path);
4404 if (err) {
4405 if (err->code != GOT_ERR_BAD_PATH)
4406 goto done;
4407 parent_path = strdup("/");
4408 if (parent_path == NULL) {
4409 err = got_error_from_errno("strdup");
4410 goto done;
4413 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4414 tree_path = strdup(parent_path);
4415 if (tree_path == NULL) {
4416 err = got_error_from_errno("strdup");
4417 goto done;
4419 } else {
4420 if (got_path_is_root_dir(parent_path)) {
4421 tree_path = strdup(a->worktree->path_prefix);
4422 if (tree_path == NULL) {
4423 err = got_error_from_errno("strdup");
4424 goto done;
4426 } else {
4427 if (asprintf(&tree_path, "%s/%s",
4428 a->worktree->path_prefix, parent_path) == -1) {
4429 err = got_error_from_errno("asprintf");
4430 goto done;
4435 err = got_object_id_by_path(&tree_id, a->repo,
4436 a->worktree->base_commit_id, tree_path);
4437 if (err) {
4438 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4439 (status == GOT_STATUS_ADD ||
4440 staged_status == GOT_STATUS_ADD)))
4441 goto done;
4442 } else {
4443 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4444 if (err)
4445 goto done;
4447 err = got_path_basename(&te_name, ie->path);
4448 if (err)
4449 goto done;
4451 te = got_object_tree_find_entry(tree, te_name);
4452 free(te_name);
4453 if (te == NULL && status != GOT_STATUS_ADD &&
4454 staged_status != GOT_STATUS_ADD) {
4455 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4456 goto done;
4460 switch (status) {
4461 case GOT_STATUS_ADD:
4462 if (a->patch_cb) {
4463 int choice = GOT_PATCH_CHOICE_NONE;
4464 err = (*a->patch_cb)(&choice, a->patch_arg,
4465 status, ie->path, NULL, 1, 1);
4466 if (err)
4467 goto done;
4468 if (choice != GOT_PATCH_CHOICE_YES)
4469 break;
4471 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4472 ie->path);
4473 if (err)
4474 goto done;
4475 got_fileindex_entry_remove(a->fileindex, ie);
4476 break;
4477 case GOT_STATUS_DELETE:
4478 if (a->patch_cb) {
4479 int choice = GOT_PATCH_CHOICE_NONE;
4480 err = (*a->patch_cb)(&choice, a->patch_arg,
4481 status, ie->path, NULL, 1, 1);
4482 if (err)
4483 goto done;
4484 if (choice != GOT_PATCH_CHOICE_YES)
4485 break;
4487 /* fall through */
4488 case GOT_STATUS_MODIFY:
4489 case GOT_STATUS_MODE_CHANGE:
4490 case GOT_STATUS_CONFLICT:
4491 case GOT_STATUS_MISSING: {
4492 struct got_object_id id;
4493 if (staged_status == GOT_STATUS_ADD ||
4494 staged_status == GOT_STATUS_MODIFY) {
4495 memcpy(id.sha1, ie->staged_blob_sha1,
4496 SHA1_DIGEST_LENGTH);
4497 } else
4498 memcpy(id.sha1, ie->blob_sha1,
4499 SHA1_DIGEST_LENGTH);
4500 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4501 if (err)
4502 goto done;
4504 if (asprintf(&ondisk_path, "%s/%s",
4505 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4506 err = got_error_from_errno("asprintf");
4507 goto done;
4510 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4511 status == GOT_STATUS_CONFLICT)) {
4512 int is_bad_symlink = 0;
4513 err = create_patched_content(&path_content, 1, &id,
4514 ondisk_path, dirfd, de_name, ie->path, a->repo,
4515 a->patch_cb, a->patch_arg);
4516 if (err || path_content == NULL)
4517 break;
4518 if (te && S_ISLNK(te->mode)) {
4519 if (unlink(path_content) == -1) {
4520 err = got_error_from_errno2("unlink",
4521 path_content);
4522 break;
4524 err = install_symlink(&is_bad_symlink,
4525 a->worktree, ondisk_path, ie->path,
4526 blob, 0, 1, 0, a->repo,
4527 a->progress_cb, a->progress_arg);
4528 } else {
4529 if (rename(path_content, ondisk_path) == -1) {
4530 err = got_error_from_errno3("rename",
4531 path_content, ondisk_path);
4532 goto done;
4535 } else {
4536 int is_bad_symlink = 0;
4537 if (te && S_ISLNK(te->mode)) {
4538 err = install_symlink(&is_bad_symlink,
4539 a->worktree, ondisk_path, ie->path,
4540 blob, 0, 1, 0, a->repo,
4541 a->progress_cb, a->progress_arg);
4542 } else {
4543 err = install_blob(a->worktree, ondisk_path,
4544 ie->path,
4545 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4546 got_fileindex_perms_to_st(ie), blob,
4547 0, 1, 0, 0, a->repo,
4548 a->progress_cb, a->progress_arg);
4550 if (err)
4551 goto done;
4552 if (status == GOT_STATUS_DELETE ||
4553 status == GOT_STATUS_MODE_CHANGE) {
4554 err = got_fileindex_entry_update(ie,
4555 ondisk_path, blob->id.sha1,
4556 a->worktree->base_commit_id->sha1, 1);
4557 if (err)
4558 goto done;
4560 if (is_bad_symlink) {
4561 got_fileindex_entry_filetype_set(ie,
4562 GOT_FILEIDX_MODE_BAD_SYMLINK);
4565 break;
4567 default:
4568 break;
4570 done:
4571 free(ondisk_path);
4572 free(path_content);
4573 free(parent_path);
4574 free(tree_path);
4575 if (blob)
4576 got_object_blob_close(blob);
4577 if (tree)
4578 got_object_tree_close(tree);
4579 free(tree_id);
4580 return err;
4583 const struct got_error *
4584 got_worktree_revert(struct got_worktree *worktree,
4585 struct got_pathlist_head *paths,
4586 got_worktree_checkout_cb progress_cb, void *progress_arg,
4587 got_worktree_patch_cb patch_cb, void *patch_arg,
4588 struct got_repository *repo)
4590 struct got_fileindex *fileindex = NULL;
4591 char *fileindex_path = NULL;
4592 const struct got_error *err = NULL, *unlockerr = NULL;
4593 const struct got_error *sync_err = NULL;
4594 struct got_pathlist_entry *pe;
4595 struct revert_file_args rfa;
4597 err = lock_worktree(worktree, LOCK_EX);
4598 if (err)
4599 return err;
4601 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4602 if (err)
4603 goto done;
4605 rfa.worktree = worktree;
4606 rfa.fileindex = fileindex;
4607 rfa.progress_cb = progress_cb;
4608 rfa.progress_arg = progress_arg;
4609 rfa.patch_cb = patch_cb;
4610 rfa.patch_arg = patch_arg;
4611 rfa.repo = repo;
4612 TAILQ_FOREACH(pe, paths, entry) {
4613 err = worktree_status(worktree, pe->path, fileindex, repo,
4614 revert_file, &rfa, NULL, NULL, 0, 0);
4615 if (err)
4616 break;
4618 sync_err = sync_fileindex(fileindex, fileindex_path);
4619 if (sync_err && err == NULL)
4620 err = sync_err;
4621 done:
4622 free(fileindex_path);
4623 if (fileindex)
4624 got_fileindex_free(fileindex);
4625 unlockerr = lock_worktree(worktree, LOCK_SH);
4626 if (unlockerr && err == NULL)
4627 err = unlockerr;
4628 return err;
4631 static void
4632 free_commitable(struct got_commitable *ct)
4634 free(ct->path);
4635 free(ct->in_repo_path);
4636 free(ct->ondisk_path);
4637 free(ct->blob_id);
4638 free(ct->base_blob_id);
4639 free(ct->staged_blob_id);
4640 free(ct->base_commit_id);
4641 free(ct);
4644 struct collect_commitables_arg {
4645 struct got_pathlist_head *commitable_paths;
4646 struct got_repository *repo;
4647 struct got_worktree *worktree;
4648 struct got_fileindex *fileindex;
4649 int have_staged_files;
4650 int allow_bad_symlinks;
4653 static const struct got_error *
4654 collect_commitables(void *arg, unsigned char status,
4655 unsigned char staged_status, const char *relpath,
4656 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4657 struct got_object_id *commit_id, int dirfd, const char *de_name)
4659 struct collect_commitables_arg *a = arg;
4660 const struct got_error *err = NULL;
4661 struct got_commitable *ct = NULL;
4662 struct got_pathlist_entry *new = NULL;
4663 char *parent_path = NULL, *path = NULL;
4664 struct stat sb;
4666 if (a->have_staged_files) {
4667 if (staged_status != GOT_STATUS_MODIFY &&
4668 staged_status != GOT_STATUS_ADD &&
4669 staged_status != GOT_STATUS_DELETE)
4670 return NULL;
4671 } else {
4672 if (status == GOT_STATUS_CONFLICT)
4673 return got_error(GOT_ERR_COMMIT_CONFLICT);
4675 if (status != GOT_STATUS_MODIFY &&
4676 status != GOT_STATUS_MODE_CHANGE &&
4677 status != GOT_STATUS_ADD &&
4678 status != GOT_STATUS_DELETE)
4679 return NULL;
4682 if (asprintf(&path, "/%s", relpath) == -1) {
4683 err = got_error_from_errno("asprintf");
4684 goto done;
4686 if (strcmp(path, "/") == 0) {
4687 parent_path = strdup("");
4688 if (parent_path == NULL)
4689 return got_error_from_errno("strdup");
4690 } else {
4691 err = got_path_dirname(&parent_path, path);
4692 if (err)
4693 return err;
4696 ct = calloc(1, sizeof(*ct));
4697 if (ct == NULL) {
4698 err = got_error_from_errno("calloc");
4699 goto done;
4702 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4703 relpath) == -1) {
4704 err = got_error_from_errno("asprintf");
4705 goto done;
4708 if (staged_status == GOT_STATUS_ADD ||
4709 staged_status == GOT_STATUS_MODIFY) {
4710 struct got_fileindex_entry *ie;
4711 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4712 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4713 case GOT_FILEIDX_MODE_REGULAR_FILE:
4714 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4715 ct->mode = S_IFREG;
4716 break;
4717 case GOT_FILEIDX_MODE_SYMLINK:
4718 ct->mode = S_IFLNK;
4719 break;
4720 default:
4721 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4722 goto done;
4724 ct->mode |= got_fileindex_entry_perms_get(ie);
4725 } else if (status != GOT_STATUS_DELETE &&
4726 staged_status != GOT_STATUS_DELETE) {
4727 if (dirfd != -1) {
4728 if (fstatat(dirfd, de_name, &sb,
4729 AT_SYMLINK_NOFOLLOW) == -1) {
4730 err = got_error_from_errno2("fstatat",
4731 ct->ondisk_path);
4732 goto done;
4734 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4735 err = got_error_from_errno2("lstat", ct->ondisk_path);
4736 goto done;
4738 ct->mode = sb.st_mode;
4741 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4742 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4743 relpath) == -1) {
4744 err = got_error_from_errno("asprintf");
4745 goto done;
4748 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4749 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4750 int is_bad_symlink;
4751 char target_path[PATH_MAX];
4752 ssize_t target_len;
4753 target_len = readlink(ct->ondisk_path, target_path,
4754 sizeof(target_path));
4755 if (target_len == -1) {
4756 err = got_error_from_errno2("readlink",
4757 ct->ondisk_path);
4758 goto done;
4760 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4761 target_len, ct->ondisk_path, a->worktree->root_path);
4762 if (err)
4763 goto done;
4764 if (is_bad_symlink) {
4765 err = got_error_path(ct->ondisk_path,
4766 GOT_ERR_BAD_SYMLINK);
4767 goto done;
4772 ct->status = status;
4773 ct->staged_status = staged_status;
4774 ct->blob_id = NULL; /* will be filled in when blob gets created */
4775 if (ct->status != GOT_STATUS_ADD &&
4776 ct->staged_status != GOT_STATUS_ADD) {
4777 ct->base_blob_id = got_object_id_dup(blob_id);
4778 if (ct->base_blob_id == NULL) {
4779 err = got_error_from_errno("got_object_id_dup");
4780 goto done;
4782 ct->base_commit_id = got_object_id_dup(commit_id);
4783 if (ct->base_commit_id == NULL) {
4784 err = got_error_from_errno("got_object_id_dup");
4785 goto done;
4788 if (ct->staged_status == GOT_STATUS_ADD ||
4789 ct->staged_status == GOT_STATUS_MODIFY) {
4790 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4791 if (ct->staged_blob_id == NULL) {
4792 err = got_error_from_errno("got_object_id_dup");
4793 goto done;
4796 ct->path = strdup(path);
4797 if (ct->path == NULL) {
4798 err = got_error_from_errno("strdup");
4799 goto done;
4801 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4802 done:
4803 if (ct && (err || new == NULL))
4804 free_commitable(ct);
4805 free(parent_path);
4806 free(path);
4807 return err;
4810 static const struct got_error *write_tree(struct got_object_id **, int *,
4811 struct got_tree_object *, const char *, struct got_pathlist_head *,
4812 got_worktree_status_cb status_cb, void *status_arg,
4813 struct got_repository *);
4815 static const struct got_error *
4816 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4817 struct got_tree_entry *te, const char *parent_path,
4818 struct got_pathlist_head *commitable_paths,
4819 got_worktree_status_cb status_cb, void *status_arg,
4820 struct got_repository *repo)
4822 const struct got_error *err = NULL;
4823 struct got_tree_object *subtree;
4824 char *subpath;
4826 if (asprintf(&subpath, "%s%s%s", parent_path,
4827 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4828 return got_error_from_errno("asprintf");
4830 err = got_object_open_as_tree(&subtree, repo, &te->id);
4831 if (err)
4832 return err;
4834 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4835 commitable_paths, status_cb, status_arg, repo);
4836 got_object_tree_close(subtree);
4837 free(subpath);
4838 return err;
4841 static const struct got_error *
4842 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4844 const struct got_error *err = NULL;
4845 char *ct_parent_path = NULL;
4847 *match = 0;
4849 if (strchr(ct->in_repo_path, '/') == NULL) {
4850 *match = got_path_is_root_dir(path);
4851 return NULL;
4854 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4855 if (err)
4856 return err;
4857 *match = (strcmp(path, ct_parent_path) == 0);
4858 free(ct_parent_path);
4859 return err;
4862 static mode_t
4863 get_ct_file_mode(struct got_commitable *ct)
4865 if (S_ISLNK(ct->mode))
4866 return S_IFLNK;
4868 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4871 static const struct got_error *
4872 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4873 struct got_tree_entry *te, struct got_commitable *ct)
4875 const struct got_error *err = NULL;
4877 *new_te = NULL;
4879 err = got_object_tree_entry_dup(new_te, te);
4880 if (err)
4881 goto done;
4883 (*new_te)->mode = get_ct_file_mode(ct);
4885 if (ct->staged_status == GOT_STATUS_MODIFY)
4886 memcpy(&(*new_te)->id, ct->staged_blob_id,
4887 sizeof((*new_te)->id));
4888 else
4889 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4890 done:
4891 if (err && *new_te) {
4892 free(*new_te);
4893 *new_te = NULL;
4895 return err;
4898 static const struct got_error *
4899 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4900 struct got_commitable *ct)
4902 const struct got_error *err = NULL;
4903 char *ct_name = NULL;
4905 *new_te = NULL;
4907 *new_te = calloc(1, sizeof(**new_te));
4908 if (*new_te == NULL)
4909 return got_error_from_errno("calloc");
4911 err = got_path_basename(&ct_name, ct->path);
4912 if (err)
4913 goto done;
4914 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4915 sizeof((*new_te)->name)) {
4916 err = got_error(GOT_ERR_NO_SPACE);
4917 goto done;
4920 (*new_te)->mode = get_ct_file_mode(ct);
4922 if (ct->staged_status == GOT_STATUS_ADD)
4923 memcpy(&(*new_te)->id, ct->staged_blob_id,
4924 sizeof((*new_te)->id));
4925 else
4926 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4927 done:
4928 free(ct_name);
4929 if (err && *new_te) {
4930 free(*new_te);
4931 *new_te = NULL;
4933 return err;
4936 static const struct got_error *
4937 insert_tree_entry(struct got_tree_entry *new_te,
4938 struct got_pathlist_head *paths)
4940 const struct got_error *err = NULL;
4941 struct got_pathlist_entry *new_pe;
4943 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4944 if (err)
4945 return err;
4946 if (new_pe == NULL)
4947 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4948 return NULL;
4951 static const struct got_error *
4952 report_ct_status(struct got_commitable *ct,
4953 got_worktree_status_cb status_cb, void *status_arg)
4955 const char *ct_path = ct->path;
4956 unsigned char status;
4958 while (ct_path[0] == '/')
4959 ct_path++;
4961 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4962 status = ct->staged_status;
4963 else
4964 status = ct->status;
4966 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4967 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4970 static const struct got_error *
4971 match_modified_subtree(int *modified, struct got_tree_entry *te,
4972 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4974 const struct got_error *err = NULL;
4975 struct got_pathlist_entry *pe;
4976 char *te_path;
4978 *modified = 0;
4980 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4981 got_path_is_root_dir(base_tree_path) ? "" : "/",
4982 te->name) == -1)
4983 return got_error_from_errno("asprintf");
4985 TAILQ_FOREACH(pe, commitable_paths, entry) {
4986 struct got_commitable *ct = pe->data;
4987 *modified = got_path_is_child(ct->in_repo_path, te_path,
4988 strlen(te_path));
4989 if (*modified)
4990 break;
4993 free(te_path);
4994 return err;
4997 static const struct got_error *
4998 match_deleted_or_modified_ct(struct got_commitable **ctp,
4999 struct got_tree_entry *te, const char *base_tree_path,
5000 struct got_pathlist_head *commitable_paths)
5002 const struct got_error *err = NULL;
5003 struct got_pathlist_entry *pe;
5005 *ctp = NULL;
5007 TAILQ_FOREACH(pe, commitable_paths, entry) {
5008 struct got_commitable *ct = pe->data;
5009 char *ct_name = NULL;
5010 int path_matches;
5012 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5013 if (ct->status != GOT_STATUS_MODIFY &&
5014 ct->status != GOT_STATUS_MODE_CHANGE &&
5015 ct->status != GOT_STATUS_DELETE)
5016 continue;
5017 } else {
5018 if (ct->staged_status != GOT_STATUS_MODIFY &&
5019 ct->staged_status != GOT_STATUS_DELETE)
5020 continue;
5023 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5024 continue;
5026 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5027 if (err)
5028 return err;
5029 if (!path_matches)
5030 continue;
5032 err = got_path_basename(&ct_name, pe->path);
5033 if (err)
5034 return err;
5036 if (strcmp(te->name, ct_name) != 0) {
5037 free(ct_name);
5038 continue;
5040 free(ct_name);
5042 *ctp = ct;
5043 break;
5046 return err;
5049 static const struct got_error *
5050 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5051 const char *child_path, const char *path_base_tree,
5052 struct got_pathlist_head *commitable_paths,
5053 got_worktree_status_cb status_cb, void *status_arg,
5054 struct got_repository *repo)
5056 const struct got_error *err = NULL;
5057 struct got_tree_entry *new_te;
5058 char *subtree_path;
5059 struct got_object_id *id = NULL;
5060 int nentries;
5062 *new_tep = NULL;
5064 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5065 got_path_is_root_dir(path_base_tree) ? "" : "/",
5066 child_path) == -1)
5067 return got_error_from_errno("asprintf");
5069 new_te = calloc(1, sizeof(*new_te));
5070 if (new_te == NULL)
5071 return got_error_from_errno("calloc");
5072 new_te->mode = S_IFDIR;
5074 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5075 sizeof(new_te->name)) {
5076 err = got_error(GOT_ERR_NO_SPACE);
5077 goto done;
5079 err = write_tree(&id, &nentries, NULL, subtree_path,
5080 commitable_paths, status_cb, status_arg, repo);
5081 if (err) {
5082 free(new_te);
5083 goto done;
5085 memcpy(&new_te->id, id, sizeof(new_te->id));
5086 done:
5087 free(id);
5088 free(subtree_path);
5089 if (err == NULL)
5090 *new_tep = new_te;
5091 return err;
5094 static const struct got_error *
5095 write_tree(struct got_object_id **new_tree_id, int *nentries,
5096 struct got_tree_object *base_tree, const char *path_base_tree,
5097 struct got_pathlist_head *commitable_paths,
5098 got_worktree_status_cb status_cb, void *status_arg,
5099 struct got_repository *repo)
5101 const struct got_error *err = NULL;
5102 struct got_pathlist_head paths;
5103 struct got_tree_entry *te, *new_te = NULL;
5104 struct got_pathlist_entry *pe;
5106 TAILQ_INIT(&paths);
5107 *nentries = 0;
5109 /* Insert, and recurse into, newly added entries first. */
5110 TAILQ_FOREACH(pe, commitable_paths, entry) {
5111 struct got_commitable *ct = pe->data;
5112 char *child_path = NULL, *slash;
5114 if ((ct->status != GOT_STATUS_ADD &&
5115 ct->staged_status != GOT_STATUS_ADD) ||
5116 (ct->flags & GOT_COMMITABLE_ADDED))
5117 continue;
5119 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5120 strlen(path_base_tree)))
5121 continue;
5123 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5124 ct->in_repo_path);
5125 if (err)
5126 goto done;
5128 slash = strchr(child_path, '/');
5129 if (slash == NULL) {
5130 err = alloc_added_blob_tree_entry(&new_te, ct);
5131 if (err)
5132 goto done;
5133 err = report_ct_status(ct, status_cb, status_arg);
5134 if (err)
5135 goto done;
5136 ct->flags |= GOT_COMMITABLE_ADDED;
5137 err = insert_tree_entry(new_te, &paths);
5138 if (err)
5139 goto done;
5140 (*nentries)++;
5141 } else {
5142 *slash = '\0'; /* trim trailing path components */
5143 if (base_tree == NULL ||
5144 got_object_tree_find_entry(base_tree, child_path)
5145 == NULL) {
5146 err = make_subtree_for_added_blob(&new_te,
5147 child_path, path_base_tree,
5148 commitable_paths, status_cb, status_arg,
5149 repo);
5150 if (err)
5151 goto done;
5152 err = insert_tree_entry(new_te, &paths);
5153 if (err)
5154 goto done;
5155 (*nentries)++;
5160 if (base_tree) {
5161 int i, nbase_entries;
5162 /* Handle modified and deleted entries. */
5163 nbase_entries = got_object_tree_get_nentries(base_tree);
5164 for (i = 0; i < nbase_entries; i++) {
5165 struct got_commitable *ct = NULL;
5167 te = got_object_tree_get_entry(base_tree, i);
5168 if (got_object_tree_entry_is_submodule(te)) {
5169 /* Entry is a submodule; just copy it. */
5170 err = got_object_tree_entry_dup(&new_te, te);
5171 if (err)
5172 goto done;
5173 err = insert_tree_entry(new_te, &paths);
5174 if (err)
5175 goto done;
5176 (*nentries)++;
5177 continue;
5180 if (S_ISDIR(te->mode)) {
5181 int modified;
5182 err = got_object_tree_entry_dup(&new_te, te);
5183 if (err)
5184 goto done;
5185 err = match_modified_subtree(&modified, te,
5186 path_base_tree, commitable_paths);
5187 if (err)
5188 goto done;
5189 /* Avoid recursion into unmodified subtrees. */
5190 if (modified) {
5191 struct got_object_id *new_id;
5192 int nsubentries;
5193 err = write_subtree(&new_id,
5194 &nsubentries, te,
5195 path_base_tree, commitable_paths,
5196 status_cb, status_arg, repo);
5197 if (err)
5198 goto done;
5199 if (nsubentries == 0) {
5200 /* All entries were deleted. */
5201 free(new_id);
5202 continue;
5204 memcpy(&new_te->id, new_id,
5205 sizeof(new_te->id));
5206 free(new_id);
5208 err = insert_tree_entry(new_te, &paths);
5209 if (err)
5210 goto done;
5211 (*nentries)++;
5212 continue;
5215 err = match_deleted_or_modified_ct(&ct, te,
5216 path_base_tree, commitable_paths);
5217 if (err)
5218 goto done;
5219 if (ct) {
5220 /* NB: Deleted entries get dropped here. */
5221 if (ct->status == GOT_STATUS_MODIFY ||
5222 ct->status == GOT_STATUS_MODE_CHANGE ||
5223 ct->staged_status == GOT_STATUS_MODIFY) {
5224 err = alloc_modified_blob_tree_entry(
5225 &new_te, te, ct);
5226 if (err)
5227 goto done;
5228 err = insert_tree_entry(new_te, &paths);
5229 if (err)
5230 goto done;
5231 (*nentries)++;
5233 err = report_ct_status(ct, status_cb,
5234 status_arg);
5235 if (err)
5236 goto done;
5237 } else {
5238 /* Entry is unchanged; just copy it. */
5239 err = got_object_tree_entry_dup(&new_te, te);
5240 if (err)
5241 goto done;
5242 err = insert_tree_entry(new_te, &paths);
5243 if (err)
5244 goto done;
5245 (*nentries)++;
5250 /* Write new list of entries; deleted entries have been dropped. */
5251 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5252 done:
5253 got_pathlist_free(&paths);
5254 return err;
5257 static const struct got_error *
5258 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5259 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5260 int have_staged_files)
5262 const struct got_error *err = NULL;
5263 struct got_pathlist_entry *pe;
5265 TAILQ_FOREACH(pe, commitable_paths, entry) {
5266 struct got_fileindex_entry *ie;
5267 struct got_commitable *ct = pe->data;
5269 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5270 if (ie) {
5271 if (ct->status == GOT_STATUS_DELETE ||
5272 ct->staged_status == GOT_STATUS_DELETE) {
5273 got_fileindex_entry_remove(fileindex, ie);
5274 } else if (ct->staged_status == GOT_STATUS_ADD ||
5275 ct->staged_status == GOT_STATUS_MODIFY) {
5276 got_fileindex_entry_stage_set(ie,
5277 GOT_FILEIDX_STAGE_NONE);
5278 err = got_fileindex_entry_update(ie,
5279 ct->ondisk_path, ct->staged_blob_id->sha1,
5280 new_base_commit_id->sha1,
5281 !have_staged_files);
5282 } else
5283 err = got_fileindex_entry_update(ie,
5284 ct->ondisk_path, ct->blob_id->sha1,
5285 new_base_commit_id->sha1,
5286 !have_staged_files);
5287 } else {
5288 err = got_fileindex_entry_alloc(&ie, pe->path);
5289 if (err)
5290 break;
5291 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5292 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5293 if (err) {
5294 got_fileindex_entry_free(ie);
5295 break;
5297 err = got_fileindex_entry_add(fileindex, ie);
5298 if (err) {
5299 got_fileindex_entry_free(ie);
5300 break;
5304 return err;
5308 static const struct got_error *
5309 check_out_of_date(const char *in_repo_path, unsigned char status,
5310 unsigned char staged_status, struct got_object_id *base_blob_id,
5311 struct got_object_id *base_commit_id,
5312 struct got_object_id *head_commit_id, struct got_repository *repo,
5313 int ood_errcode)
5315 const struct got_error *err = NULL;
5316 struct got_object_id *id = NULL;
5318 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5319 /* Trivial case: base commit == head commit */
5320 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5321 return NULL;
5323 * Ensure file content which local changes were based
5324 * on matches file content in the branch head.
5326 err = got_object_id_by_path(&id, repo, head_commit_id,
5327 in_repo_path);
5328 if (err) {
5329 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5330 err = got_error(ood_errcode);
5331 goto done;
5332 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5333 err = got_error(ood_errcode);
5334 } else {
5335 /* Require that added files don't exist in the branch head. */
5336 err = got_object_id_by_path(&id, repo, head_commit_id,
5337 in_repo_path);
5338 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5339 goto done;
5340 err = id ? got_error(ood_errcode) : NULL;
5342 done:
5343 free(id);
5344 return err;
5347 const struct got_error *
5348 commit_worktree(struct got_object_id **new_commit_id,
5349 struct got_pathlist_head *commitable_paths,
5350 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5351 const char *author, const char *committer,
5352 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5353 got_worktree_status_cb status_cb, void *status_arg,
5354 struct got_repository *repo)
5356 const struct got_error *err = NULL, *unlockerr = NULL;
5357 struct got_pathlist_entry *pe;
5358 const char *head_ref_name = NULL;
5359 struct got_commit_object *head_commit = NULL;
5360 struct got_reference *head_ref2 = NULL;
5361 struct got_object_id *head_commit_id2 = NULL;
5362 struct got_tree_object *head_tree = NULL;
5363 struct got_object_id *new_tree_id = NULL;
5364 int nentries;
5365 struct got_object_id_queue parent_ids;
5366 struct got_object_qid *pid = NULL;
5367 char *logmsg = NULL;
5369 *new_commit_id = NULL;
5371 SIMPLEQ_INIT(&parent_ids);
5373 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5374 if (err)
5375 goto done;
5377 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5378 if (err)
5379 goto done;
5381 if (commit_msg_cb != NULL) {
5382 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5383 if (err)
5384 goto done;
5387 if (logmsg == NULL || strlen(logmsg) == 0) {
5388 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5389 goto done;
5392 /* Create blobs from added and modified files and record their IDs. */
5393 TAILQ_FOREACH(pe, commitable_paths, entry) {
5394 struct got_commitable *ct = pe->data;
5395 char *ondisk_path;
5397 /* Blobs for staged files already exist. */
5398 if (ct->staged_status == GOT_STATUS_ADD ||
5399 ct->staged_status == GOT_STATUS_MODIFY)
5400 continue;
5402 if (ct->status != GOT_STATUS_ADD &&
5403 ct->status != GOT_STATUS_MODIFY &&
5404 ct->status != GOT_STATUS_MODE_CHANGE)
5405 continue;
5407 if (asprintf(&ondisk_path, "%s/%s",
5408 worktree->root_path, pe->path) == -1) {
5409 err = got_error_from_errno("asprintf");
5410 goto done;
5412 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5413 free(ondisk_path);
5414 if (err)
5415 goto done;
5418 /* Recursively write new tree objects. */
5419 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5420 commitable_paths, status_cb, status_arg, repo);
5421 if (err)
5422 goto done;
5424 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5425 if (err)
5426 goto done;
5427 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5428 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5429 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5430 got_object_qid_free(pid);
5431 if (logmsg != NULL)
5432 free(logmsg);
5433 if (err)
5434 goto done;
5436 /* Check if a concurrent commit to our branch has occurred. */
5437 head_ref_name = got_worktree_get_head_ref_name(worktree);
5438 if (head_ref_name == NULL) {
5439 err = got_error_from_errno("got_worktree_get_head_ref_name");
5440 goto done;
5442 /* Lock the reference here to prevent concurrent modification. */
5443 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5444 if (err)
5445 goto done;
5446 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5447 if (err)
5448 goto done;
5449 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5450 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5451 goto done;
5453 /* Update branch head in repository. */
5454 err = got_ref_change_ref(head_ref2, *new_commit_id);
5455 if (err)
5456 goto done;
5457 err = got_ref_write(head_ref2, repo);
5458 if (err)
5459 goto done;
5461 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5462 if (err)
5463 goto done;
5465 err = ref_base_commit(worktree, repo);
5466 if (err)
5467 goto done;
5468 done:
5469 if (head_tree)
5470 got_object_tree_close(head_tree);
5471 if (head_commit)
5472 got_object_commit_close(head_commit);
5473 free(head_commit_id2);
5474 if (head_ref2) {
5475 unlockerr = got_ref_unlock(head_ref2);
5476 if (unlockerr && err == NULL)
5477 err = unlockerr;
5478 got_ref_close(head_ref2);
5480 return err;
5483 static const struct got_error *
5484 check_path_is_commitable(const char *path,
5485 struct got_pathlist_head *commitable_paths)
5487 struct got_pathlist_entry *cpe = NULL;
5488 size_t path_len = strlen(path);
5490 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5491 struct got_commitable *ct = cpe->data;
5492 const char *ct_path = ct->path;
5494 while (ct_path[0] == '/')
5495 ct_path++;
5497 if (strcmp(path, ct_path) == 0 ||
5498 got_path_is_child(ct_path, path, path_len))
5499 break;
5502 if (cpe == NULL)
5503 return got_error_path(path, GOT_ERR_BAD_PATH);
5505 return NULL;
5508 static const struct got_error *
5509 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5511 int *have_staged_files = arg;
5513 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5514 *have_staged_files = 1;
5515 return got_error(GOT_ERR_CANCELLED);
5518 return NULL;
5521 static const struct got_error *
5522 check_non_staged_files(struct got_fileindex *fileindex,
5523 struct got_pathlist_head *paths)
5525 struct got_pathlist_entry *pe;
5526 struct got_fileindex_entry *ie;
5528 TAILQ_FOREACH(pe, paths, entry) {
5529 if (pe->path[0] == '\0')
5530 continue;
5531 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5532 if (ie == NULL)
5533 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5534 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5535 return got_error_path(pe->path,
5536 GOT_ERR_FILE_NOT_STAGED);
5539 return NULL;
5542 const struct got_error *
5543 got_worktree_commit(struct got_object_id **new_commit_id,
5544 struct got_worktree *worktree, struct got_pathlist_head *paths,
5545 const char *author, const char *committer, int allow_bad_symlinks,
5546 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5547 got_worktree_status_cb status_cb, void *status_arg,
5548 struct got_repository *repo)
5550 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5551 struct got_fileindex *fileindex = NULL;
5552 char *fileindex_path = NULL;
5553 struct got_pathlist_head commitable_paths;
5554 struct collect_commitables_arg cc_arg;
5555 struct got_pathlist_entry *pe;
5556 struct got_reference *head_ref = NULL;
5557 struct got_object_id *head_commit_id = NULL;
5558 int have_staged_files = 0;
5560 *new_commit_id = NULL;
5562 TAILQ_INIT(&commitable_paths);
5564 err = lock_worktree(worktree, LOCK_EX);
5565 if (err)
5566 goto done;
5568 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5569 if (err)
5570 goto done;
5572 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5573 if (err)
5574 goto done;
5576 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5577 if (err)
5578 goto done;
5580 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5581 &have_staged_files);
5582 if (err && err->code != GOT_ERR_CANCELLED)
5583 goto done;
5584 if (have_staged_files) {
5585 err = check_non_staged_files(fileindex, paths);
5586 if (err)
5587 goto done;
5590 cc_arg.commitable_paths = &commitable_paths;
5591 cc_arg.worktree = worktree;
5592 cc_arg.fileindex = fileindex;
5593 cc_arg.repo = repo;
5594 cc_arg.have_staged_files = have_staged_files;
5595 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5596 TAILQ_FOREACH(pe, paths, entry) {
5597 err = worktree_status(worktree, pe->path, fileindex, repo,
5598 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5599 if (err)
5600 goto done;
5603 if (TAILQ_EMPTY(&commitable_paths)) {
5604 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5605 goto done;
5608 TAILQ_FOREACH(pe, paths, entry) {
5609 err = check_path_is_commitable(pe->path, &commitable_paths);
5610 if (err)
5611 goto done;
5614 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5615 struct got_commitable *ct = pe->data;
5616 const char *ct_path = ct->in_repo_path;
5618 while (ct_path[0] == '/')
5619 ct_path++;
5620 err = check_out_of_date(ct_path, ct->status,
5621 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5622 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5623 if (err)
5624 goto done;
5628 err = commit_worktree(new_commit_id, &commitable_paths,
5629 head_commit_id, worktree, author, committer,
5630 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5631 if (err)
5632 goto done;
5634 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5635 fileindex, have_staged_files);
5636 sync_err = sync_fileindex(fileindex, fileindex_path);
5637 if (sync_err && err == NULL)
5638 err = sync_err;
5639 done:
5640 if (fileindex)
5641 got_fileindex_free(fileindex);
5642 free(fileindex_path);
5643 unlockerr = lock_worktree(worktree, LOCK_SH);
5644 if (unlockerr && err == NULL)
5645 err = unlockerr;
5646 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5647 struct got_commitable *ct = pe->data;
5648 free_commitable(ct);
5650 got_pathlist_free(&commitable_paths);
5651 return err;
5654 const char *
5655 got_commitable_get_path(struct got_commitable *ct)
5657 return ct->path;
5660 unsigned int
5661 got_commitable_get_status(struct got_commitable *ct)
5663 return ct->status;
5666 struct check_rebase_ok_arg {
5667 struct got_worktree *worktree;
5668 struct got_repository *repo;
5671 static const struct got_error *
5672 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5674 const struct got_error *err = NULL;
5675 struct check_rebase_ok_arg *a = arg;
5676 unsigned char status;
5677 struct stat sb;
5678 char *ondisk_path;
5680 /* Reject rebase of a work tree with mixed base commits. */
5681 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5682 SHA1_DIGEST_LENGTH))
5683 return got_error(GOT_ERR_MIXED_COMMITS);
5685 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5686 == -1)
5687 return got_error_from_errno("asprintf");
5689 /* Reject rebase of a work tree with modified or staged files. */
5690 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5691 free(ondisk_path);
5692 if (err)
5693 return err;
5695 if (status != GOT_STATUS_NO_CHANGE)
5696 return got_error(GOT_ERR_MODIFIED);
5697 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5698 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5700 return NULL;
5703 const struct got_error *
5704 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5705 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5706 struct got_worktree *worktree, struct got_reference *branch,
5707 struct got_repository *repo)
5709 const struct got_error *err = NULL;
5710 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5711 char *branch_ref_name = NULL;
5712 char *fileindex_path = NULL;
5713 struct check_rebase_ok_arg ok_arg;
5714 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5715 struct got_object_id *wt_branch_tip = NULL;
5717 *new_base_branch_ref = NULL;
5718 *tmp_branch = NULL;
5719 *fileindex = NULL;
5721 err = lock_worktree(worktree, LOCK_EX);
5722 if (err)
5723 return err;
5725 err = open_fileindex(fileindex, &fileindex_path, worktree);
5726 if (err)
5727 goto done;
5729 ok_arg.worktree = worktree;
5730 ok_arg.repo = repo;
5731 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5732 &ok_arg);
5733 if (err)
5734 goto done;
5736 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5737 if (err)
5738 goto done;
5740 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5741 if (err)
5742 goto done;
5744 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5745 if (err)
5746 goto done;
5748 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5749 0);
5750 if (err)
5751 goto done;
5753 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5754 if (err)
5755 goto done;
5756 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5757 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5758 goto done;
5761 err = got_ref_alloc_symref(new_base_branch_ref,
5762 new_base_branch_ref_name, wt_branch);
5763 if (err)
5764 goto done;
5765 err = got_ref_write(*new_base_branch_ref, repo);
5766 if (err)
5767 goto done;
5769 /* TODO Lock original branch's ref while rebasing? */
5771 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5772 if (err)
5773 goto done;
5775 err = got_ref_write(branch_ref, repo);
5776 if (err)
5777 goto done;
5779 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5780 worktree->base_commit_id);
5781 if (err)
5782 goto done;
5783 err = got_ref_write(*tmp_branch, repo);
5784 if (err)
5785 goto done;
5787 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5788 if (err)
5789 goto done;
5790 done:
5791 free(fileindex_path);
5792 free(tmp_branch_name);
5793 free(new_base_branch_ref_name);
5794 free(branch_ref_name);
5795 if (branch_ref)
5796 got_ref_close(branch_ref);
5797 if (wt_branch)
5798 got_ref_close(wt_branch);
5799 free(wt_branch_tip);
5800 if (err) {
5801 if (*new_base_branch_ref) {
5802 got_ref_close(*new_base_branch_ref);
5803 *new_base_branch_ref = NULL;
5805 if (*tmp_branch) {
5806 got_ref_close(*tmp_branch);
5807 *tmp_branch = NULL;
5809 if (*fileindex) {
5810 got_fileindex_free(*fileindex);
5811 *fileindex = NULL;
5813 lock_worktree(worktree, LOCK_SH);
5815 return err;
5818 const struct got_error *
5819 got_worktree_rebase_continue(struct got_object_id **commit_id,
5820 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5821 struct got_reference **branch, struct got_fileindex **fileindex,
5822 struct got_worktree *worktree, struct got_repository *repo)
5824 const struct got_error *err;
5825 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5826 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5827 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5828 char *fileindex_path = NULL;
5829 int have_staged_files = 0;
5831 *commit_id = NULL;
5832 *new_base_branch = NULL;
5833 *tmp_branch = NULL;
5834 *branch = NULL;
5835 *fileindex = NULL;
5837 err = lock_worktree(worktree, LOCK_EX);
5838 if (err)
5839 return err;
5841 err = open_fileindex(fileindex, &fileindex_path, worktree);
5842 if (err)
5843 goto done;
5845 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5846 &have_staged_files);
5847 if (err && err->code != GOT_ERR_CANCELLED)
5848 goto done;
5849 if (have_staged_files) {
5850 err = got_error(GOT_ERR_STAGED_PATHS);
5851 goto done;
5854 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5855 if (err)
5856 goto done;
5858 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5859 if (err)
5860 goto done;
5862 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5863 if (err)
5864 goto done;
5866 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5867 if (err)
5868 goto done;
5870 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5871 if (err)
5872 goto done;
5874 err = got_ref_open(branch, repo,
5875 got_ref_get_symref_target(branch_ref), 0);
5876 if (err)
5877 goto done;
5879 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5880 if (err)
5881 goto done;
5883 err = got_ref_resolve(commit_id, repo, commit_ref);
5884 if (err)
5885 goto done;
5887 err = got_ref_open(new_base_branch, repo,
5888 new_base_branch_ref_name, 0);
5889 if (err)
5890 goto done;
5892 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5893 if (err)
5894 goto done;
5895 done:
5896 free(commit_ref_name);
5897 free(branch_ref_name);
5898 free(fileindex_path);
5899 if (commit_ref)
5900 got_ref_close(commit_ref);
5901 if (branch_ref)
5902 got_ref_close(branch_ref);
5903 if (err) {
5904 free(*commit_id);
5905 *commit_id = NULL;
5906 if (*tmp_branch) {
5907 got_ref_close(*tmp_branch);
5908 *tmp_branch = NULL;
5910 if (*new_base_branch) {
5911 got_ref_close(*new_base_branch);
5912 *new_base_branch = NULL;
5914 if (*branch) {
5915 got_ref_close(*branch);
5916 *branch = NULL;
5918 if (*fileindex) {
5919 got_fileindex_free(*fileindex);
5920 *fileindex = NULL;
5922 lock_worktree(worktree, LOCK_SH);
5924 return err;
5927 const struct got_error *
5928 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5930 const struct got_error *err;
5931 char *tmp_branch_name = NULL;
5933 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5934 if (err)
5935 return err;
5937 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5938 free(tmp_branch_name);
5939 return NULL;
5942 static const struct got_error *
5943 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5944 char **logmsg, void *arg)
5946 *logmsg = arg;
5947 return NULL;
5950 static const struct got_error *
5951 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5952 const char *path, struct got_object_id *blob_id,
5953 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5954 int dirfd, const char *de_name)
5956 return NULL;
5959 struct collect_merged_paths_arg {
5960 got_worktree_checkout_cb progress_cb;
5961 void *progress_arg;
5962 struct got_pathlist_head *merged_paths;
5965 static const struct got_error *
5966 collect_merged_paths(void *arg, unsigned char status, const char *path)
5968 const struct got_error *err;
5969 struct collect_merged_paths_arg *a = arg;
5970 char *p;
5971 struct got_pathlist_entry *new;
5973 err = (*a->progress_cb)(a->progress_arg, status, path);
5974 if (err)
5975 return err;
5977 if (status != GOT_STATUS_MERGE &&
5978 status != GOT_STATUS_ADD &&
5979 status != GOT_STATUS_DELETE &&
5980 status != GOT_STATUS_CONFLICT)
5981 return NULL;
5983 p = strdup(path);
5984 if (p == NULL)
5985 return got_error_from_errno("strdup");
5987 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5988 if (err || new == NULL)
5989 free(p);
5990 return err;
5993 void
5994 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5996 struct got_pathlist_entry *pe;
5998 TAILQ_FOREACH(pe, merged_paths, entry)
5999 free((char *)pe->path);
6001 got_pathlist_free(merged_paths);
6004 static const struct got_error *
6005 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6006 int is_rebase, struct got_repository *repo)
6008 const struct got_error *err;
6009 struct got_reference *commit_ref = NULL;
6011 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6012 if (err) {
6013 if (err->code != GOT_ERR_NOT_REF)
6014 goto done;
6015 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6016 if (err)
6017 goto done;
6018 err = got_ref_write(commit_ref, repo);
6019 if (err)
6020 goto done;
6021 } else if (is_rebase) {
6022 struct got_object_id *stored_id;
6023 int cmp;
6025 err = got_ref_resolve(&stored_id, repo, commit_ref);
6026 if (err)
6027 goto done;
6028 cmp = got_object_id_cmp(commit_id, stored_id);
6029 free(stored_id);
6030 if (cmp != 0) {
6031 err = got_error(GOT_ERR_REBASE_COMMITID);
6032 goto done;
6035 done:
6036 if (commit_ref)
6037 got_ref_close(commit_ref);
6038 return err;
6041 static const struct got_error *
6042 rebase_merge_files(struct got_pathlist_head *merged_paths,
6043 const char *commit_ref_name, struct got_worktree *worktree,
6044 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6045 struct got_object_id *commit_id, struct got_repository *repo,
6046 got_worktree_checkout_cb progress_cb, void *progress_arg,
6047 got_cancel_cb cancel_cb, void *cancel_arg)
6049 const struct got_error *err;
6050 struct got_reference *commit_ref = NULL;
6051 struct collect_merged_paths_arg cmp_arg;
6052 char *fileindex_path;
6054 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6056 err = get_fileindex_path(&fileindex_path, worktree);
6057 if (err)
6058 return err;
6060 cmp_arg.progress_cb = progress_cb;
6061 cmp_arg.progress_arg = progress_arg;
6062 cmp_arg.merged_paths = merged_paths;
6063 err = merge_files(worktree, fileindex, fileindex_path,
6064 parent_commit_id, commit_id, repo, collect_merged_paths,
6065 &cmp_arg, cancel_cb, cancel_arg);
6066 if (commit_ref)
6067 got_ref_close(commit_ref);
6068 return err;
6071 const struct got_error *
6072 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6073 struct got_worktree *worktree, struct got_fileindex *fileindex,
6074 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6075 struct got_repository *repo,
6076 got_worktree_checkout_cb progress_cb, void *progress_arg,
6077 got_cancel_cb cancel_cb, void *cancel_arg)
6079 const struct got_error *err;
6080 char *commit_ref_name;
6082 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6083 if (err)
6084 return err;
6086 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6087 if (err)
6088 goto done;
6090 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6091 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6092 progress_arg, cancel_cb, cancel_arg);
6093 done:
6094 free(commit_ref_name);
6095 return err;
6098 const struct got_error *
6099 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6100 struct got_worktree *worktree, struct got_fileindex *fileindex,
6101 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6102 struct got_repository *repo,
6103 got_worktree_checkout_cb progress_cb, void *progress_arg,
6104 got_cancel_cb cancel_cb, void *cancel_arg)
6106 const struct got_error *err;
6107 char *commit_ref_name;
6109 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6110 if (err)
6111 return err;
6113 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6114 if (err)
6115 goto done;
6117 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6118 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6119 progress_arg, cancel_cb, cancel_arg);
6120 done:
6121 free(commit_ref_name);
6122 return err;
6125 static const struct got_error *
6126 rebase_commit(struct got_object_id **new_commit_id,
6127 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6128 struct got_worktree *worktree, struct got_fileindex *fileindex,
6129 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6130 const char *new_logmsg, struct got_repository *repo)
6132 const struct got_error *err, *sync_err;
6133 struct got_pathlist_head commitable_paths;
6134 struct collect_commitables_arg cc_arg;
6135 char *fileindex_path = NULL;
6136 struct got_reference *head_ref = NULL;
6137 struct got_object_id *head_commit_id = NULL;
6138 char *logmsg = NULL;
6140 TAILQ_INIT(&commitable_paths);
6141 *new_commit_id = NULL;
6143 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6145 err = get_fileindex_path(&fileindex_path, worktree);
6146 if (err)
6147 return err;
6149 cc_arg.commitable_paths = &commitable_paths;
6150 cc_arg.worktree = worktree;
6151 cc_arg.repo = repo;
6152 cc_arg.have_staged_files = 0;
6154 * If possible get the status of individual files directly to
6155 * avoid crawling the entire work tree once per rebased commit.
6156 * TODO: Ideally, merged_paths would contain a list of commitables
6157 * we could use so we could skip worktree_status() entirely.
6159 if (merged_paths) {
6160 struct got_pathlist_entry *pe;
6161 TAILQ_FOREACH(pe, merged_paths, entry) {
6162 err = worktree_status(worktree, pe->path, fileindex,
6163 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6164 0);
6165 if (err)
6166 goto done;
6168 } else {
6169 err = worktree_status(worktree, "", fileindex, repo,
6170 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6171 if (err)
6172 goto done;
6175 if (TAILQ_EMPTY(&commitable_paths)) {
6176 /* No-op change; commit will be elided. */
6177 err = got_ref_delete(commit_ref, repo);
6178 if (err)
6179 goto done;
6180 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6181 goto done;
6184 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6185 if (err)
6186 goto done;
6188 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6189 if (err)
6190 goto done;
6192 if (new_logmsg) {
6193 logmsg = strdup(new_logmsg);
6194 if (logmsg == NULL) {
6195 err = got_error_from_errno("strdup");
6196 goto done;
6198 } else {
6199 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6200 if (err)
6201 goto done;
6204 /* NB: commit_worktree will call free(logmsg) */
6205 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6206 worktree, got_object_commit_get_author(orig_commit),
6207 got_object_commit_get_committer(orig_commit),
6208 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6209 if (err)
6210 goto done;
6212 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6213 if (err)
6214 goto done;
6216 err = got_ref_delete(commit_ref, repo);
6217 if (err)
6218 goto done;
6220 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6221 fileindex, 0);
6222 sync_err = sync_fileindex(fileindex, fileindex_path);
6223 if (sync_err && err == NULL)
6224 err = sync_err;
6225 done:
6226 free(fileindex_path);
6227 free(head_commit_id);
6228 if (head_ref)
6229 got_ref_close(head_ref);
6230 if (err) {
6231 free(*new_commit_id);
6232 *new_commit_id = NULL;
6234 return err;
6237 const struct got_error *
6238 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6239 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6240 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6241 struct got_commit_object *orig_commit,
6242 struct got_object_id *orig_commit_id, struct got_repository *repo)
6244 const struct got_error *err;
6245 char *commit_ref_name;
6246 struct got_reference *commit_ref = NULL;
6247 struct got_object_id *commit_id = NULL;
6249 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6250 if (err)
6251 return err;
6253 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6254 if (err)
6255 goto done;
6256 err = got_ref_resolve(&commit_id, repo, commit_ref);
6257 if (err)
6258 goto done;
6259 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6260 err = got_error(GOT_ERR_REBASE_COMMITID);
6261 goto done;
6264 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6265 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6266 done:
6267 if (commit_ref)
6268 got_ref_close(commit_ref);
6269 free(commit_ref_name);
6270 free(commit_id);
6271 return err;
6274 const struct got_error *
6275 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6276 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6277 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6278 struct got_commit_object *orig_commit,
6279 struct got_object_id *orig_commit_id, const char *new_logmsg,
6280 struct got_repository *repo)
6282 const struct got_error *err;
6283 char *commit_ref_name;
6284 struct got_reference *commit_ref = NULL;
6286 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6287 if (err)
6288 return err;
6290 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6291 if (err)
6292 goto done;
6294 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6295 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6296 done:
6297 if (commit_ref)
6298 got_ref_close(commit_ref);
6299 free(commit_ref_name);
6300 return err;
6303 const struct got_error *
6304 got_worktree_rebase_postpone(struct got_worktree *worktree,
6305 struct got_fileindex *fileindex)
6307 if (fileindex)
6308 got_fileindex_free(fileindex);
6309 return lock_worktree(worktree, LOCK_SH);
6312 static const struct got_error *
6313 delete_ref(const char *name, struct got_repository *repo)
6315 const struct got_error *err;
6316 struct got_reference *ref;
6318 err = got_ref_open(&ref, repo, name, 0);
6319 if (err) {
6320 if (err->code == GOT_ERR_NOT_REF)
6321 return NULL;
6322 return err;
6325 err = got_ref_delete(ref, repo);
6326 got_ref_close(ref);
6327 return err;
6330 static const struct got_error *
6331 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6333 const struct got_error *err;
6334 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6335 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6337 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6338 if (err)
6339 goto done;
6340 err = delete_ref(tmp_branch_name, repo);
6341 if (err)
6342 goto done;
6344 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6345 if (err)
6346 goto done;
6347 err = delete_ref(new_base_branch_ref_name, repo);
6348 if (err)
6349 goto done;
6351 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6352 if (err)
6353 goto done;
6354 err = delete_ref(branch_ref_name, repo);
6355 if (err)
6356 goto done;
6358 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6359 if (err)
6360 goto done;
6361 err = delete_ref(commit_ref_name, repo);
6362 if (err)
6363 goto done;
6365 done:
6366 free(tmp_branch_name);
6367 free(new_base_branch_ref_name);
6368 free(branch_ref_name);
6369 free(commit_ref_name);
6370 return err;
6373 const struct got_error *
6374 got_worktree_rebase_complete(struct got_worktree *worktree,
6375 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6376 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6377 struct got_repository *repo)
6379 const struct got_error *err, *unlockerr;
6380 struct got_object_id *new_head_commit_id = NULL;
6382 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6383 if (err)
6384 return err;
6386 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6387 if (err)
6388 goto done;
6390 err = got_ref_write(rebased_branch, repo);
6391 if (err)
6392 goto done;
6394 err = got_worktree_set_head_ref(worktree, rebased_branch);
6395 if (err)
6396 goto done;
6398 err = delete_rebase_refs(worktree, repo);
6399 done:
6400 if (fileindex)
6401 got_fileindex_free(fileindex);
6402 free(new_head_commit_id);
6403 unlockerr = lock_worktree(worktree, LOCK_SH);
6404 if (unlockerr && err == NULL)
6405 err = unlockerr;
6406 return err;
6409 const struct got_error *
6410 got_worktree_rebase_abort(struct got_worktree *worktree,
6411 struct got_fileindex *fileindex, struct got_repository *repo,
6412 struct got_reference *new_base_branch,
6413 got_worktree_checkout_cb progress_cb, void *progress_arg)
6415 const struct got_error *err, *unlockerr, *sync_err;
6416 struct got_reference *resolved = NULL;
6417 struct got_object_id *commit_id = NULL;
6418 char *fileindex_path = NULL;
6419 struct revert_file_args rfa;
6420 struct got_object_id *tree_id = NULL;
6422 err = lock_worktree(worktree, LOCK_EX);
6423 if (err)
6424 return err;
6426 err = got_ref_open(&resolved, repo,
6427 got_ref_get_symref_target(new_base_branch), 0);
6428 if (err)
6429 goto done;
6431 err = got_worktree_set_head_ref(worktree, resolved);
6432 if (err)
6433 goto done;
6436 * XXX commits to the base branch could have happened while
6437 * we were busy rebasing; should we store the original commit ID
6438 * when rebase begins and read it back here?
6440 err = got_ref_resolve(&commit_id, repo, resolved);
6441 if (err)
6442 goto done;
6444 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6445 if (err)
6446 goto done;
6448 err = got_object_id_by_path(&tree_id, repo,
6449 worktree->base_commit_id, worktree->path_prefix);
6450 if (err)
6451 goto done;
6453 err = delete_rebase_refs(worktree, repo);
6454 if (err)
6455 goto done;
6457 err = get_fileindex_path(&fileindex_path, worktree);
6458 if (err)
6459 goto done;
6461 rfa.worktree = worktree;
6462 rfa.fileindex = fileindex;
6463 rfa.progress_cb = progress_cb;
6464 rfa.progress_arg = progress_arg;
6465 rfa.patch_cb = NULL;
6466 rfa.patch_arg = NULL;
6467 rfa.repo = repo;
6468 err = worktree_status(worktree, "", fileindex, repo,
6469 revert_file, &rfa, NULL, NULL, 0, 0);
6470 if (err)
6471 goto sync;
6473 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6474 repo, progress_cb, progress_arg, NULL, NULL);
6475 sync:
6476 sync_err = sync_fileindex(fileindex, fileindex_path);
6477 if (sync_err && err == NULL)
6478 err = sync_err;
6479 done:
6480 got_ref_close(resolved);
6481 free(tree_id);
6482 free(commit_id);
6483 if (fileindex)
6484 got_fileindex_free(fileindex);
6485 free(fileindex_path);
6487 unlockerr = lock_worktree(worktree, LOCK_SH);
6488 if (unlockerr && err == NULL)
6489 err = unlockerr;
6490 return err;
6493 const struct got_error *
6494 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6495 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6496 struct got_fileindex **fileindex, struct got_worktree *worktree,
6497 struct got_repository *repo)
6499 const struct got_error *err = NULL;
6500 char *tmp_branch_name = NULL;
6501 char *branch_ref_name = NULL;
6502 char *base_commit_ref_name = NULL;
6503 char *fileindex_path = NULL;
6504 struct check_rebase_ok_arg ok_arg;
6505 struct got_reference *wt_branch = NULL;
6506 struct got_reference *base_commit_ref = NULL;
6508 *tmp_branch = NULL;
6509 *branch_ref = NULL;
6510 *base_commit_id = NULL;
6511 *fileindex = NULL;
6513 err = lock_worktree(worktree, LOCK_EX);
6514 if (err)
6515 return err;
6517 err = open_fileindex(fileindex, &fileindex_path, worktree);
6518 if (err)
6519 goto done;
6521 ok_arg.worktree = worktree;
6522 ok_arg.repo = repo;
6523 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6524 &ok_arg);
6525 if (err)
6526 goto done;
6528 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6529 if (err)
6530 goto done;
6532 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6533 if (err)
6534 goto done;
6536 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6537 worktree);
6538 if (err)
6539 goto done;
6541 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6542 0);
6543 if (err)
6544 goto done;
6546 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6547 if (err)
6548 goto done;
6550 err = got_ref_write(*branch_ref, repo);
6551 if (err)
6552 goto done;
6554 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6555 worktree->base_commit_id);
6556 if (err)
6557 goto done;
6558 err = got_ref_write(base_commit_ref, repo);
6559 if (err)
6560 goto done;
6561 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6562 if (*base_commit_id == NULL) {
6563 err = got_error_from_errno("got_object_id_dup");
6564 goto done;
6567 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6568 worktree->base_commit_id);
6569 if (err)
6570 goto done;
6571 err = got_ref_write(*tmp_branch, repo);
6572 if (err)
6573 goto done;
6575 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6576 if (err)
6577 goto done;
6578 done:
6579 free(fileindex_path);
6580 free(tmp_branch_name);
6581 free(branch_ref_name);
6582 free(base_commit_ref_name);
6583 if (wt_branch)
6584 got_ref_close(wt_branch);
6585 if (err) {
6586 if (*branch_ref) {
6587 got_ref_close(*branch_ref);
6588 *branch_ref = NULL;
6590 if (*tmp_branch) {
6591 got_ref_close(*tmp_branch);
6592 *tmp_branch = NULL;
6594 free(*base_commit_id);
6595 if (*fileindex) {
6596 got_fileindex_free(*fileindex);
6597 *fileindex = NULL;
6599 lock_worktree(worktree, LOCK_SH);
6601 return err;
6604 const struct got_error *
6605 got_worktree_histedit_postpone(struct got_worktree *worktree,
6606 struct got_fileindex *fileindex)
6608 if (fileindex)
6609 got_fileindex_free(fileindex);
6610 return lock_worktree(worktree, LOCK_SH);
6613 const struct got_error *
6614 got_worktree_histedit_in_progress(int *in_progress,
6615 struct got_worktree *worktree)
6617 const struct got_error *err;
6618 char *tmp_branch_name = NULL;
6620 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6621 if (err)
6622 return err;
6624 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6625 free(tmp_branch_name);
6626 return NULL;
6629 const struct got_error *
6630 got_worktree_histedit_continue(struct got_object_id **commit_id,
6631 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6632 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6633 struct got_worktree *worktree, struct got_repository *repo)
6635 const struct got_error *err;
6636 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6637 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6638 struct got_reference *commit_ref = NULL;
6639 struct got_reference *base_commit_ref = NULL;
6640 char *fileindex_path = NULL;
6641 int have_staged_files = 0;
6643 *commit_id = NULL;
6644 *tmp_branch = NULL;
6645 *base_commit_id = NULL;
6646 *fileindex = NULL;
6648 err = lock_worktree(worktree, LOCK_EX);
6649 if (err)
6650 return err;
6652 err = open_fileindex(fileindex, &fileindex_path, worktree);
6653 if (err)
6654 goto done;
6656 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6657 &have_staged_files);
6658 if (err && err->code != GOT_ERR_CANCELLED)
6659 goto done;
6660 if (have_staged_files) {
6661 err = got_error(GOT_ERR_STAGED_PATHS);
6662 goto done;
6665 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6666 if (err)
6667 goto done;
6669 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6670 if (err)
6671 goto done;
6673 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6674 if (err)
6675 goto done;
6677 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6678 worktree);
6679 if (err)
6680 goto done;
6682 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6683 if (err)
6684 goto done;
6686 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6687 if (err)
6688 goto done;
6689 err = got_ref_resolve(commit_id, repo, commit_ref);
6690 if (err)
6691 goto done;
6693 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6694 if (err)
6695 goto done;
6696 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6697 if (err)
6698 goto done;
6700 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6701 if (err)
6702 goto done;
6703 done:
6704 free(commit_ref_name);
6705 free(branch_ref_name);
6706 free(fileindex_path);
6707 if (commit_ref)
6708 got_ref_close(commit_ref);
6709 if (base_commit_ref)
6710 got_ref_close(base_commit_ref);
6711 if (err) {
6712 free(*commit_id);
6713 *commit_id = NULL;
6714 free(*base_commit_id);
6715 *base_commit_id = NULL;
6716 if (*tmp_branch) {
6717 got_ref_close(*tmp_branch);
6718 *tmp_branch = NULL;
6720 if (*fileindex) {
6721 got_fileindex_free(*fileindex);
6722 *fileindex = NULL;
6724 lock_worktree(worktree, LOCK_EX);
6726 return err;
6729 static const struct got_error *
6730 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6732 const struct got_error *err;
6733 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6734 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6736 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6737 if (err)
6738 goto done;
6739 err = delete_ref(tmp_branch_name, repo);
6740 if (err)
6741 goto done;
6743 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6744 worktree);
6745 if (err)
6746 goto done;
6747 err = delete_ref(base_commit_ref_name, repo);
6748 if (err)
6749 goto done;
6751 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6752 if (err)
6753 goto done;
6754 err = delete_ref(branch_ref_name, repo);
6755 if (err)
6756 goto done;
6758 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6759 if (err)
6760 goto done;
6761 err = delete_ref(commit_ref_name, repo);
6762 if (err)
6763 goto done;
6764 done:
6765 free(tmp_branch_name);
6766 free(base_commit_ref_name);
6767 free(branch_ref_name);
6768 free(commit_ref_name);
6769 return err;
6772 const struct got_error *
6773 got_worktree_histedit_abort(struct got_worktree *worktree,
6774 struct got_fileindex *fileindex, struct got_repository *repo,
6775 struct got_reference *branch, struct got_object_id *base_commit_id,
6776 got_worktree_checkout_cb progress_cb, void *progress_arg)
6778 const struct got_error *err, *unlockerr, *sync_err;
6779 struct got_reference *resolved = NULL;
6780 char *fileindex_path = NULL;
6781 struct got_object_id *tree_id = NULL;
6782 struct revert_file_args rfa;
6784 err = lock_worktree(worktree, LOCK_EX);
6785 if (err)
6786 return err;
6788 err = got_ref_open(&resolved, repo,
6789 got_ref_get_symref_target(branch), 0);
6790 if (err)
6791 goto done;
6793 err = got_worktree_set_head_ref(worktree, resolved);
6794 if (err)
6795 goto done;
6797 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6798 if (err)
6799 goto done;
6801 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6802 worktree->path_prefix);
6803 if (err)
6804 goto done;
6806 err = delete_histedit_refs(worktree, repo);
6807 if (err)
6808 goto done;
6810 err = get_fileindex_path(&fileindex_path, worktree);
6811 if (err)
6812 goto done;
6814 rfa.worktree = worktree;
6815 rfa.fileindex = fileindex;
6816 rfa.progress_cb = progress_cb;
6817 rfa.progress_arg = progress_arg;
6818 rfa.patch_cb = NULL;
6819 rfa.patch_arg = NULL;
6820 rfa.repo = repo;
6821 err = worktree_status(worktree, "", fileindex, repo,
6822 revert_file, &rfa, NULL, NULL, 0, 0);
6823 if (err)
6824 goto sync;
6826 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6827 repo, progress_cb, progress_arg, NULL, NULL);
6828 sync:
6829 sync_err = sync_fileindex(fileindex, fileindex_path);
6830 if (sync_err && err == NULL)
6831 err = sync_err;
6832 done:
6833 got_ref_close(resolved);
6834 free(tree_id);
6835 free(fileindex_path);
6837 unlockerr = lock_worktree(worktree, LOCK_SH);
6838 if (unlockerr && err == NULL)
6839 err = unlockerr;
6840 return err;
6843 const struct got_error *
6844 got_worktree_histedit_complete(struct got_worktree *worktree,
6845 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6846 struct got_reference *edited_branch, struct got_repository *repo)
6848 const struct got_error *err, *unlockerr;
6849 struct got_object_id *new_head_commit_id = NULL;
6850 struct got_reference *resolved = NULL;
6852 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6853 if (err)
6854 return err;
6856 err = got_ref_open(&resolved, repo,
6857 got_ref_get_symref_target(edited_branch), 0);
6858 if (err)
6859 goto done;
6861 err = got_ref_change_ref(resolved, new_head_commit_id);
6862 if (err)
6863 goto done;
6865 err = got_ref_write(resolved, repo);
6866 if (err)
6867 goto done;
6869 err = got_worktree_set_head_ref(worktree, resolved);
6870 if (err)
6871 goto done;
6873 err = delete_histedit_refs(worktree, repo);
6874 done:
6875 if (fileindex)
6876 got_fileindex_free(fileindex);
6877 free(new_head_commit_id);
6878 unlockerr = lock_worktree(worktree, LOCK_SH);
6879 if (unlockerr && err == NULL)
6880 err = unlockerr;
6881 return err;
6884 const struct got_error *
6885 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6886 struct got_object_id *commit_id, struct got_repository *repo)
6888 const struct got_error *err;
6889 char *commit_ref_name;
6891 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6892 if (err)
6893 return err;
6895 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6896 if (err)
6897 goto done;
6899 err = delete_ref(commit_ref_name, repo);
6900 done:
6901 free(commit_ref_name);
6902 return err;
6905 const struct got_error *
6906 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6907 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6908 struct got_worktree *worktree, const char *refname,
6909 struct got_repository *repo)
6911 const struct got_error *err = NULL;
6912 char *fileindex_path = NULL;
6913 struct check_rebase_ok_arg ok_arg;
6915 *fileindex = NULL;
6916 *branch_ref = NULL;
6917 *base_branch_ref = NULL;
6919 err = lock_worktree(worktree, LOCK_EX);
6920 if (err)
6921 return err;
6923 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6924 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6925 "cannot integrate a branch into itself; "
6926 "update -b or different branch name required");
6927 goto done;
6930 err = open_fileindex(fileindex, &fileindex_path, worktree);
6931 if (err)
6932 goto done;
6934 /* Preconditions are the same as for rebase. */
6935 ok_arg.worktree = worktree;
6936 ok_arg.repo = repo;
6937 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6938 &ok_arg);
6939 if (err)
6940 goto done;
6942 err = got_ref_open(branch_ref, repo, refname, 1);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(base_branch_ref, repo,
6947 got_worktree_get_head_ref_name(worktree), 1);
6948 done:
6949 if (err) {
6950 if (*branch_ref) {
6951 got_ref_close(*branch_ref);
6952 *branch_ref = NULL;
6954 if (*base_branch_ref) {
6955 got_ref_close(*base_branch_ref);
6956 *base_branch_ref = NULL;
6958 if (*fileindex) {
6959 got_fileindex_free(*fileindex);
6960 *fileindex = NULL;
6962 lock_worktree(worktree, LOCK_SH);
6964 return err;
6967 const struct got_error *
6968 got_worktree_integrate_continue(struct got_worktree *worktree,
6969 struct got_fileindex *fileindex, struct got_repository *repo,
6970 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6971 got_worktree_checkout_cb progress_cb, void *progress_arg,
6972 got_cancel_cb cancel_cb, void *cancel_arg)
6974 const struct got_error *err = NULL, *sync_err, *unlockerr;
6975 char *fileindex_path = NULL;
6976 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6978 err = get_fileindex_path(&fileindex_path, worktree);
6979 if (err)
6980 goto done;
6982 err = got_ref_resolve(&commit_id, repo, branch_ref);
6983 if (err)
6984 goto done;
6986 err = got_object_id_by_path(&tree_id, repo, commit_id,
6987 worktree->path_prefix);
6988 if (err)
6989 goto done;
6991 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6992 if (err)
6993 goto done;
6995 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6996 progress_cb, progress_arg, cancel_cb, cancel_arg);
6997 if (err)
6998 goto sync;
7000 err = got_ref_change_ref(base_branch_ref, commit_id);
7001 if (err)
7002 goto sync;
7004 err = got_ref_write(base_branch_ref, repo);
7005 sync:
7006 sync_err = sync_fileindex(fileindex, fileindex_path);
7007 if (sync_err && err == NULL)
7008 err = sync_err;
7010 done:
7011 unlockerr = got_ref_unlock(branch_ref);
7012 if (unlockerr && err == NULL)
7013 err = unlockerr;
7014 got_ref_close(branch_ref);
7016 unlockerr = got_ref_unlock(base_branch_ref);
7017 if (unlockerr && err == NULL)
7018 err = unlockerr;
7019 got_ref_close(base_branch_ref);
7021 got_fileindex_free(fileindex);
7022 free(fileindex_path);
7023 free(tree_id);
7025 unlockerr = lock_worktree(worktree, LOCK_SH);
7026 if (unlockerr && err == NULL)
7027 err = unlockerr;
7028 return err;
7031 const struct got_error *
7032 got_worktree_integrate_abort(struct got_worktree *worktree,
7033 struct got_fileindex *fileindex, struct got_repository *repo,
7034 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7036 const struct got_error *err = NULL, *unlockerr = NULL;
7038 got_fileindex_free(fileindex);
7040 err = lock_worktree(worktree, LOCK_SH);
7042 unlockerr = got_ref_unlock(branch_ref);
7043 if (unlockerr && err == NULL)
7044 err = unlockerr;
7045 got_ref_close(branch_ref);
7047 unlockerr = got_ref_unlock(base_branch_ref);
7048 if (unlockerr && err == NULL)
7049 err = unlockerr;
7050 got_ref_close(base_branch_ref);
7052 return err;
7055 struct check_stage_ok_arg {
7056 struct got_object_id *head_commit_id;
7057 struct got_worktree *worktree;
7058 struct got_fileindex *fileindex;
7059 struct got_repository *repo;
7060 int have_changes;
7063 const struct got_error *
7064 check_stage_ok(void *arg, unsigned char status,
7065 unsigned char staged_status, const char *relpath,
7066 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7067 struct got_object_id *commit_id, int dirfd, const char *de_name)
7069 struct check_stage_ok_arg *a = arg;
7070 const struct got_error *err = NULL;
7071 struct got_fileindex_entry *ie;
7072 struct got_object_id base_commit_id;
7073 struct got_object_id *base_commit_idp = NULL;
7074 char *in_repo_path = NULL, *p;
7076 if (status == GOT_STATUS_UNVERSIONED ||
7077 status == GOT_STATUS_NO_CHANGE)
7078 return NULL;
7079 if (status == GOT_STATUS_NONEXISTENT)
7080 return got_error_set_errno(ENOENT, relpath);
7082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7083 if (ie == NULL)
7084 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7086 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7087 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7088 relpath) == -1)
7089 return got_error_from_errno("asprintf");
7091 if (got_fileindex_entry_has_commit(ie)) {
7092 memcpy(base_commit_id.sha1, ie->commit_sha1,
7093 SHA1_DIGEST_LENGTH);
7094 base_commit_idp = &base_commit_id;
7097 if (status == GOT_STATUS_CONFLICT) {
7098 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7099 goto done;
7100 } else if (status != GOT_STATUS_ADD &&
7101 status != GOT_STATUS_MODIFY &&
7102 status != GOT_STATUS_DELETE) {
7103 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7104 goto done;
7107 a->have_changes = 1;
7109 p = in_repo_path;
7110 while (p[0] == '/')
7111 p++;
7112 err = check_out_of_date(p, status, staged_status,
7113 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7114 GOT_ERR_STAGE_OUT_OF_DATE);
7115 done:
7116 free(in_repo_path);
7117 return err;
7120 struct stage_path_arg {
7121 struct got_worktree *worktree;
7122 struct got_fileindex *fileindex;
7123 struct got_repository *repo;
7124 got_worktree_status_cb status_cb;
7125 void *status_arg;
7126 got_worktree_patch_cb patch_cb;
7127 void *patch_arg;
7128 int staged_something;
7129 int allow_bad_symlinks;
7132 static const struct got_error *
7133 stage_path(void *arg, unsigned char status,
7134 unsigned char staged_status, const char *relpath,
7135 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7136 struct got_object_id *commit_id, int dirfd, const char *de_name)
7138 struct stage_path_arg *a = arg;
7139 const struct got_error *err = NULL;
7140 struct got_fileindex_entry *ie;
7141 char *ondisk_path = NULL, *path_content = NULL;
7142 uint32_t stage;
7143 struct got_object_id *new_staged_blob_id = NULL;
7144 struct stat sb;
7146 if (status == GOT_STATUS_UNVERSIONED)
7147 return NULL;
7149 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7150 if (ie == NULL)
7151 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7153 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7154 relpath)== -1)
7155 return got_error_from_errno("asprintf");
7157 switch (status) {
7158 case GOT_STATUS_ADD:
7159 case GOT_STATUS_MODIFY:
7160 /* XXX could sb.st_mode be passed in by our caller? */
7161 if (lstat(ondisk_path, &sb) == -1) {
7162 err = got_error_from_errno2("lstat", ondisk_path);
7163 break;
7165 if (a->patch_cb) {
7166 if (status == GOT_STATUS_ADD) {
7167 int choice = GOT_PATCH_CHOICE_NONE;
7168 err = (*a->patch_cb)(&choice, a->patch_arg,
7169 status, ie->path, NULL, 1, 1);
7170 if (err)
7171 break;
7172 if (choice != GOT_PATCH_CHOICE_YES)
7173 break;
7174 } else {
7175 err = create_patched_content(&path_content, 0,
7176 staged_blob_id ? staged_blob_id : blob_id,
7177 ondisk_path, dirfd, de_name, ie->path,
7178 a->repo, a->patch_cb, a->patch_arg);
7179 if (err || path_content == NULL)
7180 break;
7183 err = got_object_blob_create(&new_staged_blob_id,
7184 path_content ? path_content : ondisk_path, a->repo);
7185 if (err)
7186 break;
7187 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7188 SHA1_DIGEST_LENGTH);
7189 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7190 stage = GOT_FILEIDX_STAGE_ADD;
7191 else
7192 stage = GOT_FILEIDX_STAGE_MODIFY;
7193 got_fileindex_entry_stage_set(ie, stage);
7194 if (S_ISLNK(sb.st_mode)) {
7195 int is_bad_symlink = 0;
7196 if (!a->allow_bad_symlinks) {
7197 char target_path[PATH_MAX];
7198 ssize_t target_len;
7199 target_len = readlink(ondisk_path, target_path,
7200 sizeof(target_path));
7201 if (target_len == -1) {
7202 err = got_error_from_errno2("readlink",
7203 ondisk_path);
7204 break;
7206 err = is_bad_symlink_target(&is_bad_symlink,
7207 target_path, target_len, ondisk_path,
7208 a->worktree->root_path);
7209 if (err)
7210 break;
7211 if (is_bad_symlink) {
7212 err = got_error_path(ondisk_path,
7213 GOT_ERR_BAD_SYMLINK);
7214 break;
7217 if (is_bad_symlink)
7218 got_fileindex_entry_staged_filetype_set(ie,
7219 GOT_FILEIDX_MODE_BAD_SYMLINK);
7220 else
7221 got_fileindex_entry_staged_filetype_set(ie,
7222 GOT_FILEIDX_MODE_SYMLINK);
7223 } else {
7224 got_fileindex_entry_staged_filetype_set(ie,
7225 GOT_FILEIDX_MODE_REGULAR_FILE);
7227 a->staged_something = 1;
7228 if (a->status_cb == NULL)
7229 break;
7230 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7231 get_staged_status(ie), relpath, blob_id,
7232 new_staged_blob_id, NULL, dirfd, de_name);
7233 break;
7234 case GOT_STATUS_DELETE:
7235 if (staged_status == GOT_STATUS_DELETE)
7236 break;
7237 if (a->patch_cb) {
7238 int choice = GOT_PATCH_CHOICE_NONE;
7239 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7240 ie->path, NULL, 1, 1);
7241 if (err)
7242 break;
7243 if (choice == GOT_PATCH_CHOICE_NO)
7244 break;
7245 if (choice != GOT_PATCH_CHOICE_YES) {
7246 err = got_error(GOT_ERR_PATCH_CHOICE);
7247 break;
7250 stage = GOT_FILEIDX_STAGE_DELETE;
7251 got_fileindex_entry_stage_set(ie, stage);
7252 a->staged_something = 1;
7253 if (a->status_cb == NULL)
7254 break;
7255 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7256 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7257 de_name);
7258 break;
7259 case GOT_STATUS_NO_CHANGE:
7260 break;
7261 case GOT_STATUS_CONFLICT:
7262 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7263 break;
7264 case GOT_STATUS_NONEXISTENT:
7265 err = got_error_set_errno(ENOENT, relpath);
7266 break;
7267 default:
7268 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7269 break;
7272 if (path_content && unlink(path_content) == -1 && err == NULL)
7273 err = got_error_from_errno2("unlink", path_content);
7274 free(path_content);
7275 free(ondisk_path);
7276 free(new_staged_blob_id);
7277 return err;
7280 const struct got_error *
7281 got_worktree_stage(struct got_worktree *worktree,
7282 struct got_pathlist_head *paths,
7283 got_worktree_status_cb status_cb, void *status_arg,
7284 got_worktree_patch_cb patch_cb, void *patch_arg,
7285 int allow_bad_symlinks, struct got_repository *repo)
7287 const struct got_error *err = NULL, *sync_err, *unlockerr;
7288 struct got_pathlist_entry *pe;
7289 struct got_fileindex *fileindex = NULL;
7290 char *fileindex_path = NULL;
7291 struct got_reference *head_ref = NULL;
7292 struct got_object_id *head_commit_id = NULL;
7293 struct check_stage_ok_arg oka;
7294 struct stage_path_arg spa;
7296 err = lock_worktree(worktree, LOCK_EX);
7297 if (err)
7298 return err;
7300 err = got_ref_open(&head_ref, repo,
7301 got_worktree_get_head_ref_name(worktree), 0);
7302 if (err)
7303 goto done;
7304 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7305 if (err)
7306 goto done;
7307 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7308 if (err)
7309 goto done;
7311 /* Check pre-conditions before staging anything. */
7312 oka.head_commit_id = head_commit_id;
7313 oka.worktree = worktree;
7314 oka.fileindex = fileindex;
7315 oka.repo = repo;
7316 oka.have_changes = 0;
7317 TAILQ_FOREACH(pe, paths, entry) {
7318 err = worktree_status(worktree, pe->path, fileindex, repo,
7319 check_stage_ok, &oka, NULL, NULL, 0, 0);
7320 if (err)
7321 goto done;
7323 if (!oka.have_changes) {
7324 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7325 goto done;
7328 spa.worktree = worktree;
7329 spa.fileindex = fileindex;
7330 spa.repo = repo;
7331 spa.patch_cb = patch_cb;
7332 spa.patch_arg = patch_arg;
7333 spa.status_cb = status_cb;
7334 spa.status_arg = status_arg;
7335 spa.staged_something = 0;
7336 spa.allow_bad_symlinks = allow_bad_symlinks;
7337 TAILQ_FOREACH(pe, paths, entry) {
7338 err = worktree_status(worktree, pe->path, fileindex, repo,
7339 stage_path, &spa, NULL, NULL, 0, 0);
7340 if (err)
7341 goto done;
7343 if (!spa.staged_something) {
7344 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7345 goto done;
7348 sync_err = sync_fileindex(fileindex, fileindex_path);
7349 if (sync_err && err == NULL)
7350 err = sync_err;
7351 done:
7352 if (head_ref)
7353 got_ref_close(head_ref);
7354 free(head_commit_id);
7355 free(fileindex_path);
7356 if (fileindex)
7357 got_fileindex_free(fileindex);
7358 unlockerr = lock_worktree(worktree, LOCK_SH);
7359 if (unlockerr && err == NULL)
7360 err = unlockerr;
7361 return err;
7364 struct unstage_path_arg {
7365 struct got_worktree *worktree;
7366 struct got_fileindex *fileindex;
7367 struct got_repository *repo;
7368 got_worktree_checkout_cb progress_cb;
7369 void *progress_arg;
7370 got_worktree_patch_cb patch_cb;
7371 void *patch_arg;
7374 static const struct got_error *
7375 create_unstaged_content(char **path_unstaged_content,
7376 char **path_new_staged_content, struct got_object_id *blob_id,
7377 struct got_object_id *staged_blob_id, const char *relpath,
7378 struct got_repository *repo,
7379 got_worktree_patch_cb patch_cb, void *patch_arg)
7381 const struct got_error *err;
7382 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7383 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7384 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7385 struct stat sb1, sb2;
7386 struct got_diff_changes *changes = NULL;
7387 struct got_diff_state *ds = NULL;
7388 struct got_diff_args *args = NULL;
7389 struct got_diff_change *change;
7390 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7391 int have_content = 0, have_rejected_content = 0;
7393 *path_unstaged_content = NULL;
7394 *path_new_staged_content = NULL;
7396 err = got_object_id_str(&label1, blob_id);
7397 if (err)
7398 return err;
7399 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7400 if (err)
7401 goto done;
7403 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7404 if (err)
7405 goto done;
7407 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7408 if (err)
7409 goto done;
7411 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7412 if (err)
7413 goto done;
7415 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7416 if (err)
7417 goto done;
7419 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7420 if (err)
7421 goto done;
7423 if (stat(path1, &sb1) == -1) {
7424 err = got_error_from_errno2("stat", path1);
7425 goto done;
7428 if (stat(path2, &sb2) == -1) {
7429 err = got_error_from_errno2("stat", path2);
7430 goto done;
7433 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7434 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7435 if (err)
7436 goto done;
7438 err = got_opentemp_named(path_unstaged_content, &outfile,
7439 "got-unstaged-content");
7440 if (err)
7441 goto done;
7442 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7443 "got-new-staged-content");
7444 if (err)
7445 goto done;
7447 if (fseek(f1, 0L, SEEK_SET) == -1) {
7448 err = got_ferror(f1, GOT_ERR_IO);
7449 goto done;
7451 if (fseek(f2, 0L, SEEK_SET) == -1) {
7452 err = got_ferror(f2, GOT_ERR_IO);
7453 goto done;
7455 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7456 int choice;
7457 err = apply_or_reject_change(&choice, change, ++n,
7458 changes->nchanges, ds, args, diff_flags, relpath,
7459 f1, f2, &line_cur1, &line_cur2,
7460 outfile, rejectfile, patch_cb, patch_arg);
7461 if (err)
7462 goto done;
7463 if (choice == GOT_PATCH_CHOICE_YES)
7464 have_content = 1;
7465 else
7466 have_rejected_content = 1;
7467 if (choice == GOT_PATCH_CHOICE_QUIT)
7468 break;
7470 if (have_content || have_rejected_content)
7471 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7472 outfile, rejectfile);
7473 done:
7474 free(label1);
7475 if (blob)
7476 got_object_blob_close(blob);
7477 if (staged_blob)
7478 got_object_blob_close(staged_blob);
7479 if (f1 && fclose(f1) == EOF && err == NULL)
7480 err = got_error_from_errno2("fclose", path1);
7481 if (f2 && fclose(f2) == EOF && err == NULL)
7482 err = got_error_from_errno2("fclose", path2);
7483 if (outfile && fclose(outfile) == EOF && err == NULL)
7484 err = got_error_from_errno2("fclose", *path_unstaged_content);
7485 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7486 err = got_error_from_errno2("fclose", *path_new_staged_content);
7487 if (path1 && unlink(path1) == -1 && err == NULL)
7488 err = got_error_from_errno2("unlink", path1);
7489 if (path2 && unlink(path2) == -1 && err == NULL)
7490 err = got_error_from_errno2("unlink", path2);
7491 if (err || !have_content) {
7492 if (*path_unstaged_content &&
7493 unlink(*path_unstaged_content) == -1 && err == NULL)
7494 err = got_error_from_errno2("unlink",
7495 *path_unstaged_content);
7496 free(*path_unstaged_content);
7497 *path_unstaged_content = NULL;
7499 if (err || !have_content || !have_rejected_content) {
7500 if (*path_new_staged_content &&
7501 unlink(*path_new_staged_content) == -1 && err == NULL)
7502 err = got_error_from_errno2("unlink",
7503 *path_new_staged_content);
7504 free(*path_new_staged_content);
7505 *path_new_staged_content = NULL;
7507 free(args);
7508 if (ds) {
7509 got_diff_state_free(ds);
7510 free(ds);
7512 if (changes)
7513 got_diff_free_changes(changes);
7514 free(path1);
7515 free(path2);
7516 return err;
7519 static const struct got_error *
7520 unstage_hunks(struct got_object_id *staged_blob_id,
7521 struct got_blob_object *blob_base,
7522 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7523 const char *ondisk_path, const char *label_orig,
7524 struct got_worktree *worktree, struct got_repository *repo,
7525 got_worktree_patch_cb patch_cb, void *patch_arg,
7526 got_worktree_checkout_cb progress_cb, void *progress_arg)
7528 const struct got_error *err = NULL;
7529 char *path_unstaged_content = NULL;
7530 char *path_new_staged_content = NULL;
7531 struct got_object_id *new_staged_blob_id = NULL;
7532 FILE *f = NULL;
7533 struct stat sb;
7535 err = create_unstaged_content(&path_unstaged_content,
7536 &path_new_staged_content, blob_id, staged_blob_id,
7537 ie->path, repo, patch_cb, patch_arg);
7538 if (err)
7539 return err;
7541 if (path_unstaged_content == NULL)
7542 return NULL;
7544 if (path_new_staged_content) {
7545 err = got_object_blob_create(&new_staged_blob_id,
7546 path_new_staged_content, repo);
7547 if (err)
7548 goto done;
7551 f = fopen(path_unstaged_content, "r");
7552 if (f == NULL) {
7553 err = got_error_from_errno2("fopen",
7554 path_unstaged_content);
7555 goto done;
7557 if (fstat(fileno(f), &sb) == -1) {
7558 err = got_error_from_errno2("fstat", path_unstaged_content);
7559 goto done;
7561 if (got_fileindex_entry_staged_filetype_get(ie) ==
7562 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7563 char link_target[PATH_MAX];
7564 size_t r;
7565 r = fread(link_target, 1, sizeof(link_target), f);
7566 if (r == 0 && ferror(f)) {
7567 err = got_error_from_errno("fread");
7568 goto done;
7570 if (r >= sizeof(link_target)) { /* should not happen */
7571 err = got_error(GOT_ERR_NO_SPACE);
7572 goto done;
7574 link_target[r] = '\0';
7575 err = merge_symlink(worktree, blob_base,
7576 ondisk_path, ie->path, label_orig, link_target,
7577 worktree->base_commit_id, repo, progress_cb,
7578 progress_arg);
7579 } else {
7580 int local_changes_subsumed;
7581 err = merge_file(&local_changes_subsumed, worktree,
7582 blob_base, ondisk_path, ie->path,
7583 got_fileindex_perms_to_st(ie),
7584 path_unstaged_content, label_orig, "unstaged",
7585 repo, progress_cb, progress_arg);
7587 if (err)
7588 goto done;
7590 if (new_staged_blob_id) {
7591 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7592 SHA1_DIGEST_LENGTH);
7593 } else
7594 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7595 done:
7596 free(new_staged_blob_id);
7597 if (path_unstaged_content &&
7598 unlink(path_unstaged_content) == -1 && err == NULL)
7599 err = got_error_from_errno2("unlink", path_unstaged_content);
7600 if (path_new_staged_content &&
7601 unlink(path_new_staged_content) == -1 && err == NULL)
7602 err = got_error_from_errno2("unlink", path_new_staged_content);
7603 if (f && fclose(f) != 0 && err == NULL)
7604 err = got_error_from_errno2("fclose", path_unstaged_content);
7605 free(path_unstaged_content);
7606 free(path_new_staged_content);
7607 return err;
7610 static const struct got_error *
7611 unstage_path(void *arg, unsigned char status,
7612 unsigned char staged_status, const char *relpath,
7613 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7614 struct got_object_id *commit_id, int dirfd, const char *de_name)
7616 const struct got_error *err = NULL;
7617 struct unstage_path_arg *a = arg;
7618 struct got_fileindex_entry *ie;
7619 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7620 char *ondisk_path = NULL;
7621 char *id_str = NULL, *label_orig = NULL;
7622 int local_changes_subsumed;
7623 struct stat sb;
7625 if (staged_status != GOT_STATUS_ADD &&
7626 staged_status != GOT_STATUS_MODIFY &&
7627 staged_status != GOT_STATUS_DELETE)
7628 return NULL;
7630 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7631 if (ie == NULL)
7632 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7634 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7635 == -1)
7636 return got_error_from_errno("asprintf");
7638 err = got_object_id_str(&id_str,
7639 commit_id ? commit_id : a->worktree->base_commit_id);
7640 if (err)
7641 goto done;
7642 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7643 id_str) == -1) {
7644 err = got_error_from_errno("asprintf");
7645 goto done;
7648 switch (staged_status) {
7649 case GOT_STATUS_MODIFY:
7650 err = got_object_open_as_blob(&blob_base, a->repo,
7651 blob_id, 8192);
7652 if (err)
7653 break;
7654 /* fall through */
7655 case GOT_STATUS_ADD:
7656 if (a->patch_cb) {
7657 if (staged_status == GOT_STATUS_ADD) {
7658 int choice = GOT_PATCH_CHOICE_NONE;
7659 err = (*a->patch_cb)(&choice, a->patch_arg,
7660 staged_status, ie->path, NULL, 1, 1);
7661 if (err)
7662 break;
7663 if (choice != GOT_PATCH_CHOICE_YES)
7664 break;
7665 } else {
7666 err = unstage_hunks(staged_blob_id,
7667 blob_base, blob_id, ie, ondisk_path,
7668 label_orig, a->worktree, a->repo,
7669 a->patch_cb, a->patch_arg,
7670 a->progress_cb, a->progress_arg);
7671 break; /* Done with this file. */
7674 err = got_object_open_as_blob(&blob_staged, a->repo,
7675 staged_blob_id, 8192);
7676 if (err)
7677 break;
7678 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7679 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7680 case GOT_FILEIDX_MODE_REGULAR_FILE:
7681 err = merge_blob(&local_changes_subsumed, a->worktree,
7682 blob_base, ondisk_path, relpath,
7683 got_fileindex_perms_to_st(ie), label_orig,
7684 blob_staged, commit_id ? commit_id :
7685 a->worktree->base_commit_id, a->repo,
7686 a->progress_cb, a->progress_arg);
7687 break;
7688 case GOT_FILEIDX_MODE_SYMLINK:
7689 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7690 char *staged_target;
7691 err = got_object_blob_read_to_str(
7692 &staged_target, blob_staged);
7693 if (err)
7694 goto done;
7695 err = merge_symlink(a->worktree, blob_base,
7696 ondisk_path, relpath, label_orig,
7697 staged_target, commit_id ? commit_id :
7698 a->worktree->base_commit_id,
7699 a->repo, a->progress_cb, a->progress_arg);
7700 free(staged_target);
7701 } else {
7702 err = merge_blob(&local_changes_subsumed,
7703 a->worktree, blob_base, ondisk_path,
7704 relpath, got_fileindex_perms_to_st(ie),
7705 label_orig, blob_staged,
7706 commit_id ? commit_id :
7707 a->worktree->base_commit_id, a->repo,
7708 a->progress_cb, a->progress_arg);
7710 break;
7711 default:
7712 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7713 break;
7715 if (err == NULL)
7716 got_fileindex_entry_stage_set(ie,
7717 GOT_FILEIDX_STAGE_NONE);
7718 break;
7719 case GOT_STATUS_DELETE:
7720 if (a->patch_cb) {
7721 int choice = GOT_PATCH_CHOICE_NONE;
7722 err = (*a->patch_cb)(&choice, a->patch_arg,
7723 staged_status, ie->path, NULL, 1, 1);
7724 if (err)
7725 break;
7726 if (choice == GOT_PATCH_CHOICE_NO)
7727 break;
7728 if (choice != GOT_PATCH_CHOICE_YES) {
7729 err = got_error(GOT_ERR_PATCH_CHOICE);
7730 break;
7733 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7734 err = get_file_status(&status, &sb, ie, ondisk_path,
7735 dirfd, de_name, a->repo);
7736 if (err)
7737 break;
7738 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7739 break;
7741 done:
7742 free(ondisk_path);
7743 if (blob_base)
7744 got_object_blob_close(blob_base);
7745 if (blob_staged)
7746 got_object_blob_close(blob_staged);
7747 free(id_str);
7748 free(label_orig);
7749 return err;
7752 const struct got_error *
7753 got_worktree_unstage(struct got_worktree *worktree,
7754 struct got_pathlist_head *paths,
7755 got_worktree_checkout_cb progress_cb, void *progress_arg,
7756 got_worktree_patch_cb patch_cb, void *patch_arg,
7757 struct got_repository *repo)
7759 const struct got_error *err = NULL, *sync_err, *unlockerr;
7760 struct got_pathlist_entry *pe;
7761 struct got_fileindex *fileindex = NULL;
7762 char *fileindex_path = NULL;
7763 struct unstage_path_arg upa;
7765 err = lock_worktree(worktree, LOCK_EX);
7766 if (err)
7767 return err;
7769 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7770 if (err)
7771 goto done;
7773 upa.worktree = worktree;
7774 upa.fileindex = fileindex;
7775 upa.repo = repo;
7776 upa.progress_cb = progress_cb;
7777 upa.progress_arg = progress_arg;
7778 upa.patch_cb = patch_cb;
7779 upa.patch_arg = patch_arg;
7780 TAILQ_FOREACH(pe, paths, entry) {
7781 err = worktree_status(worktree, pe->path, fileindex, repo,
7782 unstage_path, &upa, NULL, NULL, 0, 0);
7783 if (err)
7784 goto done;
7787 sync_err = sync_fileindex(fileindex, fileindex_path);
7788 if (sync_err && err == NULL)
7789 err = sync_err;
7790 done:
7791 free(fileindex_path);
7792 if (fileindex)
7793 got_fileindex_free(fileindex);
7794 unlockerr = lock_worktree(worktree, LOCK_SH);
7795 if (unlockerr && err == NULL)
7796 err = unlockerr;
7797 return err;
7800 struct report_file_info_arg {
7801 struct got_worktree *worktree;
7802 got_worktree_path_info_cb info_cb;
7803 void *info_arg;
7804 struct got_pathlist_head *paths;
7805 got_cancel_cb cancel_cb;
7806 void *cancel_arg;
7809 static const struct got_error *
7810 report_file_info(void *arg, struct got_fileindex_entry *ie)
7812 struct report_file_info_arg *a = arg;
7813 struct got_pathlist_entry *pe;
7814 struct got_object_id blob_id, staged_blob_id, commit_id;
7815 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7816 struct got_object_id *commit_idp = NULL;
7817 int stage;
7819 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7820 return got_error(GOT_ERR_CANCELLED);
7822 TAILQ_FOREACH(pe, a->paths, entry) {
7823 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7824 got_path_is_child(ie->path, pe->path, pe->path_len))
7825 break;
7827 if (pe == NULL) /* not found */
7828 return NULL;
7830 if (got_fileindex_entry_has_blob(ie)) {
7831 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7832 blob_idp = &blob_id;
7834 stage = got_fileindex_entry_stage_get(ie);
7835 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7836 stage == GOT_FILEIDX_STAGE_ADD) {
7837 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7838 SHA1_DIGEST_LENGTH);
7839 staged_blob_idp = &staged_blob_id;
7842 if (got_fileindex_entry_has_commit(ie)) {
7843 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7844 commit_idp = &commit_id;
7847 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7848 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7851 const struct got_error *
7852 got_worktree_path_info(struct got_worktree *worktree,
7853 struct got_pathlist_head *paths,
7854 got_worktree_path_info_cb info_cb, void *info_arg,
7855 got_cancel_cb cancel_cb, void *cancel_arg)
7858 const struct got_error *err = NULL, *unlockerr;
7859 struct got_fileindex *fileindex = NULL;
7860 char *fileindex_path = NULL;
7861 struct report_file_info_arg arg;
7863 err = lock_worktree(worktree, LOCK_SH);
7864 if (err)
7865 return err;
7867 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7868 if (err)
7869 goto done;
7871 arg.worktree = worktree;
7872 arg.info_cb = info_cb;
7873 arg.info_arg = info_arg;
7874 arg.paths = paths;
7875 arg.cancel_cb = cancel_cb;
7876 arg.cancel_arg = cancel_arg;
7877 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7878 &arg);
7879 done:
7880 free(fileindex_path);
7881 if (fileindex)
7882 got_fileindex_free(fileindex);
7883 unlockerr = lock_worktree(worktree, LOCK_UN);
7884 if (unlockerr && err == NULL)
7885 err = unlockerr;
7886 return err;