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(int *did_something, const char *ondisk_path,
1248 const char *target_path, size_t target_len)
1250 const struct got_error *err = NULL;
1251 ssize_t elen;
1252 char etarget[PATH_MAX];
1253 int fd;
1255 *did_something = 0;
1258 * "Bad" symlinks (those pointing outside the work tree or into the
1259 * .got directory) are installed in the work tree as a regular file
1260 * which contains the bad symlink target path.
1261 * The new symlink target has already been checked for safety by our
1262 * caller. If we can successfully open a regular file then we simply
1263 * replace this file with a symlink below.
1265 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1266 if (fd == -1) {
1267 if (errno != ELOOP)
1268 return got_error_from_errno2("open", ondisk_path);
1270 /* We are updating an existing on-disk symlink. */
1271 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1272 if (elen == -1)
1273 return got_error_from_errno2("readlink", ondisk_path);
1275 if (elen == target_len &&
1276 memcmp(etarget, target_path, target_len) == 0)
1277 return NULL; /* nothing to do */
1280 *did_something = 1;
1281 err = update_symlink(ondisk_path, target_path, target_len);
1282 if (fd != -1 && close(fd) == -1 && err == NULL)
1283 err = got_error_from_errno2("close", ondisk_path);
1284 return err;
1287 static const struct got_error *
1288 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1289 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1291 const struct got_error *err = NULL;
1292 char canonpath[PATH_MAX];
1293 char *path_got = NULL;
1295 *is_bad_symlink = 0;
1297 if (target_len >= sizeof(canonpath)) {
1298 *is_bad_symlink = 1;
1299 return NULL;
1303 * We do not use realpath(3) to resolve the symlink's target
1304 * path because we don't want to resolve symlinks recursively.
1305 * Instead we make the path absolute and then canonicalize it.
1306 * Relative symlink target lookup should begin at the directory
1307 * in which the blob object is being installed.
1309 if (!got_path_is_absolute(target_path)) {
1310 char *abspath, *parent;
1311 err = got_path_dirname(&parent, ondisk_path);
1312 if (err)
1313 return err;
1314 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1315 free(parent);
1316 return got_error_from_errno("asprintf");
1318 free(parent);
1319 if (strlen(abspath) >= sizeof(canonpath)) {
1320 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1321 free(abspath);
1322 return err;
1324 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1325 free(abspath);
1326 if (err)
1327 return err;
1328 } else {
1329 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1330 if (err)
1331 return err;
1334 /* Only allow symlinks pointing at paths within the work tree. */
1335 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1336 *is_bad_symlink = 1;
1337 return NULL;
1340 /* Do not allow symlinks pointing into the .got directory. */
1341 if (asprintf(&path_got, "%s/%s", wtroot_path,
1342 GOT_WORKTREE_GOT_DIR) == -1)
1343 return got_error_from_errno("asprintf");
1344 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1345 *is_bad_symlink = 1;
1347 free(path_got);
1348 return NULL;
1351 static const struct got_error *
1352 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1353 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1354 int restoring_missing_file, int reverting_versioned_file,
1355 int path_is_unversioned, struct got_repository *repo,
1356 got_worktree_checkout_cb progress_cb, void *progress_arg)
1358 const struct got_error *err = NULL;
1359 char target_path[PATH_MAX];
1360 size_t len, target_len = 0;
1361 char *path_got = NULL;
1362 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1363 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1365 *is_bad_symlink = 0;
1368 * Blob object content specifies the target path of the link.
1369 * If a symbolic link cannot be installed we instead create
1370 * a regular file which contains the link target path stored
1371 * in the blob object.
1373 do {
1374 err = got_object_blob_read_block(&len, blob);
1375 if (len + target_len >= sizeof(target_path)) {
1376 /* Path too long; install as a regular file. */
1377 *is_bad_symlink = 1;
1378 got_object_blob_rewind(blob);
1379 return install_blob(worktree, ondisk_path, path,
1380 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1381 restoring_missing_file, reverting_versioned_file,
1382 1, path_is_unversioned, repo, progress_cb,
1383 progress_arg);
1385 if (len > 0) {
1386 /* Skip blob object header first time around. */
1387 memcpy(target_path + target_len, buf + hdrlen,
1388 len - hdrlen);
1389 target_len += len - hdrlen;
1390 hdrlen = 0;
1392 } while (len != 0);
1393 target_path[target_len] = '\0';
1395 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1396 ondisk_path, worktree->root_path);
1397 if (err)
1398 return err;
1400 if (*is_bad_symlink) {
1401 /* install as a regular file */
1402 got_object_blob_rewind(blob);
1403 err = install_blob(worktree, ondisk_path, path,
1404 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1405 restoring_missing_file, reverting_versioned_file, 1,
1406 path_is_unversioned, repo, progress_cb, progress_arg);
1407 goto done;
1410 if (symlink(target_path, ondisk_path) == -1) {
1411 if (errno == EEXIST) {
1412 int symlink_replaced;
1413 if (path_is_unversioned) {
1414 err = (*progress_cb)(progress_arg,
1415 GOT_STATUS_UNVERSIONED, path);
1416 goto done;
1418 err = replace_existing_symlink(&symlink_replaced,
1419 ondisk_path, target_path, target_len);
1420 if (err)
1421 goto done;
1422 if (progress_cb) {
1423 if (symlink_replaced) {
1424 err = (*progress_cb)(progress_arg,
1425 reverting_versioned_file ?
1426 GOT_STATUS_REVERT :
1427 GOT_STATUS_UPDATE, path);
1428 } else {
1429 err = (*progress_cb)(progress_arg,
1430 GOT_STATUS_EXISTS, path);
1433 goto done; /* Nothing else to do. */
1436 if (errno == ENOENT) {
1437 char *parent;
1438 err = got_path_dirname(&parent, ondisk_path);
1439 if (err)
1440 goto done;
1441 err = add_dir_on_disk(worktree, parent);
1442 free(parent);
1443 if (err)
1444 goto done;
1446 * Retry, and fall through to error handling
1447 * below if this second attempt fails.
1449 if (symlink(target_path, ondisk_path) != -1) {
1450 err = NULL; /* success */
1451 goto done;
1455 /* Handle errors from first or second creation attempt. */
1456 if (errno == ENAMETOOLONG) {
1457 /* bad target path; install as a regular file */
1458 *is_bad_symlink = 1;
1459 got_object_blob_rewind(blob);
1460 err = install_blob(worktree, ondisk_path, path,
1461 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1462 restoring_missing_file, reverting_versioned_file, 1,
1463 path_is_unversioned, repo,
1464 progress_cb, progress_arg);
1465 } else if (errno == ENOTDIR) {
1466 err = got_error_path(ondisk_path,
1467 GOT_ERR_FILE_OBSTRUCTED);
1468 } else {
1469 err = got_error_from_errno3("symlink",
1470 target_path, ondisk_path);
1472 } else if (progress_cb)
1473 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1474 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1475 done:
1476 free(path_got);
1477 return err;
1480 static const struct got_error *
1481 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1482 const char *path, mode_t te_mode, mode_t st_mode,
1483 struct got_blob_object *blob, int restoring_missing_file,
1484 int reverting_versioned_file, int installing_bad_symlink,
1485 int path_is_unversioned, struct got_repository *repo,
1486 got_worktree_checkout_cb progress_cb, void *progress_arg)
1488 const struct got_error *err = NULL;
1489 int fd = -1;
1490 size_t len, hdrlen;
1491 int update = 0;
1492 char *tmppath = NULL;
1494 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1495 GOT_DEFAULT_FILE_MODE);
1496 if (fd == -1) {
1497 if (errno == ENOENT) {
1498 char *parent;
1499 err = got_path_dirname(&parent, path);
1500 if (err)
1501 return err;
1502 err = add_dir_on_disk(worktree, parent);
1503 free(parent);
1504 if (err)
1505 return err;
1506 fd = open(ondisk_path,
1507 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1508 GOT_DEFAULT_FILE_MODE);
1509 if (fd == -1)
1510 return got_error_from_errno2("open",
1511 ondisk_path);
1512 } else if (errno == EEXIST) {
1513 if (path_is_unversioned) {
1514 err = (*progress_cb)(progress_arg,
1515 GOT_STATUS_UNVERSIONED, path);
1516 goto done;
1518 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1519 !S_ISREG(st_mode) && !installing_bad_symlink) {
1520 /* TODO file is obstructed; do something */
1521 err = got_error_path(ondisk_path,
1522 GOT_ERR_FILE_OBSTRUCTED);
1523 goto done;
1524 } else {
1525 err = got_opentemp_named_fd(&tmppath, &fd,
1526 ondisk_path);
1527 if (err)
1528 goto done;
1529 update = 1;
1531 } else
1532 return got_error_from_errno2("open", ondisk_path);
1535 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1536 err = got_error_from_errno2("fchmod",
1537 update ? tmppath : ondisk_path);
1538 goto done;
1541 if (progress_cb) {
1542 if (restoring_missing_file)
1543 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1544 path);
1545 else if (reverting_versioned_file)
1546 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1547 path);
1548 else
1549 err = (*progress_cb)(progress_arg,
1550 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1551 if (err)
1552 goto done;
1555 hdrlen = got_object_blob_get_hdrlen(blob);
1556 do {
1557 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1558 err = got_object_blob_read_block(&len, blob);
1559 if (err)
1560 break;
1561 if (len > 0) {
1562 /* Skip blob object header first time around. */
1563 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1564 if (outlen == -1) {
1565 err = got_error_from_errno("write");
1566 goto done;
1567 } else if (outlen != len - hdrlen) {
1568 err = got_error(GOT_ERR_IO);
1569 goto done;
1571 hdrlen = 0;
1573 } while (len != 0);
1575 if (fsync(fd) != 0) {
1576 err = got_error_from_errno("fsync");
1577 goto done;
1580 if (update) {
1581 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1582 err = got_error_from_errno2("unlink", ondisk_path);
1583 goto done;
1585 if (rename(tmppath, ondisk_path) != 0) {
1586 err = got_error_from_errno3("rename", tmppath,
1587 ondisk_path);
1588 goto done;
1590 free(tmppath);
1591 tmppath = NULL;
1594 done:
1595 if (fd != -1 && close(fd) == -1 && err == NULL)
1596 err = got_error_from_errno("close");
1597 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1598 err = got_error_from_errno2("unlink", tmppath);
1599 free(tmppath);
1600 return err;
1603 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1604 static const struct got_error *
1605 get_modified_file_content_status(unsigned char *status, FILE *f)
1607 const struct got_error *err = NULL;
1608 const char *markers[3] = {
1609 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1610 GOT_DIFF_CONFLICT_MARKER_SEP,
1611 GOT_DIFF_CONFLICT_MARKER_END
1613 int i = 0;
1614 char *line = NULL;
1615 size_t linesize = 0;
1616 ssize_t linelen;
1618 while (*status == GOT_STATUS_MODIFY) {
1619 linelen = getline(&line, &linesize, f);
1620 if (linelen == -1) {
1621 if (feof(f))
1622 break;
1623 err = got_ferror(f, GOT_ERR_IO);
1624 break;
1627 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1628 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1629 == 0)
1630 *status = GOT_STATUS_CONFLICT;
1631 else
1632 i++;
1635 free(line);
1637 return err;
1640 static int
1641 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1643 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1644 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1647 static int
1648 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1650 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1651 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1652 ie->mtime_sec == sb->st_mtim.tv_sec &&
1653 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1654 ie->size == (sb->st_size & 0xffffffff) &&
1655 !xbit_differs(ie, sb->st_mode));
1658 static unsigned char
1659 get_staged_status(struct got_fileindex_entry *ie)
1661 switch (got_fileindex_entry_stage_get(ie)) {
1662 case GOT_FILEIDX_STAGE_ADD:
1663 return GOT_STATUS_ADD;
1664 case GOT_FILEIDX_STAGE_DELETE:
1665 return GOT_STATUS_DELETE;
1666 case GOT_FILEIDX_STAGE_MODIFY:
1667 return GOT_STATUS_MODIFY;
1668 default:
1669 return GOT_STATUS_NO_CHANGE;
1673 static const struct got_error *
1674 get_symlink_modification_status(unsigned char *status,
1675 struct got_fileindex_entry *ie, const char *abspath,
1676 int dirfd, const char *de_name, struct got_blob_object *blob)
1678 const struct got_error *err = NULL;
1679 char target_path[PATH_MAX];
1680 char etarget[PATH_MAX];
1681 ssize_t elen;
1682 size_t len, target_len = 0;
1683 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1684 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1686 *status = GOT_STATUS_NO_CHANGE;
1688 /* Blob object content specifies the target path of the link. */
1689 do {
1690 err = got_object_blob_read_block(&len, blob);
1691 if (err)
1692 return err;
1693 if (len + target_len >= sizeof(target_path)) {
1695 * Should not happen. The blob contents were OK
1696 * when this symlink was installed.
1698 return got_error(GOT_ERR_NO_SPACE);
1700 if (len > 0) {
1701 /* Skip blob object header first time around. */
1702 memcpy(target_path + target_len, buf + hdrlen,
1703 len - hdrlen);
1704 target_len += len - hdrlen;
1705 hdrlen = 0;
1707 } while (len != 0);
1708 target_path[target_len] = '\0';
1710 if (dirfd != -1) {
1711 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1712 if (elen == -1)
1713 return got_error_from_errno2("readlinkat", abspath);
1714 } else {
1715 elen = readlink(abspath, etarget, sizeof(etarget));
1716 if (elen == -1)
1717 return got_error_from_errno2("readlink", abspath);
1720 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1721 *status = GOT_STATUS_MODIFY;
1723 return NULL;
1726 static const struct got_error *
1727 get_file_status(unsigned char *status, struct stat *sb,
1728 struct got_fileindex_entry *ie, const char *abspath,
1729 int dirfd, const char *de_name, struct got_repository *repo)
1731 const struct got_error *err = NULL;
1732 struct got_object_id id;
1733 size_t hdrlen;
1734 int fd = -1;
1735 FILE *f = NULL;
1736 uint8_t fbuf[8192];
1737 struct got_blob_object *blob = NULL;
1738 size_t flen, blen;
1739 unsigned char staged_status = get_staged_status(ie);
1741 *status = GOT_STATUS_NO_CHANGE;
1742 memset(sb, 0, sizeof(*sb));
1745 * Whenever the caller provides a directory descriptor and a
1746 * directory entry name for the file, use them! This prevents
1747 * race conditions if filesystem paths change beneath our feet.
1749 if (dirfd != -1) {
1750 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1751 if (errno == ENOENT) {
1752 if (got_fileindex_entry_has_file_on_disk(ie))
1753 *status = GOT_STATUS_MISSING;
1754 else
1755 *status = GOT_STATUS_DELETE;
1756 goto done;
1758 err = got_error_from_errno2("fstatat", abspath);
1759 goto done;
1761 } else {
1762 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1763 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1764 return got_error_from_errno2("open", abspath);
1765 else if (fd == -1 && errno == ELOOP) {
1766 if (lstat(abspath, sb) == -1)
1767 return got_error_from_errno2("lstat", abspath);
1768 } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 if (errno == ENOENT) {
1770 if (got_fileindex_entry_has_file_on_disk(ie))
1771 *status = GOT_STATUS_MISSING;
1772 else
1773 *status = GOT_STATUS_DELETE;
1774 goto done;
1776 err = got_error_from_errno2("fstat", abspath);
1777 goto done;
1781 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 *status = GOT_STATUS_OBSTRUCTED;
1783 goto done;
1786 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 *status = GOT_STATUS_DELETE;
1788 goto done;
1789 } else if (!got_fileindex_entry_has_blob(ie) &&
1790 staged_status != GOT_STATUS_ADD) {
1791 *status = GOT_STATUS_ADD;
1792 goto done;
1795 if (!stat_info_differs(ie, sb))
1796 goto done;
1798 if (S_ISLNK(sb->st_mode) &&
1799 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 *status = GOT_STATUS_MODIFY;
1801 goto done;
1804 if (staged_status == GOT_STATUS_MODIFY ||
1805 staged_status == GOT_STATUS_ADD)
1806 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1807 else
1808 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1810 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1811 if (err)
1812 goto done;
1814 if (S_ISLNK(sb->st_mode)) {
1815 err = get_symlink_modification_status(status, ie,
1816 abspath, dirfd, de_name, blob);
1817 goto done;
1820 if (dirfd != -1) {
1821 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1822 if (fd == -1) {
1823 err = got_error_from_errno2("openat", abspath);
1824 goto done;
1828 f = fdopen(fd, "r");
1829 if (f == NULL) {
1830 err = got_error_from_errno2("fdopen", abspath);
1831 goto done;
1833 fd = -1;
1834 hdrlen = got_object_blob_get_hdrlen(blob);
1835 for (;;) {
1836 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1837 err = got_object_blob_read_block(&blen, blob);
1838 if (err)
1839 goto done;
1840 /* Skip length of blob object header first time around. */
1841 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1842 if (flen == 0 && ferror(f)) {
1843 err = got_error_from_errno("fread");
1844 goto done;
1846 if (blen - hdrlen == 0) {
1847 if (flen != 0)
1848 *status = GOT_STATUS_MODIFY;
1849 break;
1850 } else if (flen == 0) {
1851 if (blen - hdrlen != 0)
1852 *status = GOT_STATUS_MODIFY;
1853 break;
1854 } else if (blen - hdrlen == flen) {
1855 /* Skip blob object header first time around. */
1856 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1857 *status = GOT_STATUS_MODIFY;
1858 break;
1860 } else {
1861 *status = GOT_STATUS_MODIFY;
1862 break;
1864 hdrlen = 0;
1867 if (*status == GOT_STATUS_MODIFY) {
1868 rewind(f);
1869 err = get_modified_file_content_status(status, f);
1870 } else if (xbit_differs(ie, sb->st_mode))
1871 *status = GOT_STATUS_MODE_CHANGE;
1872 done:
1873 if (blob)
1874 got_object_blob_close(blob);
1875 if (f != NULL && fclose(f) == EOF && err == NULL)
1876 err = got_error_from_errno2("fclose", abspath);
1877 if (fd != -1 && close(fd) == -1 && err == NULL)
1878 err = got_error_from_errno2("close", abspath);
1879 return err;
1883 * Update timestamps in the file index if a file is unmodified and
1884 * we had to run a full content comparison to find out.
1886 static const struct got_error *
1887 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1888 struct got_fileindex_entry *ie, struct stat *sb)
1890 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1891 return got_fileindex_entry_update(ie, wt_fd, path,
1892 ie->blob_sha1, ie->commit_sha1, 1);
1894 return NULL;
1897 static const struct got_error *
1898 update_blob(struct got_worktree *worktree,
1899 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1900 struct got_tree_entry *te, const char *path,
1901 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1902 void *progress_arg)
1904 const struct got_error *err = NULL;
1905 struct got_blob_object *blob = NULL;
1906 char *ondisk_path;
1907 unsigned char status = GOT_STATUS_NO_CHANGE;
1908 struct stat sb;
1910 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1911 return got_error_from_errno("asprintf");
1913 if (ie) {
1914 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1915 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1916 goto done;
1918 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1919 repo);
1920 if (err)
1921 goto done;
1922 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1923 sb.st_mode = got_fileindex_perms_to_st(ie);
1924 } else {
1925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 status = GOT_STATUS_UNVERSIONED;
1929 if (status == GOT_STATUS_OBSTRUCTED) {
1930 err = (*progress_cb)(progress_arg, status, path);
1931 goto done;
1933 if (status == GOT_STATUS_CONFLICT) {
1934 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1935 path);
1936 goto done;
1939 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1940 (S_ISLNK(te->mode) ||
1941 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1943 * This is a regular file or an installed bad symlink.
1944 * If the file index indicates that this file is already
1945 * up-to-date with respect to the repository we can skip
1946 * updating contents of this file.
1948 if (got_fileindex_entry_has_commit(ie) &&
1949 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1950 SHA1_DIGEST_LENGTH) == 0) {
1951 /* Same commit. */
1952 err = sync_timestamps(worktree->root_fd,
1953 path, status, ie, &sb);
1954 if (err)
1955 goto done;
1956 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1957 path);
1958 goto done;
1960 if (got_fileindex_entry_has_blob(ie) &&
1961 memcmp(ie->blob_sha1, te->id.sha1,
1962 SHA1_DIGEST_LENGTH) == 0) {
1963 /* Different commit but the same blob. */
1964 err = sync_timestamps(worktree->root_fd,
1965 path, status, ie, &sb);
1966 if (err)
1967 goto done;
1968 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1969 path);
1970 goto done;
1974 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1975 if (err)
1976 goto done;
1978 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1979 int update_timestamps;
1980 struct got_blob_object *blob2 = NULL;
1981 char *label_orig = NULL;
1982 if (got_fileindex_entry_has_blob(ie)) {
1983 struct got_object_id id2;
1984 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1985 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1986 if (err)
1987 goto done;
1989 if (got_fileindex_entry_has_commit(ie)) {
1990 char id_str[SHA1_DIGEST_STRING_LENGTH];
1991 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1992 sizeof(id_str)) == NULL) {
1993 err = got_error_path(id_str,
1994 GOT_ERR_BAD_OBJ_ID_STR);
1995 goto done;
1997 if (asprintf(&label_orig, "%s: commit %s",
1998 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1999 err = got_error_from_errno("asprintf");
2000 goto done;
2003 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2004 char *link_target;
2005 err = got_object_blob_read_to_str(&link_target, blob);
2006 if (err)
2007 goto done;
2008 err = merge_symlink(worktree, blob2, ondisk_path, path,
2009 label_orig, link_target, worktree->base_commit_id,
2010 repo, progress_cb, progress_arg);
2011 free(link_target);
2012 } else {
2013 err = merge_blob(&update_timestamps, worktree, blob2,
2014 ondisk_path, path, sb.st_mode, label_orig, blob,
2015 worktree->base_commit_id, repo,
2016 progress_cb, progress_arg);
2018 free(label_orig);
2019 if (blob2)
2020 got_object_blob_close(blob2);
2021 if (err)
2022 goto done;
2024 * Do not update timestamps of files with local changes.
2025 * Otherwise, a future status walk would treat them as
2026 * unmodified files again.
2028 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2029 blob->id.sha1, worktree->base_commit_id->sha1,
2030 update_timestamps);
2031 } else if (status == GOT_STATUS_MODE_CHANGE) {
2032 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2033 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2034 } else if (status == GOT_STATUS_DELETE) {
2035 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2036 if (err)
2037 goto done;
2038 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2040 if (err)
2041 goto done;
2042 } else {
2043 int is_bad_symlink = 0;
2044 if (S_ISLNK(te->mode)) {
2045 err = install_symlink(&is_bad_symlink, worktree,
2046 ondisk_path, path, blob,
2047 status == GOT_STATUS_MISSING, 0,
2048 status == GOT_STATUS_UNVERSIONED, repo,
2049 progress_cb, progress_arg);
2050 } else {
2051 err = install_blob(worktree, ondisk_path, path,
2052 te->mode, sb.st_mode, blob,
2053 status == GOT_STATUS_MISSING, 0, 0,
2054 status == GOT_STATUS_UNVERSIONED, repo,
2055 progress_cb, progress_arg);
2057 if (err)
2058 goto done;
2060 if (ie) {
2061 err = got_fileindex_entry_update(ie,
2062 worktree->root_fd, path, blob->id.sha1,
2063 worktree->base_commit_id->sha1, 1);
2064 } else {
2065 err = create_fileindex_entry(&ie, fileindex,
2066 worktree->base_commit_id, worktree->root_fd, path,
2067 &blob->id);
2069 if (err)
2070 goto done;
2072 if (is_bad_symlink) {
2073 got_fileindex_entry_filetype_set(ie,
2074 GOT_FILEIDX_MODE_BAD_SYMLINK);
2077 got_object_blob_close(blob);
2078 done:
2079 free(ondisk_path);
2080 return err;
2083 static const struct got_error *
2084 remove_ondisk_file(const char *root_path, const char *path)
2086 const struct got_error *err = NULL;
2087 char *ondisk_path = NULL;
2089 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2090 return got_error_from_errno("asprintf");
2092 if (unlink(ondisk_path) == -1) {
2093 if (errno != ENOENT)
2094 err = got_error_from_errno2("unlink", ondisk_path);
2095 } else {
2096 size_t root_len = strlen(root_path);
2097 do {
2098 char *parent;
2099 err = got_path_dirname(&parent, ondisk_path);
2100 if (err)
2101 break;
2102 free(ondisk_path);
2103 ondisk_path = parent;
2104 if (rmdir(ondisk_path) == -1) {
2105 if (errno != ENOTEMPTY)
2106 err = got_error_from_errno2("rmdir",
2107 ondisk_path);
2108 break;
2110 } while (got_path_cmp(ondisk_path, root_path,
2111 strlen(ondisk_path), root_len) != 0);
2113 free(ondisk_path);
2114 return err;
2117 static const struct got_error *
2118 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2119 struct got_fileindex_entry *ie, struct got_repository *repo,
2120 got_worktree_checkout_cb progress_cb, void *progress_arg)
2122 const struct got_error *err = NULL;
2123 unsigned char status;
2124 struct stat sb;
2125 char *ondisk_path;
2127 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2128 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2130 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2131 == -1)
2132 return got_error_from_errno("asprintf");
2134 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2135 if (err)
2136 goto done;
2138 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2139 char ondisk_target[PATH_MAX];
2140 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2141 sizeof(ondisk_target));
2142 if (ondisk_len == -1) {
2143 err = got_error_from_errno2("readlink", ondisk_path);
2144 goto done;
2146 ondisk_target[ondisk_len] = '\0';
2147 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2148 NULL, NULL, /* XXX pass common ancestor info? */
2149 ondisk_target, ondisk_path);
2150 if (err)
2151 goto done;
2152 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2153 ie->path);
2154 goto done;
2157 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2158 status == GOT_STATUS_ADD) {
2159 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2160 if (err)
2161 goto done;
2163 * Preserve the working file and change the deleted blob's
2164 * entry into a schedule-add entry.
2166 err = got_fileindex_entry_update(ie, worktree->root_fd,
2167 ie->path, NULL, NULL, 0);
2168 } else {
2169 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2170 if (err)
2171 goto done;
2172 if (status == GOT_STATUS_NO_CHANGE) {
2173 err = remove_ondisk_file(worktree->root_path, ie->path);
2174 if (err)
2175 goto done;
2177 got_fileindex_entry_remove(fileindex, ie);
2179 done:
2180 free(ondisk_path);
2181 return err;
2184 struct diff_cb_arg {
2185 struct got_fileindex *fileindex;
2186 struct got_worktree *worktree;
2187 struct got_repository *repo;
2188 got_worktree_checkout_cb progress_cb;
2189 void *progress_arg;
2190 got_cancel_cb cancel_cb;
2191 void *cancel_arg;
2194 static const struct got_error *
2195 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2196 struct got_tree_entry *te, const char *parent_path)
2198 struct diff_cb_arg *a = arg;
2200 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2201 return got_error(GOT_ERR_CANCELLED);
2203 return update_blob(a->worktree, a->fileindex, ie, te,
2204 ie->path, a->repo, a->progress_cb, a->progress_arg);
2207 static const struct got_error *
2208 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2210 struct diff_cb_arg *a = arg;
2212 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2213 return got_error(GOT_ERR_CANCELLED);
2215 return delete_blob(a->worktree, a->fileindex, ie,
2216 a->repo, a->progress_cb, a->progress_arg);
2219 static const struct got_error *
2220 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2222 struct diff_cb_arg *a = arg;
2223 const struct got_error *err;
2224 char *path;
2226 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2227 return got_error(GOT_ERR_CANCELLED);
2229 if (got_object_tree_entry_is_submodule(te))
2230 return NULL;
2232 if (asprintf(&path, "%s%s%s", parent_path,
2233 parent_path[0] ? "/" : "", te->name)
2234 == -1)
2235 return got_error_from_errno("asprintf");
2237 if (S_ISDIR(te->mode))
2238 err = add_dir_on_disk(a->worktree, path);
2239 else
2240 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2241 a->repo, a->progress_cb, a->progress_arg);
2243 free(path);
2244 return err;
2247 const struct got_error *
2248 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2250 uint32_t uuid_status;
2252 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2253 if (uuid_status != uuid_s_ok) {
2254 *uuidstr = NULL;
2255 return got_error_uuid(uuid_status, "uuid_to_string");
2258 return NULL;
2261 static const struct got_error *
2262 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2264 const struct got_error *err = NULL;
2265 char *uuidstr = NULL;
2267 *refname = NULL;
2269 err = got_worktree_get_uuid(&uuidstr, worktree);
2270 if (err)
2271 return err;
2273 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2274 err = got_error_from_errno("asprintf");
2275 *refname = NULL;
2277 free(uuidstr);
2278 return err;
2281 const struct got_error *
2282 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2287 static const struct got_error *
2288 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2290 return get_ref_name(refname, worktree,
2291 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2294 static const struct got_error *
2295 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2297 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2300 static const struct got_error *
2301 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2303 return get_ref_name(refname, worktree,
2304 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2307 static const struct got_error *
2308 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 return get_ref_name(refname, worktree,
2311 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2314 static const struct got_error *
2315 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2321 static const struct got_error *
2322 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree,
2325 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2328 static const struct got_error *
2329 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2331 return get_ref_name(refname, worktree,
2332 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2335 static const struct got_error *
2336 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2338 return get_ref_name(refname, worktree,
2339 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2342 const struct got_error *
2343 got_worktree_get_histedit_script_path(char **path,
2344 struct got_worktree *worktree)
2346 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2347 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2348 *path = NULL;
2349 return got_error_from_errno("asprintf");
2351 return NULL;
2355 * Prevent Git's garbage collector from deleting our base commit by
2356 * setting a reference to our base commit's ID.
2358 static const struct got_error *
2359 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2361 const struct got_error *err = NULL;
2362 struct got_reference *ref = NULL;
2363 char *refname;
2365 err = got_worktree_get_base_ref_name(&refname, worktree);
2366 if (err)
2367 return err;
2369 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2370 if (err)
2371 goto done;
2373 err = got_ref_write(ref, repo);
2374 done:
2375 free(refname);
2376 if (ref)
2377 got_ref_close(ref);
2378 return err;
2381 static const struct got_error *
2382 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2384 const struct got_error *err = NULL;
2386 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2387 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2388 err = got_error_from_errno("asprintf");
2389 *fileindex_path = NULL;
2391 return err;
2395 static const struct got_error *
2396 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2397 struct got_worktree *worktree)
2399 const struct got_error *err = NULL;
2400 FILE *index = NULL;
2402 *fileindex_path = NULL;
2403 *fileindex = got_fileindex_alloc();
2404 if (*fileindex == NULL)
2405 return got_error_from_errno("got_fileindex_alloc");
2407 err = get_fileindex_path(fileindex_path, worktree);
2408 if (err)
2409 goto done;
2411 index = fopen(*fileindex_path, "rb");
2412 if (index == NULL) {
2413 if (errno != ENOENT)
2414 err = got_error_from_errno2("fopen", *fileindex_path);
2415 } else {
2416 err = got_fileindex_read(*fileindex, index);
2417 if (fclose(index) == EOF && err == NULL)
2418 err = got_error_from_errno("fclose");
2420 done:
2421 if (err) {
2422 free(*fileindex_path);
2423 *fileindex_path = NULL;
2424 got_fileindex_free(*fileindex);
2425 *fileindex = NULL;
2427 return err;
2430 struct bump_base_commit_id_arg {
2431 struct got_object_id *base_commit_id;
2432 const char *path;
2433 size_t path_len;
2434 const char *entry_name;
2435 got_worktree_checkout_cb progress_cb;
2436 void *progress_arg;
2439 /* Bump base commit ID of all files within an updated part of the work tree. */
2440 static const struct got_error *
2441 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2443 const struct got_error *err;
2444 struct bump_base_commit_id_arg *a = arg;
2446 if (a->entry_name) {
2447 if (strcmp(ie->path, a->path) != 0)
2448 return NULL;
2449 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2450 return NULL;
2452 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2453 SHA1_DIGEST_LENGTH) == 0)
2454 return NULL;
2456 if (a->progress_cb) {
2457 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2458 ie->path);
2459 if (err)
2460 return err;
2462 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2463 return NULL;
2466 static const struct got_error *
2467 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2468 struct got_fileindex *fileindex,
2469 got_worktree_checkout_cb progress_cb, void *progress_arg)
2471 struct bump_base_commit_id_arg bbc_arg;
2473 bbc_arg.base_commit_id = worktree->base_commit_id;
2474 bbc_arg.entry_name = NULL;
2475 bbc_arg.path = "";
2476 bbc_arg.path_len = 0;
2477 bbc_arg.progress_cb = progress_cb;
2478 bbc_arg.progress_arg = progress_arg;
2480 return got_fileindex_for_each_entry_safe(fileindex,
2481 bump_base_commit_id, &bbc_arg);
2484 static const struct got_error *
2485 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2487 const struct got_error *err = NULL;
2488 char *new_fileindex_path = NULL;
2489 FILE *new_index = NULL;
2490 struct timespec timeout;
2492 err = got_opentemp_named(&new_fileindex_path, &new_index,
2493 fileindex_path);
2494 if (err)
2495 goto done;
2497 err = got_fileindex_write(fileindex, new_index);
2498 if (err)
2499 goto done;
2501 if (rename(new_fileindex_path, fileindex_path) != 0) {
2502 err = got_error_from_errno3("rename", new_fileindex_path,
2503 fileindex_path);
2504 unlink(new_fileindex_path);
2508 * Sleep for a short amount of time to ensure that files modified after
2509 * this program exits have a different time stamp from the one which
2510 * was recorded in the file index.
2512 timeout.tv_sec = 0;
2513 timeout.tv_nsec = 1;
2514 nanosleep(&timeout, NULL);
2515 done:
2516 if (new_index)
2517 fclose(new_index);
2518 free(new_fileindex_path);
2519 return err;
2522 static const struct got_error *
2523 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2524 struct got_object_id **tree_id, const char *wt_relpath,
2525 struct got_worktree *worktree, struct got_repository *repo)
2527 const struct got_error *err = NULL;
2528 struct got_object_id *id = NULL;
2529 char *in_repo_path = NULL;
2530 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2532 *entry_type = GOT_OBJ_TYPE_ANY;
2533 *tree_relpath = NULL;
2534 *tree_id = NULL;
2536 if (wt_relpath[0] == '\0') {
2537 /* Check out all files within the work tree. */
2538 *entry_type = GOT_OBJ_TYPE_TREE;
2539 *tree_relpath = strdup("");
2540 if (*tree_relpath == NULL) {
2541 err = got_error_from_errno("strdup");
2542 goto done;
2544 err = got_object_id_by_path(tree_id, repo,
2545 worktree->base_commit_id, worktree->path_prefix);
2546 if (err)
2547 goto done;
2548 return NULL;
2551 /* Check out a subset of files in the work tree. */
2553 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2554 is_root_wt ? "" : "/", wt_relpath) == -1) {
2555 err = got_error_from_errno("asprintf");
2556 goto done;
2559 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2560 in_repo_path);
2561 if (err)
2562 goto done;
2564 free(in_repo_path);
2565 in_repo_path = NULL;
2567 err = got_object_get_type(entry_type, repo, id);
2568 if (err)
2569 goto done;
2571 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2572 /* Check out a single file. */
2573 if (strchr(wt_relpath, '/') == NULL) {
2574 /* Check out a single file in work tree's root dir. */
2575 in_repo_path = strdup(worktree->path_prefix);
2576 if (in_repo_path == NULL) {
2577 err = got_error_from_errno("strdup");
2578 goto done;
2580 *tree_relpath = strdup("");
2581 if (*tree_relpath == NULL) {
2582 err = got_error_from_errno("strdup");
2583 goto done;
2585 } else {
2586 /* Check out a single file in a subdirectory. */
2587 err = got_path_dirname(tree_relpath, wt_relpath);
2588 if (err)
2589 return err;
2590 if (asprintf(&in_repo_path, "%s%s%s",
2591 worktree->path_prefix, is_root_wt ? "" : "/",
2592 *tree_relpath) == -1) {
2593 err = got_error_from_errno("asprintf");
2594 goto done;
2597 err = got_object_id_by_path(tree_id, repo,
2598 worktree->base_commit_id, in_repo_path);
2599 } else {
2600 /* Check out all files within a subdirectory. */
2601 *tree_id = got_object_id_dup(id);
2602 if (*tree_id == NULL) {
2603 err = got_error_from_errno("got_object_id_dup");
2604 goto done;
2606 *tree_relpath = strdup(wt_relpath);
2607 if (*tree_relpath == NULL) {
2608 err = got_error_from_errno("strdup");
2609 goto done;
2612 done:
2613 free(id);
2614 free(in_repo_path);
2615 if (err) {
2616 *entry_type = GOT_OBJ_TYPE_ANY;
2617 free(*tree_relpath);
2618 *tree_relpath = NULL;
2619 free(*tree_id);
2620 *tree_id = NULL;
2622 return err;
2625 static const struct got_error *
2626 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2627 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2628 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2629 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2631 const struct got_error *err = NULL;
2632 struct got_commit_object *commit = NULL;
2633 struct got_tree_object *tree = NULL;
2634 struct got_fileindex_diff_tree_cb diff_cb;
2635 struct diff_cb_arg arg;
2637 err = ref_base_commit(worktree, repo);
2638 if (err) {
2639 if (!(err->code == GOT_ERR_ERRNO &&
2640 (errno == EACCES || errno == EROFS)))
2641 goto done;
2642 err = (*progress_cb)(progress_arg,
2643 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2644 if (err)
2645 return err;
2648 err = got_object_open_as_commit(&commit, repo,
2649 worktree->base_commit_id);
2650 if (err)
2651 goto done;
2653 err = got_object_open_as_tree(&tree, repo, tree_id);
2654 if (err)
2655 goto done;
2657 if (entry_name &&
2658 got_object_tree_find_entry(tree, entry_name) == NULL) {
2659 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2660 goto done;
2663 diff_cb.diff_old_new = diff_old_new;
2664 diff_cb.diff_old = diff_old;
2665 diff_cb.diff_new = diff_new;
2666 arg.fileindex = fileindex;
2667 arg.worktree = worktree;
2668 arg.repo = repo;
2669 arg.progress_cb = progress_cb;
2670 arg.progress_arg = progress_arg;
2671 arg.cancel_cb = cancel_cb;
2672 arg.cancel_arg = cancel_arg;
2673 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2674 entry_name, repo, &diff_cb, &arg);
2675 done:
2676 if (tree)
2677 got_object_tree_close(tree);
2678 if (commit)
2679 got_object_commit_close(commit);
2680 return err;
2683 const struct got_error *
2684 got_worktree_checkout_files(struct got_worktree *worktree,
2685 struct got_pathlist_head *paths, struct got_repository *repo,
2686 got_worktree_checkout_cb progress_cb, void *progress_arg,
2687 got_cancel_cb cancel_cb, void *cancel_arg)
2689 const struct got_error *err = NULL, *sync_err, *unlockerr;
2690 struct got_commit_object *commit = NULL;
2691 struct got_tree_object *tree = NULL;
2692 struct got_fileindex *fileindex = NULL;
2693 char *fileindex_path = NULL;
2694 struct got_pathlist_entry *pe;
2695 struct tree_path_data {
2696 SIMPLEQ_ENTRY(tree_path_data) entry;
2697 struct got_object_id *tree_id;
2698 int entry_type;
2699 char *relpath;
2700 char *entry_name;
2701 } *tpd = NULL;
2702 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2704 SIMPLEQ_INIT(&tree_paths);
2706 err = lock_worktree(worktree, LOCK_EX);
2707 if (err)
2708 return err;
2710 /* Map all specified paths to in-repository trees. */
2711 TAILQ_FOREACH(pe, paths, entry) {
2712 tpd = malloc(sizeof(*tpd));
2713 if (tpd == NULL) {
2714 err = got_error_from_errno("malloc");
2715 goto done;
2718 err = find_tree_entry_for_checkout(&tpd->entry_type,
2719 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2720 if (err) {
2721 free(tpd);
2722 goto done;
2725 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2726 err = got_path_basename(&tpd->entry_name, pe->path);
2727 if (err) {
2728 free(tpd->relpath);
2729 free(tpd->tree_id);
2730 free(tpd);
2731 goto done;
2733 } else
2734 tpd->entry_name = NULL;
2736 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2740 * Read the file index.
2741 * Checking out files is supposed to be an idempotent operation.
2742 * If the on-disk file index is incomplete we will try to complete it.
2744 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2745 if (err)
2746 goto done;
2748 tpd = SIMPLEQ_FIRST(&tree_paths);
2749 TAILQ_FOREACH(pe, paths, entry) {
2750 struct bump_base_commit_id_arg bbc_arg;
2752 err = checkout_files(worktree, fileindex, tpd->relpath,
2753 tpd->tree_id, tpd->entry_name, repo,
2754 progress_cb, progress_arg, cancel_cb, cancel_arg);
2755 if (err)
2756 break;
2758 bbc_arg.base_commit_id = worktree->base_commit_id;
2759 bbc_arg.entry_name = tpd->entry_name;
2760 bbc_arg.path = pe->path;
2761 bbc_arg.path_len = pe->path_len;
2762 bbc_arg.progress_cb = progress_cb;
2763 bbc_arg.progress_arg = progress_arg;
2764 err = got_fileindex_for_each_entry_safe(fileindex,
2765 bump_base_commit_id, &bbc_arg);
2766 if (err)
2767 break;
2769 tpd = SIMPLEQ_NEXT(tpd, entry);
2771 sync_err = sync_fileindex(fileindex, fileindex_path);
2772 if (sync_err && err == NULL)
2773 err = sync_err;
2774 done:
2775 free(fileindex_path);
2776 if (tree)
2777 got_object_tree_close(tree);
2778 if (commit)
2779 got_object_commit_close(commit);
2780 if (fileindex)
2781 got_fileindex_free(fileindex);
2782 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2783 tpd = SIMPLEQ_FIRST(&tree_paths);
2784 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2785 free(tpd->relpath);
2786 free(tpd->tree_id);
2787 free(tpd);
2789 unlockerr = lock_worktree(worktree, LOCK_SH);
2790 if (unlockerr && err == NULL)
2791 err = unlockerr;
2792 return err;
2795 struct merge_file_cb_arg {
2796 struct got_worktree *worktree;
2797 struct got_fileindex *fileindex;
2798 got_worktree_checkout_cb progress_cb;
2799 void *progress_arg;
2800 got_cancel_cb cancel_cb;
2801 void *cancel_arg;
2802 const char *label_orig;
2803 struct got_object_id *commit_id2;
2806 static const struct got_error *
2807 merge_file_cb(void *arg, struct got_blob_object *blob1,
2808 struct got_blob_object *blob2, struct got_object_id *id1,
2809 struct got_object_id *id2, const char *path1, const char *path2,
2810 mode_t mode1, mode_t mode2, struct got_repository *repo)
2812 static const struct got_error *err = NULL;
2813 struct merge_file_cb_arg *a = arg;
2814 struct got_fileindex_entry *ie;
2815 char *ondisk_path = NULL;
2816 struct stat sb;
2817 unsigned char status;
2818 int local_changes_subsumed;
2820 if (blob1 && blob2) {
2821 ie = got_fileindex_entry_get(a->fileindex, path2,
2822 strlen(path2));
2823 if (ie == NULL)
2824 return (*a->progress_cb)(a->progress_arg,
2825 GOT_STATUS_MISSING, path2);
2827 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2828 path2) == -1)
2829 return got_error_from_errno("asprintf");
2831 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2832 repo);
2833 if (err)
2834 goto done;
2836 if (status == GOT_STATUS_DELETE) {
2837 err = (*a->progress_cb)(a->progress_arg,
2838 GOT_STATUS_MERGE, path2);
2839 goto done;
2841 if (status != GOT_STATUS_NO_CHANGE &&
2842 status != GOT_STATUS_MODIFY &&
2843 status != GOT_STATUS_CONFLICT &&
2844 status != GOT_STATUS_ADD) {
2845 err = (*a->progress_cb)(a->progress_arg, status, path2);
2846 goto done;
2849 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2850 char *link_target2;
2851 err = got_object_blob_read_to_str(&link_target2, blob2);
2852 if (err)
2853 goto done;
2854 err = merge_symlink(a->worktree, blob1, ondisk_path,
2855 path2, a->label_orig, link_target2, a->commit_id2,
2856 repo, a->progress_cb, a->progress_arg);
2857 free(link_target2);
2858 } else {
2859 err = merge_blob(&local_changes_subsumed, a->worktree,
2860 blob1, ondisk_path, path2, sb.st_mode,
2861 a->label_orig, blob2, a->commit_id2, repo,
2862 a->progress_cb, a->progress_arg);
2864 } else if (blob1) {
2865 ie = got_fileindex_entry_get(a->fileindex, path1,
2866 strlen(path1));
2867 if (ie == NULL)
2868 return (*a->progress_cb)(a->progress_arg,
2869 GOT_STATUS_MISSING, path1);
2871 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2872 path1) == -1)
2873 return got_error_from_errno("asprintf");
2875 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2876 repo);
2877 if (err)
2878 goto done;
2880 switch (status) {
2881 case GOT_STATUS_NO_CHANGE:
2882 err = (*a->progress_cb)(a->progress_arg,
2883 GOT_STATUS_DELETE, path1);
2884 if (err)
2885 goto done;
2886 err = remove_ondisk_file(a->worktree->root_path, path1);
2887 if (err)
2888 goto done;
2889 if (ie)
2890 got_fileindex_entry_mark_deleted_from_disk(ie);
2891 break;
2892 case GOT_STATUS_DELETE:
2893 case GOT_STATUS_MISSING:
2894 err = (*a->progress_cb)(a->progress_arg,
2895 GOT_STATUS_DELETE, path1);
2896 if (err)
2897 goto done;
2898 if (ie)
2899 got_fileindex_entry_mark_deleted_from_disk(ie);
2900 break;
2901 case GOT_STATUS_ADD: {
2902 struct got_object_id *id;
2903 FILE *blob1_f;
2905 * Delete the added file only if its content already
2906 * exists in the repository.
2908 err = got_object_blob_file_create(&id, &blob1_f, path1);
2909 if (err)
2910 goto done;
2911 if (got_object_id_cmp(id, id1) == 0) {
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_DELETE, path1);
2914 if (err)
2915 goto done;
2916 err = remove_ondisk_file(a->worktree->root_path,
2917 path1);
2918 if (err)
2919 goto done;
2920 if (ie)
2921 got_fileindex_entry_remove(a->fileindex,
2922 ie);
2923 } else {
2924 err = (*a->progress_cb)(a->progress_arg,
2925 GOT_STATUS_CANNOT_DELETE, path1);
2927 if (fclose(blob1_f) == EOF && err == NULL)
2928 err = got_error_from_errno("fclose");
2929 free(id);
2930 if (err)
2931 goto done;
2932 break;
2934 case GOT_STATUS_MODIFY:
2935 case GOT_STATUS_CONFLICT:
2936 err = (*a->progress_cb)(a->progress_arg,
2937 GOT_STATUS_CANNOT_DELETE, path1);
2938 if (err)
2939 goto done;
2940 break;
2941 case GOT_STATUS_OBSTRUCTED:
2942 err = (*a->progress_cb)(a->progress_arg, status, path1);
2943 if (err)
2944 goto done;
2945 break;
2946 default:
2947 break;
2949 } else if (blob2) {
2950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2951 path2) == -1)
2952 return got_error_from_errno("asprintf");
2953 ie = got_fileindex_entry_get(a->fileindex, path2,
2954 strlen(path2));
2955 if (ie) {
2956 err = get_file_status(&status, &sb, ie, ondisk_path,
2957 -1, NULL, repo);
2958 if (err)
2959 goto done;
2960 if (status != GOT_STATUS_NO_CHANGE &&
2961 status != GOT_STATUS_MODIFY &&
2962 status != GOT_STATUS_CONFLICT &&
2963 status != GOT_STATUS_ADD) {
2964 err = (*a->progress_cb)(a->progress_arg,
2965 status, path2);
2966 goto done;
2968 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2969 char *link_target2;
2970 err = got_object_blob_read_to_str(&link_target2,
2971 blob2);
2972 if (err)
2973 goto done;
2974 err = merge_symlink(a->worktree, NULL,
2975 ondisk_path, path2, a->label_orig,
2976 link_target2, a->commit_id2, repo,
2977 a->progress_cb, a->progress_arg);
2978 free(link_target2);
2979 } else if (S_ISREG(sb.st_mode)) {
2980 err = merge_blob(&local_changes_subsumed,
2981 a->worktree, NULL, ondisk_path, path2,
2982 sb.st_mode, a->label_orig, blob2,
2983 a->commit_id2, repo, a->progress_cb,
2984 a->progress_arg);
2985 } else {
2986 err = got_error_path(ondisk_path,
2987 GOT_ERR_FILE_OBSTRUCTED);
2989 if (err)
2990 goto done;
2991 if (status == GOT_STATUS_DELETE) {
2992 err = got_fileindex_entry_update(ie,
2993 a->worktree->root_fd, path2, blob2->id.sha1,
2994 a->worktree->base_commit_id->sha1, 0);
2995 if (err)
2996 goto done;
2998 } else {
2999 int is_bad_symlink = 0;
3000 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3001 if (S_ISLNK(mode2)) {
3002 err = install_symlink(&is_bad_symlink,
3003 a->worktree, ondisk_path, path2, blob2, 0,
3004 0, 1, repo, a->progress_cb, a->progress_arg);
3005 } else {
3006 err = install_blob(a->worktree, ondisk_path, path2,
3007 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3008 a->progress_cb, a->progress_arg);
3010 if (err)
3011 goto done;
3012 err = got_fileindex_entry_alloc(&ie, path2);
3013 if (err)
3014 goto done;
3015 err = got_fileindex_entry_update(ie,
3016 a->worktree->root_fd, path2, NULL, NULL, 1);
3017 if (err) {
3018 got_fileindex_entry_free(ie);
3019 goto done;
3021 err = got_fileindex_entry_add(a->fileindex, ie);
3022 if (err) {
3023 got_fileindex_entry_free(ie);
3024 goto done;
3026 if (is_bad_symlink) {
3027 got_fileindex_entry_filetype_set(ie,
3028 GOT_FILEIDX_MODE_BAD_SYMLINK);
3032 done:
3033 free(ondisk_path);
3034 return err;
3037 struct check_merge_ok_arg {
3038 struct got_worktree *worktree;
3039 struct got_repository *repo;
3042 static const struct got_error *
3043 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3045 const struct got_error *err = NULL;
3046 struct check_merge_ok_arg *a = arg;
3047 unsigned char status;
3048 struct stat sb;
3049 char *ondisk_path;
3051 /* Reject merges into a work tree with mixed base commits. */
3052 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3053 SHA1_DIGEST_LENGTH))
3054 return got_error(GOT_ERR_MIXED_COMMITS);
3056 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3057 == -1)
3058 return got_error_from_errno("asprintf");
3060 /* Reject merges into a work tree with conflicted files. */
3061 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3062 if (err)
3063 return err;
3064 if (status == GOT_STATUS_CONFLICT)
3065 return got_error(GOT_ERR_CONFLICTS);
3067 return NULL;
3070 static const struct got_error *
3071 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3072 const char *fileindex_path, struct got_object_id *commit_id1,
3073 struct got_object_id *commit_id2, struct got_repository *repo,
3074 got_worktree_checkout_cb progress_cb, void *progress_arg,
3075 got_cancel_cb cancel_cb, void *cancel_arg)
3077 const struct got_error *err = NULL, *sync_err;
3078 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3079 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3080 struct merge_file_cb_arg arg;
3081 char *label_orig = NULL;
3083 if (commit_id1) {
3084 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3085 worktree->path_prefix);
3086 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3087 goto done;
3089 if (tree_id1) {
3090 char *id_str;
3092 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3093 if (err)
3094 goto done;
3096 err = got_object_id_str(&id_str, commit_id1);
3097 if (err)
3098 goto done;
3100 if (asprintf(&label_orig, "%s: commit %s",
3101 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3102 err = got_error_from_errno("asprintf");
3103 free(id_str);
3104 goto done;
3106 free(id_str);
3109 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3110 worktree->path_prefix);
3111 if (err)
3112 goto done;
3114 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3115 if (err)
3116 goto done;
3118 arg.worktree = worktree;
3119 arg.fileindex = fileindex;
3120 arg.progress_cb = progress_cb;
3121 arg.progress_arg = progress_arg;
3122 arg.cancel_cb = cancel_cb;
3123 arg.cancel_arg = cancel_arg;
3124 arg.label_orig = label_orig;
3125 arg.commit_id2 = commit_id2;
3126 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3127 sync_err = sync_fileindex(fileindex, fileindex_path);
3128 if (sync_err && err == NULL)
3129 err = sync_err;
3130 done:
3131 if (tree1)
3132 got_object_tree_close(tree1);
3133 if (tree2)
3134 got_object_tree_close(tree2);
3135 free(label_orig);
3136 return err;
3139 const struct got_error *
3140 got_worktree_merge_files(struct got_worktree *worktree,
3141 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3142 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3143 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3145 const struct got_error *err, *unlockerr;
3146 char *fileindex_path = NULL;
3147 struct got_fileindex *fileindex = NULL;
3148 struct check_merge_ok_arg mok_arg;
3150 err = lock_worktree(worktree, LOCK_EX);
3151 if (err)
3152 return err;
3154 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3155 if (err)
3156 goto done;
3158 mok_arg.worktree = worktree;
3159 mok_arg.repo = repo;
3160 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3161 &mok_arg);
3162 if (err)
3163 goto done;
3165 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3166 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3167 done:
3168 if (fileindex)
3169 got_fileindex_free(fileindex);
3170 free(fileindex_path);
3171 unlockerr = lock_worktree(worktree, LOCK_SH);
3172 if (unlockerr && err == NULL)
3173 err = unlockerr;
3174 return err;
3177 struct diff_dir_cb_arg {
3178 struct got_fileindex *fileindex;
3179 struct got_worktree *worktree;
3180 const char *status_path;
3181 size_t status_path_len;
3182 struct got_repository *repo;
3183 got_worktree_status_cb status_cb;
3184 void *status_arg;
3185 got_cancel_cb cancel_cb;
3186 void *cancel_arg;
3187 /* A pathlist containing per-directory pathlists of ignore patterns. */
3188 struct got_pathlist_head ignores;
3189 int report_unchanged;
3190 int no_ignores;
3193 static const struct got_error *
3194 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3195 int dirfd, const char *de_name,
3196 got_worktree_status_cb status_cb, void *status_arg,
3197 struct got_repository *repo, int report_unchanged)
3199 const struct got_error *err = NULL;
3200 unsigned char status = GOT_STATUS_NO_CHANGE;
3201 unsigned char staged_status = get_staged_status(ie);
3202 struct stat sb;
3203 struct got_object_id blob_id, commit_id, staged_blob_id;
3204 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3205 struct got_object_id *staged_blob_idp = NULL;
3207 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3208 if (err)
3209 return err;
3211 if (status == GOT_STATUS_NO_CHANGE &&
3212 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3213 return NULL;
3215 if (got_fileindex_entry_has_blob(ie)) {
3216 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3217 blob_idp = &blob_id;
3219 if (got_fileindex_entry_has_commit(ie)) {
3220 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3221 commit_idp = &commit_id;
3223 if (staged_status == GOT_STATUS_ADD ||
3224 staged_status == GOT_STATUS_MODIFY) {
3225 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3226 SHA1_DIGEST_LENGTH);
3227 staged_blob_idp = &staged_blob_id;
3230 return (*status_cb)(status_arg, status, staged_status,
3231 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3234 static const struct got_error *
3235 status_old_new(void *arg, struct got_fileindex_entry *ie,
3236 struct dirent *de, const char *parent_path, int dirfd)
3238 const struct got_error *err = NULL;
3239 struct diff_dir_cb_arg *a = arg;
3240 char *abspath;
3242 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3243 return got_error(GOT_ERR_CANCELLED);
3245 if (got_path_cmp(parent_path, a->status_path,
3246 strlen(parent_path), a->status_path_len) != 0 &&
3247 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3248 return NULL;
3250 if (parent_path[0]) {
3251 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3252 parent_path, de->d_name) == -1)
3253 return got_error_from_errno("asprintf");
3254 } else {
3255 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3256 de->d_name) == -1)
3257 return got_error_from_errno("asprintf");
3260 err = report_file_status(ie, abspath, dirfd, de->d_name,
3261 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3262 free(abspath);
3263 return err;
3266 static const struct got_error *
3267 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3269 struct diff_dir_cb_arg *a = arg;
3270 struct got_object_id blob_id, commit_id;
3271 unsigned char status;
3273 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3274 return got_error(GOT_ERR_CANCELLED);
3276 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3277 return NULL;
3279 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3280 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3281 if (got_fileindex_entry_has_file_on_disk(ie))
3282 status = GOT_STATUS_MISSING;
3283 else
3284 status = GOT_STATUS_DELETE;
3285 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3286 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3289 void
3290 free_ignorelist(struct got_pathlist_head *ignorelist)
3292 struct got_pathlist_entry *pe;
3294 TAILQ_FOREACH(pe, ignorelist, entry)
3295 free((char *)pe->path);
3296 got_pathlist_free(ignorelist);
3299 void
3300 free_ignores(struct got_pathlist_head *ignores)
3302 struct got_pathlist_entry *pe;
3304 TAILQ_FOREACH(pe, ignores, entry) {
3305 struct got_pathlist_head *ignorelist = pe->data;
3306 free_ignorelist(ignorelist);
3307 free((char *)pe->path);
3309 got_pathlist_free(ignores);
3312 static const struct got_error *
3313 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3315 const struct got_error *err = NULL;
3316 struct got_pathlist_entry *pe = NULL;
3317 struct got_pathlist_head *ignorelist;
3318 char *line = NULL, *pattern, *dirpath = NULL;
3319 size_t linesize = 0;
3320 ssize_t linelen;
3322 ignorelist = calloc(1, sizeof(*ignorelist));
3323 if (ignorelist == NULL)
3324 return got_error_from_errno("calloc");
3325 TAILQ_INIT(ignorelist);
3327 while ((linelen = getline(&line, &linesize, f)) != -1) {
3328 if (linelen > 0 && line[linelen - 1] == '\n')
3329 line[linelen - 1] = '\0';
3331 /* Git's ignores may contain comments. */
3332 if (line[0] == '#')
3333 continue;
3335 /* Git's negated patterns are not (yet?) supported. */
3336 if (line[0] == '!')
3337 continue;
3339 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3340 line) == -1) {
3341 err = got_error_from_errno("asprintf");
3342 goto done;
3344 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3345 if (err)
3346 goto done;
3348 if (ferror(f)) {
3349 err = got_error_from_errno("getline");
3350 goto done;
3353 dirpath = strdup(path);
3354 if (dirpath == NULL) {
3355 err = got_error_from_errno("strdup");
3356 goto done;
3358 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3359 done:
3360 free(line);
3361 if (err || pe == NULL) {
3362 free(dirpath);
3363 free_ignorelist(ignorelist);
3365 return err;
3368 int
3369 match_ignores(struct got_pathlist_head *ignores, const char *path)
3371 struct got_pathlist_entry *pe;
3373 /* Handle patterns which match in all directories. */
3374 TAILQ_FOREACH(pe, ignores, entry) {
3375 struct got_pathlist_head *ignorelist = pe->data;
3376 struct got_pathlist_entry *pi;
3378 TAILQ_FOREACH(pi, ignorelist, entry) {
3379 const char *p, *pattern = pi->path;
3381 if (strncmp(pattern, "**/", 3) != 0)
3382 continue;
3383 pattern += 3;
3384 p = path;
3385 while (*p) {
3386 if (fnmatch(pattern, p,
3387 FNM_PATHNAME | FNM_LEADING_DIR)) {
3388 /* Retry in next directory. */
3389 while (*p && *p != '/')
3390 p++;
3391 while (*p == '/')
3392 p++;
3393 continue;
3395 return 1;
3401 * The ignores pathlist contains ignore lists from children before
3402 * parents, so we can find the most specific ignorelist by walking
3403 * ignores backwards.
3405 pe = TAILQ_LAST(ignores, got_pathlist_head);
3406 while (pe) {
3407 if (got_path_is_child(path, pe->path, pe->path_len)) {
3408 struct got_pathlist_head *ignorelist = pe->data;
3409 struct got_pathlist_entry *pi;
3410 TAILQ_FOREACH(pi, ignorelist, entry) {
3411 const char *pattern = pi->path;
3412 int flags = FNM_LEADING_DIR;
3413 if (strstr(pattern, "/**/") == NULL)
3414 flags |= FNM_PATHNAME;
3415 if (fnmatch(pattern, path, flags))
3416 continue;
3417 return 1;
3420 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3423 return 0;
3426 static const struct got_error *
3427 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3428 const char *path, int dirfd, const char *ignores_filename)
3430 const struct got_error *err = NULL;
3431 char *ignorespath;
3432 int fd = -1;
3433 FILE *ignoresfile = NULL;
3435 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3436 path[0] ? "/" : "", ignores_filename) == -1)
3437 return got_error_from_errno("asprintf");
3439 if (dirfd != -1) {
3440 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3441 if (fd == -1) {
3442 if (errno != ENOENT && errno != EACCES)
3443 err = got_error_from_errno2("openat",
3444 ignorespath);
3445 } else {
3446 ignoresfile = fdopen(fd, "r");
3447 if (ignoresfile == NULL)
3448 err = got_error_from_errno2("fdopen",
3449 ignorespath);
3450 else {
3451 fd = -1;
3452 err = read_ignores(ignores, path, ignoresfile);
3455 } else {
3456 ignoresfile = fopen(ignorespath, "r");
3457 if (ignoresfile == NULL) {
3458 if (errno != ENOENT && errno != EACCES)
3459 err = got_error_from_errno2("fopen",
3460 ignorespath);
3461 } else
3462 err = read_ignores(ignores, path, ignoresfile);
3465 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3466 err = got_error_from_errno2("fclose", path);
3467 if (fd != -1 && close(fd) == -1 && err == NULL)
3468 err = got_error_from_errno2("close", path);
3469 free(ignorespath);
3470 return err;
3473 static const struct got_error *
3474 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3476 const struct got_error *err = NULL;
3477 struct diff_dir_cb_arg *a = arg;
3478 char *path = NULL;
3480 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3481 return got_error(GOT_ERR_CANCELLED);
3483 if (parent_path[0]) {
3484 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3485 return got_error_from_errno("asprintf");
3486 } else {
3487 path = de->d_name;
3490 if (de->d_type != DT_DIR &&
3491 got_path_is_child(path, a->status_path, a->status_path_len)
3492 && !match_ignores(&a->ignores, path))
3493 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3494 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3495 if (parent_path[0])
3496 free(path);
3497 return err;
3500 static const struct got_error *
3501 status_traverse(void *arg, const char *path, int dirfd)
3503 const struct got_error *err = NULL;
3504 struct diff_dir_cb_arg *a = arg;
3506 if (a->no_ignores)
3507 return NULL;
3509 err = add_ignores(&a->ignores, a->worktree->root_path,
3510 path, dirfd, ".cvsignore");
3511 if (err)
3512 return err;
3514 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3515 dirfd, ".gitignore");
3517 return err;
3520 static const struct got_error *
3521 report_single_file_status(const char *path, const char *ondisk_path,
3522 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3523 void *status_arg, struct got_repository *repo, int report_unchanged)
3525 struct got_fileindex_entry *ie;
3526 struct stat sb;
3528 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3529 if (ie)
3530 return report_file_status(ie, ondisk_path, -1, NULL,
3531 status_cb, status_arg, repo, report_unchanged);
3533 if (lstat(ondisk_path, &sb) == -1) {
3534 if (errno != ENOENT)
3535 return got_error_from_errno2("lstat", ondisk_path);
3536 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3537 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3538 return NULL;
3541 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3542 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3543 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3545 return NULL;
3548 static const struct got_error *
3549 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3550 const char *root_path, const char *path)
3552 const struct got_error *err;
3553 char *parent_path, *next_parent_path = NULL;
3555 err = add_ignores(ignores, root_path, "", -1,
3556 ".cvsignore");
3557 if (err)
3558 return err;
3560 err = add_ignores(ignores, root_path, "", -1,
3561 ".gitignore");
3562 if (err)
3563 return err;
3565 err = got_path_dirname(&parent_path, path);
3566 if (err) {
3567 if (err->code == GOT_ERR_BAD_PATH)
3568 return NULL; /* cannot traverse parent */
3569 return err;
3571 for (;;) {
3572 err = add_ignores(ignores, root_path, parent_path, -1,
3573 ".cvsignore");
3574 if (err)
3575 break;
3576 err = add_ignores(ignores, root_path, parent_path, -1,
3577 ".gitignore");
3578 if (err)
3579 break;
3580 err = got_path_dirname(&next_parent_path, parent_path);
3581 if (err) {
3582 if (err->code == GOT_ERR_BAD_PATH)
3583 err = NULL; /* traversed everything */
3584 break;
3586 free(parent_path);
3587 parent_path = next_parent_path;
3588 next_parent_path = NULL;
3591 free(parent_path);
3592 free(next_parent_path);
3593 return err;
3596 static const struct got_error *
3597 worktree_status(struct got_worktree *worktree, const char *path,
3598 struct got_fileindex *fileindex, struct got_repository *repo,
3599 got_worktree_status_cb status_cb, void *status_arg,
3600 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3601 int report_unchanged)
3603 const struct got_error *err = NULL;
3604 int fd = -1;
3605 struct got_fileindex_diff_dir_cb fdiff_cb;
3606 struct diff_dir_cb_arg arg;
3607 char *ondisk_path = NULL;
3609 TAILQ_INIT(&arg.ignores);
3611 if (asprintf(&ondisk_path, "%s%s%s",
3612 worktree->root_path, path[0] ? "/" : "", path) == -1)
3613 return got_error_from_errno("asprintf");
3615 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3616 if (fd == -1) {
3617 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3618 errno != ELOOP)
3619 err = got_error_from_errno2("open", ondisk_path);
3620 else
3621 err = report_single_file_status(path, ondisk_path,
3622 fileindex, status_cb, status_arg, repo,
3623 report_unchanged);
3624 } else {
3625 fdiff_cb.diff_old_new = status_old_new;
3626 fdiff_cb.diff_old = status_old;
3627 fdiff_cb.diff_new = status_new;
3628 fdiff_cb.diff_traverse = status_traverse;
3629 arg.fileindex = fileindex;
3630 arg.worktree = worktree;
3631 arg.status_path = path;
3632 arg.status_path_len = strlen(path);
3633 arg.repo = repo;
3634 arg.status_cb = status_cb;
3635 arg.status_arg = status_arg;
3636 arg.cancel_cb = cancel_cb;
3637 arg.cancel_arg = cancel_arg;
3638 arg.report_unchanged = report_unchanged;
3639 arg.no_ignores = no_ignores;
3640 if (!no_ignores) {
3641 err = add_ignores_from_parent_paths(&arg.ignores,
3642 worktree->root_path, path);
3643 if (err)
3644 goto done;
3646 err = got_fileindex_diff_dir(fileindex, fd,
3647 worktree->root_path, path, repo, &fdiff_cb, &arg);
3649 done:
3650 free_ignores(&arg.ignores);
3651 if (fd != -1 && close(fd) == -1 && err == NULL)
3652 err = got_error_from_errno("close");
3653 free(ondisk_path);
3654 return err;
3657 const struct got_error *
3658 got_worktree_status(struct got_worktree *worktree,
3659 struct got_pathlist_head *paths, struct got_repository *repo,
3660 got_worktree_status_cb status_cb, void *status_arg,
3661 got_cancel_cb cancel_cb, void *cancel_arg)
3663 const struct got_error *err = NULL;
3664 char *fileindex_path = NULL;
3665 struct got_fileindex *fileindex = NULL;
3666 struct got_pathlist_entry *pe;
3668 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3669 if (err)
3670 return err;
3672 TAILQ_FOREACH(pe, paths, entry) {
3673 err = worktree_status(worktree, pe->path, fileindex, repo,
3674 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3675 if (err)
3676 break;
3678 free(fileindex_path);
3679 got_fileindex_free(fileindex);
3680 return err;
3683 const struct got_error *
3684 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3685 const char *arg)
3687 const struct got_error *err = NULL;
3688 char *resolved = NULL, *cwd = NULL, *path = NULL;
3689 size_t len;
3690 struct stat sb;
3691 char *abspath = NULL;
3692 char canonpath[PATH_MAX];
3694 *wt_path = NULL;
3696 cwd = getcwd(NULL, 0);
3697 if (cwd == NULL)
3698 return got_error_from_errno("getcwd");
3700 if (lstat(arg, &sb) == -1) {
3701 if (errno != ENOENT) {
3702 err = got_error_from_errno2("lstat", arg);
3703 goto done;
3705 sb.st_mode = 0;
3707 if (S_ISLNK(sb.st_mode)) {
3709 * We cannot use realpath(3) with symlinks since we want to
3710 * operate on the symlink itself.
3711 * But we can make the path absolute, assuming it is relative
3712 * to the current working directory, and then canonicalize it.
3714 if (!got_path_is_absolute(arg)) {
3715 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3716 err = got_error_from_errno("asprintf");
3717 goto done;
3721 err = got_canonpath(abspath ? abspath : arg, canonpath,
3722 sizeof(canonpath));
3723 if (err)
3724 goto done;
3725 resolved = strdup(canonpath);
3726 if (resolved == NULL) {
3727 err = got_error_from_errno("strdup");
3728 goto done;
3730 } else {
3731 resolved = realpath(arg, NULL);
3732 if (resolved == NULL) {
3733 if (errno != ENOENT) {
3734 err = got_error_from_errno2("realpath", arg);
3735 goto done;
3737 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3738 err = got_error_from_errno("asprintf");
3739 goto done;
3741 err = got_canonpath(abspath, canonpath,
3742 sizeof(canonpath));
3743 if (err)
3744 goto done;
3745 resolved = strdup(canonpath);
3746 if (resolved == NULL) {
3747 err = got_error_from_errno("strdup");
3748 goto done;
3753 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3754 strlen(got_worktree_get_root_path(worktree)))) {
3755 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3756 goto done;
3759 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3760 err = got_path_skip_common_ancestor(&path,
3761 got_worktree_get_root_path(worktree), resolved);
3762 if (err)
3763 goto done;
3764 } else {
3765 path = strdup("");
3766 if (path == NULL) {
3767 err = got_error_from_errno("strdup");
3768 goto done;
3772 /* XXX status walk can't deal with trailing slash! */
3773 len = strlen(path);
3774 while (len > 0 && path[len - 1] == '/') {
3775 path[len - 1] = '\0';
3776 len--;
3778 done:
3779 free(abspath);
3780 free(resolved);
3781 free(cwd);
3782 if (err == NULL)
3783 *wt_path = path;
3784 else
3785 free(path);
3786 return err;
3789 struct schedule_addition_args {
3790 struct got_worktree *worktree;
3791 struct got_fileindex *fileindex;
3792 got_worktree_checkout_cb progress_cb;
3793 void *progress_arg;
3794 struct got_repository *repo;
3797 static const struct got_error *
3798 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3799 const char *relpath, struct got_object_id *blob_id,
3800 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3801 int dirfd, const char *de_name)
3803 struct schedule_addition_args *a = arg;
3804 const struct got_error *err = NULL;
3805 struct got_fileindex_entry *ie;
3806 struct stat sb;
3807 char *ondisk_path;
3809 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3810 relpath) == -1)
3811 return got_error_from_errno("asprintf");
3813 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3814 if (ie) {
3815 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3816 de_name, a->repo);
3817 if (err)
3818 goto done;
3819 /* Re-adding an existing entry is a no-op. */
3820 if (status == GOT_STATUS_ADD)
3821 goto done;
3822 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3823 if (err)
3824 goto done;
3827 if (status != GOT_STATUS_UNVERSIONED) {
3828 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3829 goto done;
3832 err = got_fileindex_entry_alloc(&ie, relpath);
3833 if (err)
3834 goto done;
3835 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3836 relpath, NULL, NULL, 1);
3837 if (err) {
3838 got_fileindex_entry_free(ie);
3839 goto done;
3841 err = got_fileindex_entry_add(a->fileindex, ie);
3842 if (err) {
3843 got_fileindex_entry_free(ie);
3844 goto done;
3846 done:
3847 free(ondisk_path);
3848 if (err)
3849 return err;
3850 if (status == GOT_STATUS_ADD)
3851 return NULL;
3852 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3855 const struct got_error *
3856 got_worktree_schedule_add(struct got_worktree *worktree,
3857 struct got_pathlist_head *paths,
3858 got_worktree_checkout_cb progress_cb, void *progress_arg,
3859 struct got_repository *repo, int no_ignores)
3861 struct got_fileindex *fileindex = NULL;
3862 char *fileindex_path = NULL;
3863 const struct got_error *err = NULL, *sync_err, *unlockerr;
3864 struct got_pathlist_entry *pe;
3865 struct schedule_addition_args saa;
3867 err = lock_worktree(worktree, LOCK_EX);
3868 if (err)
3869 return err;
3871 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3872 if (err)
3873 goto done;
3875 saa.worktree = worktree;
3876 saa.fileindex = fileindex;
3877 saa.progress_cb = progress_cb;
3878 saa.progress_arg = progress_arg;
3879 saa.repo = repo;
3881 TAILQ_FOREACH(pe, paths, entry) {
3882 err = worktree_status(worktree, pe->path, fileindex, repo,
3883 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3884 if (err)
3885 break;
3887 sync_err = sync_fileindex(fileindex, fileindex_path);
3888 if (sync_err && err == NULL)
3889 err = sync_err;
3890 done:
3891 free(fileindex_path);
3892 if (fileindex)
3893 got_fileindex_free(fileindex);
3894 unlockerr = lock_worktree(worktree, LOCK_SH);
3895 if (unlockerr && err == NULL)
3896 err = unlockerr;
3897 return err;
3900 struct schedule_deletion_args {
3901 struct got_worktree *worktree;
3902 struct got_fileindex *fileindex;
3903 got_worktree_delete_cb progress_cb;
3904 void *progress_arg;
3905 struct got_repository *repo;
3906 int delete_local_mods;
3907 int keep_on_disk;
3908 const char *status_codes;
3911 static const struct got_error *
3912 schedule_for_deletion(void *arg, unsigned char status,
3913 unsigned char staged_status, const char *relpath,
3914 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3915 struct got_object_id *commit_id, int dirfd, const char *de_name)
3917 struct schedule_deletion_args *a = arg;
3918 const struct got_error *err = NULL;
3919 struct got_fileindex_entry *ie = NULL;
3920 struct stat sb;
3921 char *ondisk_path;
3923 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3924 if (ie == NULL)
3925 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3927 staged_status = get_staged_status(ie);
3928 if (staged_status != GOT_STATUS_NO_CHANGE) {
3929 if (staged_status == GOT_STATUS_DELETE)
3930 return NULL;
3931 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3934 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3935 relpath) == -1)
3936 return got_error_from_errno("asprintf");
3938 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3939 a->repo);
3940 if (err)
3941 goto done;
3943 if (a->status_codes) {
3944 size_t ncodes = strlen(a->status_codes);
3945 int i;
3946 for (i = 0; i < ncodes ; i++) {
3947 if (status == a->status_codes[i])
3948 break;
3950 if (i == ncodes) {
3951 /* Do not delete files in non-matching status. */
3952 free(ondisk_path);
3953 return NULL;
3955 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3956 a->status_codes[i] != GOT_STATUS_MISSING) {
3957 static char msg[64];
3958 snprintf(msg, sizeof(msg),
3959 "invalid status code '%c'", a->status_codes[i]);
3960 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3961 goto done;
3965 if (status != GOT_STATUS_NO_CHANGE) {
3966 if (status == GOT_STATUS_DELETE)
3967 goto done;
3968 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3969 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3970 goto done;
3972 if (status != GOT_STATUS_MODIFY &&
3973 status != GOT_STATUS_MISSING) {
3974 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3975 goto done;
3979 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3980 size_t root_len;
3982 if (dirfd != -1) {
3983 if (unlinkat(dirfd, de_name, 0) != 0) {
3984 err = got_error_from_errno2("unlinkat",
3985 ondisk_path);
3986 goto done;
3988 } else if (unlink(ondisk_path) != 0) {
3989 err = got_error_from_errno2("unlink", ondisk_path);
3990 goto done;
3993 root_len = strlen(a->worktree->root_path);
3994 do {
3995 char *parent;
3996 err = got_path_dirname(&parent, ondisk_path);
3997 if (err)
3998 goto done;
3999 free(ondisk_path);
4000 ondisk_path = parent;
4001 if (rmdir(ondisk_path) == -1) {
4002 if (errno != ENOTEMPTY)
4003 err = got_error_from_errno2("rmdir",
4004 ondisk_path);
4005 break;
4007 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4008 strlen(ondisk_path), root_len) != 0);
4011 got_fileindex_entry_mark_deleted_from_disk(ie);
4012 done:
4013 free(ondisk_path);
4014 if (err)
4015 return err;
4016 if (status == GOT_STATUS_DELETE)
4017 return NULL;
4018 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4019 staged_status, relpath);
4022 const struct got_error *
4023 got_worktree_schedule_delete(struct got_worktree *worktree,
4024 struct got_pathlist_head *paths, int delete_local_mods,
4025 const char *status_codes,
4026 got_worktree_delete_cb progress_cb, void *progress_arg,
4027 struct got_repository *repo, int keep_on_disk)
4029 struct got_fileindex *fileindex = NULL;
4030 char *fileindex_path = NULL;
4031 const struct got_error *err = NULL, *sync_err, *unlockerr;
4032 struct got_pathlist_entry *pe;
4033 struct schedule_deletion_args sda;
4035 err = lock_worktree(worktree, LOCK_EX);
4036 if (err)
4037 return err;
4039 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4040 if (err)
4041 goto done;
4043 sda.worktree = worktree;
4044 sda.fileindex = fileindex;
4045 sda.progress_cb = progress_cb;
4046 sda.progress_arg = progress_arg;
4047 sda.repo = repo;
4048 sda.delete_local_mods = delete_local_mods;
4049 sda.keep_on_disk = keep_on_disk;
4050 sda.status_codes = status_codes;
4052 TAILQ_FOREACH(pe, paths, entry) {
4053 err = worktree_status(worktree, pe->path, fileindex, repo,
4054 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4055 if (err)
4056 break;
4058 sync_err = sync_fileindex(fileindex, fileindex_path);
4059 if (sync_err && err == NULL)
4060 err = sync_err;
4061 done:
4062 free(fileindex_path);
4063 if (fileindex)
4064 got_fileindex_free(fileindex);
4065 unlockerr = lock_worktree(worktree, LOCK_SH);
4066 if (unlockerr && err == NULL)
4067 err = unlockerr;
4068 return err;
4071 static const struct got_error *
4072 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4074 const struct got_error *err = NULL;
4075 char *line = NULL;
4076 size_t linesize = 0, n;
4077 ssize_t linelen;
4079 linelen = getline(&line, &linesize, infile);
4080 if (linelen == -1) {
4081 if (ferror(infile)) {
4082 err = got_error_from_errno("getline");
4083 goto done;
4085 return NULL;
4087 if (outfile) {
4088 n = fwrite(line, 1, linelen, outfile);
4089 if (n != linelen) {
4090 err = got_ferror(outfile, GOT_ERR_IO);
4091 goto done;
4094 if (rejectfile) {
4095 n = fwrite(line, 1, linelen, rejectfile);
4096 if (n != linelen)
4097 err = got_ferror(outfile, GOT_ERR_IO);
4099 done:
4100 free(line);
4101 return err;
4104 static const struct got_error *
4105 skip_one_line(FILE *f)
4107 char *line = NULL;
4108 size_t linesize = 0;
4109 ssize_t linelen;
4111 linelen = getline(&line, &linesize, f);
4112 if (linelen == -1) {
4113 if (ferror(f))
4114 return got_error_from_errno("getline");
4115 return NULL;
4117 free(line);
4118 return NULL;
4121 static const struct got_error *
4122 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4123 int start_old, int end_old, int start_new, int end_new,
4124 FILE *outfile, FILE *rejectfile)
4126 const struct got_error *err;
4128 /* Copy old file's lines leading up to patch. */
4129 while (!feof(f1) && *line_cur1 < start_old) {
4130 err = copy_one_line(f1, outfile, NULL);
4131 if (err)
4132 return err;
4133 (*line_cur1)++;
4135 /* Skip new file's lines leading up to patch. */
4136 while (!feof(f2) && *line_cur2 < start_new) {
4137 if (rejectfile)
4138 err = copy_one_line(f2, NULL, rejectfile);
4139 else
4140 err = skip_one_line(f2);
4141 if (err)
4142 return err;
4143 (*line_cur2)++;
4145 /* Copy patched lines. */
4146 while (!feof(f2) && *line_cur2 <= end_new) {
4147 err = copy_one_line(f2, outfile, NULL);
4148 if (err)
4149 return err;
4150 (*line_cur2)++;
4152 /* Skip over old file's replaced lines. */
4153 while (!feof(f1) && *line_cur1 <= end_old) {
4154 if (rejectfile)
4155 err = copy_one_line(f1, NULL, rejectfile);
4156 else
4157 err = skip_one_line(f1);
4158 if (err)
4159 return err;
4160 (*line_cur1)++;
4163 return NULL;
4166 static const struct got_error *
4167 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4168 FILE *outfile, FILE *rejectfile)
4170 const struct got_error *err;
4172 if (outfile) {
4173 /* Copy old file's lines until EOF. */
4174 while (!feof(f1)) {
4175 err = copy_one_line(f1, outfile, NULL);
4176 if (err)
4177 return err;
4178 (*line_cur1)++;
4181 if (rejectfile) {
4182 /* Copy new file's lines until EOF. */
4183 while (!feof(f2)) {
4184 err = copy_one_line(f2, NULL, rejectfile);
4185 if (err)
4186 return err;
4187 (*line_cur2)++;
4191 return NULL;
4194 static const struct got_error *
4195 apply_or_reject_change(int *choice, int *nchunks_used,
4196 struct diff_result *diff_result, int n,
4197 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4198 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4199 got_worktree_patch_cb patch_cb, void *patch_arg)
4201 const struct got_error *err = NULL;
4202 struct diff_chunk_context cc = {};
4203 int start_old, end_old, start_new, end_new;
4204 FILE *hunkfile;
4205 struct diff_output_unidiff_state *diff_state;
4206 struct diff_input_info diff_info;
4207 int rc;
4209 *choice = GOT_PATCH_CHOICE_NONE;
4211 /* Get changed line numbers without context lines for copy_change(). */
4212 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4213 start_old = cc.left.start;
4214 end_old = cc.left.end;
4215 start_new = cc.right.start;
4216 end_new = cc.right.end;
4218 /* Get the same change with context lines for display. */
4219 memset(&cc, 0, sizeof(cc));
4220 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4222 memset(&diff_info, 0, sizeof(diff_info));
4223 diff_info.left_path = relpath;
4224 diff_info.right_path = relpath;
4226 diff_state = diff_output_unidiff_state_alloc();
4227 if (diff_state == NULL)
4228 return got_error_set_errno(ENOMEM,
4229 "diff_output_unidiff_state_alloc");
4231 hunkfile = got_opentemp();
4232 if (hunkfile == NULL) {
4233 err = got_error_from_errno("got_opentemp");
4234 goto done;
4237 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4238 diff_result, &cc);
4239 if (rc != DIFF_RC_OK) {
4240 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4241 goto done;
4244 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4245 err = got_ferror(hunkfile, GOT_ERR_IO);
4246 goto done;
4249 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4250 hunkfile, changeno, nchanges);
4251 if (err)
4252 goto done;
4254 switch (*choice) {
4255 case GOT_PATCH_CHOICE_YES:
4256 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4257 end_old, start_new, end_new, outfile, rejectfile);
4258 break;
4259 case GOT_PATCH_CHOICE_NO:
4260 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4261 end_old, start_new, end_new, rejectfile, outfile);
4262 break;
4263 case GOT_PATCH_CHOICE_QUIT:
4264 break;
4265 default:
4266 err = got_error(GOT_ERR_PATCH_CHOICE);
4267 break;
4269 done:
4270 diff_output_unidiff_state_free(diff_state);
4271 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4272 err = got_error_from_errno("fclose");
4273 return err;
4276 struct revert_file_args {
4277 struct got_worktree *worktree;
4278 struct got_fileindex *fileindex;
4279 got_worktree_checkout_cb progress_cb;
4280 void *progress_arg;
4281 got_worktree_patch_cb patch_cb;
4282 void *patch_arg;
4283 struct got_repository *repo;
4286 static const struct got_error *
4287 create_patched_content(char **path_outfile, int reverse_patch,
4288 struct got_object_id *blob_id, const char *path2,
4289 int dirfd2, const char *de_name2,
4290 const char *relpath, struct got_repository *repo,
4291 got_worktree_patch_cb patch_cb, void *patch_arg)
4293 const struct got_error *err, *free_err;
4294 struct got_blob_object *blob = NULL;
4295 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4296 int fd2 = -1;
4297 char link_target[PATH_MAX];
4298 ssize_t link_len = 0;
4299 char *path1 = NULL, *id_str = NULL;
4300 struct stat sb2;
4301 struct got_diffreg_result *diffreg_result = NULL;
4302 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4303 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4305 *path_outfile = NULL;
4307 err = got_object_id_str(&id_str, blob_id);
4308 if (err)
4309 return err;
4311 if (dirfd2 != -1) {
4312 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4313 if (fd2 == -1) {
4314 if (errno != ELOOP) {
4315 err = got_error_from_errno2("openat", path2);
4316 goto done;
4318 link_len = readlinkat(dirfd2, de_name2,
4319 link_target, sizeof(link_target));
4320 if (link_len == -1)
4321 return got_error_from_errno2("readlinkat", path2);
4322 sb2.st_mode = S_IFLNK;
4323 sb2.st_size = link_len;
4325 } else {
4326 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4327 if (fd2 == -1) {
4328 if (errno != ELOOP) {
4329 err = got_error_from_errno2("open", path2);
4330 goto done;
4332 link_len = readlink(path2, link_target,
4333 sizeof(link_target));
4334 if (link_len == -1)
4335 return got_error_from_errno2("readlink", path2);
4336 sb2.st_mode = S_IFLNK;
4337 sb2.st_size = link_len;
4340 if (fd2 != -1) {
4341 if (fstat(fd2, &sb2) == -1) {
4342 err = got_error_from_errno2("fstat", path2);
4343 goto done;
4346 f2 = fdopen(fd2, "r");
4347 if (f2 == NULL) {
4348 err = got_error_from_errno2("fdopen", path2);
4349 goto done;
4351 fd2 = -1;
4352 } else {
4353 size_t n;
4354 f2 = got_opentemp();
4355 if (f2 == NULL) {
4356 err = got_error_from_errno2("got_opentemp", path2);
4357 goto done;
4359 n = fwrite(link_target, 1, link_len, f2);
4360 if (n != link_len) {
4361 err = got_ferror(f2, GOT_ERR_IO);
4362 goto done;
4364 if (fflush(f2) == EOF) {
4365 err = got_error_from_errno("fflush");
4366 goto done;
4368 rewind(f2);
4371 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4372 if (err)
4373 goto done;
4375 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4376 if (err)
4377 goto done;
4379 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4380 if (err)
4381 goto done;
4383 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4384 NULL);
4385 if (err)
4386 goto done;
4388 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4389 if (err)
4390 goto done;
4392 if (fseek(f1, 0L, SEEK_SET) == -1)
4393 return got_ferror(f1, GOT_ERR_IO);
4394 if (fseek(f2, 0L, SEEK_SET) == -1)
4395 return got_ferror(f2, GOT_ERR_IO);
4397 /* Count the number of actual changes in the diff result. */
4398 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4399 struct diff_chunk_context cc = {};
4400 diff_chunk_context_load_change(&cc, &nchunks_used,
4401 diffreg_result->result, n, 0);
4402 nchanges++;
4404 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4405 int choice;
4406 err = apply_or_reject_change(&choice, &nchunks_used,
4407 diffreg_result->result, n, relpath, f1, f2,
4408 &line_cur1, &line_cur2,
4409 reverse_patch ? NULL : outfile,
4410 reverse_patch ? outfile : NULL,
4411 ++i, nchanges, patch_cb, patch_arg);
4412 if (err)
4413 goto done;
4414 if (choice == GOT_PATCH_CHOICE_YES)
4415 have_content = 1;
4416 else if (choice == GOT_PATCH_CHOICE_QUIT)
4417 break;
4419 if (have_content) {
4420 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4421 reverse_patch ? NULL : outfile,
4422 reverse_patch ? outfile : NULL);
4423 if (err)
4424 goto done;
4426 if (!S_ISLNK(sb2.st_mode)) {
4427 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4428 err = got_error_from_errno2("fchmod", path2);
4429 goto done;
4433 done:
4434 free(id_str);
4435 if (blob)
4436 got_object_blob_close(blob);
4437 free_err = got_diffreg_result_free(diffreg_result);
4438 if (err == NULL)
4439 err = free_err;
4440 if (f1 && fclose(f1) == EOF && err == NULL)
4441 err = got_error_from_errno2("fclose", path1);
4442 if (f2 && fclose(f2) == EOF && err == NULL)
4443 err = got_error_from_errno2("fclose", path2);
4444 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4445 err = got_error_from_errno2("close", path2);
4446 if (outfile && fclose(outfile) == EOF && err == NULL)
4447 err = got_error_from_errno2("fclose", *path_outfile);
4448 if (path1 && unlink(path1) == -1 && err == NULL)
4449 err = got_error_from_errno2("unlink", path1);
4450 if (err || !have_content) {
4451 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4452 err = got_error_from_errno2("unlink", *path_outfile);
4453 free(*path_outfile);
4454 *path_outfile = NULL;
4456 free(path1);
4457 return err;
4460 static const struct got_error *
4461 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4462 const char *relpath, struct got_object_id *blob_id,
4463 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4464 int dirfd, const char *de_name)
4466 struct revert_file_args *a = arg;
4467 const struct got_error *err = NULL;
4468 char *parent_path = NULL;
4469 struct got_fileindex_entry *ie;
4470 struct got_tree_object *tree = NULL;
4471 struct got_object_id *tree_id = NULL;
4472 const struct got_tree_entry *te = NULL;
4473 char *tree_path = NULL, *te_name;
4474 char *ondisk_path = NULL, *path_content = NULL;
4475 struct got_blob_object *blob = NULL;
4477 /* Reverting a staged deletion is a no-op. */
4478 if (status == GOT_STATUS_DELETE &&
4479 staged_status != GOT_STATUS_NO_CHANGE)
4480 return NULL;
4482 if (status == GOT_STATUS_UNVERSIONED)
4483 return (*a->progress_cb)(a->progress_arg,
4484 GOT_STATUS_UNVERSIONED, relpath);
4486 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4487 if (ie == NULL)
4488 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4490 /* Construct in-repository path of tree which contains this blob. */
4491 err = got_path_dirname(&parent_path, ie->path);
4492 if (err) {
4493 if (err->code != GOT_ERR_BAD_PATH)
4494 goto done;
4495 parent_path = strdup("/");
4496 if (parent_path == NULL) {
4497 err = got_error_from_errno("strdup");
4498 goto done;
4501 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4502 tree_path = strdup(parent_path);
4503 if (tree_path == NULL) {
4504 err = got_error_from_errno("strdup");
4505 goto done;
4507 } else {
4508 if (got_path_is_root_dir(parent_path)) {
4509 tree_path = strdup(a->worktree->path_prefix);
4510 if (tree_path == NULL) {
4511 err = got_error_from_errno("strdup");
4512 goto done;
4514 } else {
4515 if (asprintf(&tree_path, "%s/%s",
4516 a->worktree->path_prefix, parent_path) == -1) {
4517 err = got_error_from_errno("asprintf");
4518 goto done;
4523 err = got_object_id_by_path(&tree_id, a->repo,
4524 a->worktree->base_commit_id, tree_path);
4525 if (err) {
4526 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4527 (status == GOT_STATUS_ADD ||
4528 staged_status == GOT_STATUS_ADD)))
4529 goto done;
4530 } else {
4531 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4532 if (err)
4533 goto done;
4535 err = got_path_basename(&te_name, ie->path);
4536 if (err)
4537 goto done;
4539 te = got_object_tree_find_entry(tree, te_name);
4540 free(te_name);
4541 if (te == NULL && status != GOT_STATUS_ADD &&
4542 staged_status != GOT_STATUS_ADD) {
4543 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4544 goto done;
4548 switch (status) {
4549 case GOT_STATUS_ADD:
4550 if (a->patch_cb) {
4551 int choice = GOT_PATCH_CHOICE_NONE;
4552 err = (*a->patch_cb)(&choice, a->patch_arg,
4553 status, ie->path, NULL, 1, 1);
4554 if (err)
4555 goto done;
4556 if (choice != GOT_PATCH_CHOICE_YES)
4557 break;
4559 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4560 ie->path);
4561 if (err)
4562 goto done;
4563 got_fileindex_entry_remove(a->fileindex, ie);
4564 break;
4565 case GOT_STATUS_DELETE:
4566 if (a->patch_cb) {
4567 int choice = GOT_PATCH_CHOICE_NONE;
4568 err = (*a->patch_cb)(&choice, a->patch_arg,
4569 status, ie->path, NULL, 1, 1);
4570 if (err)
4571 goto done;
4572 if (choice != GOT_PATCH_CHOICE_YES)
4573 break;
4575 /* fall through */
4576 case GOT_STATUS_MODIFY:
4577 case GOT_STATUS_MODE_CHANGE:
4578 case GOT_STATUS_CONFLICT:
4579 case GOT_STATUS_MISSING: {
4580 struct got_object_id id;
4581 if (staged_status == GOT_STATUS_ADD ||
4582 staged_status == GOT_STATUS_MODIFY) {
4583 memcpy(id.sha1, ie->staged_blob_sha1,
4584 SHA1_DIGEST_LENGTH);
4585 } else
4586 memcpy(id.sha1, ie->blob_sha1,
4587 SHA1_DIGEST_LENGTH);
4588 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4589 if (err)
4590 goto done;
4592 if (asprintf(&ondisk_path, "%s/%s",
4593 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4594 err = got_error_from_errno("asprintf");
4595 goto done;
4598 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4599 status == GOT_STATUS_CONFLICT)) {
4600 int is_bad_symlink = 0;
4601 err = create_patched_content(&path_content, 1, &id,
4602 ondisk_path, dirfd, de_name, ie->path, a->repo,
4603 a->patch_cb, a->patch_arg);
4604 if (err || path_content == NULL)
4605 break;
4606 if (te && S_ISLNK(te->mode)) {
4607 if (unlink(path_content) == -1) {
4608 err = got_error_from_errno2("unlink",
4609 path_content);
4610 break;
4612 err = install_symlink(&is_bad_symlink,
4613 a->worktree, ondisk_path, ie->path,
4614 blob, 0, 1, 0, a->repo,
4615 a->progress_cb, a->progress_arg);
4616 } else {
4617 if (rename(path_content, ondisk_path) == -1) {
4618 err = got_error_from_errno3("rename",
4619 path_content, ondisk_path);
4620 goto done;
4623 } else {
4624 int is_bad_symlink = 0;
4625 if (te && S_ISLNK(te->mode)) {
4626 err = install_symlink(&is_bad_symlink,
4627 a->worktree, ondisk_path, ie->path,
4628 blob, 0, 1, 0, a->repo,
4629 a->progress_cb, a->progress_arg);
4630 } else {
4631 err = install_blob(a->worktree, ondisk_path,
4632 ie->path,
4633 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4634 got_fileindex_perms_to_st(ie), blob,
4635 0, 1, 0, 0, a->repo,
4636 a->progress_cb, a->progress_arg);
4638 if (err)
4639 goto done;
4640 if (status == GOT_STATUS_DELETE ||
4641 status == GOT_STATUS_MODE_CHANGE) {
4642 err = got_fileindex_entry_update(ie,
4643 a->worktree->root_fd, relpath,
4644 blob->id.sha1,
4645 a->worktree->base_commit_id->sha1, 1);
4646 if (err)
4647 goto done;
4649 if (is_bad_symlink) {
4650 got_fileindex_entry_filetype_set(ie,
4651 GOT_FILEIDX_MODE_BAD_SYMLINK);
4654 break;
4656 default:
4657 break;
4659 done:
4660 free(ondisk_path);
4661 free(path_content);
4662 free(parent_path);
4663 free(tree_path);
4664 if (blob)
4665 got_object_blob_close(blob);
4666 if (tree)
4667 got_object_tree_close(tree);
4668 free(tree_id);
4669 return err;
4672 const struct got_error *
4673 got_worktree_revert(struct got_worktree *worktree,
4674 struct got_pathlist_head *paths,
4675 got_worktree_checkout_cb progress_cb, void *progress_arg,
4676 got_worktree_patch_cb patch_cb, void *patch_arg,
4677 struct got_repository *repo)
4679 struct got_fileindex *fileindex = NULL;
4680 char *fileindex_path = NULL;
4681 const struct got_error *err = NULL, *unlockerr = NULL;
4682 const struct got_error *sync_err = NULL;
4683 struct got_pathlist_entry *pe;
4684 struct revert_file_args rfa;
4686 err = lock_worktree(worktree, LOCK_EX);
4687 if (err)
4688 return err;
4690 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4691 if (err)
4692 goto done;
4694 rfa.worktree = worktree;
4695 rfa.fileindex = fileindex;
4696 rfa.progress_cb = progress_cb;
4697 rfa.progress_arg = progress_arg;
4698 rfa.patch_cb = patch_cb;
4699 rfa.patch_arg = patch_arg;
4700 rfa.repo = repo;
4701 TAILQ_FOREACH(pe, paths, entry) {
4702 err = worktree_status(worktree, pe->path, fileindex, repo,
4703 revert_file, &rfa, NULL, NULL, 0, 0);
4704 if (err)
4705 break;
4707 sync_err = sync_fileindex(fileindex, fileindex_path);
4708 if (sync_err && err == NULL)
4709 err = sync_err;
4710 done:
4711 free(fileindex_path);
4712 if (fileindex)
4713 got_fileindex_free(fileindex);
4714 unlockerr = lock_worktree(worktree, LOCK_SH);
4715 if (unlockerr && err == NULL)
4716 err = unlockerr;
4717 return err;
4720 static void
4721 free_commitable(struct got_commitable *ct)
4723 free(ct->path);
4724 free(ct->in_repo_path);
4725 free(ct->ondisk_path);
4726 free(ct->blob_id);
4727 free(ct->base_blob_id);
4728 free(ct->staged_blob_id);
4729 free(ct->base_commit_id);
4730 free(ct);
4733 struct collect_commitables_arg {
4734 struct got_pathlist_head *commitable_paths;
4735 struct got_repository *repo;
4736 struct got_worktree *worktree;
4737 struct got_fileindex *fileindex;
4738 int have_staged_files;
4739 int allow_bad_symlinks;
4742 static const struct got_error *
4743 collect_commitables(void *arg, unsigned char status,
4744 unsigned char staged_status, const char *relpath,
4745 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4746 struct got_object_id *commit_id, int dirfd, const char *de_name)
4748 struct collect_commitables_arg *a = arg;
4749 const struct got_error *err = NULL;
4750 struct got_commitable *ct = NULL;
4751 struct got_pathlist_entry *new = NULL;
4752 char *parent_path = NULL, *path = NULL;
4753 struct stat sb;
4755 if (a->have_staged_files) {
4756 if (staged_status != GOT_STATUS_MODIFY &&
4757 staged_status != GOT_STATUS_ADD &&
4758 staged_status != GOT_STATUS_DELETE)
4759 return NULL;
4760 } else {
4761 if (status == GOT_STATUS_CONFLICT)
4762 return got_error(GOT_ERR_COMMIT_CONFLICT);
4764 if (status != GOT_STATUS_MODIFY &&
4765 status != GOT_STATUS_MODE_CHANGE &&
4766 status != GOT_STATUS_ADD &&
4767 status != GOT_STATUS_DELETE)
4768 return NULL;
4771 if (asprintf(&path, "/%s", relpath) == -1) {
4772 err = got_error_from_errno("asprintf");
4773 goto done;
4775 if (strcmp(path, "/") == 0) {
4776 parent_path = strdup("");
4777 if (parent_path == NULL)
4778 return got_error_from_errno("strdup");
4779 } else {
4780 err = got_path_dirname(&parent_path, path);
4781 if (err)
4782 return err;
4785 ct = calloc(1, sizeof(*ct));
4786 if (ct == NULL) {
4787 err = got_error_from_errno("calloc");
4788 goto done;
4791 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4792 relpath) == -1) {
4793 err = got_error_from_errno("asprintf");
4794 goto done;
4797 if (staged_status == GOT_STATUS_ADD ||
4798 staged_status == GOT_STATUS_MODIFY) {
4799 struct got_fileindex_entry *ie;
4800 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4801 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4802 case GOT_FILEIDX_MODE_REGULAR_FILE:
4803 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4804 ct->mode = S_IFREG;
4805 break;
4806 case GOT_FILEIDX_MODE_SYMLINK:
4807 ct->mode = S_IFLNK;
4808 break;
4809 default:
4810 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4811 goto done;
4813 ct->mode |= got_fileindex_entry_perms_get(ie);
4814 } else if (status != GOT_STATUS_DELETE &&
4815 staged_status != GOT_STATUS_DELETE) {
4816 if (dirfd != -1) {
4817 if (fstatat(dirfd, de_name, &sb,
4818 AT_SYMLINK_NOFOLLOW) == -1) {
4819 err = got_error_from_errno2("fstatat",
4820 ct->ondisk_path);
4821 goto done;
4823 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4824 err = got_error_from_errno2("lstat", ct->ondisk_path);
4825 goto done;
4827 ct->mode = sb.st_mode;
4830 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4831 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4832 relpath) == -1) {
4833 err = got_error_from_errno("asprintf");
4834 goto done;
4837 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4838 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4839 int is_bad_symlink;
4840 char target_path[PATH_MAX];
4841 ssize_t target_len;
4842 target_len = readlink(ct->ondisk_path, target_path,
4843 sizeof(target_path));
4844 if (target_len == -1) {
4845 err = got_error_from_errno2("readlink",
4846 ct->ondisk_path);
4847 goto done;
4849 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4850 target_len, ct->ondisk_path, a->worktree->root_path);
4851 if (err)
4852 goto done;
4853 if (is_bad_symlink) {
4854 err = got_error_path(ct->ondisk_path,
4855 GOT_ERR_BAD_SYMLINK);
4856 goto done;
4861 ct->status = status;
4862 ct->staged_status = staged_status;
4863 ct->blob_id = NULL; /* will be filled in when blob gets created */
4864 if (ct->status != GOT_STATUS_ADD &&
4865 ct->staged_status != GOT_STATUS_ADD) {
4866 ct->base_blob_id = got_object_id_dup(blob_id);
4867 if (ct->base_blob_id == NULL) {
4868 err = got_error_from_errno("got_object_id_dup");
4869 goto done;
4871 ct->base_commit_id = got_object_id_dup(commit_id);
4872 if (ct->base_commit_id == NULL) {
4873 err = got_error_from_errno("got_object_id_dup");
4874 goto done;
4877 if (ct->staged_status == GOT_STATUS_ADD ||
4878 ct->staged_status == GOT_STATUS_MODIFY) {
4879 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4880 if (ct->staged_blob_id == NULL) {
4881 err = got_error_from_errno("got_object_id_dup");
4882 goto done;
4885 ct->path = strdup(path);
4886 if (ct->path == NULL) {
4887 err = got_error_from_errno("strdup");
4888 goto done;
4890 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4891 done:
4892 if (ct && (err || new == NULL))
4893 free_commitable(ct);
4894 free(parent_path);
4895 free(path);
4896 return err;
4899 static const struct got_error *write_tree(struct got_object_id **, int *,
4900 struct got_tree_object *, const char *, struct got_pathlist_head *,
4901 got_worktree_status_cb status_cb, void *status_arg,
4902 struct got_repository *);
4904 static const struct got_error *
4905 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4906 struct got_tree_entry *te, const char *parent_path,
4907 struct got_pathlist_head *commitable_paths,
4908 got_worktree_status_cb status_cb, void *status_arg,
4909 struct got_repository *repo)
4911 const struct got_error *err = NULL;
4912 struct got_tree_object *subtree;
4913 char *subpath;
4915 if (asprintf(&subpath, "%s%s%s", parent_path,
4916 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4917 return got_error_from_errno("asprintf");
4919 err = got_object_open_as_tree(&subtree, repo, &te->id);
4920 if (err)
4921 return err;
4923 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4924 commitable_paths, status_cb, status_arg, repo);
4925 got_object_tree_close(subtree);
4926 free(subpath);
4927 return err;
4930 static const struct got_error *
4931 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4933 const struct got_error *err = NULL;
4934 char *ct_parent_path = NULL;
4936 *match = 0;
4938 if (strchr(ct->in_repo_path, '/') == NULL) {
4939 *match = got_path_is_root_dir(path);
4940 return NULL;
4943 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4944 if (err)
4945 return err;
4946 *match = (strcmp(path, ct_parent_path) == 0);
4947 free(ct_parent_path);
4948 return err;
4951 static mode_t
4952 get_ct_file_mode(struct got_commitable *ct)
4954 if (S_ISLNK(ct->mode))
4955 return S_IFLNK;
4957 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4960 static const struct got_error *
4961 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4962 struct got_tree_entry *te, struct got_commitable *ct)
4964 const struct got_error *err = NULL;
4966 *new_te = NULL;
4968 err = got_object_tree_entry_dup(new_te, te);
4969 if (err)
4970 goto done;
4972 (*new_te)->mode = get_ct_file_mode(ct);
4974 if (ct->staged_status == GOT_STATUS_MODIFY)
4975 memcpy(&(*new_te)->id, ct->staged_blob_id,
4976 sizeof((*new_te)->id));
4977 else
4978 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4979 done:
4980 if (err && *new_te) {
4981 free(*new_te);
4982 *new_te = NULL;
4984 return err;
4987 static const struct got_error *
4988 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4989 struct got_commitable *ct)
4991 const struct got_error *err = NULL;
4992 char *ct_name = NULL;
4994 *new_te = NULL;
4996 *new_te = calloc(1, sizeof(**new_te));
4997 if (*new_te == NULL)
4998 return got_error_from_errno("calloc");
5000 err = got_path_basename(&ct_name, ct->path);
5001 if (err)
5002 goto done;
5003 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5004 sizeof((*new_te)->name)) {
5005 err = got_error(GOT_ERR_NO_SPACE);
5006 goto done;
5009 (*new_te)->mode = get_ct_file_mode(ct);
5011 if (ct->staged_status == GOT_STATUS_ADD)
5012 memcpy(&(*new_te)->id, ct->staged_blob_id,
5013 sizeof((*new_te)->id));
5014 else
5015 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5016 done:
5017 free(ct_name);
5018 if (err && *new_te) {
5019 free(*new_te);
5020 *new_te = NULL;
5022 return err;
5025 static const struct got_error *
5026 insert_tree_entry(struct got_tree_entry *new_te,
5027 struct got_pathlist_head *paths)
5029 const struct got_error *err = NULL;
5030 struct got_pathlist_entry *new_pe;
5032 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5033 if (err)
5034 return err;
5035 if (new_pe == NULL)
5036 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5037 return NULL;
5040 static const struct got_error *
5041 report_ct_status(struct got_commitable *ct,
5042 got_worktree_status_cb status_cb, void *status_arg)
5044 const char *ct_path = ct->path;
5045 unsigned char status;
5047 while (ct_path[0] == '/')
5048 ct_path++;
5050 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5051 status = ct->staged_status;
5052 else
5053 status = ct->status;
5055 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5056 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5059 static const struct got_error *
5060 match_modified_subtree(int *modified, struct got_tree_entry *te,
5061 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5063 const struct got_error *err = NULL;
5064 struct got_pathlist_entry *pe;
5065 char *te_path;
5067 *modified = 0;
5069 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5070 got_path_is_root_dir(base_tree_path) ? "" : "/",
5071 te->name) == -1)
5072 return got_error_from_errno("asprintf");
5074 TAILQ_FOREACH(pe, commitable_paths, entry) {
5075 struct got_commitable *ct = pe->data;
5076 *modified = got_path_is_child(ct->in_repo_path, te_path,
5077 strlen(te_path));
5078 if (*modified)
5079 break;
5082 free(te_path);
5083 return err;
5086 static const struct got_error *
5087 match_deleted_or_modified_ct(struct got_commitable **ctp,
5088 struct got_tree_entry *te, const char *base_tree_path,
5089 struct got_pathlist_head *commitable_paths)
5091 const struct got_error *err = NULL;
5092 struct got_pathlist_entry *pe;
5094 *ctp = NULL;
5096 TAILQ_FOREACH(pe, commitable_paths, entry) {
5097 struct got_commitable *ct = pe->data;
5098 char *ct_name = NULL;
5099 int path_matches;
5101 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5102 if (ct->status != GOT_STATUS_MODIFY &&
5103 ct->status != GOT_STATUS_MODE_CHANGE &&
5104 ct->status != GOT_STATUS_DELETE)
5105 continue;
5106 } else {
5107 if (ct->staged_status != GOT_STATUS_MODIFY &&
5108 ct->staged_status != GOT_STATUS_DELETE)
5109 continue;
5112 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5113 continue;
5115 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5116 if (err)
5117 return err;
5118 if (!path_matches)
5119 continue;
5121 err = got_path_basename(&ct_name, pe->path);
5122 if (err)
5123 return err;
5125 if (strcmp(te->name, ct_name) != 0) {
5126 free(ct_name);
5127 continue;
5129 free(ct_name);
5131 *ctp = ct;
5132 break;
5135 return err;
5138 static const struct got_error *
5139 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5140 const char *child_path, const char *path_base_tree,
5141 struct got_pathlist_head *commitable_paths,
5142 got_worktree_status_cb status_cb, void *status_arg,
5143 struct got_repository *repo)
5145 const struct got_error *err = NULL;
5146 struct got_tree_entry *new_te;
5147 char *subtree_path;
5148 struct got_object_id *id = NULL;
5149 int nentries;
5151 *new_tep = NULL;
5153 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5154 got_path_is_root_dir(path_base_tree) ? "" : "/",
5155 child_path) == -1)
5156 return got_error_from_errno("asprintf");
5158 new_te = calloc(1, sizeof(*new_te));
5159 if (new_te == NULL)
5160 return got_error_from_errno("calloc");
5161 new_te->mode = S_IFDIR;
5163 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5164 sizeof(new_te->name)) {
5165 err = got_error(GOT_ERR_NO_SPACE);
5166 goto done;
5168 err = write_tree(&id, &nentries, NULL, subtree_path,
5169 commitable_paths, status_cb, status_arg, repo);
5170 if (err) {
5171 free(new_te);
5172 goto done;
5174 memcpy(&new_te->id, id, sizeof(new_te->id));
5175 done:
5176 free(id);
5177 free(subtree_path);
5178 if (err == NULL)
5179 *new_tep = new_te;
5180 return err;
5183 static const struct got_error *
5184 write_tree(struct got_object_id **new_tree_id, int *nentries,
5185 struct got_tree_object *base_tree, const char *path_base_tree,
5186 struct got_pathlist_head *commitable_paths,
5187 got_worktree_status_cb status_cb, void *status_arg,
5188 struct got_repository *repo)
5190 const struct got_error *err = NULL;
5191 struct got_pathlist_head paths;
5192 struct got_tree_entry *te, *new_te = NULL;
5193 struct got_pathlist_entry *pe;
5195 TAILQ_INIT(&paths);
5196 *nentries = 0;
5198 /* Insert, and recurse into, newly added entries first. */
5199 TAILQ_FOREACH(pe, commitable_paths, entry) {
5200 struct got_commitable *ct = pe->data;
5201 char *child_path = NULL, *slash;
5203 if ((ct->status != GOT_STATUS_ADD &&
5204 ct->staged_status != GOT_STATUS_ADD) ||
5205 (ct->flags & GOT_COMMITABLE_ADDED))
5206 continue;
5208 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5209 strlen(path_base_tree)))
5210 continue;
5212 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5213 ct->in_repo_path);
5214 if (err)
5215 goto done;
5217 slash = strchr(child_path, '/');
5218 if (slash == NULL) {
5219 err = alloc_added_blob_tree_entry(&new_te, ct);
5220 if (err)
5221 goto done;
5222 err = report_ct_status(ct, status_cb, status_arg);
5223 if (err)
5224 goto done;
5225 ct->flags |= GOT_COMMITABLE_ADDED;
5226 err = insert_tree_entry(new_te, &paths);
5227 if (err)
5228 goto done;
5229 (*nentries)++;
5230 } else {
5231 *slash = '\0'; /* trim trailing path components */
5232 if (base_tree == NULL ||
5233 got_object_tree_find_entry(base_tree, child_path)
5234 == NULL) {
5235 err = make_subtree_for_added_blob(&new_te,
5236 child_path, path_base_tree,
5237 commitable_paths, status_cb, status_arg,
5238 repo);
5239 if (err)
5240 goto done;
5241 err = insert_tree_entry(new_te, &paths);
5242 if (err)
5243 goto done;
5244 (*nentries)++;
5249 if (base_tree) {
5250 int i, nbase_entries;
5251 /* Handle modified and deleted entries. */
5252 nbase_entries = got_object_tree_get_nentries(base_tree);
5253 for (i = 0; i < nbase_entries; i++) {
5254 struct got_commitable *ct = NULL;
5256 te = got_object_tree_get_entry(base_tree, i);
5257 if (got_object_tree_entry_is_submodule(te)) {
5258 /* Entry is a submodule; just copy it. */
5259 err = got_object_tree_entry_dup(&new_te, te);
5260 if (err)
5261 goto done;
5262 err = insert_tree_entry(new_te, &paths);
5263 if (err)
5264 goto done;
5265 (*nentries)++;
5266 continue;
5269 if (S_ISDIR(te->mode)) {
5270 int modified;
5271 err = got_object_tree_entry_dup(&new_te, te);
5272 if (err)
5273 goto done;
5274 err = match_modified_subtree(&modified, te,
5275 path_base_tree, commitable_paths);
5276 if (err)
5277 goto done;
5278 /* Avoid recursion into unmodified subtrees. */
5279 if (modified) {
5280 struct got_object_id *new_id;
5281 int nsubentries;
5282 err = write_subtree(&new_id,
5283 &nsubentries, te,
5284 path_base_tree, commitable_paths,
5285 status_cb, status_arg, repo);
5286 if (err)
5287 goto done;
5288 if (nsubentries == 0) {
5289 /* All entries were deleted. */
5290 free(new_id);
5291 continue;
5293 memcpy(&new_te->id, new_id,
5294 sizeof(new_te->id));
5295 free(new_id);
5297 err = insert_tree_entry(new_te, &paths);
5298 if (err)
5299 goto done;
5300 (*nentries)++;
5301 continue;
5304 err = match_deleted_or_modified_ct(&ct, te,
5305 path_base_tree, commitable_paths);
5306 if (err)
5307 goto done;
5308 if (ct) {
5309 /* NB: Deleted entries get dropped here. */
5310 if (ct->status == GOT_STATUS_MODIFY ||
5311 ct->status == GOT_STATUS_MODE_CHANGE ||
5312 ct->staged_status == GOT_STATUS_MODIFY) {
5313 err = alloc_modified_blob_tree_entry(
5314 &new_te, te, ct);
5315 if (err)
5316 goto done;
5317 err = insert_tree_entry(new_te, &paths);
5318 if (err)
5319 goto done;
5320 (*nentries)++;
5322 err = report_ct_status(ct, status_cb,
5323 status_arg);
5324 if (err)
5325 goto done;
5326 } else {
5327 /* Entry is unchanged; just copy it. */
5328 err = got_object_tree_entry_dup(&new_te, te);
5329 if (err)
5330 goto done;
5331 err = insert_tree_entry(new_te, &paths);
5332 if (err)
5333 goto done;
5334 (*nentries)++;
5339 /* Write new list of entries; deleted entries have been dropped. */
5340 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5341 done:
5342 got_pathlist_free(&paths);
5343 return err;
5346 static const struct got_error *
5347 update_fileindex_after_commit(struct got_worktree *worktree,
5348 struct got_pathlist_head *commitable_paths,
5349 struct got_object_id *new_base_commit_id,
5350 struct got_fileindex *fileindex, int have_staged_files)
5352 const struct got_error *err = NULL;
5353 struct got_pathlist_entry *pe;
5354 char *relpath = NULL;
5356 TAILQ_FOREACH(pe, commitable_paths, entry) {
5357 struct got_fileindex_entry *ie;
5358 struct got_commitable *ct = pe->data;
5360 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5362 err = got_path_skip_common_ancestor(&relpath,
5363 worktree->root_path, ct->ondisk_path);
5364 if (err)
5365 goto done;
5367 if (ie) {
5368 if (ct->status == GOT_STATUS_DELETE ||
5369 ct->staged_status == GOT_STATUS_DELETE) {
5370 got_fileindex_entry_remove(fileindex, ie);
5371 } else if (ct->staged_status == GOT_STATUS_ADD ||
5372 ct->staged_status == GOT_STATUS_MODIFY) {
5373 got_fileindex_entry_stage_set(ie,
5374 GOT_FILEIDX_STAGE_NONE);
5375 got_fileindex_entry_staged_filetype_set(ie, 0);
5377 err = got_fileindex_entry_update(ie,
5378 worktree->root_fd, relpath,
5379 ct->staged_blob_id->sha1,
5380 new_base_commit_id->sha1,
5381 !have_staged_files);
5382 } else
5383 err = got_fileindex_entry_update(ie,
5384 worktree->root_fd, relpath,
5385 ct->blob_id->sha1,
5386 new_base_commit_id->sha1,
5387 !have_staged_files);
5388 } else {
5389 err = got_fileindex_entry_alloc(&ie, pe->path);
5390 if (err)
5391 goto done;
5392 err = got_fileindex_entry_update(ie,
5393 worktree->root_fd, relpath, ct->blob_id->sha1,
5394 new_base_commit_id->sha1, 1);
5395 if (err) {
5396 got_fileindex_entry_free(ie);
5397 goto done;
5399 err = got_fileindex_entry_add(fileindex, ie);
5400 if (err) {
5401 got_fileindex_entry_free(ie);
5402 goto done;
5405 free(relpath);
5406 relpath = NULL;
5408 done:
5409 free(relpath);
5410 return err;
5414 static const struct got_error *
5415 check_out_of_date(const char *in_repo_path, unsigned char status,
5416 unsigned char staged_status, struct got_object_id *base_blob_id,
5417 struct got_object_id *base_commit_id,
5418 struct got_object_id *head_commit_id, struct got_repository *repo,
5419 int ood_errcode)
5421 const struct got_error *err = NULL;
5422 struct got_object_id *id = NULL;
5424 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5425 /* Trivial case: base commit == head commit */
5426 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5427 return NULL;
5429 * Ensure file content which local changes were based
5430 * on matches file content in the branch head.
5432 err = got_object_id_by_path(&id, repo, head_commit_id,
5433 in_repo_path);
5434 if (err) {
5435 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5436 err = got_error(ood_errcode);
5437 goto done;
5438 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5439 err = got_error(ood_errcode);
5440 } else {
5441 /* Require that added files don't exist in the branch head. */
5442 err = got_object_id_by_path(&id, repo, head_commit_id,
5443 in_repo_path);
5444 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5445 goto done;
5446 err = id ? got_error(ood_errcode) : NULL;
5448 done:
5449 free(id);
5450 return err;
5453 const struct got_error *
5454 commit_worktree(struct got_object_id **new_commit_id,
5455 struct got_pathlist_head *commitable_paths,
5456 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5457 const char *author, const char *committer,
5458 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5459 got_worktree_status_cb status_cb, void *status_arg,
5460 struct got_repository *repo)
5462 const struct got_error *err = NULL, *unlockerr = NULL;
5463 struct got_pathlist_entry *pe;
5464 const char *head_ref_name = NULL;
5465 struct got_commit_object *head_commit = NULL;
5466 struct got_reference *head_ref2 = NULL;
5467 struct got_object_id *head_commit_id2 = NULL;
5468 struct got_tree_object *head_tree = NULL;
5469 struct got_object_id *new_tree_id = NULL;
5470 int nentries;
5471 struct got_object_id_queue parent_ids;
5472 struct got_object_qid *pid = NULL;
5473 char *logmsg = NULL;
5475 *new_commit_id = NULL;
5477 SIMPLEQ_INIT(&parent_ids);
5479 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5480 if (err)
5481 goto done;
5483 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5484 if (err)
5485 goto done;
5487 if (commit_msg_cb != NULL) {
5488 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5489 if (err)
5490 goto done;
5493 if (logmsg == NULL || strlen(logmsg) == 0) {
5494 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5495 goto done;
5498 /* Create blobs from added and modified files and record their IDs. */
5499 TAILQ_FOREACH(pe, commitable_paths, entry) {
5500 struct got_commitable *ct = pe->data;
5501 char *ondisk_path;
5503 /* Blobs for staged files already exist. */
5504 if (ct->staged_status == GOT_STATUS_ADD ||
5505 ct->staged_status == GOT_STATUS_MODIFY)
5506 continue;
5508 if (ct->status != GOT_STATUS_ADD &&
5509 ct->status != GOT_STATUS_MODIFY &&
5510 ct->status != GOT_STATUS_MODE_CHANGE)
5511 continue;
5513 if (asprintf(&ondisk_path, "%s/%s",
5514 worktree->root_path, pe->path) == -1) {
5515 err = got_error_from_errno("asprintf");
5516 goto done;
5518 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5519 free(ondisk_path);
5520 if (err)
5521 goto done;
5524 /* Recursively write new tree objects. */
5525 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5526 commitable_paths, status_cb, status_arg, repo);
5527 if (err)
5528 goto done;
5530 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5531 if (err)
5532 goto done;
5533 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5534 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5535 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5536 got_object_qid_free(pid);
5537 if (logmsg != NULL)
5538 free(logmsg);
5539 if (err)
5540 goto done;
5542 /* Check if a concurrent commit to our branch has occurred. */
5543 head_ref_name = got_worktree_get_head_ref_name(worktree);
5544 if (head_ref_name == NULL) {
5545 err = got_error_from_errno("got_worktree_get_head_ref_name");
5546 goto done;
5548 /* Lock the reference here to prevent concurrent modification. */
5549 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5550 if (err)
5551 goto done;
5552 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5553 if (err)
5554 goto done;
5555 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5556 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5557 goto done;
5559 /* Update branch head in repository. */
5560 err = got_ref_change_ref(head_ref2, *new_commit_id);
5561 if (err)
5562 goto done;
5563 err = got_ref_write(head_ref2, repo);
5564 if (err)
5565 goto done;
5567 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5568 if (err)
5569 goto done;
5571 err = ref_base_commit(worktree, repo);
5572 if (err)
5573 goto done;
5574 done:
5575 if (head_tree)
5576 got_object_tree_close(head_tree);
5577 if (head_commit)
5578 got_object_commit_close(head_commit);
5579 free(head_commit_id2);
5580 if (head_ref2) {
5581 unlockerr = got_ref_unlock(head_ref2);
5582 if (unlockerr && err == NULL)
5583 err = unlockerr;
5584 got_ref_close(head_ref2);
5586 return err;
5589 static const struct got_error *
5590 check_path_is_commitable(const char *path,
5591 struct got_pathlist_head *commitable_paths)
5593 struct got_pathlist_entry *cpe = NULL;
5594 size_t path_len = strlen(path);
5596 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5597 struct got_commitable *ct = cpe->data;
5598 const char *ct_path = ct->path;
5600 while (ct_path[0] == '/')
5601 ct_path++;
5603 if (strcmp(path, ct_path) == 0 ||
5604 got_path_is_child(ct_path, path, path_len))
5605 break;
5608 if (cpe == NULL)
5609 return got_error_path(path, GOT_ERR_BAD_PATH);
5611 return NULL;
5614 static const struct got_error *
5615 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5617 int *have_staged_files = arg;
5619 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5620 *have_staged_files = 1;
5621 return got_error(GOT_ERR_CANCELLED);
5624 return NULL;
5627 static const struct got_error *
5628 check_non_staged_files(struct got_fileindex *fileindex,
5629 struct got_pathlist_head *paths)
5631 struct got_pathlist_entry *pe;
5632 struct got_fileindex_entry *ie;
5634 TAILQ_FOREACH(pe, paths, entry) {
5635 if (pe->path[0] == '\0')
5636 continue;
5637 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5638 if (ie == NULL)
5639 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5640 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5641 return got_error_path(pe->path,
5642 GOT_ERR_FILE_NOT_STAGED);
5645 return NULL;
5648 const struct got_error *
5649 got_worktree_commit(struct got_object_id **new_commit_id,
5650 struct got_worktree *worktree, struct got_pathlist_head *paths,
5651 const char *author, const char *committer, int allow_bad_symlinks,
5652 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5653 got_worktree_status_cb status_cb, void *status_arg,
5654 struct got_repository *repo)
5656 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5657 struct got_fileindex *fileindex = NULL;
5658 char *fileindex_path = NULL;
5659 struct got_pathlist_head commitable_paths;
5660 struct collect_commitables_arg cc_arg;
5661 struct got_pathlist_entry *pe;
5662 struct got_reference *head_ref = NULL;
5663 struct got_object_id *head_commit_id = NULL;
5664 int have_staged_files = 0;
5666 *new_commit_id = NULL;
5668 TAILQ_INIT(&commitable_paths);
5670 err = lock_worktree(worktree, LOCK_EX);
5671 if (err)
5672 goto done;
5674 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5675 if (err)
5676 goto done;
5678 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5679 if (err)
5680 goto done;
5682 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5683 if (err)
5684 goto done;
5686 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5687 &have_staged_files);
5688 if (err && err->code != GOT_ERR_CANCELLED)
5689 goto done;
5690 if (have_staged_files) {
5691 err = check_non_staged_files(fileindex, paths);
5692 if (err)
5693 goto done;
5696 cc_arg.commitable_paths = &commitable_paths;
5697 cc_arg.worktree = worktree;
5698 cc_arg.fileindex = fileindex;
5699 cc_arg.repo = repo;
5700 cc_arg.have_staged_files = have_staged_files;
5701 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5702 TAILQ_FOREACH(pe, paths, entry) {
5703 err = worktree_status(worktree, pe->path, fileindex, repo,
5704 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5705 if (err)
5706 goto done;
5709 if (TAILQ_EMPTY(&commitable_paths)) {
5710 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5711 goto done;
5714 TAILQ_FOREACH(pe, paths, entry) {
5715 err = check_path_is_commitable(pe->path, &commitable_paths);
5716 if (err)
5717 goto done;
5720 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5721 struct got_commitable *ct = pe->data;
5722 const char *ct_path = ct->in_repo_path;
5724 while (ct_path[0] == '/')
5725 ct_path++;
5726 err = check_out_of_date(ct_path, ct->status,
5727 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5728 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5729 if (err)
5730 goto done;
5734 err = commit_worktree(new_commit_id, &commitable_paths,
5735 head_commit_id, worktree, author, committer,
5736 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5737 if (err)
5738 goto done;
5740 err = update_fileindex_after_commit(worktree, &commitable_paths,
5741 *new_commit_id, fileindex, have_staged_files);
5742 sync_err = sync_fileindex(fileindex, fileindex_path);
5743 if (sync_err && err == NULL)
5744 err = sync_err;
5745 done:
5746 if (fileindex)
5747 got_fileindex_free(fileindex);
5748 free(fileindex_path);
5749 unlockerr = lock_worktree(worktree, LOCK_SH);
5750 if (unlockerr && err == NULL)
5751 err = unlockerr;
5752 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5753 struct got_commitable *ct = pe->data;
5754 free_commitable(ct);
5756 got_pathlist_free(&commitable_paths);
5757 return err;
5760 const char *
5761 got_commitable_get_path(struct got_commitable *ct)
5763 return ct->path;
5766 unsigned int
5767 got_commitable_get_status(struct got_commitable *ct)
5769 return ct->status;
5772 struct check_rebase_ok_arg {
5773 struct got_worktree *worktree;
5774 struct got_repository *repo;
5777 static const struct got_error *
5778 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5780 const struct got_error *err = NULL;
5781 struct check_rebase_ok_arg *a = arg;
5782 unsigned char status;
5783 struct stat sb;
5784 char *ondisk_path;
5786 /* Reject rebase of a work tree with mixed base commits. */
5787 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5788 SHA1_DIGEST_LENGTH))
5789 return got_error(GOT_ERR_MIXED_COMMITS);
5791 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5792 == -1)
5793 return got_error_from_errno("asprintf");
5795 /* Reject rebase of a work tree with modified or staged files. */
5796 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5797 free(ondisk_path);
5798 if (err)
5799 return err;
5801 if (status != GOT_STATUS_NO_CHANGE)
5802 return got_error(GOT_ERR_MODIFIED);
5803 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5804 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5806 return NULL;
5809 const struct got_error *
5810 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5811 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5812 struct got_worktree *worktree, struct got_reference *branch,
5813 struct got_repository *repo)
5815 const struct got_error *err = NULL;
5816 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5817 char *branch_ref_name = NULL;
5818 char *fileindex_path = NULL;
5819 struct check_rebase_ok_arg ok_arg;
5820 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5821 struct got_object_id *wt_branch_tip = NULL;
5823 *new_base_branch_ref = NULL;
5824 *tmp_branch = NULL;
5825 *fileindex = NULL;
5827 err = lock_worktree(worktree, LOCK_EX);
5828 if (err)
5829 return err;
5831 err = open_fileindex(fileindex, &fileindex_path, worktree);
5832 if (err)
5833 goto done;
5835 ok_arg.worktree = worktree;
5836 ok_arg.repo = repo;
5837 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5838 &ok_arg);
5839 if (err)
5840 goto done;
5842 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5843 if (err)
5844 goto done;
5846 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5847 if (err)
5848 goto done;
5850 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5851 if (err)
5852 goto done;
5854 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5855 0);
5856 if (err)
5857 goto done;
5859 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5860 if (err)
5861 goto done;
5862 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5863 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5864 goto done;
5867 err = got_ref_alloc_symref(new_base_branch_ref,
5868 new_base_branch_ref_name, wt_branch);
5869 if (err)
5870 goto done;
5871 err = got_ref_write(*new_base_branch_ref, repo);
5872 if (err)
5873 goto done;
5875 /* TODO Lock original branch's ref while rebasing? */
5877 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5878 if (err)
5879 goto done;
5881 err = got_ref_write(branch_ref, repo);
5882 if (err)
5883 goto done;
5885 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5886 worktree->base_commit_id);
5887 if (err)
5888 goto done;
5889 err = got_ref_write(*tmp_branch, repo);
5890 if (err)
5891 goto done;
5893 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5894 if (err)
5895 goto done;
5896 done:
5897 free(fileindex_path);
5898 free(tmp_branch_name);
5899 free(new_base_branch_ref_name);
5900 free(branch_ref_name);
5901 if (branch_ref)
5902 got_ref_close(branch_ref);
5903 if (wt_branch)
5904 got_ref_close(wt_branch);
5905 free(wt_branch_tip);
5906 if (err) {
5907 if (*new_base_branch_ref) {
5908 got_ref_close(*new_base_branch_ref);
5909 *new_base_branch_ref = NULL;
5911 if (*tmp_branch) {
5912 got_ref_close(*tmp_branch);
5913 *tmp_branch = NULL;
5915 if (*fileindex) {
5916 got_fileindex_free(*fileindex);
5917 *fileindex = NULL;
5919 lock_worktree(worktree, LOCK_SH);
5921 return err;
5924 const struct got_error *
5925 got_worktree_rebase_continue(struct got_object_id **commit_id,
5926 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5927 struct got_reference **branch, struct got_fileindex **fileindex,
5928 struct got_worktree *worktree, struct got_repository *repo)
5930 const struct got_error *err;
5931 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5932 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5933 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5934 char *fileindex_path = NULL;
5935 int have_staged_files = 0;
5937 *commit_id = NULL;
5938 *new_base_branch = NULL;
5939 *tmp_branch = NULL;
5940 *branch = NULL;
5941 *fileindex = NULL;
5943 err = lock_worktree(worktree, LOCK_EX);
5944 if (err)
5945 return err;
5947 err = open_fileindex(fileindex, &fileindex_path, worktree);
5948 if (err)
5949 goto done;
5951 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5952 &have_staged_files);
5953 if (err && err->code != GOT_ERR_CANCELLED)
5954 goto done;
5955 if (have_staged_files) {
5956 err = got_error(GOT_ERR_STAGED_PATHS);
5957 goto done;
5960 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5961 if (err)
5962 goto done;
5964 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5965 if (err)
5966 goto done;
5968 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5969 if (err)
5970 goto done;
5972 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5973 if (err)
5974 goto done;
5976 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5977 if (err)
5978 goto done;
5980 err = got_ref_open(branch, repo,
5981 got_ref_get_symref_target(branch_ref), 0);
5982 if (err)
5983 goto done;
5985 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5986 if (err)
5987 goto done;
5989 err = got_ref_resolve(commit_id, repo, commit_ref);
5990 if (err)
5991 goto done;
5993 err = got_ref_open(new_base_branch, repo,
5994 new_base_branch_ref_name, 0);
5995 if (err)
5996 goto done;
5998 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5999 if (err)
6000 goto done;
6001 done:
6002 free(commit_ref_name);
6003 free(branch_ref_name);
6004 free(fileindex_path);
6005 if (commit_ref)
6006 got_ref_close(commit_ref);
6007 if (branch_ref)
6008 got_ref_close(branch_ref);
6009 if (err) {
6010 free(*commit_id);
6011 *commit_id = NULL;
6012 if (*tmp_branch) {
6013 got_ref_close(*tmp_branch);
6014 *tmp_branch = NULL;
6016 if (*new_base_branch) {
6017 got_ref_close(*new_base_branch);
6018 *new_base_branch = NULL;
6020 if (*branch) {
6021 got_ref_close(*branch);
6022 *branch = NULL;
6024 if (*fileindex) {
6025 got_fileindex_free(*fileindex);
6026 *fileindex = NULL;
6028 lock_worktree(worktree, LOCK_SH);
6030 return err;
6033 const struct got_error *
6034 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6036 const struct got_error *err;
6037 char *tmp_branch_name = NULL;
6039 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6040 if (err)
6041 return err;
6043 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6044 free(tmp_branch_name);
6045 return NULL;
6048 static const struct got_error *
6049 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6050 char **logmsg, void *arg)
6052 *logmsg = arg;
6053 return NULL;
6056 static const struct got_error *
6057 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6058 const char *path, struct got_object_id *blob_id,
6059 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6060 int dirfd, const char *de_name)
6062 return NULL;
6065 struct collect_merged_paths_arg {
6066 got_worktree_checkout_cb progress_cb;
6067 void *progress_arg;
6068 struct got_pathlist_head *merged_paths;
6071 static const struct got_error *
6072 collect_merged_paths(void *arg, unsigned char status, const char *path)
6074 const struct got_error *err;
6075 struct collect_merged_paths_arg *a = arg;
6076 char *p;
6077 struct got_pathlist_entry *new;
6079 err = (*a->progress_cb)(a->progress_arg, status, path);
6080 if (err)
6081 return err;
6083 if (status != GOT_STATUS_MERGE &&
6084 status != GOT_STATUS_ADD &&
6085 status != GOT_STATUS_DELETE &&
6086 status != GOT_STATUS_CONFLICT)
6087 return NULL;
6089 p = strdup(path);
6090 if (p == NULL)
6091 return got_error_from_errno("strdup");
6093 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6094 if (err || new == NULL)
6095 free(p);
6096 return err;
6099 void
6100 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6102 struct got_pathlist_entry *pe;
6104 TAILQ_FOREACH(pe, merged_paths, entry)
6105 free((char *)pe->path);
6107 got_pathlist_free(merged_paths);
6110 static const struct got_error *
6111 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6112 int is_rebase, struct got_repository *repo)
6114 const struct got_error *err;
6115 struct got_reference *commit_ref = NULL;
6117 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6118 if (err) {
6119 if (err->code != GOT_ERR_NOT_REF)
6120 goto done;
6121 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6122 if (err)
6123 goto done;
6124 err = got_ref_write(commit_ref, repo);
6125 if (err)
6126 goto done;
6127 } else if (is_rebase) {
6128 struct got_object_id *stored_id;
6129 int cmp;
6131 err = got_ref_resolve(&stored_id, repo, commit_ref);
6132 if (err)
6133 goto done;
6134 cmp = got_object_id_cmp(commit_id, stored_id);
6135 free(stored_id);
6136 if (cmp != 0) {
6137 err = got_error(GOT_ERR_REBASE_COMMITID);
6138 goto done;
6141 done:
6142 if (commit_ref)
6143 got_ref_close(commit_ref);
6144 return err;
6147 static const struct got_error *
6148 rebase_merge_files(struct got_pathlist_head *merged_paths,
6149 const char *commit_ref_name, struct got_worktree *worktree,
6150 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6151 struct got_object_id *commit_id, struct got_repository *repo,
6152 got_worktree_checkout_cb progress_cb, void *progress_arg,
6153 got_cancel_cb cancel_cb, void *cancel_arg)
6155 const struct got_error *err;
6156 struct got_reference *commit_ref = NULL;
6157 struct collect_merged_paths_arg cmp_arg;
6158 char *fileindex_path;
6160 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6162 err = get_fileindex_path(&fileindex_path, worktree);
6163 if (err)
6164 return err;
6166 cmp_arg.progress_cb = progress_cb;
6167 cmp_arg.progress_arg = progress_arg;
6168 cmp_arg.merged_paths = merged_paths;
6169 err = merge_files(worktree, fileindex, fileindex_path,
6170 parent_commit_id, commit_id, repo, collect_merged_paths,
6171 &cmp_arg, cancel_cb, cancel_arg);
6172 if (commit_ref)
6173 got_ref_close(commit_ref);
6174 return err;
6177 const struct got_error *
6178 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6179 struct got_worktree *worktree, struct got_fileindex *fileindex,
6180 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6181 struct got_repository *repo,
6182 got_worktree_checkout_cb progress_cb, void *progress_arg,
6183 got_cancel_cb cancel_cb, void *cancel_arg)
6185 const struct got_error *err;
6186 char *commit_ref_name;
6188 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6189 if (err)
6190 return err;
6192 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6193 if (err)
6194 goto done;
6196 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6197 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6198 progress_arg, cancel_cb, cancel_arg);
6199 done:
6200 free(commit_ref_name);
6201 return err;
6204 const struct got_error *
6205 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6206 struct got_worktree *worktree, struct got_fileindex *fileindex,
6207 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6208 struct got_repository *repo,
6209 got_worktree_checkout_cb progress_cb, void *progress_arg,
6210 got_cancel_cb cancel_cb, void *cancel_arg)
6212 const struct got_error *err;
6213 char *commit_ref_name;
6215 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6216 if (err)
6217 return err;
6219 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6220 if (err)
6221 goto done;
6223 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6224 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6225 progress_arg, cancel_cb, cancel_arg);
6226 done:
6227 free(commit_ref_name);
6228 return err;
6231 static const struct got_error *
6232 rebase_commit(struct got_object_id **new_commit_id,
6233 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6234 struct got_worktree *worktree, struct got_fileindex *fileindex,
6235 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6236 const char *new_logmsg, struct got_repository *repo)
6238 const struct got_error *err, *sync_err;
6239 struct got_pathlist_head commitable_paths;
6240 struct collect_commitables_arg cc_arg;
6241 char *fileindex_path = NULL;
6242 struct got_reference *head_ref = NULL;
6243 struct got_object_id *head_commit_id = NULL;
6244 char *logmsg = NULL;
6246 TAILQ_INIT(&commitable_paths);
6247 *new_commit_id = NULL;
6249 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6251 err = get_fileindex_path(&fileindex_path, worktree);
6252 if (err)
6253 return err;
6255 cc_arg.commitable_paths = &commitable_paths;
6256 cc_arg.worktree = worktree;
6257 cc_arg.repo = repo;
6258 cc_arg.have_staged_files = 0;
6260 * If possible get the status of individual files directly to
6261 * avoid crawling the entire work tree once per rebased commit.
6262 * TODO: Ideally, merged_paths would contain a list of commitables
6263 * we could use so we could skip worktree_status() entirely.
6265 if (merged_paths) {
6266 struct got_pathlist_entry *pe;
6267 TAILQ_FOREACH(pe, merged_paths, entry) {
6268 err = worktree_status(worktree, pe->path, fileindex,
6269 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6270 0);
6271 if (err)
6272 goto done;
6274 } else {
6275 err = worktree_status(worktree, "", fileindex, repo,
6276 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6277 if (err)
6278 goto done;
6281 if (TAILQ_EMPTY(&commitable_paths)) {
6282 /* No-op change; commit will be elided. */
6283 err = got_ref_delete(commit_ref, repo);
6284 if (err)
6285 goto done;
6286 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6287 goto done;
6290 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6291 if (err)
6292 goto done;
6294 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6295 if (err)
6296 goto done;
6298 if (new_logmsg) {
6299 logmsg = strdup(new_logmsg);
6300 if (logmsg == NULL) {
6301 err = got_error_from_errno("strdup");
6302 goto done;
6304 } else {
6305 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6306 if (err)
6307 goto done;
6310 /* NB: commit_worktree will call free(logmsg) */
6311 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6312 worktree, got_object_commit_get_author(orig_commit),
6313 got_object_commit_get_committer(orig_commit),
6314 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6315 if (err)
6316 goto done;
6318 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6319 if (err)
6320 goto done;
6322 err = got_ref_delete(commit_ref, repo);
6323 if (err)
6324 goto done;
6326 err = update_fileindex_after_commit(worktree, &commitable_paths,
6327 *new_commit_id, fileindex, 0);
6328 sync_err = sync_fileindex(fileindex, fileindex_path);
6329 if (sync_err && err == NULL)
6330 err = sync_err;
6331 done:
6332 free(fileindex_path);
6333 free(head_commit_id);
6334 if (head_ref)
6335 got_ref_close(head_ref);
6336 if (err) {
6337 free(*new_commit_id);
6338 *new_commit_id = NULL;
6340 return err;
6343 const struct got_error *
6344 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6345 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6346 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6347 struct got_commit_object *orig_commit,
6348 struct got_object_id *orig_commit_id, struct got_repository *repo)
6350 const struct got_error *err;
6351 char *commit_ref_name;
6352 struct got_reference *commit_ref = NULL;
6353 struct got_object_id *commit_id = NULL;
6355 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6356 if (err)
6357 return err;
6359 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6360 if (err)
6361 goto done;
6362 err = got_ref_resolve(&commit_id, repo, commit_ref);
6363 if (err)
6364 goto done;
6365 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6366 err = got_error(GOT_ERR_REBASE_COMMITID);
6367 goto done;
6370 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6371 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6372 done:
6373 if (commit_ref)
6374 got_ref_close(commit_ref);
6375 free(commit_ref_name);
6376 free(commit_id);
6377 return err;
6380 const struct got_error *
6381 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6382 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6383 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6384 struct got_commit_object *orig_commit,
6385 struct got_object_id *orig_commit_id, const char *new_logmsg,
6386 struct got_repository *repo)
6388 const struct got_error *err;
6389 char *commit_ref_name;
6390 struct got_reference *commit_ref = NULL;
6392 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6393 if (err)
6394 return err;
6396 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6397 if (err)
6398 goto done;
6400 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6401 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6402 done:
6403 if (commit_ref)
6404 got_ref_close(commit_ref);
6405 free(commit_ref_name);
6406 return err;
6409 const struct got_error *
6410 got_worktree_rebase_postpone(struct got_worktree *worktree,
6411 struct got_fileindex *fileindex)
6413 if (fileindex)
6414 got_fileindex_free(fileindex);
6415 return lock_worktree(worktree, LOCK_SH);
6418 static const struct got_error *
6419 delete_ref(const char *name, struct got_repository *repo)
6421 const struct got_error *err;
6422 struct got_reference *ref;
6424 err = got_ref_open(&ref, repo, name, 0);
6425 if (err) {
6426 if (err->code == GOT_ERR_NOT_REF)
6427 return NULL;
6428 return err;
6431 err = got_ref_delete(ref, repo);
6432 got_ref_close(ref);
6433 return err;
6436 static const struct got_error *
6437 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6439 const struct got_error *err;
6440 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6441 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6443 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6444 if (err)
6445 goto done;
6446 err = delete_ref(tmp_branch_name, repo);
6447 if (err)
6448 goto done;
6450 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6451 if (err)
6452 goto done;
6453 err = delete_ref(new_base_branch_ref_name, repo);
6454 if (err)
6455 goto done;
6457 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6458 if (err)
6459 goto done;
6460 err = delete_ref(branch_ref_name, repo);
6461 if (err)
6462 goto done;
6464 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6465 if (err)
6466 goto done;
6467 err = delete_ref(commit_ref_name, repo);
6468 if (err)
6469 goto done;
6471 done:
6472 free(tmp_branch_name);
6473 free(new_base_branch_ref_name);
6474 free(branch_ref_name);
6475 free(commit_ref_name);
6476 return err;
6479 const struct got_error *
6480 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6481 struct got_object_id *new_commit_id, struct got_repository *repo)
6483 const struct got_error *err;
6484 struct got_reference *ref = NULL;
6485 struct got_object_id *old_commit_id = NULL;
6486 const char *branch_name = NULL;
6487 char *new_id_str = NULL;
6488 char *refname = NULL;
6490 branch_name = got_ref_get_name(branch);
6491 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6492 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6493 branch_name += 11;
6495 err = got_object_id_str(&new_id_str, new_commit_id);
6496 if (err)
6497 return err;
6499 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6500 new_id_str) == -1) {
6501 err = got_error_from_errno("asprintf");
6502 goto done;
6505 err = got_ref_resolve(&old_commit_id, repo, branch);
6506 if (err)
6507 goto done;
6509 err = got_ref_alloc(&ref, refname, old_commit_id);
6510 if (err)
6511 goto done;
6513 err = got_ref_write(ref, repo);
6514 done:
6515 free(new_id_str);
6516 free(refname);
6517 free(old_commit_id);
6518 if (ref)
6519 got_ref_close(ref);
6520 return err;
6523 const struct got_error *
6524 got_worktree_rebase_complete(struct got_worktree *worktree,
6525 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6526 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6527 struct got_repository *repo, int create_backup)
6529 const struct got_error *err, *unlockerr, *sync_err;
6530 struct got_object_id *new_head_commit_id = NULL;
6531 char *fileindex_path = NULL;
6533 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6534 if (err)
6535 return err;
6537 if (create_backup) {
6538 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6539 rebased_branch, new_head_commit_id, repo);
6540 if (err)
6541 goto done;
6544 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6545 if (err)
6546 goto done;
6548 err = got_ref_write(rebased_branch, repo);
6549 if (err)
6550 goto done;
6552 err = got_worktree_set_head_ref(worktree, rebased_branch);
6553 if (err)
6554 goto done;
6556 err = delete_rebase_refs(worktree, repo);
6557 if (err)
6558 goto done;
6560 err = get_fileindex_path(&fileindex_path, worktree);
6561 if (err)
6562 goto done;
6563 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6564 sync_err = sync_fileindex(fileindex, fileindex_path);
6565 if (sync_err && err == NULL)
6566 err = sync_err;
6567 done:
6568 got_fileindex_free(fileindex);
6569 free(fileindex_path);
6570 free(new_head_commit_id);
6571 unlockerr = lock_worktree(worktree, LOCK_SH);
6572 if (unlockerr && err == NULL)
6573 err = unlockerr;
6574 return err;
6577 const struct got_error *
6578 got_worktree_rebase_abort(struct got_worktree *worktree,
6579 struct got_fileindex *fileindex, struct got_repository *repo,
6580 struct got_reference *new_base_branch,
6581 got_worktree_checkout_cb progress_cb, void *progress_arg)
6583 const struct got_error *err, *unlockerr, *sync_err;
6584 struct got_reference *resolved = NULL;
6585 struct got_object_id *commit_id = NULL;
6586 char *fileindex_path = NULL;
6587 struct revert_file_args rfa;
6588 struct got_object_id *tree_id = NULL;
6590 err = lock_worktree(worktree, LOCK_EX);
6591 if (err)
6592 return err;
6594 err = got_ref_open(&resolved, repo,
6595 got_ref_get_symref_target(new_base_branch), 0);
6596 if (err)
6597 goto done;
6599 err = got_worktree_set_head_ref(worktree, resolved);
6600 if (err)
6601 goto done;
6604 * XXX commits to the base branch could have happened while
6605 * we were busy rebasing; should we store the original commit ID
6606 * when rebase begins and read it back here?
6608 err = got_ref_resolve(&commit_id, repo, resolved);
6609 if (err)
6610 goto done;
6612 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6613 if (err)
6614 goto done;
6616 err = got_object_id_by_path(&tree_id, repo,
6617 worktree->base_commit_id, worktree->path_prefix);
6618 if (err)
6619 goto done;
6621 err = delete_rebase_refs(worktree, repo);
6622 if (err)
6623 goto done;
6625 err = get_fileindex_path(&fileindex_path, worktree);
6626 if (err)
6627 goto done;
6629 rfa.worktree = worktree;
6630 rfa.fileindex = fileindex;
6631 rfa.progress_cb = progress_cb;
6632 rfa.progress_arg = progress_arg;
6633 rfa.patch_cb = NULL;
6634 rfa.patch_arg = NULL;
6635 rfa.repo = repo;
6636 err = worktree_status(worktree, "", fileindex, repo,
6637 revert_file, &rfa, NULL, NULL, 0, 0);
6638 if (err)
6639 goto sync;
6641 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6642 repo, progress_cb, progress_arg, NULL, NULL);
6643 sync:
6644 sync_err = sync_fileindex(fileindex, fileindex_path);
6645 if (sync_err && err == NULL)
6646 err = sync_err;
6647 done:
6648 got_ref_close(resolved);
6649 free(tree_id);
6650 free(commit_id);
6651 if (fileindex)
6652 got_fileindex_free(fileindex);
6653 free(fileindex_path);
6655 unlockerr = lock_worktree(worktree, LOCK_SH);
6656 if (unlockerr && err == NULL)
6657 err = unlockerr;
6658 return err;
6661 const struct got_error *
6662 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6663 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6664 struct got_fileindex **fileindex, struct got_worktree *worktree,
6665 struct got_repository *repo)
6667 const struct got_error *err = NULL;
6668 char *tmp_branch_name = NULL;
6669 char *branch_ref_name = NULL;
6670 char *base_commit_ref_name = NULL;
6671 char *fileindex_path = NULL;
6672 struct check_rebase_ok_arg ok_arg;
6673 struct got_reference *wt_branch = NULL;
6674 struct got_reference *base_commit_ref = NULL;
6676 *tmp_branch = NULL;
6677 *branch_ref = NULL;
6678 *base_commit_id = NULL;
6679 *fileindex = NULL;
6681 err = lock_worktree(worktree, LOCK_EX);
6682 if (err)
6683 return err;
6685 err = open_fileindex(fileindex, &fileindex_path, worktree);
6686 if (err)
6687 goto done;
6689 ok_arg.worktree = worktree;
6690 ok_arg.repo = repo;
6691 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6692 &ok_arg);
6693 if (err)
6694 goto done;
6696 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6697 if (err)
6698 goto done;
6700 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6701 if (err)
6702 goto done;
6704 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6705 worktree);
6706 if (err)
6707 goto done;
6709 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6710 0);
6711 if (err)
6712 goto done;
6714 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6715 if (err)
6716 goto done;
6718 err = got_ref_write(*branch_ref, repo);
6719 if (err)
6720 goto done;
6722 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6723 worktree->base_commit_id);
6724 if (err)
6725 goto done;
6726 err = got_ref_write(base_commit_ref, repo);
6727 if (err)
6728 goto done;
6729 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6730 if (*base_commit_id == NULL) {
6731 err = got_error_from_errno("got_object_id_dup");
6732 goto done;
6735 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6736 worktree->base_commit_id);
6737 if (err)
6738 goto done;
6739 err = got_ref_write(*tmp_branch, repo);
6740 if (err)
6741 goto done;
6743 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6744 if (err)
6745 goto done;
6746 done:
6747 free(fileindex_path);
6748 free(tmp_branch_name);
6749 free(branch_ref_name);
6750 free(base_commit_ref_name);
6751 if (wt_branch)
6752 got_ref_close(wt_branch);
6753 if (err) {
6754 if (*branch_ref) {
6755 got_ref_close(*branch_ref);
6756 *branch_ref = NULL;
6758 if (*tmp_branch) {
6759 got_ref_close(*tmp_branch);
6760 *tmp_branch = NULL;
6762 free(*base_commit_id);
6763 if (*fileindex) {
6764 got_fileindex_free(*fileindex);
6765 *fileindex = NULL;
6767 lock_worktree(worktree, LOCK_SH);
6769 return err;
6772 const struct got_error *
6773 got_worktree_histedit_postpone(struct got_worktree *worktree,
6774 struct got_fileindex *fileindex)
6776 if (fileindex)
6777 got_fileindex_free(fileindex);
6778 return lock_worktree(worktree, LOCK_SH);
6781 const struct got_error *
6782 got_worktree_histedit_in_progress(int *in_progress,
6783 struct got_worktree *worktree)
6785 const struct got_error *err;
6786 char *tmp_branch_name = NULL;
6788 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6789 if (err)
6790 return err;
6792 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6793 free(tmp_branch_name);
6794 return NULL;
6797 const struct got_error *
6798 got_worktree_histedit_continue(struct got_object_id **commit_id,
6799 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6800 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6801 struct got_worktree *worktree, struct got_repository *repo)
6803 const struct got_error *err;
6804 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6805 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6806 struct got_reference *commit_ref = NULL;
6807 struct got_reference *base_commit_ref = NULL;
6808 char *fileindex_path = NULL;
6809 int have_staged_files = 0;
6811 *commit_id = NULL;
6812 *tmp_branch = NULL;
6813 *base_commit_id = NULL;
6814 *fileindex = NULL;
6816 err = lock_worktree(worktree, LOCK_EX);
6817 if (err)
6818 return err;
6820 err = open_fileindex(fileindex, &fileindex_path, worktree);
6821 if (err)
6822 goto done;
6824 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6825 &have_staged_files);
6826 if (err && err->code != GOT_ERR_CANCELLED)
6827 goto done;
6828 if (have_staged_files) {
6829 err = got_error(GOT_ERR_STAGED_PATHS);
6830 goto done;
6833 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6834 if (err)
6835 goto done;
6837 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6838 if (err)
6839 goto done;
6841 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6842 if (err)
6843 goto done;
6845 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6846 worktree);
6847 if (err)
6848 goto done;
6850 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6851 if (err)
6852 goto done;
6854 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6855 if (err)
6856 goto done;
6857 err = got_ref_resolve(commit_id, repo, commit_ref);
6858 if (err)
6859 goto done;
6861 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6862 if (err)
6863 goto done;
6864 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6865 if (err)
6866 goto done;
6868 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6869 if (err)
6870 goto done;
6871 done:
6872 free(commit_ref_name);
6873 free(branch_ref_name);
6874 free(fileindex_path);
6875 if (commit_ref)
6876 got_ref_close(commit_ref);
6877 if (base_commit_ref)
6878 got_ref_close(base_commit_ref);
6879 if (err) {
6880 free(*commit_id);
6881 *commit_id = NULL;
6882 free(*base_commit_id);
6883 *base_commit_id = NULL;
6884 if (*tmp_branch) {
6885 got_ref_close(*tmp_branch);
6886 *tmp_branch = NULL;
6888 if (*fileindex) {
6889 got_fileindex_free(*fileindex);
6890 *fileindex = NULL;
6892 lock_worktree(worktree, LOCK_EX);
6894 return err;
6897 static const struct got_error *
6898 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6900 const struct got_error *err;
6901 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6902 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6904 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6905 if (err)
6906 goto done;
6907 err = delete_ref(tmp_branch_name, repo);
6908 if (err)
6909 goto done;
6911 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6912 worktree);
6913 if (err)
6914 goto done;
6915 err = delete_ref(base_commit_ref_name, repo);
6916 if (err)
6917 goto done;
6919 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6920 if (err)
6921 goto done;
6922 err = delete_ref(branch_ref_name, repo);
6923 if (err)
6924 goto done;
6926 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6927 if (err)
6928 goto done;
6929 err = delete_ref(commit_ref_name, repo);
6930 if (err)
6931 goto done;
6932 done:
6933 free(tmp_branch_name);
6934 free(base_commit_ref_name);
6935 free(branch_ref_name);
6936 free(commit_ref_name);
6937 return err;
6940 const struct got_error *
6941 got_worktree_histedit_abort(struct got_worktree *worktree,
6942 struct got_fileindex *fileindex, struct got_repository *repo,
6943 struct got_reference *branch, struct got_object_id *base_commit_id,
6944 got_worktree_checkout_cb progress_cb, void *progress_arg)
6946 const struct got_error *err, *unlockerr, *sync_err;
6947 struct got_reference *resolved = NULL;
6948 char *fileindex_path = NULL;
6949 struct got_object_id *tree_id = NULL;
6950 struct revert_file_args rfa;
6952 err = lock_worktree(worktree, LOCK_EX);
6953 if (err)
6954 return err;
6956 err = got_ref_open(&resolved, repo,
6957 got_ref_get_symref_target(branch), 0);
6958 if (err)
6959 goto done;
6961 err = got_worktree_set_head_ref(worktree, resolved);
6962 if (err)
6963 goto done;
6965 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6966 if (err)
6967 goto done;
6969 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6970 worktree->path_prefix);
6971 if (err)
6972 goto done;
6974 err = delete_histedit_refs(worktree, repo);
6975 if (err)
6976 goto done;
6978 err = get_fileindex_path(&fileindex_path, worktree);
6979 if (err)
6980 goto done;
6982 rfa.worktree = worktree;
6983 rfa.fileindex = fileindex;
6984 rfa.progress_cb = progress_cb;
6985 rfa.progress_arg = progress_arg;
6986 rfa.patch_cb = NULL;
6987 rfa.patch_arg = NULL;
6988 rfa.repo = repo;
6989 err = worktree_status(worktree, "", fileindex, repo,
6990 revert_file, &rfa, NULL, NULL, 0, 0);
6991 if (err)
6992 goto sync;
6994 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6995 repo, progress_cb, progress_arg, NULL, NULL);
6996 sync:
6997 sync_err = sync_fileindex(fileindex, fileindex_path);
6998 if (sync_err && err == NULL)
6999 err = sync_err;
7000 done:
7001 got_ref_close(resolved);
7002 free(tree_id);
7003 free(fileindex_path);
7005 unlockerr = lock_worktree(worktree, LOCK_SH);
7006 if (unlockerr && err == NULL)
7007 err = unlockerr;
7008 return err;
7011 const struct got_error *
7012 got_worktree_histedit_complete(struct got_worktree *worktree,
7013 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7014 struct got_reference *edited_branch, struct got_repository *repo)
7016 const struct got_error *err, *unlockerr, *sync_err;
7017 struct got_object_id *new_head_commit_id = NULL;
7018 struct got_reference *resolved = NULL;
7019 char *fileindex_path = NULL;
7021 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7022 if (err)
7023 return err;
7025 err = got_ref_open(&resolved, repo,
7026 got_ref_get_symref_target(edited_branch), 0);
7027 if (err)
7028 goto done;
7030 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7031 resolved, new_head_commit_id, repo);
7032 if (err)
7033 goto done;
7035 err = got_ref_change_ref(resolved, new_head_commit_id);
7036 if (err)
7037 goto done;
7039 err = got_ref_write(resolved, repo);
7040 if (err)
7041 goto done;
7043 err = got_worktree_set_head_ref(worktree, resolved);
7044 if (err)
7045 goto done;
7047 err = delete_histedit_refs(worktree, repo);
7048 if (err)
7049 goto done;
7051 err = get_fileindex_path(&fileindex_path, worktree);
7052 if (err)
7053 goto done;
7054 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7055 sync_err = sync_fileindex(fileindex, fileindex_path);
7056 if (sync_err && err == NULL)
7057 err = sync_err;
7058 done:
7059 got_fileindex_free(fileindex);
7060 free(fileindex_path);
7061 free(new_head_commit_id);
7062 unlockerr = lock_worktree(worktree, LOCK_SH);
7063 if (unlockerr && err == NULL)
7064 err = unlockerr;
7065 return err;
7068 const struct got_error *
7069 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7070 struct got_object_id *commit_id, struct got_repository *repo)
7072 const struct got_error *err;
7073 char *commit_ref_name;
7075 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7076 if (err)
7077 return err;
7079 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7080 if (err)
7081 goto done;
7083 err = delete_ref(commit_ref_name, repo);
7084 done:
7085 free(commit_ref_name);
7086 return err;
7089 const struct got_error *
7090 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7091 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7092 struct got_worktree *worktree, const char *refname,
7093 struct got_repository *repo)
7095 const struct got_error *err = NULL;
7096 char *fileindex_path = NULL;
7097 struct check_rebase_ok_arg ok_arg;
7099 *fileindex = NULL;
7100 *branch_ref = NULL;
7101 *base_branch_ref = NULL;
7103 err = lock_worktree(worktree, LOCK_EX);
7104 if (err)
7105 return err;
7107 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7108 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7109 "cannot integrate a branch into itself; "
7110 "update -b or different branch name required");
7111 goto done;
7114 err = open_fileindex(fileindex, &fileindex_path, worktree);
7115 if (err)
7116 goto done;
7118 /* Preconditions are the same as for rebase. */
7119 ok_arg.worktree = worktree;
7120 ok_arg.repo = repo;
7121 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7122 &ok_arg);
7123 if (err)
7124 goto done;
7126 err = got_ref_open(branch_ref, repo, refname, 1);
7127 if (err)
7128 goto done;
7130 err = got_ref_open(base_branch_ref, repo,
7131 got_worktree_get_head_ref_name(worktree), 1);
7132 done:
7133 if (err) {
7134 if (*branch_ref) {
7135 got_ref_close(*branch_ref);
7136 *branch_ref = NULL;
7138 if (*base_branch_ref) {
7139 got_ref_close(*base_branch_ref);
7140 *base_branch_ref = NULL;
7142 if (*fileindex) {
7143 got_fileindex_free(*fileindex);
7144 *fileindex = NULL;
7146 lock_worktree(worktree, LOCK_SH);
7148 return err;
7151 const struct got_error *
7152 got_worktree_integrate_continue(struct got_worktree *worktree,
7153 struct got_fileindex *fileindex, struct got_repository *repo,
7154 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7155 got_worktree_checkout_cb progress_cb, void *progress_arg,
7156 got_cancel_cb cancel_cb, void *cancel_arg)
7158 const struct got_error *err = NULL, *sync_err, *unlockerr;
7159 char *fileindex_path = NULL;
7160 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7162 err = get_fileindex_path(&fileindex_path, worktree);
7163 if (err)
7164 goto done;
7166 err = got_ref_resolve(&commit_id, repo, branch_ref);
7167 if (err)
7168 goto done;
7170 err = got_object_id_by_path(&tree_id, repo, commit_id,
7171 worktree->path_prefix);
7172 if (err)
7173 goto done;
7175 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7176 if (err)
7177 goto done;
7179 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7180 progress_cb, progress_arg, cancel_cb, cancel_arg);
7181 if (err)
7182 goto sync;
7184 err = got_ref_change_ref(base_branch_ref, commit_id);
7185 if (err)
7186 goto sync;
7188 err = got_ref_write(base_branch_ref, repo);
7189 if (err)
7190 goto sync;
7192 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7193 sync:
7194 sync_err = sync_fileindex(fileindex, fileindex_path);
7195 if (sync_err && err == NULL)
7196 err = sync_err;
7198 done:
7199 unlockerr = got_ref_unlock(branch_ref);
7200 if (unlockerr && err == NULL)
7201 err = unlockerr;
7202 got_ref_close(branch_ref);
7204 unlockerr = got_ref_unlock(base_branch_ref);
7205 if (unlockerr && err == NULL)
7206 err = unlockerr;
7207 got_ref_close(base_branch_ref);
7209 got_fileindex_free(fileindex);
7210 free(fileindex_path);
7211 free(tree_id);
7213 unlockerr = lock_worktree(worktree, LOCK_SH);
7214 if (unlockerr && err == NULL)
7215 err = unlockerr;
7216 return err;
7219 const struct got_error *
7220 got_worktree_integrate_abort(struct got_worktree *worktree,
7221 struct got_fileindex *fileindex, struct got_repository *repo,
7222 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7224 const struct got_error *err = NULL, *unlockerr = NULL;
7226 got_fileindex_free(fileindex);
7228 err = lock_worktree(worktree, LOCK_SH);
7230 unlockerr = got_ref_unlock(branch_ref);
7231 if (unlockerr && err == NULL)
7232 err = unlockerr;
7233 got_ref_close(branch_ref);
7235 unlockerr = got_ref_unlock(base_branch_ref);
7236 if (unlockerr && err == NULL)
7237 err = unlockerr;
7238 got_ref_close(base_branch_ref);
7240 return err;
7243 struct check_stage_ok_arg {
7244 struct got_object_id *head_commit_id;
7245 struct got_worktree *worktree;
7246 struct got_fileindex *fileindex;
7247 struct got_repository *repo;
7248 int have_changes;
7251 const struct got_error *
7252 check_stage_ok(void *arg, unsigned char status,
7253 unsigned char staged_status, const char *relpath,
7254 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7255 struct got_object_id *commit_id, int dirfd, const char *de_name)
7257 struct check_stage_ok_arg *a = arg;
7258 const struct got_error *err = NULL;
7259 struct got_fileindex_entry *ie;
7260 struct got_object_id base_commit_id;
7261 struct got_object_id *base_commit_idp = NULL;
7262 char *in_repo_path = NULL, *p;
7264 if (status == GOT_STATUS_UNVERSIONED ||
7265 status == GOT_STATUS_NO_CHANGE)
7266 return NULL;
7267 if (status == GOT_STATUS_NONEXISTENT)
7268 return got_error_set_errno(ENOENT, relpath);
7270 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7271 if (ie == NULL)
7272 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7274 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7275 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7276 relpath) == -1)
7277 return got_error_from_errno("asprintf");
7279 if (got_fileindex_entry_has_commit(ie)) {
7280 memcpy(base_commit_id.sha1, ie->commit_sha1,
7281 SHA1_DIGEST_LENGTH);
7282 base_commit_idp = &base_commit_id;
7285 if (status == GOT_STATUS_CONFLICT) {
7286 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7287 goto done;
7288 } else if (status != GOT_STATUS_ADD &&
7289 status != GOT_STATUS_MODIFY &&
7290 status != GOT_STATUS_DELETE) {
7291 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7292 goto done;
7295 a->have_changes = 1;
7297 p = in_repo_path;
7298 while (p[0] == '/')
7299 p++;
7300 err = check_out_of_date(p, status, staged_status,
7301 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7302 GOT_ERR_STAGE_OUT_OF_DATE);
7303 done:
7304 free(in_repo_path);
7305 return err;
7308 struct stage_path_arg {
7309 struct got_worktree *worktree;
7310 struct got_fileindex *fileindex;
7311 struct got_repository *repo;
7312 got_worktree_status_cb status_cb;
7313 void *status_arg;
7314 got_worktree_patch_cb patch_cb;
7315 void *patch_arg;
7316 int staged_something;
7317 int allow_bad_symlinks;
7320 static const struct got_error *
7321 stage_path(void *arg, unsigned char status,
7322 unsigned char staged_status, const char *relpath,
7323 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7324 struct got_object_id *commit_id, int dirfd, const char *de_name)
7326 struct stage_path_arg *a = arg;
7327 const struct got_error *err = NULL;
7328 struct got_fileindex_entry *ie;
7329 char *ondisk_path = NULL, *path_content = NULL;
7330 uint32_t stage;
7331 struct got_object_id *new_staged_blob_id = NULL;
7332 struct stat sb;
7334 if (status == GOT_STATUS_UNVERSIONED)
7335 return NULL;
7337 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7338 if (ie == NULL)
7339 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7341 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7342 relpath)== -1)
7343 return got_error_from_errno("asprintf");
7345 switch (status) {
7346 case GOT_STATUS_ADD:
7347 case GOT_STATUS_MODIFY:
7348 /* XXX could sb.st_mode be passed in by our caller? */
7349 if (lstat(ondisk_path, &sb) == -1) {
7350 err = got_error_from_errno2("lstat", ondisk_path);
7351 break;
7353 if (a->patch_cb) {
7354 if (status == GOT_STATUS_ADD) {
7355 int choice = GOT_PATCH_CHOICE_NONE;
7356 err = (*a->patch_cb)(&choice, a->patch_arg,
7357 status, ie->path, NULL, 1, 1);
7358 if (err)
7359 break;
7360 if (choice != GOT_PATCH_CHOICE_YES)
7361 break;
7362 } else {
7363 err = create_patched_content(&path_content, 0,
7364 staged_blob_id ? staged_blob_id : blob_id,
7365 ondisk_path, dirfd, de_name, ie->path,
7366 a->repo, a->patch_cb, a->patch_arg);
7367 if (err || path_content == NULL)
7368 break;
7371 err = got_object_blob_create(&new_staged_blob_id,
7372 path_content ? path_content : ondisk_path, a->repo);
7373 if (err)
7374 break;
7375 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7376 SHA1_DIGEST_LENGTH);
7377 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7378 stage = GOT_FILEIDX_STAGE_ADD;
7379 else
7380 stage = GOT_FILEIDX_STAGE_MODIFY;
7381 got_fileindex_entry_stage_set(ie, stage);
7382 if (S_ISLNK(sb.st_mode)) {
7383 int is_bad_symlink = 0;
7384 if (!a->allow_bad_symlinks) {
7385 char target_path[PATH_MAX];
7386 ssize_t target_len;
7387 target_len = readlink(ondisk_path, target_path,
7388 sizeof(target_path));
7389 if (target_len == -1) {
7390 err = got_error_from_errno2("readlink",
7391 ondisk_path);
7392 break;
7394 err = is_bad_symlink_target(&is_bad_symlink,
7395 target_path, target_len, ondisk_path,
7396 a->worktree->root_path);
7397 if (err)
7398 break;
7399 if (is_bad_symlink) {
7400 err = got_error_path(ondisk_path,
7401 GOT_ERR_BAD_SYMLINK);
7402 break;
7405 if (is_bad_symlink)
7406 got_fileindex_entry_staged_filetype_set(ie,
7407 GOT_FILEIDX_MODE_BAD_SYMLINK);
7408 else
7409 got_fileindex_entry_staged_filetype_set(ie,
7410 GOT_FILEIDX_MODE_SYMLINK);
7411 } else {
7412 got_fileindex_entry_staged_filetype_set(ie,
7413 GOT_FILEIDX_MODE_REGULAR_FILE);
7415 a->staged_something = 1;
7416 if (a->status_cb == NULL)
7417 break;
7418 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7419 get_staged_status(ie), relpath, blob_id,
7420 new_staged_blob_id, NULL, dirfd, de_name);
7421 break;
7422 case GOT_STATUS_DELETE:
7423 if (staged_status == GOT_STATUS_DELETE)
7424 break;
7425 if (a->patch_cb) {
7426 int choice = GOT_PATCH_CHOICE_NONE;
7427 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7428 ie->path, NULL, 1, 1);
7429 if (err)
7430 break;
7431 if (choice == GOT_PATCH_CHOICE_NO)
7432 break;
7433 if (choice != GOT_PATCH_CHOICE_YES) {
7434 err = got_error(GOT_ERR_PATCH_CHOICE);
7435 break;
7438 stage = GOT_FILEIDX_STAGE_DELETE;
7439 got_fileindex_entry_stage_set(ie, stage);
7440 a->staged_something = 1;
7441 if (a->status_cb == NULL)
7442 break;
7443 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7444 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7445 de_name);
7446 break;
7447 case GOT_STATUS_NO_CHANGE:
7448 break;
7449 case GOT_STATUS_CONFLICT:
7450 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7451 break;
7452 case GOT_STATUS_NONEXISTENT:
7453 err = got_error_set_errno(ENOENT, relpath);
7454 break;
7455 default:
7456 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7457 break;
7460 if (path_content && unlink(path_content) == -1 && err == NULL)
7461 err = got_error_from_errno2("unlink", path_content);
7462 free(path_content);
7463 free(ondisk_path);
7464 free(new_staged_blob_id);
7465 return err;
7468 const struct got_error *
7469 got_worktree_stage(struct got_worktree *worktree,
7470 struct got_pathlist_head *paths,
7471 got_worktree_status_cb status_cb, void *status_arg,
7472 got_worktree_patch_cb patch_cb, void *patch_arg,
7473 int allow_bad_symlinks, struct got_repository *repo)
7475 const struct got_error *err = NULL, *sync_err, *unlockerr;
7476 struct got_pathlist_entry *pe;
7477 struct got_fileindex *fileindex = NULL;
7478 char *fileindex_path = NULL;
7479 struct got_reference *head_ref = NULL;
7480 struct got_object_id *head_commit_id = NULL;
7481 struct check_stage_ok_arg oka;
7482 struct stage_path_arg spa;
7484 err = lock_worktree(worktree, LOCK_EX);
7485 if (err)
7486 return err;
7488 err = got_ref_open(&head_ref, repo,
7489 got_worktree_get_head_ref_name(worktree), 0);
7490 if (err)
7491 goto done;
7492 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7493 if (err)
7494 goto done;
7495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7496 if (err)
7497 goto done;
7499 /* Check pre-conditions before staging anything. */
7500 oka.head_commit_id = head_commit_id;
7501 oka.worktree = worktree;
7502 oka.fileindex = fileindex;
7503 oka.repo = repo;
7504 oka.have_changes = 0;
7505 TAILQ_FOREACH(pe, paths, entry) {
7506 err = worktree_status(worktree, pe->path, fileindex, repo,
7507 check_stage_ok, &oka, NULL, NULL, 0, 0);
7508 if (err)
7509 goto done;
7511 if (!oka.have_changes) {
7512 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7513 goto done;
7516 spa.worktree = worktree;
7517 spa.fileindex = fileindex;
7518 spa.repo = repo;
7519 spa.patch_cb = patch_cb;
7520 spa.patch_arg = patch_arg;
7521 spa.status_cb = status_cb;
7522 spa.status_arg = status_arg;
7523 spa.staged_something = 0;
7524 spa.allow_bad_symlinks = allow_bad_symlinks;
7525 TAILQ_FOREACH(pe, paths, entry) {
7526 err = worktree_status(worktree, pe->path, fileindex, repo,
7527 stage_path, &spa, NULL, NULL, 0, 0);
7528 if (err)
7529 goto done;
7531 if (!spa.staged_something) {
7532 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7533 goto done;
7536 sync_err = sync_fileindex(fileindex, fileindex_path);
7537 if (sync_err && err == NULL)
7538 err = sync_err;
7539 done:
7540 if (head_ref)
7541 got_ref_close(head_ref);
7542 free(head_commit_id);
7543 free(fileindex_path);
7544 if (fileindex)
7545 got_fileindex_free(fileindex);
7546 unlockerr = lock_worktree(worktree, LOCK_SH);
7547 if (unlockerr && err == NULL)
7548 err = unlockerr;
7549 return err;
7552 struct unstage_path_arg {
7553 struct got_worktree *worktree;
7554 struct got_fileindex *fileindex;
7555 struct got_repository *repo;
7556 got_worktree_checkout_cb progress_cb;
7557 void *progress_arg;
7558 got_worktree_patch_cb patch_cb;
7559 void *patch_arg;
7562 static const struct got_error *
7563 create_unstaged_content(char **path_unstaged_content,
7564 char **path_new_staged_content, struct got_object_id *blob_id,
7565 struct got_object_id *staged_blob_id, const char *relpath,
7566 struct got_repository *repo,
7567 got_worktree_patch_cb patch_cb, void *patch_arg)
7569 const struct got_error *err, *free_err;
7570 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7571 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7572 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7573 struct got_diffreg_result *diffreg_result = NULL;
7574 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7575 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7577 *path_unstaged_content = NULL;
7578 *path_new_staged_content = NULL;
7580 err = got_object_id_str(&label1, blob_id);
7581 if (err)
7582 return err;
7583 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7584 if (err)
7585 goto done;
7587 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7588 if (err)
7589 goto done;
7591 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7592 if (err)
7593 goto done;
7595 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7596 if (err)
7597 goto done;
7599 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7600 if (err)
7601 goto done;
7603 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7604 if (err)
7605 goto done;
7607 err = got_diff_files(&diffreg_result, f1, label1, f2,
7608 path2, 3, 0, 1, NULL);
7609 if (err)
7610 goto done;
7612 err = got_opentemp_named(path_unstaged_content, &outfile,
7613 "got-unstaged-content");
7614 if (err)
7615 goto done;
7616 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7617 "got-new-staged-content");
7618 if (err)
7619 goto done;
7621 if (fseek(f1, 0L, SEEK_SET) == -1) {
7622 err = got_ferror(f1, GOT_ERR_IO);
7623 goto done;
7625 if (fseek(f2, 0L, SEEK_SET) == -1) {
7626 err = got_ferror(f2, GOT_ERR_IO);
7627 goto done;
7629 /* Count the number of actual changes in the diff result. */
7630 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7631 struct diff_chunk_context cc = {};
7632 diff_chunk_context_load_change(&cc, &nchunks_used,
7633 diffreg_result->result, n, 0);
7634 nchanges++;
7636 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7637 int choice;
7638 err = apply_or_reject_change(&choice, &nchunks_used,
7639 diffreg_result->result, n, relpath, f1, f2,
7640 &line_cur1, &line_cur2,
7641 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7642 if (err)
7643 goto done;
7644 if (choice == GOT_PATCH_CHOICE_YES)
7645 have_content = 1;
7646 else
7647 have_rejected_content = 1;
7648 if (choice == GOT_PATCH_CHOICE_QUIT)
7649 break;
7651 if (have_content || have_rejected_content)
7652 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7653 outfile, rejectfile);
7654 done:
7655 free(label1);
7656 if (blob)
7657 got_object_blob_close(blob);
7658 if (staged_blob)
7659 got_object_blob_close(staged_blob);
7660 free_err = got_diffreg_result_free(diffreg_result);
7661 if (free_err && err == NULL)
7662 err = free_err;
7663 if (f1 && fclose(f1) == EOF && err == NULL)
7664 err = got_error_from_errno2("fclose", path1);
7665 if (f2 && fclose(f2) == EOF && err == NULL)
7666 err = got_error_from_errno2("fclose", path2);
7667 if (outfile && fclose(outfile) == EOF && err == NULL)
7668 err = got_error_from_errno2("fclose", *path_unstaged_content);
7669 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7670 err = got_error_from_errno2("fclose", *path_new_staged_content);
7671 if (path1 && unlink(path1) == -1 && err == NULL)
7672 err = got_error_from_errno2("unlink", path1);
7673 if (path2 && unlink(path2) == -1 && err == NULL)
7674 err = got_error_from_errno2("unlink", path2);
7675 if (err || !have_content) {
7676 if (*path_unstaged_content &&
7677 unlink(*path_unstaged_content) == -1 && err == NULL)
7678 err = got_error_from_errno2("unlink",
7679 *path_unstaged_content);
7680 free(*path_unstaged_content);
7681 *path_unstaged_content = NULL;
7683 if (err || !have_content || !have_rejected_content) {
7684 if (*path_new_staged_content &&
7685 unlink(*path_new_staged_content) == -1 && err == NULL)
7686 err = got_error_from_errno2("unlink",
7687 *path_new_staged_content);
7688 free(*path_new_staged_content);
7689 *path_new_staged_content = NULL;
7691 free(path1);
7692 free(path2);
7693 return err;
7696 static const struct got_error *
7697 unstage_hunks(struct got_object_id *staged_blob_id,
7698 struct got_blob_object *blob_base,
7699 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7700 const char *ondisk_path, const char *label_orig,
7701 struct got_worktree *worktree, struct got_repository *repo,
7702 got_worktree_patch_cb patch_cb, void *patch_arg,
7703 got_worktree_checkout_cb progress_cb, void *progress_arg)
7705 const struct got_error *err = NULL;
7706 char *path_unstaged_content = NULL;
7707 char *path_new_staged_content = NULL;
7708 struct got_object_id *new_staged_blob_id = NULL;
7709 FILE *f = NULL;
7710 struct stat sb;
7712 err = create_unstaged_content(&path_unstaged_content,
7713 &path_new_staged_content, blob_id, staged_blob_id,
7714 ie->path, repo, patch_cb, patch_arg);
7715 if (err)
7716 return err;
7718 if (path_unstaged_content == NULL)
7719 return NULL;
7721 if (path_new_staged_content) {
7722 err = got_object_blob_create(&new_staged_blob_id,
7723 path_new_staged_content, repo);
7724 if (err)
7725 goto done;
7728 f = fopen(path_unstaged_content, "r");
7729 if (f == NULL) {
7730 err = got_error_from_errno2("fopen",
7731 path_unstaged_content);
7732 goto done;
7734 if (fstat(fileno(f), &sb) == -1) {
7735 err = got_error_from_errno2("fstat", path_unstaged_content);
7736 goto done;
7738 if (got_fileindex_entry_staged_filetype_get(ie) ==
7739 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7740 char link_target[PATH_MAX];
7741 size_t r;
7742 r = fread(link_target, 1, sizeof(link_target), f);
7743 if (r == 0 && ferror(f)) {
7744 err = got_error_from_errno("fread");
7745 goto done;
7747 if (r >= sizeof(link_target)) { /* should not happen */
7748 err = got_error(GOT_ERR_NO_SPACE);
7749 goto done;
7751 link_target[r] = '\0';
7752 err = merge_symlink(worktree, blob_base,
7753 ondisk_path, ie->path, label_orig, link_target,
7754 worktree->base_commit_id, repo, progress_cb,
7755 progress_arg);
7756 } else {
7757 int local_changes_subsumed;
7758 err = merge_file(&local_changes_subsumed, worktree,
7759 blob_base, ondisk_path, ie->path,
7760 got_fileindex_perms_to_st(ie),
7761 path_unstaged_content, label_orig, "unstaged",
7762 repo, progress_cb, progress_arg);
7764 if (err)
7765 goto done;
7767 if (new_staged_blob_id) {
7768 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7769 SHA1_DIGEST_LENGTH);
7770 } else {
7771 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7772 got_fileindex_entry_staged_filetype_set(ie, 0);
7774 done:
7775 free(new_staged_blob_id);
7776 if (path_unstaged_content &&
7777 unlink(path_unstaged_content) == -1 && err == NULL)
7778 err = got_error_from_errno2("unlink", path_unstaged_content);
7779 if (path_new_staged_content &&
7780 unlink(path_new_staged_content) == -1 && err == NULL)
7781 err = got_error_from_errno2("unlink", path_new_staged_content);
7782 if (f && fclose(f) == EOF && err == NULL)
7783 err = got_error_from_errno2("fclose", path_unstaged_content);
7784 free(path_unstaged_content);
7785 free(path_new_staged_content);
7786 return err;
7789 static const struct got_error *
7790 unstage_path(void *arg, unsigned char status,
7791 unsigned char staged_status, const char *relpath,
7792 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7793 struct got_object_id *commit_id, int dirfd, const char *de_name)
7795 const struct got_error *err = NULL;
7796 struct unstage_path_arg *a = arg;
7797 struct got_fileindex_entry *ie;
7798 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7799 char *ondisk_path = NULL;
7800 char *id_str = NULL, *label_orig = NULL;
7801 int local_changes_subsumed;
7802 struct stat sb;
7804 if (staged_status != GOT_STATUS_ADD &&
7805 staged_status != GOT_STATUS_MODIFY &&
7806 staged_status != GOT_STATUS_DELETE)
7807 return NULL;
7809 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7810 if (ie == NULL)
7811 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7813 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7814 == -1)
7815 return got_error_from_errno("asprintf");
7817 err = got_object_id_str(&id_str,
7818 commit_id ? commit_id : a->worktree->base_commit_id);
7819 if (err)
7820 goto done;
7821 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7822 id_str) == -1) {
7823 err = got_error_from_errno("asprintf");
7824 goto done;
7827 switch (staged_status) {
7828 case GOT_STATUS_MODIFY:
7829 err = got_object_open_as_blob(&blob_base, a->repo,
7830 blob_id, 8192);
7831 if (err)
7832 break;
7833 /* fall through */
7834 case GOT_STATUS_ADD:
7835 if (a->patch_cb) {
7836 if (staged_status == GOT_STATUS_ADD) {
7837 int choice = GOT_PATCH_CHOICE_NONE;
7838 err = (*a->patch_cb)(&choice, a->patch_arg,
7839 staged_status, ie->path, NULL, 1, 1);
7840 if (err)
7841 break;
7842 if (choice != GOT_PATCH_CHOICE_YES)
7843 break;
7844 } else {
7845 err = unstage_hunks(staged_blob_id,
7846 blob_base, blob_id, ie, ondisk_path,
7847 label_orig, a->worktree, a->repo,
7848 a->patch_cb, a->patch_arg,
7849 a->progress_cb, a->progress_arg);
7850 break; /* Done with this file. */
7853 err = got_object_open_as_blob(&blob_staged, a->repo,
7854 staged_blob_id, 8192);
7855 if (err)
7856 break;
7857 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7858 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7859 case GOT_FILEIDX_MODE_REGULAR_FILE:
7860 err = merge_blob(&local_changes_subsumed, a->worktree,
7861 blob_base, ondisk_path, relpath,
7862 got_fileindex_perms_to_st(ie), label_orig,
7863 blob_staged, commit_id ? commit_id :
7864 a->worktree->base_commit_id, a->repo,
7865 a->progress_cb, a->progress_arg);
7866 break;
7867 case GOT_FILEIDX_MODE_SYMLINK:
7868 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7869 char *staged_target;
7870 err = got_object_blob_read_to_str(
7871 &staged_target, blob_staged);
7872 if (err)
7873 goto done;
7874 err = merge_symlink(a->worktree, blob_base,
7875 ondisk_path, relpath, label_orig,
7876 staged_target, commit_id ? commit_id :
7877 a->worktree->base_commit_id,
7878 a->repo, a->progress_cb, a->progress_arg);
7879 free(staged_target);
7880 } else {
7881 err = merge_blob(&local_changes_subsumed,
7882 a->worktree, blob_base, ondisk_path,
7883 relpath, got_fileindex_perms_to_st(ie),
7884 label_orig, blob_staged,
7885 commit_id ? commit_id :
7886 a->worktree->base_commit_id, a->repo,
7887 a->progress_cb, a->progress_arg);
7889 break;
7890 default:
7891 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7892 break;
7894 if (err == NULL) {
7895 got_fileindex_entry_stage_set(ie,
7896 GOT_FILEIDX_STAGE_NONE);
7897 got_fileindex_entry_staged_filetype_set(ie, 0);
7899 break;
7900 case GOT_STATUS_DELETE:
7901 if (a->patch_cb) {
7902 int choice = GOT_PATCH_CHOICE_NONE;
7903 err = (*a->patch_cb)(&choice, a->patch_arg,
7904 staged_status, ie->path, NULL, 1, 1);
7905 if (err)
7906 break;
7907 if (choice == GOT_PATCH_CHOICE_NO)
7908 break;
7909 if (choice != GOT_PATCH_CHOICE_YES) {
7910 err = got_error(GOT_ERR_PATCH_CHOICE);
7911 break;
7914 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7915 got_fileindex_entry_staged_filetype_set(ie, 0);
7916 err = get_file_status(&status, &sb, ie, ondisk_path,
7917 dirfd, de_name, a->repo);
7918 if (err)
7919 break;
7920 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7921 break;
7923 done:
7924 free(ondisk_path);
7925 if (blob_base)
7926 got_object_blob_close(blob_base);
7927 if (blob_staged)
7928 got_object_blob_close(blob_staged);
7929 free(id_str);
7930 free(label_orig);
7931 return err;
7934 const struct got_error *
7935 got_worktree_unstage(struct got_worktree *worktree,
7936 struct got_pathlist_head *paths,
7937 got_worktree_checkout_cb progress_cb, void *progress_arg,
7938 got_worktree_patch_cb patch_cb, void *patch_arg,
7939 struct got_repository *repo)
7941 const struct got_error *err = NULL, *sync_err, *unlockerr;
7942 struct got_pathlist_entry *pe;
7943 struct got_fileindex *fileindex = NULL;
7944 char *fileindex_path = NULL;
7945 struct unstage_path_arg upa;
7947 err = lock_worktree(worktree, LOCK_EX);
7948 if (err)
7949 return err;
7951 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7952 if (err)
7953 goto done;
7955 upa.worktree = worktree;
7956 upa.fileindex = fileindex;
7957 upa.repo = repo;
7958 upa.progress_cb = progress_cb;
7959 upa.progress_arg = progress_arg;
7960 upa.patch_cb = patch_cb;
7961 upa.patch_arg = patch_arg;
7962 TAILQ_FOREACH(pe, paths, entry) {
7963 err = worktree_status(worktree, pe->path, fileindex, repo,
7964 unstage_path, &upa, NULL, NULL, 0, 0);
7965 if (err)
7966 goto done;
7969 sync_err = sync_fileindex(fileindex, fileindex_path);
7970 if (sync_err && err == NULL)
7971 err = sync_err;
7972 done:
7973 free(fileindex_path);
7974 if (fileindex)
7975 got_fileindex_free(fileindex);
7976 unlockerr = lock_worktree(worktree, LOCK_SH);
7977 if (unlockerr && err == NULL)
7978 err = unlockerr;
7979 return err;
7982 struct report_file_info_arg {
7983 struct got_worktree *worktree;
7984 got_worktree_path_info_cb info_cb;
7985 void *info_arg;
7986 struct got_pathlist_head *paths;
7987 got_cancel_cb cancel_cb;
7988 void *cancel_arg;
7991 static const struct got_error *
7992 report_file_info(void *arg, struct got_fileindex_entry *ie)
7994 struct report_file_info_arg *a = arg;
7995 struct got_pathlist_entry *pe;
7996 struct got_object_id blob_id, staged_blob_id, commit_id;
7997 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7998 struct got_object_id *commit_idp = NULL;
7999 int stage;
8001 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8002 return got_error(GOT_ERR_CANCELLED);
8004 TAILQ_FOREACH(pe, a->paths, entry) {
8005 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8006 got_path_is_child(ie->path, pe->path, pe->path_len))
8007 break;
8009 if (pe == NULL) /* not found */
8010 return NULL;
8012 if (got_fileindex_entry_has_blob(ie)) {
8013 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8014 blob_idp = &blob_id;
8016 stage = got_fileindex_entry_stage_get(ie);
8017 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8018 stage == GOT_FILEIDX_STAGE_ADD) {
8019 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8020 SHA1_DIGEST_LENGTH);
8021 staged_blob_idp = &staged_blob_id;
8024 if (got_fileindex_entry_has_commit(ie)) {
8025 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8026 commit_idp = &commit_id;
8029 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8030 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8033 const struct got_error *
8034 got_worktree_path_info(struct got_worktree *worktree,
8035 struct got_pathlist_head *paths,
8036 got_worktree_path_info_cb info_cb, void *info_arg,
8037 got_cancel_cb cancel_cb, void *cancel_arg)
8040 const struct got_error *err = NULL, *unlockerr;
8041 struct got_fileindex *fileindex = NULL;
8042 char *fileindex_path = NULL;
8043 struct report_file_info_arg arg;
8045 err = lock_worktree(worktree, LOCK_SH);
8046 if (err)
8047 return err;
8049 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8050 if (err)
8051 goto done;
8053 arg.worktree = worktree;
8054 arg.info_cb = info_cb;
8055 arg.info_arg = info_arg;
8056 arg.paths = paths;
8057 arg.cancel_cb = cancel_cb;
8058 arg.cancel_arg = cancel_arg;
8059 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8060 &arg);
8061 done:
8062 free(fileindex_path);
8063 if (fileindex)
8064 got_fileindex_free(fileindex);
8065 unlockerr = lock_worktree(worktree, LOCK_UN);
8066 if (unlockerr && err == NULL)
8067 err = unlockerr;
8068 return err;