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 "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t
66 apply_umask(mode_t mode)
67 {
68 mode_t um;
70 um = umask(000);
71 umask(um);
72 return mode & ~um;
73 }
75 static const struct got_error *
76 create_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 char *path;
81 if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 return got_error_from_errno("asprintf");
84 err = got_path_create_file(path, content);
85 free(path);
86 return err;
87 }
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
91 {
92 const struct got_error *err = NULL;
93 FILE *tmpfile = NULL;
94 char *tmppath = NULL;
95 char *path = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno("asprintf");
99 path = NULL;
100 goto done;
103 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
104 if (err)
105 goto done;
107 if (content) {
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno2("fprintf", tmppath);
111 goto done;
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno3("rename", tmppath, path);
117 unlink(tmppath);
118 goto done;
121 done:
122 if (fclose(tmpfile) == EOF && err == NULL)
123 err = got_error_from_errno2("fclose", tmppath);
124 free(tmppath);
125 return err;
128 static const struct got_error *
129 write_head_ref(const char *path_got, struct got_reference *head_ref)
131 const struct got_error *err = NULL;
132 char *refstr = NULL;
134 if (got_ref_is_symbolic(head_ref)) {
135 refstr = got_ref_to_str(head_ref);
136 if (refstr == NULL)
137 return got_error_from_errno("got_ref_to_str");
138 } else {
139 refstr = strdup(got_ref_get_name(head_ref));
140 if (refstr == NULL)
141 return got_error_from_errno("strdup");
143 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 free(refstr);
145 return err;
148 const struct got_error *
149 got_worktree_init(const char *path, struct got_reference *head_ref,
150 const char *prefix, const char *meta_dir, struct got_repository *repo)
152 const struct got_error *err = NULL;
153 struct got_object_id *commit_id = NULL;
154 uuid_t uuid;
155 uint32_t uuid_status;
156 int obj_type;
157 char *path_got = NULL;
158 char *formatstr = NULL;
159 char *absprefix = NULL;
160 char *basestr = NULL;
161 char *uuidstr = NULL;
163 if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 err = got_error(GOT_ERR_WORKTREE_REPO);
165 goto done;
168 err = got_ref_resolve(&commit_id, repo, head_ref);
169 if (err)
170 return err;
171 err = got_object_get_type(&obj_type, repo, commit_id);
172 if (err)
173 return err;
174 if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 return got_error(GOT_ERR_OBJ_TYPE);
177 if (!got_path_is_absolute(prefix)) {
178 if (asprintf(&absprefix, "/%s", prefix) == -1)
179 return got_error_from_errno("asprintf");
182 /* Create top-level directory (may already exist). */
183 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path);
185 goto done;
188 /* Create .got/.cvg directory (may already exist). */
189 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
190 err = got_error_from_errno("asprintf");
191 goto done;
193 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 err = got_error_from_errno2("mkdir", path_got);
195 goto done;
198 /* Create an empty lock file. */
199 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
200 if (err)
201 goto done;
203 /* Create an empty file index. */
204 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
205 if (err)
206 goto done;
208 /* Write the HEAD reference. */
209 err = write_head_ref(path_got, head_ref);
210 if (err)
211 goto done;
213 /* Record our base commit. */
214 err = got_object_id_str(&basestr, commit_id);
215 if (err)
216 goto done;
217 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
218 if (err)
219 goto done;
221 /* Store path to repository. */
222 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 got_repo_get_path(repo));
224 if (err)
225 goto done;
227 /* Store in-repository path prefix. */
228 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 absprefix ? absprefix : prefix);
230 if (err)
231 goto done;
233 /* Generate UUID. */
234 uuid_create(&uuid, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_create");
237 goto done;
239 uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 if (uuid_status != uuid_s_ok) {
241 err = got_error_uuid(uuid_status, "uuid_to_string");
242 goto done;
244 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
245 if (err)
246 goto done;
248 /* Stamp work tree with format file. */
249 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 err = got_error_from_errno("asprintf");
251 goto done;
253 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
254 if (err)
255 goto done;
257 done:
258 free(commit_id);
259 free(path_got);
260 free(formatstr);
261 free(absprefix);
262 free(basestr);
263 free(uuidstr);
264 return err;
267 const struct got_error *
268 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 const char *path_prefix)
271 char *absprefix = NULL;
273 if (!got_path_is_absolute(path_prefix)) {
274 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 return got_error_from_errno("asprintf");
277 *match = (strcmp(absprefix ? absprefix : path_prefix,
278 worktree->path_prefix) == 0);
279 free(absprefix);
280 return NULL;
283 const char *
284 got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 return worktree->head_ref_name;
289 const struct got_error *
290 got_worktree_set_head_ref(struct got_worktree *worktree,
291 struct got_reference *head_ref)
293 const struct got_error *err = NULL;
294 char *path_got = NULL, *head_ref_name = NULL;
296 if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 worktree->meta_dir) == -1) {
298 err = got_error_from_errno("asprintf");
299 path_got = NULL;
300 goto done;
303 head_ref_name = strdup(got_ref_get_name(head_ref));
304 if (head_ref_name == NULL) {
305 err = got_error_from_errno("strdup");
306 goto done;
309 err = write_head_ref(path_got, head_ref);
310 if (err)
311 goto done;
313 free(worktree->head_ref_name);
314 worktree->head_ref_name = head_ref_name;
315 done:
316 free(path_got);
317 if (err)
318 free(head_ref_name);
319 return err;
322 struct got_object_id *
323 got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 return worktree->base_commit_id;
328 const struct got_error *
329 got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 struct got_repository *repo, struct got_object_id *commit_id)
332 const struct got_error *err;
333 struct got_object *obj = NULL;
334 char *id_str = NULL;
335 char *path_got = NULL;
337 if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 worktree->meta_dir) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 err = got_object_open(&obj, repo, commit_id);
345 if (err)
346 return err;
348 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 err = got_error(GOT_ERR_OBJ_TYPE);
350 goto done;
353 /* Record our base commit. */
354 err = got_object_id_str(&id_str, commit_id);
355 if (err)
356 goto done;
357 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
358 if (err)
359 goto done;
361 free(worktree->base_commit_id);
362 worktree->base_commit_id = got_object_id_dup(commit_id);
363 if (worktree->base_commit_id == NULL) {
364 err = got_error_from_errno("got_object_id_dup");
365 goto done;
367 done:
368 if (obj)
369 got_object_close(obj);
370 free(id_str);
371 free(path_got);
372 return err;
375 const struct got_gotconfig *
376 got_worktree_get_gotconfig(struct got_worktree *worktree)
378 return worktree->gotconfig;
381 static const struct got_error *
382 lock_worktree(struct got_worktree *worktree, int operation)
384 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 : got_error_from_errno2("flock",
387 got_worktree_get_root_path(worktree)));
388 return NULL;
391 static const struct got_error *
392 add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 const struct got_error *err = NULL;
395 char *abspath;
397 /* We only accept worktree-relative paths here. */
398 if (got_path_is_absolute(path)) {
399 return got_error_fmt(GOT_ERR_BAD_PATH,
400 "%s does not accept absolute paths: %s",
401 __func__, path);
404 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 return got_error_from_errno("asprintf");
407 err = got_path_mkdir(abspath);
408 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 struct stat sb;
410 err = NULL;
411 if (lstat(abspath, &sb) == -1) {
412 err = got_error_from_errno2("lstat", abspath);
413 } else if (!S_ISDIR(sb.st_mode)) {
414 /* TODO directory is obstructed; do something */
415 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
418 free(abspath);
419 return err;
422 static const struct got_error *
423 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
425 const struct got_error *err = NULL;
426 uint8_t fbuf1[8192];
427 uint8_t fbuf2[8192];
428 size_t flen1 = 0, flen2 = 0;
430 *same = 1;
432 for (;;) {
433 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 if (flen1 == 0 && ferror(f1)) {
435 err = got_error_from_errno("fread");
436 break;
438 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 if (flen2 == 0 && ferror(f2)) {
440 err = got_error_from_errno("fread");
441 break;
443 if (flen1 == 0) {
444 if (flen2 != 0)
445 *same = 0;
446 break;
447 } else if (flen2 == 0) {
448 if (flen1 != 0)
449 *same = 0;
450 break;
451 } else if (flen1 == flen2) {
452 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
453 *same = 0;
454 break;
456 } else {
457 *same = 0;
458 break;
462 return err;
465 static const struct got_error *
466 check_files_equal(int *same, FILE *f1, FILE *f2)
468 struct stat sb;
469 size_t size1, size2;
471 *same = 1;
473 if (fstat(fileno(f1), &sb) != 0)
474 return got_error_from_errno("fstat");
475 size1 = sb.st_size;
477 if (fstat(fileno(f2), &sb) != 0)
478 return got_error_from_errno("fstat");
479 size2 = sb.st_size;
481 if (size1 != size2) {
482 *same = 0;
483 return NULL;
486 if (fseek(f1, 0L, SEEK_SET) == -1)
487 return got_ferror(f1, GOT_ERR_IO);
488 if (fseek(f2, 0L, SEEK_SET) == -1)
489 return got_ferror(f2, GOT_ERR_IO);
491 return check_file_contents_equal(same, f1, f2);
494 static const struct got_error *
495 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
497 uint8_t fbuf[65536];
498 size_t flen;
499 ssize_t outlen;
501 *outsize = 0;
503 if (fseek(f, 0L, SEEK_SET) == -1)
504 return got_ferror(f, GOT_ERR_IO);
506 for (;;) {
507 flen = fread(fbuf, 1, sizeof(fbuf), f);
508 if (flen == 0) {
509 if (ferror(f))
510 return got_error_from_errno("fread");
511 if (feof(f))
512 break;
514 outlen = write(outfd, fbuf, flen);
515 if (outlen == -1)
516 return got_error_from_errno("write");
517 if (outlen != flen)
518 return got_error(GOT_ERR_IO);
519 *outsize += outlen;
522 return NULL;
525 static const struct got_error *
526 merge_binary_file(int *overlapcnt, int merged_fd,
527 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 const char *ondisk_path)
531 const struct got_error *err = NULL;
532 int same_content, changed_deriv, changed_deriv2;
533 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 char *base_path_orig = NULL, *base_path_deriv = NULL;
537 char *base_path_deriv2 = NULL;
539 *overlapcnt = 0;
541 err = check_files_equal(&same_content, f_deriv, f_deriv2);
542 if (err)
543 return err;
545 if (same_content)
546 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
548 err = check_files_equal(&same_content, f_deriv, f_orig);
549 if (err)
550 return err;
551 changed_deriv = !same_content;
552 err = check_files_equal(&same_content, f_deriv2, f_orig);
553 if (err)
554 return err;
555 changed_deriv2 = !same_content;
557 if (changed_deriv && changed_deriv2) {
558 *overlapcnt = 1;
559 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 err = got_error_from_errno("asprintf");
561 goto done;
563 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 err = got_error_from_errno("asprintf");
565 goto done;
567 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 err = got_error_from_errno("asprintf");
569 goto done;
571 err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 base_path_orig, "");
573 if (err)
574 goto done;
575 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 base_path_deriv, "");
577 if (err)
578 goto done;
579 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 base_path_deriv2, "");
581 if (err)
582 goto done;
583 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
590 if (err)
591 goto done;
592 if (dprintf(merged_fd, "Binary files differ and cannot be "
593 "merged automatically:\n") < 0) {
594 err = got_error_from_errno("dprintf");
595 goto done;
597 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 label_deriv ? " " : "",
600 label_deriv ? label_deriv : "",
601 path_deriv) < 0) {
602 err = got_error_from_errno("dprintf");
603 goto done;
605 if (size_orig > 0) {
606 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 GOT_DIFF_CONFLICT_MARKER_ORIG,
608 label_orig ? " " : "",
609 label_orig ? label_orig : "",
610 path_orig) < 0) {
611 err = got_error_from_errno("dprintf");
612 goto done;
615 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 GOT_DIFF_CONFLICT_MARKER_SEP,
617 path_deriv2,
618 GOT_DIFF_CONFLICT_MARKER_END,
619 label_deriv2 ? " " : "",
620 label_deriv2 ? label_deriv2 : "") < 0) {
621 err = got_error_from_errno("dprintf");
622 goto done;
624 } else if (changed_deriv)
625 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 else if (changed_deriv2)
627 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
628 done:
629 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 err == NULL)
631 err = got_error_from_errno2("unlink", path_orig);
632 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 err = got_error_from_errno2("close", path_orig);
634 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 err = got_error_from_errno2("close", path_deriv);
636 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 err = got_error_from_errno2("close", path_deriv2);
638 free(path_orig);
639 free(path_deriv);
640 free(path_deriv2);
641 free(base_path_orig);
642 free(base_path_deriv);
643 free(base_path_deriv2);
644 return err;
647 /*
648 * Perform a 3-way merge where the file f_orig acts as the common
649 * ancestor, the file f_deriv acts as the first derived version,
650 * and the file f_deriv2 acts as the second derived version.
651 * The merge result will be written to a new file at ondisk_path; any
652 * existing file at this path will be replaced.
653 */
654 static const struct got_error *
655 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 const char *path, uint16_t st_mode,
658 const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 got_worktree_checkout_cb progress_cb, void *progress_arg)
662 const struct got_error *err = NULL;
663 int merged_fd = -1;
664 FILE *f_merged = NULL;
665 char *merged_path = NULL, *base_path = NULL;
666 int overlapcnt = 0;
667 char *parent = NULL;
669 *local_changes_subsumed = 0;
671 err = got_path_dirname(&parent, ondisk_path);
672 if (err)
673 return err;
675 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 err = got_error_from_errno("asprintf");
677 goto done;
680 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
681 if (err)
682 goto done;
684 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 if (err) {
687 if (err->code != GOT_ERR_FILE_BINARY)
688 goto done;
689 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 ondisk_path);
692 if (err)
693 goto done;
696 err = (*progress_cb)(progress_arg,
697 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
698 if (err)
699 goto done;
701 if (fsync(merged_fd) != 0) {
702 err = got_error_from_errno("fsync");
703 goto done;
706 f_merged = fdopen(merged_fd, "r");
707 if (f_merged == NULL) {
708 err = got_error_from_errno("fdopen");
709 goto done;
711 merged_fd = -1;
713 /* Check if a clean merge has subsumed all local changes. */
714 if (overlapcnt == 0) {
715 err = check_files_equal(local_changes_subsumed, f_deriv,
716 f_merged);
717 if (err)
718 goto done;
721 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 err = got_error_from_errno2("fchmod", merged_path);
723 goto done;
726 if (rename(merged_path, ondisk_path) != 0) {
727 err = got_error_from_errno3("rename", merged_path,
728 ondisk_path);
729 goto done;
731 done:
732 if (err) {
733 if (merged_path)
734 unlink(merged_path);
736 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 err = got_error_from_errno("close");
738 if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 err = got_error_from_errno("fclose");
740 free(merged_path);
741 free(base_path);
742 free(parent);
743 return err;
746 static const struct got_error *
747 update_symlink(const char *ondisk_path, const char *target_path,
748 size_t target_len)
750 /* This is not atomic but matches what 'ln -sf' does. */
751 if (unlink(ondisk_path) == -1)
752 return got_error_from_errno2("unlink", ondisk_path);
753 if (symlink(target_path, ondisk_path) == -1)
754 return got_error_from_errno3("symlink", target_path,
755 ondisk_path);
756 return NULL;
759 /*
760 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 * in the work tree with a file that contains conflict markers and the
762 * conflicting target paths of the original version, a "derived version"
763 * of a symlink from an incoming change, and a local version of the symlink.
765 * The original versions's target path can be NULL if it is not available,
766 * such as if both derived versions added a new symlink at the same path.
768 * The incoming derived symlink target is NULL in case the incoming change
769 * has deleted this symlink.
770 */
771 static const struct got_error *
772 install_symlink_conflict(const char *deriv_target,
773 struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 const char *label_orig, const char *local_target, const char *ondisk_path)
776 const struct got_error *err;
777 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 FILE *f = NULL;
780 err = got_object_id_str(&id_str, deriv_base_commit_id);
781 if (err)
782 return got_error_from_errno("asprintf");
784 if (asprintf(&label_deriv, "%s: commit %s",
785 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 err = got_error_from_errno("asprintf");
787 goto done;
790 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
791 if (err)
792 goto done;
794 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 err = got_error_from_errno2("fchmod", path);
796 goto done;
799 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 deriv_target ? deriv_target : "(symlink was deleted)",
802 orig_target ? label_orig : "",
803 orig_target ? "\n" : "",
804 orig_target ? orig_target : "",
805 orig_target ? "\n" : "",
806 GOT_DIFF_CONFLICT_MARKER_SEP,
807 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 err = got_error_from_errno2("fprintf", path);
809 goto done;
812 if (unlink(ondisk_path) == -1) {
813 err = got_error_from_errno2("unlink", ondisk_path);
814 goto done;
816 if (rename(path, ondisk_path) == -1) {
817 err = got_error_from_errno3("rename", path, ondisk_path);
818 goto done;
820 done:
821 if (f != NULL && fclose(f) == EOF && err == NULL)
822 err = got_error_from_errno2("fclose", path);
823 free(path);
824 free(id_str);
825 free(label_deriv);
826 return err;
829 /* forward declaration */
830 static const struct got_error *
831 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 const char *, const char *, uint16_t, const char *,
833 struct got_blob_object *, struct got_object_id *,
834 struct got_repository *, got_worktree_checkout_cb, void *);
836 /*
837 * Merge a symlink into the work tree, where blob_orig acts as the common
838 * ancestor, deriv_target is the link target of the first derived version,
839 * and the symlink on disk acts as the second derived version.
840 * Assume that contents of both blobs represent symlinks.
841 */
842 static const struct got_error *
843 merge_symlink(struct got_worktree *worktree,
844 struct got_blob_object *blob_orig, const char *ondisk_path,
845 const char *path, const char *label_orig, const char *deriv_target,
846 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 got_worktree_checkout_cb progress_cb, void *progress_arg)
849 const struct got_error *err = NULL;
850 char *ancestor_target = NULL;
851 struct stat sb;
852 ssize_t ondisk_len, deriv_len;
853 char ondisk_target[PATH_MAX];
854 int have_local_change = 0;
855 int have_incoming_change = 0;
857 if (lstat(ondisk_path, &sb) == -1)
858 return got_error_from_errno2("lstat", ondisk_path);
860 ondisk_len = readlink(ondisk_path, ondisk_target,
861 sizeof(ondisk_target));
862 if (ondisk_len == -1) {
863 err = got_error_from_errno2("readlink",
864 ondisk_path);
865 goto done;
867 ondisk_target[ondisk_len] = '\0';
869 if (blob_orig) {
870 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
871 if (err)
872 goto done;
875 if (ancestor_target == NULL ||
876 (ondisk_len != strlen(ancestor_target) ||
877 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 have_local_change = 1;
880 deriv_len = strlen(deriv_target);
881 if (ancestor_target == NULL ||
882 (deriv_len != strlen(ancestor_target) ||
883 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 have_incoming_change = 1;
886 if (!have_local_change && !have_incoming_change) {
887 if (ancestor_target) {
888 /* Both sides made the same change. */
889 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
890 path);
891 } else if (deriv_len == ondisk_len &&
892 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 /* Both sides added the same symlink. */
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
895 path);
896 } else {
897 /* Both sides added symlinks which don't match. */
898 err = install_symlink_conflict(deriv_target,
899 deriv_base_commit_id, ancestor_target,
900 label_orig, ondisk_target, ondisk_path);
901 if (err)
902 goto done;
903 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
904 path);
906 } else if (!have_local_change && have_incoming_change) {
907 /* Apply the incoming change. */
908 err = update_symlink(ondisk_path, deriv_target,
909 strlen(deriv_target));
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 } else if (have_local_change && have_incoming_change) {
914 if (deriv_len == ondisk_len &&
915 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 /* Both sides made the same change. */
917 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
918 path);
919 } else {
920 err = install_symlink_conflict(deriv_target,
921 deriv_base_commit_id, ancestor_target, label_orig,
922 ondisk_target, ondisk_path);
923 if (err)
924 goto done;
925 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
926 path);
930 done:
931 free(ancestor_target);
932 return err;
935 static const struct got_error *
936 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
938 const struct got_error *err = NULL;
939 char target_path[PATH_MAX];
940 ssize_t target_len;
941 size_t n;
942 FILE *f;
944 *outfile = NULL;
946 f = got_opentemp();
947 if (f == NULL)
948 return got_error_from_errno("got_opentemp");
949 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 if (target_len == -1) {
951 err = got_error_from_errno2("readlink", ondisk_path);
952 goto done;
954 n = fwrite(target_path, 1, target_len, f);
955 if (n != target_len) {
956 err = got_ferror(f, GOT_ERR_IO);
957 goto done;
959 if (fflush(f) == EOF) {
960 err = got_error_from_errno("fflush");
961 goto done;
963 if (fseek(f, 0L, SEEK_SET) == -1) {
964 err = got_ferror(f, GOT_ERR_IO);
965 goto done;
967 done:
968 if (err)
969 fclose(f);
970 else
971 *outfile = f;
972 return err;
975 /*
976 * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 * blob_deriv acts as the first derived version, and the file on disk
978 * acts as the second derived version.
979 */
980 static const struct got_error *
981 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 struct got_blob_object *blob_orig, const char *ondisk_path,
983 const char *path, uint16_t st_mode, const char *label_orig,
984 struct got_blob_object *blob_deriv,
985 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 got_worktree_checkout_cb progress_cb, void *progress_arg)
988 const struct got_error *err = NULL;
989 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 char *blob_orig_path = NULL;
991 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 char *label_deriv = NULL, *parent = NULL;
994 *local_changes_subsumed = 0;
996 err = got_path_dirname(&parent, ondisk_path);
997 if (err)
998 return err;
1000 if (blob_orig) {
1001 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 parent) == -1) {
1003 err = got_error_from_errno("asprintf");
1004 base_path = NULL;
1005 goto done;
1008 err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 base_path, "");
1010 if (err)
1011 goto done;
1012 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 blob_orig);
1014 if (err)
1015 goto done;
1016 free(base_path);
1017 } else {
1019 * No common ancestor exists. This is an "add vs add" conflict
1020 * and we simply use an empty ancestor file to make both files
1021 * appear in the merged result in their entirety.
1023 f_orig = got_opentemp();
1024 if (f_orig == NULL) {
1025 err = got_error_from_errno("got_opentemp");
1026 goto done;
1030 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 err = got_error_from_errno("asprintf");
1032 base_path = NULL;
1033 goto done;
1036 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1037 if (err)
1038 goto done;
1039 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 blob_deriv);
1041 if (err)
1042 goto done;
1044 err = got_object_id_str(&id_str, deriv_base_commit_id);
1045 if (err)
1046 goto done;
1047 if (asprintf(&label_deriv, "%s: commit %s",
1048 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 err = got_error_from_errno("asprintf");
1050 goto done;
1054 * In order the run a 3-way merge with a symlink we copy the symlink's
1055 * target path into a temporary file and use that file with diff3.
1057 if (S_ISLNK(st_mode)) {
1058 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1059 if (err)
1060 goto done;
1061 } else {
1062 int fd;
1063 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 if (fd == -1) {
1065 err = got_error_from_errno2("open", ondisk_path);
1066 goto done;
1068 f_deriv2 = fdopen(fd, "r");
1069 if (f_deriv2 == NULL) {
1070 err = got_error_from_errno2("fdopen", ondisk_path);
1071 close(fd);
1072 goto done;
1076 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1079 done:
1080 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 err = got_error_from_errno("fclose");
1082 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 err = got_error_from_errno("fclose");
1084 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 err = got_error_from_errno("fclose");
1086 free(base_path);
1087 if (blob_orig_path) {
1088 unlink(blob_orig_path);
1089 free(blob_orig_path);
1091 if (blob_deriv_path) {
1092 unlink(blob_deriv_path);
1093 free(blob_deriv_path);
1095 free(id_str);
1096 free(label_deriv);
1097 free(parent);
1098 return err;
1101 static const struct got_error *
1102 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 int wt_fd, const char *path, struct got_object_id *blob_id)
1106 const struct got_error *err = NULL;
1107 struct got_fileindex_entry *new_ie;
1109 *new_iep = NULL;
1111 err = got_fileindex_entry_alloc(&new_ie, path);
1112 if (err)
1113 return err;
1115 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1116 blob_id->sha1, base_commit_id->sha1, 1);
1117 if (err)
1118 goto done;
1120 err = got_fileindex_entry_add(fileindex, new_ie);
1121 done:
1122 if (err)
1123 got_fileindex_entry_free(new_ie);
1124 else
1125 *new_iep = new_ie;
1126 return err;
1129 static mode_t
1130 get_ondisk_perms(int executable, mode_t st_mode)
1132 mode_t xbits = S_IXUSR;
1134 if (executable) {
1135 /* Map read bits to execute bits. */
1136 if (st_mode & S_IRGRP)
1137 xbits |= S_IXGRP;
1138 if (st_mode & S_IROTH)
1139 xbits |= S_IXOTH;
1140 return st_mode | xbits;
1143 return st_mode;
1146 /* forward declaration */
1147 static const struct got_error *
1148 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 const char *path, mode_t te_mode, mode_t st_mode,
1150 struct got_blob_object *blob, int restoring_missing_file,
1151 int reverting_versioned_file, int installing_bad_symlink,
1152 int path_is_unversioned, struct got_repository *repo,
1153 got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 * This function assumes that the provided symlink target points at a
1157 * safe location in the work tree!
1159 static const struct got_error *
1160 replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 const char *target_path, size_t target_len)
1163 const struct got_error *err = NULL;
1164 ssize_t elen;
1165 char etarget[PATH_MAX];
1166 int fd;
1168 *did_something = 0;
1171 * "Bad" symlinks (those pointing outside the work tree or into the
1172 * .got directory) are installed in the work tree as a regular file
1173 * which contains the bad symlink target path.
1174 * The new symlink target has already been checked for safety by our
1175 * caller. If we can successfully open a regular file then we simply
1176 * replace this file with a symlink below.
1178 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1179 if (fd == -1) {
1180 if (!got_err_open_nofollow_on_symlink())
1181 return got_error_from_errno2("open", ondisk_path);
1183 /* We are updating an existing on-disk symlink. */
1184 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 if (elen == -1)
1186 return got_error_from_errno2("readlink", ondisk_path);
1188 if (elen == target_len &&
1189 memcmp(etarget, target_path, target_len) == 0)
1190 return NULL; /* nothing to do */
1193 *did_something = 1;
1194 err = update_symlink(ondisk_path, target_path, target_len);
1195 if (fd != -1 && close(fd) == -1 && err == NULL)
1196 err = got_error_from_errno2("close", ondisk_path);
1197 return err;
1200 static const struct got_error *
1201 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1203 const char *meta_dir)
1205 const struct got_error *err = NULL;
1206 char canonpath[PATH_MAX];
1207 char *path_got = NULL;
1209 *is_bad_symlink = 0;
1211 if (target_len >= sizeof(canonpath)) {
1212 *is_bad_symlink = 1;
1213 return NULL;
1217 * We do not use realpath(3) to resolve the symlink's target
1218 * path because we don't want to resolve symlinks recursively.
1219 * Instead we make the path absolute and then canonicalize it.
1220 * Relative symlink target lookup should begin at the directory
1221 * in which the blob object is being installed.
1223 if (!got_path_is_absolute(target_path)) {
1224 char *abspath, *parent;
1225 err = got_path_dirname(&parent, ondisk_path);
1226 if (err)
1227 return err;
1228 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1229 free(parent);
1230 return got_error_from_errno("asprintf");
1232 free(parent);
1233 if (strlen(abspath) >= sizeof(canonpath)) {
1234 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1235 free(abspath);
1236 return err;
1238 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1239 free(abspath);
1240 if (err)
1241 return err;
1242 } else {
1243 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 if (err)
1245 return err;
1248 /* Only allow symlinks pointing at paths within the work tree. */
1249 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1250 *is_bad_symlink = 1;
1251 return NULL;
1254 /* Do not allow symlinks pointing into the meta directory. */
1255 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1256 return got_error_from_errno("asprintf");
1257 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 *is_bad_symlink = 1;
1260 free(path_got);
1261 return NULL;
1264 static const struct got_error *
1265 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 int restoring_missing_file, int reverting_versioned_file,
1268 int path_is_unversioned, int allow_bad_symlinks,
1269 struct got_repository *repo,
1270 got_worktree_checkout_cb progress_cb, void *progress_arg)
1272 const struct got_error *err = NULL;
1273 char target_path[PATH_MAX];
1274 size_t len, target_len = 0;
1275 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1278 *is_bad_symlink = 0;
1281 * Blob object content specifies the target path of the link.
1282 * If a symbolic link cannot be installed we instead create
1283 * a regular file which contains the link target path stored
1284 * in the blob object.
1286 do {
1287 err = got_object_blob_read_block(&len, blob);
1288 if (err)
1289 return err;
1291 if (len + target_len >= sizeof(target_path)) {
1292 /* Path too long; install as a regular file. */
1293 *is_bad_symlink = 1;
1294 got_object_blob_rewind(blob);
1295 return install_blob(worktree, ondisk_path, path,
1296 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 restoring_missing_file, reverting_versioned_file,
1298 1, path_is_unversioned, repo, progress_cb,
1299 progress_arg);
1301 if (len > 0) {
1302 /* Skip blob object header first time around. */
1303 memcpy(target_path + target_len, buf + hdrlen,
1304 len - hdrlen);
1305 target_len += len - hdrlen;
1306 hdrlen = 0;
1308 } while (len != 0);
1309 target_path[target_len] = '\0';
1311 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 ondisk_path, worktree->root_path, worktree->meta_dir);
1313 if (err)
1314 return err;
1316 if (*is_bad_symlink && !allow_bad_symlinks) {
1317 /* install as a regular file */
1318 got_object_blob_rewind(blob);
1319 err = install_blob(worktree, ondisk_path, path,
1320 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 restoring_missing_file, reverting_versioned_file, 1,
1322 path_is_unversioned, repo, progress_cb, progress_arg);
1323 return err;
1326 if (symlink(target_path, ondisk_path) == -1) {
1327 if (errno == EEXIST) {
1328 int symlink_replaced;
1329 if (path_is_unversioned) {
1330 err = (*progress_cb)(progress_arg,
1331 GOT_STATUS_UNVERSIONED, path);
1332 return err;
1334 err = replace_existing_symlink(&symlink_replaced,
1335 ondisk_path, target_path, target_len);
1336 if (err)
1337 return err;
1338 if (progress_cb) {
1339 if (symlink_replaced) {
1340 err = (*progress_cb)(progress_arg,
1341 reverting_versioned_file ?
1342 GOT_STATUS_REVERT :
1343 GOT_STATUS_UPDATE, path);
1344 } else {
1345 err = (*progress_cb)(progress_arg,
1346 GOT_STATUS_EXISTS, path);
1349 return err; /* Nothing else to do. */
1352 if (errno == ENOENT) {
1353 char *parent;
1354 err = got_path_dirname(&parent, ondisk_path);
1355 if (err)
1356 return err;
1357 err = got_path_mkdir(parent);
1358 free(parent);
1359 if (err)
1360 return err;
1362 * Retry, and fall through to error handling
1363 * below if this second attempt fails.
1365 if (symlink(target_path, ondisk_path) != -1) {
1366 err = NULL; /* success */
1367 if (progress_cb) {
1368 err = (*progress_cb)(progress_arg,
1369 reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1371 path);
1373 return err;
1377 /* Handle errors from first or second creation attempt. */
1378 if (errno == ENAMETOOLONG) {
1379 /* bad target path; install as a regular file */
1380 *is_bad_symlink = 1;
1381 got_object_blob_rewind(blob);
1382 err = install_blob(worktree, ondisk_path, path,
1383 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 restoring_missing_file, reverting_versioned_file, 1,
1385 path_is_unversioned, repo,
1386 progress_cb, progress_arg);
1387 } else if (errno == ENOTDIR) {
1388 err = got_error_path(ondisk_path,
1389 GOT_ERR_FILE_OBSTRUCTED);
1390 } else {
1391 err = got_error_from_errno3("symlink",
1392 target_path, ondisk_path);
1394 } else if (progress_cb)
1395 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1396 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1397 return err;
1400 static const struct got_error *
1401 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1402 const char *path, mode_t te_mode, mode_t st_mode,
1403 struct got_blob_object *blob, int restoring_missing_file,
1404 int reverting_versioned_file, int installing_bad_symlink,
1405 int path_is_unversioned, struct got_repository *repo,
1406 got_worktree_checkout_cb progress_cb, void *progress_arg)
1408 const struct got_error *err = NULL;
1409 int fd = -1;
1410 size_t len, hdrlen;
1411 int update = 0;
1412 char *tmppath = NULL;
1413 mode_t mode;
1415 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1416 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1417 O_CLOEXEC, mode);
1418 if (fd == -1) {
1419 if (errno == ENOENT || errno == ENOTDIR) {
1420 char *parent;
1421 err = got_path_dirname(&parent, path);
1422 if (err)
1423 return err;
1424 err = add_dir_on_disk(worktree, parent);
1425 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1426 err = got_error_path(path, err->code);
1427 free(parent);
1428 if (err)
1429 return err;
1430 fd = open(ondisk_path,
1431 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1432 mode);
1433 if (fd == -1)
1434 return got_error_from_errno2("open",
1435 ondisk_path);
1436 } else if (errno == EEXIST) {
1437 if (path_is_unversioned) {
1438 err = (*progress_cb)(progress_arg,
1439 GOT_STATUS_UNVERSIONED, path);
1440 goto done;
1442 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1443 !S_ISREG(st_mode) && !installing_bad_symlink) {
1444 /* TODO file is obstructed; do something */
1445 err = got_error_path(ondisk_path,
1446 GOT_ERR_FILE_OBSTRUCTED);
1447 goto done;
1448 } else {
1449 err = got_opentemp_named_fd(&tmppath, &fd,
1450 ondisk_path, "");
1451 if (err)
1452 goto done;
1453 update = 1;
1455 if (fchmod(fd, apply_umask(mode)) == -1) {
1456 err = got_error_from_errno2("fchmod",
1457 tmppath);
1458 goto done;
1461 } else
1462 return got_error_from_errno2("open", ondisk_path);
1465 if (progress_cb) {
1466 if (restoring_missing_file)
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1468 path);
1469 else if (reverting_versioned_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1471 path);
1472 else
1473 err = (*progress_cb)(progress_arg,
1474 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1475 if (err)
1476 goto done;
1479 hdrlen = got_object_blob_get_hdrlen(blob);
1480 do {
1481 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 err = got_object_blob_read_block(&len, blob);
1483 if (err)
1484 break;
1485 if (len > 0) {
1486 /* Skip blob object header first time around. */
1487 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 if (outlen == -1) {
1489 err = got_error_from_errno("write");
1490 goto done;
1491 } else if (outlen != len - hdrlen) {
1492 err = got_error(GOT_ERR_IO);
1493 goto done;
1495 hdrlen = 0;
1497 } while (len != 0);
1499 if (fsync(fd) != 0) {
1500 err = got_error_from_errno("fsync");
1501 goto done;
1504 if (update) {
1505 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1506 err = got_error_from_errno2("unlink", ondisk_path);
1507 goto done;
1509 if (rename(tmppath, ondisk_path) != 0) {
1510 err = got_error_from_errno3("rename", tmppath,
1511 ondisk_path);
1512 goto done;
1514 free(tmppath);
1515 tmppath = NULL;
1518 done:
1519 if (fd != -1 && close(fd) == -1 && err == NULL)
1520 err = got_error_from_errno("close");
1521 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1522 err = got_error_from_errno2("unlink", tmppath);
1523 free(tmppath);
1524 return err;
1528 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1529 * conflict marker is found in newly added lines only.
1531 static const struct got_error *
1532 get_modified_file_content_status(unsigned char *status,
1533 struct got_blob_object *blob, const char *path, struct stat *sb,
1534 FILE *ondisk_file)
1536 const struct got_error *err, *free_err;
1537 const char *markers[3] = {
1538 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 GOT_DIFF_CONFLICT_MARKER_SEP,
1540 GOT_DIFF_CONFLICT_MARKER_END
1542 FILE *f1 = NULL;
1543 struct got_diffreg_result *diffreg_result = NULL;
1544 struct diff_result *r;
1545 int nchunks_parsed, n, i = 0, ln = 0;
1546 char *line = NULL;
1547 size_t linesize = 0;
1548 ssize_t linelen;
1550 if (*status != GOT_STATUS_MODIFY)
1551 return NULL;
1553 f1 = got_opentemp();
1554 if (f1 == NULL)
1555 return got_error_from_errno("got_opentemp");
1557 if (blob) {
1558 got_object_blob_rewind(blob);
1559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1560 if (err)
1561 goto done;
1564 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1565 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1566 if (err)
1567 goto done;
1569 r = diffreg_result->result;
1571 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1572 struct diff_chunk *c;
1573 struct diff_chunk_context cc = {};
1574 off_t pos;
1577 * We can optimise a little by advancing straight
1578 * to the next chunk if this one has no added lines.
1580 c = diff_chunk_get(r, n);
1582 if (diff_chunk_type(c) != CHUNK_PLUS) {
1583 nchunks_parsed = 1;
1584 continue; /* removed or unchanged lines */
1587 pos = diff_chunk_get_right_start_pos(c);
1588 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1589 err = got_ferror(ondisk_file, GOT_ERR_IO);
1590 goto done;
1593 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1594 ln = cc.right.start;
1596 while (ln < cc.right.end) {
1597 linelen = getline(&line, &linesize, ondisk_file);
1598 if (linelen == -1) {
1599 if (feof(ondisk_file))
1600 break;
1601 err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 break;
1605 if (line && strncmp(line, markers[i],
1606 strlen(markers[i])) == 0) {
1607 if (strcmp(markers[i],
1608 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1609 *status = GOT_STATUS_CONFLICT;
1610 goto done;
1611 } else
1612 i++;
1614 ++ln;
1618 done:
1619 free(line);
1620 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1621 err = got_error_from_errno("fclose");
1622 free_err = got_diffreg_result_free(diffreg_result);
1623 if (err == NULL)
1624 err = free_err;
1626 return err;
1629 static int
1630 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1632 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1636 static int
1637 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1639 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 ie->size == (sb->st_size & 0xffffffff) &&
1644 !xbit_differs(ie, sb->st_mode));
1647 static unsigned char
1648 get_staged_status(struct got_fileindex_entry *ie)
1650 switch (got_fileindex_entry_stage_get(ie)) {
1651 case GOT_FILEIDX_STAGE_ADD:
1652 return GOT_STATUS_ADD;
1653 case GOT_FILEIDX_STAGE_DELETE:
1654 return GOT_STATUS_DELETE;
1655 case GOT_FILEIDX_STAGE_MODIFY:
1656 return GOT_STATUS_MODIFY;
1657 default:
1658 return GOT_STATUS_NO_CHANGE;
1662 static const struct got_error *
1663 get_symlink_modification_status(unsigned char *status,
1664 struct got_fileindex_entry *ie, const char *abspath,
1665 int dirfd, const char *de_name, struct got_blob_object *blob)
1667 const struct got_error *err = NULL;
1668 char target_path[PATH_MAX];
1669 char etarget[PATH_MAX];
1670 ssize_t elen;
1671 size_t len, target_len = 0;
1672 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1675 *status = GOT_STATUS_NO_CHANGE;
1677 /* Blob object content specifies the target path of the link. */
1678 do {
1679 err = got_object_blob_read_block(&len, blob);
1680 if (err)
1681 return err;
1682 if (len + target_len >= sizeof(target_path)) {
1684 * Should not happen. The blob contents were OK
1685 * when this symlink was installed.
1687 return got_error(GOT_ERR_NO_SPACE);
1689 if (len > 0) {
1690 /* Skip blob object header first time around. */
1691 memcpy(target_path + target_len, buf + hdrlen,
1692 len - hdrlen);
1693 target_len += len - hdrlen;
1694 hdrlen = 0;
1696 } while (len != 0);
1697 target_path[target_len] = '\0';
1699 if (dirfd != -1) {
1700 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 if (elen == -1)
1702 return got_error_from_errno2("readlinkat", abspath);
1703 } else {
1704 elen = readlink(abspath, etarget, sizeof(etarget));
1705 if (elen == -1)
1706 return got_error_from_errno2("readlink", abspath);
1709 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 *status = GOT_STATUS_MODIFY;
1712 return NULL;
1715 static const struct got_error *
1716 get_file_status(unsigned char *status, struct stat *sb,
1717 struct got_fileindex_entry *ie, const char *abspath,
1718 int dirfd, const char *de_name, struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 struct got_object_id id;
1722 size_t hdrlen;
1723 int fd = -1, fd1 = -1;
1724 FILE *f = NULL;
1725 uint8_t fbuf[8192];
1726 struct got_blob_object *blob = NULL;
1727 size_t flen, blen;
1728 unsigned char staged_status;
1730 staged_status = get_staged_status(ie);
1731 *status = GOT_STATUS_NO_CHANGE;
1732 memset(sb, 0, sizeof(*sb));
1735 * Whenever the caller provides a directory descriptor and a
1736 * directory entry name for the file, use them! This prevents
1737 * race conditions if filesystem paths change beneath our feet.
1739 if (dirfd != -1) {
1740 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 if (errno == ENOENT) {
1742 if (got_fileindex_entry_has_file_on_disk(ie))
1743 *status = GOT_STATUS_MISSING;
1744 else
1745 *status = GOT_STATUS_DELETE;
1746 goto done;
1748 err = got_error_from_errno2("fstatat", abspath);
1749 goto done;
1751 } else {
1752 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1753 if (fd == -1 && errno != ENOENT &&
1754 !got_err_open_nofollow_on_symlink())
1755 return got_error_from_errno2("open", abspath);
1756 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1757 if (lstat(abspath, sb) == -1)
1758 return got_error_from_errno2("lstat", abspath);
1759 } else if (fd == -1 || fstat(fd, sb) == -1) {
1760 if (errno == ENOENT) {
1761 if (got_fileindex_entry_has_file_on_disk(ie))
1762 *status = GOT_STATUS_MISSING;
1763 else
1764 *status = GOT_STATUS_DELETE;
1765 goto done;
1767 err = got_error_from_errno2("fstat", abspath);
1768 goto done;
1772 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1773 *status = GOT_STATUS_OBSTRUCTED;
1774 goto done;
1777 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1778 *status = GOT_STATUS_DELETE;
1779 goto done;
1780 } else if (!got_fileindex_entry_has_blob(ie) &&
1781 staged_status != GOT_STATUS_ADD) {
1782 *status = GOT_STATUS_ADD;
1783 goto done;
1786 if (!stat_info_differs(ie, sb))
1787 goto done;
1789 if (S_ISLNK(sb->st_mode) &&
1790 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1791 *status = GOT_STATUS_MODIFY;
1792 goto done;
1795 if (staged_status == GOT_STATUS_MODIFY ||
1796 staged_status == GOT_STATUS_ADD)
1797 got_fileindex_entry_get_staged_blob_id(&id, ie);
1798 else
1799 got_fileindex_entry_get_blob_id(&id, ie);
1801 fd1 = got_opentempfd();
1802 if (fd1 == -1) {
1803 err = got_error_from_errno("got_opentempfd");
1804 goto done;
1806 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1807 if (err)
1808 goto done;
1810 if (S_ISLNK(sb->st_mode)) {
1811 err = get_symlink_modification_status(status, ie,
1812 abspath, dirfd, de_name, blob);
1813 goto done;
1816 if (dirfd != -1) {
1817 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1818 if (fd == -1) {
1819 err = got_error_from_errno2("openat", abspath);
1820 goto done;
1824 f = fdopen(fd, "r");
1825 if (f == NULL) {
1826 err = got_error_from_errno2("fdopen", abspath);
1827 goto done;
1829 fd = -1;
1830 hdrlen = got_object_blob_get_hdrlen(blob);
1831 for (;;) {
1832 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1833 err = got_object_blob_read_block(&blen, blob);
1834 if (err)
1835 goto done;
1836 /* Skip length of blob object header first time around. */
1837 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1838 if (flen == 0 && ferror(f)) {
1839 err = got_error_from_errno("fread");
1840 goto done;
1842 if (blen - hdrlen == 0) {
1843 if (flen != 0)
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1846 } else if (flen == 0) {
1847 if (blen - hdrlen != 0)
1848 *status = GOT_STATUS_MODIFY;
1849 break;
1850 } else if (blen - hdrlen == flen) {
1851 /* Skip blob object header first time around. */
1852 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1853 *status = GOT_STATUS_MODIFY;
1854 break;
1856 } else {
1857 *status = GOT_STATUS_MODIFY;
1858 break;
1860 hdrlen = 0;
1863 if (*status == GOT_STATUS_MODIFY) {
1864 rewind(f);
1865 err = get_modified_file_content_status(status, blob, ie->path,
1866 sb, f);
1867 } else if (xbit_differs(ie, sb->st_mode))
1868 *status = GOT_STATUS_MODE_CHANGE;
1869 done:
1870 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1871 err = got_error_from_errno("close");
1872 if (blob)
1873 got_object_blob_close(blob);
1874 if (f != NULL && fclose(f) == EOF && err == NULL)
1875 err = got_error_from_errno2("fclose", abspath);
1876 if (fd != -1 && close(fd) == -1 && err == NULL)
1877 err = got_error_from_errno2("close", abspath);
1878 return err;
1882 * Update timestamps in the file index if a file is unmodified and
1883 * we had to run a full content comparison to find out.
1885 static const struct got_error *
1886 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1887 struct got_fileindex_entry *ie, struct stat *sb)
1889 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1890 return got_fileindex_entry_update(ie, wt_fd, path,
1891 ie->blob_sha1, ie->commit_sha1, 1);
1893 return NULL;
1896 static const struct got_error *remove_ondisk_file(const char *, const char *);
1898 static const struct got_error *
1899 update_blob(struct got_worktree *worktree,
1900 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1901 struct got_tree_entry *te, const char *path,
1902 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1903 void *progress_arg)
1905 const struct got_error *err = NULL;
1906 struct got_blob_object *blob = NULL;
1907 char *ondisk_path = NULL;
1908 unsigned char status = GOT_STATUS_NO_CHANGE;
1909 struct stat sb;
1910 int fd1 = -1, fd2 = -1;
1912 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1913 return got_error_from_errno("asprintf");
1915 if (ie) {
1916 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1917 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1918 goto done;
1920 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1921 repo);
1922 if (err)
1923 goto done;
1924 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1925 sb.st_mode = got_fileindex_perms_to_st(ie);
1926 } else {
1927 if (stat(ondisk_path, &sb) == -1) {
1928 if (errno != ENOENT && errno != ENOTDIR) {
1929 err = got_error_from_errno2("stat",
1930 ondisk_path);
1931 goto done;
1933 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 status = GOT_STATUS_UNVERSIONED;
1935 } else {
1936 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1937 status = GOT_STATUS_UNVERSIONED;
1938 else
1939 status = GOT_STATUS_OBSTRUCTED;
1943 if (status == GOT_STATUS_OBSTRUCTED) {
1944 if (ie)
1945 got_fileindex_entry_mark_skipped(ie);
1946 err = (*progress_cb)(progress_arg, status, path);
1947 goto done;
1949 if (status == GOT_STATUS_CONFLICT) {
1950 if (ie)
1951 got_fileindex_entry_mark_skipped(ie);
1952 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1953 path);
1954 goto done;
1957 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1958 if (status == GOT_STATUS_UNVERSIONED) {
1959 err = (*progress_cb)(progress_arg, status, path);
1960 } else if (status != GOT_STATUS_NO_CHANGE &&
1961 status != GOT_STATUS_DELETE &&
1962 status != GOT_STATUS_NONEXISTENT &&
1963 status != GOT_STATUS_MISSING) {
1964 err = (*progress_cb)(progress_arg,
1965 GOT_STATUS_CANNOT_DELETE, path);
1966 } else if (ie) {
1967 if (status != GOT_STATUS_DELETE &&
1968 status != GOT_STATUS_NONEXISTENT &&
1969 status != GOT_STATUS_MISSING) {
1970 err = remove_ondisk_file(worktree->root_path,
1971 ie->path);
1972 if (err && !(err->code == GOT_ERR_ERRNO &&
1973 errno == ENOENT))
1974 goto done;
1976 got_fileindex_entry_remove(fileindex, ie);
1977 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1978 ie->path);
1980 goto done; /* nothing else to do */
1983 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1984 (S_ISLNK(te->mode) ||
1985 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1987 * This is a regular file or an installed bad symlink.
1988 * If the file index indicates that this file is already
1989 * up-to-date with respect to the repository we can skip
1990 * updating contents of this file.
1992 if (got_fileindex_entry_has_commit(ie) &&
1993 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1994 SHA1_DIGEST_LENGTH) == 0) {
1995 /* Same commit. */
1996 err = sync_timestamps(worktree->root_fd,
1997 path, status, ie, &sb);
1998 if (err)
1999 goto done;
2000 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2001 path);
2002 goto done;
2004 if (got_fileindex_entry_has_blob(ie) &&
2005 memcmp(ie->blob_sha1, te->id.sha1,
2006 SHA1_DIGEST_LENGTH) == 0) {
2007 /* Different commit but the same blob. */
2008 if (got_fileindex_entry_has_commit(ie)) {
2009 /* Update the base commit ID of this file. */
2010 memcpy(ie->commit_sha1,
2011 worktree->base_commit_id->sha1,
2012 sizeof(ie->commit_sha1));
2014 err = sync_timestamps(worktree->root_fd,
2015 path, status, ie, &sb);
2016 if (err)
2017 goto done;
2018 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2019 path);
2020 goto done;
2024 fd1 = got_opentempfd();
2025 if (fd1 == -1) {
2026 err = got_error_from_errno("got_opentempfd");
2027 goto done;
2029 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2030 if (err)
2031 goto done;
2033 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2034 int update_timestamps;
2035 struct got_blob_object *blob2 = NULL;
2036 char *label_orig = NULL;
2037 if (got_fileindex_entry_has_blob(ie)) {
2038 fd2 = got_opentempfd();
2039 if (fd2 == -1) {
2040 err = got_error_from_errno("got_opentempfd");
2041 goto done;
2043 struct got_object_id id2;
2044 got_fileindex_entry_get_blob_id(&id2, ie);
2045 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2046 fd2);
2047 if (err)
2048 goto done;
2050 if (got_fileindex_entry_has_commit(ie)) {
2051 char id_str[SHA1_DIGEST_STRING_LENGTH];
2052 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2053 sizeof(id_str)) == NULL) {
2054 err = got_error_path(id_str,
2055 GOT_ERR_BAD_OBJ_ID_STR);
2056 goto done;
2058 if (asprintf(&label_orig, "%s: commit %s",
2059 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2060 err = got_error_from_errno("asprintf");
2061 goto done;
2064 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2065 char *link_target;
2066 err = got_object_blob_read_to_str(&link_target, blob);
2067 if (err)
2068 goto done;
2069 err = merge_symlink(worktree, blob2, ondisk_path, path,
2070 label_orig, link_target, worktree->base_commit_id,
2071 repo, progress_cb, progress_arg);
2072 free(link_target);
2073 } else {
2074 err = merge_blob(&update_timestamps, worktree, blob2,
2075 ondisk_path, path, sb.st_mode, label_orig, blob,
2076 worktree->base_commit_id, repo,
2077 progress_cb, progress_arg);
2079 free(label_orig);
2080 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2081 err = got_error_from_errno("close");
2082 goto done;
2084 if (blob2)
2085 got_object_blob_close(blob2);
2086 if (err)
2087 goto done;
2089 * Do not update timestamps of files with local changes.
2090 * Otherwise, a future status walk would treat them as
2091 * unmodified files again.
2093 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 blob->id.sha1, worktree->base_commit_id->sha1,
2095 update_timestamps);
2096 } else if (status == GOT_STATUS_MODE_CHANGE) {
2097 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2098 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2099 } else if (status == GOT_STATUS_DELETE) {
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2101 if (err)
2102 goto done;
2103 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2105 if (err)
2106 goto done;
2107 } else {
2108 int is_bad_symlink = 0;
2109 if (S_ISLNK(te->mode)) {
2110 err = install_symlink(&is_bad_symlink, worktree,
2111 ondisk_path, path, blob,
2112 status == GOT_STATUS_MISSING, 0,
2113 status == GOT_STATUS_UNVERSIONED, 0,
2114 repo, progress_cb, progress_arg);
2115 } else {
2116 err = install_blob(worktree, ondisk_path, path,
2117 te->mode, sb.st_mode, blob,
2118 status == GOT_STATUS_MISSING, 0, 0,
2119 status == GOT_STATUS_UNVERSIONED, repo,
2120 progress_cb, progress_arg);
2122 if (err)
2123 goto done;
2125 if (ie) {
2126 err = got_fileindex_entry_update(ie,
2127 worktree->root_fd, path, blob->id.sha1,
2128 worktree->base_commit_id->sha1, 1);
2129 } else {
2130 err = create_fileindex_entry(&ie, fileindex,
2131 worktree->base_commit_id, worktree->root_fd, path,
2132 &blob->id);
2134 if (err)
2135 goto done;
2137 if (is_bad_symlink) {
2138 got_fileindex_entry_filetype_set(ie,
2139 GOT_FILEIDX_MODE_BAD_SYMLINK);
2143 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2144 err = got_error_from_errno("close");
2145 goto done;
2147 got_object_blob_close(blob);
2148 done:
2149 free(ondisk_path);
2150 return err;
2153 static const struct got_error *
2154 remove_ondisk_file(const char *root_path, const char *path)
2156 const struct got_error *err = NULL;
2157 char *ondisk_path = NULL, *parent = NULL;
2159 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2160 return got_error_from_errno("asprintf");
2162 if (unlink(ondisk_path) == -1) {
2163 if (errno != ENOENT)
2164 err = got_error_from_errno2("unlink", ondisk_path);
2165 } else {
2166 size_t root_len = strlen(root_path);
2167 err = got_path_dirname(&parent, ondisk_path);
2168 if (err)
2169 goto done;
2170 while (got_path_cmp(parent, root_path,
2171 strlen(parent), root_len) != 0) {
2172 free(ondisk_path);
2173 ondisk_path = parent;
2174 parent = NULL;
2175 if (rmdir(ondisk_path) == -1) {
2176 if (errno != ENOTEMPTY)
2177 err = got_error_from_errno2("rmdir",
2178 ondisk_path);
2179 break;
2181 err = got_path_dirname(&parent, ondisk_path);
2182 if (err)
2183 break;
2186 done:
2187 free(ondisk_path);
2188 free(parent);
2189 return err;
2192 static const struct got_error *
2193 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 struct got_fileindex_entry *ie, struct got_repository *repo,
2195 got_worktree_checkout_cb progress_cb, void *progress_arg)
2197 const struct got_error *err = NULL;
2198 unsigned char status;
2199 struct stat sb;
2200 char *ondisk_path;
2202 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2203 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2205 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2206 == -1)
2207 return got_error_from_errno("asprintf");
2209 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2210 if (err)
2211 goto done;
2213 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2214 char ondisk_target[PATH_MAX];
2215 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2216 sizeof(ondisk_target));
2217 if (ondisk_len == -1) {
2218 err = got_error_from_errno2("readlink", ondisk_path);
2219 goto done;
2221 ondisk_target[ondisk_len] = '\0';
2222 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2223 NULL, NULL, /* XXX pass common ancestor info? */
2224 ondisk_target, ondisk_path);
2225 if (err)
2226 goto done;
2227 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2228 ie->path);
2229 goto done;
2232 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2233 status == GOT_STATUS_ADD) {
2234 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2235 if (err)
2236 goto done;
2238 * Preserve the working file and change the deleted blob's
2239 * entry into a schedule-add entry.
2241 err = got_fileindex_entry_update(ie, worktree->root_fd,
2242 ie->path, NULL, NULL, 0);
2243 } else {
2244 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2245 if (err)
2246 goto done;
2247 if (status == GOT_STATUS_NO_CHANGE) {
2248 err = remove_ondisk_file(worktree->root_path, ie->path);
2249 if (err)
2250 goto done;
2252 got_fileindex_entry_remove(fileindex, ie);
2254 done:
2255 free(ondisk_path);
2256 return err;
2259 struct diff_cb_arg {
2260 struct got_fileindex *fileindex;
2261 struct got_worktree *worktree;
2262 struct got_repository *repo;
2263 got_worktree_checkout_cb progress_cb;
2264 void *progress_arg;
2265 got_cancel_cb cancel_cb;
2266 void *cancel_arg;
2269 static const struct got_error *
2270 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2271 struct got_tree_entry *te, const char *parent_path)
2273 struct diff_cb_arg *a = arg;
2275 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2276 return got_error(GOT_ERR_CANCELLED);
2278 return update_blob(a->worktree, a->fileindex, ie, te,
2279 ie->path, a->repo, a->progress_cb, a->progress_arg);
2282 static const struct got_error *
2283 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2285 struct diff_cb_arg *a = arg;
2287 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 return got_error(GOT_ERR_CANCELLED);
2290 return delete_blob(a->worktree, a->fileindex, ie,
2291 a->repo, a->progress_cb, a->progress_arg);
2294 static const struct got_error *
2295 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2297 struct diff_cb_arg *a = arg;
2298 const struct got_error *err;
2299 char *path;
2301 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2302 return got_error(GOT_ERR_CANCELLED);
2304 if (got_object_tree_entry_is_submodule(te))
2305 return NULL;
2307 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2308 return NULL;
2310 if (asprintf(&path, "%s%s%s", parent_path,
2311 parent_path[0] ? "/" : "", te->name)
2312 == -1)
2313 return got_error_from_errno("asprintf");
2315 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2316 a->repo, a->progress_cb, a->progress_arg);
2318 free(path);
2319 return err;
2322 const struct got_error *
2323 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2325 uint32_t uuid_status;
2327 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2328 if (uuid_status != uuid_s_ok) {
2329 *uuidstr = NULL;
2330 return got_error_uuid(uuid_status, "uuid_to_string");
2333 return NULL;
2336 static const struct got_error *
2337 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2339 const struct got_error *err = NULL;
2340 char *uuidstr = NULL;
2342 *refname = NULL;
2344 err = got_worktree_get_uuid(&uuidstr, worktree);
2345 if (err)
2346 return err;
2348 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2349 err = got_error_from_errno("asprintf");
2350 *refname = NULL;
2352 free(uuidstr);
2353 return err;
2356 const struct got_error *
2357 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2358 const char *prefix)
2360 return get_ref_name(refname, worktree, prefix);
2363 static const struct got_error *
2364 get_base_ref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2369 static const struct got_error *
2370 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2372 return get_ref_name(refname, worktree,
2373 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2376 static const struct got_error *
2377 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2379 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2382 static const struct got_error *
2383 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2385 return get_ref_name(refname, worktree,
2386 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2389 static const struct got_error *
2390 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2396 static const struct got_error *
2397 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2403 static const struct got_error *
2404 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2410 static const struct got_error *
2411 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2424 const struct got_error *
2425 got_worktree_get_histedit_script_path(char **path,
2426 struct got_worktree *worktree)
2428 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2429 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2430 *path = NULL;
2431 return got_error_from_errno("asprintf");
2433 return NULL;
2436 static const struct got_error *
2437 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2439 return get_ref_name(refname, worktree,
2440 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2443 static const struct got_error *
2444 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2446 return get_ref_name(refname, worktree,
2447 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2451 * Prevent Git's garbage collector from deleting our base commit by
2452 * setting a reference to our base commit's ID.
2454 static const struct got_error *
2455 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2457 const struct got_error *err = NULL;
2458 struct got_reference *ref = NULL;
2459 char *refname;
2461 err = get_base_ref_name(&refname, worktree);
2462 if (err)
2463 return err;
2465 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2466 if (err)
2467 goto done;
2469 err = got_ref_write(ref, repo);
2470 done:
2471 free(refname);
2472 if (ref)
2473 got_ref_close(ref);
2474 return err;
2477 static const struct got_error *
2478 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2480 const struct got_error *err = NULL;
2482 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2483 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2484 err = got_error_from_errno("asprintf");
2485 *fileindex_path = NULL;
2487 return err;
2491 static const struct got_error *
2492 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2493 struct got_worktree *worktree)
2495 const struct got_error *err = NULL;
2496 FILE *index = NULL;
2498 *fileindex_path = NULL;
2499 *fileindex = got_fileindex_alloc();
2500 if (*fileindex == NULL)
2501 return got_error_from_errno("got_fileindex_alloc");
2503 err = get_fileindex_path(fileindex_path, worktree);
2504 if (err)
2505 goto done;
2507 index = fopen(*fileindex_path, "rbe");
2508 if (index == NULL) {
2509 if (errno != ENOENT)
2510 err = got_error_from_errno2("fopen", *fileindex_path);
2511 } else {
2512 err = got_fileindex_read(*fileindex, index);
2513 if (fclose(index) == EOF && err == NULL)
2514 err = got_error_from_errno("fclose");
2516 done:
2517 if (err) {
2518 free(*fileindex_path);
2519 *fileindex_path = NULL;
2520 got_fileindex_free(*fileindex);
2521 *fileindex = NULL;
2523 return err;
2526 struct bump_base_commit_id_arg {
2527 struct got_object_id *base_commit_id;
2528 const char *path;
2529 size_t path_len;
2530 const char *entry_name;
2531 got_worktree_checkout_cb progress_cb;
2532 void *progress_arg;
2535 static const struct got_error *
2536 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2538 const struct got_error *err;
2539 struct bump_base_commit_id_arg *a = arg;
2541 if (a->entry_name) {
2542 if (strcmp(ie->path, a->path) != 0)
2543 return NULL;
2544 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2545 return NULL;
2547 if (got_fileindex_entry_was_skipped(ie))
2548 return NULL;
2550 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2551 SHA1_DIGEST_LENGTH) == 0)
2552 return NULL;
2554 if (a->progress_cb) {
2555 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2556 ie->path);
2557 if (err)
2558 return err;
2560 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2561 return NULL;
2564 /* Bump base commit ID of all files within an updated part of the work tree. */
2565 static const struct got_error *
2566 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2567 struct got_fileindex *fileindex,
2568 got_worktree_checkout_cb progress_cb, void *progress_arg)
2570 struct bump_base_commit_id_arg bbc_arg;
2572 bbc_arg.base_commit_id = worktree->base_commit_id;
2573 bbc_arg.entry_name = NULL;
2574 bbc_arg.path = "";
2575 bbc_arg.path_len = 0;
2576 bbc_arg.progress_cb = progress_cb;
2577 bbc_arg.progress_arg = progress_arg;
2579 return got_fileindex_for_each_entry_safe(fileindex,
2580 bump_base_commit_id, &bbc_arg);
2583 static const struct got_error *
2584 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2586 const struct got_error *err = NULL;
2587 char *new_fileindex_path = NULL;
2588 FILE *new_index = NULL;
2589 struct timespec timeout;
2591 err = got_opentemp_named(&new_fileindex_path, &new_index,
2592 fileindex_path, "");
2593 if (err)
2594 goto done;
2596 err = got_fileindex_write(fileindex, new_index);
2597 if (err)
2598 goto done;
2600 if (rename(new_fileindex_path, fileindex_path) != 0) {
2601 err = got_error_from_errno3("rename", new_fileindex_path,
2602 fileindex_path);
2603 unlink(new_fileindex_path);
2607 * Sleep for a short amount of time to ensure that files modified after
2608 * this program exits have a different time stamp from the one which
2609 * was recorded in the file index.
2611 timeout.tv_sec = 0;
2612 timeout.tv_nsec = 1;
2613 nanosleep(&timeout, NULL);
2614 done:
2615 if (new_index)
2616 fclose(new_index);
2617 free(new_fileindex_path);
2618 return err;
2621 static const struct got_error *
2622 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2623 struct got_object_id **tree_id, const char *wt_relpath,
2624 struct got_commit_object *base_commit, struct got_worktree *worktree,
2625 struct got_repository *repo)
2627 const struct got_error *err = NULL;
2628 struct got_object_id *id = NULL;
2629 char *in_repo_path = NULL;
2630 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2632 *entry_type = GOT_OBJ_TYPE_ANY;
2633 *tree_relpath = NULL;
2634 *tree_id = NULL;
2636 if (wt_relpath[0] == '\0') {
2637 /* Check out all files within the work tree. */
2638 *entry_type = GOT_OBJ_TYPE_TREE;
2639 *tree_relpath = strdup("");
2640 if (*tree_relpath == NULL) {
2641 err = got_error_from_errno("strdup");
2642 goto done;
2644 err = got_object_id_by_path(tree_id, repo, base_commit,
2645 worktree->path_prefix);
2646 if (err)
2647 goto done;
2648 return NULL;
2651 /* Check out a subset of files in the work tree. */
2653 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2654 is_root_wt ? "" : "/", wt_relpath) == -1) {
2655 err = got_error_from_errno("asprintf");
2656 goto done;
2659 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2660 if (err)
2661 goto done;
2663 free(in_repo_path);
2664 in_repo_path = NULL;
2666 err = got_object_get_type(entry_type, repo, id);
2667 if (err)
2668 goto done;
2670 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2671 /* Check out a single file. */
2672 if (strchr(wt_relpath, '/') == NULL) {
2673 /* Check out a single file in work tree's root dir. */
2674 in_repo_path = strdup(worktree->path_prefix);
2675 if (in_repo_path == NULL) {
2676 err = got_error_from_errno("strdup");
2677 goto done;
2679 *tree_relpath = strdup("");
2680 if (*tree_relpath == NULL) {
2681 err = got_error_from_errno("strdup");
2682 goto done;
2684 } else {
2685 /* Check out a single file in a subdirectory. */
2686 err = got_path_dirname(tree_relpath, wt_relpath);
2687 if (err)
2688 return err;
2689 if (asprintf(&in_repo_path, "%s%s%s",
2690 worktree->path_prefix, is_root_wt ? "" : "/",
2691 *tree_relpath) == -1) {
2692 err = got_error_from_errno("asprintf");
2693 goto done;
2696 err = got_object_id_by_path(tree_id, repo,
2697 base_commit, in_repo_path);
2698 } else {
2699 /* Check out all files within a subdirectory. */
2700 *tree_id = got_object_id_dup(id);
2701 if (*tree_id == NULL) {
2702 err = got_error_from_errno("got_object_id_dup");
2703 goto done;
2705 *tree_relpath = strdup(wt_relpath);
2706 if (*tree_relpath == NULL) {
2707 err = got_error_from_errno("strdup");
2708 goto done;
2711 done:
2712 free(id);
2713 free(in_repo_path);
2714 if (err) {
2715 *entry_type = GOT_OBJ_TYPE_ANY;
2716 free(*tree_relpath);
2717 *tree_relpath = NULL;
2718 free(*tree_id);
2719 *tree_id = NULL;
2721 return err;
2724 static const struct got_error *
2725 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2726 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2727 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2728 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2730 const struct got_error *err = NULL;
2731 struct got_commit_object *commit = NULL;
2732 struct got_tree_object *tree = NULL;
2733 struct got_fileindex_diff_tree_cb diff_cb;
2734 struct diff_cb_arg arg;
2736 err = ref_base_commit(worktree, repo);
2737 if (err) {
2738 if (!(err->code == GOT_ERR_ERRNO &&
2739 (errno == EACCES || errno == EROFS)))
2740 goto done;
2741 err = (*progress_cb)(progress_arg,
2742 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2743 if (err)
2744 return err;
2747 err = got_object_open_as_commit(&commit, repo,
2748 worktree->base_commit_id);
2749 if (err)
2750 goto done;
2752 err = got_object_open_as_tree(&tree, repo, tree_id);
2753 if (err)
2754 goto done;
2756 if (entry_name &&
2757 got_object_tree_find_entry(tree, entry_name) == NULL) {
2758 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2759 goto done;
2762 diff_cb.diff_old_new = diff_old_new;
2763 diff_cb.diff_old = diff_old;
2764 diff_cb.diff_new = diff_new;
2765 arg.fileindex = fileindex;
2766 arg.worktree = worktree;
2767 arg.repo = repo;
2768 arg.progress_cb = progress_cb;
2769 arg.progress_arg = progress_arg;
2770 arg.cancel_cb = cancel_cb;
2771 arg.cancel_arg = cancel_arg;
2772 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2773 entry_name, repo, &diff_cb, &arg);
2774 done:
2775 if (tree)
2776 got_object_tree_close(tree);
2777 if (commit)
2778 got_object_commit_close(commit);
2779 return err;
2782 const struct got_error *
2783 got_worktree_checkout_files(struct got_worktree *worktree,
2784 struct got_pathlist_head *paths, struct got_repository *repo,
2785 got_worktree_checkout_cb progress_cb, void *progress_arg,
2786 got_cancel_cb cancel_cb, void *cancel_arg)
2788 const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 struct got_commit_object *commit = NULL;
2790 struct got_tree_object *tree = NULL;
2791 struct got_fileindex *fileindex = NULL;
2792 char *fileindex_path = NULL;
2793 struct got_pathlist_entry *pe;
2794 struct tree_path_data {
2795 STAILQ_ENTRY(tree_path_data) entry;
2796 struct got_object_id *tree_id;
2797 int entry_type;
2798 char *relpath;
2799 char *entry_name;
2800 } *tpd = NULL;
2801 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2803 STAILQ_INIT(&tree_paths);
2805 err = lock_worktree(worktree, LOCK_EX);
2806 if (err)
2807 return err;
2809 err = got_object_open_as_commit(&commit, repo,
2810 worktree->base_commit_id);
2811 if (err)
2812 goto done;
2814 /* Map all specified paths to in-repository trees. */
2815 TAILQ_FOREACH(pe, paths, entry) {
2816 tpd = malloc(sizeof(*tpd));
2817 if (tpd == NULL) {
2818 err = got_error_from_errno("malloc");
2819 goto done;
2822 err = find_tree_entry_for_checkout(&tpd->entry_type,
2823 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2824 worktree, repo);
2825 if (err) {
2826 free(tpd);
2827 goto done;
2830 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2831 err = got_path_basename(&tpd->entry_name, pe->path);
2832 if (err) {
2833 free(tpd->relpath);
2834 free(tpd->tree_id);
2835 free(tpd);
2836 goto done;
2838 } else
2839 tpd->entry_name = NULL;
2841 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2845 * Read the file index.
2846 * Checking out files is supposed to be an idempotent operation.
2847 * If the on-disk file index is incomplete we will try to complete it.
2849 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2850 if (err)
2851 goto done;
2853 tpd = STAILQ_FIRST(&tree_paths);
2854 TAILQ_FOREACH(pe, paths, entry) {
2855 struct bump_base_commit_id_arg bbc_arg;
2857 err = checkout_files(worktree, fileindex, tpd->relpath,
2858 tpd->tree_id, tpd->entry_name, repo,
2859 progress_cb, progress_arg, cancel_cb, cancel_arg);
2860 if (err)
2861 break;
2863 bbc_arg.base_commit_id = worktree->base_commit_id;
2864 bbc_arg.entry_name = tpd->entry_name;
2865 bbc_arg.path = pe->path;
2866 bbc_arg.path_len = pe->path_len;
2867 bbc_arg.progress_cb = progress_cb;
2868 bbc_arg.progress_arg = progress_arg;
2869 err = got_fileindex_for_each_entry_safe(fileindex,
2870 bump_base_commit_id, &bbc_arg);
2871 if (err)
2872 break;
2874 tpd = STAILQ_NEXT(tpd, entry);
2876 sync_err = sync_fileindex(fileindex, fileindex_path);
2877 if (sync_err && err == NULL)
2878 err = sync_err;
2879 done:
2880 free(fileindex_path);
2881 if (tree)
2882 got_object_tree_close(tree);
2883 if (commit)
2884 got_object_commit_close(commit);
2885 if (fileindex)
2886 got_fileindex_free(fileindex);
2887 while (!STAILQ_EMPTY(&tree_paths)) {
2888 tpd = STAILQ_FIRST(&tree_paths);
2889 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2890 free(tpd->relpath);
2891 free(tpd->tree_id);
2892 free(tpd);
2894 unlockerr = lock_worktree(worktree, LOCK_SH);
2895 if (unlockerr && err == NULL)
2896 err = unlockerr;
2897 return err;
2900 static const struct got_error *
2901 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2902 struct got_fileindex_entry *ie, const char *ondisk_path,
2903 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2904 int restoring_missing_file, int reverting_versioned_file,
2905 int path_is_unversioned, int allow_bad_symlinks,
2906 struct got_repository *repo,
2907 got_worktree_checkout_cb progress_cb, void *progress_arg)
2909 const struct got_error *err = NULL;
2910 int is_bad_symlink = 0;
2912 if (S_ISLNK(mode2)) {
2913 err = install_symlink(&is_bad_symlink,
2914 worktree, ondisk_path, path2, blob2,
2915 restoring_missing_file,
2916 reverting_versioned_file,
2917 path_is_unversioned, allow_bad_symlinks,
2918 repo, progress_cb, progress_arg);
2919 } else {
2920 err = install_blob(worktree, ondisk_path, path2,
2921 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2922 restoring_missing_file, reverting_versioned_file, 0,
2923 path_is_unversioned, repo, progress_cb, progress_arg);
2925 if (err)
2926 return err;
2927 if (ie == NULL) {
2928 /* Adding an unversioned file. */
2929 err = got_fileindex_entry_alloc(&ie, path2);
2930 if (err)
2931 return err;
2932 err = got_fileindex_entry_update(ie,
2933 worktree->root_fd, path2, NULL, NULL, 1);
2934 if (err) {
2935 got_fileindex_entry_free(ie);
2936 return err;
2938 err = got_fileindex_entry_add(fileindex, ie);
2939 if (err) {
2940 got_fileindex_entry_free(ie);
2941 return err;
2943 } else {
2944 /* Re-adding a locally deleted file. */
2945 err = got_fileindex_entry_update(ie,
2946 worktree->root_fd, path2, ie->blob_sha1,
2947 worktree->base_commit_id->sha1, 0);
2948 if (err)
2949 return err;
2952 if (is_bad_symlink) {
2953 got_fileindex_entry_filetype_set(ie,
2954 GOT_FILEIDX_MODE_BAD_SYMLINK);
2957 return NULL;
2960 struct merge_file_cb_arg {
2961 struct got_worktree *worktree;
2962 struct got_fileindex *fileindex;
2963 got_worktree_checkout_cb progress_cb;
2964 void *progress_arg;
2965 got_cancel_cb cancel_cb;
2966 void *cancel_arg;
2967 const char *label_orig;
2968 struct got_object_id *commit_id2;
2969 int allow_bad_symlinks;
2972 static const struct got_error *
2973 merge_file_cb(void *arg, struct got_blob_object *blob1,
2974 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2975 struct got_object_id *id1, struct got_object_id *id2,
2976 const char *path1, const char *path2,
2977 mode_t mode1, mode_t mode2, struct got_repository *repo)
2979 static const struct got_error *err = NULL;
2980 struct merge_file_cb_arg *a = arg;
2981 struct got_fileindex_entry *ie;
2982 char *ondisk_path = NULL;
2983 struct stat sb;
2984 unsigned char status;
2985 int local_changes_subsumed;
2986 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2987 char *id_str = NULL, *label_deriv2 = NULL;
2989 if (blob1 && blob2) {
2990 ie = got_fileindex_entry_get(a->fileindex, path2,
2991 strlen(path2));
2992 if (ie == NULL)
2993 return (*a->progress_cb)(a->progress_arg,
2994 GOT_STATUS_MISSING, path2);
2996 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2997 path2) == -1)
2998 return got_error_from_errno("asprintf");
3000 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3001 repo);
3002 if (err)
3003 goto done;
3005 if (status == GOT_STATUS_DELETE) {
3006 err = (*a->progress_cb)(a->progress_arg,
3007 GOT_STATUS_MERGE, path2);
3008 goto done;
3010 if (status != GOT_STATUS_NO_CHANGE &&
3011 status != GOT_STATUS_MODIFY &&
3012 status != GOT_STATUS_CONFLICT &&
3013 status != GOT_STATUS_ADD) {
3014 err = (*a->progress_cb)(a->progress_arg, status, path2);
3015 goto done;
3018 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3019 char *link_target2;
3020 err = got_object_blob_read_to_str(&link_target2, blob2);
3021 if (err)
3022 goto done;
3023 err = merge_symlink(a->worktree, blob1, ondisk_path,
3024 path2, a->label_orig, link_target2, a->commit_id2,
3025 repo, a->progress_cb, a->progress_arg);
3026 free(link_target2);
3027 } else {
3028 int fd;
3030 f_orig = got_opentemp();
3031 if (f_orig == NULL) {
3032 err = got_error_from_errno("got_opentemp");
3033 goto done;
3035 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3036 f_orig, blob1);
3037 if (err)
3038 goto done;
3040 f_deriv2 = got_opentemp();
3041 if (f_deriv2 == NULL)
3042 goto done;
3043 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3044 f_deriv2, blob2);
3045 if (err)
3046 goto done;
3048 fd = open(ondisk_path,
3049 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3050 if (fd == -1) {
3051 err = got_error_from_errno2("open",
3052 ondisk_path);
3053 goto done;
3055 f_deriv = fdopen(fd, "r");
3056 if (f_deriv == NULL) {
3057 err = got_error_from_errno2("fdopen",
3058 ondisk_path);
3059 close(fd);
3060 goto done;
3062 err = got_object_id_str(&id_str, a->commit_id2);
3063 if (err)
3064 goto done;
3065 if (asprintf(&label_deriv2, "%s: commit %s",
3066 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3067 err = got_error_from_errno("asprintf");
3068 goto done;
3070 err = merge_file(&local_changes_subsumed, a->worktree,
3071 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3072 mode2, a->label_orig, NULL, label_deriv2,
3073 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3074 a->progress_cb, a->progress_arg);
3076 } else if (blob1) {
3077 ie = got_fileindex_entry_get(a->fileindex, path1,
3078 strlen(path1));
3079 if (ie == NULL)
3080 return (*a->progress_cb)(a->progress_arg,
3081 GOT_STATUS_MISSING, path1);
3083 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3084 path1) == -1)
3085 return got_error_from_errno("asprintf");
3087 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3088 repo);
3089 if (err)
3090 goto done;
3092 switch (status) {
3093 case GOT_STATUS_NO_CHANGE:
3094 err = (*a->progress_cb)(a->progress_arg,
3095 GOT_STATUS_DELETE, path1);
3096 if (err)
3097 goto done;
3098 err = remove_ondisk_file(a->worktree->root_path, path1);
3099 if (err)
3100 goto done;
3101 if (ie)
3102 got_fileindex_entry_mark_deleted_from_disk(ie);
3103 break;
3104 case GOT_STATUS_DELETE:
3105 case GOT_STATUS_MISSING:
3106 err = (*a->progress_cb)(a->progress_arg,
3107 GOT_STATUS_DELETE, path1);
3108 if (err)
3109 goto done;
3110 if (ie)
3111 got_fileindex_entry_mark_deleted_from_disk(ie);
3112 break;
3113 case GOT_STATUS_ADD: {
3114 struct got_object_id *id;
3115 FILE *blob1_f;
3116 off_t blob1_size;
3118 * Delete the added file only if its content already
3119 * exists in the repository.
3121 err = got_object_blob_file_create(&id, &blob1_f,
3122 &blob1_size, path1);
3123 if (err)
3124 goto done;
3125 if (got_object_id_cmp(id, id1) == 0) {
3126 err = (*a->progress_cb)(a->progress_arg,
3127 GOT_STATUS_DELETE, path1);
3128 if (err)
3129 goto done;
3130 err = remove_ondisk_file(a->worktree->root_path,
3131 path1);
3132 if (err)
3133 goto done;
3134 if (ie)
3135 got_fileindex_entry_remove(a->fileindex,
3136 ie);
3137 } else {
3138 err = (*a->progress_cb)(a->progress_arg,
3139 GOT_STATUS_CANNOT_DELETE, path1);
3141 if (fclose(blob1_f) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 free(id);
3144 if (err)
3145 goto done;
3146 break;
3148 case GOT_STATUS_MODIFY:
3149 case GOT_STATUS_CONFLICT:
3150 err = (*a->progress_cb)(a->progress_arg,
3151 GOT_STATUS_CANNOT_DELETE, path1);
3152 if (err)
3153 goto done;
3154 break;
3155 case GOT_STATUS_OBSTRUCTED:
3156 err = (*a->progress_cb)(a->progress_arg, status, path1);
3157 if (err)
3158 goto done;
3159 break;
3160 default:
3161 break;
3163 } else if (blob2) {
3164 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3165 path2) == -1)
3166 return got_error_from_errno("asprintf");
3167 ie = got_fileindex_entry_get(a->fileindex, path2,
3168 strlen(path2));
3169 if (ie) {
3170 err = get_file_status(&status, &sb, ie, ondisk_path,
3171 -1, NULL, repo);
3172 if (err)
3173 goto done;
3174 if (status != GOT_STATUS_NO_CHANGE &&
3175 status != GOT_STATUS_MODIFY &&
3176 status != GOT_STATUS_CONFLICT &&
3177 status != GOT_STATUS_ADD &&
3178 status != GOT_STATUS_DELETE) {
3179 err = (*a->progress_cb)(a->progress_arg,
3180 status, path2);
3181 goto done;
3183 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3184 char *link_target2;
3185 err = got_object_blob_read_to_str(&link_target2,
3186 blob2);
3187 if (err)
3188 goto done;
3189 err = merge_symlink(a->worktree, NULL,
3190 ondisk_path, path2, a->label_orig,
3191 link_target2, a->commit_id2, repo,
3192 a->progress_cb, a->progress_arg);
3193 free(link_target2);
3194 } else if (S_ISREG(sb.st_mode)) {
3195 err = merge_blob(&local_changes_subsumed,
3196 a->worktree, NULL, ondisk_path, path2,
3197 sb.st_mode, a->label_orig, blob2,
3198 a->commit_id2, repo, a->progress_cb,
3199 a->progress_arg);
3200 } else if (status != GOT_STATUS_DELETE) {
3201 err = got_error_path(ondisk_path,
3202 GOT_ERR_FILE_OBSTRUCTED);
3204 if (err)
3205 goto done;
3206 if (status == GOT_STATUS_DELETE) {
3207 /* Re-add file with content from new blob. */
3208 err = add_file(a->worktree, a->fileindex, ie,
3209 ondisk_path, path2, blob2, mode2,
3210 0, 0, 0, a->allow_bad_symlinks,
3211 repo, a->progress_cb, a->progress_arg);
3212 if (err)
3213 goto done;
3215 } else {
3216 err = add_file(a->worktree, a->fileindex, NULL,
3217 ondisk_path, path2, blob2, mode2,
3218 0, 0, 1, a->allow_bad_symlinks,
3219 repo, a->progress_cb, a->progress_arg);
3220 if (err)
3221 goto done;
3224 done:
3225 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3226 err = got_error_from_errno("fclose");
3227 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3228 err = got_error_from_errno("fclose");
3229 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3230 err = got_error_from_errno("fclose");
3231 free(id_str);
3232 free(label_deriv2);
3233 free(ondisk_path);
3234 return err;
3237 static const struct got_error *
3238 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3240 struct got_worktree *worktree = arg;
3242 /* Reject merges into a work tree with mixed base commits. */
3243 if (got_fileindex_entry_has_commit(ie) &&
3244 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3245 SHA1_DIGEST_LENGTH) != 0)
3246 return got_error(GOT_ERR_MIXED_COMMITS);
3248 return NULL;
3251 const struct got_error *
3252 got_worktree_get_state(char *state, struct got_repository *repo,
3253 struct got_worktree *worktree)
3255 const struct got_error *err;
3256 struct got_object_id *base_id, *head_id = NULL;
3257 struct got_reference *head_ref;
3258 struct got_fileindex *fileindex = NULL;
3259 char *fileindex_path = NULL;
3261 if (worktree == NULL)
3262 return got_error(GOT_ERR_NOT_WORKTREE);
3264 err = got_ref_open(&head_ref, repo,
3265 got_worktree_get_head_ref_name(worktree), 0);
3266 if (err)
3267 return err;
3269 err = got_ref_resolve(&head_id, repo, head_ref);
3270 if (err)
3271 goto done;
3273 *state = GOT_WORKTREE_STATE_UNKNOWN;
3274 base_id = got_worktree_get_base_commit_id(worktree);
3276 if (got_object_id_cmp(base_id, head_id) == 0) {
3277 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3278 if (err)
3279 goto done;
3281 err = got_fileindex_for_each_entry_safe(fileindex,
3282 check_mixed_commits, worktree);
3283 if (err == NULL)
3284 *state = GOT_WORKTREE_STATE_UPTODATE;
3285 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3286 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3287 err = NULL;
3289 } else
3290 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3292 done:
3293 free(head_id);
3294 free(fileindex_path);
3295 got_ref_close(head_ref);
3296 if (fileindex != NULL)
3297 got_fileindex_free(fileindex);
3298 return err;
3301 struct check_merge_conflicts_arg {
3302 struct got_worktree *worktree;
3303 struct got_fileindex *fileindex;
3304 struct got_repository *repo;
3307 static const struct got_error *
3308 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3309 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3310 struct got_object_id *id1, struct got_object_id *id2,
3311 const char *path1, const char *path2,
3312 mode_t mode1, mode_t mode2, struct got_repository *repo)
3314 const struct got_error *err = NULL;
3315 struct check_merge_conflicts_arg *a = arg;
3316 unsigned char status;
3317 struct stat sb;
3318 struct got_fileindex_entry *ie;
3319 const char *path = path2 ? path2 : path1;
3320 struct got_object_id *id = id2 ? id2 : id1;
3321 char *ondisk_path;
3323 if (id == NULL)
3324 return NULL;
3326 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3327 if (ie == NULL)
3328 return NULL;
3330 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3331 == -1)
3332 return got_error_from_errno("asprintf");
3334 /* Reject merges into a work tree with conflicted files. */
3335 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3336 free(ondisk_path);
3337 if (err)
3338 return err;
3339 if (status == GOT_STATUS_CONFLICT)
3340 return got_error(GOT_ERR_CONFLICTS);
3342 return NULL;
3345 static const struct got_error *
3346 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3347 const char *fileindex_path, struct got_object_id *commit_id1,
3348 struct got_object_id *commit_id2, struct got_repository *repo,
3349 got_worktree_checkout_cb progress_cb, void *progress_arg,
3350 got_cancel_cb cancel_cb, void *cancel_arg)
3352 const struct got_error *err = NULL, *sync_err;
3353 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3354 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3355 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3356 struct check_merge_conflicts_arg cmc_arg;
3357 struct merge_file_cb_arg arg;
3358 char *label_orig = NULL;
3359 FILE *f1 = NULL, *f2 = NULL;
3360 int fd1 = -1, fd2 = -1;
3362 if (commit_id1) {
3363 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3364 if (err)
3365 goto done;
3366 err = got_object_id_by_path(&tree_id1, repo, commit1,
3367 worktree->path_prefix);
3368 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3369 goto done;
3371 if (tree_id1) {
3372 char *id_str;
3374 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3375 if (err)
3376 goto done;
3378 err = got_object_id_str(&id_str, commit_id1);
3379 if (err)
3380 goto done;
3382 if (asprintf(&label_orig, "%s: commit %s",
3383 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3384 err = got_error_from_errno("asprintf");
3385 free(id_str);
3386 goto done;
3388 free(id_str);
3390 f1 = got_opentemp();
3391 if (f1 == NULL) {
3392 err = got_error_from_errno("got_opentemp");
3393 goto done;
3397 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3398 if (err)
3399 goto done;
3401 err = got_object_id_by_path(&tree_id2, repo, commit2,
3402 worktree->path_prefix);
3403 if (err)
3404 goto done;
3406 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3407 if (err)
3408 goto done;
3410 f2 = got_opentemp();
3411 if (f2 == NULL) {
3412 err = got_error_from_errno("got_opentemp");
3413 goto done;
3416 fd1 = got_opentempfd();
3417 if (fd1 == -1) {
3418 err = got_error_from_errno("got_opentempfd");
3419 goto done;
3422 fd2 = got_opentempfd();
3423 if (fd2 == -1) {
3424 err = got_error_from_errno("got_opentempfd");
3425 goto done;
3428 cmc_arg.worktree = worktree;
3429 cmc_arg.fileindex = fileindex;
3430 cmc_arg.repo = repo;
3431 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3432 check_merge_conflicts, &cmc_arg, 0);
3433 if (err)
3434 goto done;
3436 arg.worktree = worktree;
3437 arg.fileindex = fileindex;
3438 arg.progress_cb = progress_cb;
3439 arg.progress_arg = progress_arg;
3440 arg.cancel_cb = cancel_cb;
3441 arg.cancel_arg = cancel_arg;
3442 arg.label_orig = label_orig;
3443 arg.commit_id2 = commit_id2;
3444 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3445 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3446 merge_file_cb, &arg, 1);
3447 sync_err = sync_fileindex(fileindex, fileindex_path);
3448 if (sync_err && err == NULL)
3449 err = sync_err;
3450 done:
3451 if (commit1)
3452 got_object_commit_close(commit1);
3453 if (commit2)
3454 got_object_commit_close(commit2);
3455 if (tree1)
3456 got_object_tree_close(tree1);
3457 if (tree2)
3458 got_object_tree_close(tree2);
3459 if (f1 && fclose(f1) == EOF && err == NULL)
3460 err = got_error_from_errno("fclose");
3461 if (f2 && fclose(f2) == EOF && err == NULL)
3462 err = got_error_from_errno("fclose");
3463 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3464 err = got_error_from_errno("close");
3465 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3466 err = got_error_from_errno("close");
3467 free(label_orig);
3468 return err;
3471 const struct got_error *
3472 got_worktree_merge_files(struct got_worktree *worktree,
3473 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3474 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3475 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3477 const struct got_error *err, *unlockerr;
3478 char *fileindex_path = NULL;
3479 struct got_fileindex *fileindex = NULL;
3481 err = lock_worktree(worktree, LOCK_EX);
3482 if (err)
3483 return err;
3485 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3486 if (err)
3487 goto done;
3489 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3490 worktree);
3491 if (err)
3492 goto done;
3494 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3495 commit_id2, repo, progress_cb, progress_arg,
3496 cancel_cb, cancel_arg);
3497 done:
3498 if (fileindex)
3499 got_fileindex_free(fileindex);
3500 free(fileindex_path);
3501 unlockerr = lock_worktree(worktree, LOCK_SH);
3502 if (unlockerr && err == NULL)
3503 err = unlockerr;
3504 return err;
3507 struct diff_dir_cb_arg {
3508 struct got_fileindex *fileindex;
3509 struct got_worktree *worktree;
3510 const char *status_path;
3511 size_t status_path_len;
3512 struct got_repository *repo;
3513 got_worktree_status_cb status_cb;
3514 void *status_arg;
3515 got_cancel_cb cancel_cb;
3516 void *cancel_arg;
3517 /* A pathlist containing per-directory pathlists of ignore patterns. */
3518 struct got_pathlist_head *ignores;
3519 int report_unchanged;
3520 int no_ignores;
3523 static const struct got_error *
3524 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3525 int dirfd, const char *de_name,
3526 got_worktree_status_cb status_cb, void *status_arg,
3527 struct got_repository *repo, int report_unchanged)
3529 const struct got_error *err = NULL;
3530 unsigned char status = GOT_STATUS_NO_CHANGE;
3531 unsigned char staged_status;
3532 struct stat sb;
3533 struct got_object_id blob_id, commit_id, staged_blob_id;
3534 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3535 struct got_object_id *staged_blob_idp = NULL;
3537 staged_status = get_staged_status(ie);
3538 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3539 if (err)
3540 return err;
3542 if (status == GOT_STATUS_NO_CHANGE &&
3543 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3544 return NULL;
3546 if (got_fileindex_entry_has_blob(ie))
3547 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3548 if (got_fileindex_entry_has_commit(ie))
3549 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3550 if (staged_status == GOT_STATUS_ADD ||
3551 staged_status == GOT_STATUS_MODIFY) {
3552 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3553 &staged_blob_id, ie);
3556 return (*status_cb)(status_arg, status, staged_status,
3557 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3560 static const struct got_error *
3561 status_old_new(void *arg, struct got_fileindex_entry *ie,
3562 struct dirent *de, const char *parent_path, int dirfd)
3564 const struct got_error *err = NULL;
3565 struct diff_dir_cb_arg *a = arg;
3566 char *abspath;
3568 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3569 return got_error(GOT_ERR_CANCELLED);
3571 if (got_path_cmp(parent_path, a->status_path,
3572 strlen(parent_path), a->status_path_len) != 0 &&
3573 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3574 return NULL;
3576 if (parent_path[0]) {
3577 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3578 parent_path, de->d_name) == -1)
3579 return got_error_from_errno("asprintf");
3580 } else {
3581 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3582 de->d_name) == -1)
3583 return got_error_from_errno("asprintf");
3586 err = report_file_status(ie, abspath, dirfd, de->d_name,
3587 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3588 free(abspath);
3589 return err;
3592 static const struct got_error *
3593 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3595 struct diff_dir_cb_arg *a = arg;
3596 struct got_object_id blob_id, commit_id;
3597 unsigned char status;
3599 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3600 return got_error(GOT_ERR_CANCELLED);
3602 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3603 return NULL;
3605 got_fileindex_entry_get_blob_id(&blob_id, ie);
3606 got_fileindex_entry_get_commit_id(&commit_id, ie);
3607 if (got_fileindex_entry_has_file_on_disk(ie))
3608 status = GOT_STATUS_MISSING;
3609 else
3610 status = GOT_STATUS_DELETE;
3611 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3612 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3615 static void
3616 free_ignores(struct got_pathlist_head *ignores)
3618 struct got_pathlist_entry *pe;
3620 TAILQ_FOREACH(pe, ignores, entry) {
3621 struct got_pathlist_head *ignorelist = pe->data;
3623 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3625 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3628 static const struct got_error *
3629 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3631 const struct got_error *err = NULL;
3632 struct got_pathlist_entry *pe = NULL;
3633 struct got_pathlist_head *ignorelist;
3634 char *line = NULL, *pattern, *dirpath = NULL;
3635 size_t linesize = 0;
3636 ssize_t linelen;
3638 ignorelist = calloc(1, sizeof(*ignorelist));
3639 if (ignorelist == NULL)
3640 return got_error_from_errno("calloc");
3641 TAILQ_INIT(ignorelist);
3643 while ((linelen = getline(&line, &linesize, f)) != -1) {
3644 if (linelen > 0 && line[linelen - 1] == '\n')
3645 line[linelen - 1] = '\0';
3647 /* Git's ignores may contain comments. */
3648 if (line[0] == '#')
3649 continue;
3651 /* Git's negated patterns are not (yet?) supported. */
3652 if (line[0] == '!')
3653 continue;
3655 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3656 line) == -1) {
3657 err = got_error_from_errno("asprintf");
3658 goto done;
3660 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3661 if (err)
3662 goto done;
3664 if (ferror(f)) {
3665 err = got_error_from_errno("getline");
3666 goto done;
3669 dirpath = strdup(path);
3670 if (dirpath == NULL) {
3671 err = got_error_from_errno("strdup");
3672 goto done;
3674 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3675 done:
3676 free(line);
3677 if (err || pe == NULL) {
3678 free(dirpath);
3679 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3681 return err;
3684 static int
3685 match_path(const char *pattern, size_t pattern_len, const char *path,
3686 int flags)
3688 char buf[PATH_MAX];
3691 * Trailing slashes signify directories.
3692 * Append a * to make such patterns conform to fnmatch rules.
3694 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3695 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3696 return FNM_NOMATCH; /* XXX */
3698 return fnmatch(buf, path, flags);
3701 return fnmatch(pattern, path, flags);
3704 static int
3705 match_ignores(struct got_pathlist_head *ignores, const char *path)
3707 struct got_pathlist_entry *pe;
3709 /* Handle patterns which match in all directories. */
3710 TAILQ_FOREACH(pe, ignores, entry) {
3711 struct got_pathlist_head *ignorelist = pe->data;
3712 struct got_pathlist_entry *pi;
3714 TAILQ_FOREACH(pi, ignorelist, entry) {
3715 const char *p;
3717 if (pi->path_len < 3 ||
3718 strncmp(pi->path, "**/", 3) != 0)
3719 continue;
3720 p = path;
3721 while (*p) {
3722 if (match_path(pi->path + 3,
3723 pi->path_len - 3, p,
3724 FNM_PATHNAME | FNM_LEADING_DIR)) {
3725 /* Retry in next directory. */
3726 while (*p && *p != '/')
3727 p++;
3728 while (*p == '/')
3729 p++;
3730 continue;
3732 return 1;
3738 * The ignores pathlist contains ignore lists from children before
3739 * parents, so we can find the most specific ignorelist by walking
3740 * ignores backwards.
3742 pe = TAILQ_LAST(ignores, got_pathlist_head);
3743 while (pe) {
3744 if (got_path_is_child(path, pe->path, pe->path_len)) {
3745 struct got_pathlist_head *ignorelist = pe->data;
3746 struct got_pathlist_entry *pi;
3747 TAILQ_FOREACH(pi, ignorelist, entry) {
3748 int flags = FNM_LEADING_DIR;
3749 if (strstr(pi->path, "/**/") == NULL)
3750 flags |= FNM_PATHNAME;
3751 if (match_path(pi->path, pi->path_len,
3752 path, flags))
3753 continue;
3754 return 1;
3757 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3760 return 0;
3763 static const struct got_error *
3764 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3765 const char *path, int dirfd, const char *ignores_filename)
3767 const struct got_error *err = NULL;
3768 char *ignorespath;
3769 int fd = -1;
3770 FILE *ignoresfile = NULL;
3772 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3773 path[0] ? "/" : "", ignores_filename) == -1)
3774 return got_error_from_errno("asprintf");
3776 if (dirfd != -1) {
3777 fd = openat(dirfd, ignores_filename,
3778 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3779 if (fd == -1) {
3780 if (errno != ENOENT && errno != EACCES)
3781 err = got_error_from_errno2("openat",
3782 ignorespath);
3783 } else {
3784 ignoresfile = fdopen(fd, "r");
3785 if (ignoresfile == NULL)
3786 err = got_error_from_errno2("fdopen",
3787 ignorespath);
3788 else {
3789 fd = -1;
3790 err = read_ignores(ignores, path, ignoresfile);
3793 } else {
3794 ignoresfile = fopen(ignorespath, "re");
3795 if (ignoresfile == NULL) {
3796 if (errno != ENOENT && errno != EACCES)
3797 err = got_error_from_errno2("fopen",
3798 ignorespath);
3799 } else
3800 err = read_ignores(ignores, path, ignoresfile);
3803 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3804 err = got_error_from_errno2("fclose", path);
3805 if (fd != -1 && close(fd) == -1 && err == NULL)
3806 err = got_error_from_errno2("close", path);
3807 free(ignorespath);
3808 return err;
3811 static const struct got_error *
3812 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3813 int dirfd)
3815 const struct got_error *err = NULL;
3816 struct diff_dir_cb_arg *a = arg;
3817 char *path = NULL;
3819 if (ignore != NULL)
3820 *ignore = 0;
3822 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3823 return got_error(GOT_ERR_CANCELLED);
3825 if (parent_path[0]) {
3826 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3827 return got_error_from_errno("asprintf");
3828 } else {
3829 path = de->d_name;
3832 if (de->d_type == DT_DIR) {
3833 if (!a->no_ignores && ignore != NULL &&
3834 match_ignores(a->ignores, path))
3835 *ignore = 1;
3836 } else if (!match_ignores(a->ignores, path) &&
3837 got_path_is_child(path, a->status_path, a->status_path_len))
3838 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3839 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3840 if (parent_path[0])
3841 free(path);
3842 return err;
3845 static const struct got_error *
3846 status_traverse(void *arg, const char *path, int dirfd)
3848 const struct got_error *err = NULL;
3849 struct diff_dir_cb_arg *a = arg;
3851 if (a->no_ignores)
3852 return NULL;
3854 err = add_ignores(a->ignores, a->worktree->root_path,
3855 path, dirfd, ".cvsignore");
3856 if (err)
3857 return err;
3859 err = add_ignores(a->ignores, a->worktree->root_path, path,
3860 dirfd, ".gitignore");
3862 return err;
3865 static const struct got_error *
3866 report_single_file_status(const char *path, const char *ondisk_path,
3867 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3868 void *status_arg, struct got_repository *repo, int report_unchanged,
3869 struct got_pathlist_head *ignores, int no_ignores)
3871 struct got_fileindex_entry *ie;
3872 struct stat sb;
3874 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3875 if (ie)
3876 return report_file_status(ie, ondisk_path, -1, NULL,
3877 status_cb, status_arg, repo, report_unchanged);
3879 if (lstat(ondisk_path, &sb) == -1) {
3880 if (errno != ENOENT)
3881 return got_error_from_errno2("lstat", ondisk_path);
3882 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3883 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3886 if (!no_ignores && match_ignores(ignores, path))
3887 return NULL;
3889 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3890 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3891 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3893 return NULL;
3896 static const struct got_error *
3897 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3898 const char *root_path, const char *path)
3900 const struct got_error *err;
3901 char *parent_path, *next_parent_path = NULL;
3903 err = add_ignores(ignores, root_path, "", -1,
3904 ".cvsignore");
3905 if (err)
3906 return err;
3908 err = add_ignores(ignores, root_path, "", -1,
3909 ".gitignore");
3910 if (err)
3911 return err;
3913 err = got_path_dirname(&parent_path, path);
3914 if (err) {
3915 if (err->code == GOT_ERR_BAD_PATH)
3916 return NULL; /* cannot traverse parent */
3917 return err;
3919 for (;;) {
3920 err = add_ignores(ignores, root_path, parent_path, -1,
3921 ".cvsignore");
3922 if (err)
3923 break;
3924 err = add_ignores(ignores, root_path, parent_path, -1,
3925 ".gitignore");
3926 if (err)
3927 break;
3928 err = got_path_dirname(&next_parent_path, parent_path);
3929 if (err) {
3930 if (err->code == GOT_ERR_BAD_PATH)
3931 err = NULL; /* traversed everything */
3932 break;
3934 if (got_path_is_root_dir(parent_path))
3935 break;
3936 free(parent_path);
3937 parent_path = next_parent_path;
3938 next_parent_path = NULL;
3941 free(parent_path);
3942 free(next_parent_path);
3943 return err;
3946 struct find_missing_children_args {
3947 const char *parent_path;
3948 size_t parent_len;
3949 struct got_pathlist_head *children;
3950 got_cancel_cb cancel_cb;
3951 void *cancel_arg;
3954 static const struct got_error *
3955 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3957 const struct got_error *err = NULL;
3958 struct find_missing_children_args *a = arg;
3960 if (a->cancel_cb) {
3961 err = a->cancel_cb(a->cancel_arg);
3962 if (err)
3963 return err;
3966 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3967 err = got_pathlist_append(a->children, ie->path, NULL);
3969 return err;
3972 static const struct got_error *
3973 report_children(struct got_pathlist_head *children,
3974 struct got_worktree *worktree, struct got_fileindex *fileindex,
3975 struct got_repository *repo, int is_root_dir, int report_unchanged,
3976 struct got_pathlist_head *ignores, int no_ignores,
3977 got_worktree_status_cb status_cb, void *status_arg,
3978 got_cancel_cb cancel_cb, void *cancel_arg)
3980 const struct got_error *err = NULL;
3981 struct got_pathlist_entry *pe;
3982 char *ondisk_path = NULL;
3984 TAILQ_FOREACH(pe, children, entry) {
3985 if (cancel_cb) {
3986 err = cancel_cb(cancel_arg);
3987 if (err)
3988 break;
3991 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3992 !is_root_dir ? "/" : "", pe->path) == -1) {
3993 err = got_error_from_errno("asprintf");
3994 ondisk_path = NULL;
3995 break;
3998 err = report_single_file_status(pe->path, ondisk_path,
3999 fileindex, status_cb, status_arg, repo, report_unchanged,
4000 ignores, no_ignores);
4001 if (err)
4002 break;
4004 free(ondisk_path);
4005 ondisk_path = NULL;
4008 free(ondisk_path);
4009 return err;
4012 static const struct got_error *
4013 worktree_status(struct got_worktree *worktree, const char *path,
4014 struct got_fileindex *fileindex, struct got_repository *repo,
4015 got_worktree_status_cb status_cb, void *status_arg,
4016 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4017 int report_unchanged)
4019 const struct got_error *err = NULL;
4020 int fd = -1;
4021 struct got_fileindex_diff_dir_cb fdiff_cb;
4022 struct diff_dir_cb_arg arg;
4023 char *ondisk_path = NULL;
4024 struct got_pathlist_head ignores, missing_children;
4025 struct got_fileindex_entry *ie;
4027 TAILQ_INIT(&ignores);
4028 TAILQ_INIT(&missing_children);
4030 if (asprintf(&ondisk_path, "%s%s%s",
4031 worktree->root_path, path[0] ? "/" : "", path) == -1)
4032 return got_error_from_errno("asprintf");
4034 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4035 if (ie) {
4036 err = report_single_file_status(path, ondisk_path,
4037 fileindex, status_cb, status_arg, repo,
4038 report_unchanged, &ignores, no_ignores);
4039 goto done;
4040 } else {
4041 struct find_missing_children_args fmca;
4042 fmca.parent_path = path;
4043 fmca.parent_len = strlen(path);
4044 fmca.children = &missing_children;
4045 fmca.cancel_cb = cancel_cb;
4046 fmca.cancel_arg = cancel_arg;
4047 err = got_fileindex_for_each_entry_safe(fileindex,
4048 find_missing_children, &fmca);
4049 if (err)
4050 goto done;
4053 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4054 if (fd == -1) {
4055 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4056 !got_err_open_nofollow_on_symlink())
4057 err = got_error_from_errno2("open", ondisk_path);
4058 else {
4059 if (!no_ignores) {
4060 err = add_ignores_from_parent_paths(&ignores,
4061 worktree->root_path, ondisk_path);
4062 if (err)
4063 goto done;
4065 if (TAILQ_EMPTY(&missing_children)) {
4066 err = report_single_file_status(path,
4067 ondisk_path, fileindex,
4068 status_cb, status_arg, repo,
4069 report_unchanged, &ignores, no_ignores);
4070 if (err)
4071 goto done;
4072 } else {
4073 err = report_children(&missing_children,
4074 worktree, fileindex, repo,
4075 (path[0] == '\0'), report_unchanged,
4076 &ignores, no_ignores,
4077 status_cb, status_arg,
4078 cancel_cb, cancel_arg);
4079 if (err)
4080 goto done;
4083 } else {
4084 fdiff_cb.diff_old_new = status_old_new;
4085 fdiff_cb.diff_old = status_old;
4086 fdiff_cb.diff_new = status_new;
4087 fdiff_cb.diff_traverse = status_traverse;
4088 arg.fileindex = fileindex;
4089 arg.worktree = worktree;
4090 arg.status_path = path;
4091 arg.status_path_len = strlen(path);
4092 arg.repo = repo;
4093 arg.status_cb = status_cb;
4094 arg.status_arg = status_arg;
4095 arg.cancel_cb = cancel_cb;
4096 arg.cancel_arg = cancel_arg;
4097 arg.report_unchanged = report_unchanged;
4098 arg.no_ignores = no_ignores;
4099 if (!no_ignores) {
4100 err = add_ignores_from_parent_paths(&ignores,
4101 worktree->root_path, path);
4102 if (err)
4103 goto done;
4105 arg.ignores = &ignores;
4106 err = got_fileindex_diff_dir(fileindex, fd,
4107 worktree->root_path, path, repo, &fdiff_cb, &arg);
4109 done:
4110 free_ignores(&ignores);
4111 if (fd != -1 && close(fd) == -1 && err == NULL)
4112 err = got_error_from_errno("close");
4113 free(ondisk_path);
4114 return err;
4117 const struct got_error *
4118 got_worktree_status(struct got_worktree *worktree,
4119 struct got_pathlist_head *paths, struct got_repository *repo,
4120 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4121 got_cancel_cb cancel_cb, void *cancel_arg)
4123 const struct got_error *err = NULL;
4124 char *fileindex_path = NULL;
4125 struct got_fileindex *fileindex = NULL;
4126 struct got_pathlist_entry *pe;
4128 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4129 if (err)
4130 return err;
4132 TAILQ_FOREACH(pe, paths, entry) {
4133 err = worktree_status(worktree, pe->path, fileindex, repo,
4134 status_cb, status_arg, cancel_cb, cancel_arg,
4135 no_ignores, 0);
4136 if (err)
4137 break;
4139 free(fileindex_path);
4140 got_fileindex_free(fileindex);
4141 return err;
4144 const struct got_error *
4145 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4146 const char *arg)
4148 const struct got_error *err = NULL;
4149 char *resolved = NULL, *cwd = NULL, *path = NULL;
4150 size_t len;
4151 struct stat sb;
4152 char *abspath = NULL;
4153 char canonpath[PATH_MAX];
4155 *wt_path = NULL;
4157 cwd = getcwd(NULL, 0);
4158 if (cwd == NULL)
4159 return got_error_from_errno("getcwd");
4161 if (lstat(arg, &sb) == -1) {
4162 if (errno != ENOENT) {
4163 err = got_error_from_errno2("lstat", arg);
4164 goto done;
4166 sb.st_mode = 0;
4168 if (S_ISLNK(sb.st_mode)) {
4170 * We cannot use realpath(3) with symlinks since we want to
4171 * operate on the symlink itself.
4172 * But we can make the path absolute, assuming it is relative
4173 * to the current working directory, and then canonicalize it.
4175 if (!got_path_is_absolute(arg)) {
4176 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4177 err = got_error_from_errno("asprintf");
4178 goto done;
4182 err = got_canonpath(abspath ? abspath : arg, canonpath,
4183 sizeof(canonpath));
4184 if (err)
4185 goto done;
4186 resolved = strdup(canonpath);
4187 if (resolved == NULL) {
4188 err = got_error_from_errno("strdup");
4189 goto done;
4191 } else {
4192 resolved = realpath(arg, NULL);
4193 if (resolved == NULL) {
4194 if (errno != ENOENT) {
4195 err = got_error_from_errno2("realpath", arg);
4196 goto done;
4198 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4199 err = got_error_from_errno("asprintf");
4200 goto done;
4202 err = got_canonpath(abspath, canonpath,
4203 sizeof(canonpath));
4204 if (err)
4205 goto done;
4206 resolved = strdup(canonpath);
4207 if (resolved == NULL) {
4208 err = got_error_from_errno("strdup");
4209 goto done;
4214 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4215 strlen(got_worktree_get_root_path(worktree)))) {
4216 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4217 goto done;
4220 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4221 err = got_path_skip_common_ancestor(&path,
4222 got_worktree_get_root_path(worktree), resolved);
4223 if (err)
4224 goto done;
4225 } else {
4226 path = strdup("");
4227 if (path == NULL) {
4228 err = got_error_from_errno("strdup");
4229 goto done;
4233 /* XXX status walk can't deal with trailing slash! */
4234 len = strlen(path);
4235 while (len > 0 && path[len - 1] == '/') {
4236 path[len - 1] = '\0';
4237 len--;
4239 done:
4240 free(abspath);
4241 free(resolved);
4242 free(cwd);
4243 if (err == NULL)
4244 *wt_path = path;
4245 else
4246 free(path);
4247 return err;
4250 struct schedule_addition_args {
4251 struct got_worktree *worktree;
4252 struct got_fileindex *fileindex;
4253 got_worktree_checkout_cb progress_cb;
4254 void *progress_arg;
4255 struct got_repository *repo;
4258 static int
4259 add_noop_status(unsigned char status)
4261 return (status == GOT_STATUS_ADD ||
4262 status == GOT_STATUS_MODIFY ||
4263 status == GOT_STATUS_CONFLICT ||
4264 status == GOT_STATUS_MODE_CHANGE ||
4265 status == GOT_STATUS_NO_CHANGE);
4268 static const struct got_error *
4269 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4270 const char *relpath, struct got_object_id *blob_id,
4271 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4272 int dirfd, const char *de_name)
4274 struct schedule_addition_args *a = arg;
4275 const struct got_error *err = NULL;
4276 struct got_fileindex_entry *ie;
4277 struct stat sb;
4278 char *ondisk_path;
4280 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4281 relpath) == -1)
4282 return got_error_from_errno("asprintf");
4284 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4285 if (ie) {
4286 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4287 de_name, a->repo);
4288 if (err)
4289 goto done;
4290 /* Re-adding an existing entry is a no-op. */
4291 if (staged_status == GOT_STATUS_NO_CHANGE &&
4292 add_noop_status(status))
4293 goto done;
4294 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4295 if (err)
4296 goto done;
4299 if (status != GOT_STATUS_UNVERSIONED) {
4300 if (status == GOT_STATUS_NONEXISTENT)
4301 err = got_error_set_errno(ENOENT, ondisk_path);
4302 else
4303 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4304 goto done;
4307 err = got_fileindex_entry_alloc(&ie, relpath);
4308 if (err)
4309 goto done;
4310 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4311 relpath, NULL, NULL, 1);
4312 if (err) {
4313 got_fileindex_entry_free(ie);
4314 goto done;
4316 err = got_fileindex_entry_add(a->fileindex, ie);
4317 if (err) {
4318 got_fileindex_entry_free(ie);
4319 goto done;
4321 done:
4322 free(ondisk_path);
4323 if (err)
4324 return err;
4325 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4326 return NULL;
4327 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4330 const struct got_error *
4331 got_worktree_schedule_add(struct got_worktree *worktree,
4332 struct got_pathlist_head *paths,
4333 got_worktree_checkout_cb progress_cb, void *progress_arg,
4334 struct got_repository *repo, int no_ignores)
4336 struct got_fileindex *fileindex = NULL;
4337 char *fileindex_path = NULL;
4338 const struct got_error *err = NULL, *sync_err, *unlockerr;
4339 struct got_pathlist_entry *pe;
4340 struct schedule_addition_args saa;
4342 err = lock_worktree(worktree, LOCK_EX);
4343 if (err)
4344 return err;
4346 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4347 if (err)
4348 goto done;
4350 saa.worktree = worktree;
4351 saa.fileindex = fileindex;
4352 saa.progress_cb = progress_cb;
4353 saa.progress_arg = progress_arg;
4354 saa.repo = repo;
4356 TAILQ_FOREACH(pe, paths, entry) {
4357 err = worktree_status(worktree, pe->path, fileindex, repo,
4358 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4359 if (err)
4360 break;
4362 sync_err = sync_fileindex(fileindex, fileindex_path);
4363 if (sync_err && err == NULL)
4364 err = sync_err;
4365 done:
4366 free(fileindex_path);
4367 if (fileindex)
4368 got_fileindex_free(fileindex);
4369 unlockerr = lock_worktree(worktree, LOCK_SH);
4370 if (unlockerr && err == NULL)
4371 err = unlockerr;
4372 return err;
4375 struct schedule_deletion_args {
4376 struct got_worktree *worktree;
4377 struct got_fileindex *fileindex;
4378 got_worktree_delete_cb progress_cb;
4379 void *progress_arg;
4380 struct got_repository *repo;
4381 int delete_local_mods;
4382 int keep_on_disk;
4383 int ignore_missing_paths;
4384 const char *status_path;
4385 size_t status_path_len;
4386 const char *status_codes;
4389 static const struct got_error *
4390 schedule_for_deletion(void *arg, unsigned char status,
4391 unsigned char staged_status, const char *relpath,
4392 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4393 struct got_object_id *commit_id, int dirfd, const char *de_name)
4395 struct schedule_deletion_args *a = arg;
4396 const struct got_error *err = NULL;
4397 struct got_fileindex_entry *ie = NULL;
4398 struct stat sb;
4399 char *ondisk_path;
4401 if (status == GOT_STATUS_NONEXISTENT) {
4402 if (a->ignore_missing_paths)
4403 return NULL;
4404 return got_error_set_errno(ENOENT, relpath);
4407 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4408 if (ie == NULL)
4409 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4411 staged_status = get_staged_status(ie);
4412 if (staged_status != GOT_STATUS_NO_CHANGE) {
4413 if (staged_status == GOT_STATUS_DELETE)
4414 return NULL;
4415 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4418 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4419 relpath) == -1)
4420 return got_error_from_errno("asprintf");
4422 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4423 a->repo);
4424 if (err)
4425 goto done;
4427 if (a->status_codes) {
4428 size_t ncodes = strlen(a->status_codes);
4429 int i;
4430 for (i = 0; i < ncodes ; i++) {
4431 if (status == a->status_codes[i])
4432 break;
4434 if (i == ncodes) {
4435 /* Do not delete files in non-matching status. */
4436 free(ondisk_path);
4437 return NULL;
4439 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4440 a->status_codes[i] != GOT_STATUS_MISSING) {
4441 static char msg[64];
4442 snprintf(msg, sizeof(msg),
4443 "invalid status code '%c'", a->status_codes[i]);
4444 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4445 goto done;
4449 if (status != GOT_STATUS_NO_CHANGE) {
4450 if (status == GOT_STATUS_DELETE)
4451 goto done;
4452 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4453 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4454 goto done;
4456 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4457 err = got_error_set_errno(ENOENT, relpath);
4458 goto done;
4460 if (status != GOT_STATUS_MODIFY &&
4461 status != GOT_STATUS_MISSING) {
4462 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4463 goto done;
4467 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4468 size_t root_len;
4470 if (dirfd != -1) {
4471 if (unlinkat(dirfd, de_name, 0) == -1) {
4472 err = got_error_from_errno2("unlinkat",
4473 ondisk_path);
4474 goto done;
4476 } else if (unlink(ondisk_path) == -1) {
4477 err = got_error_from_errno2("unlink", ondisk_path);
4478 goto done;
4481 root_len = strlen(a->worktree->root_path);
4482 do {
4483 char *parent;
4485 err = got_path_dirname(&parent, ondisk_path);
4486 if (err)
4487 goto done;
4488 free(ondisk_path);
4489 ondisk_path = parent;
4490 if (got_path_cmp(ondisk_path, a->status_path,
4491 strlen(ondisk_path), a->status_path_len) != 0 &&
4492 !got_path_is_child(ondisk_path, a->status_path,
4493 a->status_path_len))
4494 break;
4495 if (rmdir(ondisk_path) == -1) {
4496 if (errno != ENOTEMPTY)
4497 err = got_error_from_errno2("rmdir",
4498 ondisk_path);
4499 break;
4501 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4502 strlen(ondisk_path), root_len) != 0);
4505 got_fileindex_entry_mark_deleted_from_disk(ie);
4506 done:
4507 free(ondisk_path);
4508 if (err)
4509 return err;
4510 if (status == GOT_STATUS_DELETE)
4511 return NULL;
4512 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4513 staged_status, relpath);
4516 const struct got_error *
4517 got_worktree_schedule_delete(struct got_worktree *worktree,
4518 struct got_pathlist_head *paths, int delete_local_mods,
4519 const char *status_codes,
4520 got_worktree_delete_cb progress_cb, void *progress_arg,
4521 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4523 struct got_fileindex *fileindex = NULL;
4524 char *fileindex_path = NULL;
4525 const struct got_error *err = NULL, *sync_err, *unlockerr;
4526 struct got_pathlist_entry *pe;
4527 struct schedule_deletion_args sda;
4529 err = lock_worktree(worktree, LOCK_EX);
4530 if (err)
4531 return err;
4533 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4534 if (err)
4535 goto done;
4537 sda.worktree = worktree;
4538 sda.fileindex = fileindex;
4539 sda.progress_cb = progress_cb;
4540 sda.progress_arg = progress_arg;
4541 sda.repo = repo;
4542 sda.delete_local_mods = delete_local_mods;
4543 sda.keep_on_disk = keep_on_disk;
4544 sda.ignore_missing_paths = ignore_missing_paths;
4545 sda.status_codes = status_codes;
4547 TAILQ_FOREACH(pe, paths, entry) {
4548 char *ondisk_status_path;
4550 if (asprintf(&ondisk_status_path, "%s%s%s",
4551 got_worktree_get_root_path(worktree),
4552 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4553 err = got_error_from_errno("asprintf");
4554 goto done;
4556 sda.status_path = ondisk_status_path;
4557 sda.status_path_len = strlen(ondisk_status_path);
4558 err = worktree_status(worktree, pe->path, fileindex, repo,
4559 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4560 free(ondisk_status_path);
4561 if (err)
4562 break;
4564 sync_err = sync_fileindex(fileindex, fileindex_path);
4565 if (sync_err && err == NULL)
4566 err = sync_err;
4567 done:
4568 free(fileindex_path);
4569 if (fileindex)
4570 got_fileindex_free(fileindex);
4571 unlockerr = lock_worktree(worktree, LOCK_SH);
4572 if (unlockerr && err == NULL)
4573 err = unlockerr;
4574 return err;
4577 static const struct got_error *
4578 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4580 const struct got_error *err = NULL;
4581 char *line = NULL;
4582 size_t linesize = 0, n;
4583 ssize_t linelen;
4585 linelen = getline(&line, &linesize, infile);
4586 if (linelen == -1) {
4587 if (ferror(infile)) {
4588 err = got_error_from_errno("getline");
4589 goto done;
4591 return NULL;
4593 if (outfile) {
4594 n = fwrite(line, 1, linelen, outfile);
4595 if (n != linelen) {
4596 err = got_ferror(outfile, GOT_ERR_IO);
4597 goto done;
4600 if (rejectfile) {
4601 n = fwrite(line, 1, linelen, rejectfile);
4602 if (n != linelen)
4603 err = got_ferror(rejectfile, GOT_ERR_IO);
4605 done:
4606 free(line);
4607 return err;
4610 static const struct got_error *
4611 skip_one_line(FILE *f)
4613 char *line = NULL;
4614 size_t linesize = 0;
4615 ssize_t linelen;
4617 linelen = getline(&line, &linesize, f);
4618 if (linelen == -1) {
4619 if (ferror(f))
4620 return got_error_from_errno("getline");
4621 return NULL;
4623 free(line);
4624 return NULL;
4627 static const struct got_error *
4628 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4629 int start_old, int end_old, int start_new, int end_new,
4630 FILE *outfile, FILE *rejectfile)
4632 const struct got_error *err;
4634 /* Copy old file's lines leading up to patch. */
4635 while (!feof(f1) && *line_cur1 < start_old) {
4636 err = copy_one_line(f1, outfile, NULL);
4637 if (err)
4638 return err;
4639 (*line_cur1)++;
4641 /* Skip new file's lines leading up to patch. */
4642 while (!feof(f2) && *line_cur2 < start_new) {
4643 if (rejectfile)
4644 err = copy_one_line(f2, NULL, rejectfile);
4645 else
4646 err = skip_one_line(f2);
4647 if (err)
4648 return err;
4649 (*line_cur2)++;
4651 /* Copy patched lines. */
4652 while (!feof(f2) && *line_cur2 <= end_new) {
4653 err = copy_one_line(f2, outfile, NULL);
4654 if (err)
4655 return err;
4656 (*line_cur2)++;
4658 /* Skip over old file's replaced lines. */
4659 while (!feof(f1) && *line_cur1 <= end_old) {
4660 if (rejectfile)
4661 err = copy_one_line(f1, NULL, rejectfile);
4662 else
4663 err = skip_one_line(f1);
4664 if (err)
4665 return err;
4666 (*line_cur1)++;
4669 return NULL;
4672 static const struct got_error *
4673 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4674 FILE *outfile, FILE *rejectfile)
4676 const struct got_error *err;
4678 if (outfile) {
4679 /* Copy old file's lines until EOF. */
4680 while (!feof(f1)) {
4681 err = copy_one_line(f1, outfile, NULL);
4682 if (err)
4683 return err;
4684 (*line_cur1)++;
4687 if (rejectfile) {
4688 /* Copy new file's lines until EOF. */
4689 while (!feof(f2)) {
4690 err = copy_one_line(f2, NULL, rejectfile);
4691 if (err)
4692 return err;
4693 (*line_cur2)++;
4697 return NULL;
4700 static const struct got_error *
4701 apply_or_reject_change(int *choice, int *nchunks_used,
4702 struct diff_result *diff_result, int n,
4703 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4704 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4705 got_worktree_patch_cb patch_cb, void *patch_arg)
4707 const struct got_error *err = NULL;
4708 struct diff_chunk_context cc = {};
4709 int start_old, end_old, start_new, end_new;
4710 FILE *hunkfile;
4711 struct diff_output_unidiff_state *diff_state;
4712 struct diff_input_info diff_info;
4713 int rc;
4715 *choice = GOT_PATCH_CHOICE_NONE;
4717 /* Get changed line numbers without context lines for copy_change(). */
4718 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4719 start_old = cc.left.start;
4720 end_old = cc.left.end;
4721 start_new = cc.right.start;
4722 end_new = cc.right.end;
4724 /* Get the same change with context lines for display. */
4725 memset(&cc, 0, sizeof(cc));
4726 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4728 memset(&diff_info, 0, sizeof(diff_info));
4729 diff_info.left_path = relpath;
4730 diff_info.right_path = relpath;
4732 diff_state = diff_output_unidiff_state_alloc();
4733 if (diff_state == NULL)
4734 return got_error_set_errno(ENOMEM,
4735 "diff_output_unidiff_state_alloc");
4737 hunkfile = got_opentemp();
4738 if (hunkfile == NULL) {
4739 err = got_error_from_errno("got_opentemp");
4740 goto done;
4743 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4744 diff_result, &cc);
4745 if (rc != DIFF_RC_OK) {
4746 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4747 goto done;
4750 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4751 err = got_ferror(hunkfile, GOT_ERR_IO);
4752 goto done;
4755 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4756 hunkfile, changeno, nchanges);
4757 if (err)
4758 goto done;
4760 switch (*choice) {
4761 case GOT_PATCH_CHOICE_YES:
4762 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4763 end_old, start_new, end_new, outfile, rejectfile);
4764 break;
4765 case GOT_PATCH_CHOICE_NO:
4766 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4767 end_old, start_new, end_new, rejectfile, outfile);
4768 break;
4769 case GOT_PATCH_CHOICE_QUIT:
4770 break;
4771 default:
4772 err = got_error(GOT_ERR_PATCH_CHOICE);
4773 break;
4775 done:
4776 diff_output_unidiff_state_free(diff_state);
4777 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4778 err = got_error_from_errno("fclose");
4779 return err;
4782 struct revert_file_args {
4783 struct got_worktree *worktree;
4784 struct got_fileindex *fileindex;
4785 got_worktree_checkout_cb progress_cb;
4786 void *progress_arg;
4787 got_worktree_patch_cb patch_cb;
4788 void *patch_arg;
4789 struct got_repository *repo;
4790 int unlink_added_files;
4791 struct got_pathlist_head *added_files_to_unlink;
4794 static const struct got_error *
4795 create_patched_content(char **path_outfile, int reverse_patch,
4796 struct got_object_id *blob_id, const char *path2,
4797 int dirfd2, const char *de_name2,
4798 const char *relpath, struct got_repository *repo,
4799 got_worktree_patch_cb patch_cb, void *patch_arg)
4801 const struct got_error *err, *free_err;
4802 struct got_blob_object *blob = NULL;
4803 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4804 int fd = -1, fd2 = -1;
4805 char link_target[PATH_MAX];
4806 ssize_t link_len = 0;
4807 char *path1 = NULL, *id_str = NULL;
4808 struct stat sb2;
4809 struct got_diffreg_result *diffreg_result = NULL;
4810 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4811 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4813 *path_outfile = NULL;
4815 err = got_object_id_str(&id_str, blob_id);
4816 if (err)
4817 return err;
4819 if (dirfd2 != -1) {
4820 fd2 = openat(dirfd2, de_name2,
4821 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4822 if (fd2 == -1) {
4823 if (!got_err_open_nofollow_on_symlink()) {
4824 err = got_error_from_errno2("openat", path2);
4825 goto done;
4827 link_len = readlinkat(dirfd2, de_name2,
4828 link_target, sizeof(link_target));
4829 if (link_len == -1) {
4830 return got_error_from_errno2("readlinkat",
4831 path2);
4833 sb2.st_mode = S_IFLNK;
4834 sb2.st_size = link_len;
4836 } else {
4837 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4838 if (fd2 == -1) {
4839 if (!got_err_open_nofollow_on_symlink()) {
4840 err = got_error_from_errno2("open", path2);
4841 goto done;
4843 link_len = readlink(path2, link_target,
4844 sizeof(link_target));
4845 if (link_len == -1)
4846 return got_error_from_errno2("readlink", path2);
4847 sb2.st_mode = S_IFLNK;
4848 sb2.st_size = link_len;
4851 if (fd2 != -1) {
4852 if (fstat(fd2, &sb2) == -1) {
4853 err = got_error_from_errno2("fstat", path2);
4854 goto done;
4857 f2 = fdopen(fd2, "r");
4858 if (f2 == NULL) {
4859 err = got_error_from_errno2("fdopen", path2);
4860 goto done;
4862 fd2 = -1;
4863 } else {
4864 size_t n;
4865 f2 = got_opentemp();
4866 if (f2 == NULL) {
4867 err = got_error_from_errno2("got_opentemp", path2);
4868 goto done;
4870 n = fwrite(link_target, 1, link_len, f2);
4871 if (n != link_len) {
4872 err = got_ferror(f2, GOT_ERR_IO);
4873 goto done;
4875 if (fflush(f2) == EOF) {
4876 err = got_error_from_errno("fflush");
4877 goto done;
4879 rewind(f2);
4882 fd = got_opentempfd();
4883 if (fd == -1) {
4884 err = got_error_from_errno("got_opentempfd");
4885 goto done;
4888 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4889 if (err)
4890 goto done;
4892 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4893 if (err)
4894 goto done;
4896 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4897 if (err)
4898 goto done;
4900 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4901 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4902 if (err)
4903 goto done;
4905 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4906 "");
4907 if (err)
4908 goto done;
4910 if (fseek(f1, 0L, SEEK_SET) == -1)
4911 return got_ferror(f1, GOT_ERR_IO);
4912 if (fseek(f2, 0L, SEEK_SET) == -1)
4913 return got_ferror(f2, GOT_ERR_IO);
4915 /* Count the number of actual changes in the diff result. */
4916 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4917 struct diff_chunk_context cc = {};
4918 diff_chunk_context_load_change(&cc, &nchunks_used,
4919 diffreg_result->result, n, 0);
4920 nchanges++;
4922 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4923 int choice;
4924 err = apply_or_reject_change(&choice, &nchunks_used,
4925 diffreg_result->result, n, relpath, f1, f2,
4926 &line_cur1, &line_cur2,
4927 reverse_patch ? NULL : outfile,
4928 reverse_patch ? outfile : NULL,
4929 ++i, nchanges, patch_cb, patch_arg);
4930 if (err)
4931 goto done;
4932 if (choice == GOT_PATCH_CHOICE_YES)
4933 have_content = 1;
4934 else if (choice == GOT_PATCH_CHOICE_QUIT)
4935 break;
4937 if (have_content) {
4938 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4939 reverse_patch ? NULL : outfile,
4940 reverse_patch ? outfile : NULL);
4941 if (err)
4942 goto done;
4944 if (!S_ISLNK(sb2.st_mode)) {
4945 mode_t mode;
4947 mode = apply_umask(sb2.st_mode);
4948 if (fchmod(fileno(outfile), mode) == -1) {
4949 err = got_error_from_errno2("fchmod", path2);
4950 goto done;
4954 done:
4955 free(id_str);
4956 if (fd != -1 && close(fd) == -1 && err == NULL)
4957 err = got_error_from_errno("close");
4958 if (blob)
4959 got_object_blob_close(blob);
4960 free_err = got_diffreg_result_free(diffreg_result);
4961 if (err == NULL)
4962 err = free_err;
4963 if (f1 && fclose(f1) == EOF && err == NULL)
4964 err = got_error_from_errno2("fclose", path1);
4965 if (f2 && fclose(f2) == EOF && err == NULL)
4966 err = got_error_from_errno2("fclose", path2);
4967 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4968 err = got_error_from_errno2("close", path2);
4969 if (outfile && fclose(outfile) == EOF && err == NULL)
4970 err = got_error_from_errno2("fclose", *path_outfile);
4971 if (path1 && unlink(path1) == -1 && err == NULL)
4972 err = got_error_from_errno2("unlink", path1);
4973 if (err || !have_content) {
4974 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4975 err = got_error_from_errno2("unlink", *path_outfile);
4976 free(*path_outfile);
4977 *path_outfile = NULL;
4979 free(path1);
4980 return err;
4983 static const struct got_error *
4984 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4985 const char *relpath, struct got_object_id *blob_id,
4986 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4987 int dirfd, const char *de_name)
4989 struct revert_file_args *a = arg;
4990 const struct got_error *err = NULL;
4991 char *parent_path = NULL;
4992 struct got_fileindex_entry *ie;
4993 struct got_commit_object *base_commit = NULL;
4994 struct got_tree_object *tree = NULL;
4995 struct got_object_id *tree_id = NULL;
4996 const struct got_tree_entry *te = NULL;
4997 char *tree_path = NULL, *te_name;
4998 char *ondisk_path = NULL, *path_content = NULL;
4999 struct got_blob_object *blob = NULL;
5000 int fd = -1;
5002 /* Reverting a staged deletion is a no-op. */
5003 if (status == GOT_STATUS_DELETE &&
5004 staged_status != GOT_STATUS_NO_CHANGE)
5005 return NULL;
5007 if (status == GOT_STATUS_UNVERSIONED)
5008 return (*a->progress_cb)(a->progress_arg,
5009 GOT_STATUS_UNVERSIONED, relpath);
5011 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5012 if (ie == NULL)
5013 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5015 /* Construct in-repository path of tree which contains this blob. */
5016 err = got_path_dirname(&parent_path, ie->path);
5017 if (err) {
5018 if (err->code != GOT_ERR_BAD_PATH)
5019 goto done;
5020 parent_path = strdup("/");
5021 if (parent_path == NULL) {
5022 err = got_error_from_errno("strdup");
5023 goto done;
5026 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5027 tree_path = strdup(parent_path);
5028 if (tree_path == NULL) {
5029 err = got_error_from_errno("strdup");
5030 goto done;
5032 } else {
5033 if (got_path_is_root_dir(parent_path)) {
5034 tree_path = strdup(a->worktree->path_prefix);
5035 if (tree_path == NULL) {
5036 err = got_error_from_errno("strdup");
5037 goto done;
5039 } else {
5040 if (asprintf(&tree_path, "%s/%s",
5041 a->worktree->path_prefix, parent_path) == -1) {
5042 err = got_error_from_errno("asprintf");
5043 goto done;
5048 err = got_object_open_as_commit(&base_commit, a->repo,
5049 a->worktree->base_commit_id);
5050 if (err)
5051 goto done;
5053 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5054 if (err) {
5055 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5056 (status == GOT_STATUS_ADD ||
5057 staged_status == GOT_STATUS_ADD)))
5058 goto done;
5059 } else {
5060 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5061 if (err)
5062 goto done;
5064 err = got_path_basename(&te_name, ie->path);
5065 if (err)
5066 goto done;
5068 te = got_object_tree_find_entry(tree, te_name);
5069 free(te_name);
5070 if (te == NULL && status != GOT_STATUS_ADD &&
5071 staged_status != GOT_STATUS_ADD) {
5072 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5073 goto done;
5077 switch (status) {
5078 case GOT_STATUS_ADD:
5079 if (a->patch_cb) {
5080 int choice = GOT_PATCH_CHOICE_NONE;
5081 err = (*a->patch_cb)(&choice, a->patch_arg,
5082 status, ie->path, NULL, 1, 1);
5083 if (err)
5084 goto done;
5085 if (choice != GOT_PATCH_CHOICE_YES)
5086 break;
5088 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5089 ie->path);
5090 if (err)
5091 goto done;
5092 got_fileindex_entry_remove(a->fileindex, ie);
5093 if (a->unlink_added_files) {
5094 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5096 if (a->added_files_to_unlink) {
5097 struct got_pathlist_entry *pe;
5099 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5100 entry) {
5101 if (got_path_cmp(pe->path, relpath,
5102 pe->path_len, strlen(relpath)))
5103 continue;
5104 do_unlink = 1;
5105 break;
5109 if (do_unlink) {
5110 if (asprintf(&ondisk_path, "%s/%s",
5111 got_worktree_get_root_path(a->worktree),
5112 relpath) == -1) {
5113 err = got_error_from_errno("asprintf");
5114 goto done;
5116 if (unlink(ondisk_path) == -1) {
5117 err = got_error_from_errno2("unlink",
5118 ondisk_path);
5119 break;
5123 break;
5124 case GOT_STATUS_DELETE:
5125 if (a->patch_cb) {
5126 int choice = GOT_PATCH_CHOICE_NONE;
5127 err = (*a->patch_cb)(&choice, a->patch_arg,
5128 status, ie->path, NULL, 1, 1);
5129 if (err)
5130 goto done;
5131 if (choice != GOT_PATCH_CHOICE_YES)
5132 break;
5134 /* fall through */
5135 case GOT_STATUS_MODIFY:
5136 case GOT_STATUS_MODE_CHANGE:
5137 case GOT_STATUS_CONFLICT:
5138 case GOT_STATUS_MISSING: {
5139 struct got_object_id id;
5140 if (staged_status == GOT_STATUS_ADD ||
5141 staged_status == GOT_STATUS_MODIFY)
5142 got_fileindex_entry_get_staged_blob_id(&id, ie);
5143 else
5144 got_fileindex_entry_get_blob_id(&id, ie);
5145 fd = got_opentempfd();
5146 if (fd == -1) {
5147 err = got_error_from_errno("got_opentempfd");
5148 goto done;
5151 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5152 if (err)
5153 goto done;
5155 if (asprintf(&ondisk_path, "%s/%s",
5156 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5157 err = got_error_from_errno("asprintf");
5158 goto done;
5161 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5162 status == GOT_STATUS_CONFLICT)) {
5163 int is_bad_symlink = 0;
5164 err = create_patched_content(&path_content, 1, &id,
5165 ondisk_path, dirfd, de_name, ie->path, a->repo,
5166 a->patch_cb, a->patch_arg);
5167 if (err || path_content == NULL)
5168 break;
5169 if (te && S_ISLNK(te->mode)) {
5170 if (unlink(path_content) == -1) {
5171 err = got_error_from_errno2("unlink",
5172 path_content);
5173 break;
5175 err = install_symlink(&is_bad_symlink,
5176 a->worktree, ondisk_path, ie->path,
5177 blob, 0, 1, 0, 0, a->repo,
5178 a->progress_cb, a->progress_arg);
5179 } else {
5180 if (rename(path_content, ondisk_path) == -1) {
5181 err = got_error_from_errno3("rename",
5182 path_content, ondisk_path);
5183 goto done;
5186 } else {
5187 int is_bad_symlink = 0;
5188 if (te && S_ISLNK(te->mode)) {
5189 err = install_symlink(&is_bad_symlink,
5190 a->worktree, ondisk_path, ie->path,
5191 blob, 0, 1, 0, 0, a->repo,
5192 a->progress_cb, a->progress_arg);
5193 } else {
5194 err = install_blob(a->worktree, ondisk_path,
5195 ie->path,
5196 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5197 got_fileindex_perms_to_st(ie), blob,
5198 0, 1, 0, 0, a->repo,
5199 a->progress_cb, a->progress_arg);
5201 if (err)
5202 goto done;
5203 if (status == GOT_STATUS_DELETE ||
5204 status == GOT_STATUS_MODE_CHANGE) {
5205 err = got_fileindex_entry_update(ie,
5206 a->worktree->root_fd, relpath,
5207 blob->id.sha1,
5208 a->worktree->base_commit_id->sha1, 1);
5209 if (err)
5210 goto done;
5212 if (is_bad_symlink) {
5213 got_fileindex_entry_filetype_set(ie,
5214 GOT_FILEIDX_MODE_BAD_SYMLINK);
5217 break;
5219 default:
5220 break;
5222 done:
5223 free(ondisk_path);
5224 free(path_content);
5225 free(parent_path);
5226 free(tree_path);
5227 if (fd != -1 && close(fd) == -1 && err == NULL)
5228 err = got_error_from_errno("close");
5229 if (blob)
5230 got_object_blob_close(blob);
5231 if (tree)
5232 got_object_tree_close(tree);
5233 free(tree_id);
5234 if (base_commit)
5235 got_object_commit_close(base_commit);
5236 return err;
5239 const struct got_error *
5240 got_worktree_revert(struct got_worktree *worktree,
5241 struct got_pathlist_head *paths,
5242 got_worktree_checkout_cb progress_cb, void *progress_arg,
5243 got_worktree_patch_cb patch_cb, void *patch_arg,
5244 struct got_repository *repo)
5246 struct got_fileindex *fileindex = NULL;
5247 char *fileindex_path = NULL;
5248 const struct got_error *err = NULL, *unlockerr = NULL;
5249 const struct got_error *sync_err = NULL;
5250 struct got_pathlist_entry *pe;
5251 struct revert_file_args rfa;
5253 err = lock_worktree(worktree, LOCK_EX);
5254 if (err)
5255 return err;
5257 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5258 if (err)
5259 goto done;
5261 rfa.worktree = worktree;
5262 rfa.fileindex = fileindex;
5263 rfa.progress_cb = progress_cb;
5264 rfa.progress_arg = progress_arg;
5265 rfa.patch_cb = patch_cb;
5266 rfa.patch_arg = patch_arg;
5267 rfa.repo = repo;
5268 rfa.unlink_added_files = 0;
5269 TAILQ_FOREACH(pe, paths, entry) {
5270 err = worktree_status(worktree, pe->path, fileindex, repo,
5271 revert_file, &rfa, NULL, NULL, 1, 0);
5272 if (err)
5273 break;
5275 sync_err = sync_fileindex(fileindex, fileindex_path);
5276 if (sync_err && err == NULL)
5277 err = sync_err;
5278 done:
5279 free(fileindex_path);
5280 if (fileindex)
5281 got_fileindex_free(fileindex);
5282 unlockerr = lock_worktree(worktree, LOCK_SH);
5283 if (unlockerr && err == NULL)
5284 err = unlockerr;
5285 return err;
5288 static void
5289 free_commitable(struct got_commitable *ct)
5291 free(ct->path);
5292 free(ct->in_repo_path);
5293 free(ct->ondisk_path);
5294 free(ct->blob_id);
5295 free(ct->base_blob_id);
5296 free(ct->staged_blob_id);
5297 free(ct->base_commit_id);
5298 free(ct);
5301 struct collect_commitables_arg {
5302 struct got_pathlist_head *commitable_paths;
5303 struct got_repository *repo;
5304 struct got_worktree *worktree;
5305 struct got_fileindex *fileindex;
5306 int have_staged_files;
5307 int allow_bad_symlinks;
5308 int diff_header_shown;
5309 int commit_conflicts;
5310 FILE *diff_outfile;
5311 FILE *f1;
5312 FILE *f2;
5316 * Create a file which contains the target path of a symlink so we can feed
5317 * it as content to the diff engine.
5319 static const struct got_error *
5320 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5321 const char *abspath)
5323 const struct got_error *err = NULL;
5324 char target_path[PATH_MAX];
5325 ssize_t target_len, outlen;
5327 *fd = -1;
5329 if (dirfd != -1) {
5330 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5331 if (target_len == -1)
5332 return got_error_from_errno2("readlinkat", abspath);
5333 } else {
5334 target_len = readlink(abspath, target_path, PATH_MAX);
5335 if (target_len == -1)
5336 return got_error_from_errno2("readlink", abspath);
5339 *fd = got_opentempfd();
5340 if (*fd == -1)
5341 return got_error_from_errno("got_opentempfd");
5343 outlen = write(*fd, target_path, target_len);
5344 if (outlen == -1) {
5345 err = got_error_from_errno("got_opentempfd");
5346 goto done;
5349 if (lseek(*fd, 0, SEEK_SET) == -1) {
5350 err = got_error_from_errno2("lseek", abspath);
5351 goto done;
5353 done:
5354 if (err) {
5355 close(*fd);
5356 *fd = -1;
5358 return err;
5361 static const struct got_error *
5362 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5363 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5364 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5366 const struct got_error *err = NULL;
5367 struct got_blob_object *blob1 = NULL;
5368 int fd = -1, fd1 = -1, fd2 = -1;
5369 FILE *ondisk_file = NULL;
5370 char *label1 = NULL;
5371 struct stat sb;
5372 off_t size1 = 0;
5373 int f2_exists = 0;
5374 char *id_str = NULL;
5376 memset(&sb, 0, sizeof(sb));
5378 if (diff_staged) {
5379 if (ct->staged_status != GOT_STATUS_MODIFY &&
5380 ct->staged_status != GOT_STATUS_ADD &&
5381 ct->staged_status != GOT_STATUS_DELETE)
5382 return NULL;
5383 } else {
5384 if (ct->status != GOT_STATUS_MODIFY &&
5385 ct->status != GOT_STATUS_ADD &&
5386 ct->status != GOT_STATUS_DELETE &&
5387 ct->status != GOT_STATUS_CONFLICT)
5388 return NULL;
5391 err = got_opentemp_truncate(f1);
5392 if (err)
5393 return got_error_from_errno("got_opentemp_truncate");
5394 err = got_opentemp_truncate(f2);
5395 if (err)
5396 return got_error_from_errno("got_opentemp_truncate");
5398 if (!*diff_header_shown) {
5399 err = got_object_id_str(&id_str, worktree->base_commit_id);
5400 if (err)
5401 return err;
5402 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5403 got_worktree_get_root_path(worktree));
5404 fprintf(diff_outfile, "commit - %s\n", id_str);
5405 fprintf(diff_outfile, "path + %s%s\n",
5406 got_worktree_get_root_path(worktree),
5407 diff_staged ? " (staged changes)" : "");
5408 *diff_header_shown = 1;
5411 if (diff_staged) {
5412 const char *label1 = NULL, *label2 = NULL;
5413 switch (ct->staged_status) {
5414 case GOT_STATUS_MODIFY:
5415 label1 = ct->path;
5416 label2 = ct->path;
5417 break;
5418 case GOT_STATUS_ADD:
5419 label2 = ct->path;
5420 break;
5421 case GOT_STATUS_DELETE:
5422 label1 = ct->path;
5423 break;
5424 default:
5425 return got_error(GOT_ERR_FILE_STATUS);
5427 fd1 = got_opentempfd();
5428 if (fd1 == -1) {
5429 err = got_error_from_errno("got_opentempfd");
5430 goto done;
5432 fd2 = got_opentempfd();
5433 if (fd2 == -1) {
5434 err = got_error_from_errno("got_opentempfd");
5435 goto done;
5437 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5438 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5439 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5440 NULL, repo, diff_outfile);
5441 goto done;
5444 fd1 = got_opentempfd();
5445 if (fd1 == -1) {
5446 err = got_error_from_errno("got_opentempfd");
5447 goto done;
5450 if (ct->status != GOT_STATUS_ADD) {
5451 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5452 8192, fd1);
5453 if (err)
5454 goto done;
5457 if (ct->status != GOT_STATUS_DELETE) {
5458 if (dirfd != -1) {
5459 fd = openat(dirfd, de_name,
5460 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5461 if (fd == -1) {
5462 if (!got_err_open_nofollow_on_symlink()) {
5463 err = got_error_from_errno2("openat",
5464 ct->ondisk_path);
5465 goto done;
5467 err = get_symlink_target_file(&fd, dirfd,
5468 de_name, ct->ondisk_path);
5469 if (err)
5470 goto done;
5472 } else {
5473 fd = open(ct->ondisk_path,
5474 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5475 if (fd == -1) {
5476 if (!got_err_open_nofollow_on_symlink()) {
5477 err = got_error_from_errno2("open",
5478 ct->ondisk_path);
5479 goto done;
5481 err = get_symlink_target_file(&fd, dirfd,
5482 de_name, ct->ondisk_path);
5483 if (err)
5484 goto done;
5487 if (fstatat(fd, ct->ondisk_path, &sb,
5488 AT_SYMLINK_NOFOLLOW) == -1) {
5489 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5490 goto done;
5492 ondisk_file = fdopen(fd, "r");
5493 if (ondisk_file == NULL) {
5494 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5495 goto done;
5497 fd = -1;
5498 f2_exists = 1;
5501 if (blob1) {
5502 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5503 f1, blob1);
5504 if (err)
5505 goto done;
5508 err = got_diff_blob_file(blob1, f1, size1, label1,
5509 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5510 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5511 done:
5512 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5513 err = got_error_from_errno("close");
5514 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5515 err = got_error_from_errno("close");
5516 if (blob1)
5517 got_object_blob_close(blob1);
5518 if (fd != -1 && close(fd) == -1 && err == NULL)
5519 err = got_error_from_errno("close");
5520 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5521 err = got_error_from_errno("fclose");
5522 return err;
5525 static const struct got_error *
5526 collect_commitables(void *arg, unsigned char status,
5527 unsigned char staged_status, const char *relpath,
5528 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5529 struct got_object_id *commit_id, int dirfd, const char *de_name)
5531 struct collect_commitables_arg *a = arg;
5532 const struct got_error *err = NULL;
5533 struct got_commitable *ct = NULL;
5534 struct got_pathlist_entry *new = NULL;
5535 char *parent_path = NULL, *path = NULL;
5536 struct stat sb;
5538 if (a->have_staged_files) {
5539 if (staged_status != GOT_STATUS_MODIFY &&
5540 staged_status != GOT_STATUS_ADD &&
5541 staged_status != GOT_STATUS_DELETE)
5542 return NULL;
5543 } else {
5544 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5545 printf("C %s\n", relpath);
5546 return got_error(GOT_ERR_COMMIT_CONFLICT);
5549 if (status != GOT_STATUS_MODIFY &&
5550 status != GOT_STATUS_MODE_CHANGE &&
5551 status != GOT_STATUS_ADD &&
5552 status != GOT_STATUS_DELETE &&
5553 status != GOT_STATUS_CONFLICT)
5554 return NULL;
5557 if (asprintf(&path, "/%s", relpath) == -1) {
5558 err = got_error_from_errno("asprintf");
5559 goto done;
5561 if (strcmp(path, "/") == 0) {
5562 parent_path = strdup("");
5563 if (parent_path == NULL)
5564 return got_error_from_errno("strdup");
5565 } else {
5566 err = got_path_dirname(&parent_path, path);
5567 if (err)
5568 return err;
5571 ct = calloc(1, sizeof(*ct));
5572 if (ct == NULL) {
5573 err = got_error_from_errno("calloc");
5574 goto done;
5577 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5578 relpath) == -1) {
5579 err = got_error_from_errno("asprintf");
5580 goto done;
5583 if (staged_status == GOT_STATUS_ADD ||
5584 staged_status == GOT_STATUS_MODIFY) {
5585 struct got_fileindex_entry *ie;
5586 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5587 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5588 case GOT_FILEIDX_MODE_REGULAR_FILE:
5589 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5590 ct->mode = S_IFREG;
5591 break;
5592 case GOT_FILEIDX_MODE_SYMLINK:
5593 ct->mode = S_IFLNK;
5594 break;
5595 default:
5596 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5597 goto done;
5599 ct->mode |= got_fileindex_entry_perms_get(ie);
5600 } else if (status != GOT_STATUS_DELETE &&
5601 staged_status != GOT_STATUS_DELETE) {
5602 if (dirfd != -1) {
5603 if (fstatat(dirfd, de_name, &sb,
5604 AT_SYMLINK_NOFOLLOW) == -1) {
5605 err = got_error_from_errno2("fstatat",
5606 ct->ondisk_path);
5607 goto done;
5609 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5610 err = got_error_from_errno2("lstat", ct->ondisk_path);
5611 goto done;
5613 ct->mode = sb.st_mode;
5616 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5617 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5618 relpath) == -1) {
5619 err = got_error_from_errno("asprintf");
5620 goto done;
5623 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5624 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5625 int is_bad_symlink;
5626 char target_path[PATH_MAX];
5627 ssize_t target_len;
5628 target_len = readlink(ct->ondisk_path, target_path,
5629 sizeof(target_path));
5630 if (target_len == -1) {
5631 err = got_error_from_errno2("readlink",
5632 ct->ondisk_path);
5633 goto done;
5635 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5636 target_len, ct->ondisk_path, a->worktree->root_path,
5637 a->worktree->meta_dir);
5638 if (err)
5639 goto done;
5640 if (is_bad_symlink) {
5641 err = got_error_path(ct->ondisk_path,
5642 GOT_ERR_BAD_SYMLINK);
5643 goto done;
5648 ct->status = status;
5649 ct->staged_status = staged_status;
5650 ct->blob_id = NULL; /* will be filled in when blob gets created */
5651 if (ct->status != GOT_STATUS_ADD &&
5652 ct->staged_status != GOT_STATUS_ADD) {
5653 ct->base_blob_id = got_object_id_dup(blob_id);
5654 if (ct->base_blob_id == NULL) {
5655 err = got_error_from_errno("got_object_id_dup");
5656 goto done;
5658 ct->base_commit_id = got_object_id_dup(commit_id);
5659 if (ct->base_commit_id == NULL) {
5660 err = got_error_from_errno("got_object_id_dup");
5661 goto done;
5664 if (ct->staged_status == GOT_STATUS_ADD ||
5665 ct->staged_status == GOT_STATUS_MODIFY) {
5666 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5667 if (ct->staged_blob_id == NULL) {
5668 err = got_error_from_errno("got_object_id_dup");
5669 goto done;
5672 ct->path = strdup(path);
5673 if (ct->path == NULL) {
5674 err = got_error_from_errno("strdup");
5675 goto done;
5677 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5678 if (err)
5679 goto done;
5681 if (a->diff_outfile && ct && new != NULL) {
5682 err = append_ct_diff(ct, &a->diff_header_shown,
5683 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5684 a->have_staged_files, a->repo, a->worktree);
5685 if (err)
5686 goto done;
5688 done:
5689 if (ct && (err || new == NULL))
5690 free_commitable(ct);
5691 free(parent_path);
5692 free(path);
5693 return err;
5696 static const struct got_error *write_tree(struct got_object_id **, int *,
5697 struct got_tree_object *, const char *, struct got_pathlist_head *,
5698 got_worktree_status_cb status_cb, void *status_arg,
5699 struct got_repository *);
5701 static const struct got_error *
5702 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5703 struct got_tree_entry *te, const char *parent_path,
5704 struct got_pathlist_head *commitable_paths,
5705 got_worktree_status_cb status_cb, void *status_arg,
5706 struct got_repository *repo)
5708 const struct got_error *err = NULL;
5709 struct got_tree_object *subtree;
5710 char *subpath;
5712 if (asprintf(&subpath, "%s%s%s", parent_path,
5713 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5714 return got_error_from_errno("asprintf");
5716 err = got_object_open_as_tree(&subtree, repo, &te->id);
5717 if (err)
5718 return err;
5720 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5721 commitable_paths, status_cb, status_arg, repo);
5722 got_object_tree_close(subtree);
5723 free(subpath);
5724 return err;
5727 static const struct got_error *
5728 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5730 const struct got_error *err = NULL;
5731 char *ct_parent_path = NULL;
5733 *match = 0;
5735 if (strchr(ct->in_repo_path, '/') == NULL) {
5736 *match = got_path_is_root_dir(path);
5737 return NULL;
5740 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5741 if (err)
5742 return err;
5743 *match = (strcmp(path, ct_parent_path) == 0);
5744 free(ct_parent_path);
5745 return err;
5748 static mode_t
5749 get_ct_file_mode(struct got_commitable *ct)
5751 if (S_ISLNK(ct->mode))
5752 return S_IFLNK;
5754 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5757 static const struct got_error *
5758 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5759 struct got_tree_entry *te, struct got_commitable *ct)
5761 const struct got_error *err = NULL;
5763 *new_te = NULL;
5765 err = got_object_tree_entry_dup(new_te, te);
5766 if (err)
5767 goto done;
5769 (*new_te)->mode = get_ct_file_mode(ct);
5771 if (ct->staged_status == GOT_STATUS_MODIFY)
5772 memcpy(&(*new_te)->id, ct->staged_blob_id,
5773 sizeof((*new_te)->id));
5774 else
5775 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5776 done:
5777 if (err && *new_te) {
5778 free(*new_te);
5779 *new_te = NULL;
5781 return err;
5784 static const struct got_error *
5785 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5786 struct got_commitable *ct)
5788 const struct got_error *err = NULL;
5789 char *ct_name = NULL;
5791 *new_te = NULL;
5793 *new_te = calloc(1, sizeof(**new_te));
5794 if (*new_te == NULL)
5795 return got_error_from_errno("calloc");
5797 err = got_path_basename(&ct_name, ct->path);
5798 if (err)
5799 goto done;
5800 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5801 sizeof((*new_te)->name)) {
5802 err = got_error(GOT_ERR_NO_SPACE);
5803 goto done;
5806 (*new_te)->mode = get_ct_file_mode(ct);
5808 if (ct->staged_status == GOT_STATUS_ADD)
5809 memcpy(&(*new_te)->id, ct->staged_blob_id,
5810 sizeof((*new_te)->id));
5811 else
5812 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5813 done:
5814 free(ct_name);
5815 if (err && *new_te) {
5816 free(*new_te);
5817 *new_te = NULL;
5819 return err;
5822 static const struct got_error *
5823 insert_tree_entry(struct got_tree_entry *new_te,
5824 struct got_pathlist_head *paths)
5826 const struct got_error *err = NULL;
5827 struct got_pathlist_entry *new_pe;
5829 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5830 if (err)
5831 return err;
5832 if (new_pe == NULL)
5833 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5834 return NULL;
5837 static const struct got_error *
5838 report_ct_status(struct got_commitable *ct,
5839 got_worktree_status_cb status_cb, void *status_arg)
5841 const char *ct_path = ct->path;
5842 unsigned char status;
5844 if (status_cb == NULL) /* no commit progress output desired */
5845 return NULL;
5847 while (ct_path[0] == '/')
5848 ct_path++;
5850 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5851 status = ct->staged_status;
5852 else
5853 status = ct->status;
5855 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5856 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5859 static const struct got_error *
5860 match_modified_subtree(int *modified, struct got_tree_entry *te,
5861 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5863 const struct got_error *err = NULL;
5864 struct got_pathlist_entry *pe;
5865 char *te_path;
5867 *modified = 0;
5869 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5870 got_path_is_root_dir(base_tree_path) ? "" : "/",
5871 te->name) == -1)
5872 return got_error_from_errno("asprintf");
5874 TAILQ_FOREACH(pe, commitable_paths, entry) {
5875 struct got_commitable *ct = pe->data;
5876 *modified = got_path_is_child(ct->in_repo_path, te_path,
5877 strlen(te_path));
5878 if (*modified)
5879 break;
5882 free(te_path);
5883 return err;
5886 static const struct got_error *
5887 match_deleted_or_modified_ct(struct got_commitable **ctp,
5888 struct got_tree_entry *te, const char *base_tree_path,
5889 struct got_pathlist_head *commitable_paths)
5891 const struct got_error *err = NULL;
5892 struct got_pathlist_entry *pe;
5894 *ctp = NULL;
5896 TAILQ_FOREACH(pe, commitable_paths, entry) {
5897 struct got_commitable *ct = pe->data;
5898 char *ct_name = NULL;
5899 int path_matches;
5901 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5902 if (ct->status != GOT_STATUS_MODIFY &&
5903 ct->status != GOT_STATUS_MODE_CHANGE &&
5904 ct->status != GOT_STATUS_DELETE &&
5905 ct->status != GOT_STATUS_CONFLICT)
5906 continue;
5907 } else {
5908 if (ct->staged_status != GOT_STATUS_MODIFY &&
5909 ct->staged_status != GOT_STATUS_DELETE)
5910 continue;
5913 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5914 continue;
5916 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5917 if (err)
5918 return err;
5919 if (!path_matches)
5920 continue;
5922 err = got_path_basename(&ct_name, pe->path);
5923 if (err)
5924 return err;
5926 if (strcmp(te->name, ct_name) != 0) {
5927 free(ct_name);
5928 continue;
5930 free(ct_name);
5932 *ctp = ct;
5933 break;
5936 return err;
5939 static const struct got_error *
5940 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5941 const char *child_path, const char *path_base_tree,
5942 struct got_pathlist_head *commitable_paths,
5943 got_worktree_status_cb status_cb, void *status_arg,
5944 struct got_repository *repo)
5946 const struct got_error *err = NULL;
5947 struct got_tree_entry *new_te;
5948 char *subtree_path;
5949 struct got_object_id *id = NULL;
5950 int nentries;
5952 *new_tep = NULL;
5954 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5955 got_path_is_root_dir(path_base_tree) ? "" : "/",
5956 child_path) == -1)
5957 return got_error_from_errno("asprintf");
5959 new_te = calloc(1, sizeof(*new_te));
5960 if (new_te == NULL)
5961 return got_error_from_errno("calloc");
5962 new_te->mode = S_IFDIR;
5964 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5965 sizeof(new_te->name)) {
5966 err = got_error(GOT_ERR_NO_SPACE);
5967 goto done;
5969 err = write_tree(&id, &nentries, NULL, subtree_path,
5970 commitable_paths, status_cb, status_arg, repo);
5971 if (err) {
5972 free(new_te);
5973 goto done;
5975 memcpy(&new_te->id, id, sizeof(new_te->id));
5976 done:
5977 free(id);
5978 free(subtree_path);
5979 if (err == NULL)
5980 *new_tep = new_te;
5981 return err;
5984 static const struct got_error *
5985 write_tree(struct got_object_id **new_tree_id, int *nentries,
5986 struct got_tree_object *base_tree, const char *path_base_tree,
5987 struct got_pathlist_head *commitable_paths,
5988 got_worktree_status_cb status_cb, void *status_arg,
5989 struct got_repository *repo)
5991 const struct got_error *err = NULL;
5992 struct got_pathlist_head paths;
5993 struct got_tree_entry *te, *new_te = NULL;
5994 struct got_pathlist_entry *pe;
5996 TAILQ_INIT(&paths);
5997 *nentries = 0;
5999 /* Insert, and recurse into, newly added entries first. */
6000 TAILQ_FOREACH(pe, commitable_paths, entry) {
6001 struct got_commitable *ct = pe->data;
6002 char *child_path = NULL, *slash;
6004 if ((ct->status != GOT_STATUS_ADD &&
6005 ct->staged_status != GOT_STATUS_ADD) ||
6006 (ct->flags & GOT_COMMITABLE_ADDED))
6007 continue;
6009 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6010 strlen(path_base_tree)))
6011 continue;
6013 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6014 ct->in_repo_path);
6015 if (err)
6016 goto done;
6018 slash = strchr(child_path, '/');
6019 if (slash == NULL) {
6020 err = alloc_added_blob_tree_entry(&new_te, ct);
6021 if (err)
6022 goto done;
6023 err = report_ct_status(ct, status_cb, status_arg);
6024 if (err)
6025 goto done;
6026 ct->flags |= GOT_COMMITABLE_ADDED;
6027 err = insert_tree_entry(new_te, &paths);
6028 if (err)
6029 goto done;
6030 (*nentries)++;
6031 } else {
6032 *slash = '\0'; /* trim trailing path components */
6033 if (base_tree == NULL ||
6034 got_object_tree_find_entry(base_tree, child_path)
6035 == NULL) {
6036 err = make_subtree_for_added_blob(&new_te,
6037 child_path, path_base_tree,
6038 commitable_paths, status_cb, status_arg,
6039 repo);
6040 if (err)
6041 goto done;
6042 err = insert_tree_entry(new_te, &paths);
6043 if (err)
6044 goto done;
6045 (*nentries)++;
6050 if (base_tree) {
6051 int i, nbase_entries;
6052 /* Handle modified and deleted entries. */
6053 nbase_entries = got_object_tree_get_nentries(base_tree);
6054 for (i = 0; i < nbase_entries; i++) {
6055 struct got_commitable *ct = NULL;
6057 te = got_object_tree_get_entry(base_tree, i);
6058 if (got_object_tree_entry_is_submodule(te)) {
6059 /* Entry is a submodule; just copy it. */
6060 err = got_object_tree_entry_dup(&new_te, te);
6061 if (err)
6062 goto done;
6063 err = insert_tree_entry(new_te, &paths);
6064 if (err)
6065 goto done;
6066 (*nentries)++;
6067 continue;
6070 if (S_ISDIR(te->mode)) {
6071 int modified;
6072 err = got_object_tree_entry_dup(&new_te, te);
6073 if (err)
6074 goto done;
6075 err = match_modified_subtree(&modified, te,
6076 path_base_tree, commitable_paths);
6077 if (err)
6078 goto done;
6079 /* Avoid recursion into unmodified subtrees. */
6080 if (modified) {
6081 struct got_object_id *new_id;
6082 int nsubentries;
6083 err = write_subtree(&new_id,
6084 &nsubentries, te,
6085 path_base_tree, commitable_paths,
6086 status_cb, status_arg, repo);
6087 if (err)
6088 goto done;
6089 if (nsubentries == 0) {
6090 /* All entries were deleted. */
6091 free(new_id);
6092 continue;
6094 memcpy(&new_te->id, new_id,
6095 sizeof(new_te->id));
6096 free(new_id);
6098 err = insert_tree_entry(new_te, &paths);
6099 if (err)
6100 goto done;
6101 (*nentries)++;
6102 continue;
6105 err = match_deleted_or_modified_ct(&ct, te,
6106 path_base_tree, commitable_paths);
6107 if (err)
6108 goto done;
6109 if (ct) {
6110 /* NB: Deleted entries get dropped here. */
6111 if (ct->status == GOT_STATUS_MODIFY ||
6112 ct->status == GOT_STATUS_MODE_CHANGE ||
6113 ct->status == GOT_STATUS_CONFLICT ||
6114 ct->staged_status == GOT_STATUS_MODIFY) {
6115 err = alloc_modified_blob_tree_entry(
6116 &new_te, te, ct);
6117 if (err)
6118 goto done;
6119 err = insert_tree_entry(new_te, &paths);
6120 if (err)
6121 goto done;
6122 (*nentries)++;
6124 err = report_ct_status(ct, status_cb,
6125 status_arg);
6126 if (err)
6127 goto done;
6128 } else {
6129 /* Entry is unchanged; just copy it. */
6130 err = got_object_tree_entry_dup(&new_te, te);
6131 if (err)
6132 goto done;
6133 err = insert_tree_entry(new_te, &paths);
6134 if (err)
6135 goto done;
6136 (*nentries)++;
6141 /* Write new list of entries; deleted entries have been dropped. */
6142 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6143 done:
6144 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6145 return err;
6148 static const struct got_error *
6149 update_fileindex_after_commit(struct got_worktree *worktree,
6150 struct got_pathlist_head *commitable_paths,
6151 struct got_object_id *new_base_commit_id,
6152 struct got_fileindex *fileindex, int have_staged_files)
6154 const struct got_error *err = NULL;
6155 struct got_pathlist_entry *pe;
6156 char *relpath = NULL;
6158 TAILQ_FOREACH(pe, commitable_paths, entry) {
6159 struct got_fileindex_entry *ie;
6160 struct got_commitable *ct = pe->data;
6162 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6164 err = got_path_skip_common_ancestor(&relpath,
6165 worktree->root_path, ct->ondisk_path);
6166 if (err)
6167 goto done;
6169 if (ie) {
6170 if (ct->status == GOT_STATUS_DELETE ||
6171 ct->staged_status == GOT_STATUS_DELETE) {
6172 got_fileindex_entry_remove(fileindex, ie);
6173 } else if (ct->staged_status == GOT_STATUS_ADD ||
6174 ct->staged_status == GOT_STATUS_MODIFY) {
6175 got_fileindex_entry_stage_set(ie,
6176 GOT_FILEIDX_STAGE_NONE);
6177 got_fileindex_entry_staged_filetype_set(ie, 0);
6179 err = got_fileindex_entry_update(ie,
6180 worktree->root_fd, relpath,
6181 ct->staged_blob_id->sha1,
6182 new_base_commit_id->sha1,
6183 !have_staged_files);
6184 } else
6185 err = got_fileindex_entry_update(ie,
6186 worktree->root_fd, relpath,
6187 ct->blob_id->sha1,
6188 new_base_commit_id->sha1,
6189 !have_staged_files);
6190 } else {
6191 err = got_fileindex_entry_alloc(&ie, pe->path);
6192 if (err)
6193 goto done;
6194 err = got_fileindex_entry_update(ie,
6195 worktree->root_fd, relpath, ct->blob_id->sha1,
6196 new_base_commit_id->sha1, 1);
6197 if (err) {
6198 got_fileindex_entry_free(ie);
6199 goto done;
6201 err = got_fileindex_entry_add(fileindex, ie);
6202 if (err) {
6203 got_fileindex_entry_free(ie);
6204 goto done;
6207 free(relpath);
6208 relpath = NULL;
6210 done:
6211 free(relpath);
6212 return err;
6216 static const struct got_error *
6217 check_out_of_date(const char *in_repo_path, unsigned char status,
6218 unsigned char staged_status, struct got_object_id *base_blob_id,
6219 struct got_object_id *base_commit_id,
6220 struct got_object_id *head_commit_id, struct got_repository *repo,
6221 int ood_errcode)
6223 const struct got_error *err = NULL;
6224 struct got_commit_object *commit = NULL;
6225 struct got_object_id *id = NULL;
6227 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6228 /* Trivial case: base commit == head commit */
6229 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6230 return NULL;
6232 * Ensure file content which local changes were based
6233 * on matches file content in the branch head.
6235 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6236 if (err)
6237 goto done;
6238 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6239 if (err) {
6240 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6241 err = got_error(ood_errcode);
6242 goto done;
6243 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6244 err = got_error(ood_errcode);
6245 } else {
6246 /* Require that added files don't exist in the branch head. */
6247 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6248 if (err)
6249 goto done;
6250 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6251 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6252 goto done;
6253 err = id ? got_error(ood_errcode) : NULL;
6255 done:
6256 free(id);
6257 if (commit)
6258 got_object_commit_close(commit);
6259 return err;
6262 static const struct got_error *
6263 commit_worktree(struct got_object_id **new_commit_id,
6264 struct got_pathlist_head *commitable_paths,
6265 struct got_object_id *head_commit_id,
6266 struct got_object_id *parent_id2,
6267 struct got_worktree *worktree,
6268 const char *author, const char *committer, char *diff_path,
6269 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6270 got_worktree_status_cb status_cb, void *status_arg,
6271 struct got_repository *repo)
6273 const struct got_error *err = NULL, *unlockerr = NULL;
6274 struct got_pathlist_entry *pe;
6275 const char *head_ref_name = NULL;
6276 struct got_commit_object *head_commit = NULL;
6277 struct got_reference *head_ref2 = NULL;
6278 struct got_object_id *head_commit_id2 = NULL;
6279 struct got_tree_object *head_tree = NULL;
6280 struct got_object_id *new_tree_id = NULL;
6281 int nentries, nparents = 0;
6282 struct got_object_id_queue parent_ids;
6283 struct got_object_qid *pid = NULL;
6284 char *logmsg = NULL;
6285 time_t timestamp;
6287 *new_commit_id = NULL;
6289 STAILQ_INIT(&parent_ids);
6291 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6292 if (err)
6293 goto done;
6295 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6296 if (err)
6297 goto done;
6299 if (commit_msg_cb != NULL) {
6300 err = commit_msg_cb(commitable_paths, diff_path,
6301 &logmsg, commit_arg);
6302 if (err)
6303 goto done;
6306 if (logmsg == NULL || strlen(logmsg) == 0) {
6307 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6308 goto done;
6311 /* Create blobs from added and modified files and record their IDs. */
6312 TAILQ_FOREACH(pe, commitable_paths, entry) {
6313 struct got_commitable *ct = pe->data;
6314 char *ondisk_path;
6316 /* Blobs for staged files already exist. */
6317 if (ct->staged_status == GOT_STATUS_ADD ||
6318 ct->staged_status == GOT_STATUS_MODIFY)
6319 continue;
6321 if (ct->status != GOT_STATUS_ADD &&
6322 ct->status != GOT_STATUS_MODIFY &&
6323 ct->status != GOT_STATUS_MODE_CHANGE &&
6324 ct->status != GOT_STATUS_CONFLICT)
6325 continue;
6327 if (asprintf(&ondisk_path, "%s/%s",
6328 worktree->root_path, pe->path) == -1) {
6329 err = got_error_from_errno("asprintf");
6330 goto done;
6332 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6333 free(ondisk_path);
6334 if (err)
6335 goto done;
6338 /* Recursively write new tree objects. */
6339 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6340 commitable_paths, status_cb, status_arg, repo);
6341 if (err)
6342 goto done;
6344 err = got_object_qid_alloc(&pid, head_commit_id);
6345 if (err)
6346 goto done;
6347 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6348 nparents++;
6349 if (parent_id2) {
6350 err = got_object_qid_alloc(&pid, parent_id2);
6351 if (err)
6352 goto done;
6353 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6354 nparents++;
6356 timestamp = time(NULL);
6357 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6358 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6359 if (logmsg != NULL)
6360 free(logmsg);
6361 if (err)
6362 goto done;
6364 /* Check if a concurrent commit to our branch has occurred. */
6365 head_ref_name = got_worktree_get_head_ref_name(worktree);
6366 if (head_ref_name == NULL) {
6367 err = got_error_from_errno("got_worktree_get_head_ref_name");
6368 goto done;
6370 /* Lock the reference here to prevent concurrent modification. */
6371 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6372 if (err)
6373 goto done;
6374 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6375 if (err)
6376 goto done;
6377 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6378 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6379 goto done;
6381 /* Update branch head in repository. */
6382 err = got_ref_change_ref(head_ref2, *new_commit_id);
6383 if (err)
6384 goto done;
6385 err = got_ref_write(head_ref2, repo);
6386 if (err)
6387 goto done;
6389 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6390 if (err)
6391 goto done;
6393 err = ref_base_commit(worktree, repo);
6394 if (err)
6395 goto done;
6396 done:
6397 got_object_id_queue_free(&parent_ids);
6398 if (head_tree)
6399 got_object_tree_close(head_tree);
6400 if (head_commit)
6401 got_object_commit_close(head_commit);
6402 free(head_commit_id2);
6403 if (head_ref2) {
6404 unlockerr = got_ref_unlock(head_ref2);
6405 if (unlockerr && err == NULL)
6406 err = unlockerr;
6407 got_ref_close(head_ref2);
6409 return err;
6412 static const struct got_error *
6413 check_path_is_commitable(const char *path,
6414 struct got_pathlist_head *commitable_paths)
6416 struct got_pathlist_entry *cpe = NULL;
6417 size_t path_len = strlen(path);
6419 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6420 struct got_commitable *ct = cpe->data;
6421 const char *ct_path = ct->path;
6423 while (ct_path[0] == '/')
6424 ct_path++;
6426 if (strcmp(path, ct_path) == 0 ||
6427 got_path_is_child(ct_path, path, path_len))
6428 break;
6431 if (cpe == NULL)
6432 return got_error_path(path, GOT_ERR_BAD_PATH);
6434 return NULL;
6437 static const struct got_error *
6438 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6440 int *have_staged_files = arg;
6442 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6443 *have_staged_files = 1;
6444 return got_error(GOT_ERR_CANCELLED);
6447 return NULL;
6450 static const struct got_error *
6451 check_non_staged_files(struct got_fileindex *fileindex,
6452 struct got_pathlist_head *paths)
6454 struct got_pathlist_entry *pe;
6455 struct got_fileindex_entry *ie;
6457 TAILQ_FOREACH(pe, paths, entry) {
6458 if (pe->path[0] == '\0')
6459 continue;
6460 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6461 if (ie == NULL)
6462 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6463 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6464 return got_error_path(pe->path,
6465 GOT_ERR_FILE_NOT_STAGED);
6468 return NULL;
6471 const struct got_error *
6472 got_worktree_commit(struct got_object_id **new_commit_id,
6473 struct got_worktree *worktree, struct got_pathlist_head *paths,
6474 const char *author, const char *committer, int allow_bad_symlinks,
6475 int show_diff, int commit_conflicts,
6476 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6477 got_worktree_status_cb status_cb, void *status_arg,
6478 struct got_repository *repo)
6480 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6481 struct got_fileindex *fileindex = NULL;
6482 char *fileindex_path = NULL;
6483 struct got_pathlist_head commitable_paths;
6484 struct collect_commitables_arg cc_arg;
6485 struct got_pathlist_entry *pe;
6486 struct got_reference *head_ref = NULL;
6487 struct got_object_id *head_commit_id = NULL;
6488 char *diff_path = NULL;
6489 int have_staged_files = 0;
6491 *new_commit_id = NULL;
6493 memset(&cc_arg, 0, sizeof(cc_arg));
6494 TAILQ_INIT(&commitable_paths);
6496 err = lock_worktree(worktree, LOCK_EX);
6497 if (err)
6498 goto done;
6500 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6501 if (err)
6502 goto done;
6504 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6505 if (err)
6506 goto done;
6508 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6509 if (err)
6510 goto done;
6512 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6513 &have_staged_files);
6514 if (err && err->code != GOT_ERR_CANCELLED)
6515 goto done;
6516 if (have_staged_files) {
6517 err = check_non_staged_files(fileindex, paths);
6518 if (err)
6519 goto done;
6522 cc_arg.commitable_paths = &commitable_paths;
6523 cc_arg.worktree = worktree;
6524 cc_arg.fileindex = fileindex;
6525 cc_arg.repo = repo;
6526 cc_arg.have_staged_files = have_staged_files;
6527 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6528 cc_arg.diff_header_shown = 0;
6529 cc_arg.commit_conflicts = commit_conflicts;
6530 if (show_diff) {
6531 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6532 GOT_TMPDIR_STR "/got", ".diff");
6533 if (err)
6534 goto done;
6535 cc_arg.f1 = got_opentemp();
6536 if (cc_arg.f1 == NULL) {
6537 err = got_error_from_errno("got_opentemp");
6538 goto done;
6540 cc_arg.f2 = got_opentemp();
6541 if (cc_arg.f2 == NULL) {
6542 err = got_error_from_errno("got_opentemp");
6543 goto done;
6547 TAILQ_FOREACH(pe, paths, entry) {
6548 err = worktree_status(worktree, pe->path, fileindex, repo,
6549 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6550 if (err)
6551 goto done;
6554 if (show_diff) {
6555 if (fflush(cc_arg.diff_outfile) == EOF) {
6556 err = got_error_from_errno("fflush");
6557 goto done;
6561 if (TAILQ_EMPTY(&commitable_paths)) {
6562 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6563 goto done;
6566 TAILQ_FOREACH(pe, paths, entry) {
6567 err = check_path_is_commitable(pe->path, &commitable_paths);
6568 if (err)
6569 goto done;
6572 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6573 struct got_commitable *ct = pe->data;
6574 const char *ct_path = ct->in_repo_path;
6576 while (ct_path[0] == '/')
6577 ct_path++;
6578 err = check_out_of_date(ct_path, ct->status,
6579 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6580 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6581 if (err)
6582 goto done;
6586 err = commit_worktree(new_commit_id, &commitable_paths,
6587 head_commit_id, NULL, worktree, author, committer,
6588 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6589 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6590 if (err)
6591 goto done;
6593 err = update_fileindex_after_commit(worktree, &commitable_paths,
6594 *new_commit_id, fileindex, have_staged_files);
6595 sync_err = sync_fileindex(fileindex, fileindex_path);
6596 if (sync_err && err == NULL)
6597 err = sync_err;
6598 done:
6599 if (fileindex)
6600 got_fileindex_free(fileindex);
6601 free(fileindex_path);
6602 unlockerr = lock_worktree(worktree, LOCK_SH);
6603 if (unlockerr && err == NULL)
6604 err = unlockerr;
6605 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6606 struct got_commitable *ct = pe->data;
6608 free_commitable(ct);
6610 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6611 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6612 err = got_error_from_errno2("unlink", diff_path);
6613 free(diff_path);
6614 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6615 err == NULL)
6616 err = got_error_from_errno("fclose");
6617 return err;
6620 const char *
6621 got_commitable_get_path(struct got_commitable *ct)
6623 return ct->path;
6626 unsigned int
6627 got_commitable_get_status(struct got_commitable *ct)
6629 return ct->status;
6632 struct check_rebase_ok_arg {
6633 struct got_worktree *worktree;
6634 struct got_repository *repo;
6637 static const struct got_error *
6638 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6640 const struct got_error *err = NULL;
6641 struct check_rebase_ok_arg *a = arg;
6642 unsigned char status;
6643 struct stat sb;
6644 char *ondisk_path;
6646 /* Reject rebase of a work tree with mixed base commits. */
6647 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6648 SHA1_DIGEST_LENGTH))
6649 return got_error(GOT_ERR_MIXED_COMMITS);
6651 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6652 == -1)
6653 return got_error_from_errno("asprintf");
6655 /* Reject rebase of a work tree with modified or staged files. */
6656 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6657 free(ondisk_path);
6658 if (err)
6659 return err;
6661 if (status != GOT_STATUS_NO_CHANGE)
6662 return got_error(GOT_ERR_MODIFIED);
6663 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6664 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6666 return NULL;
6669 const struct got_error *
6670 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6671 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6672 struct got_worktree *worktree, struct got_reference *branch,
6673 struct got_repository *repo)
6675 const struct got_error *err = NULL;
6676 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6677 char *branch_ref_name = NULL;
6678 char *fileindex_path = NULL;
6679 struct check_rebase_ok_arg ok_arg;
6680 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6681 struct got_object_id *wt_branch_tip = NULL;
6683 *new_base_branch_ref = NULL;
6684 *tmp_branch = NULL;
6685 *fileindex = NULL;
6687 err = lock_worktree(worktree, LOCK_EX);
6688 if (err)
6689 return err;
6691 err = open_fileindex(fileindex, &fileindex_path, worktree);
6692 if (err)
6693 goto done;
6695 ok_arg.worktree = worktree;
6696 ok_arg.repo = repo;
6697 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6698 &ok_arg);
6699 if (err)
6700 goto done;
6702 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6703 if (err)
6704 goto done;
6706 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6707 if (err)
6708 goto done;
6710 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6711 if (err)
6712 goto done;
6714 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6715 0);
6716 if (err)
6717 goto done;
6719 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6720 if (err)
6721 goto done;
6722 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6723 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6724 goto done;
6727 err = got_ref_alloc_symref(new_base_branch_ref,
6728 new_base_branch_ref_name, wt_branch);
6729 if (err)
6730 goto done;
6731 err = got_ref_write(*new_base_branch_ref, repo);
6732 if (err)
6733 goto done;
6735 /* TODO Lock original branch's ref while rebasing? */
6737 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6738 if (err)
6739 goto done;
6741 err = got_ref_write(branch_ref, repo);
6742 if (err)
6743 goto done;
6745 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6746 worktree->base_commit_id);
6747 if (err)
6748 goto done;
6749 err = got_ref_write(*tmp_branch, repo);
6750 if (err)
6751 goto done;
6753 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6754 if (err)
6755 goto done;
6756 done:
6757 free(fileindex_path);
6758 free(tmp_branch_name);
6759 free(new_base_branch_ref_name);
6760 free(branch_ref_name);
6761 if (branch_ref)
6762 got_ref_close(branch_ref);
6763 if (wt_branch)
6764 got_ref_close(wt_branch);
6765 free(wt_branch_tip);
6766 if (err) {
6767 if (*new_base_branch_ref) {
6768 got_ref_close(*new_base_branch_ref);
6769 *new_base_branch_ref = NULL;
6771 if (*tmp_branch) {
6772 got_ref_close(*tmp_branch);
6773 *tmp_branch = NULL;
6775 if (*fileindex) {
6776 got_fileindex_free(*fileindex);
6777 *fileindex = NULL;
6779 lock_worktree(worktree, LOCK_SH);
6781 return err;
6784 const struct got_error *
6785 got_worktree_rebase_continue(struct got_object_id **commit_id,
6786 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6787 struct got_reference **branch, struct got_fileindex **fileindex,
6788 struct got_worktree *worktree, struct got_repository *repo)
6790 const struct got_error *err;
6791 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6792 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6793 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6794 char *fileindex_path = NULL;
6795 int have_staged_files = 0;
6797 *commit_id = NULL;
6798 *new_base_branch = NULL;
6799 *tmp_branch = NULL;
6800 *branch = NULL;
6801 *fileindex = NULL;
6803 err = lock_worktree(worktree, LOCK_EX);
6804 if (err)
6805 return err;
6807 err = open_fileindex(fileindex, &fileindex_path, worktree);
6808 if (err)
6809 goto done;
6811 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6812 &have_staged_files);
6813 if (err && err->code != GOT_ERR_CANCELLED)
6814 goto done;
6815 if (have_staged_files) {
6816 err = got_error(GOT_ERR_STAGED_PATHS);
6817 goto done;
6820 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6821 if (err)
6822 goto done;
6824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6825 if (err)
6826 goto done;
6828 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6829 if (err)
6830 goto done;
6832 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6833 if (err)
6834 goto done;
6836 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6837 if (err)
6838 goto done;
6840 err = got_ref_open(branch, repo,
6841 got_ref_get_symref_target(branch_ref), 0);
6842 if (err)
6843 goto done;
6845 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6846 if (err)
6847 goto done;
6849 err = got_ref_resolve(commit_id, repo, commit_ref);
6850 if (err)
6851 goto done;
6853 err = got_ref_open(new_base_branch, repo,
6854 new_base_branch_ref_name, 0);
6855 if (err)
6856 goto done;
6858 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6859 if (err)
6860 goto done;
6861 done:
6862 free(commit_ref_name);
6863 free(branch_ref_name);
6864 free(fileindex_path);
6865 if (commit_ref)
6866 got_ref_close(commit_ref);
6867 if (branch_ref)
6868 got_ref_close(branch_ref);
6869 if (err) {
6870 free(*commit_id);
6871 *commit_id = NULL;
6872 if (*tmp_branch) {
6873 got_ref_close(*tmp_branch);
6874 *tmp_branch = NULL;
6876 if (*new_base_branch) {
6877 got_ref_close(*new_base_branch);
6878 *new_base_branch = NULL;
6880 if (*branch) {
6881 got_ref_close(*branch);
6882 *branch = NULL;
6884 if (*fileindex) {
6885 got_fileindex_free(*fileindex);
6886 *fileindex = NULL;
6888 lock_worktree(worktree, LOCK_SH);
6890 return err;
6893 const struct got_error *
6894 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6896 const struct got_error *err;
6897 char *tmp_branch_name = NULL;
6899 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6900 if (err)
6901 return err;
6903 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6904 free(tmp_branch_name);
6905 return NULL;
6908 static const struct got_error *
6909 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6910 const char *diff_path, char **logmsg, void *arg)
6912 *logmsg = arg;
6913 return NULL;
6916 static const struct got_error *
6917 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6918 const char *path, struct got_object_id *blob_id,
6919 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6920 int dirfd, const char *de_name)
6922 return NULL;
6925 struct collect_merged_paths_arg {
6926 got_worktree_checkout_cb progress_cb;
6927 void *progress_arg;
6928 struct got_pathlist_head *merged_paths;
6931 static const struct got_error *
6932 collect_merged_paths(void *arg, unsigned char status, const char *path)
6934 const struct got_error *err;
6935 struct collect_merged_paths_arg *a = arg;
6936 char *p;
6937 struct got_pathlist_entry *new;
6939 err = (*a->progress_cb)(a->progress_arg, status, path);
6940 if (err)
6941 return err;
6943 if (status != GOT_STATUS_MERGE &&
6944 status != GOT_STATUS_ADD &&
6945 status != GOT_STATUS_DELETE &&
6946 status != GOT_STATUS_CONFLICT)
6947 return NULL;
6949 p = strdup(path);
6950 if (p == NULL)
6951 return got_error_from_errno("strdup");
6953 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6954 if (err || new == NULL)
6955 free(p);
6956 return err;
6959 static const struct got_error *
6960 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6961 int is_rebase, struct got_repository *repo)
6963 const struct got_error *err;
6964 struct got_reference *commit_ref = NULL;
6966 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6967 if (err) {
6968 if (err->code != GOT_ERR_NOT_REF)
6969 goto done;
6970 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6971 if (err)
6972 goto done;
6973 err = got_ref_write(commit_ref, repo);
6974 if (err)
6975 goto done;
6976 } else if (is_rebase) {
6977 struct got_object_id *stored_id;
6978 int cmp;
6980 err = got_ref_resolve(&stored_id, repo, commit_ref);
6981 if (err)
6982 goto done;
6983 cmp = got_object_id_cmp(commit_id, stored_id);
6984 free(stored_id);
6985 if (cmp != 0) {
6986 err = got_error(GOT_ERR_REBASE_COMMITID);
6987 goto done;
6990 done:
6991 if (commit_ref)
6992 got_ref_close(commit_ref);
6993 return err;
6996 static const struct got_error *
6997 rebase_merge_files(struct got_pathlist_head *merged_paths,
6998 const char *commit_ref_name, struct got_worktree *worktree,
6999 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7000 struct got_object_id *commit_id, struct got_repository *repo,
7001 got_worktree_checkout_cb progress_cb, void *progress_arg,
7002 got_cancel_cb cancel_cb, void *cancel_arg)
7004 const struct got_error *err;
7005 struct got_reference *commit_ref = NULL;
7006 struct collect_merged_paths_arg cmp_arg;
7007 char *fileindex_path;
7009 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7011 err = get_fileindex_path(&fileindex_path, worktree);
7012 if (err)
7013 return err;
7015 cmp_arg.progress_cb = progress_cb;
7016 cmp_arg.progress_arg = progress_arg;
7017 cmp_arg.merged_paths = merged_paths;
7018 err = merge_files(worktree, fileindex, fileindex_path,
7019 parent_commit_id, commit_id, repo, collect_merged_paths,
7020 &cmp_arg, cancel_cb, cancel_arg);
7021 if (commit_ref)
7022 got_ref_close(commit_ref);
7023 return err;
7026 const struct got_error *
7027 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7028 struct got_worktree *worktree, struct got_fileindex *fileindex,
7029 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7030 struct got_repository *repo,
7031 got_worktree_checkout_cb progress_cb, void *progress_arg,
7032 got_cancel_cb cancel_cb, void *cancel_arg)
7034 const struct got_error *err;
7035 char *commit_ref_name;
7037 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7038 if (err)
7039 return err;
7041 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7042 if (err)
7043 goto done;
7045 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7046 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7047 progress_arg, cancel_cb, cancel_arg);
7048 done:
7049 free(commit_ref_name);
7050 return err;
7053 const struct got_error *
7054 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7055 struct got_worktree *worktree, struct got_fileindex *fileindex,
7056 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7057 struct got_repository *repo,
7058 got_worktree_checkout_cb progress_cb, void *progress_arg,
7059 got_cancel_cb cancel_cb, void *cancel_arg)
7061 const struct got_error *err;
7062 char *commit_ref_name;
7064 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7065 if (err)
7066 return err;
7068 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7069 if (err)
7070 goto done;
7072 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7073 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7074 progress_arg, cancel_cb, cancel_arg);
7075 done:
7076 free(commit_ref_name);
7077 return err;
7080 static const struct got_error *
7081 rebase_commit(struct got_object_id **new_commit_id,
7082 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7083 struct got_worktree *worktree, struct got_fileindex *fileindex,
7084 struct got_reference *tmp_branch, const char *committer,
7085 struct got_commit_object *orig_commit, const char *new_logmsg,
7086 int allow_conflict, struct got_repository *repo)
7088 const struct got_error *err, *sync_err;
7089 struct got_pathlist_head commitable_paths;
7090 struct collect_commitables_arg cc_arg;
7091 char *fileindex_path = NULL;
7092 struct got_reference *head_ref = NULL;
7093 struct got_object_id *head_commit_id = NULL;
7094 char *logmsg = NULL;
7096 memset(&cc_arg, 0, sizeof(cc_arg));
7097 TAILQ_INIT(&commitable_paths);
7098 *new_commit_id = NULL;
7100 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7102 err = get_fileindex_path(&fileindex_path, worktree);
7103 if (err)
7104 return err;
7106 cc_arg.commitable_paths = &commitable_paths;
7107 cc_arg.worktree = worktree;
7108 cc_arg.repo = repo;
7109 cc_arg.have_staged_files = 0;
7110 cc_arg.commit_conflicts = allow_conflict;
7112 * If possible get the status of individual files directly to
7113 * avoid crawling the entire work tree once per rebased commit.
7115 * Ideally, merged_paths would contain a list of commitables
7116 * we could use so we could skip worktree_status() entirely.
7117 * However, we would then need carefully keep track of cumulative
7118 * effects of operations such as file additions and deletions
7119 * in 'got histedit -f' (folding multiple commits into one),
7120 * and this extra complexity is not really worth it.
7122 if (merged_paths) {
7123 struct got_pathlist_entry *pe;
7124 TAILQ_FOREACH(pe, merged_paths, entry) {
7125 err = worktree_status(worktree, pe->path, fileindex,
7126 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7127 0);
7128 if (err)
7129 goto done;
7131 } else {
7132 err = worktree_status(worktree, "", fileindex, repo,
7133 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7134 if (err)
7135 goto done;
7138 if (TAILQ_EMPTY(&commitable_paths)) {
7139 /* No-op change; commit will be elided. */
7140 err = got_ref_delete(commit_ref, repo);
7141 if (err)
7142 goto done;
7143 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7144 goto done;
7147 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7148 if (err)
7149 goto done;
7151 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7152 if (err)
7153 goto done;
7155 if (new_logmsg) {
7156 logmsg = strdup(new_logmsg);
7157 if (logmsg == NULL) {
7158 err = got_error_from_errno("strdup");
7159 goto done;
7161 } else {
7162 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7163 if (err)
7164 goto done;
7167 /* NB: commit_worktree will call free(logmsg) */
7168 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7169 NULL, worktree, got_object_commit_get_author(orig_commit),
7170 committer ? committer :
7171 got_object_commit_get_committer(orig_commit), NULL,
7172 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7173 if (err)
7174 goto done;
7176 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7177 if (err)
7178 goto done;
7180 err = got_ref_delete(commit_ref, repo);
7181 if (err)
7182 goto done;
7184 err = update_fileindex_after_commit(worktree, &commitable_paths,
7185 *new_commit_id, fileindex, 0);
7186 sync_err = sync_fileindex(fileindex, fileindex_path);
7187 if (sync_err && err == NULL)
7188 err = sync_err;
7189 done:
7190 free(fileindex_path);
7191 free(head_commit_id);
7192 if (head_ref)
7193 got_ref_close(head_ref);
7194 if (err) {
7195 free(*new_commit_id);
7196 *new_commit_id = NULL;
7198 return err;
7201 const struct got_error *
7202 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7203 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7204 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7205 const char *committer, struct got_commit_object *orig_commit,
7206 struct got_object_id *orig_commit_id, int allow_conflict,
7207 struct got_repository *repo)
7209 const struct got_error *err;
7210 char *commit_ref_name;
7211 struct got_reference *commit_ref = NULL;
7212 struct got_object_id *commit_id = NULL;
7214 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7215 if (err)
7216 return err;
7218 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7219 if (err)
7220 goto done;
7221 err = got_ref_resolve(&commit_id, repo, commit_ref);
7222 if (err)
7223 goto done;
7224 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7225 err = got_error(GOT_ERR_REBASE_COMMITID);
7226 goto done;
7229 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7230 worktree, fileindex, tmp_branch, committer, orig_commit,
7231 NULL, allow_conflict, repo);
7232 done:
7233 if (commit_ref)
7234 got_ref_close(commit_ref);
7235 free(commit_ref_name);
7236 free(commit_id);
7237 return err;
7240 const struct got_error *
7241 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7242 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7243 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7244 const char *committer, struct got_commit_object *orig_commit,
7245 struct got_object_id *orig_commit_id, const char *new_logmsg,
7246 int allow_conflict, struct got_repository *repo)
7248 const struct got_error *err;
7249 char *commit_ref_name;
7250 struct got_reference *commit_ref = NULL;
7252 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7253 if (err)
7254 return err;
7256 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7257 if (err)
7258 goto done;
7260 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7261 worktree, fileindex, tmp_branch, committer, orig_commit,
7262 new_logmsg, allow_conflict, repo);
7263 done:
7264 if (commit_ref)
7265 got_ref_close(commit_ref);
7266 free(commit_ref_name);
7267 return err;
7270 const struct got_error *
7271 got_worktree_rebase_postpone(struct got_worktree *worktree,
7272 struct got_fileindex *fileindex)
7274 if (fileindex)
7275 got_fileindex_free(fileindex);
7276 return lock_worktree(worktree, LOCK_SH);
7279 static const struct got_error *
7280 delete_ref(const char *name, struct got_repository *repo)
7282 const struct got_error *err;
7283 struct got_reference *ref;
7285 err = got_ref_open(&ref, repo, name, 0);
7286 if (err) {
7287 if (err->code == GOT_ERR_NOT_REF)
7288 return NULL;
7289 return err;
7292 err = got_ref_delete(ref, repo);
7293 got_ref_close(ref);
7294 return err;
7297 static const struct got_error *
7298 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7300 const struct got_error *err;
7301 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7302 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7304 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7305 if (err)
7306 goto done;
7307 err = delete_ref(tmp_branch_name, repo);
7308 if (err)
7309 goto done;
7311 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7312 if (err)
7313 goto done;
7314 err = delete_ref(new_base_branch_ref_name, repo);
7315 if (err)
7316 goto done;
7318 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7319 if (err)
7320 goto done;
7321 err = delete_ref(branch_ref_name, repo);
7322 if (err)
7323 goto done;
7325 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7326 if (err)
7327 goto done;
7328 err = delete_ref(commit_ref_name, repo);
7329 if (err)
7330 goto done;
7332 done:
7333 free(tmp_branch_name);
7334 free(new_base_branch_ref_name);
7335 free(branch_ref_name);
7336 free(commit_ref_name);
7337 return err;
7340 static const struct got_error *
7341 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7342 struct got_object_id *new_commit_id, struct got_repository *repo)
7344 const struct got_error *err;
7345 struct got_reference *ref = NULL;
7346 struct got_object_id *old_commit_id = NULL;
7347 const char *branch_name = NULL;
7348 char *new_id_str = NULL;
7349 char *refname = NULL;
7351 branch_name = got_ref_get_name(branch);
7352 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7353 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7354 branch_name += 11;
7356 err = got_object_id_str(&new_id_str, new_commit_id);
7357 if (err)
7358 return err;
7360 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7361 new_id_str) == -1) {
7362 err = got_error_from_errno("asprintf");
7363 goto done;
7366 err = got_ref_resolve(&old_commit_id, repo, branch);
7367 if (err)
7368 goto done;
7370 err = got_ref_alloc(&ref, refname, old_commit_id);
7371 if (err)
7372 goto done;
7374 err = got_ref_write(ref, repo);
7375 done:
7376 free(new_id_str);
7377 free(refname);
7378 free(old_commit_id);
7379 if (ref)
7380 got_ref_close(ref);
7381 return err;
7384 const struct got_error *
7385 got_worktree_rebase_complete(struct got_worktree *worktree,
7386 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7387 struct got_reference *rebased_branch, struct got_repository *repo,
7388 int create_backup)
7390 const struct got_error *err, *unlockerr, *sync_err;
7391 struct got_object_id *new_head_commit_id = NULL;
7392 char *fileindex_path = NULL;
7394 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7395 if (err)
7396 return err;
7398 if (create_backup) {
7399 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7400 rebased_branch, new_head_commit_id, repo);
7401 if (err)
7402 goto done;
7405 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7406 if (err)
7407 goto done;
7409 err = got_ref_write(rebased_branch, repo);
7410 if (err)
7411 goto done;
7413 err = got_worktree_set_head_ref(worktree, rebased_branch);
7414 if (err)
7415 goto done;
7417 err = delete_rebase_refs(worktree, repo);
7418 if (err)
7419 goto done;
7421 err = get_fileindex_path(&fileindex_path, worktree);
7422 if (err)
7423 goto done;
7424 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7425 sync_err = sync_fileindex(fileindex, fileindex_path);
7426 if (sync_err && err == NULL)
7427 err = sync_err;
7428 done:
7429 got_fileindex_free(fileindex);
7430 free(fileindex_path);
7431 free(new_head_commit_id);
7432 unlockerr = lock_worktree(worktree, LOCK_SH);
7433 if (unlockerr && err == NULL)
7434 err = unlockerr;
7435 return err;
7438 static const struct got_error *
7439 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7440 struct got_object_id *id1, struct got_object_id *id2,
7441 struct got_repository *repo)
7443 const struct got_error *err;
7444 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7445 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7447 if (id1) {
7448 err = got_object_open_as_commit(&commit1, repo, id1);
7449 if (err)
7450 goto done;
7452 err = got_object_open_as_tree(&tree1, repo,
7453 got_object_commit_get_tree_id(commit1));
7454 if (err)
7455 goto done;
7458 if (id2) {
7459 err = got_object_open_as_commit(&commit2, repo, id2);
7460 if (err)
7461 goto done;
7463 err = got_object_open_as_tree(&tree2, repo,
7464 got_object_commit_get_tree_id(commit2));
7465 if (err)
7466 goto done;
7469 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7470 got_diff_tree_collect_changed_paths, paths, 0);
7471 if (err)
7472 goto done;
7473 done:
7474 if (commit1)
7475 got_object_commit_close(commit1);
7476 if (commit2)
7477 got_object_commit_close(commit2);
7478 if (tree1)
7479 got_object_tree_close(tree1);
7480 if (tree2)
7481 got_object_tree_close(tree2);
7482 return err;
7485 static const struct got_error *
7486 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7487 struct got_object_id *id1, struct got_object_id *id2,
7488 const char *path_prefix, struct got_repository *repo)
7490 const struct got_error *err;
7491 struct got_pathlist_head merged_paths;
7492 struct got_pathlist_entry *pe;
7493 char *abspath = NULL, *wt_path = NULL;
7495 TAILQ_INIT(&merged_paths);
7497 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7498 if (err)
7499 goto done;
7501 TAILQ_FOREACH(pe, &merged_paths, entry) {
7502 struct got_diff_changed_path *change = pe->data;
7504 if (change->status != GOT_STATUS_ADD)
7505 continue;
7507 if (got_path_is_root_dir(path_prefix)) {
7508 wt_path = strdup(pe->path);
7509 if (wt_path == NULL) {
7510 err = got_error_from_errno("strdup");
7511 goto done;
7513 } else {
7514 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7515 err = got_error_from_errno("asprintf");
7516 goto done;
7519 err = got_path_skip_common_ancestor(&wt_path,
7520 path_prefix, abspath);
7521 if (err)
7522 goto done;
7523 free(abspath);
7524 abspath = NULL;
7527 err = got_pathlist_append(added_paths, wt_path, NULL);
7528 if (err)
7529 goto done;
7530 wt_path = NULL;
7533 done:
7534 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7535 free(abspath);
7536 free(wt_path);
7537 return err;
7540 static const struct got_error *
7541 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7542 struct got_object_id *id, const char *path_prefix,
7543 struct got_repository *repo)
7545 const struct got_error *err;
7546 struct got_commit_object *commit = NULL;
7547 struct got_object_qid *pid;
7549 err = got_object_open_as_commit(&commit, repo, id);
7550 if (err)
7551 goto done;
7553 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7555 err = get_paths_added_between_commits(added_paths,
7556 pid ? &pid->id : NULL, id, path_prefix, repo);
7557 if (err)
7558 goto done;
7559 done:
7560 if (commit)
7561 got_object_commit_close(commit);
7562 return err;
7565 const struct got_error *
7566 got_worktree_rebase_abort(struct got_worktree *worktree,
7567 struct got_fileindex *fileindex, struct got_repository *repo,
7568 struct got_reference *new_base_branch,
7569 got_worktree_checkout_cb progress_cb, void *progress_arg)
7571 const struct got_error *err, *unlockerr, *sync_err;
7572 struct got_reference *resolved = NULL;
7573 struct got_object_id *commit_id = NULL;
7574 struct got_object_id *merged_commit_id = NULL;
7575 struct got_commit_object *commit = NULL;
7576 char *fileindex_path = NULL;
7577 char *commit_ref_name = NULL;
7578 struct got_reference *commit_ref = NULL;
7579 struct revert_file_args rfa;
7580 struct got_object_id *tree_id = NULL;
7581 struct got_pathlist_head added_paths;
7583 TAILQ_INIT(&added_paths);
7585 err = lock_worktree(worktree, LOCK_EX);
7586 if (err)
7587 return err;
7589 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7590 if (err)
7591 goto done;
7593 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7594 if (err)
7595 goto done;
7597 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7598 if (err)
7599 goto done;
7602 * Determine which files in added status can be safely removed
7603 * from disk while reverting changes in the work tree.
7604 * We want to avoid deleting unrelated files which were added by
7605 * the user for conflict resolution purposes.
7607 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7608 got_worktree_get_path_prefix(worktree), repo);
7609 if (err)
7610 goto done;
7612 err = got_ref_open(&resolved, repo,
7613 got_ref_get_symref_target(new_base_branch), 0);
7614 if (err)
7615 goto done;
7617 err = got_worktree_set_head_ref(worktree, resolved);
7618 if (err)
7619 goto done;
7622 * XXX commits to the base branch could have happened while
7623 * we were busy rebasing; should we store the original commit ID
7624 * when rebase begins and read it back here?
7626 err = got_ref_resolve(&commit_id, repo, resolved);
7627 if (err)
7628 goto done;
7630 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7631 if (err)
7632 goto done;
7634 err = got_object_open_as_commit(&commit, repo,
7635 worktree->base_commit_id);
7636 if (err)
7637 goto done;
7639 err = got_object_id_by_path(&tree_id, repo, commit,
7640 worktree->path_prefix);
7641 if (err)
7642 goto done;
7644 err = delete_rebase_refs(worktree, repo);
7645 if (err)
7646 goto done;
7648 err = get_fileindex_path(&fileindex_path, worktree);
7649 if (err)
7650 goto done;
7652 rfa.worktree = worktree;
7653 rfa.fileindex = fileindex;
7654 rfa.progress_cb = progress_cb;
7655 rfa.progress_arg = progress_arg;
7656 rfa.patch_cb = NULL;
7657 rfa.patch_arg = NULL;
7658 rfa.repo = repo;
7659 rfa.unlink_added_files = 1;
7660 rfa.added_files_to_unlink = &added_paths;
7661 err = worktree_status(worktree, "", fileindex, repo,
7662 revert_file, &rfa, NULL, NULL, 1, 0);
7663 if (err)
7664 goto sync;
7666 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7667 repo, progress_cb, progress_arg, NULL, NULL);
7668 sync:
7669 sync_err = sync_fileindex(fileindex, fileindex_path);
7670 if (sync_err && err == NULL)
7671 err = sync_err;
7672 done:
7673 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7674 got_ref_close(resolved);
7675 free(tree_id);
7676 free(commit_id);
7677 free(merged_commit_id);
7678 if (commit)
7679 got_object_commit_close(commit);
7680 if (fileindex)
7681 got_fileindex_free(fileindex);
7682 free(fileindex_path);
7683 free(commit_ref_name);
7684 if (commit_ref)
7685 got_ref_close(commit_ref);
7687 unlockerr = lock_worktree(worktree, LOCK_SH);
7688 if (unlockerr && err == NULL)
7689 err = unlockerr;
7690 return err;
7693 const struct got_error *
7694 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7695 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7696 struct got_fileindex **fileindex, struct got_worktree *worktree,
7697 struct got_repository *repo)
7699 const struct got_error *err = NULL;
7700 char *tmp_branch_name = NULL;
7701 char *branch_ref_name = NULL;
7702 char *base_commit_ref_name = NULL;
7703 char *fileindex_path = NULL;
7704 struct check_rebase_ok_arg ok_arg;
7705 struct got_reference *wt_branch = NULL;
7706 struct got_reference *base_commit_ref = NULL;
7708 *tmp_branch = NULL;
7709 *branch_ref = NULL;
7710 *base_commit_id = NULL;
7711 *fileindex = NULL;
7713 err = lock_worktree(worktree, LOCK_EX);
7714 if (err)
7715 return err;
7717 err = open_fileindex(fileindex, &fileindex_path, worktree);
7718 if (err)
7719 goto done;
7721 ok_arg.worktree = worktree;
7722 ok_arg.repo = repo;
7723 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7724 &ok_arg);
7725 if (err)
7726 goto done;
7728 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7729 if (err)
7730 goto done;
7732 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7733 if (err)
7734 goto done;
7736 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7737 worktree);
7738 if (err)
7739 goto done;
7741 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7742 0);
7743 if (err)
7744 goto done;
7746 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7747 if (err)
7748 goto done;
7750 err = got_ref_write(*branch_ref, repo);
7751 if (err)
7752 goto done;
7754 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7755 worktree->base_commit_id);
7756 if (err)
7757 goto done;
7758 err = got_ref_write(base_commit_ref, repo);
7759 if (err)
7760 goto done;
7761 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7762 if (*base_commit_id == NULL) {
7763 err = got_error_from_errno("got_object_id_dup");
7764 goto done;
7767 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7768 worktree->base_commit_id);
7769 if (err)
7770 goto done;
7771 err = got_ref_write(*tmp_branch, repo);
7772 if (err)
7773 goto done;
7775 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7776 if (err)
7777 goto done;
7778 done:
7779 free(fileindex_path);
7780 free(tmp_branch_name);
7781 free(branch_ref_name);
7782 free(base_commit_ref_name);
7783 if (wt_branch)
7784 got_ref_close(wt_branch);
7785 if (err) {
7786 if (*branch_ref) {
7787 got_ref_close(*branch_ref);
7788 *branch_ref = NULL;
7790 if (*tmp_branch) {
7791 got_ref_close(*tmp_branch);
7792 *tmp_branch = NULL;
7794 free(*base_commit_id);
7795 if (*fileindex) {
7796 got_fileindex_free(*fileindex);
7797 *fileindex = NULL;
7799 lock_worktree(worktree, LOCK_SH);
7801 return err;
7804 const struct got_error *
7805 got_worktree_histedit_postpone(struct got_worktree *worktree,
7806 struct got_fileindex *fileindex)
7808 if (fileindex)
7809 got_fileindex_free(fileindex);
7810 return lock_worktree(worktree, LOCK_SH);
7813 const struct got_error *
7814 got_worktree_histedit_in_progress(int *in_progress,
7815 struct got_worktree *worktree)
7817 const struct got_error *err;
7818 char *tmp_branch_name = NULL;
7820 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7821 if (err)
7822 return err;
7824 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7825 free(tmp_branch_name);
7826 return NULL;
7829 const struct got_error *
7830 got_worktree_histedit_continue(struct got_object_id **commit_id,
7831 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7832 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7833 struct got_worktree *worktree, struct got_repository *repo)
7835 const struct got_error *err;
7836 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7837 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7838 struct got_reference *commit_ref = NULL;
7839 struct got_reference *base_commit_ref = NULL;
7840 char *fileindex_path = NULL;
7841 int have_staged_files = 0;
7843 *commit_id = NULL;
7844 *tmp_branch = NULL;
7845 *base_commit_id = NULL;
7846 *fileindex = NULL;
7848 err = lock_worktree(worktree, LOCK_EX);
7849 if (err)
7850 return err;
7852 err = open_fileindex(fileindex, &fileindex_path, worktree);
7853 if (err)
7854 goto done;
7856 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7857 &have_staged_files);
7858 if (err && err->code != GOT_ERR_CANCELLED)
7859 goto done;
7860 if (have_staged_files) {
7861 err = got_error(GOT_ERR_STAGED_PATHS);
7862 goto done;
7865 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7866 if (err)
7867 goto done;
7869 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7870 if (err)
7871 goto done;
7873 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7874 if (err)
7875 goto done;
7877 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7878 worktree);
7879 if (err)
7880 goto done;
7882 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7883 if (err)
7884 goto done;
7886 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7887 if (err)
7888 goto done;
7889 err = got_ref_resolve(commit_id, repo, commit_ref);
7890 if (err)
7891 goto done;
7893 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7894 if (err)
7895 goto done;
7896 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7897 if (err)
7898 goto done;
7900 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7901 if (err)
7902 goto done;
7903 done:
7904 free(commit_ref_name);
7905 free(branch_ref_name);
7906 free(fileindex_path);
7907 if (commit_ref)
7908 got_ref_close(commit_ref);
7909 if (base_commit_ref)
7910 got_ref_close(base_commit_ref);
7911 if (err) {
7912 free(*commit_id);
7913 *commit_id = NULL;
7914 free(*base_commit_id);
7915 *base_commit_id = NULL;
7916 if (*tmp_branch) {
7917 got_ref_close(*tmp_branch);
7918 *tmp_branch = NULL;
7920 if (*fileindex) {
7921 got_fileindex_free(*fileindex);
7922 *fileindex = NULL;
7924 lock_worktree(worktree, LOCK_EX);
7926 return err;
7929 static const struct got_error *
7930 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7932 const struct got_error *err;
7933 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7934 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7936 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7937 if (err)
7938 goto done;
7939 err = delete_ref(tmp_branch_name, repo);
7940 if (err)
7941 goto done;
7943 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7944 worktree);
7945 if (err)
7946 goto done;
7947 err = delete_ref(base_commit_ref_name, repo);
7948 if (err)
7949 goto done;
7951 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7952 if (err)
7953 goto done;
7954 err = delete_ref(branch_ref_name, repo);
7955 if (err)
7956 goto done;
7958 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7959 if (err)
7960 goto done;
7961 err = delete_ref(commit_ref_name, repo);
7962 if (err)
7963 goto done;
7964 done:
7965 free(tmp_branch_name);
7966 free(base_commit_ref_name);
7967 free(branch_ref_name);
7968 free(commit_ref_name);
7969 return err;
7972 const struct got_error *
7973 got_worktree_histedit_abort(struct got_worktree *worktree,
7974 struct got_fileindex *fileindex, struct got_repository *repo,
7975 struct got_reference *branch, struct got_object_id *base_commit_id,
7976 got_worktree_checkout_cb progress_cb, void *progress_arg)
7978 const struct got_error *err, *unlockerr, *sync_err;
7979 struct got_reference *resolved = NULL;
7980 char *fileindex_path = NULL;
7981 struct got_object_id *merged_commit_id = NULL;
7982 struct got_commit_object *commit = NULL;
7983 char *commit_ref_name = NULL;
7984 struct got_reference *commit_ref = NULL;
7985 struct got_object_id *tree_id = NULL;
7986 struct revert_file_args rfa;
7987 struct got_pathlist_head added_paths;
7989 TAILQ_INIT(&added_paths);
7991 err = lock_worktree(worktree, LOCK_EX);
7992 if (err)
7993 return err;
7995 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7996 if (err)
7997 goto done;
7999 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8000 if (err) {
8001 if (err->code != GOT_ERR_NOT_REF)
8002 goto done;
8003 /* Can happen on early abort due to invalid histedit script. */
8004 commit_ref = NULL;
8007 if (commit_ref) {
8008 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8009 if (err)
8010 goto done;
8013 * Determine which files in added status can be safely removed
8014 * from disk while reverting changes in the work tree.
8015 * We want to avoid deleting unrelated files added by the
8016 * user during conflict resolution or during histedit -e.
8018 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8019 got_worktree_get_path_prefix(worktree), repo);
8020 if (err)
8021 goto done;
8024 err = got_ref_open(&resolved, repo,
8025 got_ref_get_symref_target(branch), 0);
8026 if (err)
8027 goto done;
8029 err = got_worktree_set_head_ref(worktree, resolved);
8030 if (err)
8031 goto done;
8033 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8034 if (err)
8035 goto done;
8037 err = got_object_open_as_commit(&commit, repo,
8038 worktree->base_commit_id);
8039 if (err)
8040 goto done;
8042 err = got_object_id_by_path(&tree_id, repo, commit,
8043 worktree->path_prefix);
8044 if (err)
8045 goto done;
8047 err = delete_histedit_refs(worktree, repo);
8048 if (err)
8049 goto done;
8051 err = get_fileindex_path(&fileindex_path, worktree);
8052 if (err)
8053 goto done;
8055 rfa.worktree = worktree;
8056 rfa.fileindex = fileindex;
8057 rfa.progress_cb = progress_cb;
8058 rfa.progress_arg = progress_arg;
8059 rfa.patch_cb = NULL;
8060 rfa.patch_arg = NULL;
8061 rfa.repo = repo;
8062 rfa.unlink_added_files = 1;
8063 rfa.added_files_to_unlink = &added_paths;
8064 err = worktree_status(worktree, "", fileindex, repo,
8065 revert_file, &rfa, NULL, NULL, 1, 0);
8066 if (err)
8067 goto sync;
8069 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8070 repo, progress_cb, progress_arg, NULL, NULL);
8071 sync:
8072 sync_err = sync_fileindex(fileindex, fileindex_path);
8073 if (sync_err && err == NULL)
8074 err = sync_err;
8075 done:
8076 if (resolved)
8077 got_ref_close(resolved);
8078 if (commit_ref)
8079 got_ref_close(commit_ref);
8080 free(merged_commit_id);
8081 free(tree_id);
8082 free(fileindex_path);
8083 free(commit_ref_name);
8085 unlockerr = lock_worktree(worktree, LOCK_SH);
8086 if (unlockerr && err == NULL)
8087 err = unlockerr;
8088 return err;
8091 const struct got_error *
8092 got_worktree_histedit_complete(struct got_worktree *worktree,
8093 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8094 struct got_reference *edited_branch, struct got_repository *repo)
8096 const struct got_error *err, *unlockerr, *sync_err;
8097 struct got_object_id *new_head_commit_id = NULL;
8098 struct got_reference *resolved = NULL;
8099 char *fileindex_path = NULL;
8101 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8102 if (err)
8103 return err;
8105 err = got_ref_open(&resolved, repo,
8106 got_ref_get_symref_target(edited_branch), 0);
8107 if (err)
8108 goto done;
8110 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8111 resolved, new_head_commit_id, repo);
8112 if (err)
8113 goto done;
8115 err = got_ref_change_ref(resolved, new_head_commit_id);
8116 if (err)
8117 goto done;
8119 err = got_ref_write(resolved, repo);
8120 if (err)
8121 goto done;
8123 err = got_worktree_set_head_ref(worktree, resolved);
8124 if (err)
8125 goto done;
8127 err = delete_histedit_refs(worktree, repo);
8128 if (err)
8129 goto done;
8131 err = get_fileindex_path(&fileindex_path, worktree);
8132 if (err)
8133 goto done;
8134 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8135 sync_err = sync_fileindex(fileindex, fileindex_path);
8136 if (sync_err && err == NULL)
8137 err = sync_err;
8138 done:
8139 got_fileindex_free(fileindex);
8140 free(fileindex_path);
8141 free(new_head_commit_id);
8142 unlockerr = lock_worktree(worktree, LOCK_SH);
8143 if (unlockerr && err == NULL)
8144 err = unlockerr;
8145 return err;
8148 const struct got_error *
8149 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8150 struct got_object_id *commit_id, struct got_repository *repo)
8152 const struct got_error *err;
8153 char *commit_ref_name;
8155 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8156 if (err)
8157 return err;
8159 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8160 if (err)
8161 goto done;
8163 err = delete_ref(commit_ref_name, repo);
8164 done:
8165 free(commit_ref_name);
8166 return err;
8169 const struct got_error *
8170 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8171 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8172 struct got_worktree *worktree, const char *refname,
8173 struct got_repository *repo)
8175 const struct got_error *err = NULL;
8176 char *fileindex_path = NULL;
8177 struct check_rebase_ok_arg ok_arg;
8179 *fileindex = NULL;
8180 *branch_ref = NULL;
8181 *base_branch_ref = NULL;
8183 err = lock_worktree(worktree, LOCK_EX);
8184 if (err)
8185 return err;
8187 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8188 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8189 "cannot integrate a branch into itself; "
8190 "update -b or different branch name required");
8191 goto done;
8194 err = open_fileindex(fileindex, &fileindex_path, worktree);
8195 if (err)
8196 goto done;
8198 /* Preconditions are the same as for rebase. */
8199 ok_arg.worktree = worktree;
8200 ok_arg.repo = repo;
8201 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8202 &ok_arg);
8203 if (err)
8204 goto done;
8206 err = got_ref_open(branch_ref, repo, refname, 1);
8207 if (err)
8208 goto done;
8210 err = got_ref_open(base_branch_ref, repo,
8211 got_worktree_get_head_ref_name(worktree), 1);
8212 done:
8213 if (err) {
8214 if (*branch_ref) {
8215 got_ref_close(*branch_ref);
8216 *branch_ref = NULL;
8218 if (*base_branch_ref) {
8219 got_ref_close(*base_branch_ref);
8220 *base_branch_ref = NULL;
8222 if (*fileindex) {
8223 got_fileindex_free(*fileindex);
8224 *fileindex = NULL;
8226 lock_worktree(worktree, LOCK_SH);
8228 return err;
8231 const struct got_error *
8232 got_worktree_integrate_continue(struct got_worktree *worktree,
8233 struct got_fileindex *fileindex, struct got_repository *repo,
8234 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8235 got_worktree_checkout_cb progress_cb, void *progress_arg,
8236 got_cancel_cb cancel_cb, void *cancel_arg)
8238 const struct got_error *err = NULL, *sync_err, *unlockerr;
8239 char *fileindex_path = NULL;
8240 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8241 struct got_commit_object *commit = NULL;
8243 err = get_fileindex_path(&fileindex_path, worktree);
8244 if (err)
8245 goto done;
8247 err = got_ref_resolve(&commit_id, repo, branch_ref);
8248 if (err)
8249 goto done;
8251 err = got_object_open_as_commit(&commit, repo, commit_id);
8252 if (err)
8253 goto done;
8255 err = got_object_id_by_path(&tree_id, repo, commit,
8256 worktree->path_prefix);
8257 if (err)
8258 goto done;
8260 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8261 if (err)
8262 goto done;
8264 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8265 progress_cb, progress_arg, cancel_cb, cancel_arg);
8266 if (err)
8267 goto sync;
8269 err = got_ref_change_ref(base_branch_ref, commit_id);
8270 if (err)
8271 goto sync;
8273 err = got_ref_write(base_branch_ref, repo);
8274 if (err)
8275 goto sync;
8277 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8278 sync:
8279 sync_err = sync_fileindex(fileindex, fileindex_path);
8280 if (sync_err && err == NULL)
8281 err = sync_err;
8283 done:
8284 unlockerr = got_ref_unlock(branch_ref);
8285 if (unlockerr && err == NULL)
8286 err = unlockerr;
8287 got_ref_close(branch_ref);
8289 unlockerr = got_ref_unlock(base_branch_ref);
8290 if (unlockerr && err == NULL)
8291 err = unlockerr;
8292 got_ref_close(base_branch_ref);
8294 got_fileindex_free(fileindex);
8295 free(fileindex_path);
8296 free(tree_id);
8297 if (commit)
8298 got_object_commit_close(commit);
8300 unlockerr = lock_worktree(worktree, LOCK_SH);
8301 if (unlockerr && err == NULL)
8302 err = unlockerr;
8303 return err;
8306 const struct got_error *
8307 got_worktree_integrate_abort(struct got_worktree *worktree,
8308 struct got_fileindex *fileindex, struct got_repository *repo,
8309 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8311 const struct got_error *err = NULL, *unlockerr = NULL;
8313 got_fileindex_free(fileindex);
8315 err = lock_worktree(worktree, LOCK_SH);
8317 unlockerr = got_ref_unlock(branch_ref);
8318 if (unlockerr && err == NULL)
8319 err = unlockerr;
8320 got_ref_close(branch_ref);
8322 unlockerr = got_ref_unlock(base_branch_ref);
8323 if (unlockerr && err == NULL)
8324 err = unlockerr;
8325 got_ref_close(base_branch_ref);
8327 return err;
8330 const struct got_error *
8331 got_worktree_merge_postpone(struct got_worktree *worktree,
8332 struct got_fileindex *fileindex)
8334 const struct got_error *err, *sync_err;
8335 char *fileindex_path = NULL;
8337 err = get_fileindex_path(&fileindex_path, worktree);
8338 if (err)
8339 goto done;
8341 sync_err = sync_fileindex(fileindex, fileindex_path);
8343 err = lock_worktree(worktree, LOCK_SH);
8344 if (sync_err && err == NULL)
8345 err = sync_err;
8346 done:
8347 got_fileindex_free(fileindex);
8348 free(fileindex_path);
8349 return err;
8352 static const struct got_error *
8353 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8355 const struct got_error *err;
8356 char *branch_refname = NULL, *commit_refname = NULL;
8358 err = get_merge_branch_ref_name(&branch_refname, worktree);
8359 if (err)
8360 goto done;
8361 err = delete_ref(branch_refname, repo);
8362 if (err)
8363 goto done;
8365 err = get_merge_commit_ref_name(&commit_refname, worktree);
8366 if (err)
8367 goto done;
8368 err = delete_ref(commit_refname, repo);
8369 if (err)
8370 goto done;
8372 done:
8373 free(branch_refname);
8374 free(commit_refname);
8375 return err;
8378 struct merge_commit_msg_arg {
8379 struct got_worktree *worktree;
8380 const char *branch_name;
8383 static const struct got_error *
8384 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8385 const char *diff_path, char **logmsg, void *arg)
8387 struct merge_commit_msg_arg *a = arg;
8389 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8390 got_worktree_get_head_ref_name(a->worktree)) == -1)
8391 return got_error_from_errno("asprintf");
8393 return NULL;
8397 const struct got_error *
8398 got_worktree_merge_branch(struct got_worktree *worktree,
8399 struct got_fileindex *fileindex,
8400 struct got_object_id *yca_commit_id,
8401 struct got_object_id *branch_tip,
8402 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8403 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8405 const struct got_error *err;
8406 char *fileindex_path = NULL;
8408 err = get_fileindex_path(&fileindex_path, worktree);
8409 if (err)
8410 goto done;
8412 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8413 worktree);
8414 if (err)
8415 goto done;
8417 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8418 branch_tip, repo, progress_cb, progress_arg,
8419 cancel_cb, cancel_arg);
8420 done:
8421 free(fileindex_path);
8422 return err;
8425 const struct got_error *
8426 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8427 struct got_worktree *worktree, struct got_fileindex *fileindex,
8428 const char *author, const char *committer, int allow_bad_symlinks,
8429 struct got_object_id *branch_tip, const char *branch_name,
8430 int allow_conflict, struct got_repository *repo,
8431 got_worktree_status_cb status_cb, void *status_arg)
8434 const struct got_error *err = NULL, *sync_err;
8435 struct got_pathlist_head commitable_paths;
8436 struct collect_commitables_arg cc_arg;
8437 struct got_pathlist_entry *pe;
8438 struct got_reference *head_ref = NULL;
8439 struct got_object_id *head_commit_id = NULL;
8440 int have_staged_files = 0;
8441 struct merge_commit_msg_arg mcm_arg;
8442 char *fileindex_path = NULL;
8444 memset(&cc_arg, 0, sizeof(cc_arg));
8445 *new_commit_id = NULL;
8447 TAILQ_INIT(&commitable_paths);
8449 err = get_fileindex_path(&fileindex_path, worktree);
8450 if (err)
8451 goto done;
8453 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8454 if (err)
8455 goto done;
8457 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8458 if (err)
8459 goto done;
8461 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8462 &have_staged_files);
8463 if (err && err->code != GOT_ERR_CANCELLED)
8464 goto done;
8465 if (have_staged_files) {
8466 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8467 goto done;
8470 cc_arg.commitable_paths = &commitable_paths;
8471 cc_arg.worktree = worktree;
8472 cc_arg.fileindex = fileindex;
8473 cc_arg.repo = repo;
8474 cc_arg.have_staged_files = have_staged_files;
8475 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8476 cc_arg.commit_conflicts = allow_conflict;
8477 err = worktree_status(worktree, "", fileindex, repo,
8478 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8479 if (err)
8480 goto done;
8482 mcm_arg.worktree = worktree;
8483 mcm_arg.branch_name = branch_name;
8484 err = commit_worktree(new_commit_id, &commitable_paths,
8485 head_commit_id, branch_tip, worktree, author, committer, NULL,
8486 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8487 if (err)
8488 goto done;
8490 err = update_fileindex_after_commit(worktree, &commitable_paths,
8491 *new_commit_id, fileindex, have_staged_files);
8492 sync_err = sync_fileindex(fileindex, fileindex_path);
8493 if (sync_err && err == NULL)
8494 err = sync_err;
8495 done:
8496 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8497 struct got_commitable *ct = pe->data;
8499 free_commitable(ct);
8501 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8502 free(fileindex_path);
8503 return err;
8506 const struct got_error *
8507 got_worktree_merge_complete(struct got_worktree *worktree,
8508 struct got_fileindex *fileindex, struct got_repository *repo)
8510 const struct got_error *err, *unlockerr, *sync_err;
8511 char *fileindex_path = NULL;
8513 err = delete_merge_refs(worktree, repo);
8514 if (err)
8515 goto done;
8517 err = get_fileindex_path(&fileindex_path, worktree);
8518 if (err)
8519 goto done;
8520 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8521 sync_err = sync_fileindex(fileindex, fileindex_path);
8522 if (sync_err && err == NULL)
8523 err = sync_err;
8524 done:
8525 got_fileindex_free(fileindex);
8526 free(fileindex_path);
8527 unlockerr = lock_worktree(worktree, LOCK_SH);
8528 if (unlockerr && err == NULL)
8529 err = unlockerr;
8530 return err;
8533 const struct got_error *
8534 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8535 struct got_repository *repo)
8537 const struct got_error *err;
8538 char *branch_refname = NULL;
8539 struct got_reference *branch_ref = NULL;
8541 *in_progress = 0;
8543 err = get_merge_branch_ref_name(&branch_refname, worktree);
8544 if (err)
8545 return err;
8546 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8547 free(branch_refname);
8548 if (err) {
8549 if (err->code != GOT_ERR_NOT_REF)
8550 return err;
8551 } else
8552 *in_progress = 1;
8554 return NULL;
8557 const struct got_error *got_worktree_merge_prepare(
8558 struct got_fileindex **fileindex, struct got_worktree *worktree,
8559 struct got_repository *repo)
8561 const struct got_error *err = NULL;
8562 char *fileindex_path = NULL;
8563 struct got_reference *wt_branch = NULL;
8564 struct got_object_id *wt_branch_tip = NULL;
8565 struct check_rebase_ok_arg ok_arg;
8567 *fileindex = NULL;
8569 err = lock_worktree(worktree, LOCK_EX);
8570 if (err)
8571 return err;
8573 err = open_fileindex(fileindex, &fileindex_path, worktree);
8574 if (err)
8575 goto done;
8577 /* Preconditions are the same as for rebase. */
8578 ok_arg.worktree = worktree;
8579 ok_arg.repo = repo;
8580 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8581 &ok_arg);
8582 if (err)
8583 goto done;
8585 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8586 0);
8587 if (err)
8588 goto done;
8590 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8591 if (err)
8592 goto done;
8594 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8595 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8596 goto done;
8599 done:
8600 free(fileindex_path);
8601 if (wt_branch)
8602 got_ref_close(wt_branch);
8603 free(wt_branch_tip);
8604 if (err) {
8605 if (*fileindex) {
8606 got_fileindex_free(*fileindex);
8607 *fileindex = NULL;
8609 lock_worktree(worktree, LOCK_SH);
8611 return err;
8614 const struct got_error *got_worktree_merge_write_refs(
8615 struct got_worktree *worktree, struct got_reference *branch,
8616 struct got_repository *repo)
8618 const struct got_error *err = NULL;
8619 char *branch_refname = NULL, *commit_refname = NULL;
8620 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8621 struct got_object_id *branch_tip = NULL;
8623 err = get_merge_branch_ref_name(&branch_refname, worktree);
8624 if (err)
8625 return err;
8627 err = get_merge_commit_ref_name(&commit_refname, worktree);
8628 if (err)
8629 return err;
8631 err = got_ref_resolve(&branch_tip, repo, branch);
8632 if (err)
8633 goto done;
8635 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8636 if (err)
8637 goto done;
8638 err = got_ref_write(branch_ref, repo);
8639 if (err)
8640 goto done;
8642 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8643 if (err)
8644 goto done;
8645 err = got_ref_write(commit_ref, repo);
8646 if (err)
8647 goto done;
8649 done:
8650 free(branch_refname);
8651 free(commit_refname);
8652 if (branch_ref)
8653 got_ref_close(branch_ref);
8654 if (commit_ref)
8655 got_ref_close(commit_ref);
8656 free(branch_tip);
8657 return err;
8660 const struct got_error *
8661 got_worktree_merge_continue(char **branch_name,
8662 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8663 struct got_worktree *worktree, struct got_repository *repo)
8665 const struct got_error *err;
8666 char *commit_refname = NULL, *branch_refname = NULL;
8667 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8668 char *fileindex_path = NULL;
8669 int have_staged_files = 0;
8671 *branch_name = NULL;
8672 *branch_tip = NULL;
8673 *fileindex = NULL;
8675 err = lock_worktree(worktree, LOCK_EX);
8676 if (err)
8677 return err;
8679 err = open_fileindex(fileindex, &fileindex_path, worktree);
8680 if (err)
8681 goto done;
8683 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8684 &have_staged_files);
8685 if (err && err->code != GOT_ERR_CANCELLED)
8686 goto done;
8687 if (have_staged_files) {
8688 err = got_error(GOT_ERR_STAGED_PATHS);
8689 goto done;
8692 err = get_merge_branch_ref_name(&branch_refname, worktree);
8693 if (err)
8694 goto done;
8696 err = get_merge_commit_ref_name(&commit_refname, worktree);
8697 if (err)
8698 goto done;
8700 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8701 if (err)
8702 goto done;
8704 if (!got_ref_is_symbolic(branch_ref)) {
8705 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8706 "%s is not a symbolic reference",
8707 got_ref_get_name(branch_ref));
8708 goto done;
8710 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8711 if (*branch_name == NULL) {
8712 err = got_error_from_errno("strdup");
8713 goto done;
8716 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8717 if (err)
8718 goto done;
8720 err = got_ref_resolve(branch_tip, repo, commit_ref);
8721 if (err)
8722 goto done;
8723 done:
8724 free(commit_refname);
8725 free(branch_refname);
8726 free(fileindex_path);
8727 if (commit_ref)
8728 got_ref_close(commit_ref);
8729 if (branch_ref)
8730 got_ref_close(branch_ref);
8731 if (err) {
8732 if (*branch_name) {
8733 free(*branch_name);
8734 *branch_name = NULL;
8736 free(*branch_tip);
8737 *branch_tip = NULL;
8738 if (*fileindex) {
8739 got_fileindex_free(*fileindex);
8740 *fileindex = NULL;
8742 lock_worktree(worktree, LOCK_SH);
8744 return err;
8747 const struct got_error *
8748 got_worktree_merge_abort(struct got_worktree *worktree,
8749 struct got_fileindex *fileindex, struct got_repository *repo,
8750 got_worktree_checkout_cb progress_cb, void *progress_arg)
8752 const struct got_error *err, *unlockerr, *sync_err;
8753 struct got_commit_object *commit = NULL;
8754 char *fileindex_path = NULL;
8755 struct revert_file_args rfa;
8756 char *commit_ref_name = NULL;
8757 struct got_reference *commit_ref = NULL;
8758 struct got_object_id *merged_commit_id = NULL;
8759 struct got_object_id *tree_id = NULL;
8760 struct got_pathlist_head added_paths;
8762 TAILQ_INIT(&added_paths);
8764 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8765 if (err)
8766 goto done;
8768 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8769 if (err)
8770 goto done;
8772 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8773 if (err)
8774 goto done;
8777 * Determine which files in added status can be safely removed
8778 * from disk while reverting changes in the work tree.
8779 * We want to avoid deleting unrelated files which were added by
8780 * the user for conflict resolution purposes.
8782 err = get_paths_added_between_commits(&added_paths,
8783 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8784 got_worktree_get_path_prefix(worktree), repo);
8785 if (err)
8786 goto done;
8789 err = got_object_open_as_commit(&commit, repo,
8790 worktree->base_commit_id);
8791 if (err)
8792 goto done;
8794 err = got_object_id_by_path(&tree_id, repo, commit,
8795 worktree->path_prefix);
8796 if (err)
8797 goto done;
8799 err = delete_merge_refs(worktree, repo);
8800 if (err)
8801 goto done;
8803 err = get_fileindex_path(&fileindex_path, worktree);
8804 if (err)
8805 goto done;
8807 rfa.worktree = worktree;
8808 rfa.fileindex = fileindex;
8809 rfa.progress_cb = progress_cb;
8810 rfa.progress_arg = progress_arg;
8811 rfa.patch_cb = NULL;
8812 rfa.patch_arg = NULL;
8813 rfa.repo = repo;
8814 rfa.unlink_added_files = 1;
8815 rfa.added_files_to_unlink = &added_paths;
8816 err = worktree_status(worktree, "", fileindex, repo,
8817 revert_file, &rfa, NULL, NULL, 1, 0);
8818 if (err)
8819 goto sync;
8821 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8822 repo, progress_cb, progress_arg, NULL, NULL);
8823 sync:
8824 sync_err = sync_fileindex(fileindex, fileindex_path);
8825 if (sync_err && err == NULL)
8826 err = sync_err;
8827 done:
8828 free(tree_id);
8829 free(merged_commit_id);
8830 if (commit)
8831 got_object_commit_close(commit);
8832 if (fileindex)
8833 got_fileindex_free(fileindex);
8834 free(fileindex_path);
8835 if (commit_ref)
8836 got_ref_close(commit_ref);
8837 free(commit_ref_name);
8839 unlockerr = lock_worktree(worktree, LOCK_SH);
8840 if (unlockerr && err == NULL)
8841 err = unlockerr;
8842 return err;
8845 struct check_stage_ok_arg {
8846 struct got_object_id *head_commit_id;
8847 struct got_worktree *worktree;
8848 struct got_fileindex *fileindex;
8849 struct got_repository *repo;
8850 int have_changes;
8853 static const struct got_error *
8854 check_stage_ok(void *arg, unsigned char status,
8855 unsigned char staged_status, const char *relpath,
8856 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8857 struct got_object_id *commit_id, int dirfd, const char *de_name)
8859 struct check_stage_ok_arg *a = arg;
8860 const struct got_error *err = NULL;
8861 struct got_fileindex_entry *ie;
8862 struct got_object_id base_commit_id;
8863 struct got_object_id *base_commit_idp = NULL;
8864 char *in_repo_path = NULL, *p;
8866 if (status == GOT_STATUS_UNVERSIONED ||
8867 status == GOT_STATUS_NO_CHANGE)
8868 return NULL;
8869 if (status == GOT_STATUS_NONEXISTENT)
8870 return got_error_set_errno(ENOENT, relpath);
8872 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8873 if (ie == NULL)
8874 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8876 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8877 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8878 relpath) == -1)
8879 return got_error_from_errno("asprintf");
8881 if (got_fileindex_entry_has_commit(ie)) {
8882 base_commit_idp = got_fileindex_entry_get_commit_id(
8883 &base_commit_id, ie);
8886 if (status == GOT_STATUS_CONFLICT) {
8887 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8888 goto done;
8889 } else if (status != GOT_STATUS_ADD &&
8890 status != GOT_STATUS_MODIFY &&
8891 status != GOT_STATUS_DELETE) {
8892 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8893 goto done;
8896 a->have_changes = 1;
8898 p = in_repo_path;
8899 while (p[0] == '/')
8900 p++;
8901 err = check_out_of_date(p, status, staged_status,
8902 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8903 GOT_ERR_STAGE_OUT_OF_DATE);
8904 done:
8905 free(in_repo_path);
8906 return err;
8909 struct stage_path_arg {
8910 struct got_worktree *worktree;
8911 struct got_fileindex *fileindex;
8912 struct got_repository *repo;
8913 got_worktree_status_cb status_cb;
8914 void *status_arg;
8915 got_worktree_patch_cb patch_cb;
8916 void *patch_arg;
8917 int staged_something;
8918 int allow_bad_symlinks;
8921 static const struct got_error *
8922 stage_path(void *arg, unsigned char status,
8923 unsigned char staged_status, const char *relpath,
8924 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8925 struct got_object_id *commit_id, int dirfd, const char *de_name)
8927 struct stage_path_arg *a = arg;
8928 const struct got_error *err = NULL;
8929 struct got_fileindex_entry *ie;
8930 char *ondisk_path = NULL, *path_content = NULL;
8931 uint32_t stage;
8932 struct got_object_id *new_staged_blob_id = NULL;
8933 struct stat sb;
8935 if (status == GOT_STATUS_UNVERSIONED)
8936 return NULL;
8938 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8939 if (ie == NULL)
8940 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8942 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8943 relpath)== -1)
8944 return got_error_from_errno("asprintf");
8946 switch (status) {
8947 case GOT_STATUS_ADD:
8948 case GOT_STATUS_MODIFY:
8949 /* XXX could sb.st_mode be passed in by our caller? */
8950 if (lstat(ondisk_path, &sb) == -1) {
8951 err = got_error_from_errno2("lstat", ondisk_path);
8952 break;
8954 if (a->patch_cb) {
8955 if (status == GOT_STATUS_ADD) {
8956 int choice = GOT_PATCH_CHOICE_NONE;
8957 err = (*a->patch_cb)(&choice, a->patch_arg,
8958 status, ie->path, NULL, 1, 1);
8959 if (err)
8960 break;
8961 if (choice != GOT_PATCH_CHOICE_YES)
8962 break;
8963 } else {
8964 err = create_patched_content(&path_content, 0,
8965 staged_blob_id ? staged_blob_id : blob_id,
8966 ondisk_path, dirfd, de_name, ie->path,
8967 a->repo, a->patch_cb, a->patch_arg);
8968 if (err || path_content == NULL)
8969 break;
8972 err = got_object_blob_create(&new_staged_blob_id,
8973 path_content ? path_content : ondisk_path, a->repo);
8974 if (err)
8975 break;
8976 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8977 SHA1_DIGEST_LENGTH);
8978 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8979 stage = GOT_FILEIDX_STAGE_ADD;
8980 else
8981 stage = GOT_FILEIDX_STAGE_MODIFY;
8982 got_fileindex_entry_stage_set(ie, stage);
8983 if (S_ISLNK(sb.st_mode)) {
8984 int is_bad_symlink = 0;
8985 if (!a->allow_bad_symlinks) {
8986 char target_path[PATH_MAX];
8987 ssize_t target_len;
8988 target_len = readlink(ondisk_path, target_path,
8989 sizeof(target_path));
8990 if (target_len == -1) {
8991 err = got_error_from_errno2("readlink",
8992 ondisk_path);
8993 break;
8995 err = is_bad_symlink_target(&is_bad_symlink,
8996 target_path, target_len, ondisk_path,
8997 a->worktree->root_path,
8998 a->worktree->meta_dir);
8999 if (err)
9000 break;
9001 if (is_bad_symlink) {
9002 err = got_error_path(ondisk_path,
9003 GOT_ERR_BAD_SYMLINK);
9004 break;
9007 if (is_bad_symlink)
9008 got_fileindex_entry_staged_filetype_set(ie,
9009 GOT_FILEIDX_MODE_BAD_SYMLINK);
9010 else
9011 got_fileindex_entry_staged_filetype_set(ie,
9012 GOT_FILEIDX_MODE_SYMLINK);
9013 } else {
9014 got_fileindex_entry_staged_filetype_set(ie,
9015 GOT_FILEIDX_MODE_REGULAR_FILE);
9017 a->staged_something = 1;
9018 if (a->status_cb == NULL)
9019 break;
9020 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9021 get_staged_status(ie), relpath, blob_id,
9022 new_staged_blob_id, NULL, dirfd, de_name);
9023 if (err)
9024 break;
9026 * When staging the reverse of the staged diff,
9027 * implicitly unstage the file.
9029 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9030 sizeof(ie->blob_sha1)) == 0) {
9031 got_fileindex_entry_stage_set(ie,
9032 GOT_FILEIDX_STAGE_NONE);
9034 break;
9035 case GOT_STATUS_DELETE:
9036 if (staged_status == GOT_STATUS_DELETE)
9037 break;
9038 if (a->patch_cb) {
9039 int choice = GOT_PATCH_CHOICE_NONE;
9040 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9041 ie->path, NULL, 1, 1);
9042 if (err)
9043 break;
9044 if (choice == GOT_PATCH_CHOICE_NO)
9045 break;
9046 if (choice != GOT_PATCH_CHOICE_YES) {
9047 err = got_error(GOT_ERR_PATCH_CHOICE);
9048 break;
9051 stage = GOT_FILEIDX_STAGE_DELETE;
9052 got_fileindex_entry_stage_set(ie, stage);
9053 a->staged_something = 1;
9054 if (a->status_cb == NULL)
9055 break;
9056 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9057 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9058 de_name);
9059 break;
9060 case GOT_STATUS_NO_CHANGE:
9061 break;
9062 case GOT_STATUS_CONFLICT:
9063 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9064 break;
9065 case GOT_STATUS_NONEXISTENT:
9066 err = got_error_set_errno(ENOENT, relpath);
9067 break;
9068 default:
9069 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9070 break;
9073 if (path_content && unlink(path_content) == -1 && err == NULL)
9074 err = got_error_from_errno2("unlink", path_content);
9075 free(path_content);
9076 free(ondisk_path);
9077 free(new_staged_blob_id);
9078 return err;
9081 const struct got_error *
9082 got_worktree_stage(struct got_worktree *worktree,
9083 struct got_pathlist_head *paths,
9084 got_worktree_status_cb status_cb, void *status_arg,
9085 got_worktree_patch_cb patch_cb, void *patch_arg,
9086 int allow_bad_symlinks, struct got_repository *repo)
9088 const struct got_error *err = NULL, *sync_err, *unlockerr;
9089 struct got_pathlist_entry *pe;
9090 struct got_fileindex *fileindex = NULL;
9091 char *fileindex_path = NULL;
9092 struct got_reference *head_ref = NULL;
9093 struct got_object_id *head_commit_id = NULL;
9094 struct check_stage_ok_arg oka;
9095 struct stage_path_arg spa;
9097 err = lock_worktree(worktree, LOCK_EX);
9098 if (err)
9099 return err;
9101 err = got_ref_open(&head_ref, repo,
9102 got_worktree_get_head_ref_name(worktree), 0);
9103 if (err)
9104 goto done;
9105 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9106 if (err)
9107 goto done;
9108 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9109 if (err)
9110 goto done;
9112 /* Check pre-conditions before staging anything. */
9113 oka.head_commit_id = head_commit_id;
9114 oka.worktree = worktree;
9115 oka.fileindex = fileindex;
9116 oka.repo = repo;
9117 oka.have_changes = 0;
9118 TAILQ_FOREACH(pe, paths, entry) {
9119 err = worktree_status(worktree, pe->path, fileindex, repo,
9120 check_stage_ok, &oka, NULL, NULL, 1, 0);
9121 if (err)
9122 goto done;
9124 if (!oka.have_changes) {
9125 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9126 goto done;
9129 spa.worktree = worktree;
9130 spa.fileindex = fileindex;
9131 spa.repo = repo;
9132 spa.patch_cb = patch_cb;
9133 spa.patch_arg = patch_arg;
9134 spa.status_cb = status_cb;
9135 spa.status_arg = status_arg;
9136 spa.staged_something = 0;
9137 spa.allow_bad_symlinks = allow_bad_symlinks;
9138 TAILQ_FOREACH(pe, paths, entry) {
9139 err = worktree_status(worktree, pe->path, fileindex, repo,
9140 stage_path, &spa, NULL, NULL, 1, 0);
9141 if (err)
9142 goto done;
9144 if (!spa.staged_something) {
9145 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9146 goto done;
9149 sync_err = sync_fileindex(fileindex, fileindex_path);
9150 if (sync_err && err == NULL)
9151 err = sync_err;
9152 done:
9153 if (head_ref)
9154 got_ref_close(head_ref);
9155 free(head_commit_id);
9156 free(fileindex_path);
9157 if (fileindex)
9158 got_fileindex_free(fileindex);
9159 unlockerr = lock_worktree(worktree, LOCK_SH);
9160 if (unlockerr && err == NULL)
9161 err = unlockerr;
9162 return err;
9165 struct unstage_path_arg {
9166 struct got_worktree *worktree;
9167 struct got_fileindex *fileindex;
9168 struct got_repository *repo;
9169 got_worktree_checkout_cb progress_cb;
9170 void *progress_arg;
9171 got_worktree_patch_cb patch_cb;
9172 void *patch_arg;
9175 static const struct got_error *
9176 create_unstaged_content(char **path_unstaged_content,
9177 char **path_new_staged_content, struct got_object_id *blob_id,
9178 struct got_object_id *staged_blob_id, const char *relpath,
9179 struct got_repository *repo,
9180 got_worktree_patch_cb patch_cb, void *patch_arg)
9182 const struct got_error *err, *free_err;
9183 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9184 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9185 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9186 struct got_diffreg_result *diffreg_result = NULL;
9187 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9188 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9189 int fd1 = -1, fd2 = -1;
9191 *path_unstaged_content = NULL;
9192 *path_new_staged_content = NULL;
9194 err = got_object_id_str(&label1, blob_id);
9195 if (err)
9196 return err;
9198 fd1 = got_opentempfd();
9199 if (fd1 == -1) {
9200 err = got_error_from_errno("got_opentempfd");
9201 goto done;
9203 fd2 = got_opentempfd();
9204 if (fd2 == -1) {
9205 err = got_error_from_errno("got_opentempfd");
9206 goto done;
9209 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9210 if (err)
9211 goto done;
9213 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9214 if (err)
9215 goto done;
9217 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9218 if (err)
9219 goto done;
9221 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9222 fd2);
9223 if (err)
9224 goto done;
9226 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9227 if (err)
9228 goto done;
9230 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9231 if (err)
9232 goto done;
9234 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9235 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9236 if (err)
9237 goto done;
9239 err = got_opentemp_named(path_unstaged_content, &outfile,
9240 "got-unstaged-content", "");
9241 if (err)
9242 goto done;
9243 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9244 "got-new-staged-content", "");
9245 if (err)
9246 goto done;
9248 if (fseek(f1, 0L, SEEK_SET) == -1) {
9249 err = got_ferror(f1, GOT_ERR_IO);
9250 goto done;
9252 if (fseek(f2, 0L, SEEK_SET) == -1) {
9253 err = got_ferror(f2, GOT_ERR_IO);
9254 goto done;
9256 /* Count the number of actual changes in the diff result. */
9257 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9258 struct diff_chunk_context cc = {};
9259 diff_chunk_context_load_change(&cc, &nchunks_used,
9260 diffreg_result->result, n, 0);
9261 nchanges++;
9263 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9264 int choice;
9265 err = apply_or_reject_change(&choice, &nchunks_used,
9266 diffreg_result->result, n, relpath, f1, f2,
9267 &line_cur1, &line_cur2,
9268 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9269 if (err)
9270 goto done;
9271 if (choice == GOT_PATCH_CHOICE_YES)
9272 have_content = 1;
9273 else
9274 have_rejected_content = 1;
9275 if (choice == GOT_PATCH_CHOICE_QUIT)
9276 break;
9278 if (have_content || have_rejected_content)
9279 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9280 outfile, rejectfile);
9281 done:
9282 free(label1);
9283 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9284 err = got_error_from_errno("close");
9285 if (blob)
9286 got_object_blob_close(blob);
9287 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9288 err = got_error_from_errno("close");
9289 if (staged_blob)
9290 got_object_blob_close(staged_blob);
9291 free_err = got_diffreg_result_free(diffreg_result);
9292 if (free_err && err == NULL)
9293 err = free_err;
9294 if (f1 && fclose(f1) == EOF && err == NULL)
9295 err = got_error_from_errno2("fclose", path1);
9296 if (f2 && fclose(f2) == EOF && err == NULL)
9297 err = got_error_from_errno2("fclose", path2);
9298 if (outfile && fclose(outfile) == EOF && err == NULL)
9299 err = got_error_from_errno2("fclose", *path_unstaged_content);
9300 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9301 err = got_error_from_errno2("fclose", *path_new_staged_content);
9302 if (path1 && unlink(path1) == -1 && err == NULL)
9303 err = got_error_from_errno2("unlink", path1);
9304 if (path2 && unlink(path2) == -1 && err == NULL)
9305 err = got_error_from_errno2("unlink", path2);
9306 if (err || !have_content) {
9307 if (*path_unstaged_content &&
9308 unlink(*path_unstaged_content) == -1 && err == NULL)
9309 err = got_error_from_errno2("unlink",
9310 *path_unstaged_content);
9311 free(*path_unstaged_content);
9312 *path_unstaged_content = NULL;
9314 if (err || !have_content || !have_rejected_content) {
9315 if (*path_new_staged_content &&
9316 unlink(*path_new_staged_content) == -1 && err == NULL)
9317 err = got_error_from_errno2("unlink",
9318 *path_new_staged_content);
9319 free(*path_new_staged_content);
9320 *path_new_staged_content = NULL;
9322 free(path1);
9323 free(path2);
9324 return err;
9327 static const struct got_error *
9328 unstage_hunks(struct got_object_id *staged_blob_id,
9329 struct got_blob_object *blob_base,
9330 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9331 const char *ondisk_path, const char *label_orig,
9332 struct got_worktree *worktree, struct got_repository *repo,
9333 got_worktree_patch_cb patch_cb, void *patch_arg,
9334 got_worktree_checkout_cb progress_cb, void *progress_arg)
9336 const struct got_error *err = NULL;
9337 char *path_unstaged_content = NULL;
9338 char *path_new_staged_content = NULL;
9339 char *parent = NULL, *base_path = NULL;
9340 char *blob_base_path = NULL;
9341 struct got_object_id *new_staged_blob_id = NULL;
9342 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9343 struct stat sb;
9345 err = create_unstaged_content(&path_unstaged_content,
9346 &path_new_staged_content, blob_id, staged_blob_id,
9347 ie->path, repo, patch_cb, patch_arg);
9348 if (err)
9349 return err;
9351 if (path_unstaged_content == NULL)
9352 return NULL;
9354 if (path_new_staged_content) {
9355 err = got_object_blob_create(&new_staged_blob_id,
9356 path_new_staged_content, repo);
9357 if (err)
9358 goto done;
9361 f = fopen(path_unstaged_content, "re");
9362 if (f == NULL) {
9363 err = got_error_from_errno2("fopen",
9364 path_unstaged_content);
9365 goto done;
9367 if (fstat(fileno(f), &sb) == -1) {
9368 err = got_error_from_errno2("fstat", path_unstaged_content);
9369 goto done;
9371 if (got_fileindex_entry_staged_filetype_get(ie) ==
9372 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9373 char link_target[PATH_MAX];
9374 size_t r;
9375 r = fread(link_target, 1, sizeof(link_target), f);
9376 if (r == 0 && ferror(f)) {
9377 err = got_error_from_errno("fread");
9378 goto done;
9380 if (r >= sizeof(link_target)) { /* should not happen */
9381 err = got_error(GOT_ERR_NO_SPACE);
9382 goto done;
9384 link_target[r] = '\0';
9385 err = merge_symlink(worktree, blob_base,
9386 ondisk_path, ie->path, label_orig, link_target,
9387 worktree->base_commit_id, repo, progress_cb,
9388 progress_arg);
9389 } else {
9390 int local_changes_subsumed;
9392 err = got_path_dirname(&parent, ondisk_path);
9393 if (err)
9394 return err;
9396 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9397 parent) == -1) {
9398 err = got_error_from_errno("asprintf");
9399 base_path = NULL;
9400 goto done;
9403 err = got_opentemp_named(&blob_base_path, &f_base,
9404 base_path, "");
9405 if (err)
9406 goto done;
9407 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9408 blob_base);
9409 if (err)
9410 goto done;
9413 * In order the run a 3-way merge with a symlink we copy the symlink's
9414 * target path into a temporary file and use that file with diff3.
9416 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9417 err = dump_symlink_target_path_to_file(&f_deriv2,
9418 ondisk_path);
9419 if (err)
9420 goto done;
9421 } else {
9422 int fd;
9423 fd = open(ondisk_path,
9424 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9425 if (fd == -1) {
9426 err = got_error_from_errno2("open", ondisk_path);
9427 goto done;
9429 f_deriv2 = fdopen(fd, "r");
9430 if (f_deriv2 == NULL) {
9431 err = got_error_from_errno2("fdopen", ondisk_path);
9432 close(fd);
9433 goto done;
9437 err = merge_file(&local_changes_subsumed, worktree,
9438 f_base, f, f_deriv2, ondisk_path, ie->path,
9439 got_fileindex_perms_to_st(ie),
9440 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9441 repo, progress_cb, progress_arg);
9443 if (err)
9444 goto done;
9446 if (new_staged_blob_id) {
9447 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9448 SHA1_DIGEST_LENGTH);
9449 } else {
9450 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9451 got_fileindex_entry_staged_filetype_set(ie, 0);
9453 done:
9454 free(new_staged_blob_id);
9455 if (path_unstaged_content &&
9456 unlink(path_unstaged_content) == -1 && err == NULL)
9457 err = got_error_from_errno2("unlink", path_unstaged_content);
9458 if (path_new_staged_content &&
9459 unlink(path_new_staged_content) == -1 && err == NULL)
9460 err = got_error_from_errno2("unlink", path_new_staged_content);
9461 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9462 err = got_error_from_errno2("unlink", blob_base_path);
9463 if (f_base && fclose(f_base) == EOF && err == NULL)
9464 err = got_error_from_errno2("fclose", path_unstaged_content);
9465 if (f && fclose(f) == EOF && err == NULL)
9466 err = got_error_from_errno2("fclose", path_unstaged_content);
9467 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9468 err = got_error_from_errno2("fclose", ondisk_path);
9469 free(path_unstaged_content);
9470 free(path_new_staged_content);
9471 free(blob_base_path);
9472 free(parent);
9473 free(base_path);
9474 return err;
9477 static const struct got_error *
9478 unstage_path(void *arg, unsigned char status,
9479 unsigned char staged_status, const char *relpath,
9480 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9481 struct got_object_id *commit_id, int dirfd, const char *de_name)
9483 const struct got_error *err = NULL;
9484 struct unstage_path_arg *a = arg;
9485 struct got_fileindex_entry *ie;
9486 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9487 char *ondisk_path = NULL;
9488 char *id_str = NULL, *label_orig = NULL;
9489 int local_changes_subsumed;
9490 struct stat sb;
9491 int fd1 = -1, fd2 = -1;
9493 if (staged_status != GOT_STATUS_ADD &&
9494 staged_status != GOT_STATUS_MODIFY &&
9495 staged_status != GOT_STATUS_DELETE)
9496 return NULL;
9498 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9499 if (ie == NULL)
9500 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9502 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9503 == -1)
9504 return got_error_from_errno("asprintf");
9506 err = got_object_id_str(&id_str,
9507 commit_id ? commit_id : a->worktree->base_commit_id);
9508 if (err)
9509 goto done;
9510 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9511 id_str) == -1) {
9512 err = got_error_from_errno("asprintf");
9513 goto done;
9516 fd1 = got_opentempfd();
9517 if (fd1 == -1) {
9518 err = got_error_from_errno("got_opentempfd");
9519 goto done;
9521 fd2 = got_opentempfd();
9522 if (fd2 == -1) {
9523 err = got_error_from_errno("got_opentempfd");
9524 goto done;
9527 switch (staged_status) {
9528 case GOT_STATUS_MODIFY:
9529 err = got_object_open_as_blob(&blob_base, a->repo,
9530 blob_id, 8192, fd1);
9531 if (err)
9532 break;
9533 /* fall through */
9534 case GOT_STATUS_ADD:
9535 if (a->patch_cb) {
9536 if (staged_status == GOT_STATUS_ADD) {
9537 int choice = GOT_PATCH_CHOICE_NONE;
9538 err = (*a->patch_cb)(&choice, a->patch_arg,
9539 staged_status, ie->path, NULL, 1, 1);
9540 if (err)
9541 break;
9542 if (choice != GOT_PATCH_CHOICE_YES)
9543 break;
9544 } else {
9545 err = unstage_hunks(staged_blob_id,
9546 blob_base, blob_id, ie, ondisk_path,
9547 label_orig, a->worktree, a->repo,
9548 a->patch_cb, a->patch_arg,
9549 a->progress_cb, a->progress_arg);
9550 break; /* Done with this file. */
9553 err = got_object_open_as_blob(&blob_staged, a->repo,
9554 staged_blob_id, 8192, fd2);
9555 if (err)
9556 break;
9557 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9558 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9559 case GOT_FILEIDX_MODE_REGULAR_FILE:
9560 err = merge_blob(&local_changes_subsumed, a->worktree,
9561 blob_base, ondisk_path, relpath,
9562 got_fileindex_perms_to_st(ie), label_orig,
9563 blob_staged, commit_id ? commit_id :
9564 a->worktree->base_commit_id, a->repo,
9565 a->progress_cb, a->progress_arg);
9566 break;
9567 case GOT_FILEIDX_MODE_SYMLINK:
9568 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9569 char *staged_target;
9570 err = got_object_blob_read_to_str(
9571 &staged_target, blob_staged);
9572 if (err)
9573 goto done;
9574 err = merge_symlink(a->worktree, blob_base,
9575 ondisk_path, relpath, label_orig,
9576 staged_target, commit_id ? commit_id :
9577 a->worktree->base_commit_id,
9578 a->repo, a->progress_cb, a->progress_arg);
9579 free(staged_target);
9580 } else {
9581 err = merge_blob(&local_changes_subsumed,
9582 a->worktree, blob_base, ondisk_path,
9583 relpath, got_fileindex_perms_to_st(ie),
9584 label_orig, blob_staged,
9585 commit_id ? commit_id :
9586 a->worktree->base_commit_id, a->repo,
9587 a->progress_cb, a->progress_arg);
9589 break;
9590 default:
9591 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9592 break;
9594 if (err == NULL) {
9595 got_fileindex_entry_stage_set(ie,
9596 GOT_FILEIDX_STAGE_NONE);
9597 got_fileindex_entry_staged_filetype_set(ie, 0);
9599 break;
9600 case GOT_STATUS_DELETE:
9601 if (a->patch_cb) {
9602 int choice = GOT_PATCH_CHOICE_NONE;
9603 err = (*a->patch_cb)(&choice, a->patch_arg,
9604 staged_status, ie->path, NULL, 1, 1);
9605 if (err)
9606 break;
9607 if (choice == GOT_PATCH_CHOICE_NO)
9608 break;
9609 if (choice != GOT_PATCH_CHOICE_YES) {
9610 err = got_error(GOT_ERR_PATCH_CHOICE);
9611 break;
9614 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9615 got_fileindex_entry_staged_filetype_set(ie, 0);
9616 err = get_file_status(&status, &sb, ie, ondisk_path,
9617 dirfd, de_name, a->repo);
9618 if (err)
9619 break;
9620 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9621 break;
9623 done:
9624 free(ondisk_path);
9625 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9626 err = got_error_from_errno("close");
9627 if (blob_base)
9628 got_object_blob_close(blob_base);
9629 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9630 err = got_error_from_errno("close");
9631 if (blob_staged)
9632 got_object_blob_close(blob_staged);
9633 free(id_str);
9634 free(label_orig);
9635 return err;
9638 const struct got_error *
9639 got_worktree_unstage(struct got_worktree *worktree,
9640 struct got_pathlist_head *paths,
9641 got_worktree_checkout_cb progress_cb, void *progress_arg,
9642 got_worktree_patch_cb patch_cb, void *patch_arg,
9643 struct got_repository *repo)
9645 const struct got_error *err = NULL, *sync_err, *unlockerr;
9646 struct got_pathlist_entry *pe;
9647 struct got_fileindex *fileindex = NULL;
9648 char *fileindex_path = NULL;
9649 struct unstage_path_arg upa;
9651 err = lock_worktree(worktree, LOCK_EX);
9652 if (err)
9653 return err;
9655 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9656 if (err)
9657 goto done;
9659 upa.worktree = worktree;
9660 upa.fileindex = fileindex;
9661 upa.repo = repo;
9662 upa.progress_cb = progress_cb;
9663 upa.progress_arg = progress_arg;
9664 upa.patch_cb = patch_cb;
9665 upa.patch_arg = patch_arg;
9666 TAILQ_FOREACH(pe, paths, entry) {
9667 err = worktree_status(worktree, pe->path, fileindex, repo,
9668 unstage_path, &upa, NULL, NULL, 1, 0);
9669 if (err)
9670 goto done;
9673 sync_err = sync_fileindex(fileindex, fileindex_path);
9674 if (sync_err && err == NULL)
9675 err = sync_err;
9676 done:
9677 free(fileindex_path);
9678 if (fileindex)
9679 got_fileindex_free(fileindex);
9680 unlockerr = lock_worktree(worktree, LOCK_SH);
9681 if (unlockerr && err == NULL)
9682 err = unlockerr;
9683 return err;
9686 struct report_file_info_arg {
9687 struct got_worktree *worktree;
9688 got_worktree_path_info_cb info_cb;
9689 void *info_arg;
9690 struct got_pathlist_head *paths;
9691 got_cancel_cb cancel_cb;
9692 void *cancel_arg;
9695 static const struct got_error *
9696 report_file_info(void *arg, struct got_fileindex_entry *ie)
9698 struct report_file_info_arg *a = arg;
9699 struct got_pathlist_entry *pe;
9700 struct got_object_id blob_id, staged_blob_id, commit_id;
9701 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9702 struct got_object_id *commit_idp = NULL;
9703 int stage;
9705 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9706 return got_error(GOT_ERR_CANCELLED);
9708 TAILQ_FOREACH(pe, a->paths, entry) {
9709 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9710 got_path_is_child(ie->path, pe->path, pe->path_len))
9711 break;
9713 if (pe == NULL) /* not found */
9714 return NULL;
9716 if (got_fileindex_entry_has_blob(ie))
9717 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9718 stage = got_fileindex_entry_stage_get(ie);
9719 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9720 stage == GOT_FILEIDX_STAGE_ADD) {
9721 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9722 &staged_blob_id, ie);
9725 if (got_fileindex_entry_has_commit(ie))
9726 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9728 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9729 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9732 const struct got_error *
9733 got_worktree_path_info(struct got_worktree *worktree,
9734 struct got_pathlist_head *paths,
9735 got_worktree_path_info_cb info_cb, void *info_arg,
9736 got_cancel_cb cancel_cb, void *cancel_arg)
9739 const struct got_error *err = NULL, *unlockerr;
9740 struct got_fileindex *fileindex = NULL;
9741 char *fileindex_path = NULL;
9742 struct report_file_info_arg arg;
9744 err = lock_worktree(worktree, LOCK_SH);
9745 if (err)
9746 return err;
9748 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9749 if (err)
9750 goto done;
9752 arg.worktree = worktree;
9753 arg.info_cb = info_cb;
9754 arg.info_arg = info_arg;
9755 arg.paths = paths;
9756 arg.cancel_cb = cancel_cb;
9757 arg.cancel_arg = cancel_arg;
9758 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9759 &arg);
9760 done:
9761 free(fileindex_path);
9762 if (fileindex)
9763 got_fileindex_free(fileindex);
9764 unlockerr = lock_worktree(worktree, LOCK_UN);
9765 if (unlockerr && err == NULL)
9766 err = unlockerr;
9767 return err;
9770 static const struct got_error *
9771 patch_check_path(const char *p, char **path, unsigned char *status,
9772 unsigned char *staged_status, struct got_fileindex *fileindex,
9773 struct got_worktree *worktree, struct got_repository *repo)
9775 const struct got_error *err;
9776 struct got_fileindex_entry *ie;
9777 struct stat sb;
9778 char *ondisk_path = NULL;
9780 err = got_worktree_resolve_path(path, worktree, p);
9781 if (err)
9782 return err;
9784 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9785 *path[0] ? "/" : "", *path) == -1)
9786 return got_error_from_errno("asprintf");
9788 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9789 if (ie) {
9790 *staged_status = get_staged_status(ie);
9791 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9792 repo);
9793 if (err)
9794 goto done;
9795 } else {
9796 *staged_status = GOT_STATUS_NO_CHANGE;
9797 *status = GOT_STATUS_UNVERSIONED;
9798 if (lstat(ondisk_path, &sb) == -1) {
9799 if (errno != ENOENT) {
9800 err = got_error_from_errno2("lstat",
9801 ondisk_path);
9802 goto done;
9804 *status = GOT_STATUS_NONEXISTENT;
9808 done:
9809 free(ondisk_path);
9810 return err;
9813 static const struct got_error *
9814 patch_can_rm(const char *path, unsigned char status,
9815 unsigned char staged_status)
9817 if (status == GOT_STATUS_NONEXISTENT)
9818 return got_error_set_errno(ENOENT, path);
9819 if (status != GOT_STATUS_NO_CHANGE &&
9820 status != GOT_STATUS_ADD &&
9821 status != GOT_STATUS_MODIFY &&
9822 status != GOT_STATUS_MODE_CHANGE)
9823 return got_error_path(path, GOT_ERR_FILE_STATUS);
9824 if (staged_status == GOT_STATUS_DELETE)
9825 return got_error_path(path, GOT_ERR_FILE_STATUS);
9826 return NULL;
9829 static const struct got_error *
9830 patch_can_add(const char *path, unsigned char status)
9832 if (status != GOT_STATUS_NONEXISTENT)
9833 return got_error_path(path, GOT_ERR_FILE_STATUS);
9834 return NULL;
9837 static const struct got_error *
9838 patch_can_edit(const char *path, unsigned char status,
9839 unsigned char staged_status)
9841 if (status == GOT_STATUS_NONEXISTENT)
9842 return got_error_set_errno(ENOENT, path);
9843 if (status != GOT_STATUS_NO_CHANGE &&
9844 status != GOT_STATUS_ADD &&
9845 status != GOT_STATUS_MODIFY)
9846 return got_error_path(path, GOT_ERR_FILE_STATUS);
9847 if (staged_status == GOT_STATUS_DELETE)
9848 return got_error_path(path, GOT_ERR_FILE_STATUS);
9849 return NULL;
9852 const struct got_error *
9853 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9854 char **fileindex_path, struct got_worktree *worktree)
9856 return open_fileindex(fileindex, fileindex_path, worktree);
9859 const struct got_error *
9860 got_worktree_patch_check_path(const char *old, const char *new,
9861 char **oldpath, char **newpath, struct got_worktree *worktree,
9862 struct got_repository *repo, struct got_fileindex *fileindex)
9864 const struct got_error *err = NULL;
9865 int file_renamed = 0;
9866 unsigned char status_old, staged_status_old;
9867 unsigned char status_new, staged_status_new;
9869 *oldpath = NULL;
9870 *newpath = NULL;
9872 err = patch_check_path(old != NULL ? old : new, oldpath,
9873 &status_old, &staged_status_old, fileindex, worktree, repo);
9874 if (err)
9875 goto done;
9877 err = patch_check_path(new != NULL ? new : old, newpath,
9878 &status_new, &staged_status_new, fileindex, worktree, repo);
9879 if (err)
9880 goto done;
9882 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9883 file_renamed = 1;
9885 if (old != NULL && new == NULL)
9886 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9887 else if (file_renamed) {
9888 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9889 if (err == NULL)
9890 err = patch_can_add(*newpath, status_new);
9891 } else if (old == NULL)
9892 err = patch_can_add(*newpath, status_new);
9893 else
9894 err = patch_can_edit(*newpath, status_new, staged_status_new);
9896 done:
9897 if (err) {
9898 free(*oldpath);
9899 *oldpath = NULL;
9900 free(*newpath);
9901 *newpath = NULL;
9903 return err;
9906 const struct got_error *
9907 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9908 struct got_worktree *worktree, struct got_fileindex *fileindex,
9909 got_worktree_checkout_cb progress_cb, void *progress_arg)
9911 struct schedule_addition_args saa;
9913 memset(&saa, 0, sizeof(saa));
9914 saa.worktree = worktree;
9915 saa.fileindex = fileindex;
9916 saa.progress_cb = progress_cb;
9917 saa.progress_arg = progress_arg;
9918 saa.repo = repo;
9920 return worktree_status(worktree, path, fileindex, repo,
9921 schedule_addition, &saa, NULL, NULL, 1, 0);
9924 const struct got_error *
9925 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9926 struct got_worktree *worktree, struct got_fileindex *fileindex,
9927 got_worktree_delete_cb progress_cb, void *progress_arg)
9929 const struct got_error *err;
9930 struct schedule_deletion_args sda;
9931 char *ondisk_status_path;
9933 memset(&sda, 0, sizeof(sda));
9934 sda.worktree = worktree;
9935 sda.fileindex = fileindex;
9936 sda.progress_cb = progress_cb;
9937 sda.progress_arg = progress_arg;
9938 sda.repo = repo;
9939 sda.delete_local_mods = 0;
9940 sda.keep_on_disk = 0;
9941 sda.ignore_missing_paths = 0;
9942 sda.status_codes = NULL;
9943 if (asprintf(&ondisk_status_path, "%s/%s",
9944 got_worktree_get_root_path(worktree), path) == -1)
9945 return got_error_from_errno("asprintf");
9946 sda.status_path = ondisk_status_path;
9947 sda.status_path_len = strlen(ondisk_status_path);
9949 err = worktree_status(worktree, path, fileindex, repo,
9950 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9951 free(ondisk_status_path);
9952 return err;
9955 const struct got_error *
9956 got_worktree_patch_complete(struct got_fileindex *fileindex,
9957 const char *fileindex_path)
9959 const struct got_error *err = NULL;
9961 err = sync_fileindex(fileindex, fileindex_path);
9962 got_fileindex_free(fileindex);
9964 return err;