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;
503 if (worktree->lockfd != -1) {
504 if (close(worktree->lockfd) == -1)
505 err = got_error_from_errno2("close",
506 got_worktree_get_root_path(worktree));
508 if (close(worktree->root_fd) == -1 && err == NULL)
509 err = got_error_from_errno2("close",
510 got_worktree_get_root_path(worktree));
511 free(worktree->repo_path);
512 free(worktree->path_prefix);
513 free(worktree->base_commit_id);
514 free(worktree->head_ref_name);
515 free(worktree->root_path);
516 free(worktree->gotconfig_path);
517 got_gotconfig_free(worktree->gotconfig);
518 free(worktree);
519 return err;
522 const char *
523 got_worktree_get_root_path(struct got_worktree *worktree)
525 return worktree->root_path;
528 const char *
529 got_worktree_get_repo_path(struct got_worktree *worktree)
531 return worktree->repo_path;
533 const char *
534 got_worktree_get_path_prefix(struct got_worktree *worktree)
536 return worktree->path_prefix;
539 const struct got_error *
540 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 const char *path_prefix)
543 char *absprefix = NULL;
545 if (!got_path_is_absolute(path_prefix)) {
546 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 return got_error_from_errno("asprintf");
549 *match = (strcmp(absprefix ? absprefix : path_prefix,
550 worktree->path_prefix) == 0);
551 free(absprefix);
552 return NULL;
555 const char *
556 got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 return worktree->head_ref_name;
561 const struct got_error *
562 got_worktree_set_head_ref(struct got_worktree *worktree,
563 struct got_reference *head_ref)
565 const struct got_error *err = NULL;
566 char *path_got = NULL, *head_ref_name = NULL;
568 if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 GOT_WORKTREE_GOT_DIR) == -1) {
570 err = got_error_from_errno("asprintf");
571 path_got = NULL;
572 goto done;
575 head_ref_name = strdup(got_ref_get_name(head_ref));
576 if (head_ref_name == NULL) {
577 err = got_error_from_errno("strdup");
578 goto done;
581 err = write_head_ref(path_got, head_ref);
582 if (err)
583 goto done;
585 free(worktree->head_ref_name);
586 worktree->head_ref_name = head_ref_name;
587 done:
588 free(path_got);
589 if (err)
590 free(head_ref_name);
591 return err;
594 struct got_object_id *
595 got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 return worktree->base_commit_id;
600 const struct got_error *
601 got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 struct got_repository *repo, struct got_object_id *commit_id)
604 const struct got_error *err;
605 struct got_object *obj = NULL;
606 char *id_str = NULL;
607 char *path_got = NULL;
609 if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 GOT_WORKTREE_GOT_DIR) == -1) {
611 err = got_error_from_errno("asprintf");
612 path_got = NULL;
613 goto done;
616 err = got_object_open(&obj, repo, commit_id);
617 if (err)
618 return err;
620 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 err = got_error(GOT_ERR_OBJ_TYPE);
622 goto done;
625 /* Record our base commit. */
626 err = got_object_id_str(&id_str, commit_id);
627 if (err)
628 goto done;
629 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 if (err)
631 goto done;
633 free(worktree->base_commit_id);
634 worktree->base_commit_id = got_object_id_dup(commit_id);
635 if (worktree->base_commit_id == NULL) {
636 err = got_error_from_errno("got_object_id_dup");
637 goto done;
639 done:
640 if (obj)
641 got_object_close(obj);
642 free(id_str);
643 free(path_got);
644 return err;
647 const struct got_gotconfig *
648 got_worktree_get_gotconfig(struct got_worktree *worktree)
650 return worktree->gotconfig;
653 static const struct got_error *
654 lock_worktree(struct got_worktree *worktree, int operation)
656 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 : got_error_from_errno2("flock",
659 got_worktree_get_root_path(worktree)));
660 return NULL;
663 static const struct got_error *
664 add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 const struct got_error *err = NULL;
667 char *abspath;
669 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 return got_error_from_errno("asprintf");
672 err = got_path_mkdir(abspath);
673 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
674 struct stat sb;
675 err = NULL;
676 if (lstat(abspath, &sb) == -1) {
677 err = got_error_from_errno2("lstat", abspath);
678 } else if (!S_ISDIR(sb.st_mode)) {
679 /* TODO directory is obstructed; do something */
680 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
683 free(abspath);
684 return err;
687 static const struct got_error *
688 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 const struct got_error *err = NULL;
691 uint8_t fbuf1[8192];
692 uint8_t fbuf2[8192];
693 size_t flen1 = 0, flen2 = 0;
695 *same = 1;
697 for (;;) {
698 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 if (flen1 == 0 && ferror(f1)) {
700 err = got_error_from_errno("fread");
701 break;
703 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 if (flen2 == 0 && ferror(f2)) {
705 err = got_error_from_errno("fread");
706 break;
708 if (flen1 == 0) {
709 if (flen2 != 0)
710 *same = 0;
711 break;
712 } else if (flen2 == 0) {
713 if (flen1 != 0)
714 *same = 0;
715 break;
716 } else if (flen1 == flen2) {
717 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
718 *same = 0;
719 break;
721 } else {
722 *same = 0;
723 break;
727 return err;
730 static const struct got_error *
731 check_files_equal(int *same, const char *f1_path, const char *f2_path)
733 const struct got_error *err = NULL;
734 struct stat sb;
735 size_t size1, size2;
736 FILE *f1 = NULL, *f2 = NULL;
738 *same = 1;
740 if (lstat(f1_path, &sb) != 0) {
741 err = got_error_from_errno2("lstat", f1_path);
742 goto done;
744 size1 = sb.st_size;
746 if (lstat(f2_path, &sb) != 0) {
747 err = got_error_from_errno2("lstat", f2_path);
748 goto done;
750 size2 = sb.st_size;
752 if (size1 != size2) {
753 *same = 0;
754 return NULL;
757 f1 = fopen(f1_path, "r");
758 if (f1 == NULL)
759 return got_error_from_errno2("fopen", f1_path);
761 f2 = fopen(f2_path, "r");
762 if (f2 == NULL) {
763 err = got_error_from_errno2("fopen", f2_path);
764 goto done;
767 err = check_file_contents_equal(same, f1, f2);
768 done:
769 if (f1 && fclose(f1) == EOF && err == NULL)
770 err = got_error_from_errno("fclose");
771 if (f2 && fclose(f2) == EOF && err == NULL)
772 err = got_error_from_errno("fclose");
774 return err;
777 /*
778 * Perform a 3-way merge where blob_orig acts as the common ancestor,
779 * the file at deriv_path acts as the first derived version, and the
780 * file on disk acts as the second derived version.
781 */
782 static const struct got_error *
783 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
784 struct got_blob_object *blob_orig, const char *ondisk_path,
785 const char *path, uint16_t st_mode, const char *deriv_path,
786 const char *label_orig, const char *label_deriv,
787 struct got_repository *repo,
788 got_worktree_checkout_cb progress_cb, void *progress_arg)
790 const struct got_error *err = NULL;
791 int merged_fd = -1;
792 FILE *f_orig = NULL;
793 char *blob_orig_path = NULL;
794 char *merged_path = NULL, *base_path = NULL;
795 int overlapcnt = 0;
796 char *parent = NULL;
797 char *symlink_path = NULL;
798 FILE *symlinkf = NULL;
800 *local_changes_subsumed = 0;
802 err = got_path_dirname(&parent, ondisk_path);
803 if (err)
804 return err;
806 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
807 err = got_error_from_errno("asprintf");
808 goto done;
811 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
812 if (err)
813 goto done;
815 free(base_path);
816 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
817 err = got_error_from_errno("asprintf");
818 base_path = NULL;
819 goto done;
822 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
823 if (err)
824 goto done;
825 if (blob_orig) {
826 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
827 blob_orig);
828 if (err)
829 goto done;
830 } else {
831 /*
832 * If the file has no blob, this is an "add vs add" conflict,
833 * and we simply use an empty ancestor file to make both files
834 * appear in the merged result in their entirety.
835 */
838 /*
839 * In order the run a 3-way merge with a symlink we copy the symlink's
840 * target path into a temporary file and use that file with diff3.
841 */
842 if (S_ISLNK(st_mode)) {
843 char target_path[PATH_MAX];
844 ssize_t target_len;
845 size_t n;
847 free(base_path);
848 if (asprintf(&base_path, "%s/got-symlink-merge",
849 parent) == -1) {
850 err = got_error_from_errno("asprintf");
851 base_path = NULL;
852 goto done;
854 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
855 if (err)
856 goto done;
857 target_len = readlink(ondisk_path, target_path,
858 sizeof(target_path));
859 if (target_len == -1) {
860 err = got_error_from_errno2("readlink", ondisk_path);
861 goto done;
863 n = fwrite(target_path, 1, target_len, symlinkf);
864 if (n != target_len) {
865 err = got_ferror(symlinkf, GOT_ERR_IO);
866 goto done;
868 if (fflush(symlinkf) == EOF) {
869 err = got_error_from_errno2("fflush", symlink_path);
870 goto done;
874 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
875 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
876 label_deriv, label_orig, NULL);
877 if (err)
878 goto done;
880 err = (*progress_cb)(progress_arg,
881 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
882 if (err)
883 goto done;
885 if (fsync(merged_fd) != 0) {
886 err = got_error_from_errno("fsync");
887 goto done;
890 /* Check if a clean merge has subsumed all local changes. */
891 if (overlapcnt == 0) {
892 err = check_files_equal(local_changes_subsumed, deriv_path,
893 merged_path);
894 if (err)
895 goto done;
898 if (fchmod(merged_fd, st_mode) != 0) {
899 err = got_error_from_errno2("fchmod", merged_path);
900 goto done;
903 if (rename(merged_path, ondisk_path) != 0) {
904 err = got_error_from_errno3("rename", merged_path,
905 ondisk_path);
906 goto done;
908 done:
909 if (err) {
910 if (merged_path)
911 unlink(merged_path);
913 if (symlink_path) {
914 if (unlink(symlink_path) == -1 && err == NULL)
915 err = got_error_from_errno2("unlink", symlink_path);
917 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
918 err = got_error_from_errno2("fclose", symlink_path);
919 free(symlink_path);
920 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
921 err = got_error_from_errno("close");
922 if (f_orig && fclose(f_orig) == EOF && err == NULL)
923 err = got_error_from_errno("fclose");
924 free(merged_path);
925 free(base_path);
926 if (blob_orig_path) {
927 unlink(blob_orig_path);
928 free(blob_orig_path);
930 free(parent);
931 return err;
934 static const struct got_error *
935 update_symlink(const char *ondisk_path, const char *target_path,
936 size_t target_len)
938 /* This is not atomic but matches what 'ln -sf' does. */
939 if (unlink(ondisk_path) == -1)
940 return got_error_from_errno2("unlink", ondisk_path);
941 if (symlink(target_path, ondisk_path) == -1)
942 return got_error_from_errno3("symlink", target_path,
943 ondisk_path);
944 return NULL;
947 /*
948 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
949 * in the work tree with a file that contains conflict markers and the
950 * conflicting target paths of the original version, a "derived version"
951 * of a symlink from an incoming change, and a local version of the symlink.
953 * The original versions's target path can be NULL if it is not available,
954 * such as if both derived versions added a new symlink at the same path.
956 * The incoming derived symlink target is NULL in case the incoming change
957 * has deleted this symlink.
958 */
959 static const struct got_error *
960 install_symlink_conflict(const char *deriv_target,
961 struct got_object_id *deriv_base_commit_id, const char *orig_target,
962 const char *label_orig, const char *local_target, const char *ondisk_path)
964 const struct got_error *err;
965 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
966 FILE *f = NULL;
968 err = got_object_id_str(&id_str, deriv_base_commit_id);
969 if (err)
970 return got_error_from_errno("asprintf");
972 if (asprintf(&label_deriv, "%s: commit %s",
973 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
974 err = got_error_from_errno("asprintf");
975 goto done;
978 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
979 if (err)
980 goto done;
982 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
983 err = got_error_from_errno2("fchmod", path);
984 goto done;
987 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
988 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
989 deriv_target ? deriv_target : "(symlink was deleted)",
990 orig_target ? label_orig : "",
991 orig_target ? "\n" : "",
992 orig_target ? orig_target : "",
993 orig_target ? "\n" : "",
994 GOT_DIFF_CONFLICT_MARKER_SEP,
995 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
996 err = got_error_from_errno2("fprintf", path);
997 goto done;
1000 if (unlink(ondisk_path) == -1) {
1001 err = got_error_from_errno2("unlink", ondisk_path);
1002 goto done;
1004 if (rename(path, ondisk_path) == -1) {
1005 err = got_error_from_errno3("rename", path, ondisk_path);
1006 goto done;
1008 done:
1009 if (f != NULL && fclose(f) == EOF && err == NULL)
1010 err = got_error_from_errno2("fclose", path);
1011 free(path);
1012 free(id_str);
1013 free(label_deriv);
1014 return err;
1017 /* forward declaration */
1018 static const struct got_error *
1019 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1020 const char *, const char *, uint16_t, const char *,
1021 struct got_blob_object *, struct got_object_id *,
1022 struct got_repository *, got_worktree_checkout_cb, void *);
1025 * Merge a symlink into the work tree, where blob_orig acts as the common
1026 * ancestor, deriv_target is the link target of the first derived version,
1027 * and the symlink on disk acts as the second derived version.
1028 * Assume that contents of both blobs represent symlinks.
1030 static const struct got_error *
1031 merge_symlink(struct got_worktree *worktree,
1032 struct got_blob_object *blob_orig, const char *ondisk_path,
1033 const char *path, const char *label_orig, const char *deriv_target,
1034 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1035 got_worktree_checkout_cb progress_cb, void *progress_arg)
1037 const struct got_error *err = NULL;
1038 char *ancestor_target = NULL;
1039 struct stat sb;
1040 ssize_t ondisk_len, deriv_len;
1041 char ondisk_target[PATH_MAX];
1042 int have_local_change = 0;
1043 int have_incoming_change = 0;
1045 if (lstat(ondisk_path, &sb) == -1)
1046 return got_error_from_errno2("lstat", ondisk_path);
1048 ondisk_len = readlink(ondisk_path, ondisk_target,
1049 sizeof(ondisk_target));
1050 if (ondisk_len == -1) {
1051 err = got_error_from_errno2("readlink",
1052 ondisk_path);
1053 goto done;
1055 ondisk_target[ondisk_len] = '\0';
1057 if (blob_orig) {
1058 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1059 if (err)
1060 goto done;
1063 if (ancestor_target == NULL ||
1064 (ondisk_len != strlen(ancestor_target) ||
1065 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1066 have_local_change = 1;
1068 deriv_len = strlen(deriv_target);
1069 if (ancestor_target == NULL ||
1070 (deriv_len != strlen(ancestor_target) ||
1071 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1072 have_incoming_change = 1;
1074 if (!have_local_change && !have_incoming_change) {
1075 if (ancestor_target) {
1076 /* Both sides made the same change. */
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1078 path);
1079 } else if (deriv_len == ondisk_len &&
1080 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1081 /* Both sides added the same symlink. */
1082 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1083 path);
1084 } else {
1085 /* Both sides added symlinks which don't match. */
1086 err = install_symlink_conflict(deriv_target,
1087 deriv_base_commit_id, ancestor_target,
1088 label_orig, ondisk_target, ondisk_path);
1089 if (err)
1090 goto done;
1091 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1092 path);
1094 } else if (!have_local_change && have_incoming_change) {
1095 /* Apply the incoming change. */
1096 err = update_symlink(ondisk_path, deriv_target,
1097 strlen(deriv_target));
1098 if (err)
1099 goto done;
1100 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1101 } else if (have_local_change && have_incoming_change) {
1102 if (deriv_len == ondisk_len &&
1103 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1104 /* Both sides made the same change. */
1105 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1106 path);
1107 } else {
1108 err = install_symlink_conflict(deriv_target,
1109 deriv_base_commit_id, ancestor_target, label_orig,
1110 ondisk_target, ondisk_path);
1111 if (err)
1112 goto done;
1113 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1114 path);
1118 done:
1119 free(ancestor_target);
1120 return err;
1124 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1125 * blob_deriv acts as the first derived version, and the file on disk
1126 * acts as the second derived version.
1128 static const struct got_error *
1129 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1130 struct got_blob_object *blob_orig, const char *ondisk_path,
1131 const char *path, uint16_t st_mode, const char *label_orig,
1132 struct got_blob_object *blob_deriv,
1133 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg)
1136 const struct got_error *err = NULL;
1137 FILE *f_deriv = NULL;
1138 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1139 char *label_deriv = NULL, *parent = NULL;
1141 *local_changes_subsumed = 0;
1143 err = got_path_dirname(&parent, ondisk_path);
1144 if (err)
1145 return err;
1147 free(base_path);
1148 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1149 err = got_error_from_errno("asprintf");
1150 base_path = NULL;
1151 goto done;
1154 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1155 if (err)
1156 goto done;
1157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1158 blob_deriv);
1159 if (err)
1160 goto done;
1162 err = got_object_id_str(&id_str, deriv_base_commit_id);
1163 if (err)
1164 goto done;
1165 if (asprintf(&label_deriv, "%s: commit %s",
1166 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1167 err = got_error_from_errno("asprintf");
1168 goto done;
1171 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1172 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1173 label_deriv, repo, progress_cb, progress_arg);
1174 done:
1175 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1176 err = got_error_from_errno("fclose");
1177 free(base_path);
1178 if (blob_deriv_path) {
1179 unlink(blob_deriv_path);
1180 free(blob_deriv_path);
1182 free(id_str);
1183 free(label_deriv);
1184 free(parent);
1185 return err;
1188 static const struct got_error *
1189 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1190 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1191 int wt_fd, const char *path, struct got_object_id *blob_id)
1193 const struct got_error *err = NULL;
1194 struct got_fileindex_entry *new_ie;
1196 *new_iep = NULL;
1198 err = got_fileindex_entry_alloc(&new_ie, path);
1199 if (err)
1200 return err;
1202 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1203 blob_id->sha1, base_commit_id->sha1, 1);
1204 if (err)
1205 goto done;
1207 err = got_fileindex_entry_add(fileindex, new_ie);
1208 done:
1209 if (err)
1210 got_fileindex_entry_free(new_ie);
1211 else
1212 *new_iep = new_ie;
1213 return err;
1216 static mode_t
1217 get_ondisk_perms(int executable, mode_t st_mode)
1219 mode_t xbits = S_IXUSR;
1221 if (executable) {
1222 /* Map read bits to execute bits. */
1223 if (st_mode & S_IRGRP)
1224 xbits |= S_IXGRP;
1225 if (st_mode & S_IROTH)
1226 xbits |= S_IXOTH;
1227 return st_mode | xbits;
1230 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1233 /* forward declaration */
1234 static const struct got_error *
1235 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1236 const char *path, mode_t te_mode, mode_t st_mode,
1237 struct got_blob_object *blob, int restoring_missing_file,
1238 int reverting_versioned_file, int installing_bad_symlink,
1239 int path_is_unversioned, struct got_repository *repo,
1240 got_worktree_checkout_cb progress_cb, void *progress_arg);
1243 * This function assumes that the provided symlink target points at a
1244 * safe location in the work tree!
1246 static const struct got_error *
1247 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1248 size_t target_len)
1250 const struct got_error *err = NULL;
1251 ssize_t elen;
1252 char etarget[PATH_MAX];
1253 int fd;
1256 * "Bad" symlinks (those pointing outside the work tree or into the
1257 * .got directory) are installed in the work tree as a regular file
1258 * which contains the bad symlink target path.
1259 * The new symlink target has already been checked for safety by our
1260 * caller. If we can successfully open a regular file then we simply
1261 * replace this file with a symlink below.
1263 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1264 if (fd == -1) {
1265 if (errno != ELOOP)
1266 return got_error_from_errno2("open", ondisk_path);
1268 /* We are updating an existing on-disk symlink. */
1269 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1270 if (elen == -1)
1271 return got_error_from_errno2("readlink", ondisk_path);
1273 if (elen == target_len &&
1274 memcmp(etarget, target_path, target_len) == 0)
1275 return NULL; /* nothing to do */
1278 err = update_symlink(ondisk_path, target_path, target_len);
1279 if (fd != -1 && close(fd) == -1 && err == NULL)
1280 err = got_error_from_errno2("close", ondisk_path);
1281 return err;
1284 static const struct got_error *
1285 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1286 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1288 const struct got_error *err = NULL;
1289 char canonpath[PATH_MAX];
1290 char *path_got = NULL;
1292 *is_bad_symlink = 0;
1294 if (target_len >= sizeof(canonpath)) {
1295 *is_bad_symlink = 1;
1296 return NULL;
1300 * We do not use realpath(3) to resolve the symlink's target
1301 * path because we don't want to resolve symlinks recursively.
1302 * Instead we make the path absolute and then canonicalize it.
1303 * Relative symlink target lookup should begin at the directory
1304 * in which the blob object is being installed.
1306 if (!got_path_is_absolute(target_path)) {
1307 char *abspath, *parent;
1308 err = got_path_dirname(&parent, ondisk_path);
1309 if (err)
1310 return err;
1311 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1312 free(parent);
1313 return got_error_from_errno("asprintf");
1315 free(parent);
1316 if (strlen(abspath) >= sizeof(canonpath)) {
1317 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1318 free(abspath);
1319 return err;
1321 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1322 free(abspath);
1323 if (err)
1324 return err;
1325 } else {
1326 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1327 if (err)
1328 return err;
1331 /* Only allow symlinks pointing at paths within the work tree. */
1332 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1333 *is_bad_symlink = 1;
1334 return NULL;
1337 /* Do not allow symlinks pointing into the .got directory. */
1338 if (asprintf(&path_got, "%s/%s", wtroot_path,
1339 GOT_WORKTREE_GOT_DIR) == -1)
1340 return got_error_from_errno("asprintf");
1341 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1342 *is_bad_symlink = 1;
1344 free(path_got);
1345 return NULL;
1348 static const struct got_error *
1349 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1350 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1351 int restoring_missing_file, int reverting_versioned_file,
1352 int path_is_unversioned, struct got_repository *repo,
1353 got_worktree_checkout_cb progress_cb, void *progress_arg)
1355 const struct got_error *err = NULL;
1356 char target_path[PATH_MAX];
1357 size_t len, target_len = 0;
1358 char *path_got = NULL;
1359 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1360 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1362 *is_bad_symlink = 0;
1365 * Blob object content specifies the target path of the link.
1366 * If a symbolic link cannot be installed we instead create
1367 * a regular file which contains the link target path stored
1368 * in the blob object.
1370 do {
1371 err = got_object_blob_read_block(&len, blob);
1372 if (len + target_len >= sizeof(target_path)) {
1373 /* Path too long; install as a regular file. */
1374 *is_bad_symlink = 1;
1375 got_object_blob_rewind(blob);
1376 return install_blob(worktree, ondisk_path, path,
1377 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1378 restoring_missing_file, reverting_versioned_file,
1379 1, path_is_unversioned, repo, progress_cb,
1380 progress_arg);
1382 if (len > 0) {
1383 /* Skip blob object header first time around. */
1384 memcpy(target_path + target_len, buf + hdrlen,
1385 len - hdrlen);
1386 target_len += len - hdrlen;
1387 hdrlen = 0;
1389 } while (len != 0);
1390 target_path[target_len] = '\0';
1392 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1393 ondisk_path, worktree->root_path);
1394 if (err)
1395 return err;
1397 if (*is_bad_symlink) {
1398 /* install as a regular file */
1399 *is_bad_symlink = 1;
1400 got_object_blob_rewind(blob);
1401 err = install_blob(worktree, ondisk_path, path,
1402 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1403 restoring_missing_file, reverting_versioned_file, 1,
1404 path_is_unversioned, repo, progress_cb, progress_arg);
1405 goto done;
1408 if (symlink(target_path, ondisk_path) == -1) {
1409 if (errno == EEXIST) {
1410 if (path_is_unversioned) {
1411 err = (*progress_cb)(progress_arg,
1412 GOT_STATUS_UNVERSIONED, path);
1413 goto done;
1415 err = replace_existing_symlink(ondisk_path,
1416 target_path, target_len);
1417 if (err)
1418 goto done;
1419 if (progress_cb) {
1420 err = (*progress_cb)(progress_arg,
1421 reverting_versioned_file ?
1422 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1423 path);
1425 goto done; /* Nothing else to do. */
1428 if (errno == ENOENT) {
1429 char *parent;
1430 err = got_path_dirname(&parent, ondisk_path);
1431 if (err)
1432 goto done;
1433 err = add_dir_on_disk(worktree, parent);
1434 free(parent);
1435 if (err)
1436 goto done;
1438 * Retry, and fall through to error handling
1439 * below if this second attempt fails.
1441 if (symlink(target_path, ondisk_path) != -1) {
1442 err = NULL; /* success */
1443 goto done;
1447 /* Handle errors from first or second creation attempt. */
1448 if (errno == ENAMETOOLONG) {
1449 /* bad target path; install as a regular file */
1450 *is_bad_symlink = 1;
1451 got_object_blob_rewind(blob);
1452 err = install_blob(worktree, ondisk_path, path,
1453 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1454 restoring_missing_file, reverting_versioned_file, 1,
1455 path_is_unversioned, repo,
1456 progress_cb, progress_arg);
1457 } else if (errno == ENOTDIR) {
1458 err = got_error_path(ondisk_path,
1459 GOT_ERR_FILE_OBSTRUCTED);
1460 } else {
1461 err = got_error_from_errno3("symlink",
1462 target_path, ondisk_path);
1464 } else if (progress_cb)
1465 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1466 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1467 done:
1468 free(path_got);
1469 return err;
1472 static const struct got_error *
1473 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1474 const char *path, mode_t te_mode, mode_t st_mode,
1475 struct got_blob_object *blob, int restoring_missing_file,
1476 int reverting_versioned_file, int installing_bad_symlink,
1477 int path_is_unversioned, struct got_repository *repo,
1478 got_worktree_checkout_cb progress_cb, void *progress_arg)
1480 const struct got_error *err = NULL;
1481 int fd = -1;
1482 size_t len, hdrlen;
1483 int update = 0;
1484 char *tmppath = NULL;
1486 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1487 GOT_DEFAULT_FILE_MODE);
1488 if (fd == -1) {
1489 if (errno == ENOENT) {
1490 char *parent;
1491 err = got_path_dirname(&parent, path);
1492 if (err)
1493 return err;
1494 err = add_dir_on_disk(worktree, parent);
1495 free(parent);
1496 if (err)
1497 return err;
1498 fd = open(ondisk_path,
1499 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1500 GOT_DEFAULT_FILE_MODE);
1501 if (fd == -1)
1502 return got_error_from_errno2("open",
1503 ondisk_path);
1504 } else if (errno == EEXIST) {
1505 if (path_is_unversioned) {
1506 err = (*progress_cb)(progress_arg,
1507 GOT_STATUS_UNVERSIONED, path);
1508 goto done;
1510 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1511 !S_ISREG(st_mode) && !installing_bad_symlink) {
1512 /* TODO file is obstructed; do something */
1513 err = got_error_path(ondisk_path,
1514 GOT_ERR_FILE_OBSTRUCTED);
1515 goto done;
1516 } else {
1517 err = got_opentemp_named_fd(&tmppath, &fd,
1518 ondisk_path);
1519 if (err)
1520 goto done;
1521 update = 1;
1523 } else
1524 return got_error_from_errno2("open", ondisk_path);
1527 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1528 err = got_error_from_errno2("fchmod",
1529 update ? tmppath : ondisk_path);
1530 goto done;
1533 if (progress_cb) {
1534 if (restoring_missing_file)
1535 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1536 path);
1537 else if (reverting_versioned_file)
1538 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1539 path);
1540 else
1541 err = (*progress_cb)(progress_arg,
1542 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1543 if (err)
1544 goto done;
1547 hdrlen = got_object_blob_get_hdrlen(blob);
1548 do {
1549 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1550 err = got_object_blob_read_block(&len, blob);
1551 if (err)
1552 break;
1553 if (len > 0) {
1554 /* Skip blob object header first time around. */
1555 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1556 if (outlen == -1) {
1557 err = got_error_from_errno("write");
1558 goto done;
1559 } else if (outlen != len - hdrlen) {
1560 err = got_error(GOT_ERR_IO);
1561 goto done;
1563 hdrlen = 0;
1565 } while (len != 0);
1567 if (fsync(fd) != 0) {
1568 err = got_error_from_errno("fsync");
1569 goto done;
1572 if (update) {
1573 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1574 err = got_error_from_errno2("unlink", ondisk_path);
1575 goto done;
1577 if (rename(tmppath, ondisk_path) != 0) {
1578 err = got_error_from_errno3("rename", tmppath,
1579 ondisk_path);
1580 goto done;
1582 free(tmppath);
1583 tmppath = NULL;
1586 done:
1587 if (fd != -1 && close(fd) == -1 && err == NULL)
1588 err = got_error_from_errno("close");
1589 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1590 err = got_error_from_errno2("unlink", tmppath);
1591 free(tmppath);
1592 return err;
1595 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1596 static const struct got_error *
1597 get_modified_file_content_status(unsigned char *status, FILE *f)
1599 const struct got_error *err = NULL;
1600 const char *markers[3] = {
1601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1602 GOT_DIFF_CONFLICT_MARKER_SEP,
1603 GOT_DIFF_CONFLICT_MARKER_END
1605 int i = 0;
1606 char *line = NULL;
1607 size_t linesize = 0;
1608 ssize_t linelen;
1610 while (*status == GOT_STATUS_MODIFY) {
1611 linelen = getline(&line, &linesize, f);
1612 if (linelen == -1) {
1613 if (feof(f))
1614 break;
1615 err = got_ferror(f, GOT_ERR_IO);
1616 break;
1619 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1620 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1621 == 0)
1622 *status = GOT_STATUS_CONFLICT;
1623 else
1624 i++;
1627 free(line);
1629 return err;
1632 static int
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1639 static int
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1660 default:
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1673 ssize_t elen;
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1681 do {
1682 err = got_object_blob_read_block(&len, blob);
1683 if (err)
1684 return err;
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1692 if (len > 0) {
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1695 len - hdrlen);
1696 target_len += len - hdrlen;
1697 hdrlen = 0;
1699 } while (len != 0);
1700 target_path[target_len] = '\0';
1702 if (dirfd != -1) {
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 if (elen == -1)
1705 return got_error_from_errno2("readlinkat", abspath);
1706 } else {
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1708 if (elen == -1)
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1715 return NULL;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1725 size_t hdrlen;
1726 int fd = -1;
1727 FILE *f = NULL;
1728 uint8_t fbuf[8192];
1729 struct got_blob_object *blob = NULL;
1730 size_t flen, blen;
1731 unsigned char staged_status = get_staged_status(ie);
1733 *status = GOT_STATUS_NO_CHANGE;
1736 * Whenever the caller provides a directory descriptor and a
1737 * directory entry name for the file, use them! This prevents
1738 * race conditions if filesystem paths change beneath our feet.
1740 if (dirfd != -1) {
1741 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1742 if (errno == ENOENT) {
1743 if (got_fileindex_entry_has_file_on_disk(ie))
1744 *status = GOT_STATUS_MISSING;
1745 else
1746 *status = GOT_STATUS_DELETE;
1747 goto done;
1749 err = got_error_from_errno2("fstatat", abspath);
1750 goto done;
1752 } else {
1753 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1754 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1755 return got_error_from_errno2("open", abspath);
1756 else if (fd == -1 && errno == ELOOP) {
1757 if (lstat(abspath, sb) == -1)
1758 return got_error_from_errno2("lstat", abspath);
1759 } else if (fd == -1 || fstat(fd, sb) == -1) {
1760 if (errno == ENOENT) {
1761 if (got_fileindex_entry_has_file_on_disk(ie))
1762 *status = GOT_STATUS_MISSING;
1763 else
1764 *status = GOT_STATUS_DELETE;
1765 goto done;
1767 err = got_error_from_errno2("fstat", abspath);
1768 goto done;
1772 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1773 *status = GOT_STATUS_OBSTRUCTED;
1774 goto done;
1777 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1778 *status = GOT_STATUS_DELETE;
1779 goto done;
1780 } else if (!got_fileindex_entry_has_blob(ie) &&
1781 staged_status != GOT_STATUS_ADD) {
1782 *status = GOT_STATUS_ADD;
1783 goto done;
1786 if (!stat_info_differs(ie, sb))
1787 goto done;
1789 if (S_ISLNK(sb->st_mode) &&
1790 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1791 *status = GOT_STATUS_MODIFY;
1792 goto done;
1795 if (staged_status == GOT_STATUS_MODIFY ||
1796 staged_status == GOT_STATUS_ADD)
1797 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1798 else
1799 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1801 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1802 if (err)
1803 goto done;
1805 if (S_ISLNK(sb->st_mode)) {
1806 err = get_symlink_modification_status(status, ie,
1807 abspath, dirfd, de_name, blob);
1808 goto done;
1811 if (dirfd != -1) {
1812 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1813 if (fd == -1) {
1814 err = got_error_from_errno2("openat", abspath);
1815 goto done;
1819 f = fdopen(fd, "r");
1820 if (f == NULL) {
1821 err = got_error_from_errno2("fdopen", abspath);
1822 goto done;
1824 fd = -1;
1825 hdrlen = got_object_blob_get_hdrlen(blob);
1826 for (;;) {
1827 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1828 err = got_object_blob_read_block(&blen, blob);
1829 if (err)
1830 goto done;
1831 /* Skip length of blob object header first time around. */
1832 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1833 if (flen == 0 && ferror(f)) {
1834 err = got_error_from_errno("fread");
1835 goto done;
1837 if (blen - hdrlen == 0) {
1838 if (flen != 0)
1839 *status = GOT_STATUS_MODIFY;
1840 break;
1841 } else if (flen == 0) {
1842 if (blen - hdrlen != 0)
1843 *status = GOT_STATUS_MODIFY;
1844 break;
1845 } else if (blen - hdrlen == flen) {
1846 /* Skip blob object header first time around. */
1847 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1848 *status = GOT_STATUS_MODIFY;
1849 break;
1851 } else {
1852 *status = GOT_STATUS_MODIFY;
1853 break;
1855 hdrlen = 0;
1858 if (*status == GOT_STATUS_MODIFY) {
1859 rewind(f);
1860 err = get_modified_file_content_status(status, f);
1861 } else if (xbit_differs(ie, sb->st_mode))
1862 *status = GOT_STATUS_MODE_CHANGE;
1863 done:
1864 if (blob)
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1870 return err;
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1885 return NULL;
1888 static const struct got_error *
1889 update_blob(struct got_worktree *worktree,
1890 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1891 struct got_tree_entry *te, const char *path,
1892 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1893 void *progress_arg)
1895 const struct got_error *err = NULL;
1896 struct got_blob_object *blob = NULL;
1897 char *ondisk_path;
1898 unsigned char status = GOT_STATUS_NO_CHANGE;
1899 struct stat sb;
1901 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 return got_error_from_errno("asprintf");
1904 if (ie) {
1905 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 goto done;
1909 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 repo);
1911 if (err)
1912 goto done;
1913 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 sb.st_mode = got_fileindex_perms_to_st(ie);
1915 } else {
1916 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1917 status = GOT_STATUS_UNVERSIONED;
1920 if (status == GOT_STATUS_OBSTRUCTED) {
1921 err = (*progress_cb)(progress_arg, status, path);
1922 goto done;
1924 if (status == GOT_STATUS_CONFLICT) {
1925 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1926 path);
1927 goto done;
1930 if (ie && status != GOT_STATUS_MISSING &&
1931 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1932 if (got_fileindex_entry_has_commit(ie) &&
1933 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1934 SHA1_DIGEST_LENGTH) == 0) {
1935 err = sync_timestamps(worktree->root_fd,
1936 path, status, ie, &sb);
1937 if (err)
1938 goto done;
1939 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1940 path);
1941 goto done;
1943 if (got_fileindex_entry_has_blob(ie) &&
1944 memcmp(ie->blob_sha1, te->id.sha1,
1945 SHA1_DIGEST_LENGTH) == 0) {
1946 err = sync_timestamps(worktree->root_fd,
1947 path, status, ie, &sb);
1948 goto done;
1952 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1953 if (err)
1954 goto done;
1956 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1957 int update_timestamps;
1958 struct got_blob_object *blob2 = NULL;
1959 char *label_orig = NULL;
1960 if (got_fileindex_entry_has_blob(ie)) {
1961 struct got_object_id id2;
1962 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1963 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1964 if (err)
1965 goto done;
1967 if (got_fileindex_entry_has_commit(ie)) {
1968 char id_str[SHA1_DIGEST_STRING_LENGTH];
1969 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1970 sizeof(id_str)) == NULL) {
1971 err = got_error_path(id_str,
1972 GOT_ERR_BAD_OBJ_ID_STR);
1973 goto done;
1975 if (asprintf(&label_orig, "%s: commit %s",
1976 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1977 err = got_error_from_errno("asprintf");
1978 goto done;
1981 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1982 char *link_target;
1983 err = got_object_blob_read_to_str(&link_target, blob);
1984 if (err)
1985 goto done;
1986 err = merge_symlink(worktree, blob2, ondisk_path, path,
1987 label_orig, link_target, worktree->base_commit_id,
1988 repo, progress_cb, progress_arg);
1989 free(link_target);
1990 } else {
1991 err = merge_blob(&update_timestamps, worktree, blob2,
1992 ondisk_path, path, sb.st_mode, label_orig, blob,
1993 worktree->base_commit_id, repo,
1994 progress_cb, progress_arg);
1996 free(label_orig);
1997 if (blob2)
1998 got_object_blob_close(blob2);
1999 if (err)
2000 goto done;
2002 * Do not update timestamps of files with local changes.
2003 * Otherwise, a future status walk would treat them as
2004 * unmodified files again.
2006 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2007 blob->id.sha1, worktree->base_commit_id->sha1,
2008 update_timestamps);
2009 } else if (status == GOT_STATUS_MODE_CHANGE) {
2010 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2011 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2012 } else if (status == GOT_STATUS_DELETE) {
2013 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2014 if (err)
2015 goto done;
2016 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2017 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2018 if (err)
2019 goto done;
2020 } else {
2021 int is_bad_symlink = 0;
2022 if (S_ISLNK(te->mode)) {
2023 err = install_symlink(&is_bad_symlink, worktree,
2024 ondisk_path, path, blob,
2025 status == GOT_STATUS_MISSING, 0,
2026 status == GOT_STATUS_UNVERSIONED, repo,
2027 progress_cb, progress_arg);
2028 } else {
2029 err = install_blob(worktree, ondisk_path, path,
2030 te->mode, sb.st_mode, blob,
2031 status == GOT_STATUS_MISSING, 0, 0,
2032 status == GOT_STATUS_UNVERSIONED, repo,
2033 progress_cb, progress_arg);
2035 if (err)
2036 goto done;
2038 if (ie) {
2039 err = got_fileindex_entry_update(ie,
2040 worktree->root_fd, path, blob->id.sha1,
2041 worktree->base_commit_id->sha1, 1);
2042 } else {
2043 err = create_fileindex_entry(&ie, fileindex,
2044 worktree->base_commit_id, worktree->root_fd, path,
2045 &blob->id);
2047 if (err)
2048 goto done;
2050 if (is_bad_symlink) {
2051 got_fileindex_entry_filetype_set(ie,
2052 GOT_FILEIDX_MODE_BAD_SYMLINK);
2055 got_object_blob_close(blob);
2056 done:
2057 free(ondisk_path);
2058 return err;
2061 static const struct got_error *
2062 remove_ondisk_file(const char *root_path, const char *path)
2064 const struct got_error *err = NULL;
2065 char *ondisk_path = NULL;
2067 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2068 return got_error_from_errno("asprintf");
2070 if (unlink(ondisk_path) == -1) {
2071 if (errno != ENOENT)
2072 err = got_error_from_errno2("unlink", ondisk_path);
2073 } else {
2074 size_t root_len = strlen(root_path);
2075 do {
2076 char *parent;
2077 err = got_path_dirname(&parent, ondisk_path);
2078 if (err)
2079 break;
2080 free(ondisk_path);
2081 ondisk_path = parent;
2082 if (rmdir(ondisk_path) == -1) {
2083 if (errno != ENOTEMPTY)
2084 err = got_error_from_errno2("rmdir",
2085 ondisk_path);
2086 break;
2088 } while (got_path_cmp(ondisk_path, root_path,
2089 strlen(ondisk_path), root_len) != 0);
2091 free(ondisk_path);
2092 return err;
2095 static const struct got_error *
2096 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2097 struct got_fileindex_entry *ie, struct got_repository *repo,
2098 got_worktree_checkout_cb progress_cb, void *progress_arg)
2100 const struct got_error *err = NULL;
2101 unsigned char status;
2102 struct stat sb;
2103 char *ondisk_path;
2105 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2106 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2108 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2109 == -1)
2110 return got_error_from_errno("asprintf");
2112 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2113 if (err)
2114 goto done;
2116 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2117 char ondisk_target[PATH_MAX];
2118 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2119 sizeof(ondisk_target));
2120 if (ondisk_len == -1) {
2121 err = got_error_from_errno2("readlink", ondisk_path);
2122 goto done;
2124 ondisk_target[ondisk_len] = '\0';
2125 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2126 NULL, NULL, /* XXX pass common ancestor info? */
2127 ondisk_target, ondisk_path);
2128 if (err)
2129 goto done;
2130 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2131 ie->path);
2132 goto done;
2135 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2136 status == GOT_STATUS_ADD) {
2137 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2138 if (err)
2139 goto done;
2141 * Preserve the working file and change the deleted blob's
2142 * entry into a schedule-add entry.
2144 err = got_fileindex_entry_update(ie, worktree->root_fd,
2145 ie->path, NULL, NULL, 0);
2146 } else {
2147 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2148 if (err)
2149 goto done;
2150 if (status == GOT_STATUS_NO_CHANGE) {
2151 err = remove_ondisk_file(worktree->root_path, ie->path);
2152 if (err)
2153 goto done;
2155 got_fileindex_entry_remove(fileindex, ie);
2157 done:
2158 free(ondisk_path);
2159 return err;
2162 struct diff_cb_arg {
2163 struct got_fileindex *fileindex;
2164 struct got_worktree *worktree;
2165 struct got_repository *repo;
2166 got_worktree_checkout_cb progress_cb;
2167 void *progress_arg;
2168 got_cancel_cb cancel_cb;
2169 void *cancel_arg;
2172 static const struct got_error *
2173 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2174 struct got_tree_entry *te, const char *parent_path)
2176 struct diff_cb_arg *a = arg;
2178 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2179 return got_error(GOT_ERR_CANCELLED);
2181 return update_blob(a->worktree, a->fileindex, ie, te,
2182 ie->path, a->repo, a->progress_cb, a->progress_arg);
2185 static const struct got_error *
2186 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2188 struct diff_cb_arg *a = arg;
2190 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2191 return got_error(GOT_ERR_CANCELLED);
2193 return delete_blob(a->worktree, a->fileindex, ie,
2194 a->repo, a->progress_cb, a->progress_arg);
2197 static const struct got_error *
2198 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2200 struct diff_cb_arg *a = arg;
2201 const struct got_error *err;
2202 char *path;
2204 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2205 return got_error(GOT_ERR_CANCELLED);
2207 if (got_object_tree_entry_is_submodule(te))
2208 return NULL;
2210 if (asprintf(&path, "%s%s%s", parent_path,
2211 parent_path[0] ? "/" : "", te->name)
2212 == -1)
2213 return got_error_from_errno("asprintf");
2215 if (S_ISDIR(te->mode))
2216 err = add_dir_on_disk(a->worktree, path);
2217 else
2218 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2219 a->repo, a->progress_cb, a->progress_arg);
2221 free(path);
2222 return err;
2225 const struct got_error *
2226 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2228 uint32_t uuid_status;
2230 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2231 if (uuid_status != uuid_s_ok) {
2232 *uuidstr = NULL;
2233 return got_error_uuid(uuid_status, "uuid_to_string");
2236 return NULL;
2239 static const struct got_error *
2240 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2242 const struct got_error *err = NULL;
2243 char *uuidstr = NULL;
2245 *refname = NULL;
2247 err = got_worktree_get_uuid(&uuidstr, worktree);
2248 if (err)
2249 return err;
2251 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2252 err = got_error_from_errno("asprintf");
2253 *refname = NULL;
2255 free(uuidstr);
2256 return err;
2259 const struct got_error *
2260 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2262 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2265 static const struct got_error *
2266 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree,
2269 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2272 static const struct got_error *
2273 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2278 static const struct got_error *
2279 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2285 static const struct got_error *
2286 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2288 return get_ref_name(refname, worktree,
2289 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2292 static const struct got_error *
2293 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2295 return get_ref_name(refname, worktree,
2296 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2299 static const struct got_error *
2300 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2306 static const struct got_error *
2307 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2313 static const struct got_error *
2314 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2316 return get_ref_name(refname, worktree,
2317 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2320 const struct got_error *
2321 got_worktree_get_histedit_script_path(char **path,
2322 struct got_worktree *worktree)
2324 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2325 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2326 *path = NULL;
2327 return got_error_from_errno("asprintf");
2329 return NULL;
2333 * Prevent Git's garbage collector from deleting our base commit by
2334 * setting a reference to our base commit's ID.
2336 static const struct got_error *
2337 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2339 const struct got_error *err = NULL;
2340 struct got_reference *ref = NULL;
2341 char *refname;
2343 err = got_worktree_get_base_ref_name(&refname, worktree);
2344 if (err)
2345 return err;
2347 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2348 if (err)
2349 goto done;
2351 err = got_ref_write(ref, repo);
2352 done:
2353 free(refname);
2354 if (ref)
2355 got_ref_close(ref);
2356 return err;
2359 static const struct got_error *
2360 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2362 const struct got_error *err = NULL;
2364 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2365 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2366 err = got_error_from_errno("asprintf");
2367 *fileindex_path = NULL;
2369 return err;
2373 static const struct got_error *
2374 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2375 struct got_worktree *worktree)
2377 const struct got_error *err = NULL;
2378 FILE *index = NULL;
2380 *fileindex_path = NULL;
2381 *fileindex = got_fileindex_alloc();
2382 if (*fileindex == NULL)
2383 return got_error_from_errno("got_fileindex_alloc");
2385 err = get_fileindex_path(fileindex_path, worktree);
2386 if (err)
2387 goto done;
2389 index = fopen(*fileindex_path, "rb");
2390 if (index == NULL) {
2391 if (errno != ENOENT)
2392 err = got_error_from_errno2("fopen", *fileindex_path);
2393 } else {
2394 err = got_fileindex_read(*fileindex, index);
2395 if (fclose(index) == EOF && err == NULL)
2396 err = got_error_from_errno("fclose");
2398 done:
2399 if (err) {
2400 free(*fileindex_path);
2401 *fileindex_path = NULL;
2402 got_fileindex_free(*fileindex);
2403 *fileindex = NULL;
2405 return err;
2408 struct bump_base_commit_id_arg {
2409 struct got_object_id *base_commit_id;
2410 const char *path;
2411 size_t path_len;
2412 const char *entry_name;
2413 got_worktree_checkout_cb progress_cb;
2414 void *progress_arg;
2417 /* Bump base commit ID of all files within an updated part of the work tree. */
2418 static const struct got_error *
2419 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2421 const struct got_error *err;
2422 struct bump_base_commit_id_arg *a = arg;
2424 if (a->entry_name) {
2425 if (strcmp(ie->path, a->path) != 0)
2426 return NULL;
2427 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2428 return NULL;
2430 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2431 SHA1_DIGEST_LENGTH) == 0)
2432 return NULL;
2434 if (a->progress_cb) {
2435 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2436 ie->path);
2437 if (err)
2438 return err;
2440 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2441 return NULL;
2444 static const struct got_error *
2445 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2446 struct got_fileindex *fileindex,
2447 got_worktree_checkout_cb progress_cb, void *progress_arg)
2449 struct bump_base_commit_id_arg bbc_arg;
2451 bbc_arg.base_commit_id = worktree->base_commit_id;
2452 bbc_arg.entry_name = NULL;
2453 bbc_arg.path = "";
2454 bbc_arg.path_len = 0;
2455 bbc_arg.progress_cb = progress_cb;
2456 bbc_arg.progress_arg = progress_arg;
2458 return got_fileindex_for_each_entry_safe(fileindex,
2459 bump_base_commit_id, &bbc_arg);
2462 static const struct got_error *
2463 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2465 const struct got_error *err = NULL;
2466 char *new_fileindex_path = NULL;
2467 FILE *new_index = NULL;
2468 struct timespec timeout;
2470 err = got_opentemp_named(&new_fileindex_path, &new_index,
2471 fileindex_path);
2472 if (err)
2473 goto done;
2475 err = got_fileindex_write(fileindex, new_index);
2476 if (err)
2477 goto done;
2479 if (rename(new_fileindex_path, fileindex_path) != 0) {
2480 err = got_error_from_errno3("rename", new_fileindex_path,
2481 fileindex_path);
2482 unlink(new_fileindex_path);
2486 * Sleep for a short amount of time to ensure that files modified after
2487 * this program exits have a different time stamp from the one which
2488 * was recorded in the file index.
2490 timeout.tv_sec = 0;
2491 timeout.tv_nsec = 1;
2492 nanosleep(&timeout, NULL);
2493 done:
2494 if (new_index)
2495 fclose(new_index);
2496 free(new_fileindex_path);
2497 return err;
2500 static const struct got_error *
2501 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2502 struct got_object_id **tree_id, const char *wt_relpath,
2503 struct got_worktree *worktree, struct got_repository *repo)
2505 const struct got_error *err = NULL;
2506 struct got_object_id *id = NULL;
2507 char *in_repo_path = NULL;
2508 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2510 *entry_type = GOT_OBJ_TYPE_ANY;
2511 *tree_relpath = NULL;
2512 *tree_id = NULL;
2514 if (wt_relpath[0] == '\0') {
2515 /* Check out all files within the work tree. */
2516 *entry_type = GOT_OBJ_TYPE_TREE;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 goto done;
2522 err = got_object_id_by_path(tree_id, repo,
2523 worktree->base_commit_id, worktree->path_prefix);
2524 if (err)
2525 goto done;
2526 return NULL;
2529 /* Check out a subset of files in the work tree. */
2531 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2532 is_root_wt ? "" : "/", wt_relpath) == -1) {
2533 err = got_error_from_errno("asprintf");
2534 goto done;
2537 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2538 in_repo_path);
2539 if (err)
2540 goto done;
2542 free(in_repo_path);
2543 in_repo_path = NULL;
2545 err = got_object_get_type(entry_type, repo, id);
2546 if (err)
2547 goto done;
2549 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2550 /* Check out a single file. */
2551 if (strchr(wt_relpath, '/') == NULL) {
2552 /* Check out a single file in work tree's root dir. */
2553 in_repo_path = strdup(worktree->path_prefix);
2554 if (in_repo_path == NULL) {
2555 err = got_error_from_errno("strdup");
2556 goto done;
2558 *tree_relpath = strdup("");
2559 if (*tree_relpath == NULL) {
2560 err = got_error_from_errno("strdup");
2561 goto done;
2563 } else {
2564 /* Check out a single file in a subdirectory. */
2565 err = got_path_dirname(tree_relpath, wt_relpath);
2566 if (err)
2567 return err;
2568 if (asprintf(&in_repo_path, "%s%s%s",
2569 worktree->path_prefix, is_root_wt ? "" : "/",
2570 *tree_relpath) == -1) {
2571 err = got_error_from_errno("asprintf");
2572 goto done;
2575 err = got_object_id_by_path(tree_id, repo,
2576 worktree->base_commit_id, in_repo_path);
2577 } else {
2578 /* Check out all files within a subdirectory. */
2579 *tree_id = got_object_id_dup(id);
2580 if (*tree_id == NULL) {
2581 err = got_error_from_errno("got_object_id_dup");
2582 goto done;
2584 *tree_relpath = strdup(wt_relpath);
2585 if (*tree_relpath == NULL) {
2586 err = got_error_from_errno("strdup");
2587 goto done;
2590 done:
2591 free(id);
2592 free(in_repo_path);
2593 if (err) {
2594 *entry_type = GOT_OBJ_TYPE_ANY;
2595 free(*tree_relpath);
2596 *tree_relpath = NULL;
2597 free(*tree_id);
2598 *tree_id = NULL;
2600 return err;
2603 static const struct got_error *
2604 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2605 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2606 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2607 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2609 const struct got_error *err = NULL;
2610 struct got_commit_object *commit = NULL;
2611 struct got_tree_object *tree = NULL;
2612 struct got_fileindex_diff_tree_cb diff_cb;
2613 struct diff_cb_arg arg;
2615 err = ref_base_commit(worktree, repo);
2616 if (err) {
2617 if (!(err->code == GOT_ERR_ERRNO &&
2618 (errno == EACCES || errno == EROFS)))
2619 goto done;
2620 err = (*progress_cb)(progress_arg,
2621 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2622 if (err)
2623 return err;
2626 err = got_object_open_as_commit(&commit, repo,
2627 worktree->base_commit_id);
2628 if (err)
2629 goto done;
2631 err = got_object_open_as_tree(&tree, repo, tree_id);
2632 if (err)
2633 goto done;
2635 if (entry_name &&
2636 got_object_tree_find_entry(tree, entry_name) == NULL) {
2637 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2638 goto done;
2641 diff_cb.diff_old_new = diff_old_new;
2642 diff_cb.diff_old = diff_old;
2643 diff_cb.diff_new = diff_new;
2644 arg.fileindex = fileindex;
2645 arg.worktree = worktree;
2646 arg.repo = repo;
2647 arg.progress_cb = progress_cb;
2648 arg.progress_arg = progress_arg;
2649 arg.cancel_cb = cancel_cb;
2650 arg.cancel_arg = cancel_arg;
2651 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2652 entry_name, repo, &diff_cb, &arg);
2653 done:
2654 if (tree)
2655 got_object_tree_close(tree);
2656 if (commit)
2657 got_object_commit_close(commit);
2658 return err;
2661 const struct got_error *
2662 got_worktree_checkout_files(struct got_worktree *worktree,
2663 struct got_pathlist_head *paths, struct got_repository *repo,
2664 got_worktree_checkout_cb progress_cb, void *progress_arg,
2665 got_cancel_cb cancel_cb, void *cancel_arg)
2667 const struct got_error *err = NULL, *sync_err, *unlockerr;
2668 struct got_commit_object *commit = NULL;
2669 struct got_tree_object *tree = NULL;
2670 struct got_fileindex *fileindex = NULL;
2671 char *fileindex_path = NULL;
2672 struct got_pathlist_entry *pe;
2673 struct tree_path_data {
2674 SIMPLEQ_ENTRY(tree_path_data) entry;
2675 struct got_object_id *tree_id;
2676 int entry_type;
2677 char *relpath;
2678 char *entry_name;
2679 } *tpd = NULL;
2680 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2682 SIMPLEQ_INIT(&tree_paths);
2684 err = lock_worktree(worktree, LOCK_EX);
2685 if (err)
2686 return err;
2688 /* Map all specified paths to in-repository trees. */
2689 TAILQ_FOREACH(pe, paths, entry) {
2690 tpd = malloc(sizeof(*tpd));
2691 if (tpd == NULL) {
2692 err = got_error_from_errno("malloc");
2693 goto done;
2696 err = find_tree_entry_for_checkout(&tpd->entry_type,
2697 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2698 if (err) {
2699 free(tpd);
2700 goto done;
2703 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2704 err = got_path_basename(&tpd->entry_name, pe->path);
2705 if (err) {
2706 free(tpd->relpath);
2707 free(tpd->tree_id);
2708 free(tpd);
2709 goto done;
2711 } else
2712 tpd->entry_name = NULL;
2714 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2718 * Read the file index.
2719 * Checking out files is supposed to be an idempotent operation.
2720 * If the on-disk file index is incomplete we will try to complete it.
2722 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2723 if (err)
2724 goto done;
2726 tpd = SIMPLEQ_FIRST(&tree_paths);
2727 TAILQ_FOREACH(pe, paths, entry) {
2728 struct bump_base_commit_id_arg bbc_arg;
2730 err = checkout_files(worktree, fileindex, tpd->relpath,
2731 tpd->tree_id, tpd->entry_name, repo,
2732 progress_cb, progress_arg, cancel_cb, cancel_arg);
2733 if (err)
2734 break;
2736 bbc_arg.base_commit_id = worktree->base_commit_id;
2737 bbc_arg.entry_name = tpd->entry_name;
2738 bbc_arg.path = pe->path;
2739 bbc_arg.path_len = pe->path_len;
2740 bbc_arg.progress_cb = progress_cb;
2741 bbc_arg.progress_arg = progress_arg;
2742 err = got_fileindex_for_each_entry_safe(fileindex,
2743 bump_base_commit_id, &bbc_arg);
2744 if (err)
2745 break;
2747 tpd = SIMPLEQ_NEXT(tpd, entry);
2749 sync_err = sync_fileindex(fileindex, fileindex_path);
2750 if (sync_err && err == NULL)
2751 err = sync_err;
2752 done:
2753 free(fileindex_path);
2754 if (tree)
2755 got_object_tree_close(tree);
2756 if (commit)
2757 got_object_commit_close(commit);
2758 if (fileindex)
2759 got_fileindex_free(fileindex);
2760 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2761 tpd = SIMPLEQ_FIRST(&tree_paths);
2762 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2763 free(tpd->relpath);
2764 free(tpd->tree_id);
2765 free(tpd);
2767 unlockerr = lock_worktree(worktree, LOCK_SH);
2768 if (unlockerr && err == NULL)
2769 err = unlockerr;
2770 return err;
2773 struct merge_file_cb_arg {
2774 struct got_worktree *worktree;
2775 struct got_fileindex *fileindex;
2776 got_worktree_checkout_cb progress_cb;
2777 void *progress_arg;
2778 got_cancel_cb cancel_cb;
2779 void *cancel_arg;
2780 const char *label_orig;
2781 struct got_object_id *commit_id2;
2784 static const struct got_error *
2785 merge_file_cb(void *arg, struct got_blob_object *blob1,
2786 struct got_blob_object *blob2, struct got_object_id *id1,
2787 struct got_object_id *id2, const char *path1, const char *path2,
2788 mode_t mode1, mode_t mode2, struct got_repository *repo)
2790 static const struct got_error *err = NULL;
2791 struct merge_file_cb_arg *a = arg;
2792 struct got_fileindex_entry *ie;
2793 char *ondisk_path = NULL;
2794 struct stat sb;
2795 unsigned char status;
2796 int local_changes_subsumed;
2798 if (blob1 && blob2) {
2799 ie = got_fileindex_entry_get(a->fileindex, path2,
2800 strlen(path2));
2801 if (ie == NULL)
2802 return (*a->progress_cb)(a->progress_arg,
2803 GOT_STATUS_MISSING, path2);
2805 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2806 path2) == -1)
2807 return got_error_from_errno("asprintf");
2809 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2810 repo);
2811 if (err)
2812 goto done;
2814 if (status == GOT_STATUS_DELETE) {
2815 err = (*a->progress_cb)(a->progress_arg,
2816 GOT_STATUS_MERGE, path2);
2817 goto done;
2819 if (status != GOT_STATUS_NO_CHANGE &&
2820 status != GOT_STATUS_MODIFY &&
2821 status != GOT_STATUS_CONFLICT &&
2822 status != GOT_STATUS_ADD) {
2823 err = (*a->progress_cb)(a->progress_arg, status, path2);
2824 goto done;
2827 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2828 char *link_target2;
2829 err = got_object_blob_read_to_str(&link_target2, blob2);
2830 if (err)
2831 goto done;
2832 err = merge_symlink(a->worktree, blob1, ondisk_path,
2833 path2, a->label_orig, link_target2, a->commit_id2,
2834 repo, a->progress_cb, a->progress_arg);
2835 free(link_target2);
2836 } else {
2837 err = merge_blob(&local_changes_subsumed, a->worktree,
2838 blob1, ondisk_path, path2, sb.st_mode,
2839 a->label_orig, blob2, a->commit_id2, repo,
2840 a->progress_cb, a->progress_arg);
2842 } else if (blob1) {
2843 ie = got_fileindex_entry_get(a->fileindex, path1,
2844 strlen(path1));
2845 if (ie == NULL)
2846 return (*a->progress_cb)(a->progress_arg,
2847 GOT_STATUS_MISSING, path1);
2849 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2850 path1) == -1)
2851 return got_error_from_errno("asprintf");
2853 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2854 repo);
2855 if (err)
2856 goto done;
2858 switch (status) {
2859 case GOT_STATUS_NO_CHANGE:
2860 err = (*a->progress_cb)(a->progress_arg,
2861 GOT_STATUS_DELETE, path1);
2862 if (err)
2863 goto done;
2864 err = remove_ondisk_file(a->worktree->root_path, path1);
2865 if (err)
2866 goto done;
2867 if (ie)
2868 got_fileindex_entry_mark_deleted_from_disk(ie);
2869 break;
2870 case GOT_STATUS_DELETE:
2871 case GOT_STATUS_MISSING:
2872 err = (*a->progress_cb)(a->progress_arg,
2873 GOT_STATUS_DELETE, path1);
2874 if (err)
2875 goto done;
2876 if (ie)
2877 got_fileindex_entry_mark_deleted_from_disk(ie);
2878 break;
2879 case GOT_STATUS_ADD: {
2880 struct got_object_id *id;
2881 FILE *blob1_f;
2883 * Delete the added file only if its content already
2884 * exists in the repository.
2886 err = got_object_blob_file_create(&id, &blob1_f, path1);
2887 if (err)
2888 goto done;
2889 if (got_object_id_cmp(id, id1) == 0) {
2890 err = (*a->progress_cb)(a->progress_arg,
2891 GOT_STATUS_DELETE, path1);
2892 if (err)
2893 goto done;
2894 err = remove_ondisk_file(a->worktree->root_path,
2895 path1);
2896 if (err)
2897 goto done;
2898 if (ie)
2899 got_fileindex_entry_remove(a->fileindex,
2900 ie);
2901 } else {
2902 err = (*a->progress_cb)(a->progress_arg,
2903 GOT_STATUS_CANNOT_DELETE, path1);
2905 if (fclose(blob1_f) == EOF && err == NULL)
2906 err = got_error_from_errno("fclose");
2907 free(id);
2908 if (err)
2909 goto done;
2910 break;
2912 case GOT_STATUS_MODIFY:
2913 case GOT_STATUS_CONFLICT:
2914 err = (*a->progress_cb)(a->progress_arg,
2915 GOT_STATUS_CANNOT_DELETE, path1);
2916 if (err)
2917 goto done;
2918 break;
2919 case GOT_STATUS_OBSTRUCTED:
2920 err = (*a->progress_cb)(a->progress_arg, status, path1);
2921 if (err)
2922 goto done;
2923 break;
2924 default:
2925 break;
2927 } else if (blob2) {
2928 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2929 path2) == -1)
2930 return got_error_from_errno("asprintf");
2931 ie = got_fileindex_entry_get(a->fileindex, path2,
2932 strlen(path2));
2933 if (ie) {
2934 err = get_file_status(&status, &sb, ie, ondisk_path,
2935 -1, NULL, repo);
2936 if (err)
2937 goto done;
2938 if (status != GOT_STATUS_NO_CHANGE &&
2939 status != GOT_STATUS_MODIFY &&
2940 status != GOT_STATUS_CONFLICT &&
2941 status != GOT_STATUS_ADD) {
2942 err = (*a->progress_cb)(a->progress_arg,
2943 status, path2);
2944 goto done;
2946 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2947 char *link_target2;
2948 err = got_object_blob_read_to_str(&link_target2,
2949 blob2);
2950 if (err)
2951 goto done;
2952 err = merge_symlink(a->worktree, NULL,
2953 ondisk_path, path2, a->label_orig,
2954 link_target2, a->commit_id2, repo,
2955 a->progress_cb, a->progress_arg);
2956 free(link_target2);
2957 } else if (S_ISREG(sb.st_mode)) {
2958 err = merge_blob(&local_changes_subsumed,
2959 a->worktree, NULL, ondisk_path, path2,
2960 sb.st_mode, a->label_orig, blob2,
2961 a->commit_id2, repo, a->progress_cb,
2962 a->progress_arg);
2963 } else {
2964 err = got_error_path(ondisk_path,
2965 GOT_ERR_FILE_OBSTRUCTED);
2967 if (err)
2968 goto done;
2969 if (status == GOT_STATUS_DELETE) {
2970 err = got_fileindex_entry_update(ie,
2971 a->worktree->root_fd, path2, blob2->id.sha1,
2972 a->worktree->base_commit_id->sha1, 0);
2973 if (err)
2974 goto done;
2976 } else {
2977 int is_bad_symlink = 0;
2978 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2979 if (S_ISLNK(mode2)) {
2980 err = install_symlink(&is_bad_symlink,
2981 a->worktree, ondisk_path, path2, blob2, 0,
2982 0, 1, repo, a->progress_cb, a->progress_arg);
2983 } else {
2984 err = install_blob(a->worktree, ondisk_path, path2,
2985 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2986 a->progress_cb, a->progress_arg);
2988 if (err)
2989 goto done;
2990 err = got_fileindex_entry_alloc(&ie, path2);
2991 if (err)
2992 goto done;
2993 err = got_fileindex_entry_update(ie,
2994 a->worktree->root_fd, path2, NULL, NULL, 1);
2995 if (err) {
2996 got_fileindex_entry_free(ie);
2997 goto done;
2999 err = got_fileindex_entry_add(a->fileindex, ie);
3000 if (err) {
3001 got_fileindex_entry_free(ie);
3002 goto done;
3004 if (is_bad_symlink) {
3005 got_fileindex_entry_filetype_set(ie,
3006 GOT_FILEIDX_MODE_BAD_SYMLINK);
3010 done:
3011 free(ondisk_path);
3012 return err;
3015 struct check_merge_ok_arg {
3016 struct got_worktree *worktree;
3017 struct got_repository *repo;
3020 static const struct got_error *
3021 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3023 const struct got_error *err = NULL;
3024 struct check_merge_ok_arg *a = arg;
3025 unsigned char status;
3026 struct stat sb;
3027 char *ondisk_path;
3029 /* Reject merges into a work tree with mixed base commits. */
3030 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3031 SHA1_DIGEST_LENGTH))
3032 return got_error(GOT_ERR_MIXED_COMMITS);
3034 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3035 == -1)
3036 return got_error_from_errno("asprintf");
3038 /* Reject merges into a work tree with conflicted files. */
3039 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3040 if (err)
3041 return err;
3042 if (status == GOT_STATUS_CONFLICT)
3043 return got_error(GOT_ERR_CONFLICTS);
3045 return NULL;
3048 static const struct got_error *
3049 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3050 const char *fileindex_path, struct got_object_id *commit_id1,
3051 struct got_object_id *commit_id2, struct got_repository *repo,
3052 got_worktree_checkout_cb progress_cb, void *progress_arg,
3053 got_cancel_cb cancel_cb, void *cancel_arg)
3055 const struct got_error *err = NULL, *sync_err;
3056 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3057 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3058 struct merge_file_cb_arg arg;
3059 char *label_orig = NULL;
3061 if (commit_id1) {
3062 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3063 worktree->path_prefix);
3064 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3065 goto done;
3067 if (tree_id1) {
3068 char *id_str;
3070 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3071 if (err)
3072 goto done;
3074 err = got_object_id_str(&id_str, commit_id1);
3075 if (err)
3076 goto done;
3078 if (asprintf(&label_orig, "%s: commit %s",
3079 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3080 err = got_error_from_errno("asprintf");
3081 free(id_str);
3082 goto done;
3084 free(id_str);
3087 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3088 worktree->path_prefix);
3089 if (err)
3090 goto done;
3092 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3093 if (err)
3094 goto done;
3096 arg.worktree = worktree;
3097 arg.fileindex = fileindex;
3098 arg.progress_cb = progress_cb;
3099 arg.progress_arg = progress_arg;
3100 arg.cancel_cb = cancel_cb;
3101 arg.cancel_arg = cancel_arg;
3102 arg.label_orig = label_orig;
3103 arg.commit_id2 = commit_id2;
3104 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3105 sync_err = sync_fileindex(fileindex, fileindex_path);
3106 if (sync_err && err == NULL)
3107 err = sync_err;
3108 done:
3109 if (tree1)
3110 got_object_tree_close(tree1);
3111 if (tree2)
3112 got_object_tree_close(tree2);
3113 free(label_orig);
3114 return err;
3117 const struct got_error *
3118 got_worktree_merge_files(struct got_worktree *worktree,
3119 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3120 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3121 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3123 const struct got_error *err, *unlockerr;
3124 char *fileindex_path = NULL;
3125 struct got_fileindex *fileindex = NULL;
3126 struct check_merge_ok_arg mok_arg;
3128 err = lock_worktree(worktree, LOCK_EX);
3129 if (err)
3130 return err;
3132 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3133 if (err)
3134 goto done;
3136 mok_arg.worktree = worktree;
3137 mok_arg.repo = repo;
3138 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3139 &mok_arg);
3140 if (err)
3141 goto done;
3143 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3144 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3145 done:
3146 if (fileindex)
3147 got_fileindex_free(fileindex);
3148 free(fileindex_path);
3149 unlockerr = lock_worktree(worktree, LOCK_SH);
3150 if (unlockerr && err == NULL)
3151 err = unlockerr;
3152 return err;
3155 struct diff_dir_cb_arg {
3156 struct got_fileindex *fileindex;
3157 struct got_worktree *worktree;
3158 const char *status_path;
3159 size_t status_path_len;
3160 struct got_repository *repo;
3161 got_worktree_status_cb status_cb;
3162 void *status_arg;
3163 got_cancel_cb cancel_cb;
3164 void *cancel_arg;
3165 /* A pathlist containing per-directory pathlists of ignore patterns. */
3166 struct got_pathlist_head ignores;
3167 int report_unchanged;
3168 int no_ignores;
3171 static const struct got_error *
3172 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3173 int dirfd, const char *de_name,
3174 got_worktree_status_cb status_cb, void *status_arg,
3175 struct got_repository *repo, int report_unchanged)
3177 const struct got_error *err = NULL;
3178 unsigned char status = GOT_STATUS_NO_CHANGE;
3179 unsigned char staged_status = get_staged_status(ie);
3180 struct stat sb;
3181 struct got_object_id blob_id, commit_id, staged_blob_id;
3182 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3183 struct got_object_id *staged_blob_idp = NULL;
3185 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3186 if (err)
3187 return err;
3189 if (status == GOT_STATUS_NO_CHANGE &&
3190 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3191 return NULL;
3193 if (got_fileindex_entry_has_blob(ie)) {
3194 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3195 blob_idp = &blob_id;
3197 if (got_fileindex_entry_has_commit(ie)) {
3198 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3199 commit_idp = &commit_id;
3201 if (staged_status == GOT_STATUS_ADD ||
3202 staged_status == GOT_STATUS_MODIFY) {
3203 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3204 SHA1_DIGEST_LENGTH);
3205 staged_blob_idp = &staged_blob_id;
3208 return (*status_cb)(status_arg, status, staged_status,
3209 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3212 static const struct got_error *
3213 status_old_new(void *arg, struct got_fileindex_entry *ie,
3214 struct dirent *de, const char *parent_path, int dirfd)
3216 const struct got_error *err = NULL;
3217 struct diff_dir_cb_arg *a = arg;
3218 char *abspath;
3220 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3221 return got_error(GOT_ERR_CANCELLED);
3223 if (got_path_cmp(parent_path, a->status_path,
3224 strlen(parent_path), a->status_path_len) != 0 &&
3225 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3226 return NULL;
3228 if (parent_path[0]) {
3229 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3230 parent_path, de->d_name) == -1)
3231 return got_error_from_errno("asprintf");
3232 } else {
3233 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3234 de->d_name) == -1)
3235 return got_error_from_errno("asprintf");
3238 err = report_file_status(ie, abspath, dirfd, de->d_name,
3239 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3240 free(abspath);
3241 return err;
3244 static const struct got_error *
3245 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3247 struct diff_dir_cb_arg *a = arg;
3248 struct got_object_id blob_id, commit_id;
3249 unsigned char status;
3251 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3252 return got_error(GOT_ERR_CANCELLED);
3254 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3255 return NULL;
3257 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3258 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3259 if (got_fileindex_entry_has_file_on_disk(ie))
3260 status = GOT_STATUS_MISSING;
3261 else
3262 status = GOT_STATUS_DELETE;
3263 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3264 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3267 void
3268 free_ignorelist(struct got_pathlist_head *ignorelist)
3270 struct got_pathlist_entry *pe;
3272 TAILQ_FOREACH(pe, ignorelist, entry)
3273 free((char *)pe->path);
3274 got_pathlist_free(ignorelist);
3277 void
3278 free_ignores(struct got_pathlist_head *ignores)
3280 struct got_pathlist_entry *pe;
3282 TAILQ_FOREACH(pe, ignores, entry) {
3283 struct got_pathlist_head *ignorelist = pe->data;
3284 free_ignorelist(ignorelist);
3285 free((char *)pe->path);
3287 got_pathlist_free(ignores);
3290 static const struct got_error *
3291 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3293 const struct got_error *err = NULL;
3294 struct got_pathlist_entry *pe = NULL;
3295 struct got_pathlist_head *ignorelist;
3296 char *line = NULL, *pattern, *dirpath = NULL;
3297 size_t linesize = 0;
3298 ssize_t linelen;
3300 ignorelist = calloc(1, sizeof(*ignorelist));
3301 if (ignorelist == NULL)
3302 return got_error_from_errno("calloc");
3303 TAILQ_INIT(ignorelist);
3305 while ((linelen = getline(&line, &linesize, f)) != -1) {
3306 if (linelen > 0 && line[linelen - 1] == '\n')
3307 line[linelen - 1] = '\0';
3309 /* Git's ignores may contain comments. */
3310 if (line[0] == '#')
3311 continue;
3313 /* Git's negated patterns are not (yet?) supported. */
3314 if (line[0] == '!')
3315 continue;
3317 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3318 line) == -1) {
3319 err = got_error_from_errno("asprintf");
3320 goto done;
3322 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3323 if (err)
3324 goto done;
3326 if (ferror(f)) {
3327 err = got_error_from_errno("getline");
3328 goto done;
3331 dirpath = strdup(path);
3332 if (dirpath == NULL) {
3333 err = got_error_from_errno("strdup");
3334 goto done;
3336 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3337 done:
3338 free(line);
3339 if (err || pe == NULL) {
3340 free(dirpath);
3341 free_ignorelist(ignorelist);
3343 return err;
3346 int
3347 match_ignores(struct got_pathlist_head *ignores, const char *path)
3349 struct got_pathlist_entry *pe;
3351 /* Handle patterns which match in all directories. */
3352 TAILQ_FOREACH(pe, ignores, entry) {
3353 struct got_pathlist_head *ignorelist = pe->data;
3354 struct got_pathlist_entry *pi;
3356 TAILQ_FOREACH(pi, ignorelist, entry) {
3357 const char *p, *pattern = pi->path;
3359 if (strncmp(pattern, "**/", 3) != 0)
3360 continue;
3361 pattern += 3;
3362 p = path;
3363 while (*p) {
3364 if (fnmatch(pattern, p,
3365 FNM_PATHNAME | FNM_LEADING_DIR)) {
3366 /* Retry in next directory. */
3367 while (*p && *p != '/')
3368 p++;
3369 while (*p == '/')
3370 p++;
3371 continue;
3373 return 1;
3379 * The ignores pathlist contains ignore lists from children before
3380 * parents, so we can find the most specific ignorelist by walking
3381 * ignores backwards.
3383 pe = TAILQ_LAST(ignores, got_pathlist_head);
3384 while (pe) {
3385 if (got_path_is_child(path, pe->path, pe->path_len)) {
3386 struct got_pathlist_head *ignorelist = pe->data;
3387 struct got_pathlist_entry *pi;
3388 TAILQ_FOREACH(pi, ignorelist, entry) {
3389 const char *pattern = pi->path;
3390 int flags = FNM_LEADING_DIR;
3391 if (strstr(pattern, "/**/") == NULL)
3392 flags |= FNM_PATHNAME;
3393 if (fnmatch(pattern, path, flags))
3394 continue;
3395 return 1;
3398 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3401 return 0;
3404 static const struct got_error *
3405 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3406 const char *path, int dirfd, const char *ignores_filename)
3408 const struct got_error *err = NULL;
3409 char *ignorespath;
3410 int fd = -1;
3411 FILE *ignoresfile = NULL;
3413 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3414 path[0] ? "/" : "", ignores_filename) == -1)
3415 return got_error_from_errno("asprintf");
3417 if (dirfd != -1) {
3418 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3419 if (fd == -1) {
3420 if (errno != ENOENT && errno != EACCES)
3421 err = got_error_from_errno2("openat",
3422 ignorespath);
3423 } else {
3424 ignoresfile = fdopen(fd, "r");
3425 if (ignoresfile == NULL)
3426 err = got_error_from_errno2("fdopen",
3427 ignorespath);
3428 else {
3429 fd = -1;
3430 err = read_ignores(ignores, path, ignoresfile);
3433 } else {
3434 ignoresfile = fopen(ignorespath, "r");
3435 if (ignoresfile == NULL) {
3436 if (errno != ENOENT && errno != EACCES)
3437 err = got_error_from_errno2("fopen",
3438 ignorespath);
3439 } else
3440 err = read_ignores(ignores, path, ignoresfile);
3443 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3444 err = got_error_from_errno2("fclose", path);
3445 if (fd != -1 && close(fd) == -1 && err == NULL)
3446 err = got_error_from_errno2("close", path);
3447 free(ignorespath);
3448 return err;
3451 static const struct got_error *
3452 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3454 const struct got_error *err = NULL;
3455 struct diff_dir_cb_arg *a = arg;
3456 char *path = NULL;
3458 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3459 return got_error(GOT_ERR_CANCELLED);
3461 if (parent_path[0]) {
3462 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3463 return got_error_from_errno("asprintf");
3464 } else {
3465 path = de->d_name;
3468 if (de->d_type != DT_DIR &&
3469 got_path_is_child(path, a->status_path, a->status_path_len)
3470 && !match_ignores(&a->ignores, path))
3471 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3472 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3473 if (parent_path[0])
3474 free(path);
3475 return err;
3478 static const struct got_error *
3479 status_traverse(void *arg, const char *path, int dirfd)
3481 const struct got_error *err = NULL;
3482 struct diff_dir_cb_arg *a = arg;
3484 if (a->no_ignores)
3485 return NULL;
3487 err = add_ignores(&a->ignores, a->worktree->root_path,
3488 path, dirfd, ".cvsignore");
3489 if (err)
3490 return err;
3492 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3493 dirfd, ".gitignore");
3495 return err;
3498 static const struct got_error *
3499 report_single_file_status(const char *path, const char *ondisk_path,
3500 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3501 void *status_arg, struct got_repository *repo, int report_unchanged)
3503 struct got_fileindex_entry *ie;
3504 struct stat sb;
3506 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3507 if (ie)
3508 return report_file_status(ie, ondisk_path, -1, NULL,
3509 status_cb, status_arg, repo, report_unchanged);
3511 if (lstat(ondisk_path, &sb) == -1) {
3512 if (errno != ENOENT)
3513 return got_error_from_errno2("lstat", ondisk_path);
3514 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3515 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3516 return NULL;
3519 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3520 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3521 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3523 return NULL;
3526 static const struct got_error *
3527 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3528 const char *root_path, const char *path)
3530 const struct got_error *err;
3531 char *parent_path, *next_parent_path = NULL;
3533 err = add_ignores(ignores, root_path, "", -1,
3534 ".cvsignore");
3535 if (err)
3536 return err;
3538 err = add_ignores(ignores, root_path, "", -1,
3539 ".gitignore");
3540 if (err)
3541 return err;
3543 err = got_path_dirname(&parent_path, path);
3544 if (err) {
3545 if (err->code == GOT_ERR_BAD_PATH)
3546 return NULL; /* cannot traverse parent */
3547 return err;
3549 for (;;) {
3550 err = add_ignores(ignores, root_path, parent_path, -1,
3551 ".cvsignore");
3552 if (err)
3553 break;
3554 err = add_ignores(ignores, root_path, parent_path, -1,
3555 ".gitignore");
3556 if (err)
3557 break;
3558 err = got_path_dirname(&next_parent_path, parent_path);
3559 if (err) {
3560 if (err->code == GOT_ERR_BAD_PATH)
3561 err = NULL; /* traversed everything */
3562 break;
3564 free(parent_path);
3565 parent_path = next_parent_path;
3566 next_parent_path = NULL;
3569 free(parent_path);
3570 free(next_parent_path);
3571 return err;
3574 static const struct got_error *
3575 worktree_status(struct got_worktree *worktree, const char *path,
3576 struct got_fileindex *fileindex, struct got_repository *repo,
3577 got_worktree_status_cb status_cb, void *status_arg,
3578 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3579 int report_unchanged)
3581 const struct got_error *err = NULL;
3582 int fd = -1;
3583 struct got_fileindex_diff_dir_cb fdiff_cb;
3584 struct diff_dir_cb_arg arg;
3585 char *ondisk_path = NULL;
3587 TAILQ_INIT(&arg.ignores);
3589 if (asprintf(&ondisk_path, "%s%s%s",
3590 worktree->root_path, path[0] ? "/" : "", path) == -1)
3591 return got_error_from_errno("asprintf");
3593 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3594 if (fd == -1) {
3595 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3596 errno != ELOOP)
3597 err = got_error_from_errno2("open", ondisk_path);
3598 else
3599 err = report_single_file_status(path, ondisk_path,
3600 fileindex, status_cb, status_arg, repo,
3601 report_unchanged);
3602 } else {
3603 fdiff_cb.diff_old_new = status_old_new;
3604 fdiff_cb.diff_old = status_old;
3605 fdiff_cb.diff_new = status_new;
3606 fdiff_cb.diff_traverse = status_traverse;
3607 arg.fileindex = fileindex;
3608 arg.worktree = worktree;
3609 arg.status_path = path;
3610 arg.status_path_len = strlen(path);
3611 arg.repo = repo;
3612 arg.status_cb = status_cb;
3613 arg.status_arg = status_arg;
3614 arg.cancel_cb = cancel_cb;
3615 arg.cancel_arg = cancel_arg;
3616 arg.report_unchanged = report_unchanged;
3617 arg.no_ignores = no_ignores;
3618 if (!no_ignores) {
3619 err = add_ignores_from_parent_paths(&arg.ignores,
3620 worktree->root_path, path);
3621 if (err)
3622 goto done;
3624 err = got_fileindex_diff_dir(fileindex, fd,
3625 worktree->root_path, path, repo, &fdiff_cb, &arg);
3627 done:
3628 free_ignores(&arg.ignores);
3629 if (fd != -1 && close(fd) == -1 && err == NULL)
3630 err = got_error_from_errno("close");
3631 free(ondisk_path);
3632 return err;
3635 const struct got_error *
3636 got_worktree_status(struct got_worktree *worktree,
3637 struct got_pathlist_head *paths, struct got_repository *repo,
3638 got_worktree_status_cb status_cb, void *status_arg,
3639 got_cancel_cb cancel_cb, void *cancel_arg)
3641 const struct got_error *err = NULL;
3642 char *fileindex_path = NULL;
3643 struct got_fileindex *fileindex = NULL;
3644 struct got_pathlist_entry *pe;
3646 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3647 if (err)
3648 return err;
3650 TAILQ_FOREACH(pe, paths, entry) {
3651 err = worktree_status(worktree, pe->path, fileindex, repo,
3652 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3653 if (err)
3654 break;
3656 free(fileindex_path);
3657 got_fileindex_free(fileindex);
3658 return err;
3661 const struct got_error *
3662 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3663 const char *arg)
3665 const struct got_error *err = NULL;
3666 char *resolved = NULL, *cwd = NULL, *path = NULL;
3667 size_t len;
3668 struct stat sb;
3669 char *abspath = NULL;
3670 char canonpath[PATH_MAX];
3672 *wt_path = NULL;
3674 cwd = getcwd(NULL, 0);
3675 if (cwd == NULL)
3676 return got_error_from_errno("getcwd");
3678 if (lstat(arg, &sb) == -1) {
3679 if (errno != ENOENT) {
3680 err = got_error_from_errno2("lstat", arg);
3681 goto done;
3683 sb.st_mode = 0;
3685 if (S_ISLNK(sb.st_mode)) {
3687 * We cannot use realpath(3) with symlinks since we want to
3688 * operate on the symlink itself.
3689 * But we can make the path absolute, assuming it is relative
3690 * to the current working directory, and then canonicalize it.
3692 if (!got_path_is_absolute(arg)) {
3693 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3694 err = got_error_from_errno("asprintf");
3695 goto done;
3699 err = got_canonpath(abspath ? abspath : arg, canonpath,
3700 sizeof(canonpath));
3701 if (err)
3702 goto done;
3703 resolved = strdup(canonpath);
3704 if (resolved == NULL) {
3705 err = got_error_from_errno("strdup");
3706 goto done;
3708 } else {
3709 resolved = realpath(arg, NULL);
3710 if (resolved == NULL) {
3711 if (errno != ENOENT) {
3712 err = got_error_from_errno2("realpath", arg);
3713 goto done;
3715 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3716 err = got_error_from_errno("asprintf");
3717 goto done;
3719 err = got_canonpath(abspath, canonpath,
3720 sizeof(canonpath));
3721 if (err)
3722 goto done;
3723 resolved = strdup(canonpath);
3724 if (resolved == NULL) {
3725 err = got_error_from_errno("strdup");
3726 goto done;
3731 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3732 strlen(got_worktree_get_root_path(worktree)))) {
3733 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3734 goto done;
3737 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3738 err = got_path_skip_common_ancestor(&path,
3739 got_worktree_get_root_path(worktree), resolved);
3740 if (err)
3741 goto done;
3742 } else {
3743 path = strdup("");
3744 if (path == NULL) {
3745 err = got_error_from_errno("strdup");
3746 goto done;
3750 /* XXX status walk can't deal with trailing slash! */
3751 len = strlen(path);
3752 while (len > 0 && path[len - 1] == '/') {
3753 path[len - 1] = '\0';
3754 len--;
3756 done:
3757 free(abspath);
3758 free(resolved);
3759 free(cwd);
3760 if (err == NULL)
3761 *wt_path = path;
3762 else
3763 free(path);
3764 return err;
3767 struct schedule_addition_args {
3768 struct got_worktree *worktree;
3769 struct got_fileindex *fileindex;
3770 got_worktree_checkout_cb progress_cb;
3771 void *progress_arg;
3772 struct got_repository *repo;
3775 static const struct got_error *
3776 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3777 const char *relpath, struct got_object_id *blob_id,
3778 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3779 int dirfd, const char *de_name)
3781 struct schedule_addition_args *a = arg;
3782 const struct got_error *err = NULL;
3783 struct got_fileindex_entry *ie;
3784 struct stat sb;
3785 char *ondisk_path;
3787 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3788 relpath) == -1)
3789 return got_error_from_errno("asprintf");
3791 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3792 if (ie) {
3793 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3794 de_name, a->repo);
3795 if (err)
3796 goto done;
3797 /* Re-adding an existing entry is a no-op. */
3798 if (status == GOT_STATUS_ADD)
3799 goto done;
3800 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3801 if (err)
3802 goto done;
3805 if (status != GOT_STATUS_UNVERSIONED) {
3806 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3807 goto done;
3810 err = got_fileindex_entry_alloc(&ie, relpath);
3811 if (err)
3812 goto done;
3813 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3814 relpath, NULL, NULL, 1);
3815 if (err) {
3816 got_fileindex_entry_free(ie);
3817 goto done;
3819 err = got_fileindex_entry_add(a->fileindex, ie);
3820 if (err) {
3821 got_fileindex_entry_free(ie);
3822 goto done;
3824 done:
3825 free(ondisk_path);
3826 if (err)
3827 return err;
3828 if (status == GOT_STATUS_ADD)
3829 return NULL;
3830 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3833 const struct got_error *
3834 got_worktree_schedule_add(struct got_worktree *worktree,
3835 struct got_pathlist_head *paths,
3836 got_worktree_checkout_cb progress_cb, void *progress_arg,
3837 struct got_repository *repo, int no_ignores)
3839 struct got_fileindex *fileindex = NULL;
3840 char *fileindex_path = NULL;
3841 const struct got_error *err = NULL, *sync_err, *unlockerr;
3842 struct got_pathlist_entry *pe;
3843 struct schedule_addition_args saa;
3845 err = lock_worktree(worktree, LOCK_EX);
3846 if (err)
3847 return err;
3849 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3850 if (err)
3851 goto done;
3853 saa.worktree = worktree;
3854 saa.fileindex = fileindex;
3855 saa.progress_cb = progress_cb;
3856 saa.progress_arg = progress_arg;
3857 saa.repo = repo;
3859 TAILQ_FOREACH(pe, paths, entry) {
3860 err = worktree_status(worktree, pe->path, fileindex, repo,
3861 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3862 if (err)
3863 break;
3865 sync_err = sync_fileindex(fileindex, fileindex_path);
3866 if (sync_err && err == NULL)
3867 err = sync_err;
3868 done:
3869 free(fileindex_path);
3870 if (fileindex)
3871 got_fileindex_free(fileindex);
3872 unlockerr = lock_worktree(worktree, LOCK_SH);
3873 if (unlockerr && err == NULL)
3874 err = unlockerr;
3875 return err;
3878 struct schedule_deletion_args {
3879 struct got_worktree *worktree;
3880 struct got_fileindex *fileindex;
3881 got_worktree_delete_cb progress_cb;
3882 void *progress_arg;
3883 struct got_repository *repo;
3884 int delete_local_mods;
3885 int keep_on_disk;
3886 const char *status_codes;
3889 static const struct got_error *
3890 schedule_for_deletion(void *arg, unsigned char status,
3891 unsigned char staged_status, const char *relpath,
3892 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3893 struct got_object_id *commit_id, int dirfd, const char *de_name)
3895 struct schedule_deletion_args *a = arg;
3896 const struct got_error *err = NULL;
3897 struct got_fileindex_entry *ie = NULL;
3898 struct stat sb;
3899 char *ondisk_path;
3901 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3902 if (ie == NULL)
3903 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3905 staged_status = get_staged_status(ie);
3906 if (staged_status != GOT_STATUS_NO_CHANGE) {
3907 if (staged_status == GOT_STATUS_DELETE)
3908 return NULL;
3909 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3912 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3913 relpath) == -1)
3914 return got_error_from_errno("asprintf");
3916 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3917 a->repo);
3918 if (err)
3919 goto done;
3921 if (a->status_codes) {
3922 size_t ncodes = strlen(a->status_codes);
3923 int i;
3924 for (i = 0; i < ncodes ; i++) {
3925 if (status == a->status_codes[i])
3926 break;
3928 if (i == ncodes) {
3929 /* Do not delete files in non-matching status. */
3930 free(ondisk_path);
3931 return NULL;
3933 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3934 a->status_codes[i] != GOT_STATUS_MISSING) {
3935 static char msg[64];
3936 snprintf(msg, sizeof(msg),
3937 "invalid status code '%c'", a->status_codes[i]);
3938 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3939 goto done;
3943 if (status != GOT_STATUS_NO_CHANGE) {
3944 if (status == GOT_STATUS_DELETE)
3945 goto done;
3946 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3947 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3948 goto done;
3950 if (status != GOT_STATUS_MODIFY &&
3951 status != GOT_STATUS_MISSING) {
3952 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3953 goto done;
3957 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3958 size_t root_len;
3960 if (dirfd != -1) {
3961 if (unlinkat(dirfd, de_name, 0) != 0) {
3962 err = got_error_from_errno2("unlinkat",
3963 ondisk_path);
3964 goto done;
3966 } else if (unlink(ondisk_path) != 0) {
3967 err = got_error_from_errno2("unlink", ondisk_path);
3968 goto done;
3971 root_len = strlen(a->worktree->root_path);
3972 do {
3973 char *parent;
3974 err = got_path_dirname(&parent, ondisk_path);
3975 if (err)
3976 goto done;
3977 free(ondisk_path);
3978 ondisk_path = parent;
3979 if (rmdir(ondisk_path) == -1) {
3980 if (errno != ENOTEMPTY)
3981 err = got_error_from_errno2("rmdir",
3982 ondisk_path);
3983 break;
3985 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3986 strlen(ondisk_path), root_len) != 0);
3989 got_fileindex_entry_mark_deleted_from_disk(ie);
3990 done:
3991 free(ondisk_path);
3992 if (err)
3993 return err;
3994 if (status == GOT_STATUS_DELETE)
3995 return NULL;
3996 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3997 staged_status, relpath);
4000 const struct got_error *
4001 got_worktree_schedule_delete(struct got_worktree *worktree,
4002 struct got_pathlist_head *paths, int delete_local_mods,
4003 const char *status_codes,
4004 got_worktree_delete_cb progress_cb, void *progress_arg,
4005 struct got_repository *repo, int keep_on_disk)
4007 struct got_fileindex *fileindex = NULL;
4008 char *fileindex_path = NULL;
4009 const struct got_error *err = NULL, *sync_err, *unlockerr;
4010 struct got_pathlist_entry *pe;
4011 struct schedule_deletion_args sda;
4013 err = lock_worktree(worktree, LOCK_EX);
4014 if (err)
4015 return err;
4017 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4018 if (err)
4019 goto done;
4021 sda.worktree = worktree;
4022 sda.fileindex = fileindex;
4023 sda.progress_cb = progress_cb;
4024 sda.progress_arg = progress_arg;
4025 sda.repo = repo;
4026 sda.delete_local_mods = delete_local_mods;
4027 sda.keep_on_disk = keep_on_disk;
4028 sda.status_codes = status_codes;
4030 TAILQ_FOREACH(pe, paths, entry) {
4031 err = worktree_status(worktree, pe->path, fileindex, repo,
4032 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4033 if (err)
4034 break;
4036 sync_err = sync_fileindex(fileindex, fileindex_path);
4037 if (sync_err && err == NULL)
4038 err = sync_err;
4039 done:
4040 free(fileindex_path);
4041 if (fileindex)
4042 got_fileindex_free(fileindex);
4043 unlockerr = lock_worktree(worktree, LOCK_SH);
4044 if (unlockerr && err == NULL)
4045 err = unlockerr;
4046 return err;
4049 static const struct got_error *
4050 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4052 const struct got_error *err = NULL;
4053 char *line = NULL;
4054 size_t linesize = 0, n;
4055 ssize_t linelen;
4057 linelen = getline(&line, &linesize, infile);
4058 if (linelen == -1) {
4059 if (ferror(infile)) {
4060 err = got_error_from_errno("getline");
4061 goto done;
4063 return NULL;
4065 if (outfile) {
4066 n = fwrite(line, 1, linelen, outfile);
4067 if (n != linelen) {
4068 err = got_ferror(outfile, GOT_ERR_IO);
4069 goto done;
4072 if (rejectfile) {
4073 n = fwrite(line, 1, linelen, rejectfile);
4074 if (n != linelen)
4075 err = got_ferror(outfile, GOT_ERR_IO);
4077 done:
4078 free(line);
4079 return err;
4082 static const struct got_error *
4083 skip_one_line(FILE *f)
4085 char *line = NULL;
4086 size_t linesize = 0;
4087 ssize_t linelen;
4089 linelen = getline(&line, &linesize, f);
4090 if (linelen == -1) {
4091 if (ferror(f))
4092 return got_error_from_errno("getline");
4093 return NULL;
4095 free(line);
4096 return NULL;
4099 static const struct got_error *
4100 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4101 int start_old, int end_old, int start_new, int end_new,
4102 FILE *outfile, FILE *rejectfile)
4104 const struct got_error *err;
4106 /* Copy old file's lines leading up to patch. */
4107 while (!feof(f1) && *line_cur1 < start_old) {
4108 err = copy_one_line(f1, outfile, NULL);
4109 if (err)
4110 return err;
4111 (*line_cur1)++;
4113 /* Skip new file's lines leading up to patch. */
4114 while (!feof(f2) && *line_cur2 < start_new) {
4115 if (rejectfile)
4116 err = copy_one_line(f2, NULL, rejectfile);
4117 else
4118 err = skip_one_line(f2);
4119 if (err)
4120 return err;
4121 (*line_cur2)++;
4123 /* Copy patched lines. */
4124 while (!feof(f2) && *line_cur2 <= end_new) {
4125 err = copy_one_line(f2, outfile, NULL);
4126 if (err)
4127 return err;
4128 (*line_cur2)++;
4130 /* Skip over old file's replaced lines. */
4131 while (!feof(f1) && *line_cur1 <= end_old) {
4132 if (rejectfile)
4133 err = copy_one_line(f1, NULL, rejectfile);
4134 else
4135 err = skip_one_line(f1);
4136 if (err)
4137 return err;
4138 (*line_cur1)++;
4141 return NULL;
4144 static const struct got_error *
4145 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4146 FILE *outfile, FILE *rejectfile)
4148 const struct got_error *err;
4150 if (outfile) {
4151 /* Copy old file's lines until EOF. */
4152 while (!feof(f1)) {
4153 err = copy_one_line(f1, outfile, NULL);
4154 if (err)
4155 return err;
4156 (*line_cur1)++;
4159 if (rejectfile) {
4160 /* Copy new file's lines until EOF. */
4161 while (!feof(f2)) {
4162 err = copy_one_line(f2, NULL, rejectfile);
4163 if (err)
4164 return err;
4165 (*line_cur2)++;
4169 return NULL;
4172 static const struct got_error *
4173 apply_or_reject_change(int *choice, int *nchunks_used,
4174 struct diff_result *diff_result, int n,
4175 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4176 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4177 got_worktree_patch_cb patch_cb, void *patch_arg)
4179 const struct got_error *err = NULL;
4180 struct diff_chunk_context cc = {};
4181 int start_old, end_old, start_new, end_new;
4182 FILE *hunkfile;
4183 struct diff_output_unidiff_state *diff_state;
4184 struct diff_input_info diff_info;
4185 int rc;
4187 *choice = GOT_PATCH_CHOICE_NONE;
4189 /* Get changed line numbers without context lines for copy_change(). */
4190 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4191 start_old = cc.left.start;
4192 end_old = cc.left.end;
4193 start_new = cc.right.start;
4194 end_new = cc.right.end;
4196 /* Get the same change with context lines for display. */
4197 memset(&cc, 0, sizeof(cc));
4198 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4200 memset(&diff_info, 0, sizeof(diff_info));
4201 diff_info.left_path = relpath;
4202 diff_info.right_path = relpath;
4204 diff_state = diff_output_unidiff_state_alloc();
4205 if (diff_state == NULL)
4206 return got_error_set_errno(ENOMEM,
4207 "diff_output_unidiff_state_alloc");
4209 hunkfile = got_opentemp();
4210 if (hunkfile == NULL) {
4211 err = got_error_from_errno("got_opentemp");
4212 goto done;
4215 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4216 diff_result, &cc);
4217 if (rc != DIFF_RC_OK) {
4218 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4219 goto done;
4222 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4223 err = got_ferror(hunkfile, GOT_ERR_IO);
4224 goto done;
4227 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4228 hunkfile, changeno, nchanges);
4229 if (err)
4230 goto done;
4232 switch (*choice) {
4233 case GOT_PATCH_CHOICE_YES:
4234 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4235 end_old, start_new, end_new, outfile, rejectfile);
4236 break;
4237 case GOT_PATCH_CHOICE_NO:
4238 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4239 end_old, start_new, end_new, rejectfile, outfile);
4240 break;
4241 case GOT_PATCH_CHOICE_QUIT:
4242 break;
4243 default:
4244 err = got_error(GOT_ERR_PATCH_CHOICE);
4245 break;
4247 done:
4248 diff_output_unidiff_state_free(diff_state);
4249 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4250 err = got_error_from_errno("fclose");
4251 return err;
4254 struct revert_file_args {
4255 struct got_worktree *worktree;
4256 struct got_fileindex *fileindex;
4257 got_worktree_checkout_cb progress_cb;
4258 void *progress_arg;
4259 got_worktree_patch_cb patch_cb;
4260 void *patch_arg;
4261 struct got_repository *repo;
4264 static const struct got_error *
4265 create_patched_content(char **path_outfile, int reverse_patch,
4266 struct got_object_id *blob_id, const char *path2,
4267 int dirfd2, const char *de_name2,
4268 const char *relpath, struct got_repository *repo,
4269 got_worktree_patch_cb patch_cb, void *patch_arg)
4271 const struct got_error *err, *free_err;
4272 struct got_blob_object *blob = NULL;
4273 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4274 int fd2 = -1;
4275 char link_target[PATH_MAX];
4276 ssize_t link_len = 0;
4277 char *path1 = NULL, *id_str = NULL;
4278 struct stat sb2;
4279 struct got_diffreg_result *diffreg_result = NULL;
4280 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4281 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4283 *path_outfile = NULL;
4285 err = got_object_id_str(&id_str, blob_id);
4286 if (err)
4287 return err;
4289 if (dirfd2 != -1) {
4290 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4291 if (fd2 == -1) {
4292 if (errno != ELOOP) {
4293 err = got_error_from_errno2("openat", path2);
4294 goto done;
4296 link_len = readlinkat(dirfd2, de_name2,
4297 link_target, sizeof(link_target));
4298 if (link_len == -1)
4299 return got_error_from_errno2("readlinkat", path2);
4300 sb2.st_mode = S_IFLNK;
4301 sb2.st_size = link_len;
4303 } else {
4304 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4305 if (fd2 == -1) {
4306 if (errno != ELOOP) {
4307 err = got_error_from_errno2("open", path2);
4308 goto done;
4310 link_len = readlink(path2, link_target,
4311 sizeof(link_target));
4312 if (link_len == -1)
4313 return got_error_from_errno2("readlink", path2);
4314 sb2.st_mode = S_IFLNK;
4315 sb2.st_size = link_len;
4318 if (fd2 != -1) {
4319 if (fstat(fd2, &sb2) == -1) {
4320 err = got_error_from_errno2("fstat", path2);
4321 goto done;
4324 f2 = fdopen(fd2, "r");
4325 if (f2 == NULL) {
4326 err = got_error_from_errno2("fdopen", path2);
4327 goto done;
4329 fd2 = -1;
4330 } else {
4331 size_t n;
4332 f2 = got_opentemp();
4333 if (f2 == NULL) {
4334 err = got_error_from_errno2("got_opentemp", path2);
4335 goto done;
4337 n = fwrite(link_target, 1, link_len, f2);
4338 if (n != link_len) {
4339 err = got_ferror(f2, GOT_ERR_IO);
4340 goto done;
4342 if (fflush(f2) == EOF) {
4343 err = got_error_from_errno("fflush");
4344 goto done;
4346 rewind(f2);
4349 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4350 if (err)
4351 goto done;
4353 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4354 if (err)
4355 goto done;
4357 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4358 if (err)
4359 goto done;
4361 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4362 NULL);
4363 if (err)
4364 goto done;
4366 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4367 if (err)
4368 goto done;
4370 if (fseek(f1, 0L, SEEK_SET) == -1)
4371 return got_ferror(f1, GOT_ERR_IO);
4372 if (fseek(f2, 0L, SEEK_SET) == -1)
4373 return got_ferror(f2, GOT_ERR_IO);
4375 /* Count the number of actual changes in the diff result. */
4376 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4377 struct diff_chunk_context cc = {};
4378 diff_chunk_context_load_change(&cc, &nchunks_used,
4379 diffreg_result->result, n, 0);
4380 nchanges++;
4382 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4383 int choice;
4384 err = apply_or_reject_change(&choice, &nchunks_used,
4385 diffreg_result->result, n, relpath, f1, f2,
4386 &line_cur1, &line_cur2,
4387 reverse_patch ? NULL : outfile,
4388 reverse_patch ? outfile : NULL,
4389 ++i, nchanges, patch_cb, patch_arg);
4390 if (err)
4391 goto done;
4392 if (choice == GOT_PATCH_CHOICE_YES)
4393 have_content = 1;
4394 else if (choice == GOT_PATCH_CHOICE_QUIT)
4395 break;
4397 if (have_content) {
4398 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4399 reverse_patch ? NULL : outfile,
4400 reverse_patch ? outfile : NULL);
4401 if (err)
4402 goto done;
4404 if (!S_ISLNK(sb2.st_mode)) {
4405 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4406 err = got_error_from_errno2("fchmod", path2);
4407 goto done;
4411 done:
4412 free(id_str);
4413 if (blob)
4414 got_object_blob_close(blob);
4415 free_err = got_diffreg_result_free(diffreg_result);
4416 if (err == NULL)
4417 err = free_err;
4418 if (f1 && fclose(f1) == EOF && err == NULL)
4419 err = got_error_from_errno2("fclose", path1);
4420 if (f2 && fclose(f2) == EOF && err == NULL)
4421 err = got_error_from_errno2("fclose", path2);
4422 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4423 err = got_error_from_errno2("close", path2);
4424 if (outfile && fclose(outfile) == EOF && err == NULL)
4425 err = got_error_from_errno2("fclose", *path_outfile);
4426 if (path1 && unlink(path1) == -1 && err == NULL)
4427 err = got_error_from_errno2("unlink", path1);
4428 if (err || !have_content) {
4429 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4430 err = got_error_from_errno2("unlink", *path_outfile);
4431 free(*path_outfile);
4432 *path_outfile = NULL;
4434 free(path1);
4435 return err;
4438 static const struct got_error *
4439 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4440 const char *relpath, struct got_object_id *blob_id,
4441 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4442 int dirfd, const char *de_name)
4444 struct revert_file_args *a = arg;
4445 const struct got_error *err = NULL;
4446 char *parent_path = NULL;
4447 struct got_fileindex_entry *ie;
4448 struct got_tree_object *tree = NULL;
4449 struct got_object_id *tree_id = NULL;
4450 const struct got_tree_entry *te = NULL;
4451 char *tree_path = NULL, *te_name;
4452 char *ondisk_path = NULL, *path_content = NULL;
4453 struct got_blob_object *blob = NULL;
4455 /* Reverting a staged deletion is a no-op. */
4456 if (status == GOT_STATUS_DELETE &&
4457 staged_status != GOT_STATUS_NO_CHANGE)
4458 return NULL;
4460 if (status == GOT_STATUS_UNVERSIONED)
4461 return (*a->progress_cb)(a->progress_arg,
4462 GOT_STATUS_UNVERSIONED, relpath);
4464 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4465 if (ie == NULL)
4466 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4468 /* Construct in-repository path of tree which contains this blob. */
4469 err = got_path_dirname(&parent_path, ie->path);
4470 if (err) {
4471 if (err->code != GOT_ERR_BAD_PATH)
4472 goto done;
4473 parent_path = strdup("/");
4474 if (parent_path == NULL) {
4475 err = got_error_from_errno("strdup");
4476 goto done;
4479 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4480 tree_path = strdup(parent_path);
4481 if (tree_path == NULL) {
4482 err = got_error_from_errno("strdup");
4483 goto done;
4485 } else {
4486 if (got_path_is_root_dir(parent_path)) {
4487 tree_path = strdup(a->worktree->path_prefix);
4488 if (tree_path == NULL) {
4489 err = got_error_from_errno("strdup");
4490 goto done;
4492 } else {
4493 if (asprintf(&tree_path, "%s/%s",
4494 a->worktree->path_prefix, parent_path) == -1) {
4495 err = got_error_from_errno("asprintf");
4496 goto done;
4501 err = got_object_id_by_path(&tree_id, a->repo,
4502 a->worktree->base_commit_id, tree_path);
4503 if (err) {
4504 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4505 (status == GOT_STATUS_ADD ||
4506 staged_status == GOT_STATUS_ADD)))
4507 goto done;
4508 } else {
4509 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4510 if (err)
4511 goto done;
4513 err = got_path_basename(&te_name, ie->path);
4514 if (err)
4515 goto done;
4517 te = got_object_tree_find_entry(tree, te_name);
4518 free(te_name);
4519 if (te == NULL && status != GOT_STATUS_ADD &&
4520 staged_status != GOT_STATUS_ADD) {
4521 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4522 goto done;
4526 switch (status) {
4527 case GOT_STATUS_ADD:
4528 if (a->patch_cb) {
4529 int choice = GOT_PATCH_CHOICE_NONE;
4530 err = (*a->patch_cb)(&choice, a->patch_arg,
4531 status, ie->path, NULL, 1, 1);
4532 if (err)
4533 goto done;
4534 if (choice != GOT_PATCH_CHOICE_YES)
4535 break;
4537 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4538 ie->path);
4539 if (err)
4540 goto done;
4541 got_fileindex_entry_remove(a->fileindex, ie);
4542 break;
4543 case GOT_STATUS_DELETE:
4544 if (a->patch_cb) {
4545 int choice = GOT_PATCH_CHOICE_NONE;
4546 err = (*a->patch_cb)(&choice, a->patch_arg,
4547 status, ie->path, NULL, 1, 1);
4548 if (err)
4549 goto done;
4550 if (choice != GOT_PATCH_CHOICE_YES)
4551 break;
4553 /* fall through */
4554 case GOT_STATUS_MODIFY:
4555 case GOT_STATUS_MODE_CHANGE:
4556 case GOT_STATUS_CONFLICT:
4557 case GOT_STATUS_MISSING: {
4558 struct got_object_id id;
4559 if (staged_status == GOT_STATUS_ADD ||
4560 staged_status == GOT_STATUS_MODIFY) {
4561 memcpy(id.sha1, ie->staged_blob_sha1,
4562 SHA1_DIGEST_LENGTH);
4563 } else
4564 memcpy(id.sha1, ie->blob_sha1,
4565 SHA1_DIGEST_LENGTH);
4566 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4567 if (err)
4568 goto done;
4570 if (asprintf(&ondisk_path, "%s/%s",
4571 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4572 err = got_error_from_errno("asprintf");
4573 goto done;
4576 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4577 status == GOT_STATUS_CONFLICT)) {
4578 int is_bad_symlink = 0;
4579 err = create_patched_content(&path_content, 1, &id,
4580 ondisk_path, dirfd, de_name, ie->path, a->repo,
4581 a->patch_cb, a->patch_arg);
4582 if (err || path_content == NULL)
4583 break;
4584 if (te && S_ISLNK(te->mode)) {
4585 if (unlink(path_content) == -1) {
4586 err = got_error_from_errno2("unlink",
4587 path_content);
4588 break;
4590 err = install_symlink(&is_bad_symlink,
4591 a->worktree, ondisk_path, ie->path,
4592 blob, 0, 1, 0, a->repo,
4593 a->progress_cb, a->progress_arg);
4594 } else {
4595 if (rename(path_content, ondisk_path) == -1) {
4596 err = got_error_from_errno3("rename",
4597 path_content, ondisk_path);
4598 goto done;
4601 } else {
4602 int is_bad_symlink = 0;
4603 if (te && S_ISLNK(te->mode)) {
4604 err = install_symlink(&is_bad_symlink,
4605 a->worktree, ondisk_path, ie->path,
4606 blob, 0, 1, 0, a->repo,
4607 a->progress_cb, a->progress_arg);
4608 } else {
4609 err = install_blob(a->worktree, ondisk_path,
4610 ie->path,
4611 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4612 got_fileindex_perms_to_st(ie), blob,
4613 0, 1, 0, 0, a->repo,
4614 a->progress_cb, a->progress_arg);
4616 if (err)
4617 goto done;
4618 if (status == GOT_STATUS_DELETE ||
4619 status == GOT_STATUS_MODE_CHANGE) {
4620 err = got_fileindex_entry_update(ie,
4621 a->worktree->root_fd, relpath,
4622 blob->id.sha1,
4623 a->worktree->base_commit_id->sha1, 1);
4624 if (err)
4625 goto done;
4627 if (is_bad_symlink) {
4628 got_fileindex_entry_filetype_set(ie,
4629 GOT_FILEIDX_MODE_BAD_SYMLINK);
4632 break;
4634 default:
4635 break;
4637 done:
4638 free(ondisk_path);
4639 free(path_content);
4640 free(parent_path);
4641 free(tree_path);
4642 if (blob)
4643 got_object_blob_close(blob);
4644 if (tree)
4645 got_object_tree_close(tree);
4646 free(tree_id);
4647 return err;
4650 const struct got_error *
4651 got_worktree_revert(struct got_worktree *worktree,
4652 struct got_pathlist_head *paths,
4653 got_worktree_checkout_cb progress_cb, void *progress_arg,
4654 got_worktree_patch_cb patch_cb, void *patch_arg,
4655 struct got_repository *repo)
4657 struct got_fileindex *fileindex = NULL;
4658 char *fileindex_path = NULL;
4659 const struct got_error *err = NULL, *unlockerr = NULL;
4660 const struct got_error *sync_err = NULL;
4661 struct got_pathlist_entry *pe;
4662 struct revert_file_args rfa;
4664 err = lock_worktree(worktree, LOCK_EX);
4665 if (err)
4666 return err;
4668 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4669 if (err)
4670 goto done;
4672 rfa.worktree = worktree;
4673 rfa.fileindex = fileindex;
4674 rfa.progress_cb = progress_cb;
4675 rfa.progress_arg = progress_arg;
4676 rfa.patch_cb = patch_cb;
4677 rfa.patch_arg = patch_arg;
4678 rfa.repo = repo;
4679 TAILQ_FOREACH(pe, paths, entry) {
4680 err = worktree_status(worktree, pe->path, fileindex, repo,
4681 revert_file, &rfa, NULL, NULL, 0, 0);
4682 if (err)
4683 break;
4685 sync_err = sync_fileindex(fileindex, fileindex_path);
4686 if (sync_err && err == NULL)
4687 err = sync_err;
4688 done:
4689 free(fileindex_path);
4690 if (fileindex)
4691 got_fileindex_free(fileindex);
4692 unlockerr = lock_worktree(worktree, LOCK_SH);
4693 if (unlockerr && err == NULL)
4694 err = unlockerr;
4695 return err;
4698 static void
4699 free_commitable(struct got_commitable *ct)
4701 free(ct->path);
4702 free(ct->in_repo_path);
4703 free(ct->ondisk_path);
4704 free(ct->blob_id);
4705 free(ct->base_blob_id);
4706 free(ct->staged_blob_id);
4707 free(ct->base_commit_id);
4708 free(ct);
4711 struct collect_commitables_arg {
4712 struct got_pathlist_head *commitable_paths;
4713 struct got_repository *repo;
4714 struct got_worktree *worktree;
4715 struct got_fileindex *fileindex;
4716 int have_staged_files;
4717 int allow_bad_symlinks;
4720 static const struct got_error *
4721 collect_commitables(void *arg, unsigned char status,
4722 unsigned char staged_status, const char *relpath,
4723 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4724 struct got_object_id *commit_id, int dirfd, const char *de_name)
4726 struct collect_commitables_arg *a = arg;
4727 const struct got_error *err = NULL;
4728 struct got_commitable *ct = NULL;
4729 struct got_pathlist_entry *new = NULL;
4730 char *parent_path = NULL, *path = NULL;
4731 struct stat sb;
4733 if (a->have_staged_files) {
4734 if (staged_status != GOT_STATUS_MODIFY &&
4735 staged_status != GOT_STATUS_ADD &&
4736 staged_status != GOT_STATUS_DELETE)
4737 return NULL;
4738 } else {
4739 if (status == GOT_STATUS_CONFLICT)
4740 return got_error(GOT_ERR_COMMIT_CONFLICT);
4742 if (status != GOT_STATUS_MODIFY &&
4743 status != GOT_STATUS_MODE_CHANGE &&
4744 status != GOT_STATUS_ADD &&
4745 status != GOT_STATUS_DELETE)
4746 return NULL;
4749 if (asprintf(&path, "/%s", relpath) == -1) {
4750 err = got_error_from_errno("asprintf");
4751 goto done;
4753 if (strcmp(path, "/") == 0) {
4754 parent_path = strdup("");
4755 if (parent_path == NULL)
4756 return got_error_from_errno("strdup");
4757 } else {
4758 err = got_path_dirname(&parent_path, path);
4759 if (err)
4760 return err;
4763 ct = calloc(1, sizeof(*ct));
4764 if (ct == NULL) {
4765 err = got_error_from_errno("calloc");
4766 goto done;
4769 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4770 relpath) == -1) {
4771 err = got_error_from_errno("asprintf");
4772 goto done;
4775 if (staged_status == GOT_STATUS_ADD ||
4776 staged_status == GOT_STATUS_MODIFY) {
4777 struct got_fileindex_entry *ie;
4778 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4779 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4780 case GOT_FILEIDX_MODE_REGULAR_FILE:
4781 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4782 ct->mode = S_IFREG;
4783 break;
4784 case GOT_FILEIDX_MODE_SYMLINK:
4785 ct->mode = S_IFLNK;
4786 break;
4787 default:
4788 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4789 goto done;
4791 ct->mode |= got_fileindex_entry_perms_get(ie);
4792 } else if (status != GOT_STATUS_DELETE &&
4793 staged_status != GOT_STATUS_DELETE) {
4794 if (dirfd != -1) {
4795 if (fstatat(dirfd, de_name, &sb,
4796 AT_SYMLINK_NOFOLLOW) == -1) {
4797 err = got_error_from_errno2("fstatat",
4798 ct->ondisk_path);
4799 goto done;
4801 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4802 err = got_error_from_errno2("lstat", ct->ondisk_path);
4803 goto done;
4805 ct->mode = sb.st_mode;
4808 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4809 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4810 relpath) == -1) {
4811 err = got_error_from_errno("asprintf");
4812 goto done;
4815 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4816 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4817 int is_bad_symlink;
4818 char target_path[PATH_MAX];
4819 ssize_t target_len;
4820 target_len = readlink(ct->ondisk_path, target_path,
4821 sizeof(target_path));
4822 if (target_len == -1) {
4823 err = got_error_from_errno2("readlink",
4824 ct->ondisk_path);
4825 goto done;
4827 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4828 target_len, ct->ondisk_path, a->worktree->root_path);
4829 if (err)
4830 goto done;
4831 if (is_bad_symlink) {
4832 err = got_error_path(ct->ondisk_path,
4833 GOT_ERR_BAD_SYMLINK);
4834 goto done;
4839 ct->status = status;
4840 ct->staged_status = staged_status;
4841 ct->blob_id = NULL; /* will be filled in when blob gets created */
4842 if (ct->status != GOT_STATUS_ADD &&
4843 ct->staged_status != GOT_STATUS_ADD) {
4844 ct->base_blob_id = got_object_id_dup(blob_id);
4845 if (ct->base_blob_id == NULL) {
4846 err = got_error_from_errno("got_object_id_dup");
4847 goto done;
4849 ct->base_commit_id = got_object_id_dup(commit_id);
4850 if (ct->base_commit_id == NULL) {
4851 err = got_error_from_errno("got_object_id_dup");
4852 goto done;
4855 if (ct->staged_status == GOT_STATUS_ADD ||
4856 ct->staged_status == GOT_STATUS_MODIFY) {
4857 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4858 if (ct->staged_blob_id == NULL) {
4859 err = got_error_from_errno("got_object_id_dup");
4860 goto done;
4863 ct->path = strdup(path);
4864 if (ct->path == NULL) {
4865 err = got_error_from_errno("strdup");
4866 goto done;
4868 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4869 done:
4870 if (ct && (err || new == NULL))
4871 free_commitable(ct);
4872 free(parent_path);
4873 free(path);
4874 return err;
4877 static const struct got_error *write_tree(struct got_object_id **, int *,
4878 struct got_tree_object *, const char *, struct got_pathlist_head *,
4879 got_worktree_status_cb status_cb, void *status_arg,
4880 struct got_repository *);
4882 static const struct got_error *
4883 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4884 struct got_tree_entry *te, const char *parent_path,
4885 struct got_pathlist_head *commitable_paths,
4886 got_worktree_status_cb status_cb, void *status_arg,
4887 struct got_repository *repo)
4889 const struct got_error *err = NULL;
4890 struct got_tree_object *subtree;
4891 char *subpath;
4893 if (asprintf(&subpath, "%s%s%s", parent_path,
4894 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4895 return got_error_from_errno("asprintf");
4897 err = got_object_open_as_tree(&subtree, repo, &te->id);
4898 if (err)
4899 return err;
4901 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4902 commitable_paths, status_cb, status_arg, repo);
4903 got_object_tree_close(subtree);
4904 free(subpath);
4905 return err;
4908 static const struct got_error *
4909 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4911 const struct got_error *err = NULL;
4912 char *ct_parent_path = NULL;
4914 *match = 0;
4916 if (strchr(ct->in_repo_path, '/') == NULL) {
4917 *match = got_path_is_root_dir(path);
4918 return NULL;
4921 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4922 if (err)
4923 return err;
4924 *match = (strcmp(path, ct_parent_path) == 0);
4925 free(ct_parent_path);
4926 return err;
4929 static mode_t
4930 get_ct_file_mode(struct got_commitable *ct)
4932 if (S_ISLNK(ct->mode))
4933 return S_IFLNK;
4935 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4938 static const struct got_error *
4939 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4940 struct got_tree_entry *te, struct got_commitable *ct)
4942 const struct got_error *err = NULL;
4944 *new_te = NULL;
4946 err = got_object_tree_entry_dup(new_te, te);
4947 if (err)
4948 goto done;
4950 (*new_te)->mode = get_ct_file_mode(ct);
4952 if (ct->staged_status == GOT_STATUS_MODIFY)
4953 memcpy(&(*new_te)->id, ct->staged_blob_id,
4954 sizeof((*new_te)->id));
4955 else
4956 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4957 done:
4958 if (err && *new_te) {
4959 free(*new_te);
4960 *new_te = NULL;
4962 return err;
4965 static const struct got_error *
4966 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4967 struct got_commitable *ct)
4969 const struct got_error *err = NULL;
4970 char *ct_name = NULL;
4972 *new_te = NULL;
4974 *new_te = calloc(1, sizeof(**new_te));
4975 if (*new_te == NULL)
4976 return got_error_from_errno("calloc");
4978 err = got_path_basename(&ct_name, ct->path);
4979 if (err)
4980 goto done;
4981 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4982 sizeof((*new_te)->name)) {
4983 err = got_error(GOT_ERR_NO_SPACE);
4984 goto done;
4987 (*new_te)->mode = get_ct_file_mode(ct);
4989 if (ct->staged_status == GOT_STATUS_ADD)
4990 memcpy(&(*new_te)->id, ct->staged_blob_id,
4991 sizeof((*new_te)->id));
4992 else
4993 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4994 done:
4995 free(ct_name);
4996 if (err && *new_te) {
4997 free(*new_te);
4998 *new_te = NULL;
5000 return err;
5003 static const struct got_error *
5004 insert_tree_entry(struct got_tree_entry *new_te,
5005 struct got_pathlist_head *paths)
5007 const struct got_error *err = NULL;
5008 struct got_pathlist_entry *new_pe;
5010 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5011 if (err)
5012 return err;
5013 if (new_pe == NULL)
5014 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5015 return NULL;
5018 static const struct got_error *
5019 report_ct_status(struct got_commitable *ct,
5020 got_worktree_status_cb status_cb, void *status_arg)
5022 const char *ct_path = ct->path;
5023 unsigned char status;
5025 while (ct_path[0] == '/')
5026 ct_path++;
5028 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5029 status = ct->staged_status;
5030 else
5031 status = ct->status;
5033 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5034 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5037 static const struct got_error *
5038 match_modified_subtree(int *modified, struct got_tree_entry *te,
5039 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5041 const struct got_error *err = NULL;
5042 struct got_pathlist_entry *pe;
5043 char *te_path;
5045 *modified = 0;
5047 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5048 got_path_is_root_dir(base_tree_path) ? "" : "/",
5049 te->name) == -1)
5050 return got_error_from_errno("asprintf");
5052 TAILQ_FOREACH(pe, commitable_paths, entry) {
5053 struct got_commitable *ct = pe->data;
5054 *modified = got_path_is_child(ct->in_repo_path, te_path,
5055 strlen(te_path));
5056 if (*modified)
5057 break;
5060 free(te_path);
5061 return err;
5064 static const struct got_error *
5065 match_deleted_or_modified_ct(struct got_commitable **ctp,
5066 struct got_tree_entry *te, const char *base_tree_path,
5067 struct got_pathlist_head *commitable_paths)
5069 const struct got_error *err = NULL;
5070 struct got_pathlist_entry *pe;
5072 *ctp = NULL;
5074 TAILQ_FOREACH(pe, commitable_paths, entry) {
5075 struct got_commitable *ct = pe->data;
5076 char *ct_name = NULL;
5077 int path_matches;
5079 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5080 if (ct->status != GOT_STATUS_MODIFY &&
5081 ct->status != GOT_STATUS_MODE_CHANGE &&
5082 ct->status != GOT_STATUS_DELETE)
5083 continue;
5084 } else {
5085 if (ct->staged_status != GOT_STATUS_MODIFY &&
5086 ct->staged_status != GOT_STATUS_DELETE)
5087 continue;
5090 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5091 continue;
5093 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5094 if (err)
5095 return err;
5096 if (!path_matches)
5097 continue;
5099 err = got_path_basename(&ct_name, pe->path);
5100 if (err)
5101 return err;
5103 if (strcmp(te->name, ct_name) != 0) {
5104 free(ct_name);
5105 continue;
5107 free(ct_name);
5109 *ctp = ct;
5110 break;
5113 return err;
5116 static const struct got_error *
5117 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5118 const char *child_path, const char *path_base_tree,
5119 struct got_pathlist_head *commitable_paths,
5120 got_worktree_status_cb status_cb, void *status_arg,
5121 struct got_repository *repo)
5123 const struct got_error *err = NULL;
5124 struct got_tree_entry *new_te;
5125 char *subtree_path;
5126 struct got_object_id *id = NULL;
5127 int nentries;
5129 *new_tep = NULL;
5131 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5132 got_path_is_root_dir(path_base_tree) ? "" : "/",
5133 child_path) == -1)
5134 return got_error_from_errno("asprintf");
5136 new_te = calloc(1, sizeof(*new_te));
5137 if (new_te == NULL)
5138 return got_error_from_errno("calloc");
5139 new_te->mode = S_IFDIR;
5141 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5142 sizeof(new_te->name)) {
5143 err = got_error(GOT_ERR_NO_SPACE);
5144 goto done;
5146 err = write_tree(&id, &nentries, NULL, subtree_path,
5147 commitable_paths, status_cb, status_arg, repo);
5148 if (err) {
5149 free(new_te);
5150 goto done;
5152 memcpy(&new_te->id, id, sizeof(new_te->id));
5153 done:
5154 free(id);
5155 free(subtree_path);
5156 if (err == NULL)
5157 *new_tep = new_te;
5158 return err;
5161 static const struct got_error *
5162 write_tree(struct got_object_id **new_tree_id, int *nentries,
5163 struct got_tree_object *base_tree, const char *path_base_tree,
5164 struct got_pathlist_head *commitable_paths,
5165 got_worktree_status_cb status_cb, void *status_arg,
5166 struct got_repository *repo)
5168 const struct got_error *err = NULL;
5169 struct got_pathlist_head paths;
5170 struct got_tree_entry *te, *new_te = NULL;
5171 struct got_pathlist_entry *pe;
5173 TAILQ_INIT(&paths);
5174 *nentries = 0;
5176 /* Insert, and recurse into, newly added entries first. */
5177 TAILQ_FOREACH(pe, commitable_paths, entry) {
5178 struct got_commitable *ct = pe->data;
5179 char *child_path = NULL, *slash;
5181 if ((ct->status != GOT_STATUS_ADD &&
5182 ct->staged_status != GOT_STATUS_ADD) ||
5183 (ct->flags & GOT_COMMITABLE_ADDED))
5184 continue;
5186 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5187 strlen(path_base_tree)))
5188 continue;
5190 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5191 ct->in_repo_path);
5192 if (err)
5193 goto done;
5195 slash = strchr(child_path, '/');
5196 if (slash == NULL) {
5197 err = alloc_added_blob_tree_entry(&new_te, ct);
5198 if (err)
5199 goto done;
5200 err = report_ct_status(ct, status_cb, status_arg);
5201 if (err)
5202 goto done;
5203 ct->flags |= GOT_COMMITABLE_ADDED;
5204 err = insert_tree_entry(new_te, &paths);
5205 if (err)
5206 goto done;
5207 (*nentries)++;
5208 } else {
5209 *slash = '\0'; /* trim trailing path components */
5210 if (base_tree == NULL ||
5211 got_object_tree_find_entry(base_tree, child_path)
5212 == NULL) {
5213 err = make_subtree_for_added_blob(&new_te,
5214 child_path, path_base_tree,
5215 commitable_paths, status_cb, status_arg,
5216 repo);
5217 if (err)
5218 goto done;
5219 err = insert_tree_entry(new_te, &paths);
5220 if (err)
5221 goto done;
5222 (*nentries)++;
5227 if (base_tree) {
5228 int i, nbase_entries;
5229 /* Handle modified and deleted entries. */
5230 nbase_entries = got_object_tree_get_nentries(base_tree);
5231 for (i = 0; i < nbase_entries; i++) {
5232 struct got_commitable *ct = NULL;
5234 te = got_object_tree_get_entry(base_tree, i);
5235 if (got_object_tree_entry_is_submodule(te)) {
5236 /* Entry is a submodule; just copy it. */
5237 err = got_object_tree_entry_dup(&new_te, te);
5238 if (err)
5239 goto done;
5240 err = insert_tree_entry(new_te, &paths);
5241 if (err)
5242 goto done;
5243 (*nentries)++;
5244 continue;
5247 if (S_ISDIR(te->mode)) {
5248 int modified;
5249 err = got_object_tree_entry_dup(&new_te, te);
5250 if (err)
5251 goto done;
5252 err = match_modified_subtree(&modified, te,
5253 path_base_tree, commitable_paths);
5254 if (err)
5255 goto done;
5256 /* Avoid recursion into unmodified subtrees. */
5257 if (modified) {
5258 struct got_object_id *new_id;
5259 int nsubentries;
5260 err = write_subtree(&new_id,
5261 &nsubentries, te,
5262 path_base_tree, commitable_paths,
5263 status_cb, status_arg, repo);
5264 if (err)
5265 goto done;
5266 if (nsubentries == 0) {
5267 /* All entries were deleted. */
5268 free(new_id);
5269 continue;
5271 memcpy(&new_te->id, new_id,
5272 sizeof(new_te->id));
5273 free(new_id);
5275 err = insert_tree_entry(new_te, &paths);
5276 if (err)
5277 goto done;
5278 (*nentries)++;
5279 continue;
5282 err = match_deleted_or_modified_ct(&ct, te,
5283 path_base_tree, commitable_paths);
5284 if (err)
5285 goto done;
5286 if (ct) {
5287 /* NB: Deleted entries get dropped here. */
5288 if (ct->status == GOT_STATUS_MODIFY ||
5289 ct->status == GOT_STATUS_MODE_CHANGE ||
5290 ct->staged_status == GOT_STATUS_MODIFY) {
5291 err = alloc_modified_blob_tree_entry(
5292 &new_te, te, ct);
5293 if (err)
5294 goto done;
5295 err = insert_tree_entry(new_te, &paths);
5296 if (err)
5297 goto done;
5298 (*nentries)++;
5300 err = report_ct_status(ct, status_cb,
5301 status_arg);
5302 if (err)
5303 goto done;
5304 } else {
5305 /* Entry is unchanged; just copy it. */
5306 err = got_object_tree_entry_dup(&new_te, te);
5307 if (err)
5308 goto done;
5309 err = insert_tree_entry(new_te, &paths);
5310 if (err)
5311 goto done;
5312 (*nentries)++;
5317 /* Write new list of entries; deleted entries have been dropped. */
5318 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5319 done:
5320 got_pathlist_free(&paths);
5321 return err;
5324 static const struct got_error *
5325 update_fileindex_after_commit(struct got_worktree *worktree,
5326 struct got_pathlist_head *commitable_paths,
5327 struct got_object_id *new_base_commit_id,
5328 struct got_fileindex *fileindex, int have_staged_files)
5330 const struct got_error *err = NULL;
5331 struct got_pathlist_entry *pe;
5332 char *relpath = NULL;
5334 TAILQ_FOREACH(pe, commitable_paths, entry) {
5335 struct got_fileindex_entry *ie;
5336 struct got_commitable *ct = pe->data;
5338 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5340 err = got_path_skip_common_ancestor(&relpath,
5341 worktree->root_path, ct->ondisk_path);
5342 if (err)
5343 goto done;
5345 if (ie) {
5346 if (ct->status == GOT_STATUS_DELETE ||
5347 ct->staged_status == GOT_STATUS_DELETE) {
5348 got_fileindex_entry_remove(fileindex, ie);
5349 } else if (ct->staged_status == GOT_STATUS_ADD ||
5350 ct->staged_status == GOT_STATUS_MODIFY) {
5351 got_fileindex_entry_stage_set(ie,
5352 GOT_FILEIDX_STAGE_NONE);
5353 got_fileindex_entry_staged_filetype_set(ie, 0);
5355 err = got_fileindex_entry_update(ie,
5356 worktree->root_fd, relpath,
5357 ct->staged_blob_id->sha1,
5358 new_base_commit_id->sha1,
5359 !have_staged_files);
5360 } else
5361 err = got_fileindex_entry_update(ie,
5362 worktree->root_fd, relpath,
5363 ct->blob_id->sha1,
5364 new_base_commit_id->sha1,
5365 !have_staged_files);
5366 } else {
5367 err = got_fileindex_entry_alloc(&ie, pe->path);
5368 if (err)
5369 goto done;
5370 err = got_fileindex_entry_update(ie,
5371 worktree->root_fd, relpath, ct->blob_id->sha1,
5372 new_base_commit_id->sha1, 1);
5373 if (err) {
5374 got_fileindex_entry_free(ie);
5375 goto done;
5377 err = got_fileindex_entry_add(fileindex, ie);
5378 if (err) {
5379 got_fileindex_entry_free(ie);
5380 goto done;
5383 free(relpath);
5384 relpath = NULL;
5386 done:
5387 free(relpath);
5388 return err;
5392 static const struct got_error *
5393 check_out_of_date(const char *in_repo_path, unsigned char status,
5394 unsigned char staged_status, struct got_object_id *base_blob_id,
5395 struct got_object_id *base_commit_id,
5396 struct got_object_id *head_commit_id, struct got_repository *repo,
5397 int ood_errcode)
5399 const struct got_error *err = NULL;
5400 struct got_object_id *id = NULL;
5402 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5403 /* Trivial case: base commit == head commit */
5404 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5405 return NULL;
5407 * Ensure file content which local changes were based
5408 * on matches file content in the branch head.
5410 err = got_object_id_by_path(&id, repo, head_commit_id,
5411 in_repo_path);
5412 if (err) {
5413 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5414 err = got_error(ood_errcode);
5415 goto done;
5416 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5417 err = got_error(ood_errcode);
5418 } else {
5419 /* Require that added files don't exist in the branch head. */
5420 err = got_object_id_by_path(&id, repo, head_commit_id,
5421 in_repo_path);
5422 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5423 goto done;
5424 err = id ? got_error(ood_errcode) : NULL;
5426 done:
5427 free(id);
5428 return err;
5431 const struct got_error *
5432 commit_worktree(struct got_object_id **new_commit_id,
5433 struct got_pathlist_head *commitable_paths,
5434 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5435 const char *author, const char *committer,
5436 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5437 got_worktree_status_cb status_cb, void *status_arg,
5438 struct got_repository *repo)
5440 const struct got_error *err = NULL, *unlockerr = NULL;
5441 struct got_pathlist_entry *pe;
5442 const char *head_ref_name = NULL;
5443 struct got_commit_object *head_commit = NULL;
5444 struct got_reference *head_ref2 = NULL;
5445 struct got_object_id *head_commit_id2 = NULL;
5446 struct got_tree_object *head_tree = NULL;
5447 struct got_object_id *new_tree_id = NULL;
5448 int nentries;
5449 struct got_object_id_queue parent_ids;
5450 struct got_object_qid *pid = NULL;
5451 char *logmsg = NULL;
5453 *new_commit_id = NULL;
5455 SIMPLEQ_INIT(&parent_ids);
5457 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5458 if (err)
5459 goto done;
5461 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5462 if (err)
5463 goto done;
5465 if (commit_msg_cb != NULL) {
5466 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5467 if (err)
5468 goto done;
5471 if (logmsg == NULL || strlen(logmsg) == 0) {
5472 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5473 goto done;
5476 /* Create blobs from added and modified files and record their IDs. */
5477 TAILQ_FOREACH(pe, commitable_paths, entry) {
5478 struct got_commitable *ct = pe->data;
5479 char *ondisk_path;
5481 /* Blobs for staged files already exist. */
5482 if (ct->staged_status == GOT_STATUS_ADD ||
5483 ct->staged_status == GOT_STATUS_MODIFY)
5484 continue;
5486 if (ct->status != GOT_STATUS_ADD &&
5487 ct->status != GOT_STATUS_MODIFY &&
5488 ct->status != GOT_STATUS_MODE_CHANGE)
5489 continue;
5491 if (asprintf(&ondisk_path, "%s/%s",
5492 worktree->root_path, pe->path) == -1) {
5493 err = got_error_from_errno("asprintf");
5494 goto done;
5496 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5497 free(ondisk_path);
5498 if (err)
5499 goto done;
5502 /* Recursively write new tree objects. */
5503 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5504 commitable_paths, status_cb, status_arg, repo);
5505 if (err)
5506 goto done;
5508 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5509 if (err)
5510 goto done;
5511 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5512 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5513 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5514 got_object_qid_free(pid);
5515 if (logmsg != NULL)
5516 free(logmsg);
5517 if (err)
5518 goto done;
5520 /* Check if a concurrent commit to our branch has occurred. */
5521 head_ref_name = got_worktree_get_head_ref_name(worktree);
5522 if (head_ref_name == NULL) {
5523 err = got_error_from_errno("got_worktree_get_head_ref_name");
5524 goto done;
5526 /* Lock the reference here to prevent concurrent modification. */
5527 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5528 if (err)
5529 goto done;
5530 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5531 if (err)
5532 goto done;
5533 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5534 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5535 goto done;
5537 /* Update branch head in repository. */
5538 err = got_ref_change_ref(head_ref2, *new_commit_id);
5539 if (err)
5540 goto done;
5541 err = got_ref_write(head_ref2, repo);
5542 if (err)
5543 goto done;
5545 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5546 if (err)
5547 goto done;
5549 err = ref_base_commit(worktree, repo);
5550 if (err)
5551 goto done;
5552 done:
5553 if (head_tree)
5554 got_object_tree_close(head_tree);
5555 if (head_commit)
5556 got_object_commit_close(head_commit);
5557 free(head_commit_id2);
5558 if (head_ref2) {
5559 unlockerr = got_ref_unlock(head_ref2);
5560 if (unlockerr && err == NULL)
5561 err = unlockerr;
5562 got_ref_close(head_ref2);
5564 return err;
5567 static const struct got_error *
5568 check_path_is_commitable(const char *path,
5569 struct got_pathlist_head *commitable_paths)
5571 struct got_pathlist_entry *cpe = NULL;
5572 size_t path_len = strlen(path);
5574 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5575 struct got_commitable *ct = cpe->data;
5576 const char *ct_path = ct->path;
5578 while (ct_path[0] == '/')
5579 ct_path++;
5581 if (strcmp(path, ct_path) == 0 ||
5582 got_path_is_child(ct_path, path, path_len))
5583 break;
5586 if (cpe == NULL)
5587 return got_error_path(path, GOT_ERR_BAD_PATH);
5589 return NULL;
5592 static const struct got_error *
5593 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5595 int *have_staged_files = arg;
5597 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5598 *have_staged_files = 1;
5599 return got_error(GOT_ERR_CANCELLED);
5602 return NULL;
5605 static const struct got_error *
5606 check_non_staged_files(struct got_fileindex *fileindex,
5607 struct got_pathlist_head *paths)
5609 struct got_pathlist_entry *pe;
5610 struct got_fileindex_entry *ie;
5612 TAILQ_FOREACH(pe, paths, entry) {
5613 if (pe->path[0] == '\0')
5614 continue;
5615 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5616 if (ie == NULL)
5617 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5618 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5619 return got_error_path(pe->path,
5620 GOT_ERR_FILE_NOT_STAGED);
5623 return NULL;
5626 const struct got_error *
5627 got_worktree_commit(struct got_object_id **new_commit_id,
5628 struct got_worktree *worktree, struct got_pathlist_head *paths,
5629 const char *author, const char *committer, int allow_bad_symlinks,
5630 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5631 got_worktree_status_cb status_cb, void *status_arg,
5632 struct got_repository *repo)
5634 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5635 struct got_fileindex *fileindex = NULL;
5636 char *fileindex_path = NULL;
5637 struct got_pathlist_head commitable_paths;
5638 struct collect_commitables_arg cc_arg;
5639 struct got_pathlist_entry *pe;
5640 struct got_reference *head_ref = NULL;
5641 struct got_object_id *head_commit_id = NULL;
5642 int have_staged_files = 0;
5644 *new_commit_id = NULL;
5646 TAILQ_INIT(&commitable_paths);
5648 err = lock_worktree(worktree, LOCK_EX);
5649 if (err)
5650 goto done;
5652 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5653 if (err)
5654 goto done;
5656 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5657 if (err)
5658 goto done;
5660 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5661 if (err)
5662 goto done;
5664 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5665 &have_staged_files);
5666 if (err && err->code != GOT_ERR_CANCELLED)
5667 goto done;
5668 if (have_staged_files) {
5669 err = check_non_staged_files(fileindex, paths);
5670 if (err)
5671 goto done;
5674 cc_arg.commitable_paths = &commitable_paths;
5675 cc_arg.worktree = worktree;
5676 cc_arg.fileindex = fileindex;
5677 cc_arg.repo = repo;
5678 cc_arg.have_staged_files = have_staged_files;
5679 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5680 TAILQ_FOREACH(pe, paths, entry) {
5681 err = worktree_status(worktree, pe->path, fileindex, repo,
5682 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5683 if (err)
5684 goto done;
5687 if (TAILQ_EMPTY(&commitable_paths)) {
5688 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5689 goto done;
5692 TAILQ_FOREACH(pe, paths, entry) {
5693 err = check_path_is_commitable(pe->path, &commitable_paths);
5694 if (err)
5695 goto done;
5698 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5699 struct got_commitable *ct = pe->data;
5700 const char *ct_path = ct->in_repo_path;
5702 while (ct_path[0] == '/')
5703 ct_path++;
5704 err = check_out_of_date(ct_path, ct->status,
5705 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5706 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5707 if (err)
5708 goto done;
5712 err = commit_worktree(new_commit_id, &commitable_paths,
5713 head_commit_id, worktree, author, committer,
5714 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5715 if (err)
5716 goto done;
5718 err = update_fileindex_after_commit(worktree, &commitable_paths,
5719 *new_commit_id, fileindex, have_staged_files);
5720 sync_err = sync_fileindex(fileindex, fileindex_path);
5721 if (sync_err && err == NULL)
5722 err = sync_err;
5723 done:
5724 if (fileindex)
5725 got_fileindex_free(fileindex);
5726 free(fileindex_path);
5727 unlockerr = lock_worktree(worktree, LOCK_SH);
5728 if (unlockerr && err == NULL)
5729 err = unlockerr;
5730 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5731 struct got_commitable *ct = pe->data;
5732 free_commitable(ct);
5734 got_pathlist_free(&commitable_paths);
5735 return err;
5738 const char *
5739 got_commitable_get_path(struct got_commitable *ct)
5741 return ct->path;
5744 unsigned int
5745 got_commitable_get_status(struct got_commitable *ct)
5747 return ct->status;
5750 struct check_rebase_ok_arg {
5751 struct got_worktree *worktree;
5752 struct got_repository *repo;
5755 static const struct got_error *
5756 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5758 const struct got_error *err = NULL;
5759 struct check_rebase_ok_arg *a = arg;
5760 unsigned char status;
5761 struct stat sb;
5762 char *ondisk_path;
5764 /* Reject rebase of a work tree with mixed base commits. */
5765 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5766 SHA1_DIGEST_LENGTH))
5767 return got_error(GOT_ERR_MIXED_COMMITS);
5769 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5770 == -1)
5771 return got_error_from_errno("asprintf");
5773 /* Reject rebase of a work tree with modified or staged files. */
5774 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5775 free(ondisk_path);
5776 if (err)
5777 return err;
5779 if (status != GOT_STATUS_NO_CHANGE)
5780 return got_error(GOT_ERR_MODIFIED);
5781 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5782 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5784 return NULL;
5787 const struct got_error *
5788 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5789 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5790 struct got_worktree *worktree, struct got_reference *branch,
5791 struct got_repository *repo)
5793 const struct got_error *err = NULL;
5794 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5795 char *branch_ref_name = NULL;
5796 char *fileindex_path = NULL;
5797 struct check_rebase_ok_arg ok_arg;
5798 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5799 struct got_object_id *wt_branch_tip = NULL;
5801 *new_base_branch_ref = NULL;
5802 *tmp_branch = NULL;
5803 *fileindex = NULL;
5805 err = lock_worktree(worktree, LOCK_EX);
5806 if (err)
5807 return err;
5809 err = open_fileindex(fileindex, &fileindex_path, worktree);
5810 if (err)
5811 goto done;
5813 ok_arg.worktree = worktree;
5814 ok_arg.repo = repo;
5815 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5816 &ok_arg);
5817 if (err)
5818 goto done;
5820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5821 if (err)
5822 goto done;
5824 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5825 if (err)
5826 goto done;
5828 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5829 if (err)
5830 goto done;
5832 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5833 0);
5834 if (err)
5835 goto done;
5837 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5838 if (err)
5839 goto done;
5840 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5841 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5842 goto done;
5845 err = got_ref_alloc_symref(new_base_branch_ref,
5846 new_base_branch_ref_name, wt_branch);
5847 if (err)
5848 goto done;
5849 err = got_ref_write(*new_base_branch_ref, repo);
5850 if (err)
5851 goto done;
5853 /* TODO Lock original branch's ref while rebasing? */
5855 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5856 if (err)
5857 goto done;
5859 err = got_ref_write(branch_ref, repo);
5860 if (err)
5861 goto done;
5863 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5864 worktree->base_commit_id);
5865 if (err)
5866 goto done;
5867 err = got_ref_write(*tmp_branch, repo);
5868 if (err)
5869 goto done;
5871 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5872 if (err)
5873 goto done;
5874 done:
5875 free(fileindex_path);
5876 free(tmp_branch_name);
5877 free(new_base_branch_ref_name);
5878 free(branch_ref_name);
5879 if (branch_ref)
5880 got_ref_close(branch_ref);
5881 if (wt_branch)
5882 got_ref_close(wt_branch);
5883 free(wt_branch_tip);
5884 if (err) {
5885 if (*new_base_branch_ref) {
5886 got_ref_close(*new_base_branch_ref);
5887 *new_base_branch_ref = NULL;
5889 if (*tmp_branch) {
5890 got_ref_close(*tmp_branch);
5891 *tmp_branch = NULL;
5893 if (*fileindex) {
5894 got_fileindex_free(*fileindex);
5895 *fileindex = NULL;
5897 lock_worktree(worktree, LOCK_SH);
5899 return err;
5902 const struct got_error *
5903 got_worktree_rebase_continue(struct got_object_id **commit_id,
5904 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5905 struct got_reference **branch, struct got_fileindex **fileindex,
5906 struct got_worktree *worktree, struct got_repository *repo)
5908 const struct got_error *err;
5909 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5910 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5911 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5912 char *fileindex_path = NULL;
5913 int have_staged_files = 0;
5915 *commit_id = NULL;
5916 *new_base_branch = NULL;
5917 *tmp_branch = NULL;
5918 *branch = NULL;
5919 *fileindex = NULL;
5921 err = lock_worktree(worktree, LOCK_EX);
5922 if (err)
5923 return err;
5925 err = open_fileindex(fileindex, &fileindex_path, worktree);
5926 if (err)
5927 goto done;
5929 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5930 &have_staged_files);
5931 if (err && err->code != GOT_ERR_CANCELLED)
5932 goto done;
5933 if (have_staged_files) {
5934 err = got_error(GOT_ERR_STAGED_PATHS);
5935 goto done;
5938 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5939 if (err)
5940 goto done;
5942 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5943 if (err)
5944 goto done;
5946 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5947 if (err)
5948 goto done;
5950 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5951 if (err)
5952 goto done;
5954 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5955 if (err)
5956 goto done;
5958 err = got_ref_open(branch, repo,
5959 got_ref_get_symref_target(branch_ref), 0);
5960 if (err)
5961 goto done;
5963 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5964 if (err)
5965 goto done;
5967 err = got_ref_resolve(commit_id, repo, commit_ref);
5968 if (err)
5969 goto done;
5971 err = got_ref_open(new_base_branch, repo,
5972 new_base_branch_ref_name, 0);
5973 if (err)
5974 goto done;
5976 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5977 if (err)
5978 goto done;
5979 done:
5980 free(commit_ref_name);
5981 free(branch_ref_name);
5982 free(fileindex_path);
5983 if (commit_ref)
5984 got_ref_close(commit_ref);
5985 if (branch_ref)
5986 got_ref_close(branch_ref);
5987 if (err) {
5988 free(*commit_id);
5989 *commit_id = NULL;
5990 if (*tmp_branch) {
5991 got_ref_close(*tmp_branch);
5992 *tmp_branch = NULL;
5994 if (*new_base_branch) {
5995 got_ref_close(*new_base_branch);
5996 *new_base_branch = NULL;
5998 if (*branch) {
5999 got_ref_close(*branch);
6000 *branch = NULL;
6002 if (*fileindex) {
6003 got_fileindex_free(*fileindex);
6004 *fileindex = NULL;
6006 lock_worktree(worktree, LOCK_SH);
6008 return err;
6011 const struct got_error *
6012 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6014 const struct got_error *err;
6015 char *tmp_branch_name = NULL;
6017 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6018 if (err)
6019 return err;
6021 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6022 free(tmp_branch_name);
6023 return NULL;
6026 static const struct got_error *
6027 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6028 char **logmsg, void *arg)
6030 *logmsg = arg;
6031 return NULL;
6034 static const struct got_error *
6035 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6036 const char *path, struct got_object_id *blob_id,
6037 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6038 int dirfd, const char *de_name)
6040 return NULL;
6043 struct collect_merged_paths_arg {
6044 got_worktree_checkout_cb progress_cb;
6045 void *progress_arg;
6046 struct got_pathlist_head *merged_paths;
6049 static const struct got_error *
6050 collect_merged_paths(void *arg, unsigned char status, const char *path)
6052 const struct got_error *err;
6053 struct collect_merged_paths_arg *a = arg;
6054 char *p;
6055 struct got_pathlist_entry *new;
6057 err = (*a->progress_cb)(a->progress_arg, status, path);
6058 if (err)
6059 return err;
6061 if (status != GOT_STATUS_MERGE &&
6062 status != GOT_STATUS_ADD &&
6063 status != GOT_STATUS_DELETE &&
6064 status != GOT_STATUS_CONFLICT)
6065 return NULL;
6067 p = strdup(path);
6068 if (p == NULL)
6069 return got_error_from_errno("strdup");
6071 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6072 if (err || new == NULL)
6073 free(p);
6074 return err;
6077 void
6078 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6080 struct got_pathlist_entry *pe;
6082 TAILQ_FOREACH(pe, merged_paths, entry)
6083 free((char *)pe->path);
6085 got_pathlist_free(merged_paths);
6088 static const struct got_error *
6089 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6090 int is_rebase, struct got_repository *repo)
6092 const struct got_error *err;
6093 struct got_reference *commit_ref = NULL;
6095 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6096 if (err) {
6097 if (err->code != GOT_ERR_NOT_REF)
6098 goto done;
6099 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6100 if (err)
6101 goto done;
6102 err = got_ref_write(commit_ref, repo);
6103 if (err)
6104 goto done;
6105 } else if (is_rebase) {
6106 struct got_object_id *stored_id;
6107 int cmp;
6109 err = got_ref_resolve(&stored_id, repo, commit_ref);
6110 if (err)
6111 goto done;
6112 cmp = got_object_id_cmp(commit_id, stored_id);
6113 free(stored_id);
6114 if (cmp != 0) {
6115 err = got_error(GOT_ERR_REBASE_COMMITID);
6116 goto done;
6119 done:
6120 if (commit_ref)
6121 got_ref_close(commit_ref);
6122 return err;
6125 static const struct got_error *
6126 rebase_merge_files(struct got_pathlist_head *merged_paths,
6127 const char *commit_ref_name, struct got_worktree *worktree,
6128 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6129 struct got_object_id *commit_id, struct got_repository *repo,
6130 got_worktree_checkout_cb progress_cb, void *progress_arg,
6131 got_cancel_cb cancel_cb, void *cancel_arg)
6133 const struct got_error *err;
6134 struct got_reference *commit_ref = NULL;
6135 struct collect_merged_paths_arg cmp_arg;
6136 char *fileindex_path;
6138 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6140 err = get_fileindex_path(&fileindex_path, worktree);
6141 if (err)
6142 return err;
6144 cmp_arg.progress_cb = progress_cb;
6145 cmp_arg.progress_arg = progress_arg;
6146 cmp_arg.merged_paths = merged_paths;
6147 err = merge_files(worktree, fileindex, fileindex_path,
6148 parent_commit_id, commit_id, repo, collect_merged_paths,
6149 &cmp_arg, cancel_cb, cancel_arg);
6150 if (commit_ref)
6151 got_ref_close(commit_ref);
6152 return err;
6155 const struct got_error *
6156 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6157 struct got_worktree *worktree, struct got_fileindex *fileindex,
6158 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6159 struct got_repository *repo,
6160 got_worktree_checkout_cb progress_cb, void *progress_arg,
6161 got_cancel_cb cancel_cb, void *cancel_arg)
6163 const struct got_error *err;
6164 char *commit_ref_name;
6166 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6167 if (err)
6168 return err;
6170 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6171 if (err)
6172 goto done;
6174 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6175 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6176 progress_arg, cancel_cb, cancel_arg);
6177 done:
6178 free(commit_ref_name);
6179 return err;
6182 const struct got_error *
6183 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6184 struct got_worktree *worktree, struct got_fileindex *fileindex,
6185 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6186 struct got_repository *repo,
6187 got_worktree_checkout_cb progress_cb, void *progress_arg,
6188 got_cancel_cb cancel_cb, void *cancel_arg)
6190 const struct got_error *err;
6191 char *commit_ref_name;
6193 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6194 if (err)
6195 return err;
6197 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6198 if (err)
6199 goto done;
6201 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6202 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6203 progress_arg, cancel_cb, cancel_arg);
6204 done:
6205 free(commit_ref_name);
6206 return err;
6209 static const struct got_error *
6210 rebase_commit(struct got_object_id **new_commit_id,
6211 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6212 struct got_worktree *worktree, struct got_fileindex *fileindex,
6213 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6214 const char *new_logmsg, struct got_repository *repo)
6216 const struct got_error *err, *sync_err;
6217 struct got_pathlist_head commitable_paths;
6218 struct collect_commitables_arg cc_arg;
6219 char *fileindex_path = NULL;
6220 struct got_reference *head_ref = NULL;
6221 struct got_object_id *head_commit_id = NULL;
6222 char *logmsg = NULL;
6224 TAILQ_INIT(&commitable_paths);
6225 *new_commit_id = NULL;
6227 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6229 err = get_fileindex_path(&fileindex_path, worktree);
6230 if (err)
6231 return err;
6233 cc_arg.commitable_paths = &commitable_paths;
6234 cc_arg.worktree = worktree;
6235 cc_arg.repo = repo;
6236 cc_arg.have_staged_files = 0;
6238 * If possible get the status of individual files directly to
6239 * avoid crawling the entire work tree once per rebased commit.
6240 * TODO: Ideally, merged_paths would contain a list of commitables
6241 * we could use so we could skip worktree_status() entirely.
6243 if (merged_paths) {
6244 struct got_pathlist_entry *pe;
6245 TAILQ_FOREACH(pe, merged_paths, entry) {
6246 err = worktree_status(worktree, pe->path, fileindex,
6247 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6248 0);
6249 if (err)
6250 goto done;
6252 } else {
6253 err = worktree_status(worktree, "", fileindex, repo,
6254 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6255 if (err)
6256 goto done;
6259 if (TAILQ_EMPTY(&commitable_paths)) {
6260 /* No-op change; commit will be elided. */
6261 err = got_ref_delete(commit_ref, repo);
6262 if (err)
6263 goto done;
6264 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6265 goto done;
6268 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6269 if (err)
6270 goto done;
6272 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6273 if (err)
6274 goto done;
6276 if (new_logmsg) {
6277 logmsg = strdup(new_logmsg);
6278 if (logmsg == NULL) {
6279 err = got_error_from_errno("strdup");
6280 goto done;
6282 } else {
6283 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6284 if (err)
6285 goto done;
6288 /* NB: commit_worktree will call free(logmsg) */
6289 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6290 worktree, got_object_commit_get_author(orig_commit),
6291 got_object_commit_get_committer(orig_commit),
6292 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6293 if (err)
6294 goto done;
6296 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6297 if (err)
6298 goto done;
6300 err = got_ref_delete(commit_ref, repo);
6301 if (err)
6302 goto done;
6304 err = update_fileindex_after_commit(worktree, &commitable_paths,
6305 *new_commit_id, fileindex, 0);
6306 sync_err = sync_fileindex(fileindex, fileindex_path);
6307 if (sync_err && err == NULL)
6308 err = sync_err;
6309 done:
6310 free(fileindex_path);
6311 free(head_commit_id);
6312 if (head_ref)
6313 got_ref_close(head_ref);
6314 if (err) {
6315 free(*new_commit_id);
6316 *new_commit_id = NULL;
6318 return err;
6321 const struct got_error *
6322 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6323 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6324 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6325 struct got_commit_object *orig_commit,
6326 struct got_object_id *orig_commit_id, struct got_repository *repo)
6328 const struct got_error *err;
6329 char *commit_ref_name;
6330 struct got_reference *commit_ref = NULL;
6331 struct got_object_id *commit_id = NULL;
6333 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6334 if (err)
6335 return err;
6337 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6338 if (err)
6339 goto done;
6340 err = got_ref_resolve(&commit_id, repo, commit_ref);
6341 if (err)
6342 goto done;
6343 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6344 err = got_error(GOT_ERR_REBASE_COMMITID);
6345 goto done;
6348 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6349 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6350 done:
6351 if (commit_ref)
6352 got_ref_close(commit_ref);
6353 free(commit_ref_name);
6354 free(commit_id);
6355 return err;
6358 const struct got_error *
6359 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6360 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6361 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6362 struct got_commit_object *orig_commit,
6363 struct got_object_id *orig_commit_id, const char *new_logmsg,
6364 struct got_repository *repo)
6366 const struct got_error *err;
6367 char *commit_ref_name;
6368 struct got_reference *commit_ref = NULL;
6370 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6371 if (err)
6372 return err;
6374 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6375 if (err)
6376 goto done;
6378 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6379 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6380 done:
6381 if (commit_ref)
6382 got_ref_close(commit_ref);
6383 free(commit_ref_name);
6384 return err;
6387 const struct got_error *
6388 got_worktree_rebase_postpone(struct got_worktree *worktree,
6389 struct got_fileindex *fileindex)
6391 if (fileindex)
6392 got_fileindex_free(fileindex);
6393 return lock_worktree(worktree, LOCK_SH);
6396 static const struct got_error *
6397 delete_ref(const char *name, struct got_repository *repo)
6399 const struct got_error *err;
6400 struct got_reference *ref;
6402 err = got_ref_open(&ref, repo, name, 0);
6403 if (err) {
6404 if (err->code == GOT_ERR_NOT_REF)
6405 return NULL;
6406 return err;
6409 err = got_ref_delete(ref, repo);
6410 got_ref_close(ref);
6411 return err;
6414 static const struct got_error *
6415 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6417 const struct got_error *err;
6418 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6419 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6421 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6422 if (err)
6423 goto done;
6424 err = delete_ref(tmp_branch_name, repo);
6425 if (err)
6426 goto done;
6428 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6429 if (err)
6430 goto done;
6431 err = delete_ref(new_base_branch_ref_name, repo);
6432 if (err)
6433 goto done;
6435 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6436 if (err)
6437 goto done;
6438 err = delete_ref(branch_ref_name, repo);
6439 if (err)
6440 goto done;
6442 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6443 if (err)
6444 goto done;
6445 err = delete_ref(commit_ref_name, repo);
6446 if (err)
6447 goto done;
6449 done:
6450 free(tmp_branch_name);
6451 free(new_base_branch_ref_name);
6452 free(branch_ref_name);
6453 free(commit_ref_name);
6454 return err;
6457 const struct got_error *
6458 got_worktree_rebase_complete(struct got_worktree *worktree,
6459 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6460 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6461 struct got_repository *repo)
6463 const struct got_error *err, *unlockerr, *sync_err;
6464 struct got_object_id *new_head_commit_id = NULL;
6465 char *fileindex_path = NULL;
6467 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6468 if (err)
6469 return err;
6471 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6472 if (err)
6473 goto done;
6475 err = got_ref_write(rebased_branch, repo);
6476 if (err)
6477 goto done;
6479 err = got_worktree_set_head_ref(worktree, rebased_branch);
6480 if (err)
6481 goto done;
6483 err = delete_rebase_refs(worktree, repo);
6484 if (err)
6485 goto done;
6487 err = get_fileindex_path(&fileindex_path, worktree);
6488 if (err)
6489 goto done;
6490 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6491 sync_err = sync_fileindex(fileindex, fileindex_path);
6492 if (sync_err && err == NULL)
6493 err = sync_err;
6494 done:
6495 got_fileindex_free(fileindex);
6496 free(fileindex_path);
6497 free(new_head_commit_id);
6498 unlockerr = lock_worktree(worktree, LOCK_SH);
6499 if (unlockerr && err == NULL)
6500 err = unlockerr;
6501 return err;
6504 const struct got_error *
6505 got_worktree_rebase_abort(struct got_worktree *worktree,
6506 struct got_fileindex *fileindex, struct got_repository *repo,
6507 struct got_reference *new_base_branch,
6508 got_worktree_checkout_cb progress_cb, void *progress_arg)
6510 const struct got_error *err, *unlockerr, *sync_err;
6511 struct got_reference *resolved = NULL;
6512 struct got_object_id *commit_id = NULL;
6513 char *fileindex_path = NULL;
6514 struct revert_file_args rfa;
6515 struct got_object_id *tree_id = NULL;
6517 err = lock_worktree(worktree, LOCK_EX);
6518 if (err)
6519 return err;
6521 err = got_ref_open(&resolved, repo,
6522 got_ref_get_symref_target(new_base_branch), 0);
6523 if (err)
6524 goto done;
6526 err = got_worktree_set_head_ref(worktree, resolved);
6527 if (err)
6528 goto done;
6531 * XXX commits to the base branch could have happened while
6532 * we were busy rebasing; should we store the original commit ID
6533 * when rebase begins and read it back here?
6535 err = got_ref_resolve(&commit_id, repo, resolved);
6536 if (err)
6537 goto done;
6539 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6540 if (err)
6541 goto done;
6543 err = got_object_id_by_path(&tree_id, repo,
6544 worktree->base_commit_id, worktree->path_prefix);
6545 if (err)
6546 goto done;
6548 err = delete_rebase_refs(worktree, repo);
6549 if (err)
6550 goto done;
6552 err = get_fileindex_path(&fileindex_path, worktree);
6553 if (err)
6554 goto done;
6556 rfa.worktree = worktree;
6557 rfa.fileindex = fileindex;
6558 rfa.progress_cb = progress_cb;
6559 rfa.progress_arg = progress_arg;
6560 rfa.patch_cb = NULL;
6561 rfa.patch_arg = NULL;
6562 rfa.repo = repo;
6563 err = worktree_status(worktree, "", fileindex, repo,
6564 revert_file, &rfa, NULL, NULL, 0, 0);
6565 if (err)
6566 goto sync;
6568 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6569 repo, progress_cb, progress_arg, NULL, NULL);
6570 sync:
6571 sync_err = sync_fileindex(fileindex, fileindex_path);
6572 if (sync_err && err == NULL)
6573 err = sync_err;
6574 done:
6575 got_ref_close(resolved);
6576 free(tree_id);
6577 free(commit_id);
6578 if (fileindex)
6579 got_fileindex_free(fileindex);
6580 free(fileindex_path);
6582 unlockerr = lock_worktree(worktree, LOCK_SH);
6583 if (unlockerr && err == NULL)
6584 err = unlockerr;
6585 return err;
6588 const struct got_error *
6589 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6590 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6591 struct got_fileindex **fileindex, struct got_worktree *worktree,
6592 struct got_repository *repo)
6594 const struct got_error *err = NULL;
6595 char *tmp_branch_name = NULL;
6596 char *branch_ref_name = NULL;
6597 char *base_commit_ref_name = NULL;
6598 char *fileindex_path = NULL;
6599 struct check_rebase_ok_arg ok_arg;
6600 struct got_reference *wt_branch = NULL;
6601 struct got_reference *base_commit_ref = NULL;
6603 *tmp_branch = NULL;
6604 *branch_ref = NULL;
6605 *base_commit_id = NULL;
6606 *fileindex = NULL;
6608 err = lock_worktree(worktree, LOCK_EX);
6609 if (err)
6610 return err;
6612 err = open_fileindex(fileindex, &fileindex_path, worktree);
6613 if (err)
6614 goto done;
6616 ok_arg.worktree = worktree;
6617 ok_arg.repo = repo;
6618 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6619 &ok_arg);
6620 if (err)
6621 goto done;
6623 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6624 if (err)
6625 goto done;
6627 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6628 if (err)
6629 goto done;
6631 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6632 worktree);
6633 if (err)
6634 goto done;
6636 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6637 0);
6638 if (err)
6639 goto done;
6641 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6642 if (err)
6643 goto done;
6645 err = got_ref_write(*branch_ref, repo);
6646 if (err)
6647 goto done;
6649 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6650 worktree->base_commit_id);
6651 if (err)
6652 goto done;
6653 err = got_ref_write(base_commit_ref, repo);
6654 if (err)
6655 goto done;
6656 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6657 if (*base_commit_id == NULL) {
6658 err = got_error_from_errno("got_object_id_dup");
6659 goto done;
6662 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6663 worktree->base_commit_id);
6664 if (err)
6665 goto done;
6666 err = got_ref_write(*tmp_branch, repo);
6667 if (err)
6668 goto done;
6670 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6671 if (err)
6672 goto done;
6673 done:
6674 free(fileindex_path);
6675 free(tmp_branch_name);
6676 free(branch_ref_name);
6677 free(base_commit_ref_name);
6678 if (wt_branch)
6679 got_ref_close(wt_branch);
6680 if (err) {
6681 if (*branch_ref) {
6682 got_ref_close(*branch_ref);
6683 *branch_ref = NULL;
6685 if (*tmp_branch) {
6686 got_ref_close(*tmp_branch);
6687 *tmp_branch = NULL;
6689 free(*base_commit_id);
6690 if (*fileindex) {
6691 got_fileindex_free(*fileindex);
6692 *fileindex = NULL;
6694 lock_worktree(worktree, LOCK_SH);
6696 return err;
6699 const struct got_error *
6700 got_worktree_histedit_postpone(struct got_worktree *worktree,
6701 struct got_fileindex *fileindex)
6703 if (fileindex)
6704 got_fileindex_free(fileindex);
6705 return lock_worktree(worktree, LOCK_SH);
6708 const struct got_error *
6709 got_worktree_histedit_in_progress(int *in_progress,
6710 struct got_worktree *worktree)
6712 const struct got_error *err;
6713 char *tmp_branch_name = NULL;
6715 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6716 if (err)
6717 return err;
6719 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6720 free(tmp_branch_name);
6721 return NULL;
6724 const struct got_error *
6725 got_worktree_histedit_continue(struct got_object_id **commit_id,
6726 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6727 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6728 struct got_worktree *worktree, struct got_repository *repo)
6730 const struct got_error *err;
6731 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6732 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6733 struct got_reference *commit_ref = NULL;
6734 struct got_reference *base_commit_ref = NULL;
6735 char *fileindex_path = NULL;
6736 int have_staged_files = 0;
6738 *commit_id = NULL;
6739 *tmp_branch = NULL;
6740 *base_commit_id = NULL;
6741 *fileindex = NULL;
6743 err = lock_worktree(worktree, LOCK_EX);
6744 if (err)
6745 return err;
6747 err = open_fileindex(fileindex, &fileindex_path, worktree);
6748 if (err)
6749 goto done;
6751 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6752 &have_staged_files);
6753 if (err && err->code != GOT_ERR_CANCELLED)
6754 goto done;
6755 if (have_staged_files) {
6756 err = got_error(GOT_ERR_STAGED_PATHS);
6757 goto done;
6760 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6761 if (err)
6762 goto done;
6764 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6765 if (err)
6766 goto done;
6768 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6769 if (err)
6770 goto done;
6772 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6773 worktree);
6774 if (err)
6775 goto done;
6777 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6778 if (err)
6779 goto done;
6781 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6782 if (err)
6783 goto done;
6784 err = got_ref_resolve(commit_id, repo, commit_ref);
6785 if (err)
6786 goto done;
6788 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6789 if (err)
6790 goto done;
6791 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6792 if (err)
6793 goto done;
6795 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6796 if (err)
6797 goto done;
6798 done:
6799 free(commit_ref_name);
6800 free(branch_ref_name);
6801 free(fileindex_path);
6802 if (commit_ref)
6803 got_ref_close(commit_ref);
6804 if (base_commit_ref)
6805 got_ref_close(base_commit_ref);
6806 if (err) {
6807 free(*commit_id);
6808 *commit_id = NULL;
6809 free(*base_commit_id);
6810 *base_commit_id = NULL;
6811 if (*tmp_branch) {
6812 got_ref_close(*tmp_branch);
6813 *tmp_branch = NULL;
6815 if (*fileindex) {
6816 got_fileindex_free(*fileindex);
6817 *fileindex = NULL;
6819 lock_worktree(worktree, LOCK_EX);
6821 return err;
6824 static const struct got_error *
6825 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6827 const struct got_error *err;
6828 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6829 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6831 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6832 if (err)
6833 goto done;
6834 err = delete_ref(tmp_branch_name, repo);
6835 if (err)
6836 goto done;
6838 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6839 worktree);
6840 if (err)
6841 goto done;
6842 err = delete_ref(base_commit_ref_name, repo);
6843 if (err)
6844 goto done;
6846 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6847 if (err)
6848 goto done;
6849 err = delete_ref(branch_ref_name, repo);
6850 if (err)
6851 goto done;
6853 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6854 if (err)
6855 goto done;
6856 err = delete_ref(commit_ref_name, repo);
6857 if (err)
6858 goto done;
6859 done:
6860 free(tmp_branch_name);
6861 free(base_commit_ref_name);
6862 free(branch_ref_name);
6863 free(commit_ref_name);
6864 return err;
6867 const struct got_error *
6868 got_worktree_histedit_abort(struct got_worktree *worktree,
6869 struct got_fileindex *fileindex, struct got_repository *repo,
6870 struct got_reference *branch, struct got_object_id *base_commit_id,
6871 got_worktree_checkout_cb progress_cb, void *progress_arg)
6873 const struct got_error *err, *unlockerr, *sync_err;
6874 struct got_reference *resolved = NULL;
6875 char *fileindex_path = NULL;
6876 struct got_object_id *tree_id = NULL;
6877 struct revert_file_args rfa;
6879 err = lock_worktree(worktree, LOCK_EX);
6880 if (err)
6881 return err;
6883 err = got_ref_open(&resolved, repo,
6884 got_ref_get_symref_target(branch), 0);
6885 if (err)
6886 goto done;
6888 err = got_worktree_set_head_ref(worktree, resolved);
6889 if (err)
6890 goto done;
6892 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6893 if (err)
6894 goto done;
6896 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6897 worktree->path_prefix);
6898 if (err)
6899 goto done;
6901 err = delete_histedit_refs(worktree, repo);
6902 if (err)
6903 goto done;
6905 err = get_fileindex_path(&fileindex_path, worktree);
6906 if (err)
6907 goto done;
6909 rfa.worktree = worktree;
6910 rfa.fileindex = fileindex;
6911 rfa.progress_cb = progress_cb;
6912 rfa.progress_arg = progress_arg;
6913 rfa.patch_cb = NULL;
6914 rfa.patch_arg = NULL;
6915 rfa.repo = repo;
6916 err = worktree_status(worktree, "", fileindex, repo,
6917 revert_file, &rfa, NULL, NULL, 0, 0);
6918 if (err)
6919 goto sync;
6921 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6922 repo, progress_cb, progress_arg, NULL, NULL);
6923 sync:
6924 sync_err = sync_fileindex(fileindex, fileindex_path);
6925 if (sync_err && err == NULL)
6926 err = sync_err;
6927 done:
6928 got_ref_close(resolved);
6929 free(tree_id);
6930 free(fileindex_path);
6932 unlockerr = lock_worktree(worktree, LOCK_SH);
6933 if (unlockerr && err == NULL)
6934 err = unlockerr;
6935 return err;
6938 const struct got_error *
6939 got_worktree_histedit_complete(struct got_worktree *worktree,
6940 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6941 struct got_reference *edited_branch, struct got_repository *repo)
6943 const struct got_error *err, *unlockerr, *sync_err;
6944 struct got_object_id *new_head_commit_id = NULL;
6945 struct got_reference *resolved = NULL;
6946 char *fileindex_path = NULL;
6948 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6949 if (err)
6950 return err;
6952 err = got_ref_open(&resolved, repo,
6953 got_ref_get_symref_target(edited_branch), 0);
6954 if (err)
6955 goto done;
6957 err = got_ref_change_ref(resolved, new_head_commit_id);
6958 if (err)
6959 goto done;
6961 err = got_ref_write(resolved, repo);
6962 if (err)
6963 goto done;
6965 err = got_worktree_set_head_ref(worktree, resolved);
6966 if (err)
6967 goto done;
6969 err = delete_histedit_refs(worktree, repo);
6970 if (err)
6971 goto done;
6973 err = get_fileindex_path(&fileindex_path, worktree);
6974 if (err)
6975 goto done;
6976 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6977 sync_err = sync_fileindex(fileindex, fileindex_path);
6978 if (sync_err && err == NULL)
6979 err = sync_err;
6980 done:
6981 got_fileindex_free(fileindex);
6982 free(fileindex_path);
6983 free(new_head_commit_id);
6984 unlockerr = lock_worktree(worktree, LOCK_SH);
6985 if (unlockerr && err == NULL)
6986 err = unlockerr;
6987 return err;
6990 const struct got_error *
6991 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6992 struct got_object_id *commit_id, struct got_repository *repo)
6994 const struct got_error *err;
6995 char *commit_ref_name;
6997 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6998 if (err)
6999 return err;
7001 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7002 if (err)
7003 goto done;
7005 err = delete_ref(commit_ref_name, repo);
7006 done:
7007 free(commit_ref_name);
7008 return err;
7011 const struct got_error *
7012 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7013 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7014 struct got_worktree *worktree, const char *refname,
7015 struct got_repository *repo)
7017 const struct got_error *err = NULL;
7018 char *fileindex_path = NULL;
7019 struct check_rebase_ok_arg ok_arg;
7021 *fileindex = NULL;
7022 *branch_ref = NULL;
7023 *base_branch_ref = NULL;
7025 err = lock_worktree(worktree, LOCK_EX);
7026 if (err)
7027 return err;
7029 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7030 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7031 "cannot integrate a branch into itself; "
7032 "update -b or different branch name required");
7033 goto done;
7036 err = open_fileindex(fileindex, &fileindex_path, worktree);
7037 if (err)
7038 goto done;
7040 /* Preconditions are the same as for rebase. */
7041 ok_arg.worktree = worktree;
7042 ok_arg.repo = repo;
7043 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7044 &ok_arg);
7045 if (err)
7046 goto done;
7048 err = got_ref_open(branch_ref, repo, refname, 1);
7049 if (err)
7050 goto done;
7052 err = got_ref_open(base_branch_ref, repo,
7053 got_worktree_get_head_ref_name(worktree), 1);
7054 done:
7055 if (err) {
7056 if (*branch_ref) {
7057 got_ref_close(*branch_ref);
7058 *branch_ref = NULL;
7060 if (*base_branch_ref) {
7061 got_ref_close(*base_branch_ref);
7062 *base_branch_ref = NULL;
7064 if (*fileindex) {
7065 got_fileindex_free(*fileindex);
7066 *fileindex = NULL;
7068 lock_worktree(worktree, LOCK_SH);
7070 return err;
7073 const struct got_error *
7074 got_worktree_integrate_continue(struct got_worktree *worktree,
7075 struct got_fileindex *fileindex, struct got_repository *repo,
7076 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7077 got_worktree_checkout_cb progress_cb, void *progress_arg,
7078 got_cancel_cb cancel_cb, void *cancel_arg)
7080 const struct got_error *err = NULL, *sync_err, *unlockerr;
7081 char *fileindex_path = NULL;
7082 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7084 err = get_fileindex_path(&fileindex_path, worktree);
7085 if (err)
7086 goto done;
7088 err = got_ref_resolve(&commit_id, repo, branch_ref);
7089 if (err)
7090 goto done;
7092 err = got_object_id_by_path(&tree_id, repo, commit_id,
7093 worktree->path_prefix);
7094 if (err)
7095 goto done;
7097 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7098 if (err)
7099 goto done;
7101 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7102 progress_cb, progress_arg, cancel_cb, cancel_arg);
7103 if (err)
7104 goto sync;
7106 err = got_ref_change_ref(base_branch_ref, commit_id);
7107 if (err)
7108 goto sync;
7110 err = got_ref_write(base_branch_ref, repo);
7111 if (err)
7112 goto sync;
7114 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7115 sync:
7116 sync_err = sync_fileindex(fileindex, fileindex_path);
7117 if (sync_err && err == NULL)
7118 err = sync_err;
7120 done:
7121 unlockerr = got_ref_unlock(branch_ref);
7122 if (unlockerr && err == NULL)
7123 err = unlockerr;
7124 got_ref_close(branch_ref);
7126 unlockerr = got_ref_unlock(base_branch_ref);
7127 if (unlockerr && err == NULL)
7128 err = unlockerr;
7129 got_ref_close(base_branch_ref);
7131 got_fileindex_free(fileindex);
7132 free(fileindex_path);
7133 free(tree_id);
7135 unlockerr = lock_worktree(worktree, LOCK_SH);
7136 if (unlockerr && err == NULL)
7137 err = unlockerr;
7138 return err;
7141 const struct got_error *
7142 got_worktree_integrate_abort(struct got_worktree *worktree,
7143 struct got_fileindex *fileindex, struct got_repository *repo,
7144 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7146 const struct got_error *err = NULL, *unlockerr = NULL;
7148 got_fileindex_free(fileindex);
7150 err = lock_worktree(worktree, LOCK_SH);
7152 unlockerr = got_ref_unlock(branch_ref);
7153 if (unlockerr && err == NULL)
7154 err = unlockerr;
7155 got_ref_close(branch_ref);
7157 unlockerr = got_ref_unlock(base_branch_ref);
7158 if (unlockerr && err == NULL)
7159 err = unlockerr;
7160 got_ref_close(base_branch_ref);
7162 return err;
7165 struct check_stage_ok_arg {
7166 struct got_object_id *head_commit_id;
7167 struct got_worktree *worktree;
7168 struct got_fileindex *fileindex;
7169 struct got_repository *repo;
7170 int have_changes;
7173 const struct got_error *
7174 check_stage_ok(void *arg, unsigned char status,
7175 unsigned char staged_status, const char *relpath,
7176 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7177 struct got_object_id *commit_id, int dirfd, const char *de_name)
7179 struct check_stage_ok_arg *a = arg;
7180 const struct got_error *err = NULL;
7181 struct got_fileindex_entry *ie;
7182 struct got_object_id base_commit_id;
7183 struct got_object_id *base_commit_idp = NULL;
7184 char *in_repo_path = NULL, *p;
7186 if (status == GOT_STATUS_UNVERSIONED ||
7187 status == GOT_STATUS_NO_CHANGE)
7188 return NULL;
7189 if (status == GOT_STATUS_NONEXISTENT)
7190 return got_error_set_errno(ENOENT, relpath);
7192 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7193 if (ie == NULL)
7194 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7196 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7197 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7198 relpath) == -1)
7199 return got_error_from_errno("asprintf");
7201 if (got_fileindex_entry_has_commit(ie)) {
7202 memcpy(base_commit_id.sha1, ie->commit_sha1,
7203 SHA1_DIGEST_LENGTH);
7204 base_commit_idp = &base_commit_id;
7207 if (status == GOT_STATUS_CONFLICT) {
7208 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7209 goto done;
7210 } else if (status != GOT_STATUS_ADD &&
7211 status != GOT_STATUS_MODIFY &&
7212 status != GOT_STATUS_DELETE) {
7213 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7214 goto done;
7217 a->have_changes = 1;
7219 p = in_repo_path;
7220 while (p[0] == '/')
7221 p++;
7222 err = check_out_of_date(p, status, staged_status,
7223 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7224 GOT_ERR_STAGE_OUT_OF_DATE);
7225 done:
7226 free(in_repo_path);
7227 return err;
7230 struct stage_path_arg {
7231 struct got_worktree *worktree;
7232 struct got_fileindex *fileindex;
7233 struct got_repository *repo;
7234 got_worktree_status_cb status_cb;
7235 void *status_arg;
7236 got_worktree_patch_cb patch_cb;
7237 void *patch_arg;
7238 int staged_something;
7239 int allow_bad_symlinks;
7242 static const struct got_error *
7243 stage_path(void *arg, unsigned char status,
7244 unsigned char staged_status, const char *relpath,
7245 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7246 struct got_object_id *commit_id, int dirfd, const char *de_name)
7248 struct stage_path_arg *a = arg;
7249 const struct got_error *err = NULL;
7250 struct got_fileindex_entry *ie;
7251 char *ondisk_path = NULL, *path_content = NULL;
7252 uint32_t stage;
7253 struct got_object_id *new_staged_blob_id = NULL;
7254 struct stat sb;
7256 if (status == GOT_STATUS_UNVERSIONED)
7257 return NULL;
7259 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7260 if (ie == NULL)
7261 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7263 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7264 relpath)== -1)
7265 return got_error_from_errno("asprintf");
7267 switch (status) {
7268 case GOT_STATUS_ADD:
7269 case GOT_STATUS_MODIFY:
7270 /* XXX could sb.st_mode be passed in by our caller? */
7271 if (lstat(ondisk_path, &sb) == -1) {
7272 err = got_error_from_errno2("lstat", ondisk_path);
7273 break;
7275 if (a->patch_cb) {
7276 if (status == GOT_STATUS_ADD) {
7277 int choice = GOT_PATCH_CHOICE_NONE;
7278 err = (*a->patch_cb)(&choice, a->patch_arg,
7279 status, ie->path, NULL, 1, 1);
7280 if (err)
7281 break;
7282 if (choice != GOT_PATCH_CHOICE_YES)
7283 break;
7284 } else {
7285 err = create_patched_content(&path_content, 0,
7286 staged_blob_id ? staged_blob_id : blob_id,
7287 ondisk_path, dirfd, de_name, ie->path,
7288 a->repo, a->patch_cb, a->patch_arg);
7289 if (err || path_content == NULL)
7290 break;
7293 err = got_object_blob_create(&new_staged_blob_id,
7294 path_content ? path_content : ondisk_path, a->repo);
7295 if (err)
7296 break;
7297 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7298 SHA1_DIGEST_LENGTH);
7299 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7300 stage = GOT_FILEIDX_STAGE_ADD;
7301 else
7302 stage = GOT_FILEIDX_STAGE_MODIFY;
7303 got_fileindex_entry_stage_set(ie, stage);
7304 if (S_ISLNK(sb.st_mode)) {
7305 int is_bad_symlink = 0;
7306 if (!a->allow_bad_symlinks) {
7307 char target_path[PATH_MAX];
7308 ssize_t target_len;
7309 target_len = readlink(ondisk_path, target_path,
7310 sizeof(target_path));
7311 if (target_len == -1) {
7312 err = got_error_from_errno2("readlink",
7313 ondisk_path);
7314 break;
7316 err = is_bad_symlink_target(&is_bad_symlink,
7317 target_path, target_len, ondisk_path,
7318 a->worktree->root_path);
7319 if (err)
7320 break;
7321 if (is_bad_symlink) {
7322 err = got_error_path(ondisk_path,
7323 GOT_ERR_BAD_SYMLINK);
7324 break;
7327 if (is_bad_symlink)
7328 got_fileindex_entry_staged_filetype_set(ie,
7329 GOT_FILEIDX_MODE_BAD_SYMLINK);
7330 else
7331 got_fileindex_entry_staged_filetype_set(ie,
7332 GOT_FILEIDX_MODE_SYMLINK);
7333 } else {
7334 got_fileindex_entry_staged_filetype_set(ie,
7335 GOT_FILEIDX_MODE_REGULAR_FILE);
7337 a->staged_something = 1;
7338 if (a->status_cb == NULL)
7339 break;
7340 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7341 get_staged_status(ie), relpath, blob_id,
7342 new_staged_blob_id, NULL, dirfd, de_name);
7343 break;
7344 case GOT_STATUS_DELETE:
7345 if (staged_status == GOT_STATUS_DELETE)
7346 break;
7347 if (a->patch_cb) {
7348 int choice = GOT_PATCH_CHOICE_NONE;
7349 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7350 ie->path, NULL, 1, 1);
7351 if (err)
7352 break;
7353 if (choice == GOT_PATCH_CHOICE_NO)
7354 break;
7355 if (choice != GOT_PATCH_CHOICE_YES) {
7356 err = got_error(GOT_ERR_PATCH_CHOICE);
7357 break;
7360 stage = GOT_FILEIDX_STAGE_DELETE;
7361 got_fileindex_entry_stage_set(ie, stage);
7362 a->staged_something = 1;
7363 if (a->status_cb == NULL)
7364 break;
7365 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7366 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7367 de_name);
7368 break;
7369 case GOT_STATUS_NO_CHANGE:
7370 break;
7371 case GOT_STATUS_CONFLICT:
7372 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7373 break;
7374 case GOT_STATUS_NONEXISTENT:
7375 err = got_error_set_errno(ENOENT, relpath);
7376 break;
7377 default:
7378 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7379 break;
7382 if (path_content && unlink(path_content) == -1 && err == NULL)
7383 err = got_error_from_errno2("unlink", path_content);
7384 free(path_content);
7385 free(ondisk_path);
7386 free(new_staged_blob_id);
7387 return err;
7390 const struct got_error *
7391 got_worktree_stage(struct got_worktree *worktree,
7392 struct got_pathlist_head *paths,
7393 got_worktree_status_cb status_cb, void *status_arg,
7394 got_worktree_patch_cb patch_cb, void *patch_arg,
7395 int allow_bad_symlinks, struct got_repository *repo)
7397 const struct got_error *err = NULL, *sync_err, *unlockerr;
7398 struct got_pathlist_entry *pe;
7399 struct got_fileindex *fileindex = NULL;
7400 char *fileindex_path = NULL;
7401 struct got_reference *head_ref = NULL;
7402 struct got_object_id *head_commit_id = NULL;
7403 struct check_stage_ok_arg oka;
7404 struct stage_path_arg spa;
7406 err = lock_worktree(worktree, LOCK_EX);
7407 if (err)
7408 return err;
7410 err = got_ref_open(&head_ref, repo,
7411 got_worktree_get_head_ref_name(worktree), 0);
7412 if (err)
7413 goto done;
7414 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7415 if (err)
7416 goto done;
7417 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7418 if (err)
7419 goto done;
7421 /* Check pre-conditions before staging anything. */
7422 oka.head_commit_id = head_commit_id;
7423 oka.worktree = worktree;
7424 oka.fileindex = fileindex;
7425 oka.repo = repo;
7426 oka.have_changes = 0;
7427 TAILQ_FOREACH(pe, paths, entry) {
7428 err = worktree_status(worktree, pe->path, fileindex, repo,
7429 check_stage_ok, &oka, NULL, NULL, 0, 0);
7430 if (err)
7431 goto done;
7433 if (!oka.have_changes) {
7434 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7435 goto done;
7438 spa.worktree = worktree;
7439 spa.fileindex = fileindex;
7440 spa.repo = repo;
7441 spa.patch_cb = patch_cb;
7442 spa.patch_arg = patch_arg;
7443 spa.status_cb = status_cb;
7444 spa.status_arg = status_arg;
7445 spa.staged_something = 0;
7446 spa.allow_bad_symlinks = allow_bad_symlinks;
7447 TAILQ_FOREACH(pe, paths, entry) {
7448 err = worktree_status(worktree, pe->path, fileindex, repo,
7449 stage_path, &spa, NULL, NULL, 0, 0);
7450 if (err)
7451 goto done;
7453 if (!spa.staged_something) {
7454 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7455 goto done;
7458 sync_err = sync_fileindex(fileindex, fileindex_path);
7459 if (sync_err && err == NULL)
7460 err = sync_err;
7461 done:
7462 if (head_ref)
7463 got_ref_close(head_ref);
7464 free(head_commit_id);
7465 free(fileindex_path);
7466 if (fileindex)
7467 got_fileindex_free(fileindex);
7468 unlockerr = lock_worktree(worktree, LOCK_SH);
7469 if (unlockerr && err == NULL)
7470 err = unlockerr;
7471 return err;
7474 struct unstage_path_arg {
7475 struct got_worktree *worktree;
7476 struct got_fileindex *fileindex;
7477 struct got_repository *repo;
7478 got_worktree_checkout_cb progress_cb;
7479 void *progress_arg;
7480 got_worktree_patch_cb patch_cb;
7481 void *patch_arg;
7484 static const struct got_error *
7485 create_unstaged_content(char **path_unstaged_content,
7486 char **path_new_staged_content, struct got_object_id *blob_id,
7487 struct got_object_id *staged_blob_id, const char *relpath,
7488 struct got_repository *repo,
7489 got_worktree_patch_cb patch_cb, void *patch_arg)
7491 const struct got_error *err, *free_err;
7492 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7493 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7494 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7495 struct got_diffreg_result *diffreg_result = NULL;
7496 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7497 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7499 *path_unstaged_content = NULL;
7500 *path_new_staged_content = NULL;
7502 err = got_object_id_str(&label1, blob_id);
7503 if (err)
7504 return err;
7505 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7506 if (err)
7507 goto done;
7509 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7510 if (err)
7511 goto done;
7513 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7514 if (err)
7515 goto done;
7517 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7518 if (err)
7519 goto done;
7521 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7522 if (err)
7523 goto done;
7525 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7526 if (err)
7527 goto done;
7529 err = got_diff_files(&diffreg_result, f1, label1, f2,
7530 path2, 3, 0, 1, NULL);
7531 if (err)
7532 goto done;
7534 err = got_opentemp_named(path_unstaged_content, &outfile,
7535 "got-unstaged-content");
7536 if (err)
7537 goto done;
7538 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7539 "got-new-staged-content");
7540 if (err)
7541 goto done;
7543 if (fseek(f1, 0L, SEEK_SET) == -1) {
7544 err = got_ferror(f1, GOT_ERR_IO);
7545 goto done;
7547 if (fseek(f2, 0L, SEEK_SET) == -1) {
7548 err = got_ferror(f2, GOT_ERR_IO);
7549 goto done;
7551 /* Count the number of actual changes in the diff result. */
7552 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7553 struct diff_chunk_context cc = {};
7554 diff_chunk_context_load_change(&cc, &nchunks_used,
7555 diffreg_result->result, n, 0);
7556 nchanges++;
7558 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7559 int choice;
7560 err = apply_or_reject_change(&choice, &nchunks_used,
7561 diffreg_result->result, n, relpath, f1, f2,
7562 &line_cur1, &line_cur2,
7563 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7564 if (err)
7565 goto done;
7566 if (choice == GOT_PATCH_CHOICE_YES)
7567 have_content = 1;
7568 else
7569 have_rejected_content = 1;
7570 if (choice == GOT_PATCH_CHOICE_QUIT)
7571 break;
7573 if (have_content || have_rejected_content)
7574 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7575 outfile, rejectfile);
7576 done:
7577 free(label1);
7578 if (blob)
7579 got_object_blob_close(blob);
7580 if (staged_blob)
7581 got_object_blob_close(staged_blob);
7582 free_err = got_diffreg_result_free(diffreg_result);
7583 if (free_err && err == NULL)
7584 err = free_err;
7585 if (f1 && fclose(f1) == EOF && err == NULL)
7586 err = got_error_from_errno2("fclose", path1);
7587 if (f2 && fclose(f2) == EOF && err == NULL)
7588 err = got_error_from_errno2("fclose", path2);
7589 if (outfile && fclose(outfile) == EOF && err == NULL)
7590 err = got_error_from_errno2("fclose", *path_unstaged_content);
7591 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7592 err = got_error_from_errno2("fclose", *path_new_staged_content);
7593 if (path1 && unlink(path1) == -1 && err == NULL)
7594 err = got_error_from_errno2("unlink", path1);
7595 if (path2 && unlink(path2) == -1 && err == NULL)
7596 err = got_error_from_errno2("unlink", path2);
7597 if (err || !have_content) {
7598 if (*path_unstaged_content &&
7599 unlink(*path_unstaged_content) == -1 && err == NULL)
7600 err = got_error_from_errno2("unlink",
7601 *path_unstaged_content);
7602 free(*path_unstaged_content);
7603 *path_unstaged_content = NULL;
7605 if (err || !have_content || !have_rejected_content) {
7606 if (*path_new_staged_content &&
7607 unlink(*path_new_staged_content) == -1 && err == NULL)
7608 err = got_error_from_errno2("unlink",
7609 *path_new_staged_content);
7610 free(*path_new_staged_content);
7611 *path_new_staged_content = NULL;
7613 free(path1);
7614 free(path2);
7615 return err;
7618 static const struct got_error *
7619 unstage_hunks(struct got_object_id *staged_blob_id,
7620 struct got_blob_object *blob_base,
7621 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7622 const char *ondisk_path, const char *label_orig,
7623 struct got_worktree *worktree, struct got_repository *repo,
7624 got_worktree_patch_cb patch_cb, void *patch_arg,
7625 got_worktree_checkout_cb progress_cb, void *progress_arg)
7627 const struct got_error *err = NULL;
7628 char *path_unstaged_content = NULL;
7629 char *path_new_staged_content = NULL;
7630 struct got_object_id *new_staged_blob_id = NULL;
7631 FILE *f = NULL;
7632 struct stat sb;
7634 err = create_unstaged_content(&path_unstaged_content,
7635 &path_new_staged_content, blob_id, staged_blob_id,
7636 ie->path, repo, patch_cb, patch_arg);
7637 if (err)
7638 return err;
7640 if (path_unstaged_content == NULL)
7641 return NULL;
7643 if (path_new_staged_content) {
7644 err = got_object_blob_create(&new_staged_blob_id,
7645 path_new_staged_content, repo);
7646 if (err)
7647 goto done;
7650 f = fopen(path_unstaged_content, "r");
7651 if (f == NULL) {
7652 err = got_error_from_errno2("fopen",
7653 path_unstaged_content);
7654 goto done;
7656 if (fstat(fileno(f), &sb) == -1) {
7657 err = got_error_from_errno2("fstat", path_unstaged_content);
7658 goto done;
7660 if (got_fileindex_entry_staged_filetype_get(ie) ==
7661 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7662 char link_target[PATH_MAX];
7663 size_t r;
7664 r = fread(link_target, 1, sizeof(link_target), f);
7665 if (r == 0 && ferror(f)) {
7666 err = got_error_from_errno("fread");
7667 goto done;
7669 if (r >= sizeof(link_target)) { /* should not happen */
7670 err = got_error(GOT_ERR_NO_SPACE);
7671 goto done;
7673 link_target[r] = '\0';
7674 err = merge_symlink(worktree, blob_base,
7675 ondisk_path, ie->path, label_orig, link_target,
7676 worktree->base_commit_id, repo, progress_cb,
7677 progress_arg);
7678 } else {
7679 int local_changes_subsumed;
7680 err = merge_file(&local_changes_subsumed, worktree,
7681 blob_base, ondisk_path, ie->path,
7682 got_fileindex_perms_to_st(ie),
7683 path_unstaged_content, label_orig, "unstaged",
7684 repo, progress_cb, progress_arg);
7686 if (err)
7687 goto done;
7689 if (new_staged_blob_id) {
7690 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7691 SHA1_DIGEST_LENGTH);
7692 } else {
7693 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7694 got_fileindex_entry_staged_filetype_set(ie, 0);
7696 done:
7697 free(new_staged_blob_id);
7698 if (path_unstaged_content &&
7699 unlink(path_unstaged_content) == -1 && err == NULL)
7700 err = got_error_from_errno2("unlink", path_unstaged_content);
7701 if (path_new_staged_content &&
7702 unlink(path_new_staged_content) == -1 && err == NULL)
7703 err = got_error_from_errno2("unlink", path_new_staged_content);
7704 if (f && fclose(f) == EOF && err == NULL)
7705 err = got_error_from_errno2("fclose", path_unstaged_content);
7706 free(path_unstaged_content);
7707 free(path_new_staged_content);
7708 return err;
7711 static const struct got_error *
7712 unstage_path(void *arg, unsigned char status,
7713 unsigned char staged_status, const char *relpath,
7714 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7715 struct got_object_id *commit_id, int dirfd, const char *de_name)
7717 const struct got_error *err = NULL;
7718 struct unstage_path_arg *a = arg;
7719 struct got_fileindex_entry *ie;
7720 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7721 char *ondisk_path = NULL;
7722 char *id_str = NULL, *label_orig = NULL;
7723 int local_changes_subsumed;
7724 struct stat sb;
7726 if (staged_status != GOT_STATUS_ADD &&
7727 staged_status != GOT_STATUS_MODIFY &&
7728 staged_status != GOT_STATUS_DELETE)
7729 return NULL;
7731 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7732 if (ie == NULL)
7733 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7735 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7736 == -1)
7737 return got_error_from_errno("asprintf");
7739 err = got_object_id_str(&id_str,
7740 commit_id ? commit_id : a->worktree->base_commit_id);
7741 if (err)
7742 goto done;
7743 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7744 id_str) == -1) {
7745 err = got_error_from_errno("asprintf");
7746 goto done;
7749 switch (staged_status) {
7750 case GOT_STATUS_MODIFY:
7751 err = got_object_open_as_blob(&blob_base, a->repo,
7752 blob_id, 8192);
7753 if (err)
7754 break;
7755 /* fall through */
7756 case GOT_STATUS_ADD:
7757 if (a->patch_cb) {
7758 if (staged_status == GOT_STATUS_ADD) {
7759 int choice = GOT_PATCH_CHOICE_NONE;
7760 err = (*a->patch_cb)(&choice, a->patch_arg,
7761 staged_status, ie->path, NULL, 1, 1);
7762 if (err)
7763 break;
7764 if (choice != GOT_PATCH_CHOICE_YES)
7765 break;
7766 } else {
7767 err = unstage_hunks(staged_blob_id,
7768 blob_base, blob_id, ie, ondisk_path,
7769 label_orig, a->worktree, a->repo,
7770 a->patch_cb, a->patch_arg,
7771 a->progress_cb, a->progress_arg);
7772 break; /* Done with this file. */
7775 err = got_object_open_as_blob(&blob_staged, a->repo,
7776 staged_blob_id, 8192);
7777 if (err)
7778 break;
7779 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7780 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7781 case GOT_FILEIDX_MODE_REGULAR_FILE:
7782 err = merge_blob(&local_changes_subsumed, a->worktree,
7783 blob_base, ondisk_path, relpath,
7784 got_fileindex_perms_to_st(ie), label_orig,
7785 blob_staged, commit_id ? commit_id :
7786 a->worktree->base_commit_id, a->repo,
7787 a->progress_cb, a->progress_arg);
7788 break;
7789 case GOT_FILEIDX_MODE_SYMLINK:
7790 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7791 char *staged_target;
7792 err = got_object_blob_read_to_str(
7793 &staged_target, blob_staged);
7794 if (err)
7795 goto done;
7796 err = merge_symlink(a->worktree, blob_base,
7797 ondisk_path, relpath, label_orig,
7798 staged_target, commit_id ? commit_id :
7799 a->worktree->base_commit_id,
7800 a->repo, a->progress_cb, a->progress_arg);
7801 free(staged_target);
7802 } else {
7803 err = merge_blob(&local_changes_subsumed,
7804 a->worktree, blob_base, ondisk_path,
7805 relpath, got_fileindex_perms_to_st(ie),
7806 label_orig, blob_staged,
7807 commit_id ? commit_id :
7808 a->worktree->base_commit_id, a->repo,
7809 a->progress_cb, a->progress_arg);
7811 break;
7812 default:
7813 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7814 break;
7816 if (err == NULL) {
7817 got_fileindex_entry_stage_set(ie,
7818 GOT_FILEIDX_STAGE_NONE);
7819 got_fileindex_entry_staged_filetype_set(ie, 0);
7821 break;
7822 case GOT_STATUS_DELETE:
7823 if (a->patch_cb) {
7824 int choice = GOT_PATCH_CHOICE_NONE;
7825 err = (*a->patch_cb)(&choice, a->patch_arg,
7826 staged_status, ie->path, NULL, 1, 1);
7827 if (err)
7828 break;
7829 if (choice == GOT_PATCH_CHOICE_NO)
7830 break;
7831 if (choice != GOT_PATCH_CHOICE_YES) {
7832 err = got_error(GOT_ERR_PATCH_CHOICE);
7833 break;
7836 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7837 got_fileindex_entry_staged_filetype_set(ie, 0);
7838 err = get_file_status(&status, &sb, ie, ondisk_path,
7839 dirfd, de_name, a->repo);
7840 if (err)
7841 break;
7842 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7843 break;
7845 done:
7846 free(ondisk_path);
7847 if (blob_base)
7848 got_object_blob_close(blob_base);
7849 if (blob_staged)
7850 got_object_blob_close(blob_staged);
7851 free(id_str);
7852 free(label_orig);
7853 return err;
7856 const struct got_error *
7857 got_worktree_unstage(struct got_worktree *worktree,
7858 struct got_pathlist_head *paths,
7859 got_worktree_checkout_cb progress_cb, void *progress_arg,
7860 got_worktree_patch_cb patch_cb, void *patch_arg,
7861 struct got_repository *repo)
7863 const struct got_error *err = NULL, *sync_err, *unlockerr;
7864 struct got_pathlist_entry *pe;
7865 struct got_fileindex *fileindex = NULL;
7866 char *fileindex_path = NULL;
7867 struct unstage_path_arg upa;
7869 err = lock_worktree(worktree, LOCK_EX);
7870 if (err)
7871 return err;
7873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7874 if (err)
7875 goto done;
7877 upa.worktree = worktree;
7878 upa.fileindex = fileindex;
7879 upa.repo = repo;
7880 upa.progress_cb = progress_cb;
7881 upa.progress_arg = progress_arg;
7882 upa.patch_cb = patch_cb;
7883 upa.patch_arg = patch_arg;
7884 TAILQ_FOREACH(pe, paths, entry) {
7885 err = worktree_status(worktree, pe->path, fileindex, repo,
7886 unstage_path, &upa, NULL, NULL, 0, 0);
7887 if (err)
7888 goto done;
7891 sync_err = sync_fileindex(fileindex, fileindex_path);
7892 if (sync_err && err == NULL)
7893 err = sync_err;
7894 done:
7895 free(fileindex_path);
7896 if (fileindex)
7897 got_fileindex_free(fileindex);
7898 unlockerr = lock_worktree(worktree, LOCK_SH);
7899 if (unlockerr && err == NULL)
7900 err = unlockerr;
7901 return err;
7904 struct report_file_info_arg {
7905 struct got_worktree *worktree;
7906 got_worktree_path_info_cb info_cb;
7907 void *info_arg;
7908 struct got_pathlist_head *paths;
7909 got_cancel_cb cancel_cb;
7910 void *cancel_arg;
7913 static const struct got_error *
7914 report_file_info(void *arg, struct got_fileindex_entry *ie)
7916 struct report_file_info_arg *a = arg;
7917 struct got_pathlist_entry *pe;
7918 struct got_object_id blob_id, staged_blob_id, commit_id;
7919 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7920 struct got_object_id *commit_idp = NULL;
7921 int stage;
7923 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7924 return got_error(GOT_ERR_CANCELLED);
7926 TAILQ_FOREACH(pe, a->paths, entry) {
7927 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7928 got_path_is_child(ie->path, pe->path, pe->path_len))
7929 break;
7931 if (pe == NULL) /* not found */
7932 return NULL;
7934 if (got_fileindex_entry_has_blob(ie)) {
7935 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7936 blob_idp = &blob_id;
7938 stage = got_fileindex_entry_stage_get(ie);
7939 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7940 stage == GOT_FILEIDX_STAGE_ADD) {
7941 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7942 SHA1_DIGEST_LENGTH);
7943 staged_blob_idp = &staged_blob_id;
7946 if (got_fileindex_entry_has_commit(ie)) {
7947 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7948 commit_idp = &commit_id;
7951 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7952 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7955 const struct got_error *
7956 got_worktree_path_info(struct got_worktree *worktree,
7957 struct got_pathlist_head *paths,
7958 got_worktree_path_info_cb info_cb, void *info_arg,
7959 got_cancel_cb cancel_cb, void *cancel_arg)
7962 const struct got_error *err = NULL, *unlockerr;
7963 struct got_fileindex *fileindex = NULL;
7964 char *fileindex_path = NULL;
7965 struct report_file_info_arg arg;
7967 err = lock_worktree(worktree, LOCK_SH);
7968 if (err)
7969 return err;
7971 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7972 if (err)
7973 goto done;
7975 arg.worktree = worktree;
7976 arg.info_cb = info_cb;
7977 arg.info_arg = info_arg;
7978 arg.paths = paths;
7979 arg.cancel_cb = cancel_cb;
7980 arg.cancel_arg = cancel_arg;
7981 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7982 &arg);
7983 done:
7984 free(fileindex_path);
7985 if (fileindex)
7986 got_fileindex_free(fileindex);
7987 unlockerr = lock_worktree(worktree, LOCK_UN);
7988 if (unlockerr && err == NULL)
7989 err = unlockerr;
7990 return err;