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) == EOF && 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);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
436 goto done;
438 done:
439 if (repo)
440 got_repo_close(repo);
441 free(path_got);
442 free(path_lock);
443 free(base_commit_id_str);
444 free(uuidstr);
445 free(formatstr);
446 if (err) {
447 if (fd != -1)
448 close(fd);
449 if (*worktree != NULL)
450 got_worktree_close(*worktree);
451 *worktree = NULL;
452 } else
453 (*worktree)->lockfd = fd;
455 return err;
458 const struct got_error *
459 got_worktree_open(struct got_worktree **worktree, const char *path)
461 const struct got_error *err = NULL;
462 char *worktree_path;
464 worktree_path = strdup(path);
465 if (worktree_path == NULL)
466 return got_error_from_errno("strdup");
468 for (;;) {
469 char *parent_path;
471 err = open_worktree(worktree, worktree_path);
472 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 free(worktree_path);
474 return err;
476 if (*worktree) {
477 free(worktree_path);
478 return NULL;
480 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
481 break;
482 err = got_path_dirname(&parent_path, worktree_path);
483 if (err) {
484 if (err->code != GOT_ERR_BAD_PATH) {
485 free(worktree_path);
486 return err;
488 break;
490 free(worktree_path);
491 worktree_path = parent_path;
494 free(worktree_path);
495 return got_error(GOT_ERR_NOT_WORKTREE);
498 const struct got_error *
499 got_worktree_close(struct got_worktree *worktree)
501 const struct got_error *err = NULL;
502 free(worktree->repo_path);
503 free(worktree->path_prefix);
504 free(worktree->base_commit_id);
505 free(worktree->head_ref_name);
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 free(worktree->root_path);
512 free(worktree->gotconfig_path);
513 got_gotconfig_free(worktree->gotconfig);
514 if (close(worktree->root_fd) == -1 && err == NULL)
515 err = got_error_from_errno2("close",
516 got_worktree_get_root_path(worktree));
517 free(worktree);
518 return err;
521 const char *
522 got_worktree_get_root_path(struct got_worktree *worktree)
524 return worktree->root_path;
527 const char *
528 got_worktree_get_repo_path(struct got_worktree *worktree)
530 return worktree->repo_path;
532 const char *
533 got_worktree_get_path_prefix(struct got_worktree *worktree)
535 return worktree->path_prefix;
538 const struct got_error *
539 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
540 const char *path_prefix)
542 char *absprefix = NULL;
544 if (!got_path_is_absolute(path_prefix)) {
545 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
546 return got_error_from_errno("asprintf");
548 *match = (strcmp(absprefix ? absprefix : path_prefix,
549 worktree->path_prefix) == 0);
550 free(absprefix);
551 return NULL;
554 const char *
555 got_worktree_get_head_ref_name(struct got_worktree *worktree)
557 return worktree->head_ref_name;
560 const struct got_error *
561 got_worktree_set_head_ref(struct got_worktree *worktree,
562 struct got_reference *head_ref)
564 const struct got_error *err = NULL;
565 char *path_got = NULL, *head_ref_name = NULL;
567 if (asprintf(&path_got, "%s/%s", worktree->root_path,
568 GOT_WORKTREE_GOT_DIR) == -1) {
569 err = got_error_from_errno("asprintf");
570 path_got = NULL;
571 goto done;
574 head_ref_name = strdup(got_ref_get_name(head_ref));
575 if (head_ref_name == NULL) {
576 err = got_error_from_errno("strdup");
577 goto done;
580 err = write_head_ref(path_got, head_ref);
581 if (err)
582 goto done;
584 free(worktree->head_ref_name);
585 worktree->head_ref_name = head_ref_name;
586 done:
587 free(path_got);
588 if (err)
589 free(head_ref_name);
590 return err;
593 struct got_object_id *
594 got_worktree_get_base_commit_id(struct got_worktree *worktree)
596 return worktree->base_commit_id;
599 const struct got_error *
600 got_worktree_set_base_commit_id(struct got_worktree *worktree,
601 struct got_repository *repo, struct got_object_id *commit_id)
603 const struct got_error *err;
604 struct got_object *obj = NULL;
605 char *id_str = NULL;
606 char *path_got = NULL;
608 if (asprintf(&path_got, "%s/%s", worktree->root_path,
609 GOT_WORKTREE_GOT_DIR) == -1) {
610 err = got_error_from_errno("asprintf");
611 path_got = NULL;
612 goto done;
615 err = got_object_open(&obj, repo, commit_id);
616 if (err)
617 return err;
619 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
620 err = got_error(GOT_ERR_OBJ_TYPE);
621 goto done;
624 /* Record our base commit. */
625 err = got_object_id_str(&id_str, commit_id);
626 if (err)
627 goto done;
628 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
629 if (err)
630 goto done;
632 free(worktree->base_commit_id);
633 worktree->base_commit_id = got_object_id_dup(commit_id);
634 if (worktree->base_commit_id == NULL) {
635 err = got_error_from_errno("got_object_id_dup");
636 goto done;
638 done:
639 if (obj)
640 got_object_close(obj);
641 free(id_str);
642 free(path_got);
643 return err;
646 const struct got_gotconfig *
647 got_worktree_get_gotconfig(struct got_worktree *worktree)
649 return worktree->gotconfig;
652 static const struct got_error *
653 lock_worktree(struct got_worktree *worktree, int operation)
655 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
656 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
657 : got_error_from_errno2("flock",
658 got_worktree_get_root_path(worktree)));
659 return NULL;
662 static const struct got_error *
663 add_dir_on_disk(struct got_worktree *worktree, const char *path)
665 const struct got_error *err = NULL;
666 char *abspath;
668 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
669 return got_error_from_errno("asprintf");
671 err = got_path_mkdir(abspath);
672 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
673 struct stat sb;
674 err = NULL;
675 if (lstat(abspath, &sb) == -1) {
676 err = got_error_from_errno2("lstat", abspath);
677 } else if (!S_ISDIR(sb.st_mode)) {
678 /* TODO directory is obstructed; do something */
679 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
682 free(abspath);
683 return err;
686 static const struct got_error *
687 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
689 const struct got_error *err = NULL;
690 uint8_t fbuf1[8192];
691 uint8_t fbuf2[8192];
692 size_t flen1 = 0, flen2 = 0;
694 *same = 1;
696 for (;;) {
697 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
698 if (flen1 == 0 && ferror(f1)) {
699 err = got_error_from_errno("fread");
700 break;
702 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
703 if (flen2 == 0 && ferror(f2)) {
704 err = got_error_from_errno("fread");
705 break;
707 if (flen1 == 0) {
708 if (flen2 != 0)
709 *same = 0;
710 break;
711 } else if (flen2 == 0) {
712 if (flen1 != 0)
713 *same = 0;
714 break;
715 } else if (flen1 == flen2) {
716 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
717 *same = 0;
718 break;
720 } else {
721 *same = 0;
722 break;
726 return err;
729 static const struct got_error *
730 check_files_equal(int *same, const char *f1_path, const char *f2_path)
732 const struct got_error *err = NULL;
733 struct stat sb;
734 size_t size1, size2;
735 FILE *f1 = NULL, *f2 = NULL;
737 *same = 1;
739 if (lstat(f1_path, &sb) != 0) {
740 err = got_error_from_errno2("lstat", f1_path);
741 goto done;
743 size1 = sb.st_size;
745 if (lstat(f2_path, &sb) != 0) {
746 err = got_error_from_errno2("lstat", f2_path);
747 goto done;
749 size2 = sb.st_size;
751 if (size1 != size2) {
752 *same = 0;
753 return NULL;
756 f1 = fopen(f1_path, "r");
757 if (f1 == NULL)
758 return got_error_from_errno2("fopen", f1_path);
760 f2 = fopen(f2_path, "r");
761 if (f2 == NULL) {
762 err = got_error_from_errno2("fopen", f2_path);
763 goto done;
766 err = check_file_contents_equal(same, f1, f2);
767 done:
768 if (f1 && fclose(f1) == EOF && err == NULL)
769 err = got_error_from_errno("fclose");
770 if (f2 && fclose(f2) == EOF && err == NULL)
771 err = got_error_from_errno("fclose");
773 return err;
776 /*
777 * Perform a 3-way merge where blob_orig acts as the common ancestor,
778 * the file at deriv_path acts as the first derived version, and the
779 * file on disk acts as the second derived version.
780 */
781 static const struct got_error *
782 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
783 struct got_blob_object *blob_orig, const char *ondisk_path,
784 const char *path, uint16_t st_mode, const char *deriv_path,
785 const char *label_orig, const char *label_deriv,
786 struct got_repository *repo,
787 got_worktree_checkout_cb progress_cb, void *progress_arg)
789 const struct got_error *err = NULL;
790 int merged_fd = -1;
791 FILE *f_orig = NULL;
792 char *blob_orig_path = NULL;
793 char *merged_path = NULL, *base_path = NULL;
794 int overlapcnt = 0;
795 char *parent = NULL;
796 char *symlink_path = NULL;
797 FILE *symlinkf = NULL;
799 *local_changes_subsumed = 0;
801 err = got_path_dirname(&parent, ondisk_path);
802 if (err)
803 return err;
805 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
806 err = got_error_from_errno("asprintf");
807 goto done;
810 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
811 if (err)
812 goto done;
814 free(base_path);
815 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
816 err = got_error_from_errno("asprintf");
817 base_path = NULL;
818 goto done;
821 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
822 if (err)
823 goto done;
824 if (blob_orig) {
825 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
826 blob_orig);
827 if (err)
828 goto done;
829 } else {
830 /*
831 * If the file has no blob, this is an "add vs add" conflict,
832 * and we simply use an empty ancestor file to make both files
833 * appear in the merged result in their entirety.
834 */
837 /*
838 * In order the run a 3-way merge with a symlink we copy the symlink's
839 * target path into a temporary file and use that file with diff3.
840 */
841 if (S_ISLNK(st_mode)) {
842 char target_path[PATH_MAX];
843 ssize_t target_len;
844 size_t n;
846 free(base_path);
847 if (asprintf(&base_path, "%s/got-symlink-merge",
848 parent) == -1) {
849 err = got_error_from_errno("asprintf");
850 base_path = NULL;
851 goto done;
853 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
854 if (err)
855 goto done;
856 target_len = readlink(ondisk_path, target_path,
857 sizeof(target_path));
858 if (target_len == -1) {
859 err = got_error_from_errno2("readlink", ondisk_path);
860 goto done;
862 n = fwrite(target_path, 1, target_len, symlinkf);
863 if (n != target_len) {
864 err = got_ferror(symlinkf, GOT_ERR_IO);
865 goto done;
867 if (fflush(symlinkf) == EOF) {
868 err = got_error_from_errno2("fflush", symlink_path);
869 goto done;
873 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
874 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
875 label_deriv, label_orig, NULL);
876 if (err)
877 goto done;
879 err = (*progress_cb)(progress_arg,
880 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
881 if (err)
882 goto done;
884 if (fsync(merged_fd) != 0) {
885 err = got_error_from_errno("fsync");
886 goto done;
889 /* Check if a clean merge has subsumed all local changes. */
890 if (overlapcnt == 0) {
891 err = check_files_equal(local_changes_subsumed, deriv_path,
892 merged_path);
893 if (err)
894 goto done;
897 if (fchmod(merged_fd, st_mode) != 0) {
898 err = got_error_from_errno2("fchmod", merged_path);
899 goto done;
902 if (rename(merged_path, ondisk_path) != 0) {
903 err = got_error_from_errno3("rename", merged_path,
904 ondisk_path);
905 goto done;
907 done:
908 if (err) {
909 if (merged_path)
910 unlink(merged_path);
912 if (symlink_path) {
913 if (unlink(symlink_path) == -1 && err == NULL)
914 err = got_error_from_errno2("unlink", symlink_path);
916 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
917 err = got_error_from_errno2("fclose", symlink_path);
918 free(symlink_path);
919 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
920 err = got_error_from_errno("close");
921 if (f_orig && fclose(f_orig) == EOF && err == NULL)
922 err = got_error_from_errno("fclose");
923 free(merged_path);
924 free(base_path);
925 if (blob_orig_path) {
926 unlink(blob_orig_path);
927 free(blob_orig_path);
929 free(parent);
930 return err;
933 static const struct got_error *
934 update_symlink(const char *ondisk_path, const char *target_path,
935 size_t target_len)
937 /* This is not atomic but matches what 'ln -sf' does. */
938 if (unlink(ondisk_path) == -1)
939 return got_error_from_errno2("unlink", ondisk_path);
940 if (symlink(target_path, ondisk_path) == -1)
941 return got_error_from_errno3("symlink", target_path,
942 ondisk_path);
943 return NULL;
946 /*
947 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
948 * in the work tree with a file that contains conflict markers and the
949 * conflicting target paths of the original version, a "derived version"
950 * of a symlink from an incoming change, and a local version of the symlink.
952 * The original versions's target path can be NULL if it is not available,
953 * such as if both derived versions added a new symlink at the same path.
955 * The incoming derived symlink target is NULL in case the incoming change
956 * has deleted this symlink.
957 */
958 static const struct got_error *
959 install_symlink_conflict(const char *deriv_target,
960 struct got_object_id *deriv_base_commit_id, const char *orig_target,
961 const char *label_orig, const char *local_target, const char *ondisk_path)
963 const struct got_error *err;
964 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
965 FILE *f = NULL;
967 err = got_object_id_str(&id_str, deriv_base_commit_id);
968 if (err)
969 return got_error_from_errno("asprintf");
971 if (asprintf(&label_deriv, "%s: commit %s",
972 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
973 err = got_error_from_errno("asprintf");
974 goto done;
977 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
978 if (err)
979 goto done;
981 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
982 err = got_error_from_errno2("fchmod", path);
983 goto done;
986 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
987 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
988 deriv_target ? deriv_target : "(symlink was deleted)",
989 orig_target ? label_orig : "",
990 orig_target ? "\n" : "",
991 orig_target ? orig_target : "",
992 orig_target ? "\n" : "",
993 GOT_DIFF_CONFLICT_MARKER_SEP,
994 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
995 err = got_error_from_errno2("fprintf", path);
996 goto done;
999 if (unlink(ondisk_path) == -1) {
1000 err = got_error_from_errno2("unlink", ondisk_path);
1001 goto done;
1003 if (rename(path, ondisk_path) == -1) {
1004 err = got_error_from_errno3("rename", path, ondisk_path);
1005 goto done;
1007 done:
1008 if (f != NULL && fclose(f) == EOF && err == NULL)
1009 err = got_error_from_errno2("fclose", path);
1010 free(path);
1011 free(id_str);
1012 free(label_deriv);
1013 return err;
1016 /* forward declaration */
1017 static const struct got_error *
1018 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1019 const char *, const char *, uint16_t, const char *,
1020 struct got_blob_object *, struct got_object_id *,
1021 struct got_repository *, got_worktree_checkout_cb, void *);
1024 * Merge a symlink into the work tree, where blob_orig acts as the common
1025 * ancestor, deriv_target is the link target of the first derived version,
1026 * and the symlink on disk acts as the second derived version.
1027 * Assume that contents of both blobs represent symlinks.
1029 static const struct got_error *
1030 merge_symlink(struct got_worktree *worktree,
1031 struct got_blob_object *blob_orig, const char *ondisk_path,
1032 const char *path, const char *label_orig, const char *deriv_target,
1033 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1034 got_worktree_checkout_cb progress_cb, void *progress_arg)
1036 const struct got_error *err = NULL;
1037 char *ancestor_target = NULL;
1038 struct stat sb;
1039 ssize_t ondisk_len, deriv_len;
1040 char ondisk_target[PATH_MAX];
1041 int have_local_change = 0;
1042 int have_incoming_change = 0;
1044 if (lstat(ondisk_path, &sb) == -1)
1045 return got_error_from_errno2("lstat", ondisk_path);
1047 ondisk_len = readlink(ondisk_path, ondisk_target,
1048 sizeof(ondisk_target));
1049 if (ondisk_len == -1) {
1050 err = got_error_from_errno2("readlink",
1051 ondisk_path);
1052 goto done;
1054 ondisk_target[ondisk_len] = '\0';
1056 if (blob_orig) {
1057 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1058 if (err)
1059 goto done;
1062 if (ancestor_target == NULL ||
1063 (ondisk_len != strlen(ancestor_target) ||
1064 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1065 have_local_change = 1;
1067 deriv_len = strlen(deriv_target);
1068 if (ancestor_target == NULL ||
1069 (deriv_len != strlen(ancestor_target) ||
1070 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1071 have_incoming_change = 1;
1073 if (!have_local_change && !have_incoming_change) {
1074 if (ancestor_target) {
1075 /* Both sides made the same change. */
1076 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1077 path);
1078 } else if (deriv_len == ondisk_len &&
1079 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1080 /* Both sides added the same symlink. */
1081 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1082 path);
1083 } else {
1084 /* Both sides added symlinks which don't match. */
1085 err = install_symlink_conflict(deriv_target,
1086 deriv_base_commit_id, ancestor_target,
1087 label_orig, ondisk_target, ondisk_path);
1088 if (err)
1089 goto done;
1090 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1091 path);
1093 } else if (!have_local_change && have_incoming_change) {
1094 /* Apply the incoming change. */
1095 err = update_symlink(ondisk_path, deriv_target,
1096 strlen(deriv_target));
1097 if (err)
1098 goto done;
1099 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1100 } else if (have_local_change && have_incoming_change) {
1101 if (deriv_len == ondisk_len &&
1102 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1103 /* Both sides made the same change. */
1104 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1105 path);
1106 } else {
1107 err = install_symlink_conflict(deriv_target,
1108 deriv_base_commit_id, ancestor_target, label_orig,
1109 ondisk_target, ondisk_path);
1110 if (err)
1111 goto done;
1112 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1113 path);
1117 done:
1118 free(ancestor_target);
1119 return err;
1123 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1124 * blob_deriv acts as the first derived version, and the file on disk
1125 * acts as the second derived version.
1127 static const struct got_error *
1128 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1129 struct got_blob_object *blob_orig, const char *ondisk_path,
1130 const char *path, uint16_t st_mode, const char *label_orig,
1131 struct got_blob_object *blob_deriv,
1132 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1133 got_worktree_checkout_cb progress_cb, void *progress_arg)
1135 const struct got_error *err = NULL;
1136 FILE *f_deriv = NULL;
1137 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1138 char *label_deriv = NULL, *parent = NULL;
1140 *local_changes_subsumed = 0;
1142 err = got_path_dirname(&parent, ondisk_path);
1143 if (err)
1144 return err;
1146 free(base_path);
1147 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1148 err = got_error_from_errno("asprintf");
1149 base_path = NULL;
1150 goto done;
1153 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1154 if (err)
1155 goto done;
1156 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1157 blob_deriv);
1158 if (err)
1159 goto done;
1161 err = got_object_id_str(&id_str, deriv_base_commit_id);
1162 if (err)
1163 goto done;
1164 if (asprintf(&label_deriv, "%s: commit %s",
1165 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1166 err = got_error_from_errno("asprintf");
1167 goto done;
1170 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1171 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1172 label_deriv, repo, progress_cb, progress_arg);
1173 done:
1174 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1175 err = got_error_from_errno("fclose");
1176 free(base_path);
1177 if (blob_deriv_path) {
1178 unlink(blob_deriv_path);
1179 free(blob_deriv_path);
1181 free(id_str);
1182 free(label_deriv);
1183 free(parent);
1184 return err;
1187 static const struct got_error *
1188 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1189 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1190 int wt_fd, const char *path, struct got_object_id *blob_id)
1192 const struct got_error *err = NULL;
1193 struct got_fileindex_entry *new_ie;
1195 *new_iep = NULL;
1197 err = got_fileindex_entry_alloc(&new_ie, path);
1198 if (err)
1199 return err;
1201 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1202 blob_id->sha1, base_commit_id->sha1, 1);
1203 if (err)
1204 goto done;
1206 err = got_fileindex_entry_add(fileindex, new_ie);
1207 done:
1208 if (err)
1209 got_fileindex_entry_free(new_ie);
1210 else
1211 *new_iep = new_ie;
1212 return err;
1215 static mode_t
1216 get_ondisk_perms(int executable, mode_t st_mode)
1218 mode_t xbits = S_IXUSR;
1220 if (executable) {
1221 /* Map read bits to execute bits. */
1222 if (st_mode & S_IRGRP)
1223 xbits |= S_IXGRP;
1224 if (st_mode & S_IROTH)
1225 xbits |= S_IXOTH;
1226 return st_mode | xbits;
1229 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1232 /* forward declaration */
1233 static const struct got_error *
1234 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1235 const char *path, mode_t te_mode, mode_t st_mode,
1236 struct got_blob_object *blob, int restoring_missing_file,
1237 int reverting_versioned_file, int installing_bad_symlink,
1238 int path_is_unversioned, struct got_repository *repo,
1239 got_worktree_checkout_cb progress_cb, void *progress_arg);
1242 * This function assumes that the provided symlink target points at a
1243 * safe location in the work tree!
1245 static const struct got_error *
1246 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1247 size_t target_len)
1249 const struct got_error *err = NULL;
1250 ssize_t elen;
1251 char etarget[PATH_MAX];
1252 int fd;
1255 * "Bad" symlinks (those pointing outside the work tree or into the
1256 * .got directory) are installed in the work tree as a regular file
1257 * which contains the bad symlink target path.
1258 * The new symlink target has already been checked for safety by our
1259 * caller. If we can successfully open a regular file then we simply
1260 * replace this file with a symlink below.
1262 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1263 if (fd == -1) {
1264 if (errno != ELOOP)
1265 return got_error_from_errno2("open", ondisk_path);
1267 /* We are updating an existing on-disk symlink. */
1268 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1269 if (elen == -1)
1270 return got_error_from_errno2("readlink", ondisk_path);
1272 if (elen == target_len &&
1273 memcmp(etarget, target_path, target_len) == 0)
1274 return NULL; /* nothing to do */
1277 err = update_symlink(ondisk_path, target_path, target_len);
1278 if (fd != -1 && close(fd) == -1 && err == NULL)
1279 err = got_error_from_errno2("close", ondisk_path);
1280 return err;
1283 static const struct got_error *
1284 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1285 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1287 const struct got_error *err = NULL;
1288 char canonpath[PATH_MAX];
1289 char *path_got = NULL;
1291 *is_bad_symlink = 0;
1293 if (target_len >= sizeof(canonpath)) {
1294 *is_bad_symlink = 1;
1295 return NULL;
1299 * We do not use realpath(3) to resolve the symlink's target
1300 * path because we don't want to resolve symlinks recursively.
1301 * Instead we make the path absolute and then canonicalize it.
1302 * Relative symlink target lookup should begin at the directory
1303 * in which the blob object is being installed.
1305 if (!got_path_is_absolute(target_path)) {
1306 char *abspath, *parent;
1307 err = got_path_dirname(&parent, ondisk_path);
1308 if (err)
1309 return err;
1310 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1311 free(parent);
1312 return got_error_from_errno("asprintf");
1314 free(parent);
1315 if (strlen(abspath) >= sizeof(canonpath)) {
1316 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1317 free(abspath);
1318 return err;
1320 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1321 free(abspath);
1322 if (err)
1323 return err;
1324 } else {
1325 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1326 if (err)
1327 return err;
1330 /* Only allow symlinks pointing at paths within the work tree. */
1331 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1332 *is_bad_symlink = 1;
1333 return NULL;
1336 /* Do not allow symlinks pointing into the .got directory. */
1337 if (asprintf(&path_got, "%s/%s", wtroot_path,
1338 GOT_WORKTREE_GOT_DIR) == -1)
1339 return got_error_from_errno("asprintf");
1340 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1341 *is_bad_symlink = 1;
1343 free(path_got);
1344 return NULL;
1347 static const struct got_error *
1348 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1349 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1350 int restoring_missing_file, int reverting_versioned_file,
1351 int path_is_unversioned, struct got_repository *repo,
1352 got_worktree_checkout_cb progress_cb, void *progress_arg)
1354 const struct got_error *err = NULL;
1355 char target_path[PATH_MAX];
1356 size_t len, target_len = 0;
1357 char *path_got = NULL;
1358 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1359 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1361 *is_bad_symlink = 0;
1364 * Blob object content specifies the target path of the link.
1365 * If a symbolic link cannot be installed we instead create
1366 * a regular file which contains the link target path stored
1367 * in the blob object.
1369 do {
1370 err = got_object_blob_read_block(&len, blob);
1371 if (len + target_len >= sizeof(target_path)) {
1372 /* Path too long; install as a regular file. */
1373 *is_bad_symlink = 1;
1374 got_object_blob_rewind(blob);
1375 return install_blob(worktree, ondisk_path, path,
1376 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1377 restoring_missing_file, reverting_versioned_file,
1378 1, path_is_unversioned, repo, progress_cb,
1379 progress_arg);
1381 if (len > 0) {
1382 /* Skip blob object header first time around. */
1383 memcpy(target_path + target_len, buf + hdrlen,
1384 len - hdrlen);
1385 target_len += len - hdrlen;
1386 hdrlen = 0;
1388 } while (len != 0);
1389 target_path[target_len] = '\0';
1391 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1392 ondisk_path, worktree->root_path);
1393 if (err)
1394 return err;
1396 if (*is_bad_symlink) {
1397 /* install as a regular file */
1398 *is_bad_symlink = 1;
1399 got_object_blob_rewind(blob);
1400 err = install_blob(worktree, ondisk_path, path,
1401 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 restoring_missing_file, reverting_versioned_file, 1,
1403 path_is_unversioned, repo, progress_cb, progress_arg);
1404 goto done;
1407 if (symlink(target_path, ondisk_path) == -1) {
1408 if (errno == EEXIST) {
1409 if (path_is_unversioned) {
1410 err = (*progress_cb)(progress_arg,
1411 GOT_STATUS_UNVERSIONED, path);
1412 goto done;
1414 err = replace_existing_symlink(ondisk_path,
1415 target_path, target_len);
1416 if (err)
1417 goto done;
1418 if (progress_cb) {
1419 err = (*progress_cb)(progress_arg,
1420 reverting_versioned_file ?
1421 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1422 path);
1424 goto done; /* Nothing else to do. */
1427 if (errno == ENOENT) {
1428 char *parent;
1429 err = got_path_dirname(&parent, ondisk_path);
1430 if (err)
1431 goto done;
1432 err = add_dir_on_disk(worktree, parent);
1433 free(parent);
1434 if (err)
1435 goto done;
1437 * Retry, and fall through to error handling
1438 * below if this second attempt fails.
1440 if (symlink(target_path, ondisk_path) != -1) {
1441 err = NULL; /* success */
1442 goto done;
1446 /* Handle errors from first or second creation attempt. */
1447 if (errno == ENAMETOOLONG) {
1448 /* bad target path; install as a regular file */
1449 *is_bad_symlink = 1;
1450 got_object_blob_rewind(blob);
1451 err = install_blob(worktree, ondisk_path, path,
1452 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1453 restoring_missing_file, reverting_versioned_file, 1,
1454 path_is_unversioned, repo,
1455 progress_cb, progress_arg);
1456 } else if (errno == ENOTDIR) {
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1459 } else {
1460 err = got_error_from_errno3("symlink",
1461 target_path, ondisk_path);
1463 } else if (progress_cb)
1464 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1465 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1466 done:
1467 free(path_got);
1468 return err;
1471 static const struct got_error *
1472 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1473 const char *path, mode_t te_mode, mode_t st_mode,
1474 struct got_blob_object *blob, int restoring_missing_file,
1475 int reverting_versioned_file, int installing_bad_symlink,
1476 int path_is_unversioned, struct got_repository *repo,
1477 got_worktree_checkout_cb progress_cb, void *progress_arg)
1479 const struct got_error *err = NULL;
1480 int fd = -1;
1481 size_t len, hdrlen;
1482 int update = 0;
1483 char *tmppath = NULL;
1485 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1486 GOT_DEFAULT_FILE_MODE);
1487 if (fd == -1) {
1488 if (errno == ENOENT) {
1489 char *parent;
1490 err = got_path_dirname(&parent, path);
1491 if (err)
1492 return err;
1493 err = add_dir_on_disk(worktree, parent);
1494 free(parent);
1495 if (err)
1496 return err;
1497 fd = open(ondisk_path,
1498 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1499 GOT_DEFAULT_FILE_MODE);
1500 if (fd == -1)
1501 return got_error_from_errno2("open",
1502 ondisk_path);
1503 } else if (errno == EEXIST) {
1504 if (path_is_unversioned) {
1505 err = (*progress_cb)(progress_arg,
1506 GOT_STATUS_UNVERSIONED, path);
1507 goto done;
1509 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1510 !S_ISREG(st_mode) && !installing_bad_symlink) {
1511 /* TODO file is obstructed; do something */
1512 err = got_error_path(ondisk_path,
1513 GOT_ERR_FILE_OBSTRUCTED);
1514 goto done;
1515 } else {
1516 err = got_opentemp_named_fd(&tmppath, &fd,
1517 ondisk_path);
1518 if (err)
1519 goto done;
1520 update = 1;
1522 } else
1523 return got_error_from_errno2("open", ondisk_path);
1526 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1527 err = got_error_from_errno2("fchmod",
1528 update ? tmppath : ondisk_path);
1529 goto done;
1532 if (progress_cb) {
1533 if (restoring_missing_file)
1534 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1535 path);
1536 else if (reverting_versioned_file)
1537 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1538 path);
1539 else
1540 err = (*progress_cb)(progress_arg,
1541 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1542 if (err)
1543 goto done;
1546 hdrlen = got_object_blob_get_hdrlen(blob);
1547 do {
1548 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1549 err = got_object_blob_read_block(&len, blob);
1550 if (err)
1551 break;
1552 if (len > 0) {
1553 /* Skip blob object header first time around. */
1554 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1555 if (outlen == -1) {
1556 err = got_error_from_errno("write");
1557 goto done;
1558 } else if (outlen != len - hdrlen) {
1559 err = got_error(GOT_ERR_IO);
1560 goto done;
1562 hdrlen = 0;
1564 } while (len != 0);
1566 if (fsync(fd) != 0) {
1567 err = got_error_from_errno("fsync");
1568 goto done;
1571 if (update) {
1572 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1573 err = got_error_from_errno2("unlink", ondisk_path);
1574 goto done;
1576 if (rename(tmppath, ondisk_path) != 0) {
1577 err = got_error_from_errno3("rename", tmppath,
1578 ondisk_path);
1579 goto done;
1581 free(tmppath);
1582 tmppath = NULL;
1585 done:
1586 if (fd != -1 && close(fd) == -1 && err == NULL)
1587 err = got_error_from_errno("close");
1588 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1589 err = got_error_from_errno2("unlink", tmppath);
1590 free(tmppath);
1591 return err;
1594 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1595 static const struct got_error *
1596 get_modified_file_content_status(unsigned char *status, FILE *f)
1598 const struct got_error *err = NULL;
1599 const char *markers[3] = {
1600 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1601 GOT_DIFF_CONFLICT_MARKER_SEP,
1602 GOT_DIFF_CONFLICT_MARKER_END
1604 int i = 0;
1605 char *line = NULL;
1606 size_t linesize = 0;
1607 ssize_t linelen;
1609 while (*status == GOT_STATUS_MODIFY) {
1610 linelen = getline(&line, &linesize, f);
1611 if (linelen == -1) {
1612 if (feof(f))
1613 break;
1614 err = got_ferror(f, GOT_ERR_IO);
1615 break;
1618 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1619 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1620 == 0)
1621 *status = GOT_STATUS_CONFLICT;
1622 else
1623 i++;
1626 free(line);
1628 return err;
1631 static int
1632 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1634 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1635 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1638 static int
1639 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1641 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1642 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1643 ie->mtime_sec == sb->st_mtim.tv_sec &&
1644 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1645 ie->size == (sb->st_size & 0xffffffff) &&
1646 !xbit_differs(ie, sb->st_mode));
1649 static unsigned char
1650 get_staged_status(struct got_fileindex_entry *ie)
1652 switch (got_fileindex_entry_stage_get(ie)) {
1653 case GOT_FILEIDX_STAGE_ADD:
1654 return GOT_STATUS_ADD;
1655 case GOT_FILEIDX_STAGE_DELETE:
1656 return GOT_STATUS_DELETE;
1657 case GOT_FILEIDX_STAGE_MODIFY:
1658 return GOT_STATUS_MODIFY;
1659 default:
1660 return GOT_STATUS_NO_CHANGE;
1664 static const struct got_error *
1665 get_symlink_modification_status(unsigned char *status,
1666 struct got_fileindex_entry *ie, const char *abspath,
1667 int dirfd, const char *de_name, struct got_blob_object *blob)
1669 const struct got_error *err = NULL;
1670 char target_path[PATH_MAX];
1671 char etarget[PATH_MAX];
1672 ssize_t elen;
1673 size_t len, target_len = 0;
1674 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1675 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1677 *status = GOT_STATUS_NO_CHANGE;
1679 /* Blob object content specifies the target path of the link. */
1680 do {
1681 err = got_object_blob_read_block(&len, blob);
1682 if (err)
1683 return err;
1684 if (len + target_len >= sizeof(target_path)) {
1686 * Should not happen. The blob contents were OK
1687 * when this symlink was installed.
1689 return got_error(GOT_ERR_NO_SPACE);
1691 if (len > 0) {
1692 /* Skip blob object header first time around. */
1693 memcpy(target_path + target_len, buf + hdrlen,
1694 len - hdrlen);
1695 target_len += len - hdrlen;
1696 hdrlen = 0;
1698 } while (len != 0);
1699 target_path[target_len] = '\0';
1701 if (dirfd != -1) {
1702 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1703 if (elen == -1)
1704 return got_error_from_errno2("readlinkat", abspath);
1705 } else {
1706 elen = readlink(abspath, etarget, sizeof(etarget));
1707 if (elen == -1)
1708 return got_error_from_errno2("readlink", abspath);
1711 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1712 *status = GOT_STATUS_MODIFY;
1714 return NULL;
1717 static const struct got_error *
1718 get_file_status(unsigned char *status, struct stat *sb,
1719 struct got_fileindex_entry *ie, const char *abspath,
1720 int dirfd, const char *de_name, struct got_repository *repo)
1722 const struct got_error *err = NULL;
1723 struct got_object_id id;
1724 size_t hdrlen;
1725 int fd = -1;
1726 FILE *f = NULL;
1727 uint8_t fbuf[8192];
1728 struct got_blob_object *blob = NULL;
1729 size_t flen, blen;
1730 unsigned char staged_status = get_staged_status(ie);
1732 *status = GOT_STATUS_NO_CHANGE;
1735 * Whenever the caller provides a directory descriptor and a
1736 * directory entry name for the file, use them! This prevents
1737 * race conditions if filesystem paths change beneath our feet.
1739 if (dirfd != -1) {
1740 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 if (errno == ENOENT) {
1742 if (got_fileindex_entry_has_file_on_disk(ie))
1743 *status = GOT_STATUS_MISSING;
1744 else
1745 *status = GOT_STATUS_DELETE;
1746 goto done;
1748 err = got_error_from_errno2("fstatat", abspath);
1749 goto done;
1751 } else {
1752 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1753 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1754 return got_error_from_errno2("open", abspath);
1755 else if (fd == -1 && errno == ELOOP) {
1756 if (lstat(abspath, sb) == -1)
1757 return got_error_from_errno2("lstat", abspath);
1758 } else if (fd == -1 || fstat(fd, sb) == -1) {
1759 if (errno == ENOENT) {
1760 if (got_fileindex_entry_has_file_on_disk(ie))
1761 *status = GOT_STATUS_MISSING;
1762 else
1763 *status = GOT_STATUS_DELETE;
1764 goto done;
1766 err = got_error_from_errno2("fstat", abspath);
1767 goto done;
1771 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1772 *status = GOT_STATUS_OBSTRUCTED;
1773 goto done;
1776 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1777 *status = GOT_STATUS_DELETE;
1778 goto done;
1779 } else if (!got_fileindex_entry_has_blob(ie) &&
1780 staged_status != GOT_STATUS_ADD) {
1781 *status = GOT_STATUS_ADD;
1782 goto done;
1785 if (!stat_info_differs(ie, sb))
1786 goto done;
1788 if (S_ISLNK(sb->st_mode) &&
1789 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1790 *status = GOT_STATUS_MODIFY;
1791 goto done;
1794 if (staged_status == GOT_STATUS_MODIFY ||
1795 staged_status == GOT_STATUS_ADD)
1796 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1797 else
1798 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1800 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1801 if (err)
1802 goto done;
1804 if (S_ISLNK(sb->st_mode)) {
1805 err = get_symlink_modification_status(status, ie,
1806 abspath, dirfd, de_name, blob);
1807 goto done;
1810 if (dirfd != -1) {
1811 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1812 if (fd == -1) {
1813 err = got_error_from_errno2("openat", abspath);
1814 goto done;
1818 f = fdopen(fd, "r");
1819 if (f == NULL) {
1820 err = got_error_from_errno2("fdopen", abspath);
1821 goto done;
1823 fd = -1;
1824 hdrlen = got_object_blob_get_hdrlen(blob);
1825 for (;;) {
1826 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1827 err = got_object_blob_read_block(&blen, blob);
1828 if (err)
1829 goto done;
1830 /* Skip length of blob object header first time around. */
1831 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1832 if (flen == 0 && ferror(f)) {
1833 err = got_error_from_errno("fread");
1834 goto done;
1836 if (blen - hdrlen == 0) {
1837 if (flen != 0)
1838 *status = GOT_STATUS_MODIFY;
1839 break;
1840 } else if (flen == 0) {
1841 if (blen - hdrlen != 0)
1842 *status = GOT_STATUS_MODIFY;
1843 break;
1844 } else if (blen - hdrlen == flen) {
1845 /* Skip blob object header first time around. */
1846 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1850 } else {
1851 *status = GOT_STATUS_MODIFY;
1852 break;
1854 hdrlen = 0;
1857 if (*status == GOT_STATUS_MODIFY) {
1858 rewind(f);
1859 err = get_modified_file_content_status(status, f);
1860 } else if (xbit_differs(ie, sb->st_mode))
1861 *status = GOT_STATUS_MODE_CHANGE;
1862 done:
1863 if (blob)
1864 got_object_blob_close(blob);
1865 if (f != NULL && fclose(f) == EOF && err == NULL)
1866 err = got_error_from_errno2("fclose", abspath);
1867 if (fd != -1 && close(fd) == -1 && err == NULL)
1868 err = got_error_from_errno2("close", abspath);
1869 return err;
1873 * Update timestamps in the file index if a file is unmodified and
1874 * we had to run a full content comparison to find out.
1876 static const struct got_error *
1877 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1878 struct got_fileindex_entry *ie, struct stat *sb)
1880 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1881 return got_fileindex_entry_update(ie, wt_fd, path,
1882 ie->blob_sha1, ie->commit_sha1, 1);
1884 return NULL;
1887 static const struct got_error *
1888 update_blob(struct got_worktree *worktree,
1889 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 struct got_tree_entry *te, const char *path,
1891 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 void *progress_arg)
1894 const struct got_error *err = NULL;
1895 struct got_blob_object *blob = NULL;
1896 char *ondisk_path;
1897 unsigned char status = GOT_STATUS_NO_CHANGE;
1898 struct stat sb;
1900 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1901 return got_error_from_errno("asprintf");
1903 if (ie) {
1904 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1905 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1906 goto done;
1908 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 repo);
1910 if (err)
1911 goto done;
1912 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1913 sb.st_mode = got_fileindex_perms_to_st(ie);
1914 } else {
1915 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1916 status = GOT_STATUS_UNVERSIONED;
1919 if (status == GOT_STATUS_OBSTRUCTED) {
1920 err = (*progress_cb)(progress_arg, status, path);
1921 goto done;
1923 if (status == GOT_STATUS_CONFLICT) {
1924 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1925 path);
1926 goto done;
1929 if (ie && status != GOT_STATUS_MISSING &&
1930 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1931 if (got_fileindex_entry_has_commit(ie) &&
1932 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1933 SHA1_DIGEST_LENGTH) == 0) {
1934 err = sync_timestamps(worktree->root_fd,
1935 path, status, ie, &sb);
1936 if (err)
1937 goto done;
1938 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1939 path);
1940 goto done;
1942 if (got_fileindex_entry_has_blob(ie) &&
1943 memcmp(ie->blob_sha1, te->id.sha1,
1944 SHA1_DIGEST_LENGTH) == 0) {
1945 err = sync_timestamps(worktree->root_fd,
1946 path, status, ie, &sb);
1947 goto done;
1951 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1952 if (err)
1953 goto done;
1955 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1956 int update_timestamps;
1957 struct got_blob_object *blob2 = NULL;
1958 char *label_orig = NULL;
1959 if (got_fileindex_entry_has_blob(ie)) {
1960 struct got_object_id id2;
1961 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1962 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1963 if (err)
1964 goto done;
1966 if (got_fileindex_entry_has_commit(ie)) {
1967 char id_str[SHA1_DIGEST_STRING_LENGTH];
1968 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1969 sizeof(id_str)) == NULL) {
1970 err = got_error_path(id_str,
1971 GOT_ERR_BAD_OBJ_ID_STR);
1972 goto done;
1974 if (asprintf(&label_orig, "%s: commit %s",
1975 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1976 err = got_error_from_errno("asprintf");
1977 goto done;
1980 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1981 char *link_target;
1982 err = got_object_blob_read_to_str(&link_target, blob);
1983 if (err)
1984 goto done;
1985 err = merge_symlink(worktree, blob2, ondisk_path, path,
1986 label_orig, link_target, worktree->base_commit_id,
1987 repo, progress_cb, progress_arg);
1988 free(link_target);
1989 } else {
1990 err = merge_blob(&update_timestamps, worktree, blob2,
1991 ondisk_path, path, sb.st_mode, label_orig, blob,
1992 worktree->base_commit_id, repo,
1993 progress_cb, progress_arg);
1995 free(label_orig);
1996 if (blob2)
1997 got_object_blob_close(blob2);
1998 if (err)
1999 goto done;
2001 * Do not update timestamps of files with local changes.
2002 * Otherwise, a future status walk would treat them as
2003 * unmodified files again.
2005 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2006 blob->id.sha1, worktree->base_commit_id->sha1,
2007 update_timestamps);
2008 } else if (status == GOT_STATUS_MODE_CHANGE) {
2009 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2010 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2011 } else if (status == GOT_STATUS_DELETE) {
2012 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2013 if (err)
2014 goto done;
2015 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2016 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2017 if (err)
2018 goto done;
2019 } else {
2020 int is_bad_symlink = 0;
2021 if (S_ISLNK(te->mode)) {
2022 err = install_symlink(&is_bad_symlink, worktree,
2023 ondisk_path, path, blob,
2024 status == GOT_STATUS_MISSING, 0,
2025 status == GOT_STATUS_UNVERSIONED, repo,
2026 progress_cb, progress_arg);
2027 } else {
2028 err = install_blob(worktree, ondisk_path, path,
2029 te->mode, sb.st_mode, blob,
2030 status == GOT_STATUS_MISSING, 0, 0,
2031 status == GOT_STATUS_UNVERSIONED, repo,
2032 progress_cb, progress_arg);
2034 if (err)
2035 goto done;
2037 if (ie) {
2038 err = got_fileindex_entry_update(ie,
2039 worktree->root_fd, path, blob->id.sha1,
2040 worktree->base_commit_id->sha1, 1);
2041 } else {
2042 err = create_fileindex_entry(&ie, fileindex,
2043 worktree->base_commit_id, worktree->root_fd, path,
2044 &blob->id);
2046 if (err)
2047 goto done;
2049 if (is_bad_symlink) {
2050 got_fileindex_entry_filetype_set(ie,
2051 GOT_FILEIDX_MODE_BAD_SYMLINK);
2054 got_object_blob_close(blob);
2055 done:
2056 free(ondisk_path);
2057 return err;
2060 static const struct got_error *
2061 remove_ondisk_file(const char *root_path, const char *path)
2063 const struct got_error *err = NULL;
2064 char *ondisk_path = NULL;
2066 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2067 return got_error_from_errno("asprintf");
2069 if (unlink(ondisk_path) == -1) {
2070 if (errno != ENOENT)
2071 err = got_error_from_errno2("unlink", ondisk_path);
2072 } else {
2073 size_t root_len = strlen(root_path);
2074 do {
2075 char *parent;
2076 err = got_path_dirname(&parent, ondisk_path);
2077 if (err)
2078 break;
2079 free(ondisk_path);
2080 ondisk_path = parent;
2081 if (rmdir(ondisk_path) == -1) {
2082 if (errno != ENOTEMPTY)
2083 err = got_error_from_errno2("rmdir",
2084 ondisk_path);
2085 break;
2087 } while (got_path_cmp(ondisk_path, root_path,
2088 strlen(ondisk_path), root_len) != 0);
2090 free(ondisk_path);
2091 return err;
2094 static const struct got_error *
2095 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2096 struct got_fileindex_entry *ie, struct got_repository *repo,
2097 got_worktree_checkout_cb progress_cb, void *progress_arg)
2099 const struct got_error *err = NULL;
2100 unsigned char status;
2101 struct stat sb;
2102 char *ondisk_path;
2104 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2105 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2107 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2108 == -1)
2109 return got_error_from_errno("asprintf");
2111 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2112 if (err)
2113 goto done;
2115 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2116 char ondisk_target[PATH_MAX];
2117 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2118 sizeof(ondisk_target));
2119 if (ondisk_len == -1) {
2120 err = got_error_from_errno2("readlink", ondisk_path);
2121 goto done;
2123 ondisk_target[ondisk_len] = '\0';
2124 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2125 NULL, NULL, /* XXX pass common ancestor info? */
2126 ondisk_target, ondisk_path);
2127 if (err)
2128 goto done;
2129 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2130 ie->path);
2131 goto done;
2134 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2135 status == GOT_STATUS_ADD) {
2136 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2137 if (err)
2138 goto done;
2140 * Preserve the working file and change the deleted blob's
2141 * entry into a schedule-add entry.
2143 err = got_fileindex_entry_update(ie, worktree->root_fd,
2144 ie->path, NULL, NULL, 0);
2145 } else {
2146 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2147 if (err)
2148 goto done;
2149 if (status == GOT_STATUS_NO_CHANGE) {
2150 err = remove_ondisk_file(worktree->root_path, ie->path);
2151 if (err)
2152 goto done;
2154 got_fileindex_entry_remove(fileindex, ie);
2156 done:
2157 free(ondisk_path);
2158 return err;
2161 struct diff_cb_arg {
2162 struct got_fileindex *fileindex;
2163 struct got_worktree *worktree;
2164 struct got_repository *repo;
2165 got_worktree_checkout_cb progress_cb;
2166 void *progress_arg;
2167 got_cancel_cb cancel_cb;
2168 void *cancel_arg;
2171 static const struct got_error *
2172 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2173 struct got_tree_entry *te, const char *parent_path)
2175 struct diff_cb_arg *a = arg;
2177 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2178 return got_error(GOT_ERR_CANCELLED);
2180 return update_blob(a->worktree, a->fileindex, ie, te,
2181 ie->path, a->repo, a->progress_cb, a->progress_arg);
2184 static const struct got_error *
2185 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2187 struct diff_cb_arg *a = arg;
2189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 return got_error(GOT_ERR_CANCELLED);
2192 return delete_blob(a->worktree, a->fileindex, ie,
2193 a->repo, a->progress_cb, a->progress_arg);
2196 static const struct got_error *
2197 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2199 struct diff_cb_arg *a = arg;
2200 const struct got_error *err;
2201 char *path;
2203 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2204 return got_error(GOT_ERR_CANCELLED);
2206 if (got_object_tree_entry_is_submodule(te))
2207 return NULL;
2209 if (asprintf(&path, "%s%s%s", parent_path,
2210 parent_path[0] ? "/" : "", te->name)
2211 == -1)
2212 return got_error_from_errno("asprintf");
2214 if (S_ISDIR(te->mode))
2215 err = add_dir_on_disk(a->worktree, path);
2216 else
2217 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2218 a->repo, a->progress_cb, a->progress_arg);
2220 free(path);
2221 return err;
2224 const struct got_error *
2225 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2227 uint32_t uuid_status;
2229 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2230 if (uuid_status != uuid_s_ok) {
2231 *uuidstr = NULL;
2232 return got_error_uuid(uuid_status, "uuid_to_string");
2235 return NULL;
2238 static const struct got_error *
2239 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2241 const struct got_error *err = NULL;
2242 char *uuidstr = NULL;
2244 *refname = NULL;
2246 err = got_worktree_get_uuid(&uuidstr, worktree);
2247 if (err)
2248 return err;
2250 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2251 err = got_error_from_errno("asprintf");
2252 *refname = NULL;
2254 free(uuidstr);
2255 return err;
2258 const struct got_error *
2259 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2264 static const struct got_error *
2265 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2267 return get_ref_name(refname, worktree,
2268 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2271 static const struct got_error *
2272 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2277 static const struct got_error *
2278 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2280 return get_ref_name(refname, worktree,
2281 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2284 static const struct got_error *
2285 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2287 return get_ref_name(refname, worktree,
2288 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2291 static const struct got_error *
2292 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree,
2295 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2298 static const struct got_error *
2299 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2301 return get_ref_name(refname, worktree,
2302 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2305 static const struct got_error *
2306 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2308 return get_ref_name(refname, worktree,
2309 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2312 static const struct got_error *
2313 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2315 return get_ref_name(refname, worktree,
2316 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2319 const struct got_error *
2320 got_worktree_get_histedit_script_path(char **path,
2321 struct got_worktree *worktree)
2323 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2324 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2325 *path = NULL;
2326 return got_error_from_errno("asprintf");
2328 return NULL;
2332 * Prevent Git's garbage collector from deleting our base commit by
2333 * setting a reference to our base commit's ID.
2335 static const struct got_error *
2336 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2338 const struct got_error *err = NULL;
2339 struct got_reference *ref = NULL;
2340 char *refname;
2342 err = got_worktree_get_base_ref_name(&refname, worktree);
2343 if (err)
2344 return err;
2346 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2347 if (err)
2348 goto done;
2350 err = got_ref_write(ref, repo);
2351 done:
2352 free(refname);
2353 if (ref)
2354 got_ref_close(ref);
2355 return err;
2358 static const struct got_error *
2359 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2361 const struct got_error *err = NULL;
2363 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2364 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2365 err = got_error_from_errno("asprintf");
2366 *fileindex_path = NULL;
2368 return err;
2372 static const struct got_error *
2373 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2374 struct got_worktree *worktree)
2376 const struct got_error *err = NULL;
2377 FILE *index = NULL;
2379 *fileindex_path = NULL;
2380 *fileindex = got_fileindex_alloc();
2381 if (*fileindex == NULL)
2382 return got_error_from_errno("got_fileindex_alloc");
2384 err = get_fileindex_path(fileindex_path, worktree);
2385 if (err)
2386 goto done;
2388 index = fopen(*fileindex_path, "rb");
2389 if (index == NULL) {
2390 if (errno != ENOENT)
2391 err = got_error_from_errno2("fopen", *fileindex_path);
2392 } else {
2393 err = got_fileindex_read(*fileindex, index);
2394 if (fclose(index) == EOF && err == NULL)
2395 err = got_error_from_errno("fclose");
2397 done:
2398 if (err) {
2399 free(*fileindex_path);
2400 *fileindex_path = NULL;
2401 got_fileindex_free(*fileindex);
2402 *fileindex = NULL;
2404 return err;
2407 struct bump_base_commit_id_arg {
2408 struct got_object_id *base_commit_id;
2409 const char *path;
2410 size_t path_len;
2411 const char *entry_name;
2412 got_worktree_checkout_cb progress_cb;
2413 void *progress_arg;
2416 /* Bump base commit ID of all files within an updated part of the work tree. */
2417 static const struct got_error *
2418 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2420 const struct got_error *err;
2421 struct bump_base_commit_id_arg *a = arg;
2423 if (a->entry_name) {
2424 if (strcmp(ie->path, a->path) != 0)
2425 return NULL;
2426 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2427 return NULL;
2429 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2430 SHA1_DIGEST_LENGTH) == 0)
2431 return NULL;
2433 if (a->progress_cb) {
2434 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2435 ie->path);
2436 if (err)
2437 return err;
2439 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2440 return NULL;
2443 static const struct got_error *
2444 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2445 struct got_fileindex *fileindex,
2446 got_worktree_checkout_cb progress_cb, void *progress_arg)
2448 struct bump_base_commit_id_arg bbc_arg;
2450 bbc_arg.base_commit_id = worktree->base_commit_id;
2451 bbc_arg.entry_name = NULL;
2452 bbc_arg.path = "";
2453 bbc_arg.path_len = 0;
2454 bbc_arg.progress_cb = progress_cb;
2455 bbc_arg.progress_arg = progress_arg;
2457 return got_fileindex_for_each_entry_safe(fileindex,
2458 bump_base_commit_id, &bbc_arg);
2461 static const struct got_error *
2462 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2464 const struct got_error *err = NULL;
2465 char *new_fileindex_path = NULL;
2466 FILE *new_index = NULL;
2467 struct timespec timeout;
2469 err = got_opentemp_named(&new_fileindex_path, &new_index,
2470 fileindex_path);
2471 if (err)
2472 goto done;
2474 err = got_fileindex_write(fileindex, new_index);
2475 if (err)
2476 goto done;
2478 if (rename(new_fileindex_path, fileindex_path) != 0) {
2479 err = got_error_from_errno3("rename", new_fileindex_path,
2480 fileindex_path);
2481 unlink(new_fileindex_path);
2485 * Sleep for a short amount of time to ensure that files modified after
2486 * this program exits have a different time stamp from the one which
2487 * was recorded in the file index.
2489 timeout.tv_sec = 0;
2490 timeout.tv_nsec = 1;
2491 nanosleep(&timeout, NULL);
2492 done:
2493 if (new_index)
2494 fclose(new_index);
2495 free(new_fileindex_path);
2496 return err;
2499 static const struct got_error *
2500 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2501 struct got_object_id **tree_id, const char *wt_relpath,
2502 struct got_worktree *worktree, struct got_repository *repo)
2504 const struct got_error *err = NULL;
2505 struct got_object_id *id = NULL;
2506 char *in_repo_path = NULL;
2507 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2509 *entry_type = GOT_OBJ_TYPE_ANY;
2510 *tree_relpath = NULL;
2511 *tree_id = NULL;
2513 if (wt_relpath[0] == '\0') {
2514 /* Check out all files within the work tree. */
2515 *entry_type = GOT_OBJ_TYPE_TREE;
2516 *tree_relpath = strdup("");
2517 if (*tree_relpath == NULL) {
2518 err = got_error_from_errno("strdup");
2519 goto done;
2521 err = got_object_id_by_path(tree_id, repo,
2522 worktree->base_commit_id, worktree->path_prefix);
2523 if (err)
2524 goto done;
2525 return NULL;
2528 /* Check out a subset of files in the work tree. */
2530 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2531 is_root_wt ? "" : "/", wt_relpath) == -1) {
2532 err = got_error_from_errno("asprintf");
2533 goto done;
2536 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2537 in_repo_path);
2538 if (err)
2539 goto done;
2541 free(in_repo_path);
2542 in_repo_path = NULL;
2544 err = got_object_get_type(entry_type, repo, id);
2545 if (err)
2546 goto done;
2548 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2549 /* Check out a single file. */
2550 if (strchr(wt_relpath, '/') == NULL) {
2551 /* Check out a single file in work tree's root dir. */
2552 in_repo_path = strdup(worktree->path_prefix);
2553 if (in_repo_path == NULL) {
2554 err = got_error_from_errno("strdup");
2555 goto done;
2557 *tree_relpath = strdup("");
2558 if (*tree_relpath == NULL) {
2559 err = got_error_from_errno("strdup");
2560 goto done;
2562 } else {
2563 /* Check out a single file in a subdirectory. */
2564 err = got_path_dirname(tree_relpath, wt_relpath);
2565 if (err)
2566 return err;
2567 if (asprintf(&in_repo_path, "%s%s%s",
2568 worktree->path_prefix, is_root_wt ? "" : "/",
2569 *tree_relpath) == -1) {
2570 err = got_error_from_errno("asprintf");
2571 goto done;
2574 err = got_object_id_by_path(tree_id, repo,
2575 worktree->base_commit_id, in_repo_path);
2576 } else {
2577 /* Check out all files within a subdirectory. */
2578 *tree_id = got_object_id_dup(id);
2579 if (*tree_id == NULL) {
2580 err = got_error_from_errno("got_object_id_dup");
2581 goto done;
2583 *tree_relpath = strdup(wt_relpath);
2584 if (*tree_relpath == NULL) {
2585 err = got_error_from_errno("strdup");
2586 goto done;
2589 done:
2590 free(id);
2591 free(in_repo_path);
2592 if (err) {
2593 *entry_type = GOT_OBJ_TYPE_ANY;
2594 free(*tree_relpath);
2595 *tree_relpath = NULL;
2596 free(*tree_id);
2597 *tree_id = NULL;
2599 return err;
2602 static const struct got_error *
2603 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2604 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2605 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2608 const struct got_error *err = NULL;
2609 struct got_commit_object *commit = NULL;
2610 struct got_tree_object *tree = NULL;
2611 struct got_fileindex_diff_tree_cb diff_cb;
2612 struct diff_cb_arg arg;
2614 err = ref_base_commit(worktree, repo);
2615 if (err) {
2616 if (!(err->code == GOT_ERR_ERRNO &&
2617 (errno == EACCES || errno == EROFS)))
2618 goto done;
2619 err = (*progress_cb)(progress_arg,
2620 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2621 if (err)
2622 return err;
2625 err = got_object_open_as_commit(&commit, repo,
2626 worktree->base_commit_id);
2627 if (err)
2628 goto done;
2630 err = got_object_open_as_tree(&tree, repo, tree_id);
2631 if (err)
2632 goto done;
2634 if (entry_name &&
2635 got_object_tree_find_entry(tree, entry_name) == NULL) {
2636 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2637 goto done;
2640 diff_cb.diff_old_new = diff_old_new;
2641 diff_cb.diff_old = diff_old;
2642 diff_cb.diff_new = diff_new;
2643 arg.fileindex = fileindex;
2644 arg.worktree = worktree;
2645 arg.repo = repo;
2646 arg.progress_cb = progress_cb;
2647 arg.progress_arg = progress_arg;
2648 arg.cancel_cb = cancel_cb;
2649 arg.cancel_arg = cancel_arg;
2650 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2651 entry_name, repo, &diff_cb, &arg);
2652 done:
2653 if (tree)
2654 got_object_tree_close(tree);
2655 if (commit)
2656 got_object_commit_close(commit);
2657 return err;
2660 const struct got_error *
2661 got_worktree_checkout_files(struct got_worktree *worktree,
2662 struct got_pathlist_head *paths, struct got_repository *repo,
2663 got_worktree_checkout_cb progress_cb, void *progress_arg,
2664 got_cancel_cb cancel_cb, void *cancel_arg)
2666 const struct got_error *err = NULL, *sync_err, *unlockerr;
2667 struct got_commit_object *commit = NULL;
2668 struct got_tree_object *tree = NULL;
2669 struct got_fileindex *fileindex = NULL;
2670 char *fileindex_path = NULL;
2671 struct got_pathlist_entry *pe;
2672 struct tree_path_data {
2673 SIMPLEQ_ENTRY(tree_path_data) entry;
2674 struct got_object_id *tree_id;
2675 int entry_type;
2676 char *relpath;
2677 char *entry_name;
2678 } *tpd = NULL;
2679 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2681 SIMPLEQ_INIT(&tree_paths);
2683 err = lock_worktree(worktree, LOCK_EX);
2684 if (err)
2685 return err;
2687 /* Map all specified paths to in-repository trees. */
2688 TAILQ_FOREACH(pe, paths, entry) {
2689 tpd = malloc(sizeof(*tpd));
2690 if (tpd == NULL) {
2691 err = got_error_from_errno("malloc");
2692 goto done;
2695 err = find_tree_entry_for_checkout(&tpd->entry_type,
2696 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2697 if (err) {
2698 free(tpd);
2699 goto done;
2702 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2703 err = got_path_basename(&tpd->entry_name, pe->path);
2704 if (err) {
2705 free(tpd->relpath);
2706 free(tpd->tree_id);
2707 free(tpd);
2708 goto done;
2710 } else
2711 tpd->entry_name = NULL;
2713 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2717 * Read the file index.
2718 * Checking out files is supposed to be an idempotent operation.
2719 * If the on-disk file index is incomplete we will try to complete it.
2721 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2722 if (err)
2723 goto done;
2725 tpd = SIMPLEQ_FIRST(&tree_paths);
2726 TAILQ_FOREACH(pe, paths, entry) {
2727 struct bump_base_commit_id_arg bbc_arg;
2729 err = checkout_files(worktree, fileindex, tpd->relpath,
2730 tpd->tree_id, tpd->entry_name, repo,
2731 progress_cb, progress_arg, cancel_cb, cancel_arg);
2732 if (err)
2733 break;
2735 bbc_arg.base_commit_id = worktree->base_commit_id;
2736 bbc_arg.entry_name = tpd->entry_name;
2737 bbc_arg.path = pe->path;
2738 bbc_arg.path_len = pe->path_len;
2739 bbc_arg.progress_cb = progress_cb;
2740 bbc_arg.progress_arg = progress_arg;
2741 err = got_fileindex_for_each_entry_safe(fileindex,
2742 bump_base_commit_id, &bbc_arg);
2743 if (err)
2744 break;
2746 tpd = SIMPLEQ_NEXT(tpd, entry);
2748 sync_err = sync_fileindex(fileindex, fileindex_path);
2749 if (sync_err && err == NULL)
2750 err = sync_err;
2751 done:
2752 free(fileindex_path);
2753 if (tree)
2754 got_object_tree_close(tree);
2755 if (commit)
2756 got_object_commit_close(commit);
2757 if (fileindex)
2758 got_fileindex_free(fileindex);
2759 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2760 tpd = SIMPLEQ_FIRST(&tree_paths);
2761 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2762 free(tpd->relpath);
2763 free(tpd->tree_id);
2764 free(tpd);
2766 unlockerr = lock_worktree(worktree, LOCK_SH);
2767 if (unlockerr && err == NULL)
2768 err = unlockerr;
2769 return err;
2772 struct merge_file_cb_arg {
2773 struct got_worktree *worktree;
2774 struct got_fileindex *fileindex;
2775 got_worktree_checkout_cb progress_cb;
2776 void *progress_arg;
2777 got_cancel_cb cancel_cb;
2778 void *cancel_arg;
2779 const char *label_orig;
2780 struct got_object_id *commit_id2;
2783 static const struct got_error *
2784 merge_file_cb(void *arg, struct got_blob_object *blob1,
2785 struct got_blob_object *blob2, struct got_object_id *id1,
2786 struct got_object_id *id2, const char *path1, const char *path2,
2787 mode_t mode1, mode_t mode2, struct got_repository *repo)
2789 static const struct got_error *err = NULL;
2790 struct merge_file_cb_arg *a = arg;
2791 struct got_fileindex_entry *ie;
2792 char *ondisk_path = NULL;
2793 struct stat sb;
2794 unsigned char status;
2795 int local_changes_subsumed;
2797 if (blob1 && blob2) {
2798 ie = got_fileindex_entry_get(a->fileindex, path2,
2799 strlen(path2));
2800 if (ie == NULL)
2801 return (*a->progress_cb)(a->progress_arg,
2802 GOT_STATUS_MISSING, path2);
2804 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2805 path2) == -1)
2806 return got_error_from_errno("asprintf");
2808 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2809 repo);
2810 if (err)
2811 goto done;
2813 if (status == GOT_STATUS_DELETE) {
2814 err = (*a->progress_cb)(a->progress_arg,
2815 GOT_STATUS_MERGE, path2);
2816 goto done;
2818 if (status != GOT_STATUS_NO_CHANGE &&
2819 status != GOT_STATUS_MODIFY &&
2820 status != GOT_STATUS_CONFLICT &&
2821 status != GOT_STATUS_ADD) {
2822 err = (*a->progress_cb)(a->progress_arg, status, path2);
2823 goto done;
2826 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2827 char *link_target2;
2828 err = got_object_blob_read_to_str(&link_target2, blob2);
2829 if (err)
2830 goto done;
2831 err = merge_symlink(a->worktree, blob1, ondisk_path,
2832 path2, a->label_orig, link_target2, a->commit_id2,
2833 repo, a->progress_cb, a->progress_arg);
2834 free(link_target2);
2835 } else {
2836 err = merge_blob(&local_changes_subsumed, a->worktree,
2837 blob1, ondisk_path, path2, sb.st_mode,
2838 a->label_orig, blob2, a->commit_id2, repo,
2839 a->progress_cb, a->progress_arg);
2841 } else if (blob1) {
2842 ie = got_fileindex_entry_get(a->fileindex, path1,
2843 strlen(path1));
2844 if (ie == NULL)
2845 return (*a->progress_cb)(a->progress_arg,
2846 GOT_STATUS_MISSING, path1);
2848 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2849 path1) == -1)
2850 return got_error_from_errno("asprintf");
2852 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2853 repo);
2854 if (err)
2855 goto done;
2857 switch (status) {
2858 case GOT_STATUS_NO_CHANGE:
2859 err = (*a->progress_cb)(a->progress_arg,
2860 GOT_STATUS_DELETE, path1);
2861 if (err)
2862 goto done;
2863 err = remove_ondisk_file(a->worktree->root_path, path1);
2864 if (err)
2865 goto done;
2866 if (ie)
2867 got_fileindex_entry_mark_deleted_from_disk(ie);
2868 break;
2869 case GOT_STATUS_DELETE:
2870 case GOT_STATUS_MISSING:
2871 err = (*a->progress_cb)(a->progress_arg,
2872 GOT_STATUS_DELETE, path1);
2873 if (err)
2874 goto done;
2875 if (ie)
2876 got_fileindex_entry_mark_deleted_from_disk(ie);
2877 break;
2878 case GOT_STATUS_ADD: {
2879 struct got_object_id *id;
2880 FILE *blob1_f;
2882 * Delete the added file only if its content already
2883 * exists in the repository.
2885 err = got_object_blob_file_create(&id, &blob1_f, path1);
2886 if (err)
2887 goto done;
2888 if (got_object_id_cmp(id, id1) == 0) {
2889 err = (*a->progress_cb)(a->progress_arg,
2890 GOT_STATUS_DELETE, path1);
2891 if (err)
2892 goto done;
2893 err = remove_ondisk_file(a->worktree->root_path,
2894 path1);
2895 if (err)
2896 goto done;
2897 if (ie)
2898 got_fileindex_entry_remove(a->fileindex,
2899 ie);
2900 } else {
2901 err = (*a->progress_cb)(a->progress_arg,
2902 GOT_STATUS_CANNOT_DELETE, path1);
2904 if (fclose(blob1_f) == EOF && err == NULL)
2905 err = got_error_from_errno("fclose");
2906 free(id);
2907 if (err)
2908 goto done;
2909 break;
2911 case GOT_STATUS_MODIFY:
2912 case GOT_STATUS_CONFLICT:
2913 err = (*a->progress_cb)(a->progress_arg,
2914 GOT_STATUS_CANNOT_DELETE, path1);
2915 if (err)
2916 goto done;
2917 break;
2918 case GOT_STATUS_OBSTRUCTED:
2919 err = (*a->progress_cb)(a->progress_arg, status, path1);
2920 if (err)
2921 goto done;
2922 break;
2923 default:
2924 break;
2926 } else if (blob2) {
2927 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2928 path2) == -1)
2929 return got_error_from_errno("asprintf");
2930 ie = got_fileindex_entry_get(a->fileindex, path2,
2931 strlen(path2));
2932 if (ie) {
2933 err = get_file_status(&status, &sb, ie, ondisk_path,
2934 -1, NULL, repo);
2935 if (err)
2936 goto done;
2937 if (status != GOT_STATUS_NO_CHANGE &&
2938 status != GOT_STATUS_MODIFY &&
2939 status != GOT_STATUS_CONFLICT &&
2940 status != GOT_STATUS_ADD) {
2941 err = (*a->progress_cb)(a->progress_arg,
2942 status, path2);
2943 goto done;
2945 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2946 char *link_target2;
2947 err = got_object_blob_read_to_str(&link_target2,
2948 blob2);
2949 if (err)
2950 goto done;
2951 err = merge_symlink(a->worktree, NULL,
2952 ondisk_path, path2, a->label_orig,
2953 link_target2, a->commit_id2, repo,
2954 a->progress_cb, a->progress_arg);
2955 free(link_target2);
2956 } else if (S_ISREG(sb.st_mode)) {
2957 err = merge_blob(&local_changes_subsumed,
2958 a->worktree, NULL, ondisk_path, path2,
2959 sb.st_mode, a->label_orig, blob2,
2960 a->commit_id2, repo, a->progress_cb,
2961 a->progress_arg);
2962 } else {
2963 err = got_error_path(ondisk_path,
2964 GOT_ERR_FILE_OBSTRUCTED);
2966 if (err)
2967 goto done;
2968 if (status == GOT_STATUS_DELETE) {
2969 err = got_fileindex_entry_update(ie,
2970 a->worktree->root_fd, path2, blob2->id.sha1,
2971 a->worktree->base_commit_id->sha1, 0);
2972 if (err)
2973 goto done;
2975 } else {
2976 int is_bad_symlink = 0;
2977 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2978 if (S_ISLNK(mode2)) {
2979 err = install_symlink(&is_bad_symlink,
2980 a->worktree, ondisk_path, path2, blob2, 0,
2981 0, 1, repo, a->progress_cb, a->progress_arg);
2982 } else {
2983 err = install_blob(a->worktree, ondisk_path, path2,
2984 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2985 a->progress_cb, a->progress_arg);
2987 if (err)
2988 goto done;
2989 err = got_fileindex_entry_alloc(&ie, path2);
2990 if (err)
2991 goto done;
2992 err = got_fileindex_entry_update(ie,
2993 a->worktree->root_fd, path2, NULL, NULL, 1);
2994 if (err) {
2995 got_fileindex_entry_free(ie);
2996 goto done;
2998 err = got_fileindex_entry_add(a->fileindex, ie);
2999 if (err) {
3000 got_fileindex_entry_free(ie);
3001 goto done;
3003 if (is_bad_symlink) {
3004 got_fileindex_entry_filetype_set(ie,
3005 GOT_FILEIDX_MODE_BAD_SYMLINK);
3009 done:
3010 free(ondisk_path);
3011 return err;
3014 struct check_merge_ok_arg {
3015 struct got_worktree *worktree;
3016 struct got_repository *repo;
3019 static const struct got_error *
3020 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3022 const struct got_error *err = NULL;
3023 struct check_merge_ok_arg *a = arg;
3024 unsigned char status;
3025 struct stat sb;
3026 char *ondisk_path;
3028 /* Reject merges into a work tree with mixed base commits. */
3029 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3030 SHA1_DIGEST_LENGTH))
3031 return got_error(GOT_ERR_MIXED_COMMITS);
3033 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3034 == -1)
3035 return got_error_from_errno("asprintf");
3037 /* Reject merges into a work tree with conflicted files. */
3038 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3039 if (err)
3040 return err;
3041 if (status == GOT_STATUS_CONFLICT)
3042 return got_error(GOT_ERR_CONFLICTS);
3044 return NULL;
3047 static const struct got_error *
3048 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3049 const char *fileindex_path, struct got_object_id *commit_id1,
3050 struct got_object_id *commit_id2, struct got_repository *repo,
3051 got_worktree_checkout_cb progress_cb, void *progress_arg,
3052 got_cancel_cb cancel_cb, void *cancel_arg)
3054 const struct got_error *err = NULL, *sync_err;
3055 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3056 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3057 struct merge_file_cb_arg arg;
3058 char *label_orig = NULL;
3060 if (commit_id1) {
3061 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3062 worktree->path_prefix);
3063 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3064 goto done;
3066 if (tree_id1) {
3067 char *id_str;
3069 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3070 if (err)
3071 goto done;
3073 err = got_object_id_str(&id_str, commit_id1);
3074 if (err)
3075 goto done;
3077 if (asprintf(&label_orig, "%s: commit %s",
3078 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3079 err = got_error_from_errno("asprintf");
3080 free(id_str);
3081 goto done;
3083 free(id_str);
3086 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3087 worktree->path_prefix);
3088 if (err)
3089 goto done;
3091 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3092 if (err)
3093 goto done;
3095 arg.worktree = worktree;
3096 arg.fileindex = fileindex;
3097 arg.progress_cb = progress_cb;
3098 arg.progress_arg = progress_arg;
3099 arg.cancel_cb = cancel_cb;
3100 arg.cancel_arg = cancel_arg;
3101 arg.label_orig = label_orig;
3102 arg.commit_id2 = commit_id2;
3103 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3104 sync_err = sync_fileindex(fileindex, fileindex_path);
3105 if (sync_err && err == NULL)
3106 err = sync_err;
3107 done:
3108 if (tree1)
3109 got_object_tree_close(tree1);
3110 if (tree2)
3111 got_object_tree_close(tree2);
3112 free(label_orig);
3113 return err;
3116 const struct got_error *
3117 got_worktree_merge_files(struct got_worktree *worktree,
3118 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3119 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3120 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3122 const struct got_error *err, *unlockerr;
3123 char *fileindex_path = NULL;
3124 struct got_fileindex *fileindex = NULL;
3125 struct check_merge_ok_arg mok_arg;
3127 err = lock_worktree(worktree, LOCK_EX);
3128 if (err)
3129 return err;
3131 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3132 if (err)
3133 goto done;
3135 mok_arg.worktree = worktree;
3136 mok_arg.repo = repo;
3137 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3138 &mok_arg);
3139 if (err)
3140 goto done;
3142 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3143 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3144 done:
3145 if (fileindex)
3146 got_fileindex_free(fileindex);
3147 free(fileindex_path);
3148 unlockerr = lock_worktree(worktree, LOCK_SH);
3149 if (unlockerr && err == NULL)
3150 err = unlockerr;
3151 return err;
3154 struct diff_dir_cb_arg {
3155 struct got_fileindex *fileindex;
3156 struct got_worktree *worktree;
3157 const char *status_path;
3158 size_t status_path_len;
3159 struct got_repository *repo;
3160 got_worktree_status_cb status_cb;
3161 void *status_arg;
3162 got_cancel_cb cancel_cb;
3163 void *cancel_arg;
3164 /* A pathlist containing per-directory pathlists of ignore patterns. */
3165 struct got_pathlist_head ignores;
3166 int report_unchanged;
3167 int no_ignores;
3170 static const struct got_error *
3171 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3172 int dirfd, const char *de_name,
3173 got_worktree_status_cb status_cb, void *status_arg,
3174 struct got_repository *repo, int report_unchanged)
3176 const struct got_error *err = NULL;
3177 unsigned char status = GOT_STATUS_NO_CHANGE;
3178 unsigned char staged_status = get_staged_status(ie);
3179 struct stat sb;
3180 struct got_object_id blob_id, commit_id, staged_blob_id;
3181 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3182 struct got_object_id *staged_blob_idp = NULL;
3184 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3185 if (err)
3186 return err;
3188 if (status == GOT_STATUS_NO_CHANGE &&
3189 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3190 return NULL;
3192 if (got_fileindex_entry_has_blob(ie)) {
3193 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3194 blob_idp = &blob_id;
3196 if (got_fileindex_entry_has_commit(ie)) {
3197 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3198 commit_idp = &commit_id;
3200 if (staged_status == GOT_STATUS_ADD ||
3201 staged_status == GOT_STATUS_MODIFY) {
3202 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3203 SHA1_DIGEST_LENGTH);
3204 staged_blob_idp = &staged_blob_id;
3207 return (*status_cb)(status_arg, status, staged_status,
3208 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3211 static const struct got_error *
3212 status_old_new(void *arg, struct got_fileindex_entry *ie,
3213 struct dirent *de, const char *parent_path, int dirfd)
3215 const struct got_error *err = NULL;
3216 struct diff_dir_cb_arg *a = arg;
3217 char *abspath;
3219 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3220 return got_error(GOT_ERR_CANCELLED);
3222 if (got_path_cmp(parent_path, a->status_path,
3223 strlen(parent_path), a->status_path_len) != 0 &&
3224 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3225 return NULL;
3227 if (parent_path[0]) {
3228 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3229 parent_path, de->d_name) == -1)
3230 return got_error_from_errno("asprintf");
3231 } else {
3232 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3233 de->d_name) == -1)
3234 return got_error_from_errno("asprintf");
3237 err = report_file_status(ie, abspath, dirfd, de->d_name,
3238 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3239 free(abspath);
3240 return err;
3243 static const struct got_error *
3244 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3246 struct diff_dir_cb_arg *a = arg;
3247 struct got_object_id blob_id, commit_id;
3248 unsigned char status;
3250 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3251 return got_error(GOT_ERR_CANCELLED);
3253 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3254 return NULL;
3256 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3257 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3258 if (got_fileindex_entry_has_file_on_disk(ie))
3259 status = GOT_STATUS_MISSING;
3260 else
3261 status = GOT_STATUS_DELETE;
3262 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3263 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3266 void
3267 free_ignorelist(struct got_pathlist_head *ignorelist)
3269 struct got_pathlist_entry *pe;
3271 TAILQ_FOREACH(pe, ignorelist, entry)
3272 free((char *)pe->path);
3273 got_pathlist_free(ignorelist);
3276 void
3277 free_ignores(struct got_pathlist_head *ignores)
3279 struct got_pathlist_entry *pe;
3281 TAILQ_FOREACH(pe, ignores, entry) {
3282 struct got_pathlist_head *ignorelist = pe->data;
3283 free_ignorelist(ignorelist);
3284 free((char *)pe->path);
3286 got_pathlist_free(ignores);
3289 static const struct got_error *
3290 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3292 const struct got_error *err = NULL;
3293 struct got_pathlist_entry *pe = NULL;
3294 struct got_pathlist_head *ignorelist;
3295 char *line = NULL, *pattern, *dirpath = NULL;
3296 size_t linesize = 0;
3297 ssize_t linelen;
3299 ignorelist = calloc(1, sizeof(*ignorelist));
3300 if (ignorelist == NULL)
3301 return got_error_from_errno("calloc");
3302 TAILQ_INIT(ignorelist);
3304 while ((linelen = getline(&line, &linesize, f)) != -1) {
3305 if (linelen > 0 && line[linelen - 1] == '\n')
3306 line[linelen - 1] = '\0';
3308 /* Git's ignores may contain comments. */
3309 if (line[0] == '#')
3310 continue;
3312 /* Git's negated patterns are not (yet?) supported. */
3313 if (line[0] == '!')
3314 continue;
3316 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3317 line) == -1) {
3318 err = got_error_from_errno("asprintf");
3319 goto done;
3321 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3322 if (err)
3323 goto done;
3325 if (ferror(f)) {
3326 err = got_error_from_errno("getline");
3327 goto done;
3330 dirpath = strdup(path);
3331 if (dirpath == NULL) {
3332 err = got_error_from_errno("strdup");
3333 goto done;
3335 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3336 done:
3337 free(line);
3338 if (err || pe == NULL) {
3339 free(dirpath);
3340 free_ignorelist(ignorelist);
3342 return err;
3345 int
3346 match_ignores(struct got_pathlist_head *ignores, const char *path)
3348 struct got_pathlist_entry *pe;
3350 /* Handle patterns which match in all directories. */
3351 TAILQ_FOREACH(pe, ignores, entry) {
3352 struct got_pathlist_head *ignorelist = pe->data;
3353 struct got_pathlist_entry *pi;
3355 TAILQ_FOREACH(pi, ignorelist, entry) {
3356 const char *p, *pattern = pi->path;
3358 if (strncmp(pattern, "**/", 3) != 0)
3359 continue;
3360 pattern += 3;
3361 p = path;
3362 while (*p) {
3363 if (fnmatch(pattern, p,
3364 FNM_PATHNAME | FNM_LEADING_DIR)) {
3365 /* Retry in next directory. */
3366 while (*p && *p != '/')
3367 p++;
3368 while (*p == '/')
3369 p++;
3370 continue;
3372 return 1;
3378 * The ignores pathlist contains ignore lists from children before
3379 * parents, so we can find the most specific ignorelist by walking
3380 * ignores backwards.
3382 pe = TAILQ_LAST(ignores, got_pathlist_head);
3383 while (pe) {
3384 if (got_path_is_child(path, pe->path, pe->path_len)) {
3385 struct got_pathlist_head *ignorelist = pe->data;
3386 struct got_pathlist_entry *pi;
3387 TAILQ_FOREACH(pi, ignorelist, entry) {
3388 const char *pattern = pi->path;
3389 int flags = FNM_LEADING_DIR;
3390 if (strstr(pattern, "/**/") == NULL)
3391 flags |= FNM_PATHNAME;
3392 if (fnmatch(pattern, path, flags))
3393 continue;
3394 return 1;
3397 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3400 return 0;
3403 static const struct got_error *
3404 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3405 const char *path, int dirfd, const char *ignores_filename)
3407 const struct got_error *err = NULL;
3408 char *ignorespath;
3409 int fd = -1;
3410 FILE *ignoresfile = NULL;
3412 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3413 path[0] ? "/" : "", ignores_filename) == -1)
3414 return got_error_from_errno("asprintf");
3416 if (dirfd != -1) {
3417 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3418 if (fd == -1) {
3419 if (errno != ENOENT && errno != EACCES)
3420 err = got_error_from_errno2("openat",
3421 ignorespath);
3422 } else {
3423 ignoresfile = fdopen(fd, "r");
3424 if (ignoresfile == NULL)
3425 err = got_error_from_errno2("fdopen",
3426 ignorespath);
3427 else {
3428 fd = -1;
3429 err = read_ignores(ignores, path, ignoresfile);
3432 } else {
3433 ignoresfile = fopen(ignorespath, "r");
3434 if (ignoresfile == NULL) {
3435 if (errno != ENOENT && errno != EACCES)
3436 err = got_error_from_errno2("fopen",
3437 ignorespath);
3438 } else
3439 err = read_ignores(ignores, path, ignoresfile);
3442 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3443 err = got_error_from_errno2("fclose", path);
3444 if (fd != -1 && close(fd) == -1 && err == NULL)
3445 err = got_error_from_errno2("close", path);
3446 free(ignorespath);
3447 return err;
3450 static const struct got_error *
3451 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3453 const struct got_error *err = NULL;
3454 struct diff_dir_cb_arg *a = arg;
3455 char *path = NULL;
3457 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3458 return got_error(GOT_ERR_CANCELLED);
3460 if (parent_path[0]) {
3461 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3462 return got_error_from_errno("asprintf");
3463 } else {
3464 path = de->d_name;
3467 if (de->d_type != DT_DIR &&
3468 got_path_is_child(path, a->status_path, a->status_path_len)
3469 && !match_ignores(&a->ignores, path))
3470 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3471 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3472 if (parent_path[0])
3473 free(path);
3474 return err;
3477 static const struct got_error *
3478 status_traverse(void *arg, const char *path, int dirfd)
3480 const struct got_error *err = NULL;
3481 struct diff_dir_cb_arg *a = arg;
3483 if (a->no_ignores)
3484 return NULL;
3486 err = add_ignores(&a->ignores, a->worktree->root_path,
3487 path, dirfd, ".cvsignore");
3488 if (err)
3489 return err;
3491 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3492 dirfd, ".gitignore");
3494 return err;
3497 static const struct got_error *
3498 report_single_file_status(const char *path, const char *ondisk_path,
3499 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3500 void *status_arg, struct got_repository *repo, int report_unchanged)
3502 struct got_fileindex_entry *ie;
3503 struct stat sb;
3505 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3506 if (ie)
3507 return report_file_status(ie, ondisk_path, -1, NULL,
3508 status_cb, status_arg, repo, report_unchanged);
3510 if (lstat(ondisk_path, &sb) == -1) {
3511 if (errno != ENOENT)
3512 return got_error_from_errno2("lstat", ondisk_path);
3513 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3514 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3515 return NULL;
3518 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3519 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3520 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3522 return NULL;
3525 static const struct got_error *
3526 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3527 const char *root_path, const char *path)
3529 const struct got_error *err;
3530 char *parent_path, *next_parent_path = NULL;
3532 err = add_ignores(ignores, root_path, "", -1,
3533 ".cvsignore");
3534 if (err)
3535 return err;
3537 err = add_ignores(ignores, root_path, "", -1,
3538 ".gitignore");
3539 if (err)
3540 return err;
3542 err = got_path_dirname(&parent_path, path);
3543 if (err) {
3544 if (err->code == GOT_ERR_BAD_PATH)
3545 return NULL; /* cannot traverse parent */
3546 return err;
3548 for (;;) {
3549 err = add_ignores(ignores, root_path, parent_path, -1,
3550 ".cvsignore");
3551 if (err)
3552 break;
3553 err = add_ignores(ignores, root_path, parent_path, -1,
3554 ".gitignore");
3555 if (err)
3556 break;
3557 err = got_path_dirname(&next_parent_path, parent_path);
3558 if (err) {
3559 if (err->code == GOT_ERR_BAD_PATH)
3560 err = NULL; /* traversed everything */
3561 break;
3563 free(parent_path);
3564 parent_path = next_parent_path;
3565 next_parent_path = NULL;
3568 free(parent_path);
3569 free(next_parent_path);
3570 return err;
3573 static const struct got_error *
3574 worktree_status(struct got_worktree *worktree, const char *path,
3575 struct got_fileindex *fileindex, struct got_repository *repo,
3576 got_worktree_status_cb status_cb, void *status_arg,
3577 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3578 int report_unchanged)
3580 const struct got_error *err = NULL;
3581 int fd = -1;
3582 struct got_fileindex_diff_dir_cb fdiff_cb;
3583 struct diff_dir_cb_arg arg;
3584 char *ondisk_path = NULL;
3586 TAILQ_INIT(&arg.ignores);
3588 if (asprintf(&ondisk_path, "%s%s%s",
3589 worktree->root_path, path[0] ? "/" : "", path) == -1)
3590 return got_error_from_errno("asprintf");
3592 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3593 if (fd == -1) {
3594 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3595 errno != ELOOP)
3596 err = got_error_from_errno2("open", ondisk_path);
3597 else
3598 err = report_single_file_status(path, ondisk_path,
3599 fileindex, status_cb, status_arg, repo,
3600 report_unchanged);
3601 } else {
3602 fdiff_cb.diff_old_new = status_old_new;
3603 fdiff_cb.diff_old = status_old;
3604 fdiff_cb.diff_new = status_new;
3605 fdiff_cb.diff_traverse = status_traverse;
3606 arg.fileindex = fileindex;
3607 arg.worktree = worktree;
3608 arg.status_path = path;
3609 arg.status_path_len = strlen(path);
3610 arg.repo = repo;
3611 arg.status_cb = status_cb;
3612 arg.status_arg = status_arg;
3613 arg.cancel_cb = cancel_cb;
3614 arg.cancel_arg = cancel_arg;
3615 arg.report_unchanged = report_unchanged;
3616 arg.no_ignores = no_ignores;
3617 if (!no_ignores) {
3618 err = add_ignores_from_parent_paths(&arg.ignores,
3619 worktree->root_path, path);
3620 if (err)
3621 goto done;
3623 err = got_fileindex_diff_dir(fileindex, fd,
3624 worktree->root_path, path, repo, &fdiff_cb, &arg);
3626 done:
3627 free_ignores(&arg.ignores);
3628 if (fd != -1 && close(fd) == -1 && err == NULL)
3629 err = got_error_from_errno("close");
3630 free(ondisk_path);
3631 return err;
3634 const struct got_error *
3635 got_worktree_status(struct got_worktree *worktree,
3636 struct got_pathlist_head *paths, struct got_repository *repo,
3637 got_worktree_status_cb status_cb, void *status_arg,
3638 got_cancel_cb cancel_cb, void *cancel_arg)
3640 const struct got_error *err = NULL;
3641 char *fileindex_path = NULL;
3642 struct got_fileindex *fileindex = NULL;
3643 struct got_pathlist_entry *pe;
3645 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3646 if (err)
3647 return err;
3649 TAILQ_FOREACH(pe, paths, entry) {
3650 err = worktree_status(worktree, pe->path, fileindex, repo,
3651 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3652 if (err)
3653 break;
3655 free(fileindex_path);
3656 got_fileindex_free(fileindex);
3657 return err;
3660 const struct got_error *
3661 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3662 const char *arg)
3664 const struct got_error *err = NULL;
3665 char *resolved = NULL, *cwd = NULL, *path = NULL;
3666 size_t len;
3667 struct stat sb;
3668 char *abspath = NULL;
3669 char canonpath[PATH_MAX];
3671 *wt_path = NULL;
3673 cwd = getcwd(NULL, 0);
3674 if (cwd == NULL)
3675 return got_error_from_errno("getcwd");
3677 if (lstat(arg, &sb) == -1) {
3678 if (errno != ENOENT) {
3679 err = got_error_from_errno2("lstat", arg);
3680 goto done;
3682 sb.st_mode = 0;
3684 if (S_ISLNK(sb.st_mode)) {
3686 * We cannot use realpath(3) with symlinks since we want to
3687 * operate on the symlink itself.
3688 * But we can make the path absolute, assuming it is relative
3689 * to the current working directory, and then canonicalize it.
3691 if (!got_path_is_absolute(arg)) {
3692 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3693 err = got_error_from_errno("asprintf");
3694 goto done;
3698 err = got_canonpath(abspath ? abspath : arg, canonpath,
3699 sizeof(canonpath));
3700 if (err)
3701 goto done;
3702 resolved = strdup(canonpath);
3703 if (resolved == NULL) {
3704 err = got_error_from_errno("strdup");
3705 goto done;
3707 } else {
3708 resolved = realpath(arg, NULL);
3709 if (resolved == NULL) {
3710 if (errno != ENOENT) {
3711 err = got_error_from_errno2("realpath", arg);
3712 goto done;
3714 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3715 err = got_error_from_errno("asprintf");
3716 goto done;
3718 err = got_canonpath(abspath, canonpath,
3719 sizeof(canonpath));
3720 if (err)
3721 goto done;
3722 resolved = strdup(canonpath);
3723 if (resolved == NULL) {
3724 err = got_error_from_errno("strdup");
3725 goto done;
3730 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3731 strlen(got_worktree_get_root_path(worktree)))) {
3732 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3733 goto done;
3736 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3737 err = got_path_skip_common_ancestor(&path,
3738 got_worktree_get_root_path(worktree), resolved);
3739 if (err)
3740 goto done;
3741 } else {
3742 path = strdup("");
3743 if (path == NULL) {
3744 err = got_error_from_errno("strdup");
3745 goto done;
3749 /* XXX status walk can't deal with trailing slash! */
3750 len = strlen(path);
3751 while (len > 0 && path[len - 1] == '/') {
3752 path[len - 1] = '\0';
3753 len--;
3755 done:
3756 free(abspath);
3757 free(resolved);
3758 free(cwd);
3759 if (err == NULL)
3760 *wt_path = path;
3761 else
3762 free(path);
3763 return err;
3766 struct schedule_addition_args {
3767 struct got_worktree *worktree;
3768 struct got_fileindex *fileindex;
3769 got_worktree_checkout_cb progress_cb;
3770 void *progress_arg;
3771 struct got_repository *repo;
3774 static const struct got_error *
3775 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3776 const char *relpath, struct got_object_id *blob_id,
3777 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3778 int dirfd, const char *de_name)
3780 struct schedule_addition_args *a = arg;
3781 const struct got_error *err = NULL;
3782 struct got_fileindex_entry *ie;
3783 struct stat sb;
3784 char *ondisk_path;
3786 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3787 relpath) == -1)
3788 return got_error_from_errno("asprintf");
3790 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3791 if (ie) {
3792 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3793 de_name, a->repo);
3794 if (err)
3795 goto done;
3796 /* Re-adding an existing entry is a no-op. */
3797 if (status == GOT_STATUS_ADD)
3798 goto done;
3799 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3800 if (err)
3801 goto done;
3804 if (status != GOT_STATUS_UNVERSIONED) {
3805 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3806 goto done;
3809 err = got_fileindex_entry_alloc(&ie, relpath);
3810 if (err)
3811 goto done;
3812 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3813 relpath, NULL, NULL, 1);
3814 if (err) {
3815 got_fileindex_entry_free(ie);
3816 goto done;
3818 err = got_fileindex_entry_add(a->fileindex, ie);
3819 if (err) {
3820 got_fileindex_entry_free(ie);
3821 goto done;
3823 done:
3824 free(ondisk_path);
3825 if (err)
3826 return err;
3827 if (status == GOT_STATUS_ADD)
3828 return NULL;
3829 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3832 const struct got_error *
3833 got_worktree_schedule_add(struct got_worktree *worktree,
3834 struct got_pathlist_head *paths,
3835 got_worktree_checkout_cb progress_cb, void *progress_arg,
3836 struct got_repository *repo, int no_ignores)
3838 struct got_fileindex *fileindex = NULL;
3839 char *fileindex_path = NULL;
3840 const struct got_error *err = NULL, *sync_err, *unlockerr;
3841 struct got_pathlist_entry *pe;
3842 struct schedule_addition_args saa;
3844 err = lock_worktree(worktree, LOCK_EX);
3845 if (err)
3846 return err;
3848 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3849 if (err)
3850 goto done;
3852 saa.worktree = worktree;
3853 saa.fileindex = fileindex;
3854 saa.progress_cb = progress_cb;
3855 saa.progress_arg = progress_arg;
3856 saa.repo = repo;
3858 TAILQ_FOREACH(pe, paths, entry) {
3859 err = worktree_status(worktree, pe->path, fileindex, repo,
3860 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3861 if (err)
3862 break;
3864 sync_err = sync_fileindex(fileindex, fileindex_path);
3865 if (sync_err && err == NULL)
3866 err = sync_err;
3867 done:
3868 free(fileindex_path);
3869 if (fileindex)
3870 got_fileindex_free(fileindex);
3871 unlockerr = lock_worktree(worktree, LOCK_SH);
3872 if (unlockerr && err == NULL)
3873 err = unlockerr;
3874 return err;
3877 struct schedule_deletion_args {
3878 struct got_worktree *worktree;
3879 struct got_fileindex *fileindex;
3880 got_worktree_delete_cb progress_cb;
3881 void *progress_arg;
3882 struct got_repository *repo;
3883 int delete_local_mods;
3884 int keep_on_disk;
3885 const char *status_codes;
3888 static const struct got_error *
3889 schedule_for_deletion(void *arg, unsigned char status,
3890 unsigned char staged_status, const char *relpath,
3891 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3892 struct got_object_id *commit_id, int dirfd, const char *de_name)
3894 struct schedule_deletion_args *a = arg;
3895 const struct got_error *err = NULL;
3896 struct got_fileindex_entry *ie = NULL;
3897 struct stat sb;
3898 char *ondisk_path;
3900 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3901 if (ie == NULL)
3902 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3904 staged_status = get_staged_status(ie);
3905 if (staged_status != GOT_STATUS_NO_CHANGE) {
3906 if (staged_status == GOT_STATUS_DELETE)
3907 return NULL;
3908 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3911 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3912 relpath) == -1)
3913 return got_error_from_errno("asprintf");
3915 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3916 a->repo);
3917 if (err)
3918 goto done;
3920 if (a->status_codes) {
3921 size_t ncodes = strlen(a->status_codes);
3922 int i;
3923 for (i = 0; i < ncodes ; i++) {
3924 if (status == a->status_codes[i])
3925 break;
3927 if (i == ncodes) {
3928 /* Do not delete files in non-matching status. */
3929 free(ondisk_path);
3930 return NULL;
3932 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3933 a->status_codes[i] != GOT_STATUS_MISSING) {
3934 static char msg[64];
3935 snprintf(msg, sizeof(msg),
3936 "invalid status code '%c'", a->status_codes[i]);
3937 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3938 goto done;
3942 if (status != GOT_STATUS_NO_CHANGE) {
3943 if (status == GOT_STATUS_DELETE)
3944 goto done;
3945 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3946 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3947 goto done;
3949 if (status != GOT_STATUS_MODIFY &&
3950 status != GOT_STATUS_MISSING) {
3951 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3952 goto done;
3956 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3957 size_t root_len;
3959 if (dirfd != -1) {
3960 if (unlinkat(dirfd, de_name, 0) != 0) {
3961 err = got_error_from_errno2("unlinkat",
3962 ondisk_path);
3963 goto done;
3965 } else if (unlink(ondisk_path) != 0) {
3966 err = got_error_from_errno2("unlink", ondisk_path);
3967 goto done;
3970 root_len = strlen(a->worktree->root_path);
3971 do {
3972 char *parent;
3973 err = got_path_dirname(&parent, ondisk_path);
3974 if (err)
3975 goto done;
3976 free(ondisk_path);
3977 ondisk_path = parent;
3978 if (rmdir(ondisk_path) == -1) {
3979 if (errno != ENOTEMPTY)
3980 err = got_error_from_errno2("rmdir",
3981 ondisk_path);
3982 break;
3984 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3985 strlen(ondisk_path), root_len) != 0);
3988 got_fileindex_entry_mark_deleted_from_disk(ie);
3989 done:
3990 free(ondisk_path);
3991 if (err)
3992 return err;
3993 if (status == GOT_STATUS_DELETE)
3994 return NULL;
3995 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3996 staged_status, relpath);
3999 const struct got_error *
4000 got_worktree_schedule_delete(struct got_worktree *worktree,
4001 struct got_pathlist_head *paths, int delete_local_mods,
4002 const char *status_codes,
4003 got_worktree_delete_cb progress_cb, void *progress_arg,
4004 struct got_repository *repo, int keep_on_disk)
4006 struct got_fileindex *fileindex = NULL;
4007 char *fileindex_path = NULL;
4008 const struct got_error *err = NULL, *sync_err, *unlockerr;
4009 struct got_pathlist_entry *pe;
4010 struct schedule_deletion_args sda;
4012 err = lock_worktree(worktree, LOCK_EX);
4013 if (err)
4014 return err;
4016 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4017 if (err)
4018 goto done;
4020 sda.worktree = worktree;
4021 sda.fileindex = fileindex;
4022 sda.progress_cb = progress_cb;
4023 sda.progress_arg = progress_arg;
4024 sda.repo = repo;
4025 sda.delete_local_mods = delete_local_mods;
4026 sda.keep_on_disk = keep_on_disk;
4027 sda.status_codes = status_codes;
4029 TAILQ_FOREACH(pe, paths, entry) {
4030 err = worktree_status(worktree, pe->path, fileindex, repo,
4031 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4032 if (err)
4033 break;
4035 sync_err = sync_fileindex(fileindex, fileindex_path);
4036 if (sync_err && err == NULL)
4037 err = sync_err;
4038 done:
4039 free(fileindex_path);
4040 if (fileindex)
4041 got_fileindex_free(fileindex);
4042 unlockerr = lock_worktree(worktree, LOCK_SH);
4043 if (unlockerr && err == NULL)
4044 err = unlockerr;
4045 return err;
4048 static const struct got_error *
4049 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4051 const struct got_error *err = NULL;
4052 char *line = NULL;
4053 size_t linesize = 0, n;
4054 ssize_t linelen;
4056 linelen = getline(&line, &linesize, infile);
4057 if (linelen == -1) {
4058 if (ferror(infile)) {
4059 err = got_error_from_errno("getline");
4060 goto done;
4062 return NULL;
4064 if (outfile) {
4065 n = fwrite(line, 1, linelen, outfile);
4066 if (n != linelen) {
4067 err = got_ferror(outfile, GOT_ERR_IO);
4068 goto done;
4071 if (rejectfile) {
4072 n = fwrite(line, 1, linelen, rejectfile);
4073 if (n != linelen)
4074 err = got_ferror(outfile, GOT_ERR_IO);
4076 done:
4077 free(line);
4078 return err;
4081 static const struct got_error *
4082 skip_one_line(FILE *f)
4084 char *line = NULL;
4085 size_t linesize = 0;
4086 ssize_t linelen;
4088 linelen = getline(&line, &linesize, f);
4089 if (linelen == -1) {
4090 if (ferror(f))
4091 return got_error_from_errno("getline");
4092 return NULL;
4094 free(line);
4095 return NULL;
4098 static const struct got_error *
4099 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4100 int start_old, int end_old, int start_new, int end_new,
4101 FILE *outfile, FILE *rejectfile)
4103 const struct got_error *err;
4105 /* Copy old file's lines leading up to patch. */
4106 while (!feof(f1) && *line_cur1 < start_old) {
4107 err = copy_one_line(f1, outfile, NULL);
4108 if (err)
4109 return err;
4110 (*line_cur1)++;
4112 /* Skip new file's lines leading up to patch. */
4113 while (!feof(f2) && *line_cur2 < start_new) {
4114 if (rejectfile)
4115 err = copy_one_line(f2, NULL, rejectfile);
4116 else
4117 err = skip_one_line(f2);
4118 if (err)
4119 return err;
4120 (*line_cur2)++;
4122 /* Copy patched lines. */
4123 while (!feof(f2) && *line_cur2 <= end_new) {
4124 err = copy_one_line(f2, outfile, NULL);
4125 if (err)
4126 return err;
4127 (*line_cur2)++;
4129 /* Skip over old file's replaced lines. */
4130 while (!feof(f1) && *line_cur1 <= end_old) {
4131 if (rejectfile)
4132 err = copy_one_line(f1, NULL, rejectfile);
4133 else
4134 err = skip_one_line(f1);
4135 if (err)
4136 return err;
4137 (*line_cur1)++;
4140 return NULL;
4143 static const struct got_error *
4144 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4145 FILE *outfile, FILE *rejectfile)
4147 const struct got_error *err;
4149 if (outfile) {
4150 /* Copy old file's lines until EOF. */
4151 while (!feof(f1)) {
4152 err = copy_one_line(f1, outfile, NULL);
4153 if (err)
4154 return err;
4155 (*line_cur1)++;
4158 if (rejectfile) {
4159 /* Copy new file's lines until EOF. */
4160 while (!feof(f2)) {
4161 err = copy_one_line(f2, NULL, rejectfile);
4162 if (err)
4163 return err;
4164 (*line_cur2)++;
4168 return NULL;
4171 static const struct got_error *
4172 apply_or_reject_change(int *choice, int *nchunks_used,
4173 struct diff_result *diff_result, int n,
4174 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4175 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4176 got_worktree_patch_cb patch_cb, void *patch_arg)
4178 const struct got_error *err = NULL;
4179 struct diff_chunk_context cc = {};
4180 int start_old, end_old, start_new, end_new;
4181 FILE *hunkfile;
4182 struct diff_output_unidiff_state *diff_state;
4183 struct diff_input_info diff_info;
4184 int rc;
4186 *choice = GOT_PATCH_CHOICE_NONE;
4188 /* Get changed line numbers without context lines for copy_change(). */
4189 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4190 start_old = cc.left.start;
4191 end_old = cc.left.end;
4192 start_new = cc.right.start;
4193 end_new = cc.right.end;
4195 /* Get the same change with context lines for display. */
4196 memset(&cc, 0, sizeof(cc));
4197 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4199 memset(&diff_info, 0, sizeof(diff_info));
4200 diff_info.left_path = relpath;
4201 diff_info.right_path = relpath;
4203 diff_state = diff_output_unidiff_state_alloc();
4204 if (diff_state == NULL)
4205 return got_error_set_errno(ENOMEM,
4206 "diff_output_unidiff_state_alloc");
4208 hunkfile = got_opentemp();
4209 if (hunkfile == NULL) {
4210 err = got_error_from_errno("got_opentemp");
4211 goto done;
4214 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4215 diff_result, &cc);
4216 if (rc != DIFF_RC_OK) {
4217 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4218 goto done;
4221 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4222 err = got_ferror(hunkfile, GOT_ERR_IO);
4223 goto done;
4226 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4227 hunkfile, changeno, nchanges);
4228 if (err)
4229 goto done;
4231 switch (*choice) {
4232 case GOT_PATCH_CHOICE_YES:
4233 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4234 end_old, start_new, end_new, outfile, rejectfile);
4235 break;
4236 case GOT_PATCH_CHOICE_NO:
4237 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4238 end_old, start_new, end_new, rejectfile, outfile);
4239 break;
4240 case GOT_PATCH_CHOICE_QUIT:
4241 break;
4242 default:
4243 err = got_error(GOT_ERR_PATCH_CHOICE);
4244 break;
4246 done:
4247 diff_output_unidiff_state_free(diff_state);
4248 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4249 err = got_error_from_errno("fclose");
4250 return err;
4253 struct revert_file_args {
4254 struct got_worktree *worktree;
4255 struct got_fileindex *fileindex;
4256 got_worktree_checkout_cb progress_cb;
4257 void *progress_arg;
4258 got_worktree_patch_cb patch_cb;
4259 void *patch_arg;
4260 struct got_repository *repo;
4263 static const struct got_error *
4264 create_patched_content(char **path_outfile, int reverse_patch,
4265 struct got_object_id *blob_id, const char *path2,
4266 int dirfd2, const char *de_name2,
4267 const char *relpath, struct got_repository *repo,
4268 got_worktree_patch_cb patch_cb, void *patch_arg)
4270 const struct got_error *err, *free_err;
4271 struct got_blob_object *blob = NULL;
4272 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4273 int fd2 = -1;
4274 char link_target[PATH_MAX];
4275 ssize_t link_len = 0;
4276 char *path1 = NULL, *id_str = NULL;
4277 struct stat sb2;
4278 struct got_diffreg_result *diffreg_result = NULL;
4279 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4280 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4282 *path_outfile = NULL;
4284 err = got_object_id_str(&id_str, blob_id);
4285 if (err)
4286 return err;
4288 if (dirfd2 != -1) {
4289 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4290 if (fd2 == -1) {
4291 if (errno != ELOOP) {
4292 err = got_error_from_errno2("openat", path2);
4293 goto done;
4295 link_len = readlinkat(dirfd2, de_name2,
4296 link_target, sizeof(link_target));
4297 if (link_len == -1)
4298 return got_error_from_errno2("readlinkat", path2);
4299 sb2.st_mode = S_IFLNK;
4300 sb2.st_size = link_len;
4302 } else {
4303 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4304 if (fd2 == -1) {
4305 if (errno != ELOOP) {
4306 err = got_error_from_errno2("open", path2);
4307 goto done;
4309 link_len = readlink(path2, link_target,
4310 sizeof(link_target));
4311 if (link_len == -1)
4312 return got_error_from_errno2("readlink", path2);
4313 sb2.st_mode = S_IFLNK;
4314 sb2.st_size = link_len;
4317 if (fd2 != -1) {
4318 if (fstat(fd2, &sb2) == -1) {
4319 err = got_error_from_errno2("fstat", path2);
4320 goto done;
4323 f2 = fdopen(fd2, "r");
4324 if (f2 == NULL) {
4325 err = got_error_from_errno2("fdopen", path2);
4326 goto done;
4328 fd2 = -1;
4329 } else {
4330 size_t n;
4331 f2 = got_opentemp();
4332 if (f2 == NULL) {
4333 err = got_error_from_errno2("got_opentemp", path2);
4334 goto done;
4336 n = fwrite(link_target, 1, link_len, f2);
4337 if (n != link_len) {
4338 err = got_ferror(f2, GOT_ERR_IO);
4339 goto done;
4341 if (fflush(f2) == EOF) {
4342 err = got_error_from_errno("fflush");
4343 goto done;
4345 rewind(f2);
4348 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4349 if (err)
4350 goto done;
4352 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4353 if (err)
4354 goto done;
4356 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4357 if (err)
4358 goto done;
4360 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4361 NULL);
4362 if (err)
4363 goto done;
4365 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4366 if (err)
4367 goto done;
4369 if (fseek(f1, 0L, SEEK_SET) == -1)
4370 return got_ferror(f1, GOT_ERR_IO);
4371 if (fseek(f2, 0L, SEEK_SET) == -1)
4372 return got_ferror(f2, GOT_ERR_IO);
4374 /* Count the number of actual changes in the diff result. */
4375 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4376 struct diff_chunk_context cc = {};
4377 diff_chunk_context_load_change(&cc, &nchunks_used,
4378 diffreg_result->result, n, 0);
4379 nchanges++;
4381 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4382 int choice;
4383 err = apply_or_reject_change(&choice, &nchunks_used,
4384 diffreg_result->result, n, relpath, f1, f2,
4385 &line_cur1, &line_cur2,
4386 reverse_patch ? NULL : outfile,
4387 reverse_patch ? outfile : NULL,
4388 ++i, nchanges, patch_cb, patch_arg);
4389 if (err)
4390 goto done;
4391 if (choice == GOT_PATCH_CHOICE_YES)
4392 have_content = 1;
4393 else if (choice == GOT_PATCH_CHOICE_QUIT)
4394 break;
4396 if (have_content) {
4397 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4398 reverse_patch ? NULL : outfile,
4399 reverse_patch ? outfile : NULL);
4400 if (err)
4401 goto done;
4403 if (!S_ISLNK(sb2.st_mode)) {
4404 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4405 err = got_error_from_errno2("fchmod", path2);
4406 goto done;
4410 done:
4411 free(id_str);
4412 if (blob)
4413 got_object_blob_close(blob);
4414 free_err = got_diffreg_result_free(diffreg_result);
4415 if (err == NULL)
4416 err = free_err;
4417 if (f1 && fclose(f1) == EOF && err == NULL)
4418 err = got_error_from_errno2("fclose", path1);
4419 if (f2 && fclose(f2) == EOF && err == NULL)
4420 err = got_error_from_errno2("fclose", path2);
4421 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4422 err = got_error_from_errno2("close", path2);
4423 if (outfile && fclose(outfile) == EOF && err == NULL)
4424 err = got_error_from_errno2("fclose", *path_outfile);
4425 if (path1 && unlink(path1) == -1 && err == NULL)
4426 err = got_error_from_errno2("unlink", path1);
4427 if (err || !have_content) {
4428 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4429 err = got_error_from_errno2("unlink", *path_outfile);
4430 free(*path_outfile);
4431 *path_outfile = NULL;
4433 free(path1);
4434 return err;
4437 static const struct got_error *
4438 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4439 const char *relpath, struct got_object_id *blob_id,
4440 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4441 int dirfd, const char *de_name)
4443 struct revert_file_args *a = arg;
4444 const struct got_error *err = NULL;
4445 char *parent_path = NULL;
4446 struct got_fileindex_entry *ie;
4447 struct got_tree_object *tree = NULL;
4448 struct got_object_id *tree_id = NULL;
4449 const struct got_tree_entry *te = NULL;
4450 char *tree_path = NULL, *te_name;
4451 char *ondisk_path = NULL, *path_content = NULL;
4452 struct got_blob_object *blob = NULL;
4454 /* Reverting a staged deletion is a no-op. */
4455 if (status == GOT_STATUS_DELETE &&
4456 staged_status != GOT_STATUS_NO_CHANGE)
4457 return NULL;
4459 if (status == GOT_STATUS_UNVERSIONED)
4460 return (*a->progress_cb)(a->progress_arg,
4461 GOT_STATUS_UNVERSIONED, relpath);
4463 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4464 if (ie == NULL)
4465 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4467 /* Construct in-repository path of tree which contains this blob. */
4468 err = got_path_dirname(&parent_path, ie->path);
4469 if (err) {
4470 if (err->code != GOT_ERR_BAD_PATH)
4471 goto done;
4472 parent_path = strdup("/");
4473 if (parent_path == NULL) {
4474 err = got_error_from_errno("strdup");
4475 goto done;
4478 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4479 tree_path = strdup(parent_path);
4480 if (tree_path == NULL) {
4481 err = got_error_from_errno("strdup");
4482 goto done;
4484 } else {
4485 if (got_path_is_root_dir(parent_path)) {
4486 tree_path = strdup(a->worktree->path_prefix);
4487 if (tree_path == NULL) {
4488 err = got_error_from_errno("strdup");
4489 goto done;
4491 } else {
4492 if (asprintf(&tree_path, "%s/%s",
4493 a->worktree->path_prefix, parent_path) == -1) {
4494 err = got_error_from_errno("asprintf");
4495 goto done;
4500 err = got_object_id_by_path(&tree_id, a->repo,
4501 a->worktree->base_commit_id, tree_path);
4502 if (err) {
4503 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4504 (status == GOT_STATUS_ADD ||
4505 staged_status == GOT_STATUS_ADD)))
4506 goto done;
4507 } else {
4508 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4509 if (err)
4510 goto done;
4512 err = got_path_basename(&te_name, ie->path);
4513 if (err)
4514 goto done;
4516 te = got_object_tree_find_entry(tree, te_name);
4517 free(te_name);
4518 if (te == NULL && status != GOT_STATUS_ADD &&
4519 staged_status != GOT_STATUS_ADD) {
4520 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4521 goto done;
4525 switch (status) {
4526 case GOT_STATUS_ADD:
4527 if (a->patch_cb) {
4528 int choice = GOT_PATCH_CHOICE_NONE;
4529 err = (*a->patch_cb)(&choice, a->patch_arg,
4530 status, ie->path, NULL, 1, 1);
4531 if (err)
4532 goto done;
4533 if (choice != GOT_PATCH_CHOICE_YES)
4534 break;
4536 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4537 ie->path);
4538 if (err)
4539 goto done;
4540 got_fileindex_entry_remove(a->fileindex, ie);
4541 break;
4542 case GOT_STATUS_DELETE:
4543 if (a->patch_cb) {
4544 int choice = GOT_PATCH_CHOICE_NONE;
4545 err = (*a->patch_cb)(&choice, a->patch_arg,
4546 status, ie->path, NULL, 1, 1);
4547 if (err)
4548 goto done;
4549 if (choice != GOT_PATCH_CHOICE_YES)
4550 break;
4552 /* fall through */
4553 case GOT_STATUS_MODIFY:
4554 case GOT_STATUS_MODE_CHANGE:
4555 case GOT_STATUS_CONFLICT:
4556 case GOT_STATUS_MISSING: {
4557 struct got_object_id id;
4558 if (staged_status == GOT_STATUS_ADD ||
4559 staged_status == GOT_STATUS_MODIFY) {
4560 memcpy(id.sha1, ie->staged_blob_sha1,
4561 SHA1_DIGEST_LENGTH);
4562 } else
4563 memcpy(id.sha1, ie->blob_sha1,
4564 SHA1_DIGEST_LENGTH);
4565 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4566 if (err)
4567 goto done;
4569 if (asprintf(&ondisk_path, "%s/%s",
4570 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4571 err = got_error_from_errno("asprintf");
4572 goto done;
4575 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4576 status == GOT_STATUS_CONFLICT)) {
4577 int is_bad_symlink = 0;
4578 err = create_patched_content(&path_content, 1, &id,
4579 ondisk_path, dirfd, de_name, ie->path, a->repo,
4580 a->patch_cb, a->patch_arg);
4581 if (err || path_content == NULL)
4582 break;
4583 if (te && S_ISLNK(te->mode)) {
4584 if (unlink(path_content) == -1) {
4585 err = got_error_from_errno2("unlink",
4586 path_content);
4587 break;
4589 err = install_symlink(&is_bad_symlink,
4590 a->worktree, ondisk_path, ie->path,
4591 blob, 0, 1, 0, a->repo,
4592 a->progress_cb, a->progress_arg);
4593 } else {
4594 if (rename(path_content, ondisk_path) == -1) {
4595 err = got_error_from_errno3("rename",
4596 path_content, ondisk_path);
4597 goto done;
4600 } else {
4601 int is_bad_symlink = 0;
4602 if (te && S_ISLNK(te->mode)) {
4603 err = install_symlink(&is_bad_symlink,
4604 a->worktree, ondisk_path, ie->path,
4605 blob, 0, 1, 0, a->repo,
4606 a->progress_cb, a->progress_arg);
4607 } else {
4608 err = install_blob(a->worktree, ondisk_path,
4609 ie->path,
4610 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4611 got_fileindex_perms_to_st(ie), blob,
4612 0, 1, 0, 0, a->repo,
4613 a->progress_cb, a->progress_arg);
4615 if (err)
4616 goto done;
4617 if (status == GOT_STATUS_DELETE ||
4618 status == GOT_STATUS_MODE_CHANGE) {
4619 err = got_fileindex_entry_update(ie,
4620 a->worktree->root_fd, relpath,
4621 blob->id.sha1,
4622 a->worktree->base_commit_id->sha1, 1);
4623 if (err)
4624 goto done;
4626 if (is_bad_symlink) {
4627 got_fileindex_entry_filetype_set(ie,
4628 GOT_FILEIDX_MODE_BAD_SYMLINK);
4631 break;
4633 default:
4634 break;
4636 done:
4637 free(ondisk_path);
4638 free(path_content);
4639 free(parent_path);
4640 free(tree_path);
4641 if (blob)
4642 got_object_blob_close(blob);
4643 if (tree)
4644 got_object_tree_close(tree);
4645 free(tree_id);
4646 return err;
4649 const struct got_error *
4650 got_worktree_revert(struct got_worktree *worktree,
4651 struct got_pathlist_head *paths,
4652 got_worktree_checkout_cb progress_cb, void *progress_arg,
4653 got_worktree_patch_cb patch_cb, void *patch_arg,
4654 struct got_repository *repo)
4656 struct got_fileindex *fileindex = NULL;
4657 char *fileindex_path = NULL;
4658 const struct got_error *err = NULL, *unlockerr = NULL;
4659 const struct got_error *sync_err = NULL;
4660 struct got_pathlist_entry *pe;
4661 struct revert_file_args rfa;
4663 err = lock_worktree(worktree, LOCK_EX);
4664 if (err)
4665 return err;
4667 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4668 if (err)
4669 goto done;
4671 rfa.worktree = worktree;
4672 rfa.fileindex = fileindex;
4673 rfa.progress_cb = progress_cb;
4674 rfa.progress_arg = progress_arg;
4675 rfa.patch_cb = patch_cb;
4676 rfa.patch_arg = patch_arg;
4677 rfa.repo = repo;
4678 TAILQ_FOREACH(pe, paths, entry) {
4679 err = worktree_status(worktree, pe->path, fileindex, repo,
4680 revert_file, &rfa, NULL, NULL, 0, 0);
4681 if (err)
4682 break;
4684 sync_err = sync_fileindex(fileindex, fileindex_path);
4685 if (sync_err && err == NULL)
4686 err = sync_err;
4687 done:
4688 free(fileindex_path);
4689 if (fileindex)
4690 got_fileindex_free(fileindex);
4691 unlockerr = lock_worktree(worktree, LOCK_SH);
4692 if (unlockerr && err == NULL)
4693 err = unlockerr;
4694 return err;
4697 static void
4698 free_commitable(struct got_commitable *ct)
4700 free(ct->path);
4701 free(ct->in_repo_path);
4702 free(ct->ondisk_path);
4703 free(ct->blob_id);
4704 free(ct->base_blob_id);
4705 free(ct->staged_blob_id);
4706 free(ct->base_commit_id);
4707 free(ct);
4710 struct collect_commitables_arg {
4711 struct got_pathlist_head *commitable_paths;
4712 struct got_repository *repo;
4713 struct got_worktree *worktree;
4714 struct got_fileindex *fileindex;
4715 int have_staged_files;
4716 int allow_bad_symlinks;
4719 static const struct got_error *
4720 collect_commitables(void *arg, unsigned char status,
4721 unsigned char staged_status, const char *relpath,
4722 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4723 struct got_object_id *commit_id, int dirfd, const char *de_name)
4725 struct collect_commitables_arg *a = arg;
4726 const struct got_error *err = NULL;
4727 struct got_commitable *ct = NULL;
4728 struct got_pathlist_entry *new = NULL;
4729 char *parent_path = NULL, *path = NULL;
4730 struct stat sb;
4732 if (a->have_staged_files) {
4733 if (staged_status != GOT_STATUS_MODIFY &&
4734 staged_status != GOT_STATUS_ADD &&
4735 staged_status != GOT_STATUS_DELETE)
4736 return NULL;
4737 } else {
4738 if (status == GOT_STATUS_CONFLICT)
4739 return got_error(GOT_ERR_COMMIT_CONFLICT);
4741 if (status != GOT_STATUS_MODIFY &&
4742 status != GOT_STATUS_MODE_CHANGE &&
4743 status != GOT_STATUS_ADD &&
4744 status != GOT_STATUS_DELETE)
4745 return NULL;
4748 if (asprintf(&path, "/%s", relpath) == -1) {
4749 err = got_error_from_errno("asprintf");
4750 goto done;
4752 if (strcmp(path, "/") == 0) {
4753 parent_path = strdup("");
4754 if (parent_path == NULL)
4755 return got_error_from_errno("strdup");
4756 } else {
4757 err = got_path_dirname(&parent_path, path);
4758 if (err)
4759 return err;
4762 ct = calloc(1, sizeof(*ct));
4763 if (ct == NULL) {
4764 err = got_error_from_errno("calloc");
4765 goto done;
4768 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4769 relpath) == -1) {
4770 err = got_error_from_errno("asprintf");
4771 goto done;
4774 if (staged_status == GOT_STATUS_ADD ||
4775 staged_status == GOT_STATUS_MODIFY) {
4776 struct got_fileindex_entry *ie;
4777 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4778 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4779 case GOT_FILEIDX_MODE_REGULAR_FILE:
4780 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4781 ct->mode = S_IFREG;
4782 break;
4783 case GOT_FILEIDX_MODE_SYMLINK:
4784 ct->mode = S_IFLNK;
4785 break;
4786 default:
4787 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4788 goto done;
4790 ct->mode |= got_fileindex_entry_perms_get(ie);
4791 } else if (status != GOT_STATUS_DELETE &&
4792 staged_status != GOT_STATUS_DELETE) {
4793 if (dirfd != -1) {
4794 if (fstatat(dirfd, de_name, &sb,
4795 AT_SYMLINK_NOFOLLOW) == -1) {
4796 err = got_error_from_errno2("fstatat",
4797 ct->ondisk_path);
4798 goto done;
4800 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4801 err = got_error_from_errno2("lstat", ct->ondisk_path);
4802 goto done;
4804 ct->mode = sb.st_mode;
4807 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4808 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4809 relpath) == -1) {
4810 err = got_error_from_errno("asprintf");
4811 goto done;
4814 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4815 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4816 int is_bad_symlink;
4817 char target_path[PATH_MAX];
4818 ssize_t target_len;
4819 target_len = readlink(ct->ondisk_path, target_path,
4820 sizeof(target_path));
4821 if (target_len == -1) {
4822 err = got_error_from_errno2("readlink",
4823 ct->ondisk_path);
4824 goto done;
4826 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4827 target_len, ct->ondisk_path, a->worktree->root_path);
4828 if (err)
4829 goto done;
4830 if (is_bad_symlink) {
4831 err = got_error_path(ct->ondisk_path,
4832 GOT_ERR_BAD_SYMLINK);
4833 goto done;
4838 ct->status = status;
4839 ct->staged_status = staged_status;
4840 ct->blob_id = NULL; /* will be filled in when blob gets created */
4841 if (ct->status != GOT_STATUS_ADD &&
4842 ct->staged_status != GOT_STATUS_ADD) {
4843 ct->base_blob_id = got_object_id_dup(blob_id);
4844 if (ct->base_blob_id == NULL) {
4845 err = got_error_from_errno("got_object_id_dup");
4846 goto done;
4848 ct->base_commit_id = got_object_id_dup(commit_id);
4849 if (ct->base_commit_id == NULL) {
4850 err = got_error_from_errno("got_object_id_dup");
4851 goto done;
4854 if (ct->staged_status == GOT_STATUS_ADD ||
4855 ct->staged_status == GOT_STATUS_MODIFY) {
4856 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4857 if (ct->staged_blob_id == NULL) {
4858 err = got_error_from_errno("got_object_id_dup");
4859 goto done;
4862 ct->path = strdup(path);
4863 if (ct->path == NULL) {
4864 err = got_error_from_errno("strdup");
4865 goto done;
4867 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4868 done:
4869 if (ct && (err || new == NULL))
4870 free_commitable(ct);
4871 free(parent_path);
4872 free(path);
4873 return err;
4876 static const struct got_error *write_tree(struct got_object_id **, int *,
4877 struct got_tree_object *, const char *, struct got_pathlist_head *,
4878 got_worktree_status_cb status_cb, void *status_arg,
4879 struct got_repository *);
4881 static const struct got_error *
4882 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4883 struct got_tree_entry *te, const char *parent_path,
4884 struct got_pathlist_head *commitable_paths,
4885 got_worktree_status_cb status_cb, void *status_arg,
4886 struct got_repository *repo)
4888 const struct got_error *err = NULL;
4889 struct got_tree_object *subtree;
4890 char *subpath;
4892 if (asprintf(&subpath, "%s%s%s", parent_path,
4893 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4894 return got_error_from_errno("asprintf");
4896 err = got_object_open_as_tree(&subtree, repo, &te->id);
4897 if (err)
4898 return err;
4900 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4901 commitable_paths, status_cb, status_arg, repo);
4902 got_object_tree_close(subtree);
4903 free(subpath);
4904 return err;
4907 static const struct got_error *
4908 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4910 const struct got_error *err = NULL;
4911 char *ct_parent_path = NULL;
4913 *match = 0;
4915 if (strchr(ct->in_repo_path, '/') == NULL) {
4916 *match = got_path_is_root_dir(path);
4917 return NULL;
4920 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4921 if (err)
4922 return err;
4923 *match = (strcmp(path, ct_parent_path) == 0);
4924 free(ct_parent_path);
4925 return err;
4928 static mode_t
4929 get_ct_file_mode(struct got_commitable *ct)
4931 if (S_ISLNK(ct->mode))
4932 return S_IFLNK;
4934 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4937 static const struct got_error *
4938 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4939 struct got_tree_entry *te, struct got_commitable *ct)
4941 const struct got_error *err = NULL;
4943 *new_te = NULL;
4945 err = got_object_tree_entry_dup(new_te, te);
4946 if (err)
4947 goto done;
4949 (*new_te)->mode = get_ct_file_mode(ct);
4951 if (ct->staged_status == GOT_STATUS_MODIFY)
4952 memcpy(&(*new_te)->id, ct->staged_blob_id,
4953 sizeof((*new_te)->id));
4954 else
4955 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4956 done:
4957 if (err && *new_te) {
4958 free(*new_te);
4959 *new_te = NULL;
4961 return err;
4964 static const struct got_error *
4965 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4966 struct got_commitable *ct)
4968 const struct got_error *err = NULL;
4969 char *ct_name = NULL;
4971 *new_te = NULL;
4973 *new_te = calloc(1, sizeof(**new_te));
4974 if (*new_te == NULL)
4975 return got_error_from_errno("calloc");
4977 err = got_path_basename(&ct_name, ct->path);
4978 if (err)
4979 goto done;
4980 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4981 sizeof((*new_te)->name)) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4983 goto done;
4986 (*new_te)->mode = get_ct_file_mode(ct);
4988 if (ct->staged_status == GOT_STATUS_ADD)
4989 memcpy(&(*new_te)->id, ct->staged_blob_id,
4990 sizeof((*new_te)->id));
4991 else
4992 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4993 done:
4994 free(ct_name);
4995 if (err && *new_te) {
4996 free(*new_te);
4997 *new_te = NULL;
4999 return err;
5002 static const struct got_error *
5003 insert_tree_entry(struct got_tree_entry *new_te,
5004 struct got_pathlist_head *paths)
5006 const struct got_error *err = NULL;
5007 struct got_pathlist_entry *new_pe;
5009 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5010 if (err)
5011 return err;
5012 if (new_pe == NULL)
5013 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5014 return NULL;
5017 static const struct got_error *
5018 report_ct_status(struct got_commitable *ct,
5019 got_worktree_status_cb status_cb, void *status_arg)
5021 const char *ct_path = ct->path;
5022 unsigned char status;
5024 while (ct_path[0] == '/')
5025 ct_path++;
5027 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5028 status = ct->staged_status;
5029 else
5030 status = ct->status;
5032 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5033 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5036 static const struct got_error *
5037 match_modified_subtree(int *modified, struct got_tree_entry *te,
5038 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5040 const struct got_error *err = NULL;
5041 struct got_pathlist_entry *pe;
5042 char *te_path;
5044 *modified = 0;
5046 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5047 got_path_is_root_dir(base_tree_path) ? "" : "/",
5048 te->name) == -1)
5049 return got_error_from_errno("asprintf");
5051 TAILQ_FOREACH(pe, commitable_paths, entry) {
5052 struct got_commitable *ct = pe->data;
5053 *modified = got_path_is_child(ct->in_repo_path, te_path,
5054 strlen(te_path));
5055 if (*modified)
5056 break;
5059 free(te_path);
5060 return err;
5063 static const struct got_error *
5064 match_deleted_or_modified_ct(struct got_commitable **ctp,
5065 struct got_tree_entry *te, const char *base_tree_path,
5066 struct got_pathlist_head *commitable_paths)
5068 const struct got_error *err = NULL;
5069 struct got_pathlist_entry *pe;
5071 *ctp = NULL;
5073 TAILQ_FOREACH(pe, commitable_paths, entry) {
5074 struct got_commitable *ct = pe->data;
5075 char *ct_name = NULL;
5076 int path_matches;
5078 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5079 if (ct->status != GOT_STATUS_MODIFY &&
5080 ct->status != GOT_STATUS_MODE_CHANGE &&
5081 ct->status != GOT_STATUS_DELETE)
5082 continue;
5083 } else {
5084 if (ct->staged_status != GOT_STATUS_MODIFY &&
5085 ct->staged_status != GOT_STATUS_DELETE)
5086 continue;
5089 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5090 continue;
5092 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5093 if (err)
5094 return err;
5095 if (!path_matches)
5096 continue;
5098 err = got_path_basename(&ct_name, pe->path);
5099 if (err)
5100 return err;
5102 if (strcmp(te->name, ct_name) != 0) {
5103 free(ct_name);
5104 continue;
5106 free(ct_name);
5108 *ctp = ct;
5109 break;
5112 return err;
5115 static const struct got_error *
5116 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5117 const char *child_path, const char *path_base_tree,
5118 struct got_pathlist_head *commitable_paths,
5119 got_worktree_status_cb status_cb, void *status_arg,
5120 struct got_repository *repo)
5122 const struct got_error *err = NULL;
5123 struct got_tree_entry *new_te;
5124 char *subtree_path;
5125 struct got_object_id *id = NULL;
5126 int nentries;
5128 *new_tep = NULL;
5130 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5131 got_path_is_root_dir(path_base_tree) ? "" : "/",
5132 child_path) == -1)
5133 return got_error_from_errno("asprintf");
5135 new_te = calloc(1, sizeof(*new_te));
5136 if (new_te == NULL)
5137 return got_error_from_errno("calloc");
5138 new_te->mode = S_IFDIR;
5140 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5141 sizeof(new_te->name)) {
5142 err = got_error(GOT_ERR_NO_SPACE);
5143 goto done;
5145 err = write_tree(&id, &nentries, NULL, subtree_path,
5146 commitable_paths, status_cb, status_arg, repo);
5147 if (err) {
5148 free(new_te);
5149 goto done;
5151 memcpy(&new_te->id, id, sizeof(new_te->id));
5152 done:
5153 free(id);
5154 free(subtree_path);
5155 if (err == NULL)
5156 *new_tep = new_te;
5157 return err;
5160 static const struct got_error *
5161 write_tree(struct got_object_id **new_tree_id, int *nentries,
5162 struct got_tree_object *base_tree, const char *path_base_tree,
5163 struct got_pathlist_head *commitable_paths,
5164 got_worktree_status_cb status_cb, void *status_arg,
5165 struct got_repository *repo)
5167 const struct got_error *err = NULL;
5168 struct got_pathlist_head paths;
5169 struct got_tree_entry *te, *new_te = NULL;
5170 struct got_pathlist_entry *pe;
5172 TAILQ_INIT(&paths);
5173 *nentries = 0;
5175 /* Insert, and recurse into, newly added entries first. */
5176 TAILQ_FOREACH(pe, commitable_paths, entry) {
5177 struct got_commitable *ct = pe->data;
5178 char *child_path = NULL, *slash;
5180 if ((ct->status != GOT_STATUS_ADD &&
5181 ct->staged_status != GOT_STATUS_ADD) ||
5182 (ct->flags & GOT_COMMITABLE_ADDED))
5183 continue;
5185 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5186 strlen(path_base_tree)))
5187 continue;
5189 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5190 ct->in_repo_path);
5191 if (err)
5192 goto done;
5194 slash = strchr(child_path, '/');
5195 if (slash == NULL) {
5196 err = alloc_added_blob_tree_entry(&new_te, ct);
5197 if (err)
5198 goto done;
5199 err = report_ct_status(ct, status_cb, status_arg);
5200 if (err)
5201 goto done;
5202 ct->flags |= GOT_COMMITABLE_ADDED;
5203 err = insert_tree_entry(new_te, &paths);
5204 if (err)
5205 goto done;
5206 (*nentries)++;
5207 } else {
5208 *slash = '\0'; /* trim trailing path components */
5209 if (base_tree == NULL ||
5210 got_object_tree_find_entry(base_tree, child_path)
5211 == NULL) {
5212 err = make_subtree_for_added_blob(&new_te,
5213 child_path, path_base_tree,
5214 commitable_paths, status_cb, status_arg,
5215 repo);
5216 if (err)
5217 goto done;
5218 err = insert_tree_entry(new_te, &paths);
5219 if (err)
5220 goto done;
5221 (*nentries)++;
5226 if (base_tree) {
5227 int i, nbase_entries;
5228 /* Handle modified and deleted entries. */
5229 nbase_entries = got_object_tree_get_nentries(base_tree);
5230 for (i = 0; i < nbase_entries; i++) {
5231 struct got_commitable *ct = NULL;
5233 te = got_object_tree_get_entry(base_tree, i);
5234 if (got_object_tree_entry_is_submodule(te)) {
5235 /* Entry is a submodule; just copy it. */
5236 err = got_object_tree_entry_dup(&new_te, te);
5237 if (err)
5238 goto done;
5239 err = insert_tree_entry(new_te, &paths);
5240 if (err)
5241 goto done;
5242 (*nentries)++;
5243 continue;
5246 if (S_ISDIR(te->mode)) {
5247 int modified;
5248 err = got_object_tree_entry_dup(&new_te, te);
5249 if (err)
5250 goto done;
5251 err = match_modified_subtree(&modified, te,
5252 path_base_tree, commitable_paths);
5253 if (err)
5254 goto done;
5255 /* Avoid recursion into unmodified subtrees. */
5256 if (modified) {
5257 struct got_object_id *new_id;
5258 int nsubentries;
5259 err = write_subtree(&new_id,
5260 &nsubentries, te,
5261 path_base_tree, commitable_paths,
5262 status_cb, status_arg, repo);
5263 if (err)
5264 goto done;
5265 if (nsubentries == 0) {
5266 /* All entries were deleted. */
5267 free(new_id);
5268 continue;
5270 memcpy(&new_te->id, new_id,
5271 sizeof(new_te->id));
5272 free(new_id);
5274 err = insert_tree_entry(new_te, &paths);
5275 if (err)
5276 goto done;
5277 (*nentries)++;
5278 continue;
5281 err = match_deleted_or_modified_ct(&ct, te,
5282 path_base_tree, commitable_paths);
5283 if (err)
5284 goto done;
5285 if (ct) {
5286 /* NB: Deleted entries get dropped here. */
5287 if (ct->status == GOT_STATUS_MODIFY ||
5288 ct->status == GOT_STATUS_MODE_CHANGE ||
5289 ct->staged_status == GOT_STATUS_MODIFY) {
5290 err = alloc_modified_blob_tree_entry(
5291 &new_te, te, ct);
5292 if (err)
5293 goto done;
5294 err = insert_tree_entry(new_te, &paths);
5295 if (err)
5296 goto done;
5297 (*nentries)++;
5299 err = report_ct_status(ct, status_cb,
5300 status_arg);
5301 if (err)
5302 goto done;
5303 } else {
5304 /* Entry is unchanged; just copy it. */
5305 err = got_object_tree_entry_dup(&new_te, te);
5306 if (err)
5307 goto done;
5308 err = insert_tree_entry(new_te, &paths);
5309 if (err)
5310 goto done;
5311 (*nentries)++;
5316 /* Write new list of entries; deleted entries have been dropped. */
5317 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5318 done:
5319 got_pathlist_free(&paths);
5320 return err;
5323 static const struct got_error *
5324 update_fileindex_after_commit(struct got_worktree *worktree,
5325 struct got_pathlist_head *commitable_paths,
5326 struct got_object_id *new_base_commit_id,
5327 struct got_fileindex *fileindex, int have_staged_files)
5329 const struct got_error *err = NULL;
5330 struct got_pathlist_entry *pe;
5331 char *relpath = NULL;
5333 TAILQ_FOREACH(pe, commitable_paths, entry) {
5334 struct got_fileindex_entry *ie;
5335 struct got_commitable *ct = pe->data;
5337 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5339 err = got_path_skip_common_ancestor(&relpath,
5340 worktree->root_path, ct->ondisk_path);
5341 if (err)
5342 goto done;
5344 if (ie) {
5345 if (ct->status == GOT_STATUS_DELETE ||
5346 ct->staged_status == GOT_STATUS_DELETE) {
5347 got_fileindex_entry_remove(fileindex, ie);
5348 } else if (ct->staged_status == GOT_STATUS_ADD ||
5349 ct->staged_status == GOT_STATUS_MODIFY) {
5350 got_fileindex_entry_stage_set(ie,
5351 GOT_FILEIDX_STAGE_NONE);
5352 got_fileindex_entry_staged_filetype_set(ie, 0);
5354 err = got_fileindex_entry_update(ie,
5355 worktree->root_fd, relpath,
5356 ct->staged_blob_id->sha1,
5357 new_base_commit_id->sha1,
5358 !have_staged_files);
5359 } else
5360 err = got_fileindex_entry_update(ie,
5361 worktree->root_fd, relpath,
5362 ct->blob_id->sha1,
5363 new_base_commit_id->sha1,
5364 !have_staged_files);
5365 } else {
5366 err = got_fileindex_entry_alloc(&ie, pe->path);
5367 if (err)
5368 goto done;
5369 err = got_fileindex_entry_update(ie,
5370 worktree->root_fd, relpath, ct->blob_id->sha1,
5371 new_base_commit_id->sha1, 1);
5372 if (err) {
5373 got_fileindex_entry_free(ie);
5374 goto done;
5376 err = got_fileindex_entry_add(fileindex, ie);
5377 if (err) {
5378 got_fileindex_entry_free(ie);
5379 goto done;
5382 free(relpath);
5383 relpath = NULL;
5385 done:
5386 free(relpath);
5387 return err;
5391 static const struct got_error *
5392 check_out_of_date(const char *in_repo_path, unsigned char status,
5393 unsigned char staged_status, struct got_object_id *base_blob_id,
5394 struct got_object_id *base_commit_id,
5395 struct got_object_id *head_commit_id, struct got_repository *repo,
5396 int ood_errcode)
5398 const struct got_error *err = NULL;
5399 struct got_object_id *id = NULL;
5401 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5402 /* Trivial case: base commit == head commit */
5403 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5404 return NULL;
5406 * Ensure file content which local changes were based
5407 * on matches file content in the branch head.
5409 err = got_object_id_by_path(&id, repo, head_commit_id,
5410 in_repo_path);
5411 if (err) {
5412 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5413 err = got_error(ood_errcode);
5414 goto done;
5415 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5416 err = got_error(ood_errcode);
5417 } else {
5418 /* Require that added files don't exist in the branch head. */
5419 err = got_object_id_by_path(&id, repo, head_commit_id,
5420 in_repo_path);
5421 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5422 goto done;
5423 err = id ? got_error(ood_errcode) : NULL;
5425 done:
5426 free(id);
5427 return err;
5430 const struct got_error *
5431 commit_worktree(struct got_object_id **new_commit_id,
5432 struct got_pathlist_head *commitable_paths,
5433 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5434 const char *author, const char *committer,
5435 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5436 got_worktree_status_cb status_cb, void *status_arg,
5437 struct got_repository *repo)
5439 const struct got_error *err = NULL, *unlockerr = NULL;
5440 struct got_pathlist_entry *pe;
5441 const char *head_ref_name = NULL;
5442 struct got_commit_object *head_commit = NULL;
5443 struct got_reference *head_ref2 = NULL;
5444 struct got_object_id *head_commit_id2 = NULL;
5445 struct got_tree_object *head_tree = NULL;
5446 struct got_object_id *new_tree_id = NULL;
5447 int nentries;
5448 struct got_object_id_queue parent_ids;
5449 struct got_object_qid *pid = NULL;
5450 char *logmsg = NULL;
5452 *new_commit_id = NULL;
5454 SIMPLEQ_INIT(&parent_ids);
5456 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5457 if (err)
5458 goto done;
5460 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5461 if (err)
5462 goto done;
5464 if (commit_msg_cb != NULL) {
5465 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5466 if (err)
5467 goto done;
5470 if (logmsg == NULL || strlen(logmsg) == 0) {
5471 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5472 goto done;
5475 /* Create blobs from added and modified files and record their IDs. */
5476 TAILQ_FOREACH(pe, commitable_paths, entry) {
5477 struct got_commitable *ct = pe->data;
5478 char *ondisk_path;
5480 /* Blobs for staged files already exist. */
5481 if (ct->staged_status == GOT_STATUS_ADD ||
5482 ct->staged_status == GOT_STATUS_MODIFY)
5483 continue;
5485 if (ct->status != GOT_STATUS_ADD &&
5486 ct->status != GOT_STATUS_MODIFY &&
5487 ct->status != GOT_STATUS_MODE_CHANGE)
5488 continue;
5490 if (asprintf(&ondisk_path, "%s/%s",
5491 worktree->root_path, pe->path) == -1) {
5492 err = got_error_from_errno("asprintf");
5493 goto done;
5495 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5496 free(ondisk_path);
5497 if (err)
5498 goto done;
5501 /* Recursively write new tree objects. */
5502 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5503 commitable_paths, status_cb, status_arg, repo);
5504 if (err)
5505 goto done;
5507 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5508 if (err)
5509 goto done;
5510 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5511 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5512 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5513 got_object_qid_free(pid);
5514 if (logmsg != NULL)
5515 free(logmsg);
5516 if (err)
5517 goto done;
5519 /* Check if a concurrent commit to our branch has occurred. */
5520 head_ref_name = got_worktree_get_head_ref_name(worktree);
5521 if (head_ref_name == NULL) {
5522 err = got_error_from_errno("got_worktree_get_head_ref_name");
5523 goto done;
5525 /* Lock the reference here to prevent concurrent modification. */
5526 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5527 if (err)
5528 goto done;
5529 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5530 if (err)
5531 goto done;
5532 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5533 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5534 goto done;
5536 /* Update branch head in repository. */
5537 err = got_ref_change_ref(head_ref2, *new_commit_id);
5538 if (err)
5539 goto done;
5540 err = got_ref_write(head_ref2, repo);
5541 if (err)
5542 goto done;
5544 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5545 if (err)
5546 goto done;
5548 err = ref_base_commit(worktree, repo);
5549 if (err)
5550 goto done;
5551 done:
5552 if (head_tree)
5553 got_object_tree_close(head_tree);
5554 if (head_commit)
5555 got_object_commit_close(head_commit);
5556 free(head_commit_id2);
5557 if (head_ref2) {
5558 unlockerr = got_ref_unlock(head_ref2);
5559 if (unlockerr && err == NULL)
5560 err = unlockerr;
5561 got_ref_close(head_ref2);
5563 return err;
5566 static const struct got_error *
5567 check_path_is_commitable(const char *path,
5568 struct got_pathlist_head *commitable_paths)
5570 struct got_pathlist_entry *cpe = NULL;
5571 size_t path_len = strlen(path);
5573 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5574 struct got_commitable *ct = cpe->data;
5575 const char *ct_path = ct->path;
5577 while (ct_path[0] == '/')
5578 ct_path++;
5580 if (strcmp(path, ct_path) == 0 ||
5581 got_path_is_child(ct_path, path, path_len))
5582 break;
5585 if (cpe == NULL)
5586 return got_error_path(path, GOT_ERR_BAD_PATH);
5588 return NULL;
5591 static const struct got_error *
5592 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5594 int *have_staged_files = arg;
5596 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5597 *have_staged_files = 1;
5598 return got_error(GOT_ERR_CANCELLED);
5601 return NULL;
5604 static const struct got_error *
5605 check_non_staged_files(struct got_fileindex *fileindex,
5606 struct got_pathlist_head *paths)
5608 struct got_pathlist_entry *pe;
5609 struct got_fileindex_entry *ie;
5611 TAILQ_FOREACH(pe, paths, entry) {
5612 if (pe->path[0] == '\0')
5613 continue;
5614 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5615 if (ie == NULL)
5616 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5617 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5618 return got_error_path(pe->path,
5619 GOT_ERR_FILE_NOT_STAGED);
5622 return NULL;
5625 const struct got_error *
5626 got_worktree_commit(struct got_object_id **new_commit_id,
5627 struct got_worktree *worktree, struct got_pathlist_head *paths,
5628 const char *author, const char *committer, int allow_bad_symlinks,
5629 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5630 got_worktree_status_cb status_cb, void *status_arg,
5631 struct got_repository *repo)
5633 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5634 struct got_fileindex *fileindex = NULL;
5635 char *fileindex_path = NULL;
5636 struct got_pathlist_head commitable_paths;
5637 struct collect_commitables_arg cc_arg;
5638 struct got_pathlist_entry *pe;
5639 struct got_reference *head_ref = NULL;
5640 struct got_object_id *head_commit_id = NULL;
5641 int have_staged_files = 0;
5643 *new_commit_id = NULL;
5645 TAILQ_INIT(&commitable_paths);
5647 err = lock_worktree(worktree, LOCK_EX);
5648 if (err)
5649 goto done;
5651 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5652 if (err)
5653 goto done;
5655 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5656 if (err)
5657 goto done;
5659 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5660 if (err)
5661 goto done;
5663 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5664 &have_staged_files);
5665 if (err && err->code != GOT_ERR_CANCELLED)
5666 goto done;
5667 if (have_staged_files) {
5668 err = check_non_staged_files(fileindex, paths);
5669 if (err)
5670 goto done;
5673 cc_arg.commitable_paths = &commitable_paths;
5674 cc_arg.worktree = worktree;
5675 cc_arg.fileindex = fileindex;
5676 cc_arg.repo = repo;
5677 cc_arg.have_staged_files = have_staged_files;
5678 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5679 TAILQ_FOREACH(pe, paths, entry) {
5680 err = worktree_status(worktree, pe->path, fileindex, repo,
5681 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5682 if (err)
5683 goto done;
5686 if (TAILQ_EMPTY(&commitable_paths)) {
5687 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5688 goto done;
5691 TAILQ_FOREACH(pe, paths, entry) {
5692 err = check_path_is_commitable(pe->path, &commitable_paths);
5693 if (err)
5694 goto done;
5697 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5698 struct got_commitable *ct = pe->data;
5699 const char *ct_path = ct->in_repo_path;
5701 while (ct_path[0] == '/')
5702 ct_path++;
5703 err = check_out_of_date(ct_path, ct->status,
5704 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5705 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5706 if (err)
5707 goto done;
5711 err = commit_worktree(new_commit_id, &commitable_paths,
5712 head_commit_id, worktree, author, committer,
5713 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5714 if (err)
5715 goto done;
5717 err = update_fileindex_after_commit(worktree, &commitable_paths,
5718 *new_commit_id, fileindex, have_staged_files);
5719 sync_err = sync_fileindex(fileindex, fileindex_path);
5720 if (sync_err && err == NULL)
5721 err = sync_err;
5722 done:
5723 if (fileindex)
5724 got_fileindex_free(fileindex);
5725 free(fileindex_path);
5726 unlockerr = lock_worktree(worktree, LOCK_SH);
5727 if (unlockerr && err == NULL)
5728 err = unlockerr;
5729 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5730 struct got_commitable *ct = pe->data;
5731 free_commitable(ct);
5733 got_pathlist_free(&commitable_paths);
5734 return err;
5737 const char *
5738 got_commitable_get_path(struct got_commitable *ct)
5740 return ct->path;
5743 unsigned int
5744 got_commitable_get_status(struct got_commitable *ct)
5746 return ct->status;
5749 struct check_rebase_ok_arg {
5750 struct got_worktree *worktree;
5751 struct got_repository *repo;
5754 static const struct got_error *
5755 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5757 const struct got_error *err = NULL;
5758 struct check_rebase_ok_arg *a = arg;
5759 unsigned char status;
5760 struct stat sb;
5761 char *ondisk_path;
5763 /* Reject rebase of a work tree with mixed base commits. */
5764 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5765 SHA1_DIGEST_LENGTH))
5766 return got_error(GOT_ERR_MIXED_COMMITS);
5768 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5769 == -1)
5770 return got_error_from_errno("asprintf");
5772 /* Reject rebase of a work tree with modified or staged files. */
5773 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5774 free(ondisk_path);
5775 if (err)
5776 return err;
5778 if (status != GOT_STATUS_NO_CHANGE)
5779 return got_error(GOT_ERR_MODIFIED);
5780 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5781 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5783 return NULL;
5786 const struct got_error *
5787 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5788 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5789 struct got_worktree *worktree, struct got_reference *branch,
5790 struct got_repository *repo)
5792 const struct got_error *err = NULL;
5793 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5794 char *branch_ref_name = NULL;
5795 char *fileindex_path = NULL;
5796 struct check_rebase_ok_arg ok_arg;
5797 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5798 struct got_object_id *wt_branch_tip = NULL;
5800 *new_base_branch_ref = NULL;
5801 *tmp_branch = NULL;
5802 *fileindex = NULL;
5804 err = lock_worktree(worktree, LOCK_EX);
5805 if (err)
5806 return err;
5808 err = open_fileindex(fileindex, &fileindex_path, worktree);
5809 if (err)
5810 goto done;
5812 ok_arg.worktree = worktree;
5813 ok_arg.repo = repo;
5814 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5815 &ok_arg);
5816 if (err)
5817 goto done;
5819 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5820 if (err)
5821 goto done;
5823 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5824 if (err)
5825 goto done;
5827 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5828 if (err)
5829 goto done;
5831 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5832 0);
5833 if (err)
5834 goto done;
5836 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5837 if (err)
5838 goto done;
5839 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5840 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5841 goto done;
5844 err = got_ref_alloc_symref(new_base_branch_ref,
5845 new_base_branch_ref_name, wt_branch);
5846 if (err)
5847 goto done;
5848 err = got_ref_write(*new_base_branch_ref, repo);
5849 if (err)
5850 goto done;
5852 /* TODO Lock original branch's ref while rebasing? */
5854 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5855 if (err)
5856 goto done;
5858 err = got_ref_write(branch_ref, repo);
5859 if (err)
5860 goto done;
5862 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5863 worktree->base_commit_id);
5864 if (err)
5865 goto done;
5866 err = got_ref_write(*tmp_branch, repo);
5867 if (err)
5868 goto done;
5870 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5871 if (err)
5872 goto done;
5873 done:
5874 free(fileindex_path);
5875 free(tmp_branch_name);
5876 free(new_base_branch_ref_name);
5877 free(branch_ref_name);
5878 if (branch_ref)
5879 got_ref_close(branch_ref);
5880 if (wt_branch)
5881 got_ref_close(wt_branch);
5882 free(wt_branch_tip);
5883 if (err) {
5884 if (*new_base_branch_ref) {
5885 got_ref_close(*new_base_branch_ref);
5886 *new_base_branch_ref = NULL;
5888 if (*tmp_branch) {
5889 got_ref_close(*tmp_branch);
5890 *tmp_branch = NULL;
5892 if (*fileindex) {
5893 got_fileindex_free(*fileindex);
5894 *fileindex = NULL;
5896 lock_worktree(worktree, LOCK_SH);
5898 return err;
5901 const struct got_error *
5902 got_worktree_rebase_continue(struct got_object_id **commit_id,
5903 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5904 struct got_reference **branch, struct got_fileindex **fileindex,
5905 struct got_worktree *worktree, struct got_repository *repo)
5907 const struct got_error *err;
5908 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5909 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5910 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5911 char *fileindex_path = NULL;
5912 int have_staged_files = 0;
5914 *commit_id = NULL;
5915 *new_base_branch = NULL;
5916 *tmp_branch = NULL;
5917 *branch = NULL;
5918 *fileindex = NULL;
5920 err = lock_worktree(worktree, LOCK_EX);
5921 if (err)
5922 return err;
5924 err = open_fileindex(fileindex, &fileindex_path, worktree);
5925 if (err)
5926 goto done;
5928 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5929 &have_staged_files);
5930 if (err && err->code != GOT_ERR_CANCELLED)
5931 goto done;
5932 if (have_staged_files) {
5933 err = got_error(GOT_ERR_STAGED_PATHS);
5934 goto done;
5937 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5938 if (err)
5939 goto done;
5941 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5942 if (err)
5943 goto done;
5945 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5946 if (err)
5947 goto done;
5949 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5950 if (err)
5951 goto done;
5953 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5954 if (err)
5955 goto done;
5957 err = got_ref_open(branch, repo,
5958 got_ref_get_symref_target(branch_ref), 0);
5959 if (err)
5960 goto done;
5962 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5963 if (err)
5964 goto done;
5966 err = got_ref_resolve(commit_id, repo, commit_ref);
5967 if (err)
5968 goto done;
5970 err = got_ref_open(new_base_branch, repo,
5971 new_base_branch_ref_name, 0);
5972 if (err)
5973 goto done;
5975 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5976 if (err)
5977 goto done;
5978 done:
5979 free(commit_ref_name);
5980 free(branch_ref_name);
5981 free(fileindex_path);
5982 if (commit_ref)
5983 got_ref_close(commit_ref);
5984 if (branch_ref)
5985 got_ref_close(branch_ref);
5986 if (err) {
5987 free(*commit_id);
5988 *commit_id = NULL;
5989 if (*tmp_branch) {
5990 got_ref_close(*tmp_branch);
5991 *tmp_branch = NULL;
5993 if (*new_base_branch) {
5994 got_ref_close(*new_base_branch);
5995 *new_base_branch = NULL;
5997 if (*branch) {
5998 got_ref_close(*branch);
5999 *branch = NULL;
6001 if (*fileindex) {
6002 got_fileindex_free(*fileindex);
6003 *fileindex = NULL;
6005 lock_worktree(worktree, LOCK_SH);
6007 return err;
6010 const struct got_error *
6011 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6013 const struct got_error *err;
6014 char *tmp_branch_name = NULL;
6016 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6017 if (err)
6018 return err;
6020 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6021 free(tmp_branch_name);
6022 return NULL;
6025 static const struct got_error *
6026 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6027 char **logmsg, void *arg)
6029 *logmsg = arg;
6030 return NULL;
6033 static const struct got_error *
6034 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6035 const char *path, struct got_object_id *blob_id,
6036 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6037 int dirfd, const char *de_name)
6039 return NULL;
6042 struct collect_merged_paths_arg {
6043 got_worktree_checkout_cb progress_cb;
6044 void *progress_arg;
6045 struct got_pathlist_head *merged_paths;
6048 static const struct got_error *
6049 collect_merged_paths(void *arg, unsigned char status, const char *path)
6051 const struct got_error *err;
6052 struct collect_merged_paths_arg *a = arg;
6053 char *p;
6054 struct got_pathlist_entry *new;
6056 err = (*a->progress_cb)(a->progress_arg, status, path);
6057 if (err)
6058 return err;
6060 if (status != GOT_STATUS_MERGE &&
6061 status != GOT_STATUS_ADD &&
6062 status != GOT_STATUS_DELETE &&
6063 status != GOT_STATUS_CONFLICT)
6064 return NULL;
6066 p = strdup(path);
6067 if (p == NULL)
6068 return got_error_from_errno("strdup");
6070 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6071 if (err || new == NULL)
6072 free(p);
6073 return err;
6076 void
6077 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6079 struct got_pathlist_entry *pe;
6081 TAILQ_FOREACH(pe, merged_paths, entry)
6082 free((char *)pe->path);
6084 got_pathlist_free(merged_paths);
6087 static const struct got_error *
6088 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6089 int is_rebase, struct got_repository *repo)
6091 const struct got_error *err;
6092 struct got_reference *commit_ref = NULL;
6094 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6095 if (err) {
6096 if (err->code != GOT_ERR_NOT_REF)
6097 goto done;
6098 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6099 if (err)
6100 goto done;
6101 err = got_ref_write(commit_ref, repo);
6102 if (err)
6103 goto done;
6104 } else if (is_rebase) {
6105 struct got_object_id *stored_id;
6106 int cmp;
6108 err = got_ref_resolve(&stored_id, repo, commit_ref);
6109 if (err)
6110 goto done;
6111 cmp = got_object_id_cmp(commit_id, stored_id);
6112 free(stored_id);
6113 if (cmp != 0) {
6114 err = got_error(GOT_ERR_REBASE_COMMITID);
6115 goto done;
6118 done:
6119 if (commit_ref)
6120 got_ref_close(commit_ref);
6121 return err;
6124 static const struct got_error *
6125 rebase_merge_files(struct got_pathlist_head *merged_paths,
6126 const char *commit_ref_name, struct got_worktree *worktree,
6127 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6128 struct got_object_id *commit_id, struct got_repository *repo,
6129 got_worktree_checkout_cb progress_cb, void *progress_arg,
6130 got_cancel_cb cancel_cb, void *cancel_arg)
6132 const struct got_error *err;
6133 struct got_reference *commit_ref = NULL;
6134 struct collect_merged_paths_arg cmp_arg;
6135 char *fileindex_path;
6137 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6139 err = get_fileindex_path(&fileindex_path, worktree);
6140 if (err)
6141 return err;
6143 cmp_arg.progress_cb = progress_cb;
6144 cmp_arg.progress_arg = progress_arg;
6145 cmp_arg.merged_paths = merged_paths;
6146 err = merge_files(worktree, fileindex, fileindex_path,
6147 parent_commit_id, commit_id, repo, collect_merged_paths,
6148 &cmp_arg, cancel_cb, cancel_arg);
6149 if (commit_ref)
6150 got_ref_close(commit_ref);
6151 return err;
6154 const struct got_error *
6155 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6156 struct got_worktree *worktree, struct got_fileindex *fileindex,
6157 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6158 struct got_repository *repo,
6159 got_worktree_checkout_cb progress_cb, void *progress_arg,
6160 got_cancel_cb cancel_cb, void *cancel_arg)
6162 const struct got_error *err;
6163 char *commit_ref_name;
6165 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6166 if (err)
6167 return err;
6169 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6170 if (err)
6171 goto done;
6173 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6174 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6175 progress_arg, cancel_cb, cancel_arg);
6176 done:
6177 free(commit_ref_name);
6178 return err;
6181 const struct got_error *
6182 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6183 struct got_worktree *worktree, struct got_fileindex *fileindex,
6184 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6185 struct got_repository *repo,
6186 got_worktree_checkout_cb progress_cb, void *progress_arg,
6187 got_cancel_cb cancel_cb, void *cancel_arg)
6189 const struct got_error *err;
6190 char *commit_ref_name;
6192 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6193 if (err)
6194 return err;
6196 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6197 if (err)
6198 goto done;
6200 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6201 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6202 progress_arg, cancel_cb, cancel_arg);
6203 done:
6204 free(commit_ref_name);
6205 return err;
6208 static const struct got_error *
6209 rebase_commit(struct got_object_id **new_commit_id,
6210 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6211 struct got_worktree *worktree, struct got_fileindex *fileindex,
6212 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6213 const char *new_logmsg, struct got_repository *repo)
6215 const struct got_error *err, *sync_err;
6216 struct got_pathlist_head commitable_paths;
6217 struct collect_commitables_arg cc_arg;
6218 char *fileindex_path = NULL;
6219 struct got_reference *head_ref = NULL;
6220 struct got_object_id *head_commit_id = NULL;
6221 char *logmsg = NULL;
6223 TAILQ_INIT(&commitable_paths);
6224 *new_commit_id = NULL;
6226 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6228 err = get_fileindex_path(&fileindex_path, worktree);
6229 if (err)
6230 return err;
6232 cc_arg.commitable_paths = &commitable_paths;
6233 cc_arg.worktree = worktree;
6234 cc_arg.repo = repo;
6235 cc_arg.have_staged_files = 0;
6237 * If possible get the status of individual files directly to
6238 * avoid crawling the entire work tree once per rebased commit.
6239 * TODO: Ideally, merged_paths would contain a list of commitables
6240 * we could use so we could skip worktree_status() entirely.
6242 if (merged_paths) {
6243 struct got_pathlist_entry *pe;
6244 TAILQ_FOREACH(pe, merged_paths, entry) {
6245 err = worktree_status(worktree, pe->path, fileindex,
6246 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6247 0);
6248 if (err)
6249 goto done;
6251 } else {
6252 err = worktree_status(worktree, "", fileindex, repo,
6253 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6254 if (err)
6255 goto done;
6258 if (TAILQ_EMPTY(&commitable_paths)) {
6259 /* No-op change; commit will be elided. */
6260 err = got_ref_delete(commit_ref, repo);
6261 if (err)
6262 goto done;
6263 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6264 goto done;
6267 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6268 if (err)
6269 goto done;
6271 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6272 if (err)
6273 goto done;
6275 if (new_logmsg) {
6276 logmsg = strdup(new_logmsg);
6277 if (logmsg == NULL) {
6278 err = got_error_from_errno("strdup");
6279 goto done;
6281 } else {
6282 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6283 if (err)
6284 goto done;
6287 /* NB: commit_worktree will call free(logmsg) */
6288 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6289 worktree, got_object_commit_get_author(orig_commit),
6290 got_object_commit_get_committer(orig_commit),
6291 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6292 if (err)
6293 goto done;
6295 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6296 if (err)
6297 goto done;
6299 err = got_ref_delete(commit_ref, repo);
6300 if (err)
6301 goto done;
6303 err = update_fileindex_after_commit(worktree, &commitable_paths,
6304 *new_commit_id, fileindex, 0);
6305 sync_err = sync_fileindex(fileindex, fileindex_path);
6306 if (sync_err && err == NULL)
6307 err = sync_err;
6308 done:
6309 free(fileindex_path);
6310 free(head_commit_id);
6311 if (head_ref)
6312 got_ref_close(head_ref);
6313 if (err) {
6314 free(*new_commit_id);
6315 *new_commit_id = NULL;
6317 return err;
6320 const struct got_error *
6321 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6322 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6323 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6324 struct got_commit_object *orig_commit,
6325 struct got_object_id *orig_commit_id, struct got_repository *repo)
6327 const struct got_error *err;
6328 char *commit_ref_name;
6329 struct got_reference *commit_ref = NULL;
6330 struct got_object_id *commit_id = NULL;
6332 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6333 if (err)
6334 return err;
6336 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6337 if (err)
6338 goto done;
6339 err = got_ref_resolve(&commit_id, repo, commit_ref);
6340 if (err)
6341 goto done;
6342 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6343 err = got_error(GOT_ERR_REBASE_COMMITID);
6344 goto done;
6347 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6348 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6349 done:
6350 if (commit_ref)
6351 got_ref_close(commit_ref);
6352 free(commit_ref_name);
6353 free(commit_id);
6354 return err;
6357 const struct got_error *
6358 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6359 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6360 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6361 struct got_commit_object *orig_commit,
6362 struct got_object_id *orig_commit_id, const char *new_logmsg,
6363 struct got_repository *repo)
6365 const struct got_error *err;
6366 char *commit_ref_name;
6367 struct got_reference *commit_ref = NULL;
6369 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6370 if (err)
6371 return err;
6373 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6374 if (err)
6375 goto done;
6377 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6378 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6379 done:
6380 if (commit_ref)
6381 got_ref_close(commit_ref);
6382 free(commit_ref_name);
6383 return err;
6386 const struct got_error *
6387 got_worktree_rebase_postpone(struct got_worktree *worktree,
6388 struct got_fileindex *fileindex)
6390 if (fileindex)
6391 got_fileindex_free(fileindex);
6392 return lock_worktree(worktree, LOCK_SH);
6395 static const struct got_error *
6396 delete_ref(const char *name, struct got_repository *repo)
6398 const struct got_error *err;
6399 struct got_reference *ref;
6401 err = got_ref_open(&ref, repo, name, 0);
6402 if (err) {
6403 if (err->code == GOT_ERR_NOT_REF)
6404 return NULL;
6405 return err;
6408 err = got_ref_delete(ref, repo);
6409 got_ref_close(ref);
6410 return err;
6413 static const struct got_error *
6414 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6416 const struct got_error *err;
6417 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6418 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6420 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6421 if (err)
6422 goto done;
6423 err = delete_ref(tmp_branch_name, repo);
6424 if (err)
6425 goto done;
6427 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6428 if (err)
6429 goto done;
6430 err = delete_ref(new_base_branch_ref_name, repo);
6431 if (err)
6432 goto done;
6434 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6435 if (err)
6436 goto done;
6437 err = delete_ref(branch_ref_name, repo);
6438 if (err)
6439 goto done;
6441 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6442 if (err)
6443 goto done;
6444 err = delete_ref(commit_ref_name, repo);
6445 if (err)
6446 goto done;
6448 done:
6449 free(tmp_branch_name);
6450 free(new_base_branch_ref_name);
6451 free(branch_ref_name);
6452 free(commit_ref_name);
6453 return err;
6456 const struct got_error *
6457 got_worktree_rebase_complete(struct got_worktree *worktree,
6458 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6459 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6460 struct got_repository *repo)
6462 const struct got_error *err, *unlockerr, *sync_err;
6463 struct got_object_id *new_head_commit_id = NULL;
6464 char *fileindex_path = NULL;
6466 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6467 if (err)
6468 return err;
6470 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6471 if (err)
6472 goto done;
6474 err = got_ref_write(rebased_branch, repo);
6475 if (err)
6476 goto done;
6478 err = got_worktree_set_head_ref(worktree, rebased_branch);
6479 if (err)
6480 goto done;
6482 err = delete_rebase_refs(worktree, repo);
6483 if (err)
6484 goto done;
6486 err = get_fileindex_path(&fileindex_path, worktree);
6487 if (err)
6488 goto done;
6489 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6490 sync_err = sync_fileindex(fileindex, fileindex_path);
6491 if (sync_err && err == NULL)
6492 err = sync_err;
6493 done:
6494 got_fileindex_free(fileindex);
6495 free(fileindex_path);
6496 free(new_head_commit_id);
6497 unlockerr = lock_worktree(worktree, LOCK_SH);
6498 if (unlockerr && err == NULL)
6499 err = unlockerr;
6500 return err;
6503 const struct got_error *
6504 got_worktree_rebase_abort(struct got_worktree *worktree,
6505 struct got_fileindex *fileindex, struct got_repository *repo,
6506 struct got_reference *new_base_branch,
6507 got_worktree_checkout_cb progress_cb, void *progress_arg)
6509 const struct got_error *err, *unlockerr, *sync_err;
6510 struct got_reference *resolved = NULL;
6511 struct got_object_id *commit_id = NULL;
6512 char *fileindex_path = NULL;
6513 struct revert_file_args rfa;
6514 struct got_object_id *tree_id = NULL;
6516 err = lock_worktree(worktree, LOCK_EX);
6517 if (err)
6518 return err;
6520 err = got_ref_open(&resolved, repo,
6521 got_ref_get_symref_target(new_base_branch), 0);
6522 if (err)
6523 goto done;
6525 err = got_worktree_set_head_ref(worktree, resolved);
6526 if (err)
6527 goto done;
6530 * XXX commits to the base branch could have happened while
6531 * we were busy rebasing; should we store the original commit ID
6532 * when rebase begins and read it back here?
6534 err = got_ref_resolve(&commit_id, repo, resolved);
6535 if (err)
6536 goto done;
6538 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6539 if (err)
6540 goto done;
6542 err = got_object_id_by_path(&tree_id, repo,
6543 worktree->base_commit_id, worktree->path_prefix);
6544 if (err)
6545 goto done;
6547 err = delete_rebase_refs(worktree, repo);
6548 if (err)
6549 goto done;
6551 err = get_fileindex_path(&fileindex_path, worktree);
6552 if (err)
6553 goto done;
6555 rfa.worktree = worktree;
6556 rfa.fileindex = fileindex;
6557 rfa.progress_cb = progress_cb;
6558 rfa.progress_arg = progress_arg;
6559 rfa.patch_cb = NULL;
6560 rfa.patch_arg = NULL;
6561 rfa.repo = repo;
6562 err = worktree_status(worktree, "", fileindex, repo,
6563 revert_file, &rfa, NULL, NULL, 0, 0);
6564 if (err)
6565 goto sync;
6567 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6568 repo, progress_cb, progress_arg, NULL, NULL);
6569 sync:
6570 sync_err = sync_fileindex(fileindex, fileindex_path);
6571 if (sync_err && err == NULL)
6572 err = sync_err;
6573 done:
6574 got_ref_close(resolved);
6575 free(tree_id);
6576 free(commit_id);
6577 if (fileindex)
6578 got_fileindex_free(fileindex);
6579 free(fileindex_path);
6581 unlockerr = lock_worktree(worktree, LOCK_SH);
6582 if (unlockerr && err == NULL)
6583 err = unlockerr;
6584 return err;
6587 const struct got_error *
6588 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6589 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6590 struct got_fileindex **fileindex, struct got_worktree *worktree,
6591 struct got_repository *repo)
6593 const struct got_error *err = NULL;
6594 char *tmp_branch_name = NULL;
6595 char *branch_ref_name = NULL;
6596 char *base_commit_ref_name = NULL;
6597 char *fileindex_path = NULL;
6598 struct check_rebase_ok_arg ok_arg;
6599 struct got_reference *wt_branch = NULL;
6600 struct got_reference *base_commit_ref = NULL;
6602 *tmp_branch = NULL;
6603 *branch_ref = NULL;
6604 *base_commit_id = NULL;
6605 *fileindex = NULL;
6607 err = lock_worktree(worktree, LOCK_EX);
6608 if (err)
6609 return err;
6611 err = open_fileindex(fileindex, &fileindex_path, worktree);
6612 if (err)
6613 goto done;
6615 ok_arg.worktree = worktree;
6616 ok_arg.repo = repo;
6617 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6618 &ok_arg);
6619 if (err)
6620 goto done;
6622 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6623 if (err)
6624 goto done;
6626 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6627 if (err)
6628 goto done;
6630 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6631 worktree);
6632 if (err)
6633 goto done;
6635 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6636 0);
6637 if (err)
6638 goto done;
6640 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6641 if (err)
6642 goto done;
6644 err = got_ref_write(*branch_ref, repo);
6645 if (err)
6646 goto done;
6648 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6649 worktree->base_commit_id);
6650 if (err)
6651 goto done;
6652 err = got_ref_write(base_commit_ref, repo);
6653 if (err)
6654 goto done;
6655 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6656 if (*base_commit_id == NULL) {
6657 err = got_error_from_errno("got_object_id_dup");
6658 goto done;
6661 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6662 worktree->base_commit_id);
6663 if (err)
6664 goto done;
6665 err = got_ref_write(*tmp_branch, repo);
6666 if (err)
6667 goto done;
6669 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6670 if (err)
6671 goto done;
6672 done:
6673 free(fileindex_path);
6674 free(tmp_branch_name);
6675 free(branch_ref_name);
6676 free(base_commit_ref_name);
6677 if (wt_branch)
6678 got_ref_close(wt_branch);
6679 if (err) {
6680 if (*branch_ref) {
6681 got_ref_close(*branch_ref);
6682 *branch_ref = NULL;
6684 if (*tmp_branch) {
6685 got_ref_close(*tmp_branch);
6686 *tmp_branch = NULL;
6688 free(*base_commit_id);
6689 if (*fileindex) {
6690 got_fileindex_free(*fileindex);
6691 *fileindex = NULL;
6693 lock_worktree(worktree, LOCK_SH);
6695 return err;
6698 const struct got_error *
6699 got_worktree_histedit_postpone(struct got_worktree *worktree,
6700 struct got_fileindex *fileindex)
6702 if (fileindex)
6703 got_fileindex_free(fileindex);
6704 return lock_worktree(worktree, LOCK_SH);
6707 const struct got_error *
6708 got_worktree_histedit_in_progress(int *in_progress,
6709 struct got_worktree *worktree)
6711 const struct got_error *err;
6712 char *tmp_branch_name = NULL;
6714 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6715 if (err)
6716 return err;
6718 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6719 free(tmp_branch_name);
6720 return NULL;
6723 const struct got_error *
6724 got_worktree_histedit_continue(struct got_object_id **commit_id,
6725 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6726 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6727 struct got_worktree *worktree, struct got_repository *repo)
6729 const struct got_error *err;
6730 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6731 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6732 struct got_reference *commit_ref = NULL;
6733 struct got_reference *base_commit_ref = NULL;
6734 char *fileindex_path = NULL;
6735 int have_staged_files = 0;
6737 *commit_id = NULL;
6738 *tmp_branch = NULL;
6739 *base_commit_id = NULL;
6740 *fileindex = NULL;
6742 err = lock_worktree(worktree, LOCK_EX);
6743 if (err)
6744 return err;
6746 err = open_fileindex(fileindex, &fileindex_path, worktree);
6747 if (err)
6748 goto done;
6750 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6751 &have_staged_files);
6752 if (err && err->code != GOT_ERR_CANCELLED)
6753 goto done;
6754 if (have_staged_files) {
6755 err = got_error(GOT_ERR_STAGED_PATHS);
6756 goto done;
6759 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6760 if (err)
6761 goto done;
6763 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6764 if (err)
6765 goto done;
6767 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6768 if (err)
6769 goto done;
6771 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6772 worktree);
6773 if (err)
6774 goto done;
6776 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6777 if (err)
6778 goto done;
6780 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6781 if (err)
6782 goto done;
6783 err = got_ref_resolve(commit_id, repo, commit_ref);
6784 if (err)
6785 goto done;
6787 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6788 if (err)
6789 goto done;
6790 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6791 if (err)
6792 goto done;
6794 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6795 if (err)
6796 goto done;
6797 done:
6798 free(commit_ref_name);
6799 free(branch_ref_name);
6800 free(fileindex_path);
6801 if (commit_ref)
6802 got_ref_close(commit_ref);
6803 if (base_commit_ref)
6804 got_ref_close(base_commit_ref);
6805 if (err) {
6806 free(*commit_id);
6807 *commit_id = NULL;
6808 free(*base_commit_id);
6809 *base_commit_id = NULL;
6810 if (*tmp_branch) {
6811 got_ref_close(*tmp_branch);
6812 *tmp_branch = NULL;
6814 if (*fileindex) {
6815 got_fileindex_free(*fileindex);
6816 *fileindex = NULL;
6818 lock_worktree(worktree, LOCK_EX);
6820 return err;
6823 static const struct got_error *
6824 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6826 const struct got_error *err;
6827 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6828 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6830 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6831 if (err)
6832 goto done;
6833 err = delete_ref(tmp_branch_name, repo);
6834 if (err)
6835 goto done;
6837 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6838 worktree);
6839 if (err)
6840 goto done;
6841 err = delete_ref(base_commit_ref_name, repo);
6842 if (err)
6843 goto done;
6845 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6846 if (err)
6847 goto done;
6848 err = delete_ref(branch_ref_name, repo);
6849 if (err)
6850 goto done;
6852 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6853 if (err)
6854 goto done;
6855 err = delete_ref(commit_ref_name, repo);
6856 if (err)
6857 goto done;
6858 done:
6859 free(tmp_branch_name);
6860 free(base_commit_ref_name);
6861 free(branch_ref_name);
6862 free(commit_ref_name);
6863 return err;
6866 const struct got_error *
6867 got_worktree_histedit_abort(struct got_worktree *worktree,
6868 struct got_fileindex *fileindex, struct got_repository *repo,
6869 struct got_reference *branch, struct got_object_id *base_commit_id,
6870 got_worktree_checkout_cb progress_cb, void *progress_arg)
6872 const struct got_error *err, *unlockerr, *sync_err;
6873 struct got_reference *resolved = NULL;
6874 char *fileindex_path = NULL;
6875 struct got_object_id *tree_id = NULL;
6876 struct revert_file_args rfa;
6878 err = lock_worktree(worktree, LOCK_EX);
6879 if (err)
6880 return err;
6882 err = got_ref_open(&resolved, repo,
6883 got_ref_get_symref_target(branch), 0);
6884 if (err)
6885 goto done;
6887 err = got_worktree_set_head_ref(worktree, resolved);
6888 if (err)
6889 goto done;
6891 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6892 if (err)
6893 goto done;
6895 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6896 worktree->path_prefix);
6897 if (err)
6898 goto done;
6900 err = delete_histedit_refs(worktree, repo);
6901 if (err)
6902 goto done;
6904 err = get_fileindex_path(&fileindex_path, worktree);
6905 if (err)
6906 goto done;
6908 rfa.worktree = worktree;
6909 rfa.fileindex = fileindex;
6910 rfa.progress_cb = progress_cb;
6911 rfa.progress_arg = progress_arg;
6912 rfa.patch_cb = NULL;
6913 rfa.patch_arg = NULL;
6914 rfa.repo = repo;
6915 err = worktree_status(worktree, "", fileindex, repo,
6916 revert_file, &rfa, NULL, NULL, 0, 0);
6917 if (err)
6918 goto sync;
6920 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6921 repo, progress_cb, progress_arg, NULL, NULL);
6922 sync:
6923 sync_err = sync_fileindex(fileindex, fileindex_path);
6924 if (sync_err && err == NULL)
6925 err = sync_err;
6926 done:
6927 got_ref_close(resolved);
6928 free(tree_id);
6929 free(fileindex_path);
6931 unlockerr = lock_worktree(worktree, LOCK_SH);
6932 if (unlockerr && err == NULL)
6933 err = unlockerr;
6934 return err;
6937 const struct got_error *
6938 got_worktree_histedit_complete(struct got_worktree *worktree,
6939 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6940 struct got_reference *edited_branch, struct got_repository *repo)
6942 const struct got_error *err, *unlockerr, *sync_err;
6943 struct got_object_id *new_head_commit_id = NULL;
6944 struct got_reference *resolved = NULL;
6945 char *fileindex_path = NULL;
6947 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6948 if (err)
6949 return err;
6951 err = got_ref_open(&resolved, repo,
6952 got_ref_get_symref_target(edited_branch), 0);
6953 if (err)
6954 goto done;
6956 err = got_ref_change_ref(resolved, new_head_commit_id);
6957 if (err)
6958 goto done;
6960 err = got_ref_write(resolved, repo);
6961 if (err)
6962 goto done;
6964 err = got_worktree_set_head_ref(worktree, resolved);
6965 if (err)
6966 goto done;
6968 err = delete_histedit_refs(worktree, repo);
6969 if (err)
6970 goto done;
6972 err = get_fileindex_path(&fileindex_path, worktree);
6973 if (err)
6974 goto done;
6975 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6976 sync_err = sync_fileindex(fileindex, fileindex_path);
6977 if (sync_err && err == NULL)
6978 err = sync_err;
6979 done:
6980 got_fileindex_free(fileindex);
6981 free(fileindex_path);
6982 free(new_head_commit_id);
6983 unlockerr = lock_worktree(worktree, LOCK_SH);
6984 if (unlockerr && err == NULL)
6985 err = unlockerr;
6986 return err;
6989 const struct got_error *
6990 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6991 struct got_object_id *commit_id, struct got_repository *repo)
6993 const struct got_error *err;
6994 char *commit_ref_name;
6996 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6997 if (err)
6998 return err;
7000 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7001 if (err)
7002 goto done;
7004 err = delete_ref(commit_ref_name, repo);
7005 done:
7006 free(commit_ref_name);
7007 return err;
7010 const struct got_error *
7011 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7012 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7013 struct got_worktree *worktree, const char *refname,
7014 struct got_repository *repo)
7016 const struct got_error *err = NULL;
7017 char *fileindex_path = NULL;
7018 struct check_rebase_ok_arg ok_arg;
7020 *fileindex = NULL;
7021 *branch_ref = NULL;
7022 *base_branch_ref = NULL;
7024 err = lock_worktree(worktree, LOCK_EX);
7025 if (err)
7026 return err;
7028 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7029 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7030 "cannot integrate a branch into itself; "
7031 "update -b or different branch name required");
7032 goto done;
7035 err = open_fileindex(fileindex, &fileindex_path, worktree);
7036 if (err)
7037 goto done;
7039 /* Preconditions are the same as for rebase. */
7040 ok_arg.worktree = worktree;
7041 ok_arg.repo = repo;
7042 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7043 &ok_arg);
7044 if (err)
7045 goto done;
7047 err = got_ref_open(branch_ref, repo, refname, 1);
7048 if (err)
7049 goto done;
7051 err = got_ref_open(base_branch_ref, repo,
7052 got_worktree_get_head_ref_name(worktree), 1);
7053 done:
7054 if (err) {
7055 if (*branch_ref) {
7056 got_ref_close(*branch_ref);
7057 *branch_ref = NULL;
7059 if (*base_branch_ref) {
7060 got_ref_close(*base_branch_ref);
7061 *base_branch_ref = NULL;
7063 if (*fileindex) {
7064 got_fileindex_free(*fileindex);
7065 *fileindex = NULL;
7067 lock_worktree(worktree, LOCK_SH);
7069 return err;
7072 const struct got_error *
7073 got_worktree_integrate_continue(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_repository *repo,
7075 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7076 got_worktree_checkout_cb progress_cb, void *progress_arg,
7077 got_cancel_cb cancel_cb, void *cancel_arg)
7079 const struct got_error *err = NULL, *sync_err, *unlockerr;
7080 char *fileindex_path = NULL;
7081 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7083 err = get_fileindex_path(&fileindex_path, worktree);
7084 if (err)
7085 goto done;
7087 err = got_ref_resolve(&commit_id, repo, branch_ref);
7088 if (err)
7089 goto done;
7091 err = got_object_id_by_path(&tree_id, repo, commit_id,
7092 worktree->path_prefix);
7093 if (err)
7094 goto done;
7096 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7097 if (err)
7098 goto done;
7100 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7101 progress_cb, progress_arg, cancel_cb, cancel_arg);
7102 if (err)
7103 goto sync;
7105 err = got_ref_change_ref(base_branch_ref, commit_id);
7106 if (err)
7107 goto sync;
7109 err = got_ref_write(base_branch_ref, repo);
7110 sync:
7111 sync_err = sync_fileindex(fileindex, fileindex_path);
7112 if (sync_err && err == NULL)
7113 err = sync_err;
7115 done:
7116 unlockerr = got_ref_unlock(branch_ref);
7117 if (unlockerr && err == NULL)
7118 err = unlockerr;
7119 got_ref_close(branch_ref);
7121 unlockerr = got_ref_unlock(base_branch_ref);
7122 if (unlockerr && err == NULL)
7123 err = unlockerr;
7124 got_ref_close(base_branch_ref);
7126 got_fileindex_free(fileindex);
7127 free(fileindex_path);
7128 free(tree_id);
7130 unlockerr = lock_worktree(worktree, LOCK_SH);
7131 if (unlockerr && err == NULL)
7132 err = unlockerr;
7133 return err;
7136 const struct got_error *
7137 got_worktree_integrate_abort(struct got_worktree *worktree,
7138 struct got_fileindex *fileindex, struct got_repository *repo,
7139 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7141 const struct got_error *err = NULL, *unlockerr = NULL;
7143 got_fileindex_free(fileindex);
7145 err = lock_worktree(worktree, LOCK_SH);
7147 unlockerr = got_ref_unlock(branch_ref);
7148 if (unlockerr && err == NULL)
7149 err = unlockerr;
7150 got_ref_close(branch_ref);
7152 unlockerr = got_ref_unlock(base_branch_ref);
7153 if (unlockerr && err == NULL)
7154 err = unlockerr;
7155 got_ref_close(base_branch_ref);
7157 return err;
7160 struct check_stage_ok_arg {
7161 struct got_object_id *head_commit_id;
7162 struct got_worktree *worktree;
7163 struct got_fileindex *fileindex;
7164 struct got_repository *repo;
7165 int have_changes;
7168 const struct got_error *
7169 check_stage_ok(void *arg, unsigned char status,
7170 unsigned char staged_status, const char *relpath,
7171 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7172 struct got_object_id *commit_id, int dirfd, const char *de_name)
7174 struct check_stage_ok_arg *a = arg;
7175 const struct got_error *err = NULL;
7176 struct got_fileindex_entry *ie;
7177 struct got_object_id base_commit_id;
7178 struct got_object_id *base_commit_idp = NULL;
7179 char *in_repo_path = NULL, *p;
7181 if (status == GOT_STATUS_UNVERSIONED ||
7182 status == GOT_STATUS_NO_CHANGE)
7183 return NULL;
7184 if (status == GOT_STATUS_NONEXISTENT)
7185 return got_error_set_errno(ENOENT, relpath);
7187 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7188 if (ie == NULL)
7189 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7191 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7192 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7193 relpath) == -1)
7194 return got_error_from_errno("asprintf");
7196 if (got_fileindex_entry_has_commit(ie)) {
7197 memcpy(base_commit_id.sha1, ie->commit_sha1,
7198 SHA1_DIGEST_LENGTH);
7199 base_commit_idp = &base_commit_id;
7202 if (status == GOT_STATUS_CONFLICT) {
7203 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7204 goto done;
7205 } else if (status != GOT_STATUS_ADD &&
7206 status != GOT_STATUS_MODIFY &&
7207 status != GOT_STATUS_DELETE) {
7208 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7209 goto done;
7212 a->have_changes = 1;
7214 p = in_repo_path;
7215 while (p[0] == '/')
7216 p++;
7217 err = check_out_of_date(p, status, staged_status,
7218 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7219 GOT_ERR_STAGE_OUT_OF_DATE);
7220 done:
7221 free(in_repo_path);
7222 return err;
7225 struct stage_path_arg {
7226 struct got_worktree *worktree;
7227 struct got_fileindex *fileindex;
7228 struct got_repository *repo;
7229 got_worktree_status_cb status_cb;
7230 void *status_arg;
7231 got_worktree_patch_cb patch_cb;
7232 void *patch_arg;
7233 int staged_something;
7234 int allow_bad_symlinks;
7237 static const struct got_error *
7238 stage_path(void *arg, unsigned char status,
7239 unsigned char staged_status, const char *relpath,
7240 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7241 struct got_object_id *commit_id, int dirfd, const char *de_name)
7243 struct stage_path_arg *a = arg;
7244 const struct got_error *err = NULL;
7245 struct got_fileindex_entry *ie;
7246 char *ondisk_path = NULL, *path_content = NULL;
7247 uint32_t stage;
7248 struct got_object_id *new_staged_blob_id = NULL;
7249 struct stat sb;
7251 if (status == GOT_STATUS_UNVERSIONED)
7252 return NULL;
7254 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7255 if (ie == NULL)
7256 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7258 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7259 relpath)== -1)
7260 return got_error_from_errno("asprintf");
7262 switch (status) {
7263 case GOT_STATUS_ADD:
7264 case GOT_STATUS_MODIFY:
7265 /* XXX could sb.st_mode be passed in by our caller? */
7266 if (lstat(ondisk_path, &sb) == -1) {
7267 err = got_error_from_errno2("lstat", ondisk_path);
7268 break;
7270 if (a->patch_cb) {
7271 if (status == GOT_STATUS_ADD) {
7272 int choice = GOT_PATCH_CHOICE_NONE;
7273 err = (*a->patch_cb)(&choice, a->patch_arg,
7274 status, ie->path, NULL, 1, 1);
7275 if (err)
7276 break;
7277 if (choice != GOT_PATCH_CHOICE_YES)
7278 break;
7279 } else {
7280 err = create_patched_content(&path_content, 0,
7281 staged_blob_id ? staged_blob_id : blob_id,
7282 ondisk_path, dirfd, de_name, ie->path,
7283 a->repo, a->patch_cb, a->patch_arg);
7284 if (err || path_content == NULL)
7285 break;
7288 err = got_object_blob_create(&new_staged_blob_id,
7289 path_content ? path_content : ondisk_path, a->repo);
7290 if (err)
7291 break;
7292 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7293 SHA1_DIGEST_LENGTH);
7294 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7295 stage = GOT_FILEIDX_STAGE_ADD;
7296 else
7297 stage = GOT_FILEIDX_STAGE_MODIFY;
7298 got_fileindex_entry_stage_set(ie, stage);
7299 if (S_ISLNK(sb.st_mode)) {
7300 int is_bad_symlink = 0;
7301 if (!a->allow_bad_symlinks) {
7302 char target_path[PATH_MAX];
7303 ssize_t target_len;
7304 target_len = readlink(ondisk_path, target_path,
7305 sizeof(target_path));
7306 if (target_len == -1) {
7307 err = got_error_from_errno2("readlink",
7308 ondisk_path);
7309 break;
7311 err = is_bad_symlink_target(&is_bad_symlink,
7312 target_path, target_len, ondisk_path,
7313 a->worktree->root_path);
7314 if (err)
7315 break;
7316 if (is_bad_symlink) {
7317 err = got_error_path(ondisk_path,
7318 GOT_ERR_BAD_SYMLINK);
7319 break;
7322 if (is_bad_symlink)
7323 got_fileindex_entry_staged_filetype_set(ie,
7324 GOT_FILEIDX_MODE_BAD_SYMLINK);
7325 else
7326 got_fileindex_entry_staged_filetype_set(ie,
7327 GOT_FILEIDX_MODE_SYMLINK);
7328 } else {
7329 got_fileindex_entry_staged_filetype_set(ie,
7330 GOT_FILEIDX_MODE_REGULAR_FILE);
7332 a->staged_something = 1;
7333 if (a->status_cb == NULL)
7334 break;
7335 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7336 get_staged_status(ie), relpath, blob_id,
7337 new_staged_blob_id, NULL, dirfd, de_name);
7338 break;
7339 case GOT_STATUS_DELETE:
7340 if (staged_status == GOT_STATUS_DELETE)
7341 break;
7342 if (a->patch_cb) {
7343 int choice = GOT_PATCH_CHOICE_NONE;
7344 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7345 ie->path, NULL, 1, 1);
7346 if (err)
7347 break;
7348 if (choice == GOT_PATCH_CHOICE_NO)
7349 break;
7350 if (choice != GOT_PATCH_CHOICE_YES) {
7351 err = got_error(GOT_ERR_PATCH_CHOICE);
7352 break;
7355 stage = GOT_FILEIDX_STAGE_DELETE;
7356 got_fileindex_entry_stage_set(ie, stage);
7357 a->staged_something = 1;
7358 if (a->status_cb == NULL)
7359 break;
7360 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7361 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7362 de_name);
7363 break;
7364 case GOT_STATUS_NO_CHANGE:
7365 break;
7366 case GOT_STATUS_CONFLICT:
7367 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7368 break;
7369 case GOT_STATUS_NONEXISTENT:
7370 err = got_error_set_errno(ENOENT, relpath);
7371 break;
7372 default:
7373 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7374 break;
7377 if (path_content && unlink(path_content) == -1 && err == NULL)
7378 err = got_error_from_errno2("unlink", path_content);
7379 free(path_content);
7380 free(ondisk_path);
7381 free(new_staged_blob_id);
7382 return err;
7385 const struct got_error *
7386 got_worktree_stage(struct got_worktree *worktree,
7387 struct got_pathlist_head *paths,
7388 got_worktree_status_cb status_cb, void *status_arg,
7389 got_worktree_patch_cb patch_cb, void *patch_arg,
7390 int allow_bad_symlinks, struct got_repository *repo)
7392 const struct got_error *err = NULL, *sync_err, *unlockerr;
7393 struct got_pathlist_entry *pe;
7394 struct got_fileindex *fileindex = NULL;
7395 char *fileindex_path = NULL;
7396 struct got_reference *head_ref = NULL;
7397 struct got_object_id *head_commit_id = NULL;
7398 struct check_stage_ok_arg oka;
7399 struct stage_path_arg spa;
7401 err = lock_worktree(worktree, LOCK_EX);
7402 if (err)
7403 return err;
7405 err = got_ref_open(&head_ref, repo,
7406 got_worktree_get_head_ref_name(worktree), 0);
7407 if (err)
7408 goto done;
7409 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7410 if (err)
7411 goto done;
7412 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7413 if (err)
7414 goto done;
7416 /* Check pre-conditions before staging anything. */
7417 oka.head_commit_id = head_commit_id;
7418 oka.worktree = worktree;
7419 oka.fileindex = fileindex;
7420 oka.repo = repo;
7421 oka.have_changes = 0;
7422 TAILQ_FOREACH(pe, paths, entry) {
7423 err = worktree_status(worktree, pe->path, fileindex, repo,
7424 check_stage_ok, &oka, NULL, NULL, 0, 0);
7425 if (err)
7426 goto done;
7428 if (!oka.have_changes) {
7429 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7430 goto done;
7433 spa.worktree = worktree;
7434 spa.fileindex = fileindex;
7435 spa.repo = repo;
7436 spa.patch_cb = patch_cb;
7437 spa.patch_arg = patch_arg;
7438 spa.status_cb = status_cb;
7439 spa.status_arg = status_arg;
7440 spa.staged_something = 0;
7441 spa.allow_bad_symlinks = allow_bad_symlinks;
7442 TAILQ_FOREACH(pe, paths, entry) {
7443 err = worktree_status(worktree, pe->path, fileindex, repo,
7444 stage_path, &spa, NULL, NULL, 0, 0);
7445 if (err)
7446 goto done;
7448 if (!spa.staged_something) {
7449 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7450 goto done;
7453 sync_err = sync_fileindex(fileindex, fileindex_path);
7454 if (sync_err && err == NULL)
7455 err = sync_err;
7456 done:
7457 if (head_ref)
7458 got_ref_close(head_ref);
7459 free(head_commit_id);
7460 free(fileindex_path);
7461 if (fileindex)
7462 got_fileindex_free(fileindex);
7463 unlockerr = lock_worktree(worktree, LOCK_SH);
7464 if (unlockerr && err == NULL)
7465 err = unlockerr;
7466 return err;
7469 struct unstage_path_arg {
7470 struct got_worktree *worktree;
7471 struct got_fileindex *fileindex;
7472 struct got_repository *repo;
7473 got_worktree_checkout_cb progress_cb;
7474 void *progress_arg;
7475 got_worktree_patch_cb patch_cb;
7476 void *patch_arg;
7479 static const struct got_error *
7480 create_unstaged_content(char **path_unstaged_content,
7481 char **path_new_staged_content, struct got_object_id *blob_id,
7482 struct got_object_id *staged_blob_id, const char *relpath,
7483 struct got_repository *repo,
7484 got_worktree_patch_cb patch_cb, void *patch_arg)
7486 const struct got_error *err, *free_err;
7487 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7488 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7489 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7490 struct got_diffreg_result *diffreg_result = NULL;
7491 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7492 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7494 *path_unstaged_content = NULL;
7495 *path_new_staged_content = NULL;
7497 err = got_object_id_str(&label1, blob_id);
7498 if (err)
7499 return err;
7500 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7501 if (err)
7502 goto done;
7504 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7505 if (err)
7506 goto done;
7508 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7509 if (err)
7510 goto done;
7512 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7513 if (err)
7514 goto done;
7516 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7517 if (err)
7518 goto done;
7520 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7521 if (err)
7522 goto done;
7524 err = got_diff_files(&diffreg_result, f1, label1, f2,
7525 path2, 3, 0, 1, NULL);
7526 if (err)
7527 goto done;
7529 err = got_opentemp_named(path_unstaged_content, &outfile,
7530 "got-unstaged-content");
7531 if (err)
7532 goto done;
7533 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7534 "got-new-staged-content");
7535 if (err)
7536 goto done;
7538 if (fseek(f1, 0L, SEEK_SET) == -1) {
7539 err = got_ferror(f1, GOT_ERR_IO);
7540 goto done;
7542 if (fseek(f2, 0L, SEEK_SET) == -1) {
7543 err = got_ferror(f2, GOT_ERR_IO);
7544 goto done;
7546 /* Count the number of actual changes in the diff result. */
7547 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7548 struct diff_chunk_context cc = {};
7549 diff_chunk_context_load_change(&cc, &nchunks_used,
7550 diffreg_result->result, n, 0);
7551 nchanges++;
7553 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7554 int choice;
7555 err = apply_or_reject_change(&choice, &nchunks_used,
7556 diffreg_result->result, n, relpath, f1, f2,
7557 &line_cur1, &line_cur2,
7558 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7559 if (err)
7560 goto done;
7561 if (choice == GOT_PATCH_CHOICE_YES)
7562 have_content = 1;
7563 else
7564 have_rejected_content = 1;
7565 if (choice == GOT_PATCH_CHOICE_QUIT)
7566 break;
7568 if (have_content || have_rejected_content)
7569 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7570 outfile, rejectfile);
7571 done:
7572 free(label1);
7573 if (blob)
7574 got_object_blob_close(blob);
7575 if (staged_blob)
7576 got_object_blob_close(staged_blob);
7577 free_err = got_diffreg_result_free(diffreg_result);
7578 if (free_err && err == NULL)
7579 err = free_err;
7580 if (f1 && fclose(f1) == EOF && err == NULL)
7581 err = got_error_from_errno2("fclose", path1);
7582 if (f2 && fclose(f2) == EOF && err == NULL)
7583 err = got_error_from_errno2("fclose", path2);
7584 if (outfile && fclose(outfile) == EOF && err == NULL)
7585 err = got_error_from_errno2("fclose", *path_unstaged_content);
7586 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7587 err = got_error_from_errno2("fclose", *path_new_staged_content);
7588 if (path1 && unlink(path1) == -1 && err == NULL)
7589 err = got_error_from_errno2("unlink", path1);
7590 if (path2 && unlink(path2) == -1 && err == NULL)
7591 err = got_error_from_errno2("unlink", path2);
7592 if (err || !have_content) {
7593 if (*path_unstaged_content &&
7594 unlink(*path_unstaged_content) == -1 && err == NULL)
7595 err = got_error_from_errno2("unlink",
7596 *path_unstaged_content);
7597 free(*path_unstaged_content);
7598 *path_unstaged_content = NULL;
7600 if (err || !have_content || !have_rejected_content) {
7601 if (*path_new_staged_content &&
7602 unlink(*path_new_staged_content) == -1 && err == NULL)
7603 err = got_error_from_errno2("unlink",
7604 *path_new_staged_content);
7605 free(*path_new_staged_content);
7606 *path_new_staged_content = NULL;
7608 free(path1);
7609 free(path2);
7610 return err;
7613 static const struct got_error *
7614 unstage_hunks(struct got_object_id *staged_blob_id,
7615 struct got_blob_object *blob_base,
7616 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7617 const char *ondisk_path, const char *label_orig,
7618 struct got_worktree *worktree, struct got_repository *repo,
7619 got_worktree_patch_cb patch_cb, void *patch_arg,
7620 got_worktree_checkout_cb progress_cb, void *progress_arg)
7622 const struct got_error *err = NULL;
7623 char *path_unstaged_content = NULL;
7624 char *path_new_staged_content = NULL;
7625 struct got_object_id *new_staged_blob_id = NULL;
7626 FILE *f = NULL;
7627 struct stat sb;
7629 err = create_unstaged_content(&path_unstaged_content,
7630 &path_new_staged_content, blob_id, staged_blob_id,
7631 ie->path, repo, patch_cb, patch_arg);
7632 if (err)
7633 return err;
7635 if (path_unstaged_content == NULL)
7636 return NULL;
7638 if (path_new_staged_content) {
7639 err = got_object_blob_create(&new_staged_blob_id,
7640 path_new_staged_content, repo);
7641 if (err)
7642 goto done;
7645 f = fopen(path_unstaged_content, "r");
7646 if (f == NULL) {
7647 err = got_error_from_errno2("fopen",
7648 path_unstaged_content);
7649 goto done;
7651 if (fstat(fileno(f), &sb) == -1) {
7652 err = got_error_from_errno2("fstat", path_unstaged_content);
7653 goto done;
7655 if (got_fileindex_entry_staged_filetype_get(ie) ==
7656 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7657 char link_target[PATH_MAX];
7658 size_t r;
7659 r = fread(link_target, 1, sizeof(link_target), f);
7660 if (r == 0 && ferror(f)) {
7661 err = got_error_from_errno("fread");
7662 goto done;
7664 if (r >= sizeof(link_target)) { /* should not happen */
7665 err = got_error(GOT_ERR_NO_SPACE);
7666 goto done;
7668 link_target[r] = '\0';
7669 err = merge_symlink(worktree, blob_base,
7670 ondisk_path, ie->path, label_orig, link_target,
7671 worktree->base_commit_id, repo, progress_cb,
7672 progress_arg);
7673 } else {
7674 int local_changes_subsumed;
7675 err = merge_file(&local_changes_subsumed, worktree,
7676 blob_base, ondisk_path, ie->path,
7677 got_fileindex_perms_to_st(ie),
7678 path_unstaged_content, label_orig, "unstaged",
7679 repo, progress_cb, progress_arg);
7681 if (err)
7682 goto done;
7684 if (new_staged_blob_id) {
7685 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7686 SHA1_DIGEST_LENGTH);
7687 } else {
7688 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7689 got_fileindex_entry_staged_filetype_set(ie, 0);
7691 done:
7692 free(new_staged_blob_id);
7693 if (path_unstaged_content &&
7694 unlink(path_unstaged_content) == -1 && err == NULL)
7695 err = got_error_from_errno2("unlink", path_unstaged_content);
7696 if (path_new_staged_content &&
7697 unlink(path_new_staged_content) == -1 && err == NULL)
7698 err = got_error_from_errno2("unlink", path_new_staged_content);
7699 if (f && fclose(f) == EOF && err == NULL)
7700 err = got_error_from_errno2("fclose", path_unstaged_content);
7701 free(path_unstaged_content);
7702 free(path_new_staged_content);
7703 return err;
7706 static const struct got_error *
7707 unstage_path(void *arg, unsigned char status,
7708 unsigned char staged_status, const char *relpath,
7709 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7710 struct got_object_id *commit_id, int dirfd, const char *de_name)
7712 const struct got_error *err = NULL;
7713 struct unstage_path_arg *a = arg;
7714 struct got_fileindex_entry *ie;
7715 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7716 char *ondisk_path = NULL;
7717 char *id_str = NULL, *label_orig = NULL;
7718 int local_changes_subsumed;
7719 struct stat sb;
7721 if (staged_status != GOT_STATUS_ADD &&
7722 staged_status != GOT_STATUS_MODIFY &&
7723 staged_status != GOT_STATUS_DELETE)
7724 return NULL;
7726 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7727 if (ie == NULL)
7728 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7730 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7731 == -1)
7732 return got_error_from_errno("asprintf");
7734 err = got_object_id_str(&id_str,
7735 commit_id ? commit_id : a->worktree->base_commit_id);
7736 if (err)
7737 goto done;
7738 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7739 id_str) == -1) {
7740 err = got_error_from_errno("asprintf");
7741 goto done;
7744 switch (staged_status) {
7745 case GOT_STATUS_MODIFY:
7746 err = got_object_open_as_blob(&blob_base, a->repo,
7747 blob_id, 8192);
7748 if (err)
7749 break;
7750 /* fall through */
7751 case GOT_STATUS_ADD:
7752 if (a->patch_cb) {
7753 if (staged_status == GOT_STATUS_ADD) {
7754 int choice = GOT_PATCH_CHOICE_NONE;
7755 err = (*a->patch_cb)(&choice, a->patch_arg,
7756 staged_status, ie->path, NULL, 1, 1);
7757 if (err)
7758 break;
7759 if (choice != GOT_PATCH_CHOICE_YES)
7760 break;
7761 } else {
7762 err = unstage_hunks(staged_blob_id,
7763 blob_base, blob_id, ie, ondisk_path,
7764 label_orig, a->worktree, a->repo,
7765 a->patch_cb, a->patch_arg,
7766 a->progress_cb, a->progress_arg);
7767 break; /* Done with this file. */
7770 err = got_object_open_as_blob(&blob_staged, a->repo,
7771 staged_blob_id, 8192);
7772 if (err)
7773 break;
7774 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7775 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7776 case GOT_FILEIDX_MODE_REGULAR_FILE:
7777 err = merge_blob(&local_changes_subsumed, a->worktree,
7778 blob_base, ondisk_path, relpath,
7779 got_fileindex_perms_to_st(ie), label_orig,
7780 blob_staged, commit_id ? commit_id :
7781 a->worktree->base_commit_id, a->repo,
7782 a->progress_cb, a->progress_arg);
7783 break;
7784 case GOT_FILEIDX_MODE_SYMLINK:
7785 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7786 char *staged_target;
7787 err = got_object_blob_read_to_str(
7788 &staged_target, blob_staged);
7789 if (err)
7790 goto done;
7791 err = merge_symlink(a->worktree, blob_base,
7792 ondisk_path, relpath, label_orig,
7793 staged_target, commit_id ? commit_id :
7794 a->worktree->base_commit_id,
7795 a->repo, a->progress_cb, a->progress_arg);
7796 free(staged_target);
7797 } else {
7798 err = merge_blob(&local_changes_subsumed,
7799 a->worktree, blob_base, ondisk_path,
7800 relpath, got_fileindex_perms_to_st(ie),
7801 label_orig, blob_staged,
7802 commit_id ? commit_id :
7803 a->worktree->base_commit_id, a->repo,
7804 a->progress_cb, a->progress_arg);
7806 break;
7807 default:
7808 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7809 break;
7811 if (err == NULL) {
7812 got_fileindex_entry_stage_set(ie,
7813 GOT_FILEIDX_STAGE_NONE);
7814 got_fileindex_entry_staged_filetype_set(ie, 0);
7816 break;
7817 case GOT_STATUS_DELETE:
7818 if (a->patch_cb) {
7819 int choice = GOT_PATCH_CHOICE_NONE;
7820 err = (*a->patch_cb)(&choice, a->patch_arg,
7821 staged_status, ie->path, NULL, 1, 1);
7822 if (err)
7823 break;
7824 if (choice == GOT_PATCH_CHOICE_NO)
7825 break;
7826 if (choice != GOT_PATCH_CHOICE_YES) {
7827 err = got_error(GOT_ERR_PATCH_CHOICE);
7828 break;
7831 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7832 got_fileindex_entry_staged_filetype_set(ie, 0);
7833 err = get_file_status(&status, &sb, ie, ondisk_path,
7834 dirfd, de_name, a->repo);
7835 if (err)
7836 break;
7837 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7838 break;
7840 done:
7841 free(ondisk_path);
7842 if (blob_base)
7843 got_object_blob_close(blob_base);
7844 if (blob_staged)
7845 got_object_blob_close(blob_staged);
7846 free(id_str);
7847 free(label_orig);
7848 return err;
7851 const struct got_error *
7852 got_worktree_unstage(struct got_worktree *worktree,
7853 struct got_pathlist_head *paths,
7854 got_worktree_checkout_cb progress_cb, void *progress_arg,
7855 got_worktree_patch_cb patch_cb, void *patch_arg,
7856 struct got_repository *repo)
7858 const struct got_error *err = NULL, *sync_err, *unlockerr;
7859 struct got_pathlist_entry *pe;
7860 struct got_fileindex *fileindex = NULL;
7861 char *fileindex_path = NULL;
7862 struct unstage_path_arg upa;
7864 err = lock_worktree(worktree, LOCK_EX);
7865 if (err)
7866 return err;
7868 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7869 if (err)
7870 goto done;
7872 upa.worktree = worktree;
7873 upa.fileindex = fileindex;
7874 upa.repo = repo;
7875 upa.progress_cb = progress_cb;
7876 upa.progress_arg = progress_arg;
7877 upa.patch_cb = patch_cb;
7878 upa.patch_arg = patch_arg;
7879 TAILQ_FOREACH(pe, paths, entry) {
7880 err = worktree_status(worktree, pe->path, fileindex, repo,
7881 unstage_path, &upa, NULL, NULL, 0, 0);
7882 if (err)
7883 goto done;
7886 sync_err = sync_fileindex(fileindex, fileindex_path);
7887 if (sync_err && err == NULL)
7888 err = sync_err;
7889 done:
7890 free(fileindex_path);
7891 if (fileindex)
7892 got_fileindex_free(fileindex);
7893 unlockerr = lock_worktree(worktree, LOCK_SH);
7894 if (unlockerr && err == NULL)
7895 err = unlockerr;
7896 return err;
7899 struct report_file_info_arg {
7900 struct got_worktree *worktree;
7901 got_worktree_path_info_cb info_cb;
7902 void *info_arg;
7903 struct got_pathlist_head *paths;
7904 got_cancel_cb cancel_cb;
7905 void *cancel_arg;
7908 static const struct got_error *
7909 report_file_info(void *arg, struct got_fileindex_entry *ie)
7911 struct report_file_info_arg *a = arg;
7912 struct got_pathlist_entry *pe;
7913 struct got_object_id blob_id, staged_blob_id, commit_id;
7914 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7915 struct got_object_id *commit_idp = NULL;
7916 int stage;
7918 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7919 return got_error(GOT_ERR_CANCELLED);
7921 TAILQ_FOREACH(pe, a->paths, entry) {
7922 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7923 got_path_is_child(ie->path, pe->path, pe->path_len))
7924 break;
7926 if (pe == NULL) /* not found */
7927 return NULL;
7929 if (got_fileindex_entry_has_blob(ie)) {
7930 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7931 blob_idp = &blob_id;
7933 stage = got_fileindex_entry_stage_get(ie);
7934 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7935 stage == GOT_FILEIDX_STAGE_ADD) {
7936 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7937 SHA1_DIGEST_LENGTH);
7938 staged_blob_idp = &staged_blob_id;
7941 if (got_fileindex_entry_has_commit(ie)) {
7942 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7943 commit_idp = &commit_id;
7946 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7947 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7950 const struct got_error *
7951 got_worktree_path_info(struct got_worktree *worktree,
7952 struct got_pathlist_head *paths,
7953 got_worktree_path_info_cb info_cb, void *info_arg,
7954 got_cancel_cb cancel_cb, void *cancel_arg)
7957 const struct got_error *err = NULL, *unlockerr;
7958 struct got_fileindex *fileindex = NULL;
7959 char *fileindex_path = NULL;
7960 struct report_file_info_arg arg;
7962 err = lock_worktree(worktree, LOCK_SH);
7963 if (err)
7964 return err;
7966 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7967 if (err)
7968 goto done;
7970 arg.worktree = worktree;
7971 arg.info_cb = info_cb;
7972 arg.info_arg = info_arg;
7973 arg.paths = paths;
7974 arg.cancel_cb = cancel_cb;
7975 arg.cancel_arg = cancel_arg;
7976 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7977 &arg);
7978 done:
7979 free(fileindex_path);
7980 if (fileindex)
7981 got_fileindex_free(fileindex);
7982 unlockerr = lock_worktree(worktree, LOCK_UN);
7983 if (unlockerr && err == NULL)
7984 err = unlockerr;
7985 return err;