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 struct check_mixed_commits_args {
3238 struct got_worktree *worktree;
3239 got_cancel_cb cancel_cb;
3240 void *cancel_arg;
3243 static const struct got_error *
3244 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3246 const struct got_error *err;
3247 struct check_mixed_commits_args *a = arg;
3249 if (a->cancel_cb) {
3250 err = a->cancel_cb(a->cancel_arg);
3251 if (err)
3252 return err;
3255 /* Reject merges into a work tree with mixed base commits. */
3256 if (got_fileindex_entry_has_commit(ie) &&
3257 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3258 SHA1_DIGEST_LENGTH) != 0)
3259 return got_error(GOT_ERR_MIXED_COMMITS);
3261 return NULL;
3264 const struct got_error *
3265 got_worktree_get_state(char *state, struct got_repository *repo,
3266 struct got_worktree *worktree,
3267 got_cancel_cb cancel_cb, void *cancel_arg)
3269 const struct got_error *err;
3270 struct got_object_id *base_id, *head_id = NULL;
3271 struct got_reference *head_ref;
3272 struct got_fileindex *fileindex = NULL;
3273 char *fileindex_path = NULL;
3274 struct check_mixed_commits_args cma;
3276 if (worktree == NULL)
3277 return got_error(GOT_ERR_NOT_WORKTREE);
3279 err = got_ref_open(&head_ref, repo,
3280 got_worktree_get_head_ref_name(worktree), 0);
3281 if (err)
3282 return err;
3284 err = got_ref_resolve(&head_id, repo, head_ref);
3285 if (err)
3286 goto done;
3288 *state = GOT_WORKTREE_STATE_UNKNOWN;
3289 base_id = got_worktree_get_base_commit_id(worktree);
3291 cma.worktree = worktree;
3292 cma.cancel_cb = cancel_cb;
3293 cma.cancel_arg = cancel_arg;
3295 if (got_object_id_cmp(base_id, head_id) == 0) {
3296 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3297 if (err)
3298 goto done;
3300 err = got_fileindex_for_each_entry_safe(fileindex,
3301 check_mixed_commits, &cma);
3302 if (err == NULL)
3303 *state = GOT_WORKTREE_STATE_UPTODATE;
3304 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3305 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3306 err = NULL;
3308 } else
3309 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3311 done:
3312 free(head_id);
3313 free(fileindex_path);
3314 got_ref_close(head_ref);
3315 if (fileindex != NULL)
3316 got_fileindex_free(fileindex);
3317 return err;
3320 struct check_merge_conflicts_arg {
3321 struct got_worktree *worktree;
3322 struct got_fileindex *fileindex;
3323 struct got_repository *repo;
3326 static const struct got_error *
3327 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3328 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3329 struct got_object_id *id1, struct got_object_id *id2,
3330 const char *path1, const char *path2,
3331 mode_t mode1, mode_t mode2, struct got_repository *repo)
3333 const struct got_error *err = NULL;
3334 struct check_merge_conflicts_arg *a = arg;
3335 unsigned char status;
3336 struct stat sb;
3337 struct got_fileindex_entry *ie;
3338 const char *path = path2 ? path2 : path1;
3339 struct got_object_id *id = id2 ? id2 : id1;
3340 char *ondisk_path;
3342 if (id == NULL)
3343 return NULL;
3345 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3346 if (ie == NULL)
3347 return NULL;
3349 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3350 == -1)
3351 return got_error_from_errno("asprintf");
3353 /* Reject merges into a work tree with conflicted files. */
3354 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3355 free(ondisk_path);
3356 if (err)
3357 return err;
3358 if (status == GOT_STATUS_CONFLICT)
3359 return got_error(GOT_ERR_CONFLICTS);
3361 return NULL;
3364 static const struct got_error *
3365 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3366 const char *fileindex_path, struct got_object_id *commit_id1,
3367 struct got_object_id *commit_id2, struct got_repository *repo,
3368 got_worktree_checkout_cb progress_cb, void *progress_arg,
3369 got_cancel_cb cancel_cb, void *cancel_arg)
3371 const struct got_error *err = NULL, *sync_err;
3372 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3373 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3374 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3375 struct check_merge_conflicts_arg cmc_arg;
3376 struct merge_file_cb_arg arg;
3377 char *label_orig = NULL;
3378 FILE *f1 = NULL, *f2 = NULL;
3379 int fd1 = -1, fd2 = -1;
3381 if (commit_id1) {
3382 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3383 if (err)
3384 goto done;
3385 err = got_object_id_by_path(&tree_id1, repo, commit1,
3386 worktree->path_prefix);
3387 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3388 goto done;
3390 if (tree_id1) {
3391 char *id_str;
3393 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3394 if (err)
3395 goto done;
3397 err = got_object_id_str(&id_str, commit_id1);
3398 if (err)
3399 goto done;
3401 if (asprintf(&label_orig, "%s: commit %s",
3402 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3403 err = got_error_from_errno("asprintf");
3404 free(id_str);
3405 goto done;
3407 free(id_str);
3409 f1 = got_opentemp();
3410 if (f1 == NULL) {
3411 err = got_error_from_errno("got_opentemp");
3412 goto done;
3416 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3417 if (err)
3418 goto done;
3420 err = got_object_id_by_path(&tree_id2, repo, commit2,
3421 worktree->path_prefix);
3422 if (err)
3423 goto done;
3425 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3426 if (err)
3427 goto done;
3429 f2 = got_opentemp();
3430 if (f2 == NULL) {
3431 err = got_error_from_errno("got_opentemp");
3432 goto done;
3435 fd1 = got_opentempfd();
3436 if (fd1 == -1) {
3437 err = got_error_from_errno("got_opentempfd");
3438 goto done;
3441 fd2 = got_opentempfd();
3442 if (fd2 == -1) {
3443 err = got_error_from_errno("got_opentempfd");
3444 goto done;
3447 cmc_arg.worktree = worktree;
3448 cmc_arg.fileindex = fileindex;
3449 cmc_arg.repo = repo;
3450 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3451 check_merge_conflicts, &cmc_arg, 0);
3452 if (err)
3453 goto done;
3455 arg.worktree = worktree;
3456 arg.fileindex = fileindex;
3457 arg.progress_cb = progress_cb;
3458 arg.progress_arg = progress_arg;
3459 arg.cancel_cb = cancel_cb;
3460 arg.cancel_arg = cancel_arg;
3461 arg.label_orig = label_orig;
3462 arg.commit_id2 = commit_id2;
3463 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3464 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3465 merge_file_cb, &arg, 1);
3466 sync_err = sync_fileindex(fileindex, fileindex_path);
3467 if (sync_err && err == NULL)
3468 err = sync_err;
3469 done:
3470 if (commit1)
3471 got_object_commit_close(commit1);
3472 if (commit2)
3473 got_object_commit_close(commit2);
3474 if (tree1)
3475 got_object_tree_close(tree1);
3476 if (tree2)
3477 got_object_tree_close(tree2);
3478 if (f1 && fclose(f1) == EOF && err == NULL)
3479 err = got_error_from_errno("fclose");
3480 if (f2 && fclose(f2) == EOF && err == NULL)
3481 err = got_error_from_errno("fclose");
3482 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3483 err = got_error_from_errno("close");
3484 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3485 err = got_error_from_errno("close");
3486 free(label_orig);
3487 return err;
3490 const struct got_error *
3491 got_worktree_merge_files(struct got_worktree *worktree,
3492 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3493 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3494 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3496 const struct got_error *err, *unlockerr;
3497 char *fileindex_path = NULL;
3498 struct got_fileindex *fileindex = NULL;
3499 struct check_mixed_commits_args cma;
3501 err = lock_worktree(worktree, LOCK_EX);
3502 if (err)
3503 return err;
3505 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3506 if (err)
3507 goto done;
3509 cma.worktree = worktree;
3510 cma.cancel_cb = cancel_cb;
3511 cma.cancel_arg = cancel_arg;
3513 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3514 &cma);
3515 if (err)
3516 goto done;
3518 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3519 commit_id2, repo, progress_cb, progress_arg,
3520 cancel_cb, cancel_arg);
3521 done:
3522 if (fileindex)
3523 got_fileindex_free(fileindex);
3524 free(fileindex_path);
3525 unlockerr = lock_worktree(worktree, LOCK_SH);
3526 if (unlockerr && err == NULL)
3527 err = unlockerr;
3528 return err;
3531 struct diff_dir_cb_arg {
3532 struct got_fileindex *fileindex;
3533 struct got_worktree *worktree;
3534 const char *status_path;
3535 size_t status_path_len;
3536 struct got_repository *repo;
3537 got_worktree_status_cb status_cb;
3538 void *status_arg;
3539 got_cancel_cb cancel_cb;
3540 void *cancel_arg;
3541 /* A pathlist containing per-directory pathlists of ignore patterns. */
3542 struct got_pathlist_head *ignores;
3543 int report_unchanged;
3544 int no_ignores;
3547 static const struct got_error *
3548 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3549 int dirfd, const char *de_name,
3550 got_worktree_status_cb status_cb, void *status_arg,
3551 struct got_repository *repo, int report_unchanged)
3553 const struct got_error *err = NULL;
3554 unsigned char status = GOT_STATUS_NO_CHANGE;
3555 unsigned char staged_status;
3556 struct stat sb;
3557 struct got_object_id blob_id, commit_id, staged_blob_id;
3558 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3559 struct got_object_id *staged_blob_idp = NULL;
3561 staged_status = get_staged_status(ie);
3562 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3563 if (err)
3564 return err;
3566 if (status == GOT_STATUS_NO_CHANGE &&
3567 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3568 return NULL;
3570 if (got_fileindex_entry_has_blob(ie))
3571 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3572 if (got_fileindex_entry_has_commit(ie))
3573 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3574 if (staged_status == GOT_STATUS_ADD ||
3575 staged_status == GOT_STATUS_MODIFY) {
3576 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3577 &staged_blob_id, ie);
3580 return (*status_cb)(status_arg, status, staged_status,
3581 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3584 static const struct got_error *
3585 status_old_new(void *arg, struct got_fileindex_entry *ie,
3586 struct dirent *de, const char *parent_path, int dirfd)
3588 const struct got_error *err = NULL;
3589 struct diff_dir_cb_arg *a = arg;
3590 char *abspath;
3592 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3593 return got_error(GOT_ERR_CANCELLED);
3595 if (got_path_cmp(parent_path, a->status_path,
3596 strlen(parent_path), a->status_path_len) != 0 &&
3597 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3598 return NULL;
3600 if (parent_path[0]) {
3601 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3602 parent_path, de->d_name) == -1)
3603 return got_error_from_errno("asprintf");
3604 } else {
3605 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3606 de->d_name) == -1)
3607 return got_error_from_errno("asprintf");
3610 err = report_file_status(ie, abspath, dirfd, de->d_name,
3611 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3612 free(abspath);
3613 return err;
3616 static const struct got_error *
3617 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3619 struct diff_dir_cb_arg *a = arg;
3620 struct got_object_id blob_id, commit_id;
3621 unsigned char status;
3623 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3624 return got_error(GOT_ERR_CANCELLED);
3626 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3627 return NULL;
3629 got_fileindex_entry_get_blob_id(&blob_id, ie);
3630 got_fileindex_entry_get_commit_id(&commit_id, ie);
3631 if (got_fileindex_entry_has_file_on_disk(ie))
3632 status = GOT_STATUS_MISSING;
3633 else
3634 status = GOT_STATUS_DELETE;
3635 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3636 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3639 static void
3640 free_ignores(struct got_pathlist_head *ignores)
3642 struct got_pathlist_entry *pe;
3644 TAILQ_FOREACH(pe, ignores, entry) {
3645 struct got_pathlist_head *ignorelist = pe->data;
3647 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3649 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3652 static const struct got_error *
3653 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3655 const struct got_error *err = NULL;
3656 struct got_pathlist_entry *pe = NULL;
3657 struct got_pathlist_head *ignorelist;
3658 char *line = NULL, *pattern, *dirpath = NULL;
3659 size_t linesize = 0;
3660 ssize_t linelen;
3662 ignorelist = calloc(1, sizeof(*ignorelist));
3663 if (ignorelist == NULL)
3664 return got_error_from_errno("calloc");
3665 TAILQ_INIT(ignorelist);
3667 while ((linelen = getline(&line, &linesize, f)) != -1) {
3668 if (linelen > 0 && line[linelen - 1] == '\n')
3669 line[linelen - 1] = '\0';
3671 /* Git's ignores may contain comments. */
3672 if (line[0] == '#')
3673 continue;
3675 /* Git's negated patterns are not (yet?) supported. */
3676 if (line[0] == '!')
3677 continue;
3679 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3680 line) == -1) {
3681 err = got_error_from_errno("asprintf");
3682 goto done;
3684 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3685 if (err)
3686 goto done;
3688 if (ferror(f)) {
3689 err = got_error_from_errno("getline");
3690 goto done;
3693 dirpath = strdup(path);
3694 if (dirpath == NULL) {
3695 err = got_error_from_errno("strdup");
3696 goto done;
3698 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3699 done:
3700 free(line);
3701 if (err || pe == NULL) {
3702 free(dirpath);
3703 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3705 return err;
3708 static int
3709 match_path(const char *pattern, size_t pattern_len, const char *path,
3710 int flags)
3712 char buf[PATH_MAX];
3715 * Trailing slashes signify directories.
3716 * Append a * to make such patterns conform to fnmatch rules.
3718 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3719 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3720 return FNM_NOMATCH; /* XXX */
3722 return fnmatch(buf, path, flags);
3725 return fnmatch(pattern, path, flags);
3728 static int
3729 match_ignores(struct got_pathlist_head *ignores, const char *path)
3731 struct got_pathlist_entry *pe;
3733 /* Handle patterns which match in all directories. */
3734 TAILQ_FOREACH(pe, ignores, entry) {
3735 struct got_pathlist_head *ignorelist = pe->data;
3736 struct got_pathlist_entry *pi;
3738 TAILQ_FOREACH(pi, ignorelist, entry) {
3739 const char *p;
3741 if (pi->path_len < 3 ||
3742 strncmp(pi->path, "**/", 3) != 0)
3743 continue;
3744 p = path;
3745 while (*p) {
3746 if (match_path(pi->path + 3,
3747 pi->path_len - 3, p,
3748 FNM_PATHNAME | FNM_LEADING_DIR)) {
3749 /* Retry in next directory. */
3750 while (*p && *p != '/')
3751 p++;
3752 while (*p == '/')
3753 p++;
3754 continue;
3756 return 1;
3762 * The ignores pathlist contains ignore lists from children before
3763 * parents, so we can find the most specific ignorelist by walking
3764 * ignores backwards.
3766 pe = TAILQ_LAST(ignores, got_pathlist_head);
3767 while (pe) {
3768 if (got_path_is_child(path, pe->path, pe->path_len)) {
3769 struct got_pathlist_head *ignorelist = pe->data;
3770 struct got_pathlist_entry *pi;
3771 TAILQ_FOREACH(pi, ignorelist, entry) {
3772 int flags = FNM_LEADING_DIR;
3773 if (strstr(pi->path, "/**/") == NULL)
3774 flags |= FNM_PATHNAME;
3775 if (match_path(pi->path, pi->path_len,
3776 path, flags))
3777 continue;
3778 return 1;
3781 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3784 return 0;
3787 static const struct got_error *
3788 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3789 const char *path, int dirfd, const char *ignores_filename)
3791 const struct got_error *err = NULL;
3792 char *ignorespath;
3793 int fd = -1;
3794 FILE *ignoresfile = NULL;
3796 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3797 path[0] ? "/" : "", ignores_filename) == -1)
3798 return got_error_from_errno("asprintf");
3800 if (dirfd != -1) {
3801 fd = openat(dirfd, ignores_filename,
3802 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3803 if (fd == -1) {
3804 if (errno != ENOENT && errno != EACCES)
3805 err = got_error_from_errno2("openat",
3806 ignorespath);
3807 } else {
3808 ignoresfile = fdopen(fd, "r");
3809 if (ignoresfile == NULL)
3810 err = got_error_from_errno2("fdopen",
3811 ignorespath);
3812 else {
3813 fd = -1;
3814 err = read_ignores(ignores, path, ignoresfile);
3817 } else {
3818 ignoresfile = fopen(ignorespath, "re");
3819 if (ignoresfile == NULL) {
3820 if (errno != ENOENT && errno != EACCES)
3821 err = got_error_from_errno2("fopen",
3822 ignorespath);
3823 } else
3824 err = read_ignores(ignores, path, ignoresfile);
3827 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3828 err = got_error_from_errno2("fclose", path);
3829 if (fd != -1 && close(fd) == -1 && err == NULL)
3830 err = got_error_from_errno2("close", path);
3831 free(ignorespath);
3832 return err;
3835 static const struct got_error *
3836 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3837 int dirfd)
3839 const struct got_error *err = NULL;
3840 struct diff_dir_cb_arg *a = arg;
3841 char *path = NULL;
3843 if (ignore != NULL)
3844 *ignore = 0;
3846 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3847 return got_error(GOT_ERR_CANCELLED);
3849 if (parent_path[0]) {
3850 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3851 return got_error_from_errno("asprintf");
3852 } else {
3853 path = de->d_name;
3856 if (de->d_type == DT_DIR) {
3857 if (!a->no_ignores && ignore != NULL &&
3858 match_ignores(a->ignores, path))
3859 *ignore = 1;
3860 } else if (!match_ignores(a->ignores, path) &&
3861 got_path_is_child(path, a->status_path, a->status_path_len))
3862 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3863 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3864 if (parent_path[0])
3865 free(path);
3866 return err;
3869 static const struct got_error *
3870 status_traverse(void *arg, const char *path, int dirfd)
3872 const struct got_error *err = NULL;
3873 struct diff_dir_cb_arg *a = arg;
3875 if (a->no_ignores)
3876 return NULL;
3878 err = add_ignores(a->ignores, a->worktree->root_path,
3879 path, dirfd, ".cvsignore");
3880 if (err)
3881 return err;
3883 err = add_ignores(a->ignores, a->worktree->root_path, path,
3884 dirfd, ".gitignore");
3886 return err;
3889 static const struct got_error *
3890 report_single_file_status(const char *path, const char *ondisk_path,
3891 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3892 void *status_arg, struct got_repository *repo, int report_unchanged,
3893 struct got_pathlist_head *ignores, int no_ignores)
3895 struct got_fileindex_entry *ie;
3896 struct stat sb;
3898 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3899 if (ie)
3900 return report_file_status(ie, ondisk_path, -1, NULL,
3901 status_cb, status_arg, repo, report_unchanged);
3903 if (lstat(ondisk_path, &sb) == -1) {
3904 if (errno != ENOENT)
3905 return got_error_from_errno2("lstat", ondisk_path);
3906 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3907 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3910 if (!no_ignores && match_ignores(ignores, path))
3911 return NULL;
3913 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3914 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3915 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3917 return NULL;
3920 static const struct got_error *
3921 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3922 const char *root_path, const char *path)
3924 const struct got_error *err;
3925 char *parent_path, *next_parent_path = NULL;
3927 err = add_ignores(ignores, root_path, "", -1,
3928 ".cvsignore");
3929 if (err)
3930 return err;
3932 err = add_ignores(ignores, root_path, "", -1,
3933 ".gitignore");
3934 if (err)
3935 return err;
3937 err = got_path_dirname(&parent_path, path);
3938 if (err) {
3939 if (err->code == GOT_ERR_BAD_PATH)
3940 return NULL; /* cannot traverse parent */
3941 return err;
3943 for (;;) {
3944 err = add_ignores(ignores, root_path, parent_path, -1,
3945 ".cvsignore");
3946 if (err)
3947 break;
3948 err = add_ignores(ignores, root_path, parent_path, -1,
3949 ".gitignore");
3950 if (err)
3951 break;
3952 err = got_path_dirname(&next_parent_path, parent_path);
3953 if (err) {
3954 if (err->code == GOT_ERR_BAD_PATH)
3955 err = NULL; /* traversed everything */
3956 break;
3958 if (got_path_is_root_dir(parent_path))
3959 break;
3960 free(parent_path);
3961 parent_path = next_parent_path;
3962 next_parent_path = NULL;
3965 free(parent_path);
3966 free(next_parent_path);
3967 return err;
3970 struct find_missing_children_args {
3971 const char *parent_path;
3972 size_t parent_len;
3973 struct got_pathlist_head *children;
3974 got_cancel_cb cancel_cb;
3975 void *cancel_arg;
3978 static const struct got_error *
3979 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3981 const struct got_error *err = NULL;
3982 struct find_missing_children_args *a = arg;
3984 if (a->cancel_cb) {
3985 err = a->cancel_cb(a->cancel_arg);
3986 if (err)
3987 return err;
3990 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3991 err = got_pathlist_append(a->children, ie->path, NULL);
3993 return err;
3996 static const struct got_error *
3997 report_children(struct got_pathlist_head *children,
3998 struct got_worktree *worktree, struct got_fileindex *fileindex,
3999 struct got_repository *repo, int is_root_dir, int report_unchanged,
4000 struct got_pathlist_head *ignores, int no_ignores,
4001 got_worktree_status_cb status_cb, void *status_arg,
4002 got_cancel_cb cancel_cb, void *cancel_arg)
4004 const struct got_error *err = NULL;
4005 struct got_pathlist_entry *pe;
4006 char *ondisk_path = NULL;
4008 TAILQ_FOREACH(pe, children, entry) {
4009 if (cancel_cb) {
4010 err = cancel_cb(cancel_arg);
4011 if (err)
4012 break;
4015 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4016 !is_root_dir ? "/" : "", pe->path) == -1) {
4017 err = got_error_from_errno("asprintf");
4018 ondisk_path = NULL;
4019 break;
4022 err = report_single_file_status(pe->path, ondisk_path,
4023 fileindex, status_cb, status_arg, repo, report_unchanged,
4024 ignores, no_ignores);
4025 if (err)
4026 break;
4028 free(ondisk_path);
4029 ondisk_path = NULL;
4032 free(ondisk_path);
4033 return err;
4036 static const struct got_error *
4037 worktree_status(struct got_worktree *worktree, const char *path,
4038 struct got_fileindex *fileindex, struct got_repository *repo,
4039 got_worktree_status_cb status_cb, void *status_arg,
4040 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4041 int report_unchanged)
4043 const struct got_error *err = NULL;
4044 int fd = -1;
4045 struct got_fileindex_diff_dir_cb fdiff_cb;
4046 struct diff_dir_cb_arg arg;
4047 char *ondisk_path = NULL;
4048 struct got_pathlist_head ignores, missing_children;
4049 struct got_fileindex_entry *ie;
4051 TAILQ_INIT(&ignores);
4052 TAILQ_INIT(&missing_children);
4054 if (asprintf(&ondisk_path, "%s%s%s",
4055 worktree->root_path, path[0] ? "/" : "", path) == -1)
4056 return got_error_from_errno("asprintf");
4058 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4059 if (ie) {
4060 err = report_single_file_status(path, ondisk_path,
4061 fileindex, status_cb, status_arg, repo,
4062 report_unchanged, &ignores, no_ignores);
4063 goto done;
4064 } else {
4065 struct find_missing_children_args fmca;
4066 fmca.parent_path = path;
4067 fmca.parent_len = strlen(path);
4068 fmca.children = &missing_children;
4069 fmca.cancel_cb = cancel_cb;
4070 fmca.cancel_arg = cancel_arg;
4071 err = got_fileindex_for_each_entry_safe(fileindex,
4072 find_missing_children, &fmca);
4073 if (err)
4074 goto done;
4077 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4078 if (fd == -1) {
4079 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4080 !got_err_open_nofollow_on_symlink())
4081 err = got_error_from_errno2("open", ondisk_path);
4082 else {
4083 if (!no_ignores) {
4084 err = add_ignores_from_parent_paths(&ignores,
4085 worktree->root_path, ondisk_path);
4086 if (err)
4087 goto done;
4089 if (TAILQ_EMPTY(&missing_children)) {
4090 err = report_single_file_status(path,
4091 ondisk_path, fileindex,
4092 status_cb, status_arg, repo,
4093 report_unchanged, &ignores, no_ignores);
4094 if (err)
4095 goto done;
4096 } else {
4097 err = report_children(&missing_children,
4098 worktree, fileindex, repo,
4099 (path[0] == '\0'), report_unchanged,
4100 &ignores, no_ignores,
4101 status_cb, status_arg,
4102 cancel_cb, cancel_arg);
4103 if (err)
4104 goto done;
4107 } else {
4108 fdiff_cb.diff_old_new = status_old_new;
4109 fdiff_cb.diff_old = status_old;
4110 fdiff_cb.diff_new = status_new;
4111 fdiff_cb.diff_traverse = status_traverse;
4112 arg.fileindex = fileindex;
4113 arg.worktree = worktree;
4114 arg.status_path = path;
4115 arg.status_path_len = strlen(path);
4116 arg.repo = repo;
4117 arg.status_cb = status_cb;
4118 arg.status_arg = status_arg;
4119 arg.cancel_cb = cancel_cb;
4120 arg.cancel_arg = cancel_arg;
4121 arg.report_unchanged = report_unchanged;
4122 arg.no_ignores = no_ignores;
4123 if (!no_ignores) {
4124 err = add_ignores_from_parent_paths(&ignores,
4125 worktree->root_path, path);
4126 if (err)
4127 goto done;
4129 arg.ignores = &ignores;
4130 err = got_fileindex_diff_dir(fileindex, fd,
4131 worktree->root_path, path, repo, &fdiff_cb, &arg);
4133 done:
4134 free_ignores(&ignores);
4135 if (fd != -1 && close(fd) == -1 && err == NULL)
4136 err = got_error_from_errno("close");
4137 free(ondisk_path);
4138 return err;
4141 const struct got_error *
4142 got_worktree_status(struct got_worktree *worktree,
4143 struct got_pathlist_head *paths, struct got_repository *repo,
4144 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4145 got_cancel_cb cancel_cb, void *cancel_arg)
4147 const struct got_error *err = NULL;
4148 char *fileindex_path = NULL;
4149 struct got_fileindex *fileindex = NULL;
4150 struct got_pathlist_entry *pe;
4152 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4153 if (err)
4154 return err;
4156 TAILQ_FOREACH(pe, paths, entry) {
4157 err = worktree_status(worktree, pe->path, fileindex, repo,
4158 status_cb, status_arg, cancel_cb, cancel_arg,
4159 no_ignores, 0);
4160 if (err)
4161 break;
4163 free(fileindex_path);
4164 got_fileindex_free(fileindex);
4165 return err;
4168 const struct got_error *
4169 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4170 const char *arg)
4172 const struct got_error *err = NULL;
4173 char *resolved = NULL, *cwd = NULL, *path = NULL;
4174 size_t len;
4175 struct stat sb;
4176 char *abspath = NULL;
4177 char canonpath[PATH_MAX];
4179 *wt_path = NULL;
4181 cwd = getcwd(NULL, 0);
4182 if (cwd == NULL)
4183 return got_error_from_errno("getcwd");
4185 if (lstat(arg, &sb) == -1) {
4186 if (errno != ENOENT) {
4187 err = got_error_from_errno2("lstat", arg);
4188 goto done;
4190 sb.st_mode = 0;
4192 if (S_ISLNK(sb.st_mode)) {
4194 * We cannot use realpath(3) with symlinks since we want to
4195 * operate on the symlink itself.
4196 * But we can make the path absolute, assuming it is relative
4197 * to the current working directory, and then canonicalize it.
4199 if (!got_path_is_absolute(arg)) {
4200 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4201 err = got_error_from_errno("asprintf");
4202 goto done;
4206 err = got_canonpath(abspath ? abspath : arg, canonpath,
4207 sizeof(canonpath));
4208 if (err)
4209 goto done;
4210 resolved = strdup(canonpath);
4211 if (resolved == NULL) {
4212 err = got_error_from_errno("strdup");
4213 goto done;
4215 } else {
4216 resolved = realpath(arg, NULL);
4217 if (resolved == NULL) {
4218 if (errno != ENOENT) {
4219 err = got_error_from_errno2("realpath", arg);
4220 goto done;
4222 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4223 err = got_error_from_errno("asprintf");
4224 goto done;
4226 err = got_canonpath(abspath, canonpath,
4227 sizeof(canonpath));
4228 if (err)
4229 goto done;
4230 resolved = strdup(canonpath);
4231 if (resolved == NULL) {
4232 err = got_error_from_errno("strdup");
4233 goto done;
4238 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4239 strlen(got_worktree_get_root_path(worktree)))) {
4240 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4241 goto done;
4244 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4245 err = got_path_skip_common_ancestor(&path,
4246 got_worktree_get_root_path(worktree), resolved);
4247 if (err)
4248 goto done;
4249 } else {
4250 path = strdup("");
4251 if (path == NULL) {
4252 err = got_error_from_errno("strdup");
4253 goto done;
4257 /* XXX status walk can't deal with trailing slash! */
4258 len = strlen(path);
4259 while (len > 0 && path[len - 1] == '/') {
4260 path[len - 1] = '\0';
4261 len--;
4263 done:
4264 free(abspath);
4265 free(resolved);
4266 free(cwd);
4267 if (err == NULL)
4268 *wt_path = path;
4269 else
4270 free(path);
4271 return err;
4274 struct schedule_addition_args {
4275 struct got_worktree *worktree;
4276 struct got_fileindex *fileindex;
4277 got_worktree_checkout_cb progress_cb;
4278 void *progress_arg;
4279 struct got_repository *repo;
4282 static int
4283 add_noop_status(unsigned char status)
4285 return (status == GOT_STATUS_ADD ||
4286 status == GOT_STATUS_MODIFY ||
4287 status == GOT_STATUS_CONFLICT ||
4288 status == GOT_STATUS_MODE_CHANGE ||
4289 status == GOT_STATUS_NO_CHANGE);
4292 static const struct got_error *
4293 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4294 const char *relpath, struct got_object_id *blob_id,
4295 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4296 int dirfd, const char *de_name)
4298 struct schedule_addition_args *a = arg;
4299 const struct got_error *err = NULL;
4300 struct got_fileindex_entry *ie;
4301 struct stat sb;
4302 char *ondisk_path;
4304 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4305 relpath) == -1)
4306 return got_error_from_errno("asprintf");
4308 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4309 if (ie) {
4310 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4311 de_name, a->repo);
4312 if (err)
4313 goto done;
4314 /* Re-adding an existing entry is a no-op. */
4315 if (staged_status == GOT_STATUS_NO_CHANGE &&
4316 add_noop_status(status))
4317 goto done;
4318 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4319 if (err)
4320 goto done;
4323 if (status != GOT_STATUS_UNVERSIONED) {
4324 if (status == GOT_STATUS_NONEXISTENT)
4325 err = got_error_set_errno(ENOENT, ondisk_path);
4326 else
4327 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4328 goto done;
4331 err = got_fileindex_entry_alloc(&ie, relpath);
4332 if (err)
4333 goto done;
4334 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4335 relpath, NULL, NULL, 1);
4336 if (err) {
4337 got_fileindex_entry_free(ie);
4338 goto done;
4340 err = got_fileindex_entry_add(a->fileindex, ie);
4341 if (err) {
4342 got_fileindex_entry_free(ie);
4343 goto done;
4345 done:
4346 free(ondisk_path);
4347 if (err)
4348 return err;
4349 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4350 return NULL;
4351 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4354 const struct got_error *
4355 got_worktree_schedule_add(struct got_worktree *worktree,
4356 struct got_pathlist_head *paths,
4357 got_worktree_checkout_cb progress_cb, void *progress_arg,
4358 struct got_repository *repo, int no_ignores)
4360 struct got_fileindex *fileindex = NULL;
4361 char *fileindex_path = NULL;
4362 const struct got_error *err = NULL, *sync_err, *unlockerr;
4363 struct got_pathlist_entry *pe;
4364 struct schedule_addition_args saa;
4366 err = lock_worktree(worktree, LOCK_EX);
4367 if (err)
4368 return err;
4370 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4371 if (err)
4372 goto done;
4374 saa.worktree = worktree;
4375 saa.fileindex = fileindex;
4376 saa.progress_cb = progress_cb;
4377 saa.progress_arg = progress_arg;
4378 saa.repo = repo;
4380 TAILQ_FOREACH(pe, paths, entry) {
4381 err = worktree_status(worktree, pe->path, fileindex, repo,
4382 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4383 if (err)
4384 break;
4386 sync_err = sync_fileindex(fileindex, fileindex_path);
4387 if (sync_err && err == NULL)
4388 err = sync_err;
4389 done:
4390 free(fileindex_path);
4391 if (fileindex)
4392 got_fileindex_free(fileindex);
4393 unlockerr = lock_worktree(worktree, LOCK_SH);
4394 if (unlockerr && err == NULL)
4395 err = unlockerr;
4396 return err;
4399 struct schedule_deletion_args {
4400 struct got_worktree *worktree;
4401 struct got_fileindex *fileindex;
4402 got_worktree_delete_cb progress_cb;
4403 void *progress_arg;
4404 struct got_repository *repo;
4405 int delete_local_mods;
4406 int keep_on_disk;
4407 int ignore_missing_paths;
4408 const char *status_path;
4409 size_t status_path_len;
4410 const char *status_codes;
4413 static const struct got_error *
4414 schedule_for_deletion(void *arg, unsigned char status,
4415 unsigned char staged_status, const char *relpath,
4416 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4417 struct got_object_id *commit_id, int dirfd, const char *de_name)
4419 struct schedule_deletion_args *a = arg;
4420 const struct got_error *err = NULL;
4421 struct got_fileindex_entry *ie = NULL;
4422 struct stat sb;
4423 char *ondisk_path;
4425 if (status == GOT_STATUS_NONEXISTENT) {
4426 if (a->ignore_missing_paths)
4427 return NULL;
4428 return got_error_set_errno(ENOENT, relpath);
4431 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4432 if (ie == NULL)
4433 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4435 staged_status = get_staged_status(ie);
4436 if (staged_status != GOT_STATUS_NO_CHANGE) {
4437 if (staged_status == GOT_STATUS_DELETE)
4438 return NULL;
4439 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4442 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4443 relpath) == -1)
4444 return got_error_from_errno("asprintf");
4446 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4447 a->repo);
4448 if (err)
4449 goto done;
4451 if (a->status_codes) {
4452 size_t ncodes = strlen(a->status_codes);
4453 int i;
4454 for (i = 0; i < ncodes ; i++) {
4455 if (status == a->status_codes[i])
4456 break;
4458 if (i == ncodes) {
4459 /* Do not delete files in non-matching status. */
4460 free(ondisk_path);
4461 return NULL;
4463 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4464 a->status_codes[i] != GOT_STATUS_MISSING) {
4465 static char msg[64];
4466 snprintf(msg, sizeof(msg),
4467 "invalid status code '%c'", a->status_codes[i]);
4468 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4469 goto done;
4473 if (status != GOT_STATUS_NO_CHANGE) {
4474 if (status == GOT_STATUS_DELETE)
4475 goto done;
4476 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4477 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4478 goto done;
4480 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4481 err = got_error_set_errno(ENOENT, relpath);
4482 goto done;
4484 if (status != GOT_STATUS_MODIFY &&
4485 status != GOT_STATUS_MISSING) {
4486 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4487 goto done;
4491 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4492 size_t root_len;
4494 if (dirfd != -1) {
4495 if (unlinkat(dirfd, de_name, 0) == -1) {
4496 err = got_error_from_errno2("unlinkat",
4497 ondisk_path);
4498 goto done;
4500 } else if (unlink(ondisk_path) == -1) {
4501 err = got_error_from_errno2("unlink", ondisk_path);
4502 goto done;
4505 root_len = strlen(a->worktree->root_path);
4506 do {
4507 char *parent;
4509 err = got_path_dirname(&parent, ondisk_path);
4510 if (err)
4511 goto done;
4512 free(ondisk_path);
4513 ondisk_path = parent;
4514 if (got_path_cmp(ondisk_path, a->status_path,
4515 strlen(ondisk_path), a->status_path_len) != 0 &&
4516 !got_path_is_child(ondisk_path, a->status_path,
4517 a->status_path_len))
4518 break;
4519 if (rmdir(ondisk_path) == -1) {
4520 if (errno != ENOTEMPTY)
4521 err = got_error_from_errno2("rmdir",
4522 ondisk_path);
4523 break;
4525 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4526 strlen(ondisk_path), root_len) != 0);
4529 got_fileindex_entry_mark_deleted_from_disk(ie);
4530 done:
4531 free(ondisk_path);
4532 if (err)
4533 return err;
4534 if (status == GOT_STATUS_DELETE)
4535 return NULL;
4536 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4537 staged_status, relpath);
4540 const struct got_error *
4541 got_worktree_schedule_delete(struct got_worktree *worktree,
4542 struct got_pathlist_head *paths, int delete_local_mods,
4543 const char *status_codes,
4544 got_worktree_delete_cb progress_cb, void *progress_arg,
4545 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4547 struct got_fileindex *fileindex = NULL;
4548 char *fileindex_path = NULL;
4549 const struct got_error *err = NULL, *sync_err, *unlockerr;
4550 struct got_pathlist_entry *pe;
4551 struct schedule_deletion_args sda;
4553 err = lock_worktree(worktree, LOCK_EX);
4554 if (err)
4555 return err;
4557 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4558 if (err)
4559 goto done;
4561 sda.worktree = worktree;
4562 sda.fileindex = fileindex;
4563 sda.progress_cb = progress_cb;
4564 sda.progress_arg = progress_arg;
4565 sda.repo = repo;
4566 sda.delete_local_mods = delete_local_mods;
4567 sda.keep_on_disk = keep_on_disk;
4568 sda.ignore_missing_paths = ignore_missing_paths;
4569 sda.status_codes = status_codes;
4571 TAILQ_FOREACH(pe, paths, entry) {
4572 char *ondisk_status_path;
4574 if (asprintf(&ondisk_status_path, "%s%s%s",
4575 got_worktree_get_root_path(worktree),
4576 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4577 err = got_error_from_errno("asprintf");
4578 goto done;
4580 sda.status_path = ondisk_status_path;
4581 sda.status_path_len = strlen(ondisk_status_path);
4582 err = worktree_status(worktree, pe->path, fileindex, repo,
4583 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4584 free(ondisk_status_path);
4585 if (err)
4586 break;
4588 sync_err = sync_fileindex(fileindex, fileindex_path);
4589 if (sync_err && err == NULL)
4590 err = sync_err;
4591 done:
4592 free(fileindex_path);
4593 if (fileindex)
4594 got_fileindex_free(fileindex);
4595 unlockerr = lock_worktree(worktree, LOCK_SH);
4596 if (unlockerr && err == NULL)
4597 err = unlockerr;
4598 return err;
4601 static const struct got_error *
4602 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4604 const struct got_error *err = NULL;
4605 char *line = NULL;
4606 size_t linesize = 0, n;
4607 ssize_t linelen;
4609 linelen = getline(&line, &linesize, infile);
4610 if (linelen == -1) {
4611 if (ferror(infile)) {
4612 err = got_error_from_errno("getline");
4613 goto done;
4615 return NULL;
4617 if (outfile) {
4618 n = fwrite(line, 1, linelen, outfile);
4619 if (n != linelen) {
4620 err = got_ferror(outfile, GOT_ERR_IO);
4621 goto done;
4624 if (rejectfile) {
4625 n = fwrite(line, 1, linelen, rejectfile);
4626 if (n != linelen)
4627 err = got_ferror(rejectfile, GOT_ERR_IO);
4629 done:
4630 free(line);
4631 return err;
4634 static const struct got_error *
4635 skip_one_line(FILE *f)
4637 char *line = NULL;
4638 size_t linesize = 0;
4639 ssize_t linelen;
4641 linelen = getline(&line, &linesize, f);
4642 if (linelen == -1) {
4643 if (ferror(f))
4644 return got_error_from_errno("getline");
4645 return NULL;
4647 free(line);
4648 return NULL;
4651 static const struct got_error *
4652 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4653 int start_old, int end_old, int start_new, int end_new,
4654 FILE *outfile, FILE *rejectfile)
4656 const struct got_error *err;
4658 /* Copy old file's lines leading up to patch. */
4659 while (!feof(f1) && *line_cur1 < start_old) {
4660 err = copy_one_line(f1, outfile, NULL);
4661 if (err)
4662 return err;
4663 (*line_cur1)++;
4665 /* Skip new file's lines leading up to patch. */
4666 while (!feof(f2) && *line_cur2 < start_new) {
4667 if (rejectfile)
4668 err = copy_one_line(f2, NULL, rejectfile);
4669 else
4670 err = skip_one_line(f2);
4671 if (err)
4672 return err;
4673 (*line_cur2)++;
4675 /* Copy patched lines. */
4676 while (!feof(f2) && *line_cur2 <= end_new) {
4677 err = copy_one_line(f2, outfile, NULL);
4678 if (err)
4679 return err;
4680 (*line_cur2)++;
4682 /* Skip over old file's replaced lines. */
4683 while (!feof(f1) && *line_cur1 <= end_old) {
4684 if (rejectfile)
4685 err = copy_one_line(f1, NULL, rejectfile);
4686 else
4687 err = skip_one_line(f1);
4688 if (err)
4689 return err;
4690 (*line_cur1)++;
4693 return NULL;
4696 static const struct got_error *
4697 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4698 FILE *outfile, FILE *rejectfile)
4700 const struct got_error *err;
4702 if (outfile) {
4703 /* Copy old file's lines until EOF. */
4704 while (!feof(f1)) {
4705 err = copy_one_line(f1, outfile, NULL);
4706 if (err)
4707 return err;
4708 (*line_cur1)++;
4711 if (rejectfile) {
4712 /* Copy new file's lines until EOF. */
4713 while (!feof(f2)) {
4714 err = copy_one_line(f2, NULL, rejectfile);
4715 if (err)
4716 return err;
4717 (*line_cur2)++;
4721 return NULL;
4724 static const struct got_error *
4725 apply_or_reject_change(int *choice, int *nchunks_used,
4726 struct diff_result *diff_result, int n,
4727 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4728 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4729 got_worktree_patch_cb patch_cb, void *patch_arg)
4731 const struct got_error *err = NULL;
4732 struct diff_chunk_context cc = {};
4733 int start_old, end_old, start_new, end_new;
4734 FILE *hunkfile;
4735 struct diff_output_unidiff_state *diff_state;
4736 struct diff_input_info diff_info;
4737 int rc;
4739 *choice = GOT_PATCH_CHOICE_NONE;
4741 /* Get changed line numbers without context lines for copy_change(). */
4742 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4743 start_old = cc.left.start;
4744 end_old = cc.left.end;
4745 start_new = cc.right.start;
4746 end_new = cc.right.end;
4748 /* Get the same change with context lines for display. */
4749 memset(&cc, 0, sizeof(cc));
4750 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4752 memset(&diff_info, 0, sizeof(diff_info));
4753 diff_info.left_path = relpath;
4754 diff_info.right_path = relpath;
4756 diff_state = diff_output_unidiff_state_alloc();
4757 if (diff_state == NULL)
4758 return got_error_set_errno(ENOMEM,
4759 "diff_output_unidiff_state_alloc");
4761 hunkfile = got_opentemp();
4762 if (hunkfile == NULL) {
4763 err = got_error_from_errno("got_opentemp");
4764 goto done;
4767 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4768 diff_result, &cc);
4769 if (rc != DIFF_RC_OK) {
4770 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4771 goto done;
4774 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4775 err = got_ferror(hunkfile, GOT_ERR_IO);
4776 goto done;
4779 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4780 hunkfile, changeno, nchanges);
4781 if (err)
4782 goto done;
4784 switch (*choice) {
4785 case GOT_PATCH_CHOICE_YES:
4786 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4787 end_old, start_new, end_new, outfile, rejectfile);
4788 break;
4789 case GOT_PATCH_CHOICE_NO:
4790 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4791 end_old, start_new, end_new, rejectfile, outfile);
4792 break;
4793 case GOT_PATCH_CHOICE_QUIT:
4794 break;
4795 default:
4796 err = got_error(GOT_ERR_PATCH_CHOICE);
4797 break;
4799 done:
4800 diff_output_unidiff_state_free(diff_state);
4801 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4802 err = got_error_from_errno("fclose");
4803 return err;
4806 struct revert_file_args {
4807 struct got_worktree *worktree;
4808 struct got_fileindex *fileindex;
4809 got_worktree_checkout_cb progress_cb;
4810 void *progress_arg;
4811 got_worktree_patch_cb patch_cb;
4812 void *patch_arg;
4813 struct got_repository *repo;
4814 int unlink_added_files;
4815 struct got_pathlist_head *added_files_to_unlink;
4818 static const struct got_error *
4819 create_patched_content(char **path_outfile, int reverse_patch,
4820 struct got_object_id *blob_id, const char *path2,
4821 int dirfd2, const char *de_name2,
4822 const char *relpath, struct got_repository *repo,
4823 got_worktree_patch_cb patch_cb, void *patch_arg)
4825 const struct got_error *err, *free_err;
4826 struct got_blob_object *blob = NULL;
4827 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4828 int fd = -1, fd2 = -1;
4829 char link_target[PATH_MAX];
4830 ssize_t link_len = 0;
4831 char *path1 = NULL, *id_str = NULL;
4832 struct stat sb2;
4833 struct got_diffreg_result *diffreg_result = NULL;
4834 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4835 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4837 *path_outfile = NULL;
4839 err = got_object_id_str(&id_str, blob_id);
4840 if (err)
4841 return err;
4843 if (dirfd2 != -1) {
4844 fd2 = openat(dirfd2, de_name2,
4845 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4846 if (fd2 == -1) {
4847 if (!got_err_open_nofollow_on_symlink()) {
4848 err = got_error_from_errno2("openat", path2);
4849 goto done;
4851 link_len = readlinkat(dirfd2, de_name2,
4852 link_target, sizeof(link_target));
4853 if (link_len == -1) {
4854 return got_error_from_errno2("readlinkat",
4855 path2);
4857 sb2.st_mode = S_IFLNK;
4858 sb2.st_size = link_len;
4860 } else {
4861 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4862 if (fd2 == -1) {
4863 if (!got_err_open_nofollow_on_symlink()) {
4864 err = got_error_from_errno2("open", path2);
4865 goto done;
4867 link_len = readlink(path2, link_target,
4868 sizeof(link_target));
4869 if (link_len == -1)
4870 return got_error_from_errno2("readlink", path2);
4871 sb2.st_mode = S_IFLNK;
4872 sb2.st_size = link_len;
4875 if (fd2 != -1) {
4876 if (fstat(fd2, &sb2) == -1) {
4877 err = got_error_from_errno2("fstat", path2);
4878 goto done;
4881 f2 = fdopen(fd2, "r");
4882 if (f2 == NULL) {
4883 err = got_error_from_errno2("fdopen", path2);
4884 goto done;
4886 fd2 = -1;
4887 } else {
4888 size_t n;
4889 f2 = got_opentemp();
4890 if (f2 == NULL) {
4891 err = got_error_from_errno2("got_opentemp", path2);
4892 goto done;
4894 n = fwrite(link_target, 1, link_len, f2);
4895 if (n != link_len) {
4896 err = got_ferror(f2, GOT_ERR_IO);
4897 goto done;
4899 if (fflush(f2) == EOF) {
4900 err = got_error_from_errno("fflush");
4901 goto done;
4903 rewind(f2);
4906 fd = got_opentempfd();
4907 if (fd == -1) {
4908 err = got_error_from_errno("got_opentempfd");
4909 goto done;
4912 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4913 if (err)
4914 goto done;
4916 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4917 if (err)
4918 goto done;
4920 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4921 if (err)
4922 goto done;
4924 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4925 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4926 if (err)
4927 goto done;
4929 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4930 "");
4931 if (err)
4932 goto done;
4934 if (fseek(f1, 0L, SEEK_SET) == -1)
4935 return got_ferror(f1, GOT_ERR_IO);
4936 if (fseek(f2, 0L, SEEK_SET) == -1)
4937 return got_ferror(f2, GOT_ERR_IO);
4939 /* Count the number of actual changes in the diff result. */
4940 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4941 struct diff_chunk_context cc = {};
4942 diff_chunk_context_load_change(&cc, &nchunks_used,
4943 diffreg_result->result, n, 0);
4944 nchanges++;
4946 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4947 int choice;
4948 err = apply_or_reject_change(&choice, &nchunks_used,
4949 diffreg_result->result, n, relpath, f1, f2,
4950 &line_cur1, &line_cur2,
4951 reverse_patch ? NULL : outfile,
4952 reverse_patch ? outfile : NULL,
4953 ++i, nchanges, patch_cb, patch_arg);
4954 if (err)
4955 goto done;
4956 if (choice == GOT_PATCH_CHOICE_YES)
4957 have_content = 1;
4958 else if (choice == GOT_PATCH_CHOICE_QUIT)
4959 break;
4961 if (have_content) {
4962 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4963 reverse_patch ? NULL : outfile,
4964 reverse_patch ? outfile : NULL);
4965 if (err)
4966 goto done;
4968 if (!S_ISLNK(sb2.st_mode)) {
4969 mode_t mode;
4971 mode = apply_umask(sb2.st_mode);
4972 if (fchmod(fileno(outfile), mode) == -1) {
4973 err = got_error_from_errno2("fchmod", path2);
4974 goto done;
4978 done:
4979 free(id_str);
4980 if (fd != -1 && close(fd) == -1 && err == NULL)
4981 err = got_error_from_errno("close");
4982 if (blob)
4983 got_object_blob_close(blob);
4984 free_err = got_diffreg_result_free(diffreg_result);
4985 if (err == NULL)
4986 err = free_err;
4987 if (f1 && fclose(f1) == EOF && err == NULL)
4988 err = got_error_from_errno2("fclose", path1);
4989 if (f2 && fclose(f2) == EOF && err == NULL)
4990 err = got_error_from_errno2("fclose", path2);
4991 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4992 err = got_error_from_errno2("close", path2);
4993 if (outfile && fclose(outfile) == EOF && err == NULL)
4994 err = got_error_from_errno2("fclose", *path_outfile);
4995 if (path1 && unlink(path1) == -1 && err == NULL)
4996 err = got_error_from_errno2("unlink", path1);
4997 if (err || !have_content) {
4998 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4999 err = got_error_from_errno2("unlink", *path_outfile);
5000 free(*path_outfile);
5001 *path_outfile = NULL;
5003 free(path1);
5004 return err;
5007 static const struct got_error *
5008 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5009 const char *relpath, struct got_object_id *blob_id,
5010 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5011 int dirfd, const char *de_name)
5013 struct revert_file_args *a = arg;
5014 const struct got_error *err = NULL;
5015 char *parent_path = NULL;
5016 struct got_fileindex_entry *ie;
5017 struct got_commit_object *base_commit = NULL;
5018 struct got_tree_object *tree = NULL;
5019 struct got_object_id *tree_id = NULL;
5020 const struct got_tree_entry *te = NULL;
5021 char *tree_path = NULL, *te_name;
5022 char *ondisk_path = NULL, *path_content = NULL;
5023 struct got_blob_object *blob = NULL;
5024 int fd = -1;
5026 /* Reverting a staged deletion is a no-op. */
5027 if (status == GOT_STATUS_DELETE &&
5028 staged_status != GOT_STATUS_NO_CHANGE)
5029 return NULL;
5031 if (status == GOT_STATUS_UNVERSIONED)
5032 return (*a->progress_cb)(a->progress_arg,
5033 GOT_STATUS_UNVERSIONED, relpath);
5035 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5036 if (ie == NULL)
5037 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5039 /* Construct in-repository path of tree which contains this blob. */
5040 err = got_path_dirname(&parent_path, ie->path);
5041 if (err) {
5042 if (err->code != GOT_ERR_BAD_PATH)
5043 goto done;
5044 parent_path = strdup("/");
5045 if (parent_path == NULL) {
5046 err = got_error_from_errno("strdup");
5047 goto done;
5050 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5051 tree_path = strdup(parent_path);
5052 if (tree_path == NULL) {
5053 err = got_error_from_errno("strdup");
5054 goto done;
5056 } else {
5057 if (got_path_is_root_dir(parent_path)) {
5058 tree_path = strdup(a->worktree->path_prefix);
5059 if (tree_path == NULL) {
5060 err = got_error_from_errno("strdup");
5061 goto done;
5063 } else {
5064 if (asprintf(&tree_path, "%s/%s",
5065 a->worktree->path_prefix, parent_path) == -1) {
5066 err = got_error_from_errno("asprintf");
5067 goto done;
5072 err = got_object_open_as_commit(&base_commit, a->repo,
5073 a->worktree->base_commit_id);
5074 if (err)
5075 goto done;
5077 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5078 if (err) {
5079 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5080 (status == GOT_STATUS_ADD ||
5081 staged_status == GOT_STATUS_ADD)))
5082 goto done;
5083 } else {
5084 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5085 if (err)
5086 goto done;
5088 err = got_path_basename(&te_name, ie->path);
5089 if (err)
5090 goto done;
5092 te = got_object_tree_find_entry(tree, te_name);
5093 free(te_name);
5094 if (te == NULL && status != GOT_STATUS_ADD &&
5095 staged_status != GOT_STATUS_ADD) {
5096 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5097 goto done;
5101 switch (status) {
5102 case GOT_STATUS_ADD:
5103 if (a->patch_cb) {
5104 int choice = GOT_PATCH_CHOICE_NONE;
5105 err = (*a->patch_cb)(&choice, a->patch_arg,
5106 status, ie->path, NULL, 1, 1);
5107 if (err)
5108 goto done;
5109 if (choice != GOT_PATCH_CHOICE_YES)
5110 break;
5112 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5113 ie->path);
5114 if (err)
5115 goto done;
5116 got_fileindex_entry_remove(a->fileindex, ie);
5117 if (a->unlink_added_files) {
5118 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5120 if (a->added_files_to_unlink) {
5121 struct got_pathlist_entry *pe;
5123 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5124 entry) {
5125 if (got_path_cmp(pe->path, relpath,
5126 pe->path_len, strlen(relpath)))
5127 continue;
5128 do_unlink = 1;
5129 break;
5133 if (do_unlink) {
5134 if (asprintf(&ondisk_path, "%s/%s",
5135 got_worktree_get_root_path(a->worktree),
5136 relpath) == -1) {
5137 err = got_error_from_errno("asprintf");
5138 goto done;
5140 if (unlink(ondisk_path) == -1) {
5141 err = got_error_from_errno2("unlink",
5142 ondisk_path);
5143 break;
5147 break;
5148 case GOT_STATUS_DELETE:
5149 if (a->patch_cb) {
5150 int choice = GOT_PATCH_CHOICE_NONE;
5151 err = (*a->patch_cb)(&choice, a->patch_arg,
5152 status, ie->path, NULL, 1, 1);
5153 if (err)
5154 goto done;
5155 if (choice != GOT_PATCH_CHOICE_YES)
5156 break;
5158 /* fall through */
5159 case GOT_STATUS_MODIFY:
5160 case GOT_STATUS_MODE_CHANGE:
5161 case GOT_STATUS_CONFLICT:
5162 case GOT_STATUS_MISSING: {
5163 struct got_object_id id;
5164 if (staged_status == GOT_STATUS_ADD ||
5165 staged_status == GOT_STATUS_MODIFY)
5166 got_fileindex_entry_get_staged_blob_id(&id, ie);
5167 else
5168 got_fileindex_entry_get_blob_id(&id, ie);
5169 fd = got_opentempfd();
5170 if (fd == -1) {
5171 err = got_error_from_errno("got_opentempfd");
5172 goto done;
5175 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5176 if (err)
5177 goto done;
5179 if (asprintf(&ondisk_path, "%s/%s",
5180 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5181 err = got_error_from_errno("asprintf");
5182 goto done;
5185 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5186 status == GOT_STATUS_CONFLICT)) {
5187 int is_bad_symlink = 0;
5188 err = create_patched_content(&path_content, 1, &id,
5189 ondisk_path, dirfd, de_name, ie->path, a->repo,
5190 a->patch_cb, a->patch_arg);
5191 if (err || path_content == NULL)
5192 break;
5193 if (te && S_ISLNK(te->mode)) {
5194 if (unlink(path_content) == -1) {
5195 err = got_error_from_errno2("unlink",
5196 path_content);
5197 break;
5199 err = install_symlink(&is_bad_symlink,
5200 a->worktree, ondisk_path, ie->path,
5201 blob, 0, 1, 0, 0, a->repo,
5202 a->progress_cb, a->progress_arg);
5203 } else {
5204 if (rename(path_content, ondisk_path) == -1) {
5205 err = got_error_from_errno3("rename",
5206 path_content, ondisk_path);
5207 goto done;
5210 } else {
5211 int is_bad_symlink = 0;
5212 if (te && S_ISLNK(te->mode)) {
5213 err = install_symlink(&is_bad_symlink,
5214 a->worktree, ondisk_path, ie->path,
5215 blob, 0, 1, 0, 0, a->repo,
5216 a->progress_cb, a->progress_arg);
5217 } else {
5218 err = install_blob(a->worktree, ondisk_path,
5219 ie->path,
5220 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5221 got_fileindex_perms_to_st(ie), blob,
5222 0, 1, 0, 0, a->repo,
5223 a->progress_cb, a->progress_arg);
5225 if (err)
5226 goto done;
5227 if (status == GOT_STATUS_DELETE ||
5228 status == GOT_STATUS_MODE_CHANGE) {
5229 err = got_fileindex_entry_update(ie,
5230 a->worktree->root_fd, relpath,
5231 blob->id.sha1,
5232 a->worktree->base_commit_id->sha1, 1);
5233 if (err)
5234 goto done;
5236 if (is_bad_symlink) {
5237 got_fileindex_entry_filetype_set(ie,
5238 GOT_FILEIDX_MODE_BAD_SYMLINK);
5241 break;
5243 default:
5244 break;
5246 done:
5247 free(ondisk_path);
5248 free(path_content);
5249 free(parent_path);
5250 free(tree_path);
5251 if (fd != -1 && close(fd) == -1 && err == NULL)
5252 err = got_error_from_errno("close");
5253 if (blob)
5254 got_object_blob_close(blob);
5255 if (tree)
5256 got_object_tree_close(tree);
5257 free(tree_id);
5258 if (base_commit)
5259 got_object_commit_close(base_commit);
5260 return err;
5263 const struct got_error *
5264 got_worktree_revert(struct got_worktree *worktree,
5265 struct got_pathlist_head *paths,
5266 got_worktree_checkout_cb progress_cb, void *progress_arg,
5267 got_worktree_patch_cb patch_cb, void *patch_arg,
5268 struct got_repository *repo)
5270 struct got_fileindex *fileindex = NULL;
5271 char *fileindex_path = NULL;
5272 const struct got_error *err = NULL, *unlockerr = NULL;
5273 const struct got_error *sync_err = NULL;
5274 struct got_pathlist_entry *pe;
5275 struct revert_file_args rfa;
5277 err = lock_worktree(worktree, LOCK_EX);
5278 if (err)
5279 return err;
5281 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5282 if (err)
5283 goto done;
5285 rfa.worktree = worktree;
5286 rfa.fileindex = fileindex;
5287 rfa.progress_cb = progress_cb;
5288 rfa.progress_arg = progress_arg;
5289 rfa.patch_cb = patch_cb;
5290 rfa.patch_arg = patch_arg;
5291 rfa.repo = repo;
5292 rfa.unlink_added_files = 0;
5293 TAILQ_FOREACH(pe, paths, entry) {
5294 err = worktree_status(worktree, pe->path, fileindex, repo,
5295 revert_file, &rfa, NULL, NULL, 1, 0);
5296 if (err)
5297 break;
5299 sync_err = sync_fileindex(fileindex, fileindex_path);
5300 if (sync_err && err == NULL)
5301 err = sync_err;
5302 done:
5303 free(fileindex_path);
5304 if (fileindex)
5305 got_fileindex_free(fileindex);
5306 unlockerr = lock_worktree(worktree, LOCK_SH);
5307 if (unlockerr && err == NULL)
5308 err = unlockerr;
5309 return err;
5312 static void
5313 free_commitable(struct got_commitable *ct)
5315 free(ct->path);
5316 free(ct->in_repo_path);
5317 free(ct->ondisk_path);
5318 free(ct->blob_id);
5319 free(ct->base_blob_id);
5320 free(ct->staged_blob_id);
5321 free(ct->base_commit_id);
5322 free(ct);
5325 struct collect_commitables_arg {
5326 struct got_pathlist_head *commitable_paths;
5327 struct got_repository *repo;
5328 struct got_worktree *worktree;
5329 struct got_fileindex *fileindex;
5330 int have_staged_files;
5331 int allow_bad_symlinks;
5332 int diff_header_shown;
5333 int commit_conflicts;
5334 FILE *diff_outfile;
5335 FILE *f1;
5336 FILE *f2;
5340 * Create a file which contains the target path of a symlink so we can feed
5341 * it as content to the diff engine.
5343 static const struct got_error *
5344 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5345 const char *abspath)
5347 const struct got_error *err = NULL;
5348 char target_path[PATH_MAX];
5349 ssize_t target_len, outlen;
5351 *fd = -1;
5353 if (dirfd != -1) {
5354 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5355 if (target_len == -1)
5356 return got_error_from_errno2("readlinkat", abspath);
5357 } else {
5358 target_len = readlink(abspath, target_path, PATH_MAX);
5359 if (target_len == -1)
5360 return got_error_from_errno2("readlink", abspath);
5363 *fd = got_opentempfd();
5364 if (*fd == -1)
5365 return got_error_from_errno("got_opentempfd");
5367 outlen = write(*fd, target_path, target_len);
5368 if (outlen == -1) {
5369 err = got_error_from_errno("got_opentempfd");
5370 goto done;
5373 if (lseek(*fd, 0, SEEK_SET) == -1) {
5374 err = got_error_from_errno2("lseek", abspath);
5375 goto done;
5377 done:
5378 if (err) {
5379 close(*fd);
5380 *fd = -1;
5382 return err;
5385 static const struct got_error *
5386 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5387 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5388 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5390 const struct got_error *err = NULL;
5391 struct got_blob_object *blob1 = NULL;
5392 int fd = -1, fd1 = -1, fd2 = -1;
5393 FILE *ondisk_file = NULL;
5394 char *label1 = NULL;
5395 struct stat sb;
5396 off_t size1 = 0;
5397 int f2_exists = 0;
5398 char *id_str = NULL;
5400 memset(&sb, 0, sizeof(sb));
5402 if (diff_staged) {
5403 if (ct->staged_status != GOT_STATUS_MODIFY &&
5404 ct->staged_status != GOT_STATUS_ADD &&
5405 ct->staged_status != GOT_STATUS_DELETE)
5406 return NULL;
5407 } else {
5408 if (ct->status != GOT_STATUS_MODIFY &&
5409 ct->status != GOT_STATUS_ADD &&
5410 ct->status != GOT_STATUS_DELETE &&
5411 ct->status != GOT_STATUS_CONFLICT)
5412 return NULL;
5415 err = got_opentemp_truncate(f1);
5416 if (err)
5417 return got_error_from_errno("got_opentemp_truncate");
5418 err = got_opentemp_truncate(f2);
5419 if (err)
5420 return got_error_from_errno("got_opentemp_truncate");
5422 if (!*diff_header_shown) {
5423 err = got_object_id_str(&id_str, worktree->base_commit_id);
5424 if (err)
5425 return err;
5426 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5427 got_worktree_get_root_path(worktree));
5428 fprintf(diff_outfile, "commit - %s\n", id_str);
5429 fprintf(diff_outfile, "path + %s%s\n",
5430 got_worktree_get_root_path(worktree),
5431 diff_staged ? " (staged changes)" : "");
5432 *diff_header_shown = 1;
5435 if (diff_staged) {
5436 const char *label1 = NULL, *label2 = NULL;
5437 switch (ct->staged_status) {
5438 case GOT_STATUS_MODIFY:
5439 label1 = ct->path;
5440 label2 = ct->path;
5441 break;
5442 case GOT_STATUS_ADD:
5443 label2 = ct->path;
5444 break;
5445 case GOT_STATUS_DELETE:
5446 label1 = ct->path;
5447 break;
5448 default:
5449 return got_error(GOT_ERR_FILE_STATUS);
5451 fd1 = got_opentempfd();
5452 if (fd1 == -1) {
5453 err = got_error_from_errno("got_opentempfd");
5454 goto done;
5456 fd2 = got_opentempfd();
5457 if (fd2 == -1) {
5458 err = got_error_from_errno("got_opentempfd");
5459 goto done;
5461 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5462 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5463 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5464 NULL, repo, diff_outfile);
5465 goto done;
5468 fd1 = got_opentempfd();
5469 if (fd1 == -1) {
5470 err = got_error_from_errno("got_opentempfd");
5471 goto done;
5474 if (ct->status != GOT_STATUS_ADD) {
5475 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5476 8192, fd1);
5477 if (err)
5478 goto done;
5481 if (ct->status != GOT_STATUS_DELETE) {
5482 if (dirfd != -1) {
5483 fd = openat(dirfd, de_name,
5484 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5485 if (fd == -1) {
5486 if (!got_err_open_nofollow_on_symlink()) {
5487 err = got_error_from_errno2("openat",
5488 ct->ondisk_path);
5489 goto done;
5491 err = get_symlink_target_file(&fd, dirfd,
5492 de_name, ct->ondisk_path);
5493 if (err)
5494 goto done;
5496 } else {
5497 fd = open(ct->ondisk_path,
5498 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5499 if (fd == -1) {
5500 if (!got_err_open_nofollow_on_symlink()) {
5501 err = got_error_from_errno2("open",
5502 ct->ondisk_path);
5503 goto done;
5505 err = get_symlink_target_file(&fd, dirfd,
5506 de_name, ct->ondisk_path);
5507 if (err)
5508 goto done;
5511 if (fstatat(fd, ct->ondisk_path, &sb,
5512 AT_SYMLINK_NOFOLLOW) == -1) {
5513 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5514 goto done;
5516 ondisk_file = fdopen(fd, "r");
5517 if (ondisk_file == NULL) {
5518 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5519 goto done;
5521 fd = -1;
5522 f2_exists = 1;
5525 if (blob1) {
5526 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5527 f1, blob1);
5528 if (err)
5529 goto done;
5532 err = got_diff_blob_file(blob1, f1, size1, label1,
5533 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5534 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5535 done:
5536 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5537 err = got_error_from_errno("close");
5538 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5539 err = got_error_from_errno("close");
5540 if (blob1)
5541 got_object_blob_close(blob1);
5542 if (fd != -1 && close(fd) == -1 && err == NULL)
5543 err = got_error_from_errno("close");
5544 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5545 err = got_error_from_errno("fclose");
5546 return err;
5549 static const struct got_error *
5550 collect_commitables(void *arg, unsigned char status,
5551 unsigned char staged_status, const char *relpath,
5552 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5553 struct got_object_id *commit_id, int dirfd, const char *de_name)
5555 struct collect_commitables_arg *a = arg;
5556 const struct got_error *err = NULL;
5557 struct got_commitable *ct = NULL;
5558 struct got_pathlist_entry *new = NULL;
5559 char *parent_path = NULL, *path = NULL;
5560 struct stat sb;
5562 if (a->have_staged_files) {
5563 if (staged_status != GOT_STATUS_MODIFY &&
5564 staged_status != GOT_STATUS_ADD &&
5565 staged_status != GOT_STATUS_DELETE)
5566 return NULL;
5567 } else {
5568 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5569 printf("C %s\n", relpath);
5570 return got_error(GOT_ERR_COMMIT_CONFLICT);
5573 if (status != GOT_STATUS_MODIFY &&
5574 status != GOT_STATUS_MODE_CHANGE &&
5575 status != GOT_STATUS_ADD &&
5576 status != GOT_STATUS_DELETE &&
5577 status != GOT_STATUS_CONFLICT)
5578 return NULL;
5581 if (asprintf(&path, "/%s", relpath) == -1) {
5582 err = got_error_from_errno("asprintf");
5583 goto done;
5585 if (strcmp(path, "/") == 0) {
5586 parent_path = strdup("");
5587 if (parent_path == NULL)
5588 return got_error_from_errno("strdup");
5589 } else {
5590 err = got_path_dirname(&parent_path, path);
5591 if (err)
5592 return err;
5595 ct = calloc(1, sizeof(*ct));
5596 if (ct == NULL) {
5597 err = got_error_from_errno("calloc");
5598 goto done;
5601 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5602 relpath) == -1) {
5603 err = got_error_from_errno("asprintf");
5604 goto done;
5607 if (staged_status == GOT_STATUS_ADD ||
5608 staged_status == GOT_STATUS_MODIFY) {
5609 struct got_fileindex_entry *ie;
5610 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5611 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5612 case GOT_FILEIDX_MODE_REGULAR_FILE:
5613 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5614 ct->mode = S_IFREG;
5615 break;
5616 case GOT_FILEIDX_MODE_SYMLINK:
5617 ct->mode = S_IFLNK;
5618 break;
5619 default:
5620 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5621 goto done;
5623 ct->mode |= got_fileindex_entry_perms_get(ie);
5624 } else if (status != GOT_STATUS_DELETE &&
5625 staged_status != GOT_STATUS_DELETE) {
5626 if (dirfd != -1) {
5627 if (fstatat(dirfd, de_name, &sb,
5628 AT_SYMLINK_NOFOLLOW) == -1) {
5629 err = got_error_from_errno2("fstatat",
5630 ct->ondisk_path);
5631 goto done;
5633 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5634 err = got_error_from_errno2("lstat", ct->ondisk_path);
5635 goto done;
5637 ct->mode = sb.st_mode;
5640 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5641 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5642 relpath) == -1) {
5643 err = got_error_from_errno("asprintf");
5644 goto done;
5647 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5648 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5649 int is_bad_symlink;
5650 char target_path[PATH_MAX];
5651 ssize_t target_len;
5652 target_len = readlink(ct->ondisk_path, target_path,
5653 sizeof(target_path));
5654 if (target_len == -1) {
5655 err = got_error_from_errno2("readlink",
5656 ct->ondisk_path);
5657 goto done;
5659 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5660 target_len, ct->ondisk_path, a->worktree->root_path,
5661 a->worktree->meta_dir);
5662 if (err)
5663 goto done;
5664 if (is_bad_symlink) {
5665 err = got_error_path(ct->ondisk_path,
5666 GOT_ERR_BAD_SYMLINK);
5667 goto done;
5672 ct->status = status;
5673 ct->staged_status = staged_status;
5674 ct->blob_id = NULL; /* will be filled in when blob gets created */
5675 if (ct->status != GOT_STATUS_ADD &&
5676 ct->staged_status != GOT_STATUS_ADD) {
5677 ct->base_blob_id = got_object_id_dup(blob_id);
5678 if (ct->base_blob_id == NULL) {
5679 err = got_error_from_errno("got_object_id_dup");
5680 goto done;
5682 ct->base_commit_id = got_object_id_dup(commit_id);
5683 if (ct->base_commit_id == NULL) {
5684 err = got_error_from_errno("got_object_id_dup");
5685 goto done;
5688 if (ct->staged_status == GOT_STATUS_ADD ||
5689 ct->staged_status == GOT_STATUS_MODIFY) {
5690 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5691 if (ct->staged_blob_id == NULL) {
5692 err = got_error_from_errno("got_object_id_dup");
5693 goto done;
5696 ct->path = strdup(path);
5697 if (ct->path == NULL) {
5698 err = got_error_from_errno("strdup");
5699 goto done;
5701 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5702 if (err)
5703 goto done;
5705 if (a->diff_outfile && ct && new != NULL) {
5706 err = append_ct_diff(ct, &a->diff_header_shown,
5707 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5708 a->have_staged_files, a->repo, a->worktree);
5709 if (err)
5710 goto done;
5712 done:
5713 if (ct && (err || new == NULL))
5714 free_commitable(ct);
5715 free(parent_path);
5716 free(path);
5717 return err;
5720 static const struct got_error *write_tree(struct got_object_id **, int *,
5721 struct got_tree_object *, const char *, struct got_pathlist_head *,
5722 got_worktree_status_cb status_cb, void *status_arg,
5723 struct got_repository *);
5725 static const struct got_error *
5726 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5727 struct got_tree_entry *te, const char *parent_path,
5728 struct got_pathlist_head *commitable_paths,
5729 got_worktree_status_cb status_cb, void *status_arg,
5730 struct got_repository *repo)
5732 const struct got_error *err = NULL;
5733 struct got_tree_object *subtree;
5734 char *subpath;
5736 if (asprintf(&subpath, "%s%s%s", parent_path,
5737 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5738 return got_error_from_errno("asprintf");
5740 err = got_object_open_as_tree(&subtree, repo, &te->id);
5741 if (err)
5742 return err;
5744 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5745 commitable_paths, status_cb, status_arg, repo);
5746 got_object_tree_close(subtree);
5747 free(subpath);
5748 return err;
5751 static const struct got_error *
5752 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5754 const struct got_error *err = NULL;
5755 char *ct_parent_path = NULL;
5757 *match = 0;
5759 if (strchr(ct->in_repo_path, '/') == NULL) {
5760 *match = got_path_is_root_dir(path);
5761 return NULL;
5764 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5765 if (err)
5766 return err;
5767 *match = (strcmp(path, ct_parent_path) == 0);
5768 free(ct_parent_path);
5769 return err;
5772 static mode_t
5773 get_ct_file_mode(struct got_commitable *ct)
5775 if (S_ISLNK(ct->mode))
5776 return S_IFLNK;
5778 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5781 static const struct got_error *
5782 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5783 struct got_tree_entry *te, struct got_commitable *ct)
5785 const struct got_error *err = NULL;
5787 *new_te = NULL;
5789 err = got_object_tree_entry_dup(new_te, te);
5790 if (err)
5791 goto done;
5793 (*new_te)->mode = get_ct_file_mode(ct);
5795 if (ct->staged_status == GOT_STATUS_MODIFY)
5796 memcpy(&(*new_te)->id, ct->staged_blob_id,
5797 sizeof((*new_te)->id));
5798 else
5799 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5800 done:
5801 if (err && *new_te) {
5802 free(*new_te);
5803 *new_te = NULL;
5805 return err;
5808 static const struct got_error *
5809 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5810 struct got_commitable *ct)
5812 const struct got_error *err = NULL;
5813 char *ct_name = NULL;
5815 *new_te = NULL;
5817 *new_te = calloc(1, sizeof(**new_te));
5818 if (*new_te == NULL)
5819 return got_error_from_errno("calloc");
5821 err = got_path_basename(&ct_name, ct->path);
5822 if (err)
5823 goto done;
5824 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5825 sizeof((*new_te)->name)) {
5826 err = got_error(GOT_ERR_NO_SPACE);
5827 goto done;
5830 (*new_te)->mode = get_ct_file_mode(ct);
5832 if (ct->staged_status == GOT_STATUS_ADD)
5833 memcpy(&(*new_te)->id, ct->staged_blob_id,
5834 sizeof((*new_te)->id));
5835 else
5836 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5837 done:
5838 free(ct_name);
5839 if (err && *new_te) {
5840 free(*new_te);
5841 *new_te = NULL;
5843 return err;
5846 static const struct got_error *
5847 insert_tree_entry(struct got_tree_entry *new_te,
5848 struct got_pathlist_head *paths)
5850 const struct got_error *err = NULL;
5851 struct got_pathlist_entry *new_pe;
5853 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5854 if (err)
5855 return err;
5856 if (new_pe == NULL)
5857 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5858 return NULL;
5861 static const struct got_error *
5862 report_ct_status(struct got_commitable *ct,
5863 got_worktree_status_cb status_cb, void *status_arg)
5865 const char *ct_path = ct->path;
5866 unsigned char status;
5868 if (status_cb == NULL) /* no commit progress output desired */
5869 return NULL;
5871 while (ct_path[0] == '/')
5872 ct_path++;
5874 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5875 status = ct->staged_status;
5876 else
5877 status = ct->status;
5879 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5880 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5883 static const struct got_error *
5884 match_modified_subtree(int *modified, struct got_tree_entry *te,
5885 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5887 const struct got_error *err = NULL;
5888 struct got_pathlist_entry *pe;
5889 char *te_path;
5891 *modified = 0;
5893 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5894 got_path_is_root_dir(base_tree_path) ? "" : "/",
5895 te->name) == -1)
5896 return got_error_from_errno("asprintf");
5898 TAILQ_FOREACH(pe, commitable_paths, entry) {
5899 struct got_commitable *ct = pe->data;
5900 *modified = got_path_is_child(ct->in_repo_path, te_path,
5901 strlen(te_path));
5902 if (*modified)
5903 break;
5906 free(te_path);
5907 return err;
5910 static const struct got_error *
5911 match_deleted_or_modified_ct(struct got_commitable **ctp,
5912 struct got_tree_entry *te, const char *base_tree_path,
5913 struct got_pathlist_head *commitable_paths)
5915 const struct got_error *err = NULL;
5916 struct got_pathlist_entry *pe;
5918 *ctp = NULL;
5920 TAILQ_FOREACH(pe, commitable_paths, entry) {
5921 struct got_commitable *ct = pe->data;
5922 char *ct_name = NULL;
5923 int path_matches;
5925 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5926 if (ct->status != GOT_STATUS_MODIFY &&
5927 ct->status != GOT_STATUS_MODE_CHANGE &&
5928 ct->status != GOT_STATUS_DELETE &&
5929 ct->status != GOT_STATUS_CONFLICT)
5930 continue;
5931 } else {
5932 if (ct->staged_status != GOT_STATUS_MODIFY &&
5933 ct->staged_status != GOT_STATUS_DELETE)
5934 continue;
5937 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5938 continue;
5940 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5941 if (err)
5942 return err;
5943 if (!path_matches)
5944 continue;
5946 err = got_path_basename(&ct_name, pe->path);
5947 if (err)
5948 return err;
5950 if (strcmp(te->name, ct_name) != 0) {
5951 free(ct_name);
5952 continue;
5954 free(ct_name);
5956 *ctp = ct;
5957 break;
5960 return err;
5963 static const struct got_error *
5964 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5965 const char *child_path, const char *path_base_tree,
5966 struct got_pathlist_head *commitable_paths,
5967 got_worktree_status_cb status_cb, void *status_arg,
5968 struct got_repository *repo)
5970 const struct got_error *err = NULL;
5971 struct got_tree_entry *new_te;
5972 char *subtree_path;
5973 struct got_object_id *id = NULL;
5974 int nentries;
5976 *new_tep = NULL;
5978 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5979 got_path_is_root_dir(path_base_tree) ? "" : "/",
5980 child_path) == -1)
5981 return got_error_from_errno("asprintf");
5983 new_te = calloc(1, sizeof(*new_te));
5984 if (new_te == NULL)
5985 return got_error_from_errno("calloc");
5986 new_te->mode = S_IFDIR;
5988 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5989 sizeof(new_te->name)) {
5990 err = got_error(GOT_ERR_NO_SPACE);
5991 goto done;
5993 err = write_tree(&id, &nentries, NULL, subtree_path,
5994 commitable_paths, status_cb, status_arg, repo);
5995 if (err) {
5996 free(new_te);
5997 goto done;
5999 memcpy(&new_te->id, id, sizeof(new_te->id));
6000 done:
6001 free(id);
6002 free(subtree_path);
6003 if (err == NULL)
6004 *new_tep = new_te;
6005 return err;
6008 static const struct got_error *
6009 write_tree(struct got_object_id **new_tree_id, int *nentries,
6010 struct got_tree_object *base_tree, const char *path_base_tree,
6011 struct got_pathlist_head *commitable_paths,
6012 got_worktree_status_cb status_cb, void *status_arg,
6013 struct got_repository *repo)
6015 const struct got_error *err = NULL;
6016 struct got_pathlist_head paths;
6017 struct got_tree_entry *te, *new_te = NULL;
6018 struct got_pathlist_entry *pe;
6020 TAILQ_INIT(&paths);
6021 *nentries = 0;
6023 /* Insert, and recurse into, newly added entries first. */
6024 TAILQ_FOREACH(pe, commitable_paths, entry) {
6025 struct got_commitable *ct = pe->data;
6026 char *child_path = NULL, *slash;
6028 if ((ct->status != GOT_STATUS_ADD &&
6029 ct->staged_status != GOT_STATUS_ADD) ||
6030 (ct->flags & GOT_COMMITABLE_ADDED))
6031 continue;
6033 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6034 strlen(path_base_tree)))
6035 continue;
6037 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6038 ct->in_repo_path);
6039 if (err)
6040 goto done;
6042 slash = strchr(child_path, '/');
6043 if (slash == NULL) {
6044 err = alloc_added_blob_tree_entry(&new_te, ct);
6045 if (err)
6046 goto done;
6047 err = report_ct_status(ct, status_cb, status_arg);
6048 if (err)
6049 goto done;
6050 ct->flags |= GOT_COMMITABLE_ADDED;
6051 err = insert_tree_entry(new_te, &paths);
6052 if (err)
6053 goto done;
6054 (*nentries)++;
6055 } else {
6056 *slash = '\0'; /* trim trailing path components */
6057 if (base_tree == NULL ||
6058 got_object_tree_find_entry(base_tree, child_path)
6059 == NULL) {
6060 err = make_subtree_for_added_blob(&new_te,
6061 child_path, path_base_tree,
6062 commitable_paths, status_cb, status_arg,
6063 repo);
6064 if (err)
6065 goto done;
6066 err = insert_tree_entry(new_te, &paths);
6067 if (err)
6068 goto done;
6069 (*nentries)++;
6074 if (base_tree) {
6075 int i, nbase_entries;
6076 /* Handle modified and deleted entries. */
6077 nbase_entries = got_object_tree_get_nentries(base_tree);
6078 for (i = 0; i < nbase_entries; i++) {
6079 struct got_commitable *ct = NULL;
6081 te = got_object_tree_get_entry(base_tree, i);
6082 if (got_object_tree_entry_is_submodule(te)) {
6083 /* Entry is a submodule; just copy it. */
6084 err = got_object_tree_entry_dup(&new_te, te);
6085 if (err)
6086 goto done;
6087 err = insert_tree_entry(new_te, &paths);
6088 if (err)
6089 goto done;
6090 (*nentries)++;
6091 continue;
6094 if (S_ISDIR(te->mode)) {
6095 int modified;
6096 err = got_object_tree_entry_dup(&new_te, te);
6097 if (err)
6098 goto done;
6099 err = match_modified_subtree(&modified, te,
6100 path_base_tree, commitable_paths);
6101 if (err)
6102 goto done;
6103 /* Avoid recursion into unmodified subtrees. */
6104 if (modified) {
6105 struct got_object_id *new_id;
6106 int nsubentries;
6107 err = write_subtree(&new_id,
6108 &nsubentries, te,
6109 path_base_tree, commitable_paths,
6110 status_cb, status_arg, repo);
6111 if (err)
6112 goto done;
6113 if (nsubentries == 0) {
6114 /* All entries were deleted. */
6115 free(new_id);
6116 continue;
6118 memcpy(&new_te->id, new_id,
6119 sizeof(new_te->id));
6120 free(new_id);
6122 err = insert_tree_entry(new_te, &paths);
6123 if (err)
6124 goto done;
6125 (*nentries)++;
6126 continue;
6129 err = match_deleted_or_modified_ct(&ct, te,
6130 path_base_tree, commitable_paths);
6131 if (err)
6132 goto done;
6133 if (ct) {
6134 /* NB: Deleted entries get dropped here. */
6135 if (ct->status == GOT_STATUS_MODIFY ||
6136 ct->status == GOT_STATUS_MODE_CHANGE ||
6137 ct->status == GOT_STATUS_CONFLICT ||
6138 ct->staged_status == GOT_STATUS_MODIFY) {
6139 err = alloc_modified_blob_tree_entry(
6140 &new_te, te, ct);
6141 if (err)
6142 goto done;
6143 err = insert_tree_entry(new_te, &paths);
6144 if (err)
6145 goto done;
6146 (*nentries)++;
6148 err = report_ct_status(ct, status_cb,
6149 status_arg);
6150 if (err)
6151 goto done;
6152 } else {
6153 /* Entry is unchanged; just copy it. */
6154 err = got_object_tree_entry_dup(&new_te, te);
6155 if (err)
6156 goto done;
6157 err = insert_tree_entry(new_te, &paths);
6158 if (err)
6159 goto done;
6160 (*nentries)++;
6165 /* Write new list of entries; deleted entries have been dropped. */
6166 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6167 done:
6168 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6169 return err;
6172 static const struct got_error *
6173 update_fileindex_after_commit(struct got_worktree *worktree,
6174 struct got_pathlist_head *commitable_paths,
6175 struct got_object_id *new_base_commit_id,
6176 struct got_fileindex *fileindex, int have_staged_files)
6178 const struct got_error *err = NULL;
6179 struct got_pathlist_entry *pe;
6180 char *relpath = NULL;
6182 TAILQ_FOREACH(pe, commitable_paths, entry) {
6183 struct got_fileindex_entry *ie;
6184 struct got_commitable *ct = pe->data;
6186 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6188 err = got_path_skip_common_ancestor(&relpath,
6189 worktree->root_path, ct->ondisk_path);
6190 if (err)
6191 goto done;
6193 if (ie) {
6194 if (ct->status == GOT_STATUS_DELETE ||
6195 ct->staged_status == GOT_STATUS_DELETE) {
6196 got_fileindex_entry_remove(fileindex, ie);
6197 } else if (ct->staged_status == GOT_STATUS_ADD ||
6198 ct->staged_status == GOT_STATUS_MODIFY) {
6199 got_fileindex_entry_stage_set(ie,
6200 GOT_FILEIDX_STAGE_NONE);
6201 got_fileindex_entry_staged_filetype_set(ie, 0);
6203 err = got_fileindex_entry_update(ie,
6204 worktree->root_fd, relpath,
6205 ct->staged_blob_id->sha1,
6206 new_base_commit_id->sha1,
6207 !have_staged_files);
6208 } else
6209 err = got_fileindex_entry_update(ie,
6210 worktree->root_fd, relpath,
6211 ct->blob_id->sha1,
6212 new_base_commit_id->sha1,
6213 !have_staged_files);
6214 } else {
6215 err = got_fileindex_entry_alloc(&ie, pe->path);
6216 if (err)
6217 goto done;
6218 err = got_fileindex_entry_update(ie,
6219 worktree->root_fd, relpath, ct->blob_id->sha1,
6220 new_base_commit_id->sha1, 1);
6221 if (err) {
6222 got_fileindex_entry_free(ie);
6223 goto done;
6225 err = got_fileindex_entry_add(fileindex, ie);
6226 if (err) {
6227 got_fileindex_entry_free(ie);
6228 goto done;
6231 free(relpath);
6232 relpath = NULL;
6234 done:
6235 free(relpath);
6236 return err;
6240 static const struct got_error *
6241 check_out_of_date(const char *in_repo_path, unsigned char status,
6242 unsigned char staged_status, struct got_object_id *base_blob_id,
6243 struct got_object_id *base_commit_id,
6244 struct got_object_id *head_commit_id, struct got_repository *repo,
6245 int ood_errcode)
6247 const struct got_error *err = NULL;
6248 struct got_commit_object *commit = NULL;
6249 struct got_object_id *id = NULL;
6251 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6252 /* Trivial case: base commit == head commit */
6253 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6254 return NULL;
6256 * Ensure file content which local changes were based
6257 * on matches file content in the branch head.
6259 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6260 if (err)
6261 goto done;
6262 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6263 if (err) {
6264 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6265 err = got_error(ood_errcode);
6266 goto done;
6267 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6268 err = got_error(ood_errcode);
6269 } else {
6270 /* Require that added files don't exist in the branch head. */
6271 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6272 if (err)
6273 goto done;
6274 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6275 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6276 goto done;
6277 err = id ? got_error(ood_errcode) : NULL;
6279 done:
6280 free(id);
6281 if (commit)
6282 got_object_commit_close(commit);
6283 return err;
6286 static const struct got_error *
6287 commit_worktree(struct got_object_id **new_commit_id,
6288 struct got_pathlist_head *commitable_paths,
6289 struct got_object_id *head_commit_id,
6290 struct got_object_id *parent_id2,
6291 struct got_worktree *worktree,
6292 const char *author, const char *committer, char *diff_path,
6293 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6294 got_worktree_status_cb status_cb, void *status_arg,
6295 struct got_repository *repo)
6297 const struct got_error *err = NULL, *unlockerr = NULL;
6298 struct got_pathlist_entry *pe;
6299 const char *head_ref_name = NULL;
6300 struct got_commit_object *head_commit = NULL;
6301 struct got_reference *head_ref2 = NULL;
6302 struct got_object_id *head_commit_id2 = NULL;
6303 struct got_tree_object *head_tree = NULL;
6304 struct got_object_id *new_tree_id = NULL;
6305 int nentries, nparents = 0;
6306 struct got_object_id_queue parent_ids;
6307 struct got_object_qid *pid = NULL;
6308 char *logmsg = NULL;
6309 time_t timestamp;
6311 *new_commit_id = NULL;
6313 STAILQ_INIT(&parent_ids);
6315 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6316 if (err)
6317 goto done;
6319 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6320 if (err)
6321 goto done;
6323 if (commit_msg_cb != NULL) {
6324 err = commit_msg_cb(commitable_paths, diff_path,
6325 &logmsg, commit_arg);
6326 if (err)
6327 goto done;
6330 if (logmsg == NULL || strlen(logmsg) == 0) {
6331 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6332 goto done;
6335 /* Create blobs from added and modified files and record their IDs. */
6336 TAILQ_FOREACH(pe, commitable_paths, entry) {
6337 struct got_commitable *ct = pe->data;
6338 char *ondisk_path;
6340 /* Blobs for staged files already exist. */
6341 if (ct->staged_status == GOT_STATUS_ADD ||
6342 ct->staged_status == GOT_STATUS_MODIFY)
6343 continue;
6345 if (ct->status != GOT_STATUS_ADD &&
6346 ct->status != GOT_STATUS_MODIFY &&
6347 ct->status != GOT_STATUS_MODE_CHANGE &&
6348 ct->status != GOT_STATUS_CONFLICT)
6349 continue;
6351 if (asprintf(&ondisk_path, "%s/%s",
6352 worktree->root_path, pe->path) == -1) {
6353 err = got_error_from_errno("asprintf");
6354 goto done;
6356 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6357 free(ondisk_path);
6358 if (err)
6359 goto done;
6362 /* Recursively write new tree objects. */
6363 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6364 commitable_paths, status_cb, status_arg, repo);
6365 if (err)
6366 goto done;
6368 err = got_object_qid_alloc(&pid, head_commit_id);
6369 if (err)
6370 goto done;
6371 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6372 nparents++;
6373 if (parent_id2) {
6374 err = got_object_qid_alloc(&pid, parent_id2);
6375 if (err)
6376 goto done;
6377 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6378 nparents++;
6380 timestamp = time(NULL);
6381 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6382 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6383 if (logmsg != NULL)
6384 free(logmsg);
6385 if (err)
6386 goto done;
6388 /* Check if a concurrent commit to our branch has occurred. */
6389 head_ref_name = got_worktree_get_head_ref_name(worktree);
6390 if (head_ref_name == NULL) {
6391 err = got_error_from_errno("got_worktree_get_head_ref_name");
6392 goto done;
6394 /* Lock the reference here to prevent concurrent modification. */
6395 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6396 if (err)
6397 goto done;
6398 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6399 if (err)
6400 goto done;
6401 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6402 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6403 goto done;
6405 /* Update branch head in repository. */
6406 err = got_ref_change_ref(head_ref2, *new_commit_id);
6407 if (err)
6408 goto done;
6409 err = got_ref_write(head_ref2, repo);
6410 if (err)
6411 goto done;
6413 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6414 if (err)
6415 goto done;
6417 err = ref_base_commit(worktree, repo);
6418 if (err)
6419 goto done;
6420 done:
6421 got_object_id_queue_free(&parent_ids);
6422 if (head_tree)
6423 got_object_tree_close(head_tree);
6424 if (head_commit)
6425 got_object_commit_close(head_commit);
6426 free(head_commit_id2);
6427 if (head_ref2) {
6428 unlockerr = got_ref_unlock(head_ref2);
6429 if (unlockerr && err == NULL)
6430 err = unlockerr;
6431 got_ref_close(head_ref2);
6433 return err;
6436 static const struct got_error *
6437 check_path_is_commitable(const char *path,
6438 struct got_pathlist_head *commitable_paths)
6440 struct got_pathlist_entry *cpe = NULL;
6441 size_t path_len = strlen(path);
6443 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6444 struct got_commitable *ct = cpe->data;
6445 const char *ct_path = ct->path;
6447 while (ct_path[0] == '/')
6448 ct_path++;
6450 if (strcmp(path, ct_path) == 0 ||
6451 got_path_is_child(ct_path, path, path_len))
6452 break;
6455 if (cpe == NULL)
6456 return got_error_path(path, GOT_ERR_BAD_PATH);
6458 return NULL;
6461 static const struct got_error *
6462 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6464 int *have_staged_files = arg;
6466 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6467 *have_staged_files = 1;
6468 return got_error(GOT_ERR_CANCELLED);
6471 return NULL;
6474 static const struct got_error *
6475 check_non_staged_files(struct got_fileindex *fileindex,
6476 struct got_pathlist_head *paths)
6478 struct got_pathlist_entry *pe;
6479 struct got_fileindex_entry *ie;
6481 TAILQ_FOREACH(pe, paths, entry) {
6482 if (pe->path[0] == '\0')
6483 continue;
6484 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6485 if (ie == NULL)
6486 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6487 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6488 return got_error_path(pe->path,
6489 GOT_ERR_FILE_NOT_STAGED);
6492 return NULL;
6495 const struct got_error *
6496 got_worktree_commit(struct got_object_id **new_commit_id,
6497 struct got_worktree *worktree, struct got_pathlist_head *paths,
6498 const char *author, const char *committer, int allow_bad_symlinks,
6499 int show_diff, int commit_conflicts,
6500 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6501 got_worktree_status_cb status_cb, void *status_arg,
6502 struct got_repository *repo)
6504 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6505 struct got_fileindex *fileindex = NULL;
6506 char *fileindex_path = NULL;
6507 struct got_pathlist_head commitable_paths;
6508 struct collect_commitables_arg cc_arg;
6509 struct got_pathlist_entry *pe;
6510 struct got_reference *head_ref = NULL;
6511 struct got_object_id *head_commit_id = NULL;
6512 char *diff_path = NULL;
6513 int have_staged_files = 0;
6515 *new_commit_id = NULL;
6517 memset(&cc_arg, 0, sizeof(cc_arg));
6518 TAILQ_INIT(&commitable_paths);
6520 err = lock_worktree(worktree, LOCK_EX);
6521 if (err)
6522 goto done;
6524 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6525 if (err)
6526 goto done;
6528 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6529 if (err)
6530 goto done;
6532 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6533 if (err)
6534 goto done;
6536 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6537 &have_staged_files);
6538 if (err && err->code != GOT_ERR_CANCELLED)
6539 goto done;
6540 if (have_staged_files) {
6541 err = check_non_staged_files(fileindex, paths);
6542 if (err)
6543 goto done;
6546 cc_arg.commitable_paths = &commitable_paths;
6547 cc_arg.worktree = worktree;
6548 cc_arg.fileindex = fileindex;
6549 cc_arg.repo = repo;
6550 cc_arg.have_staged_files = have_staged_files;
6551 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6552 cc_arg.diff_header_shown = 0;
6553 cc_arg.commit_conflicts = commit_conflicts;
6554 if (show_diff) {
6555 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6556 GOT_TMPDIR_STR "/got", ".diff");
6557 if (err)
6558 goto done;
6559 cc_arg.f1 = got_opentemp();
6560 if (cc_arg.f1 == NULL) {
6561 err = got_error_from_errno("got_opentemp");
6562 goto done;
6564 cc_arg.f2 = got_opentemp();
6565 if (cc_arg.f2 == NULL) {
6566 err = got_error_from_errno("got_opentemp");
6567 goto done;
6571 TAILQ_FOREACH(pe, paths, entry) {
6572 err = worktree_status(worktree, pe->path, fileindex, repo,
6573 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6574 if (err)
6575 goto done;
6578 if (show_diff) {
6579 if (fflush(cc_arg.diff_outfile) == EOF) {
6580 err = got_error_from_errno("fflush");
6581 goto done;
6585 if (TAILQ_EMPTY(&commitable_paths)) {
6586 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6587 goto done;
6590 TAILQ_FOREACH(pe, paths, entry) {
6591 err = check_path_is_commitable(pe->path, &commitable_paths);
6592 if (err)
6593 goto done;
6596 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6597 struct got_commitable *ct = pe->data;
6598 const char *ct_path = ct->in_repo_path;
6600 while (ct_path[0] == '/')
6601 ct_path++;
6602 err = check_out_of_date(ct_path, ct->status,
6603 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6604 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6605 if (err)
6606 goto done;
6610 err = commit_worktree(new_commit_id, &commitable_paths,
6611 head_commit_id, NULL, worktree, author, committer,
6612 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6613 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6614 if (err)
6615 goto done;
6617 err = update_fileindex_after_commit(worktree, &commitable_paths,
6618 *new_commit_id, fileindex, have_staged_files);
6619 sync_err = sync_fileindex(fileindex, fileindex_path);
6620 if (sync_err && err == NULL)
6621 err = sync_err;
6622 done:
6623 if (fileindex)
6624 got_fileindex_free(fileindex);
6625 free(fileindex_path);
6626 unlockerr = lock_worktree(worktree, LOCK_SH);
6627 if (unlockerr && err == NULL)
6628 err = unlockerr;
6629 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6630 struct got_commitable *ct = pe->data;
6632 free_commitable(ct);
6634 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6635 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6636 err = got_error_from_errno2("unlink", diff_path);
6637 free(diff_path);
6638 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6639 err == NULL)
6640 err = got_error_from_errno("fclose");
6641 return err;
6644 const char *
6645 got_commitable_get_path(struct got_commitable *ct)
6647 return ct->path;
6650 unsigned int
6651 got_commitable_get_status(struct got_commitable *ct)
6653 return ct->status;
6656 struct check_rebase_ok_arg {
6657 struct got_worktree *worktree;
6658 struct got_repository *repo;
6661 static const struct got_error *
6662 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6664 const struct got_error *err = NULL;
6665 struct check_rebase_ok_arg *a = arg;
6666 unsigned char status;
6667 struct stat sb;
6668 char *ondisk_path;
6670 /* Reject rebase of a work tree with mixed base commits. */
6671 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6672 SHA1_DIGEST_LENGTH))
6673 return got_error(GOT_ERR_MIXED_COMMITS);
6675 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6676 == -1)
6677 return got_error_from_errno("asprintf");
6679 /* Reject rebase of a work tree with modified or staged files. */
6680 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6681 free(ondisk_path);
6682 if (err)
6683 return err;
6685 if (status != GOT_STATUS_NO_CHANGE)
6686 return got_error(GOT_ERR_MODIFIED);
6687 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6688 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6690 return NULL;
6693 const struct got_error *
6694 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6695 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6696 struct got_worktree *worktree, struct got_reference *branch,
6697 struct got_repository *repo)
6699 const struct got_error *err = NULL;
6700 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6701 char *branch_ref_name = NULL;
6702 char *fileindex_path = NULL;
6703 struct check_rebase_ok_arg ok_arg;
6704 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6705 struct got_object_id *wt_branch_tip = NULL;
6707 *new_base_branch_ref = NULL;
6708 *tmp_branch = NULL;
6709 *fileindex = NULL;
6711 err = lock_worktree(worktree, LOCK_EX);
6712 if (err)
6713 return err;
6715 err = open_fileindex(fileindex, &fileindex_path, worktree);
6716 if (err)
6717 goto done;
6719 ok_arg.worktree = worktree;
6720 ok_arg.repo = repo;
6721 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6722 &ok_arg);
6723 if (err)
6724 goto done;
6726 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6727 if (err)
6728 goto done;
6730 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6731 if (err)
6732 goto done;
6734 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6735 if (err)
6736 goto done;
6738 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6739 0);
6740 if (err)
6741 goto done;
6743 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6744 if (err)
6745 goto done;
6746 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6747 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6748 goto done;
6751 err = got_ref_alloc_symref(new_base_branch_ref,
6752 new_base_branch_ref_name, wt_branch);
6753 if (err)
6754 goto done;
6755 err = got_ref_write(*new_base_branch_ref, repo);
6756 if (err)
6757 goto done;
6759 /* TODO Lock original branch's ref while rebasing? */
6761 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6762 if (err)
6763 goto done;
6765 err = got_ref_write(branch_ref, repo);
6766 if (err)
6767 goto done;
6769 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6770 worktree->base_commit_id);
6771 if (err)
6772 goto done;
6773 err = got_ref_write(*tmp_branch, repo);
6774 if (err)
6775 goto done;
6777 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6778 if (err)
6779 goto done;
6780 done:
6781 free(fileindex_path);
6782 free(tmp_branch_name);
6783 free(new_base_branch_ref_name);
6784 free(branch_ref_name);
6785 if (branch_ref)
6786 got_ref_close(branch_ref);
6787 if (wt_branch)
6788 got_ref_close(wt_branch);
6789 free(wt_branch_tip);
6790 if (err) {
6791 if (*new_base_branch_ref) {
6792 got_ref_close(*new_base_branch_ref);
6793 *new_base_branch_ref = NULL;
6795 if (*tmp_branch) {
6796 got_ref_close(*tmp_branch);
6797 *tmp_branch = NULL;
6799 if (*fileindex) {
6800 got_fileindex_free(*fileindex);
6801 *fileindex = NULL;
6803 lock_worktree(worktree, LOCK_SH);
6805 return err;
6808 const struct got_error *
6809 got_worktree_rebase_continue(struct got_object_id **commit_id,
6810 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6811 struct got_reference **branch, struct got_fileindex **fileindex,
6812 struct got_worktree *worktree, struct got_repository *repo)
6814 const struct got_error *err;
6815 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6816 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6817 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6818 char *fileindex_path = NULL;
6819 int have_staged_files = 0;
6821 *commit_id = NULL;
6822 *new_base_branch = NULL;
6823 *tmp_branch = NULL;
6824 *branch = NULL;
6825 *fileindex = NULL;
6827 err = lock_worktree(worktree, LOCK_EX);
6828 if (err)
6829 return err;
6831 err = open_fileindex(fileindex, &fileindex_path, worktree);
6832 if (err)
6833 goto done;
6835 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6836 &have_staged_files);
6837 if (err && err->code != GOT_ERR_CANCELLED)
6838 goto done;
6839 if (have_staged_files) {
6840 err = got_error(GOT_ERR_STAGED_PATHS);
6841 goto done;
6844 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6845 if (err)
6846 goto done;
6848 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6849 if (err)
6850 goto done;
6852 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6853 if (err)
6854 goto done;
6856 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6857 if (err)
6858 goto done;
6860 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6861 if (err)
6862 goto done;
6864 err = got_ref_open(branch, repo,
6865 got_ref_get_symref_target(branch_ref), 0);
6866 if (err)
6867 goto done;
6869 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6870 if (err)
6871 goto done;
6873 err = got_ref_resolve(commit_id, repo, commit_ref);
6874 if (err)
6875 goto done;
6877 err = got_ref_open(new_base_branch, repo,
6878 new_base_branch_ref_name, 0);
6879 if (err)
6880 goto done;
6882 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6883 if (err)
6884 goto done;
6885 done:
6886 free(commit_ref_name);
6887 free(branch_ref_name);
6888 free(fileindex_path);
6889 if (commit_ref)
6890 got_ref_close(commit_ref);
6891 if (branch_ref)
6892 got_ref_close(branch_ref);
6893 if (err) {
6894 free(*commit_id);
6895 *commit_id = NULL;
6896 if (*tmp_branch) {
6897 got_ref_close(*tmp_branch);
6898 *tmp_branch = NULL;
6900 if (*new_base_branch) {
6901 got_ref_close(*new_base_branch);
6902 *new_base_branch = NULL;
6904 if (*branch) {
6905 got_ref_close(*branch);
6906 *branch = NULL;
6908 if (*fileindex) {
6909 got_fileindex_free(*fileindex);
6910 *fileindex = NULL;
6912 lock_worktree(worktree, LOCK_SH);
6914 return err;
6917 const struct got_error *
6918 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6920 const struct got_error *err;
6921 char *tmp_branch_name = NULL;
6923 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6924 if (err)
6925 return err;
6927 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6928 free(tmp_branch_name);
6929 return NULL;
6932 static const struct got_error *
6933 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6934 const char *diff_path, char **logmsg, void *arg)
6936 *logmsg = arg;
6937 return NULL;
6940 static const struct got_error *
6941 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6942 const char *path, struct got_object_id *blob_id,
6943 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6944 int dirfd, const char *de_name)
6946 return NULL;
6949 struct collect_merged_paths_arg {
6950 got_worktree_checkout_cb progress_cb;
6951 void *progress_arg;
6952 struct got_pathlist_head *merged_paths;
6955 static const struct got_error *
6956 collect_merged_paths(void *arg, unsigned char status, const char *path)
6958 const struct got_error *err;
6959 struct collect_merged_paths_arg *a = arg;
6960 char *p;
6961 struct got_pathlist_entry *new;
6963 err = (*a->progress_cb)(a->progress_arg, status, path);
6964 if (err)
6965 return err;
6967 if (status != GOT_STATUS_MERGE &&
6968 status != GOT_STATUS_ADD &&
6969 status != GOT_STATUS_DELETE &&
6970 status != GOT_STATUS_CONFLICT)
6971 return NULL;
6973 p = strdup(path);
6974 if (p == NULL)
6975 return got_error_from_errno("strdup");
6977 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6978 if (err || new == NULL)
6979 free(p);
6980 return err;
6983 static const struct got_error *
6984 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6985 int is_rebase, struct got_repository *repo)
6987 const struct got_error *err;
6988 struct got_reference *commit_ref = NULL;
6990 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6991 if (err) {
6992 if (err->code != GOT_ERR_NOT_REF)
6993 goto done;
6994 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6995 if (err)
6996 goto done;
6997 err = got_ref_write(commit_ref, repo);
6998 if (err)
6999 goto done;
7000 } else if (is_rebase) {
7001 struct got_object_id *stored_id;
7002 int cmp;
7004 err = got_ref_resolve(&stored_id, repo, commit_ref);
7005 if (err)
7006 goto done;
7007 cmp = got_object_id_cmp(commit_id, stored_id);
7008 free(stored_id);
7009 if (cmp != 0) {
7010 err = got_error(GOT_ERR_REBASE_COMMITID);
7011 goto done;
7014 done:
7015 if (commit_ref)
7016 got_ref_close(commit_ref);
7017 return err;
7020 static const struct got_error *
7021 rebase_merge_files(struct got_pathlist_head *merged_paths,
7022 const char *commit_ref_name, struct got_worktree *worktree,
7023 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7024 struct got_object_id *commit_id, struct got_repository *repo,
7025 got_worktree_checkout_cb progress_cb, void *progress_arg,
7026 got_cancel_cb cancel_cb, void *cancel_arg)
7028 const struct got_error *err;
7029 struct got_reference *commit_ref = NULL;
7030 struct collect_merged_paths_arg cmp_arg;
7031 char *fileindex_path;
7033 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7035 err = get_fileindex_path(&fileindex_path, worktree);
7036 if (err)
7037 return err;
7039 cmp_arg.progress_cb = progress_cb;
7040 cmp_arg.progress_arg = progress_arg;
7041 cmp_arg.merged_paths = merged_paths;
7042 err = merge_files(worktree, fileindex, fileindex_path,
7043 parent_commit_id, commit_id, repo, collect_merged_paths,
7044 &cmp_arg, cancel_cb, cancel_arg);
7045 if (commit_ref)
7046 got_ref_close(commit_ref);
7047 return err;
7050 const struct got_error *
7051 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7052 struct got_worktree *worktree, struct got_fileindex *fileindex,
7053 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7054 struct got_repository *repo,
7055 got_worktree_checkout_cb progress_cb, void *progress_arg,
7056 got_cancel_cb cancel_cb, void *cancel_arg)
7058 const struct got_error *err;
7059 char *commit_ref_name;
7061 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7062 if (err)
7063 return err;
7065 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7066 if (err)
7067 goto done;
7069 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7070 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7071 progress_arg, cancel_cb, cancel_arg);
7072 done:
7073 free(commit_ref_name);
7074 return err;
7077 const struct got_error *
7078 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7079 struct got_worktree *worktree, struct got_fileindex *fileindex,
7080 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7081 struct got_repository *repo,
7082 got_worktree_checkout_cb progress_cb, void *progress_arg,
7083 got_cancel_cb cancel_cb, void *cancel_arg)
7085 const struct got_error *err;
7086 char *commit_ref_name;
7088 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7089 if (err)
7090 return err;
7092 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7093 if (err)
7094 goto done;
7096 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7097 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7098 progress_arg, cancel_cb, cancel_arg);
7099 done:
7100 free(commit_ref_name);
7101 return err;
7104 static const struct got_error *
7105 rebase_commit(struct got_object_id **new_commit_id,
7106 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7107 struct got_worktree *worktree, struct got_fileindex *fileindex,
7108 struct got_reference *tmp_branch, const char *committer,
7109 struct got_commit_object *orig_commit, const char *new_logmsg,
7110 int allow_conflict, struct got_repository *repo)
7112 const struct got_error *err, *sync_err;
7113 struct got_pathlist_head commitable_paths;
7114 struct collect_commitables_arg cc_arg;
7115 char *fileindex_path = NULL;
7116 struct got_reference *head_ref = NULL;
7117 struct got_object_id *head_commit_id = NULL;
7118 char *logmsg = NULL;
7120 memset(&cc_arg, 0, sizeof(cc_arg));
7121 TAILQ_INIT(&commitable_paths);
7122 *new_commit_id = NULL;
7124 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7126 err = get_fileindex_path(&fileindex_path, worktree);
7127 if (err)
7128 return err;
7130 cc_arg.commitable_paths = &commitable_paths;
7131 cc_arg.worktree = worktree;
7132 cc_arg.repo = repo;
7133 cc_arg.have_staged_files = 0;
7134 cc_arg.commit_conflicts = allow_conflict;
7136 * If possible get the status of individual files directly to
7137 * avoid crawling the entire work tree once per rebased commit.
7139 * Ideally, merged_paths would contain a list of commitables
7140 * we could use so we could skip worktree_status() entirely.
7141 * However, we would then need carefully keep track of cumulative
7142 * effects of operations such as file additions and deletions
7143 * in 'got histedit -f' (folding multiple commits into one),
7144 * and this extra complexity is not really worth it.
7146 if (merged_paths) {
7147 struct got_pathlist_entry *pe;
7148 TAILQ_FOREACH(pe, merged_paths, entry) {
7149 err = worktree_status(worktree, pe->path, fileindex,
7150 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7151 0);
7152 if (err)
7153 goto done;
7155 } else {
7156 err = worktree_status(worktree, "", fileindex, repo,
7157 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7158 if (err)
7159 goto done;
7162 if (TAILQ_EMPTY(&commitable_paths)) {
7163 /* No-op change; commit will be elided. */
7164 err = got_ref_delete(commit_ref, repo);
7165 if (err)
7166 goto done;
7167 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7168 goto done;
7171 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7172 if (err)
7173 goto done;
7175 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7176 if (err)
7177 goto done;
7179 if (new_logmsg) {
7180 logmsg = strdup(new_logmsg);
7181 if (logmsg == NULL) {
7182 err = got_error_from_errno("strdup");
7183 goto done;
7185 } else {
7186 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7187 if (err)
7188 goto done;
7191 /* NB: commit_worktree will call free(logmsg) */
7192 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7193 NULL, worktree, got_object_commit_get_author(orig_commit),
7194 committer ? committer :
7195 got_object_commit_get_committer(orig_commit), NULL,
7196 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7197 if (err)
7198 goto done;
7200 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7201 if (err)
7202 goto done;
7204 err = got_ref_delete(commit_ref, repo);
7205 if (err)
7206 goto done;
7208 err = update_fileindex_after_commit(worktree, &commitable_paths,
7209 *new_commit_id, fileindex, 0);
7210 sync_err = sync_fileindex(fileindex, fileindex_path);
7211 if (sync_err && err == NULL)
7212 err = sync_err;
7213 done:
7214 free(fileindex_path);
7215 free(head_commit_id);
7216 if (head_ref)
7217 got_ref_close(head_ref);
7218 if (err) {
7219 free(*new_commit_id);
7220 *new_commit_id = NULL;
7222 return err;
7225 const struct got_error *
7226 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7227 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7228 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7229 const char *committer, struct got_commit_object *orig_commit,
7230 struct got_object_id *orig_commit_id, int allow_conflict,
7231 struct got_repository *repo)
7233 const struct got_error *err;
7234 char *commit_ref_name;
7235 struct got_reference *commit_ref = NULL;
7236 struct got_object_id *commit_id = NULL;
7238 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7239 if (err)
7240 return err;
7242 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7243 if (err)
7244 goto done;
7245 err = got_ref_resolve(&commit_id, repo, commit_ref);
7246 if (err)
7247 goto done;
7248 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7249 err = got_error(GOT_ERR_REBASE_COMMITID);
7250 goto done;
7253 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7254 worktree, fileindex, tmp_branch, committer, orig_commit,
7255 NULL, allow_conflict, repo);
7256 done:
7257 if (commit_ref)
7258 got_ref_close(commit_ref);
7259 free(commit_ref_name);
7260 free(commit_id);
7261 return err;
7264 const struct got_error *
7265 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7266 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7267 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7268 const char *committer, struct got_commit_object *orig_commit,
7269 struct got_object_id *orig_commit_id, const char *new_logmsg,
7270 int allow_conflict, struct got_repository *repo)
7272 const struct got_error *err;
7273 char *commit_ref_name;
7274 struct got_reference *commit_ref = NULL;
7276 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7277 if (err)
7278 return err;
7280 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7281 if (err)
7282 goto done;
7284 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7285 worktree, fileindex, tmp_branch, committer, orig_commit,
7286 new_logmsg, allow_conflict, repo);
7287 done:
7288 if (commit_ref)
7289 got_ref_close(commit_ref);
7290 free(commit_ref_name);
7291 return err;
7294 const struct got_error *
7295 got_worktree_rebase_postpone(struct got_worktree *worktree,
7296 struct got_fileindex *fileindex)
7298 if (fileindex)
7299 got_fileindex_free(fileindex);
7300 return lock_worktree(worktree, LOCK_SH);
7303 static const struct got_error *
7304 delete_ref(const char *name, struct got_repository *repo)
7306 const struct got_error *err;
7307 struct got_reference *ref;
7309 err = got_ref_open(&ref, repo, name, 0);
7310 if (err) {
7311 if (err->code == GOT_ERR_NOT_REF)
7312 return NULL;
7313 return err;
7316 err = got_ref_delete(ref, repo);
7317 got_ref_close(ref);
7318 return err;
7321 static const struct got_error *
7322 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7324 const struct got_error *err;
7325 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7326 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7328 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7329 if (err)
7330 goto done;
7331 err = delete_ref(tmp_branch_name, repo);
7332 if (err)
7333 goto done;
7335 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7336 if (err)
7337 goto done;
7338 err = delete_ref(new_base_branch_ref_name, repo);
7339 if (err)
7340 goto done;
7342 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7343 if (err)
7344 goto done;
7345 err = delete_ref(branch_ref_name, repo);
7346 if (err)
7347 goto done;
7349 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7350 if (err)
7351 goto done;
7352 err = delete_ref(commit_ref_name, repo);
7353 if (err)
7354 goto done;
7356 done:
7357 free(tmp_branch_name);
7358 free(new_base_branch_ref_name);
7359 free(branch_ref_name);
7360 free(commit_ref_name);
7361 return err;
7364 static const struct got_error *
7365 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7366 struct got_object_id *new_commit_id, struct got_repository *repo)
7368 const struct got_error *err;
7369 struct got_reference *ref = NULL;
7370 struct got_object_id *old_commit_id = NULL;
7371 const char *branch_name = NULL;
7372 char *new_id_str = NULL;
7373 char *refname = NULL;
7375 branch_name = got_ref_get_name(branch);
7376 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7377 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7378 branch_name += 11;
7380 err = got_object_id_str(&new_id_str, new_commit_id);
7381 if (err)
7382 return err;
7384 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7385 new_id_str) == -1) {
7386 err = got_error_from_errno("asprintf");
7387 goto done;
7390 err = got_ref_resolve(&old_commit_id, repo, branch);
7391 if (err)
7392 goto done;
7394 err = got_ref_alloc(&ref, refname, old_commit_id);
7395 if (err)
7396 goto done;
7398 err = got_ref_write(ref, repo);
7399 done:
7400 free(new_id_str);
7401 free(refname);
7402 free(old_commit_id);
7403 if (ref)
7404 got_ref_close(ref);
7405 return err;
7408 const struct got_error *
7409 got_worktree_rebase_complete(struct got_worktree *worktree,
7410 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7411 struct got_reference *rebased_branch, struct got_repository *repo,
7412 int create_backup)
7414 const struct got_error *err, *unlockerr, *sync_err;
7415 struct got_object_id *new_head_commit_id = NULL;
7416 char *fileindex_path = NULL;
7418 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7419 if (err)
7420 return err;
7422 if (create_backup) {
7423 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7424 rebased_branch, new_head_commit_id, repo);
7425 if (err)
7426 goto done;
7429 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7430 if (err)
7431 goto done;
7433 err = got_ref_write(rebased_branch, repo);
7434 if (err)
7435 goto done;
7437 err = got_worktree_set_head_ref(worktree, rebased_branch);
7438 if (err)
7439 goto done;
7441 err = delete_rebase_refs(worktree, repo);
7442 if (err)
7443 goto done;
7445 err = get_fileindex_path(&fileindex_path, worktree);
7446 if (err)
7447 goto done;
7448 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7449 sync_err = sync_fileindex(fileindex, fileindex_path);
7450 if (sync_err && err == NULL)
7451 err = sync_err;
7452 done:
7453 got_fileindex_free(fileindex);
7454 free(fileindex_path);
7455 free(new_head_commit_id);
7456 unlockerr = lock_worktree(worktree, LOCK_SH);
7457 if (unlockerr && err == NULL)
7458 err = unlockerr;
7459 return err;
7462 static const struct got_error *
7463 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7464 struct got_object_id *id1, struct got_object_id *id2,
7465 struct got_repository *repo)
7467 const struct got_error *err;
7468 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7469 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7471 if (id1) {
7472 err = got_object_open_as_commit(&commit1, repo, id1);
7473 if (err)
7474 goto done;
7476 err = got_object_open_as_tree(&tree1, repo,
7477 got_object_commit_get_tree_id(commit1));
7478 if (err)
7479 goto done;
7482 if (id2) {
7483 err = got_object_open_as_commit(&commit2, repo, id2);
7484 if (err)
7485 goto done;
7487 err = got_object_open_as_tree(&tree2, repo,
7488 got_object_commit_get_tree_id(commit2));
7489 if (err)
7490 goto done;
7493 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7494 got_diff_tree_collect_changed_paths, paths, 0);
7495 if (err)
7496 goto done;
7497 done:
7498 if (commit1)
7499 got_object_commit_close(commit1);
7500 if (commit2)
7501 got_object_commit_close(commit2);
7502 if (tree1)
7503 got_object_tree_close(tree1);
7504 if (tree2)
7505 got_object_tree_close(tree2);
7506 return err;
7509 static const struct got_error *
7510 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7511 struct got_object_id *id1, struct got_object_id *id2,
7512 const char *path_prefix, struct got_repository *repo)
7514 const struct got_error *err;
7515 struct got_pathlist_head merged_paths;
7516 struct got_pathlist_entry *pe;
7517 char *abspath = NULL, *wt_path = NULL;
7519 TAILQ_INIT(&merged_paths);
7521 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7522 if (err)
7523 goto done;
7525 TAILQ_FOREACH(pe, &merged_paths, entry) {
7526 struct got_diff_changed_path *change = pe->data;
7528 if (change->status != GOT_STATUS_ADD)
7529 continue;
7531 if (got_path_is_root_dir(path_prefix)) {
7532 wt_path = strdup(pe->path);
7533 if (wt_path == NULL) {
7534 err = got_error_from_errno("strdup");
7535 goto done;
7537 } else {
7538 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7539 err = got_error_from_errno("asprintf");
7540 goto done;
7543 err = got_path_skip_common_ancestor(&wt_path,
7544 path_prefix, abspath);
7545 if (err)
7546 goto done;
7547 free(abspath);
7548 abspath = NULL;
7551 err = got_pathlist_append(added_paths, wt_path, NULL);
7552 if (err)
7553 goto done;
7554 wt_path = NULL;
7557 done:
7558 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7559 free(abspath);
7560 free(wt_path);
7561 return err;
7564 static const struct got_error *
7565 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7566 struct got_object_id *id, const char *path_prefix,
7567 struct got_repository *repo)
7569 const struct got_error *err;
7570 struct got_commit_object *commit = NULL;
7571 struct got_object_qid *pid;
7573 err = got_object_open_as_commit(&commit, repo, id);
7574 if (err)
7575 goto done;
7577 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7579 err = get_paths_added_between_commits(added_paths,
7580 pid ? &pid->id : NULL, id, path_prefix, repo);
7581 if (err)
7582 goto done;
7583 done:
7584 if (commit)
7585 got_object_commit_close(commit);
7586 return err;
7589 const struct got_error *
7590 got_worktree_rebase_abort(struct got_worktree *worktree,
7591 struct got_fileindex *fileindex, struct got_repository *repo,
7592 struct got_reference *new_base_branch,
7593 got_worktree_checkout_cb progress_cb, void *progress_arg)
7595 const struct got_error *err, *unlockerr, *sync_err;
7596 struct got_reference *resolved = NULL;
7597 struct got_object_id *commit_id = NULL;
7598 struct got_object_id *merged_commit_id = NULL;
7599 struct got_commit_object *commit = NULL;
7600 char *fileindex_path = NULL;
7601 char *commit_ref_name = NULL;
7602 struct got_reference *commit_ref = NULL;
7603 struct revert_file_args rfa;
7604 struct got_object_id *tree_id = NULL;
7605 struct got_pathlist_head added_paths;
7607 TAILQ_INIT(&added_paths);
7609 err = lock_worktree(worktree, LOCK_EX);
7610 if (err)
7611 return err;
7613 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7614 if (err)
7615 goto done;
7617 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7618 if (err)
7619 goto done;
7621 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7622 if (err)
7623 goto done;
7626 * Determine which files in added status can be safely removed
7627 * from disk while reverting changes in the work tree.
7628 * We want to avoid deleting unrelated files which were added by
7629 * the user for conflict resolution purposes.
7631 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7632 got_worktree_get_path_prefix(worktree), repo);
7633 if (err)
7634 goto done;
7636 err = got_ref_open(&resolved, repo,
7637 got_ref_get_symref_target(new_base_branch), 0);
7638 if (err)
7639 goto done;
7641 err = got_worktree_set_head_ref(worktree, resolved);
7642 if (err)
7643 goto done;
7646 * XXX commits to the base branch could have happened while
7647 * we were busy rebasing; should we store the original commit ID
7648 * when rebase begins and read it back here?
7650 err = got_ref_resolve(&commit_id, repo, resolved);
7651 if (err)
7652 goto done;
7654 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7655 if (err)
7656 goto done;
7658 err = got_object_open_as_commit(&commit, repo,
7659 worktree->base_commit_id);
7660 if (err)
7661 goto done;
7663 err = got_object_id_by_path(&tree_id, repo, commit,
7664 worktree->path_prefix);
7665 if (err)
7666 goto done;
7668 err = delete_rebase_refs(worktree, repo);
7669 if (err)
7670 goto done;
7672 err = get_fileindex_path(&fileindex_path, worktree);
7673 if (err)
7674 goto done;
7676 rfa.worktree = worktree;
7677 rfa.fileindex = fileindex;
7678 rfa.progress_cb = progress_cb;
7679 rfa.progress_arg = progress_arg;
7680 rfa.patch_cb = NULL;
7681 rfa.patch_arg = NULL;
7682 rfa.repo = repo;
7683 rfa.unlink_added_files = 1;
7684 rfa.added_files_to_unlink = &added_paths;
7685 err = worktree_status(worktree, "", fileindex, repo,
7686 revert_file, &rfa, NULL, NULL, 1, 0);
7687 if (err)
7688 goto sync;
7690 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7691 repo, progress_cb, progress_arg, NULL, NULL);
7692 sync:
7693 sync_err = sync_fileindex(fileindex, fileindex_path);
7694 if (sync_err && err == NULL)
7695 err = sync_err;
7696 done:
7697 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7698 got_ref_close(resolved);
7699 free(tree_id);
7700 free(commit_id);
7701 free(merged_commit_id);
7702 if (commit)
7703 got_object_commit_close(commit);
7704 if (fileindex)
7705 got_fileindex_free(fileindex);
7706 free(fileindex_path);
7707 free(commit_ref_name);
7708 if (commit_ref)
7709 got_ref_close(commit_ref);
7711 unlockerr = lock_worktree(worktree, LOCK_SH);
7712 if (unlockerr && err == NULL)
7713 err = unlockerr;
7714 return err;
7717 const struct got_error *
7718 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7719 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7720 struct got_fileindex **fileindex, struct got_worktree *worktree,
7721 struct got_repository *repo)
7723 const struct got_error *err = NULL;
7724 char *tmp_branch_name = NULL;
7725 char *branch_ref_name = NULL;
7726 char *base_commit_ref_name = NULL;
7727 char *fileindex_path = NULL;
7728 struct check_rebase_ok_arg ok_arg;
7729 struct got_reference *wt_branch = NULL;
7730 struct got_reference *base_commit_ref = NULL;
7732 *tmp_branch = NULL;
7733 *branch_ref = NULL;
7734 *base_commit_id = NULL;
7735 *fileindex = NULL;
7737 err = lock_worktree(worktree, LOCK_EX);
7738 if (err)
7739 return err;
7741 err = open_fileindex(fileindex, &fileindex_path, worktree);
7742 if (err)
7743 goto done;
7745 ok_arg.worktree = worktree;
7746 ok_arg.repo = repo;
7747 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7748 &ok_arg);
7749 if (err)
7750 goto done;
7752 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7753 if (err)
7754 goto done;
7756 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7757 if (err)
7758 goto done;
7760 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7761 worktree);
7762 if (err)
7763 goto done;
7765 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7766 0);
7767 if (err)
7768 goto done;
7770 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7771 if (err)
7772 goto done;
7774 err = got_ref_write(*branch_ref, repo);
7775 if (err)
7776 goto done;
7778 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7779 worktree->base_commit_id);
7780 if (err)
7781 goto done;
7782 err = got_ref_write(base_commit_ref, repo);
7783 if (err)
7784 goto done;
7785 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7786 if (*base_commit_id == NULL) {
7787 err = got_error_from_errno("got_object_id_dup");
7788 goto done;
7791 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7792 worktree->base_commit_id);
7793 if (err)
7794 goto done;
7795 err = got_ref_write(*tmp_branch, repo);
7796 if (err)
7797 goto done;
7799 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7800 if (err)
7801 goto done;
7802 done:
7803 free(fileindex_path);
7804 free(tmp_branch_name);
7805 free(branch_ref_name);
7806 free(base_commit_ref_name);
7807 if (wt_branch)
7808 got_ref_close(wt_branch);
7809 if (err) {
7810 if (*branch_ref) {
7811 got_ref_close(*branch_ref);
7812 *branch_ref = NULL;
7814 if (*tmp_branch) {
7815 got_ref_close(*tmp_branch);
7816 *tmp_branch = NULL;
7818 free(*base_commit_id);
7819 if (*fileindex) {
7820 got_fileindex_free(*fileindex);
7821 *fileindex = NULL;
7823 lock_worktree(worktree, LOCK_SH);
7825 return err;
7828 const struct got_error *
7829 got_worktree_histedit_postpone(struct got_worktree *worktree,
7830 struct got_fileindex *fileindex)
7832 if (fileindex)
7833 got_fileindex_free(fileindex);
7834 return lock_worktree(worktree, LOCK_SH);
7837 const struct got_error *
7838 got_worktree_histedit_in_progress(int *in_progress,
7839 struct got_worktree *worktree)
7841 const struct got_error *err;
7842 char *tmp_branch_name = NULL;
7844 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7845 if (err)
7846 return err;
7848 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7849 free(tmp_branch_name);
7850 return NULL;
7853 const struct got_error *
7854 got_worktree_histedit_continue(struct got_object_id **commit_id,
7855 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7856 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7857 struct got_worktree *worktree, struct got_repository *repo)
7859 const struct got_error *err;
7860 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7861 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7862 struct got_reference *commit_ref = NULL;
7863 struct got_reference *base_commit_ref = NULL;
7864 char *fileindex_path = NULL;
7865 int have_staged_files = 0;
7867 *commit_id = NULL;
7868 *tmp_branch = NULL;
7869 *base_commit_id = NULL;
7870 *fileindex = NULL;
7872 err = lock_worktree(worktree, LOCK_EX);
7873 if (err)
7874 return err;
7876 err = open_fileindex(fileindex, &fileindex_path, worktree);
7877 if (err)
7878 goto done;
7880 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7881 &have_staged_files);
7882 if (err && err->code != GOT_ERR_CANCELLED)
7883 goto done;
7884 if (have_staged_files) {
7885 err = got_error(GOT_ERR_STAGED_PATHS);
7886 goto done;
7889 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7890 if (err)
7891 goto done;
7893 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7894 if (err)
7895 goto done;
7897 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7898 if (err)
7899 goto done;
7901 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7902 worktree);
7903 if (err)
7904 goto done;
7906 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7907 if (err)
7908 goto done;
7910 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7911 if (err)
7912 goto done;
7913 err = got_ref_resolve(commit_id, repo, commit_ref);
7914 if (err)
7915 goto done;
7917 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7918 if (err)
7919 goto done;
7920 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7921 if (err)
7922 goto done;
7924 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7925 if (err)
7926 goto done;
7927 done:
7928 free(commit_ref_name);
7929 free(branch_ref_name);
7930 free(fileindex_path);
7931 if (commit_ref)
7932 got_ref_close(commit_ref);
7933 if (base_commit_ref)
7934 got_ref_close(base_commit_ref);
7935 if (err) {
7936 free(*commit_id);
7937 *commit_id = NULL;
7938 free(*base_commit_id);
7939 *base_commit_id = NULL;
7940 if (*tmp_branch) {
7941 got_ref_close(*tmp_branch);
7942 *tmp_branch = NULL;
7944 if (*fileindex) {
7945 got_fileindex_free(*fileindex);
7946 *fileindex = NULL;
7948 lock_worktree(worktree, LOCK_EX);
7950 return err;
7953 static const struct got_error *
7954 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7956 const struct got_error *err;
7957 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7958 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7960 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7961 if (err)
7962 goto done;
7963 err = delete_ref(tmp_branch_name, repo);
7964 if (err)
7965 goto done;
7967 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7968 worktree);
7969 if (err)
7970 goto done;
7971 err = delete_ref(base_commit_ref_name, repo);
7972 if (err)
7973 goto done;
7975 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7976 if (err)
7977 goto done;
7978 err = delete_ref(branch_ref_name, repo);
7979 if (err)
7980 goto done;
7982 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7983 if (err)
7984 goto done;
7985 err = delete_ref(commit_ref_name, repo);
7986 if (err)
7987 goto done;
7988 done:
7989 free(tmp_branch_name);
7990 free(base_commit_ref_name);
7991 free(branch_ref_name);
7992 free(commit_ref_name);
7993 return err;
7996 const struct got_error *
7997 got_worktree_histedit_abort(struct got_worktree *worktree,
7998 struct got_fileindex *fileindex, struct got_repository *repo,
7999 struct got_reference *branch, struct got_object_id *base_commit_id,
8000 got_worktree_checkout_cb progress_cb, void *progress_arg)
8002 const struct got_error *err, *unlockerr, *sync_err;
8003 struct got_reference *resolved = NULL;
8004 char *fileindex_path = NULL;
8005 struct got_object_id *merged_commit_id = NULL;
8006 struct got_commit_object *commit = NULL;
8007 char *commit_ref_name = NULL;
8008 struct got_reference *commit_ref = NULL;
8009 struct got_object_id *tree_id = NULL;
8010 struct revert_file_args rfa;
8011 struct got_pathlist_head added_paths;
8013 TAILQ_INIT(&added_paths);
8015 err = lock_worktree(worktree, LOCK_EX);
8016 if (err)
8017 return err;
8019 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8020 if (err)
8021 goto done;
8023 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8024 if (err) {
8025 if (err->code != GOT_ERR_NOT_REF)
8026 goto done;
8027 /* Can happen on early abort due to invalid histedit script. */
8028 commit_ref = NULL;
8031 if (commit_ref) {
8032 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8033 if (err)
8034 goto done;
8037 * Determine which files in added status can be safely removed
8038 * from disk while reverting changes in the work tree.
8039 * We want to avoid deleting unrelated files added by the
8040 * user during conflict resolution or during histedit -e.
8042 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8043 got_worktree_get_path_prefix(worktree), repo);
8044 if (err)
8045 goto done;
8048 err = got_ref_open(&resolved, repo,
8049 got_ref_get_symref_target(branch), 0);
8050 if (err)
8051 goto done;
8053 err = got_worktree_set_head_ref(worktree, resolved);
8054 if (err)
8055 goto done;
8057 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8058 if (err)
8059 goto done;
8061 err = got_object_open_as_commit(&commit, repo,
8062 worktree->base_commit_id);
8063 if (err)
8064 goto done;
8066 err = got_object_id_by_path(&tree_id, repo, commit,
8067 worktree->path_prefix);
8068 if (err)
8069 goto done;
8071 err = delete_histedit_refs(worktree, repo);
8072 if (err)
8073 goto done;
8075 err = get_fileindex_path(&fileindex_path, worktree);
8076 if (err)
8077 goto done;
8079 rfa.worktree = worktree;
8080 rfa.fileindex = fileindex;
8081 rfa.progress_cb = progress_cb;
8082 rfa.progress_arg = progress_arg;
8083 rfa.patch_cb = NULL;
8084 rfa.patch_arg = NULL;
8085 rfa.repo = repo;
8086 rfa.unlink_added_files = 1;
8087 rfa.added_files_to_unlink = &added_paths;
8088 err = worktree_status(worktree, "", fileindex, repo,
8089 revert_file, &rfa, NULL, NULL, 1, 0);
8090 if (err)
8091 goto sync;
8093 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8094 repo, progress_cb, progress_arg, NULL, NULL);
8095 sync:
8096 sync_err = sync_fileindex(fileindex, fileindex_path);
8097 if (sync_err && err == NULL)
8098 err = sync_err;
8099 done:
8100 if (resolved)
8101 got_ref_close(resolved);
8102 if (commit_ref)
8103 got_ref_close(commit_ref);
8104 free(merged_commit_id);
8105 free(tree_id);
8106 free(fileindex_path);
8107 free(commit_ref_name);
8109 unlockerr = lock_worktree(worktree, LOCK_SH);
8110 if (unlockerr && err == NULL)
8111 err = unlockerr;
8112 return err;
8115 const struct got_error *
8116 got_worktree_histedit_complete(struct got_worktree *worktree,
8117 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8118 struct got_reference *edited_branch, struct got_repository *repo)
8120 const struct got_error *err, *unlockerr, *sync_err;
8121 struct got_object_id *new_head_commit_id = NULL;
8122 struct got_reference *resolved = NULL;
8123 char *fileindex_path = NULL;
8125 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8126 if (err)
8127 return err;
8129 err = got_ref_open(&resolved, repo,
8130 got_ref_get_symref_target(edited_branch), 0);
8131 if (err)
8132 goto done;
8134 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8135 resolved, new_head_commit_id, repo);
8136 if (err)
8137 goto done;
8139 err = got_ref_change_ref(resolved, new_head_commit_id);
8140 if (err)
8141 goto done;
8143 err = got_ref_write(resolved, repo);
8144 if (err)
8145 goto done;
8147 err = got_worktree_set_head_ref(worktree, resolved);
8148 if (err)
8149 goto done;
8151 err = delete_histedit_refs(worktree, repo);
8152 if (err)
8153 goto done;
8155 err = get_fileindex_path(&fileindex_path, worktree);
8156 if (err)
8157 goto done;
8158 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8159 sync_err = sync_fileindex(fileindex, fileindex_path);
8160 if (sync_err && err == NULL)
8161 err = sync_err;
8162 done:
8163 got_fileindex_free(fileindex);
8164 free(fileindex_path);
8165 free(new_head_commit_id);
8166 unlockerr = lock_worktree(worktree, LOCK_SH);
8167 if (unlockerr && err == NULL)
8168 err = unlockerr;
8169 return err;
8172 const struct got_error *
8173 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8174 struct got_object_id *commit_id, struct got_repository *repo)
8176 const struct got_error *err;
8177 char *commit_ref_name;
8179 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8180 if (err)
8181 return err;
8183 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8184 if (err)
8185 goto done;
8187 err = delete_ref(commit_ref_name, repo);
8188 done:
8189 free(commit_ref_name);
8190 return err;
8193 const struct got_error *
8194 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8195 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8196 struct got_worktree *worktree, const char *refname,
8197 struct got_repository *repo)
8199 const struct got_error *err = NULL;
8200 char *fileindex_path = NULL;
8201 struct check_rebase_ok_arg ok_arg;
8203 *fileindex = NULL;
8204 *branch_ref = NULL;
8205 *base_branch_ref = NULL;
8207 err = lock_worktree(worktree, LOCK_EX);
8208 if (err)
8209 return err;
8211 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8212 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8213 "cannot integrate a branch into itself; "
8214 "update -b or different branch name required");
8215 goto done;
8218 err = open_fileindex(fileindex, &fileindex_path, worktree);
8219 if (err)
8220 goto done;
8222 /* Preconditions are the same as for rebase. */
8223 ok_arg.worktree = worktree;
8224 ok_arg.repo = repo;
8225 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8226 &ok_arg);
8227 if (err)
8228 goto done;
8230 err = got_ref_open(branch_ref, repo, refname, 1);
8231 if (err)
8232 goto done;
8234 err = got_ref_open(base_branch_ref, repo,
8235 got_worktree_get_head_ref_name(worktree), 1);
8236 done:
8237 if (err) {
8238 if (*branch_ref) {
8239 got_ref_close(*branch_ref);
8240 *branch_ref = NULL;
8242 if (*base_branch_ref) {
8243 got_ref_close(*base_branch_ref);
8244 *base_branch_ref = NULL;
8246 if (*fileindex) {
8247 got_fileindex_free(*fileindex);
8248 *fileindex = NULL;
8250 lock_worktree(worktree, LOCK_SH);
8252 return err;
8255 const struct got_error *
8256 got_worktree_integrate_continue(struct got_worktree *worktree,
8257 struct got_fileindex *fileindex, struct got_repository *repo,
8258 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8259 got_worktree_checkout_cb progress_cb, void *progress_arg,
8260 got_cancel_cb cancel_cb, void *cancel_arg)
8262 const struct got_error *err = NULL, *sync_err, *unlockerr;
8263 char *fileindex_path = NULL;
8264 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8265 struct got_commit_object *commit = NULL;
8267 err = get_fileindex_path(&fileindex_path, worktree);
8268 if (err)
8269 goto done;
8271 err = got_ref_resolve(&commit_id, repo, branch_ref);
8272 if (err)
8273 goto done;
8275 err = got_object_open_as_commit(&commit, repo, commit_id);
8276 if (err)
8277 goto done;
8279 err = got_object_id_by_path(&tree_id, repo, commit,
8280 worktree->path_prefix);
8281 if (err)
8282 goto done;
8284 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8285 if (err)
8286 goto done;
8288 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8289 progress_cb, progress_arg, cancel_cb, cancel_arg);
8290 if (err)
8291 goto sync;
8293 err = got_ref_change_ref(base_branch_ref, commit_id);
8294 if (err)
8295 goto sync;
8297 err = got_ref_write(base_branch_ref, repo);
8298 if (err)
8299 goto sync;
8301 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8302 sync:
8303 sync_err = sync_fileindex(fileindex, fileindex_path);
8304 if (sync_err && err == NULL)
8305 err = sync_err;
8307 done:
8308 unlockerr = got_ref_unlock(branch_ref);
8309 if (unlockerr && err == NULL)
8310 err = unlockerr;
8311 got_ref_close(branch_ref);
8313 unlockerr = got_ref_unlock(base_branch_ref);
8314 if (unlockerr && err == NULL)
8315 err = unlockerr;
8316 got_ref_close(base_branch_ref);
8318 got_fileindex_free(fileindex);
8319 free(fileindex_path);
8320 free(tree_id);
8321 if (commit)
8322 got_object_commit_close(commit);
8324 unlockerr = lock_worktree(worktree, LOCK_SH);
8325 if (unlockerr && err == NULL)
8326 err = unlockerr;
8327 return err;
8330 const struct got_error *
8331 got_worktree_integrate_abort(struct got_worktree *worktree,
8332 struct got_fileindex *fileindex, struct got_repository *repo,
8333 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8335 const struct got_error *err = NULL, *unlockerr = NULL;
8337 got_fileindex_free(fileindex);
8339 err = lock_worktree(worktree, LOCK_SH);
8341 unlockerr = got_ref_unlock(branch_ref);
8342 if (unlockerr && err == NULL)
8343 err = unlockerr;
8344 got_ref_close(branch_ref);
8346 unlockerr = got_ref_unlock(base_branch_ref);
8347 if (unlockerr && err == NULL)
8348 err = unlockerr;
8349 got_ref_close(base_branch_ref);
8351 return err;
8354 const struct got_error *
8355 got_worktree_merge_postpone(struct got_worktree *worktree,
8356 struct got_fileindex *fileindex)
8358 const struct got_error *err, *sync_err;
8359 char *fileindex_path = NULL;
8361 err = get_fileindex_path(&fileindex_path, worktree);
8362 if (err)
8363 goto done;
8365 sync_err = sync_fileindex(fileindex, fileindex_path);
8367 err = lock_worktree(worktree, LOCK_SH);
8368 if (sync_err && err == NULL)
8369 err = sync_err;
8370 done:
8371 got_fileindex_free(fileindex);
8372 free(fileindex_path);
8373 return err;
8376 static const struct got_error *
8377 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8379 const struct got_error *err;
8380 char *branch_refname = NULL, *commit_refname = NULL;
8382 err = get_merge_branch_ref_name(&branch_refname, worktree);
8383 if (err)
8384 goto done;
8385 err = delete_ref(branch_refname, repo);
8386 if (err)
8387 goto done;
8389 err = get_merge_commit_ref_name(&commit_refname, worktree);
8390 if (err)
8391 goto done;
8392 err = delete_ref(commit_refname, repo);
8393 if (err)
8394 goto done;
8396 done:
8397 free(branch_refname);
8398 free(commit_refname);
8399 return err;
8402 struct merge_commit_msg_arg {
8403 struct got_worktree *worktree;
8404 const char *branch_name;
8407 static const struct got_error *
8408 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8409 const char *diff_path, char **logmsg, void *arg)
8411 struct merge_commit_msg_arg *a = arg;
8413 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8414 got_worktree_get_head_ref_name(a->worktree)) == -1)
8415 return got_error_from_errno("asprintf");
8417 return NULL;
8421 const struct got_error *
8422 got_worktree_merge_branch(struct got_worktree *worktree,
8423 struct got_fileindex *fileindex,
8424 struct got_object_id *yca_commit_id,
8425 struct got_object_id *branch_tip,
8426 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8427 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8429 const struct got_error *err;
8430 char *fileindex_path = NULL;
8431 struct check_mixed_commits_args cma;
8433 err = get_fileindex_path(&fileindex_path, worktree);
8434 if (err)
8435 goto done;
8437 cma.worktree = worktree;
8438 cma.cancel_cb = cancel_cb;
8439 cma.cancel_arg = cancel_arg;
8441 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8442 worktree);
8443 if (err)
8444 goto done;
8446 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8447 branch_tip, repo, progress_cb, progress_arg,
8448 cancel_cb, cancel_arg);
8449 done:
8450 free(fileindex_path);
8451 return err;
8454 const struct got_error *
8455 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8456 struct got_worktree *worktree, struct got_fileindex *fileindex,
8457 const char *author, const char *committer, int allow_bad_symlinks,
8458 struct got_object_id *branch_tip, const char *branch_name,
8459 int allow_conflict, struct got_repository *repo,
8460 got_worktree_status_cb status_cb, void *status_arg)
8463 const struct got_error *err = NULL, *sync_err;
8464 struct got_pathlist_head commitable_paths;
8465 struct collect_commitables_arg cc_arg;
8466 struct got_pathlist_entry *pe;
8467 struct got_reference *head_ref = NULL;
8468 struct got_object_id *head_commit_id = NULL;
8469 int have_staged_files = 0;
8470 struct merge_commit_msg_arg mcm_arg;
8471 char *fileindex_path = NULL;
8473 memset(&cc_arg, 0, sizeof(cc_arg));
8474 *new_commit_id = NULL;
8476 TAILQ_INIT(&commitable_paths);
8478 err = get_fileindex_path(&fileindex_path, worktree);
8479 if (err)
8480 goto done;
8482 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8483 if (err)
8484 goto done;
8486 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8487 if (err)
8488 goto done;
8490 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8491 &have_staged_files);
8492 if (err && err->code != GOT_ERR_CANCELLED)
8493 goto done;
8494 if (have_staged_files) {
8495 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8496 goto done;
8499 cc_arg.commitable_paths = &commitable_paths;
8500 cc_arg.worktree = worktree;
8501 cc_arg.fileindex = fileindex;
8502 cc_arg.repo = repo;
8503 cc_arg.have_staged_files = have_staged_files;
8504 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8505 cc_arg.commit_conflicts = allow_conflict;
8506 err = worktree_status(worktree, "", fileindex, repo,
8507 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8508 if (err)
8509 goto done;
8511 mcm_arg.worktree = worktree;
8512 mcm_arg.branch_name = branch_name;
8513 err = commit_worktree(new_commit_id, &commitable_paths,
8514 head_commit_id, branch_tip, worktree, author, committer, NULL,
8515 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8516 if (err)
8517 goto done;
8519 err = update_fileindex_after_commit(worktree, &commitable_paths,
8520 *new_commit_id, fileindex, have_staged_files);
8521 sync_err = sync_fileindex(fileindex, fileindex_path);
8522 if (sync_err && err == NULL)
8523 err = sync_err;
8524 done:
8525 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8526 struct got_commitable *ct = pe->data;
8528 free_commitable(ct);
8530 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8531 free(fileindex_path);
8532 return err;
8535 const struct got_error *
8536 got_worktree_merge_complete(struct got_worktree *worktree,
8537 struct got_fileindex *fileindex, struct got_repository *repo)
8539 const struct got_error *err, *unlockerr, *sync_err;
8540 char *fileindex_path = NULL;
8542 err = delete_merge_refs(worktree, repo);
8543 if (err)
8544 goto done;
8546 err = get_fileindex_path(&fileindex_path, worktree);
8547 if (err)
8548 goto done;
8549 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8550 sync_err = sync_fileindex(fileindex, fileindex_path);
8551 if (sync_err && err == NULL)
8552 err = sync_err;
8553 done:
8554 got_fileindex_free(fileindex);
8555 free(fileindex_path);
8556 unlockerr = lock_worktree(worktree, LOCK_SH);
8557 if (unlockerr && err == NULL)
8558 err = unlockerr;
8559 return err;
8562 const struct got_error *
8563 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8564 struct got_repository *repo)
8566 const struct got_error *err;
8567 char *branch_refname = NULL;
8568 struct got_reference *branch_ref = NULL;
8570 *in_progress = 0;
8572 err = get_merge_branch_ref_name(&branch_refname, worktree);
8573 if (err)
8574 return err;
8575 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8576 free(branch_refname);
8577 if (err) {
8578 if (err->code != GOT_ERR_NOT_REF)
8579 return err;
8580 } else
8581 *in_progress = 1;
8583 return NULL;
8586 const struct got_error *got_worktree_merge_prepare(
8587 struct got_fileindex **fileindex, struct got_worktree *worktree,
8588 struct got_repository *repo)
8590 const struct got_error *err = NULL;
8591 char *fileindex_path = NULL;
8592 struct got_reference *wt_branch = NULL;
8593 struct got_object_id *wt_branch_tip = NULL;
8594 struct check_rebase_ok_arg ok_arg;
8596 *fileindex = NULL;
8598 err = lock_worktree(worktree, LOCK_EX);
8599 if (err)
8600 return err;
8602 err = open_fileindex(fileindex, &fileindex_path, worktree);
8603 if (err)
8604 goto done;
8606 /* Preconditions are the same as for rebase. */
8607 ok_arg.worktree = worktree;
8608 ok_arg.repo = repo;
8609 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8610 &ok_arg);
8611 if (err)
8612 goto done;
8614 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8615 0);
8616 if (err)
8617 goto done;
8619 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8620 if (err)
8621 goto done;
8623 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8624 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8625 goto done;
8628 done:
8629 free(fileindex_path);
8630 if (wt_branch)
8631 got_ref_close(wt_branch);
8632 free(wt_branch_tip);
8633 if (err) {
8634 if (*fileindex) {
8635 got_fileindex_free(*fileindex);
8636 *fileindex = NULL;
8638 lock_worktree(worktree, LOCK_SH);
8640 return err;
8643 const struct got_error *got_worktree_merge_write_refs(
8644 struct got_worktree *worktree, struct got_reference *branch,
8645 struct got_repository *repo)
8647 const struct got_error *err = NULL;
8648 char *branch_refname = NULL, *commit_refname = NULL;
8649 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8650 struct got_object_id *branch_tip = NULL;
8652 err = get_merge_branch_ref_name(&branch_refname, worktree);
8653 if (err)
8654 return err;
8656 err = get_merge_commit_ref_name(&commit_refname, worktree);
8657 if (err)
8658 return err;
8660 err = got_ref_resolve(&branch_tip, repo, branch);
8661 if (err)
8662 goto done;
8664 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8665 if (err)
8666 goto done;
8667 err = got_ref_write(branch_ref, repo);
8668 if (err)
8669 goto done;
8671 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8672 if (err)
8673 goto done;
8674 err = got_ref_write(commit_ref, repo);
8675 if (err)
8676 goto done;
8678 done:
8679 free(branch_refname);
8680 free(commit_refname);
8681 if (branch_ref)
8682 got_ref_close(branch_ref);
8683 if (commit_ref)
8684 got_ref_close(commit_ref);
8685 free(branch_tip);
8686 return err;
8689 const struct got_error *
8690 got_worktree_merge_continue(char **branch_name,
8691 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8692 struct got_worktree *worktree, struct got_repository *repo)
8694 const struct got_error *err;
8695 char *commit_refname = NULL, *branch_refname = NULL;
8696 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8697 char *fileindex_path = NULL;
8698 int have_staged_files = 0;
8700 *branch_name = NULL;
8701 *branch_tip = NULL;
8702 *fileindex = NULL;
8704 err = lock_worktree(worktree, LOCK_EX);
8705 if (err)
8706 return err;
8708 err = open_fileindex(fileindex, &fileindex_path, worktree);
8709 if (err)
8710 goto done;
8712 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8713 &have_staged_files);
8714 if (err && err->code != GOT_ERR_CANCELLED)
8715 goto done;
8716 if (have_staged_files) {
8717 err = got_error(GOT_ERR_STAGED_PATHS);
8718 goto done;
8721 err = get_merge_branch_ref_name(&branch_refname, worktree);
8722 if (err)
8723 goto done;
8725 err = get_merge_commit_ref_name(&commit_refname, worktree);
8726 if (err)
8727 goto done;
8729 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8730 if (err)
8731 goto done;
8733 if (!got_ref_is_symbolic(branch_ref)) {
8734 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8735 "%s is not a symbolic reference",
8736 got_ref_get_name(branch_ref));
8737 goto done;
8739 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8740 if (*branch_name == NULL) {
8741 err = got_error_from_errno("strdup");
8742 goto done;
8745 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8746 if (err)
8747 goto done;
8749 err = got_ref_resolve(branch_tip, repo, commit_ref);
8750 if (err)
8751 goto done;
8752 done:
8753 free(commit_refname);
8754 free(branch_refname);
8755 free(fileindex_path);
8756 if (commit_ref)
8757 got_ref_close(commit_ref);
8758 if (branch_ref)
8759 got_ref_close(branch_ref);
8760 if (err) {
8761 if (*branch_name) {
8762 free(*branch_name);
8763 *branch_name = NULL;
8765 free(*branch_tip);
8766 *branch_tip = NULL;
8767 if (*fileindex) {
8768 got_fileindex_free(*fileindex);
8769 *fileindex = NULL;
8771 lock_worktree(worktree, LOCK_SH);
8773 return err;
8776 const struct got_error *
8777 got_worktree_merge_abort(struct got_worktree *worktree,
8778 struct got_fileindex *fileindex, struct got_repository *repo,
8779 got_worktree_checkout_cb progress_cb, void *progress_arg)
8781 const struct got_error *err, *unlockerr, *sync_err;
8782 struct got_commit_object *commit = NULL;
8783 char *fileindex_path = NULL;
8784 struct revert_file_args rfa;
8785 char *commit_ref_name = NULL;
8786 struct got_reference *commit_ref = NULL;
8787 struct got_object_id *merged_commit_id = NULL;
8788 struct got_object_id *tree_id = NULL;
8789 struct got_pathlist_head added_paths;
8791 TAILQ_INIT(&added_paths);
8793 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8794 if (err)
8795 goto done;
8797 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8798 if (err)
8799 goto done;
8801 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8802 if (err)
8803 goto done;
8806 * Determine which files in added status can be safely removed
8807 * from disk while reverting changes in the work tree.
8808 * We want to avoid deleting unrelated files which were added by
8809 * the user for conflict resolution purposes.
8811 err = get_paths_added_between_commits(&added_paths,
8812 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8813 got_worktree_get_path_prefix(worktree), repo);
8814 if (err)
8815 goto done;
8818 err = got_object_open_as_commit(&commit, repo,
8819 worktree->base_commit_id);
8820 if (err)
8821 goto done;
8823 err = got_object_id_by_path(&tree_id, repo, commit,
8824 worktree->path_prefix);
8825 if (err)
8826 goto done;
8828 err = delete_merge_refs(worktree, repo);
8829 if (err)
8830 goto done;
8832 err = get_fileindex_path(&fileindex_path, worktree);
8833 if (err)
8834 goto done;
8836 rfa.worktree = worktree;
8837 rfa.fileindex = fileindex;
8838 rfa.progress_cb = progress_cb;
8839 rfa.progress_arg = progress_arg;
8840 rfa.patch_cb = NULL;
8841 rfa.patch_arg = NULL;
8842 rfa.repo = repo;
8843 rfa.unlink_added_files = 1;
8844 rfa.added_files_to_unlink = &added_paths;
8845 err = worktree_status(worktree, "", fileindex, repo,
8846 revert_file, &rfa, NULL, NULL, 1, 0);
8847 if (err)
8848 goto sync;
8850 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8851 repo, progress_cb, progress_arg, NULL, NULL);
8852 sync:
8853 sync_err = sync_fileindex(fileindex, fileindex_path);
8854 if (sync_err && err == NULL)
8855 err = sync_err;
8856 done:
8857 free(tree_id);
8858 free(merged_commit_id);
8859 if (commit)
8860 got_object_commit_close(commit);
8861 if (fileindex)
8862 got_fileindex_free(fileindex);
8863 free(fileindex_path);
8864 if (commit_ref)
8865 got_ref_close(commit_ref);
8866 free(commit_ref_name);
8868 unlockerr = lock_worktree(worktree, LOCK_SH);
8869 if (unlockerr && err == NULL)
8870 err = unlockerr;
8871 return err;
8874 struct check_stage_ok_arg {
8875 struct got_object_id *head_commit_id;
8876 struct got_worktree *worktree;
8877 struct got_fileindex *fileindex;
8878 struct got_repository *repo;
8879 int have_changes;
8882 static const struct got_error *
8883 check_stage_ok(void *arg, unsigned char status,
8884 unsigned char staged_status, const char *relpath,
8885 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8886 struct got_object_id *commit_id, int dirfd, const char *de_name)
8888 struct check_stage_ok_arg *a = arg;
8889 const struct got_error *err = NULL;
8890 struct got_fileindex_entry *ie;
8891 struct got_object_id base_commit_id;
8892 struct got_object_id *base_commit_idp = NULL;
8893 char *in_repo_path = NULL, *p;
8895 if (status == GOT_STATUS_UNVERSIONED ||
8896 status == GOT_STATUS_NO_CHANGE)
8897 return NULL;
8898 if (status == GOT_STATUS_NONEXISTENT)
8899 return got_error_set_errno(ENOENT, relpath);
8901 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8902 if (ie == NULL)
8903 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8905 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8906 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8907 relpath) == -1)
8908 return got_error_from_errno("asprintf");
8910 if (got_fileindex_entry_has_commit(ie)) {
8911 base_commit_idp = got_fileindex_entry_get_commit_id(
8912 &base_commit_id, ie);
8915 if (status == GOT_STATUS_CONFLICT) {
8916 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8917 goto done;
8918 } else if (status != GOT_STATUS_ADD &&
8919 status != GOT_STATUS_MODIFY &&
8920 status != GOT_STATUS_DELETE) {
8921 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8922 goto done;
8925 a->have_changes = 1;
8927 p = in_repo_path;
8928 while (p[0] == '/')
8929 p++;
8930 err = check_out_of_date(p, status, staged_status,
8931 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8932 GOT_ERR_STAGE_OUT_OF_DATE);
8933 done:
8934 free(in_repo_path);
8935 return err;
8938 struct stage_path_arg {
8939 struct got_worktree *worktree;
8940 struct got_fileindex *fileindex;
8941 struct got_repository *repo;
8942 got_worktree_status_cb status_cb;
8943 void *status_arg;
8944 got_worktree_patch_cb patch_cb;
8945 void *patch_arg;
8946 int staged_something;
8947 int allow_bad_symlinks;
8950 static const struct got_error *
8951 stage_path(void *arg, unsigned char status,
8952 unsigned char staged_status, const char *relpath,
8953 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8954 struct got_object_id *commit_id, int dirfd, const char *de_name)
8956 struct stage_path_arg *a = arg;
8957 const struct got_error *err = NULL;
8958 struct got_fileindex_entry *ie;
8959 char *ondisk_path = NULL, *path_content = NULL;
8960 uint32_t stage;
8961 struct got_object_id *new_staged_blob_id = NULL;
8962 struct stat sb;
8964 if (status == GOT_STATUS_UNVERSIONED)
8965 return NULL;
8967 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8968 if (ie == NULL)
8969 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8971 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8972 relpath)== -1)
8973 return got_error_from_errno("asprintf");
8975 switch (status) {
8976 case GOT_STATUS_ADD:
8977 case GOT_STATUS_MODIFY:
8978 /* XXX could sb.st_mode be passed in by our caller? */
8979 if (lstat(ondisk_path, &sb) == -1) {
8980 err = got_error_from_errno2("lstat", ondisk_path);
8981 break;
8983 if (a->patch_cb) {
8984 if (status == GOT_STATUS_ADD) {
8985 int choice = GOT_PATCH_CHOICE_NONE;
8986 err = (*a->patch_cb)(&choice, a->patch_arg,
8987 status, ie->path, NULL, 1, 1);
8988 if (err)
8989 break;
8990 if (choice != GOT_PATCH_CHOICE_YES)
8991 break;
8992 } else {
8993 err = create_patched_content(&path_content, 0,
8994 staged_blob_id ? staged_blob_id : blob_id,
8995 ondisk_path, dirfd, de_name, ie->path,
8996 a->repo, a->patch_cb, a->patch_arg);
8997 if (err || path_content == NULL)
8998 break;
9001 err = got_object_blob_create(&new_staged_blob_id,
9002 path_content ? path_content : ondisk_path, a->repo);
9003 if (err)
9004 break;
9005 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9006 SHA1_DIGEST_LENGTH);
9007 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9008 stage = GOT_FILEIDX_STAGE_ADD;
9009 else
9010 stage = GOT_FILEIDX_STAGE_MODIFY;
9011 got_fileindex_entry_stage_set(ie, stage);
9012 if (S_ISLNK(sb.st_mode)) {
9013 int is_bad_symlink = 0;
9014 if (!a->allow_bad_symlinks) {
9015 char target_path[PATH_MAX];
9016 ssize_t target_len;
9017 target_len = readlink(ondisk_path, target_path,
9018 sizeof(target_path));
9019 if (target_len == -1) {
9020 err = got_error_from_errno2("readlink",
9021 ondisk_path);
9022 break;
9024 err = is_bad_symlink_target(&is_bad_symlink,
9025 target_path, target_len, ondisk_path,
9026 a->worktree->root_path,
9027 a->worktree->meta_dir);
9028 if (err)
9029 break;
9030 if (is_bad_symlink) {
9031 err = got_error_path(ondisk_path,
9032 GOT_ERR_BAD_SYMLINK);
9033 break;
9036 if (is_bad_symlink)
9037 got_fileindex_entry_staged_filetype_set(ie,
9038 GOT_FILEIDX_MODE_BAD_SYMLINK);
9039 else
9040 got_fileindex_entry_staged_filetype_set(ie,
9041 GOT_FILEIDX_MODE_SYMLINK);
9042 } else {
9043 got_fileindex_entry_staged_filetype_set(ie,
9044 GOT_FILEIDX_MODE_REGULAR_FILE);
9046 a->staged_something = 1;
9047 if (a->status_cb == NULL)
9048 break;
9049 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9050 get_staged_status(ie), relpath, blob_id,
9051 new_staged_blob_id, NULL, dirfd, de_name);
9052 if (err)
9053 break;
9055 * When staging the reverse of the staged diff,
9056 * implicitly unstage the file.
9058 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9059 sizeof(ie->blob_sha1)) == 0) {
9060 got_fileindex_entry_stage_set(ie,
9061 GOT_FILEIDX_STAGE_NONE);
9063 break;
9064 case GOT_STATUS_DELETE:
9065 if (staged_status == GOT_STATUS_DELETE)
9066 break;
9067 if (a->patch_cb) {
9068 int choice = GOT_PATCH_CHOICE_NONE;
9069 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9070 ie->path, NULL, 1, 1);
9071 if (err)
9072 break;
9073 if (choice == GOT_PATCH_CHOICE_NO)
9074 break;
9075 if (choice != GOT_PATCH_CHOICE_YES) {
9076 err = got_error(GOT_ERR_PATCH_CHOICE);
9077 break;
9080 stage = GOT_FILEIDX_STAGE_DELETE;
9081 got_fileindex_entry_stage_set(ie, stage);
9082 a->staged_something = 1;
9083 if (a->status_cb == NULL)
9084 break;
9085 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9086 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9087 de_name);
9088 break;
9089 case GOT_STATUS_NO_CHANGE:
9090 break;
9091 case GOT_STATUS_CONFLICT:
9092 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9093 break;
9094 case GOT_STATUS_NONEXISTENT:
9095 err = got_error_set_errno(ENOENT, relpath);
9096 break;
9097 default:
9098 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9099 break;
9102 if (path_content && unlink(path_content) == -1 && err == NULL)
9103 err = got_error_from_errno2("unlink", path_content);
9104 free(path_content);
9105 free(ondisk_path);
9106 free(new_staged_blob_id);
9107 return err;
9110 const struct got_error *
9111 got_worktree_stage(struct got_worktree *worktree,
9112 struct got_pathlist_head *paths,
9113 got_worktree_status_cb status_cb, void *status_arg,
9114 got_worktree_patch_cb patch_cb, void *patch_arg,
9115 int allow_bad_symlinks, struct got_repository *repo)
9117 const struct got_error *err = NULL, *sync_err, *unlockerr;
9118 struct got_pathlist_entry *pe;
9119 struct got_fileindex *fileindex = NULL;
9120 char *fileindex_path = NULL;
9121 struct got_reference *head_ref = NULL;
9122 struct got_object_id *head_commit_id = NULL;
9123 struct check_stage_ok_arg oka;
9124 struct stage_path_arg spa;
9126 err = lock_worktree(worktree, LOCK_EX);
9127 if (err)
9128 return err;
9130 err = got_ref_open(&head_ref, repo,
9131 got_worktree_get_head_ref_name(worktree), 0);
9132 if (err)
9133 goto done;
9134 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9135 if (err)
9136 goto done;
9137 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9138 if (err)
9139 goto done;
9141 /* Check pre-conditions before staging anything. */
9142 oka.head_commit_id = head_commit_id;
9143 oka.worktree = worktree;
9144 oka.fileindex = fileindex;
9145 oka.repo = repo;
9146 oka.have_changes = 0;
9147 TAILQ_FOREACH(pe, paths, entry) {
9148 err = worktree_status(worktree, pe->path, fileindex, repo,
9149 check_stage_ok, &oka, NULL, NULL, 1, 0);
9150 if (err)
9151 goto done;
9153 if (!oka.have_changes) {
9154 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9155 goto done;
9158 spa.worktree = worktree;
9159 spa.fileindex = fileindex;
9160 spa.repo = repo;
9161 spa.patch_cb = patch_cb;
9162 spa.patch_arg = patch_arg;
9163 spa.status_cb = status_cb;
9164 spa.status_arg = status_arg;
9165 spa.staged_something = 0;
9166 spa.allow_bad_symlinks = allow_bad_symlinks;
9167 TAILQ_FOREACH(pe, paths, entry) {
9168 err = worktree_status(worktree, pe->path, fileindex, repo,
9169 stage_path, &spa, NULL, NULL, 1, 0);
9170 if (err)
9171 goto done;
9173 if (!spa.staged_something) {
9174 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9175 goto done;
9178 sync_err = sync_fileindex(fileindex, fileindex_path);
9179 if (sync_err && err == NULL)
9180 err = sync_err;
9181 done:
9182 if (head_ref)
9183 got_ref_close(head_ref);
9184 free(head_commit_id);
9185 free(fileindex_path);
9186 if (fileindex)
9187 got_fileindex_free(fileindex);
9188 unlockerr = lock_worktree(worktree, LOCK_SH);
9189 if (unlockerr && err == NULL)
9190 err = unlockerr;
9191 return err;
9194 struct unstage_path_arg {
9195 struct got_worktree *worktree;
9196 struct got_fileindex *fileindex;
9197 struct got_repository *repo;
9198 got_worktree_checkout_cb progress_cb;
9199 void *progress_arg;
9200 got_worktree_patch_cb patch_cb;
9201 void *patch_arg;
9204 static const struct got_error *
9205 create_unstaged_content(char **path_unstaged_content,
9206 char **path_new_staged_content, struct got_object_id *blob_id,
9207 struct got_object_id *staged_blob_id, const char *relpath,
9208 struct got_repository *repo,
9209 got_worktree_patch_cb patch_cb, void *patch_arg)
9211 const struct got_error *err, *free_err;
9212 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9213 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9214 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9215 struct got_diffreg_result *diffreg_result = NULL;
9216 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9217 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9218 int fd1 = -1, fd2 = -1;
9220 *path_unstaged_content = NULL;
9221 *path_new_staged_content = NULL;
9223 err = got_object_id_str(&label1, blob_id);
9224 if (err)
9225 return err;
9227 fd1 = got_opentempfd();
9228 if (fd1 == -1) {
9229 err = got_error_from_errno("got_opentempfd");
9230 goto done;
9232 fd2 = got_opentempfd();
9233 if (fd2 == -1) {
9234 err = got_error_from_errno("got_opentempfd");
9235 goto done;
9238 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9239 if (err)
9240 goto done;
9242 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9243 if (err)
9244 goto done;
9246 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9247 if (err)
9248 goto done;
9250 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9251 fd2);
9252 if (err)
9253 goto done;
9255 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9256 if (err)
9257 goto done;
9259 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9260 if (err)
9261 goto done;
9263 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9264 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9265 if (err)
9266 goto done;
9268 err = got_opentemp_named(path_unstaged_content, &outfile,
9269 "got-unstaged-content", "");
9270 if (err)
9271 goto done;
9272 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9273 "got-new-staged-content", "");
9274 if (err)
9275 goto done;
9277 if (fseek(f1, 0L, SEEK_SET) == -1) {
9278 err = got_ferror(f1, GOT_ERR_IO);
9279 goto done;
9281 if (fseek(f2, 0L, SEEK_SET) == -1) {
9282 err = got_ferror(f2, GOT_ERR_IO);
9283 goto done;
9285 /* Count the number of actual changes in the diff result. */
9286 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9287 struct diff_chunk_context cc = {};
9288 diff_chunk_context_load_change(&cc, &nchunks_used,
9289 diffreg_result->result, n, 0);
9290 nchanges++;
9292 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9293 int choice;
9294 err = apply_or_reject_change(&choice, &nchunks_used,
9295 diffreg_result->result, n, relpath, f1, f2,
9296 &line_cur1, &line_cur2,
9297 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9298 if (err)
9299 goto done;
9300 if (choice == GOT_PATCH_CHOICE_YES)
9301 have_content = 1;
9302 else
9303 have_rejected_content = 1;
9304 if (choice == GOT_PATCH_CHOICE_QUIT)
9305 break;
9307 if (have_content || have_rejected_content)
9308 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9309 outfile, rejectfile);
9310 done:
9311 free(label1);
9312 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9313 err = got_error_from_errno("close");
9314 if (blob)
9315 got_object_blob_close(blob);
9316 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9317 err = got_error_from_errno("close");
9318 if (staged_blob)
9319 got_object_blob_close(staged_blob);
9320 free_err = got_diffreg_result_free(diffreg_result);
9321 if (free_err && err == NULL)
9322 err = free_err;
9323 if (f1 && fclose(f1) == EOF && err == NULL)
9324 err = got_error_from_errno2("fclose", path1);
9325 if (f2 && fclose(f2) == EOF && err == NULL)
9326 err = got_error_from_errno2("fclose", path2);
9327 if (outfile && fclose(outfile) == EOF && err == NULL)
9328 err = got_error_from_errno2("fclose", *path_unstaged_content);
9329 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9330 err = got_error_from_errno2("fclose", *path_new_staged_content);
9331 if (path1 && unlink(path1) == -1 && err == NULL)
9332 err = got_error_from_errno2("unlink", path1);
9333 if (path2 && unlink(path2) == -1 && err == NULL)
9334 err = got_error_from_errno2("unlink", path2);
9335 if (err || !have_content) {
9336 if (*path_unstaged_content &&
9337 unlink(*path_unstaged_content) == -1 && err == NULL)
9338 err = got_error_from_errno2("unlink",
9339 *path_unstaged_content);
9340 free(*path_unstaged_content);
9341 *path_unstaged_content = NULL;
9343 if (err || !have_content || !have_rejected_content) {
9344 if (*path_new_staged_content &&
9345 unlink(*path_new_staged_content) == -1 && err == NULL)
9346 err = got_error_from_errno2("unlink",
9347 *path_new_staged_content);
9348 free(*path_new_staged_content);
9349 *path_new_staged_content = NULL;
9351 free(path1);
9352 free(path2);
9353 return err;
9356 static const struct got_error *
9357 unstage_hunks(struct got_object_id *staged_blob_id,
9358 struct got_blob_object *blob_base,
9359 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9360 const char *ondisk_path, const char *label_orig,
9361 struct got_worktree *worktree, struct got_repository *repo,
9362 got_worktree_patch_cb patch_cb, void *patch_arg,
9363 got_worktree_checkout_cb progress_cb, void *progress_arg)
9365 const struct got_error *err = NULL;
9366 char *path_unstaged_content = NULL;
9367 char *path_new_staged_content = NULL;
9368 char *parent = NULL, *base_path = NULL;
9369 char *blob_base_path = NULL;
9370 struct got_object_id *new_staged_blob_id = NULL;
9371 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9372 struct stat sb;
9374 err = create_unstaged_content(&path_unstaged_content,
9375 &path_new_staged_content, blob_id, staged_blob_id,
9376 ie->path, repo, patch_cb, patch_arg);
9377 if (err)
9378 return err;
9380 if (path_unstaged_content == NULL)
9381 return NULL;
9383 if (path_new_staged_content) {
9384 err = got_object_blob_create(&new_staged_blob_id,
9385 path_new_staged_content, repo);
9386 if (err)
9387 goto done;
9390 f = fopen(path_unstaged_content, "re");
9391 if (f == NULL) {
9392 err = got_error_from_errno2("fopen",
9393 path_unstaged_content);
9394 goto done;
9396 if (fstat(fileno(f), &sb) == -1) {
9397 err = got_error_from_errno2("fstat", path_unstaged_content);
9398 goto done;
9400 if (got_fileindex_entry_staged_filetype_get(ie) ==
9401 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9402 char link_target[PATH_MAX];
9403 size_t r;
9404 r = fread(link_target, 1, sizeof(link_target), f);
9405 if (r == 0 && ferror(f)) {
9406 err = got_error_from_errno("fread");
9407 goto done;
9409 if (r >= sizeof(link_target)) { /* should not happen */
9410 err = got_error(GOT_ERR_NO_SPACE);
9411 goto done;
9413 link_target[r] = '\0';
9414 err = merge_symlink(worktree, blob_base,
9415 ondisk_path, ie->path, label_orig, link_target,
9416 worktree->base_commit_id, repo, progress_cb,
9417 progress_arg);
9418 } else {
9419 int local_changes_subsumed;
9421 err = got_path_dirname(&parent, ondisk_path);
9422 if (err)
9423 return err;
9425 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9426 parent) == -1) {
9427 err = got_error_from_errno("asprintf");
9428 base_path = NULL;
9429 goto done;
9432 err = got_opentemp_named(&blob_base_path, &f_base,
9433 base_path, "");
9434 if (err)
9435 goto done;
9436 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9437 blob_base);
9438 if (err)
9439 goto done;
9442 * In order the run a 3-way merge with a symlink we copy the symlink's
9443 * target path into a temporary file and use that file with diff3.
9445 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9446 err = dump_symlink_target_path_to_file(&f_deriv2,
9447 ondisk_path);
9448 if (err)
9449 goto done;
9450 } else {
9451 int fd;
9452 fd = open(ondisk_path,
9453 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9454 if (fd == -1) {
9455 err = got_error_from_errno2("open", ondisk_path);
9456 goto done;
9458 f_deriv2 = fdopen(fd, "r");
9459 if (f_deriv2 == NULL) {
9460 err = got_error_from_errno2("fdopen", ondisk_path);
9461 close(fd);
9462 goto done;
9466 err = merge_file(&local_changes_subsumed, worktree,
9467 f_base, f, f_deriv2, ondisk_path, ie->path,
9468 got_fileindex_perms_to_st(ie),
9469 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9470 repo, progress_cb, progress_arg);
9472 if (err)
9473 goto done;
9475 if (new_staged_blob_id) {
9476 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9477 SHA1_DIGEST_LENGTH);
9478 } else {
9479 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9480 got_fileindex_entry_staged_filetype_set(ie, 0);
9482 done:
9483 free(new_staged_blob_id);
9484 if (path_unstaged_content &&
9485 unlink(path_unstaged_content) == -1 && err == NULL)
9486 err = got_error_from_errno2("unlink", path_unstaged_content);
9487 if (path_new_staged_content &&
9488 unlink(path_new_staged_content) == -1 && err == NULL)
9489 err = got_error_from_errno2("unlink", path_new_staged_content);
9490 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9491 err = got_error_from_errno2("unlink", blob_base_path);
9492 if (f_base && fclose(f_base) == EOF && err == NULL)
9493 err = got_error_from_errno2("fclose", path_unstaged_content);
9494 if (f && fclose(f) == EOF && err == NULL)
9495 err = got_error_from_errno2("fclose", path_unstaged_content);
9496 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9497 err = got_error_from_errno2("fclose", ondisk_path);
9498 free(path_unstaged_content);
9499 free(path_new_staged_content);
9500 free(blob_base_path);
9501 free(parent);
9502 free(base_path);
9503 return err;
9506 static const struct got_error *
9507 unstage_path(void *arg, unsigned char status,
9508 unsigned char staged_status, const char *relpath,
9509 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9510 struct got_object_id *commit_id, int dirfd, const char *de_name)
9512 const struct got_error *err = NULL;
9513 struct unstage_path_arg *a = arg;
9514 struct got_fileindex_entry *ie;
9515 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9516 char *ondisk_path = NULL;
9517 char *id_str = NULL, *label_orig = NULL;
9518 int local_changes_subsumed;
9519 struct stat sb;
9520 int fd1 = -1, fd2 = -1;
9522 if (staged_status != GOT_STATUS_ADD &&
9523 staged_status != GOT_STATUS_MODIFY &&
9524 staged_status != GOT_STATUS_DELETE)
9525 return NULL;
9527 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9528 if (ie == NULL)
9529 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9531 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9532 == -1)
9533 return got_error_from_errno("asprintf");
9535 err = got_object_id_str(&id_str,
9536 commit_id ? commit_id : a->worktree->base_commit_id);
9537 if (err)
9538 goto done;
9539 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9540 id_str) == -1) {
9541 err = got_error_from_errno("asprintf");
9542 goto done;
9545 fd1 = got_opentempfd();
9546 if (fd1 == -1) {
9547 err = got_error_from_errno("got_opentempfd");
9548 goto done;
9550 fd2 = got_opentempfd();
9551 if (fd2 == -1) {
9552 err = got_error_from_errno("got_opentempfd");
9553 goto done;
9556 switch (staged_status) {
9557 case GOT_STATUS_MODIFY:
9558 err = got_object_open_as_blob(&blob_base, a->repo,
9559 blob_id, 8192, fd1);
9560 if (err)
9561 break;
9562 /* fall through */
9563 case GOT_STATUS_ADD:
9564 if (a->patch_cb) {
9565 if (staged_status == GOT_STATUS_ADD) {
9566 int choice = GOT_PATCH_CHOICE_NONE;
9567 err = (*a->patch_cb)(&choice, a->patch_arg,
9568 staged_status, ie->path, NULL, 1, 1);
9569 if (err)
9570 break;
9571 if (choice != GOT_PATCH_CHOICE_YES)
9572 break;
9573 } else {
9574 err = unstage_hunks(staged_blob_id,
9575 blob_base, blob_id, ie, ondisk_path,
9576 label_orig, a->worktree, a->repo,
9577 a->patch_cb, a->patch_arg,
9578 a->progress_cb, a->progress_arg);
9579 break; /* Done with this file. */
9582 err = got_object_open_as_blob(&blob_staged, a->repo,
9583 staged_blob_id, 8192, fd2);
9584 if (err)
9585 break;
9586 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9587 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9588 case GOT_FILEIDX_MODE_REGULAR_FILE:
9589 err = merge_blob(&local_changes_subsumed, a->worktree,
9590 blob_base, ondisk_path, relpath,
9591 got_fileindex_perms_to_st(ie), label_orig,
9592 blob_staged, commit_id ? commit_id :
9593 a->worktree->base_commit_id, a->repo,
9594 a->progress_cb, a->progress_arg);
9595 break;
9596 case GOT_FILEIDX_MODE_SYMLINK:
9597 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9598 char *staged_target;
9599 err = got_object_blob_read_to_str(
9600 &staged_target, blob_staged);
9601 if (err)
9602 goto done;
9603 err = merge_symlink(a->worktree, blob_base,
9604 ondisk_path, relpath, label_orig,
9605 staged_target, commit_id ? commit_id :
9606 a->worktree->base_commit_id,
9607 a->repo, a->progress_cb, a->progress_arg);
9608 free(staged_target);
9609 } else {
9610 err = merge_blob(&local_changes_subsumed,
9611 a->worktree, blob_base, ondisk_path,
9612 relpath, got_fileindex_perms_to_st(ie),
9613 label_orig, blob_staged,
9614 commit_id ? commit_id :
9615 a->worktree->base_commit_id, a->repo,
9616 a->progress_cb, a->progress_arg);
9618 break;
9619 default:
9620 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9621 break;
9623 if (err == NULL) {
9624 got_fileindex_entry_stage_set(ie,
9625 GOT_FILEIDX_STAGE_NONE);
9626 got_fileindex_entry_staged_filetype_set(ie, 0);
9628 break;
9629 case GOT_STATUS_DELETE:
9630 if (a->patch_cb) {
9631 int choice = GOT_PATCH_CHOICE_NONE;
9632 err = (*a->patch_cb)(&choice, a->patch_arg,
9633 staged_status, ie->path, NULL, 1, 1);
9634 if (err)
9635 break;
9636 if (choice == GOT_PATCH_CHOICE_NO)
9637 break;
9638 if (choice != GOT_PATCH_CHOICE_YES) {
9639 err = got_error(GOT_ERR_PATCH_CHOICE);
9640 break;
9643 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9644 got_fileindex_entry_staged_filetype_set(ie, 0);
9645 err = get_file_status(&status, &sb, ie, ondisk_path,
9646 dirfd, de_name, a->repo);
9647 if (err)
9648 break;
9649 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9650 break;
9652 done:
9653 free(ondisk_path);
9654 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9655 err = got_error_from_errno("close");
9656 if (blob_base)
9657 got_object_blob_close(blob_base);
9658 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9659 err = got_error_from_errno("close");
9660 if (blob_staged)
9661 got_object_blob_close(blob_staged);
9662 free(id_str);
9663 free(label_orig);
9664 return err;
9667 const struct got_error *
9668 got_worktree_unstage(struct got_worktree *worktree,
9669 struct got_pathlist_head *paths,
9670 got_worktree_checkout_cb progress_cb, void *progress_arg,
9671 got_worktree_patch_cb patch_cb, void *patch_arg,
9672 struct got_repository *repo)
9674 const struct got_error *err = NULL, *sync_err, *unlockerr;
9675 struct got_pathlist_entry *pe;
9676 struct got_fileindex *fileindex = NULL;
9677 char *fileindex_path = NULL;
9678 struct unstage_path_arg upa;
9680 err = lock_worktree(worktree, LOCK_EX);
9681 if (err)
9682 return err;
9684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9685 if (err)
9686 goto done;
9688 upa.worktree = worktree;
9689 upa.fileindex = fileindex;
9690 upa.repo = repo;
9691 upa.progress_cb = progress_cb;
9692 upa.progress_arg = progress_arg;
9693 upa.patch_cb = patch_cb;
9694 upa.patch_arg = patch_arg;
9695 TAILQ_FOREACH(pe, paths, entry) {
9696 err = worktree_status(worktree, pe->path, fileindex, repo,
9697 unstage_path, &upa, NULL, NULL, 1, 0);
9698 if (err)
9699 goto done;
9702 sync_err = sync_fileindex(fileindex, fileindex_path);
9703 if (sync_err && err == NULL)
9704 err = sync_err;
9705 done:
9706 free(fileindex_path);
9707 if (fileindex)
9708 got_fileindex_free(fileindex);
9709 unlockerr = lock_worktree(worktree, LOCK_SH);
9710 if (unlockerr && err == NULL)
9711 err = unlockerr;
9712 return err;
9715 struct report_file_info_arg {
9716 struct got_worktree *worktree;
9717 got_worktree_path_info_cb info_cb;
9718 void *info_arg;
9719 struct got_pathlist_head *paths;
9720 got_cancel_cb cancel_cb;
9721 void *cancel_arg;
9724 static const struct got_error *
9725 report_file_info(void *arg, struct got_fileindex_entry *ie)
9727 struct report_file_info_arg *a = arg;
9728 struct got_pathlist_entry *pe;
9729 struct got_object_id blob_id, staged_blob_id, commit_id;
9730 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9731 struct got_object_id *commit_idp = NULL;
9732 int stage;
9734 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9735 return got_error(GOT_ERR_CANCELLED);
9737 TAILQ_FOREACH(pe, a->paths, entry) {
9738 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9739 got_path_is_child(ie->path, pe->path, pe->path_len))
9740 break;
9742 if (pe == NULL) /* not found */
9743 return NULL;
9745 if (got_fileindex_entry_has_blob(ie))
9746 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9747 stage = got_fileindex_entry_stage_get(ie);
9748 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9749 stage == GOT_FILEIDX_STAGE_ADD) {
9750 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9751 &staged_blob_id, ie);
9754 if (got_fileindex_entry_has_commit(ie))
9755 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9757 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9758 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9761 const struct got_error *
9762 got_worktree_path_info(struct got_worktree *worktree,
9763 struct got_pathlist_head *paths,
9764 got_worktree_path_info_cb info_cb, void *info_arg,
9765 got_cancel_cb cancel_cb, void *cancel_arg)
9768 const struct got_error *err = NULL, *unlockerr;
9769 struct got_fileindex *fileindex = NULL;
9770 char *fileindex_path = NULL;
9771 struct report_file_info_arg arg;
9773 err = lock_worktree(worktree, LOCK_SH);
9774 if (err)
9775 return err;
9777 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9778 if (err)
9779 goto done;
9781 arg.worktree = worktree;
9782 arg.info_cb = info_cb;
9783 arg.info_arg = info_arg;
9784 arg.paths = paths;
9785 arg.cancel_cb = cancel_cb;
9786 arg.cancel_arg = cancel_arg;
9787 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9788 &arg);
9789 done:
9790 free(fileindex_path);
9791 if (fileindex)
9792 got_fileindex_free(fileindex);
9793 unlockerr = lock_worktree(worktree, LOCK_UN);
9794 if (unlockerr && err == NULL)
9795 err = unlockerr;
9796 return err;
9799 static const struct got_error *
9800 patch_check_path(const char *p, char **path, unsigned char *status,
9801 unsigned char *staged_status, struct got_fileindex *fileindex,
9802 struct got_worktree *worktree, struct got_repository *repo)
9804 const struct got_error *err;
9805 struct got_fileindex_entry *ie;
9806 struct stat sb;
9807 char *ondisk_path = NULL;
9809 err = got_worktree_resolve_path(path, worktree, p);
9810 if (err)
9811 return err;
9813 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9814 *path[0] ? "/" : "", *path) == -1)
9815 return got_error_from_errno("asprintf");
9817 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9818 if (ie) {
9819 *staged_status = get_staged_status(ie);
9820 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9821 repo);
9822 if (err)
9823 goto done;
9824 } else {
9825 *staged_status = GOT_STATUS_NO_CHANGE;
9826 *status = GOT_STATUS_UNVERSIONED;
9827 if (lstat(ondisk_path, &sb) == -1) {
9828 if (errno != ENOENT) {
9829 err = got_error_from_errno2("lstat",
9830 ondisk_path);
9831 goto done;
9833 *status = GOT_STATUS_NONEXISTENT;
9837 done:
9838 free(ondisk_path);
9839 return err;
9842 static const struct got_error *
9843 patch_can_rm(const char *path, unsigned char status,
9844 unsigned char staged_status)
9846 if (status == GOT_STATUS_NONEXISTENT)
9847 return got_error_set_errno(ENOENT, path);
9848 if (status != GOT_STATUS_NO_CHANGE &&
9849 status != GOT_STATUS_ADD &&
9850 status != GOT_STATUS_MODIFY &&
9851 status != GOT_STATUS_MODE_CHANGE)
9852 return got_error_path(path, GOT_ERR_FILE_STATUS);
9853 if (staged_status == GOT_STATUS_DELETE)
9854 return got_error_path(path, GOT_ERR_FILE_STATUS);
9855 return NULL;
9858 static const struct got_error *
9859 patch_can_add(const char *path, unsigned char status)
9861 if (status != GOT_STATUS_NONEXISTENT)
9862 return got_error_path(path, GOT_ERR_FILE_STATUS);
9863 return NULL;
9866 static const struct got_error *
9867 patch_can_edit(const char *path, unsigned char status,
9868 unsigned char staged_status)
9870 if (status == GOT_STATUS_NONEXISTENT)
9871 return got_error_set_errno(ENOENT, path);
9872 if (status != GOT_STATUS_NO_CHANGE &&
9873 status != GOT_STATUS_ADD &&
9874 status != GOT_STATUS_MODIFY)
9875 return got_error_path(path, GOT_ERR_FILE_STATUS);
9876 if (staged_status == GOT_STATUS_DELETE)
9877 return got_error_path(path, GOT_ERR_FILE_STATUS);
9878 return NULL;
9881 const struct got_error *
9882 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9883 char **fileindex_path, struct got_worktree *worktree)
9885 return open_fileindex(fileindex, fileindex_path, worktree);
9888 const struct got_error *
9889 got_worktree_patch_check_path(const char *old, const char *new,
9890 char **oldpath, char **newpath, struct got_worktree *worktree,
9891 struct got_repository *repo, struct got_fileindex *fileindex)
9893 const struct got_error *err = NULL;
9894 int file_renamed = 0;
9895 unsigned char status_old, staged_status_old;
9896 unsigned char status_new, staged_status_new;
9898 *oldpath = NULL;
9899 *newpath = NULL;
9901 err = patch_check_path(old != NULL ? old : new, oldpath,
9902 &status_old, &staged_status_old, fileindex, worktree, repo);
9903 if (err)
9904 goto done;
9906 err = patch_check_path(new != NULL ? new : old, newpath,
9907 &status_new, &staged_status_new, fileindex, worktree, repo);
9908 if (err)
9909 goto done;
9911 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9912 file_renamed = 1;
9914 if (old != NULL && new == NULL)
9915 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9916 else if (file_renamed) {
9917 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9918 if (err == NULL)
9919 err = patch_can_add(*newpath, status_new);
9920 } else if (old == NULL)
9921 err = patch_can_add(*newpath, status_new);
9922 else
9923 err = patch_can_edit(*newpath, status_new, staged_status_new);
9925 done:
9926 if (err) {
9927 free(*oldpath);
9928 *oldpath = NULL;
9929 free(*newpath);
9930 *newpath = NULL;
9932 return err;
9935 const struct got_error *
9936 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9937 struct got_worktree *worktree, struct got_fileindex *fileindex,
9938 got_worktree_checkout_cb progress_cb, void *progress_arg)
9940 struct schedule_addition_args saa;
9942 memset(&saa, 0, sizeof(saa));
9943 saa.worktree = worktree;
9944 saa.fileindex = fileindex;
9945 saa.progress_cb = progress_cb;
9946 saa.progress_arg = progress_arg;
9947 saa.repo = repo;
9949 return worktree_status(worktree, path, fileindex, repo,
9950 schedule_addition, &saa, NULL, NULL, 1, 0);
9953 const struct got_error *
9954 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9955 struct got_worktree *worktree, struct got_fileindex *fileindex,
9956 got_worktree_delete_cb progress_cb, void *progress_arg)
9958 const struct got_error *err;
9959 struct schedule_deletion_args sda;
9960 char *ondisk_status_path;
9962 memset(&sda, 0, sizeof(sda));
9963 sda.worktree = worktree;
9964 sda.fileindex = fileindex;
9965 sda.progress_cb = progress_cb;
9966 sda.progress_arg = progress_arg;
9967 sda.repo = repo;
9968 sda.delete_local_mods = 0;
9969 sda.keep_on_disk = 0;
9970 sda.ignore_missing_paths = 0;
9971 sda.status_codes = NULL;
9972 if (asprintf(&ondisk_status_path, "%s/%s",
9973 got_worktree_get_root_path(worktree), path) == -1)
9974 return got_error_from_errno("asprintf");
9975 sda.status_path = ondisk_status_path;
9976 sda.status_path_len = strlen(ondisk_status_path);
9978 err = worktree_status(worktree, path, fileindex, repo,
9979 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9980 free(ondisk_status_path);
9981 return err;
9984 const struct got_error *
9985 got_worktree_patch_complete(struct got_fileindex *fileindex,
9986 const char *fileindex_path)
9988 const struct got_error *err = NULL;
9990 err = sync_fileindex(fileindex, fileindex_path);
9991 got_fileindex_free(fileindex);
9993 return err;