Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
432 done:
433 if (repo)
434 got_repo_close(repo);
435 free(path_got);
436 free(path_lock);
437 free(base_commit_id_str);
438 free(uuidstr);
439 free(formatstr);
440 if (err) {
441 if (fd != -1)
442 close(fd);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
445 *worktree = NULL;
446 } else
447 (*worktree)->lockfd = fd;
449 return err;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
456 char *worktree_path;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
462 for (;;) {
463 char *parent_path;
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 free(worktree_path);
468 return err;
470 if (*worktree) {
471 free(worktree_path);
472 return NULL;
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
475 break;
476 err = got_path_dirname(&parent_path, worktree_path);
477 if (err) {
478 if (err->code != GOT_ERR_BAD_PATH) {
479 free(worktree_path);
480 return err;
482 break;
484 free(worktree_path);
485 worktree_path = parent_path;
488 free(worktree_path);
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
507 free(worktree);
508 return err;
511 const char *
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
517 const char *
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
522 const char *
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
540 free(absprefix);
541 return NULL;
544 const char *
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
567 goto done;
570 err = write_head_ref(path_got, head_ref);
571 if (err)
572 goto done;
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
576 done:
577 free(path_got);
578 if (err)
579 free(head_ref_name);
580 return err;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
595 char *id_str = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
601 path_got = NULL;
602 goto done;
605 err = got_object_open(&obj, repo, commit_id);
606 if (err)
607 return err;
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
616 if (err)
617 goto done;
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
619 if (err)
620 goto done;
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
626 goto done;
628 done:
629 if (obj)
630 got_object_close(obj);
631 free(id_str);
632 free(path_got);
633 return err;
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
649 return NULL;
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
656 char *abspath;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 struct stat sb;
664 err = NULL;
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
672 free(abspath);
673 return err;
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
680 uint8_t fbuf1[8192];
681 uint8_t fbuf2[8192];
682 size_t flen1 = 0, flen2 = 0;
684 *same = 1;
686 for (;;) {
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
690 break;
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
695 break;
697 if (flen1 == 0) {
698 if (flen2 != 0)
699 *same = 0;
700 break;
701 } else if (flen2 == 0) {
702 if (flen1 != 0)
703 *same = 0;
704 break;
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
707 *same = 0;
708 break;
710 } else {
711 *same = 0;
712 break;
716 return err;
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
723 struct stat sb;
724 size_t size1, size2;
725 FILE *f1 = NULL, *f2 = NULL;
727 *same = 1;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
731 goto done;
733 size1 = sb.st_size;
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
737 goto done;
739 size2 = sb.st_size;
741 if (size1 != size2) {
742 *same = 0;
743 return NULL;
746 f1 = fopen(f1_path, "r");
747 if (f1 == NULL)
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
751 if (f2 == NULL) {
752 err = got_error_from_errno2("fopen", f2_path);
753 goto done;
756 err = check_file_contents_equal(same, f1, f2);
757 done:
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
763 return err;
766 /*
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
770 */
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
780 int merged_fd = -1;
781 FILE *f_orig = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
784 int overlapcnt = 0;
785 char *parent = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
792 if (err)
793 return err;
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 goto done;
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
801 if (err)
802 goto done;
804 free(base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
807 base_path = NULL;
808 goto done;
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
812 if (err)
813 goto done;
814 if (blob_orig) {
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
816 blob_orig);
817 if (err)
818 goto done;
819 } else {
820 /*
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
824 */
827 /*
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
830 */
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
833 ssize_t target_len;
834 size_t n;
836 free(base_path);
837 if (asprintf(&base_path, "%s/got-symlink-merge",
838 parent) == -1) {
839 err = got_error_from_errno("asprintf");
840 base_path = NULL;
841 goto done;
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 if (err)
845 goto done;
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
850 goto done;
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
855 goto done;
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
859 goto done;
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
866 if (err)
867 goto done;
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
871 if (err)
872 goto done;
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
876 goto done;
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
882 merged_path);
883 if (err)
884 goto done;
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
889 goto done;
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
894 ondisk_path);
895 goto done;
897 done:
898 if (err) {
899 if (merged_path)
900 unlink(merged_path);
902 if (symlink_path) {
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
908 free(symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
913 free(merged_path);
914 free(base_path);
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
919 free(parent);
920 return err;
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
925 size_t target_len)
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
932 ondisk_path);
933 return NULL;
936 /*
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
947 */
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
955 FILE *f = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
958 if (err)
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
964 goto done;
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 if (err)
969 goto done;
971 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
972 err = got_error_from_errno2("fchmod", path);
973 goto done;
976 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
977 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
978 deriv_target ? deriv_target : "(symlink was deleted)",
979 orig_target ? label_orig : "",
980 orig_target ? "\n" : "",
981 orig_target ? orig_target : "",
982 orig_target ? "\n" : "",
983 GOT_DIFF_CONFLICT_MARKER_SEP,
984 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
985 err = got_error_from_errno2("fprintf", path);
986 goto done;
989 if (unlink(ondisk_path) == -1) {
990 err = got_error_from_errno2("unlink", ondisk_path);
991 goto done;
993 if (rename(path, ondisk_path) == -1) {
994 err = got_error_from_errno3("rename", path, ondisk_path);
995 goto done;
997 done:
998 if (f != NULL && fclose(f) == EOF && err == NULL)
999 err = got_error_from_errno2("fclose", path);
1000 free(path);
1001 free(id_str);
1002 free(label_deriv);
1003 return err;
1006 /* forward declaration */
1007 static const struct got_error *
1008 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1009 const char *, const char *, uint16_t, const char *,
1010 struct got_blob_object *, struct got_object_id *,
1011 struct got_repository *, got_worktree_checkout_cb, void *);
1014 * Merge a symlink into the work tree, where blob_orig acts as the common
1015 * ancestor, deriv_target is the link target of the first derived version,
1016 * and the symlink on disk acts as the second derived version.
1017 * Assume that contents of both blobs represent symlinks.
1019 static const struct got_error *
1020 merge_symlink(struct got_worktree *worktree,
1021 struct got_blob_object *blob_orig, const char *ondisk_path,
1022 const char *path, const char *label_orig, const char *deriv_target,
1023 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1024 got_worktree_checkout_cb progress_cb, void *progress_arg)
1026 const struct got_error *err = NULL;
1027 char *ancestor_target = NULL;
1028 struct stat sb;
1029 ssize_t ondisk_len, deriv_len;
1030 char ondisk_target[PATH_MAX];
1031 int have_local_change = 0;
1032 int have_incoming_change = 0;
1034 if (lstat(ondisk_path, &sb) == -1)
1035 return got_error_from_errno2("lstat", ondisk_path);
1037 ondisk_len = readlink(ondisk_path, ondisk_target,
1038 sizeof(ondisk_target));
1039 if (ondisk_len == -1) {
1040 err = got_error_from_errno2("readlink",
1041 ondisk_path);
1042 goto done;
1044 ondisk_target[ondisk_len] = '\0';
1046 if (blob_orig) {
1047 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1048 if (err)
1049 goto done;
1052 if (ancestor_target == NULL ||
1053 (ondisk_len != strlen(ancestor_target) ||
1054 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1055 have_local_change = 1;
1057 deriv_len = strlen(deriv_target);
1058 if (ancestor_target == NULL ||
1059 (deriv_len != strlen(ancestor_target) ||
1060 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1061 have_incoming_change = 1;
1063 if (!have_local_change && !have_incoming_change) {
1064 if (ancestor_target) {
1065 /* Both sides made the same change. */
1066 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1067 path);
1068 } else if (deriv_len == ondisk_len &&
1069 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1070 /* Both sides added the same symlink. */
1071 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1072 path);
1073 } else {
1074 /* Both sides added symlinks which don't match. */
1075 err = install_symlink_conflict(deriv_target,
1076 deriv_base_commit_id, ancestor_target,
1077 label_orig, ondisk_target, ondisk_path);
1078 if (err)
1079 goto done;
1080 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1081 path);
1083 } else if (!have_local_change && have_incoming_change) {
1084 /* Apply the incoming change. */
1085 err = update_symlink(ondisk_path, deriv_target,
1086 strlen(deriv_target));
1087 if (err)
1088 goto done;
1089 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1090 } else if (have_local_change && have_incoming_change) {
1091 if (deriv_len == ondisk_len &&
1092 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1093 /* Both sides made the same change. */
1094 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1095 path);
1096 } else {
1097 err = install_symlink_conflict(deriv_target,
1098 deriv_base_commit_id, ancestor_target, label_orig,
1099 ondisk_target, ondisk_path);
1100 if (err)
1101 goto done;
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1103 path);
1107 done:
1108 free(ancestor_target);
1109 return err;
1113 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1114 * blob_deriv acts as the first derived version, and the file on disk
1115 * acts as the second derived version.
1117 static const struct got_error *
1118 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1119 struct got_blob_object *blob_orig, const char *ondisk_path,
1120 const char *path, uint16_t st_mode, const char *label_orig,
1121 struct got_blob_object *blob_deriv,
1122 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1123 got_worktree_checkout_cb progress_cb, void *progress_arg)
1125 const struct got_error *err = NULL;
1126 FILE *f_deriv = NULL;
1127 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1128 char *label_deriv = NULL, *parent = NULL;
1130 *local_changes_subsumed = 0;
1132 err = got_path_dirname(&parent, ondisk_path);
1133 if (err)
1134 return err;
1136 free(base_path);
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 base_path = NULL;
1140 goto done;
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 if (err)
1145 goto done;
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 blob_deriv);
1148 if (err)
1149 goto done;
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (err)
1153 goto done;
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1157 goto done;
1160 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1161 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1162 label_deriv, repo, progress_cb, progress_arg);
1163 done:
1164 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1165 err = got_error_from_errno("fclose");
1166 free(base_path);
1167 if (blob_deriv_path) {
1168 unlink(blob_deriv_path);
1169 free(blob_deriv_path);
1171 free(id_str);
1172 free(label_deriv);
1173 free(parent);
1174 return err;
1177 static const struct got_error *
1178 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1179 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1180 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1182 const struct got_error *err = NULL;
1183 struct got_fileindex_entry *new_ie;
1185 *new_iep = NULL;
1187 err = got_fileindex_entry_alloc(&new_ie, path);
1188 if (err)
1189 return err;
1191 err = got_fileindex_entry_update(new_ie, ondisk_path,
1192 blob_id->sha1, base_commit_id->sha1, 1);
1193 if (err)
1194 goto done;
1196 err = got_fileindex_entry_add(fileindex, new_ie);
1197 done:
1198 if (err)
1199 got_fileindex_entry_free(new_ie);
1200 else
1201 *new_iep = new_ie;
1202 return err;
1205 static mode_t
1206 get_ondisk_perms(int executable, mode_t st_mode)
1208 mode_t xbits = S_IXUSR;
1210 if (executable) {
1211 /* Map read bits to execute bits. */
1212 if (st_mode & S_IRGRP)
1213 xbits |= S_IXGRP;
1214 if (st_mode & S_IROTH)
1215 xbits |= S_IXOTH;
1216 return st_mode | xbits;
1219 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1222 /* forward declaration */
1223 static const struct got_error *
1224 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1225 const char *path, mode_t te_mode, mode_t st_mode,
1226 struct got_blob_object *blob, int restoring_missing_file,
1227 int reverting_versioned_file, int installing_bad_symlink,
1228 int path_is_unversioned, struct got_repository *repo,
1229 got_worktree_checkout_cb progress_cb, void *progress_arg);
1232 * This function assumes that the provided symlink target points at a
1233 * safe location in the work tree!
1235 static const struct got_error *
1236 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1237 size_t target_len)
1239 const struct got_error *err = NULL;
1240 ssize_t elen;
1241 char etarget[PATH_MAX];
1242 int fd;
1245 * "Bad" symlinks (those pointing outside the work tree or into the
1246 * .got directory) are installed in the work tree as a regular file
1247 * which contains the bad symlink target path.
1248 * The new symlink target has already been checked for safety by our
1249 * caller. If we can successfully open a regular file then we simply
1250 * replace this file with a symlink below.
1252 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1253 if (fd == -1) {
1254 if (errno != ELOOP)
1255 return got_error_from_errno2("open", ondisk_path);
1257 /* We are updating an existing on-disk symlink. */
1258 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 if (elen == -1)
1260 return got_error_from_errno2("readlink", ondisk_path);
1262 if (elen == target_len &&
1263 memcmp(etarget, target_path, target_len) == 0)
1264 return NULL; /* nothing to do */
1267 err = update_symlink(ondisk_path, target_path, target_len);
1268 if (fd != -1 && close(fd) == -1 && err == NULL)
1269 err = got_error_from_errno2("close", ondisk_path);
1270 return err;
1273 static const struct got_error *
1274 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1275 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1277 const struct got_error *err = NULL;
1278 char canonpath[PATH_MAX];
1279 char *path_got = NULL;
1281 *is_bad_symlink = 0;
1283 if (target_len >= sizeof(canonpath)) {
1284 *is_bad_symlink = 1;
1285 return NULL;
1289 * We do not use realpath(3) to resolve the symlink's target
1290 * path because we don't want to resolve symlinks recursively.
1291 * Instead we make the path absolute and then canonicalize it.
1292 * Relative symlink target lookup should begin at the directory
1293 * in which the blob object is being installed.
1295 if (!got_path_is_absolute(target_path)) {
1296 char *abspath, *parent;
1297 err = got_path_dirname(&parent, ondisk_path);
1298 if (err)
1299 return err;
1300 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1301 free(parent);
1302 return got_error_from_errno("asprintf");
1304 free(parent);
1305 if (strlen(abspath) >= sizeof(canonpath)) {
1306 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1307 free(abspath);
1308 return err;
1310 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1311 free(abspath);
1312 if (err)
1313 return err;
1314 } else {
1315 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1316 if (err)
1317 return err;
1320 /* Only allow symlinks pointing at paths within the work tree. */
1321 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1322 *is_bad_symlink = 1;
1323 return NULL;
1326 /* Do not allow symlinks pointing into the .got directory. */
1327 if (asprintf(&path_got, "%s/%s", wtroot_path,
1328 GOT_WORKTREE_GOT_DIR) == -1)
1329 return got_error_from_errno("asprintf");
1330 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1331 *is_bad_symlink = 1;
1333 free(path_got);
1334 return NULL;
1337 static const struct got_error *
1338 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1339 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1340 int restoring_missing_file, int reverting_versioned_file,
1341 int path_is_unversioned, struct got_repository *repo,
1342 got_worktree_checkout_cb progress_cb, void *progress_arg)
1344 const struct got_error *err = NULL;
1345 char target_path[PATH_MAX];
1346 size_t len, target_len = 0;
1347 char *path_got = NULL;
1348 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1349 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1351 *is_bad_symlink = 0;
1354 * Blob object content specifies the target path of the link.
1355 * If a symbolic link cannot be installed we instead create
1356 * a regular file which contains the link target path stored
1357 * in the blob object.
1359 do {
1360 err = got_object_blob_read_block(&len, blob);
1361 if (len + target_len >= sizeof(target_path)) {
1362 /* Path too long; install as a regular file. */
1363 *is_bad_symlink = 1;
1364 got_object_blob_rewind(blob);
1365 return install_blob(worktree, ondisk_path, path,
1366 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1367 restoring_missing_file, reverting_versioned_file,
1368 1, path_is_unversioned, repo, progress_cb,
1369 progress_arg);
1371 if (len > 0) {
1372 /* Skip blob object header first time around. */
1373 memcpy(target_path + target_len, buf + hdrlen,
1374 len - hdrlen);
1375 target_len += len - hdrlen;
1376 hdrlen = 0;
1378 } while (len != 0);
1379 target_path[target_len] = '\0';
1381 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1382 ondisk_path, worktree->root_path);
1383 if (err)
1384 return err;
1386 if (*is_bad_symlink) {
1387 /* install as a regular file */
1388 *is_bad_symlink = 1;
1389 got_object_blob_rewind(blob);
1390 err = install_blob(worktree, ondisk_path, path,
1391 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1392 restoring_missing_file, reverting_versioned_file, 1,
1393 path_is_unversioned, repo, progress_cb, progress_arg);
1394 goto done;
1397 if (symlink(target_path, ondisk_path) == -1) {
1398 if (errno == EEXIST) {
1399 if (path_is_unversioned) {
1400 err = (*progress_cb)(progress_arg,
1401 GOT_STATUS_UNVERSIONED, path);
1402 goto done;
1404 err = replace_existing_symlink(ondisk_path,
1405 target_path, target_len);
1406 if (err)
1407 goto done;
1408 if (progress_cb) {
1409 err = (*progress_cb)(progress_arg,
1410 reverting_versioned_file ?
1411 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1412 path);
1414 goto done; /* Nothing else to do. */
1417 if (errno == ENOENT) {
1418 char *parent;
1419 err = got_path_dirname(&parent, ondisk_path);
1420 if (err)
1421 goto done;
1422 err = add_dir_on_disk(worktree, parent);
1423 free(parent);
1424 if (err)
1425 goto done;
1427 * Retry, and fall through to error handling
1428 * below if this second attempt fails.
1430 if (symlink(target_path, ondisk_path) != -1) {
1431 err = NULL; /* success */
1432 goto done;
1436 /* Handle errors from first or second creation attempt. */
1437 if (errno == ENAMETOOLONG) {
1438 /* bad target path; install as a regular file */
1439 *is_bad_symlink = 1;
1440 got_object_blob_rewind(blob);
1441 err = install_blob(worktree, ondisk_path, path,
1442 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1443 restoring_missing_file, reverting_versioned_file, 1,
1444 path_is_unversioned, repo,
1445 progress_cb, progress_arg);
1446 } else if (errno == ENOTDIR) {
1447 err = got_error_path(ondisk_path,
1448 GOT_ERR_FILE_OBSTRUCTED);
1449 } else {
1450 err = got_error_from_errno3("symlink",
1451 target_path, ondisk_path);
1453 } else if (progress_cb)
1454 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1455 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1456 done:
1457 free(path_got);
1458 return err;
1461 static const struct got_error *
1462 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1463 const char *path, mode_t te_mode, mode_t st_mode,
1464 struct got_blob_object *blob, int restoring_missing_file,
1465 int reverting_versioned_file, int installing_bad_symlink,
1466 int path_is_unversioned, struct got_repository *repo,
1467 got_worktree_checkout_cb progress_cb, void *progress_arg)
1469 const struct got_error *err = NULL;
1470 int fd = -1;
1471 size_t len, hdrlen;
1472 int update = 0;
1473 char *tmppath = NULL;
1475 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1476 GOT_DEFAULT_FILE_MODE);
1477 if (fd == -1) {
1478 if (errno == ENOENT) {
1479 char *parent;
1480 err = got_path_dirname(&parent, path);
1481 if (err)
1482 return err;
1483 err = add_dir_on_disk(worktree, parent);
1484 free(parent);
1485 if (err)
1486 return err;
1487 fd = open(ondisk_path,
1488 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1489 GOT_DEFAULT_FILE_MODE);
1490 if (fd == -1)
1491 return got_error_from_errno2("open",
1492 ondisk_path);
1493 } else if (errno == EEXIST) {
1494 if (path_is_unversioned) {
1495 err = (*progress_cb)(progress_arg,
1496 GOT_STATUS_UNVERSIONED, path);
1497 goto done;
1499 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1500 !S_ISREG(st_mode) && !installing_bad_symlink) {
1501 /* TODO file is obstructed; do something */
1502 err = got_error_path(ondisk_path,
1503 GOT_ERR_FILE_OBSTRUCTED);
1504 goto done;
1505 } else {
1506 err = got_opentemp_named_fd(&tmppath, &fd,
1507 ondisk_path);
1508 if (err)
1509 goto done;
1510 update = 1;
1512 } else
1513 return got_error_from_errno2("open", ondisk_path);
1516 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1517 err = got_error_from_errno2("fchmod",
1518 update ? tmppath : ondisk_path);
1519 goto done;
1522 if (progress_cb) {
1523 if (restoring_missing_file)
1524 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1525 path);
1526 else if (reverting_versioned_file)
1527 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1528 path);
1529 else
1530 err = (*progress_cb)(progress_arg,
1531 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1532 if (err)
1533 goto done;
1536 hdrlen = got_object_blob_get_hdrlen(blob);
1537 do {
1538 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1539 err = got_object_blob_read_block(&len, blob);
1540 if (err)
1541 break;
1542 if (len > 0) {
1543 /* Skip blob object header first time around. */
1544 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1545 if (outlen == -1) {
1546 err = got_error_from_errno("write");
1547 goto done;
1548 } else if (outlen != len - hdrlen) {
1549 err = got_error(GOT_ERR_IO);
1550 goto done;
1552 hdrlen = 0;
1554 } while (len != 0);
1556 if (fsync(fd) != 0) {
1557 err = got_error_from_errno("fsync");
1558 goto done;
1561 if (update) {
1562 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1563 err = got_error_from_errno2("unlink", ondisk_path);
1564 goto done;
1566 if (rename(tmppath, ondisk_path) != 0) {
1567 err = got_error_from_errno3("rename", tmppath,
1568 ondisk_path);
1569 goto done;
1571 free(tmppath);
1572 tmppath = NULL;
1575 done:
1576 if (fd != -1 && close(fd) != 0 && err == NULL)
1577 err = got_error_from_errno("close");
1578 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1579 err = got_error_from_errno2("unlink", tmppath);
1580 free(tmppath);
1581 return err;
1584 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1585 static const struct got_error *
1586 get_modified_file_content_status(unsigned char *status, FILE *f)
1588 const struct got_error *err = NULL;
1589 const char *markers[3] = {
1590 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1591 GOT_DIFF_CONFLICT_MARKER_SEP,
1592 GOT_DIFF_CONFLICT_MARKER_END
1594 int i = 0;
1595 char *line;
1596 size_t len;
1597 const char delim[3] = {'\0', '\0', '\0'};
1599 while (*status == GOT_STATUS_MODIFY) {
1600 line = fparseln(f, &len, NULL, delim, 0);
1601 if (line == NULL) {
1602 if (feof(f))
1603 break;
1604 err = got_ferror(f, GOT_ERR_IO);
1605 break;
1608 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1609 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1610 == 0)
1611 *status = GOT_STATUS_CONFLICT;
1612 else
1613 i++;
1617 return err;
1620 static int
1621 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1623 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1624 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1627 static int
1628 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1630 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1631 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1632 ie->mtime_sec == sb->st_mtim.tv_sec &&
1633 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1634 ie->size == (sb->st_size & 0xffffffff) &&
1635 !xbit_differs(ie, sb->st_mode));
1638 static unsigned char
1639 get_staged_status(struct got_fileindex_entry *ie)
1641 switch (got_fileindex_entry_stage_get(ie)) {
1642 case GOT_FILEIDX_STAGE_ADD:
1643 return GOT_STATUS_ADD;
1644 case GOT_FILEIDX_STAGE_DELETE:
1645 return GOT_STATUS_DELETE;
1646 case GOT_FILEIDX_STAGE_MODIFY:
1647 return GOT_STATUS_MODIFY;
1648 default:
1649 return GOT_STATUS_NO_CHANGE;
1653 static const struct got_error *
1654 get_symlink_modification_status(unsigned char *status,
1655 struct got_fileindex_entry *ie, const char *abspath,
1656 int dirfd, const char *de_name, struct got_blob_object *blob)
1658 const struct got_error *err = NULL;
1659 char target_path[PATH_MAX];
1660 char etarget[PATH_MAX];
1661 ssize_t elen;
1662 size_t len, target_len = 0;
1663 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1664 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1666 *status = GOT_STATUS_NO_CHANGE;
1668 /* Blob object content specifies the target path of the link. */
1669 do {
1670 err = got_object_blob_read_block(&len, blob);
1671 if (err)
1672 return err;
1673 if (len + target_len >= sizeof(target_path)) {
1675 * Should not happen. The blob contents were OK
1676 * when this symlink was installed.
1678 return got_error(GOT_ERR_NO_SPACE);
1680 if (len > 0) {
1681 /* Skip blob object header first time around. */
1682 memcpy(target_path + target_len, buf + hdrlen,
1683 len - hdrlen);
1684 target_len += len - hdrlen;
1685 hdrlen = 0;
1687 } while (len != 0);
1688 target_path[target_len] = '\0';
1690 if (dirfd != -1) {
1691 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1692 if (elen == -1)
1693 return got_error_from_errno2("readlinkat", abspath);
1694 } else {
1695 elen = readlink(abspath, etarget, sizeof(etarget));
1696 if (elen == -1)
1697 return got_error_from_errno2("readlink", abspath);
1700 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1701 *status = GOT_STATUS_MODIFY;
1703 return NULL;
1706 static const struct got_error *
1707 get_file_status(unsigned char *status, struct stat *sb,
1708 struct got_fileindex_entry *ie, const char *abspath,
1709 int dirfd, const char *de_name, struct got_repository *repo)
1711 const struct got_error *err = NULL;
1712 struct got_object_id id;
1713 size_t hdrlen;
1714 int fd = -1;
1715 FILE *f = NULL;
1716 uint8_t fbuf[8192];
1717 struct got_blob_object *blob = NULL;
1718 size_t flen, blen;
1719 unsigned char staged_status = get_staged_status(ie);
1721 *status = GOT_STATUS_NO_CHANGE;
1724 * Whenever the caller provides a directory descriptor and a
1725 * directory entry name for the file, use them! This prevents
1726 * race conditions if filesystem paths change beneath our feet.
1728 if (dirfd != -1) {
1729 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 if (errno == ENOENT) {
1731 if (got_fileindex_entry_has_file_on_disk(ie))
1732 *status = GOT_STATUS_MISSING;
1733 else
1734 *status = GOT_STATUS_DELETE;
1735 goto done;
1737 err = got_error_from_errno2("fstatat", abspath);
1738 goto done;
1740 } else {
1741 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1742 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1743 return got_error_from_errno2("open", abspath);
1744 else if (fd == -1 && errno == ELOOP) {
1745 if (lstat(abspath, sb) == -1)
1746 return got_error_from_errno2("lstat", abspath);
1747 } else if (fd == -1 || fstat(fd, sb) == -1) {
1748 if (errno == ENOENT) {
1749 if (got_fileindex_entry_has_file_on_disk(ie))
1750 *status = GOT_STATUS_MISSING;
1751 else
1752 *status = GOT_STATUS_DELETE;
1753 goto done;
1755 err = got_error_from_errno2("fstat", abspath);
1756 goto done;
1760 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1761 *status = GOT_STATUS_OBSTRUCTED;
1762 goto done;
1765 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1766 *status = GOT_STATUS_DELETE;
1767 goto done;
1768 } else if (!got_fileindex_entry_has_blob(ie) &&
1769 staged_status != GOT_STATUS_ADD) {
1770 *status = GOT_STATUS_ADD;
1771 goto done;
1774 if (!stat_info_differs(ie, sb))
1775 goto done;
1777 if (S_ISLNK(sb->st_mode) &&
1778 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1779 *status = GOT_STATUS_MODIFY;
1780 goto done;
1783 if (staged_status == GOT_STATUS_MODIFY ||
1784 staged_status == GOT_STATUS_ADD)
1785 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1786 else
1787 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1789 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1790 if (err)
1791 goto done;
1793 if (S_ISLNK(sb->st_mode)) {
1794 err = get_symlink_modification_status(status, ie,
1795 abspath, dirfd, de_name, blob);
1796 goto done;
1799 if (dirfd != -1) {
1800 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1801 if (fd == -1) {
1802 err = got_error_from_errno2("openat", abspath);
1803 goto done;
1807 f = fdopen(fd, "r");
1808 if (f == NULL) {
1809 err = got_error_from_errno2("fdopen", abspath);
1810 goto done;
1812 fd = -1;
1813 hdrlen = got_object_blob_get_hdrlen(blob);
1814 for (;;) {
1815 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1816 err = got_object_blob_read_block(&blen, blob);
1817 if (err)
1818 goto done;
1819 /* Skip length of blob object header first time around. */
1820 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1821 if (flen == 0 && ferror(f)) {
1822 err = got_error_from_errno("fread");
1823 goto done;
1825 if (blen - hdrlen == 0) {
1826 if (flen != 0)
1827 *status = GOT_STATUS_MODIFY;
1828 break;
1829 } else if (flen == 0) {
1830 if (blen - hdrlen != 0)
1831 *status = GOT_STATUS_MODIFY;
1832 break;
1833 } else if (blen - hdrlen == flen) {
1834 /* Skip blob object header first time around. */
1835 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1836 *status = GOT_STATUS_MODIFY;
1837 break;
1839 } else {
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1843 hdrlen = 0;
1846 if (*status == GOT_STATUS_MODIFY) {
1847 rewind(f);
1848 err = get_modified_file_content_status(status, f);
1849 } else if (xbit_differs(ie, sb->st_mode))
1850 *status = GOT_STATUS_MODE_CHANGE;
1851 done:
1852 if (blob)
1853 got_object_blob_close(blob);
1854 if (f != NULL && fclose(f) == EOF && err == NULL)
1855 err = got_error_from_errno2("fclose", abspath);
1856 if (fd != -1 && close(fd) == -1 && err == NULL)
1857 err = got_error_from_errno2("close", abspath);
1858 return err;
1862 * Update timestamps in the file index if a file is unmodified and
1863 * we had to run a full content comparison to find out.
1865 static const struct got_error *
1866 sync_timestamps(char *ondisk_path, unsigned char status,
1867 struct got_fileindex_entry *ie, struct stat *sb)
1869 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1870 return got_fileindex_entry_update(ie, ondisk_path,
1871 ie->blob_sha1, ie->commit_sha1, 1);
1873 return NULL;
1876 static const struct got_error *
1877 update_blob(struct got_worktree *worktree,
1878 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1879 struct got_tree_entry *te, const char *path,
1880 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1881 void *progress_arg)
1883 const struct got_error *err = NULL;
1884 struct got_blob_object *blob = NULL;
1885 char *ondisk_path;
1886 unsigned char status = GOT_STATUS_NO_CHANGE;
1887 struct stat sb;
1889 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1890 return got_error_from_errno("asprintf");
1892 if (ie) {
1893 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1894 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1895 goto done;
1897 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1898 repo);
1899 if (err)
1900 goto done;
1901 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1902 sb.st_mode = got_fileindex_perms_to_st(ie);
1903 } else {
1904 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1905 status = GOT_STATUS_UNVERSIONED;
1908 if (status == GOT_STATUS_OBSTRUCTED) {
1909 err = (*progress_cb)(progress_arg, status, path);
1910 goto done;
1912 if (status == GOT_STATUS_CONFLICT) {
1913 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1914 path);
1915 goto done;
1918 if (ie && status != GOT_STATUS_MISSING &&
1919 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1920 if (got_fileindex_entry_has_commit(ie) &&
1921 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1922 SHA1_DIGEST_LENGTH) == 0) {
1923 err = sync_timestamps(ondisk_path, status, ie, &sb);
1924 if (err)
1925 goto done;
1926 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1927 path);
1928 goto done;
1930 if (got_fileindex_entry_has_blob(ie) &&
1931 memcmp(ie->blob_sha1, te->id.sha1,
1932 SHA1_DIGEST_LENGTH) == 0) {
1933 err = sync_timestamps(ondisk_path, status, ie, &sb);
1934 goto done;
1938 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1939 if (err)
1940 goto done;
1942 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1943 int update_timestamps;
1944 struct got_blob_object *blob2 = NULL;
1945 char *label_orig = NULL;
1946 if (got_fileindex_entry_has_blob(ie)) {
1947 struct got_object_id id2;
1948 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1949 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1950 if (err)
1951 goto done;
1953 if (got_fileindex_entry_has_commit(ie)) {
1954 char id_str[SHA1_DIGEST_STRING_LENGTH];
1955 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1956 sizeof(id_str)) == NULL) {
1957 err = got_error_path(id_str,
1958 GOT_ERR_BAD_OBJ_ID_STR);
1959 goto done;
1961 if (asprintf(&label_orig, "%s: commit %s",
1962 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1963 err = got_error_from_errno("asprintf");
1964 goto done;
1967 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1968 char *link_target;
1969 err = got_object_blob_read_to_str(&link_target, blob);
1970 if (err)
1971 goto done;
1972 err = merge_symlink(worktree, blob2, ondisk_path, path,
1973 label_orig, link_target, worktree->base_commit_id,
1974 repo, progress_cb, progress_arg);
1975 free(link_target);
1976 } else {
1977 err = merge_blob(&update_timestamps, worktree, blob2,
1978 ondisk_path, path, sb.st_mode, label_orig, blob,
1979 worktree->base_commit_id, repo,
1980 progress_cb, progress_arg);
1982 free(label_orig);
1983 if (blob2)
1984 got_object_blob_close(blob2);
1985 if (err)
1986 goto done;
1988 * Do not update timestamps of files with local changes.
1989 * Otherwise, a future status walk would treat them as
1990 * unmodified files again.
1992 err = got_fileindex_entry_update(ie, ondisk_path,
1993 blob->id.sha1, worktree->base_commit_id->sha1,
1994 update_timestamps);
1995 } else if (status == GOT_STATUS_MODE_CHANGE) {
1996 err = got_fileindex_entry_update(ie, ondisk_path,
1997 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1998 } else if (status == GOT_STATUS_DELETE) {
1999 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2000 if (err)
2001 goto done;
2002 err = got_fileindex_entry_update(ie, ondisk_path,
2003 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2004 if (err)
2005 goto done;
2006 } else {
2007 int is_bad_symlink = 0;
2008 if (S_ISLNK(te->mode)) {
2009 err = install_symlink(&is_bad_symlink, worktree,
2010 ondisk_path, path, blob,
2011 status == GOT_STATUS_MISSING, 0,
2012 status == GOT_STATUS_UNVERSIONED, repo,
2013 progress_cb, progress_arg);
2014 } else {
2015 err = install_blob(worktree, ondisk_path, path,
2016 te->mode, sb.st_mode, blob,
2017 status == GOT_STATUS_MISSING, 0, 0,
2018 status == GOT_STATUS_UNVERSIONED, repo,
2019 progress_cb, progress_arg);
2021 if (err)
2022 goto done;
2024 if (ie) {
2025 err = got_fileindex_entry_update(ie, ondisk_path,
2026 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2027 } else {
2028 err = create_fileindex_entry(&ie, fileindex,
2029 worktree->base_commit_id, ondisk_path, path,
2030 &blob->id);
2032 if (err)
2033 goto done;
2035 if (is_bad_symlink) {
2036 got_fileindex_entry_filetype_set(ie,
2037 GOT_FILEIDX_MODE_BAD_SYMLINK);
2040 got_object_blob_close(blob);
2041 done:
2042 free(ondisk_path);
2043 return err;
2046 static const struct got_error *
2047 remove_ondisk_file(const char *root_path, const char *path)
2049 const struct got_error *err = NULL;
2050 char *ondisk_path = NULL;
2052 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2053 return got_error_from_errno("asprintf");
2055 if (unlink(ondisk_path) == -1) {
2056 if (errno != ENOENT)
2057 err = got_error_from_errno2("unlink", ondisk_path);
2058 } else {
2059 size_t root_len = strlen(root_path);
2060 do {
2061 char *parent;
2062 err = got_path_dirname(&parent, ondisk_path);
2063 if (err)
2064 break;
2065 free(ondisk_path);
2066 ondisk_path = parent;
2067 if (rmdir(ondisk_path) == -1) {
2068 if (errno != ENOTEMPTY)
2069 err = got_error_from_errno2("rmdir",
2070 ondisk_path);
2071 break;
2073 } while (got_path_cmp(ondisk_path, root_path,
2074 strlen(ondisk_path), root_len) != 0);
2076 free(ondisk_path);
2077 return err;
2080 static const struct got_error *
2081 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2082 struct got_fileindex_entry *ie, struct got_repository *repo,
2083 got_worktree_checkout_cb progress_cb, void *progress_arg)
2085 const struct got_error *err = NULL;
2086 unsigned char status;
2087 struct stat sb;
2088 char *ondisk_path;
2090 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2091 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2093 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2094 == -1)
2095 return got_error_from_errno("asprintf");
2097 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2098 if (err)
2099 goto done;
2101 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2102 char ondisk_target[PATH_MAX];
2103 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2104 sizeof(ondisk_target));
2105 if (ondisk_len == -1) {
2106 err = got_error_from_errno2("readlink", ondisk_path);
2107 goto done;
2109 ondisk_target[ondisk_len] = '\0';
2110 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2111 NULL, NULL, /* XXX pass common ancestor info? */
2112 ondisk_target, ondisk_path);
2113 if (err)
2114 goto done;
2115 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2116 ie->path);
2117 goto done;
2120 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2121 status == GOT_STATUS_ADD) {
2122 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2123 if (err)
2124 goto done;
2126 * Preserve the working file and change the deleted blob's
2127 * entry into a schedule-add entry.
2129 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2130 0);
2131 } else {
2132 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2133 if (err)
2134 goto done;
2135 if (status == GOT_STATUS_NO_CHANGE) {
2136 err = remove_ondisk_file(worktree->root_path, ie->path);
2137 if (err)
2138 goto done;
2140 got_fileindex_entry_remove(fileindex, ie);
2142 done:
2143 free(ondisk_path);
2144 return err;
2147 struct diff_cb_arg {
2148 struct got_fileindex *fileindex;
2149 struct got_worktree *worktree;
2150 struct got_repository *repo;
2151 got_worktree_checkout_cb progress_cb;
2152 void *progress_arg;
2153 got_cancel_cb cancel_cb;
2154 void *cancel_arg;
2157 static const struct got_error *
2158 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2159 struct got_tree_entry *te, const char *parent_path)
2161 struct diff_cb_arg *a = arg;
2163 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2164 return got_error(GOT_ERR_CANCELLED);
2166 return update_blob(a->worktree, a->fileindex, ie, te,
2167 ie->path, a->repo, a->progress_cb, a->progress_arg);
2170 static const struct got_error *
2171 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2173 struct diff_cb_arg *a = arg;
2175 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 return got_error(GOT_ERR_CANCELLED);
2178 return delete_blob(a->worktree, a->fileindex, ie,
2179 a->repo, a->progress_cb, a->progress_arg);
2182 static const struct got_error *
2183 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2185 struct diff_cb_arg *a = arg;
2186 const struct got_error *err;
2187 char *path;
2189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 return got_error(GOT_ERR_CANCELLED);
2192 if (got_object_tree_entry_is_submodule(te))
2193 return NULL;
2195 if (asprintf(&path, "%s%s%s", parent_path,
2196 parent_path[0] ? "/" : "", te->name)
2197 == -1)
2198 return got_error_from_errno("asprintf");
2200 if (S_ISDIR(te->mode))
2201 err = add_dir_on_disk(a->worktree, path);
2202 else
2203 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2204 a->repo, a->progress_cb, a->progress_arg);
2206 free(path);
2207 return err;
2210 const struct got_error *
2211 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2213 uint32_t uuid_status;
2215 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2216 if (uuid_status != uuid_s_ok) {
2217 *uuidstr = NULL;
2218 return got_error_uuid(uuid_status, "uuid_to_string");
2221 return NULL;
2224 static const struct got_error *
2225 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2227 const struct got_error *err = NULL;
2228 char *uuidstr = NULL;
2230 *refname = NULL;
2232 err = got_worktree_get_uuid(&uuidstr, worktree);
2233 if (err)
2234 return err;
2236 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2237 err = got_error_from_errno("asprintf");
2238 *refname = NULL;
2240 free(uuidstr);
2241 return err;
2244 const struct got_error *
2245 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2247 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2250 static const struct got_error *
2251 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2253 return get_ref_name(refname, worktree,
2254 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2257 static const struct got_error *
2258 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2260 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2263 static const struct got_error *
2264 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2266 return get_ref_name(refname, worktree,
2267 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2270 static const struct got_error *
2271 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2273 return get_ref_name(refname, worktree,
2274 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2277 static const struct got_error *
2278 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2280 return get_ref_name(refname, worktree,
2281 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2284 static const struct got_error *
2285 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2287 return get_ref_name(refname, worktree,
2288 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2291 static const struct got_error *
2292 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree,
2295 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2298 static const struct got_error *
2299 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2301 return get_ref_name(refname, worktree,
2302 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2305 const struct got_error *
2306 got_worktree_get_histedit_script_path(char **path,
2307 struct got_worktree *worktree)
2309 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2310 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2311 *path = NULL;
2312 return got_error_from_errno("asprintf");
2314 return NULL;
2318 * Prevent Git's garbage collector from deleting our base commit by
2319 * setting a reference to our base commit's ID.
2321 static const struct got_error *
2322 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2324 const struct got_error *err = NULL;
2325 struct got_reference *ref = NULL;
2326 char *refname;
2328 err = got_worktree_get_base_ref_name(&refname, worktree);
2329 if (err)
2330 return err;
2332 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2333 if (err)
2334 goto done;
2336 err = got_ref_write(ref, repo);
2337 done:
2338 free(refname);
2339 if (ref)
2340 got_ref_close(ref);
2341 return err;
2344 static const struct got_error *
2345 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2347 const struct got_error *err = NULL;
2349 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2350 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2351 err = got_error_from_errno("asprintf");
2352 *fileindex_path = NULL;
2354 return err;
2358 static const struct got_error *
2359 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2360 struct got_worktree *worktree)
2362 const struct got_error *err = NULL;
2363 FILE *index = NULL;
2365 *fileindex_path = NULL;
2366 *fileindex = got_fileindex_alloc();
2367 if (*fileindex == NULL)
2368 return got_error_from_errno("got_fileindex_alloc");
2370 err = get_fileindex_path(fileindex_path, worktree);
2371 if (err)
2372 goto done;
2374 index = fopen(*fileindex_path, "rb");
2375 if (index == NULL) {
2376 if (errno != ENOENT)
2377 err = got_error_from_errno2("fopen", *fileindex_path);
2378 } else {
2379 err = got_fileindex_read(*fileindex, index);
2380 if (fclose(index) != 0 && err == NULL)
2381 err = got_error_from_errno("fclose");
2383 done:
2384 if (err) {
2385 free(*fileindex_path);
2386 *fileindex_path = NULL;
2387 got_fileindex_free(*fileindex);
2388 *fileindex = NULL;
2390 return err;
2393 struct bump_base_commit_id_arg {
2394 struct got_object_id *base_commit_id;
2395 const char *path;
2396 size_t path_len;
2397 const char *entry_name;
2398 got_worktree_checkout_cb progress_cb;
2399 void *progress_arg;
2402 /* Bump base commit ID of all files within an updated part of the work tree. */
2403 static const struct got_error *
2404 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2406 const struct got_error *err;
2407 struct bump_base_commit_id_arg *a = arg;
2409 if (a->entry_name) {
2410 if (strcmp(ie->path, a->path) != 0)
2411 return NULL;
2412 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2413 return NULL;
2415 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2416 SHA1_DIGEST_LENGTH) == 0)
2417 return NULL;
2419 if (a->progress_cb) {
2420 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2421 ie->path);
2422 if (err)
2423 return err;
2425 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2426 return NULL;
2429 static const struct got_error *
2430 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2432 const struct got_error *err = NULL;
2433 char *new_fileindex_path = NULL;
2434 FILE *new_index = NULL;
2435 struct timespec timeout;
2437 err = got_opentemp_named(&new_fileindex_path, &new_index,
2438 fileindex_path);
2439 if (err)
2440 goto done;
2442 err = got_fileindex_write(fileindex, new_index);
2443 if (err)
2444 goto done;
2446 if (rename(new_fileindex_path, fileindex_path) != 0) {
2447 err = got_error_from_errno3("rename", new_fileindex_path,
2448 fileindex_path);
2449 unlink(new_fileindex_path);
2453 * Sleep for a short amount of time to ensure that files modified after
2454 * this program exits have a different time stamp from the one which
2455 * was recorded in the file index.
2457 timeout.tv_sec = 0;
2458 timeout.tv_nsec = 1;
2459 nanosleep(&timeout, NULL);
2460 done:
2461 if (new_index)
2462 fclose(new_index);
2463 free(new_fileindex_path);
2464 return err;
2467 static const struct got_error *
2468 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2469 struct got_object_id **tree_id, const char *wt_relpath,
2470 struct got_worktree *worktree, struct got_repository *repo)
2472 const struct got_error *err = NULL;
2473 struct got_object_id *id = NULL;
2474 char *in_repo_path = NULL;
2475 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2477 *entry_type = GOT_OBJ_TYPE_ANY;
2478 *tree_relpath = NULL;
2479 *tree_id = NULL;
2481 if (wt_relpath[0] == '\0') {
2482 /* Check out all files within the work tree. */
2483 *entry_type = GOT_OBJ_TYPE_TREE;
2484 *tree_relpath = strdup("");
2485 if (*tree_relpath == NULL) {
2486 err = got_error_from_errno("strdup");
2487 goto done;
2489 err = got_object_id_by_path(tree_id, repo,
2490 worktree->base_commit_id, worktree->path_prefix);
2491 if (err)
2492 goto done;
2493 return NULL;
2496 /* Check out a subset of files in the work tree. */
2498 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2499 is_root_wt ? "" : "/", wt_relpath) == -1) {
2500 err = got_error_from_errno("asprintf");
2501 goto done;
2504 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2505 in_repo_path);
2506 if (err)
2507 goto done;
2509 free(in_repo_path);
2510 in_repo_path = NULL;
2512 err = got_object_get_type(entry_type, repo, id);
2513 if (err)
2514 goto done;
2516 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2517 /* Check out a single file. */
2518 if (strchr(wt_relpath, '/') == NULL) {
2519 /* Check out a single file in work tree's root dir. */
2520 in_repo_path = strdup(worktree->path_prefix);
2521 if (in_repo_path == NULL) {
2522 err = got_error_from_errno("strdup");
2523 goto done;
2525 *tree_relpath = strdup("");
2526 if (*tree_relpath == NULL) {
2527 err = got_error_from_errno("strdup");
2528 goto done;
2530 } else {
2531 /* Check out a single file in a subdirectory. */
2532 err = got_path_dirname(tree_relpath, wt_relpath);
2533 if (err)
2534 return err;
2535 if (asprintf(&in_repo_path, "%s%s%s",
2536 worktree->path_prefix, is_root_wt ? "" : "/",
2537 *tree_relpath) == -1) {
2538 err = got_error_from_errno("asprintf");
2539 goto done;
2542 err = got_object_id_by_path(tree_id, repo,
2543 worktree->base_commit_id, in_repo_path);
2544 } else {
2545 /* Check out all files within a subdirectory. */
2546 *tree_id = got_object_id_dup(id);
2547 if (*tree_id == NULL) {
2548 err = got_error_from_errno("got_object_id_dup");
2549 goto done;
2551 *tree_relpath = strdup(wt_relpath);
2552 if (*tree_relpath == NULL) {
2553 err = got_error_from_errno("strdup");
2554 goto done;
2557 done:
2558 free(id);
2559 free(in_repo_path);
2560 if (err) {
2561 *entry_type = GOT_OBJ_TYPE_ANY;
2562 free(*tree_relpath);
2563 *tree_relpath = NULL;
2564 free(*tree_id);
2565 *tree_id = NULL;
2567 return err;
2570 static const struct got_error *
2571 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2572 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2573 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2574 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2576 const struct got_error *err = NULL;
2577 struct got_commit_object *commit = NULL;
2578 struct got_tree_object *tree = NULL;
2579 struct got_fileindex_diff_tree_cb diff_cb;
2580 struct diff_cb_arg arg;
2582 err = ref_base_commit(worktree, repo);
2583 if (err) {
2584 if (!(err->code == GOT_ERR_ERRNO &&
2585 (errno == EACCES || errno == EROFS)))
2586 goto done;
2587 err = (*progress_cb)(progress_arg,
2588 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2589 if (err)
2590 return err;
2593 err = got_object_open_as_commit(&commit, repo,
2594 worktree->base_commit_id);
2595 if (err)
2596 goto done;
2598 err = got_object_open_as_tree(&tree, repo, tree_id);
2599 if (err)
2600 goto done;
2602 if (entry_name &&
2603 got_object_tree_find_entry(tree, entry_name) == NULL) {
2604 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2605 goto done;
2608 diff_cb.diff_old_new = diff_old_new;
2609 diff_cb.diff_old = diff_old;
2610 diff_cb.diff_new = diff_new;
2611 arg.fileindex = fileindex;
2612 arg.worktree = worktree;
2613 arg.repo = repo;
2614 arg.progress_cb = progress_cb;
2615 arg.progress_arg = progress_arg;
2616 arg.cancel_cb = cancel_cb;
2617 arg.cancel_arg = cancel_arg;
2618 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2619 entry_name, repo, &diff_cb, &arg);
2620 done:
2621 if (tree)
2622 got_object_tree_close(tree);
2623 if (commit)
2624 got_object_commit_close(commit);
2625 return err;
2628 const struct got_error *
2629 got_worktree_checkout_files(struct got_worktree *worktree,
2630 struct got_pathlist_head *paths, struct got_repository *repo,
2631 got_worktree_checkout_cb progress_cb, void *progress_arg,
2632 got_cancel_cb cancel_cb, void *cancel_arg)
2634 const struct got_error *err = NULL, *sync_err, *unlockerr;
2635 struct got_commit_object *commit = NULL;
2636 struct got_tree_object *tree = NULL;
2637 struct got_fileindex *fileindex = NULL;
2638 char *fileindex_path = NULL;
2639 struct got_pathlist_entry *pe;
2640 struct tree_path_data {
2641 SIMPLEQ_ENTRY(tree_path_data) entry;
2642 struct got_object_id *tree_id;
2643 int entry_type;
2644 char *relpath;
2645 char *entry_name;
2646 } *tpd = NULL;
2647 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2649 SIMPLEQ_INIT(&tree_paths);
2651 err = lock_worktree(worktree, LOCK_EX);
2652 if (err)
2653 return err;
2655 /* Map all specified paths to in-repository trees. */
2656 TAILQ_FOREACH(pe, paths, entry) {
2657 tpd = malloc(sizeof(*tpd));
2658 if (tpd == NULL) {
2659 err = got_error_from_errno("malloc");
2660 goto done;
2663 err = find_tree_entry_for_checkout(&tpd->entry_type,
2664 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2665 if (err) {
2666 free(tpd);
2667 goto done;
2670 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2671 err = got_path_basename(&tpd->entry_name, pe->path);
2672 if (err) {
2673 free(tpd->relpath);
2674 free(tpd->tree_id);
2675 free(tpd);
2676 goto done;
2678 } else
2679 tpd->entry_name = NULL;
2681 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2685 * Read the file index.
2686 * Checking out files is supposed to be an idempotent operation.
2687 * If the on-disk file index is incomplete we will try to complete it.
2689 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2690 if (err)
2691 goto done;
2693 tpd = SIMPLEQ_FIRST(&tree_paths);
2694 TAILQ_FOREACH(pe, paths, entry) {
2695 struct bump_base_commit_id_arg bbc_arg;
2697 err = checkout_files(worktree, fileindex, tpd->relpath,
2698 tpd->tree_id, tpd->entry_name, repo,
2699 progress_cb, progress_arg, cancel_cb, cancel_arg);
2700 if (err)
2701 break;
2703 bbc_arg.base_commit_id = worktree->base_commit_id;
2704 bbc_arg.entry_name = tpd->entry_name;
2705 bbc_arg.path = pe->path;
2706 bbc_arg.path_len = pe->path_len;
2707 bbc_arg.progress_cb = progress_cb;
2708 bbc_arg.progress_arg = progress_arg;
2709 err = got_fileindex_for_each_entry_safe(fileindex,
2710 bump_base_commit_id, &bbc_arg);
2711 if (err)
2712 break;
2714 tpd = SIMPLEQ_NEXT(tpd, entry);
2716 sync_err = sync_fileindex(fileindex, fileindex_path);
2717 if (sync_err && err == NULL)
2718 err = sync_err;
2719 done:
2720 free(fileindex_path);
2721 if (tree)
2722 got_object_tree_close(tree);
2723 if (commit)
2724 got_object_commit_close(commit);
2725 if (fileindex)
2726 got_fileindex_free(fileindex);
2727 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2728 tpd = SIMPLEQ_FIRST(&tree_paths);
2729 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2730 free(tpd->relpath);
2731 free(tpd->tree_id);
2732 free(tpd);
2734 unlockerr = lock_worktree(worktree, LOCK_SH);
2735 if (unlockerr && err == NULL)
2736 err = unlockerr;
2737 return err;
2740 struct merge_file_cb_arg {
2741 struct got_worktree *worktree;
2742 struct got_fileindex *fileindex;
2743 got_worktree_checkout_cb progress_cb;
2744 void *progress_arg;
2745 got_cancel_cb cancel_cb;
2746 void *cancel_arg;
2747 const char *label_orig;
2748 struct got_object_id *commit_id2;
2751 static const struct got_error *
2752 merge_file_cb(void *arg, struct got_blob_object *blob1,
2753 struct got_blob_object *blob2, struct got_object_id *id1,
2754 struct got_object_id *id2, const char *path1, const char *path2,
2755 mode_t mode1, mode_t mode2, struct got_repository *repo)
2757 static const struct got_error *err = NULL;
2758 struct merge_file_cb_arg *a = arg;
2759 struct got_fileindex_entry *ie;
2760 char *ondisk_path = NULL;
2761 struct stat sb;
2762 unsigned char status;
2763 int local_changes_subsumed;
2765 if (blob1 && blob2) {
2766 ie = got_fileindex_entry_get(a->fileindex, path2,
2767 strlen(path2));
2768 if (ie == NULL)
2769 return (*a->progress_cb)(a->progress_arg,
2770 GOT_STATUS_MISSING, path2);
2772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 path2) == -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 if (status == GOT_STATUS_DELETE) {
2782 err = (*a->progress_cb)(a->progress_arg,
2783 GOT_STATUS_MERGE, path2);
2784 goto done;
2786 if (status != GOT_STATUS_NO_CHANGE &&
2787 status != GOT_STATUS_MODIFY &&
2788 status != GOT_STATUS_CONFLICT &&
2789 status != GOT_STATUS_ADD) {
2790 err = (*a->progress_cb)(a->progress_arg, status, path2);
2791 goto done;
2794 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2795 char *link_target2;
2796 err = got_object_blob_read_to_str(&link_target2, blob2);
2797 if (err)
2798 goto done;
2799 err = merge_symlink(a->worktree, blob1, ondisk_path,
2800 path2, a->label_orig, link_target2, a->commit_id2,
2801 repo, a->progress_cb, a->progress_arg);
2802 free(link_target2);
2803 } else {
2804 err = merge_blob(&local_changes_subsumed, a->worktree,
2805 blob1, ondisk_path, path2, sb.st_mode,
2806 a->label_orig, blob2, a->commit_id2, repo,
2807 a->progress_cb, a->progress_arg);
2809 } else if (blob1) {
2810 ie = got_fileindex_entry_get(a->fileindex, path1,
2811 strlen(path1));
2812 if (ie == NULL)
2813 return (*a->progress_cb)(a->progress_arg,
2814 GOT_STATUS_MISSING, path1);
2816 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2817 path1) == -1)
2818 return got_error_from_errno("asprintf");
2820 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2821 repo);
2822 if (err)
2823 goto done;
2825 switch (status) {
2826 case GOT_STATUS_NO_CHANGE:
2827 err = (*a->progress_cb)(a->progress_arg,
2828 GOT_STATUS_DELETE, path1);
2829 if (err)
2830 goto done;
2831 err = remove_ondisk_file(a->worktree->root_path, path1);
2832 if (err)
2833 goto done;
2834 if (ie)
2835 got_fileindex_entry_mark_deleted_from_disk(ie);
2836 break;
2837 case GOT_STATUS_DELETE:
2838 case GOT_STATUS_MISSING:
2839 err = (*a->progress_cb)(a->progress_arg,
2840 GOT_STATUS_DELETE, path1);
2841 if (err)
2842 goto done;
2843 if (ie)
2844 got_fileindex_entry_mark_deleted_from_disk(ie);
2845 break;
2846 case GOT_STATUS_ADD: {
2847 struct got_object_id *id;
2848 FILE *blob1_f;
2850 * Delete the added file only if its content already
2851 * exists in the repository.
2853 err = got_object_blob_file_create(&id, &blob1_f, path1);
2854 if (err)
2855 goto done;
2856 if (got_object_id_cmp(id, id1) == 0) {
2857 err = (*a->progress_cb)(a->progress_arg,
2858 GOT_STATUS_DELETE, path1);
2859 if (err)
2860 goto done;
2861 err = remove_ondisk_file(a->worktree->root_path,
2862 path1);
2863 if (err)
2864 goto done;
2865 if (ie)
2866 got_fileindex_entry_remove(a->fileindex,
2867 ie);
2868 } else {
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_CANNOT_DELETE, path1);
2872 if (fclose(blob1_f) == EOF && err == NULL)
2873 err = got_error_from_errno("fclose");
2874 free(id);
2875 if (err)
2876 goto done;
2877 break;
2879 case GOT_STATUS_MODIFY:
2880 case GOT_STATUS_CONFLICT:
2881 err = (*a->progress_cb)(a->progress_arg,
2882 GOT_STATUS_CANNOT_DELETE, path1);
2883 if (err)
2884 goto done;
2885 break;
2886 case GOT_STATUS_OBSTRUCTED:
2887 err = (*a->progress_cb)(a->progress_arg, status, path1);
2888 if (err)
2889 goto done;
2890 break;
2891 default:
2892 break;
2894 } else if (blob2) {
2895 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2896 path2) == -1)
2897 return got_error_from_errno("asprintf");
2898 ie = got_fileindex_entry_get(a->fileindex, path2,
2899 strlen(path2));
2900 if (ie) {
2901 err = get_file_status(&status, &sb, ie, ondisk_path,
2902 -1, NULL, repo);
2903 if (err)
2904 goto done;
2905 if (status != GOT_STATUS_NO_CHANGE &&
2906 status != GOT_STATUS_MODIFY &&
2907 status != GOT_STATUS_CONFLICT &&
2908 status != GOT_STATUS_ADD) {
2909 err = (*a->progress_cb)(a->progress_arg,
2910 status, path2);
2911 goto done;
2913 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2914 char *link_target2;
2915 err = got_object_blob_read_to_str(&link_target2,
2916 blob2);
2917 if (err)
2918 goto done;
2919 err = merge_symlink(a->worktree, NULL,
2920 ondisk_path, path2, a->label_orig,
2921 link_target2, a->commit_id2, repo,
2922 a->progress_cb, a->progress_arg);
2923 free(link_target2);
2924 } else if (S_ISREG(sb.st_mode)) {
2925 err = merge_blob(&local_changes_subsumed,
2926 a->worktree, NULL, ondisk_path, path2,
2927 sb.st_mode, a->label_orig, blob2,
2928 a->commit_id2, repo, a->progress_cb,
2929 a->progress_arg);
2930 } else {
2931 err = got_error_path(ondisk_path,
2932 GOT_ERR_FILE_OBSTRUCTED);
2934 if (err)
2935 goto done;
2936 if (status == GOT_STATUS_DELETE) {
2937 err = got_fileindex_entry_update(ie,
2938 ondisk_path, blob2->id.sha1,
2939 a->worktree->base_commit_id->sha1, 0);
2940 if (err)
2941 goto done;
2943 } else {
2944 int is_bad_symlink = 0;
2945 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2946 if (S_ISLNK(mode2)) {
2947 err = install_symlink(&is_bad_symlink,
2948 a->worktree, ondisk_path, path2, blob2, 0,
2949 0, 1, repo, a->progress_cb, a->progress_arg);
2950 } else {
2951 err = install_blob(a->worktree, ondisk_path, path2,
2952 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2953 a->progress_cb, a->progress_arg);
2955 if (err)
2956 goto done;
2957 err = got_fileindex_entry_alloc(&ie, path2);
2958 if (err)
2959 goto done;
2960 err = got_fileindex_entry_update(ie, ondisk_path,
2961 NULL, NULL, 1);
2962 if (err) {
2963 got_fileindex_entry_free(ie);
2964 goto done;
2966 err = got_fileindex_entry_add(a->fileindex, ie);
2967 if (err) {
2968 got_fileindex_entry_free(ie);
2969 goto done;
2971 if (is_bad_symlink) {
2972 got_fileindex_entry_filetype_set(ie,
2973 GOT_FILEIDX_MODE_BAD_SYMLINK);
2977 done:
2978 free(ondisk_path);
2979 return err;
2982 struct check_merge_ok_arg {
2983 struct got_worktree *worktree;
2984 struct got_repository *repo;
2987 static const struct got_error *
2988 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2990 const struct got_error *err = NULL;
2991 struct check_merge_ok_arg *a = arg;
2992 unsigned char status;
2993 struct stat sb;
2994 char *ondisk_path;
2996 /* Reject merges into a work tree with mixed base commits. */
2997 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2998 SHA1_DIGEST_LENGTH))
2999 return got_error(GOT_ERR_MIXED_COMMITS);
3001 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3002 == -1)
3003 return got_error_from_errno("asprintf");
3005 /* Reject merges into a work tree with conflicted files. */
3006 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3007 if (err)
3008 return err;
3009 if (status == GOT_STATUS_CONFLICT)
3010 return got_error(GOT_ERR_CONFLICTS);
3012 return NULL;
3015 static const struct got_error *
3016 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3017 const char *fileindex_path, struct got_object_id *commit_id1,
3018 struct got_object_id *commit_id2, struct got_repository *repo,
3019 got_worktree_checkout_cb progress_cb, void *progress_arg,
3020 got_cancel_cb cancel_cb, void *cancel_arg)
3022 const struct got_error *err = NULL, *sync_err;
3023 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 struct merge_file_cb_arg arg;
3026 char *label_orig = NULL;
3028 if (commit_id1) {
3029 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3030 worktree->path_prefix);
3031 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3032 goto done;
3034 if (tree_id1) {
3035 char *id_str;
3037 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3038 if (err)
3039 goto done;
3041 err = got_object_id_str(&id_str, commit_id1);
3042 if (err)
3043 goto done;
3045 if (asprintf(&label_orig, "%s: commit %s",
3046 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3047 err = got_error_from_errno("asprintf");
3048 free(id_str);
3049 goto done;
3051 free(id_str);
3054 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3055 worktree->path_prefix);
3056 if (err)
3057 goto done;
3059 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3060 if (err)
3061 goto done;
3063 arg.worktree = worktree;
3064 arg.fileindex = fileindex;
3065 arg.progress_cb = progress_cb;
3066 arg.progress_arg = progress_arg;
3067 arg.cancel_cb = cancel_cb;
3068 arg.cancel_arg = cancel_arg;
3069 arg.label_orig = label_orig;
3070 arg.commit_id2 = commit_id2;
3071 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3072 sync_err = sync_fileindex(fileindex, fileindex_path);
3073 if (sync_err && err == NULL)
3074 err = sync_err;
3075 done:
3076 if (tree1)
3077 got_object_tree_close(tree1);
3078 if (tree2)
3079 got_object_tree_close(tree2);
3080 free(label_orig);
3081 return err;
3084 const struct got_error *
3085 got_worktree_merge_files(struct got_worktree *worktree,
3086 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3087 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3088 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3090 const struct got_error *err, *unlockerr;
3091 char *fileindex_path = NULL;
3092 struct got_fileindex *fileindex = NULL;
3093 struct check_merge_ok_arg mok_arg;
3095 err = lock_worktree(worktree, LOCK_EX);
3096 if (err)
3097 return err;
3099 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3100 if (err)
3101 goto done;
3103 mok_arg.worktree = worktree;
3104 mok_arg.repo = repo;
3105 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3106 &mok_arg);
3107 if (err)
3108 goto done;
3110 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3111 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3112 done:
3113 if (fileindex)
3114 got_fileindex_free(fileindex);
3115 free(fileindex_path);
3116 unlockerr = lock_worktree(worktree, LOCK_SH);
3117 if (unlockerr && err == NULL)
3118 err = unlockerr;
3119 return err;
3122 struct diff_dir_cb_arg {
3123 struct got_fileindex *fileindex;
3124 struct got_worktree *worktree;
3125 const char *status_path;
3126 size_t status_path_len;
3127 struct got_repository *repo;
3128 got_worktree_status_cb status_cb;
3129 void *status_arg;
3130 got_cancel_cb cancel_cb;
3131 void *cancel_arg;
3132 /* A pathlist containing per-directory pathlists of ignore patterns. */
3133 struct got_pathlist_head ignores;
3134 int report_unchanged;
3135 int no_ignores;
3138 static const struct got_error *
3139 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3140 int dirfd, const char *de_name,
3141 got_worktree_status_cb status_cb, void *status_arg,
3142 struct got_repository *repo, int report_unchanged)
3144 const struct got_error *err = NULL;
3145 unsigned char status = GOT_STATUS_NO_CHANGE;
3146 unsigned char staged_status = get_staged_status(ie);
3147 struct stat sb;
3148 struct got_object_id blob_id, commit_id, staged_blob_id;
3149 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3150 struct got_object_id *staged_blob_idp = NULL;
3152 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3153 if (err)
3154 return err;
3156 if (status == GOT_STATUS_NO_CHANGE &&
3157 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3158 return NULL;
3160 if (got_fileindex_entry_has_blob(ie)) {
3161 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3162 blob_idp = &blob_id;
3164 if (got_fileindex_entry_has_commit(ie)) {
3165 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3166 commit_idp = &commit_id;
3168 if (staged_status == GOT_STATUS_ADD ||
3169 staged_status == GOT_STATUS_MODIFY) {
3170 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3171 SHA1_DIGEST_LENGTH);
3172 staged_blob_idp = &staged_blob_id;
3175 return (*status_cb)(status_arg, status, staged_status,
3176 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3179 static const struct got_error *
3180 status_old_new(void *arg, struct got_fileindex_entry *ie,
3181 struct dirent *de, const char *parent_path, int dirfd)
3183 const struct got_error *err = NULL;
3184 struct diff_dir_cb_arg *a = arg;
3185 char *abspath;
3187 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3188 return got_error(GOT_ERR_CANCELLED);
3190 if (got_path_cmp(parent_path, a->status_path,
3191 strlen(parent_path), a->status_path_len) != 0 &&
3192 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3193 return NULL;
3195 if (parent_path[0]) {
3196 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3197 parent_path, de->d_name) == -1)
3198 return got_error_from_errno("asprintf");
3199 } else {
3200 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3201 de->d_name) == -1)
3202 return got_error_from_errno("asprintf");
3205 err = report_file_status(ie, abspath, dirfd, de->d_name,
3206 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3207 free(abspath);
3208 return err;
3211 static const struct got_error *
3212 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3214 struct diff_dir_cb_arg *a = arg;
3215 struct got_object_id blob_id, commit_id;
3216 unsigned char status;
3218 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3219 return got_error(GOT_ERR_CANCELLED);
3221 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3222 return NULL;
3224 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3225 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3226 if (got_fileindex_entry_has_file_on_disk(ie))
3227 status = GOT_STATUS_MISSING;
3228 else
3229 status = GOT_STATUS_DELETE;
3230 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3231 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3234 void
3235 free_ignorelist(struct got_pathlist_head *ignorelist)
3237 struct got_pathlist_entry *pe;
3239 TAILQ_FOREACH(pe, ignorelist, entry)
3240 free((char *)pe->path);
3241 got_pathlist_free(ignorelist);
3244 void
3245 free_ignores(struct got_pathlist_head *ignores)
3247 struct got_pathlist_entry *pe;
3249 TAILQ_FOREACH(pe, ignores, entry) {
3250 struct got_pathlist_head *ignorelist = pe->data;
3251 free_ignorelist(ignorelist);
3252 free((char *)pe->path);
3254 got_pathlist_free(ignores);
3257 static const struct got_error *
3258 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3260 const struct got_error *err = NULL;
3261 struct got_pathlist_entry *pe = NULL;
3262 struct got_pathlist_head *ignorelist;
3263 char *line = NULL, *pattern, *dirpath = NULL;
3264 size_t linesize = 0;
3265 ssize_t linelen;
3267 ignorelist = calloc(1, sizeof(*ignorelist));
3268 if (ignorelist == NULL)
3269 return got_error_from_errno("calloc");
3270 TAILQ_INIT(ignorelist);
3272 while ((linelen = getline(&line, &linesize, f)) != -1) {
3273 if (linelen > 0 && line[linelen - 1] == '\n')
3274 line[linelen - 1] = '\0';
3276 /* Git's ignores may contain comments. */
3277 if (line[0] == '#')
3278 continue;
3280 /* Git's negated patterns are not (yet?) supported. */
3281 if (line[0] == '!')
3282 continue;
3284 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3285 line) == -1) {
3286 err = got_error_from_errno("asprintf");
3287 goto done;
3289 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3290 if (err)
3291 goto done;
3293 if (ferror(f)) {
3294 err = got_error_from_errno("getline");
3295 goto done;
3298 dirpath = strdup(path);
3299 if (dirpath == NULL) {
3300 err = got_error_from_errno("strdup");
3301 goto done;
3303 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3304 done:
3305 free(line);
3306 if (err || pe == NULL) {
3307 free(dirpath);
3308 free_ignorelist(ignorelist);
3310 return err;
3313 int
3314 match_ignores(struct got_pathlist_head *ignores, const char *path)
3316 struct got_pathlist_entry *pe;
3318 /* Handle patterns which match in all directories. */
3319 TAILQ_FOREACH(pe, ignores, entry) {
3320 struct got_pathlist_head *ignorelist = pe->data;
3321 struct got_pathlist_entry *pi;
3323 TAILQ_FOREACH(pi, ignorelist, entry) {
3324 const char *p, *pattern = pi->path;
3326 if (strncmp(pattern, "**/", 3) != 0)
3327 continue;
3328 pattern += 3;
3329 p = path;
3330 while (*p) {
3331 if (fnmatch(pattern, p,
3332 FNM_PATHNAME | FNM_LEADING_DIR)) {
3333 /* Retry in next directory. */
3334 while (*p && *p != '/')
3335 p++;
3336 while (*p == '/')
3337 p++;
3338 continue;
3340 return 1;
3346 * The ignores pathlist contains ignore lists from children before
3347 * parents, so we can find the most specific ignorelist by walking
3348 * ignores backwards.
3350 pe = TAILQ_LAST(ignores, got_pathlist_head);
3351 while (pe) {
3352 if (got_path_is_child(path, pe->path, pe->path_len)) {
3353 struct got_pathlist_head *ignorelist = pe->data;
3354 struct got_pathlist_entry *pi;
3355 TAILQ_FOREACH(pi, ignorelist, entry) {
3356 const char *pattern = pi->path;
3357 int flags = FNM_LEADING_DIR;
3358 if (strstr(pattern, "/**/") == NULL)
3359 flags |= FNM_PATHNAME;
3360 if (fnmatch(pattern, path, flags))
3361 continue;
3362 return 1;
3365 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3368 return 0;
3371 static const struct got_error *
3372 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3373 const char *path, int dirfd, const char *ignores_filename)
3375 const struct got_error *err = NULL;
3376 char *ignorespath;
3377 int fd = -1;
3378 FILE *ignoresfile = NULL;
3380 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3381 path[0] ? "/" : "", ignores_filename) == -1)
3382 return got_error_from_errno("asprintf");
3384 if (dirfd != -1) {
3385 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3386 if (fd == -1) {
3387 if (errno != ENOENT && errno != EACCES)
3388 err = got_error_from_errno2("openat",
3389 ignorespath);
3390 } else {
3391 ignoresfile = fdopen(fd, "r");
3392 if (ignoresfile == NULL)
3393 err = got_error_from_errno2("fdopen",
3394 ignorespath);
3395 else {
3396 fd = -1;
3397 err = read_ignores(ignores, path, ignoresfile);
3400 } else {
3401 ignoresfile = fopen(ignorespath, "r");
3402 if (ignoresfile == NULL) {
3403 if (errno != ENOENT && errno != EACCES)
3404 err = got_error_from_errno2("fopen",
3405 ignorespath);
3406 } else
3407 err = read_ignores(ignores, path, ignoresfile);
3410 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3411 err = got_error_from_errno2("fclose", path);
3412 if (fd != -1 && close(fd) == -1 && err == NULL)
3413 err = got_error_from_errno2("close", path);
3414 free(ignorespath);
3415 return err;
3418 static const struct got_error *
3419 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3421 const struct got_error *err = NULL;
3422 struct diff_dir_cb_arg *a = arg;
3423 char *path = NULL;
3425 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3426 return got_error(GOT_ERR_CANCELLED);
3428 if (parent_path[0]) {
3429 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3430 return got_error_from_errno("asprintf");
3431 } else {
3432 path = de->d_name;
3435 if (de->d_type != DT_DIR &&
3436 got_path_is_child(path, a->status_path, a->status_path_len)
3437 && !match_ignores(&a->ignores, path))
3438 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3439 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3440 if (parent_path[0])
3441 free(path);
3442 return err;
3445 static const struct got_error *
3446 status_traverse(void *arg, const char *path, int dirfd)
3448 const struct got_error *err = NULL;
3449 struct diff_dir_cb_arg *a = arg;
3451 if (a->no_ignores)
3452 return NULL;
3454 err = add_ignores(&a->ignores, a->worktree->root_path,
3455 path, dirfd, ".cvsignore");
3456 if (err)
3457 return err;
3459 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3460 dirfd, ".gitignore");
3462 return err;
3465 static const struct got_error *
3466 report_single_file_status(const char *path, const char *ondisk_path,
3467 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3468 void *status_arg, struct got_repository *repo, int report_unchanged)
3470 struct got_fileindex_entry *ie;
3471 struct stat sb;
3473 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3474 if (ie)
3475 return report_file_status(ie, ondisk_path, -1, NULL,
3476 status_cb, status_arg, repo, report_unchanged);
3478 if (lstat(ondisk_path, &sb) == -1) {
3479 if (errno != ENOENT)
3480 return got_error_from_errno2("lstat", ondisk_path);
3481 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3482 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3483 return NULL;
3486 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3487 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3488 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3490 return NULL;
3493 static const struct got_error *
3494 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3495 const char *root_path, const char *path)
3497 const struct got_error *err;
3498 char *parent_path, *next_parent_path = NULL;
3500 err = add_ignores(ignores, root_path, "", -1,
3501 ".cvsignore");
3502 if (err)
3503 return err;
3505 err = add_ignores(ignores, root_path, "", -1,
3506 ".gitignore");
3507 if (err)
3508 return err;
3510 err = got_path_dirname(&parent_path, path);
3511 if (err) {
3512 if (err->code == GOT_ERR_BAD_PATH)
3513 return NULL; /* cannot traverse parent */
3514 return err;
3516 for (;;) {
3517 err = add_ignores(ignores, root_path, parent_path, -1,
3518 ".cvsignore");
3519 if (err)
3520 break;
3521 err = add_ignores(ignores, root_path, parent_path, -1,
3522 ".gitignore");
3523 if (err)
3524 break;
3525 err = got_path_dirname(&next_parent_path, parent_path);
3526 if (err) {
3527 if (err->code == GOT_ERR_BAD_PATH)
3528 err = NULL; /* traversed everything */
3529 break;
3531 free(parent_path);
3532 parent_path = next_parent_path;
3533 next_parent_path = NULL;
3536 free(parent_path);
3537 free(next_parent_path);
3538 return err;
3541 static const struct got_error *
3542 worktree_status(struct got_worktree *worktree, const char *path,
3543 struct got_fileindex *fileindex, struct got_repository *repo,
3544 got_worktree_status_cb status_cb, void *status_arg,
3545 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3546 int report_unchanged)
3548 const struct got_error *err = NULL;
3549 int fd = -1;
3550 struct got_fileindex_diff_dir_cb fdiff_cb;
3551 struct diff_dir_cb_arg arg;
3552 char *ondisk_path = NULL;
3554 TAILQ_INIT(&arg.ignores);
3556 if (asprintf(&ondisk_path, "%s%s%s",
3557 worktree->root_path, path[0] ? "/" : "", path) == -1)
3558 return got_error_from_errno("asprintf");
3560 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3561 if (fd == -1) {
3562 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3563 errno != ELOOP)
3564 err = got_error_from_errno2("open", ondisk_path);
3565 else
3566 err = report_single_file_status(path, ondisk_path,
3567 fileindex, status_cb, status_arg, repo,
3568 report_unchanged);
3569 } else {
3570 fdiff_cb.diff_old_new = status_old_new;
3571 fdiff_cb.diff_old = status_old;
3572 fdiff_cb.diff_new = status_new;
3573 fdiff_cb.diff_traverse = status_traverse;
3574 arg.fileindex = fileindex;
3575 arg.worktree = worktree;
3576 arg.status_path = path;
3577 arg.status_path_len = strlen(path);
3578 arg.repo = repo;
3579 arg.status_cb = status_cb;
3580 arg.status_arg = status_arg;
3581 arg.cancel_cb = cancel_cb;
3582 arg.cancel_arg = cancel_arg;
3583 arg.report_unchanged = report_unchanged;
3584 arg.no_ignores = no_ignores;
3585 if (!no_ignores) {
3586 err = add_ignores_from_parent_paths(&arg.ignores,
3587 worktree->root_path, path);
3588 if (err)
3589 goto done;
3591 err = got_fileindex_diff_dir(fileindex, fd,
3592 worktree->root_path, path, repo, &fdiff_cb, &arg);
3594 done:
3595 free_ignores(&arg.ignores);
3596 if (fd != -1 && close(fd) != 0 && err == NULL)
3597 err = got_error_from_errno("close");
3598 free(ondisk_path);
3599 return err;
3602 const struct got_error *
3603 got_worktree_status(struct got_worktree *worktree,
3604 struct got_pathlist_head *paths, struct got_repository *repo,
3605 got_worktree_status_cb status_cb, void *status_arg,
3606 got_cancel_cb cancel_cb, void *cancel_arg)
3608 const struct got_error *err = NULL;
3609 char *fileindex_path = NULL;
3610 struct got_fileindex *fileindex = NULL;
3611 struct got_pathlist_entry *pe;
3613 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3614 if (err)
3615 return err;
3617 TAILQ_FOREACH(pe, paths, entry) {
3618 err = worktree_status(worktree, pe->path, fileindex, repo,
3619 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3620 if (err)
3621 break;
3623 free(fileindex_path);
3624 got_fileindex_free(fileindex);
3625 return err;
3628 const struct got_error *
3629 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3630 const char *arg)
3632 const struct got_error *err = NULL;
3633 char *resolved = NULL, *cwd = NULL, *path = NULL;
3634 size_t len;
3635 struct stat sb;
3636 char *abspath = NULL;
3637 char canonpath[PATH_MAX];
3639 *wt_path = NULL;
3641 cwd = getcwd(NULL, 0);
3642 if (cwd == NULL)
3643 return got_error_from_errno("getcwd");
3645 if (lstat(arg, &sb) == -1) {
3646 if (errno != ENOENT) {
3647 err = got_error_from_errno2("lstat", arg);
3648 goto done;
3650 sb.st_mode = 0;
3652 if (S_ISLNK(sb.st_mode)) {
3654 * We cannot use realpath(3) with symlinks since we want to
3655 * operate on the symlink itself.
3656 * But we can make the path absolute, assuming it is relative
3657 * to the current working directory, and then canonicalize it.
3659 if (!got_path_is_absolute(arg)) {
3660 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3661 err = got_error_from_errno("asprintf");
3662 goto done;
3666 err = got_canonpath(abspath ? abspath : arg, canonpath,
3667 sizeof(canonpath));
3668 if (err)
3669 goto done;
3670 resolved = strdup(canonpath);
3671 if (resolved == NULL) {
3672 err = got_error_from_errno("strdup");
3673 goto done;
3675 } else {
3676 resolved = realpath(arg, NULL);
3677 if (resolved == NULL) {
3678 if (errno != ENOENT) {
3679 err = got_error_from_errno2("realpath", arg);
3680 goto done;
3682 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3683 err = got_error_from_errno("asprintf");
3684 goto done;
3686 err = got_canonpath(abspath, canonpath,
3687 sizeof(canonpath));
3688 if (err)
3689 goto done;
3690 resolved = strdup(canonpath);
3691 if (resolved == NULL) {
3692 err = got_error_from_errno("strdup");
3693 goto done;
3698 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3699 strlen(got_worktree_get_root_path(worktree)))) {
3700 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3701 goto done;
3704 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3705 err = got_path_skip_common_ancestor(&path,
3706 got_worktree_get_root_path(worktree), resolved);
3707 if (err)
3708 goto done;
3709 } else {
3710 path = strdup("");
3711 if (path == NULL) {
3712 err = got_error_from_errno("strdup");
3713 goto done;
3717 /* XXX status walk can't deal with trailing slash! */
3718 len = strlen(path);
3719 while (len > 0 && path[len - 1] == '/') {
3720 path[len - 1] = '\0';
3721 len--;
3723 done:
3724 free(abspath);
3725 free(resolved);
3726 free(cwd);
3727 if (err == NULL)
3728 *wt_path = path;
3729 else
3730 free(path);
3731 return err;
3734 struct schedule_addition_args {
3735 struct got_worktree *worktree;
3736 struct got_fileindex *fileindex;
3737 got_worktree_checkout_cb progress_cb;
3738 void *progress_arg;
3739 struct got_repository *repo;
3742 static const struct got_error *
3743 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3744 const char *relpath, struct got_object_id *blob_id,
3745 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3746 int dirfd, const char *de_name)
3748 struct schedule_addition_args *a = arg;
3749 const struct got_error *err = NULL;
3750 struct got_fileindex_entry *ie;
3751 struct stat sb;
3752 char *ondisk_path;
3754 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3755 relpath) == -1)
3756 return got_error_from_errno("asprintf");
3758 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3759 if (ie) {
3760 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3761 de_name, a->repo);
3762 if (err)
3763 goto done;
3764 /* Re-adding an existing entry is a no-op. */
3765 if (status == GOT_STATUS_ADD)
3766 goto done;
3767 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3768 if (err)
3769 goto done;
3772 if (status != GOT_STATUS_UNVERSIONED) {
3773 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3774 goto done;
3777 err = got_fileindex_entry_alloc(&ie, relpath);
3778 if (err)
3779 goto done;
3780 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3781 if (err) {
3782 got_fileindex_entry_free(ie);
3783 goto done;
3785 err = got_fileindex_entry_add(a->fileindex, ie);
3786 if (err) {
3787 got_fileindex_entry_free(ie);
3788 goto done;
3790 done:
3791 free(ondisk_path);
3792 if (err)
3793 return err;
3794 if (status == GOT_STATUS_ADD)
3795 return NULL;
3796 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3799 const struct got_error *
3800 got_worktree_schedule_add(struct got_worktree *worktree,
3801 struct got_pathlist_head *paths,
3802 got_worktree_checkout_cb progress_cb, void *progress_arg,
3803 struct got_repository *repo, int no_ignores)
3805 struct got_fileindex *fileindex = NULL;
3806 char *fileindex_path = NULL;
3807 const struct got_error *err = NULL, *sync_err, *unlockerr;
3808 struct got_pathlist_entry *pe;
3809 struct schedule_addition_args saa;
3811 err = lock_worktree(worktree, LOCK_EX);
3812 if (err)
3813 return err;
3815 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3816 if (err)
3817 goto done;
3819 saa.worktree = worktree;
3820 saa.fileindex = fileindex;
3821 saa.progress_cb = progress_cb;
3822 saa.progress_arg = progress_arg;
3823 saa.repo = repo;
3825 TAILQ_FOREACH(pe, paths, entry) {
3826 err = worktree_status(worktree, pe->path, fileindex, repo,
3827 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3828 if (err)
3829 break;
3831 sync_err = sync_fileindex(fileindex, fileindex_path);
3832 if (sync_err && err == NULL)
3833 err = sync_err;
3834 done:
3835 free(fileindex_path);
3836 if (fileindex)
3837 got_fileindex_free(fileindex);
3838 unlockerr = lock_worktree(worktree, LOCK_SH);
3839 if (unlockerr && err == NULL)
3840 err = unlockerr;
3841 return err;
3844 struct schedule_deletion_args {
3845 struct got_worktree *worktree;
3846 struct got_fileindex *fileindex;
3847 got_worktree_delete_cb progress_cb;
3848 void *progress_arg;
3849 struct got_repository *repo;
3850 int delete_local_mods;
3851 int keep_on_disk;
3852 const char *status_codes;
3855 static const struct got_error *
3856 schedule_for_deletion(void *arg, unsigned char status,
3857 unsigned char staged_status, const char *relpath,
3858 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3859 struct got_object_id *commit_id, int dirfd, const char *de_name)
3861 struct schedule_deletion_args *a = arg;
3862 const struct got_error *err = NULL;
3863 struct got_fileindex_entry *ie = NULL;
3864 struct stat sb;
3865 char *ondisk_path;
3867 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3868 if (ie == NULL)
3869 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3871 staged_status = get_staged_status(ie);
3872 if (staged_status != GOT_STATUS_NO_CHANGE) {
3873 if (staged_status == GOT_STATUS_DELETE)
3874 return NULL;
3875 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3879 relpath) == -1)
3880 return got_error_from_errno("asprintf");
3882 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3883 a->repo);
3884 if (err)
3885 goto done;
3887 if (a->status_codes) {
3888 size_t ncodes = strlen(a->status_codes);
3889 int i;
3890 for (i = 0; i < ncodes ; i++) {
3891 if (status == a->status_codes[i])
3892 break;
3894 if (i == ncodes) {
3895 /* Do not delete files in non-matching status. */
3896 free(ondisk_path);
3897 return NULL;
3899 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3900 a->status_codes[i] != GOT_STATUS_MISSING) {
3901 static char msg[64];
3902 snprintf(msg, sizeof(msg),
3903 "invalid status code '%c'", a->status_codes[i]);
3904 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3905 goto done;
3909 if (status != GOT_STATUS_NO_CHANGE) {
3910 if (status == GOT_STATUS_DELETE)
3911 goto done;
3912 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3913 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3914 goto done;
3916 if (status != GOT_STATUS_MODIFY &&
3917 status != GOT_STATUS_MISSING) {
3918 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3919 goto done;
3923 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3924 size_t root_len;
3926 if (dirfd != -1) {
3927 if (unlinkat(dirfd, de_name, 0) != 0) {
3928 err = got_error_from_errno2("unlinkat",
3929 ondisk_path);
3930 goto done;
3932 } else if (unlink(ondisk_path) != 0) {
3933 err = got_error_from_errno2("unlink", ondisk_path);
3934 goto done;
3937 root_len = strlen(a->worktree->root_path);
3938 do {
3939 char *parent;
3940 err = got_path_dirname(&parent, ondisk_path);
3941 if (err)
3942 goto done;
3943 free(ondisk_path);
3944 ondisk_path = parent;
3945 if (rmdir(ondisk_path) == -1) {
3946 if (errno != ENOTEMPTY)
3947 err = got_error_from_errno2("rmdir",
3948 ondisk_path);
3949 break;
3951 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3952 strlen(ondisk_path), root_len) != 0);
3955 got_fileindex_entry_mark_deleted_from_disk(ie);
3956 done:
3957 free(ondisk_path);
3958 if (err)
3959 return err;
3960 if (status == GOT_STATUS_DELETE)
3961 return NULL;
3962 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3963 staged_status, relpath);
3966 const struct got_error *
3967 got_worktree_schedule_delete(struct got_worktree *worktree,
3968 struct got_pathlist_head *paths, int delete_local_mods,
3969 const char *status_codes,
3970 got_worktree_delete_cb progress_cb, void *progress_arg,
3971 struct got_repository *repo, int keep_on_disk)
3973 struct got_fileindex *fileindex = NULL;
3974 char *fileindex_path = NULL;
3975 const struct got_error *err = NULL, *sync_err, *unlockerr;
3976 struct got_pathlist_entry *pe;
3977 struct schedule_deletion_args sda;
3979 err = lock_worktree(worktree, LOCK_EX);
3980 if (err)
3981 return err;
3983 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3984 if (err)
3985 goto done;
3987 sda.worktree = worktree;
3988 sda.fileindex = fileindex;
3989 sda.progress_cb = progress_cb;
3990 sda.progress_arg = progress_arg;
3991 sda.repo = repo;
3992 sda.delete_local_mods = delete_local_mods;
3993 sda.keep_on_disk = keep_on_disk;
3994 sda.status_codes = status_codes;
3996 TAILQ_FOREACH(pe, paths, entry) {
3997 err = worktree_status(worktree, pe->path, fileindex, repo,
3998 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3999 if (err)
4000 break;
4002 sync_err = sync_fileindex(fileindex, fileindex_path);
4003 if (sync_err && err == NULL)
4004 err = sync_err;
4005 done:
4006 free(fileindex_path);
4007 if (fileindex)
4008 got_fileindex_free(fileindex);
4009 unlockerr = lock_worktree(worktree, LOCK_SH);
4010 if (unlockerr && err == NULL)
4011 err = unlockerr;
4012 return err;
4015 static const struct got_error *
4016 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4018 const struct got_error *err = NULL;
4019 char *line = NULL;
4020 size_t linesize = 0, n;
4021 ssize_t linelen;
4023 linelen = getline(&line, &linesize, infile);
4024 if (linelen == -1) {
4025 if (ferror(infile)) {
4026 err = got_error_from_errno("getline");
4027 goto done;
4029 return NULL;
4031 if (outfile) {
4032 n = fwrite(line, 1, linelen, outfile);
4033 if (n != linelen) {
4034 err = got_ferror(outfile, GOT_ERR_IO);
4035 goto done;
4038 if (rejectfile) {
4039 n = fwrite(line, 1, linelen, rejectfile);
4040 if (n != linelen)
4041 err = got_ferror(outfile, GOT_ERR_IO);
4043 done:
4044 free(line);
4045 return err;
4048 static const struct got_error *
4049 skip_one_line(FILE *f)
4051 char *line = NULL;
4052 size_t linesize = 0;
4053 ssize_t linelen;
4055 linelen = getline(&line, &linesize, f);
4056 if (linelen == -1) {
4057 if (ferror(f))
4058 return got_error_from_errno("getline");
4059 return NULL;
4061 free(line);
4062 return NULL;
4065 static const struct got_error *
4066 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4067 int start_old, int end_old, int start_new, int end_new,
4068 FILE *outfile, FILE *rejectfile)
4070 const struct got_error *err;
4072 /* Copy old file's lines leading up to patch. */
4073 while (!feof(f1) && *line_cur1 < start_old) {
4074 err = copy_one_line(f1, outfile, NULL);
4075 if (err)
4076 return err;
4077 (*line_cur1)++;
4079 /* Skip new file's lines leading up to patch. */
4080 while (!feof(f2) && *line_cur2 < start_new) {
4081 if (rejectfile)
4082 err = copy_one_line(f2, NULL, rejectfile);
4083 else
4084 err = skip_one_line(f2);
4085 if (err)
4086 return err;
4087 (*line_cur2)++;
4089 /* Copy patched lines. */
4090 while (!feof(f2) && *line_cur2 <= end_new) {
4091 err = copy_one_line(f2, outfile, NULL);
4092 if (err)
4093 return err;
4094 (*line_cur2)++;
4096 /* Skip over old file's replaced lines. */
4097 while (!feof(f1) && *line_cur1 <= end_old) {
4098 if (rejectfile)
4099 err = copy_one_line(f1, NULL, rejectfile);
4100 else
4101 err = skip_one_line(f1);
4102 if (err)
4103 return err;
4104 (*line_cur1)++;
4107 return NULL;
4110 static const struct got_error *
4111 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4112 FILE *outfile, FILE *rejectfile)
4114 const struct got_error *err;
4116 if (outfile) {
4117 /* Copy old file's lines until EOF. */
4118 while (!feof(f1)) {
4119 err = copy_one_line(f1, outfile, NULL);
4120 if (err)
4121 return err;
4122 (*line_cur1)++;
4125 if (rejectfile) {
4126 /* Copy new file's lines until EOF. */
4127 while (!feof(f2)) {
4128 err = copy_one_line(f2, NULL, rejectfile);
4129 if (err)
4130 return err;
4131 (*line_cur2)++;
4135 return NULL;
4138 static const struct got_error *
4139 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4140 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4141 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4142 int *line_cur2, FILE *outfile, FILE *rejectfile,
4143 got_worktree_patch_cb patch_cb, void *patch_arg)
4145 const struct got_error *err = NULL;
4146 int start_old = change->cv.a;
4147 int end_old = change->cv.b;
4148 int start_new = change->cv.c;
4149 int end_new = change->cv.d;
4150 long pos1, pos2;
4151 FILE *hunkfile;
4153 *choice = GOT_PATCH_CHOICE_NONE;
4155 hunkfile = got_opentemp();
4156 if (hunkfile == NULL)
4157 return got_error_from_errno("got_opentemp");
4159 pos1 = ftell(f1);
4160 pos2 = ftell(f2);
4162 /* XXX TODO needs error checking */
4163 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4165 if (fseek(f1, pos1, SEEK_SET) == -1) {
4166 err = got_ferror(f1, GOT_ERR_IO);
4167 goto done;
4169 if (fseek(f2, pos2, SEEK_SET) == -1) {
4170 err = got_ferror(f1, GOT_ERR_IO);
4171 goto done;
4173 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4174 err = got_ferror(hunkfile, GOT_ERR_IO);
4175 goto done;
4178 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4179 hunkfile, n, nchanges);
4180 if (err)
4181 goto done;
4183 switch (*choice) {
4184 case GOT_PATCH_CHOICE_YES:
4185 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4186 end_old, start_new, end_new, outfile, rejectfile);
4187 break;
4188 case GOT_PATCH_CHOICE_NO:
4189 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4190 end_old, start_new, end_new, rejectfile, outfile);
4191 break;
4192 case GOT_PATCH_CHOICE_QUIT:
4193 break;
4194 default:
4195 err = got_error(GOT_ERR_PATCH_CHOICE);
4196 break;
4198 done:
4199 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4200 err = got_error_from_errno("fclose");
4201 return err;
4204 struct revert_file_args {
4205 struct got_worktree *worktree;
4206 struct got_fileindex *fileindex;
4207 got_worktree_checkout_cb progress_cb;
4208 void *progress_arg;
4209 got_worktree_patch_cb patch_cb;
4210 void *patch_arg;
4211 struct got_repository *repo;
4214 static const struct got_error *
4215 create_patched_content(char **path_outfile, int reverse_patch,
4216 struct got_object_id *blob_id, const char *path2,
4217 int dirfd2, const char *de_name2,
4218 const char *relpath, struct got_repository *repo,
4219 got_worktree_patch_cb patch_cb, void *patch_arg)
4221 const struct got_error *err;
4222 struct got_blob_object *blob = NULL;
4223 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4224 int fd2 = -1;
4225 char link_target[PATH_MAX];
4226 ssize_t link_len = 0;
4227 char *path1 = NULL, *id_str = NULL;
4228 struct stat sb1, sb2;
4229 struct got_diff_changes *changes = NULL;
4230 struct got_diff_state *ds = NULL;
4231 struct got_diff_args *args = NULL;
4232 struct got_diff_change *change;
4233 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4234 int n = 0;
4236 *path_outfile = NULL;
4238 err = got_object_id_str(&id_str, blob_id);
4239 if (err)
4240 return err;
4242 if (dirfd2 != -1) {
4243 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4244 if (fd2 == -1) {
4245 if (errno != ELOOP) {
4246 err = got_error_from_errno2("openat", path2);
4247 goto done;
4249 link_len = readlinkat(dirfd2, de_name2,
4250 link_target, sizeof(link_target));
4251 if (link_len == -1)
4252 return got_error_from_errno2("readlinkat", path2);
4253 sb2.st_mode = S_IFLNK;
4254 sb2.st_size = link_len;
4256 } else {
4257 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4258 if (fd2 == -1) {
4259 if (errno != ELOOP) {
4260 err = got_error_from_errno2("open", path2);
4261 goto done;
4263 link_len = readlink(path2, link_target,
4264 sizeof(link_target));
4265 if (link_len == -1)
4266 return got_error_from_errno2("readlink", path2);
4267 sb2.st_mode = S_IFLNK;
4268 sb2.st_size = link_len;
4271 if (fd2 != -1) {
4272 if (fstat(fd2, &sb2) == -1) {
4273 err = got_error_from_errno2("fstat", path2);
4274 goto done;
4277 f2 = fdopen(fd2, "r");
4278 if (f2 == NULL) {
4279 err = got_error_from_errno2("fdopen", path2);
4280 goto done;
4282 fd2 = -1;
4283 } else {
4284 size_t n;
4285 f2 = got_opentemp();
4286 if (f2 == NULL) {
4287 err = got_error_from_errno2("got_opentemp", path2);
4288 goto done;
4290 n = fwrite(link_target, 1, link_len, f2);
4291 if (n != link_len) {
4292 err = got_ferror(f2, GOT_ERR_IO);
4293 goto done;
4295 if (fflush(f2) == EOF) {
4296 err = got_error_from_errno("fflush");
4297 goto done;
4299 rewind(f2);
4302 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4303 if (err)
4304 goto done;
4306 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4307 if (err)
4308 goto done;
4310 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4311 if (err)
4312 goto done;
4314 if (stat(path1, &sb1) == -1) {
4315 err = got_error_from_errno2("stat", path1);
4316 goto done;
4319 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4320 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4321 if (err)
4322 goto done;
4324 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4325 if (err)
4326 goto done;
4328 if (fseek(f1, 0L, SEEK_SET) == -1)
4329 return got_ferror(f1, GOT_ERR_IO);
4330 if (fseek(f2, 0L, SEEK_SET) == -1)
4331 return got_ferror(f2, GOT_ERR_IO);
4332 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4333 int choice;
4334 err = apply_or_reject_change(&choice, change, ++n,
4335 changes->nchanges, ds, args, diff_flags, relpath,
4336 f1, f2, &line_cur1, &line_cur2,
4337 reverse_patch ? NULL : outfile,
4338 reverse_patch ? outfile : NULL,
4339 patch_cb, patch_arg);
4340 if (err)
4341 goto done;
4342 if (choice == GOT_PATCH_CHOICE_YES)
4343 have_content = 1;
4344 else if (choice == GOT_PATCH_CHOICE_QUIT)
4345 break;
4347 if (have_content) {
4348 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4349 reverse_patch ? NULL : outfile,
4350 reverse_patch ? outfile : NULL);
4351 if (err)
4352 goto done;
4354 if (!S_ISLNK(sb2.st_mode)) {
4355 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4356 err = got_error_from_errno2("fchmod", path2);
4357 goto done;
4361 done:
4362 free(id_str);
4363 if (blob)
4364 got_object_blob_close(blob);
4365 if (f1 && fclose(f1) == EOF && err == NULL)
4366 err = got_error_from_errno2("fclose", path1);
4367 if (f2 && fclose(f2) == EOF && err == NULL)
4368 err = got_error_from_errno2("fclose", path2);
4369 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4370 err = got_error_from_errno2("close", path2);
4371 if (outfile && fclose(outfile) == EOF && err == NULL)
4372 err = got_error_from_errno2("fclose", *path_outfile);
4373 if (path1 && unlink(path1) == -1 && err == NULL)
4374 err = got_error_from_errno2("unlink", path1);
4375 if (err || !have_content) {
4376 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4377 err = got_error_from_errno2("unlink", *path_outfile);
4378 free(*path_outfile);
4379 *path_outfile = NULL;
4381 free(args);
4382 if (ds) {
4383 got_diff_state_free(ds);
4384 free(ds);
4386 if (changes)
4387 got_diff_free_changes(changes);
4388 free(path1);
4389 return err;
4392 static const struct got_error *
4393 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4394 const char *relpath, struct got_object_id *blob_id,
4395 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4396 int dirfd, const char *de_name)
4398 struct revert_file_args *a = arg;
4399 const struct got_error *err = NULL;
4400 char *parent_path = NULL;
4401 struct got_fileindex_entry *ie;
4402 struct got_tree_object *tree = NULL;
4403 struct got_object_id *tree_id = NULL;
4404 const struct got_tree_entry *te = NULL;
4405 char *tree_path = NULL, *te_name;
4406 char *ondisk_path = NULL, *path_content = NULL;
4407 struct got_blob_object *blob = NULL;
4409 /* Reverting a staged deletion is a no-op. */
4410 if (status == GOT_STATUS_DELETE &&
4411 staged_status != GOT_STATUS_NO_CHANGE)
4412 return NULL;
4414 if (status == GOT_STATUS_UNVERSIONED)
4415 return (*a->progress_cb)(a->progress_arg,
4416 GOT_STATUS_UNVERSIONED, relpath);
4418 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4419 if (ie == NULL)
4420 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4422 /* Construct in-repository path of tree which contains this blob. */
4423 err = got_path_dirname(&parent_path, ie->path);
4424 if (err) {
4425 if (err->code != GOT_ERR_BAD_PATH)
4426 goto done;
4427 parent_path = strdup("/");
4428 if (parent_path == NULL) {
4429 err = got_error_from_errno("strdup");
4430 goto done;
4433 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4434 tree_path = strdup(parent_path);
4435 if (tree_path == NULL) {
4436 err = got_error_from_errno("strdup");
4437 goto done;
4439 } else {
4440 if (got_path_is_root_dir(parent_path)) {
4441 tree_path = strdup(a->worktree->path_prefix);
4442 if (tree_path == NULL) {
4443 err = got_error_from_errno("strdup");
4444 goto done;
4446 } else {
4447 if (asprintf(&tree_path, "%s/%s",
4448 a->worktree->path_prefix, parent_path) == -1) {
4449 err = got_error_from_errno("asprintf");
4450 goto done;
4455 err = got_object_id_by_path(&tree_id, a->repo,
4456 a->worktree->base_commit_id, tree_path);
4457 if (err) {
4458 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4459 (status == GOT_STATUS_ADD ||
4460 staged_status == GOT_STATUS_ADD)))
4461 goto done;
4462 } else {
4463 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4464 if (err)
4465 goto done;
4467 err = got_path_basename(&te_name, ie->path);
4468 if (err)
4469 goto done;
4471 te = got_object_tree_find_entry(tree, te_name);
4472 free(te_name);
4473 if (te == NULL && status != GOT_STATUS_ADD &&
4474 staged_status != GOT_STATUS_ADD) {
4475 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4476 goto done;
4480 switch (status) {
4481 case GOT_STATUS_ADD:
4482 if (a->patch_cb) {
4483 int choice = GOT_PATCH_CHOICE_NONE;
4484 err = (*a->patch_cb)(&choice, a->patch_arg,
4485 status, ie->path, NULL, 1, 1);
4486 if (err)
4487 goto done;
4488 if (choice != GOT_PATCH_CHOICE_YES)
4489 break;
4491 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4492 ie->path);
4493 if (err)
4494 goto done;
4495 got_fileindex_entry_remove(a->fileindex, ie);
4496 break;
4497 case GOT_STATUS_DELETE:
4498 if (a->patch_cb) {
4499 int choice = GOT_PATCH_CHOICE_NONE;
4500 err = (*a->patch_cb)(&choice, a->patch_arg,
4501 status, ie->path, NULL, 1, 1);
4502 if (err)
4503 goto done;
4504 if (choice != GOT_PATCH_CHOICE_YES)
4505 break;
4507 /* fall through */
4508 case GOT_STATUS_MODIFY:
4509 case GOT_STATUS_MODE_CHANGE:
4510 case GOT_STATUS_CONFLICT:
4511 case GOT_STATUS_MISSING: {
4512 struct got_object_id id;
4513 if (staged_status == GOT_STATUS_ADD ||
4514 staged_status == GOT_STATUS_MODIFY) {
4515 memcpy(id.sha1, ie->staged_blob_sha1,
4516 SHA1_DIGEST_LENGTH);
4517 } else
4518 memcpy(id.sha1, ie->blob_sha1,
4519 SHA1_DIGEST_LENGTH);
4520 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4521 if (err)
4522 goto done;
4524 if (asprintf(&ondisk_path, "%s/%s",
4525 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4526 err = got_error_from_errno("asprintf");
4527 goto done;
4530 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4531 status == GOT_STATUS_CONFLICT)) {
4532 int is_bad_symlink = 0;
4533 err = create_patched_content(&path_content, 1, &id,
4534 ondisk_path, dirfd, de_name, ie->path, a->repo,
4535 a->patch_cb, a->patch_arg);
4536 if (err || path_content == NULL)
4537 break;
4538 if (te && S_ISLNK(te->mode)) {
4539 if (unlink(path_content) == -1) {
4540 err = got_error_from_errno2("unlink",
4541 path_content);
4542 break;
4544 err = install_symlink(&is_bad_symlink,
4545 a->worktree, ondisk_path, ie->path,
4546 blob, 0, 1, 0, a->repo,
4547 a->progress_cb, a->progress_arg);
4548 } else {
4549 if (rename(path_content, ondisk_path) == -1) {
4550 err = got_error_from_errno3("rename",
4551 path_content, ondisk_path);
4552 goto done;
4555 } else {
4556 int is_bad_symlink = 0;
4557 if (te && S_ISLNK(te->mode)) {
4558 err = install_symlink(&is_bad_symlink,
4559 a->worktree, ondisk_path, ie->path,
4560 blob, 0, 1, 0, a->repo,
4561 a->progress_cb, a->progress_arg);
4562 } else {
4563 err = install_blob(a->worktree, ondisk_path,
4564 ie->path,
4565 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4566 got_fileindex_perms_to_st(ie), blob,
4567 0, 1, 0, 0, a->repo,
4568 a->progress_cb, a->progress_arg);
4570 if (err)
4571 goto done;
4572 if (status == GOT_STATUS_DELETE ||
4573 status == GOT_STATUS_MODE_CHANGE) {
4574 err = got_fileindex_entry_update(ie,
4575 ondisk_path, blob->id.sha1,
4576 a->worktree->base_commit_id->sha1, 1);
4577 if (err)
4578 goto done;
4580 if (is_bad_symlink) {
4581 got_fileindex_entry_filetype_set(ie,
4582 GOT_FILEIDX_MODE_BAD_SYMLINK);
4585 break;
4587 default:
4588 break;
4590 done:
4591 free(ondisk_path);
4592 free(path_content);
4593 free(parent_path);
4594 free(tree_path);
4595 if (blob)
4596 got_object_blob_close(blob);
4597 if (tree)
4598 got_object_tree_close(tree);
4599 free(tree_id);
4600 return err;
4603 const struct got_error *
4604 got_worktree_revert(struct got_worktree *worktree,
4605 struct got_pathlist_head *paths,
4606 got_worktree_checkout_cb progress_cb, void *progress_arg,
4607 got_worktree_patch_cb patch_cb, void *patch_arg,
4608 struct got_repository *repo)
4610 struct got_fileindex *fileindex = NULL;
4611 char *fileindex_path = NULL;
4612 const struct got_error *err = NULL, *unlockerr = NULL;
4613 const struct got_error *sync_err = NULL;
4614 struct got_pathlist_entry *pe;
4615 struct revert_file_args rfa;
4617 err = lock_worktree(worktree, LOCK_EX);
4618 if (err)
4619 return err;
4621 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4622 if (err)
4623 goto done;
4625 rfa.worktree = worktree;
4626 rfa.fileindex = fileindex;
4627 rfa.progress_cb = progress_cb;
4628 rfa.progress_arg = progress_arg;
4629 rfa.patch_cb = patch_cb;
4630 rfa.patch_arg = patch_arg;
4631 rfa.repo = repo;
4632 TAILQ_FOREACH(pe, paths, entry) {
4633 err = worktree_status(worktree, pe->path, fileindex, repo,
4634 revert_file, &rfa, NULL, NULL, 0, 0);
4635 if (err)
4636 break;
4638 sync_err = sync_fileindex(fileindex, fileindex_path);
4639 if (sync_err && err == NULL)
4640 err = sync_err;
4641 done:
4642 free(fileindex_path);
4643 if (fileindex)
4644 got_fileindex_free(fileindex);
4645 unlockerr = lock_worktree(worktree, LOCK_SH);
4646 if (unlockerr && err == NULL)
4647 err = unlockerr;
4648 return err;
4651 static void
4652 free_commitable(struct got_commitable *ct)
4654 free(ct->path);
4655 free(ct->in_repo_path);
4656 free(ct->ondisk_path);
4657 free(ct->blob_id);
4658 free(ct->base_blob_id);
4659 free(ct->staged_blob_id);
4660 free(ct->base_commit_id);
4661 free(ct);
4664 struct collect_commitables_arg {
4665 struct got_pathlist_head *commitable_paths;
4666 struct got_repository *repo;
4667 struct got_worktree *worktree;
4668 struct got_fileindex *fileindex;
4669 int have_staged_files;
4670 int allow_bad_symlinks;
4673 static const struct got_error *
4674 collect_commitables(void *arg, unsigned char status,
4675 unsigned char staged_status, const char *relpath,
4676 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4677 struct got_object_id *commit_id, int dirfd, const char *de_name)
4679 struct collect_commitables_arg *a = arg;
4680 const struct got_error *err = NULL;
4681 struct got_commitable *ct = NULL;
4682 struct got_pathlist_entry *new = NULL;
4683 char *parent_path = NULL, *path = NULL;
4684 struct stat sb;
4686 if (a->have_staged_files) {
4687 if (staged_status != GOT_STATUS_MODIFY &&
4688 staged_status != GOT_STATUS_ADD &&
4689 staged_status != GOT_STATUS_DELETE)
4690 return NULL;
4691 } else {
4692 if (status == GOT_STATUS_CONFLICT)
4693 return got_error(GOT_ERR_COMMIT_CONFLICT);
4695 if (status != GOT_STATUS_MODIFY &&
4696 status != GOT_STATUS_MODE_CHANGE &&
4697 status != GOT_STATUS_ADD &&
4698 status != GOT_STATUS_DELETE)
4699 return NULL;
4702 if (asprintf(&path, "/%s", relpath) == -1) {
4703 err = got_error_from_errno("asprintf");
4704 goto done;
4706 if (strcmp(path, "/") == 0) {
4707 parent_path = strdup("");
4708 if (parent_path == NULL)
4709 return got_error_from_errno("strdup");
4710 } else {
4711 err = got_path_dirname(&parent_path, path);
4712 if (err)
4713 return err;
4716 ct = calloc(1, sizeof(*ct));
4717 if (ct == NULL) {
4718 err = got_error_from_errno("calloc");
4719 goto done;
4722 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4723 relpath) == -1) {
4724 err = got_error_from_errno("asprintf");
4725 goto done;
4728 if (staged_status == GOT_STATUS_ADD ||
4729 staged_status == GOT_STATUS_MODIFY) {
4730 struct got_fileindex_entry *ie;
4731 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4732 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4733 case GOT_FILEIDX_MODE_REGULAR_FILE:
4734 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4735 ct->mode = S_IFREG;
4736 break;
4737 case GOT_FILEIDX_MODE_SYMLINK:
4738 ct->mode = S_IFLNK;
4739 break;
4740 default:
4741 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4742 goto done;
4744 ct->mode |= got_fileindex_entry_perms_get(ie);
4745 } else if (status != GOT_STATUS_DELETE &&
4746 staged_status != GOT_STATUS_DELETE) {
4747 if (dirfd != -1) {
4748 if (fstatat(dirfd, de_name, &sb,
4749 AT_SYMLINK_NOFOLLOW) == -1) {
4750 err = got_error_from_errno2("fstatat",
4751 ct->ondisk_path);
4752 goto done;
4754 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4755 err = got_error_from_errno2("lstat", ct->ondisk_path);
4756 goto done;
4758 ct->mode = sb.st_mode;
4761 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4762 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4763 relpath) == -1) {
4764 err = got_error_from_errno("asprintf");
4765 goto done;
4768 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4769 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4770 int is_bad_symlink;
4771 char target_path[PATH_MAX];
4772 ssize_t target_len;
4773 target_len = readlink(ct->ondisk_path, target_path,
4774 sizeof(target_path));
4775 if (target_len == -1) {
4776 err = got_error_from_errno2("readlink",
4777 ct->ondisk_path);
4778 goto done;
4780 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4781 target_len, ct->ondisk_path, a->worktree->root_path);
4782 if (err)
4783 goto done;
4784 if (is_bad_symlink) {
4785 err = got_error_path(ct->ondisk_path,
4786 GOT_ERR_BAD_SYMLINK);
4787 goto done;
4792 ct->status = status;
4793 ct->staged_status = staged_status;
4794 ct->blob_id = NULL; /* will be filled in when blob gets created */
4795 if (ct->status != GOT_STATUS_ADD &&
4796 ct->staged_status != GOT_STATUS_ADD) {
4797 ct->base_blob_id = got_object_id_dup(blob_id);
4798 if (ct->base_blob_id == NULL) {
4799 err = got_error_from_errno("got_object_id_dup");
4800 goto done;
4802 ct->base_commit_id = got_object_id_dup(commit_id);
4803 if (ct->base_commit_id == NULL) {
4804 err = got_error_from_errno("got_object_id_dup");
4805 goto done;
4808 if (ct->staged_status == GOT_STATUS_ADD ||
4809 ct->staged_status == GOT_STATUS_MODIFY) {
4810 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4811 if (ct->staged_blob_id == NULL) {
4812 err = got_error_from_errno("got_object_id_dup");
4813 goto done;
4816 ct->path = strdup(path);
4817 if (ct->path == NULL) {
4818 err = got_error_from_errno("strdup");
4819 goto done;
4821 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4822 done:
4823 if (ct && (err || new == NULL))
4824 free_commitable(ct);
4825 free(parent_path);
4826 free(path);
4827 return err;
4830 static const struct got_error *write_tree(struct got_object_id **, int *,
4831 struct got_tree_object *, const char *, struct got_pathlist_head *,
4832 got_worktree_status_cb status_cb, void *status_arg,
4833 struct got_repository *);
4835 static const struct got_error *
4836 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4837 struct got_tree_entry *te, const char *parent_path,
4838 struct got_pathlist_head *commitable_paths,
4839 got_worktree_status_cb status_cb, void *status_arg,
4840 struct got_repository *repo)
4842 const struct got_error *err = NULL;
4843 struct got_tree_object *subtree;
4844 char *subpath;
4846 if (asprintf(&subpath, "%s%s%s", parent_path,
4847 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4848 return got_error_from_errno("asprintf");
4850 err = got_object_open_as_tree(&subtree, repo, &te->id);
4851 if (err)
4852 return err;
4854 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4855 commitable_paths, status_cb, status_arg, repo);
4856 got_object_tree_close(subtree);
4857 free(subpath);
4858 return err;
4861 static const struct got_error *
4862 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4864 const struct got_error *err = NULL;
4865 char *ct_parent_path = NULL;
4867 *match = 0;
4869 if (strchr(ct->in_repo_path, '/') == NULL) {
4870 *match = got_path_is_root_dir(path);
4871 return NULL;
4874 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4875 if (err)
4876 return err;
4877 *match = (strcmp(path, ct_parent_path) == 0);
4878 free(ct_parent_path);
4879 return err;
4882 static mode_t
4883 get_ct_file_mode(struct got_commitable *ct)
4885 if (S_ISLNK(ct->mode))
4886 return S_IFLNK;
4888 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4891 static const struct got_error *
4892 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4893 struct got_tree_entry *te, struct got_commitable *ct)
4895 const struct got_error *err = NULL;
4897 *new_te = NULL;
4899 err = got_object_tree_entry_dup(new_te, te);
4900 if (err)
4901 goto done;
4903 (*new_te)->mode = get_ct_file_mode(ct);
4905 if (ct->staged_status == GOT_STATUS_MODIFY)
4906 memcpy(&(*new_te)->id, ct->staged_blob_id,
4907 sizeof((*new_te)->id));
4908 else
4909 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4910 done:
4911 if (err && *new_te) {
4912 free(*new_te);
4913 *new_te = NULL;
4915 return err;
4918 static const struct got_error *
4919 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4920 struct got_commitable *ct)
4922 const struct got_error *err = NULL;
4923 char *ct_name = NULL;
4925 *new_te = NULL;
4927 *new_te = calloc(1, sizeof(**new_te));
4928 if (*new_te == NULL)
4929 return got_error_from_errno("calloc");
4931 err = got_path_basename(&ct_name, ct->path);
4932 if (err)
4933 goto done;
4934 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4935 sizeof((*new_te)->name)) {
4936 err = got_error(GOT_ERR_NO_SPACE);
4937 goto done;
4940 (*new_te)->mode = get_ct_file_mode(ct);
4942 if (ct->staged_status == GOT_STATUS_ADD)
4943 memcpy(&(*new_te)->id, ct->staged_blob_id,
4944 sizeof((*new_te)->id));
4945 else
4946 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4947 done:
4948 free(ct_name);
4949 if (err && *new_te) {
4950 free(*new_te);
4951 *new_te = NULL;
4953 return err;
4956 static const struct got_error *
4957 insert_tree_entry(struct got_tree_entry *new_te,
4958 struct got_pathlist_head *paths)
4960 const struct got_error *err = NULL;
4961 struct got_pathlist_entry *new_pe;
4963 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4964 if (err)
4965 return err;
4966 if (new_pe == NULL)
4967 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4968 return NULL;
4971 static const struct got_error *
4972 report_ct_status(struct got_commitable *ct,
4973 got_worktree_status_cb status_cb, void *status_arg)
4975 const char *ct_path = ct->path;
4976 unsigned char status;
4978 while (ct_path[0] == '/')
4979 ct_path++;
4981 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4982 status = ct->staged_status;
4983 else
4984 status = ct->status;
4986 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4987 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4990 static const struct got_error *
4991 match_modified_subtree(int *modified, struct got_tree_entry *te,
4992 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4994 const struct got_error *err = NULL;
4995 struct got_pathlist_entry *pe;
4996 char *te_path;
4998 *modified = 0;
5000 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5001 got_path_is_root_dir(base_tree_path) ? "" : "/",
5002 te->name) == -1)
5003 return got_error_from_errno("asprintf");
5005 TAILQ_FOREACH(pe, commitable_paths, entry) {
5006 struct got_commitable *ct = pe->data;
5007 *modified = got_path_is_child(ct->in_repo_path, te_path,
5008 strlen(te_path));
5009 if (*modified)
5010 break;
5013 free(te_path);
5014 return err;
5017 static const struct got_error *
5018 match_deleted_or_modified_ct(struct got_commitable **ctp,
5019 struct got_tree_entry *te, const char *base_tree_path,
5020 struct got_pathlist_head *commitable_paths)
5022 const struct got_error *err = NULL;
5023 struct got_pathlist_entry *pe;
5025 *ctp = NULL;
5027 TAILQ_FOREACH(pe, commitable_paths, entry) {
5028 struct got_commitable *ct = pe->data;
5029 char *ct_name = NULL;
5030 int path_matches;
5032 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5033 if (ct->status != GOT_STATUS_MODIFY &&
5034 ct->status != GOT_STATUS_MODE_CHANGE &&
5035 ct->status != GOT_STATUS_DELETE)
5036 continue;
5037 } else {
5038 if (ct->staged_status != GOT_STATUS_MODIFY &&
5039 ct->staged_status != GOT_STATUS_DELETE)
5040 continue;
5043 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5044 continue;
5046 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5047 if (err)
5048 return err;
5049 if (!path_matches)
5050 continue;
5052 err = got_path_basename(&ct_name, pe->path);
5053 if (err)
5054 return err;
5056 if (strcmp(te->name, ct_name) != 0) {
5057 free(ct_name);
5058 continue;
5060 free(ct_name);
5062 *ctp = ct;
5063 break;
5066 return err;
5069 static const struct got_error *
5070 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5071 const char *child_path, const char *path_base_tree,
5072 struct got_pathlist_head *commitable_paths,
5073 got_worktree_status_cb status_cb, void *status_arg,
5074 struct got_repository *repo)
5076 const struct got_error *err = NULL;
5077 struct got_tree_entry *new_te;
5078 char *subtree_path;
5079 struct got_object_id *id = NULL;
5080 int nentries;
5082 *new_tep = NULL;
5084 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5085 got_path_is_root_dir(path_base_tree) ? "" : "/",
5086 child_path) == -1)
5087 return got_error_from_errno("asprintf");
5089 new_te = calloc(1, sizeof(*new_te));
5090 if (new_te == NULL)
5091 return got_error_from_errno("calloc");
5092 new_te->mode = S_IFDIR;
5094 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5095 sizeof(new_te->name)) {
5096 err = got_error(GOT_ERR_NO_SPACE);
5097 goto done;
5099 err = write_tree(&id, &nentries, NULL, subtree_path,
5100 commitable_paths, status_cb, status_arg, repo);
5101 if (err) {
5102 free(new_te);
5103 goto done;
5105 memcpy(&new_te->id, id, sizeof(new_te->id));
5106 done:
5107 free(id);
5108 free(subtree_path);
5109 if (err == NULL)
5110 *new_tep = new_te;
5111 return err;
5114 static const struct got_error *
5115 write_tree(struct got_object_id **new_tree_id, int *nentries,
5116 struct got_tree_object *base_tree, const char *path_base_tree,
5117 struct got_pathlist_head *commitable_paths,
5118 got_worktree_status_cb status_cb, void *status_arg,
5119 struct got_repository *repo)
5121 const struct got_error *err = NULL;
5122 struct got_pathlist_head paths;
5123 struct got_tree_entry *te, *new_te = NULL;
5124 struct got_pathlist_entry *pe;
5126 TAILQ_INIT(&paths);
5127 *nentries = 0;
5129 /* Insert, and recurse into, newly added entries first. */
5130 TAILQ_FOREACH(pe, commitable_paths, entry) {
5131 struct got_commitable *ct = pe->data;
5132 char *child_path = NULL, *slash;
5134 if ((ct->status != GOT_STATUS_ADD &&
5135 ct->staged_status != GOT_STATUS_ADD) ||
5136 (ct->flags & GOT_COMMITABLE_ADDED))
5137 continue;
5139 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5140 strlen(path_base_tree)))
5141 continue;
5143 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5144 ct->in_repo_path);
5145 if (err)
5146 goto done;
5148 slash = strchr(child_path, '/');
5149 if (slash == NULL) {
5150 err = alloc_added_blob_tree_entry(&new_te, ct);
5151 if (err)
5152 goto done;
5153 err = report_ct_status(ct, status_cb, status_arg);
5154 if (err)
5155 goto done;
5156 ct->flags |= GOT_COMMITABLE_ADDED;
5157 err = insert_tree_entry(new_te, &paths);
5158 if (err)
5159 goto done;
5160 (*nentries)++;
5161 } else {
5162 *slash = '\0'; /* trim trailing path components */
5163 if (base_tree == NULL ||
5164 got_object_tree_find_entry(base_tree, child_path)
5165 == NULL) {
5166 err = make_subtree_for_added_blob(&new_te,
5167 child_path, path_base_tree,
5168 commitable_paths, status_cb, status_arg,
5169 repo);
5170 if (err)
5171 goto done;
5172 err = insert_tree_entry(new_te, &paths);
5173 if (err)
5174 goto done;
5175 (*nentries)++;
5180 if (base_tree) {
5181 int i, nbase_entries;
5182 /* Handle modified and deleted entries. */
5183 nbase_entries = got_object_tree_get_nentries(base_tree);
5184 for (i = 0; i < nbase_entries; i++) {
5185 struct got_commitable *ct = NULL;
5187 te = got_object_tree_get_entry(base_tree, i);
5188 if (got_object_tree_entry_is_submodule(te)) {
5189 /* Entry is a submodule; just copy it. */
5190 err = got_object_tree_entry_dup(&new_te, te);
5191 if (err)
5192 goto done;
5193 err = insert_tree_entry(new_te, &paths);
5194 if (err)
5195 goto done;
5196 (*nentries)++;
5197 continue;
5200 if (S_ISDIR(te->mode)) {
5201 int modified;
5202 err = got_object_tree_entry_dup(&new_te, te);
5203 if (err)
5204 goto done;
5205 err = match_modified_subtree(&modified, te,
5206 path_base_tree, commitable_paths);
5207 if (err)
5208 goto done;
5209 /* Avoid recursion into unmodified subtrees. */
5210 if (modified) {
5211 struct got_object_id *new_id;
5212 int nsubentries;
5213 err = write_subtree(&new_id,
5214 &nsubentries, te,
5215 path_base_tree, commitable_paths,
5216 status_cb, status_arg, repo);
5217 if (err)
5218 goto done;
5219 if (nsubentries == 0) {
5220 /* All entries were deleted. */
5221 free(new_id);
5222 continue;
5224 memcpy(&new_te->id, new_id,
5225 sizeof(new_te->id));
5226 free(new_id);
5228 err = insert_tree_entry(new_te, &paths);
5229 if (err)
5230 goto done;
5231 (*nentries)++;
5232 continue;
5235 err = match_deleted_or_modified_ct(&ct, te,
5236 path_base_tree, commitable_paths);
5237 if (err)
5238 goto done;
5239 if (ct) {
5240 /* NB: Deleted entries get dropped here. */
5241 if (ct->status == GOT_STATUS_MODIFY ||
5242 ct->status == GOT_STATUS_MODE_CHANGE ||
5243 ct->staged_status == GOT_STATUS_MODIFY) {
5244 err = alloc_modified_blob_tree_entry(
5245 &new_te, te, ct);
5246 if (err)
5247 goto done;
5248 err = insert_tree_entry(new_te, &paths);
5249 if (err)
5250 goto done;
5251 (*nentries)++;
5253 err = report_ct_status(ct, status_cb,
5254 status_arg);
5255 if (err)
5256 goto done;
5257 } else {
5258 /* Entry is unchanged; just copy it. */
5259 err = got_object_tree_entry_dup(&new_te, te);
5260 if (err)
5261 goto done;
5262 err = insert_tree_entry(new_te, &paths);
5263 if (err)
5264 goto done;
5265 (*nentries)++;
5270 /* Write new list of entries; deleted entries have been dropped. */
5271 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5272 done:
5273 got_pathlist_free(&paths);
5274 return err;
5277 static const struct got_error *
5278 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5279 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5280 int have_staged_files)
5282 const struct got_error *err = NULL;
5283 struct got_pathlist_entry *pe;
5285 TAILQ_FOREACH(pe, commitable_paths, entry) {
5286 struct got_fileindex_entry *ie;
5287 struct got_commitable *ct = pe->data;
5289 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5290 if (ie) {
5291 if (ct->status == GOT_STATUS_DELETE ||
5292 ct->staged_status == GOT_STATUS_DELETE) {
5293 got_fileindex_entry_remove(fileindex, ie);
5294 } else if (ct->staged_status == GOT_STATUS_ADD ||
5295 ct->staged_status == GOT_STATUS_MODIFY) {
5296 got_fileindex_entry_stage_set(ie,
5297 GOT_FILEIDX_STAGE_NONE);
5298 got_fileindex_entry_staged_filetype_set(ie, 0);
5299 err = got_fileindex_entry_update(ie,
5300 ct->ondisk_path, ct->staged_blob_id->sha1,
5301 new_base_commit_id->sha1,
5302 !have_staged_files);
5303 } else
5304 err = got_fileindex_entry_update(ie,
5305 ct->ondisk_path, ct->blob_id->sha1,
5306 new_base_commit_id->sha1,
5307 !have_staged_files);
5308 } else {
5309 err = got_fileindex_entry_alloc(&ie, pe->path);
5310 if (err)
5311 break;
5312 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5313 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5314 if (err) {
5315 got_fileindex_entry_free(ie);
5316 break;
5318 err = got_fileindex_entry_add(fileindex, ie);
5319 if (err) {
5320 got_fileindex_entry_free(ie);
5321 break;
5325 return err;
5329 static const struct got_error *
5330 check_out_of_date(const char *in_repo_path, unsigned char status,
5331 unsigned char staged_status, struct got_object_id *base_blob_id,
5332 struct got_object_id *base_commit_id,
5333 struct got_object_id *head_commit_id, struct got_repository *repo,
5334 int ood_errcode)
5336 const struct got_error *err = NULL;
5337 struct got_object_id *id = NULL;
5339 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5340 /* Trivial case: base commit == head commit */
5341 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5342 return NULL;
5344 * Ensure file content which local changes were based
5345 * on matches file content in the branch head.
5347 err = got_object_id_by_path(&id, repo, head_commit_id,
5348 in_repo_path);
5349 if (err) {
5350 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5351 err = got_error(ood_errcode);
5352 goto done;
5353 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5354 err = got_error(ood_errcode);
5355 } else {
5356 /* Require that added files don't exist in the branch head. */
5357 err = got_object_id_by_path(&id, repo, head_commit_id,
5358 in_repo_path);
5359 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5360 goto done;
5361 err = id ? got_error(ood_errcode) : NULL;
5363 done:
5364 free(id);
5365 return err;
5368 const struct got_error *
5369 commit_worktree(struct got_object_id **new_commit_id,
5370 struct got_pathlist_head *commitable_paths,
5371 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5372 const char *author, const char *committer,
5373 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5374 got_worktree_status_cb status_cb, void *status_arg,
5375 struct got_repository *repo)
5377 const struct got_error *err = NULL, *unlockerr = NULL;
5378 struct got_pathlist_entry *pe;
5379 const char *head_ref_name = NULL;
5380 struct got_commit_object *head_commit = NULL;
5381 struct got_reference *head_ref2 = NULL;
5382 struct got_object_id *head_commit_id2 = NULL;
5383 struct got_tree_object *head_tree = NULL;
5384 struct got_object_id *new_tree_id = NULL;
5385 int nentries;
5386 struct got_object_id_queue parent_ids;
5387 struct got_object_qid *pid = NULL;
5388 char *logmsg = NULL;
5390 *new_commit_id = NULL;
5392 SIMPLEQ_INIT(&parent_ids);
5394 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5395 if (err)
5396 goto done;
5398 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5399 if (err)
5400 goto done;
5402 if (commit_msg_cb != NULL) {
5403 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5404 if (err)
5405 goto done;
5408 if (logmsg == NULL || strlen(logmsg) == 0) {
5409 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5410 goto done;
5413 /* Create blobs from added and modified files and record their IDs. */
5414 TAILQ_FOREACH(pe, commitable_paths, entry) {
5415 struct got_commitable *ct = pe->data;
5416 char *ondisk_path;
5418 /* Blobs for staged files already exist. */
5419 if (ct->staged_status == GOT_STATUS_ADD ||
5420 ct->staged_status == GOT_STATUS_MODIFY)
5421 continue;
5423 if (ct->status != GOT_STATUS_ADD &&
5424 ct->status != GOT_STATUS_MODIFY &&
5425 ct->status != GOT_STATUS_MODE_CHANGE)
5426 continue;
5428 if (asprintf(&ondisk_path, "%s/%s",
5429 worktree->root_path, pe->path) == -1) {
5430 err = got_error_from_errno("asprintf");
5431 goto done;
5433 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5434 free(ondisk_path);
5435 if (err)
5436 goto done;
5439 /* Recursively write new tree objects. */
5440 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5441 commitable_paths, status_cb, status_arg, repo);
5442 if (err)
5443 goto done;
5445 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5446 if (err)
5447 goto done;
5448 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5449 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5450 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5451 got_object_qid_free(pid);
5452 if (logmsg != NULL)
5453 free(logmsg);
5454 if (err)
5455 goto done;
5457 /* Check if a concurrent commit to our branch has occurred. */
5458 head_ref_name = got_worktree_get_head_ref_name(worktree);
5459 if (head_ref_name == NULL) {
5460 err = got_error_from_errno("got_worktree_get_head_ref_name");
5461 goto done;
5463 /* Lock the reference here to prevent concurrent modification. */
5464 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5465 if (err)
5466 goto done;
5467 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5468 if (err)
5469 goto done;
5470 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5471 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5472 goto done;
5474 /* Update branch head in repository. */
5475 err = got_ref_change_ref(head_ref2, *new_commit_id);
5476 if (err)
5477 goto done;
5478 err = got_ref_write(head_ref2, repo);
5479 if (err)
5480 goto done;
5482 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5483 if (err)
5484 goto done;
5486 err = ref_base_commit(worktree, repo);
5487 if (err)
5488 goto done;
5489 done:
5490 if (head_tree)
5491 got_object_tree_close(head_tree);
5492 if (head_commit)
5493 got_object_commit_close(head_commit);
5494 free(head_commit_id2);
5495 if (head_ref2) {
5496 unlockerr = got_ref_unlock(head_ref2);
5497 if (unlockerr && err == NULL)
5498 err = unlockerr;
5499 got_ref_close(head_ref2);
5501 return err;
5504 static const struct got_error *
5505 check_path_is_commitable(const char *path,
5506 struct got_pathlist_head *commitable_paths)
5508 struct got_pathlist_entry *cpe = NULL;
5509 size_t path_len = strlen(path);
5511 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5512 struct got_commitable *ct = cpe->data;
5513 const char *ct_path = ct->path;
5515 while (ct_path[0] == '/')
5516 ct_path++;
5518 if (strcmp(path, ct_path) == 0 ||
5519 got_path_is_child(ct_path, path, path_len))
5520 break;
5523 if (cpe == NULL)
5524 return got_error_path(path, GOT_ERR_BAD_PATH);
5526 return NULL;
5529 static const struct got_error *
5530 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5532 int *have_staged_files = arg;
5534 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5535 *have_staged_files = 1;
5536 return got_error(GOT_ERR_CANCELLED);
5539 return NULL;
5542 static const struct got_error *
5543 check_non_staged_files(struct got_fileindex *fileindex,
5544 struct got_pathlist_head *paths)
5546 struct got_pathlist_entry *pe;
5547 struct got_fileindex_entry *ie;
5549 TAILQ_FOREACH(pe, paths, entry) {
5550 if (pe->path[0] == '\0')
5551 continue;
5552 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5553 if (ie == NULL)
5554 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5555 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5556 return got_error_path(pe->path,
5557 GOT_ERR_FILE_NOT_STAGED);
5560 return NULL;
5563 const struct got_error *
5564 got_worktree_commit(struct got_object_id **new_commit_id,
5565 struct got_worktree *worktree, struct got_pathlist_head *paths,
5566 const char *author, const char *committer, int allow_bad_symlinks,
5567 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5568 got_worktree_status_cb status_cb, void *status_arg,
5569 struct got_repository *repo)
5571 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5572 struct got_fileindex *fileindex = NULL;
5573 char *fileindex_path = NULL;
5574 struct got_pathlist_head commitable_paths;
5575 struct collect_commitables_arg cc_arg;
5576 struct got_pathlist_entry *pe;
5577 struct got_reference *head_ref = NULL;
5578 struct got_object_id *head_commit_id = NULL;
5579 int have_staged_files = 0;
5581 *new_commit_id = NULL;
5583 TAILQ_INIT(&commitable_paths);
5585 err = lock_worktree(worktree, LOCK_EX);
5586 if (err)
5587 goto done;
5589 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5590 if (err)
5591 goto done;
5593 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5594 if (err)
5595 goto done;
5597 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5598 if (err)
5599 goto done;
5601 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5602 &have_staged_files);
5603 if (err && err->code != GOT_ERR_CANCELLED)
5604 goto done;
5605 if (have_staged_files) {
5606 err = check_non_staged_files(fileindex, paths);
5607 if (err)
5608 goto done;
5611 cc_arg.commitable_paths = &commitable_paths;
5612 cc_arg.worktree = worktree;
5613 cc_arg.fileindex = fileindex;
5614 cc_arg.repo = repo;
5615 cc_arg.have_staged_files = have_staged_files;
5616 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5617 TAILQ_FOREACH(pe, paths, entry) {
5618 err = worktree_status(worktree, pe->path, fileindex, repo,
5619 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5620 if (err)
5621 goto done;
5624 if (TAILQ_EMPTY(&commitable_paths)) {
5625 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5626 goto done;
5629 TAILQ_FOREACH(pe, paths, entry) {
5630 err = check_path_is_commitable(pe->path, &commitable_paths);
5631 if (err)
5632 goto done;
5635 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5636 struct got_commitable *ct = pe->data;
5637 const char *ct_path = ct->in_repo_path;
5639 while (ct_path[0] == '/')
5640 ct_path++;
5641 err = check_out_of_date(ct_path, ct->status,
5642 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5643 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5644 if (err)
5645 goto done;
5649 err = commit_worktree(new_commit_id, &commitable_paths,
5650 head_commit_id, worktree, author, committer,
5651 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5652 if (err)
5653 goto done;
5655 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5656 fileindex, have_staged_files);
5657 sync_err = sync_fileindex(fileindex, fileindex_path);
5658 if (sync_err && err == NULL)
5659 err = sync_err;
5660 done:
5661 if (fileindex)
5662 got_fileindex_free(fileindex);
5663 free(fileindex_path);
5664 unlockerr = lock_worktree(worktree, LOCK_SH);
5665 if (unlockerr && err == NULL)
5666 err = unlockerr;
5667 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5668 struct got_commitable *ct = pe->data;
5669 free_commitable(ct);
5671 got_pathlist_free(&commitable_paths);
5672 return err;
5675 const char *
5676 got_commitable_get_path(struct got_commitable *ct)
5678 return ct->path;
5681 unsigned int
5682 got_commitable_get_status(struct got_commitable *ct)
5684 return ct->status;
5687 struct check_rebase_ok_arg {
5688 struct got_worktree *worktree;
5689 struct got_repository *repo;
5692 static const struct got_error *
5693 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5695 const struct got_error *err = NULL;
5696 struct check_rebase_ok_arg *a = arg;
5697 unsigned char status;
5698 struct stat sb;
5699 char *ondisk_path;
5701 /* Reject rebase of a work tree with mixed base commits. */
5702 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5703 SHA1_DIGEST_LENGTH))
5704 return got_error(GOT_ERR_MIXED_COMMITS);
5706 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5707 == -1)
5708 return got_error_from_errno("asprintf");
5710 /* Reject rebase of a work tree with modified or staged files. */
5711 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5712 free(ondisk_path);
5713 if (err)
5714 return err;
5716 if (status != GOT_STATUS_NO_CHANGE)
5717 return got_error(GOT_ERR_MODIFIED);
5718 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5719 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5721 return NULL;
5724 const struct got_error *
5725 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5726 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5727 struct got_worktree *worktree, struct got_reference *branch,
5728 struct got_repository *repo)
5730 const struct got_error *err = NULL;
5731 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5732 char *branch_ref_name = NULL;
5733 char *fileindex_path = NULL;
5734 struct check_rebase_ok_arg ok_arg;
5735 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5736 struct got_object_id *wt_branch_tip = NULL;
5738 *new_base_branch_ref = NULL;
5739 *tmp_branch = NULL;
5740 *fileindex = NULL;
5742 err = lock_worktree(worktree, LOCK_EX);
5743 if (err)
5744 return err;
5746 err = open_fileindex(fileindex, &fileindex_path, worktree);
5747 if (err)
5748 goto done;
5750 ok_arg.worktree = worktree;
5751 ok_arg.repo = repo;
5752 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5753 &ok_arg);
5754 if (err)
5755 goto done;
5757 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5758 if (err)
5759 goto done;
5761 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5762 if (err)
5763 goto done;
5765 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5766 if (err)
5767 goto done;
5769 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5770 0);
5771 if (err)
5772 goto done;
5774 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5775 if (err)
5776 goto done;
5777 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5778 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5779 goto done;
5782 err = got_ref_alloc_symref(new_base_branch_ref,
5783 new_base_branch_ref_name, wt_branch);
5784 if (err)
5785 goto done;
5786 err = got_ref_write(*new_base_branch_ref, repo);
5787 if (err)
5788 goto done;
5790 /* TODO Lock original branch's ref while rebasing? */
5792 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5793 if (err)
5794 goto done;
5796 err = got_ref_write(branch_ref, repo);
5797 if (err)
5798 goto done;
5800 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5801 worktree->base_commit_id);
5802 if (err)
5803 goto done;
5804 err = got_ref_write(*tmp_branch, repo);
5805 if (err)
5806 goto done;
5808 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5809 if (err)
5810 goto done;
5811 done:
5812 free(fileindex_path);
5813 free(tmp_branch_name);
5814 free(new_base_branch_ref_name);
5815 free(branch_ref_name);
5816 if (branch_ref)
5817 got_ref_close(branch_ref);
5818 if (wt_branch)
5819 got_ref_close(wt_branch);
5820 free(wt_branch_tip);
5821 if (err) {
5822 if (*new_base_branch_ref) {
5823 got_ref_close(*new_base_branch_ref);
5824 *new_base_branch_ref = NULL;
5826 if (*tmp_branch) {
5827 got_ref_close(*tmp_branch);
5828 *tmp_branch = NULL;
5830 if (*fileindex) {
5831 got_fileindex_free(*fileindex);
5832 *fileindex = NULL;
5834 lock_worktree(worktree, LOCK_SH);
5836 return err;
5839 const struct got_error *
5840 got_worktree_rebase_continue(struct got_object_id **commit_id,
5841 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5842 struct got_reference **branch, struct got_fileindex **fileindex,
5843 struct got_worktree *worktree, struct got_repository *repo)
5845 const struct got_error *err;
5846 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5847 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5848 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5849 char *fileindex_path = NULL;
5850 int have_staged_files = 0;
5852 *commit_id = NULL;
5853 *new_base_branch = NULL;
5854 *tmp_branch = NULL;
5855 *branch = NULL;
5856 *fileindex = NULL;
5858 err = lock_worktree(worktree, LOCK_EX);
5859 if (err)
5860 return err;
5862 err = open_fileindex(fileindex, &fileindex_path, worktree);
5863 if (err)
5864 goto done;
5866 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5867 &have_staged_files);
5868 if (err && err->code != GOT_ERR_CANCELLED)
5869 goto done;
5870 if (have_staged_files) {
5871 err = got_error(GOT_ERR_STAGED_PATHS);
5872 goto done;
5875 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5876 if (err)
5877 goto done;
5879 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5880 if (err)
5881 goto done;
5883 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5884 if (err)
5885 goto done;
5887 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5888 if (err)
5889 goto done;
5891 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5892 if (err)
5893 goto done;
5895 err = got_ref_open(branch, repo,
5896 got_ref_get_symref_target(branch_ref), 0);
5897 if (err)
5898 goto done;
5900 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5901 if (err)
5902 goto done;
5904 err = got_ref_resolve(commit_id, repo, commit_ref);
5905 if (err)
5906 goto done;
5908 err = got_ref_open(new_base_branch, repo,
5909 new_base_branch_ref_name, 0);
5910 if (err)
5911 goto done;
5913 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5914 if (err)
5915 goto done;
5916 done:
5917 free(commit_ref_name);
5918 free(branch_ref_name);
5919 free(fileindex_path);
5920 if (commit_ref)
5921 got_ref_close(commit_ref);
5922 if (branch_ref)
5923 got_ref_close(branch_ref);
5924 if (err) {
5925 free(*commit_id);
5926 *commit_id = NULL;
5927 if (*tmp_branch) {
5928 got_ref_close(*tmp_branch);
5929 *tmp_branch = NULL;
5931 if (*new_base_branch) {
5932 got_ref_close(*new_base_branch);
5933 *new_base_branch = NULL;
5935 if (*branch) {
5936 got_ref_close(*branch);
5937 *branch = NULL;
5939 if (*fileindex) {
5940 got_fileindex_free(*fileindex);
5941 *fileindex = NULL;
5943 lock_worktree(worktree, LOCK_SH);
5945 return err;
5948 const struct got_error *
5949 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5951 const struct got_error *err;
5952 char *tmp_branch_name = NULL;
5954 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5955 if (err)
5956 return err;
5958 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5959 free(tmp_branch_name);
5960 return NULL;
5963 static const struct got_error *
5964 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5965 char **logmsg, void *arg)
5967 *logmsg = arg;
5968 return NULL;
5971 static const struct got_error *
5972 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5973 const char *path, struct got_object_id *blob_id,
5974 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5975 int dirfd, const char *de_name)
5977 return NULL;
5980 struct collect_merged_paths_arg {
5981 got_worktree_checkout_cb progress_cb;
5982 void *progress_arg;
5983 struct got_pathlist_head *merged_paths;
5986 static const struct got_error *
5987 collect_merged_paths(void *arg, unsigned char status, const char *path)
5989 const struct got_error *err;
5990 struct collect_merged_paths_arg *a = arg;
5991 char *p;
5992 struct got_pathlist_entry *new;
5994 err = (*a->progress_cb)(a->progress_arg, status, path);
5995 if (err)
5996 return err;
5998 if (status != GOT_STATUS_MERGE &&
5999 status != GOT_STATUS_ADD &&
6000 status != GOT_STATUS_DELETE &&
6001 status != GOT_STATUS_CONFLICT)
6002 return NULL;
6004 p = strdup(path);
6005 if (p == NULL)
6006 return got_error_from_errno("strdup");
6008 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6009 if (err || new == NULL)
6010 free(p);
6011 return err;
6014 void
6015 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6017 struct got_pathlist_entry *pe;
6019 TAILQ_FOREACH(pe, merged_paths, entry)
6020 free((char *)pe->path);
6022 got_pathlist_free(merged_paths);
6025 static const struct got_error *
6026 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6027 int is_rebase, struct got_repository *repo)
6029 const struct got_error *err;
6030 struct got_reference *commit_ref = NULL;
6032 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6033 if (err) {
6034 if (err->code != GOT_ERR_NOT_REF)
6035 goto done;
6036 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6037 if (err)
6038 goto done;
6039 err = got_ref_write(commit_ref, repo);
6040 if (err)
6041 goto done;
6042 } else if (is_rebase) {
6043 struct got_object_id *stored_id;
6044 int cmp;
6046 err = got_ref_resolve(&stored_id, repo, commit_ref);
6047 if (err)
6048 goto done;
6049 cmp = got_object_id_cmp(commit_id, stored_id);
6050 free(stored_id);
6051 if (cmp != 0) {
6052 err = got_error(GOT_ERR_REBASE_COMMITID);
6053 goto done;
6056 done:
6057 if (commit_ref)
6058 got_ref_close(commit_ref);
6059 return err;
6062 static const struct got_error *
6063 rebase_merge_files(struct got_pathlist_head *merged_paths,
6064 const char *commit_ref_name, struct got_worktree *worktree,
6065 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6066 struct got_object_id *commit_id, struct got_repository *repo,
6067 got_worktree_checkout_cb progress_cb, void *progress_arg,
6068 got_cancel_cb cancel_cb, void *cancel_arg)
6070 const struct got_error *err;
6071 struct got_reference *commit_ref = NULL;
6072 struct collect_merged_paths_arg cmp_arg;
6073 char *fileindex_path;
6075 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6077 err = get_fileindex_path(&fileindex_path, worktree);
6078 if (err)
6079 return err;
6081 cmp_arg.progress_cb = progress_cb;
6082 cmp_arg.progress_arg = progress_arg;
6083 cmp_arg.merged_paths = merged_paths;
6084 err = merge_files(worktree, fileindex, fileindex_path,
6085 parent_commit_id, commit_id, repo, collect_merged_paths,
6086 &cmp_arg, cancel_cb, cancel_arg);
6087 if (commit_ref)
6088 got_ref_close(commit_ref);
6089 return err;
6092 const struct got_error *
6093 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6094 struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6096 struct got_repository *repo,
6097 got_worktree_checkout_cb progress_cb, void *progress_arg,
6098 got_cancel_cb cancel_cb, void *cancel_arg)
6100 const struct got_error *err;
6101 char *commit_ref_name;
6103 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6104 if (err)
6105 return err;
6107 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6108 if (err)
6109 goto done;
6111 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6112 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6113 progress_arg, cancel_cb, cancel_arg);
6114 done:
6115 free(commit_ref_name);
6116 return err;
6119 const struct got_error *
6120 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6121 struct got_worktree *worktree, struct got_fileindex *fileindex,
6122 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6123 struct got_repository *repo,
6124 got_worktree_checkout_cb progress_cb, void *progress_arg,
6125 got_cancel_cb cancel_cb, void *cancel_arg)
6127 const struct got_error *err;
6128 char *commit_ref_name;
6130 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6131 if (err)
6132 return err;
6134 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6135 if (err)
6136 goto done;
6138 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6139 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6140 progress_arg, cancel_cb, cancel_arg);
6141 done:
6142 free(commit_ref_name);
6143 return err;
6146 static const struct got_error *
6147 rebase_commit(struct got_object_id **new_commit_id,
6148 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6149 struct got_worktree *worktree, struct got_fileindex *fileindex,
6150 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6151 const char *new_logmsg, struct got_repository *repo)
6153 const struct got_error *err, *sync_err;
6154 struct got_pathlist_head commitable_paths;
6155 struct collect_commitables_arg cc_arg;
6156 char *fileindex_path = NULL;
6157 struct got_reference *head_ref = NULL;
6158 struct got_object_id *head_commit_id = NULL;
6159 char *logmsg = NULL;
6161 TAILQ_INIT(&commitable_paths);
6162 *new_commit_id = NULL;
6164 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6166 err = get_fileindex_path(&fileindex_path, worktree);
6167 if (err)
6168 return err;
6170 cc_arg.commitable_paths = &commitable_paths;
6171 cc_arg.worktree = worktree;
6172 cc_arg.repo = repo;
6173 cc_arg.have_staged_files = 0;
6175 * If possible get the status of individual files directly to
6176 * avoid crawling the entire work tree once per rebased commit.
6177 * TODO: Ideally, merged_paths would contain a list of commitables
6178 * we could use so we could skip worktree_status() entirely.
6180 if (merged_paths) {
6181 struct got_pathlist_entry *pe;
6182 TAILQ_FOREACH(pe, merged_paths, entry) {
6183 err = worktree_status(worktree, pe->path, fileindex,
6184 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6185 0);
6186 if (err)
6187 goto done;
6189 } else {
6190 err = worktree_status(worktree, "", fileindex, repo,
6191 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6192 if (err)
6193 goto done;
6196 if (TAILQ_EMPTY(&commitable_paths)) {
6197 /* No-op change; commit will be elided. */
6198 err = got_ref_delete(commit_ref, repo);
6199 if (err)
6200 goto done;
6201 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6202 goto done;
6205 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6206 if (err)
6207 goto done;
6209 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6210 if (err)
6211 goto done;
6213 if (new_logmsg) {
6214 logmsg = strdup(new_logmsg);
6215 if (logmsg == NULL) {
6216 err = got_error_from_errno("strdup");
6217 goto done;
6219 } else {
6220 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6221 if (err)
6222 goto done;
6225 /* NB: commit_worktree will call free(logmsg) */
6226 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6227 worktree, got_object_commit_get_author(orig_commit),
6228 got_object_commit_get_committer(orig_commit),
6229 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6230 if (err)
6231 goto done;
6233 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6234 if (err)
6235 goto done;
6237 err = got_ref_delete(commit_ref, repo);
6238 if (err)
6239 goto done;
6241 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6242 fileindex, 0);
6243 sync_err = sync_fileindex(fileindex, fileindex_path);
6244 if (sync_err && err == NULL)
6245 err = sync_err;
6246 done:
6247 free(fileindex_path);
6248 free(head_commit_id);
6249 if (head_ref)
6250 got_ref_close(head_ref);
6251 if (err) {
6252 free(*new_commit_id);
6253 *new_commit_id = NULL;
6255 return err;
6258 const struct got_error *
6259 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6260 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6261 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6262 struct got_commit_object *orig_commit,
6263 struct got_object_id *orig_commit_id, struct got_repository *repo)
6265 const struct got_error *err;
6266 char *commit_ref_name;
6267 struct got_reference *commit_ref = NULL;
6268 struct got_object_id *commit_id = NULL;
6270 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6271 if (err)
6272 return err;
6274 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6275 if (err)
6276 goto done;
6277 err = got_ref_resolve(&commit_id, repo, commit_ref);
6278 if (err)
6279 goto done;
6280 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6281 err = got_error(GOT_ERR_REBASE_COMMITID);
6282 goto done;
6285 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6286 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6287 done:
6288 if (commit_ref)
6289 got_ref_close(commit_ref);
6290 free(commit_ref_name);
6291 free(commit_id);
6292 return err;
6295 const struct got_error *
6296 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6297 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6298 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6299 struct got_commit_object *orig_commit,
6300 struct got_object_id *orig_commit_id, const char *new_logmsg,
6301 struct got_repository *repo)
6303 const struct got_error *err;
6304 char *commit_ref_name;
6305 struct got_reference *commit_ref = NULL;
6307 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6308 if (err)
6309 return err;
6311 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6312 if (err)
6313 goto done;
6315 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6316 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6317 done:
6318 if (commit_ref)
6319 got_ref_close(commit_ref);
6320 free(commit_ref_name);
6321 return err;
6324 const struct got_error *
6325 got_worktree_rebase_postpone(struct got_worktree *worktree,
6326 struct got_fileindex *fileindex)
6328 if (fileindex)
6329 got_fileindex_free(fileindex);
6330 return lock_worktree(worktree, LOCK_SH);
6333 static const struct got_error *
6334 delete_ref(const char *name, struct got_repository *repo)
6336 const struct got_error *err;
6337 struct got_reference *ref;
6339 err = got_ref_open(&ref, repo, name, 0);
6340 if (err) {
6341 if (err->code == GOT_ERR_NOT_REF)
6342 return NULL;
6343 return err;
6346 err = got_ref_delete(ref, repo);
6347 got_ref_close(ref);
6348 return err;
6351 static const struct got_error *
6352 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6354 const struct got_error *err;
6355 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6356 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6358 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6359 if (err)
6360 goto done;
6361 err = delete_ref(tmp_branch_name, repo);
6362 if (err)
6363 goto done;
6365 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6366 if (err)
6367 goto done;
6368 err = delete_ref(new_base_branch_ref_name, repo);
6369 if (err)
6370 goto done;
6372 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6373 if (err)
6374 goto done;
6375 err = delete_ref(branch_ref_name, repo);
6376 if (err)
6377 goto done;
6379 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6380 if (err)
6381 goto done;
6382 err = delete_ref(commit_ref_name, repo);
6383 if (err)
6384 goto done;
6386 done:
6387 free(tmp_branch_name);
6388 free(new_base_branch_ref_name);
6389 free(branch_ref_name);
6390 free(commit_ref_name);
6391 return err;
6394 const struct got_error *
6395 got_worktree_rebase_complete(struct got_worktree *worktree,
6396 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6397 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6398 struct got_repository *repo)
6400 const struct got_error *err, *unlockerr;
6401 struct got_object_id *new_head_commit_id = NULL;
6403 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6404 if (err)
6405 return err;
6407 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6408 if (err)
6409 goto done;
6411 err = got_ref_write(rebased_branch, repo);
6412 if (err)
6413 goto done;
6415 err = got_worktree_set_head_ref(worktree, rebased_branch);
6416 if (err)
6417 goto done;
6419 err = delete_rebase_refs(worktree, repo);
6420 done:
6421 if (fileindex)
6422 got_fileindex_free(fileindex);
6423 free(new_head_commit_id);
6424 unlockerr = lock_worktree(worktree, LOCK_SH);
6425 if (unlockerr && err == NULL)
6426 err = unlockerr;
6427 return err;
6430 const struct got_error *
6431 got_worktree_rebase_abort(struct got_worktree *worktree,
6432 struct got_fileindex *fileindex, struct got_repository *repo,
6433 struct got_reference *new_base_branch,
6434 got_worktree_checkout_cb progress_cb, void *progress_arg)
6436 const struct got_error *err, *unlockerr, *sync_err;
6437 struct got_reference *resolved = NULL;
6438 struct got_object_id *commit_id = NULL;
6439 char *fileindex_path = NULL;
6440 struct revert_file_args rfa;
6441 struct got_object_id *tree_id = NULL;
6443 err = lock_worktree(worktree, LOCK_EX);
6444 if (err)
6445 return err;
6447 err = got_ref_open(&resolved, repo,
6448 got_ref_get_symref_target(new_base_branch), 0);
6449 if (err)
6450 goto done;
6452 err = got_worktree_set_head_ref(worktree, resolved);
6453 if (err)
6454 goto done;
6457 * XXX commits to the base branch could have happened while
6458 * we were busy rebasing; should we store the original commit ID
6459 * when rebase begins and read it back here?
6461 err = got_ref_resolve(&commit_id, repo, resolved);
6462 if (err)
6463 goto done;
6465 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6466 if (err)
6467 goto done;
6469 err = got_object_id_by_path(&tree_id, repo,
6470 worktree->base_commit_id, worktree->path_prefix);
6471 if (err)
6472 goto done;
6474 err = delete_rebase_refs(worktree, repo);
6475 if (err)
6476 goto done;
6478 err = get_fileindex_path(&fileindex_path, worktree);
6479 if (err)
6480 goto done;
6482 rfa.worktree = worktree;
6483 rfa.fileindex = fileindex;
6484 rfa.progress_cb = progress_cb;
6485 rfa.progress_arg = progress_arg;
6486 rfa.patch_cb = NULL;
6487 rfa.patch_arg = NULL;
6488 rfa.repo = repo;
6489 err = worktree_status(worktree, "", fileindex, repo,
6490 revert_file, &rfa, NULL, NULL, 0, 0);
6491 if (err)
6492 goto sync;
6494 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6495 repo, progress_cb, progress_arg, NULL, NULL);
6496 sync:
6497 sync_err = sync_fileindex(fileindex, fileindex_path);
6498 if (sync_err && err == NULL)
6499 err = sync_err;
6500 done:
6501 got_ref_close(resolved);
6502 free(tree_id);
6503 free(commit_id);
6504 if (fileindex)
6505 got_fileindex_free(fileindex);
6506 free(fileindex_path);
6508 unlockerr = lock_worktree(worktree, LOCK_SH);
6509 if (unlockerr && err == NULL)
6510 err = unlockerr;
6511 return err;
6514 const struct got_error *
6515 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6516 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6517 struct got_fileindex **fileindex, struct got_worktree *worktree,
6518 struct got_repository *repo)
6520 const struct got_error *err = NULL;
6521 char *tmp_branch_name = NULL;
6522 char *branch_ref_name = NULL;
6523 char *base_commit_ref_name = NULL;
6524 char *fileindex_path = NULL;
6525 struct check_rebase_ok_arg ok_arg;
6526 struct got_reference *wt_branch = NULL;
6527 struct got_reference *base_commit_ref = NULL;
6529 *tmp_branch = NULL;
6530 *branch_ref = NULL;
6531 *base_commit_id = NULL;
6532 *fileindex = NULL;
6534 err = lock_worktree(worktree, LOCK_EX);
6535 if (err)
6536 return err;
6538 err = open_fileindex(fileindex, &fileindex_path, worktree);
6539 if (err)
6540 goto done;
6542 ok_arg.worktree = worktree;
6543 ok_arg.repo = repo;
6544 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6545 &ok_arg);
6546 if (err)
6547 goto done;
6549 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6550 if (err)
6551 goto done;
6553 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6554 if (err)
6555 goto done;
6557 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6558 worktree);
6559 if (err)
6560 goto done;
6562 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6563 0);
6564 if (err)
6565 goto done;
6567 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6568 if (err)
6569 goto done;
6571 err = got_ref_write(*branch_ref, repo);
6572 if (err)
6573 goto done;
6575 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6576 worktree->base_commit_id);
6577 if (err)
6578 goto done;
6579 err = got_ref_write(base_commit_ref, repo);
6580 if (err)
6581 goto done;
6582 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6583 if (*base_commit_id == NULL) {
6584 err = got_error_from_errno("got_object_id_dup");
6585 goto done;
6588 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6589 worktree->base_commit_id);
6590 if (err)
6591 goto done;
6592 err = got_ref_write(*tmp_branch, repo);
6593 if (err)
6594 goto done;
6596 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6597 if (err)
6598 goto done;
6599 done:
6600 free(fileindex_path);
6601 free(tmp_branch_name);
6602 free(branch_ref_name);
6603 free(base_commit_ref_name);
6604 if (wt_branch)
6605 got_ref_close(wt_branch);
6606 if (err) {
6607 if (*branch_ref) {
6608 got_ref_close(*branch_ref);
6609 *branch_ref = NULL;
6611 if (*tmp_branch) {
6612 got_ref_close(*tmp_branch);
6613 *tmp_branch = NULL;
6615 free(*base_commit_id);
6616 if (*fileindex) {
6617 got_fileindex_free(*fileindex);
6618 *fileindex = NULL;
6620 lock_worktree(worktree, LOCK_SH);
6622 return err;
6625 const struct got_error *
6626 got_worktree_histedit_postpone(struct got_worktree *worktree,
6627 struct got_fileindex *fileindex)
6629 if (fileindex)
6630 got_fileindex_free(fileindex);
6631 return lock_worktree(worktree, LOCK_SH);
6634 const struct got_error *
6635 got_worktree_histedit_in_progress(int *in_progress,
6636 struct got_worktree *worktree)
6638 const struct got_error *err;
6639 char *tmp_branch_name = NULL;
6641 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6642 if (err)
6643 return err;
6645 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6646 free(tmp_branch_name);
6647 return NULL;
6650 const struct got_error *
6651 got_worktree_histedit_continue(struct got_object_id **commit_id,
6652 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6653 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6654 struct got_worktree *worktree, struct got_repository *repo)
6656 const struct got_error *err;
6657 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6658 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6659 struct got_reference *commit_ref = NULL;
6660 struct got_reference *base_commit_ref = NULL;
6661 char *fileindex_path = NULL;
6662 int have_staged_files = 0;
6664 *commit_id = NULL;
6665 *tmp_branch = NULL;
6666 *base_commit_id = NULL;
6667 *fileindex = NULL;
6669 err = lock_worktree(worktree, LOCK_EX);
6670 if (err)
6671 return err;
6673 err = open_fileindex(fileindex, &fileindex_path, worktree);
6674 if (err)
6675 goto done;
6677 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6678 &have_staged_files);
6679 if (err && err->code != GOT_ERR_CANCELLED)
6680 goto done;
6681 if (have_staged_files) {
6682 err = got_error(GOT_ERR_STAGED_PATHS);
6683 goto done;
6686 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6687 if (err)
6688 goto done;
6690 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6691 if (err)
6692 goto done;
6694 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6695 if (err)
6696 goto done;
6698 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6699 worktree);
6700 if (err)
6701 goto done;
6703 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6704 if (err)
6705 goto done;
6707 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6708 if (err)
6709 goto done;
6710 err = got_ref_resolve(commit_id, repo, commit_ref);
6711 if (err)
6712 goto done;
6714 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6715 if (err)
6716 goto done;
6717 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6718 if (err)
6719 goto done;
6721 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6722 if (err)
6723 goto done;
6724 done:
6725 free(commit_ref_name);
6726 free(branch_ref_name);
6727 free(fileindex_path);
6728 if (commit_ref)
6729 got_ref_close(commit_ref);
6730 if (base_commit_ref)
6731 got_ref_close(base_commit_ref);
6732 if (err) {
6733 free(*commit_id);
6734 *commit_id = NULL;
6735 free(*base_commit_id);
6736 *base_commit_id = NULL;
6737 if (*tmp_branch) {
6738 got_ref_close(*tmp_branch);
6739 *tmp_branch = NULL;
6741 if (*fileindex) {
6742 got_fileindex_free(*fileindex);
6743 *fileindex = NULL;
6745 lock_worktree(worktree, LOCK_EX);
6747 return err;
6750 static const struct got_error *
6751 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6753 const struct got_error *err;
6754 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6755 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6757 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6758 if (err)
6759 goto done;
6760 err = delete_ref(tmp_branch_name, repo);
6761 if (err)
6762 goto done;
6764 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6765 worktree);
6766 if (err)
6767 goto done;
6768 err = delete_ref(base_commit_ref_name, repo);
6769 if (err)
6770 goto done;
6772 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6773 if (err)
6774 goto done;
6775 err = delete_ref(branch_ref_name, repo);
6776 if (err)
6777 goto done;
6779 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6780 if (err)
6781 goto done;
6782 err = delete_ref(commit_ref_name, repo);
6783 if (err)
6784 goto done;
6785 done:
6786 free(tmp_branch_name);
6787 free(base_commit_ref_name);
6788 free(branch_ref_name);
6789 free(commit_ref_name);
6790 return err;
6793 const struct got_error *
6794 got_worktree_histedit_abort(struct got_worktree *worktree,
6795 struct got_fileindex *fileindex, struct got_repository *repo,
6796 struct got_reference *branch, struct got_object_id *base_commit_id,
6797 got_worktree_checkout_cb progress_cb, void *progress_arg)
6799 const struct got_error *err, *unlockerr, *sync_err;
6800 struct got_reference *resolved = NULL;
6801 char *fileindex_path = NULL;
6802 struct got_object_id *tree_id = NULL;
6803 struct revert_file_args rfa;
6805 err = lock_worktree(worktree, LOCK_EX);
6806 if (err)
6807 return err;
6809 err = got_ref_open(&resolved, repo,
6810 got_ref_get_symref_target(branch), 0);
6811 if (err)
6812 goto done;
6814 err = got_worktree_set_head_ref(worktree, resolved);
6815 if (err)
6816 goto done;
6818 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6819 if (err)
6820 goto done;
6822 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6823 worktree->path_prefix);
6824 if (err)
6825 goto done;
6827 err = delete_histedit_refs(worktree, repo);
6828 if (err)
6829 goto done;
6831 err = get_fileindex_path(&fileindex_path, worktree);
6832 if (err)
6833 goto done;
6835 rfa.worktree = worktree;
6836 rfa.fileindex = fileindex;
6837 rfa.progress_cb = progress_cb;
6838 rfa.progress_arg = progress_arg;
6839 rfa.patch_cb = NULL;
6840 rfa.patch_arg = NULL;
6841 rfa.repo = repo;
6842 err = worktree_status(worktree, "", fileindex, repo,
6843 revert_file, &rfa, NULL, NULL, 0, 0);
6844 if (err)
6845 goto sync;
6847 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6848 repo, progress_cb, progress_arg, NULL, NULL);
6849 sync:
6850 sync_err = sync_fileindex(fileindex, fileindex_path);
6851 if (sync_err && err == NULL)
6852 err = sync_err;
6853 done:
6854 got_ref_close(resolved);
6855 free(tree_id);
6856 free(fileindex_path);
6858 unlockerr = lock_worktree(worktree, LOCK_SH);
6859 if (unlockerr && err == NULL)
6860 err = unlockerr;
6861 return err;
6864 const struct got_error *
6865 got_worktree_histedit_complete(struct got_worktree *worktree,
6866 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6867 struct got_reference *edited_branch, struct got_repository *repo)
6869 const struct got_error *err, *unlockerr;
6870 struct got_object_id *new_head_commit_id = NULL;
6871 struct got_reference *resolved = NULL;
6873 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6874 if (err)
6875 return err;
6877 err = got_ref_open(&resolved, repo,
6878 got_ref_get_symref_target(edited_branch), 0);
6879 if (err)
6880 goto done;
6882 err = got_ref_change_ref(resolved, new_head_commit_id);
6883 if (err)
6884 goto done;
6886 err = got_ref_write(resolved, repo);
6887 if (err)
6888 goto done;
6890 err = got_worktree_set_head_ref(worktree, resolved);
6891 if (err)
6892 goto done;
6894 err = delete_histedit_refs(worktree, repo);
6895 done:
6896 if (fileindex)
6897 got_fileindex_free(fileindex);
6898 free(new_head_commit_id);
6899 unlockerr = lock_worktree(worktree, LOCK_SH);
6900 if (unlockerr && err == NULL)
6901 err = unlockerr;
6902 return err;
6905 const struct got_error *
6906 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6907 struct got_object_id *commit_id, struct got_repository *repo)
6909 const struct got_error *err;
6910 char *commit_ref_name;
6912 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6913 if (err)
6914 return err;
6916 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6917 if (err)
6918 goto done;
6920 err = delete_ref(commit_ref_name, repo);
6921 done:
6922 free(commit_ref_name);
6923 return err;
6926 const struct got_error *
6927 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6928 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6929 struct got_worktree *worktree, const char *refname,
6930 struct got_repository *repo)
6932 const struct got_error *err = NULL;
6933 char *fileindex_path = NULL;
6934 struct check_rebase_ok_arg ok_arg;
6936 *fileindex = NULL;
6937 *branch_ref = NULL;
6938 *base_branch_ref = NULL;
6940 err = lock_worktree(worktree, LOCK_EX);
6941 if (err)
6942 return err;
6944 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6945 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6946 "cannot integrate a branch into itself; "
6947 "update -b or different branch name required");
6948 goto done;
6951 err = open_fileindex(fileindex, &fileindex_path, worktree);
6952 if (err)
6953 goto done;
6955 /* Preconditions are the same as for rebase. */
6956 ok_arg.worktree = worktree;
6957 ok_arg.repo = repo;
6958 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6959 &ok_arg);
6960 if (err)
6961 goto done;
6963 err = got_ref_open(branch_ref, repo, refname, 1);
6964 if (err)
6965 goto done;
6967 err = got_ref_open(base_branch_ref, repo,
6968 got_worktree_get_head_ref_name(worktree), 1);
6969 done:
6970 if (err) {
6971 if (*branch_ref) {
6972 got_ref_close(*branch_ref);
6973 *branch_ref = NULL;
6975 if (*base_branch_ref) {
6976 got_ref_close(*base_branch_ref);
6977 *base_branch_ref = NULL;
6979 if (*fileindex) {
6980 got_fileindex_free(*fileindex);
6981 *fileindex = NULL;
6983 lock_worktree(worktree, LOCK_SH);
6985 return err;
6988 const struct got_error *
6989 got_worktree_integrate_continue(struct got_worktree *worktree,
6990 struct got_fileindex *fileindex, struct got_repository *repo,
6991 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6992 got_worktree_checkout_cb progress_cb, void *progress_arg,
6993 got_cancel_cb cancel_cb, void *cancel_arg)
6995 const struct got_error *err = NULL, *sync_err, *unlockerr;
6996 char *fileindex_path = NULL;
6997 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6999 err = get_fileindex_path(&fileindex_path, worktree);
7000 if (err)
7001 goto done;
7003 err = got_ref_resolve(&commit_id, repo, branch_ref);
7004 if (err)
7005 goto done;
7007 err = got_object_id_by_path(&tree_id, repo, commit_id,
7008 worktree->path_prefix);
7009 if (err)
7010 goto done;
7012 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7013 if (err)
7014 goto done;
7016 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7017 progress_cb, progress_arg, cancel_cb, cancel_arg);
7018 if (err)
7019 goto sync;
7021 err = got_ref_change_ref(base_branch_ref, commit_id);
7022 if (err)
7023 goto sync;
7025 err = got_ref_write(base_branch_ref, repo);
7026 sync:
7027 sync_err = sync_fileindex(fileindex, fileindex_path);
7028 if (sync_err && err == NULL)
7029 err = sync_err;
7031 done:
7032 unlockerr = got_ref_unlock(branch_ref);
7033 if (unlockerr && err == NULL)
7034 err = unlockerr;
7035 got_ref_close(branch_ref);
7037 unlockerr = got_ref_unlock(base_branch_ref);
7038 if (unlockerr && err == NULL)
7039 err = unlockerr;
7040 got_ref_close(base_branch_ref);
7042 got_fileindex_free(fileindex);
7043 free(fileindex_path);
7044 free(tree_id);
7046 unlockerr = lock_worktree(worktree, LOCK_SH);
7047 if (unlockerr && err == NULL)
7048 err = unlockerr;
7049 return err;
7052 const struct got_error *
7053 got_worktree_integrate_abort(struct got_worktree *worktree,
7054 struct got_fileindex *fileindex, struct got_repository *repo,
7055 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7057 const struct got_error *err = NULL, *unlockerr = NULL;
7059 got_fileindex_free(fileindex);
7061 err = lock_worktree(worktree, LOCK_SH);
7063 unlockerr = got_ref_unlock(branch_ref);
7064 if (unlockerr && err == NULL)
7065 err = unlockerr;
7066 got_ref_close(branch_ref);
7068 unlockerr = got_ref_unlock(base_branch_ref);
7069 if (unlockerr && err == NULL)
7070 err = unlockerr;
7071 got_ref_close(base_branch_ref);
7073 return err;
7076 struct check_stage_ok_arg {
7077 struct got_object_id *head_commit_id;
7078 struct got_worktree *worktree;
7079 struct got_fileindex *fileindex;
7080 struct got_repository *repo;
7081 int have_changes;
7084 const struct got_error *
7085 check_stage_ok(void *arg, unsigned char status,
7086 unsigned char staged_status, const char *relpath,
7087 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7088 struct got_object_id *commit_id, int dirfd, const char *de_name)
7090 struct check_stage_ok_arg *a = arg;
7091 const struct got_error *err = NULL;
7092 struct got_fileindex_entry *ie;
7093 struct got_object_id base_commit_id;
7094 struct got_object_id *base_commit_idp = NULL;
7095 char *in_repo_path = NULL, *p;
7097 if (status == GOT_STATUS_UNVERSIONED ||
7098 status == GOT_STATUS_NO_CHANGE)
7099 return NULL;
7100 if (status == GOT_STATUS_NONEXISTENT)
7101 return got_error_set_errno(ENOENT, relpath);
7103 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7104 if (ie == NULL)
7105 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7107 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7108 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7109 relpath) == -1)
7110 return got_error_from_errno("asprintf");
7112 if (got_fileindex_entry_has_commit(ie)) {
7113 memcpy(base_commit_id.sha1, ie->commit_sha1,
7114 SHA1_DIGEST_LENGTH);
7115 base_commit_idp = &base_commit_id;
7118 if (status == GOT_STATUS_CONFLICT) {
7119 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7120 goto done;
7121 } else if (status != GOT_STATUS_ADD &&
7122 status != GOT_STATUS_MODIFY &&
7123 status != GOT_STATUS_DELETE) {
7124 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7125 goto done;
7128 a->have_changes = 1;
7130 p = in_repo_path;
7131 while (p[0] == '/')
7132 p++;
7133 err = check_out_of_date(p, status, staged_status,
7134 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7135 GOT_ERR_STAGE_OUT_OF_DATE);
7136 done:
7137 free(in_repo_path);
7138 return err;
7141 struct stage_path_arg {
7142 struct got_worktree *worktree;
7143 struct got_fileindex *fileindex;
7144 struct got_repository *repo;
7145 got_worktree_status_cb status_cb;
7146 void *status_arg;
7147 got_worktree_patch_cb patch_cb;
7148 void *patch_arg;
7149 int staged_something;
7150 int allow_bad_symlinks;
7153 static const struct got_error *
7154 stage_path(void *arg, unsigned char status,
7155 unsigned char staged_status, const char *relpath,
7156 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7157 struct got_object_id *commit_id, int dirfd, const char *de_name)
7159 struct stage_path_arg *a = arg;
7160 const struct got_error *err = NULL;
7161 struct got_fileindex_entry *ie;
7162 char *ondisk_path = NULL, *path_content = NULL;
7163 uint32_t stage;
7164 struct got_object_id *new_staged_blob_id = NULL;
7165 struct stat sb;
7167 if (status == GOT_STATUS_UNVERSIONED)
7168 return NULL;
7170 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7171 if (ie == NULL)
7172 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7174 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7175 relpath)== -1)
7176 return got_error_from_errno("asprintf");
7178 switch (status) {
7179 case GOT_STATUS_ADD:
7180 case GOT_STATUS_MODIFY:
7181 /* XXX could sb.st_mode be passed in by our caller? */
7182 if (lstat(ondisk_path, &sb) == -1) {
7183 err = got_error_from_errno2("lstat", ondisk_path);
7184 break;
7186 if (a->patch_cb) {
7187 if (status == GOT_STATUS_ADD) {
7188 int choice = GOT_PATCH_CHOICE_NONE;
7189 err = (*a->patch_cb)(&choice, a->patch_arg,
7190 status, ie->path, NULL, 1, 1);
7191 if (err)
7192 break;
7193 if (choice != GOT_PATCH_CHOICE_YES)
7194 break;
7195 } else {
7196 err = create_patched_content(&path_content, 0,
7197 staged_blob_id ? staged_blob_id : blob_id,
7198 ondisk_path, dirfd, de_name, ie->path,
7199 a->repo, a->patch_cb, a->patch_arg);
7200 if (err || path_content == NULL)
7201 break;
7204 err = got_object_blob_create(&new_staged_blob_id,
7205 path_content ? path_content : ondisk_path, a->repo);
7206 if (err)
7207 break;
7208 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7209 SHA1_DIGEST_LENGTH);
7210 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7211 stage = GOT_FILEIDX_STAGE_ADD;
7212 else
7213 stage = GOT_FILEIDX_STAGE_MODIFY;
7214 got_fileindex_entry_stage_set(ie, stage);
7215 if (S_ISLNK(sb.st_mode)) {
7216 int is_bad_symlink = 0;
7217 if (!a->allow_bad_symlinks) {
7218 char target_path[PATH_MAX];
7219 ssize_t target_len;
7220 target_len = readlink(ondisk_path, target_path,
7221 sizeof(target_path));
7222 if (target_len == -1) {
7223 err = got_error_from_errno2("readlink",
7224 ondisk_path);
7225 break;
7227 err = is_bad_symlink_target(&is_bad_symlink,
7228 target_path, target_len, ondisk_path,
7229 a->worktree->root_path);
7230 if (err)
7231 break;
7232 if (is_bad_symlink) {
7233 err = got_error_path(ondisk_path,
7234 GOT_ERR_BAD_SYMLINK);
7235 break;
7238 if (is_bad_symlink)
7239 got_fileindex_entry_staged_filetype_set(ie,
7240 GOT_FILEIDX_MODE_BAD_SYMLINK);
7241 else
7242 got_fileindex_entry_staged_filetype_set(ie,
7243 GOT_FILEIDX_MODE_SYMLINK);
7244 } else {
7245 got_fileindex_entry_staged_filetype_set(ie,
7246 GOT_FILEIDX_MODE_REGULAR_FILE);
7248 a->staged_something = 1;
7249 if (a->status_cb == NULL)
7250 break;
7251 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7252 get_staged_status(ie), relpath, blob_id,
7253 new_staged_blob_id, NULL, dirfd, de_name);
7254 break;
7255 case GOT_STATUS_DELETE:
7256 if (staged_status == GOT_STATUS_DELETE)
7257 break;
7258 if (a->patch_cb) {
7259 int choice = GOT_PATCH_CHOICE_NONE;
7260 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7261 ie->path, NULL, 1, 1);
7262 if (err)
7263 break;
7264 if (choice == GOT_PATCH_CHOICE_NO)
7265 break;
7266 if (choice != GOT_PATCH_CHOICE_YES) {
7267 err = got_error(GOT_ERR_PATCH_CHOICE);
7268 break;
7271 stage = GOT_FILEIDX_STAGE_DELETE;
7272 got_fileindex_entry_stage_set(ie, stage);
7273 a->staged_something = 1;
7274 if (a->status_cb == NULL)
7275 break;
7276 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7277 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7278 de_name);
7279 break;
7280 case GOT_STATUS_NO_CHANGE:
7281 break;
7282 case GOT_STATUS_CONFLICT:
7283 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7284 break;
7285 case GOT_STATUS_NONEXISTENT:
7286 err = got_error_set_errno(ENOENT, relpath);
7287 break;
7288 default:
7289 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7290 break;
7293 if (path_content && unlink(path_content) == -1 && err == NULL)
7294 err = got_error_from_errno2("unlink", path_content);
7295 free(path_content);
7296 free(ondisk_path);
7297 free(new_staged_blob_id);
7298 return err;
7301 const struct got_error *
7302 got_worktree_stage(struct got_worktree *worktree,
7303 struct got_pathlist_head *paths,
7304 got_worktree_status_cb status_cb, void *status_arg,
7305 got_worktree_patch_cb patch_cb, void *patch_arg,
7306 int allow_bad_symlinks, struct got_repository *repo)
7308 const struct got_error *err = NULL, *sync_err, *unlockerr;
7309 struct got_pathlist_entry *pe;
7310 struct got_fileindex *fileindex = NULL;
7311 char *fileindex_path = NULL;
7312 struct got_reference *head_ref = NULL;
7313 struct got_object_id *head_commit_id = NULL;
7314 struct check_stage_ok_arg oka;
7315 struct stage_path_arg spa;
7317 err = lock_worktree(worktree, LOCK_EX);
7318 if (err)
7319 return err;
7321 err = got_ref_open(&head_ref, repo,
7322 got_worktree_get_head_ref_name(worktree), 0);
7323 if (err)
7324 goto done;
7325 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7326 if (err)
7327 goto done;
7328 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7329 if (err)
7330 goto done;
7332 /* Check pre-conditions before staging anything. */
7333 oka.head_commit_id = head_commit_id;
7334 oka.worktree = worktree;
7335 oka.fileindex = fileindex;
7336 oka.repo = repo;
7337 oka.have_changes = 0;
7338 TAILQ_FOREACH(pe, paths, entry) {
7339 err = worktree_status(worktree, pe->path, fileindex, repo,
7340 check_stage_ok, &oka, NULL, NULL, 0, 0);
7341 if (err)
7342 goto done;
7344 if (!oka.have_changes) {
7345 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7346 goto done;
7349 spa.worktree = worktree;
7350 spa.fileindex = fileindex;
7351 spa.repo = repo;
7352 spa.patch_cb = patch_cb;
7353 spa.patch_arg = patch_arg;
7354 spa.status_cb = status_cb;
7355 spa.status_arg = status_arg;
7356 spa.staged_something = 0;
7357 spa.allow_bad_symlinks = allow_bad_symlinks;
7358 TAILQ_FOREACH(pe, paths, entry) {
7359 err = worktree_status(worktree, pe->path, fileindex, repo,
7360 stage_path, &spa, NULL, NULL, 0, 0);
7361 if (err)
7362 goto done;
7364 if (!spa.staged_something) {
7365 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7366 goto done;
7369 sync_err = sync_fileindex(fileindex, fileindex_path);
7370 if (sync_err && err == NULL)
7371 err = sync_err;
7372 done:
7373 if (head_ref)
7374 got_ref_close(head_ref);
7375 free(head_commit_id);
7376 free(fileindex_path);
7377 if (fileindex)
7378 got_fileindex_free(fileindex);
7379 unlockerr = lock_worktree(worktree, LOCK_SH);
7380 if (unlockerr && err == NULL)
7381 err = unlockerr;
7382 return err;
7385 struct unstage_path_arg {
7386 struct got_worktree *worktree;
7387 struct got_fileindex *fileindex;
7388 struct got_repository *repo;
7389 got_worktree_checkout_cb progress_cb;
7390 void *progress_arg;
7391 got_worktree_patch_cb patch_cb;
7392 void *patch_arg;
7395 static const struct got_error *
7396 create_unstaged_content(char **path_unstaged_content,
7397 char **path_new_staged_content, struct got_object_id *blob_id,
7398 struct got_object_id *staged_blob_id, const char *relpath,
7399 struct got_repository *repo,
7400 got_worktree_patch_cb patch_cb, void *patch_arg)
7402 const struct got_error *err;
7403 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7404 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7405 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7406 struct stat sb1, sb2;
7407 struct got_diff_changes *changes = NULL;
7408 struct got_diff_state *ds = NULL;
7409 struct got_diff_args *args = NULL;
7410 struct got_diff_change *change;
7411 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7412 int have_content = 0, have_rejected_content = 0;
7414 *path_unstaged_content = NULL;
7415 *path_new_staged_content = NULL;
7417 err = got_object_id_str(&label1, blob_id);
7418 if (err)
7419 return err;
7420 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7421 if (err)
7422 goto done;
7424 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7425 if (err)
7426 goto done;
7428 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7429 if (err)
7430 goto done;
7432 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7433 if (err)
7434 goto done;
7436 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7437 if (err)
7438 goto done;
7440 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7441 if (err)
7442 goto done;
7444 if (stat(path1, &sb1) == -1) {
7445 err = got_error_from_errno2("stat", path1);
7446 goto done;
7449 if (stat(path2, &sb2) == -1) {
7450 err = got_error_from_errno2("stat", path2);
7451 goto done;
7454 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7455 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7456 if (err)
7457 goto done;
7459 err = got_opentemp_named(path_unstaged_content, &outfile,
7460 "got-unstaged-content");
7461 if (err)
7462 goto done;
7463 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7464 "got-new-staged-content");
7465 if (err)
7466 goto done;
7468 if (fseek(f1, 0L, SEEK_SET) == -1) {
7469 err = got_ferror(f1, GOT_ERR_IO);
7470 goto done;
7472 if (fseek(f2, 0L, SEEK_SET) == -1) {
7473 err = got_ferror(f2, GOT_ERR_IO);
7474 goto done;
7476 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7477 int choice;
7478 err = apply_or_reject_change(&choice, change, ++n,
7479 changes->nchanges, ds, args, diff_flags, relpath,
7480 f1, f2, &line_cur1, &line_cur2,
7481 outfile, rejectfile, patch_cb, patch_arg);
7482 if (err)
7483 goto done;
7484 if (choice == GOT_PATCH_CHOICE_YES)
7485 have_content = 1;
7486 else
7487 have_rejected_content = 1;
7488 if (choice == GOT_PATCH_CHOICE_QUIT)
7489 break;
7491 if (have_content || have_rejected_content)
7492 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7493 outfile, rejectfile);
7494 done:
7495 free(label1);
7496 if (blob)
7497 got_object_blob_close(blob);
7498 if (staged_blob)
7499 got_object_blob_close(staged_blob);
7500 if (f1 && fclose(f1) == EOF && err == NULL)
7501 err = got_error_from_errno2("fclose", path1);
7502 if (f2 && fclose(f2) == EOF && err == NULL)
7503 err = got_error_from_errno2("fclose", path2);
7504 if (outfile && fclose(outfile) == EOF && err == NULL)
7505 err = got_error_from_errno2("fclose", *path_unstaged_content);
7506 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7507 err = got_error_from_errno2("fclose", *path_new_staged_content);
7508 if (path1 && unlink(path1) == -1 && err == NULL)
7509 err = got_error_from_errno2("unlink", path1);
7510 if (path2 && unlink(path2) == -1 && err == NULL)
7511 err = got_error_from_errno2("unlink", path2);
7512 if (err || !have_content) {
7513 if (*path_unstaged_content &&
7514 unlink(*path_unstaged_content) == -1 && err == NULL)
7515 err = got_error_from_errno2("unlink",
7516 *path_unstaged_content);
7517 free(*path_unstaged_content);
7518 *path_unstaged_content = NULL;
7520 if (err || !have_content || !have_rejected_content) {
7521 if (*path_new_staged_content &&
7522 unlink(*path_new_staged_content) == -1 && err == NULL)
7523 err = got_error_from_errno2("unlink",
7524 *path_new_staged_content);
7525 free(*path_new_staged_content);
7526 *path_new_staged_content = NULL;
7528 free(args);
7529 if (ds) {
7530 got_diff_state_free(ds);
7531 free(ds);
7533 if (changes)
7534 got_diff_free_changes(changes);
7535 free(path1);
7536 free(path2);
7537 return err;
7540 static const struct got_error *
7541 unstage_hunks(struct got_object_id *staged_blob_id,
7542 struct got_blob_object *blob_base,
7543 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7544 const char *ondisk_path, const char *label_orig,
7545 struct got_worktree *worktree, struct got_repository *repo,
7546 got_worktree_patch_cb patch_cb, void *patch_arg,
7547 got_worktree_checkout_cb progress_cb, void *progress_arg)
7549 const struct got_error *err = NULL;
7550 char *path_unstaged_content = NULL;
7551 char *path_new_staged_content = NULL;
7552 struct got_object_id *new_staged_blob_id = NULL;
7553 FILE *f = NULL;
7554 struct stat sb;
7556 err = create_unstaged_content(&path_unstaged_content,
7557 &path_new_staged_content, blob_id, staged_blob_id,
7558 ie->path, repo, patch_cb, patch_arg);
7559 if (err)
7560 return err;
7562 if (path_unstaged_content == NULL)
7563 return NULL;
7565 if (path_new_staged_content) {
7566 err = got_object_blob_create(&new_staged_blob_id,
7567 path_new_staged_content, repo);
7568 if (err)
7569 goto done;
7572 f = fopen(path_unstaged_content, "r");
7573 if (f == NULL) {
7574 err = got_error_from_errno2("fopen",
7575 path_unstaged_content);
7576 goto done;
7578 if (fstat(fileno(f), &sb) == -1) {
7579 err = got_error_from_errno2("fstat", path_unstaged_content);
7580 goto done;
7582 if (got_fileindex_entry_staged_filetype_get(ie) ==
7583 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7584 char link_target[PATH_MAX];
7585 size_t r;
7586 r = fread(link_target, 1, sizeof(link_target), f);
7587 if (r == 0 && ferror(f)) {
7588 err = got_error_from_errno("fread");
7589 goto done;
7591 if (r >= sizeof(link_target)) { /* should not happen */
7592 err = got_error(GOT_ERR_NO_SPACE);
7593 goto done;
7595 link_target[r] = '\0';
7596 err = merge_symlink(worktree, blob_base,
7597 ondisk_path, ie->path, label_orig, link_target,
7598 worktree->base_commit_id, repo, progress_cb,
7599 progress_arg);
7600 } else {
7601 int local_changes_subsumed;
7602 err = merge_file(&local_changes_subsumed, worktree,
7603 blob_base, ondisk_path, ie->path,
7604 got_fileindex_perms_to_st(ie),
7605 path_unstaged_content, label_orig, "unstaged",
7606 repo, progress_cb, progress_arg);
7608 if (err)
7609 goto done;
7611 if (new_staged_blob_id) {
7612 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7613 SHA1_DIGEST_LENGTH);
7614 } else {
7615 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7616 got_fileindex_entry_staged_filetype_set(ie, 0);
7618 done:
7619 free(new_staged_blob_id);
7620 if (path_unstaged_content &&
7621 unlink(path_unstaged_content) == -1 && err == NULL)
7622 err = got_error_from_errno2("unlink", path_unstaged_content);
7623 if (path_new_staged_content &&
7624 unlink(path_new_staged_content) == -1 && err == NULL)
7625 err = got_error_from_errno2("unlink", path_new_staged_content);
7626 if (f && fclose(f) != 0 && err == NULL)
7627 err = got_error_from_errno2("fclose", path_unstaged_content);
7628 free(path_unstaged_content);
7629 free(path_new_staged_content);
7630 return err;
7633 static const struct got_error *
7634 unstage_path(void *arg, unsigned char status,
7635 unsigned char staged_status, const char *relpath,
7636 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7637 struct got_object_id *commit_id, int dirfd, const char *de_name)
7639 const struct got_error *err = NULL;
7640 struct unstage_path_arg *a = arg;
7641 struct got_fileindex_entry *ie;
7642 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7643 char *ondisk_path = NULL;
7644 char *id_str = NULL, *label_orig = NULL;
7645 int local_changes_subsumed;
7646 struct stat sb;
7648 if (staged_status != GOT_STATUS_ADD &&
7649 staged_status != GOT_STATUS_MODIFY &&
7650 staged_status != GOT_STATUS_DELETE)
7651 return NULL;
7653 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7654 if (ie == NULL)
7655 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7657 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7658 == -1)
7659 return got_error_from_errno("asprintf");
7661 err = got_object_id_str(&id_str,
7662 commit_id ? commit_id : a->worktree->base_commit_id);
7663 if (err)
7664 goto done;
7665 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7666 id_str) == -1) {
7667 err = got_error_from_errno("asprintf");
7668 goto done;
7671 switch (staged_status) {
7672 case GOT_STATUS_MODIFY:
7673 err = got_object_open_as_blob(&blob_base, a->repo,
7674 blob_id, 8192);
7675 if (err)
7676 break;
7677 /* fall through */
7678 case GOT_STATUS_ADD:
7679 if (a->patch_cb) {
7680 if (staged_status == GOT_STATUS_ADD) {
7681 int choice = GOT_PATCH_CHOICE_NONE;
7682 err = (*a->patch_cb)(&choice, a->patch_arg,
7683 staged_status, ie->path, NULL, 1, 1);
7684 if (err)
7685 break;
7686 if (choice != GOT_PATCH_CHOICE_YES)
7687 break;
7688 } else {
7689 err = unstage_hunks(staged_blob_id,
7690 blob_base, blob_id, ie, ondisk_path,
7691 label_orig, a->worktree, a->repo,
7692 a->patch_cb, a->patch_arg,
7693 a->progress_cb, a->progress_arg);
7694 break; /* Done with this file. */
7697 err = got_object_open_as_blob(&blob_staged, a->repo,
7698 staged_blob_id, 8192);
7699 if (err)
7700 break;
7701 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7702 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7703 case GOT_FILEIDX_MODE_REGULAR_FILE:
7704 err = merge_blob(&local_changes_subsumed, a->worktree,
7705 blob_base, ondisk_path, relpath,
7706 got_fileindex_perms_to_st(ie), label_orig,
7707 blob_staged, commit_id ? commit_id :
7708 a->worktree->base_commit_id, a->repo,
7709 a->progress_cb, a->progress_arg);
7710 break;
7711 case GOT_FILEIDX_MODE_SYMLINK:
7712 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7713 char *staged_target;
7714 err = got_object_blob_read_to_str(
7715 &staged_target, blob_staged);
7716 if (err)
7717 goto done;
7718 err = merge_symlink(a->worktree, blob_base,
7719 ondisk_path, relpath, label_orig,
7720 staged_target, commit_id ? commit_id :
7721 a->worktree->base_commit_id,
7722 a->repo, a->progress_cb, a->progress_arg);
7723 free(staged_target);
7724 } else {
7725 err = merge_blob(&local_changes_subsumed,
7726 a->worktree, blob_base, ondisk_path,
7727 relpath, got_fileindex_perms_to_st(ie),
7728 label_orig, blob_staged,
7729 commit_id ? commit_id :
7730 a->worktree->base_commit_id, a->repo,
7731 a->progress_cb, a->progress_arg);
7733 break;
7734 default:
7735 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7736 break;
7738 if (err == NULL) {
7739 got_fileindex_entry_stage_set(ie,
7740 GOT_FILEIDX_STAGE_NONE);
7741 got_fileindex_entry_staged_filetype_set(ie, 0);
7743 break;
7744 case GOT_STATUS_DELETE:
7745 if (a->patch_cb) {
7746 int choice = GOT_PATCH_CHOICE_NONE;
7747 err = (*a->patch_cb)(&choice, a->patch_arg,
7748 staged_status, ie->path, NULL, 1, 1);
7749 if (err)
7750 break;
7751 if (choice == GOT_PATCH_CHOICE_NO)
7752 break;
7753 if (choice != GOT_PATCH_CHOICE_YES) {
7754 err = got_error(GOT_ERR_PATCH_CHOICE);
7755 break;
7758 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7759 got_fileindex_entry_staged_filetype_set(ie, 0);
7760 err = get_file_status(&status, &sb, ie, ondisk_path,
7761 dirfd, de_name, a->repo);
7762 if (err)
7763 break;
7764 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7765 break;
7767 done:
7768 free(ondisk_path);
7769 if (blob_base)
7770 got_object_blob_close(blob_base);
7771 if (blob_staged)
7772 got_object_blob_close(blob_staged);
7773 free(id_str);
7774 free(label_orig);
7775 return err;
7778 const struct got_error *
7779 got_worktree_unstage(struct got_worktree *worktree,
7780 struct got_pathlist_head *paths,
7781 got_worktree_checkout_cb progress_cb, void *progress_arg,
7782 got_worktree_patch_cb patch_cb, void *patch_arg,
7783 struct got_repository *repo)
7785 const struct got_error *err = NULL, *sync_err, *unlockerr;
7786 struct got_pathlist_entry *pe;
7787 struct got_fileindex *fileindex = NULL;
7788 char *fileindex_path = NULL;
7789 struct unstage_path_arg upa;
7791 err = lock_worktree(worktree, LOCK_EX);
7792 if (err)
7793 return err;
7795 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7796 if (err)
7797 goto done;
7799 upa.worktree = worktree;
7800 upa.fileindex = fileindex;
7801 upa.repo = repo;
7802 upa.progress_cb = progress_cb;
7803 upa.progress_arg = progress_arg;
7804 upa.patch_cb = patch_cb;
7805 upa.patch_arg = patch_arg;
7806 TAILQ_FOREACH(pe, paths, entry) {
7807 err = worktree_status(worktree, pe->path, fileindex, repo,
7808 unstage_path, &upa, NULL, NULL, 0, 0);
7809 if (err)
7810 goto done;
7813 sync_err = sync_fileindex(fileindex, fileindex_path);
7814 if (sync_err && err == NULL)
7815 err = sync_err;
7816 done:
7817 free(fileindex_path);
7818 if (fileindex)
7819 got_fileindex_free(fileindex);
7820 unlockerr = lock_worktree(worktree, LOCK_SH);
7821 if (unlockerr && err == NULL)
7822 err = unlockerr;
7823 return err;
7826 struct report_file_info_arg {
7827 struct got_worktree *worktree;
7828 got_worktree_path_info_cb info_cb;
7829 void *info_arg;
7830 struct got_pathlist_head *paths;
7831 got_cancel_cb cancel_cb;
7832 void *cancel_arg;
7835 static const struct got_error *
7836 report_file_info(void *arg, struct got_fileindex_entry *ie)
7838 struct report_file_info_arg *a = arg;
7839 struct got_pathlist_entry *pe;
7840 struct got_object_id blob_id, staged_blob_id, commit_id;
7841 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7842 struct got_object_id *commit_idp = NULL;
7843 int stage;
7845 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7846 return got_error(GOT_ERR_CANCELLED);
7848 TAILQ_FOREACH(pe, a->paths, entry) {
7849 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7850 got_path_is_child(ie->path, pe->path, pe->path_len))
7851 break;
7853 if (pe == NULL) /* not found */
7854 return NULL;
7856 if (got_fileindex_entry_has_blob(ie)) {
7857 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7858 blob_idp = &blob_id;
7860 stage = got_fileindex_entry_stage_get(ie);
7861 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7862 stage == GOT_FILEIDX_STAGE_ADD) {
7863 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7864 SHA1_DIGEST_LENGTH);
7865 staged_blob_idp = &staged_blob_id;
7868 if (got_fileindex_entry_has_commit(ie)) {
7869 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7870 commit_idp = &commit_id;
7873 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7874 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7877 const struct got_error *
7878 got_worktree_path_info(struct got_worktree *worktree,
7879 struct got_pathlist_head *paths,
7880 got_worktree_path_info_cb info_cb, void *info_arg,
7881 got_cancel_cb cancel_cb, void *cancel_arg)
7884 const struct got_error *err = NULL, *unlockerr;
7885 struct got_fileindex *fileindex = NULL;
7886 char *fileindex_path = NULL;
7887 struct report_file_info_arg arg;
7889 err = lock_worktree(worktree, LOCK_SH);
7890 if (err)
7891 return err;
7893 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7894 if (err)
7895 goto done;
7897 arg.worktree = worktree;
7898 arg.info_cb = info_cb;
7899 arg.info_arg = info_arg;
7900 arg.paths = paths;
7901 arg.cancel_cb = cancel_cb;
7902 arg.cancel_arg = cancel_arg;
7903 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7904 &arg);
7905 done:
7906 free(fileindex_path);
7907 if (fileindex)
7908 got_fileindex_free(fileindex);
7909 unlockerr = lock_worktree(worktree, LOCK_UN);
7910 if (unlockerr && err == NULL)
7911 err = unlockerr;
7912 return err;