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;
457 do {
458 err = open_worktree(worktree, path);
459 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
460 return err;
461 if (*worktree)
462 return NULL;
463 path = dirname(path);
464 if (path == NULL)
465 return got_error_from_errno2("dirname", path);
466 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
468 return got_error(GOT_ERR_NOT_WORKTREE);
471 const struct got_error *
472 got_worktree_close(struct got_worktree *worktree)
474 const struct got_error *err = NULL;
475 free(worktree->repo_path);
476 free(worktree->path_prefix);
477 free(worktree->base_commit_id);
478 free(worktree->head_ref_name);
479 if (worktree->lockfd != -1)
480 if (close(worktree->lockfd) != 0)
481 err = got_error_from_errno2("close",
482 got_worktree_get_root_path(worktree));
483 free(worktree->root_path);
484 free(worktree->gotconfig_path);
485 got_gotconfig_free(worktree->gotconfig);
486 free(worktree);
487 return err;
490 const char *
491 got_worktree_get_root_path(struct got_worktree *worktree)
493 return worktree->root_path;
496 const char *
497 got_worktree_get_repo_path(struct got_worktree *worktree)
499 return worktree->repo_path;
501 const char *
502 got_worktree_get_path_prefix(struct got_worktree *worktree)
504 return worktree->path_prefix;
507 const struct got_error *
508 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 const char *path_prefix)
511 char *absprefix = NULL;
513 if (!got_path_is_absolute(path_prefix)) {
514 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 return got_error_from_errno("asprintf");
517 *match = (strcmp(absprefix ? absprefix : path_prefix,
518 worktree->path_prefix) == 0);
519 free(absprefix);
520 return NULL;
523 const char *
524 got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 return worktree->head_ref_name;
529 const struct got_error *
530 got_worktree_set_head_ref(struct got_worktree *worktree,
531 struct got_reference *head_ref)
533 const struct got_error *err = NULL;
534 char *path_got = NULL, *head_ref_name = NULL;
536 if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 GOT_WORKTREE_GOT_DIR) == -1) {
538 err = got_error_from_errno("asprintf");
539 path_got = NULL;
540 goto done;
543 head_ref_name = strdup(got_ref_get_name(head_ref));
544 if (head_ref_name == NULL) {
545 err = got_error_from_errno("strdup");
546 goto done;
549 err = write_head_ref(path_got, head_ref);
550 if (err)
551 goto done;
553 free(worktree->head_ref_name);
554 worktree->head_ref_name = head_ref_name;
555 done:
556 free(path_got);
557 if (err)
558 free(head_ref_name);
559 return err;
562 struct got_object_id *
563 got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 return worktree->base_commit_id;
568 const struct got_error *
569 got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 struct got_repository *repo, struct got_object_id *commit_id)
572 const struct got_error *err;
573 struct got_object *obj = NULL;
574 char *id_str = NULL;
575 char *path_got = NULL;
577 if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 GOT_WORKTREE_GOT_DIR) == -1) {
579 err = got_error_from_errno("asprintf");
580 path_got = NULL;
581 goto done;
584 err = got_object_open(&obj, repo, commit_id);
585 if (err)
586 return err;
588 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 err = got_error(GOT_ERR_OBJ_TYPE);
590 goto done;
593 /* Record our base commit. */
594 err = got_object_id_str(&id_str, commit_id);
595 if (err)
596 goto done;
597 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
598 if (err)
599 goto done;
601 free(worktree->base_commit_id);
602 worktree->base_commit_id = got_object_id_dup(commit_id);
603 if (worktree->base_commit_id == NULL) {
604 err = got_error_from_errno("got_object_id_dup");
605 goto done;
607 done:
608 if (obj)
609 got_object_close(obj);
610 free(id_str);
611 free(path_got);
612 return err;
615 const struct got_gotconfig *
616 got_worktree_get_gotconfig(struct got_worktree *worktree)
618 return worktree->gotconfig;
621 static const struct got_error *
622 lock_worktree(struct got_worktree *worktree, int operation)
624 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
625 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
626 : got_error_from_errno2("flock",
627 got_worktree_get_root_path(worktree)));
628 return NULL;
631 static const struct got_error *
632 add_dir_on_disk(struct got_worktree *worktree, const char *path)
634 const struct got_error *err = NULL;
635 char *abspath;
637 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
638 return got_error_from_errno("asprintf");
640 err = got_path_mkdir(abspath);
641 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
642 struct stat sb;
643 err = NULL;
644 if (lstat(abspath, &sb) == -1) {
645 err = got_error_from_errno2("lstat", abspath);
646 } else if (!S_ISDIR(sb.st_mode)) {
647 /* TODO directory is obstructed; do something */
648 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
651 free(abspath);
652 return err;
655 static const struct got_error *
656 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
658 const struct got_error *err = NULL;
659 uint8_t fbuf1[8192];
660 uint8_t fbuf2[8192];
661 size_t flen1 = 0, flen2 = 0;
663 *same = 1;
665 for (;;) {
666 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
667 if (flen1 == 0 && ferror(f1)) {
668 err = got_error_from_errno("fread");
669 break;
671 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
672 if (flen2 == 0 && ferror(f2)) {
673 err = got_error_from_errno("fread");
674 break;
676 if (flen1 == 0) {
677 if (flen2 != 0)
678 *same = 0;
679 break;
680 } else if (flen2 == 0) {
681 if (flen1 != 0)
682 *same = 0;
683 break;
684 } else if (flen1 == flen2) {
685 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
686 *same = 0;
687 break;
689 } else {
690 *same = 0;
691 break;
695 return err;
698 static const struct got_error *
699 check_files_equal(int *same, const char *f1_path, const char *f2_path)
701 const struct got_error *err = NULL;
702 struct stat sb;
703 size_t size1, size2;
704 FILE *f1 = NULL, *f2 = NULL;
706 *same = 1;
708 if (lstat(f1_path, &sb) != 0) {
709 err = got_error_from_errno2("lstat", f1_path);
710 goto done;
712 size1 = sb.st_size;
714 if (lstat(f2_path, &sb) != 0) {
715 err = got_error_from_errno2("lstat", f2_path);
716 goto done;
718 size2 = sb.st_size;
720 if (size1 != size2) {
721 *same = 0;
722 return NULL;
725 f1 = fopen(f1_path, "r");
726 if (f1 == NULL)
727 return got_error_from_errno2("fopen", f1_path);
729 f2 = fopen(f2_path, "r");
730 if (f2 == NULL) {
731 err = got_error_from_errno2("fopen", f2_path);
732 goto done;
735 err = check_file_contents_equal(same, f1, f2);
736 done:
737 if (f1 && fclose(f1) != 0 && err == NULL)
738 err = got_error_from_errno("fclose");
739 if (f2 && fclose(f2) != 0 && err == NULL)
740 err = got_error_from_errno("fclose");
742 return err;
745 /*
746 * Perform a 3-way merge where blob_orig acts as the common ancestor,
747 * the file at deriv_path acts as the first derived version, and the
748 * file on disk acts as the second derived version.
749 */
750 static const struct got_error *
751 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
752 struct got_blob_object *blob_orig, const char *ondisk_path,
753 const char *path, uint16_t st_mode, const char *deriv_path,
754 const char *label_orig, const char *label_deriv,
755 struct got_repository *repo,
756 got_worktree_checkout_cb progress_cb, void *progress_arg)
758 const struct got_error *err = NULL;
759 int merged_fd = -1;
760 FILE *f_orig = NULL;
761 char *blob_orig_path = NULL;
762 char *merged_path = NULL, *base_path = NULL;
763 int overlapcnt = 0;
764 char *parent;
765 char *symlink_path = NULL;
766 FILE *symlinkf = NULL;
768 *local_changes_subsumed = 0;
770 parent = dirname(ondisk_path);
771 if (parent == NULL)
772 return got_error_from_errno2("dirname", ondisk_path);
774 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
775 return got_error_from_errno("asprintf");
777 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
778 if (err)
779 goto done;
781 free(base_path);
782 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
783 err = got_error_from_errno("asprintf");
784 base_path = NULL;
785 goto done;
788 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
789 if (err)
790 goto done;
791 if (blob_orig) {
792 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
793 blob_orig);
794 if (err)
795 goto done;
796 } else {
797 /*
798 * If the file has no blob, this is an "add vs add" conflict,
799 * and we simply use an empty ancestor file to make both files
800 * appear in the merged result in their entirety.
801 */
804 /*
805 * In order the run a 3-way merge with a symlink we copy the symlink's
806 * target path into a temporary file and use that file with diff3.
807 */
808 if (S_ISLNK(st_mode)) {
809 char target_path[PATH_MAX];
810 ssize_t target_len;
811 size_t n;
813 free(base_path);
814 if (asprintf(&base_path, "%s/got-symlink-merge",
815 parent) == -1) {
816 err = got_error_from_errno("asprintf");
817 base_path = NULL;
818 goto done;
820 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
821 if (err)
822 goto done;
823 target_len = readlink(ondisk_path, target_path,
824 sizeof(target_path));
825 if (target_len == -1) {
826 err = got_error_from_errno2("readlink", ondisk_path);
827 goto done;
829 n = fwrite(target_path, 1, target_len, symlinkf);
830 if (n != target_len) {
831 err = got_ferror(symlinkf, GOT_ERR_IO);
832 goto done;
834 if (fflush(symlinkf) == EOF) {
835 err = got_error_from_errno2("fflush", symlink_path);
836 goto done;
840 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
841 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
842 label_deriv, label_orig, NULL);
843 if (err)
844 goto done;
846 err = (*progress_cb)(progress_arg,
847 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
848 if (err)
849 goto done;
851 if (fsync(merged_fd) != 0) {
852 err = got_error_from_errno("fsync");
853 goto done;
856 /* Check if a clean merge has subsumed all local changes. */
857 if (overlapcnt == 0) {
858 err = check_files_equal(local_changes_subsumed, deriv_path,
859 merged_path);
860 if (err)
861 goto done;
864 if (fchmod(merged_fd, st_mode) != 0) {
865 err = got_error_from_errno2("fchmod", merged_path);
866 goto done;
869 if (rename(merged_path, ondisk_path) != 0) {
870 err = got_error_from_errno3("rename", merged_path,
871 ondisk_path);
872 goto done;
874 done:
875 if (err) {
876 if (merged_path)
877 unlink(merged_path);
879 if (symlink_path) {
880 if (unlink(symlink_path) == -1 && err == NULL)
881 err = got_error_from_errno2("unlink", symlink_path);
883 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
884 err = got_error_from_errno2("fclose", symlink_path);
885 free(symlink_path);
886 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
887 err = got_error_from_errno("close");
888 if (f_orig && fclose(f_orig) != 0 && err == NULL)
889 err = got_error_from_errno("fclose");
890 free(merged_path);
891 free(base_path);
892 if (blob_orig_path) {
893 unlink(blob_orig_path);
894 free(blob_orig_path);
896 return err;
899 static const struct got_error *
900 update_symlink(const char *ondisk_path, const char *target_path,
901 size_t target_len)
903 /* This is not atomic but matches what 'ln -sf' does. */
904 if (unlink(ondisk_path) == -1)
905 return got_error_from_errno2("unlink", ondisk_path);
906 if (symlink(target_path, ondisk_path) == -1)
907 return got_error_from_errno3("symlink", target_path,
908 ondisk_path);
909 return NULL;
912 /*
913 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
914 * in the work tree with a file that contains conflict markers and the
915 * conflicting target paths of the original version, a "derived version"
916 * of a symlink from an incoming change, and a local version of the symlink.
918 * The original versions's target path can be NULL if it is not available,
919 * such as if both derived versions added a new symlink at the same path.
921 * The incoming derived symlink target is NULL in case the incoming change
922 * has deleted this symlink.
923 */
924 static const struct got_error *
925 install_symlink_conflict(const char *deriv_target,
926 struct got_object_id *deriv_base_commit_id, const char *orig_target,
927 const char *label_orig, const char *local_target, const char *ondisk_path)
929 const struct got_error *err;
930 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
931 FILE *f = NULL;
933 err = got_object_id_str(&id_str, deriv_base_commit_id);
934 if (err)
935 return got_error_from_errno("asprintf");
937 if (asprintf(&label_deriv, "%s: commit %s",
938 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
939 err = got_error_from_errno("asprintf");
940 goto done;
943 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
944 if (err)
945 goto done;
947 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
948 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
949 deriv_target ? deriv_target : "(symlink was deleted)",
950 orig_target ? label_orig : "",
951 orig_target ? "\n" : "",
952 orig_target ? orig_target : "",
953 orig_target ? "\n" : "",
954 GOT_DIFF_CONFLICT_MARKER_SEP,
955 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
956 err = got_error_from_errno2("fprintf", path);
957 goto done;
960 if (unlink(ondisk_path) == -1) {
961 err = got_error_from_errno2("unlink", ondisk_path);
962 goto done;
964 if (rename(path, ondisk_path) == -1) {
965 err = got_error_from_errno3("rename", path, ondisk_path);
966 goto done;
968 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
969 err = got_error_from_errno2("chmod", ondisk_path);
970 goto done;
972 done:
973 if (f != NULL && fclose(f) == EOF && err == NULL)
974 err = got_error_from_errno2("fclose", path);
975 free(path);
976 free(id_str);
977 free(label_deriv);
978 return err;
981 /* forward declaration */
982 static const struct got_error *
983 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
984 const char *, const char *, uint16_t, const char *,
985 struct got_blob_object *, struct got_object_id *,
986 struct got_repository *, got_worktree_checkout_cb, void *);
988 /*
989 * Merge a symlink into the work tree, where blob_orig acts as the common
990 * ancestor, deriv_target is the link target of the first derived version,
991 * and the symlink on disk acts as the second derived version.
992 * Assume that contents of both blobs represent symlinks.
993 */
994 static const struct got_error *
995 merge_symlink(struct got_worktree *worktree,
996 struct got_blob_object *blob_orig, const char *ondisk_path,
997 const char *path, const char *label_orig, const char *deriv_target,
998 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
999 got_worktree_checkout_cb progress_cb, void *progress_arg)
1001 const struct got_error *err = NULL;
1002 char *ancestor_target = NULL;
1003 struct stat sb;
1004 ssize_t ondisk_len, deriv_len;
1005 char ondisk_target[PATH_MAX];
1006 int have_local_change = 0;
1007 int have_incoming_change = 0;
1009 if (lstat(ondisk_path, &sb) == -1)
1010 return got_error_from_errno2("lstat", ondisk_path);
1012 ondisk_len = readlink(ondisk_path, ondisk_target,
1013 sizeof(ondisk_target));
1014 if (ondisk_len == -1) {
1015 err = got_error_from_errno2("readlink",
1016 ondisk_path);
1017 goto done;
1019 ondisk_target[ondisk_len] = '\0';
1021 if (blob_orig) {
1022 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1023 if (err)
1024 goto done;
1027 if (ancestor_target == NULL ||
1028 (ondisk_len != strlen(ancestor_target) ||
1029 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1030 have_local_change = 1;
1032 deriv_len = strlen(deriv_target);
1033 if (ancestor_target == NULL ||
1034 (deriv_len != strlen(ancestor_target) ||
1035 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1036 have_incoming_change = 1;
1038 if (!have_local_change && !have_incoming_change) {
1039 if (ancestor_target) {
1040 /* Both sides made the same change. */
1041 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1042 path);
1043 } else if (deriv_len == ondisk_len &&
1044 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1045 /* Both sides added the same symlink. */
1046 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1047 path);
1048 } else {
1049 /* Both sides added symlinks which don't match. */
1050 err = install_symlink_conflict(deriv_target,
1051 deriv_base_commit_id, ancestor_target,
1052 label_orig, ondisk_target, ondisk_path);
1053 if (err)
1054 goto done;
1055 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1056 path);
1058 } else if (!have_local_change && have_incoming_change) {
1059 /* Apply the incoming change. */
1060 err = update_symlink(ondisk_path, deriv_target,
1061 strlen(deriv_target));
1062 if (err)
1063 goto done;
1064 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1065 } else if (have_local_change && have_incoming_change) {
1066 if (deriv_len == ondisk_len &&
1067 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1068 /* Both sides made the same change. */
1069 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1070 path);
1071 } else {
1072 err = install_symlink_conflict(deriv_target,
1073 deriv_base_commit_id, ancestor_target, label_orig,
1074 ondisk_target, ondisk_path);
1075 if (err)
1076 goto done;
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1078 path);
1082 done:
1083 free(ancestor_target);
1084 return err;
1088 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1089 * blob_deriv acts as the first derived version, and the file on disk
1090 * acts as the second derived version.
1092 static const struct got_error *
1093 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1094 struct got_blob_object *blob_orig, const char *ondisk_path,
1095 const char *path, uint16_t st_mode, const char *label_orig,
1096 struct got_blob_object *blob_deriv,
1097 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1098 got_worktree_checkout_cb progress_cb, void *progress_arg)
1100 const struct got_error *err = NULL;
1101 FILE *f_deriv = NULL;
1102 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1103 char *label_deriv = NULL, *parent;
1105 *local_changes_subsumed = 0;
1107 parent = dirname(ondisk_path);
1108 if (parent == NULL)
1109 return got_error_from_errno2("dirname", ondisk_path);
1111 free(base_path);
1112 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1113 err = got_error_from_errno("asprintf");
1114 base_path = NULL;
1115 goto done;
1118 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1119 if (err)
1120 goto done;
1121 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1122 blob_deriv);
1123 if (err)
1124 goto done;
1126 err = got_object_id_str(&id_str, deriv_base_commit_id);
1127 if (err)
1128 goto done;
1129 if (asprintf(&label_deriv, "%s: commit %s",
1130 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1131 err = got_error_from_errno("asprintf");
1132 goto done;
1135 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1136 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1137 label_deriv, repo, progress_cb, progress_arg);
1138 done:
1139 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1140 err = got_error_from_errno("fclose");
1141 free(base_path);
1142 if (blob_deriv_path) {
1143 unlink(blob_deriv_path);
1144 free(blob_deriv_path);
1146 free(id_str);
1147 free(label_deriv);
1148 return err;
1151 static const struct got_error *
1152 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1153 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1154 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1156 const struct got_error *err = NULL;
1157 struct got_fileindex_entry *new_ie;
1159 *new_iep = NULL;
1161 err = got_fileindex_entry_alloc(&new_ie, path);
1162 if (err)
1163 return err;
1165 err = got_fileindex_entry_update(new_ie, ondisk_path,
1166 blob_id->sha1, base_commit_id->sha1, 1);
1167 if (err)
1168 goto done;
1170 err = got_fileindex_entry_add(fileindex, new_ie);
1171 done:
1172 if (err)
1173 got_fileindex_entry_free(new_ie);
1174 else
1175 *new_iep = new_ie;
1176 return err;
1179 static mode_t
1180 get_ondisk_perms(int executable, mode_t st_mode)
1182 mode_t xbits = S_IXUSR;
1184 if (executable) {
1185 /* Map read bits to execute bits. */
1186 if (st_mode & S_IRGRP)
1187 xbits |= S_IXGRP;
1188 if (st_mode & S_IROTH)
1189 xbits |= S_IXOTH;
1190 return st_mode | xbits;
1193 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1196 /* forward declaration */
1197 static const struct got_error *
1198 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1199 const char *path, mode_t te_mode, mode_t st_mode,
1200 struct got_blob_object *blob, int restoring_missing_file,
1201 int reverting_versioned_file, int installing_bad_symlink,
1202 int path_is_unversioned, struct got_repository *repo,
1203 got_worktree_checkout_cb progress_cb, void *progress_arg);
1206 * This function assumes that the provided symlink target points at a
1207 * safe location in the work tree!
1209 static const struct got_error *
1210 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1211 size_t target_len)
1213 const struct got_error *err = NULL;
1214 ssize_t elen;
1215 char etarget[PATH_MAX];
1216 int fd;
1219 * "Bad" symlinks (those pointing outside the work tree or into the
1220 * .got directory) are installed in the work tree as a regular file
1221 * which contains the bad symlink target path.
1222 * The new symlink target has already been checked for safety by our
1223 * caller. If we can successfully open a regular file then we simply
1224 * replace this file with a symlink below.
1226 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1227 if (fd == -1) {
1228 if (errno != ELOOP)
1229 return got_error_from_errno2("open", ondisk_path);
1231 /* We are updating an existing on-disk symlink. */
1232 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1233 if (elen == -1)
1234 return got_error_from_errno2("readlink", ondisk_path);
1236 if (elen == target_len &&
1237 memcmp(etarget, target_path, target_len) == 0)
1238 return NULL; /* nothing to do */
1241 err = update_symlink(ondisk_path, target_path, target_len);
1242 if (fd != -1 && close(fd) == -1 && err == NULL)
1243 err = got_error_from_errno2("close", ondisk_path);
1244 return err;
1247 static const struct got_error *
1248 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1249 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1251 const struct got_error *err = NULL;
1252 char canonpath[PATH_MAX];
1253 char *path_got = NULL;
1255 *is_bad_symlink = 0;
1257 if (target_len >= sizeof(canonpath)) {
1258 *is_bad_symlink = 1;
1259 return NULL;
1263 * We do not use realpath(3) to resolve the symlink's target
1264 * path because we don't want to resolve symlinks recursively.
1265 * Instead we make the path absolute and then canonicalize it.
1266 * Relative symlink target lookup should begin at the directory
1267 * in which the blob object is being installed.
1269 if (!got_path_is_absolute(target_path)) {
1270 char *abspath;
1271 char *parent = dirname(ondisk_path);
1272 if (parent == NULL)
1273 return got_error_from_errno2("dirname", ondisk_path);
1274 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1275 return got_error_from_errno("asprintf");
1276 if (strlen(abspath) >= sizeof(canonpath)) {
1277 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1278 free(abspath);
1279 return err;
1281 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1282 free(abspath);
1283 if (err)
1284 return err;
1285 } else {
1286 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1287 if (err)
1288 return err;
1291 /* Only allow symlinks pointing at paths within the work tree. */
1292 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1293 *is_bad_symlink = 1;
1294 return NULL;
1297 /* Do not allow symlinks pointing into the .got directory. */
1298 if (asprintf(&path_got, "%s/%s", wtroot_path,
1299 GOT_WORKTREE_GOT_DIR) == -1)
1300 return got_error_from_errno("asprintf");
1301 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1302 *is_bad_symlink = 1;
1304 free(path_got);
1305 return NULL;
1308 static const struct got_error *
1309 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1310 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1311 int restoring_missing_file, int reverting_versioned_file,
1312 int path_is_unversioned, struct got_repository *repo,
1313 got_worktree_checkout_cb progress_cb, void *progress_arg)
1315 const struct got_error *err = NULL;
1316 char target_path[PATH_MAX];
1317 size_t len, target_len = 0;
1318 char *path_got = NULL;
1319 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1320 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1322 *is_bad_symlink = 0;
1325 * Blob object content specifies the target path of the link.
1326 * If a symbolic link cannot be installed we instead create
1327 * a regular file which contains the link target path stored
1328 * in the blob object.
1330 do {
1331 err = got_object_blob_read_block(&len, blob);
1332 if (len + target_len >= sizeof(target_path)) {
1333 /* Path too long; install as a regular file. */
1334 *is_bad_symlink = 1;
1335 got_object_blob_rewind(blob);
1336 return install_blob(worktree, ondisk_path, path,
1337 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1338 restoring_missing_file, reverting_versioned_file,
1339 1, path_is_unversioned, repo, progress_cb,
1340 progress_arg);
1342 if (len > 0) {
1343 /* Skip blob object header first time around. */
1344 memcpy(target_path + target_len, buf + hdrlen,
1345 len - hdrlen);
1346 target_len += len - hdrlen;
1347 hdrlen = 0;
1349 } while (len != 0);
1350 target_path[target_len] = '\0';
1352 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1353 ondisk_path, worktree->root_path);
1354 if (err)
1355 return err;
1357 if (*is_bad_symlink) {
1358 /* install as a regular file */
1359 *is_bad_symlink = 1;
1360 got_object_blob_rewind(blob);
1361 err = install_blob(worktree, ondisk_path, path,
1362 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 restoring_missing_file, reverting_versioned_file, 1,
1364 path_is_unversioned, repo, progress_cb, progress_arg);
1365 goto done;
1368 if (symlink(target_path, ondisk_path) == -1) {
1369 if (errno == EEXIST) {
1370 if (path_is_unversioned) {
1371 err = (*progress_cb)(progress_arg,
1372 GOT_STATUS_UNVERSIONED, path);
1373 goto done;
1375 err = replace_existing_symlink(ondisk_path,
1376 target_path, target_len);
1377 if (err)
1378 goto done;
1379 if (progress_cb) {
1380 err = (*progress_cb)(progress_arg,
1381 reverting_versioned_file ?
1382 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1383 path);
1385 goto done; /* Nothing else to do. */
1388 if (errno == ENOENT) {
1389 char *parent = dirname(ondisk_path);
1390 if (parent == NULL) {
1391 err = got_error_from_errno2("dirname",
1392 ondisk_path);
1393 goto done;
1395 err = add_dir_on_disk(worktree, parent);
1396 if (err)
1397 goto done;
1399 * Retry, and fall through to error handling
1400 * below if this second attempt fails.
1402 if (symlink(target_path, ondisk_path) != -1) {
1403 err = NULL; /* success */
1404 goto done;
1408 /* Handle errors from first or second creation attempt. */
1409 if (errno == ENAMETOOLONG) {
1410 /* bad target path; install as a regular file */
1411 *is_bad_symlink = 1;
1412 got_object_blob_rewind(blob);
1413 err = install_blob(worktree, ondisk_path, path,
1414 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 restoring_missing_file, reverting_versioned_file, 1,
1416 path_is_unversioned, repo,
1417 progress_cb, progress_arg);
1418 } else if (errno == ENOTDIR) {
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1421 } else {
1422 err = got_error_from_errno3("symlink",
1423 target_path, ondisk_path);
1425 } else if (progress_cb)
1426 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1427 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1428 done:
1429 free(path_got);
1430 return err;
1433 static const struct got_error *
1434 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1435 const char *path, mode_t te_mode, mode_t st_mode,
1436 struct got_blob_object *blob, int restoring_missing_file,
1437 int reverting_versioned_file, int installing_bad_symlink,
1438 int path_is_unversioned, struct got_repository *repo,
1439 got_worktree_checkout_cb progress_cb, void *progress_arg)
1441 const struct got_error *err = NULL;
1442 int fd = -1;
1443 size_t len, hdrlen;
1444 int update = 0;
1445 char *tmppath = NULL;
1447 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 GOT_DEFAULT_FILE_MODE);
1449 if (fd == -1) {
1450 if (errno == ENOENT) {
1451 char *parent = dirname(path);
1452 if (parent == NULL)
1453 return got_error_from_errno2("dirname", path);
1454 err = add_dir_on_disk(worktree, parent);
1455 if (err)
1456 return err;
1457 fd = open(ondisk_path,
1458 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1459 GOT_DEFAULT_FILE_MODE);
1460 if (fd == -1)
1461 return got_error_from_errno2("open",
1462 ondisk_path);
1463 } else if (errno == EEXIST) {
1464 if (path_is_unversioned) {
1465 err = (*progress_cb)(progress_arg,
1466 GOT_STATUS_UNVERSIONED, path);
1467 goto done;
1469 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1470 /* TODO file is obstructed; do something */
1471 err = got_error_path(ondisk_path,
1472 GOT_ERR_FILE_OBSTRUCTED);
1473 goto done;
1474 } else {
1475 err = got_opentemp_named_fd(&tmppath, &fd,
1476 ondisk_path);
1477 if (err)
1478 goto done;
1479 update = 1;
1481 } else
1482 return got_error_from_errno2("open", ondisk_path);
1485 if (progress_cb) {
1486 if (restoring_missing_file)
1487 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1488 path);
1489 else if (reverting_versioned_file)
1490 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1491 path);
1492 else
1493 err = (*progress_cb)(progress_arg,
1494 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1495 if (err)
1496 goto done;
1499 hdrlen = got_object_blob_get_hdrlen(blob);
1500 do {
1501 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1502 err = got_object_blob_read_block(&len, blob);
1503 if (err)
1504 break;
1505 if (len > 0) {
1506 /* Skip blob object header first time around. */
1507 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1508 if (outlen == -1) {
1509 err = got_error_from_errno("write");
1510 goto done;
1511 } else if (outlen != len - hdrlen) {
1512 err = got_error(GOT_ERR_IO);
1513 goto done;
1515 hdrlen = 0;
1517 } while (len != 0);
1519 if (fsync(fd) != 0) {
1520 err = got_error_from_errno("fsync");
1521 goto done;
1524 if (update) {
1525 if (rename(tmppath, ondisk_path) != 0) {
1526 err = got_error_from_errno3("rename", tmppath,
1527 ondisk_path);
1528 unlink(tmppath);
1529 goto done;
1533 if (chmod(ondisk_path,
1534 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1535 err = got_error_from_errno2("chmod", ondisk_path);
1536 goto done;
1539 done:
1540 if (fd != -1 && close(fd) != 0 && err == NULL)
1541 err = got_error_from_errno("close");
1542 free(tmppath);
1543 return err;
1546 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1547 static const struct got_error *
1548 get_modified_file_content_status(unsigned char *status, FILE *f)
1550 const struct got_error *err = NULL;
1551 const char *markers[3] = {
1552 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1553 GOT_DIFF_CONFLICT_MARKER_SEP,
1554 GOT_DIFF_CONFLICT_MARKER_END
1556 int i = 0;
1557 char *line;
1558 size_t len;
1559 const char delim[3] = {'\0', '\0', '\0'};
1561 while (*status == GOT_STATUS_MODIFY) {
1562 line = fparseln(f, &len, NULL, delim, 0);
1563 if (line == NULL) {
1564 if (feof(f))
1565 break;
1566 err = got_ferror(f, GOT_ERR_IO);
1567 break;
1570 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1571 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1572 == 0)
1573 *status = GOT_STATUS_CONFLICT;
1574 else
1575 i++;
1579 return err;
1582 static int
1583 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1585 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1586 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1589 static int
1590 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1592 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1593 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1594 ie->mtime_sec == sb->st_mtim.tv_sec &&
1595 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1596 ie->size == (sb->st_size & 0xffffffff) &&
1597 !xbit_differs(ie, sb->st_mode));
1600 static unsigned char
1601 get_staged_status(struct got_fileindex_entry *ie)
1603 switch (got_fileindex_entry_stage_get(ie)) {
1604 case GOT_FILEIDX_STAGE_ADD:
1605 return GOT_STATUS_ADD;
1606 case GOT_FILEIDX_STAGE_DELETE:
1607 return GOT_STATUS_DELETE;
1608 case GOT_FILEIDX_STAGE_MODIFY:
1609 return GOT_STATUS_MODIFY;
1610 default:
1611 return GOT_STATUS_NO_CHANGE;
1615 static const struct got_error *
1616 get_symlink_modification_status(unsigned char *status,
1617 struct got_fileindex_entry *ie, const char *abspath,
1618 int dirfd, const char *de_name, struct got_blob_object *blob)
1620 const struct got_error *err = NULL;
1621 char target_path[PATH_MAX];
1622 char etarget[PATH_MAX];
1623 ssize_t elen;
1624 size_t len, target_len = 0;
1625 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1626 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1628 *status = GOT_STATUS_NO_CHANGE;
1630 /* Blob object content specifies the target path of the link. */
1631 do {
1632 err = got_object_blob_read_block(&len, blob);
1633 if (err)
1634 return err;
1635 if (len + target_len >= sizeof(target_path)) {
1637 * Should not happen. The blob contents were OK
1638 * when this symlink was installed.
1640 return got_error(GOT_ERR_NO_SPACE);
1642 if (len > 0) {
1643 /* Skip blob object header first time around. */
1644 memcpy(target_path + target_len, buf + hdrlen,
1645 len - hdrlen);
1646 target_len += len - hdrlen;
1647 hdrlen = 0;
1649 } while (len != 0);
1650 target_path[target_len] = '\0';
1652 if (dirfd != -1) {
1653 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1654 if (elen == -1)
1655 return got_error_from_errno2("readlinkat", abspath);
1656 } else {
1657 elen = readlink(abspath, etarget, sizeof(etarget));
1658 if (elen == -1)
1659 return got_error_from_errno2("readlink", abspath);
1662 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1663 *status = GOT_STATUS_MODIFY;
1665 return NULL;
1668 static const struct got_error *
1669 get_file_status(unsigned char *status, struct stat *sb,
1670 struct got_fileindex_entry *ie, const char *abspath,
1671 int dirfd, const char *de_name, struct got_repository *repo)
1673 const struct got_error *err = NULL;
1674 struct got_object_id id;
1675 size_t hdrlen;
1676 int fd = -1;
1677 FILE *f = NULL;
1678 uint8_t fbuf[8192];
1679 struct got_blob_object *blob = NULL;
1680 size_t flen, blen;
1681 unsigned char staged_status = get_staged_status(ie);
1683 *status = GOT_STATUS_NO_CHANGE;
1686 * Whenever the caller provides a directory descriptor and a
1687 * directory entry name for the file, use them! This prevents
1688 * race conditions if filesystem paths change beneath our feet.
1690 if (dirfd != -1) {
1691 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1692 if (errno == ENOENT) {
1693 if (got_fileindex_entry_has_file_on_disk(ie))
1694 *status = GOT_STATUS_MISSING;
1695 else
1696 *status = GOT_STATUS_DELETE;
1697 goto done;
1699 err = got_error_from_errno2("fstatat", abspath);
1700 goto done;
1702 } else {
1703 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1704 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1705 return got_error_from_errno2("open", abspath);
1706 else if (fd == -1 && errno == ELOOP) {
1707 if (lstat(abspath, sb) == -1)
1708 return got_error_from_errno2("lstat", abspath);
1709 } else if (fd == -1 || fstat(fd, sb) == -1) {
1710 if (errno == ENOENT) {
1711 if (got_fileindex_entry_has_file_on_disk(ie))
1712 *status = GOT_STATUS_MISSING;
1713 else
1714 *status = GOT_STATUS_DELETE;
1715 goto done;
1717 err = got_error_from_errno2("fstat", abspath);
1718 goto done;
1722 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1723 *status = GOT_STATUS_OBSTRUCTED;
1724 goto done;
1727 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1728 *status = GOT_STATUS_DELETE;
1729 goto done;
1730 } else if (!got_fileindex_entry_has_blob(ie) &&
1731 staged_status != GOT_STATUS_ADD) {
1732 *status = GOT_STATUS_ADD;
1733 goto done;
1736 if (!stat_info_differs(ie, sb))
1737 goto done;
1739 if (S_ISLNK(sb->st_mode) &&
1740 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1741 *status = GOT_STATUS_MODIFY;
1742 goto done;
1745 if (staged_status == GOT_STATUS_MODIFY ||
1746 staged_status == GOT_STATUS_ADD)
1747 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1748 else
1749 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1751 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1752 if (err)
1753 goto done;
1755 if (S_ISLNK(sb->st_mode)) {
1756 err = get_symlink_modification_status(status, ie,
1757 abspath, dirfd, de_name, blob);
1758 goto done;
1761 if (dirfd != -1) {
1762 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1763 if (fd == -1) {
1764 err = got_error_from_errno2("openat", abspath);
1765 goto done;
1769 f = fdopen(fd, "r");
1770 if (f == NULL) {
1771 err = got_error_from_errno2("fdopen", abspath);
1772 goto done;
1774 fd = -1;
1775 hdrlen = got_object_blob_get_hdrlen(blob);
1776 for (;;) {
1777 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1778 err = got_object_blob_read_block(&blen, blob);
1779 if (err)
1780 goto done;
1781 /* Skip length of blob object header first time around. */
1782 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1783 if (flen == 0 && ferror(f)) {
1784 err = got_error_from_errno("fread");
1785 goto done;
1787 if (blen - hdrlen == 0) {
1788 if (flen != 0)
1789 *status = GOT_STATUS_MODIFY;
1790 break;
1791 } else if (flen == 0) {
1792 if (blen - hdrlen != 0)
1793 *status = GOT_STATUS_MODIFY;
1794 break;
1795 } else if (blen - hdrlen == flen) {
1796 /* Skip blob object header first time around. */
1797 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1798 *status = GOT_STATUS_MODIFY;
1799 break;
1801 } else {
1802 *status = GOT_STATUS_MODIFY;
1803 break;
1805 hdrlen = 0;
1808 if (*status == GOT_STATUS_MODIFY) {
1809 rewind(f);
1810 err = get_modified_file_content_status(status, f);
1811 } else if (xbit_differs(ie, sb->st_mode))
1812 *status = GOT_STATUS_MODE_CHANGE;
1813 done:
1814 if (blob)
1815 got_object_blob_close(blob);
1816 if (f != NULL && fclose(f) == EOF && err == NULL)
1817 err = got_error_from_errno2("fclose", abspath);
1818 if (fd != -1 && close(fd) == -1 && err == NULL)
1819 err = got_error_from_errno2("close", abspath);
1820 return err;
1824 * Update timestamps in the file index if a file is unmodified and
1825 * we had to run a full content comparison to find out.
1827 static const struct got_error *
1828 sync_timestamps(char *ondisk_path, unsigned char status,
1829 struct got_fileindex_entry *ie, struct stat *sb)
1831 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1832 return got_fileindex_entry_update(ie, ondisk_path,
1833 ie->blob_sha1, ie->commit_sha1, 1);
1835 return NULL;
1838 static const struct got_error *
1839 update_blob(struct got_worktree *worktree,
1840 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1841 struct got_tree_entry *te, const char *path,
1842 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1843 void *progress_arg)
1845 const struct got_error *err = NULL;
1846 struct got_blob_object *blob = NULL;
1847 char *ondisk_path;
1848 unsigned char status = GOT_STATUS_NO_CHANGE;
1849 struct stat sb;
1851 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1852 return got_error_from_errno("asprintf");
1854 if (ie) {
1855 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1856 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1857 goto done;
1859 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1860 repo);
1861 if (err)
1862 goto done;
1863 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1864 sb.st_mode = got_fileindex_perms_to_st(ie);
1865 } else {
1866 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1867 status = GOT_STATUS_UNVERSIONED;
1870 if (status == GOT_STATUS_OBSTRUCTED) {
1871 err = (*progress_cb)(progress_arg, status, path);
1872 goto done;
1874 if (status == GOT_STATUS_CONFLICT) {
1875 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1876 path);
1877 goto done;
1880 if (ie && status != GOT_STATUS_MISSING &&
1881 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1882 if (got_fileindex_entry_has_commit(ie) &&
1883 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1884 SHA1_DIGEST_LENGTH) == 0) {
1885 err = sync_timestamps(ondisk_path, status, ie, &sb);
1886 if (err)
1887 goto done;
1888 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1889 path);
1890 goto done;
1892 if (got_fileindex_entry_has_blob(ie) &&
1893 memcmp(ie->blob_sha1, te->id.sha1,
1894 SHA1_DIGEST_LENGTH) == 0) {
1895 err = sync_timestamps(ondisk_path, status, ie, &sb);
1896 goto done;
1900 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1901 if (err)
1902 goto done;
1904 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 int update_timestamps;
1906 struct got_blob_object *blob2 = NULL;
1907 char *label_orig = NULL;
1908 if (got_fileindex_entry_has_blob(ie)) {
1909 struct got_object_id id2;
1910 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1911 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1912 if (err)
1913 goto done;
1915 if (got_fileindex_entry_has_commit(ie)) {
1916 char id_str[SHA1_DIGEST_STRING_LENGTH];
1917 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1918 sizeof(id_str)) == NULL) {
1919 err = got_error_path(id_str,
1920 GOT_ERR_BAD_OBJ_ID_STR);
1921 goto done;
1923 if (asprintf(&label_orig, "%s: commit %s",
1924 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1925 err = got_error_from_errno("asprintf");
1926 goto done;
1929 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1930 char *link_target;
1931 err = got_object_blob_read_to_str(&link_target, blob);
1932 if (err)
1933 goto done;
1934 err = merge_symlink(worktree, blob2, ondisk_path, path,
1935 label_orig, link_target, worktree->base_commit_id,
1936 repo, progress_cb, progress_arg);
1937 free(link_target);
1938 } else {
1939 err = merge_blob(&update_timestamps, worktree, blob2,
1940 ondisk_path, path, sb.st_mode, label_orig, blob,
1941 worktree->base_commit_id, repo,
1942 progress_cb, progress_arg);
1944 free(label_orig);
1945 if (blob2)
1946 got_object_blob_close(blob2);
1947 if (err)
1948 goto done;
1950 * Do not update timestamps of files with local changes.
1951 * Otherwise, a future status walk would treat them as
1952 * unmodified files again.
1954 err = got_fileindex_entry_update(ie, ondisk_path,
1955 blob->id.sha1, worktree->base_commit_id->sha1,
1956 update_timestamps);
1957 } else if (status == GOT_STATUS_MODE_CHANGE) {
1958 err = got_fileindex_entry_update(ie, ondisk_path,
1959 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1960 } else if (status == GOT_STATUS_DELETE) {
1961 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1962 if (err)
1963 goto done;
1964 err = got_fileindex_entry_update(ie, ondisk_path,
1965 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1966 if (err)
1967 goto done;
1968 } else {
1969 int is_bad_symlink = 0;
1970 if (S_ISLNK(te->mode)) {
1971 err = install_symlink(&is_bad_symlink, worktree,
1972 ondisk_path, path, blob,
1973 status == GOT_STATUS_MISSING, 0,
1974 status == GOT_STATUS_UNVERSIONED, repo,
1975 progress_cb, progress_arg);
1976 } else {
1977 err = install_blob(worktree, ondisk_path, path,
1978 te->mode, sb.st_mode, blob,
1979 status == GOT_STATUS_MISSING, 0, 0,
1980 status == GOT_STATUS_UNVERSIONED, repo,
1981 progress_cb, progress_arg);
1983 if (err)
1984 goto done;
1986 if (ie) {
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1989 } else {
1990 err = create_fileindex_entry(&ie, fileindex,
1991 worktree->base_commit_id, ondisk_path, path,
1992 &blob->id);
1994 if (err)
1995 goto done;
1997 if (is_bad_symlink) {
1998 got_fileindex_entry_filetype_set(ie,
1999 GOT_FILEIDX_MODE_BAD_SYMLINK);
2002 got_object_blob_close(blob);
2003 done:
2004 free(ondisk_path);
2005 return err;
2008 static const struct got_error *
2009 remove_ondisk_file(const char *root_path, const char *path)
2011 const struct got_error *err = NULL;
2012 char *ondisk_path = NULL;
2014 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2015 return got_error_from_errno("asprintf");
2017 if (unlink(ondisk_path) == -1) {
2018 if (errno != ENOENT)
2019 err = got_error_from_errno2("unlink", ondisk_path);
2020 } else {
2021 char *parent = dirname(ondisk_path);
2022 while (parent && strcmp(parent, root_path) != 0) {
2023 if (rmdir(parent) == -1) {
2024 if (errno != ENOTEMPTY)
2025 err = got_error_from_errno2("rmdir",
2026 parent);
2027 break;
2029 parent = dirname(parent);
2032 free(ondisk_path);
2033 return err;
2036 static const struct got_error *
2037 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2038 struct got_fileindex_entry *ie, struct got_repository *repo,
2039 got_worktree_checkout_cb progress_cb, void *progress_arg)
2041 const struct got_error *err = NULL;
2042 unsigned char status;
2043 struct stat sb;
2044 char *ondisk_path;
2046 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2047 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2049 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2050 == -1)
2051 return got_error_from_errno("asprintf");
2053 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2054 if (err)
2055 goto done;
2057 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2058 char ondisk_target[PATH_MAX];
2059 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2060 sizeof(ondisk_target));
2061 if (ondisk_len == -1) {
2062 err = got_error_from_errno2("readlink", ondisk_path);
2063 goto done;
2065 ondisk_target[ondisk_len] = '\0';
2066 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2067 NULL, NULL, /* XXX pass common ancestor info? */
2068 ondisk_target, ondisk_path);
2069 if (err)
2070 goto done;
2071 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2072 ie->path);
2073 goto done;
2076 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2077 status == GOT_STATUS_ADD) {
2078 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2079 if (err)
2080 goto done;
2082 * Preserve the working file and change the deleted blob's
2083 * entry into a schedule-add entry.
2085 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2086 0);
2087 } else {
2088 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2089 if (err)
2090 goto done;
2091 if (status == GOT_STATUS_NO_CHANGE) {
2092 err = remove_ondisk_file(worktree->root_path, ie->path);
2093 if (err)
2094 goto done;
2096 got_fileindex_entry_remove(fileindex, ie);
2098 done:
2099 free(ondisk_path);
2100 return err;
2103 struct diff_cb_arg {
2104 struct got_fileindex *fileindex;
2105 struct got_worktree *worktree;
2106 struct got_repository *repo;
2107 got_worktree_checkout_cb progress_cb;
2108 void *progress_arg;
2109 got_cancel_cb cancel_cb;
2110 void *cancel_arg;
2113 static const struct got_error *
2114 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2115 struct got_tree_entry *te, const char *parent_path)
2117 struct diff_cb_arg *a = arg;
2119 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 return got_error(GOT_ERR_CANCELLED);
2122 return update_blob(a->worktree, a->fileindex, ie, te,
2123 ie->path, a->repo, a->progress_cb, a->progress_arg);
2126 static const struct got_error *
2127 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2129 struct diff_cb_arg *a = arg;
2131 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2132 return got_error(GOT_ERR_CANCELLED);
2134 return delete_blob(a->worktree, a->fileindex, ie,
2135 a->repo, a->progress_cb, a->progress_arg);
2138 static const struct got_error *
2139 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2141 struct diff_cb_arg *a = arg;
2142 const struct got_error *err;
2143 char *path;
2145 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 return got_error(GOT_ERR_CANCELLED);
2148 if (got_object_tree_entry_is_submodule(te))
2149 return NULL;
2151 if (asprintf(&path, "%s%s%s", parent_path,
2152 parent_path[0] ? "/" : "", te->name)
2153 == -1)
2154 return got_error_from_errno("asprintf");
2156 if (S_ISDIR(te->mode))
2157 err = add_dir_on_disk(a->worktree, path);
2158 else
2159 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2160 a->repo, a->progress_cb, a->progress_arg);
2162 free(path);
2163 return err;
2166 const struct got_error *
2167 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2169 uint32_t uuid_status;
2171 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2172 if (uuid_status != uuid_s_ok) {
2173 *uuidstr = NULL;
2174 return got_error_uuid(uuid_status, "uuid_to_string");
2177 return NULL;
2180 static const struct got_error *
2181 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2183 const struct got_error *err = NULL;
2184 char *uuidstr = NULL;
2186 *refname = NULL;
2188 err = got_worktree_get_uuid(&uuidstr, worktree);
2189 if (err)
2190 return err;
2192 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2193 err = got_error_from_errno("asprintf");
2194 *refname = NULL;
2196 free(uuidstr);
2197 return err;
2200 const struct got_error *
2201 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2203 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2206 static const struct got_error *
2207 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 return get_ref_name(refname, worktree,
2210 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2213 static const struct got_error *
2214 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2216 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2219 static const struct got_error *
2220 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2222 return get_ref_name(refname, worktree,
2223 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2226 static const struct got_error *
2227 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 return get_ref_name(refname, worktree,
2230 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2233 static const struct got_error *
2234 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 return get_ref_name(refname, worktree,
2237 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2240 static const struct got_error *
2241 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 return get_ref_name(refname, worktree,
2244 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2247 static const struct got_error *
2248 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 return get_ref_name(refname, worktree,
2251 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2254 static const struct got_error *
2255 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree,
2258 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2261 const struct got_error *
2262 got_worktree_get_histedit_script_path(char **path,
2263 struct got_worktree *worktree)
2265 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2266 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2267 *path = NULL;
2268 return got_error_from_errno("asprintf");
2270 return NULL;
2274 * Prevent Git's garbage collector from deleting our base commit by
2275 * setting a reference to our base commit's ID.
2277 static const struct got_error *
2278 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2280 const struct got_error *err = NULL;
2281 struct got_reference *ref = NULL;
2282 char *refname;
2284 err = got_worktree_get_base_ref_name(&refname, worktree);
2285 if (err)
2286 return err;
2288 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2289 if (err)
2290 goto done;
2292 err = got_ref_write(ref, repo);
2293 done:
2294 free(refname);
2295 if (ref)
2296 got_ref_close(ref);
2297 return err;
2300 static const struct got_error *
2301 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2303 const struct got_error *err = NULL;
2305 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2306 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2307 err = got_error_from_errno("asprintf");
2308 *fileindex_path = NULL;
2310 return err;
2314 static const struct got_error *
2315 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2316 struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2319 FILE *index = NULL;
2321 *fileindex_path = NULL;
2322 *fileindex = got_fileindex_alloc();
2323 if (*fileindex == NULL)
2324 return got_error_from_errno("got_fileindex_alloc");
2326 err = get_fileindex_path(fileindex_path, worktree);
2327 if (err)
2328 goto done;
2330 index = fopen(*fileindex_path, "rb");
2331 if (index == NULL) {
2332 if (errno != ENOENT)
2333 err = got_error_from_errno2("fopen", *fileindex_path);
2334 } else {
2335 err = got_fileindex_read(*fileindex, index);
2336 if (fclose(index) != 0 && err == NULL)
2337 err = got_error_from_errno("fclose");
2339 done:
2340 if (err) {
2341 free(*fileindex_path);
2342 *fileindex_path = NULL;
2343 got_fileindex_free(*fileindex);
2344 *fileindex = NULL;
2346 return err;
2349 struct bump_base_commit_id_arg {
2350 struct got_object_id *base_commit_id;
2351 const char *path;
2352 size_t path_len;
2353 const char *entry_name;
2354 got_worktree_checkout_cb progress_cb;
2355 void *progress_arg;
2358 /* Bump base commit ID of all files within an updated part of the work tree. */
2359 static const struct got_error *
2360 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2362 const struct got_error *err;
2363 struct bump_base_commit_id_arg *a = arg;
2365 if (a->entry_name) {
2366 if (strcmp(ie->path, a->path) != 0)
2367 return NULL;
2368 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2369 return NULL;
2371 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2372 SHA1_DIGEST_LENGTH) == 0)
2373 return NULL;
2375 if (a->progress_cb) {
2376 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2377 ie->path);
2378 if (err)
2379 return err;
2381 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2382 return NULL;
2385 static const struct got_error *
2386 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2388 const struct got_error *err = NULL;
2389 char *new_fileindex_path = NULL;
2390 FILE *new_index = NULL;
2391 struct timespec timeout;
2393 err = got_opentemp_named(&new_fileindex_path, &new_index,
2394 fileindex_path);
2395 if (err)
2396 goto done;
2398 err = got_fileindex_write(fileindex, new_index);
2399 if (err)
2400 goto done;
2402 if (rename(new_fileindex_path, fileindex_path) != 0) {
2403 err = got_error_from_errno3("rename", new_fileindex_path,
2404 fileindex_path);
2405 unlink(new_fileindex_path);
2409 * Sleep for a short amount of time to ensure that files modified after
2410 * this program exits have a different time stamp from the one which
2411 * was recorded in the file index.
2413 timeout.tv_sec = 0;
2414 timeout.tv_nsec = 1;
2415 nanosleep(&timeout, NULL);
2416 done:
2417 if (new_index)
2418 fclose(new_index);
2419 free(new_fileindex_path);
2420 return err;
2423 static const struct got_error *
2424 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2425 struct got_object_id **tree_id, const char *wt_relpath,
2426 struct got_worktree *worktree, struct got_repository *repo)
2428 const struct got_error *err = NULL;
2429 struct got_object_id *id = NULL;
2430 char *in_repo_path = NULL;
2431 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2433 *entry_type = GOT_OBJ_TYPE_ANY;
2434 *tree_relpath = NULL;
2435 *tree_id = NULL;
2437 if (wt_relpath[0] == '\0') {
2438 /* Check out all files within the work tree. */
2439 *entry_type = GOT_OBJ_TYPE_TREE;
2440 *tree_relpath = strdup("");
2441 if (*tree_relpath == NULL) {
2442 err = got_error_from_errno("strdup");
2443 goto done;
2445 err = got_object_id_by_path(tree_id, repo,
2446 worktree->base_commit_id, worktree->path_prefix);
2447 if (err)
2448 goto done;
2449 return NULL;
2452 /* Check out a subset of files in the work tree. */
2454 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2455 is_root_wt ? "" : "/", wt_relpath) == -1) {
2456 err = got_error_from_errno("asprintf");
2457 goto done;
2460 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2461 in_repo_path);
2462 if (err)
2463 goto done;
2465 free(in_repo_path);
2466 in_repo_path = NULL;
2468 err = got_object_get_type(entry_type, repo, id);
2469 if (err)
2470 goto done;
2472 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2473 /* Check out a single file. */
2474 if (strchr(wt_relpath, '/') == NULL) {
2475 /* Check out a single file in work tree's root dir. */
2476 in_repo_path = strdup(worktree->path_prefix);
2477 if (in_repo_path == NULL) {
2478 err = got_error_from_errno("strdup");
2479 goto done;
2481 *tree_relpath = strdup("");
2482 if (*tree_relpath == NULL) {
2483 err = got_error_from_errno("strdup");
2484 goto done;
2486 } else {
2487 /* Check out a single file in a subdirectory. */
2488 err = got_path_dirname(tree_relpath, wt_relpath);
2489 if (err)
2490 return err;
2491 if (asprintf(&in_repo_path, "%s%s%s",
2492 worktree->path_prefix, is_root_wt ? "" : "/",
2493 *tree_relpath) == -1) {
2494 err = got_error_from_errno("asprintf");
2495 goto done;
2498 err = got_object_id_by_path(tree_id, repo,
2499 worktree->base_commit_id, in_repo_path);
2500 } else {
2501 /* Check out all files within a subdirectory. */
2502 *tree_id = got_object_id_dup(id);
2503 if (*tree_id == NULL) {
2504 err = got_error_from_errno("got_object_id_dup");
2505 goto done;
2507 *tree_relpath = strdup(wt_relpath);
2508 if (*tree_relpath == NULL) {
2509 err = got_error_from_errno("strdup");
2510 goto done;
2513 done:
2514 free(id);
2515 free(in_repo_path);
2516 if (err) {
2517 *entry_type = GOT_OBJ_TYPE_ANY;
2518 free(*tree_relpath);
2519 *tree_relpath = NULL;
2520 free(*tree_id);
2521 *tree_id = NULL;
2523 return err;
2526 static const struct got_error *
2527 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2528 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2529 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2530 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2532 const struct got_error *err = NULL;
2533 struct got_commit_object *commit = NULL;
2534 struct got_tree_object *tree = NULL;
2535 struct got_fileindex_diff_tree_cb diff_cb;
2536 struct diff_cb_arg arg;
2538 err = ref_base_commit(worktree, repo);
2539 if (err) {
2540 if (!(err->code == GOT_ERR_ERRNO &&
2541 (errno == EACCES || errno == EROFS)))
2542 goto done;
2543 err = (*progress_cb)(progress_arg,
2544 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2545 if (err)
2546 return err;
2549 err = got_object_open_as_commit(&commit, repo,
2550 worktree->base_commit_id);
2551 if (err)
2552 goto done;
2554 err = got_object_open_as_tree(&tree, repo, tree_id);
2555 if (err)
2556 goto done;
2558 if (entry_name &&
2559 got_object_tree_find_entry(tree, entry_name) == NULL) {
2560 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2561 goto done;
2564 diff_cb.diff_old_new = diff_old_new;
2565 diff_cb.diff_old = diff_old;
2566 diff_cb.diff_new = diff_new;
2567 arg.fileindex = fileindex;
2568 arg.worktree = worktree;
2569 arg.repo = repo;
2570 arg.progress_cb = progress_cb;
2571 arg.progress_arg = progress_arg;
2572 arg.cancel_cb = cancel_cb;
2573 arg.cancel_arg = cancel_arg;
2574 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2575 entry_name, repo, &diff_cb, &arg);
2576 done:
2577 if (tree)
2578 got_object_tree_close(tree);
2579 if (commit)
2580 got_object_commit_close(commit);
2581 return err;
2584 const struct got_error *
2585 got_worktree_checkout_files(struct got_worktree *worktree,
2586 struct got_pathlist_head *paths, struct got_repository *repo,
2587 got_worktree_checkout_cb progress_cb, void *progress_arg,
2588 got_cancel_cb cancel_cb, void *cancel_arg)
2590 const struct got_error *err = NULL, *sync_err, *unlockerr;
2591 struct got_commit_object *commit = NULL;
2592 struct got_tree_object *tree = NULL;
2593 struct got_fileindex *fileindex = NULL;
2594 char *fileindex_path = NULL;
2595 struct got_pathlist_entry *pe;
2596 struct tree_path_data {
2597 SIMPLEQ_ENTRY(tree_path_data) entry;
2598 struct got_object_id *tree_id;
2599 int entry_type;
2600 char *relpath;
2601 char *entry_name;
2602 } *tpd = NULL;
2603 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2605 SIMPLEQ_INIT(&tree_paths);
2607 err = lock_worktree(worktree, LOCK_EX);
2608 if (err)
2609 return err;
2611 /* Map all specified paths to in-repository trees. */
2612 TAILQ_FOREACH(pe, paths, entry) {
2613 tpd = malloc(sizeof(*tpd));
2614 if (tpd == NULL) {
2615 err = got_error_from_errno("malloc");
2616 goto done;
2619 err = find_tree_entry_for_checkout(&tpd->entry_type,
2620 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2621 if (err) {
2622 free(tpd);
2623 goto done;
2626 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2627 err = got_path_basename(&tpd->entry_name, pe->path);
2628 if (err) {
2629 free(tpd->relpath);
2630 free(tpd->tree_id);
2631 free(tpd);
2632 goto done;
2634 } else
2635 tpd->entry_name = NULL;
2637 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2641 * Read the file index.
2642 * Checking out files is supposed to be an idempotent operation.
2643 * If the on-disk file index is incomplete we will try to complete it.
2645 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2646 if (err)
2647 goto done;
2649 tpd = SIMPLEQ_FIRST(&tree_paths);
2650 TAILQ_FOREACH(pe, paths, entry) {
2651 struct bump_base_commit_id_arg bbc_arg;
2653 err = checkout_files(worktree, fileindex, tpd->relpath,
2654 tpd->tree_id, tpd->entry_name, repo,
2655 progress_cb, progress_arg, cancel_cb, cancel_arg);
2656 if (err)
2657 break;
2659 bbc_arg.base_commit_id = worktree->base_commit_id;
2660 bbc_arg.entry_name = tpd->entry_name;
2661 bbc_arg.path = pe->path;
2662 bbc_arg.path_len = pe->path_len;
2663 bbc_arg.progress_cb = progress_cb;
2664 bbc_arg.progress_arg = progress_arg;
2665 err = got_fileindex_for_each_entry_safe(fileindex,
2666 bump_base_commit_id, &bbc_arg);
2667 if (err)
2668 break;
2670 tpd = SIMPLEQ_NEXT(tpd, entry);
2672 sync_err = sync_fileindex(fileindex, fileindex_path);
2673 if (sync_err && err == NULL)
2674 err = sync_err;
2675 done:
2676 free(fileindex_path);
2677 if (tree)
2678 got_object_tree_close(tree);
2679 if (commit)
2680 got_object_commit_close(commit);
2681 if (fileindex)
2682 got_fileindex_free(fileindex);
2683 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2684 tpd = SIMPLEQ_FIRST(&tree_paths);
2685 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2686 free(tpd->relpath);
2687 free(tpd->tree_id);
2688 free(tpd);
2690 unlockerr = lock_worktree(worktree, LOCK_SH);
2691 if (unlockerr && err == NULL)
2692 err = unlockerr;
2693 return err;
2696 struct merge_file_cb_arg {
2697 struct got_worktree *worktree;
2698 struct got_fileindex *fileindex;
2699 got_worktree_checkout_cb progress_cb;
2700 void *progress_arg;
2701 got_cancel_cb cancel_cb;
2702 void *cancel_arg;
2703 const char *label_orig;
2704 struct got_object_id *commit_id2;
2707 static const struct got_error *
2708 merge_file_cb(void *arg, struct got_blob_object *blob1,
2709 struct got_blob_object *blob2, struct got_object_id *id1,
2710 struct got_object_id *id2, const char *path1, const char *path2,
2711 mode_t mode1, mode_t mode2, struct got_repository *repo)
2713 static const struct got_error *err = NULL;
2714 struct merge_file_cb_arg *a = arg;
2715 struct got_fileindex_entry *ie;
2716 char *ondisk_path = NULL;
2717 struct stat sb;
2718 unsigned char status;
2719 int local_changes_subsumed;
2721 if (blob1 && blob2) {
2722 ie = got_fileindex_entry_get(a->fileindex, path2,
2723 strlen(path2));
2724 if (ie == NULL)
2725 return (*a->progress_cb)(a->progress_arg,
2726 GOT_STATUS_MISSING, path2);
2728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2729 path2) == -1)
2730 return got_error_from_errno("asprintf");
2732 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2733 repo);
2734 if (err)
2735 goto done;
2737 if (status == GOT_STATUS_DELETE) {
2738 err = (*a->progress_cb)(a->progress_arg,
2739 GOT_STATUS_MERGE, path2);
2740 goto done;
2742 if (status != GOT_STATUS_NO_CHANGE &&
2743 status != GOT_STATUS_MODIFY &&
2744 status != GOT_STATUS_CONFLICT &&
2745 status != GOT_STATUS_ADD) {
2746 err = (*a->progress_cb)(a->progress_arg, status, path2);
2747 goto done;
2750 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2751 char *link_target2;
2752 err = got_object_blob_read_to_str(&link_target2, blob2);
2753 if (err)
2754 goto done;
2755 err = merge_symlink(a->worktree, blob1, ondisk_path,
2756 path2, a->label_orig, link_target2, a->commit_id2,
2757 repo, a->progress_cb, a->progress_arg);
2758 free(link_target2);
2759 } else {
2760 err = merge_blob(&local_changes_subsumed, a->worktree,
2761 blob1, ondisk_path, path2, sb.st_mode,
2762 a->label_orig, blob2, a->commit_id2, repo,
2763 a->progress_cb, a->progress_arg);
2765 } else if (blob1) {
2766 ie = got_fileindex_entry_get(a->fileindex, path1,
2767 strlen(path1));
2768 if (ie == NULL)
2769 return (*a->progress_cb)(a->progress_arg,
2770 GOT_STATUS_MISSING, path1);
2772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 path1) == -1)
2774 return got_error_from_errno("asprintf");
2776 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2777 repo);
2778 if (err)
2779 goto done;
2781 switch (status) {
2782 case GOT_STATUS_NO_CHANGE:
2783 err = (*a->progress_cb)(a->progress_arg,
2784 GOT_STATUS_DELETE, path1);
2785 if (err)
2786 goto done;
2787 err = remove_ondisk_file(a->worktree->root_path, path1);
2788 if (err)
2789 goto done;
2790 if (ie)
2791 got_fileindex_entry_mark_deleted_from_disk(ie);
2792 break;
2793 case GOT_STATUS_DELETE:
2794 case GOT_STATUS_MISSING:
2795 err = (*a->progress_cb)(a->progress_arg,
2796 GOT_STATUS_DELETE, path1);
2797 if (err)
2798 goto done;
2799 if (ie)
2800 got_fileindex_entry_mark_deleted_from_disk(ie);
2801 break;
2802 case GOT_STATUS_ADD: {
2803 struct got_object_id *id;
2804 FILE *blob1_f;
2806 * Delete the added file only if its content already
2807 * exists in the repository.
2809 err = got_object_blob_file_create(&id, &blob1_f, path1);
2810 if (err)
2811 goto done;
2812 if (got_object_id_cmp(id, id1) == 0) {
2813 err = (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_DELETE, path1);
2815 if (err)
2816 goto done;
2817 err = remove_ondisk_file(a->worktree->root_path,
2818 path1);
2819 if (err)
2820 goto done;
2821 if (ie)
2822 got_fileindex_entry_remove(a->fileindex,
2823 ie);
2824 } else {
2825 err = (*a->progress_cb)(a->progress_arg,
2826 GOT_STATUS_CANNOT_DELETE, path1);
2828 if (fclose(blob1_f) == EOF && err == NULL)
2829 err = got_error_from_errno("fclose");
2830 free(id);
2831 if (err)
2832 goto done;
2833 break;
2835 case GOT_STATUS_MODIFY:
2836 case GOT_STATUS_CONFLICT:
2837 err = (*a->progress_cb)(a->progress_arg,
2838 GOT_STATUS_CANNOT_DELETE, path1);
2839 if (err)
2840 goto done;
2841 break;
2842 case GOT_STATUS_OBSTRUCTED:
2843 err = (*a->progress_cb)(a->progress_arg, status, path1);
2844 if (err)
2845 goto done;
2846 break;
2847 default:
2848 break;
2850 } else if (blob2) {
2851 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2852 path2) == -1)
2853 return got_error_from_errno("asprintf");
2854 ie = got_fileindex_entry_get(a->fileindex, path2,
2855 strlen(path2));
2856 if (ie) {
2857 err = get_file_status(&status, &sb, ie, ondisk_path,
2858 -1, NULL, repo);
2859 if (err)
2860 goto done;
2861 if (status != GOT_STATUS_NO_CHANGE &&
2862 status != GOT_STATUS_MODIFY &&
2863 status != GOT_STATUS_CONFLICT &&
2864 status != GOT_STATUS_ADD) {
2865 err = (*a->progress_cb)(a->progress_arg,
2866 status, path2);
2867 goto done;
2869 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2870 char *link_target2;
2871 err = got_object_blob_read_to_str(&link_target2,
2872 blob2);
2873 if (err)
2874 goto done;
2875 err = merge_symlink(a->worktree, NULL,
2876 ondisk_path, path2, a->label_orig,
2877 link_target2, a->commit_id2, repo,
2878 a->progress_cb, a->progress_arg);
2879 free(link_target2);
2880 } else if (S_ISREG(sb.st_mode)) {
2881 err = merge_blob(&local_changes_subsumed,
2882 a->worktree, NULL, ondisk_path, path2,
2883 sb.st_mode, a->label_orig, blob2,
2884 a->commit_id2, repo, a->progress_cb,
2885 a->progress_arg);
2886 } else {
2887 err = got_error_path(ondisk_path,
2888 GOT_ERR_FILE_OBSTRUCTED);
2890 if (err)
2891 goto done;
2892 if (status == GOT_STATUS_DELETE) {
2893 err = got_fileindex_entry_update(ie,
2894 ondisk_path, blob2->id.sha1,
2895 a->worktree->base_commit_id->sha1, 0);
2896 if (err)
2897 goto done;
2899 } else {
2900 int is_bad_symlink = 0;
2901 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2902 if (S_ISLNK(mode2)) {
2903 err = install_symlink(&is_bad_symlink,
2904 a->worktree, ondisk_path, path2, blob2, 0,
2905 0, 1, repo, a->progress_cb, a->progress_arg);
2906 } else {
2907 err = install_blob(a->worktree, ondisk_path, path2,
2908 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2909 a->progress_cb, a->progress_arg);
2911 if (err)
2912 goto done;
2913 err = got_fileindex_entry_alloc(&ie, path2);
2914 if (err)
2915 goto done;
2916 err = got_fileindex_entry_update(ie, ondisk_path,
2917 NULL, NULL, 1);
2918 if (err) {
2919 got_fileindex_entry_free(ie);
2920 goto done;
2922 err = got_fileindex_entry_add(a->fileindex, ie);
2923 if (err) {
2924 got_fileindex_entry_free(ie);
2925 goto done;
2927 if (is_bad_symlink) {
2928 got_fileindex_entry_filetype_set(ie,
2929 GOT_FILEIDX_MODE_BAD_SYMLINK);
2933 done:
2934 free(ondisk_path);
2935 return err;
2938 struct check_merge_ok_arg {
2939 struct got_worktree *worktree;
2940 struct got_repository *repo;
2943 static const struct got_error *
2944 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2946 const struct got_error *err = NULL;
2947 struct check_merge_ok_arg *a = arg;
2948 unsigned char status;
2949 struct stat sb;
2950 char *ondisk_path;
2952 /* Reject merges into a work tree with mixed base commits. */
2953 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2954 SHA1_DIGEST_LENGTH))
2955 return got_error(GOT_ERR_MIXED_COMMITS);
2957 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2958 == -1)
2959 return got_error_from_errno("asprintf");
2961 /* Reject merges into a work tree with conflicted files. */
2962 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2963 if (err)
2964 return err;
2965 if (status == GOT_STATUS_CONFLICT)
2966 return got_error(GOT_ERR_CONFLICTS);
2968 return NULL;
2971 static const struct got_error *
2972 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2973 const char *fileindex_path, struct got_object_id *commit_id1,
2974 struct got_object_id *commit_id2, struct got_repository *repo,
2975 got_worktree_checkout_cb progress_cb, void *progress_arg,
2976 got_cancel_cb cancel_cb, void *cancel_arg)
2978 const struct got_error *err = NULL, *sync_err;
2979 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2980 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2981 struct merge_file_cb_arg arg;
2982 char *label_orig = NULL;
2984 if (commit_id1) {
2985 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2986 worktree->path_prefix);
2987 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2988 goto done;
2990 if (tree_id1) {
2991 char *id_str;
2993 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2994 if (err)
2995 goto done;
2997 err = got_object_id_str(&id_str, commit_id1);
2998 if (err)
2999 goto done;
3001 if (asprintf(&label_orig, "%s: commit %s",
3002 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3003 err = got_error_from_errno("asprintf");
3004 free(id_str);
3005 goto done;
3007 free(id_str);
3010 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3011 worktree->path_prefix);
3012 if (err)
3013 goto done;
3015 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3016 if (err)
3017 goto done;
3019 arg.worktree = worktree;
3020 arg.fileindex = fileindex;
3021 arg.progress_cb = progress_cb;
3022 arg.progress_arg = progress_arg;
3023 arg.cancel_cb = cancel_cb;
3024 arg.cancel_arg = cancel_arg;
3025 arg.label_orig = label_orig;
3026 arg.commit_id2 = commit_id2;
3027 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3028 sync_err = sync_fileindex(fileindex, fileindex_path);
3029 if (sync_err && err == NULL)
3030 err = sync_err;
3031 done:
3032 if (tree1)
3033 got_object_tree_close(tree1);
3034 if (tree2)
3035 got_object_tree_close(tree2);
3036 free(label_orig);
3037 return err;
3040 const struct got_error *
3041 got_worktree_merge_files(struct got_worktree *worktree,
3042 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3043 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3044 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3046 const struct got_error *err, *unlockerr;
3047 char *fileindex_path = NULL;
3048 struct got_fileindex *fileindex = NULL;
3049 struct check_merge_ok_arg mok_arg;
3051 err = lock_worktree(worktree, LOCK_EX);
3052 if (err)
3053 return err;
3055 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3056 if (err)
3057 goto done;
3059 mok_arg.worktree = worktree;
3060 mok_arg.repo = repo;
3061 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3062 &mok_arg);
3063 if (err)
3064 goto done;
3066 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3067 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3068 done:
3069 if (fileindex)
3070 got_fileindex_free(fileindex);
3071 free(fileindex_path);
3072 unlockerr = lock_worktree(worktree, LOCK_SH);
3073 if (unlockerr && err == NULL)
3074 err = unlockerr;
3075 return err;
3078 struct diff_dir_cb_arg {
3079 struct got_fileindex *fileindex;
3080 struct got_worktree *worktree;
3081 const char *status_path;
3082 size_t status_path_len;
3083 struct got_repository *repo;
3084 got_worktree_status_cb status_cb;
3085 void *status_arg;
3086 got_cancel_cb cancel_cb;
3087 void *cancel_arg;
3088 /* A pathlist containing per-directory pathlists of ignore patterns. */
3089 struct got_pathlist_head ignores;
3090 int report_unchanged;
3091 int no_ignores;
3094 static const struct got_error *
3095 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3096 int dirfd, const char *de_name,
3097 got_worktree_status_cb status_cb, void *status_arg,
3098 struct got_repository *repo, int report_unchanged)
3100 const struct got_error *err = NULL;
3101 unsigned char status = GOT_STATUS_NO_CHANGE;
3102 unsigned char staged_status = get_staged_status(ie);
3103 struct stat sb;
3104 struct got_object_id blob_id, commit_id, staged_blob_id;
3105 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3106 struct got_object_id *staged_blob_idp = NULL;
3108 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3109 if (err)
3110 return err;
3112 if (status == GOT_STATUS_NO_CHANGE &&
3113 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3114 return NULL;
3116 if (got_fileindex_entry_has_blob(ie)) {
3117 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3118 blob_idp = &blob_id;
3120 if (got_fileindex_entry_has_commit(ie)) {
3121 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3122 commit_idp = &commit_id;
3124 if (staged_status == GOT_STATUS_ADD ||
3125 staged_status == GOT_STATUS_MODIFY) {
3126 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3127 SHA1_DIGEST_LENGTH);
3128 staged_blob_idp = &staged_blob_id;
3131 return (*status_cb)(status_arg, status, staged_status,
3132 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3135 static const struct got_error *
3136 status_old_new(void *arg, struct got_fileindex_entry *ie,
3137 struct dirent *de, const char *parent_path, int dirfd)
3139 const struct got_error *err = NULL;
3140 struct diff_dir_cb_arg *a = arg;
3141 char *abspath;
3143 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3144 return got_error(GOT_ERR_CANCELLED);
3146 if (got_path_cmp(parent_path, a->status_path,
3147 strlen(parent_path), a->status_path_len) != 0 &&
3148 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3149 return NULL;
3151 if (parent_path[0]) {
3152 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3153 parent_path, de->d_name) == -1)
3154 return got_error_from_errno("asprintf");
3155 } else {
3156 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3157 de->d_name) == -1)
3158 return got_error_from_errno("asprintf");
3161 err = report_file_status(ie, abspath, dirfd, de->d_name,
3162 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3163 free(abspath);
3164 return err;
3167 static const struct got_error *
3168 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3170 struct diff_dir_cb_arg *a = arg;
3171 struct got_object_id blob_id, commit_id;
3172 unsigned char status;
3174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3175 return got_error(GOT_ERR_CANCELLED);
3177 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3178 return NULL;
3180 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3181 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3182 if (got_fileindex_entry_has_file_on_disk(ie))
3183 status = GOT_STATUS_MISSING;
3184 else
3185 status = GOT_STATUS_DELETE;
3186 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3187 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3190 void
3191 free_ignorelist(struct got_pathlist_head *ignorelist)
3193 struct got_pathlist_entry *pe;
3195 TAILQ_FOREACH(pe, ignorelist, entry)
3196 free((char *)pe->path);
3197 got_pathlist_free(ignorelist);
3200 void
3201 free_ignores(struct got_pathlist_head *ignores)
3203 struct got_pathlist_entry *pe;
3205 TAILQ_FOREACH(pe, ignores, entry) {
3206 struct got_pathlist_head *ignorelist = pe->data;
3207 free_ignorelist(ignorelist);
3208 free((char *)pe->path);
3210 got_pathlist_free(ignores);
3213 static const struct got_error *
3214 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3216 const struct got_error *err = NULL;
3217 struct got_pathlist_entry *pe = NULL;
3218 struct got_pathlist_head *ignorelist;
3219 char *line = NULL, *pattern, *dirpath = NULL;
3220 size_t linesize = 0;
3221 ssize_t linelen;
3223 ignorelist = calloc(1, sizeof(*ignorelist));
3224 if (ignorelist == NULL)
3225 return got_error_from_errno("calloc");
3226 TAILQ_INIT(ignorelist);
3228 while ((linelen = getline(&line, &linesize, f)) != -1) {
3229 if (linelen > 0 && line[linelen - 1] == '\n')
3230 line[linelen - 1] = '\0';
3232 /* Git's ignores may contain comments. */
3233 if (line[0] == '#')
3234 continue;
3236 /* Git's negated patterns are not (yet?) supported. */
3237 if (line[0] == '!')
3238 continue;
3240 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3241 line) == -1) {
3242 err = got_error_from_errno("asprintf");
3243 goto done;
3245 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3246 if (err)
3247 goto done;
3249 if (ferror(f)) {
3250 err = got_error_from_errno("getline");
3251 goto done;
3254 dirpath = strdup(path);
3255 if (dirpath == NULL) {
3256 err = got_error_from_errno("strdup");
3257 goto done;
3259 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3260 done:
3261 free(line);
3262 if (err || pe == NULL) {
3263 free(dirpath);
3264 free_ignorelist(ignorelist);
3266 return err;
3269 int
3270 match_ignores(struct got_pathlist_head *ignores, const char *path)
3272 struct got_pathlist_entry *pe;
3274 /* Handle patterns which match in all directories. */
3275 TAILQ_FOREACH(pe, ignores, entry) {
3276 struct got_pathlist_head *ignorelist = pe->data;
3277 struct got_pathlist_entry *pi;
3279 TAILQ_FOREACH(pi, ignorelist, entry) {
3280 const char *p, *pattern = pi->path;
3282 if (strncmp(pattern, "**/", 3) != 0)
3283 continue;
3284 pattern += 3;
3285 p = path;
3286 while (*p) {
3287 if (fnmatch(pattern, p,
3288 FNM_PATHNAME | FNM_LEADING_DIR)) {
3289 /* Retry in next directory. */
3290 while (*p && *p != '/')
3291 p++;
3292 while (*p == '/')
3293 p++;
3294 continue;
3296 return 1;
3302 * The ignores pathlist contains ignore lists from children before
3303 * parents, so we can find the most specific ignorelist by walking
3304 * ignores backwards.
3306 pe = TAILQ_LAST(ignores, got_pathlist_head);
3307 while (pe) {
3308 if (got_path_is_child(path, pe->path, pe->path_len)) {
3309 struct got_pathlist_head *ignorelist = pe->data;
3310 struct got_pathlist_entry *pi;
3311 TAILQ_FOREACH(pi, ignorelist, entry) {
3312 const char *pattern = pi->path;
3313 int flags = FNM_LEADING_DIR;
3314 if (strstr(pattern, "/**/") == NULL)
3315 flags |= FNM_PATHNAME;
3316 if (fnmatch(pattern, path, flags))
3317 continue;
3318 return 1;
3321 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3324 return 0;
3327 static const struct got_error *
3328 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3329 const char *path, int dirfd, const char *ignores_filename)
3331 const struct got_error *err = NULL;
3332 char *ignorespath;
3333 int fd = -1;
3334 FILE *ignoresfile = NULL;
3336 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3337 path[0] ? "/" : "", ignores_filename) == -1)
3338 return got_error_from_errno("asprintf");
3340 if (dirfd != -1) {
3341 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3342 if (fd == -1) {
3343 if (errno != ENOENT && errno != EACCES)
3344 err = got_error_from_errno2("openat",
3345 ignorespath);
3346 } else {
3347 ignoresfile = fdopen(fd, "r");
3348 if (ignoresfile == NULL)
3349 err = got_error_from_errno2("fdopen",
3350 ignorespath);
3351 else {
3352 fd = -1;
3353 err = read_ignores(ignores, path, ignoresfile);
3356 } else {
3357 ignoresfile = fopen(ignorespath, "r");
3358 if (ignoresfile == NULL) {
3359 if (errno != ENOENT && errno != EACCES)
3360 err = got_error_from_errno2("fopen",
3361 ignorespath);
3362 } else
3363 err = read_ignores(ignores, path, ignoresfile);
3366 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3367 err = got_error_from_errno2("fclose", path);
3368 if (fd != -1 && close(fd) == -1 && err == NULL)
3369 err = got_error_from_errno2("close", path);
3370 free(ignorespath);
3371 return err;
3374 static const struct got_error *
3375 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3377 const struct got_error *err = NULL;
3378 struct diff_dir_cb_arg *a = arg;
3379 char *path = NULL;
3381 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3382 return got_error(GOT_ERR_CANCELLED);
3384 if (parent_path[0]) {
3385 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3386 return got_error_from_errno("asprintf");
3387 } else {
3388 path = de->d_name;
3391 if (de->d_type != DT_DIR &&
3392 got_path_is_child(path, a->status_path, a->status_path_len)
3393 && !match_ignores(&a->ignores, path))
3394 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3395 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3396 if (parent_path[0])
3397 free(path);
3398 return err;
3401 static const struct got_error *
3402 status_traverse(void *arg, const char *path, int dirfd)
3404 const struct got_error *err = NULL;
3405 struct diff_dir_cb_arg *a = arg;
3407 if (a->no_ignores)
3408 return NULL;
3410 err = add_ignores(&a->ignores, a->worktree->root_path,
3411 path, dirfd, ".cvsignore");
3412 if (err)
3413 return err;
3415 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3416 dirfd, ".gitignore");
3418 return err;
3421 static const struct got_error *
3422 report_single_file_status(const char *path, const char *ondisk_path,
3423 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3424 void *status_arg, struct got_repository *repo, int report_unchanged)
3426 struct got_fileindex_entry *ie;
3427 struct stat sb;
3429 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3430 if (ie)
3431 return report_file_status(ie, ondisk_path, -1, NULL,
3432 status_cb, status_arg, repo, report_unchanged);
3434 if (lstat(ondisk_path, &sb) == -1) {
3435 if (errno != ENOENT)
3436 return got_error_from_errno2("lstat", ondisk_path);
3437 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3438 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3439 return NULL;
3442 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3443 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3444 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3446 return NULL;
3449 static const struct got_error *
3450 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3451 const char *root_path, const char *path)
3453 const struct got_error *err;
3454 char *parent_path, *next_parent_path = NULL;
3456 err = add_ignores(ignores, root_path, "", -1,
3457 ".cvsignore");
3458 if (err)
3459 return err;
3461 err = add_ignores(ignores, root_path, "", -1,
3462 ".gitignore");
3463 if (err)
3464 return err;
3466 err = got_path_dirname(&parent_path, path);
3467 if (err) {
3468 if (err->code == GOT_ERR_BAD_PATH)
3469 return NULL; /* cannot traverse parent */
3470 return err;
3472 for (;;) {
3473 err = add_ignores(ignores, root_path, parent_path, -1,
3474 ".cvsignore");
3475 if (err)
3476 break;
3477 err = add_ignores(ignores, root_path, parent_path, -1,
3478 ".gitignore");
3479 if (err)
3480 break;
3481 err = got_path_dirname(&next_parent_path, parent_path);
3482 if (err) {
3483 if (err->code == GOT_ERR_BAD_PATH)
3484 err = NULL; /* traversed everything */
3485 break;
3487 free(parent_path);
3488 parent_path = next_parent_path;
3489 next_parent_path = NULL;
3492 free(parent_path);
3493 free(next_parent_path);
3494 return err;
3497 static const struct got_error *
3498 worktree_status(struct got_worktree *worktree, const char *path,
3499 struct got_fileindex *fileindex, struct got_repository *repo,
3500 got_worktree_status_cb status_cb, void *status_arg,
3501 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3502 int report_unchanged)
3504 const struct got_error *err = NULL;
3505 int fd = -1;
3506 struct got_fileindex_diff_dir_cb fdiff_cb;
3507 struct diff_dir_cb_arg arg;
3508 char *ondisk_path = NULL;
3510 TAILQ_INIT(&arg.ignores);
3512 if (asprintf(&ondisk_path, "%s%s%s",
3513 worktree->root_path, path[0] ? "/" : "", path) == -1)
3514 return got_error_from_errno("asprintf");
3516 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3517 if (fd == -1) {
3518 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3519 errno != ELOOP)
3520 err = got_error_from_errno2("open", ondisk_path);
3521 else
3522 err = report_single_file_status(path, ondisk_path,
3523 fileindex, status_cb, status_arg, repo,
3524 report_unchanged);
3525 } else {
3526 fdiff_cb.diff_old_new = status_old_new;
3527 fdiff_cb.diff_old = status_old;
3528 fdiff_cb.diff_new = status_new;
3529 fdiff_cb.diff_traverse = status_traverse;
3530 arg.fileindex = fileindex;
3531 arg.worktree = worktree;
3532 arg.status_path = path;
3533 arg.status_path_len = strlen(path);
3534 arg.repo = repo;
3535 arg.status_cb = status_cb;
3536 arg.status_arg = status_arg;
3537 arg.cancel_cb = cancel_cb;
3538 arg.cancel_arg = cancel_arg;
3539 arg.report_unchanged = report_unchanged;
3540 arg.no_ignores = no_ignores;
3541 if (!no_ignores) {
3542 err = add_ignores_from_parent_paths(&arg.ignores,
3543 worktree->root_path, path);
3544 if (err)
3545 goto done;
3547 err = got_fileindex_diff_dir(fileindex, fd,
3548 worktree->root_path, path, repo, &fdiff_cb, &arg);
3550 done:
3551 free_ignores(&arg.ignores);
3552 if (fd != -1 && close(fd) != 0 && err == NULL)
3553 err = got_error_from_errno("close");
3554 free(ondisk_path);
3555 return err;
3558 const struct got_error *
3559 got_worktree_status(struct got_worktree *worktree,
3560 struct got_pathlist_head *paths, struct got_repository *repo,
3561 got_worktree_status_cb status_cb, void *status_arg,
3562 got_cancel_cb cancel_cb, void *cancel_arg)
3564 const struct got_error *err = NULL;
3565 char *fileindex_path = NULL;
3566 struct got_fileindex *fileindex = NULL;
3567 struct got_pathlist_entry *pe;
3569 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3570 if (err)
3571 return err;
3573 TAILQ_FOREACH(pe, paths, entry) {
3574 err = worktree_status(worktree, pe->path, fileindex, repo,
3575 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3576 if (err)
3577 break;
3579 free(fileindex_path);
3580 got_fileindex_free(fileindex);
3581 return err;
3584 const struct got_error *
3585 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3586 const char *arg)
3588 const struct got_error *err = NULL;
3589 char *resolved = NULL, *cwd = NULL, *path = NULL;
3590 size_t len;
3591 struct stat sb;
3593 *wt_path = NULL;
3595 cwd = getcwd(NULL, 0);
3596 if (cwd == NULL)
3597 return got_error_from_errno("getcwd");
3599 if (lstat(arg, &sb) == -1) {
3600 if (errno != ENOENT) {
3601 err = got_error_from_errno2("lstat", arg);
3602 goto done;
3605 if (S_ISLNK(sb.st_mode)) {
3607 * We cannot use realpath(3) with symlinks since we want to
3608 * operate on the symlink itself.
3609 * But we can make the path absolute, assuming it is relative
3610 * to the current working directory, and then canonicalize it.
3612 char *abspath = NULL;
3613 char canonpath[PATH_MAX];
3614 if (!got_path_is_absolute(arg)) {
3615 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3616 err = got_error_from_errno("asprintf");
3617 goto done;
3621 err = got_canonpath(abspath ? abspath : arg, canonpath,
3622 sizeof(canonpath));
3623 if (err)
3624 goto done;
3625 resolved = strdup(canonpath);
3626 if (resolved == NULL) {
3627 err = got_error_from_errno("strdup");
3628 goto done;
3630 } else {
3631 resolved = realpath(arg, NULL);
3632 if (resolved == NULL) {
3633 if (errno != ENOENT) {
3634 err = got_error_from_errno2("realpath", arg);
3635 goto done;
3637 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3638 err = got_error_from_errno("asprintf");
3639 goto done;
3644 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3645 strlen(got_worktree_get_root_path(worktree)))) {
3646 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3647 goto done;
3650 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3651 err = got_path_skip_common_ancestor(&path,
3652 got_worktree_get_root_path(worktree), resolved);
3653 if (err)
3654 goto done;
3655 } else {
3656 path = strdup("");
3657 if (path == NULL) {
3658 err = got_error_from_errno("strdup");
3659 goto done;
3663 /* XXX status walk can't deal with trailing slash! */
3664 len = strlen(path);
3665 while (len > 0 && path[len - 1] == '/') {
3666 path[len - 1] = '\0';
3667 len--;
3669 done:
3670 free(resolved);
3671 free(cwd);
3672 if (err == NULL)
3673 *wt_path = path;
3674 else
3675 free(path);
3676 return err;
3679 struct schedule_addition_args {
3680 struct got_worktree *worktree;
3681 struct got_fileindex *fileindex;
3682 got_worktree_checkout_cb progress_cb;
3683 void *progress_arg;
3684 struct got_repository *repo;
3687 static const struct got_error *
3688 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3689 const char *relpath, struct got_object_id *blob_id,
3690 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3691 int dirfd, const char *de_name)
3693 struct schedule_addition_args *a = arg;
3694 const struct got_error *err = NULL;
3695 struct got_fileindex_entry *ie;
3696 struct stat sb;
3697 char *ondisk_path;
3699 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3700 relpath) == -1)
3701 return got_error_from_errno("asprintf");
3703 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3704 if (ie) {
3705 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3706 de_name, a->repo);
3707 if (err)
3708 goto done;
3709 /* Re-adding an existing entry is a no-op. */
3710 if (status == GOT_STATUS_ADD)
3711 goto done;
3712 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3713 if (err)
3714 goto done;
3717 if (status != GOT_STATUS_UNVERSIONED) {
3718 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3719 goto done;
3722 err = got_fileindex_entry_alloc(&ie, relpath);
3723 if (err)
3724 goto done;
3725 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3726 if (err) {
3727 got_fileindex_entry_free(ie);
3728 goto done;
3730 err = got_fileindex_entry_add(a->fileindex, ie);
3731 if (err) {
3732 got_fileindex_entry_free(ie);
3733 goto done;
3735 done:
3736 free(ondisk_path);
3737 if (err)
3738 return err;
3739 if (status == GOT_STATUS_ADD)
3740 return NULL;
3741 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3744 const struct got_error *
3745 got_worktree_schedule_add(struct got_worktree *worktree,
3746 struct got_pathlist_head *paths,
3747 got_worktree_checkout_cb progress_cb, void *progress_arg,
3748 struct got_repository *repo, int no_ignores)
3750 struct got_fileindex *fileindex = NULL;
3751 char *fileindex_path = NULL;
3752 const struct got_error *err = NULL, *sync_err, *unlockerr;
3753 struct got_pathlist_entry *pe;
3754 struct schedule_addition_args saa;
3756 err = lock_worktree(worktree, LOCK_EX);
3757 if (err)
3758 return err;
3760 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3761 if (err)
3762 goto done;
3764 saa.worktree = worktree;
3765 saa.fileindex = fileindex;
3766 saa.progress_cb = progress_cb;
3767 saa.progress_arg = progress_arg;
3768 saa.repo = repo;
3770 TAILQ_FOREACH(pe, paths, entry) {
3771 err = worktree_status(worktree, pe->path, fileindex, repo,
3772 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3773 if (err)
3774 break;
3776 sync_err = sync_fileindex(fileindex, fileindex_path);
3777 if (sync_err && err == NULL)
3778 err = sync_err;
3779 done:
3780 free(fileindex_path);
3781 if (fileindex)
3782 got_fileindex_free(fileindex);
3783 unlockerr = lock_worktree(worktree, LOCK_SH);
3784 if (unlockerr && err == NULL)
3785 err = unlockerr;
3786 return err;
3789 struct schedule_deletion_args {
3790 struct got_worktree *worktree;
3791 struct got_fileindex *fileindex;
3792 got_worktree_delete_cb progress_cb;
3793 void *progress_arg;
3794 struct got_repository *repo;
3795 int delete_local_mods;
3796 int keep_on_disk;
3797 const char *status_codes;
3800 static const struct got_error *
3801 schedule_for_deletion(void *arg, unsigned char status,
3802 unsigned char staged_status, const char *relpath,
3803 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3804 struct got_object_id *commit_id, int dirfd, const char *de_name)
3806 struct schedule_deletion_args *a = arg;
3807 const struct got_error *err = NULL;
3808 struct got_fileindex_entry *ie = NULL;
3809 struct stat sb;
3810 char *ondisk_path, *parent = NULL;
3812 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3813 if (ie == NULL)
3814 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3816 staged_status = get_staged_status(ie);
3817 if (staged_status != GOT_STATUS_NO_CHANGE) {
3818 if (staged_status == GOT_STATUS_DELETE)
3819 return NULL;
3820 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3823 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3824 relpath) == -1)
3825 return got_error_from_errno("asprintf");
3827 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3828 a->repo);
3829 if (err)
3830 goto done;
3832 if (a->status_codes) {
3833 size_t ncodes = strlen(a->status_codes);
3834 int i;
3835 for (i = 0; i < ncodes ; i++) {
3836 if (status == a->status_codes[i])
3837 break;
3839 if (i == ncodes) {
3840 /* Do not delete files in non-matching status. */
3841 free(ondisk_path);
3842 return NULL;
3844 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3845 a->status_codes[i] != GOT_STATUS_MISSING) {
3846 static char msg[64];
3847 snprintf(msg, sizeof(msg),
3848 "invalid status code '%c'", a->status_codes[i]);
3849 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3850 goto done;
3854 if (status != GOT_STATUS_NO_CHANGE) {
3855 if (status == GOT_STATUS_DELETE)
3856 goto done;
3857 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3858 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3859 goto done;
3861 if (status != GOT_STATUS_MODIFY &&
3862 status != GOT_STATUS_MISSING) {
3863 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3864 goto done;
3868 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3869 if (dirfd != -1) {
3870 if (unlinkat(dirfd, de_name, 0) != 0) {
3871 err = got_error_from_errno2("unlinkat",
3872 ondisk_path);
3873 goto done;
3875 } else if (unlink(ondisk_path) != 0) {
3876 err = got_error_from_errno2("unlink", ondisk_path);
3877 goto done;
3880 parent = dirname(ondisk_path);
3882 if (parent == NULL) {
3883 err = got_error_from_errno2("dirname", ondisk_path);
3884 goto done;
3886 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3887 if (rmdir(parent) == -1) {
3888 if (errno != ENOTEMPTY)
3889 err = got_error_from_errno2("rmdir",
3890 parent);
3891 break;
3893 parent = dirname(parent);
3894 if (parent == NULL) {
3895 err = got_error_from_errno2("dirname", parent);
3896 goto done;
3901 got_fileindex_entry_mark_deleted_from_disk(ie);
3902 done:
3903 free(ondisk_path);
3904 if (err)
3905 return err;
3906 if (status == GOT_STATUS_DELETE)
3907 return NULL;
3908 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3909 staged_status, relpath);
3912 const struct got_error *
3913 got_worktree_schedule_delete(struct got_worktree *worktree,
3914 struct got_pathlist_head *paths, int delete_local_mods,
3915 const char *status_codes,
3916 got_worktree_delete_cb progress_cb, void *progress_arg,
3917 struct got_repository *repo, int keep_on_disk)
3919 struct got_fileindex *fileindex = NULL;
3920 char *fileindex_path = NULL;
3921 const struct got_error *err = NULL, *sync_err, *unlockerr;
3922 struct got_pathlist_entry *pe;
3923 struct schedule_deletion_args sda;
3925 err = lock_worktree(worktree, LOCK_EX);
3926 if (err)
3927 return err;
3929 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3930 if (err)
3931 goto done;
3933 sda.worktree = worktree;
3934 sda.fileindex = fileindex;
3935 sda.progress_cb = progress_cb;
3936 sda.progress_arg = progress_arg;
3937 sda.repo = repo;
3938 sda.delete_local_mods = delete_local_mods;
3939 sda.keep_on_disk = keep_on_disk;
3940 sda.status_codes = status_codes;
3942 TAILQ_FOREACH(pe, paths, entry) {
3943 err = worktree_status(worktree, pe->path, fileindex, repo,
3944 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3945 if (err)
3946 break;
3948 sync_err = sync_fileindex(fileindex, fileindex_path);
3949 if (sync_err && err == NULL)
3950 err = sync_err;
3951 done:
3952 free(fileindex_path);
3953 if (fileindex)
3954 got_fileindex_free(fileindex);
3955 unlockerr = lock_worktree(worktree, LOCK_SH);
3956 if (unlockerr && err == NULL)
3957 err = unlockerr;
3958 return err;
3961 static const struct got_error *
3962 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3964 const struct got_error *err = NULL;
3965 char *line = NULL;
3966 size_t linesize = 0, n;
3967 ssize_t linelen;
3969 linelen = getline(&line, &linesize, infile);
3970 if (linelen == -1) {
3971 if (ferror(infile)) {
3972 err = got_error_from_errno("getline");
3973 goto done;
3975 return NULL;
3977 if (outfile) {
3978 n = fwrite(line, 1, linelen, outfile);
3979 if (n != linelen) {
3980 err = got_ferror(outfile, GOT_ERR_IO);
3981 goto done;
3984 if (rejectfile) {
3985 n = fwrite(line, 1, linelen, rejectfile);
3986 if (n != linelen)
3987 err = got_ferror(outfile, GOT_ERR_IO);
3989 done:
3990 free(line);
3991 return err;
3994 static const struct got_error *
3995 skip_one_line(FILE *f)
3997 char *line = NULL;
3998 size_t linesize = 0;
3999 ssize_t linelen;
4001 linelen = getline(&line, &linesize, f);
4002 if (linelen == -1) {
4003 if (ferror(f))
4004 return got_error_from_errno("getline");
4005 return NULL;
4007 free(line);
4008 return NULL;
4011 static const struct got_error *
4012 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4013 int start_old, int end_old, int start_new, int end_new,
4014 FILE *outfile, FILE *rejectfile)
4016 const struct got_error *err;
4018 /* Copy old file's lines leading up to patch. */
4019 while (!feof(f1) && *line_cur1 < start_old) {
4020 err = copy_one_line(f1, outfile, NULL);
4021 if (err)
4022 return err;
4023 (*line_cur1)++;
4025 /* Skip new file's lines leading up to patch. */
4026 while (!feof(f2) && *line_cur2 < start_new) {
4027 if (rejectfile)
4028 err = copy_one_line(f2, NULL, rejectfile);
4029 else
4030 err = skip_one_line(f2);
4031 if (err)
4032 return err;
4033 (*line_cur2)++;
4035 /* Copy patched lines. */
4036 while (!feof(f2) && *line_cur2 <= end_new) {
4037 err = copy_one_line(f2, outfile, NULL);
4038 if (err)
4039 return err;
4040 (*line_cur2)++;
4042 /* Skip over old file's replaced lines. */
4043 while (!feof(f1) && *line_cur1 <= end_old) {
4044 if (rejectfile)
4045 err = copy_one_line(f1, NULL, rejectfile);
4046 else
4047 err = skip_one_line(f1);
4048 if (err)
4049 return err;
4050 (*line_cur1)++;
4053 return NULL;
4056 static const struct got_error *
4057 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4058 FILE *outfile, FILE *rejectfile)
4060 const struct got_error *err;
4062 if (outfile) {
4063 /* Copy old file's lines until EOF. */
4064 while (!feof(f1)) {
4065 err = copy_one_line(f1, outfile, NULL);
4066 if (err)
4067 return err;
4068 (*line_cur1)++;
4071 if (rejectfile) {
4072 /* Copy new file's lines until EOF. */
4073 while (!feof(f2)) {
4074 err = copy_one_line(f2, NULL, rejectfile);
4075 if (err)
4076 return err;
4077 (*line_cur2)++;
4081 return NULL;
4084 static const struct got_error *
4085 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4086 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4087 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4088 int *line_cur2, FILE *outfile, FILE *rejectfile,
4089 got_worktree_patch_cb patch_cb, void *patch_arg)
4091 const struct got_error *err = NULL;
4092 int start_old = change->cv.a;
4093 int end_old = change->cv.b;
4094 int start_new = change->cv.c;
4095 int end_new = change->cv.d;
4096 long pos1, pos2;
4097 FILE *hunkfile;
4099 *choice = GOT_PATCH_CHOICE_NONE;
4101 hunkfile = got_opentemp();
4102 if (hunkfile == NULL)
4103 return got_error_from_errno("got_opentemp");
4105 pos1 = ftell(f1);
4106 pos2 = ftell(f2);
4108 /* XXX TODO needs error checking */
4109 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4111 if (fseek(f1, pos1, SEEK_SET) == -1) {
4112 err = got_ferror(f1, GOT_ERR_IO);
4113 goto done;
4115 if (fseek(f2, pos2, SEEK_SET) == -1) {
4116 err = got_ferror(f1, GOT_ERR_IO);
4117 goto done;
4119 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4120 err = got_ferror(hunkfile, GOT_ERR_IO);
4121 goto done;
4124 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4125 hunkfile, n, nchanges);
4126 if (err)
4127 goto done;
4129 switch (*choice) {
4130 case GOT_PATCH_CHOICE_YES:
4131 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4132 end_old, start_new, end_new, outfile, rejectfile);
4133 break;
4134 case GOT_PATCH_CHOICE_NO:
4135 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4136 end_old, start_new, end_new, rejectfile, outfile);
4137 break;
4138 case GOT_PATCH_CHOICE_QUIT:
4139 break;
4140 default:
4141 err = got_error(GOT_ERR_PATCH_CHOICE);
4142 break;
4144 done:
4145 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4146 err = got_error_from_errno("fclose");
4147 return err;
4150 struct revert_file_args {
4151 struct got_worktree *worktree;
4152 struct got_fileindex *fileindex;
4153 got_worktree_checkout_cb progress_cb;
4154 void *progress_arg;
4155 got_worktree_patch_cb patch_cb;
4156 void *patch_arg;
4157 struct got_repository *repo;
4160 static const struct got_error *
4161 create_patched_content(char **path_outfile, int reverse_patch,
4162 struct got_object_id *blob_id, const char *path2,
4163 int dirfd2, const char *de_name2,
4164 const char *relpath, struct got_repository *repo,
4165 got_worktree_patch_cb patch_cb, void *patch_arg)
4167 const struct got_error *err;
4168 struct got_blob_object *blob = NULL;
4169 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4170 int fd2 = -1;
4171 char link_target[PATH_MAX];
4172 ssize_t link_len = 0;
4173 char *path1 = NULL, *id_str = NULL;
4174 struct stat sb1, sb2;
4175 struct got_diff_changes *changes = NULL;
4176 struct got_diff_state *ds = NULL;
4177 struct got_diff_args *args = NULL;
4178 struct got_diff_change *change;
4179 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4180 int n = 0;
4182 *path_outfile = NULL;
4184 err = got_object_id_str(&id_str, blob_id);
4185 if (err)
4186 return err;
4188 if (dirfd2 != -1) {
4189 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4190 if (fd2 == -1) {
4191 if (errno != ELOOP) {
4192 err = got_error_from_errno2("openat", path2);
4193 goto done;
4195 link_len = readlinkat(dirfd2, de_name2,
4196 link_target, sizeof(link_target));
4197 if (link_len == -1)
4198 return got_error_from_errno2("readlinkat", path2);
4199 sb2.st_mode = S_IFLNK;
4200 sb2.st_size = link_len;
4202 } else {
4203 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4204 if (fd2 == -1) {
4205 if (errno != ELOOP) {
4206 err = got_error_from_errno2("open", path2);
4207 goto done;
4209 link_len = readlink(path2, link_target,
4210 sizeof(link_target));
4211 if (link_len == -1)
4212 return got_error_from_errno2("readlink", path2);
4213 sb2.st_mode = S_IFLNK;
4214 sb2.st_size = link_len;
4217 if (fd2 != -1) {
4218 if (fstat(fd2, &sb2) == -1) {
4219 err = got_error_from_errno2("fstat", path2);
4220 goto done;
4223 f2 = fdopen(fd2, "r");
4224 if (f2 == NULL) {
4225 err = got_error_from_errno2("fdopen", path2);
4226 goto done;
4228 fd2 = -1;
4229 } else {
4230 size_t n;
4231 f2 = got_opentemp();
4232 if (f2 == NULL) {
4233 err = got_error_from_errno2("got_opentemp", path2);
4234 goto done;
4236 n = fwrite(link_target, 1, link_len, f2);
4237 if (n != link_len) {
4238 err = got_ferror(f2, GOT_ERR_IO);
4239 goto done;
4241 if (fflush(f2) == EOF) {
4242 err = got_error_from_errno("fflush");
4243 goto done;
4245 rewind(f2);
4248 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4249 if (err)
4250 goto done;
4252 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4253 if (err)
4254 goto done;
4256 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4257 if (err)
4258 goto done;
4260 if (stat(path1, &sb1) == -1) {
4261 err = got_error_from_errno2("stat", path1);
4262 goto done;
4265 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4266 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4267 if (err)
4268 goto done;
4270 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4271 if (err)
4272 goto done;
4274 if (fseek(f1, 0L, SEEK_SET) == -1)
4275 return got_ferror(f1, GOT_ERR_IO);
4276 if (fseek(f2, 0L, SEEK_SET) == -1)
4277 return got_ferror(f2, GOT_ERR_IO);
4278 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4279 int choice;
4280 err = apply_or_reject_change(&choice, change, ++n,
4281 changes->nchanges, ds, args, diff_flags, relpath,
4282 f1, f2, &line_cur1, &line_cur2,
4283 reverse_patch ? NULL : outfile,
4284 reverse_patch ? outfile : NULL,
4285 patch_cb, patch_arg);
4286 if (err)
4287 goto done;
4288 if (choice == GOT_PATCH_CHOICE_YES)
4289 have_content = 1;
4290 else if (choice == GOT_PATCH_CHOICE_QUIT)
4291 break;
4293 if (have_content) {
4294 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4295 reverse_patch ? NULL : outfile,
4296 reverse_patch ? outfile : NULL);
4297 if (err)
4298 goto done;
4300 if (!S_ISLNK(sb2.st_mode)) {
4301 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4302 err = got_error_from_errno2("chmod", path2);
4303 goto done;
4307 done:
4308 free(id_str);
4309 if (blob)
4310 got_object_blob_close(blob);
4311 if (f1 && fclose(f1) == EOF && err == NULL)
4312 err = got_error_from_errno2("fclose", path1);
4313 if (f2 && fclose(f2) == EOF && err == NULL)
4314 err = got_error_from_errno2("fclose", path2);
4315 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4316 err = got_error_from_errno2("close", path2);
4317 if (outfile && fclose(outfile) == EOF && err == NULL)
4318 err = got_error_from_errno2("fclose", *path_outfile);
4319 if (path1 && unlink(path1) == -1 && err == NULL)
4320 err = got_error_from_errno2("unlink", path1);
4321 if (err || !have_content) {
4322 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4323 err = got_error_from_errno2("unlink", *path_outfile);
4324 free(*path_outfile);
4325 *path_outfile = NULL;
4327 free(args);
4328 if (ds) {
4329 got_diff_state_free(ds);
4330 free(ds);
4332 if (changes)
4333 got_diff_free_changes(changes);
4334 free(path1);
4335 return err;
4338 static const struct got_error *
4339 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4340 const char *relpath, struct got_object_id *blob_id,
4341 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4342 int dirfd, const char *de_name)
4344 struct revert_file_args *a = arg;
4345 const struct got_error *err = NULL;
4346 char *parent_path = NULL;
4347 struct got_fileindex_entry *ie;
4348 struct got_tree_object *tree = NULL;
4349 struct got_object_id *tree_id = NULL;
4350 const struct got_tree_entry *te = NULL;
4351 char *tree_path = NULL, *te_name;
4352 char *ondisk_path = NULL, *path_content = NULL;
4353 struct got_blob_object *blob = NULL;
4355 /* Reverting a staged deletion is a no-op. */
4356 if (status == GOT_STATUS_DELETE &&
4357 staged_status != GOT_STATUS_NO_CHANGE)
4358 return NULL;
4360 if (status == GOT_STATUS_UNVERSIONED)
4361 return (*a->progress_cb)(a->progress_arg,
4362 GOT_STATUS_UNVERSIONED, relpath);
4364 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4365 if (ie == NULL)
4366 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4368 /* Construct in-repository path of tree which contains this blob. */
4369 err = got_path_dirname(&parent_path, ie->path);
4370 if (err) {
4371 if (err->code != GOT_ERR_BAD_PATH)
4372 goto done;
4373 parent_path = strdup("/");
4374 if (parent_path == NULL) {
4375 err = got_error_from_errno("strdup");
4376 goto done;
4379 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4380 tree_path = strdup(parent_path);
4381 if (tree_path == NULL) {
4382 err = got_error_from_errno("strdup");
4383 goto done;
4385 } else {
4386 if (got_path_is_root_dir(parent_path)) {
4387 tree_path = strdup(a->worktree->path_prefix);
4388 if (tree_path == NULL) {
4389 err = got_error_from_errno("strdup");
4390 goto done;
4392 } else {
4393 if (asprintf(&tree_path, "%s/%s",
4394 a->worktree->path_prefix, parent_path) == -1) {
4395 err = got_error_from_errno("asprintf");
4396 goto done;
4401 err = got_object_id_by_path(&tree_id, a->repo,
4402 a->worktree->base_commit_id, tree_path);
4403 if (err) {
4404 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4405 (status == GOT_STATUS_ADD ||
4406 staged_status == GOT_STATUS_ADD)))
4407 goto done;
4408 } else {
4409 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4410 if (err)
4411 goto done;
4413 err = got_path_basename(&te_name, ie->path);
4414 if (err)
4415 goto done;
4417 te = got_object_tree_find_entry(tree, te_name);
4418 free(te_name);
4419 if (te == NULL && status != GOT_STATUS_ADD &&
4420 staged_status != GOT_STATUS_ADD) {
4421 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4422 goto done;
4426 switch (status) {
4427 case GOT_STATUS_ADD:
4428 if (a->patch_cb) {
4429 int choice = GOT_PATCH_CHOICE_NONE;
4430 err = (*a->patch_cb)(&choice, a->patch_arg,
4431 status, ie->path, NULL, 1, 1);
4432 if (err)
4433 goto done;
4434 if (choice != GOT_PATCH_CHOICE_YES)
4435 break;
4437 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4438 ie->path);
4439 if (err)
4440 goto done;
4441 got_fileindex_entry_remove(a->fileindex, ie);
4442 break;
4443 case GOT_STATUS_DELETE:
4444 if (a->patch_cb) {
4445 int choice = GOT_PATCH_CHOICE_NONE;
4446 err = (*a->patch_cb)(&choice, a->patch_arg,
4447 status, ie->path, NULL, 1, 1);
4448 if (err)
4449 goto done;
4450 if (choice != GOT_PATCH_CHOICE_YES)
4451 break;
4453 /* fall through */
4454 case GOT_STATUS_MODIFY:
4455 case GOT_STATUS_MODE_CHANGE:
4456 case GOT_STATUS_CONFLICT:
4457 case GOT_STATUS_MISSING: {
4458 struct got_object_id id;
4459 if (staged_status == GOT_STATUS_ADD ||
4460 staged_status == GOT_STATUS_MODIFY) {
4461 memcpy(id.sha1, ie->staged_blob_sha1,
4462 SHA1_DIGEST_LENGTH);
4463 } else
4464 memcpy(id.sha1, ie->blob_sha1,
4465 SHA1_DIGEST_LENGTH);
4466 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4467 if (err)
4468 goto done;
4470 if (asprintf(&ondisk_path, "%s/%s",
4471 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4472 err = got_error_from_errno("asprintf");
4473 goto done;
4476 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4477 status == GOT_STATUS_CONFLICT)) {
4478 int is_bad_symlink = 0;
4479 err = create_patched_content(&path_content, 1, &id,
4480 ondisk_path, dirfd, de_name, ie->path, a->repo,
4481 a->patch_cb, a->patch_arg);
4482 if (err || path_content == NULL)
4483 break;
4484 if (te && S_ISLNK(te->mode)) {
4485 if (unlink(path_content) == -1) {
4486 err = got_error_from_errno2("unlink",
4487 path_content);
4488 break;
4490 err = install_symlink(&is_bad_symlink,
4491 a->worktree, ondisk_path, ie->path,
4492 blob, 0, 1, 0, a->repo,
4493 a->progress_cb, a->progress_arg);
4494 } else {
4495 if (rename(path_content, ondisk_path) == -1) {
4496 err = got_error_from_errno3("rename",
4497 path_content, ondisk_path);
4498 goto done;
4501 } else {
4502 int is_bad_symlink = 0;
4503 if (te && S_ISLNK(te->mode)) {
4504 err = install_symlink(&is_bad_symlink,
4505 a->worktree, ondisk_path, ie->path,
4506 blob, 0, 1, 0, a->repo,
4507 a->progress_cb, a->progress_arg);
4508 } else {
4509 err = install_blob(a->worktree, ondisk_path,
4510 ie->path,
4511 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4512 got_fileindex_perms_to_st(ie), blob,
4513 0, 1, 0, 0, a->repo,
4514 a->progress_cb, a->progress_arg);
4516 if (err)
4517 goto done;
4518 if (status == GOT_STATUS_DELETE ||
4519 status == GOT_STATUS_MODE_CHANGE) {
4520 err = got_fileindex_entry_update(ie,
4521 ondisk_path, blob->id.sha1,
4522 a->worktree->base_commit_id->sha1, 1);
4523 if (err)
4524 goto done;
4526 if (is_bad_symlink) {
4527 got_fileindex_entry_filetype_set(ie,
4528 GOT_FILEIDX_MODE_BAD_SYMLINK);
4531 break;
4533 default:
4534 break;
4536 done:
4537 free(ondisk_path);
4538 free(path_content);
4539 free(parent_path);
4540 free(tree_path);
4541 if (blob)
4542 got_object_blob_close(blob);
4543 if (tree)
4544 got_object_tree_close(tree);
4545 free(tree_id);
4546 return err;
4549 const struct got_error *
4550 got_worktree_revert(struct got_worktree *worktree,
4551 struct got_pathlist_head *paths,
4552 got_worktree_checkout_cb progress_cb, void *progress_arg,
4553 got_worktree_patch_cb patch_cb, void *patch_arg,
4554 struct got_repository *repo)
4556 struct got_fileindex *fileindex = NULL;
4557 char *fileindex_path = NULL;
4558 const struct got_error *err = NULL, *unlockerr = NULL;
4559 const struct got_error *sync_err = NULL;
4560 struct got_pathlist_entry *pe;
4561 struct revert_file_args rfa;
4563 err = lock_worktree(worktree, LOCK_EX);
4564 if (err)
4565 return err;
4567 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4568 if (err)
4569 goto done;
4571 rfa.worktree = worktree;
4572 rfa.fileindex = fileindex;
4573 rfa.progress_cb = progress_cb;
4574 rfa.progress_arg = progress_arg;
4575 rfa.patch_cb = patch_cb;
4576 rfa.patch_arg = patch_arg;
4577 rfa.repo = repo;
4578 TAILQ_FOREACH(pe, paths, entry) {
4579 err = worktree_status(worktree, pe->path, fileindex, repo,
4580 revert_file, &rfa, NULL, NULL, 0, 0);
4581 if (err)
4582 break;
4584 sync_err = sync_fileindex(fileindex, fileindex_path);
4585 if (sync_err && err == NULL)
4586 err = sync_err;
4587 done:
4588 free(fileindex_path);
4589 if (fileindex)
4590 got_fileindex_free(fileindex);
4591 unlockerr = lock_worktree(worktree, LOCK_SH);
4592 if (unlockerr && err == NULL)
4593 err = unlockerr;
4594 return err;
4597 static void
4598 free_commitable(struct got_commitable *ct)
4600 free(ct->path);
4601 free(ct->in_repo_path);
4602 free(ct->ondisk_path);
4603 free(ct->blob_id);
4604 free(ct->base_blob_id);
4605 free(ct->staged_blob_id);
4606 free(ct->base_commit_id);
4607 free(ct);
4610 struct collect_commitables_arg {
4611 struct got_pathlist_head *commitable_paths;
4612 struct got_repository *repo;
4613 struct got_worktree *worktree;
4614 struct got_fileindex *fileindex;
4615 int have_staged_files;
4616 int allow_bad_symlinks;
4619 static const struct got_error *
4620 collect_commitables(void *arg, unsigned char status,
4621 unsigned char staged_status, const char *relpath,
4622 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4623 struct got_object_id *commit_id, int dirfd, const char *de_name)
4625 struct collect_commitables_arg *a = arg;
4626 const struct got_error *err = NULL;
4627 struct got_commitable *ct = NULL;
4628 struct got_pathlist_entry *new = NULL;
4629 char *parent_path = NULL, *path = NULL;
4630 struct stat sb;
4632 if (a->have_staged_files) {
4633 if (staged_status != GOT_STATUS_MODIFY &&
4634 staged_status != GOT_STATUS_ADD &&
4635 staged_status != GOT_STATUS_DELETE)
4636 return NULL;
4637 } else {
4638 if (status == GOT_STATUS_CONFLICT)
4639 return got_error(GOT_ERR_COMMIT_CONFLICT);
4641 if (status != GOT_STATUS_MODIFY &&
4642 status != GOT_STATUS_MODE_CHANGE &&
4643 status != GOT_STATUS_ADD &&
4644 status != GOT_STATUS_DELETE)
4645 return NULL;
4648 if (asprintf(&path, "/%s", relpath) == -1) {
4649 err = got_error_from_errno("asprintf");
4650 goto done;
4652 if (strcmp(path, "/") == 0) {
4653 parent_path = strdup("");
4654 if (parent_path == NULL)
4655 return got_error_from_errno("strdup");
4656 } else {
4657 err = got_path_dirname(&parent_path, path);
4658 if (err)
4659 return err;
4662 ct = calloc(1, sizeof(*ct));
4663 if (ct == NULL) {
4664 err = got_error_from_errno("calloc");
4665 goto done;
4668 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4669 relpath) == -1) {
4670 err = got_error_from_errno("asprintf");
4671 goto done;
4674 if (staged_status == GOT_STATUS_ADD ||
4675 staged_status == GOT_STATUS_MODIFY) {
4676 struct got_fileindex_entry *ie;
4677 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4678 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4679 case GOT_FILEIDX_MODE_REGULAR_FILE:
4680 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4681 ct->mode = S_IFREG;
4682 break;
4683 case GOT_FILEIDX_MODE_SYMLINK:
4684 ct->mode = S_IFLNK;
4685 break;
4686 default:
4687 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4688 goto done;
4690 ct->mode |= got_fileindex_entry_perms_get(ie);
4691 } else if (status != GOT_STATUS_DELETE &&
4692 staged_status != GOT_STATUS_DELETE) {
4693 if (dirfd != -1) {
4694 if (fstatat(dirfd, de_name, &sb,
4695 AT_SYMLINK_NOFOLLOW) == -1) {
4696 err = got_error_from_errno2("fstatat",
4697 ct->ondisk_path);
4698 goto done;
4700 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4701 err = got_error_from_errno2("lstat", ct->ondisk_path);
4702 goto done;
4704 ct->mode = sb.st_mode;
4707 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4708 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4709 relpath) == -1) {
4710 err = got_error_from_errno("asprintf");
4711 goto done;
4714 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4715 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4716 int is_bad_symlink;
4717 char target_path[PATH_MAX];
4718 ssize_t target_len;
4719 target_len = readlink(ct->ondisk_path, target_path,
4720 sizeof(target_path));
4721 if (target_len == -1) {
4722 err = got_error_from_errno2("readlink",
4723 ct->ondisk_path);
4724 goto done;
4726 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4727 target_len, ct->ondisk_path, a->worktree->root_path);
4728 if (err)
4729 goto done;
4730 if (is_bad_symlink) {
4731 err = got_error_path(ct->ondisk_path,
4732 GOT_ERR_BAD_SYMLINK);
4733 goto done;
4738 ct->status = status;
4739 ct->staged_status = staged_status;
4740 ct->blob_id = NULL; /* will be filled in when blob gets created */
4741 if (ct->status != GOT_STATUS_ADD &&
4742 ct->staged_status != GOT_STATUS_ADD) {
4743 ct->base_blob_id = got_object_id_dup(blob_id);
4744 if (ct->base_blob_id == NULL) {
4745 err = got_error_from_errno("got_object_id_dup");
4746 goto done;
4748 ct->base_commit_id = got_object_id_dup(commit_id);
4749 if (ct->base_commit_id == NULL) {
4750 err = got_error_from_errno("got_object_id_dup");
4751 goto done;
4754 if (ct->staged_status == GOT_STATUS_ADD ||
4755 ct->staged_status == GOT_STATUS_MODIFY) {
4756 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4757 if (ct->staged_blob_id == NULL) {
4758 err = got_error_from_errno("got_object_id_dup");
4759 goto done;
4762 ct->path = strdup(path);
4763 if (ct->path == NULL) {
4764 err = got_error_from_errno("strdup");
4765 goto done;
4767 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4768 done:
4769 if (ct && (err || new == NULL))
4770 free_commitable(ct);
4771 free(parent_path);
4772 free(path);
4773 return err;
4776 static const struct got_error *write_tree(struct got_object_id **, int *,
4777 struct got_tree_object *, const char *, struct got_pathlist_head *,
4778 got_worktree_status_cb status_cb, void *status_arg,
4779 struct got_repository *);
4781 static const struct got_error *
4782 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4783 struct got_tree_entry *te, const char *parent_path,
4784 struct got_pathlist_head *commitable_paths,
4785 got_worktree_status_cb status_cb, void *status_arg,
4786 struct got_repository *repo)
4788 const struct got_error *err = NULL;
4789 struct got_tree_object *subtree;
4790 char *subpath;
4792 if (asprintf(&subpath, "%s%s%s", parent_path,
4793 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4794 return got_error_from_errno("asprintf");
4796 err = got_object_open_as_tree(&subtree, repo, &te->id);
4797 if (err)
4798 return err;
4800 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4801 commitable_paths, status_cb, status_arg, repo);
4802 got_object_tree_close(subtree);
4803 free(subpath);
4804 return err;
4807 static const struct got_error *
4808 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4810 const struct got_error *err = NULL;
4811 char *ct_parent_path = NULL;
4813 *match = 0;
4815 if (strchr(ct->in_repo_path, '/') == NULL) {
4816 *match = got_path_is_root_dir(path);
4817 return NULL;
4820 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4821 if (err)
4822 return err;
4823 *match = (strcmp(path, ct_parent_path) == 0);
4824 free(ct_parent_path);
4825 return err;
4828 static mode_t
4829 get_ct_file_mode(struct got_commitable *ct)
4831 if (S_ISLNK(ct->mode))
4832 return S_IFLNK;
4834 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4837 static const struct got_error *
4838 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4839 struct got_tree_entry *te, struct got_commitable *ct)
4841 const struct got_error *err = NULL;
4843 *new_te = NULL;
4845 err = got_object_tree_entry_dup(new_te, te);
4846 if (err)
4847 goto done;
4849 (*new_te)->mode = get_ct_file_mode(ct);
4851 if (ct->staged_status == GOT_STATUS_MODIFY)
4852 memcpy(&(*new_te)->id, ct->staged_blob_id,
4853 sizeof((*new_te)->id));
4854 else
4855 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4856 done:
4857 if (err && *new_te) {
4858 free(*new_te);
4859 *new_te = NULL;
4861 return err;
4864 static const struct got_error *
4865 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4866 struct got_commitable *ct)
4868 const struct got_error *err = NULL;
4869 char *ct_name = NULL;
4871 *new_te = NULL;
4873 *new_te = calloc(1, sizeof(**new_te));
4874 if (*new_te == NULL)
4875 return got_error_from_errno("calloc");
4877 err = got_path_basename(&ct_name, ct->path);
4878 if (err)
4879 goto done;
4880 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4881 sizeof((*new_te)->name)) {
4882 err = got_error(GOT_ERR_NO_SPACE);
4883 goto done;
4886 (*new_te)->mode = get_ct_file_mode(ct);
4888 if (ct->staged_status == GOT_STATUS_ADD)
4889 memcpy(&(*new_te)->id, ct->staged_blob_id,
4890 sizeof((*new_te)->id));
4891 else
4892 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4893 done:
4894 free(ct_name);
4895 if (err && *new_te) {
4896 free(*new_te);
4897 *new_te = NULL;
4899 return err;
4902 static const struct got_error *
4903 insert_tree_entry(struct got_tree_entry *new_te,
4904 struct got_pathlist_head *paths)
4906 const struct got_error *err = NULL;
4907 struct got_pathlist_entry *new_pe;
4909 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4910 if (err)
4911 return err;
4912 if (new_pe == NULL)
4913 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4914 return NULL;
4917 static const struct got_error *
4918 report_ct_status(struct got_commitable *ct,
4919 got_worktree_status_cb status_cb, void *status_arg)
4921 const char *ct_path = ct->path;
4922 unsigned char status;
4924 while (ct_path[0] == '/')
4925 ct_path++;
4927 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4928 status = ct->staged_status;
4929 else
4930 status = ct->status;
4932 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4933 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4936 static const struct got_error *
4937 match_modified_subtree(int *modified, struct got_tree_entry *te,
4938 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4940 const struct got_error *err = NULL;
4941 struct got_pathlist_entry *pe;
4942 char *te_path;
4944 *modified = 0;
4946 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4947 got_path_is_root_dir(base_tree_path) ? "" : "/",
4948 te->name) == -1)
4949 return got_error_from_errno("asprintf");
4951 TAILQ_FOREACH(pe, commitable_paths, entry) {
4952 struct got_commitable *ct = pe->data;
4953 *modified = got_path_is_child(ct->in_repo_path, te_path,
4954 strlen(te_path));
4955 if (*modified)
4956 break;
4959 free(te_path);
4960 return err;
4963 static const struct got_error *
4964 match_deleted_or_modified_ct(struct got_commitable **ctp,
4965 struct got_tree_entry *te, const char *base_tree_path,
4966 struct got_pathlist_head *commitable_paths)
4968 const struct got_error *err = NULL;
4969 struct got_pathlist_entry *pe;
4971 *ctp = NULL;
4973 TAILQ_FOREACH(pe, commitable_paths, entry) {
4974 struct got_commitable *ct = pe->data;
4975 char *ct_name = NULL;
4976 int path_matches;
4978 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4979 if (ct->status != GOT_STATUS_MODIFY &&
4980 ct->status != GOT_STATUS_MODE_CHANGE &&
4981 ct->status != GOT_STATUS_DELETE)
4982 continue;
4983 } else {
4984 if (ct->staged_status != GOT_STATUS_MODIFY &&
4985 ct->staged_status != GOT_STATUS_DELETE)
4986 continue;
4989 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4990 continue;
4992 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4993 if (err)
4994 return err;
4995 if (!path_matches)
4996 continue;
4998 err = got_path_basename(&ct_name, pe->path);
4999 if (err)
5000 return err;
5002 if (strcmp(te->name, ct_name) != 0) {
5003 free(ct_name);
5004 continue;
5006 free(ct_name);
5008 *ctp = ct;
5009 break;
5012 return err;
5015 static const struct got_error *
5016 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5017 const char *child_path, const char *path_base_tree,
5018 struct got_pathlist_head *commitable_paths,
5019 got_worktree_status_cb status_cb, void *status_arg,
5020 struct got_repository *repo)
5022 const struct got_error *err = NULL;
5023 struct got_tree_entry *new_te;
5024 char *subtree_path;
5025 struct got_object_id *id = NULL;
5026 int nentries;
5028 *new_tep = NULL;
5030 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5031 got_path_is_root_dir(path_base_tree) ? "" : "/",
5032 child_path) == -1)
5033 return got_error_from_errno("asprintf");
5035 new_te = calloc(1, sizeof(*new_te));
5036 if (new_te == NULL)
5037 return got_error_from_errno("calloc");
5038 new_te->mode = S_IFDIR;
5040 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5041 sizeof(new_te->name)) {
5042 err = got_error(GOT_ERR_NO_SPACE);
5043 goto done;
5045 err = write_tree(&id, &nentries, NULL, subtree_path,
5046 commitable_paths, status_cb, status_arg, repo);
5047 if (err) {
5048 free(new_te);
5049 goto done;
5051 memcpy(&new_te->id, id, sizeof(new_te->id));
5052 done:
5053 free(id);
5054 free(subtree_path);
5055 if (err == NULL)
5056 *new_tep = new_te;
5057 return err;
5060 static const struct got_error *
5061 write_tree(struct got_object_id **new_tree_id, int *nentries,
5062 struct got_tree_object *base_tree, const char *path_base_tree,
5063 struct got_pathlist_head *commitable_paths,
5064 got_worktree_status_cb status_cb, void *status_arg,
5065 struct got_repository *repo)
5067 const struct got_error *err = NULL;
5068 struct got_pathlist_head paths;
5069 struct got_tree_entry *te, *new_te = NULL;
5070 struct got_pathlist_entry *pe;
5072 TAILQ_INIT(&paths);
5073 *nentries = 0;
5075 /* Insert, and recurse into, newly added entries first. */
5076 TAILQ_FOREACH(pe, commitable_paths, entry) {
5077 struct got_commitable *ct = pe->data;
5078 char *child_path = NULL, *slash;
5080 if ((ct->status != GOT_STATUS_ADD &&
5081 ct->staged_status != GOT_STATUS_ADD) ||
5082 (ct->flags & GOT_COMMITABLE_ADDED))
5083 continue;
5085 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5086 strlen(path_base_tree)))
5087 continue;
5089 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5090 ct->in_repo_path);
5091 if (err)
5092 goto done;
5094 slash = strchr(child_path, '/');
5095 if (slash == NULL) {
5096 err = alloc_added_blob_tree_entry(&new_te, ct);
5097 if (err)
5098 goto done;
5099 err = report_ct_status(ct, status_cb, status_arg);
5100 if (err)
5101 goto done;
5102 ct->flags |= GOT_COMMITABLE_ADDED;
5103 err = insert_tree_entry(new_te, &paths);
5104 if (err)
5105 goto done;
5106 (*nentries)++;
5107 } else {
5108 *slash = '\0'; /* trim trailing path components */
5109 if (base_tree == NULL ||
5110 got_object_tree_find_entry(base_tree, child_path)
5111 == NULL) {
5112 err = make_subtree_for_added_blob(&new_te,
5113 child_path, path_base_tree,
5114 commitable_paths, status_cb, status_arg,
5115 repo);
5116 if (err)
5117 goto done;
5118 err = insert_tree_entry(new_te, &paths);
5119 if (err)
5120 goto done;
5121 (*nentries)++;
5126 if (base_tree) {
5127 int i, nbase_entries;
5128 /* Handle modified and deleted entries. */
5129 nbase_entries = got_object_tree_get_nentries(base_tree);
5130 for (i = 0; i < nbase_entries; i++) {
5131 struct got_commitable *ct = NULL;
5133 te = got_object_tree_get_entry(base_tree, i);
5134 if (got_object_tree_entry_is_submodule(te)) {
5135 /* Entry is a submodule; just copy it. */
5136 err = got_object_tree_entry_dup(&new_te, te);
5137 if (err)
5138 goto done;
5139 err = insert_tree_entry(new_te, &paths);
5140 if (err)
5141 goto done;
5142 (*nentries)++;
5143 continue;
5146 if (S_ISDIR(te->mode)) {
5147 int modified;
5148 err = got_object_tree_entry_dup(&new_te, te);
5149 if (err)
5150 goto done;
5151 err = match_modified_subtree(&modified, te,
5152 path_base_tree, commitable_paths);
5153 if (err)
5154 goto done;
5155 /* Avoid recursion into unmodified subtrees. */
5156 if (modified) {
5157 struct got_object_id *new_id;
5158 int nsubentries;
5159 err = write_subtree(&new_id,
5160 &nsubentries, te,
5161 path_base_tree, commitable_paths,
5162 status_cb, status_arg, repo);
5163 if (err)
5164 goto done;
5165 if (nsubentries == 0) {
5166 /* All entries were deleted. */
5167 free(new_id);
5168 continue;
5170 memcpy(&new_te->id, new_id,
5171 sizeof(new_te->id));
5172 free(new_id);
5174 err = insert_tree_entry(new_te, &paths);
5175 if (err)
5176 goto done;
5177 (*nentries)++;
5178 continue;
5181 err = match_deleted_or_modified_ct(&ct, te,
5182 path_base_tree, commitable_paths);
5183 if (err)
5184 goto done;
5185 if (ct) {
5186 /* NB: Deleted entries get dropped here. */
5187 if (ct->status == GOT_STATUS_MODIFY ||
5188 ct->status == GOT_STATUS_MODE_CHANGE ||
5189 ct->staged_status == GOT_STATUS_MODIFY) {
5190 err = alloc_modified_blob_tree_entry(
5191 &new_te, te, ct);
5192 if (err)
5193 goto done;
5194 err = insert_tree_entry(new_te, &paths);
5195 if (err)
5196 goto done;
5197 (*nentries)++;
5199 err = report_ct_status(ct, status_cb,
5200 status_arg);
5201 if (err)
5202 goto done;
5203 } else {
5204 /* Entry is unchanged; just copy it. */
5205 err = got_object_tree_entry_dup(&new_te, te);
5206 if (err)
5207 goto done;
5208 err = insert_tree_entry(new_te, &paths);
5209 if (err)
5210 goto done;
5211 (*nentries)++;
5216 /* Write new list of entries; deleted entries have been dropped. */
5217 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5218 done:
5219 got_pathlist_free(&paths);
5220 return err;
5223 static const struct got_error *
5224 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5225 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5226 int have_staged_files)
5228 const struct got_error *err = NULL;
5229 struct got_pathlist_entry *pe;
5231 TAILQ_FOREACH(pe, commitable_paths, entry) {
5232 struct got_fileindex_entry *ie;
5233 struct got_commitable *ct = pe->data;
5235 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5236 if (ie) {
5237 if (ct->status == GOT_STATUS_DELETE ||
5238 ct->staged_status == GOT_STATUS_DELETE) {
5239 got_fileindex_entry_remove(fileindex, ie);
5240 } else if (ct->staged_status == GOT_STATUS_ADD ||
5241 ct->staged_status == GOT_STATUS_MODIFY) {
5242 got_fileindex_entry_stage_set(ie,
5243 GOT_FILEIDX_STAGE_NONE);
5244 err = got_fileindex_entry_update(ie,
5245 ct->ondisk_path, ct->staged_blob_id->sha1,
5246 new_base_commit_id->sha1,
5247 !have_staged_files);
5248 } else
5249 err = got_fileindex_entry_update(ie,
5250 ct->ondisk_path, ct->blob_id->sha1,
5251 new_base_commit_id->sha1,
5252 !have_staged_files);
5253 } else {
5254 err = got_fileindex_entry_alloc(&ie, pe->path);
5255 if (err)
5256 break;
5257 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5258 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5259 if (err) {
5260 got_fileindex_entry_free(ie);
5261 break;
5263 err = got_fileindex_entry_add(fileindex, ie);
5264 if (err) {
5265 got_fileindex_entry_free(ie);
5266 break;
5270 return err;
5274 static const struct got_error *
5275 check_out_of_date(const char *in_repo_path, unsigned char status,
5276 unsigned char staged_status, struct got_object_id *base_blob_id,
5277 struct got_object_id *base_commit_id,
5278 struct got_object_id *head_commit_id, struct got_repository *repo,
5279 int ood_errcode)
5281 const struct got_error *err = NULL;
5282 struct got_object_id *id = NULL;
5284 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5285 /* Trivial case: base commit == head commit */
5286 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5287 return NULL;
5289 * Ensure file content which local changes were based
5290 * on matches file content in the branch head.
5292 err = got_object_id_by_path(&id, repo, head_commit_id,
5293 in_repo_path);
5294 if (err) {
5295 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5296 err = got_error(ood_errcode);
5297 goto done;
5298 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5299 err = got_error(ood_errcode);
5300 } else {
5301 /* Require that added files don't exist in the branch head. */
5302 err = got_object_id_by_path(&id, repo, head_commit_id,
5303 in_repo_path);
5304 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5305 goto done;
5306 err = id ? got_error(ood_errcode) : NULL;
5308 done:
5309 free(id);
5310 return err;
5313 const struct got_error *
5314 commit_worktree(struct got_object_id **new_commit_id,
5315 struct got_pathlist_head *commitable_paths,
5316 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5317 const char *author, const char *committer,
5318 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5319 got_worktree_status_cb status_cb, void *status_arg,
5320 struct got_repository *repo)
5322 const struct got_error *err = NULL, *unlockerr = NULL;
5323 struct got_pathlist_entry *pe;
5324 const char *head_ref_name = NULL;
5325 struct got_commit_object *head_commit = NULL;
5326 struct got_reference *head_ref2 = NULL;
5327 struct got_object_id *head_commit_id2 = NULL;
5328 struct got_tree_object *head_tree = NULL;
5329 struct got_object_id *new_tree_id = NULL;
5330 int nentries;
5331 struct got_object_id_queue parent_ids;
5332 struct got_object_qid *pid = NULL;
5333 char *logmsg = NULL;
5335 *new_commit_id = NULL;
5337 SIMPLEQ_INIT(&parent_ids);
5339 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5340 if (err)
5341 goto done;
5343 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5344 if (err)
5345 goto done;
5347 if (commit_msg_cb != NULL) {
5348 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5349 if (err)
5350 goto done;
5353 if (logmsg == NULL || strlen(logmsg) == 0) {
5354 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5355 goto done;
5358 /* Create blobs from added and modified files and record their IDs. */
5359 TAILQ_FOREACH(pe, commitable_paths, entry) {
5360 struct got_commitable *ct = pe->data;
5361 char *ondisk_path;
5363 /* Blobs for staged files already exist. */
5364 if (ct->staged_status == GOT_STATUS_ADD ||
5365 ct->staged_status == GOT_STATUS_MODIFY)
5366 continue;
5368 if (ct->status != GOT_STATUS_ADD &&
5369 ct->status != GOT_STATUS_MODIFY &&
5370 ct->status != GOT_STATUS_MODE_CHANGE)
5371 continue;
5373 if (asprintf(&ondisk_path, "%s/%s",
5374 worktree->root_path, pe->path) == -1) {
5375 err = got_error_from_errno("asprintf");
5376 goto done;
5378 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5379 free(ondisk_path);
5380 if (err)
5381 goto done;
5384 /* Recursively write new tree objects. */
5385 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5386 commitable_paths, status_cb, status_arg, repo);
5387 if (err)
5388 goto done;
5390 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5391 if (err)
5392 goto done;
5393 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5394 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5395 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5396 got_object_qid_free(pid);
5397 if (logmsg != NULL)
5398 free(logmsg);
5399 if (err)
5400 goto done;
5402 /* Check if a concurrent commit to our branch has occurred. */
5403 head_ref_name = got_worktree_get_head_ref_name(worktree);
5404 if (head_ref_name == NULL) {
5405 err = got_error_from_errno("got_worktree_get_head_ref_name");
5406 goto done;
5408 /* Lock the reference here to prevent concurrent modification. */
5409 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5410 if (err)
5411 goto done;
5412 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5413 if (err)
5414 goto done;
5415 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5416 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5417 goto done;
5419 /* Update branch head in repository. */
5420 err = got_ref_change_ref(head_ref2, *new_commit_id);
5421 if (err)
5422 goto done;
5423 err = got_ref_write(head_ref2, repo);
5424 if (err)
5425 goto done;
5427 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5428 if (err)
5429 goto done;
5431 err = ref_base_commit(worktree, repo);
5432 if (err)
5433 goto done;
5434 done:
5435 if (head_tree)
5436 got_object_tree_close(head_tree);
5437 if (head_commit)
5438 got_object_commit_close(head_commit);
5439 free(head_commit_id2);
5440 if (head_ref2) {
5441 unlockerr = got_ref_unlock(head_ref2);
5442 if (unlockerr && err == NULL)
5443 err = unlockerr;
5444 got_ref_close(head_ref2);
5446 return err;
5449 static const struct got_error *
5450 check_path_is_commitable(const char *path,
5451 struct got_pathlist_head *commitable_paths)
5453 struct got_pathlist_entry *cpe = NULL;
5454 size_t path_len = strlen(path);
5456 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5457 struct got_commitable *ct = cpe->data;
5458 const char *ct_path = ct->path;
5460 while (ct_path[0] == '/')
5461 ct_path++;
5463 if (strcmp(path, ct_path) == 0 ||
5464 got_path_is_child(ct_path, path, path_len))
5465 break;
5468 if (cpe == NULL)
5469 return got_error_path(path, GOT_ERR_BAD_PATH);
5471 return NULL;
5474 static const struct got_error *
5475 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5477 int *have_staged_files = arg;
5479 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5480 *have_staged_files = 1;
5481 return got_error(GOT_ERR_CANCELLED);
5484 return NULL;
5487 static const struct got_error *
5488 check_non_staged_files(struct got_fileindex *fileindex,
5489 struct got_pathlist_head *paths)
5491 struct got_pathlist_entry *pe;
5492 struct got_fileindex_entry *ie;
5494 TAILQ_FOREACH(pe, paths, entry) {
5495 if (pe->path[0] == '\0')
5496 continue;
5497 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5498 if (ie == NULL)
5499 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5500 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5501 return got_error_path(pe->path,
5502 GOT_ERR_FILE_NOT_STAGED);
5505 return NULL;
5508 const struct got_error *
5509 got_worktree_commit(struct got_object_id **new_commit_id,
5510 struct got_worktree *worktree, struct got_pathlist_head *paths,
5511 const char *author, const char *committer, int allow_bad_symlinks,
5512 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5513 got_worktree_status_cb status_cb, void *status_arg,
5514 struct got_repository *repo)
5516 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5517 struct got_fileindex *fileindex = NULL;
5518 char *fileindex_path = NULL;
5519 struct got_pathlist_head commitable_paths;
5520 struct collect_commitables_arg cc_arg;
5521 struct got_pathlist_entry *pe;
5522 struct got_reference *head_ref = NULL;
5523 struct got_object_id *head_commit_id = NULL;
5524 int have_staged_files = 0;
5526 *new_commit_id = NULL;
5528 TAILQ_INIT(&commitable_paths);
5530 err = lock_worktree(worktree, LOCK_EX);
5531 if (err)
5532 goto done;
5534 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5535 if (err)
5536 goto done;
5538 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5539 if (err)
5540 goto done;
5542 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5543 if (err)
5544 goto done;
5546 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5547 &have_staged_files);
5548 if (err && err->code != GOT_ERR_CANCELLED)
5549 goto done;
5550 if (have_staged_files) {
5551 err = check_non_staged_files(fileindex, paths);
5552 if (err)
5553 goto done;
5556 cc_arg.commitable_paths = &commitable_paths;
5557 cc_arg.worktree = worktree;
5558 cc_arg.fileindex = fileindex;
5559 cc_arg.repo = repo;
5560 cc_arg.have_staged_files = have_staged_files;
5561 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5562 TAILQ_FOREACH(pe, paths, entry) {
5563 err = worktree_status(worktree, pe->path, fileindex, repo,
5564 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5565 if (err)
5566 goto done;
5569 if (TAILQ_EMPTY(&commitable_paths)) {
5570 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5571 goto done;
5574 TAILQ_FOREACH(pe, paths, entry) {
5575 err = check_path_is_commitable(pe->path, &commitable_paths);
5576 if (err)
5577 goto done;
5580 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5581 struct got_commitable *ct = pe->data;
5582 const char *ct_path = ct->in_repo_path;
5584 while (ct_path[0] == '/')
5585 ct_path++;
5586 err = check_out_of_date(ct_path, ct->status,
5587 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5588 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5589 if (err)
5590 goto done;
5594 err = commit_worktree(new_commit_id, &commitable_paths,
5595 head_commit_id, worktree, author, committer,
5596 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5597 if (err)
5598 goto done;
5600 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5601 fileindex, have_staged_files);
5602 sync_err = sync_fileindex(fileindex, fileindex_path);
5603 if (sync_err && err == NULL)
5604 err = sync_err;
5605 done:
5606 if (fileindex)
5607 got_fileindex_free(fileindex);
5608 free(fileindex_path);
5609 unlockerr = lock_worktree(worktree, LOCK_SH);
5610 if (unlockerr && err == NULL)
5611 err = unlockerr;
5612 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5613 struct got_commitable *ct = pe->data;
5614 free_commitable(ct);
5616 got_pathlist_free(&commitable_paths);
5617 return err;
5620 const char *
5621 got_commitable_get_path(struct got_commitable *ct)
5623 return ct->path;
5626 unsigned int
5627 got_commitable_get_status(struct got_commitable *ct)
5629 return ct->status;
5632 struct check_rebase_ok_arg {
5633 struct got_worktree *worktree;
5634 struct got_repository *repo;
5637 static const struct got_error *
5638 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5640 const struct got_error *err = NULL;
5641 struct check_rebase_ok_arg *a = arg;
5642 unsigned char status;
5643 struct stat sb;
5644 char *ondisk_path;
5646 /* Reject rebase of a work tree with mixed base commits. */
5647 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5648 SHA1_DIGEST_LENGTH))
5649 return got_error(GOT_ERR_MIXED_COMMITS);
5651 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5652 == -1)
5653 return got_error_from_errno("asprintf");
5655 /* Reject rebase of a work tree with modified or staged files. */
5656 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5657 free(ondisk_path);
5658 if (err)
5659 return err;
5661 if (status != GOT_STATUS_NO_CHANGE)
5662 return got_error(GOT_ERR_MODIFIED);
5663 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5664 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5666 return NULL;
5669 const struct got_error *
5670 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5671 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5672 struct got_worktree *worktree, struct got_reference *branch,
5673 struct got_repository *repo)
5675 const struct got_error *err = NULL;
5676 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5677 char *branch_ref_name = NULL;
5678 char *fileindex_path = NULL;
5679 struct check_rebase_ok_arg ok_arg;
5680 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5681 struct got_object_id *wt_branch_tip = NULL;
5683 *new_base_branch_ref = NULL;
5684 *tmp_branch = NULL;
5685 *fileindex = NULL;
5687 err = lock_worktree(worktree, LOCK_EX);
5688 if (err)
5689 return err;
5691 err = open_fileindex(fileindex, &fileindex_path, worktree);
5692 if (err)
5693 goto done;
5695 ok_arg.worktree = worktree;
5696 ok_arg.repo = repo;
5697 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5698 &ok_arg);
5699 if (err)
5700 goto done;
5702 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5703 if (err)
5704 goto done;
5706 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5707 if (err)
5708 goto done;
5710 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5711 if (err)
5712 goto done;
5714 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5715 0);
5716 if (err)
5717 goto done;
5719 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5720 if (err)
5721 goto done;
5722 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5723 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5724 goto done;
5727 err = got_ref_alloc_symref(new_base_branch_ref,
5728 new_base_branch_ref_name, wt_branch);
5729 if (err)
5730 goto done;
5731 err = got_ref_write(*new_base_branch_ref, repo);
5732 if (err)
5733 goto done;
5735 /* TODO Lock original branch's ref while rebasing? */
5737 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5738 if (err)
5739 goto done;
5741 err = got_ref_write(branch_ref, repo);
5742 if (err)
5743 goto done;
5745 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5746 worktree->base_commit_id);
5747 if (err)
5748 goto done;
5749 err = got_ref_write(*tmp_branch, repo);
5750 if (err)
5751 goto done;
5753 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5754 if (err)
5755 goto done;
5756 done:
5757 free(fileindex_path);
5758 free(tmp_branch_name);
5759 free(new_base_branch_ref_name);
5760 free(branch_ref_name);
5761 if (branch_ref)
5762 got_ref_close(branch_ref);
5763 if (wt_branch)
5764 got_ref_close(wt_branch);
5765 free(wt_branch_tip);
5766 if (err) {
5767 if (*new_base_branch_ref) {
5768 got_ref_close(*new_base_branch_ref);
5769 *new_base_branch_ref = NULL;
5771 if (*tmp_branch) {
5772 got_ref_close(*tmp_branch);
5773 *tmp_branch = NULL;
5775 if (*fileindex) {
5776 got_fileindex_free(*fileindex);
5777 *fileindex = NULL;
5779 lock_worktree(worktree, LOCK_SH);
5781 return err;
5784 const struct got_error *
5785 got_worktree_rebase_continue(struct got_object_id **commit_id,
5786 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5787 struct got_reference **branch, struct got_fileindex **fileindex,
5788 struct got_worktree *worktree, struct got_repository *repo)
5790 const struct got_error *err;
5791 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5792 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5793 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5794 char *fileindex_path = NULL;
5795 int have_staged_files = 0;
5797 *commit_id = NULL;
5798 *new_base_branch = NULL;
5799 *tmp_branch = NULL;
5800 *branch = NULL;
5801 *fileindex = NULL;
5803 err = lock_worktree(worktree, LOCK_EX);
5804 if (err)
5805 return err;
5807 err = open_fileindex(fileindex, &fileindex_path, worktree);
5808 if (err)
5809 goto done;
5811 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5812 &have_staged_files);
5813 if (err && err->code != GOT_ERR_CANCELLED)
5814 goto done;
5815 if (have_staged_files) {
5816 err = got_error(GOT_ERR_STAGED_PATHS);
5817 goto done;
5820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5821 if (err)
5822 goto done;
5824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5825 if (err)
5826 goto done;
5828 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5829 if (err)
5830 goto done;
5832 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5833 if (err)
5834 goto done;
5836 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5837 if (err)
5838 goto done;
5840 err = got_ref_open(branch, repo,
5841 got_ref_get_symref_target(branch_ref), 0);
5842 if (err)
5843 goto done;
5845 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5846 if (err)
5847 goto done;
5849 err = got_ref_resolve(commit_id, repo, commit_ref);
5850 if (err)
5851 goto done;
5853 err = got_ref_open(new_base_branch, repo,
5854 new_base_branch_ref_name, 0);
5855 if (err)
5856 goto done;
5858 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5859 if (err)
5860 goto done;
5861 done:
5862 free(commit_ref_name);
5863 free(branch_ref_name);
5864 free(fileindex_path);
5865 if (commit_ref)
5866 got_ref_close(commit_ref);
5867 if (branch_ref)
5868 got_ref_close(branch_ref);
5869 if (err) {
5870 free(*commit_id);
5871 *commit_id = NULL;
5872 if (*tmp_branch) {
5873 got_ref_close(*tmp_branch);
5874 *tmp_branch = NULL;
5876 if (*new_base_branch) {
5877 got_ref_close(*new_base_branch);
5878 *new_base_branch = NULL;
5880 if (*branch) {
5881 got_ref_close(*branch);
5882 *branch = NULL;
5884 if (*fileindex) {
5885 got_fileindex_free(*fileindex);
5886 *fileindex = NULL;
5888 lock_worktree(worktree, LOCK_SH);
5890 return err;
5893 const struct got_error *
5894 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5896 const struct got_error *err;
5897 char *tmp_branch_name = NULL;
5899 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5900 if (err)
5901 return err;
5903 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5904 free(tmp_branch_name);
5905 return NULL;
5908 static const struct got_error *
5909 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5910 char **logmsg, void *arg)
5912 *logmsg = arg;
5913 return NULL;
5916 static const struct got_error *
5917 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5918 const char *path, struct got_object_id *blob_id,
5919 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5920 int dirfd, const char *de_name)
5922 return NULL;
5925 struct collect_merged_paths_arg {
5926 got_worktree_checkout_cb progress_cb;
5927 void *progress_arg;
5928 struct got_pathlist_head *merged_paths;
5931 static const struct got_error *
5932 collect_merged_paths(void *arg, unsigned char status, const char *path)
5934 const struct got_error *err;
5935 struct collect_merged_paths_arg *a = arg;
5936 char *p;
5937 struct got_pathlist_entry *new;
5939 err = (*a->progress_cb)(a->progress_arg, status, path);
5940 if (err)
5941 return err;
5943 if (status != GOT_STATUS_MERGE &&
5944 status != GOT_STATUS_ADD &&
5945 status != GOT_STATUS_DELETE &&
5946 status != GOT_STATUS_CONFLICT)
5947 return NULL;
5949 p = strdup(path);
5950 if (p == NULL)
5951 return got_error_from_errno("strdup");
5953 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5954 if (err || new == NULL)
5955 free(p);
5956 return err;
5959 void
5960 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5962 struct got_pathlist_entry *pe;
5964 TAILQ_FOREACH(pe, merged_paths, entry)
5965 free((char *)pe->path);
5967 got_pathlist_free(merged_paths);
5970 static const struct got_error *
5971 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5972 int is_rebase, struct got_repository *repo)
5974 const struct got_error *err;
5975 struct got_reference *commit_ref = NULL;
5977 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5978 if (err) {
5979 if (err->code != GOT_ERR_NOT_REF)
5980 goto done;
5981 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5982 if (err)
5983 goto done;
5984 err = got_ref_write(commit_ref, repo);
5985 if (err)
5986 goto done;
5987 } else if (is_rebase) {
5988 struct got_object_id *stored_id;
5989 int cmp;
5991 err = got_ref_resolve(&stored_id, repo, commit_ref);
5992 if (err)
5993 goto done;
5994 cmp = got_object_id_cmp(commit_id, stored_id);
5995 free(stored_id);
5996 if (cmp != 0) {
5997 err = got_error(GOT_ERR_REBASE_COMMITID);
5998 goto done;
6001 done:
6002 if (commit_ref)
6003 got_ref_close(commit_ref);
6004 return err;
6007 static const struct got_error *
6008 rebase_merge_files(struct got_pathlist_head *merged_paths,
6009 const char *commit_ref_name, struct got_worktree *worktree,
6010 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6011 struct got_object_id *commit_id, struct got_repository *repo,
6012 got_worktree_checkout_cb progress_cb, void *progress_arg,
6013 got_cancel_cb cancel_cb, void *cancel_arg)
6015 const struct got_error *err;
6016 struct got_reference *commit_ref = NULL;
6017 struct collect_merged_paths_arg cmp_arg;
6018 char *fileindex_path;
6020 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6022 err = get_fileindex_path(&fileindex_path, worktree);
6023 if (err)
6024 return err;
6026 cmp_arg.progress_cb = progress_cb;
6027 cmp_arg.progress_arg = progress_arg;
6028 cmp_arg.merged_paths = merged_paths;
6029 err = merge_files(worktree, fileindex, fileindex_path,
6030 parent_commit_id, commit_id, repo, collect_merged_paths,
6031 &cmp_arg, cancel_cb, cancel_arg);
6032 if (commit_ref)
6033 got_ref_close(commit_ref);
6034 return err;
6037 const struct got_error *
6038 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6039 struct got_worktree *worktree, struct got_fileindex *fileindex,
6040 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6041 struct got_repository *repo,
6042 got_worktree_checkout_cb progress_cb, void *progress_arg,
6043 got_cancel_cb cancel_cb, void *cancel_arg)
6045 const struct got_error *err;
6046 char *commit_ref_name;
6048 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6049 if (err)
6050 return err;
6052 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6053 if (err)
6054 goto done;
6056 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6057 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6058 progress_arg, cancel_cb, cancel_arg);
6059 done:
6060 free(commit_ref_name);
6061 return err;
6064 const struct got_error *
6065 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6066 struct got_worktree *worktree, struct got_fileindex *fileindex,
6067 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6068 struct got_repository *repo,
6069 got_worktree_checkout_cb progress_cb, void *progress_arg,
6070 got_cancel_cb cancel_cb, void *cancel_arg)
6072 const struct got_error *err;
6073 char *commit_ref_name;
6075 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6076 if (err)
6077 return err;
6079 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6080 if (err)
6081 goto done;
6083 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6084 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6085 progress_arg, cancel_cb, cancel_arg);
6086 done:
6087 free(commit_ref_name);
6088 return err;
6091 static const struct got_error *
6092 rebase_commit(struct got_object_id **new_commit_id,
6093 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6094 struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6096 const char *new_logmsg, struct got_repository *repo)
6098 const struct got_error *err, *sync_err;
6099 struct got_pathlist_head commitable_paths;
6100 struct collect_commitables_arg cc_arg;
6101 char *fileindex_path = NULL;
6102 struct got_reference *head_ref = NULL;
6103 struct got_object_id *head_commit_id = NULL;
6104 char *logmsg = NULL;
6106 TAILQ_INIT(&commitable_paths);
6107 *new_commit_id = NULL;
6109 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6111 err = get_fileindex_path(&fileindex_path, worktree);
6112 if (err)
6113 return err;
6115 cc_arg.commitable_paths = &commitable_paths;
6116 cc_arg.worktree = worktree;
6117 cc_arg.repo = repo;
6118 cc_arg.have_staged_files = 0;
6120 * If possible get the status of individual files directly to
6121 * avoid crawling the entire work tree once per rebased commit.
6122 * TODO: Ideally, merged_paths would contain a list of commitables
6123 * we could use so we could skip worktree_status() entirely.
6125 if (merged_paths) {
6126 struct got_pathlist_entry *pe;
6127 TAILQ_FOREACH(pe, merged_paths, entry) {
6128 err = worktree_status(worktree, pe->path, fileindex,
6129 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6130 0);
6131 if (err)
6132 goto done;
6134 } else {
6135 err = worktree_status(worktree, "", fileindex, repo,
6136 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6137 if (err)
6138 goto done;
6141 if (TAILQ_EMPTY(&commitable_paths)) {
6142 /* No-op change; commit will be elided. */
6143 err = got_ref_delete(commit_ref, repo);
6144 if (err)
6145 goto done;
6146 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6147 goto done;
6150 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6151 if (err)
6152 goto done;
6154 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6155 if (err)
6156 goto done;
6158 if (new_logmsg) {
6159 logmsg = strdup(new_logmsg);
6160 if (logmsg == NULL) {
6161 err = got_error_from_errno("strdup");
6162 goto done;
6164 } else {
6165 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6166 if (err)
6167 goto done;
6170 /* NB: commit_worktree will call free(logmsg) */
6171 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6172 worktree, got_object_commit_get_author(orig_commit),
6173 got_object_commit_get_committer(orig_commit),
6174 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6175 if (err)
6176 goto done;
6178 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6179 if (err)
6180 goto done;
6182 err = got_ref_delete(commit_ref, repo);
6183 if (err)
6184 goto done;
6186 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6187 fileindex, 0);
6188 sync_err = sync_fileindex(fileindex, fileindex_path);
6189 if (sync_err && err == NULL)
6190 err = sync_err;
6191 done:
6192 free(fileindex_path);
6193 free(head_commit_id);
6194 if (head_ref)
6195 got_ref_close(head_ref);
6196 if (err) {
6197 free(*new_commit_id);
6198 *new_commit_id = NULL;
6200 return err;
6203 const struct got_error *
6204 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6205 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6206 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6207 struct got_commit_object *orig_commit,
6208 struct got_object_id *orig_commit_id, struct got_repository *repo)
6210 const struct got_error *err;
6211 char *commit_ref_name;
6212 struct got_reference *commit_ref = NULL;
6213 struct got_object_id *commit_id = NULL;
6215 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6216 if (err)
6217 return err;
6219 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6220 if (err)
6221 goto done;
6222 err = got_ref_resolve(&commit_id, repo, commit_ref);
6223 if (err)
6224 goto done;
6225 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6226 err = got_error(GOT_ERR_REBASE_COMMITID);
6227 goto done;
6230 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6231 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6232 done:
6233 if (commit_ref)
6234 got_ref_close(commit_ref);
6235 free(commit_ref_name);
6236 free(commit_id);
6237 return err;
6240 const struct got_error *
6241 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6242 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6243 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6244 struct got_commit_object *orig_commit,
6245 struct got_object_id *orig_commit_id, const char *new_logmsg,
6246 struct got_repository *repo)
6248 const struct got_error *err;
6249 char *commit_ref_name;
6250 struct got_reference *commit_ref = NULL;
6252 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6253 if (err)
6254 return err;
6256 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6257 if (err)
6258 goto done;
6260 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6261 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6262 done:
6263 if (commit_ref)
6264 got_ref_close(commit_ref);
6265 free(commit_ref_name);
6266 return err;
6269 const struct got_error *
6270 got_worktree_rebase_postpone(struct got_worktree *worktree,
6271 struct got_fileindex *fileindex)
6273 if (fileindex)
6274 got_fileindex_free(fileindex);
6275 return lock_worktree(worktree, LOCK_SH);
6278 static const struct got_error *
6279 delete_ref(const char *name, struct got_repository *repo)
6281 const struct got_error *err;
6282 struct got_reference *ref;
6284 err = got_ref_open(&ref, repo, name, 0);
6285 if (err) {
6286 if (err->code == GOT_ERR_NOT_REF)
6287 return NULL;
6288 return err;
6291 err = got_ref_delete(ref, repo);
6292 got_ref_close(ref);
6293 return err;
6296 static const struct got_error *
6297 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6299 const struct got_error *err;
6300 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6301 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6303 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6304 if (err)
6305 goto done;
6306 err = delete_ref(tmp_branch_name, repo);
6307 if (err)
6308 goto done;
6310 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6311 if (err)
6312 goto done;
6313 err = delete_ref(new_base_branch_ref_name, repo);
6314 if (err)
6315 goto done;
6317 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6318 if (err)
6319 goto done;
6320 err = delete_ref(branch_ref_name, repo);
6321 if (err)
6322 goto done;
6324 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6325 if (err)
6326 goto done;
6327 err = delete_ref(commit_ref_name, repo);
6328 if (err)
6329 goto done;
6331 done:
6332 free(tmp_branch_name);
6333 free(new_base_branch_ref_name);
6334 free(branch_ref_name);
6335 free(commit_ref_name);
6336 return err;
6339 const struct got_error *
6340 got_worktree_rebase_complete(struct got_worktree *worktree,
6341 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6342 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6343 struct got_repository *repo)
6345 const struct got_error *err, *unlockerr;
6346 struct got_object_id *new_head_commit_id = NULL;
6348 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6349 if (err)
6350 return err;
6352 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6353 if (err)
6354 goto done;
6356 err = got_ref_write(rebased_branch, repo);
6357 if (err)
6358 goto done;
6360 err = got_worktree_set_head_ref(worktree, rebased_branch);
6361 if (err)
6362 goto done;
6364 err = delete_rebase_refs(worktree, repo);
6365 done:
6366 if (fileindex)
6367 got_fileindex_free(fileindex);
6368 free(new_head_commit_id);
6369 unlockerr = lock_worktree(worktree, LOCK_SH);
6370 if (unlockerr && err == NULL)
6371 err = unlockerr;
6372 return err;
6375 const struct got_error *
6376 got_worktree_rebase_abort(struct got_worktree *worktree,
6377 struct got_fileindex *fileindex, struct got_repository *repo,
6378 struct got_reference *new_base_branch,
6379 got_worktree_checkout_cb progress_cb, void *progress_arg)
6381 const struct got_error *err, *unlockerr, *sync_err;
6382 struct got_reference *resolved = NULL;
6383 struct got_object_id *commit_id = NULL;
6384 char *fileindex_path = NULL;
6385 struct revert_file_args rfa;
6386 struct got_object_id *tree_id = NULL;
6388 err = lock_worktree(worktree, LOCK_EX);
6389 if (err)
6390 return err;
6392 err = got_ref_open(&resolved, repo,
6393 got_ref_get_symref_target(new_base_branch), 0);
6394 if (err)
6395 goto done;
6397 err = got_worktree_set_head_ref(worktree, resolved);
6398 if (err)
6399 goto done;
6402 * XXX commits to the base branch could have happened while
6403 * we were busy rebasing; should we store the original commit ID
6404 * when rebase begins and read it back here?
6406 err = got_ref_resolve(&commit_id, repo, resolved);
6407 if (err)
6408 goto done;
6410 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6411 if (err)
6412 goto done;
6414 err = got_object_id_by_path(&tree_id, repo,
6415 worktree->base_commit_id, worktree->path_prefix);
6416 if (err)
6417 goto done;
6419 err = delete_rebase_refs(worktree, repo);
6420 if (err)
6421 goto done;
6423 err = get_fileindex_path(&fileindex_path, worktree);
6424 if (err)
6425 goto done;
6427 rfa.worktree = worktree;
6428 rfa.fileindex = fileindex;
6429 rfa.progress_cb = progress_cb;
6430 rfa.progress_arg = progress_arg;
6431 rfa.patch_cb = NULL;
6432 rfa.patch_arg = NULL;
6433 rfa.repo = repo;
6434 err = worktree_status(worktree, "", fileindex, repo,
6435 revert_file, &rfa, NULL, NULL, 0, 0);
6436 if (err)
6437 goto sync;
6439 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6440 repo, progress_cb, progress_arg, NULL, NULL);
6441 sync:
6442 sync_err = sync_fileindex(fileindex, fileindex_path);
6443 if (sync_err && err == NULL)
6444 err = sync_err;
6445 done:
6446 got_ref_close(resolved);
6447 free(tree_id);
6448 free(commit_id);
6449 if (fileindex)
6450 got_fileindex_free(fileindex);
6451 free(fileindex_path);
6453 unlockerr = lock_worktree(worktree, LOCK_SH);
6454 if (unlockerr && err == NULL)
6455 err = unlockerr;
6456 return err;
6459 const struct got_error *
6460 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6461 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6462 struct got_fileindex **fileindex, struct got_worktree *worktree,
6463 struct got_repository *repo)
6465 const struct got_error *err = NULL;
6466 char *tmp_branch_name = NULL;
6467 char *branch_ref_name = NULL;
6468 char *base_commit_ref_name = NULL;
6469 char *fileindex_path = NULL;
6470 struct check_rebase_ok_arg ok_arg;
6471 struct got_reference *wt_branch = NULL;
6472 struct got_reference *base_commit_ref = NULL;
6474 *tmp_branch = NULL;
6475 *branch_ref = NULL;
6476 *base_commit_id = NULL;
6477 *fileindex = NULL;
6479 err = lock_worktree(worktree, LOCK_EX);
6480 if (err)
6481 return err;
6483 err = open_fileindex(fileindex, &fileindex_path, worktree);
6484 if (err)
6485 goto done;
6487 ok_arg.worktree = worktree;
6488 ok_arg.repo = repo;
6489 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6490 &ok_arg);
6491 if (err)
6492 goto done;
6494 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6495 if (err)
6496 goto done;
6498 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6499 if (err)
6500 goto done;
6502 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6503 worktree);
6504 if (err)
6505 goto done;
6507 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6508 0);
6509 if (err)
6510 goto done;
6512 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6513 if (err)
6514 goto done;
6516 err = got_ref_write(*branch_ref, repo);
6517 if (err)
6518 goto done;
6520 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6521 worktree->base_commit_id);
6522 if (err)
6523 goto done;
6524 err = got_ref_write(base_commit_ref, repo);
6525 if (err)
6526 goto done;
6527 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6528 if (*base_commit_id == NULL) {
6529 err = got_error_from_errno("got_object_id_dup");
6530 goto done;
6533 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6534 worktree->base_commit_id);
6535 if (err)
6536 goto done;
6537 err = got_ref_write(*tmp_branch, repo);
6538 if (err)
6539 goto done;
6541 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6542 if (err)
6543 goto done;
6544 done:
6545 free(fileindex_path);
6546 free(tmp_branch_name);
6547 free(branch_ref_name);
6548 free(base_commit_ref_name);
6549 if (wt_branch)
6550 got_ref_close(wt_branch);
6551 if (err) {
6552 if (*branch_ref) {
6553 got_ref_close(*branch_ref);
6554 *branch_ref = NULL;
6556 if (*tmp_branch) {
6557 got_ref_close(*tmp_branch);
6558 *tmp_branch = NULL;
6560 free(*base_commit_id);
6561 if (*fileindex) {
6562 got_fileindex_free(*fileindex);
6563 *fileindex = NULL;
6565 lock_worktree(worktree, LOCK_SH);
6567 return err;
6570 const struct got_error *
6571 got_worktree_histedit_postpone(struct got_worktree *worktree,
6572 struct got_fileindex *fileindex)
6574 if (fileindex)
6575 got_fileindex_free(fileindex);
6576 return lock_worktree(worktree, LOCK_SH);
6579 const struct got_error *
6580 got_worktree_histedit_in_progress(int *in_progress,
6581 struct got_worktree *worktree)
6583 const struct got_error *err;
6584 char *tmp_branch_name = NULL;
6586 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6587 if (err)
6588 return err;
6590 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6591 free(tmp_branch_name);
6592 return NULL;
6595 const struct got_error *
6596 got_worktree_histedit_continue(struct got_object_id **commit_id,
6597 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6598 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6599 struct got_worktree *worktree, struct got_repository *repo)
6601 const struct got_error *err;
6602 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6603 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6604 struct got_reference *commit_ref = NULL;
6605 struct got_reference *base_commit_ref = NULL;
6606 char *fileindex_path = NULL;
6607 int have_staged_files = 0;
6609 *commit_id = NULL;
6610 *tmp_branch = NULL;
6611 *base_commit_id = NULL;
6612 *fileindex = NULL;
6614 err = lock_worktree(worktree, LOCK_EX);
6615 if (err)
6616 return err;
6618 err = open_fileindex(fileindex, &fileindex_path, worktree);
6619 if (err)
6620 goto done;
6622 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6623 &have_staged_files);
6624 if (err && err->code != GOT_ERR_CANCELLED)
6625 goto done;
6626 if (have_staged_files) {
6627 err = got_error(GOT_ERR_STAGED_PATHS);
6628 goto done;
6631 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6632 if (err)
6633 goto done;
6635 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6636 if (err)
6637 goto done;
6639 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6640 if (err)
6641 goto done;
6643 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6644 worktree);
6645 if (err)
6646 goto done;
6648 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6649 if (err)
6650 goto done;
6652 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6653 if (err)
6654 goto done;
6655 err = got_ref_resolve(commit_id, repo, commit_ref);
6656 if (err)
6657 goto done;
6659 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6660 if (err)
6661 goto done;
6662 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6663 if (err)
6664 goto done;
6666 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6667 if (err)
6668 goto done;
6669 done:
6670 free(commit_ref_name);
6671 free(branch_ref_name);
6672 free(fileindex_path);
6673 if (commit_ref)
6674 got_ref_close(commit_ref);
6675 if (base_commit_ref)
6676 got_ref_close(base_commit_ref);
6677 if (err) {
6678 free(*commit_id);
6679 *commit_id = NULL;
6680 free(*base_commit_id);
6681 *base_commit_id = NULL;
6682 if (*tmp_branch) {
6683 got_ref_close(*tmp_branch);
6684 *tmp_branch = NULL;
6686 if (*fileindex) {
6687 got_fileindex_free(*fileindex);
6688 *fileindex = NULL;
6690 lock_worktree(worktree, LOCK_EX);
6692 return err;
6695 static const struct got_error *
6696 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6698 const struct got_error *err;
6699 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6700 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6702 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6703 if (err)
6704 goto done;
6705 err = delete_ref(tmp_branch_name, repo);
6706 if (err)
6707 goto done;
6709 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6710 worktree);
6711 if (err)
6712 goto done;
6713 err = delete_ref(base_commit_ref_name, repo);
6714 if (err)
6715 goto done;
6717 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6718 if (err)
6719 goto done;
6720 err = delete_ref(branch_ref_name, repo);
6721 if (err)
6722 goto done;
6724 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6725 if (err)
6726 goto done;
6727 err = delete_ref(commit_ref_name, repo);
6728 if (err)
6729 goto done;
6730 done:
6731 free(tmp_branch_name);
6732 free(base_commit_ref_name);
6733 free(branch_ref_name);
6734 free(commit_ref_name);
6735 return err;
6738 const struct got_error *
6739 got_worktree_histedit_abort(struct got_worktree *worktree,
6740 struct got_fileindex *fileindex, struct got_repository *repo,
6741 struct got_reference *branch, struct got_object_id *base_commit_id,
6742 got_worktree_checkout_cb progress_cb, void *progress_arg)
6744 const struct got_error *err, *unlockerr, *sync_err;
6745 struct got_reference *resolved = NULL;
6746 char *fileindex_path = NULL;
6747 struct got_object_id *tree_id = NULL;
6748 struct revert_file_args rfa;
6750 err = lock_worktree(worktree, LOCK_EX);
6751 if (err)
6752 return err;
6754 err = got_ref_open(&resolved, repo,
6755 got_ref_get_symref_target(branch), 0);
6756 if (err)
6757 goto done;
6759 err = got_worktree_set_head_ref(worktree, resolved);
6760 if (err)
6761 goto done;
6763 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6764 if (err)
6765 goto done;
6767 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6768 worktree->path_prefix);
6769 if (err)
6770 goto done;
6772 err = delete_histedit_refs(worktree, repo);
6773 if (err)
6774 goto done;
6776 err = get_fileindex_path(&fileindex_path, worktree);
6777 if (err)
6778 goto done;
6780 rfa.worktree = worktree;
6781 rfa.fileindex = fileindex;
6782 rfa.progress_cb = progress_cb;
6783 rfa.progress_arg = progress_arg;
6784 rfa.patch_cb = NULL;
6785 rfa.patch_arg = NULL;
6786 rfa.repo = repo;
6787 err = worktree_status(worktree, "", fileindex, repo,
6788 revert_file, &rfa, NULL, NULL, 0, 0);
6789 if (err)
6790 goto sync;
6792 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6793 repo, progress_cb, progress_arg, NULL, NULL);
6794 sync:
6795 sync_err = sync_fileindex(fileindex, fileindex_path);
6796 if (sync_err && err == NULL)
6797 err = sync_err;
6798 done:
6799 got_ref_close(resolved);
6800 free(tree_id);
6801 free(fileindex_path);
6803 unlockerr = lock_worktree(worktree, LOCK_SH);
6804 if (unlockerr && err == NULL)
6805 err = unlockerr;
6806 return err;
6809 const struct got_error *
6810 got_worktree_histedit_complete(struct got_worktree *worktree,
6811 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6812 struct got_reference *edited_branch, struct got_repository *repo)
6814 const struct got_error *err, *unlockerr;
6815 struct got_object_id *new_head_commit_id = NULL;
6816 struct got_reference *resolved = NULL;
6818 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6819 if (err)
6820 return err;
6822 err = got_ref_open(&resolved, repo,
6823 got_ref_get_symref_target(edited_branch), 0);
6824 if (err)
6825 goto done;
6827 err = got_ref_change_ref(resolved, new_head_commit_id);
6828 if (err)
6829 goto done;
6831 err = got_ref_write(resolved, repo);
6832 if (err)
6833 goto done;
6835 err = got_worktree_set_head_ref(worktree, resolved);
6836 if (err)
6837 goto done;
6839 err = delete_histedit_refs(worktree, repo);
6840 done:
6841 if (fileindex)
6842 got_fileindex_free(fileindex);
6843 free(new_head_commit_id);
6844 unlockerr = lock_worktree(worktree, LOCK_SH);
6845 if (unlockerr && err == NULL)
6846 err = unlockerr;
6847 return err;
6850 const struct got_error *
6851 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6852 struct got_object_id *commit_id, struct got_repository *repo)
6854 const struct got_error *err;
6855 char *commit_ref_name;
6857 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6858 if (err)
6859 return err;
6861 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6862 if (err)
6863 goto done;
6865 err = delete_ref(commit_ref_name, repo);
6866 done:
6867 free(commit_ref_name);
6868 return err;
6871 const struct got_error *
6872 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6873 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6874 struct got_worktree *worktree, const char *refname,
6875 struct got_repository *repo)
6877 const struct got_error *err = NULL;
6878 char *fileindex_path = NULL;
6879 struct check_rebase_ok_arg ok_arg;
6881 *fileindex = NULL;
6882 *branch_ref = NULL;
6883 *base_branch_ref = NULL;
6885 err = lock_worktree(worktree, LOCK_EX);
6886 if (err)
6887 return err;
6889 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6890 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6891 "cannot integrate a branch into itself; "
6892 "update -b or different branch name required");
6893 goto done;
6896 err = open_fileindex(fileindex, &fileindex_path, worktree);
6897 if (err)
6898 goto done;
6900 /* Preconditions are the same as for rebase. */
6901 ok_arg.worktree = worktree;
6902 ok_arg.repo = repo;
6903 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6904 &ok_arg);
6905 if (err)
6906 goto done;
6908 err = got_ref_open(branch_ref, repo, refname, 1);
6909 if (err)
6910 goto done;
6912 err = got_ref_open(base_branch_ref, repo,
6913 got_worktree_get_head_ref_name(worktree), 1);
6914 done:
6915 if (err) {
6916 if (*branch_ref) {
6917 got_ref_close(*branch_ref);
6918 *branch_ref = NULL;
6920 if (*base_branch_ref) {
6921 got_ref_close(*base_branch_ref);
6922 *base_branch_ref = NULL;
6924 if (*fileindex) {
6925 got_fileindex_free(*fileindex);
6926 *fileindex = NULL;
6928 lock_worktree(worktree, LOCK_SH);
6930 return err;
6933 const struct got_error *
6934 got_worktree_integrate_continue(struct got_worktree *worktree,
6935 struct got_fileindex *fileindex, struct got_repository *repo,
6936 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6937 got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 got_cancel_cb cancel_cb, void *cancel_arg)
6940 const struct got_error *err = NULL, *sync_err, *unlockerr;
6941 char *fileindex_path = NULL;
6942 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6944 err = get_fileindex_path(&fileindex_path, worktree);
6945 if (err)
6946 goto done;
6948 err = got_ref_resolve(&commit_id, repo, branch_ref);
6949 if (err)
6950 goto done;
6952 err = got_object_id_by_path(&tree_id, repo, commit_id,
6953 worktree->path_prefix);
6954 if (err)
6955 goto done;
6957 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6958 if (err)
6959 goto done;
6961 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6962 progress_cb, progress_arg, cancel_cb, cancel_arg);
6963 if (err)
6964 goto sync;
6966 err = got_ref_change_ref(base_branch_ref, commit_id);
6967 if (err)
6968 goto sync;
6970 err = got_ref_write(base_branch_ref, repo);
6971 sync:
6972 sync_err = sync_fileindex(fileindex, fileindex_path);
6973 if (sync_err && err == NULL)
6974 err = sync_err;
6976 done:
6977 unlockerr = got_ref_unlock(branch_ref);
6978 if (unlockerr && err == NULL)
6979 err = unlockerr;
6980 got_ref_close(branch_ref);
6982 unlockerr = got_ref_unlock(base_branch_ref);
6983 if (unlockerr && err == NULL)
6984 err = unlockerr;
6985 got_ref_close(base_branch_ref);
6987 got_fileindex_free(fileindex);
6988 free(fileindex_path);
6989 free(tree_id);
6991 unlockerr = lock_worktree(worktree, LOCK_SH);
6992 if (unlockerr && err == NULL)
6993 err = unlockerr;
6994 return err;
6997 const struct got_error *
6998 got_worktree_integrate_abort(struct got_worktree *worktree,
6999 struct got_fileindex *fileindex, struct got_repository *repo,
7000 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7002 const struct got_error *err = NULL, *unlockerr = NULL;
7004 got_fileindex_free(fileindex);
7006 err = lock_worktree(worktree, LOCK_SH);
7008 unlockerr = got_ref_unlock(branch_ref);
7009 if (unlockerr && err == NULL)
7010 err = unlockerr;
7011 got_ref_close(branch_ref);
7013 unlockerr = got_ref_unlock(base_branch_ref);
7014 if (unlockerr && err == NULL)
7015 err = unlockerr;
7016 got_ref_close(base_branch_ref);
7018 return err;
7021 struct check_stage_ok_arg {
7022 struct got_object_id *head_commit_id;
7023 struct got_worktree *worktree;
7024 struct got_fileindex *fileindex;
7025 struct got_repository *repo;
7026 int have_changes;
7029 const struct got_error *
7030 check_stage_ok(void *arg, unsigned char status,
7031 unsigned char staged_status, const char *relpath,
7032 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7033 struct got_object_id *commit_id, int dirfd, const char *de_name)
7035 struct check_stage_ok_arg *a = arg;
7036 const struct got_error *err = NULL;
7037 struct got_fileindex_entry *ie;
7038 struct got_object_id base_commit_id;
7039 struct got_object_id *base_commit_idp = NULL;
7040 char *in_repo_path = NULL, *p;
7042 if (status == GOT_STATUS_UNVERSIONED ||
7043 status == GOT_STATUS_NO_CHANGE)
7044 return NULL;
7045 if (status == GOT_STATUS_NONEXISTENT)
7046 return got_error_set_errno(ENOENT, relpath);
7048 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7049 if (ie == NULL)
7050 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7052 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7053 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7054 relpath) == -1)
7055 return got_error_from_errno("asprintf");
7057 if (got_fileindex_entry_has_commit(ie)) {
7058 memcpy(base_commit_id.sha1, ie->commit_sha1,
7059 SHA1_DIGEST_LENGTH);
7060 base_commit_idp = &base_commit_id;
7063 if (status == GOT_STATUS_CONFLICT) {
7064 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7065 goto done;
7066 } else if (status != GOT_STATUS_ADD &&
7067 status != GOT_STATUS_MODIFY &&
7068 status != GOT_STATUS_DELETE) {
7069 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7070 goto done;
7073 a->have_changes = 1;
7075 p = in_repo_path;
7076 while (p[0] == '/')
7077 p++;
7078 err = check_out_of_date(p, status, staged_status,
7079 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7080 GOT_ERR_STAGE_OUT_OF_DATE);
7081 done:
7082 free(in_repo_path);
7083 return err;
7086 struct stage_path_arg {
7087 struct got_worktree *worktree;
7088 struct got_fileindex *fileindex;
7089 struct got_repository *repo;
7090 got_worktree_status_cb status_cb;
7091 void *status_arg;
7092 got_worktree_patch_cb patch_cb;
7093 void *patch_arg;
7094 int staged_something;
7095 int allow_bad_symlinks;
7098 static const struct got_error *
7099 stage_path(void *arg, unsigned char status,
7100 unsigned char staged_status, const char *relpath,
7101 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7102 struct got_object_id *commit_id, int dirfd, const char *de_name)
7104 struct stage_path_arg *a = arg;
7105 const struct got_error *err = NULL;
7106 struct got_fileindex_entry *ie;
7107 char *ondisk_path = NULL, *path_content = NULL;
7108 uint32_t stage;
7109 struct got_object_id *new_staged_blob_id = NULL;
7110 struct stat sb;
7112 if (status == GOT_STATUS_UNVERSIONED)
7113 return NULL;
7115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7116 if (ie == NULL)
7117 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7119 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7120 relpath)== -1)
7121 return got_error_from_errno("asprintf");
7123 switch (status) {
7124 case GOT_STATUS_ADD:
7125 case GOT_STATUS_MODIFY:
7126 /* XXX could sb.st_mode be passed in by our caller? */
7127 if (lstat(ondisk_path, &sb) == -1) {
7128 err = got_error_from_errno2("lstat", ondisk_path);
7129 break;
7131 if (a->patch_cb) {
7132 if (status == GOT_STATUS_ADD) {
7133 int choice = GOT_PATCH_CHOICE_NONE;
7134 err = (*a->patch_cb)(&choice, a->patch_arg,
7135 status, ie->path, NULL, 1, 1);
7136 if (err)
7137 break;
7138 if (choice != GOT_PATCH_CHOICE_YES)
7139 break;
7140 } else {
7141 err = create_patched_content(&path_content, 0,
7142 staged_blob_id ? staged_blob_id : blob_id,
7143 ondisk_path, dirfd, de_name, ie->path,
7144 a->repo, a->patch_cb, a->patch_arg);
7145 if (err || path_content == NULL)
7146 break;
7149 err = got_object_blob_create(&new_staged_blob_id,
7150 path_content ? path_content : ondisk_path, a->repo);
7151 if (err)
7152 break;
7153 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7154 SHA1_DIGEST_LENGTH);
7155 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7156 stage = GOT_FILEIDX_STAGE_ADD;
7157 else
7158 stage = GOT_FILEIDX_STAGE_MODIFY;
7159 got_fileindex_entry_stage_set(ie, stage);
7160 if (S_ISLNK(sb.st_mode)) {
7161 int is_bad_symlink = 0;
7162 if (!a->allow_bad_symlinks) {
7163 char target_path[PATH_MAX];
7164 ssize_t target_len;
7165 target_len = readlink(ondisk_path, target_path,
7166 sizeof(target_path));
7167 if (target_len == -1) {
7168 err = got_error_from_errno2("readlink",
7169 ondisk_path);
7170 break;
7172 err = is_bad_symlink_target(&is_bad_symlink,
7173 target_path, target_len, ondisk_path,
7174 a->worktree->root_path);
7175 if (err)
7176 break;
7177 if (is_bad_symlink) {
7178 err = got_error_path(ondisk_path,
7179 GOT_ERR_BAD_SYMLINK);
7180 break;
7183 if (is_bad_symlink)
7184 got_fileindex_entry_staged_filetype_set(ie,
7185 GOT_FILEIDX_MODE_BAD_SYMLINK);
7186 else
7187 got_fileindex_entry_staged_filetype_set(ie,
7188 GOT_FILEIDX_MODE_SYMLINK);
7189 } else {
7190 got_fileindex_entry_staged_filetype_set(ie,
7191 GOT_FILEIDX_MODE_REGULAR_FILE);
7193 a->staged_something = 1;
7194 if (a->status_cb == NULL)
7195 break;
7196 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7197 get_staged_status(ie), relpath, blob_id,
7198 new_staged_blob_id, NULL, dirfd, de_name);
7199 break;
7200 case GOT_STATUS_DELETE:
7201 if (staged_status == GOT_STATUS_DELETE)
7202 break;
7203 if (a->patch_cb) {
7204 int choice = GOT_PATCH_CHOICE_NONE;
7205 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7206 ie->path, NULL, 1, 1);
7207 if (err)
7208 break;
7209 if (choice == GOT_PATCH_CHOICE_NO)
7210 break;
7211 if (choice != GOT_PATCH_CHOICE_YES) {
7212 err = got_error(GOT_ERR_PATCH_CHOICE);
7213 break;
7216 stage = GOT_FILEIDX_STAGE_DELETE;
7217 got_fileindex_entry_stage_set(ie, stage);
7218 a->staged_something = 1;
7219 if (a->status_cb == NULL)
7220 break;
7221 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7222 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7223 de_name);
7224 break;
7225 case GOT_STATUS_NO_CHANGE:
7226 break;
7227 case GOT_STATUS_CONFLICT:
7228 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7229 break;
7230 case GOT_STATUS_NONEXISTENT:
7231 err = got_error_set_errno(ENOENT, relpath);
7232 break;
7233 default:
7234 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7235 break;
7238 if (path_content && unlink(path_content) == -1 && err == NULL)
7239 err = got_error_from_errno2("unlink", path_content);
7240 free(path_content);
7241 free(ondisk_path);
7242 free(new_staged_blob_id);
7243 return err;
7246 const struct got_error *
7247 got_worktree_stage(struct got_worktree *worktree,
7248 struct got_pathlist_head *paths,
7249 got_worktree_status_cb status_cb, void *status_arg,
7250 got_worktree_patch_cb patch_cb, void *patch_arg,
7251 int allow_bad_symlinks, struct got_repository *repo)
7253 const struct got_error *err = NULL, *sync_err, *unlockerr;
7254 struct got_pathlist_entry *pe;
7255 struct got_fileindex *fileindex = NULL;
7256 char *fileindex_path = NULL;
7257 struct got_reference *head_ref = NULL;
7258 struct got_object_id *head_commit_id = NULL;
7259 struct check_stage_ok_arg oka;
7260 struct stage_path_arg spa;
7262 err = lock_worktree(worktree, LOCK_EX);
7263 if (err)
7264 return err;
7266 err = got_ref_open(&head_ref, repo,
7267 got_worktree_get_head_ref_name(worktree), 0);
7268 if (err)
7269 goto done;
7270 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7271 if (err)
7272 goto done;
7273 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7274 if (err)
7275 goto done;
7277 /* Check pre-conditions before staging anything. */
7278 oka.head_commit_id = head_commit_id;
7279 oka.worktree = worktree;
7280 oka.fileindex = fileindex;
7281 oka.repo = repo;
7282 oka.have_changes = 0;
7283 TAILQ_FOREACH(pe, paths, entry) {
7284 err = worktree_status(worktree, pe->path, fileindex, repo,
7285 check_stage_ok, &oka, NULL, NULL, 0, 0);
7286 if (err)
7287 goto done;
7289 if (!oka.have_changes) {
7290 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7291 goto done;
7294 spa.worktree = worktree;
7295 spa.fileindex = fileindex;
7296 spa.repo = repo;
7297 spa.patch_cb = patch_cb;
7298 spa.patch_arg = patch_arg;
7299 spa.status_cb = status_cb;
7300 spa.status_arg = status_arg;
7301 spa.staged_something = 0;
7302 spa.allow_bad_symlinks = allow_bad_symlinks;
7303 TAILQ_FOREACH(pe, paths, entry) {
7304 err = worktree_status(worktree, pe->path, fileindex, repo,
7305 stage_path, &spa, NULL, NULL, 0, 0);
7306 if (err)
7307 goto done;
7309 if (!spa.staged_something) {
7310 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7311 goto done;
7314 sync_err = sync_fileindex(fileindex, fileindex_path);
7315 if (sync_err && err == NULL)
7316 err = sync_err;
7317 done:
7318 if (head_ref)
7319 got_ref_close(head_ref);
7320 free(head_commit_id);
7321 free(fileindex_path);
7322 if (fileindex)
7323 got_fileindex_free(fileindex);
7324 unlockerr = lock_worktree(worktree, LOCK_SH);
7325 if (unlockerr && err == NULL)
7326 err = unlockerr;
7327 return err;
7330 struct unstage_path_arg {
7331 struct got_worktree *worktree;
7332 struct got_fileindex *fileindex;
7333 struct got_repository *repo;
7334 got_worktree_checkout_cb progress_cb;
7335 void *progress_arg;
7336 got_worktree_patch_cb patch_cb;
7337 void *patch_arg;
7340 static const struct got_error *
7341 create_unstaged_content(char **path_unstaged_content,
7342 char **path_new_staged_content, struct got_object_id *blob_id,
7343 struct got_object_id *staged_blob_id, const char *relpath,
7344 struct got_repository *repo,
7345 got_worktree_patch_cb patch_cb, void *patch_arg)
7347 const struct got_error *err;
7348 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7349 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7350 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7351 struct stat sb1, sb2;
7352 struct got_diff_changes *changes = NULL;
7353 struct got_diff_state *ds = NULL;
7354 struct got_diff_args *args = NULL;
7355 struct got_diff_change *change;
7356 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7357 int have_content = 0, have_rejected_content = 0;
7359 *path_unstaged_content = NULL;
7360 *path_new_staged_content = NULL;
7362 err = got_object_id_str(&label1, blob_id);
7363 if (err)
7364 return err;
7365 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7366 if (err)
7367 goto done;
7369 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7370 if (err)
7371 goto done;
7373 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7374 if (err)
7375 goto done;
7377 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7378 if (err)
7379 goto done;
7381 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7382 if (err)
7383 goto done;
7385 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7386 if (err)
7387 goto done;
7389 if (stat(path1, &sb1) == -1) {
7390 err = got_error_from_errno2("stat", path1);
7391 goto done;
7394 if (stat(path2, &sb2) == -1) {
7395 err = got_error_from_errno2("stat", path2);
7396 goto done;
7399 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7400 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7401 if (err)
7402 goto done;
7404 err = got_opentemp_named(path_unstaged_content, &outfile,
7405 "got-unstaged-content");
7406 if (err)
7407 goto done;
7408 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7409 "got-new-staged-content");
7410 if (err)
7411 goto done;
7413 if (fseek(f1, 0L, SEEK_SET) == -1) {
7414 err = got_ferror(f1, GOT_ERR_IO);
7415 goto done;
7417 if (fseek(f2, 0L, SEEK_SET) == -1) {
7418 err = got_ferror(f2, GOT_ERR_IO);
7419 goto done;
7421 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7422 int choice;
7423 err = apply_or_reject_change(&choice, change, ++n,
7424 changes->nchanges, ds, args, diff_flags, relpath,
7425 f1, f2, &line_cur1, &line_cur2,
7426 outfile, rejectfile, patch_cb, patch_arg);
7427 if (err)
7428 goto done;
7429 if (choice == GOT_PATCH_CHOICE_YES)
7430 have_content = 1;
7431 else
7432 have_rejected_content = 1;
7433 if (choice == GOT_PATCH_CHOICE_QUIT)
7434 break;
7436 if (have_content || have_rejected_content)
7437 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7438 outfile, rejectfile);
7439 done:
7440 free(label1);
7441 if (blob)
7442 got_object_blob_close(blob);
7443 if (staged_blob)
7444 got_object_blob_close(staged_blob);
7445 if (f1 && fclose(f1) == EOF && err == NULL)
7446 err = got_error_from_errno2("fclose", path1);
7447 if (f2 && fclose(f2) == EOF && err == NULL)
7448 err = got_error_from_errno2("fclose", path2);
7449 if (outfile && fclose(outfile) == EOF && err == NULL)
7450 err = got_error_from_errno2("fclose", *path_unstaged_content);
7451 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7452 err = got_error_from_errno2("fclose", *path_new_staged_content);
7453 if (path1 && unlink(path1) == -1 && err == NULL)
7454 err = got_error_from_errno2("unlink", path1);
7455 if (path2 && unlink(path2) == -1 && err == NULL)
7456 err = got_error_from_errno2("unlink", path2);
7457 if (err || !have_content) {
7458 if (*path_unstaged_content &&
7459 unlink(*path_unstaged_content) == -1 && err == NULL)
7460 err = got_error_from_errno2("unlink",
7461 *path_unstaged_content);
7462 free(*path_unstaged_content);
7463 *path_unstaged_content = NULL;
7465 if (err || !have_content || !have_rejected_content) {
7466 if (*path_new_staged_content &&
7467 unlink(*path_new_staged_content) == -1 && err == NULL)
7468 err = got_error_from_errno2("unlink",
7469 *path_new_staged_content);
7470 free(*path_new_staged_content);
7471 *path_new_staged_content = NULL;
7473 free(args);
7474 if (ds) {
7475 got_diff_state_free(ds);
7476 free(ds);
7478 if (changes)
7479 got_diff_free_changes(changes);
7480 free(path1);
7481 free(path2);
7482 return err;
7485 static const struct got_error *
7486 unstage_hunks(struct got_object_id *staged_blob_id,
7487 struct got_blob_object *blob_base,
7488 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7489 const char *ondisk_path, const char *label_orig,
7490 struct got_worktree *worktree, struct got_repository *repo,
7491 got_worktree_patch_cb patch_cb, void *patch_arg,
7492 got_worktree_checkout_cb progress_cb, void *progress_arg)
7494 const struct got_error *err = NULL;
7495 char *path_unstaged_content = NULL;
7496 char *path_new_staged_content = NULL;
7497 struct got_object_id *new_staged_blob_id = NULL;
7498 FILE *f = NULL;
7499 struct stat sb;
7501 err = create_unstaged_content(&path_unstaged_content,
7502 &path_new_staged_content, blob_id, staged_blob_id,
7503 ie->path, repo, patch_cb, patch_arg);
7504 if (err)
7505 return err;
7507 if (path_unstaged_content == NULL)
7508 return NULL;
7510 if (path_new_staged_content) {
7511 err = got_object_blob_create(&new_staged_blob_id,
7512 path_new_staged_content, repo);
7513 if (err)
7514 goto done;
7517 f = fopen(path_unstaged_content, "r");
7518 if (f == NULL) {
7519 err = got_error_from_errno2("fopen",
7520 path_unstaged_content);
7521 goto done;
7523 if (fstat(fileno(f), &sb) == -1) {
7524 err = got_error_from_errno2("fstat", path_unstaged_content);
7525 goto done;
7527 if (got_fileindex_entry_staged_filetype_get(ie) ==
7528 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7529 char link_target[PATH_MAX];
7530 size_t r;
7531 r = fread(link_target, 1, sizeof(link_target), f);
7532 if (r == 0 && ferror(f)) {
7533 err = got_error_from_errno("fread");
7534 goto done;
7536 if (r >= sizeof(link_target)) { /* should not happen */
7537 err = got_error(GOT_ERR_NO_SPACE);
7538 goto done;
7540 link_target[r] = '\0';
7541 err = merge_symlink(worktree, blob_base,
7542 ondisk_path, ie->path, label_orig, link_target,
7543 worktree->base_commit_id, repo, progress_cb,
7544 progress_arg);
7545 } else {
7546 int local_changes_subsumed;
7547 err = merge_file(&local_changes_subsumed, worktree,
7548 blob_base, ondisk_path, ie->path,
7549 got_fileindex_perms_to_st(ie),
7550 path_unstaged_content, label_orig, "unstaged",
7551 repo, progress_cb, progress_arg);
7553 if (err)
7554 goto done;
7556 if (new_staged_blob_id) {
7557 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7558 SHA1_DIGEST_LENGTH);
7559 } else
7560 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7561 done:
7562 free(new_staged_blob_id);
7563 if (path_unstaged_content &&
7564 unlink(path_unstaged_content) == -1 && err == NULL)
7565 err = got_error_from_errno2("unlink", path_unstaged_content);
7566 if (path_new_staged_content &&
7567 unlink(path_new_staged_content) == -1 && err == NULL)
7568 err = got_error_from_errno2("unlink", path_new_staged_content);
7569 if (f && fclose(f) != 0 && err == NULL)
7570 err = got_error_from_errno2("fclose", path_unstaged_content);
7571 free(path_unstaged_content);
7572 free(path_new_staged_content);
7573 return err;
7576 static const struct got_error *
7577 unstage_path(void *arg, unsigned char status,
7578 unsigned char staged_status, const char *relpath,
7579 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7580 struct got_object_id *commit_id, int dirfd, const char *de_name)
7582 const struct got_error *err = NULL;
7583 struct unstage_path_arg *a = arg;
7584 struct got_fileindex_entry *ie;
7585 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7586 char *ondisk_path = NULL;
7587 char *id_str = NULL, *label_orig = NULL;
7588 int local_changes_subsumed;
7589 struct stat sb;
7591 if (staged_status != GOT_STATUS_ADD &&
7592 staged_status != GOT_STATUS_MODIFY &&
7593 staged_status != GOT_STATUS_DELETE)
7594 return NULL;
7596 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7597 if (ie == NULL)
7598 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7600 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7601 == -1)
7602 return got_error_from_errno("asprintf");
7604 err = got_object_id_str(&id_str,
7605 commit_id ? commit_id : a->worktree->base_commit_id);
7606 if (err)
7607 goto done;
7608 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7609 id_str) == -1) {
7610 err = got_error_from_errno("asprintf");
7611 goto done;
7614 switch (staged_status) {
7615 case GOT_STATUS_MODIFY:
7616 err = got_object_open_as_blob(&blob_base, a->repo,
7617 blob_id, 8192);
7618 if (err)
7619 break;
7620 /* fall through */
7621 case GOT_STATUS_ADD:
7622 if (a->patch_cb) {
7623 if (staged_status == GOT_STATUS_ADD) {
7624 int choice = GOT_PATCH_CHOICE_NONE;
7625 err = (*a->patch_cb)(&choice, a->patch_arg,
7626 staged_status, ie->path, NULL, 1, 1);
7627 if (err)
7628 break;
7629 if (choice != GOT_PATCH_CHOICE_YES)
7630 break;
7631 } else {
7632 err = unstage_hunks(staged_blob_id,
7633 blob_base, blob_id, ie, ondisk_path,
7634 label_orig, a->worktree, a->repo,
7635 a->patch_cb, a->patch_arg,
7636 a->progress_cb, a->progress_arg);
7637 break; /* Done with this file. */
7640 err = got_object_open_as_blob(&blob_staged, a->repo,
7641 staged_blob_id, 8192);
7642 if (err)
7643 break;
7644 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7645 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7646 case GOT_FILEIDX_MODE_REGULAR_FILE:
7647 err = merge_blob(&local_changes_subsumed, a->worktree,
7648 blob_base, ondisk_path, relpath,
7649 got_fileindex_perms_to_st(ie), label_orig,
7650 blob_staged, commit_id ? commit_id :
7651 a->worktree->base_commit_id, a->repo,
7652 a->progress_cb, a->progress_arg);
7653 break;
7654 case GOT_FILEIDX_MODE_SYMLINK:
7655 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7656 char *staged_target;
7657 err = got_object_blob_read_to_str(
7658 &staged_target, blob_staged);
7659 if (err)
7660 goto done;
7661 err = merge_symlink(a->worktree, blob_base,
7662 ondisk_path, relpath, label_orig,
7663 staged_target, commit_id ? commit_id :
7664 a->worktree->base_commit_id,
7665 a->repo, a->progress_cb, a->progress_arg);
7666 free(staged_target);
7667 } else {
7668 err = merge_blob(&local_changes_subsumed,
7669 a->worktree, blob_base, ondisk_path,
7670 relpath, got_fileindex_perms_to_st(ie),
7671 label_orig, blob_staged,
7672 commit_id ? commit_id :
7673 a->worktree->base_commit_id, a->repo,
7674 a->progress_cb, a->progress_arg);
7676 break;
7677 default:
7678 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7679 break;
7681 if (err == NULL)
7682 got_fileindex_entry_stage_set(ie,
7683 GOT_FILEIDX_STAGE_NONE);
7684 break;
7685 case GOT_STATUS_DELETE:
7686 if (a->patch_cb) {
7687 int choice = GOT_PATCH_CHOICE_NONE;
7688 err = (*a->patch_cb)(&choice, a->patch_arg,
7689 staged_status, ie->path, NULL, 1, 1);
7690 if (err)
7691 break;
7692 if (choice == GOT_PATCH_CHOICE_NO)
7693 break;
7694 if (choice != GOT_PATCH_CHOICE_YES) {
7695 err = got_error(GOT_ERR_PATCH_CHOICE);
7696 break;
7699 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7700 err = get_file_status(&status, &sb, ie, ondisk_path,
7701 dirfd, de_name, a->repo);
7702 if (err)
7703 break;
7704 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7705 break;
7707 done:
7708 free(ondisk_path);
7709 if (blob_base)
7710 got_object_blob_close(blob_base);
7711 if (blob_staged)
7712 got_object_blob_close(blob_staged);
7713 free(id_str);
7714 free(label_orig);
7715 return err;
7718 const struct got_error *
7719 got_worktree_unstage(struct got_worktree *worktree,
7720 struct got_pathlist_head *paths,
7721 got_worktree_checkout_cb progress_cb, void *progress_arg,
7722 got_worktree_patch_cb patch_cb, void *patch_arg,
7723 struct got_repository *repo)
7725 const struct got_error *err = NULL, *sync_err, *unlockerr;
7726 struct got_pathlist_entry *pe;
7727 struct got_fileindex *fileindex = NULL;
7728 char *fileindex_path = NULL;
7729 struct unstage_path_arg upa;
7731 err = lock_worktree(worktree, LOCK_EX);
7732 if (err)
7733 return err;
7735 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7736 if (err)
7737 goto done;
7739 upa.worktree = worktree;
7740 upa.fileindex = fileindex;
7741 upa.repo = repo;
7742 upa.progress_cb = progress_cb;
7743 upa.progress_arg = progress_arg;
7744 upa.patch_cb = patch_cb;
7745 upa.patch_arg = patch_arg;
7746 TAILQ_FOREACH(pe, paths, entry) {
7747 err = worktree_status(worktree, pe->path, fileindex, repo,
7748 unstage_path, &upa, NULL, NULL, 0, 0);
7749 if (err)
7750 goto done;
7753 sync_err = sync_fileindex(fileindex, fileindex_path);
7754 if (sync_err && err == NULL)
7755 err = sync_err;
7756 done:
7757 free(fileindex_path);
7758 if (fileindex)
7759 got_fileindex_free(fileindex);
7760 unlockerr = lock_worktree(worktree, LOCK_SH);
7761 if (unlockerr && err == NULL)
7762 err = unlockerr;
7763 return err;
7766 struct report_file_info_arg {
7767 struct got_worktree *worktree;
7768 got_worktree_path_info_cb info_cb;
7769 void *info_arg;
7770 struct got_pathlist_head *paths;
7771 got_cancel_cb cancel_cb;
7772 void *cancel_arg;
7775 static const struct got_error *
7776 report_file_info(void *arg, struct got_fileindex_entry *ie)
7778 struct report_file_info_arg *a = arg;
7779 struct got_pathlist_entry *pe;
7780 struct got_object_id blob_id, staged_blob_id, commit_id;
7781 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7782 struct got_object_id *commit_idp = NULL;
7783 int stage;
7785 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7786 return got_error(GOT_ERR_CANCELLED);
7788 TAILQ_FOREACH(pe, a->paths, entry) {
7789 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7790 got_path_is_child(ie->path, pe->path, pe->path_len))
7791 break;
7793 if (pe == NULL) /* not found */
7794 return NULL;
7796 if (got_fileindex_entry_has_blob(ie)) {
7797 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7798 blob_idp = &blob_id;
7800 stage = got_fileindex_entry_stage_get(ie);
7801 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7802 stage == GOT_FILEIDX_STAGE_ADD) {
7803 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7804 SHA1_DIGEST_LENGTH);
7805 staged_blob_idp = &staged_blob_id;
7808 if (got_fileindex_entry_has_commit(ie)) {
7809 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7810 commit_idp = &commit_id;
7813 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7814 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7817 const struct got_error *
7818 got_worktree_path_info(struct got_worktree *worktree,
7819 struct got_pathlist_head *paths,
7820 got_worktree_path_info_cb info_cb, void *info_arg,
7821 got_cancel_cb cancel_cb, void *cancel_arg)
7824 const struct got_error *err = NULL, *unlockerr;
7825 struct got_fileindex *fileindex = NULL;
7826 char *fileindex_path = NULL;
7827 struct report_file_info_arg arg;
7829 err = lock_worktree(worktree, LOCK_SH);
7830 if (err)
7831 return err;
7833 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7834 if (err)
7835 goto done;
7837 arg.worktree = worktree;
7838 arg.info_cb = info_cb;
7839 arg.info_arg = info_arg;
7840 arg.paths = paths;
7841 arg.cancel_cb = cancel_cb;
7842 arg.cancel_arg = cancel_arg;
7843 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7844 &arg);
7845 done:
7846 free(fileindex_path);
7847 if (fileindex)
7848 got_fileindex_free(fileindex);
7849 unlockerr = lock_worktree(worktree, LOCK_UN);
7850 if (unlockerr && err == NULL)
7851 err = unlockerr;
7852 return err;