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, int *nchunks_used,
4140 struct diff_result *diff_result, int n,
4141 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4142 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4143 got_worktree_patch_cb patch_cb, void *patch_arg)
4145 const struct got_error *err = NULL;
4146 struct diff_chunk_context cc = {};
4147 int start_old, end_old, start_new, end_new;
4148 FILE *hunkfile;
4149 struct diff_output_unidiff_state *diff_state;
4150 struct diff_input_info diff_info;
4151 int rc;
4153 *choice = GOT_PATCH_CHOICE_NONE;
4155 /* Get changed line numbers without context lines for copy_change(). */
4156 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4157 start_old = cc.left.start;
4158 end_old = cc.left.end;
4159 start_new = cc.right.start;
4160 end_new = cc.right.end;
4162 /* Get the same change with context lines for display. */
4163 memset(&cc, 0, sizeof(cc));
4164 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4166 memset(&diff_info, 0, sizeof(diff_info));
4167 diff_info.left_path = relpath;
4168 diff_info.right_path = relpath;
4170 diff_state = diff_output_unidiff_state_alloc();
4171 if (diff_state == NULL)
4172 return got_error_set_errno(ENOMEM,
4173 "diff_output_unidiff_state_alloc");
4175 hunkfile = got_opentemp();
4176 if (hunkfile == NULL) {
4177 err = got_error_from_errno("got_opentemp");
4178 goto done;
4181 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4182 diff_result, &cc);
4183 if (rc != DIFF_RC_OK) {
4184 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4185 goto done;
4188 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4189 err = got_ferror(hunkfile, GOT_ERR_IO);
4190 goto done;
4193 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4194 hunkfile, changeno, nchanges);
4195 if (err)
4196 goto done;
4198 switch (*choice) {
4199 case GOT_PATCH_CHOICE_YES:
4200 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4201 end_old, start_new, end_new, outfile, rejectfile);
4202 break;
4203 case GOT_PATCH_CHOICE_NO:
4204 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4205 end_old, start_new, end_new, rejectfile, outfile);
4206 break;
4207 case GOT_PATCH_CHOICE_QUIT:
4208 break;
4209 default:
4210 err = got_error(GOT_ERR_PATCH_CHOICE);
4211 break;
4213 done:
4214 diff_output_unidiff_state_free(diff_state);
4215 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4216 err = got_error_from_errno("fclose");
4217 return err;
4220 struct revert_file_args {
4221 struct got_worktree *worktree;
4222 struct got_fileindex *fileindex;
4223 got_worktree_checkout_cb progress_cb;
4224 void *progress_arg;
4225 got_worktree_patch_cb patch_cb;
4226 void *patch_arg;
4227 struct got_repository *repo;
4230 static const struct got_error *
4231 create_patched_content(char **path_outfile, int reverse_patch,
4232 struct got_object_id *blob_id, const char *path2,
4233 int dirfd2, const char *de_name2,
4234 const char *relpath, struct got_repository *repo,
4235 got_worktree_patch_cb patch_cb, void *patch_arg)
4237 const struct got_error *err, *free_err;
4238 struct got_blob_object *blob = NULL;
4239 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4240 int fd2 = -1;
4241 char link_target[PATH_MAX];
4242 ssize_t link_len = 0;
4243 char *path1 = NULL, *id_str = NULL;
4244 struct stat sb2;
4245 struct got_diffreg_result *diffreg_result = NULL;
4246 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4247 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4249 *path_outfile = NULL;
4251 err = got_object_id_str(&id_str, blob_id);
4252 if (err)
4253 return err;
4255 if (dirfd2 != -1) {
4256 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4257 if (fd2 == -1) {
4258 if (errno != ELOOP) {
4259 err = got_error_from_errno2("openat", path2);
4260 goto done;
4262 link_len = readlinkat(dirfd2, de_name2,
4263 link_target, sizeof(link_target));
4264 if (link_len == -1)
4265 return got_error_from_errno2("readlinkat", path2);
4266 sb2.st_mode = S_IFLNK;
4267 sb2.st_size = link_len;
4269 } else {
4270 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4271 if (fd2 == -1) {
4272 if (errno != ELOOP) {
4273 err = got_error_from_errno2("open", path2);
4274 goto done;
4276 link_len = readlink(path2, link_target,
4277 sizeof(link_target));
4278 if (link_len == -1)
4279 return got_error_from_errno2("readlink", path2);
4280 sb2.st_mode = S_IFLNK;
4281 sb2.st_size = link_len;
4284 if (fd2 != -1) {
4285 if (fstat(fd2, &sb2) == -1) {
4286 err = got_error_from_errno2("fstat", path2);
4287 goto done;
4290 f2 = fdopen(fd2, "r");
4291 if (f2 == NULL) {
4292 err = got_error_from_errno2("fdopen", path2);
4293 goto done;
4295 fd2 = -1;
4296 } else {
4297 size_t n;
4298 f2 = got_opentemp();
4299 if (f2 == NULL) {
4300 err = got_error_from_errno2("got_opentemp", path2);
4301 goto done;
4303 n = fwrite(link_target, 1, link_len, f2);
4304 if (n != link_len) {
4305 err = got_ferror(f2, GOT_ERR_IO);
4306 goto done;
4308 if (fflush(f2) == EOF) {
4309 err = got_error_from_errno("fflush");
4310 goto done;
4312 rewind(f2);
4315 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4316 if (err)
4317 goto done;
4319 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4320 if (err)
4321 goto done;
4323 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4324 if (err)
4325 goto done;
4327 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0,
4328 NULL);
4329 if (err)
4330 goto done;
4332 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4333 if (err)
4334 goto done;
4336 if (fseek(f1, 0L, SEEK_SET) == -1)
4337 return got_ferror(f1, GOT_ERR_IO);
4338 if (fseek(f2, 0L, SEEK_SET) == -1)
4339 return got_ferror(f2, GOT_ERR_IO);
4341 /* Count the number of actual changes in the diff result. */
4342 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4343 struct diff_chunk_context cc = {};
4344 diff_chunk_context_load_change(&cc, &nchunks_used,
4345 diffreg_result->result, n, 0);
4346 nchanges++;
4348 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4349 int choice;
4350 err = apply_or_reject_change(&choice, &nchunks_used,
4351 diffreg_result->result, n, relpath, f1, f2,
4352 &line_cur1, &line_cur2,
4353 reverse_patch ? NULL : outfile,
4354 reverse_patch ? outfile : NULL,
4355 ++i, nchanges, patch_cb, patch_arg);
4356 if (err)
4357 goto done;
4358 if (choice == GOT_PATCH_CHOICE_YES)
4359 have_content = 1;
4360 else if (choice == GOT_PATCH_CHOICE_QUIT)
4361 break;
4363 if (have_content) {
4364 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4365 reverse_patch ? NULL : outfile,
4366 reverse_patch ? outfile : NULL);
4367 if (err)
4368 goto done;
4370 if (!S_ISLNK(sb2.st_mode)) {
4371 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4372 err = got_error_from_errno2("fchmod", path2);
4373 goto done;
4377 done:
4378 free(id_str);
4379 if (blob)
4380 got_object_blob_close(blob);
4381 free_err = got_diffreg_result_free(diffreg_result);
4382 if (err == NULL)
4383 err = free_err;
4384 if (f1 && fclose(f1) == EOF && err == NULL)
4385 err = got_error_from_errno2("fclose", path1);
4386 if (f2 && fclose(f2) == EOF && err == NULL)
4387 err = got_error_from_errno2("fclose", path2);
4388 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4389 err = got_error_from_errno2("close", path2);
4390 if (outfile && fclose(outfile) == EOF && err == NULL)
4391 err = got_error_from_errno2("fclose", *path_outfile);
4392 if (path1 && unlink(path1) == -1 && err == NULL)
4393 err = got_error_from_errno2("unlink", path1);
4394 if (err || !have_content) {
4395 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4396 err = got_error_from_errno2("unlink", *path_outfile);
4397 free(*path_outfile);
4398 *path_outfile = NULL;
4400 free(path1);
4401 return err;
4404 static const struct got_error *
4405 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4406 const char *relpath, struct got_object_id *blob_id,
4407 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4408 int dirfd, const char *de_name)
4410 struct revert_file_args *a = arg;
4411 const struct got_error *err = NULL;
4412 char *parent_path = NULL;
4413 struct got_fileindex_entry *ie;
4414 struct got_tree_object *tree = NULL;
4415 struct got_object_id *tree_id = NULL;
4416 const struct got_tree_entry *te = NULL;
4417 char *tree_path = NULL, *te_name;
4418 char *ondisk_path = NULL, *path_content = NULL;
4419 struct got_blob_object *blob = NULL;
4421 /* Reverting a staged deletion is a no-op. */
4422 if (status == GOT_STATUS_DELETE &&
4423 staged_status != GOT_STATUS_NO_CHANGE)
4424 return NULL;
4426 if (status == GOT_STATUS_UNVERSIONED)
4427 return (*a->progress_cb)(a->progress_arg,
4428 GOT_STATUS_UNVERSIONED, relpath);
4430 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4431 if (ie == NULL)
4432 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4434 /* Construct in-repository path of tree which contains this blob. */
4435 err = got_path_dirname(&parent_path, ie->path);
4436 if (err) {
4437 if (err->code != GOT_ERR_BAD_PATH)
4438 goto done;
4439 parent_path = strdup("/");
4440 if (parent_path == NULL) {
4441 err = got_error_from_errno("strdup");
4442 goto done;
4445 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4446 tree_path = strdup(parent_path);
4447 if (tree_path == NULL) {
4448 err = got_error_from_errno("strdup");
4449 goto done;
4451 } else {
4452 if (got_path_is_root_dir(parent_path)) {
4453 tree_path = strdup(a->worktree->path_prefix);
4454 if (tree_path == NULL) {
4455 err = got_error_from_errno("strdup");
4456 goto done;
4458 } else {
4459 if (asprintf(&tree_path, "%s/%s",
4460 a->worktree->path_prefix, parent_path) == -1) {
4461 err = got_error_from_errno("asprintf");
4462 goto done;
4467 err = got_object_id_by_path(&tree_id, a->repo,
4468 a->worktree->base_commit_id, tree_path);
4469 if (err) {
4470 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4471 (status == GOT_STATUS_ADD ||
4472 staged_status == GOT_STATUS_ADD)))
4473 goto done;
4474 } else {
4475 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4476 if (err)
4477 goto done;
4479 err = got_path_basename(&te_name, ie->path);
4480 if (err)
4481 goto done;
4483 te = got_object_tree_find_entry(tree, te_name);
4484 free(te_name);
4485 if (te == NULL && status != GOT_STATUS_ADD &&
4486 staged_status != GOT_STATUS_ADD) {
4487 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4488 goto done;
4492 switch (status) {
4493 case GOT_STATUS_ADD:
4494 if (a->patch_cb) {
4495 int choice = GOT_PATCH_CHOICE_NONE;
4496 err = (*a->patch_cb)(&choice, a->patch_arg,
4497 status, ie->path, NULL, 1, 1);
4498 if (err)
4499 goto done;
4500 if (choice != GOT_PATCH_CHOICE_YES)
4501 break;
4503 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4504 ie->path);
4505 if (err)
4506 goto done;
4507 got_fileindex_entry_remove(a->fileindex, ie);
4508 break;
4509 case GOT_STATUS_DELETE:
4510 if (a->patch_cb) {
4511 int choice = GOT_PATCH_CHOICE_NONE;
4512 err = (*a->patch_cb)(&choice, a->patch_arg,
4513 status, ie->path, NULL, 1, 1);
4514 if (err)
4515 goto done;
4516 if (choice != GOT_PATCH_CHOICE_YES)
4517 break;
4519 /* fall through */
4520 case GOT_STATUS_MODIFY:
4521 case GOT_STATUS_MODE_CHANGE:
4522 case GOT_STATUS_CONFLICT:
4523 case GOT_STATUS_MISSING: {
4524 struct got_object_id id;
4525 if (staged_status == GOT_STATUS_ADD ||
4526 staged_status == GOT_STATUS_MODIFY) {
4527 memcpy(id.sha1, ie->staged_blob_sha1,
4528 SHA1_DIGEST_LENGTH);
4529 } else
4530 memcpy(id.sha1, ie->blob_sha1,
4531 SHA1_DIGEST_LENGTH);
4532 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4533 if (err)
4534 goto done;
4536 if (asprintf(&ondisk_path, "%s/%s",
4537 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4538 err = got_error_from_errno("asprintf");
4539 goto done;
4542 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4543 status == GOT_STATUS_CONFLICT)) {
4544 int is_bad_symlink = 0;
4545 err = create_patched_content(&path_content, 1, &id,
4546 ondisk_path, dirfd, de_name, ie->path, a->repo,
4547 a->patch_cb, a->patch_arg);
4548 if (err || path_content == NULL)
4549 break;
4550 if (te && S_ISLNK(te->mode)) {
4551 if (unlink(path_content) == -1) {
4552 err = got_error_from_errno2("unlink",
4553 path_content);
4554 break;
4556 err = install_symlink(&is_bad_symlink,
4557 a->worktree, ondisk_path, ie->path,
4558 blob, 0, 1, 0, a->repo,
4559 a->progress_cb, a->progress_arg);
4560 } else {
4561 if (rename(path_content, ondisk_path) == -1) {
4562 err = got_error_from_errno3("rename",
4563 path_content, ondisk_path);
4564 goto done;
4567 } else {
4568 int is_bad_symlink = 0;
4569 if (te && S_ISLNK(te->mode)) {
4570 err = install_symlink(&is_bad_symlink,
4571 a->worktree, ondisk_path, ie->path,
4572 blob, 0, 1, 0, a->repo,
4573 a->progress_cb, a->progress_arg);
4574 } else {
4575 err = install_blob(a->worktree, ondisk_path,
4576 ie->path,
4577 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4578 got_fileindex_perms_to_st(ie), blob,
4579 0, 1, 0, 0, a->repo,
4580 a->progress_cb, a->progress_arg);
4582 if (err)
4583 goto done;
4584 if (status == GOT_STATUS_DELETE ||
4585 status == GOT_STATUS_MODE_CHANGE) {
4586 err = got_fileindex_entry_update(ie,
4587 ondisk_path, blob->id.sha1,
4588 a->worktree->base_commit_id->sha1, 1);
4589 if (err)
4590 goto done;
4592 if (is_bad_symlink) {
4593 got_fileindex_entry_filetype_set(ie,
4594 GOT_FILEIDX_MODE_BAD_SYMLINK);
4597 break;
4599 default:
4600 break;
4602 done:
4603 free(ondisk_path);
4604 free(path_content);
4605 free(parent_path);
4606 free(tree_path);
4607 if (blob)
4608 got_object_blob_close(blob);
4609 if (tree)
4610 got_object_tree_close(tree);
4611 free(tree_id);
4612 return err;
4615 const struct got_error *
4616 got_worktree_revert(struct got_worktree *worktree,
4617 struct got_pathlist_head *paths,
4618 got_worktree_checkout_cb progress_cb, void *progress_arg,
4619 got_worktree_patch_cb patch_cb, void *patch_arg,
4620 struct got_repository *repo)
4622 struct got_fileindex *fileindex = NULL;
4623 char *fileindex_path = NULL;
4624 const struct got_error *err = NULL, *unlockerr = NULL;
4625 const struct got_error *sync_err = NULL;
4626 struct got_pathlist_entry *pe;
4627 struct revert_file_args rfa;
4629 err = lock_worktree(worktree, LOCK_EX);
4630 if (err)
4631 return err;
4633 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4634 if (err)
4635 goto done;
4637 rfa.worktree = worktree;
4638 rfa.fileindex = fileindex;
4639 rfa.progress_cb = progress_cb;
4640 rfa.progress_arg = progress_arg;
4641 rfa.patch_cb = patch_cb;
4642 rfa.patch_arg = patch_arg;
4643 rfa.repo = repo;
4644 TAILQ_FOREACH(pe, paths, entry) {
4645 err = worktree_status(worktree, pe->path, fileindex, repo,
4646 revert_file, &rfa, NULL, NULL, 0, 0);
4647 if (err)
4648 break;
4650 sync_err = sync_fileindex(fileindex, fileindex_path);
4651 if (sync_err && err == NULL)
4652 err = sync_err;
4653 done:
4654 free(fileindex_path);
4655 if (fileindex)
4656 got_fileindex_free(fileindex);
4657 unlockerr = lock_worktree(worktree, LOCK_SH);
4658 if (unlockerr && err == NULL)
4659 err = unlockerr;
4660 return err;
4663 static void
4664 free_commitable(struct got_commitable *ct)
4666 free(ct->path);
4667 free(ct->in_repo_path);
4668 free(ct->ondisk_path);
4669 free(ct->blob_id);
4670 free(ct->base_blob_id);
4671 free(ct->staged_blob_id);
4672 free(ct->base_commit_id);
4673 free(ct);
4676 struct collect_commitables_arg {
4677 struct got_pathlist_head *commitable_paths;
4678 struct got_repository *repo;
4679 struct got_worktree *worktree;
4680 struct got_fileindex *fileindex;
4681 int have_staged_files;
4682 int allow_bad_symlinks;
4685 static const struct got_error *
4686 collect_commitables(void *arg, unsigned char status,
4687 unsigned char staged_status, const char *relpath,
4688 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4689 struct got_object_id *commit_id, int dirfd, const char *de_name)
4691 struct collect_commitables_arg *a = arg;
4692 const struct got_error *err = NULL;
4693 struct got_commitable *ct = NULL;
4694 struct got_pathlist_entry *new = NULL;
4695 char *parent_path = NULL, *path = NULL;
4696 struct stat sb;
4698 if (a->have_staged_files) {
4699 if (staged_status != GOT_STATUS_MODIFY &&
4700 staged_status != GOT_STATUS_ADD &&
4701 staged_status != GOT_STATUS_DELETE)
4702 return NULL;
4703 } else {
4704 if (status == GOT_STATUS_CONFLICT)
4705 return got_error(GOT_ERR_COMMIT_CONFLICT);
4707 if (status != GOT_STATUS_MODIFY &&
4708 status != GOT_STATUS_MODE_CHANGE &&
4709 status != GOT_STATUS_ADD &&
4710 status != GOT_STATUS_DELETE)
4711 return NULL;
4714 if (asprintf(&path, "/%s", relpath) == -1) {
4715 err = got_error_from_errno("asprintf");
4716 goto done;
4718 if (strcmp(path, "/") == 0) {
4719 parent_path = strdup("");
4720 if (parent_path == NULL)
4721 return got_error_from_errno("strdup");
4722 } else {
4723 err = got_path_dirname(&parent_path, path);
4724 if (err)
4725 return err;
4728 ct = calloc(1, sizeof(*ct));
4729 if (ct == NULL) {
4730 err = got_error_from_errno("calloc");
4731 goto done;
4734 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4735 relpath) == -1) {
4736 err = got_error_from_errno("asprintf");
4737 goto done;
4740 if (staged_status == GOT_STATUS_ADD ||
4741 staged_status == GOT_STATUS_MODIFY) {
4742 struct got_fileindex_entry *ie;
4743 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4744 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4745 case GOT_FILEIDX_MODE_REGULAR_FILE:
4746 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4747 ct->mode = S_IFREG;
4748 break;
4749 case GOT_FILEIDX_MODE_SYMLINK:
4750 ct->mode = S_IFLNK;
4751 break;
4752 default:
4753 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4754 goto done;
4756 ct->mode |= got_fileindex_entry_perms_get(ie);
4757 } else if (status != GOT_STATUS_DELETE &&
4758 staged_status != GOT_STATUS_DELETE) {
4759 if (dirfd != -1) {
4760 if (fstatat(dirfd, de_name, &sb,
4761 AT_SYMLINK_NOFOLLOW) == -1) {
4762 err = got_error_from_errno2("fstatat",
4763 ct->ondisk_path);
4764 goto done;
4766 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4767 err = got_error_from_errno2("lstat", ct->ondisk_path);
4768 goto done;
4770 ct->mode = sb.st_mode;
4773 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4774 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4775 relpath) == -1) {
4776 err = got_error_from_errno("asprintf");
4777 goto done;
4780 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4781 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4782 int is_bad_symlink;
4783 char target_path[PATH_MAX];
4784 ssize_t target_len;
4785 target_len = readlink(ct->ondisk_path, target_path,
4786 sizeof(target_path));
4787 if (target_len == -1) {
4788 err = got_error_from_errno2("readlink",
4789 ct->ondisk_path);
4790 goto done;
4792 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4793 target_len, ct->ondisk_path, a->worktree->root_path);
4794 if (err)
4795 goto done;
4796 if (is_bad_symlink) {
4797 err = got_error_path(ct->ondisk_path,
4798 GOT_ERR_BAD_SYMLINK);
4799 goto done;
4804 ct->status = status;
4805 ct->staged_status = staged_status;
4806 ct->blob_id = NULL; /* will be filled in when blob gets created */
4807 if (ct->status != GOT_STATUS_ADD &&
4808 ct->staged_status != GOT_STATUS_ADD) {
4809 ct->base_blob_id = got_object_id_dup(blob_id);
4810 if (ct->base_blob_id == NULL) {
4811 err = got_error_from_errno("got_object_id_dup");
4812 goto done;
4814 ct->base_commit_id = got_object_id_dup(commit_id);
4815 if (ct->base_commit_id == NULL) {
4816 err = got_error_from_errno("got_object_id_dup");
4817 goto done;
4820 if (ct->staged_status == GOT_STATUS_ADD ||
4821 ct->staged_status == GOT_STATUS_MODIFY) {
4822 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4823 if (ct->staged_blob_id == NULL) {
4824 err = got_error_from_errno("got_object_id_dup");
4825 goto done;
4828 ct->path = strdup(path);
4829 if (ct->path == NULL) {
4830 err = got_error_from_errno("strdup");
4831 goto done;
4833 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4834 done:
4835 if (ct && (err || new == NULL))
4836 free_commitable(ct);
4837 free(parent_path);
4838 free(path);
4839 return err;
4842 static const struct got_error *write_tree(struct got_object_id **, int *,
4843 struct got_tree_object *, const char *, struct got_pathlist_head *,
4844 got_worktree_status_cb status_cb, void *status_arg,
4845 struct got_repository *);
4847 static const struct got_error *
4848 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4849 struct got_tree_entry *te, const char *parent_path,
4850 struct got_pathlist_head *commitable_paths,
4851 got_worktree_status_cb status_cb, void *status_arg,
4852 struct got_repository *repo)
4854 const struct got_error *err = NULL;
4855 struct got_tree_object *subtree;
4856 char *subpath;
4858 if (asprintf(&subpath, "%s%s%s", parent_path,
4859 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4860 return got_error_from_errno("asprintf");
4862 err = got_object_open_as_tree(&subtree, repo, &te->id);
4863 if (err)
4864 return err;
4866 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4867 commitable_paths, status_cb, status_arg, repo);
4868 got_object_tree_close(subtree);
4869 free(subpath);
4870 return err;
4873 static const struct got_error *
4874 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4876 const struct got_error *err = NULL;
4877 char *ct_parent_path = NULL;
4879 *match = 0;
4881 if (strchr(ct->in_repo_path, '/') == NULL) {
4882 *match = got_path_is_root_dir(path);
4883 return NULL;
4886 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4887 if (err)
4888 return err;
4889 *match = (strcmp(path, ct_parent_path) == 0);
4890 free(ct_parent_path);
4891 return err;
4894 static mode_t
4895 get_ct_file_mode(struct got_commitable *ct)
4897 if (S_ISLNK(ct->mode))
4898 return S_IFLNK;
4900 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4903 static const struct got_error *
4904 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4905 struct got_tree_entry *te, struct got_commitable *ct)
4907 const struct got_error *err = NULL;
4909 *new_te = NULL;
4911 err = got_object_tree_entry_dup(new_te, te);
4912 if (err)
4913 goto done;
4915 (*new_te)->mode = get_ct_file_mode(ct);
4917 if (ct->staged_status == GOT_STATUS_MODIFY)
4918 memcpy(&(*new_te)->id, ct->staged_blob_id,
4919 sizeof((*new_te)->id));
4920 else
4921 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4922 done:
4923 if (err && *new_te) {
4924 free(*new_te);
4925 *new_te = NULL;
4927 return err;
4930 static const struct got_error *
4931 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4932 struct got_commitable *ct)
4934 const struct got_error *err = NULL;
4935 char *ct_name = NULL;
4937 *new_te = NULL;
4939 *new_te = calloc(1, sizeof(**new_te));
4940 if (*new_te == NULL)
4941 return got_error_from_errno("calloc");
4943 err = got_path_basename(&ct_name, ct->path);
4944 if (err)
4945 goto done;
4946 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4947 sizeof((*new_te)->name)) {
4948 err = got_error(GOT_ERR_NO_SPACE);
4949 goto done;
4952 (*new_te)->mode = get_ct_file_mode(ct);
4954 if (ct->staged_status == GOT_STATUS_ADD)
4955 memcpy(&(*new_te)->id, ct->staged_blob_id,
4956 sizeof((*new_te)->id));
4957 else
4958 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4959 done:
4960 free(ct_name);
4961 if (err && *new_te) {
4962 free(*new_te);
4963 *new_te = NULL;
4965 return err;
4968 static const struct got_error *
4969 insert_tree_entry(struct got_tree_entry *new_te,
4970 struct got_pathlist_head *paths)
4972 const struct got_error *err = NULL;
4973 struct got_pathlist_entry *new_pe;
4975 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4976 if (err)
4977 return err;
4978 if (new_pe == NULL)
4979 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4980 return NULL;
4983 static const struct got_error *
4984 report_ct_status(struct got_commitable *ct,
4985 got_worktree_status_cb status_cb, void *status_arg)
4987 const char *ct_path = ct->path;
4988 unsigned char status;
4990 while (ct_path[0] == '/')
4991 ct_path++;
4993 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4994 status = ct->staged_status;
4995 else
4996 status = ct->status;
4998 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4999 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5002 static const struct got_error *
5003 match_modified_subtree(int *modified, struct got_tree_entry *te,
5004 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5006 const struct got_error *err = NULL;
5007 struct got_pathlist_entry *pe;
5008 char *te_path;
5010 *modified = 0;
5012 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5013 got_path_is_root_dir(base_tree_path) ? "" : "/",
5014 te->name) == -1)
5015 return got_error_from_errno("asprintf");
5017 TAILQ_FOREACH(pe, commitable_paths, entry) {
5018 struct got_commitable *ct = pe->data;
5019 *modified = got_path_is_child(ct->in_repo_path, te_path,
5020 strlen(te_path));
5021 if (*modified)
5022 break;
5025 free(te_path);
5026 return err;
5029 static const struct got_error *
5030 match_deleted_or_modified_ct(struct got_commitable **ctp,
5031 struct got_tree_entry *te, const char *base_tree_path,
5032 struct got_pathlist_head *commitable_paths)
5034 const struct got_error *err = NULL;
5035 struct got_pathlist_entry *pe;
5037 *ctp = NULL;
5039 TAILQ_FOREACH(pe, commitable_paths, entry) {
5040 struct got_commitable *ct = pe->data;
5041 char *ct_name = NULL;
5042 int path_matches;
5044 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5045 if (ct->status != GOT_STATUS_MODIFY &&
5046 ct->status != GOT_STATUS_MODE_CHANGE &&
5047 ct->status != GOT_STATUS_DELETE)
5048 continue;
5049 } else {
5050 if (ct->staged_status != GOT_STATUS_MODIFY &&
5051 ct->staged_status != GOT_STATUS_DELETE)
5052 continue;
5055 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5056 continue;
5058 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5059 if (err)
5060 return err;
5061 if (!path_matches)
5062 continue;
5064 err = got_path_basename(&ct_name, pe->path);
5065 if (err)
5066 return err;
5068 if (strcmp(te->name, ct_name) != 0) {
5069 free(ct_name);
5070 continue;
5072 free(ct_name);
5074 *ctp = ct;
5075 break;
5078 return err;
5081 static const struct got_error *
5082 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5083 const char *child_path, const char *path_base_tree,
5084 struct got_pathlist_head *commitable_paths,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *repo)
5088 const struct got_error *err = NULL;
5089 struct got_tree_entry *new_te;
5090 char *subtree_path;
5091 struct got_object_id *id = NULL;
5092 int nentries;
5094 *new_tep = NULL;
5096 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5097 got_path_is_root_dir(path_base_tree) ? "" : "/",
5098 child_path) == -1)
5099 return got_error_from_errno("asprintf");
5101 new_te = calloc(1, sizeof(*new_te));
5102 if (new_te == NULL)
5103 return got_error_from_errno("calloc");
5104 new_te->mode = S_IFDIR;
5106 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5107 sizeof(new_te->name)) {
5108 err = got_error(GOT_ERR_NO_SPACE);
5109 goto done;
5111 err = write_tree(&id, &nentries, NULL, subtree_path,
5112 commitable_paths, status_cb, status_arg, repo);
5113 if (err) {
5114 free(new_te);
5115 goto done;
5117 memcpy(&new_te->id, id, sizeof(new_te->id));
5118 done:
5119 free(id);
5120 free(subtree_path);
5121 if (err == NULL)
5122 *new_tep = new_te;
5123 return err;
5126 static const struct got_error *
5127 write_tree(struct got_object_id **new_tree_id, int *nentries,
5128 struct got_tree_object *base_tree, const char *path_base_tree,
5129 struct got_pathlist_head *commitable_paths,
5130 got_worktree_status_cb status_cb, void *status_arg,
5131 struct got_repository *repo)
5133 const struct got_error *err = NULL;
5134 struct got_pathlist_head paths;
5135 struct got_tree_entry *te, *new_te = NULL;
5136 struct got_pathlist_entry *pe;
5138 TAILQ_INIT(&paths);
5139 *nentries = 0;
5141 /* Insert, and recurse into, newly added entries first. */
5142 TAILQ_FOREACH(pe, commitable_paths, entry) {
5143 struct got_commitable *ct = pe->data;
5144 char *child_path = NULL, *slash;
5146 if ((ct->status != GOT_STATUS_ADD &&
5147 ct->staged_status != GOT_STATUS_ADD) ||
5148 (ct->flags & GOT_COMMITABLE_ADDED))
5149 continue;
5151 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5152 strlen(path_base_tree)))
5153 continue;
5155 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5156 ct->in_repo_path);
5157 if (err)
5158 goto done;
5160 slash = strchr(child_path, '/');
5161 if (slash == NULL) {
5162 err = alloc_added_blob_tree_entry(&new_te, ct);
5163 if (err)
5164 goto done;
5165 err = report_ct_status(ct, status_cb, status_arg);
5166 if (err)
5167 goto done;
5168 ct->flags |= GOT_COMMITABLE_ADDED;
5169 err = insert_tree_entry(new_te, &paths);
5170 if (err)
5171 goto done;
5172 (*nentries)++;
5173 } else {
5174 *slash = '\0'; /* trim trailing path components */
5175 if (base_tree == NULL ||
5176 got_object_tree_find_entry(base_tree, child_path)
5177 == NULL) {
5178 err = make_subtree_for_added_blob(&new_te,
5179 child_path, path_base_tree,
5180 commitable_paths, status_cb, status_arg,
5181 repo);
5182 if (err)
5183 goto done;
5184 err = insert_tree_entry(new_te, &paths);
5185 if (err)
5186 goto done;
5187 (*nentries)++;
5192 if (base_tree) {
5193 int i, nbase_entries;
5194 /* Handle modified and deleted entries. */
5195 nbase_entries = got_object_tree_get_nentries(base_tree);
5196 for (i = 0; i < nbase_entries; i++) {
5197 struct got_commitable *ct = NULL;
5199 te = got_object_tree_get_entry(base_tree, i);
5200 if (got_object_tree_entry_is_submodule(te)) {
5201 /* Entry is a submodule; just copy it. */
5202 err = got_object_tree_entry_dup(&new_te, te);
5203 if (err)
5204 goto done;
5205 err = insert_tree_entry(new_te, &paths);
5206 if (err)
5207 goto done;
5208 (*nentries)++;
5209 continue;
5212 if (S_ISDIR(te->mode)) {
5213 int modified;
5214 err = got_object_tree_entry_dup(&new_te, te);
5215 if (err)
5216 goto done;
5217 err = match_modified_subtree(&modified, te,
5218 path_base_tree, commitable_paths);
5219 if (err)
5220 goto done;
5221 /* Avoid recursion into unmodified subtrees. */
5222 if (modified) {
5223 struct got_object_id *new_id;
5224 int nsubentries;
5225 err = write_subtree(&new_id,
5226 &nsubentries, te,
5227 path_base_tree, commitable_paths,
5228 status_cb, status_arg, repo);
5229 if (err)
5230 goto done;
5231 if (nsubentries == 0) {
5232 /* All entries were deleted. */
5233 free(new_id);
5234 continue;
5236 memcpy(&new_te->id, new_id,
5237 sizeof(new_te->id));
5238 free(new_id);
5240 err = insert_tree_entry(new_te, &paths);
5241 if (err)
5242 goto done;
5243 (*nentries)++;
5244 continue;
5247 err = match_deleted_or_modified_ct(&ct, te,
5248 path_base_tree, commitable_paths);
5249 if (err)
5250 goto done;
5251 if (ct) {
5252 /* NB: Deleted entries get dropped here. */
5253 if (ct->status == GOT_STATUS_MODIFY ||
5254 ct->status == GOT_STATUS_MODE_CHANGE ||
5255 ct->staged_status == GOT_STATUS_MODIFY) {
5256 err = alloc_modified_blob_tree_entry(
5257 &new_te, te, ct);
5258 if (err)
5259 goto done;
5260 err = insert_tree_entry(new_te, &paths);
5261 if (err)
5262 goto done;
5263 (*nentries)++;
5265 err = report_ct_status(ct, status_cb,
5266 status_arg);
5267 if (err)
5268 goto done;
5269 } else {
5270 /* Entry is unchanged; just copy it. */
5271 err = got_object_tree_entry_dup(&new_te, te);
5272 if (err)
5273 goto done;
5274 err = insert_tree_entry(new_te, &paths);
5275 if (err)
5276 goto done;
5277 (*nentries)++;
5282 /* Write new list of entries; deleted entries have been dropped. */
5283 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5284 done:
5285 got_pathlist_free(&paths);
5286 return err;
5289 static const struct got_error *
5290 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5291 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5292 int have_staged_files)
5294 const struct got_error *err = NULL;
5295 struct got_pathlist_entry *pe;
5297 TAILQ_FOREACH(pe, commitable_paths, entry) {
5298 struct got_fileindex_entry *ie;
5299 struct got_commitable *ct = pe->data;
5301 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5302 if (ie) {
5303 if (ct->status == GOT_STATUS_DELETE ||
5304 ct->staged_status == GOT_STATUS_DELETE) {
5305 got_fileindex_entry_remove(fileindex, ie);
5306 } else if (ct->staged_status == GOT_STATUS_ADD ||
5307 ct->staged_status == GOT_STATUS_MODIFY) {
5308 got_fileindex_entry_stage_set(ie,
5309 GOT_FILEIDX_STAGE_NONE);
5310 got_fileindex_entry_staged_filetype_set(ie, 0);
5311 err = got_fileindex_entry_update(ie,
5312 ct->ondisk_path, ct->staged_blob_id->sha1,
5313 new_base_commit_id->sha1,
5314 !have_staged_files);
5315 } else
5316 err = got_fileindex_entry_update(ie,
5317 ct->ondisk_path, ct->blob_id->sha1,
5318 new_base_commit_id->sha1,
5319 !have_staged_files);
5320 } else {
5321 err = got_fileindex_entry_alloc(&ie, pe->path);
5322 if (err)
5323 break;
5324 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5325 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5326 if (err) {
5327 got_fileindex_entry_free(ie);
5328 break;
5330 err = got_fileindex_entry_add(fileindex, ie);
5331 if (err) {
5332 got_fileindex_entry_free(ie);
5333 break;
5337 return err;
5341 static const struct got_error *
5342 check_out_of_date(const char *in_repo_path, unsigned char status,
5343 unsigned char staged_status, struct got_object_id *base_blob_id,
5344 struct got_object_id *base_commit_id,
5345 struct got_object_id *head_commit_id, struct got_repository *repo,
5346 int ood_errcode)
5348 const struct got_error *err = NULL;
5349 struct got_object_id *id = NULL;
5351 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5352 /* Trivial case: base commit == head commit */
5353 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5354 return NULL;
5356 * Ensure file content which local changes were based
5357 * on matches file content in the branch head.
5359 err = got_object_id_by_path(&id, repo, head_commit_id,
5360 in_repo_path);
5361 if (err) {
5362 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5363 err = got_error(ood_errcode);
5364 goto done;
5365 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5366 err = got_error(ood_errcode);
5367 } else {
5368 /* Require that added files don't exist in the branch head. */
5369 err = got_object_id_by_path(&id, repo, head_commit_id,
5370 in_repo_path);
5371 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5372 goto done;
5373 err = id ? got_error(ood_errcode) : NULL;
5375 done:
5376 free(id);
5377 return err;
5380 const struct got_error *
5381 commit_worktree(struct got_object_id **new_commit_id,
5382 struct got_pathlist_head *commitable_paths,
5383 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5384 const char *author, const char *committer,
5385 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5386 got_worktree_status_cb status_cb, void *status_arg,
5387 struct got_repository *repo)
5389 const struct got_error *err = NULL, *unlockerr = NULL;
5390 struct got_pathlist_entry *pe;
5391 const char *head_ref_name = NULL;
5392 struct got_commit_object *head_commit = NULL;
5393 struct got_reference *head_ref2 = NULL;
5394 struct got_object_id *head_commit_id2 = NULL;
5395 struct got_tree_object *head_tree = NULL;
5396 struct got_object_id *new_tree_id = NULL;
5397 int nentries;
5398 struct got_object_id_queue parent_ids;
5399 struct got_object_qid *pid = NULL;
5400 char *logmsg = NULL;
5402 *new_commit_id = NULL;
5404 SIMPLEQ_INIT(&parent_ids);
5406 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5407 if (err)
5408 goto done;
5410 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5411 if (err)
5412 goto done;
5414 if (commit_msg_cb != NULL) {
5415 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5416 if (err)
5417 goto done;
5420 if (logmsg == NULL || strlen(logmsg) == 0) {
5421 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5422 goto done;
5425 /* Create blobs from added and modified files and record their IDs. */
5426 TAILQ_FOREACH(pe, commitable_paths, entry) {
5427 struct got_commitable *ct = pe->data;
5428 char *ondisk_path;
5430 /* Blobs for staged files already exist. */
5431 if (ct->staged_status == GOT_STATUS_ADD ||
5432 ct->staged_status == GOT_STATUS_MODIFY)
5433 continue;
5435 if (ct->status != GOT_STATUS_ADD &&
5436 ct->status != GOT_STATUS_MODIFY &&
5437 ct->status != GOT_STATUS_MODE_CHANGE)
5438 continue;
5440 if (asprintf(&ondisk_path, "%s/%s",
5441 worktree->root_path, pe->path) == -1) {
5442 err = got_error_from_errno("asprintf");
5443 goto done;
5445 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5446 free(ondisk_path);
5447 if (err)
5448 goto done;
5451 /* Recursively write new tree objects. */
5452 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5453 commitable_paths, status_cb, status_arg, repo);
5454 if (err)
5455 goto done;
5457 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5458 if (err)
5459 goto done;
5460 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5461 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5462 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5463 got_object_qid_free(pid);
5464 if (logmsg != NULL)
5465 free(logmsg);
5466 if (err)
5467 goto done;
5469 /* Check if a concurrent commit to our branch has occurred. */
5470 head_ref_name = got_worktree_get_head_ref_name(worktree);
5471 if (head_ref_name == NULL) {
5472 err = got_error_from_errno("got_worktree_get_head_ref_name");
5473 goto done;
5475 /* Lock the reference here to prevent concurrent modification. */
5476 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5477 if (err)
5478 goto done;
5479 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5480 if (err)
5481 goto done;
5482 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5483 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5484 goto done;
5486 /* Update branch head in repository. */
5487 err = got_ref_change_ref(head_ref2, *new_commit_id);
5488 if (err)
5489 goto done;
5490 err = got_ref_write(head_ref2, repo);
5491 if (err)
5492 goto done;
5494 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5495 if (err)
5496 goto done;
5498 err = ref_base_commit(worktree, repo);
5499 if (err)
5500 goto done;
5501 done:
5502 if (head_tree)
5503 got_object_tree_close(head_tree);
5504 if (head_commit)
5505 got_object_commit_close(head_commit);
5506 free(head_commit_id2);
5507 if (head_ref2) {
5508 unlockerr = got_ref_unlock(head_ref2);
5509 if (unlockerr && err == NULL)
5510 err = unlockerr;
5511 got_ref_close(head_ref2);
5513 return err;
5516 static const struct got_error *
5517 check_path_is_commitable(const char *path,
5518 struct got_pathlist_head *commitable_paths)
5520 struct got_pathlist_entry *cpe = NULL;
5521 size_t path_len = strlen(path);
5523 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5524 struct got_commitable *ct = cpe->data;
5525 const char *ct_path = ct->path;
5527 while (ct_path[0] == '/')
5528 ct_path++;
5530 if (strcmp(path, ct_path) == 0 ||
5531 got_path_is_child(ct_path, path, path_len))
5532 break;
5535 if (cpe == NULL)
5536 return got_error_path(path, GOT_ERR_BAD_PATH);
5538 return NULL;
5541 static const struct got_error *
5542 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5544 int *have_staged_files = arg;
5546 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5547 *have_staged_files = 1;
5548 return got_error(GOT_ERR_CANCELLED);
5551 return NULL;
5554 static const struct got_error *
5555 check_non_staged_files(struct got_fileindex *fileindex,
5556 struct got_pathlist_head *paths)
5558 struct got_pathlist_entry *pe;
5559 struct got_fileindex_entry *ie;
5561 TAILQ_FOREACH(pe, paths, entry) {
5562 if (pe->path[0] == '\0')
5563 continue;
5564 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5565 if (ie == NULL)
5566 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5567 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5568 return got_error_path(pe->path,
5569 GOT_ERR_FILE_NOT_STAGED);
5572 return NULL;
5575 const struct got_error *
5576 got_worktree_commit(struct got_object_id **new_commit_id,
5577 struct got_worktree *worktree, struct got_pathlist_head *paths,
5578 const char *author, const char *committer, int allow_bad_symlinks,
5579 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5580 got_worktree_status_cb status_cb, void *status_arg,
5581 struct got_repository *repo)
5583 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5584 struct got_fileindex *fileindex = NULL;
5585 char *fileindex_path = NULL;
5586 struct got_pathlist_head commitable_paths;
5587 struct collect_commitables_arg cc_arg;
5588 struct got_pathlist_entry *pe;
5589 struct got_reference *head_ref = NULL;
5590 struct got_object_id *head_commit_id = NULL;
5591 int have_staged_files = 0;
5593 *new_commit_id = NULL;
5595 TAILQ_INIT(&commitable_paths);
5597 err = lock_worktree(worktree, LOCK_EX);
5598 if (err)
5599 goto done;
5601 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5602 if (err)
5603 goto done;
5605 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5606 if (err)
5607 goto done;
5609 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5610 if (err)
5611 goto done;
5613 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5614 &have_staged_files);
5615 if (err && err->code != GOT_ERR_CANCELLED)
5616 goto done;
5617 if (have_staged_files) {
5618 err = check_non_staged_files(fileindex, paths);
5619 if (err)
5620 goto done;
5623 cc_arg.commitable_paths = &commitable_paths;
5624 cc_arg.worktree = worktree;
5625 cc_arg.fileindex = fileindex;
5626 cc_arg.repo = repo;
5627 cc_arg.have_staged_files = have_staged_files;
5628 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5629 TAILQ_FOREACH(pe, paths, entry) {
5630 err = worktree_status(worktree, pe->path, fileindex, repo,
5631 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5632 if (err)
5633 goto done;
5636 if (TAILQ_EMPTY(&commitable_paths)) {
5637 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5638 goto done;
5641 TAILQ_FOREACH(pe, paths, entry) {
5642 err = check_path_is_commitable(pe->path, &commitable_paths);
5643 if (err)
5644 goto done;
5647 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5648 struct got_commitable *ct = pe->data;
5649 const char *ct_path = ct->in_repo_path;
5651 while (ct_path[0] == '/')
5652 ct_path++;
5653 err = check_out_of_date(ct_path, ct->status,
5654 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5655 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5656 if (err)
5657 goto done;
5661 err = commit_worktree(new_commit_id, &commitable_paths,
5662 head_commit_id, worktree, author, committer,
5663 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5664 if (err)
5665 goto done;
5667 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5668 fileindex, have_staged_files);
5669 sync_err = sync_fileindex(fileindex, fileindex_path);
5670 if (sync_err && err == NULL)
5671 err = sync_err;
5672 done:
5673 if (fileindex)
5674 got_fileindex_free(fileindex);
5675 free(fileindex_path);
5676 unlockerr = lock_worktree(worktree, LOCK_SH);
5677 if (unlockerr && err == NULL)
5678 err = unlockerr;
5679 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5680 struct got_commitable *ct = pe->data;
5681 free_commitable(ct);
5683 got_pathlist_free(&commitable_paths);
5684 return err;
5687 const char *
5688 got_commitable_get_path(struct got_commitable *ct)
5690 return ct->path;
5693 unsigned int
5694 got_commitable_get_status(struct got_commitable *ct)
5696 return ct->status;
5699 struct check_rebase_ok_arg {
5700 struct got_worktree *worktree;
5701 struct got_repository *repo;
5704 static const struct got_error *
5705 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5707 const struct got_error *err = NULL;
5708 struct check_rebase_ok_arg *a = arg;
5709 unsigned char status;
5710 struct stat sb;
5711 char *ondisk_path;
5713 /* Reject rebase of a work tree with mixed base commits. */
5714 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5715 SHA1_DIGEST_LENGTH))
5716 return got_error(GOT_ERR_MIXED_COMMITS);
5718 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5719 == -1)
5720 return got_error_from_errno("asprintf");
5722 /* Reject rebase of a work tree with modified or staged files. */
5723 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5724 free(ondisk_path);
5725 if (err)
5726 return err;
5728 if (status != GOT_STATUS_NO_CHANGE)
5729 return got_error(GOT_ERR_MODIFIED);
5730 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5731 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5733 return NULL;
5736 const struct got_error *
5737 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5738 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5739 struct got_worktree *worktree, struct got_reference *branch,
5740 struct got_repository *repo)
5742 const struct got_error *err = NULL;
5743 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5744 char *branch_ref_name = NULL;
5745 char *fileindex_path = NULL;
5746 struct check_rebase_ok_arg ok_arg;
5747 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5748 struct got_object_id *wt_branch_tip = NULL;
5750 *new_base_branch_ref = NULL;
5751 *tmp_branch = NULL;
5752 *fileindex = NULL;
5754 err = lock_worktree(worktree, LOCK_EX);
5755 if (err)
5756 return err;
5758 err = open_fileindex(fileindex, &fileindex_path, worktree);
5759 if (err)
5760 goto done;
5762 ok_arg.worktree = worktree;
5763 ok_arg.repo = repo;
5764 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5765 &ok_arg);
5766 if (err)
5767 goto done;
5769 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5770 if (err)
5771 goto done;
5773 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5774 if (err)
5775 goto done;
5777 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5778 if (err)
5779 goto done;
5781 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5782 0);
5783 if (err)
5784 goto done;
5786 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5787 if (err)
5788 goto done;
5789 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5790 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5791 goto done;
5794 err = got_ref_alloc_symref(new_base_branch_ref,
5795 new_base_branch_ref_name, wt_branch);
5796 if (err)
5797 goto done;
5798 err = got_ref_write(*new_base_branch_ref, repo);
5799 if (err)
5800 goto done;
5802 /* TODO Lock original branch's ref while rebasing? */
5804 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5805 if (err)
5806 goto done;
5808 err = got_ref_write(branch_ref, repo);
5809 if (err)
5810 goto done;
5812 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5813 worktree->base_commit_id);
5814 if (err)
5815 goto done;
5816 err = got_ref_write(*tmp_branch, repo);
5817 if (err)
5818 goto done;
5820 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5821 if (err)
5822 goto done;
5823 done:
5824 free(fileindex_path);
5825 free(tmp_branch_name);
5826 free(new_base_branch_ref_name);
5827 free(branch_ref_name);
5828 if (branch_ref)
5829 got_ref_close(branch_ref);
5830 if (wt_branch)
5831 got_ref_close(wt_branch);
5832 free(wt_branch_tip);
5833 if (err) {
5834 if (*new_base_branch_ref) {
5835 got_ref_close(*new_base_branch_ref);
5836 *new_base_branch_ref = NULL;
5838 if (*tmp_branch) {
5839 got_ref_close(*tmp_branch);
5840 *tmp_branch = NULL;
5842 if (*fileindex) {
5843 got_fileindex_free(*fileindex);
5844 *fileindex = NULL;
5846 lock_worktree(worktree, LOCK_SH);
5848 return err;
5851 const struct got_error *
5852 got_worktree_rebase_continue(struct got_object_id **commit_id,
5853 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5854 struct got_reference **branch, struct got_fileindex **fileindex,
5855 struct got_worktree *worktree, struct got_repository *repo)
5857 const struct got_error *err;
5858 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5859 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5860 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5861 char *fileindex_path = NULL;
5862 int have_staged_files = 0;
5864 *commit_id = NULL;
5865 *new_base_branch = NULL;
5866 *tmp_branch = NULL;
5867 *branch = NULL;
5868 *fileindex = NULL;
5870 err = lock_worktree(worktree, LOCK_EX);
5871 if (err)
5872 return err;
5874 err = open_fileindex(fileindex, &fileindex_path, worktree);
5875 if (err)
5876 goto done;
5878 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5879 &have_staged_files);
5880 if (err && err->code != GOT_ERR_CANCELLED)
5881 goto done;
5882 if (have_staged_files) {
5883 err = got_error(GOT_ERR_STAGED_PATHS);
5884 goto done;
5887 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5888 if (err)
5889 goto done;
5891 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5892 if (err)
5893 goto done;
5895 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5896 if (err)
5897 goto done;
5899 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5900 if (err)
5901 goto done;
5903 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5904 if (err)
5905 goto done;
5907 err = got_ref_open(branch, repo,
5908 got_ref_get_symref_target(branch_ref), 0);
5909 if (err)
5910 goto done;
5912 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5913 if (err)
5914 goto done;
5916 err = got_ref_resolve(commit_id, repo, commit_ref);
5917 if (err)
5918 goto done;
5920 err = got_ref_open(new_base_branch, repo,
5921 new_base_branch_ref_name, 0);
5922 if (err)
5923 goto done;
5925 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5926 if (err)
5927 goto done;
5928 done:
5929 free(commit_ref_name);
5930 free(branch_ref_name);
5931 free(fileindex_path);
5932 if (commit_ref)
5933 got_ref_close(commit_ref);
5934 if (branch_ref)
5935 got_ref_close(branch_ref);
5936 if (err) {
5937 free(*commit_id);
5938 *commit_id = NULL;
5939 if (*tmp_branch) {
5940 got_ref_close(*tmp_branch);
5941 *tmp_branch = NULL;
5943 if (*new_base_branch) {
5944 got_ref_close(*new_base_branch);
5945 *new_base_branch = NULL;
5947 if (*branch) {
5948 got_ref_close(*branch);
5949 *branch = NULL;
5951 if (*fileindex) {
5952 got_fileindex_free(*fileindex);
5953 *fileindex = NULL;
5955 lock_worktree(worktree, LOCK_SH);
5957 return err;
5960 const struct got_error *
5961 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5963 const struct got_error *err;
5964 char *tmp_branch_name = NULL;
5966 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5967 if (err)
5968 return err;
5970 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5971 free(tmp_branch_name);
5972 return NULL;
5975 static const struct got_error *
5976 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5977 char **logmsg, void *arg)
5979 *logmsg = arg;
5980 return NULL;
5983 static const struct got_error *
5984 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5985 const char *path, struct got_object_id *blob_id,
5986 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5987 int dirfd, const char *de_name)
5989 return NULL;
5992 struct collect_merged_paths_arg {
5993 got_worktree_checkout_cb progress_cb;
5994 void *progress_arg;
5995 struct got_pathlist_head *merged_paths;
5998 static const struct got_error *
5999 collect_merged_paths(void *arg, unsigned char status, const char *path)
6001 const struct got_error *err;
6002 struct collect_merged_paths_arg *a = arg;
6003 char *p;
6004 struct got_pathlist_entry *new;
6006 err = (*a->progress_cb)(a->progress_arg, status, path);
6007 if (err)
6008 return err;
6010 if (status != GOT_STATUS_MERGE &&
6011 status != GOT_STATUS_ADD &&
6012 status != GOT_STATUS_DELETE &&
6013 status != GOT_STATUS_CONFLICT)
6014 return NULL;
6016 p = strdup(path);
6017 if (p == NULL)
6018 return got_error_from_errno("strdup");
6020 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6021 if (err || new == NULL)
6022 free(p);
6023 return err;
6026 void
6027 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6029 struct got_pathlist_entry *pe;
6031 TAILQ_FOREACH(pe, merged_paths, entry)
6032 free((char *)pe->path);
6034 got_pathlist_free(merged_paths);
6037 static const struct got_error *
6038 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6039 int is_rebase, struct got_repository *repo)
6041 const struct got_error *err;
6042 struct got_reference *commit_ref = NULL;
6044 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6045 if (err) {
6046 if (err->code != GOT_ERR_NOT_REF)
6047 goto done;
6048 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6049 if (err)
6050 goto done;
6051 err = got_ref_write(commit_ref, repo);
6052 if (err)
6053 goto done;
6054 } else if (is_rebase) {
6055 struct got_object_id *stored_id;
6056 int cmp;
6058 err = got_ref_resolve(&stored_id, repo, commit_ref);
6059 if (err)
6060 goto done;
6061 cmp = got_object_id_cmp(commit_id, stored_id);
6062 free(stored_id);
6063 if (cmp != 0) {
6064 err = got_error(GOT_ERR_REBASE_COMMITID);
6065 goto done;
6068 done:
6069 if (commit_ref)
6070 got_ref_close(commit_ref);
6071 return err;
6074 static const struct got_error *
6075 rebase_merge_files(struct got_pathlist_head *merged_paths,
6076 const char *commit_ref_name, struct got_worktree *worktree,
6077 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6078 struct got_object_id *commit_id, struct got_repository *repo,
6079 got_worktree_checkout_cb progress_cb, void *progress_arg,
6080 got_cancel_cb cancel_cb, void *cancel_arg)
6082 const struct got_error *err;
6083 struct got_reference *commit_ref = NULL;
6084 struct collect_merged_paths_arg cmp_arg;
6085 char *fileindex_path;
6087 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6089 err = get_fileindex_path(&fileindex_path, worktree);
6090 if (err)
6091 return err;
6093 cmp_arg.progress_cb = progress_cb;
6094 cmp_arg.progress_arg = progress_arg;
6095 cmp_arg.merged_paths = merged_paths;
6096 err = merge_files(worktree, fileindex, fileindex_path,
6097 parent_commit_id, commit_id, repo, collect_merged_paths,
6098 &cmp_arg, cancel_cb, cancel_arg);
6099 if (commit_ref)
6100 got_ref_close(commit_ref);
6101 return err;
6104 const struct got_error *
6105 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6106 struct got_worktree *worktree, struct got_fileindex *fileindex,
6107 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6108 struct got_repository *repo,
6109 got_worktree_checkout_cb progress_cb, void *progress_arg,
6110 got_cancel_cb cancel_cb, void *cancel_arg)
6112 const struct got_error *err;
6113 char *commit_ref_name;
6115 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6116 if (err)
6117 return err;
6119 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6120 if (err)
6121 goto done;
6123 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6124 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6125 progress_arg, cancel_cb, cancel_arg);
6126 done:
6127 free(commit_ref_name);
6128 return err;
6131 const struct got_error *
6132 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6133 struct got_worktree *worktree, struct got_fileindex *fileindex,
6134 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6135 struct got_repository *repo,
6136 got_worktree_checkout_cb progress_cb, void *progress_arg,
6137 got_cancel_cb cancel_cb, void *cancel_arg)
6139 const struct got_error *err;
6140 char *commit_ref_name;
6142 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6143 if (err)
6144 return err;
6146 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6147 if (err)
6148 goto done;
6150 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6151 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6152 progress_arg, cancel_cb, cancel_arg);
6153 done:
6154 free(commit_ref_name);
6155 return err;
6158 static const struct got_error *
6159 rebase_commit(struct got_object_id **new_commit_id,
6160 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6161 struct got_worktree *worktree, struct got_fileindex *fileindex,
6162 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6163 const char *new_logmsg, struct got_repository *repo)
6165 const struct got_error *err, *sync_err;
6166 struct got_pathlist_head commitable_paths;
6167 struct collect_commitables_arg cc_arg;
6168 char *fileindex_path = NULL;
6169 struct got_reference *head_ref = NULL;
6170 struct got_object_id *head_commit_id = NULL;
6171 char *logmsg = NULL;
6173 TAILQ_INIT(&commitable_paths);
6174 *new_commit_id = NULL;
6176 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6178 err = get_fileindex_path(&fileindex_path, worktree);
6179 if (err)
6180 return err;
6182 cc_arg.commitable_paths = &commitable_paths;
6183 cc_arg.worktree = worktree;
6184 cc_arg.repo = repo;
6185 cc_arg.have_staged_files = 0;
6187 * If possible get the status of individual files directly to
6188 * avoid crawling the entire work tree once per rebased commit.
6189 * TODO: Ideally, merged_paths would contain a list of commitables
6190 * we could use so we could skip worktree_status() entirely.
6192 if (merged_paths) {
6193 struct got_pathlist_entry *pe;
6194 TAILQ_FOREACH(pe, merged_paths, entry) {
6195 err = worktree_status(worktree, pe->path, fileindex,
6196 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6197 0);
6198 if (err)
6199 goto done;
6201 } else {
6202 err = worktree_status(worktree, "", fileindex, repo,
6203 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6204 if (err)
6205 goto done;
6208 if (TAILQ_EMPTY(&commitable_paths)) {
6209 /* No-op change; commit will be elided. */
6210 err = got_ref_delete(commit_ref, repo);
6211 if (err)
6212 goto done;
6213 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6214 goto done;
6217 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6218 if (err)
6219 goto done;
6221 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6222 if (err)
6223 goto done;
6225 if (new_logmsg) {
6226 logmsg = strdup(new_logmsg);
6227 if (logmsg == NULL) {
6228 err = got_error_from_errno("strdup");
6229 goto done;
6231 } else {
6232 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6233 if (err)
6234 goto done;
6237 /* NB: commit_worktree will call free(logmsg) */
6238 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6239 worktree, got_object_commit_get_author(orig_commit),
6240 got_object_commit_get_committer(orig_commit),
6241 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6242 if (err)
6243 goto done;
6245 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6246 if (err)
6247 goto done;
6249 err = got_ref_delete(commit_ref, repo);
6250 if (err)
6251 goto done;
6253 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6254 fileindex, 0);
6255 sync_err = sync_fileindex(fileindex, fileindex_path);
6256 if (sync_err && err == NULL)
6257 err = sync_err;
6258 done:
6259 free(fileindex_path);
6260 free(head_commit_id);
6261 if (head_ref)
6262 got_ref_close(head_ref);
6263 if (err) {
6264 free(*new_commit_id);
6265 *new_commit_id = NULL;
6267 return err;
6270 const struct got_error *
6271 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6272 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6273 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6274 struct got_commit_object *orig_commit,
6275 struct got_object_id *orig_commit_id, struct got_repository *repo)
6277 const struct got_error *err;
6278 char *commit_ref_name;
6279 struct got_reference *commit_ref = NULL;
6280 struct got_object_id *commit_id = NULL;
6282 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6283 if (err)
6284 return err;
6286 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6287 if (err)
6288 goto done;
6289 err = got_ref_resolve(&commit_id, repo, commit_ref);
6290 if (err)
6291 goto done;
6292 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6293 err = got_error(GOT_ERR_REBASE_COMMITID);
6294 goto done;
6297 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6298 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6299 done:
6300 if (commit_ref)
6301 got_ref_close(commit_ref);
6302 free(commit_ref_name);
6303 free(commit_id);
6304 return err;
6307 const struct got_error *
6308 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6309 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6310 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6311 struct got_commit_object *orig_commit,
6312 struct got_object_id *orig_commit_id, const char *new_logmsg,
6313 struct got_repository *repo)
6315 const struct got_error *err;
6316 char *commit_ref_name;
6317 struct got_reference *commit_ref = NULL;
6319 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6320 if (err)
6321 return err;
6323 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6324 if (err)
6325 goto done;
6327 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6328 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6329 done:
6330 if (commit_ref)
6331 got_ref_close(commit_ref);
6332 free(commit_ref_name);
6333 return err;
6336 const struct got_error *
6337 got_worktree_rebase_postpone(struct got_worktree *worktree,
6338 struct got_fileindex *fileindex)
6340 if (fileindex)
6341 got_fileindex_free(fileindex);
6342 return lock_worktree(worktree, LOCK_SH);
6345 static const struct got_error *
6346 delete_ref(const char *name, struct got_repository *repo)
6348 const struct got_error *err;
6349 struct got_reference *ref;
6351 err = got_ref_open(&ref, repo, name, 0);
6352 if (err) {
6353 if (err->code == GOT_ERR_NOT_REF)
6354 return NULL;
6355 return err;
6358 err = got_ref_delete(ref, repo);
6359 got_ref_close(ref);
6360 return err;
6363 static const struct got_error *
6364 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6366 const struct got_error *err;
6367 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6368 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6370 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6371 if (err)
6372 goto done;
6373 err = delete_ref(tmp_branch_name, repo);
6374 if (err)
6375 goto done;
6377 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6378 if (err)
6379 goto done;
6380 err = delete_ref(new_base_branch_ref_name, repo);
6381 if (err)
6382 goto done;
6384 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6385 if (err)
6386 goto done;
6387 err = delete_ref(branch_ref_name, repo);
6388 if (err)
6389 goto done;
6391 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6392 if (err)
6393 goto done;
6394 err = delete_ref(commit_ref_name, repo);
6395 if (err)
6396 goto done;
6398 done:
6399 free(tmp_branch_name);
6400 free(new_base_branch_ref_name);
6401 free(branch_ref_name);
6402 free(commit_ref_name);
6403 return err;
6406 const struct got_error *
6407 got_worktree_rebase_complete(struct got_worktree *worktree,
6408 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6409 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6410 struct got_repository *repo)
6412 const struct got_error *err, *unlockerr;
6413 struct got_object_id *new_head_commit_id = NULL;
6415 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6416 if (err)
6417 return err;
6419 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6420 if (err)
6421 goto done;
6423 err = got_ref_write(rebased_branch, repo);
6424 if (err)
6425 goto done;
6427 err = got_worktree_set_head_ref(worktree, rebased_branch);
6428 if (err)
6429 goto done;
6431 err = delete_rebase_refs(worktree, repo);
6432 done:
6433 if (fileindex)
6434 got_fileindex_free(fileindex);
6435 free(new_head_commit_id);
6436 unlockerr = lock_worktree(worktree, LOCK_SH);
6437 if (unlockerr && err == NULL)
6438 err = unlockerr;
6439 return err;
6442 const struct got_error *
6443 got_worktree_rebase_abort(struct got_worktree *worktree,
6444 struct got_fileindex *fileindex, struct got_repository *repo,
6445 struct got_reference *new_base_branch,
6446 got_worktree_checkout_cb progress_cb, void *progress_arg)
6448 const struct got_error *err, *unlockerr, *sync_err;
6449 struct got_reference *resolved = NULL;
6450 struct got_object_id *commit_id = NULL;
6451 char *fileindex_path = NULL;
6452 struct revert_file_args rfa;
6453 struct got_object_id *tree_id = NULL;
6455 err = lock_worktree(worktree, LOCK_EX);
6456 if (err)
6457 return err;
6459 err = got_ref_open(&resolved, repo,
6460 got_ref_get_symref_target(new_base_branch), 0);
6461 if (err)
6462 goto done;
6464 err = got_worktree_set_head_ref(worktree, resolved);
6465 if (err)
6466 goto done;
6469 * XXX commits to the base branch could have happened while
6470 * we were busy rebasing; should we store the original commit ID
6471 * when rebase begins and read it back here?
6473 err = got_ref_resolve(&commit_id, repo, resolved);
6474 if (err)
6475 goto done;
6477 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6478 if (err)
6479 goto done;
6481 err = got_object_id_by_path(&tree_id, repo,
6482 worktree->base_commit_id, worktree->path_prefix);
6483 if (err)
6484 goto done;
6486 err = delete_rebase_refs(worktree, repo);
6487 if (err)
6488 goto done;
6490 err = get_fileindex_path(&fileindex_path, worktree);
6491 if (err)
6492 goto done;
6494 rfa.worktree = worktree;
6495 rfa.fileindex = fileindex;
6496 rfa.progress_cb = progress_cb;
6497 rfa.progress_arg = progress_arg;
6498 rfa.patch_cb = NULL;
6499 rfa.patch_arg = NULL;
6500 rfa.repo = repo;
6501 err = worktree_status(worktree, "", fileindex, repo,
6502 revert_file, &rfa, NULL, NULL, 0, 0);
6503 if (err)
6504 goto sync;
6506 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6507 repo, progress_cb, progress_arg, NULL, NULL);
6508 sync:
6509 sync_err = sync_fileindex(fileindex, fileindex_path);
6510 if (sync_err && err == NULL)
6511 err = sync_err;
6512 done:
6513 got_ref_close(resolved);
6514 free(tree_id);
6515 free(commit_id);
6516 if (fileindex)
6517 got_fileindex_free(fileindex);
6518 free(fileindex_path);
6520 unlockerr = lock_worktree(worktree, LOCK_SH);
6521 if (unlockerr && err == NULL)
6522 err = unlockerr;
6523 return err;
6526 const struct got_error *
6527 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6528 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6529 struct got_fileindex **fileindex, struct got_worktree *worktree,
6530 struct got_repository *repo)
6532 const struct got_error *err = NULL;
6533 char *tmp_branch_name = NULL;
6534 char *branch_ref_name = NULL;
6535 char *base_commit_ref_name = NULL;
6536 char *fileindex_path = NULL;
6537 struct check_rebase_ok_arg ok_arg;
6538 struct got_reference *wt_branch = NULL;
6539 struct got_reference *base_commit_ref = NULL;
6541 *tmp_branch = NULL;
6542 *branch_ref = NULL;
6543 *base_commit_id = NULL;
6544 *fileindex = NULL;
6546 err = lock_worktree(worktree, LOCK_EX);
6547 if (err)
6548 return err;
6550 err = open_fileindex(fileindex, &fileindex_path, worktree);
6551 if (err)
6552 goto done;
6554 ok_arg.worktree = worktree;
6555 ok_arg.repo = repo;
6556 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6557 &ok_arg);
6558 if (err)
6559 goto done;
6561 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6562 if (err)
6563 goto done;
6565 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6566 if (err)
6567 goto done;
6569 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6570 worktree);
6571 if (err)
6572 goto done;
6574 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6575 0);
6576 if (err)
6577 goto done;
6579 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6580 if (err)
6581 goto done;
6583 err = got_ref_write(*branch_ref, repo);
6584 if (err)
6585 goto done;
6587 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6588 worktree->base_commit_id);
6589 if (err)
6590 goto done;
6591 err = got_ref_write(base_commit_ref, repo);
6592 if (err)
6593 goto done;
6594 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6595 if (*base_commit_id == NULL) {
6596 err = got_error_from_errno("got_object_id_dup");
6597 goto done;
6600 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6601 worktree->base_commit_id);
6602 if (err)
6603 goto done;
6604 err = got_ref_write(*tmp_branch, repo);
6605 if (err)
6606 goto done;
6608 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6609 if (err)
6610 goto done;
6611 done:
6612 free(fileindex_path);
6613 free(tmp_branch_name);
6614 free(branch_ref_name);
6615 free(base_commit_ref_name);
6616 if (wt_branch)
6617 got_ref_close(wt_branch);
6618 if (err) {
6619 if (*branch_ref) {
6620 got_ref_close(*branch_ref);
6621 *branch_ref = NULL;
6623 if (*tmp_branch) {
6624 got_ref_close(*tmp_branch);
6625 *tmp_branch = NULL;
6627 free(*base_commit_id);
6628 if (*fileindex) {
6629 got_fileindex_free(*fileindex);
6630 *fileindex = NULL;
6632 lock_worktree(worktree, LOCK_SH);
6634 return err;
6637 const struct got_error *
6638 got_worktree_histedit_postpone(struct got_worktree *worktree,
6639 struct got_fileindex *fileindex)
6641 if (fileindex)
6642 got_fileindex_free(fileindex);
6643 return lock_worktree(worktree, LOCK_SH);
6646 const struct got_error *
6647 got_worktree_histedit_in_progress(int *in_progress,
6648 struct got_worktree *worktree)
6650 const struct got_error *err;
6651 char *tmp_branch_name = NULL;
6653 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6654 if (err)
6655 return err;
6657 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6658 free(tmp_branch_name);
6659 return NULL;
6662 const struct got_error *
6663 got_worktree_histedit_continue(struct got_object_id **commit_id,
6664 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6665 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6666 struct got_worktree *worktree, struct got_repository *repo)
6668 const struct got_error *err;
6669 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6670 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6671 struct got_reference *commit_ref = NULL;
6672 struct got_reference *base_commit_ref = NULL;
6673 char *fileindex_path = NULL;
6674 int have_staged_files = 0;
6676 *commit_id = NULL;
6677 *tmp_branch = NULL;
6678 *base_commit_id = NULL;
6679 *fileindex = NULL;
6681 err = lock_worktree(worktree, LOCK_EX);
6682 if (err)
6683 return err;
6685 err = open_fileindex(fileindex, &fileindex_path, worktree);
6686 if (err)
6687 goto done;
6689 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6690 &have_staged_files);
6691 if (err && err->code != GOT_ERR_CANCELLED)
6692 goto done;
6693 if (have_staged_files) {
6694 err = got_error(GOT_ERR_STAGED_PATHS);
6695 goto done;
6698 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6699 if (err)
6700 goto done;
6702 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6703 if (err)
6704 goto done;
6706 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6707 if (err)
6708 goto done;
6710 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6711 worktree);
6712 if (err)
6713 goto done;
6715 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6716 if (err)
6717 goto done;
6719 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6720 if (err)
6721 goto done;
6722 err = got_ref_resolve(commit_id, repo, commit_ref);
6723 if (err)
6724 goto done;
6726 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6727 if (err)
6728 goto done;
6729 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6730 if (err)
6731 goto done;
6733 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6734 if (err)
6735 goto done;
6736 done:
6737 free(commit_ref_name);
6738 free(branch_ref_name);
6739 free(fileindex_path);
6740 if (commit_ref)
6741 got_ref_close(commit_ref);
6742 if (base_commit_ref)
6743 got_ref_close(base_commit_ref);
6744 if (err) {
6745 free(*commit_id);
6746 *commit_id = NULL;
6747 free(*base_commit_id);
6748 *base_commit_id = NULL;
6749 if (*tmp_branch) {
6750 got_ref_close(*tmp_branch);
6751 *tmp_branch = NULL;
6753 if (*fileindex) {
6754 got_fileindex_free(*fileindex);
6755 *fileindex = NULL;
6757 lock_worktree(worktree, LOCK_EX);
6759 return err;
6762 static const struct got_error *
6763 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6765 const struct got_error *err;
6766 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6767 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6769 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6770 if (err)
6771 goto done;
6772 err = delete_ref(tmp_branch_name, repo);
6773 if (err)
6774 goto done;
6776 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6777 worktree);
6778 if (err)
6779 goto done;
6780 err = delete_ref(base_commit_ref_name, repo);
6781 if (err)
6782 goto done;
6784 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6785 if (err)
6786 goto done;
6787 err = delete_ref(branch_ref_name, repo);
6788 if (err)
6789 goto done;
6791 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6792 if (err)
6793 goto done;
6794 err = delete_ref(commit_ref_name, repo);
6795 if (err)
6796 goto done;
6797 done:
6798 free(tmp_branch_name);
6799 free(base_commit_ref_name);
6800 free(branch_ref_name);
6801 free(commit_ref_name);
6802 return err;
6805 const struct got_error *
6806 got_worktree_histedit_abort(struct got_worktree *worktree,
6807 struct got_fileindex *fileindex, struct got_repository *repo,
6808 struct got_reference *branch, struct got_object_id *base_commit_id,
6809 got_worktree_checkout_cb progress_cb, void *progress_arg)
6811 const struct got_error *err, *unlockerr, *sync_err;
6812 struct got_reference *resolved = NULL;
6813 char *fileindex_path = NULL;
6814 struct got_object_id *tree_id = NULL;
6815 struct revert_file_args rfa;
6817 err = lock_worktree(worktree, LOCK_EX);
6818 if (err)
6819 return err;
6821 err = got_ref_open(&resolved, repo,
6822 got_ref_get_symref_target(branch), 0);
6823 if (err)
6824 goto done;
6826 err = got_worktree_set_head_ref(worktree, resolved);
6827 if (err)
6828 goto done;
6830 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6831 if (err)
6832 goto done;
6834 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6835 worktree->path_prefix);
6836 if (err)
6837 goto done;
6839 err = delete_histedit_refs(worktree, repo);
6840 if (err)
6841 goto done;
6843 err = get_fileindex_path(&fileindex_path, worktree);
6844 if (err)
6845 goto done;
6847 rfa.worktree = worktree;
6848 rfa.fileindex = fileindex;
6849 rfa.progress_cb = progress_cb;
6850 rfa.progress_arg = progress_arg;
6851 rfa.patch_cb = NULL;
6852 rfa.patch_arg = NULL;
6853 rfa.repo = repo;
6854 err = worktree_status(worktree, "", fileindex, repo,
6855 revert_file, &rfa, NULL, NULL, 0, 0);
6856 if (err)
6857 goto sync;
6859 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6860 repo, progress_cb, progress_arg, NULL, NULL);
6861 sync:
6862 sync_err = sync_fileindex(fileindex, fileindex_path);
6863 if (sync_err && err == NULL)
6864 err = sync_err;
6865 done:
6866 got_ref_close(resolved);
6867 free(tree_id);
6868 free(fileindex_path);
6870 unlockerr = lock_worktree(worktree, LOCK_SH);
6871 if (unlockerr && err == NULL)
6872 err = unlockerr;
6873 return err;
6876 const struct got_error *
6877 got_worktree_histedit_complete(struct got_worktree *worktree,
6878 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6879 struct got_reference *edited_branch, struct got_repository *repo)
6881 const struct got_error *err, *unlockerr;
6882 struct got_object_id *new_head_commit_id = NULL;
6883 struct got_reference *resolved = NULL;
6885 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6886 if (err)
6887 return err;
6889 err = got_ref_open(&resolved, repo,
6890 got_ref_get_symref_target(edited_branch), 0);
6891 if (err)
6892 goto done;
6894 err = got_ref_change_ref(resolved, new_head_commit_id);
6895 if (err)
6896 goto done;
6898 err = got_ref_write(resolved, repo);
6899 if (err)
6900 goto done;
6902 err = got_worktree_set_head_ref(worktree, resolved);
6903 if (err)
6904 goto done;
6906 err = delete_histedit_refs(worktree, repo);
6907 done:
6908 if (fileindex)
6909 got_fileindex_free(fileindex);
6910 free(new_head_commit_id);
6911 unlockerr = lock_worktree(worktree, LOCK_SH);
6912 if (unlockerr && err == NULL)
6913 err = unlockerr;
6914 return err;
6917 const struct got_error *
6918 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6919 struct got_object_id *commit_id, struct got_repository *repo)
6921 const struct got_error *err;
6922 char *commit_ref_name;
6924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6925 if (err)
6926 return err;
6928 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6929 if (err)
6930 goto done;
6932 err = delete_ref(commit_ref_name, repo);
6933 done:
6934 free(commit_ref_name);
6935 return err;
6938 const struct got_error *
6939 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6940 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6941 struct got_worktree *worktree, const char *refname,
6942 struct got_repository *repo)
6944 const struct got_error *err = NULL;
6945 char *fileindex_path = NULL;
6946 struct check_rebase_ok_arg ok_arg;
6948 *fileindex = NULL;
6949 *branch_ref = NULL;
6950 *base_branch_ref = NULL;
6952 err = lock_worktree(worktree, LOCK_EX);
6953 if (err)
6954 return err;
6956 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6957 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6958 "cannot integrate a branch into itself; "
6959 "update -b or different branch name required");
6960 goto done;
6963 err = open_fileindex(fileindex, &fileindex_path, worktree);
6964 if (err)
6965 goto done;
6967 /* Preconditions are the same as for rebase. */
6968 ok_arg.worktree = worktree;
6969 ok_arg.repo = repo;
6970 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6971 &ok_arg);
6972 if (err)
6973 goto done;
6975 err = got_ref_open(branch_ref, repo, refname, 1);
6976 if (err)
6977 goto done;
6979 err = got_ref_open(base_branch_ref, repo,
6980 got_worktree_get_head_ref_name(worktree), 1);
6981 done:
6982 if (err) {
6983 if (*branch_ref) {
6984 got_ref_close(*branch_ref);
6985 *branch_ref = NULL;
6987 if (*base_branch_ref) {
6988 got_ref_close(*base_branch_ref);
6989 *base_branch_ref = NULL;
6991 if (*fileindex) {
6992 got_fileindex_free(*fileindex);
6993 *fileindex = NULL;
6995 lock_worktree(worktree, LOCK_SH);
6997 return err;
7000 const struct got_error *
7001 got_worktree_integrate_continue(struct got_worktree *worktree,
7002 struct got_fileindex *fileindex, struct got_repository *repo,
7003 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7004 got_worktree_checkout_cb progress_cb, void *progress_arg,
7005 got_cancel_cb cancel_cb, void *cancel_arg)
7007 const struct got_error *err = NULL, *sync_err, *unlockerr;
7008 char *fileindex_path = NULL;
7009 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7011 err = get_fileindex_path(&fileindex_path, worktree);
7012 if (err)
7013 goto done;
7015 err = got_ref_resolve(&commit_id, repo, branch_ref);
7016 if (err)
7017 goto done;
7019 err = got_object_id_by_path(&tree_id, repo, commit_id,
7020 worktree->path_prefix);
7021 if (err)
7022 goto done;
7024 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7025 if (err)
7026 goto done;
7028 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7029 progress_cb, progress_arg, cancel_cb, cancel_arg);
7030 if (err)
7031 goto sync;
7033 err = got_ref_change_ref(base_branch_ref, commit_id);
7034 if (err)
7035 goto sync;
7037 err = got_ref_write(base_branch_ref, repo);
7038 sync:
7039 sync_err = sync_fileindex(fileindex, fileindex_path);
7040 if (sync_err && err == NULL)
7041 err = sync_err;
7043 done:
7044 unlockerr = got_ref_unlock(branch_ref);
7045 if (unlockerr && err == NULL)
7046 err = unlockerr;
7047 got_ref_close(branch_ref);
7049 unlockerr = got_ref_unlock(base_branch_ref);
7050 if (unlockerr && err == NULL)
7051 err = unlockerr;
7052 got_ref_close(base_branch_ref);
7054 got_fileindex_free(fileindex);
7055 free(fileindex_path);
7056 free(tree_id);
7058 unlockerr = lock_worktree(worktree, LOCK_SH);
7059 if (unlockerr && err == NULL)
7060 err = unlockerr;
7061 return err;
7064 const struct got_error *
7065 got_worktree_integrate_abort(struct got_worktree *worktree,
7066 struct got_fileindex *fileindex, struct got_repository *repo,
7067 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7069 const struct got_error *err = NULL, *unlockerr = NULL;
7071 got_fileindex_free(fileindex);
7073 err = lock_worktree(worktree, LOCK_SH);
7075 unlockerr = got_ref_unlock(branch_ref);
7076 if (unlockerr && err == NULL)
7077 err = unlockerr;
7078 got_ref_close(branch_ref);
7080 unlockerr = got_ref_unlock(base_branch_ref);
7081 if (unlockerr && err == NULL)
7082 err = unlockerr;
7083 got_ref_close(base_branch_ref);
7085 return err;
7088 struct check_stage_ok_arg {
7089 struct got_object_id *head_commit_id;
7090 struct got_worktree *worktree;
7091 struct got_fileindex *fileindex;
7092 struct got_repository *repo;
7093 int have_changes;
7096 const struct got_error *
7097 check_stage_ok(void *arg, unsigned char status,
7098 unsigned char staged_status, const char *relpath,
7099 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7100 struct got_object_id *commit_id, int dirfd, const char *de_name)
7102 struct check_stage_ok_arg *a = arg;
7103 const struct got_error *err = NULL;
7104 struct got_fileindex_entry *ie;
7105 struct got_object_id base_commit_id;
7106 struct got_object_id *base_commit_idp = NULL;
7107 char *in_repo_path = NULL, *p;
7109 if (status == GOT_STATUS_UNVERSIONED ||
7110 status == GOT_STATUS_NO_CHANGE)
7111 return NULL;
7112 if (status == GOT_STATUS_NONEXISTENT)
7113 return got_error_set_errno(ENOENT, relpath);
7115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7116 if (ie == NULL)
7117 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7119 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7120 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7121 relpath) == -1)
7122 return got_error_from_errno("asprintf");
7124 if (got_fileindex_entry_has_commit(ie)) {
7125 memcpy(base_commit_id.sha1, ie->commit_sha1,
7126 SHA1_DIGEST_LENGTH);
7127 base_commit_idp = &base_commit_id;
7130 if (status == GOT_STATUS_CONFLICT) {
7131 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7132 goto done;
7133 } else if (status != GOT_STATUS_ADD &&
7134 status != GOT_STATUS_MODIFY &&
7135 status != GOT_STATUS_DELETE) {
7136 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7137 goto done;
7140 a->have_changes = 1;
7142 p = in_repo_path;
7143 while (p[0] == '/')
7144 p++;
7145 err = check_out_of_date(p, status, staged_status,
7146 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7147 GOT_ERR_STAGE_OUT_OF_DATE);
7148 done:
7149 free(in_repo_path);
7150 return err;
7153 struct stage_path_arg {
7154 struct got_worktree *worktree;
7155 struct got_fileindex *fileindex;
7156 struct got_repository *repo;
7157 got_worktree_status_cb status_cb;
7158 void *status_arg;
7159 got_worktree_patch_cb patch_cb;
7160 void *patch_arg;
7161 int staged_something;
7162 int allow_bad_symlinks;
7165 static const struct got_error *
7166 stage_path(void *arg, unsigned char status,
7167 unsigned char staged_status, const char *relpath,
7168 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7169 struct got_object_id *commit_id, int dirfd, const char *de_name)
7171 struct stage_path_arg *a = arg;
7172 const struct got_error *err = NULL;
7173 struct got_fileindex_entry *ie;
7174 char *ondisk_path = NULL, *path_content = NULL;
7175 uint32_t stage;
7176 struct got_object_id *new_staged_blob_id = NULL;
7177 struct stat sb;
7179 if (status == GOT_STATUS_UNVERSIONED)
7180 return NULL;
7182 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7183 if (ie == NULL)
7184 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7186 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7187 relpath)== -1)
7188 return got_error_from_errno("asprintf");
7190 switch (status) {
7191 case GOT_STATUS_ADD:
7192 case GOT_STATUS_MODIFY:
7193 /* XXX could sb.st_mode be passed in by our caller? */
7194 if (lstat(ondisk_path, &sb) == -1) {
7195 err = got_error_from_errno2("lstat", ondisk_path);
7196 break;
7198 if (a->patch_cb) {
7199 if (status == GOT_STATUS_ADD) {
7200 int choice = GOT_PATCH_CHOICE_NONE;
7201 err = (*a->patch_cb)(&choice, a->patch_arg,
7202 status, ie->path, NULL, 1, 1);
7203 if (err)
7204 break;
7205 if (choice != GOT_PATCH_CHOICE_YES)
7206 break;
7207 } else {
7208 err = create_patched_content(&path_content, 0,
7209 staged_blob_id ? staged_blob_id : blob_id,
7210 ondisk_path, dirfd, de_name, ie->path,
7211 a->repo, a->patch_cb, a->patch_arg);
7212 if (err || path_content == NULL)
7213 break;
7216 err = got_object_blob_create(&new_staged_blob_id,
7217 path_content ? path_content : ondisk_path, a->repo);
7218 if (err)
7219 break;
7220 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7221 SHA1_DIGEST_LENGTH);
7222 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7223 stage = GOT_FILEIDX_STAGE_ADD;
7224 else
7225 stage = GOT_FILEIDX_STAGE_MODIFY;
7226 got_fileindex_entry_stage_set(ie, stage);
7227 if (S_ISLNK(sb.st_mode)) {
7228 int is_bad_symlink = 0;
7229 if (!a->allow_bad_symlinks) {
7230 char target_path[PATH_MAX];
7231 ssize_t target_len;
7232 target_len = readlink(ondisk_path, target_path,
7233 sizeof(target_path));
7234 if (target_len == -1) {
7235 err = got_error_from_errno2("readlink",
7236 ondisk_path);
7237 break;
7239 err = is_bad_symlink_target(&is_bad_symlink,
7240 target_path, target_len, ondisk_path,
7241 a->worktree->root_path);
7242 if (err)
7243 break;
7244 if (is_bad_symlink) {
7245 err = got_error_path(ondisk_path,
7246 GOT_ERR_BAD_SYMLINK);
7247 break;
7250 if (is_bad_symlink)
7251 got_fileindex_entry_staged_filetype_set(ie,
7252 GOT_FILEIDX_MODE_BAD_SYMLINK);
7253 else
7254 got_fileindex_entry_staged_filetype_set(ie,
7255 GOT_FILEIDX_MODE_SYMLINK);
7256 } else {
7257 got_fileindex_entry_staged_filetype_set(ie,
7258 GOT_FILEIDX_MODE_REGULAR_FILE);
7260 a->staged_something = 1;
7261 if (a->status_cb == NULL)
7262 break;
7263 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7264 get_staged_status(ie), relpath, blob_id,
7265 new_staged_blob_id, NULL, dirfd, de_name);
7266 break;
7267 case GOT_STATUS_DELETE:
7268 if (staged_status == GOT_STATUS_DELETE)
7269 break;
7270 if (a->patch_cb) {
7271 int choice = GOT_PATCH_CHOICE_NONE;
7272 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7273 ie->path, NULL, 1, 1);
7274 if (err)
7275 break;
7276 if (choice == GOT_PATCH_CHOICE_NO)
7277 break;
7278 if (choice != GOT_PATCH_CHOICE_YES) {
7279 err = got_error(GOT_ERR_PATCH_CHOICE);
7280 break;
7283 stage = GOT_FILEIDX_STAGE_DELETE;
7284 got_fileindex_entry_stage_set(ie, stage);
7285 a->staged_something = 1;
7286 if (a->status_cb == NULL)
7287 break;
7288 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7289 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7290 de_name);
7291 break;
7292 case GOT_STATUS_NO_CHANGE:
7293 break;
7294 case GOT_STATUS_CONFLICT:
7295 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7296 break;
7297 case GOT_STATUS_NONEXISTENT:
7298 err = got_error_set_errno(ENOENT, relpath);
7299 break;
7300 default:
7301 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7302 break;
7305 if (path_content && unlink(path_content) == -1 && err == NULL)
7306 err = got_error_from_errno2("unlink", path_content);
7307 free(path_content);
7308 free(ondisk_path);
7309 free(new_staged_blob_id);
7310 return err;
7313 const struct got_error *
7314 got_worktree_stage(struct got_worktree *worktree,
7315 struct got_pathlist_head *paths,
7316 got_worktree_status_cb status_cb, void *status_arg,
7317 got_worktree_patch_cb patch_cb, void *patch_arg,
7318 int allow_bad_symlinks, struct got_repository *repo)
7320 const struct got_error *err = NULL, *sync_err, *unlockerr;
7321 struct got_pathlist_entry *pe;
7322 struct got_fileindex *fileindex = NULL;
7323 char *fileindex_path = NULL;
7324 struct got_reference *head_ref = NULL;
7325 struct got_object_id *head_commit_id = NULL;
7326 struct check_stage_ok_arg oka;
7327 struct stage_path_arg spa;
7329 err = lock_worktree(worktree, LOCK_EX);
7330 if (err)
7331 return err;
7333 err = got_ref_open(&head_ref, repo,
7334 got_worktree_get_head_ref_name(worktree), 0);
7335 if (err)
7336 goto done;
7337 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7338 if (err)
7339 goto done;
7340 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7341 if (err)
7342 goto done;
7344 /* Check pre-conditions before staging anything. */
7345 oka.head_commit_id = head_commit_id;
7346 oka.worktree = worktree;
7347 oka.fileindex = fileindex;
7348 oka.repo = repo;
7349 oka.have_changes = 0;
7350 TAILQ_FOREACH(pe, paths, entry) {
7351 err = worktree_status(worktree, pe->path, fileindex, repo,
7352 check_stage_ok, &oka, NULL, NULL, 0, 0);
7353 if (err)
7354 goto done;
7356 if (!oka.have_changes) {
7357 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7358 goto done;
7361 spa.worktree = worktree;
7362 spa.fileindex = fileindex;
7363 spa.repo = repo;
7364 spa.patch_cb = patch_cb;
7365 spa.patch_arg = patch_arg;
7366 spa.status_cb = status_cb;
7367 spa.status_arg = status_arg;
7368 spa.staged_something = 0;
7369 spa.allow_bad_symlinks = allow_bad_symlinks;
7370 TAILQ_FOREACH(pe, paths, entry) {
7371 err = worktree_status(worktree, pe->path, fileindex, repo,
7372 stage_path, &spa, NULL, NULL, 0, 0);
7373 if (err)
7374 goto done;
7376 if (!spa.staged_something) {
7377 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7378 goto done;
7381 sync_err = sync_fileindex(fileindex, fileindex_path);
7382 if (sync_err && err == NULL)
7383 err = sync_err;
7384 done:
7385 if (head_ref)
7386 got_ref_close(head_ref);
7387 free(head_commit_id);
7388 free(fileindex_path);
7389 if (fileindex)
7390 got_fileindex_free(fileindex);
7391 unlockerr = lock_worktree(worktree, LOCK_SH);
7392 if (unlockerr && err == NULL)
7393 err = unlockerr;
7394 return err;
7397 struct unstage_path_arg {
7398 struct got_worktree *worktree;
7399 struct got_fileindex *fileindex;
7400 struct got_repository *repo;
7401 got_worktree_checkout_cb progress_cb;
7402 void *progress_arg;
7403 got_worktree_patch_cb patch_cb;
7404 void *patch_arg;
7407 static const struct got_error *
7408 create_unstaged_content(char **path_unstaged_content,
7409 char **path_new_staged_content, struct got_object_id *blob_id,
7410 struct got_object_id *staged_blob_id, const char *relpath,
7411 struct got_repository *repo,
7412 got_worktree_patch_cb patch_cb, void *patch_arg)
7414 const struct got_error *err, *free_err;
7415 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7416 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7417 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7418 struct got_diffreg_result *diffreg_result = NULL;
7419 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7420 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7422 *path_unstaged_content = NULL;
7423 *path_new_staged_content = NULL;
7425 err = got_object_id_str(&label1, blob_id);
7426 if (err)
7427 return err;
7428 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7429 if (err)
7430 goto done;
7432 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7433 if (err)
7434 goto done;
7436 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7437 if (err)
7438 goto done;
7440 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7441 if (err)
7442 goto done;
7444 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7445 if (err)
7446 goto done;
7448 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7449 if (err)
7450 goto done;
7452 err = got_diff_files(&diffreg_result, f1, label1, f2,
7453 path2, 3, 0, NULL);
7454 if (err)
7455 goto done;
7457 err = got_opentemp_named(path_unstaged_content, &outfile,
7458 "got-unstaged-content");
7459 if (err)
7460 goto done;
7461 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7462 "got-new-staged-content");
7463 if (err)
7464 goto done;
7466 if (fseek(f1, 0L, SEEK_SET) == -1) {
7467 err = got_ferror(f1, GOT_ERR_IO);
7468 goto done;
7470 if (fseek(f2, 0L, SEEK_SET) == -1) {
7471 err = got_ferror(f2, GOT_ERR_IO);
7472 goto done;
7474 /* Count the number of actual changes in the diff result. */
7475 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7476 struct diff_chunk_context cc = {};
7477 diff_chunk_context_load_change(&cc, &nchunks_used,
7478 diffreg_result->result, n, 0);
7479 nchanges++;
7481 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7482 int choice;
7483 err = apply_or_reject_change(&choice, &nchunks_used,
7484 diffreg_result->result, n, relpath, f1, f2,
7485 &line_cur1, &line_cur2,
7486 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7487 if (err)
7488 goto done;
7489 if (choice == GOT_PATCH_CHOICE_YES)
7490 have_content = 1;
7491 else
7492 have_rejected_content = 1;
7493 if (choice == GOT_PATCH_CHOICE_QUIT)
7494 break;
7496 if (have_content || have_rejected_content)
7497 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7498 outfile, rejectfile);
7499 done:
7500 free(label1);
7501 if (blob)
7502 got_object_blob_close(blob);
7503 if (staged_blob)
7504 got_object_blob_close(staged_blob);
7505 free_err = got_diffreg_result_free(diffreg_result);
7506 if (free_err && err == NULL)
7507 err = free_err;
7508 if (f1 && fclose(f1) == EOF && err == NULL)
7509 err = got_error_from_errno2("fclose", path1);
7510 if (f2 && fclose(f2) == EOF && err == NULL)
7511 err = got_error_from_errno2("fclose", path2);
7512 if (outfile && fclose(outfile) == EOF && err == NULL)
7513 err = got_error_from_errno2("fclose", *path_unstaged_content);
7514 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7515 err = got_error_from_errno2("fclose", *path_new_staged_content);
7516 if (path1 && unlink(path1) == -1 && err == NULL)
7517 err = got_error_from_errno2("unlink", path1);
7518 if (path2 && unlink(path2) == -1 && err == NULL)
7519 err = got_error_from_errno2("unlink", path2);
7520 if (err || !have_content) {
7521 if (*path_unstaged_content &&
7522 unlink(*path_unstaged_content) == -1 && err == NULL)
7523 err = got_error_from_errno2("unlink",
7524 *path_unstaged_content);
7525 free(*path_unstaged_content);
7526 *path_unstaged_content = NULL;
7528 if (err || !have_content || !have_rejected_content) {
7529 if (*path_new_staged_content &&
7530 unlink(*path_new_staged_content) == -1 && err == NULL)
7531 err = got_error_from_errno2("unlink",
7532 *path_new_staged_content);
7533 free(*path_new_staged_content);
7534 *path_new_staged_content = NULL;
7536 free(path1);
7537 free(path2);
7538 return err;
7541 static const struct got_error *
7542 unstage_hunks(struct got_object_id *staged_blob_id,
7543 struct got_blob_object *blob_base,
7544 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7545 const char *ondisk_path, const char *label_orig,
7546 struct got_worktree *worktree, struct got_repository *repo,
7547 got_worktree_patch_cb patch_cb, void *patch_arg,
7548 got_worktree_checkout_cb progress_cb, void *progress_arg)
7550 const struct got_error *err = NULL;
7551 char *path_unstaged_content = NULL;
7552 char *path_new_staged_content = NULL;
7553 struct got_object_id *new_staged_blob_id = NULL;
7554 FILE *f = NULL;
7555 struct stat sb;
7557 err = create_unstaged_content(&path_unstaged_content,
7558 &path_new_staged_content, blob_id, staged_blob_id,
7559 ie->path, repo, patch_cb, patch_arg);
7560 if (err)
7561 return err;
7563 if (path_unstaged_content == NULL)
7564 return NULL;
7566 if (path_new_staged_content) {
7567 err = got_object_blob_create(&new_staged_blob_id,
7568 path_new_staged_content, repo);
7569 if (err)
7570 goto done;
7573 f = fopen(path_unstaged_content, "r");
7574 if (f == NULL) {
7575 err = got_error_from_errno2("fopen",
7576 path_unstaged_content);
7577 goto done;
7579 if (fstat(fileno(f), &sb) == -1) {
7580 err = got_error_from_errno2("fstat", path_unstaged_content);
7581 goto done;
7583 if (got_fileindex_entry_staged_filetype_get(ie) ==
7584 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7585 char link_target[PATH_MAX];
7586 size_t r;
7587 r = fread(link_target, 1, sizeof(link_target), f);
7588 if (r == 0 && ferror(f)) {
7589 err = got_error_from_errno("fread");
7590 goto done;
7592 if (r >= sizeof(link_target)) { /* should not happen */
7593 err = got_error(GOT_ERR_NO_SPACE);
7594 goto done;
7596 link_target[r] = '\0';
7597 err = merge_symlink(worktree, blob_base,
7598 ondisk_path, ie->path, label_orig, link_target,
7599 worktree->base_commit_id, repo, progress_cb,
7600 progress_arg);
7601 } else {
7602 int local_changes_subsumed;
7603 err = merge_file(&local_changes_subsumed, worktree,
7604 blob_base, ondisk_path, ie->path,
7605 got_fileindex_perms_to_st(ie),
7606 path_unstaged_content, label_orig, "unstaged",
7607 repo, progress_cb, progress_arg);
7609 if (err)
7610 goto done;
7612 if (new_staged_blob_id) {
7613 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7614 SHA1_DIGEST_LENGTH);
7615 } else {
7616 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7617 got_fileindex_entry_staged_filetype_set(ie, 0);
7619 done:
7620 free(new_staged_blob_id);
7621 if (path_unstaged_content &&
7622 unlink(path_unstaged_content) == -1 && err == NULL)
7623 err = got_error_from_errno2("unlink", path_unstaged_content);
7624 if (path_new_staged_content &&
7625 unlink(path_new_staged_content) == -1 && err == NULL)
7626 err = got_error_from_errno2("unlink", path_new_staged_content);
7627 if (f && fclose(f) != 0 && err == NULL)
7628 err = got_error_from_errno2("fclose", path_unstaged_content);
7629 free(path_unstaged_content);
7630 free(path_new_staged_content);
7631 return err;
7634 static const struct got_error *
7635 unstage_path(void *arg, unsigned char status,
7636 unsigned char staged_status, const char *relpath,
7637 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7638 struct got_object_id *commit_id, int dirfd, const char *de_name)
7640 const struct got_error *err = NULL;
7641 struct unstage_path_arg *a = arg;
7642 struct got_fileindex_entry *ie;
7643 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7644 char *ondisk_path = NULL;
7645 char *id_str = NULL, *label_orig = NULL;
7646 int local_changes_subsumed;
7647 struct stat sb;
7649 if (staged_status != GOT_STATUS_ADD &&
7650 staged_status != GOT_STATUS_MODIFY &&
7651 staged_status != GOT_STATUS_DELETE)
7652 return NULL;
7654 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7655 if (ie == NULL)
7656 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7658 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7659 == -1)
7660 return got_error_from_errno("asprintf");
7662 err = got_object_id_str(&id_str,
7663 commit_id ? commit_id : a->worktree->base_commit_id);
7664 if (err)
7665 goto done;
7666 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7667 id_str) == -1) {
7668 err = got_error_from_errno("asprintf");
7669 goto done;
7672 switch (staged_status) {
7673 case GOT_STATUS_MODIFY:
7674 err = got_object_open_as_blob(&blob_base, a->repo,
7675 blob_id, 8192);
7676 if (err)
7677 break;
7678 /* fall through */
7679 case GOT_STATUS_ADD:
7680 if (a->patch_cb) {
7681 if (staged_status == GOT_STATUS_ADD) {
7682 int choice = GOT_PATCH_CHOICE_NONE;
7683 err = (*a->patch_cb)(&choice, a->patch_arg,
7684 staged_status, ie->path, NULL, 1, 1);
7685 if (err)
7686 break;
7687 if (choice != GOT_PATCH_CHOICE_YES)
7688 break;
7689 } else {
7690 err = unstage_hunks(staged_blob_id,
7691 blob_base, blob_id, ie, ondisk_path,
7692 label_orig, a->worktree, a->repo,
7693 a->patch_cb, a->patch_arg,
7694 a->progress_cb, a->progress_arg);
7695 break; /* Done with this file. */
7698 err = got_object_open_as_blob(&blob_staged, a->repo,
7699 staged_blob_id, 8192);
7700 if (err)
7701 break;
7702 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7703 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7704 case GOT_FILEIDX_MODE_REGULAR_FILE:
7705 err = merge_blob(&local_changes_subsumed, a->worktree,
7706 blob_base, ondisk_path, relpath,
7707 got_fileindex_perms_to_st(ie), label_orig,
7708 blob_staged, commit_id ? commit_id :
7709 a->worktree->base_commit_id, a->repo,
7710 a->progress_cb, a->progress_arg);
7711 break;
7712 case GOT_FILEIDX_MODE_SYMLINK:
7713 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7714 char *staged_target;
7715 err = got_object_blob_read_to_str(
7716 &staged_target, blob_staged);
7717 if (err)
7718 goto done;
7719 err = merge_symlink(a->worktree, blob_base,
7720 ondisk_path, relpath, label_orig,
7721 staged_target, commit_id ? commit_id :
7722 a->worktree->base_commit_id,
7723 a->repo, a->progress_cb, a->progress_arg);
7724 free(staged_target);
7725 } else {
7726 err = merge_blob(&local_changes_subsumed,
7727 a->worktree, blob_base, ondisk_path,
7728 relpath, got_fileindex_perms_to_st(ie),
7729 label_orig, blob_staged,
7730 commit_id ? commit_id :
7731 a->worktree->base_commit_id, a->repo,
7732 a->progress_cb, a->progress_arg);
7734 break;
7735 default:
7736 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7737 break;
7739 if (err == NULL) {
7740 got_fileindex_entry_stage_set(ie,
7741 GOT_FILEIDX_STAGE_NONE);
7742 got_fileindex_entry_staged_filetype_set(ie, 0);
7744 break;
7745 case GOT_STATUS_DELETE:
7746 if (a->patch_cb) {
7747 int choice = GOT_PATCH_CHOICE_NONE;
7748 err = (*a->patch_cb)(&choice, a->patch_arg,
7749 staged_status, ie->path, NULL, 1, 1);
7750 if (err)
7751 break;
7752 if (choice == GOT_PATCH_CHOICE_NO)
7753 break;
7754 if (choice != GOT_PATCH_CHOICE_YES) {
7755 err = got_error(GOT_ERR_PATCH_CHOICE);
7756 break;
7759 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7760 got_fileindex_entry_staged_filetype_set(ie, 0);
7761 err = get_file_status(&status, &sb, ie, ondisk_path,
7762 dirfd, de_name, a->repo);
7763 if (err)
7764 break;
7765 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7766 break;
7768 done:
7769 free(ondisk_path);
7770 if (blob_base)
7771 got_object_blob_close(blob_base);
7772 if (blob_staged)
7773 got_object_blob_close(blob_staged);
7774 free(id_str);
7775 free(label_orig);
7776 return err;
7779 const struct got_error *
7780 got_worktree_unstage(struct got_worktree *worktree,
7781 struct got_pathlist_head *paths,
7782 got_worktree_checkout_cb progress_cb, void *progress_arg,
7783 got_worktree_patch_cb patch_cb, void *patch_arg,
7784 struct got_repository *repo)
7786 const struct got_error *err = NULL, *sync_err, *unlockerr;
7787 struct got_pathlist_entry *pe;
7788 struct got_fileindex *fileindex = NULL;
7789 char *fileindex_path = NULL;
7790 struct unstage_path_arg upa;
7792 err = lock_worktree(worktree, LOCK_EX);
7793 if (err)
7794 return err;
7796 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7797 if (err)
7798 goto done;
7800 upa.worktree = worktree;
7801 upa.fileindex = fileindex;
7802 upa.repo = repo;
7803 upa.progress_cb = progress_cb;
7804 upa.progress_arg = progress_arg;
7805 upa.patch_cb = patch_cb;
7806 upa.patch_arg = patch_arg;
7807 TAILQ_FOREACH(pe, paths, entry) {
7808 err = worktree_status(worktree, pe->path, fileindex, repo,
7809 unstage_path, &upa, NULL, NULL, 0, 0);
7810 if (err)
7811 goto done;
7814 sync_err = sync_fileindex(fileindex, fileindex_path);
7815 if (sync_err && err == NULL)
7816 err = sync_err;
7817 done:
7818 free(fileindex_path);
7819 if (fileindex)
7820 got_fileindex_free(fileindex);
7821 unlockerr = lock_worktree(worktree, LOCK_SH);
7822 if (unlockerr && err == NULL)
7823 err = unlockerr;
7824 return err;
7827 struct report_file_info_arg {
7828 struct got_worktree *worktree;
7829 got_worktree_path_info_cb info_cb;
7830 void *info_arg;
7831 struct got_pathlist_head *paths;
7832 got_cancel_cb cancel_cb;
7833 void *cancel_arg;
7836 static const struct got_error *
7837 report_file_info(void *arg, struct got_fileindex_entry *ie)
7839 struct report_file_info_arg *a = arg;
7840 struct got_pathlist_entry *pe;
7841 struct got_object_id blob_id, staged_blob_id, commit_id;
7842 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7843 struct got_object_id *commit_idp = NULL;
7844 int stage;
7846 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7847 return got_error(GOT_ERR_CANCELLED);
7849 TAILQ_FOREACH(pe, a->paths, entry) {
7850 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7851 got_path_is_child(ie->path, pe->path, pe->path_len))
7852 break;
7854 if (pe == NULL) /* not found */
7855 return NULL;
7857 if (got_fileindex_entry_has_blob(ie)) {
7858 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7859 blob_idp = &blob_id;
7861 stage = got_fileindex_entry_stage_get(ie);
7862 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7863 stage == GOT_FILEIDX_STAGE_ADD) {
7864 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7865 SHA1_DIGEST_LENGTH);
7866 staged_blob_idp = &staged_blob_id;
7869 if (got_fileindex_entry_has_commit(ie)) {
7870 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7871 commit_idp = &commit_id;
7874 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7875 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7878 const struct got_error *
7879 got_worktree_path_info(struct got_worktree *worktree,
7880 struct got_pathlist_head *paths,
7881 got_worktree_path_info_cb info_cb, void *info_arg,
7882 got_cancel_cb cancel_cb, void *cancel_arg)
7885 const struct got_error *err = NULL, *unlockerr;
7886 struct got_fileindex *fileindex = NULL;
7887 char *fileindex_path = NULL;
7888 struct report_file_info_arg arg;
7890 err = lock_worktree(worktree, LOCK_SH);
7891 if (err)
7892 return err;
7894 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7895 if (err)
7896 goto done;
7898 arg.worktree = worktree;
7899 arg.info_cb = info_cb;
7900 arg.info_arg = info_arg;
7901 arg.paths = paths;
7902 arg.cancel_cb = cancel_cb;
7903 arg.cancel_arg = cancel_arg;
7904 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7905 &arg);
7906 done:
7907 free(fileindex_path);
7908 if (fileindex)
7909 got_fileindex_free(fileindex);
7910 unlockerr = lock_worktree(worktree, LOCK_UN);
7911 if (unlockerr && err == NULL)
7912 err = unlockerr;
7913 return err;