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 == 0) {
1788 if (flen != 0)
1789 *status = GOT_STATUS_MODIFY;
1790 break;
1791 } else if (flen == 0) {
1792 if (blen != 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 te_name = basename(ie->path);
4414 if (te_name == NULL) {
4415 err = got_error_from_errno2("basename", ie->path);
4416 goto done;
4419 te = got_object_tree_find_entry(tree, te_name);
4420 if (te == NULL && status != GOT_STATUS_ADD &&
4421 staged_status != GOT_STATUS_ADD) {
4422 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4423 goto done;
4427 switch (status) {
4428 case GOT_STATUS_ADD:
4429 if (a->patch_cb) {
4430 int choice = GOT_PATCH_CHOICE_NONE;
4431 err = (*a->patch_cb)(&choice, a->patch_arg,
4432 status, ie->path, NULL, 1, 1);
4433 if (err)
4434 goto done;
4435 if (choice != GOT_PATCH_CHOICE_YES)
4436 break;
4438 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4439 ie->path);
4440 if (err)
4441 goto done;
4442 got_fileindex_entry_remove(a->fileindex, ie);
4443 break;
4444 case GOT_STATUS_DELETE:
4445 if (a->patch_cb) {
4446 int choice = GOT_PATCH_CHOICE_NONE;
4447 err = (*a->patch_cb)(&choice, a->patch_arg,
4448 status, ie->path, NULL, 1, 1);
4449 if (err)
4450 goto done;
4451 if (choice != GOT_PATCH_CHOICE_YES)
4452 break;
4454 /* fall through */
4455 case GOT_STATUS_MODIFY:
4456 case GOT_STATUS_MODE_CHANGE:
4457 case GOT_STATUS_CONFLICT:
4458 case GOT_STATUS_MISSING: {
4459 struct got_object_id id;
4460 if (staged_status == GOT_STATUS_ADD ||
4461 staged_status == GOT_STATUS_MODIFY) {
4462 memcpy(id.sha1, ie->staged_blob_sha1,
4463 SHA1_DIGEST_LENGTH);
4464 } else
4465 memcpy(id.sha1, ie->blob_sha1,
4466 SHA1_DIGEST_LENGTH);
4467 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4468 if (err)
4469 goto done;
4471 if (asprintf(&ondisk_path, "%s/%s",
4472 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4473 err = got_error_from_errno("asprintf");
4474 goto done;
4477 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4478 status == GOT_STATUS_CONFLICT)) {
4479 int is_bad_symlink = 0;
4480 err = create_patched_content(&path_content, 1, &id,
4481 ondisk_path, dirfd, de_name, ie->path, a->repo,
4482 a->patch_cb, a->patch_arg);
4483 if (err || path_content == NULL)
4484 break;
4485 if (te && S_ISLNK(te->mode)) {
4486 if (unlink(path_content) == -1) {
4487 err = got_error_from_errno2("unlink",
4488 path_content);
4489 break;
4491 err = install_symlink(&is_bad_symlink,
4492 a->worktree, ondisk_path, ie->path,
4493 blob, 0, 1, 0, a->repo,
4494 a->progress_cb, a->progress_arg);
4495 } else {
4496 if (rename(path_content, ondisk_path) == -1) {
4497 err = got_error_from_errno3("rename",
4498 path_content, ondisk_path);
4499 goto done;
4502 } else {
4503 int is_bad_symlink = 0;
4504 if (te && S_ISLNK(te->mode)) {
4505 err = install_symlink(&is_bad_symlink,
4506 a->worktree, ondisk_path, ie->path,
4507 blob, 0, 1, 0, a->repo,
4508 a->progress_cb, a->progress_arg);
4509 } else {
4510 err = install_blob(a->worktree, ondisk_path,
4511 ie->path,
4512 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4513 got_fileindex_perms_to_st(ie), blob,
4514 0, 1, 0, 0, a->repo,
4515 a->progress_cb, a->progress_arg);
4517 if (err)
4518 goto done;
4519 if (status == GOT_STATUS_DELETE ||
4520 status == GOT_STATUS_MODE_CHANGE) {
4521 err = got_fileindex_entry_update(ie,
4522 ondisk_path, blob->id.sha1,
4523 a->worktree->base_commit_id->sha1, 1);
4524 if (err)
4525 goto done;
4527 if (is_bad_symlink) {
4528 got_fileindex_entry_filetype_set(ie,
4529 GOT_FILEIDX_MODE_BAD_SYMLINK);
4532 break;
4534 default:
4535 break;
4537 done:
4538 free(ondisk_path);
4539 free(path_content);
4540 free(parent_path);
4541 free(tree_path);
4542 if (blob)
4543 got_object_blob_close(blob);
4544 if (tree)
4545 got_object_tree_close(tree);
4546 free(tree_id);
4547 return err;
4550 const struct got_error *
4551 got_worktree_revert(struct got_worktree *worktree,
4552 struct got_pathlist_head *paths,
4553 got_worktree_checkout_cb progress_cb, void *progress_arg,
4554 got_worktree_patch_cb patch_cb, void *patch_arg,
4555 struct got_repository *repo)
4557 struct got_fileindex *fileindex = NULL;
4558 char *fileindex_path = NULL;
4559 const struct got_error *err = NULL, *unlockerr = NULL;
4560 const struct got_error *sync_err = NULL;
4561 struct got_pathlist_entry *pe;
4562 struct revert_file_args rfa;
4564 err = lock_worktree(worktree, LOCK_EX);
4565 if (err)
4566 return err;
4568 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4569 if (err)
4570 goto done;
4572 rfa.worktree = worktree;
4573 rfa.fileindex = fileindex;
4574 rfa.progress_cb = progress_cb;
4575 rfa.progress_arg = progress_arg;
4576 rfa.patch_cb = patch_cb;
4577 rfa.patch_arg = patch_arg;
4578 rfa.repo = repo;
4579 TAILQ_FOREACH(pe, paths, entry) {
4580 err = worktree_status(worktree, pe->path, fileindex, repo,
4581 revert_file, &rfa, NULL, NULL, 0, 0);
4582 if (err)
4583 break;
4585 sync_err = sync_fileindex(fileindex, fileindex_path);
4586 if (sync_err && err == NULL)
4587 err = sync_err;
4588 done:
4589 free(fileindex_path);
4590 if (fileindex)
4591 got_fileindex_free(fileindex);
4592 unlockerr = lock_worktree(worktree, LOCK_SH);
4593 if (unlockerr && err == NULL)
4594 err = unlockerr;
4595 return err;
4598 static void
4599 free_commitable(struct got_commitable *ct)
4601 free(ct->path);
4602 free(ct->in_repo_path);
4603 free(ct->ondisk_path);
4604 free(ct->blob_id);
4605 free(ct->base_blob_id);
4606 free(ct->staged_blob_id);
4607 free(ct->base_commit_id);
4608 free(ct);
4611 struct collect_commitables_arg {
4612 struct got_pathlist_head *commitable_paths;
4613 struct got_repository *repo;
4614 struct got_worktree *worktree;
4615 struct got_fileindex *fileindex;
4616 int have_staged_files;
4617 int allow_bad_symlinks;
4620 static const struct got_error *
4621 collect_commitables(void *arg, unsigned char status,
4622 unsigned char staged_status, const char *relpath,
4623 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4624 struct got_object_id *commit_id, int dirfd, const char *de_name)
4626 struct collect_commitables_arg *a = arg;
4627 const struct got_error *err = NULL;
4628 struct got_commitable *ct = NULL;
4629 struct got_pathlist_entry *new = NULL;
4630 char *parent_path = NULL, *path = NULL;
4631 struct stat sb;
4633 if (a->have_staged_files) {
4634 if (staged_status != GOT_STATUS_MODIFY &&
4635 staged_status != GOT_STATUS_ADD &&
4636 staged_status != GOT_STATUS_DELETE)
4637 return NULL;
4638 } else {
4639 if (status == GOT_STATUS_CONFLICT)
4640 return got_error(GOT_ERR_COMMIT_CONFLICT);
4642 if (status != GOT_STATUS_MODIFY &&
4643 status != GOT_STATUS_MODE_CHANGE &&
4644 status != GOT_STATUS_ADD &&
4645 status != GOT_STATUS_DELETE)
4646 return NULL;
4649 if (asprintf(&path, "/%s", relpath) == -1) {
4650 err = got_error_from_errno("asprintf");
4651 goto done;
4653 if (strcmp(path, "/") == 0) {
4654 parent_path = strdup("");
4655 if (parent_path == NULL)
4656 return got_error_from_errno("strdup");
4657 } else {
4658 err = got_path_dirname(&parent_path, path);
4659 if (err)
4660 return err;
4663 ct = calloc(1, sizeof(*ct));
4664 if (ct == NULL) {
4665 err = got_error_from_errno("calloc");
4666 goto done;
4669 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4670 relpath) == -1) {
4671 err = got_error_from_errno("asprintf");
4672 goto done;
4675 if (staged_status == GOT_STATUS_ADD ||
4676 staged_status == GOT_STATUS_MODIFY) {
4677 struct got_fileindex_entry *ie;
4678 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4679 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4680 case GOT_FILEIDX_MODE_REGULAR_FILE:
4681 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4682 ct->mode = S_IFREG;
4683 break;
4684 case GOT_FILEIDX_MODE_SYMLINK:
4685 ct->mode = S_IFLNK;
4686 break;
4687 default:
4688 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4689 goto done;
4691 ct->mode |= got_fileindex_entry_perms_get(ie);
4692 } else if (status != GOT_STATUS_DELETE &&
4693 staged_status != GOT_STATUS_DELETE) {
4694 if (dirfd != -1) {
4695 if (fstatat(dirfd, de_name, &sb,
4696 AT_SYMLINK_NOFOLLOW) == -1) {
4697 err = got_error_from_errno2("fstatat",
4698 ct->ondisk_path);
4699 goto done;
4701 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4702 err = got_error_from_errno2("lstat", ct->ondisk_path);
4703 goto done;
4705 ct->mode = sb.st_mode;
4708 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4709 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4710 relpath) == -1) {
4711 err = got_error_from_errno("asprintf");
4712 goto done;
4715 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4716 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4717 int is_bad_symlink;
4718 char target_path[PATH_MAX];
4719 ssize_t target_len;
4720 target_len = readlink(ct->ondisk_path, target_path,
4721 sizeof(target_path));
4722 if (target_len == -1) {
4723 err = got_error_from_errno2("readlink",
4724 ct->ondisk_path);
4725 goto done;
4727 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4728 target_len, ct->ondisk_path, a->worktree->root_path);
4729 if (err)
4730 goto done;
4731 if (is_bad_symlink) {
4732 err = got_error_path(ct->ondisk_path,
4733 GOT_ERR_BAD_SYMLINK);
4734 goto done;
4739 ct->status = status;
4740 ct->staged_status = staged_status;
4741 ct->blob_id = NULL; /* will be filled in when blob gets created */
4742 if (ct->status != GOT_STATUS_ADD &&
4743 ct->staged_status != GOT_STATUS_ADD) {
4744 ct->base_blob_id = got_object_id_dup(blob_id);
4745 if (ct->base_blob_id == NULL) {
4746 err = got_error_from_errno("got_object_id_dup");
4747 goto done;
4749 ct->base_commit_id = got_object_id_dup(commit_id);
4750 if (ct->base_commit_id == NULL) {
4751 err = got_error_from_errno("got_object_id_dup");
4752 goto done;
4755 if (ct->staged_status == GOT_STATUS_ADD ||
4756 ct->staged_status == GOT_STATUS_MODIFY) {
4757 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4758 if (ct->staged_blob_id == NULL) {
4759 err = got_error_from_errno("got_object_id_dup");
4760 goto done;
4763 ct->path = strdup(path);
4764 if (ct->path == NULL) {
4765 err = got_error_from_errno("strdup");
4766 goto done;
4768 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4769 done:
4770 if (ct && (err || new == NULL))
4771 free_commitable(ct);
4772 free(parent_path);
4773 free(path);
4774 return err;
4777 static const struct got_error *write_tree(struct got_object_id **, int *,
4778 struct got_tree_object *, const char *, struct got_pathlist_head *,
4779 got_worktree_status_cb status_cb, void *status_arg,
4780 struct got_repository *);
4782 static const struct got_error *
4783 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4784 struct got_tree_entry *te, const char *parent_path,
4785 struct got_pathlist_head *commitable_paths,
4786 got_worktree_status_cb status_cb, void *status_arg,
4787 struct got_repository *repo)
4789 const struct got_error *err = NULL;
4790 struct got_tree_object *subtree;
4791 char *subpath;
4793 if (asprintf(&subpath, "%s%s%s", parent_path,
4794 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4795 return got_error_from_errno("asprintf");
4797 err = got_object_open_as_tree(&subtree, repo, &te->id);
4798 if (err)
4799 return err;
4801 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4802 commitable_paths, status_cb, status_arg, repo);
4803 got_object_tree_close(subtree);
4804 free(subpath);
4805 return err;
4808 static const struct got_error *
4809 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4811 const struct got_error *err = NULL;
4812 char *ct_parent_path = NULL;
4814 *match = 0;
4816 if (strchr(ct->in_repo_path, '/') == NULL) {
4817 *match = got_path_is_root_dir(path);
4818 return NULL;
4821 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4822 if (err)
4823 return err;
4824 *match = (strcmp(path, ct_parent_path) == 0);
4825 free(ct_parent_path);
4826 return err;
4829 static mode_t
4830 get_ct_file_mode(struct got_commitable *ct)
4832 if (S_ISLNK(ct->mode))
4833 return S_IFLNK;
4835 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4838 static const struct got_error *
4839 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4840 struct got_tree_entry *te, struct got_commitable *ct)
4842 const struct got_error *err = NULL;
4844 *new_te = NULL;
4846 err = got_object_tree_entry_dup(new_te, te);
4847 if (err)
4848 goto done;
4850 (*new_te)->mode = get_ct_file_mode(ct);
4852 if (ct->staged_status == GOT_STATUS_MODIFY)
4853 memcpy(&(*new_te)->id, ct->staged_blob_id,
4854 sizeof((*new_te)->id));
4855 else
4856 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4857 done:
4858 if (err && *new_te) {
4859 free(*new_te);
4860 *new_te = NULL;
4862 return err;
4865 static const struct got_error *
4866 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4867 struct got_commitable *ct)
4869 const struct got_error *err = NULL;
4870 char *ct_name;
4872 *new_te = NULL;
4874 *new_te = calloc(1, sizeof(**new_te));
4875 if (*new_te == NULL)
4876 return got_error_from_errno("calloc");
4878 ct_name = basename(ct->path);
4879 if (ct_name == NULL) {
4880 err = got_error_from_errno2("basename", ct->path);
4881 goto done;
4883 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4884 sizeof((*new_te)->name)) {
4885 err = got_error(GOT_ERR_NO_SPACE);
4886 goto done;
4889 (*new_te)->mode = get_ct_file_mode(ct);
4891 if (ct->staged_status == GOT_STATUS_ADD)
4892 memcpy(&(*new_te)->id, ct->staged_blob_id,
4893 sizeof((*new_te)->id));
4894 else
4895 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4896 done:
4897 if (err && *new_te) {
4898 free(*new_te);
4899 *new_te = NULL;
4901 return err;
4904 static const struct got_error *
4905 insert_tree_entry(struct got_tree_entry *new_te,
4906 struct got_pathlist_head *paths)
4908 const struct got_error *err = NULL;
4909 struct got_pathlist_entry *new_pe;
4911 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4912 if (err)
4913 return err;
4914 if (new_pe == NULL)
4915 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4916 return NULL;
4919 static const struct got_error *
4920 report_ct_status(struct got_commitable *ct,
4921 got_worktree_status_cb status_cb, void *status_arg)
4923 const char *ct_path = ct->path;
4924 unsigned char status;
4926 while (ct_path[0] == '/')
4927 ct_path++;
4929 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4930 status = ct->staged_status;
4931 else
4932 status = ct->status;
4934 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4935 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4938 static const struct got_error *
4939 match_modified_subtree(int *modified, struct got_tree_entry *te,
4940 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4942 const struct got_error *err = NULL;
4943 struct got_pathlist_entry *pe;
4944 char *te_path;
4946 *modified = 0;
4948 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4949 got_path_is_root_dir(base_tree_path) ? "" : "/",
4950 te->name) == -1)
4951 return got_error_from_errno("asprintf");
4953 TAILQ_FOREACH(pe, commitable_paths, entry) {
4954 struct got_commitable *ct = pe->data;
4955 *modified = got_path_is_child(ct->in_repo_path, te_path,
4956 strlen(te_path));
4957 if (*modified)
4958 break;
4961 free(te_path);
4962 return err;
4965 static const struct got_error *
4966 match_deleted_or_modified_ct(struct got_commitable **ctp,
4967 struct got_tree_entry *te, const char *base_tree_path,
4968 struct got_pathlist_head *commitable_paths)
4970 const struct got_error *err = NULL;
4971 struct got_pathlist_entry *pe;
4973 *ctp = NULL;
4975 TAILQ_FOREACH(pe, commitable_paths, entry) {
4976 struct got_commitable *ct = pe->data;
4977 char *ct_name = NULL;
4978 int path_matches;
4980 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4981 if (ct->status != GOT_STATUS_MODIFY &&
4982 ct->status != GOT_STATUS_MODE_CHANGE &&
4983 ct->status != GOT_STATUS_DELETE)
4984 continue;
4985 } else {
4986 if (ct->staged_status != GOT_STATUS_MODIFY &&
4987 ct->staged_status != GOT_STATUS_DELETE)
4988 continue;
4991 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4992 continue;
4994 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4995 if (err)
4996 return err;
4997 if (!path_matches)
4998 continue;
5000 ct_name = basename(pe->path);
5001 if (ct_name == NULL)
5002 return got_error_from_errno2("basename", pe->path);
5004 if (strcmp(te->name, ct_name) != 0)
5005 continue;
5007 *ctp = ct;
5008 break;
5011 return err;
5014 static const struct got_error *
5015 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5016 const char *child_path, const char *path_base_tree,
5017 struct got_pathlist_head *commitable_paths,
5018 got_worktree_status_cb status_cb, void *status_arg,
5019 struct got_repository *repo)
5021 const struct got_error *err = NULL;
5022 struct got_tree_entry *new_te;
5023 char *subtree_path;
5024 struct got_object_id *id = NULL;
5025 int nentries;
5027 *new_tep = NULL;
5029 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5030 got_path_is_root_dir(path_base_tree) ? "" : "/",
5031 child_path) == -1)
5032 return got_error_from_errno("asprintf");
5034 new_te = calloc(1, sizeof(*new_te));
5035 if (new_te == NULL)
5036 return got_error_from_errno("calloc");
5037 new_te->mode = S_IFDIR;
5039 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5040 sizeof(new_te->name)) {
5041 err = got_error(GOT_ERR_NO_SPACE);
5042 goto done;
5044 err = write_tree(&id, &nentries, NULL, subtree_path,
5045 commitable_paths, status_cb, status_arg, repo);
5046 if (err) {
5047 free(new_te);
5048 goto done;
5050 memcpy(&new_te->id, id, sizeof(new_te->id));
5051 done:
5052 free(id);
5053 free(subtree_path);
5054 if (err == NULL)
5055 *new_tep = new_te;
5056 return err;
5059 static const struct got_error *
5060 write_tree(struct got_object_id **new_tree_id, int *nentries,
5061 struct got_tree_object *base_tree, const char *path_base_tree,
5062 struct got_pathlist_head *commitable_paths,
5063 got_worktree_status_cb status_cb, void *status_arg,
5064 struct got_repository *repo)
5066 const struct got_error *err = NULL;
5067 struct got_pathlist_head paths;
5068 struct got_tree_entry *te, *new_te = NULL;
5069 struct got_pathlist_entry *pe;
5071 TAILQ_INIT(&paths);
5072 *nentries = 0;
5074 /* Insert, and recurse into, newly added entries first. */
5075 TAILQ_FOREACH(pe, commitable_paths, entry) {
5076 struct got_commitable *ct = pe->data;
5077 char *child_path = NULL, *slash;
5079 if ((ct->status != GOT_STATUS_ADD &&
5080 ct->staged_status != GOT_STATUS_ADD) ||
5081 (ct->flags & GOT_COMMITABLE_ADDED))
5082 continue;
5084 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5085 strlen(path_base_tree)))
5086 continue;
5088 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5089 ct->in_repo_path);
5090 if (err)
5091 goto done;
5093 slash = strchr(child_path, '/');
5094 if (slash == NULL) {
5095 err = alloc_added_blob_tree_entry(&new_te, ct);
5096 if (err)
5097 goto done;
5098 err = report_ct_status(ct, status_cb, status_arg);
5099 if (err)
5100 goto done;
5101 ct->flags |= GOT_COMMITABLE_ADDED;
5102 err = insert_tree_entry(new_te, &paths);
5103 if (err)
5104 goto done;
5105 (*nentries)++;
5106 } else {
5107 *slash = '\0'; /* trim trailing path components */
5108 if (base_tree == NULL ||
5109 got_object_tree_find_entry(base_tree, child_path)
5110 == NULL) {
5111 err = make_subtree_for_added_blob(&new_te,
5112 child_path, path_base_tree,
5113 commitable_paths, status_cb, status_arg,
5114 repo);
5115 if (err)
5116 goto done;
5117 err = insert_tree_entry(new_te, &paths);
5118 if (err)
5119 goto done;
5120 (*nentries)++;
5125 if (base_tree) {
5126 int i, nbase_entries;
5127 /* Handle modified and deleted entries. */
5128 nbase_entries = got_object_tree_get_nentries(base_tree);
5129 for (i = 0; i < nbase_entries; i++) {
5130 struct got_commitable *ct = NULL;
5132 te = got_object_tree_get_entry(base_tree, i);
5133 if (got_object_tree_entry_is_submodule(te)) {
5134 /* Entry is a submodule; just copy it. */
5135 err = got_object_tree_entry_dup(&new_te, te);
5136 if (err)
5137 goto done;
5138 err = insert_tree_entry(new_te, &paths);
5139 if (err)
5140 goto done;
5141 (*nentries)++;
5142 continue;
5145 if (S_ISDIR(te->mode)) {
5146 int modified;
5147 err = got_object_tree_entry_dup(&new_te, te);
5148 if (err)
5149 goto done;
5150 err = match_modified_subtree(&modified, te,
5151 path_base_tree, commitable_paths);
5152 if (err)
5153 goto done;
5154 /* Avoid recursion into unmodified subtrees. */
5155 if (modified) {
5156 struct got_object_id *new_id;
5157 int nsubentries;
5158 err = write_subtree(&new_id,
5159 &nsubentries, te,
5160 path_base_tree, commitable_paths,
5161 status_cb, status_arg, repo);
5162 if (err)
5163 goto done;
5164 if (nsubentries == 0) {
5165 /* All entries were deleted. */
5166 free(new_id);
5167 continue;
5169 memcpy(&new_te->id, new_id,
5170 sizeof(new_te->id));
5171 free(new_id);
5173 err = insert_tree_entry(new_te, &paths);
5174 if (err)
5175 goto done;
5176 (*nentries)++;
5177 continue;
5180 err = match_deleted_or_modified_ct(&ct, te,
5181 path_base_tree, commitable_paths);
5182 if (err)
5183 goto done;
5184 if (ct) {
5185 /* NB: Deleted entries get dropped here. */
5186 if (ct->status == GOT_STATUS_MODIFY ||
5187 ct->status == GOT_STATUS_MODE_CHANGE ||
5188 ct->staged_status == GOT_STATUS_MODIFY) {
5189 err = alloc_modified_blob_tree_entry(
5190 &new_te, te, ct);
5191 if (err)
5192 goto done;
5193 err = insert_tree_entry(new_te, &paths);
5194 if (err)
5195 goto done;
5196 (*nentries)++;
5198 err = report_ct_status(ct, status_cb,
5199 status_arg);
5200 if (err)
5201 goto done;
5202 } else {
5203 /* Entry is unchanged; just copy it. */
5204 err = got_object_tree_entry_dup(&new_te, te);
5205 if (err)
5206 goto done;
5207 err = insert_tree_entry(new_te, &paths);
5208 if (err)
5209 goto done;
5210 (*nentries)++;
5215 /* Write new list of entries; deleted entries have been dropped. */
5216 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5217 done:
5218 got_pathlist_free(&paths);
5219 return err;
5222 static const struct got_error *
5223 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5224 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5225 int have_staged_files)
5227 const struct got_error *err = NULL;
5228 struct got_pathlist_entry *pe;
5230 TAILQ_FOREACH(pe, commitable_paths, entry) {
5231 struct got_fileindex_entry *ie;
5232 struct got_commitable *ct = pe->data;
5234 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5235 if (ie) {
5236 if (ct->status == GOT_STATUS_DELETE ||
5237 ct->staged_status == GOT_STATUS_DELETE) {
5238 got_fileindex_entry_remove(fileindex, ie);
5239 } else if (ct->staged_status == GOT_STATUS_ADD ||
5240 ct->staged_status == GOT_STATUS_MODIFY) {
5241 got_fileindex_entry_stage_set(ie,
5242 GOT_FILEIDX_STAGE_NONE);
5243 err = got_fileindex_entry_update(ie,
5244 ct->ondisk_path, ct->staged_blob_id->sha1,
5245 new_base_commit_id->sha1,
5246 !have_staged_files);
5247 } else
5248 err = got_fileindex_entry_update(ie,
5249 ct->ondisk_path, ct->blob_id->sha1,
5250 new_base_commit_id->sha1,
5251 !have_staged_files);
5252 } else {
5253 err = got_fileindex_entry_alloc(&ie, pe->path);
5254 if (err)
5255 break;
5256 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5257 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5258 if (err) {
5259 got_fileindex_entry_free(ie);
5260 break;
5262 err = got_fileindex_entry_add(fileindex, ie);
5263 if (err) {
5264 got_fileindex_entry_free(ie);
5265 break;
5269 return err;
5273 static const struct got_error *
5274 check_out_of_date(const char *in_repo_path, unsigned char status,
5275 unsigned char staged_status, struct got_object_id *base_blob_id,
5276 struct got_object_id *base_commit_id,
5277 struct got_object_id *head_commit_id, struct got_repository *repo,
5278 int ood_errcode)
5280 const struct got_error *err = NULL;
5281 struct got_object_id *id = NULL;
5283 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5284 /* Trivial case: base commit == head commit */
5285 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5286 return NULL;
5288 * Ensure file content which local changes were based
5289 * on matches file content in the branch head.
5291 err = got_object_id_by_path(&id, repo, head_commit_id,
5292 in_repo_path);
5293 if (err) {
5294 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5295 err = got_error(ood_errcode);
5296 goto done;
5297 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5298 err = got_error(ood_errcode);
5299 } else {
5300 /* Require that added files don't exist in the branch head. */
5301 err = got_object_id_by_path(&id, repo, head_commit_id,
5302 in_repo_path);
5303 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5304 goto done;
5305 err = id ? got_error(ood_errcode) : NULL;
5307 done:
5308 free(id);
5309 return err;
5312 const struct got_error *
5313 commit_worktree(struct got_object_id **new_commit_id,
5314 struct got_pathlist_head *commitable_paths,
5315 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5316 const char *author, const char *committer,
5317 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5318 got_worktree_status_cb status_cb, void *status_arg,
5319 struct got_repository *repo)
5321 const struct got_error *err = NULL, *unlockerr = NULL;
5322 struct got_pathlist_entry *pe;
5323 const char *head_ref_name = NULL;
5324 struct got_commit_object *head_commit = NULL;
5325 struct got_reference *head_ref2 = NULL;
5326 struct got_object_id *head_commit_id2 = NULL;
5327 struct got_tree_object *head_tree = NULL;
5328 struct got_object_id *new_tree_id = NULL;
5329 int nentries;
5330 struct got_object_id_queue parent_ids;
5331 struct got_object_qid *pid = NULL;
5332 char *logmsg = NULL;
5334 *new_commit_id = NULL;
5336 SIMPLEQ_INIT(&parent_ids);
5338 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5339 if (err)
5340 goto done;
5342 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5343 if (err)
5344 goto done;
5346 if (commit_msg_cb != NULL) {
5347 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5348 if (err)
5349 goto done;
5352 if (logmsg == NULL || strlen(logmsg) == 0) {
5353 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5354 goto done;
5357 /* Create blobs from added and modified files and record their IDs. */
5358 TAILQ_FOREACH(pe, commitable_paths, entry) {
5359 struct got_commitable *ct = pe->data;
5360 char *ondisk_path;
5362 /* Blobs for staged files already exist. */
5363 if (ct->staged_status == GOT_STATUS_ADD ||
5364 ct->staged_status == GOT_STATUS_MODIFY)
5365 continue;
5367 if (ct->status != GOT_STATUS_ADD &&
5368 ct->status != GOT_STATUS_MODIFY &&
5369 ct->status != GOT_STATUS_MODE_CHANGE)
5370 continue;
5372 if (asprintf(&ondisk_path, "%s/%s",
5373 worktree->root_path, pe->path) == -1) {
5374 err = got_error_from_errno("asprintf");
5375 goto done;
5377 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5378 free(ondisk_path);
5379 if (err)
5380 goto done;
5383 /* Recursively write new tree objects. */
5384 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5385 commitable_paths, status_cb, status_arg, repo);
5386 if (err)
5387 goto done;
5389 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5390 if (err)
5391 goto done;
5392 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5393 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5394 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5395 got_object_qid_free(pid);
5396 if (logmsg != NULL)
5397 free(logmsg);
5398 if (err)
5399 goto done;
5401 /* Check if a concurrent commit to our branch has occurred. */
5402 head_ref_name = got_worktree_get_head_ref_name(worktree);
5403 if (head_ref_name == NULL) {
5404 err = got_error_from_errno("got_worktree_get_head_ref_name");
5405 goto done;
5407 /* Lock the reference here to prevent concurrent modification. */
5408 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5409 if (err)
5410 goto done;
5411 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5412 if (err)
5413 goto done;
5414 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5415 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5416 goto done;
5418 /* Update branch head in repository. */
5419 err = got_ref_change_ref(head_ref2, *new_commit_id);
5420 if (err)
5421 goto done;
5422 err = got_ref_write(head_ref2, repo);
5423 if (err)
5424 goto done;
5426 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5427 if (err)
5428 goto done;
5430 err = ref_base_commit(worktree, repo);
5431 if (err)
5432 goto done;
5433 done:
5434 if (head_tree)
5435 got_object_tree_close(head_tree);
5436 if (head_commit)
5437 got_object_commit_close(head_commit);
5438 free(head_commit_id2);
5439 if (head_ref2) {
5440 unlockerr = got_ref_unlock(head_ref2);
5441 if (unlockerr && err == NULL)
5442 err = unlockerr;
5443 got_ref_close(head_ref2);
5445 return err;
5448 static const struct got_error *
5449 check_path_is_commitable(const char *path,
5450 struct got_pathlist_head *commitable_paths)
5452 struct got_pathlist_entry *cpe = NULL;
5453 size_t path_len = strlen(path);
5455 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5456 struct got_commitable *ct = cpe->data;
5457 const char *ct_path = ct->path;
5459 while (ct_path[0] == '/')
5460 ct_path++;
5462 if (strcmp(path, ct_path) == 0 ||
5463 got_path_is_child(ct_path, path, path_len))
5464 break;
5467 if (cpe == NULL)
5468 return got_error_path(path, GOT_ERR_BAD_PATH);
5470 return NULL;
5473 static const struct got_error *
5474 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5476 int *have_staged_files = arg;
5478 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5479 *have_staged_files = 1;
5480 return got_error(GOT_ERR_CANCELLED);
5483 return NULL;
5486 static const struct got_error *
5487 check_non_staged_files(struct got_fileindex *fileindex,
5488 struct got_pathlist_head *paths)
5490 struct got_pathlist_entry *pe;
5491 struct got_fileindex_entry *ie;
5493 TAILQ_FOREACH(pe, paths, entry) {
5494 if (pe->path[0] == '\0')
5495 continue;
5496 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5497 if (ie == NULL)
5498 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5499 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5500 return got_error_path(pe->path,
5501 GOT_ERR_FILE_NOT_STAGED);
5504 return NULL;
5507 const struct got_error *
5508 got_worktree_commit(struct got_object_id **new_commit_id,
5509 struct got_worktree *worktree, struct got_pathlist_head *paths,
5510 const char *author, const char *committer, int allow_bad_symlinks,
5511 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5512 got_worktree_status_cb status_cb, void *status_arg,
5513 struct got_repository *repo)
5515 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5516 struct got_fileindex *fileindex = NULL;
5517 char *fileindex_path = NULL;
5518 struct got_pathlist_head commitable_paths;
5519 struct collect_commitables_arg cc_arg;
5520 struct got_pathlist_entry *pe;
5521 struct got_reference *head_ref = NULL;
5522 struct got_object_id *head_commit_id = NULL;
5523 int have_staged_files = 0;
5525 *new_commit_id = NULL;
5527 TAILQ_INIT(&commitable_paths);
5529 err = lock_worktree(worktree, LOCK_EX);
5530 if (err)
5531 goto done;
5533 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5534 if (err)
5535 goto done;
5537 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5538 if (err)
5539 goto done;
5541 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5542 if (err)
5543 goto done;
5545 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5546 &have_staged_files);
5547 if (err && err->code != GOT_ERR_CANCELLED)
5548 goto done;
5549 if (have_staged_files) {
5550 err = check_non_staged_files(fileindex, paths);
5551 if (err)
5552 goto done;
5555 cc_arg.commitable_paths = &commitable_paths;
5556 cc_arg.worktree = worktree;
5557 cc_arg.fileindex = fileindex;
5558 cc_arg.repo = repo;
5559 cc_arg.have_staged_files = have_staged_files;
5560 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5561 TAILQ_FOREACH(pe, paths, entry) {
5562 err = worktree_status(worktree, pe->path, fileindex, repo,
5563 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5564 if (err)
5565 goto done;
5568 if (TAILQ_EMPTY(&commitable_paths)) {
5569 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5570 goto done;
5573 TAILQ_FOREACH(pe, paths, entry) {
5574 err = check_path_is_commitable(pe->path, &commitable_paths);
5575 if (err)
5576 goto done;
5579 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5580 struct got_commitable *ct = pe->data;
5581 const char *ct_path = ct->in_repo_path;
5583 while (ct_path[0] == '/')
5584 ct_path++;
5585 err = check_out_of_date(ct_path, ct->status,
5586 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5587 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5588 if (err)
5589 goto done;
5593 err = commit_worktree(new_commit_id, &commitable_paths,
5594 head_commit_id, worktree, author, committer,
5595 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5596 if (err)
5597 goto done;
5599 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5600 fileindex, have_staged_files);
5601 sync_err = sync_fileindex(fileindex, fileindex_path);
5602 if (sync_err && err == NULL)
5603 err = sync_err;
5604 done:
5605 if (fileindex)
5606 got_fileindex_free(fileindex);
5607 free(fileindex_path);
5608 unlockerr = lock_worktree(worktree, LOCK_SH);
5609 if (unlockerr && err == NULL)
5610 err = unlockerr;
5611 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5612 struct got_commitable *ct = pe->data;
5613 free_commitable(ct);
5615 got_pathlist_free(&commitable_paths);
5616 return err;
5619 const char *
5620 got_commitable_get_path(struct got_commitable *ct)
5622 return ct->path;
5625 unsigned int
5626 got_commitable_get_status(struct got_commitable *ct)
5628 return ct->status;
5631 struct check_rebase_ok_arg {
5632 struct got_worktree *worktree;
5633 struct got_repository *repo;
5636 static const struct got_error *
5637 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5639 const struct got_error *err = NULL;
5640 struct check_rebase_ok_arg *a = arg;
5641 unsigned char status;
5642 struct stat sb;
5643 char *ondisk_path;
5645 /* Reject rebase of a work tree with mixed base commits. */
5646 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5647 SHA1_DIGEST_LENGTH))
5648 return got_error(GOT_ERR_MIXED_COMMITS);
5650 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5651 == -1)
5652 return got_error_from_errno("asprintf");
5654 /* Reject rebase of a work tree with modified or staged files. */
5655 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5656 free(ondisk_path);
5657 if (err)
5658 return err;
5660 if (status != GOT_STATUS_NO_CHANGE)
5661 return got_error(GOT_ERR_MODIFIED);
5662 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5663 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5665 return NULL;
5668 const struct got_error *
5669 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5670 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5671 struct got_worktree *worktree, struct got_reference *branch,
5672 struct got_repository *repo)
5674 const struct got_error *err = NULL;
5675 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5676 char *branch_ref_name = NULL;
5677 char *fileindex_path = NULL;
5678 struct check_rebase_ok_arg ok_arg;
5679 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5680 struct got_object_id *wt_branch_tip = NULL;
5682 *new_base_branch_ref = NULL;
5683 *tmp_branch = NULL;
5684 *fileindex = NULL;
5686 err = lock_worktree(worktree, LOCK_EX);
5687 if (err)
5688 return err;
5690 err = open_fileindex(fileindex, &fileindex_path, worktree);
5691 if (err)
5692 goto done;
5694 ok_arg.worktree = worktree;
5695 ok_arg.repo = repo;
5696 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5697 &ok_arg);
5698 if (err)
5699 goto done;
5701 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5702 if (err)
5703 goto done;
5705 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5706 if (err)
5707 goto done;
5709 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5710 if (err)
5711 goto done;
5713 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5714 0);
5715 if (err)
5716 goto done;
5718 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5719 if (err)
5720 goto done;
5721 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5722 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5723 goto done;
5726 err = got_ref_alloc_symref(new_base_branch_ref,
5727 new_base_branch_ref_name, wt_branch);
5728 if (err)
5729 goto done;
5730 err = got_ref_write(*new_base_branch_ref, repo);
5731 if (err)
5732 goto done;
5734 /* TODO Lock original branch's ref while rebasing? */
5736 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5737 if (err)
5738 goto done;
5740 err = got_ref_write(branch_ref, repo);
5741 if (err)
5742 goto done;
5744 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5745 worktree->base_commit_id);
5746 if (err)
5747 goto done;
5748 err = got_ref_write(*tmp_branch, repo);
5749 if (err)
5750 goto done;
5752 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5753 if (err)
5754 goto done;
5755 done:
5756 free(fileindex_path);
5757 free(tmp_branch_name);
5758 free(new_base_branch_ref_name);
5759 free(branch_ref_name);
5760 if (branch_ref)
5761 got_ref_close(branch_ref);
5762 if (wt_branch)
5763 got_ref_close(wt_branch);
5764 free(wt_branch_tip);
5765 if (err) {
5766 if (*new_base_branch_ref) {
5767 got_ref_close(*new_base_branch_ref);
5768 *new_base_branch_ref = NULL;
5770 if (*tmp_branch) {
5771 got_ref_close(*tmp_branch);
5772 *tmp_branch = NULL;
5774 if (*fileindex) {
5775 got_fileindex_free(*fileindex);
5776 *fileindex = NULL;
5778 lock_worktree(worktree, LOCK_SH);
5780 return err;
5783 const struct got_error *
5784 got_worktree_rebase_continue(struct got_object_id **commit_id,
5785 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5786 struct got_reference **branch, struct got_fileindex **fileindex,
5787 struct got_worktree *worktree, struct got_repository *repo)
5789 const struct got_error *err;
5790 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5791 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5792 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5793 char *fileindex_path = NULL;
5794 int have_staged_files = 0;
5796 *commit_id = NULL;
5797 *new_base_branch = NULL;
5798 *tmp_branch = NULL;
5799 *branch = NULL;
5800 *fileindex = NULL;
5802 err = lock_worktree(worktree, LOCK_EX);
5803 if (err)
5804 return err;
5806 err = open_fileindex(fileindex, &fileindex_path, worktree);
5807 if (err)
5808 goto done;
5810 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5811 &have_staged_files);
5812 if (err && err->code != GOT_ERR_CANCELLED)
5813 goto done;
5814 if (have_staged_files) {
5815 err = got_error(GOT_ERR_STAGED_PATHS);
5816 goto done;
5819 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5820 if (err)
5821 goto done;
5823 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5824 if (err)
5825 goto done;
5827 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5828 if (err)
5829 goto done;
5831 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5832 if (err)
5833 goto done;
5835 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5836 if (err)
5837 goto done;
5839 err = got_ref_open(branch, repo,
5840 got_ref_get_symref_target(branch_ref), 0);
5841 if (err)
5842 goto done;
5844 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5845 if (err)
5846 goto done;
5848 err = got_ref_resolve(commit_id, repo, commit_ref);
5849 if (err)
5850 goto done;
5852 err = got_ref_open(new_base_branch, repo,
5853 new_base_branch_ref_name, 0);
5854 if (err)
5855 goto done;
5857 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5858 if (err)
5859 goto done;
5860 done:
5861 free(commit_ref_name);
5862 free(branch_ref_name);
5863 free(fileindex_path);
5864 if (commit_ref)
5865 got_ref_close(commit_ref);
5866 if (branch_ref)
5867 got_ref_close(branch_ref);
5868 if (err) {
5869 free(*commit_id);
5870 *commit_id = NULL;
5871 if (*tmp_branch) {
5872 got_ref_close(*tmp_branch);
5873 *tmp_branch = NULL;
5875 if (*new_base_branch) {
5876 got_ref_close(*new_base_branch);
5877 *new_base_branch = NULL;
5879 if (*branch) {
5880 got_ref_close(*branch);
5881 *branch = NULL;
5883 if (*fileindex) {
5884 got_fileindex_free(*fileindex);
5885 *fileindex = NULL;
5887 lock_worktree(worktree, LOCK_SH);
5889 return err;
5892 const struct got_error *
5893 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5895 const struct got_error *err;
5896 char *tmp_branch_name = NULL;
5898 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5899 if (err)
5900 return err;
5902 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5903 free(tmp_branch_name);
5904 return NULL;
5907 static const struct got_error *
5908 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5909 char **logmsg, void *arg)
5911 *logmsg = arg;
5912 return NULL;
5915 static const struct got_error *
5916 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5917 const char *path, struct got_object_id *blob_id,
5918 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5919 int dirfd, const char *de_name)
5921 return NULL;
5924 struct collect_merged_paths_arg {
5925 got_worktree_checkout_cb progress_cb;
5926 void *progress_arg;
5927 struct got_pathlist_head *merged_paths;
5930 static const struct got_error *
5931 collect_merged_paths(void *arg, unsigned char status, const char *path)
5933 const struct got_error *err;
5934 struct collect_merged_paths_arg *a = arg;
5935 char *p;
5936 struct got_pathlist_entry *new;
5938 err = (*a->progress_cb)(a->progress_arg, status, path);
5939 if (err)
5940 return err;
5942 if (status != GOT_STATUS_MERGE &&
5943 status != GOT_STATUS_ADD &&
5944 status != GOT_STATUS_DELETE &&
5945 status != GOT_STATUS_CONFLICT)
5946 return NULL;
5948 p = strdup(path);
5949 if (p == NULL)
5950 return got_error_from_errno("strdup");
5952 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5953 if (err || new == NULL)
5954 free(p);
5955 return err;
5958 void
5959 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5961 struct got_pathlist_entry *pe;
5963 TAILQ_FOREACH(pe, merged_paths, entry)
5964 free((char *)pe->path);
5966 got_pathlist_free(merged_paths);
5969 static const struct got_error *
5970 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5971 int is_rebase, struct got_repository *repo)
5973 const struct got_error *err;
5974 struct got_reference *commit_ref = NULL;
5976 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5977 if (err) {
5978 if (err->code != GOT_ERR_NOT_REF)
5979 goto done;
5980 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5981 if (err)
5982 goto done;
5983 err = got_ref_write(commit_ref, repo);
5984 if (err)
5985 goto done;
5986 } else if (is_rebase) {
5987 struct got_object_id *stored_id;
5988 int cmp;
5990 err = got_ref_resolve(&stored_id, repo, commit_ref);
5991 if (err)
5992 goto done;
5993 cmp = got_object_id_cmp(commit_id, stored_id);
5994 free(stored_id);
5995 if (cmp != 0) {
5996 err = got_error(GOT_ERR_REBASE_COMMITID);
5997 goto done;
6000 done:
6001 if (commit_ref)
6002 got_ref_close(commit_ref);
6003 return err;
6006 static const struct got_error *
6007 rebase_merge_files(struct got_pathlist_head *merged_paths,
6008 const char *commit_ref_name, struct got_worktree *worktree,
6009 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6010 struct got_object_id *commit_id, struct got_repository *repo,
6011 got_worktree_checkout_cb progress_cb, void *progress_arg,
6012 got_cancel_cb cancel_cb, void *cancel_arg)
6014 const struct got_error *err;
6015 struct got_reference *commit_ref = NULL;
6016 struct collect_merged_paths_arg cmp_arg;
6017 char *fileindex_path;
6019 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6021 err = get_fileindex_path(&fileindex_path, worktree);
6022 if (err)
6023 return err;
6025 cmp_arg.progress_cb = progress_cb;
6026 cmp_arg.progress_arg = progress_arg;
6027 cmp_arg.merged_paths = merged_paths;
6028 err = merge_files(worktree, fileindex, fileindex_path,
6029 parent_commit_id, commit_id, repo, collect_merged_paths,
6030 &cmp_arg, cancel_cb, cancel_arg);
6031 if (commit_ref)
6032 got_ref_close(commit_ref);
6033 return err;
6036 const struct got_error *
6037 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6038 struct got_worktree *worktree, struct got_fileindex *fileindex,
6039 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6040 struct got_repository *repo,
6041 got_worktree_checkout_cb progress_cb, void *progress_arg,
6042 got_cancel_cb cancel_cb, void *cancel_arg)
6044 const struct got_error *err;
6045 char *commit_ref_name;
6047 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6048 if (err)
6049 return err;
6051 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6052 if (err)
6053 goto done;
6055 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6056 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6057 progress_arg, cancel_cb, cancel_arg);
6058 done:
6059 free(commit_ref_name);
6060 return err;
6063 const struct got_error *
6064 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6065 struct got_worktree *worktree, struct got_fileindex *fileindex,
6066 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6067 struct got_repository *repo,
6068 got_worktree_checkout_cb progress_cb, void *progress_arg,
6069 got_cancel_cb cancel_cb, void *cancel_arg)
6071 const struct got_error *err;
6072 char *commit_ref_name;
6074 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6075 if (err)
6076 return err;
6078 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6079 if (err)
6080 goto done;
6082 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6083 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6084 progress_arg, cancel_cb, cancel_arg);
6085 done:
6086 free(commit_ref_name);
6087 return err;
6090 static const struct got_error *
6091 rebase_commit(struct got_object_id **new_commit_id,
6092 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6093 struct got_worktree *worktree, struct got_fileindex *fileindex,
6094 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6095 const char *new_logmsg, struct got_repository *repo)
6097 const struct got_error *err, *sync_err;
6098 struct got_pathlist_head commitable_paths;
6099 struct collect_commitables_arg cc_arg;
6100 char *fileindex_path = NULL;
6101 struct got_reference *head_ref = NULL;
6102 struct got_object_id *head_commit_id = NULL;
6103 char *logmsg = NULL;
6105 TAILQ_INIT(&commitable_paths);
6106 *new_commit_id = NULL;
6108 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6110 err = get_fileindex_path(&fileindex_path, worktree);
6111 if (err)
6112 return err;
6114 cc_arg.commitable_paths = &commitable_paths;
6115 cc_arg.worktree = worktree;
6116 cc_arg.repo = repo;
6117 cc_arg.have_staged_files = 0;
6119 * If possible get the status of individual files directly to
6120 * avoid crawling the entire work tree once per rebased commit.
6121 * TODO: Ideally, merged_paths would contain a list of commitables
6122 * we could use so we could skip worktree_status() entirely.
6124 if (merged_paths) {
6125 struct got_pathlist_entry *pe;
6126 TAILQ_FOREACH(pe, merged_paths, entry) {
6127 err = worktree_status(worktree, pe->path, fileindex,
6128 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6129 0);
6130 if (err)
6131 goto done;
6133 } else {
6134 err = worktree_status(worktree, "", fileindex, repo,
6135 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6136 if (err)
6137 goto done;
6140 if (TAILQ_EMPTY(&commitable_paths)) {
6141 /* No-op change; commit will be elided. */
6142 err = got_ref_delete(commit_ref, repo);
6143 if (err)
6144 goto done;
6145 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6146 goto done;
6149 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6150 if (err)
6151 goto done;
6153 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6154 if (err)
6155 goto done;
6157 if (new_logmsg) {
6158 logmsg = strdup(new_logmsg);
6159 if (logmsg == NULL) {
6160 err = got_error_from_errno("strdup");
6161 goto done;
6163 } else {
6164 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6165 if (err)
6166 goto done;
6169 /* NB: commit_worktree will call free(logmsg) */
6170 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6171 worktree, got_object_commit_get_author(orig_commit),
6172 got_object_commit_get_committer(orig_commit),
6173 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6174 if (err)
6175 goto done;
6177 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6178 if (err)
6179 goto done;
6181 err = got_ref_delete(commit_ref, repo);
6182 if (err)
6183 goto done;
6185 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6186 fileindex, 0);
6187 sync_err = sync_fileindex(fileindex, fileindex_path);
6188 if (sync_err && err == NULL)
6189 err = sync_err;
6190 done:
6191 free(fileindex_path);
6192 free(head_commit_id);
6193 if (head_ref)
6194 got_ref_close(head_ref);
6195 if (err) {
6196 free(*new_commit_id);
6197 *new_commit_id = NULL;
6199 return err;
6202 const struct got_error *
6203 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6204 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6205 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6206 struct got_commit_object *orig_commit,
6207 struct got_object_id *orig_commit_id, struct got_repository *repo)
6209 const struct got_error *err;
6210 char *commit_ref_name;
6211 struct got_reference *commit_ref = NULL;
6212 struct got_object_id *commit_id = NULL;
6214 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6215 if (err)
6216 return err;
6218 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6219 if (err)
6220 goto done;
6221 err = got_ref_resolve(&commit_id, repo, commit_ref);
6222 if (err)
6223 goto done;
6224 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6225 err = got_error(GOT_ERR_REBASE_COMMITID);
6226 goto done;
6229 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6230 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6231 done:
6232 if (commit_ref)
6233 got_ref_close(commit_ref);
6234 free(commit_ref_name);
6235 free(commit_id);
6236 return err;
6239 const struct got_error *
6240 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6241 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6242 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6243 struct got_commit_object *orig_commit,
6244 struct got_object_id *orig_commit_id, const char *new_logmsg,
6245 struct got_repository *repo)
6247 const struct got_error *err;
6248 char *commit_ref_name;
6249 struct got_reference *commit_ref = NULL;
6251 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6252 if (err)
6253 return err;
6255 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6256 if (err)
6257 goto done;
6259 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6260 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6261 done:
6262 if (commit_ref)
6263 got_ref_close(commit_ref);
6264 free(commit_ref_name);
6265 return err;
6268 const struct got_error *
6269 got_worktree_rebase_postpone(struct got_worktree *worktree,
6270 struct got_fileindex *fileindex)
6272 if (fileindex)
6273 got_fileindex_free(fileindex);
6274 return lock_worktree(worktree, LOCK_SH);
6277 static const struct got_error *
6278 delete_ref(const char *name, struct got_repository *repo)
6280 const struct got_error *err;
6281 struct got_reference *ref;
6283 err = got_ref_open(&ref, repo, name, 0);
6284 if (err) {
6285 if (err->code == GOT_ERR_NOT_REF)
6286 return NULL;
6287 return err;
6290 err = got_ref_delete(ref, repo);
6291 got_ref_close(ref);
6292 return err;
6295 static const struct got_error *
6296 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6298 const struct got_error *err;
6299 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6300 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6302 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6303 if (err)
6304 goto done;
6305 err = delete_ref(tmp_branch_name, repo);
6306 if (err)
6307 goto done;
6309 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6310 if (err)
6311 goto done;
6312 err = delete_ref(new_base_branch_ref_name, repo);
6313 if (err)
6314 goto done;
6316 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6317 if (err)
6318 goto done;
6319 err = delete_ref(branch_ref_name, repo);
6320 if (err)
6321 goto done;
6323 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6324 if (err)
6325 goto done;
6326 err = delete_ref(commit_ref_name, repo);
6327 if (err)
6328 goto done;
6330 done:
6331 free(tmp_branch_name);
6332 free(new_base_branch_ref_name);
6333 free(branch_ref_name);
6334 free(commit_ref_name);
6335 return err;
6338 const struct got_error *
6339 got_worktree_rebase_complete(struct got_worktree *worktree,
6340 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6341 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6342 struct got_repository *repo)
6344 const struct got_error *err, *unlockerr;
6345 struct got_object_id *new_head_commit_id = NULL;
6347 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6348 if (err)
6349 return err;
6351 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6352 if (err)
6353 goto done;
6355 err = got_ref_write(rebased_branch, repo);
6356 if (err)
6357 goto done;
6359 err = got_worktree_set_head_ref(worktree, rebased_branch);
6360 if (err)
6361 goto done;
6363 err = delete_rebase_refs(worktree, repo);
6364 done:
6365 if (fileindex)
6366 got_fileindex_free(fileindex);
6367 free(new_head_commit_id);
6368 unlockerr = lock_worktree(worktree, LOCK_SH);
6369 if (unlockerr && err == NULL)
6370 err = unlockerr;
6371 return err;
6374 const struct got_error *
6375 got_worktree_rebase_abort(struct got_worktree *worktree,
6376 struct got_fileindex *fileindex, struct got_repository *repo,
6377 struct got_reference *new_base_branch,
6378 got_worktree_checkout_cb progress_cb, void *progress_arg)
6380 const struct got_error *err, *unlockerr, *sync_err;
6381 struct got_reference *resolved = NULL;
6382 struct got_object_id *commit_id = NULL;
6383 char *fileindex_path = NULL;
6384 struct revert_file_args rfa;
6385 struct got_object_id *tree_id = NULL;
6387 err = lock_worktree(worktree, LOCK_EX);
6388 if (err)
6389 return err;
6391 err = got_ref_open(&resolved, repo,
6392 got_ref_get_symref_target(new_base_branch), 0);
6393 if (err)
6394 goto done;
6396 err = got_worktree_set_head_ref(worktree, resolved);
6397 if (err)
6398 goto done;
6401 * XXX commits to the base branch could have happened while
6402 * we were busy rebasing; should we store the original commit ID
6403 * when rebase begins and read it back here?
6405 err = got_ref_resolve(&commit_id, repo, resolved);
6406 if (err)
6407 goto done;
6409 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6410 if (err)
6411 goto done;
6413 err = got_object_id_by_path(&tree_id, repo,
6414 worktree->base_commit_id, worktree->path_prefix);
6415 if (err)
6416 goto done;
6418 err = delete_rebase_refs(worktree, repo);
6419 if (err)
6420 goto done;
6422 err = get_fileindex_path(&fileindex_path, worktree);
6423 if (err)
6424 goto done;
6426 rfa.worktree = worktree;
6427 rfa.fileindex = fileindex;
6428 rfa.progress_cb = progress_cb;
6429 rfa.progress_arg = progress_arg;
6430 rfa.patch_cb = NULL;
6431 rfa.patch_arg = NULL;
6432 rfa.repo = repo;
6433 err = worktree_status(worktree, "", fileindex, repo,
6434 revert_file, &rfa, NULL, NULL, 0, 0);
6435 if (err)
6436 goto sync;
6438 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6439 repo, progress_cb, progress_arg, NULL, NULL);
6440 sync:
6441 sync_err = sync_fileindex(fileindex, fileindex_path);
6442 if (sync_err && err == NULL)
6443 err = sync_err;
6444 done:
6445 got_ref_close(resolved);
6446 free(tree_id);
6447 free(commit_id);
6448 if (fileindex)
6449 got_fileindex_free(fileindex);
6450 free(fileindex_path);
6452 unlockerr = lock_worktree(worktree, LOCK_SH);
6453 if (unlockerr && err == NULL)
6454 err = unlockerr;
6455 return err;
6458 const struct got_error *
6459 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6460 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6461 struct got_fileindex **fileindex, struct got_worktree *worktree,
6462 struct got_repository *repo)
6464 const struct got_error *err = NULL;
6465 char *tmp_branch_name = NULL;
6466 char *branch_ref_name = NULL;
6467 char *base_commit_ref_name = NULL;
6468 char *fileindex_path = NULL;
6469 struct check_rebase_ok_arg ok_arg;
6470 struct got_reference *wt_branch = NULL;
6471 struct got_reference *base_commit_ref = NULL;
6473 *tmp_branch = NULL;
6474 *branch_ref = NULL;
6475 *base_commit_id = NULL;
6476 *fileindex = NULL;
6478 err = lock_worktree(worktree, LOCK_EX);
6479 if (err)
6480 return err;
6482 err = open_fileindex(fileindex, &fileindex_path, worktree);
6483 if (err)
6484 goto done;
6486 ok_arg.worktree = worktree;
6487 ok_arg.repo = repo;
6488 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6489 &ok_arg);
6490 if (err)
6491 goto done;
6493 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6494 if (err)
6495 goto done;
6497 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6498 if (err)
6499 goto done;
6501 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6502 worktree);
6503 if (err)
6504 goto done;
6506 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6507 0);
6508 if (err)
6509 goto done;
6511 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6512 if (err)
6513 goto done;
6515 err = got_ref_write(*branch_ref, repo);
6516 if (err)
6517 goto done;
6519 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6520 worktree->base_commit_id);
6521 if (err)
6522 goto done;
6523 err = got_ref_write(base_commit_ref, repo);
6524 if (err)
6525 goto done;
6526 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6527 if (*base_commit_id == NULL) {
6528 err = got_error_from_errno("got_object_id_dup");
6529 goto done;
6532 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6533 worktree->base_commit_id);
6534 if (err)
6535 goto done;
6536 err = got_ref_write(*tmp_branch, repo);
6537 if (err)
6538 goto done;
6540 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6541 if (err)
6542 goto done;
6543 done:
6544 free(fileindex_path);
6545 free(tmp_branch_name);
6546 free(branch_ref_name);
6547 free(base_commit_ref_name);
6548 if (wt_branch)
6549 got_ref_close(wt_branch);
6550 if (err) {
6551 if (*branch_ref) {
6552 got_ref_close(*branch_ref);
6553 *branch_ref = NULL;
6555 if (*tmp_branch) {
6556 got_ref_close(*tmp_branch);
6557 *tmp_branch = NULL;
6559 free(*base_commit_id);
6560 if (*fileindex) {
6561 got_fileindex_free(*fileindex);
6562 *fileindex = NULL;
6564 lock_worktree(worktree, LOCK_SH);
6566 return err;
6569 const struct got_error *
6570 got_worktree_histedit_postpone(struct got_worktree *worktree,
6571 struct got_fileindex *fileindex)
6573 if (fileindex)
6574 got_fileindex_free(fileindex);
6575 return lock_worktree(worktree, LOCK_SH);
6578 const struct got_error *
6579 got_worktree_histedit_in_progress(int *in_progress,
6580 struct got_worktree *worktree)
6582 const struct got_error *err;
6583 char *tmp_branch_name = NULL;
6585 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6586 if (err)
6587 return err;
6589 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6590 free(tmp_branch_name);
6591 return NULL;
6594 const struct got_error *
6595 got_worktree_histedit_continue(struct got_object_id **commit_id,
6596 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6597 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6598 struct got_worktree *worktree, struct got_repository *repo)
6600 const struct got_error *err;
6601 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6602 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6603 struct got_reference *commit_ref = NULL;
6604 struct got_reference *base_commit_ref = NULL;
6605 char *fileindex_path = NULL;
6606 int have_staged_files = 0;
6608 *commit_id = NULL;
6609 *tmp_branch = NULL;
6610 *base_commit_id = NULL;
6611 *fileindex = NULL;
6613 err = lock_worktree(worktree, LOCK_EX);
6614 if (err)
6615 return err;
6617 err = open_fileindex(fileindex, &fileindex_path, worktree);
6618 if (err)
6619 goto done;
6621 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6622 &have_staged_files);
6623 if (err && err->code != GOT_ERR_CANCELLED)
6624 goto done;
6625 if (have_staged_files) {
6626 err = got_error(GOT_ERR_STAGED_PATHS);
6627 goto done;
6630 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6631 if (err)
6632 goto done;
6634 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6635 if (err)
6636 goto done;
6638 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6639 if (err)
6640 goto done;
6642 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6643 worktree);
6644 if (err)
6645 goto done;
6647 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6648 if (err)
6649 goto done;
6651 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6652 if (err)
6653 goto done;
6654 err = got_ref_resolve(commit_id, repo, commit_ref);
6655 if (err)
6656 goto done;
6658 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6659 if (err)
6660 goto done;
6661 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6662 if (err)
6663 goto done;
6665 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6666 if (err)
6667 goto done;
6668 done:
6669 free(commit_ref_name);
6670 free(branch_ref_name);
6671 free(fileindex_path);
6672 if (commit_ref)
6673 got_ref_close(commit_ref);
6674 if (base_commit_ref)
6675 got_ref_close(base_commit_ref);
6676 if (err) {
6677 free(*commit_id);
6678 *commit_id = NULL;
6679 free(*base_commit_id);
6680 *base_commit_id = NULL;
6681 if (*tmp_branch) {
6682 got_ref_close(*tmp_branch);
6683 *tmp_branch = NULL;
6685 if (*fileindex) {
6686 got_fileindex_free(*fileindex);
6687 *fileindex = NULL;
6689 lock_worktree(worktree, LOCK_EX);
6691 return err;
6694 static const struct got_error *
6695 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6697 const struct got_error *err;
6698 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6699 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6701 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6702 if (err)
6703 goto done;
6704 err = delete_ref(tmp_branch_name, repo);
6705 if (err)
6706 goto done;
6708 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6709 worktree);
6710 if (err)
6711 goto done;
6712 err = delete_ref(base_commit_ref_name, repo);
6713 if (err)
6714 goto done;
6716 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6717 if (err)
6718 goto done;
6719 err = delete_ref(branch_ref_name, repo);
6720 if (err)
6721 goto done;
6723 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6724 if (err)
6725 goto done;
6726 err = delete_ref(commit_ref_name, repo);
6727 if (err)
6728 goto done;
6729 done:
6730 free(tmp_branch_name);
6731 free(base_commit_ref_name);
6732 free(branch_ref_name);
6733 free(commit_ref_name);
6734 return err;
6737 const struct got_error *
6738 got_worktree_histedit_abort(struct got_worktree *worktree,
6739 struct got_fileindex *fileindex, struct got_repository *repo,
6740 struct got_reference *branch, struct got_object_id *base_commit_id,
6741 got_worktree_checkout_cb progress_cb, void *progress_arg)
6743 const struct got_error *err, *unlockerr, *sync_err;
6744 struct got_reference *resolved = NULL;
6745 char *fileindex_path = NULL;
6746 struct got_object_id *tree_id = NULL;
6747 struct revert_file_args rfa;
6749 err = lock_worktree(worktree, LOCK_EX);
6750 if (err)
6751 return err;
6753 err = got_ref_open(&resolved, repo,
6754 got_ref_get_symref_target(branch), 0);
6755 if (err)
6756 goto done;
6758 err = got_worktree_set_head_ref(worktree, resolved);
6759 if (err)
6760 goto done;
6762 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6763 if (err)
6764 goto done;
6766 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6767 worktree->path_prefix);
6768 if (err)
6769 goto done;
6771 err = delete_histedit_refs(worktree, repo);
6772 if (err)
6773 goto done;
6775 err = get_fileindex_path(&fileindex_path, worktree);
6776 if (err)
6777 goto done;
6779 rfa.worktree = worktree;
6780 rfa.fileindex = fileindex;
6781 rfa.progress_cb = progress_cb;
6782 rfa.progress_arg = progress_arg;
6783 rfa.patch_cb = NULL;
6784 rfa.patch_arg = NULL;
6785 rfa.repo = repo;
6786 err = worktree_status(worktree, "", fileindex, repo,
6787 revert_file, &rfa, NULL, NULL, 0, 0);
6788 if (err)
6789 goto sync;
6791 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6792 repo, progress_cb, progress_arg, NULL, NULL);
6793 sync:
6794 sync_err = sync_fileindex(fileindex, fileindex_path);
6795 if (sync_err && err == NULL)
6796 err = sync_err;
6797 done:
6798 got_ref_close(resolved);
6799 free(tree_id);
6800 free(fileindex_path);
6802 unlockerr = lock_worktree(worktree, LOCK_SH);
6803 if (unlockerr && err == NULL)
6804 err = unlockerr;
6805 return err;
6808 const struct got_error *
6809 got_worktree_histedit_complete(struct got_worktree *worktree,
6810 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6811 struct got_reference *edited_branch, struct got_repository *repo)
6813 const struct got_error *err, *unlockerr;
6814 struct got_object_id *new_head_commit_id = NULL;
6815 struct got_reference *resolved = NULL;
6817 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6818 if (err)
6819 return err;
6821 err = got_ref_open(&resolved, repo,
6822 got_ref_get_symref_target(edited_branch), 0);
6823 if (err)
6824 goto done;
6826 err = got_ref_change_ref(resolved, new_head_commit_id);
6827 if (err)
6828 goto done;
6830 err = got_ref_write(resolved, repo);
6831 if (err)
6832 goto done;
6834 err = got_worktree_set_head_ref(worktree, resolved);
6835 if (err)
6836 goto done;
6838 err = delete_histedit_refs(worktree, repo);
6839 done:
6840 if (fileindex)
6841 got_fileindex_free(fileindex);
6842 free(new_head_commit_id);
6843 unlockerr = lock_worktree(worktree, LOCK_SH);
6844 if (unlockerr && err == NULL)
6845 err = unlockerr;
6846 return err;
6849 const struct got_error *
6850 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6851 struct got_object_id *commit_id, struct got_repository *repo)
6853 const struct got_error *err;
6854 char *commit_ref_name;
6856 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6857 if (err)
6858 return err;
6860 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6861 if (err)
6862 goto done;
6864 err = delete_ref(commit_ref_name, repo);
6865 done:
6866 free(commit_ref_name);
6867 return err;
6870 const struct got_error *
6871 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6872 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6873 struct got_worktree *worktree, const char *refname,
6874 struct got_repository *repo)
6876 const struct got_error *err = NULL;
6877 char *fileindex_path = NULL;
6878 struct check_rebase_ok_arg ok_arg;
6880 *fileindex = NULL;
6881 *branch_ref = NULL;
6882 *base_branch_ref = NULL;
6884 err = lock_worktree(worktree, LOCK_EX);
6885 if (err)
6886 return err;
6888 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6889 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6890 "cannot integrate a branch into itself; "
6891 "update -b or different branch name required");
6892 goto done;
6895 err = open_fileindex(fileindex, &fileindex_path, worktree);
6896 if (err)
6897 goto done;
6899 /* Preconditions are the same as for rebase. */
6900 ok_arg.worktree = worktree;
6901 ok_arg.repo = repo;
6902 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6903 &ok_arg);
6904 if (err)
6905 goto done;
6907 err = got_ref_open(branch_ref, repo, refname, 1);
6908 if (err)
6909 goto done;
6911 err = got_ref_open(base_branch_ref, repo,
6912 got_worktree_get_head_ref_name(worktree), 1);
6913 done:
6914 if (err) {
6915 if (*branch_ref) {
6916 got_ref_close(*branch_ref);
6917 *branch_ref = NULL;
6919 if (*base_branch_ref) {
6920 got_ref_close(*base_branch_ref);
6921 *base_branch_ref = NULL;
6923 if (*fileindex) {
6924 got_fileindex_free(*fileindex);
6925 *fileindex = NULL;
6927 lock_worktree(worktree, LOCK_SH);
6929 return err;
6932 const struct got_error *
6933 got_worktree_integrate_continue(struct got_worktree *worktree,
6934 struct got_fileindex *fileindex, struct got_repository *repo,
6935 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6936 got_worktree_checkout_cb progress_cb, void *progress_arg,
6937 got_cancel_cb cancel_cb, void *cancel_arg)
6939 const struct got_error *err = NULL, *sync_err, *unlockerr;
6940 char *fileindex_path = NULL;
6941 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6943 err = get_fileindex_path(&fileindex_path, worktree);
6944 if (err)
6945 goto done;
6947 err = got_ref_resolve(&commit_id, repo, branch_ref);
6948 if (err)
6949 goto done;
6951 err = got_object_id_by_path(&tree_id, repo, commit_id,
6952 worktree->path_prefix);
6953 if (err)
6954 goto done;
6956 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6957 if (err)
6958 goto done;
6960 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6961 progress_cb, progress_arg, cancel_cb, cancel_arg);
6962 if (err)
6963 goto sync;
6965 err = got_ref_change_ref(base_branch_ref, commit_id);
6966 if (err)
6967 goto sync;
6969 err = got_ref_write(base_branch_ref, repo);
6970 sync:
6971 sync_err = sync_fileindex(fileindex, fileindex_path);
6972 if (sync_err && err == NULL)
6973 err = sync_err;
6975 done:
6976 unlockerr = got_ref_unlock(branch_ref);
6977 if (unlockerr && err == NULL)
6978 err = unlockerr;
6979 got_ref_close(branch_ref);
6981 unlockerr = got_ref_unlock(base_branch_ref);
6982 if (unlockerr && err == NULL)
6983 err = unlockerr;
6984 got_ref_close(base_branch_ref);
6986 got_fileindex_free(fileindex);
6987 free(fileindex_path);
6988 free(tree_id);
6990 unlockerr = lock_worktree(worktree, LOCK_SH);
6991 if (unlockerr && err == NULL)
6992 err = unlockerr;
6993 return err;
6996 const struct got_error *
6997 got_worktree_integrate_abort(struct got_worktree *worktree,
6998 struct got_fileindex *fileindex, struct got_repository *repo,
6999 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7001 const struct got_error *err = NULL, *unlockerr = NULL;
7003 got_fileindex_free(fileindex);
7005 err = lock_worktree(worktree, LOCK_SH);
7007 unlockerr = got_ref_unlock(branch_ref);
7008 if (unlockerr && err == NULL)
7009 err = unlockerr;
7010 got_ref_close(branch_ref);
7012 unlockerr = got_ref_unlock(base_branch_ref);
7013 if (unlockerr && err == NULL)
7014 err = unlockerr;
7015 got_ref_close(base_branch_ref);
7017 return err;
7020 struct check_stage_ok_arg {
7021 struct got_object_id *head_commit_id;
7022 struct got_worktree *worktree;
7023 struct got_fileindex *fileindex;
7024 struct got_repository *repo;
7025 int have_changes;
7028 const struct got_error *
7029 check_stage_ok(void *arg, unsigned char status,
7030 unsigned char staged_status, const char *relpath,
7031 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7032 struct got_object_id *commit_id, int dirfd, const char *de_name)
7034 struct check_stage_ok_arg *a = arg;
7035 const struct got_error *err = NULL;
7036 struct got_fileindex_entry *ie;
7037 struct got_object_id base_commit_id;
7038 struct got_object_id *base_commit_idp = NULL;
7039 char *in_repo_path = NULL, *p;
7041 if (status == GOT_STATUS_UNVERSIONED ||
7042 status == GOT_STATUS_NO_CHANGE)
7043 return NULL;
7044 if (status == GOT_STATUS_NONEXISTENT)
7045 return got_error_set_errno(ENOENT, relpath);
7047 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7048 if (ie == NULL)
7049 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7051 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7052 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7053 relpath) == -1)
7054 return got_error_from_errno("asprintf");
7056 if (got_fileindex_entry_has_commit(ie)) {
7057 memcpy(base_commit_id.sha1, ie->commit_sha1,
7058 SHA1_DIGEST_LENGTH);
7059 base_commit_idp = &base_commit_id;
7062 if (status == GOT_STATUS_CONFLICT) {
7063 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7064 goto done;
7065 } else if (status != GOT_STATUS_ADD &&
7066 status != GOT_STATUS_MODIFY &&
7067 status != GOT_STATUS_DELETE) {
7068 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7069 goto done;
7072 a->have_changes = 1;
7074 p = in_repo_path;
7075 while (p[0] == '/')
7076 p++;
7077 err = check_out_of_date(p, status, staged_status,
7078 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7079 GOT_ERR_STAGE_OUT_OF_DATE);
7080 done:
7081 free(in_repo_path);
7082 return err;
7085 struct stage_path_arg {
7086 struct got_worktree *worktree;
7087 struct got_fileindex *fileindex;
7088 struct got_repository *repo;
7089 got_worktree_status_cb status_cb;
7090 void *status_arg;
7091 got_worktree_patch_cb patch_cb;
7092 void *patch_arg;
7093 int staged_something;
7094 int allow_bad_symlinks;
7097 static const struct got_error *
7098 stage_path(void *arg, unsigned char status,
7099 unsigned char staged_status, const char *relpath,
7100 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7101 struct got_object_id *commit_id, int dirfd, const char *de_name)
7103 struct stage_path_arg *a = arg;
7104 const struct got_error *err = NULL;
7105 struct got_fileindex_entry *ie;
7106 char *ondisk_path = NULL, *path_content = NULL;
7107 uint32_t stage;
7108 struct got_object_id *new_staged_blob_id = NULL;
7109 struct stat sb;
7111 if (status == GOT_STATUS_UNVERSIONED)
7112 return NULL;
7114 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7115 if (ie == NULL)
7116 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7118 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7119 relpath)== -1)
7120 return got_error_from_errno("asprintf");
7122 switch (status) {
7123 case GOT_STATUS_ADD:
7124 case GOT_STATUS_MODIFY:
7125 /* XXX could sb.st_mode be passed in by our caller? */
7126 if (lstat(ondisk_path, &sb) == -1) {
7127 err = got_error_from_errno2("lstat", ondisk_path);
7128 break;
7130 if (a->patch_cb) {
7131 if (status == GOT_STATUS_ADD) {
7132 int choice = GOT_PATCH_CHOICE_NONE;
7133 err = (*a->patch_cb)(&choice, a->patch_arg,
7134 status, ie->path, NULL, 1, 1);
7135 if (err)
7136 break;
7137 if (choice != GOT_PATCH_CHOICE_YES)
7138 break;
7139 } else {
7140 err = create_patched_content(&path_content, 0,
7141 staged_blob_id ? staged_blob_id : blob_id,
7142 ondisk_path, dirfd, de_name, ie->path,
7143 a->repo, a->patch_cb, a->patch_arg);
7144 if (err || path_content == NULL)
7145 break;
7148 err = got_object_blob_create(&new_staged_blob_id,
7149 path_content ? path_content : ondisk_path, a->repo);
7150 if (err)
7151 break;
7152 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7153 SHA1_DIGEST_LENGTH);
7154 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7155 stage = GOT_FILEIDX_STAGE_ADD;
7156 else
7157 stage = GOT_FILEIDX_STAGE_MODIFY;
7158 got_fileindex_entry_stage_set(ie, stage);
7159 if (S_ISLNK(sb.st_mode)) {
7160 int is_bad_symlink = 0;
7161 if (!a->allow_bad_symlinks) {
7162 char target_path[PATH_MAX];
7163 ssize_t target_len;
7164 target_len = readlink(ondisk_path, target_path,
7165 sizeof(target_path));
7166 if (target_len == -1) {
7167 err = got_error_from_errno2("readlink",
7168 ondisk_path);
7169 break;
7171 err = is_bad_symlink_target(&is_bad_symlink,
7172 target_path, target_len, ondisk_path,
7173 a->worktree->root_path);
7174 if (err)
7175 break;
7176 if (is_bad_symlink) {
7177 err = got_error_path(ondisk_path,
7178 GOT_ERR_BAD_SYMLINK);
7179 break;
7182 if (is_bad_symlink)
7183 got_fileindex_entry_staged_filetype_set(ie,
7184 GOT_FILEIDX_MODE_BAD_SYMLINK);
7185 else
7186 got_fileindex_entry_staged_filetype_set(ie,
7187 GOT_FILEIDX_MODE_SYMLINK);
7188 } else {
7189 got_fileindex_entry_staged_filetype_set(ie,
7190 GOT_FILEIDX_MODE_REGULAR_FILE);
7192 a->staged_something = 1;
7193 if (a->status_cb == NULL)
7194 break;
7195 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7196 get_staged_status(ie), relpath, blob_id,
7197 new_staged_blob_id, NULL, dirfd, de_name);
7198 break;
7199 case GOT_STATUS_DELETE:
7200 if (staged_status == GOT_STATUS_DELETE)
7201 break;
7202 if (a->patch_cb) {
7203 int choice = GOT_PATCH_CHOICE_NONE;
7204 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7205 ie->path, NULL, 1, 1);
7206 if (err)
7207 break;
7208 if (choice == GOT_PATCH_CHOICE_NO)
7209 break;
7210 if (choice != GOT_PATCH_CHOICE_YES) {
7211 err = got_error(GOT_ERR_PATCH_CHOICE);
7212 break;
7215 stage = GOT_FILEIDX_STAGE_DELETE;
7216 got_fileindex_entry_stage_set(ie, stage);
7217 a->staged_something = 1;
7218 if (a->status_cb == NULL)
7219 break;
7220 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7221 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7222 de_name);
7223 break;
7224 case GOT_STATUS_NO_CHANGE:
7225 break;
7226 case GOT_STATUS_CONFLICT:
7227 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7228 break;
7229 case GOT_STATUS_NONEXISTENT:
7230 err = got_error_set_errno(ENOENT, relpath);
7231 break;
7232 default:
7233 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7234 break;
7237 if (path_content && unlink(path_content) == -1 && err == NULL)
7238 err = got_error_from_errno2("unlink", path_content);
7239 free(path_content);
7240 free(ondisk_path);
7241 free(new_staged_blob_id);
7242 return err;
7245 const struct got_error *
7246 got_worktree_stage(struct got_worktree *worktree,
7247 struct got_pathlist_head *paths,
7248 got_worktree_status_cb status_cb, void *status_arg,
7249 got_worktree_patch_cb patch_cb, void *patch_arg,
7250 int allow_bad_symlinks, struct got_repository *repo)
7252 const struct got_error *err = NULL, *sync_err, *unlockerr;
7253 struct got_pathlist_entry *pe;
7254 struct got_fileindex *fileindex = NULL;
7255 char *fileindex_path = NULL;
7256 struct got_reference *head_ref = NULL;
7257 struct got_object_id *head_commit_id = NULL;
7258 struct check_stage_ok_arg oka;
7259 struct stage_path_arg spa;
7261 err = lock_worktree(worktree, LOCK_EX);
7262 if (err)
7263 return err;
7265 err = got_ref_open(&head_ref, repo,
7266 got_worktree_get_head_ref_name(worktree), 0);
7267 if (err)
7268 goto done;
7269 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7270 if (err)
7271 goto done;
7272 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7273 if (err)
7274 goto done;
7276 /* Check pre-conditions before staging anything. */
7277 oka.head_commit_id = head_commit_id;
7278 oka.worktree = worktree;
7279 oka.fileindex = fileindex;
7280 oka.repo = repo;
7281 oka.have_changes = 0;
7282 TAILQ_FOREACH(pe, paths, entry) {
7283 err = worktree_status(worktree, pe->path, fileindex, repo,
7284 check_stage_ok, &oka, NULL, NULL, 0, 0);
7285 if (err)
7286 goto done;
7288 if (!oka.have_changes) {
7289 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7290 goto done;
7293 spa.worktree = worktree;
7294 spa.fileindex = fileindex;
7295 spa.repo = repo;
7296 spa.patch_cb = patch_cb;
7297 spa.patch_arg = patch_arg;
7298 spa.status_cb = status_cb;
7299 spa.status_arg = status_arg;
7300 spa.staged_something = 0;
7301 spa.allow_bad_symlinks = allow_bad_symlinks;
7302 TAILQ_FOREACH(pe, paths, entry) {
7303 err = worktree_status(worktree, pe->path, fileindex, repo,
7304 stage_path, &spa, NULL, NULL, 0, 0);
7305 if (err)
7306 goto done;
7308 if (!spa.staged_something) {
7309 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7310 goto done;
7313 sync_err = sync_fileindex(fileindex, fileindex_path);
7314 if (sync_err && err == NULL)
7315 err = sync_err;
7316 done:
7317 if (head_ref)
7318 got_ref_close(head_ref);
7319 free(head_commit_id);
7320 free(fileindex_path);
7321 if (fileindex)
7322 got_fileindex_free(fileindex);
7323 unlockerr = lock_worktree(worktree, LOCK_SH);
7324 if (unlockerr && err == NULL)
7325 err = unlockerr;
7326 return err;
7329 struct unstage_path_arg {
7330 struct got_worktree *worktree;
7331 struct got_fileindex *fileindex;
7332 struct got_repository *repo;
7333 got_worktree_checkout_cb progress_cb;
7334 void *progress_arg;
7335 got_worktree_patch_cb patch_cb;
7336 void *patch_arg;
7339 static const struct got_error *
7340 create_unstaged_content(char **path_unstaged_content,
7341 char **path_new_staged_content, struct got_object_id *blob_id,
7342 struct got_object_id *staged_blob_id, const char *relpath,
7343 struct got_repository *repo,
7344 got_worktree_patch_cb patch_cb, void *patch_arg)
7346 const struct got_error *err;
7347 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7348 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7349 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7350 struct stat sb1, sb2;
7351 struct got_diff_changes *changes = NULL;
7352 struct got_diff_state *ds = NULL;
7353 struct got_diff_args *args = NULL;
7354 struct got_diff_change *change;
7355 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7356 int have_content = 0, have_rejected_content = 0;
7358 *path_unstaged_content = NULL;
7359 *path_new_staged_content = NULL;
7361 err = got_object_id_str(&label1, blob_id);
7362 if (err)
7363 return err;
7364 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7365 if (err)
7366 goto done;
7368 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7369 if (err)
7370 goto done;
7372 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7373 if (err)
7374 goto done;
7376 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7377 if (err)
7378 goto done;
7380 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7381 if (err)
7382 goto done;
7384 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7385 if (err)
7386 goto done;
7388 if (stat(path1, &sb1) == -1) {
7389 err = got_error_from_errno2("stat", path1);
7390 goto done;
7393 if (stat(path2, &sb2) == -1) {
7394 err = got_error_from_errno2("stat", path2);
7395 goto done;
7398 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7399 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7400 if (err)
7401 goto done;
7403 err = got_opentemp_named(path_unstaged_content, &outfile,
7404 "got-unstaged-content");
7405 if (err)
7406 goto done;
7407 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7408 "got-new-staged-content");
7409 if (err)
7410 goto done;
7412 if (fseek(f1, 0L, SEEK_SET) == -1) {
7413 err = got_ferror(f1, GOT_ERR_IO);
7414 goto done;
7416 if (fseek(f2, 0L, SEEK_SET) == -1) {
7417 err = got_ferror(f2, GOT_ERR_IO);
7418 goto done;
7420 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7421 int choice;
7422 err = apply_or_reject_change(&choice, change, ++n,
7423 changes->nchanges, ds, args, diff_flags, relpath,
7424 f1, f2, &line_cur1, &line_cur2,
7425 outfile, rejectfile, patch_cb, patch_arg);
7426 if (err)
7427 goto done;
7428 if (choice == GOT_PATCH_CHOICE_YES)
7429 have_content = 1;
7430 else
7431 have_rejected_content = 1;
7432 if (choice == GOT_PATCH_CHOICE_QUIT)
7433 break;
7435 if (have_content || have_rejected_content)
7436 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7437 outfile, rejectfile);
7438 done:
7439 free(label1);
7440 if (blob)
7441 got_object_blob_close(blob);
7442 if (staged_blob)
7443 got_object_blob_close(staged_blob);
7444 if (f1 && fclose(f1) == EOF && err == NULL)
7445 err = got_error_from_errno2("fclose", path1);
7446 if (f2 && fclose(f2) == EOF && err == NULL)
7447 err = got_error_from_errno2("fclose", path2);
7448 if (outfile && fclose(outfile) == EOF && err == NULL)
7449 err = got_error_from_errno2("fclose", *path_unstaged_content);
7450 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7451 err = got_error_from_errno2("fclose", *path_new_staged_content);
7452 if (path1 && unlink(path1) == -1 && err == NULL)
7453 err = got_error_from_errno2("unlink", path1);
7454 if (path2 && unlink(path2) == -1 && err == NULL)
7455 err = got_error_from_errno2("unlink", path2);
7456 if (err || !have_content) {
7457 if (*path_unstaged_content &&
7458 unlink(*path_unstaged_content) == -1 && err == NULL)
7459 err = got_error_from_errno2("unlink",
7460 *path_unstaged_content);
7461 free(*path_unstaged_content);
7462 *path_unstaged_content = NULL;
7464 if (err || !have_content || !have_rejected_content) {
7465 if (*path_new_staged_content &&
7466 unlink(*path_new_staged_content) == -1 && err == NULL)
7467 err = got_error_from_errno2("unlink",
7468 *path_new_staged_content);
7469 free(*path_new_staged_content);
7470 *path_new_staged_content = NULL;
7472 free(args);
7473 if (ds) {
7474 got_diff_state_free(ds);
7475 free(ds);
7477 if (changes)
7478 got_diff_free_changes(changes);
7479 free(path1);
7480 free(path2);
7481 return err;
7484 static const struct got_error *
7485 unstage_hunks(struct got_object_id *staged_blob_id,
7486 struct got_blob_object *blob_base,
7487 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7488 const char *ondisk_path, const char *label_orig,
7489 struct got_worktree *worktree, struct got_repository *repo,
7490 got_worktree_patch_cb patch_cb, void *patch_arg,
7491 got_worktree_checkout_cb progress_cb, void *progress_arg)
7493 const struct got_error *err = NULL;
7494 char *path_unstaged_content = NULL;
7495 char *path_new_staged_content = NULL;
7496 struct got_object_id *new_staged_blob_id = NULL;
7497 FILE *f = NULL;
7498 struct stat sb;
7500 err = create_unstaged_content(&path_unstaged_content,
7501 &path_new_staged_content, blob_id, staged_blob_id,
7502 ie->path, repo, patch_cb, patch_arg);
7503 if (err)
7504 return err;
7506 if (path_unstaged_content == NULL)
7507 return NULL;
7509 if (path_new_staged_content) {
7510 err = got_object_blob_create(&new_staged_blob_id,
7511 path_new_staged_content, repo);
7512 if (err)
7513 goto done;
7516 f = fopen(path_unstaged_content, "r");
7517 if (f == NULL) {
7518 err = got_error_from_errno2("fopen",
7519 path_unstaged_content);
7520 goto done;
7522 if (fstat(fileno(f), &sb) == -1) {
7523 err = got_error_from_errno2("fstat", path_unstaged_content);
7524 goto done;
7526 if (got_fileindex_entry_staged_filetype_get(ie) ==
7527 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7528 char link_target[PATH_MAX];
7529 size_t r;
7530 r = fread(link_target, 1, sizeof(link_target), f);
7531 if (r == 0 && ferror(f)) {
7532 err = got_error_from_errno("fread");
7533 goto done;
7535 if (r >= sizeof(link_target)) { /* should not happen */
7536 err = got_error(GOT_ERR_NO_SPACE);
7537 goto done;
7539 link_target[r] = '\0';
7540 err = merge_symlink(worktree, blob_base,
7541 ondisk_path, ie->path, label_orig, link_target,
7542 worktree->base_commit_id, repo, progress_cb,
7543 progress_arg);
7544 } else {
7545 int local_changes_subsumed;
7546 err = merge_file(&local_changes_subsumed, worktree,
7547 blob_base, ondisk_path, ie->path,
7548 got_fileindex_perms_to_st(ie),
7549 path_unstaged_content, label_orig, "unstaged",
7550 repo, progress_cb, progress_arg);
7552 if (err)
7553 goto done;
7555 if (new_staged_blob_id) {
7556 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7557 SHA1_DIGEST_LENGTH);
7558 } else
7559 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7560 done:
7561 free(new_staged_blob_id);
7562 if (path_unstaged_content &&
7563 unlink(path_unstaged_content) == -1 && err == NULL)
7564 err = got_error_from_errno2("unlink", path_unstaged_content);
7565 if (path_new_staged_content &&
7566 unlink(path_new_staged_content) == -1 && err == NULL)
7567 err = got_error_from_errno2("unlink", path_new_staged_content);
7568 if (f && fclose(f) != 0 && err == NULL)
7569 err = got_error_from_errno2("fclose", path_unstaged_content);
7570 free(path_unstaged_content);
7571 free(path_new_staged_content);
7572 return err;
7575 static const struct got_error *
7576 unstage_path(void *arg, unsigned char status,
7577 unsigned char staged_status, const char *relpath,
7578 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7579 struct got_object_id *commit_id, int dirfd, const char *de_name)
7581 const struct got_error *err = NULL;
7582 struct unstage_path_arg *a = arg;
7583 struct got_fileindex_entry *ie;
7584 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7585 char *ondisk_path = NULL;
7586 char *id_str = NULL, *label_orig = NULL;
7587 int local_changes_subsumed;
7588 struct stat sb;
7590 if (staged_status != GOT_STATUS_ADD &&
7591 staged_status != GOT_STATUS_MODIFY &&
7592 staged_status != GOT_STATUS_DELETE)
7593 return NULL;
7595 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7596 if (ie == NULL)
7597 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7599 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7600 == -1)
7601 return got_error_from_errno("asprintf");
7603 err = got_object_id_str(&id_str,
7604 commit_id ? commit_id : a->worktree->base_commit_id);
7605 if (err)
7606 goto done;
7607 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7608 id_str) == -1) {
7609 err = got_error_from_errno("asprintf");
7610 goto done;
7613 switch (staged_status) {
7614 case GOT_STATUS_MODIFY:
7615 err = got_object_open_as_blob(&blob_base, a->repo,
7616 blob_id, 8192);
7617 if (err)
7618 break;
7619 /* fall through */
7620 case GOT_STATUS_ADD:
7621 if (a->patch_cb) {
7622 if (staged_status == GOT_STATUS_ADD) {
7623 int choice = GOT_PATCH_CHOICE_NONE;
7624 err = (*a->patch_cb)(&choice, a->patch_arg,
7625 staged_status, ie->path, NULL, 1, 1);
7626 if (err)
7627 break;
7628 if (choice != GOT_PATCH_CHOICE_YES)
7629 break;
7630 } else {
7631 err = unstage_hunks(staged_blob_id,
7632 blob_base, blob_id, ie, ondisk_path,
7633 label_orig, a->worktree, a->repo,
7634 a->patch_cb, a->patch_arg,
7635 a->progress_cb, a->progress_arg);
7636 break; /* Done with this file. */
7639 err = got_object_open_as_blob(&blob_staged, a->repo,
7640 staged_blob_id, 8192);
7641 if (err)
7642 break;
7643 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7644 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7645 case GOT_FILEIDX_MODE_REGULAR_FILE:
7646 err = merge_blob(&local_changes_subsumed, a->worktree,
7647 blob_base, ondisk_path, relpath,
7648 got_fileindex_perms_to_st(ie), label_orig,
7649 blob_staged, commit_id ? commit_id :
7650 a->worktree->base_commit_id, a->repo,
7651 a->progress_cb, a->progress_arg);
7652 break;
7653 case GOT_FILEIDX_MODE_SYMLINK:
7654 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7655 char *staged_target;
7656 err = got_object_blob_read_to_str(
7657 &staged_target, blob_staged);
7658 if (err)
7659 goto done;
7660 err = merge_symlink(a->worktree, blob_base,
7661 ondisk_path, relpath, label_orig,
7662 staged_target, commit_id ? commit_id :
7663 a->worktree->base_commit_id,
7664 a->repo, a->progress_cb, a->progress_arg);
7665 free(staged_target);
7666 } else {
7667 err = merge_blob(&local_changes_subsumed,
7668 a->worktree, blob_base, ondisk_path,
7669 relpath, got_fileindex_perms_to_st(ie),
7670 label_orig, blob_staged,
7671 commit_id ? commit_id :
7672 a->worktree->base_commit_id, a->repo,
7673 a->progress_cb, a->progress_arg);
7675 break;
7676 default:
7677 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7678 break;
7680 if (err == NULL)
7681 got_fileindex_entry_stage_set(ie,
7682 GOT_FILEIDX_STAGE_NONE);
7683 break;
7684 case GOT_STATUS_DELETE:
7685 if (a->patch_cb) {
7686 int choice = GOT_PATCH_CHOICE_NONE;
7687 err = (*a->patch_cb)(&choice, a->patch_arg,
7688 staged_status, ie->path, NULL, 1, 1);
7689 if (err)
7690 break;
7691 if (choice == GOT_PATCH_CHOICE_NO)
7692 break;
7693 if (choice != GOT_PATCH_CHOICE_YES) {
7694 err = got_error(GOT_ERR_PATCH_CHOICE);
7695 break;
7698 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7699 err = get_file_status(&status, &sb, ie, ondisk_path,
7700 dirfd, de_name, a->repo);
7701 if (err)
7702 break;
7703 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7704 break;
7706 done:
7707 free(ondisk_path);
7708 if (blob_base)
7709 got_object_blob_close(blob_base);
7710 if (blob_staged)
7711 got_object_blob_close(blob_staged);
7712 free(id_str);
7713 free(label_orig);
7714 return err;
7717 const struct got_error *
7718 got_worktree_unstage(struct got_worktree *worktree,
7719 struct got_pathlist_head *paths,
7720 got_worktree_checkout_cb progress_cb, void *progress_arg,
7721 got_worktree_patch_cb patch_cb, void *patch_arg,
7722 struct got_repository *repo)
7724 const struct got_error *err = NULL, *sync_err, *unlockerr;
7725 struct got_pathlist_entry *pe;
7726 struct got_fileindex *fileindex = NULL;
7727 char *fileindex_path = NULL;
7728 struct unstage_path_arg upa;
7730 err = lock_worktree(worktree, LOCK_EX);
7731 if (err)
7732 return err;
7734 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7735 if (err)
7736 goto done;
7738 upa.worktree = worktree;
7739 upa.fileindex = fileindex;
7740 upa.repo = repo;
7741 upa.progress_cb = progress_cb;
7742 upa.progress_arg = progress_arg;
7743 upa.patch_cb = patch_cb;
7744 upa.patch_arg = patch_arg;
7745 TAILQ_FOREACH(pe, paths, entry) {
7746 err = worktree_status(worktree, pe->path, fileindex, repo,
7747 unstage_path, &upa, NULL, NULL, 0, 0);
7748 if (err)
7749 goto done;
7752 sync_err = sync_fileindex(fileindex, fileindex_path);
7753 if (sync_err && err == NULL)
7754 err = sync_err;
7755 done:
7756 free(fileindex_path);
7757 if (fileindex)
7758 got_fileindex_free(fileindex);
7759 unlockerr = lock_worktree(worktree, LOCK_SH);
7760 if (unlockerr && err == NULL)
7761 err = unlockerr;
7762 return err;
7765 struct report_file_info_arg {
7766 struct got_worktree *worktree;
7767 got_worktree_path_info_cb info_cb;
7768 void *info_arg;
7769 struct got_pathlist_head *paths;
7770 got_cancel_cb cancel_cb;
7771 void *cancel_arg;
7774 static const struct got_error *
7775 report_file_info(void *arg, struct got_fileindex_entry *ie)
7777 struct report_file_info_arg *a = arg;
7778 struct got_pathlist_entry *pe;
7779 struct got_object_id blob_id, staged_blob_id, commit_id;
7780 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7781 struct got_object_id *commit_idp = NULL;
7782 int stage;
7784 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7785 return got_error(GOT_ERR_CANCELLED);
7787 TAILQ_FOREACH(pe, a->paths, entry) {
7788 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7789 got_path_is_child(ie->path, pe->path, pe->path_len))
7790 break;
7792 if (pe == NULL) /* not found */
7793 return NULL;
7795 if (got_fileindex_entry_has_blob(ie)) {
7796 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7797 blob_idp = &blob_id;
7799 stage = got_fileindex_entry_stage_get(ie);
7800 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7801 stage == GOT_FILEIDX_STAGE_ADD) {
7802 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7803 SHA1_DIGEST_LENGTH);
7804 staged_blob_idp = &staged_blob_id;
7807 if (got_fileindex_entry_has_commit(ie)) {
7808 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7809 commit_idp = &commit_id;
7812 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7813 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7816 const struct got_error *
7817 got_worktree_path_info(struct got_worktree *worktree,
7818 struct got_pathlist_head *paths,
7819 got_worktree_path_info_cb info_cb, void *info_arg,
7820 got_cancel_cb cancel_cb, void *cancel_arg)
7823 const struct got_error *err = NULL, *unlockerr;
7824 struct got_fileindex *fileindex = NULL;
7825 char *fileindex_path = NULL;
7826 struct report_file_info_arg arg;
7828 err = lock_worktree(worktree, LOCK_SH);
7829 if (err)
7830 return err;
7832 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7833 if (err)
7834 goto done;
7836 arg.worktree = worktree;
7837 arg.info_cb = info_cb;
7838 arg.info_arg = info_arg;
7839 arg.paths = paths;
7840 arg.cancel_cb = cancel_cb;
7841 arg.cancel_arg = cancel_arg;
7842 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7843 &arg);
7844 done:
7845 free(fileindex_path);
7846 if (fileindex)
7847 got_fileindex_free(fileindex);
7848 unlockerr = lock_worktree(worktree, LOCK_UN);
7849 if (unlockerr && err == NULL)
7850 err = unlockerr;
7851 return err;