Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t
66 apply_umask(mode_t mode)
67 {
68 mode_t um;
70 um = umask(000);
71 umask(um);
72 return mode & ~um;
73 }
75 static const struct got_error *
76 create_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 char *path;
81 if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 return got_error_from_errno("asprintf");
84 err = got_path_create_file(path, content);
85 free(path);
86 return err;
87 }
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
91 {
92 const struct got_error *err = NULL;
93 FILE *tmpfile = NULL;
94 char *tmppath = NULL;
95 char *path = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno("asprintf");
99 path = NULL;
100 goto done;
103 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
104 if (err)
105 goto done;
107 if (content) {
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno2("fprintf", tmppath);
111 goto done;
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno3("rename", tmppath, path);
117 unlink(tmppath);
118 goto done;
121 done:
122 if (fclose(tmpfile) == EOF && err == NULL)
123 err = got_error_from_errno2("fclose", tmppath);
124 free(tmppath);
125 return err;
128 static const struct got_error *
129 write_head_ref(const char *path_got, struct got_reference *head_ref)
131 const struct got_error *err = NULL;
132 char *refstr = NULL;
134 if (got_ref_is_symbolic(head_ref)) {
135 refstr = got_ref_to_str(head_ref);
136 if (refstr == NULL)
137 return got_error_from_errno("got_ref_to_str");
138 } else {
139 refstr = strdup(got_ref_get_name(head_ref));
140 if (refstr == NULL)
141 return got_error_from_errno("strdup");
143 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 free(refstr);
145 return err;
148 const struct got_error *
149 got_worktree_init(const char *path, struct got_reference *head_ref,
150 const char *prefix, const char *meta_dir, struct got_repository *repo)
152 const struct got_error *err = NULL;
153 struct got_object_id *commit_id = NULL;
154 uuid_t uuid;
155 uint32_t uuid_status;
156 int obj_type;
157 char *path_got = NULL;
158 char *formatstr = NULL;
159 char *absprefix = NULL;
160 char *basestr = NULL;
161 char *uuidstr = NULL;
163 if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 err = got_error(GOT_ERR_WORKTREE_REPO);
165 goto done;
168 err = got_ref_resolve(&commit_id, repo, head_ref);
169 if (err)
170 return err;
171 err = got_object_get_type(&obj_type, repo, commit_id);
172 if (err)
173 return err;
174 if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 return got_error(GOT_ERR_OBJ_TYPE);
177 if (!got_path_is_absolute(prefix)) {
178 if (asprintf(&absprefix, "/%s", prefix) == -1)
179 return got_error_from_errno("asprintf");
182 /* Create top-level directory (may already exist). */
183 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path);
185 goto done;
188 /* Create .got/.cvg directory (may already exist). */
189 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
190 err = got_error_from_errno("asprintf");
191 goto done;
193 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 err = got_error_from_errno2("mkdir", path_got);
195 goto done;
198 /* Create an empty lock file. */
199 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
200 if (err)
201 goto done;
203 /* Create an empty file index. */
204 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
205 if (err)
206 goto done;
208 /* Write the HEAD reference. */
209 err = write_head_ref(path_got, head_ref);
210 if (err)
211 goto done;
213 /* Record our base commit. */
214 err = got_object_id_str(&basestr, commit_id);
215 if (err)
216 goto done;
217 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
218 if (err)
219 goto done;
221 /* Store path to repository. */
222 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 got_repo_get_path(repo));
224 if (err)
225 goto done;
227 /* Store in-repository path prefix. */
228 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 absprefix ? absprefix : prefix);
230 if (err)
231 goto done;
233 /* Generate UUID. */
234 uuid_create(&uuid, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_create");
237 goto done;
239 uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 if (uuid_status != uuid_s_ok) {
241 err = got_error_uuid(uuid_status, "uuid_to_string");
242 goto done;
244 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
245 if (err)
246 goto done;
248 /* Stamp work tree with format file. */
249 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 err = got_error_from_errno("asprintf");
251 goto done;
253 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
254 if (err)
255 goto done;
257 done:
258 free(commit_id);
259 free(path_got);
260 free(formatstr);
261 free(absprefix);
262 free(basestr);
263 free(uuidstr);
264 return err;
267 const struct got_error *
268 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 const char *path_prefix)
271 char *absprefix = NULL;
273 if (!got_path_is_absolute(path_prefix)) {
274 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 return got_error_from_errno("asprintf");
277 *match = (strcmp(absprefix ? absprefix : path_prefix,
278 worktree->path_prefix) == 0);
279 free(absprefix);
280 return NULL;
283 const char *
284 got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 return worktree->head_ref_name;
289 const struct got_error *
290 got_worktree_set_head_ref(struct got_worktree *worktree,
291 struct got_reference *head_ref)
293 const struct got_error *err = NULL;
294 char *path_got = NULL, *head_ref_name = NULL;
296 if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 worktree->meta_dir) == -1) {
298 err = got_error_from_errno("asprintf");
299 path_got = NULL;
300 goto done;
303 head_ref_name = strdup(got_ref_get_name(head_ref));
304 if (head_ref_name == NULL) {
305 err = got_error_from_errno("strdup");
306 goto done;
309 err = write_head_ref(path_got, head_ref);
310 if (err)
311 goto done;
313 free(worktree->head_ref_name);
314 worktree->head_ref_name = head_ref_name;
315 done:
316 free(path_got);
317 if (err)
318 free(head_ref_name);
319 return err;
322 struct got_object_id *
323 got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 return worktree->base_commit_id;
328 const struct got_error *
329 got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 struct got_repository *repo, struct got_object_id *commit_id)
332 const struct got_error *err;
333 struct got_object *obj = NULL;
334 char *id_str = NULL;
335 char *path_got = NULL;
337 if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 worktree->meta_dir) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 err = got_object_open(&obj, repo, commit_id);
345 if (err)
346 return err;
348 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 err = got_error(GOT_ERR_OBJ_TYPE);
350 goto done;
353 /* Record our base commit. */
354 err = got_object_id_str(&id_str, commit_id);
355 if (err)
356 goto done;
357 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
358 if (err)
359 goto done;
361 free(worktree->base_commit_id);
362 worktree->base_commit_id = got_object_id_dup(commit_id);
363 if (worktree->base_commit_id == NULL) {
364 err = got_error_from_errno("got_object_id_dup");
365 goto done;
367 done:
368 if (obj)
369 got_object_close(obj);
370 free(id_str);
371 free(path_got);
372 return err;
375 const struct got_gotconfig *
376 got_worktree_get_gotconfig(struct got_worktree *worktree)
378 return worktree->gotconfig;
381 static const struct got_error *
382 lock_worktree(struct got_worktree *worktree, int operation)
384 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 : got_error_from_errno2("flock",
387 got_worktree_get_root_path(worktree)));
388 return NULL;
391 static const struct got_error *
392 add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 const struct got_error *err = NULL;
395 char *abspath;
397 /* We only accept worktree-relative paths here. */
398 if (got_path_is_absolute(path)) {
399 return got_error_fmt(GOT_ERR_BAD_PATH,
400 "%s does not accept absolute paths: %s",
401 __func__, path);
404 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 return got_error_from_errno("asprintf");
407 err = got_path_mkdir(abspath);
408 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 struct stat sb;
410 err = NULL;
411 if (lstat(abspath, &sb) == -1) {
412 err = got_error_from_errno2("lstat", abspath);
413 } else if (!S_ISDIR(sb.st_mode)) {
414 /* TODO directory is obstructed; do something */
415 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
418 free(abspath);
419 return err;
422 static const struct got_error *
423 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
425 const struct got_error *err = NULL;
426 uint8_t fbuf1[8192];
427 uint8_t fbuf2[8192];
428 size_t flen1 = 0, flen2 = 0;
430 *same = 1;
432 for (;;) {
433 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 if (flen1 == 0 && ferror(f1)) {
435 err = got_error_from_errno("fread");
436 break;
438 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 if (flen2 == 0 && ferror(f2)) {
440 err = got_error_from_errno("fread");
441 break;
443 if (flen1 == 0) {
444 if (flen2 != 0)
445 *same = 0;
446 break;
447 } else if (flen2 == 0) {
448 if (flen1 != 0)
449 *same = 0;
450 break;
451 } else if (flen1 == flen2) {
452 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
453 *same = 0;
454 break;
456 } else {
457 *same = 0;
458 break;
462 return err;
465 static const struct got_error *
466 check_files_equal(int *same, FILE *f1, FILE *f2)
468 struct stat sb;
469 size_t size1, size2;
471 *same = 1;
473 if (fstat(fileno(f1), &sb) != 0)
474 return got_error_from_errno("fstat");
475 size1 = sb.st_size;
477 if (fstat(fileno(f2), &sb) != 0)
478 return got_error_from_errno("fstat");
479 size2 = sb.st_size;
481 if (size1 != size2) {
482 *same = 0;
483 return NULL;
486 if (fseek(f1, 0L, SEEK_SET) == -1)
487 return got_ferror(f1, GOT_ERR_IO);
488 if (fseek(f2, 0L, SEEK_SET) == -1)
489 return got_ferror(f2, GOT_ERR_IO);
491 return check_file_contents_equal(same, f1, f2);
494 static const struct got_error *
495 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
497 uint8_t fbuf[65536];
498 size_t flen;
499 ssize_t outlen;
501 *outsize = 0;
503 if (fseek(f, 0L, SEEK_SET) == -1)
504 return got_ferror(f, GOT_ERR_IO);
506 for (;;) {
507 flen = fread(fbuf, 1, sizeof(fbuf), f);
508 if (flen == 0) {
509 if (ferror(f))
510 return got_error_from_errno("fread");
511 if (feof(f))
512 break;
514 outlen = write(outfd, fbuf, flen);
515 if (outlen == -1)
516 return got_error_from_errno("write");
517 if (outlen != flen)
518 return got_error(GOT_ERR_IO);
519 *outsize += outlen;
522 return NULL;
525 static const struct got_error *
526 merge_binary_file(int *overlapcnt, int merged_fd,
527 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 const char *ondisk_path)
531 const struct got_error *err = NULL;
532 int same_content, changed_deriv, changed_deriv2;
533 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 char *base_path_orig = NULL, *base_path_deriv = NULL;
537 char *base_path_deriv2 = NULL;
539 *overlapcnt = 0;
541 err = check_files_equal(&same_content, f_deriv, f_deriv2);
542 if (err)
543 return err;
545 if (same_content)
546 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
548 err = check_files_equal(&same_content, f_deriv, f_orig);
549 if (err)
550 return err;
551 changed_deriv = !same_content;
552 err = check_files_equal(&same_content, f_deriv2, f_orig);
553 if (err)
554 return err;
555 changed_deriv2 = !same_content;
557 if (changed_deriv && changed_deriv2) {
558 *overlapcnt = 1;
559 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 err = got_error_from_errno("asprintf");
561 goto done;
563 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 err = got_error_from_errno("asprintf");
565 goto done;
567 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 err = got_error_from_errno("asprintf");
569 goto done;
571 err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 base_path_orig, "");
573 if (err)
574 goto done;
575 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 base_path_deriv, "");
577 if (err)
578 goto done;
579 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 base_path_deriv2, "");
581 if (err)
582 goto done;
583 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
590 if (err)
591 goto done;
592 if (dprintf(merged_fd, "Binary files differ and cannot be "
593 "merged automatically:\n") < 0) {
594 err = got_error_from_errno("dprintf");
595 goto done;
597 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 label_deriv ? " " : "",
600 label_deriv ? label_deriv : "",
601 path_deriv) < 0) {
602 err = got_error_from_errno("dprintf");
603 goto done;
605 if (size_orig > 0) {
606 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 GOT_DIFF_CONFLICT_MARKER_ORIG,
608 label_orig ? " " : "",
609 label_orig ? label_orig : "",
610 path_orig) < 0) {
611 err = got_error_from_errno("dprintf");
612 goto done;
615 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 GOT_DIFF_CONFLICT_MARKER_SEP,
617 path_deriv2,
618 GOT_DIFF_CONFLICT_MARKER_END,
619 label_deriv2 ? " " : "",
620 label_deriv2 ? label_deriv2 : "") < 0) {
621 err = got_error_from_errno("dprintf");
622 goto done;
624 } else if (changed_deriv)
625 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 else if (changed_deriv2)
627 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
628 done:
629 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 err == NULL)
631 err = got_error_from_errno2("unlink", path_orig);
632 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 err = got_error_from_errno2("close", path_orig);
634 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 err = got_error_from_errno2("close", path_deriv);
636 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 err = got_error_from_errno2("close", path_deriv2);
638 free(path_orig);
639 free(path_deriv);
640 free(path_deriv2);
641 free(base_path_orig);
642 free(base_path_deriv);
643 free(base_path_deriv2);
644 return err;
647 /*
648 * Perform a 3-way merge where the file f_orig acts as the common
649 * ancestor, the file f_deriv acts as the first derived version,
650 * and the file f_deriv2 acts as the second derived version.
651 * The merge result will be written to a new file at ondisk_path; any
652 * existing file at this path will be replaced.
653 */
654 static const struct got_error *
655 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 const char *path, uint16_t st_mode,
658 const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 got_worktree_checkout_cb progress_cb, void *progress_arg)
662 const struct got_error *err = NULL;
663 int merged_fd = -1;
664 FILE *f_merged = NULL;
665 char *merged_path = NULL, *base_path = NULL;
666 int overlapcnt = 0;
667 char *parent = NULL;
669 *local_changes_subsumed = 0;
671 err = got_path_dirname(&parent, ondisk_path);
672 if (err)
673 return err;
675 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 err = got_error_from_errno("asprintf");
677 goto done;
680 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
681 if (err)
682 goto done;
684 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 if (err) {
687 if (err->code != GOT_ERR_FILE_BINARY)
688 goto done;
689 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 ondisk_path);
692 if (err)
693 goto done;
696 err = (*progress_cb)(progress_arg,
697 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
698 if (err)
699 goto done;
701 if (fsync(merged_fd) != 0) {
702 err = got_error_from_errno("fsync");
703 goto done;
706 f_merged = fdopen(merged_fd, "r");
707 if (f_merged == NULL) {
708 err = got_error_from_errno("fdopen");
709 goto done;
711 merged_fd = -1;
713 /* Check if a clean merge has subsumed all local changes. */
714 if (overlapcnt == 0) {
715 err = check_files_equal(local_changes_subsumed, f_deriv,
716 f_merged);
717 if (err)
718 goto done;
721 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 err = got_error_from_errno2("fchmod", merged_path);
723 goto done;
726 if (rename(merged_path, ondisk_path) != 0) {
727 err = got_error_from_errno3("rename", merged_path,
728 ondisk_path);
729 goto done;
731 done:
732 if (err) {
733 if (merged_path)
734 unlink(merged_path);
736 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 err = got_error_from_errno("close");
738 if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 err = got_error_from_errno("fclose");
740 free(merged_path);
741 free(base_path);
742 free(parent);
743 return err;
746 static const struct got_error *
747 update_symlink(const char *ondisk_path, const char *target_path,
748 size_t target_len)
750 /* This is not atomic but matches what 'ln -sf' does. */
751 if (unlink(ondisk_path) == -1)
752 return got_error_from_errno2("unlink", ondisk_path);
753 if (symlink(target_path, ondisk_path) == -1)
754 return got_error_from_errno3("symlink", target_path,
755 ondisk_path);
756 return NULL;
759 /*
760 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 * in the work tree with a file that contains conflict markers and the
762 * conflicting target paths of the original version, a "derived version"
763 * of a symlink from an incoming change, and a local version of the symlink.
765 * The original versions's target path can be NULL if it is not available,
766 * such as if both derived versions added a new symlink at the same path.
768 * The incoming derived symlink target is NULL in case the incoming change
769 * has deleted this symlink.
770 */
771 static const struct got_error *
772 install_symlink_conflict(const char *deriv_target,
773 struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 const char *label_orig, const char *local_target, const char *ondisk_path)
776 const struct got_error *err;
777 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 FILE *f = NULL;
780 err = got_object_id_str(&id_str, deriv_base_commit_id);
781 if (err)
782 return got_error_from_errno("asprintf");
784 if (asprintf(&label_deriv, "%s: commit %s",
785 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 err = got_error_from_errno("asprintf");
787 goto done;
790 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
791 if (err)
792 goto done;
794 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 err = got_error_from_errno2("fchmod", path);
796 goto done;
799 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 deriv_target ? deriv_target : "(symlink was deleted)",
802 orig_target ? label_orig : "",
803 orig_target ? "\n" : "",
804 orig_target ? orig_target : "",
805 orig_target ? "\n" : "",
806 GOT_DIFF_CONFLICT_MARKER_SEP,
807 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 err = got_error_from_errno2("fprintf", path);
809 goto done;
812 if (unlink(ondisk_path) == -1) {
813 err = got_error_from_errno2("unlink", ondisk_path);
814 goto done;
816 if (rename(path, ondisk_path) == -1) {
817 err = got_error_from_errno3("rename", path, ondisk_path);
818 goto done;
820 done:
821 if (f != NULL && fclose(f) == EOF && err == NULL)
822 err = got_error_from_errno2("fclose", path);
823 free(path);
824 free(id_str);
825 free(label_deriv);
826 return err;
829 /* forward declaration */
830 static const struct got_error *
831 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 const char *, const char *, uint16_t, const char *,
833 struct got_blob_object *, struct got_object_id *,
834 struct got_repository *, got_worktree_checkout_cb, void *);
836 /*
837 * Merge a symlink into the work tree, where blob_orig acts as the common
838 * ancestor, deriv_target is the link target of the first derived version,
839 * and the symlink on disk acts as the second derived version.
840 * Assume that contents of both blobs represent symlinks.
841 */
842 static const struct got_error *
843 merge_symlink(struct got_worktree *worktree,
844 struct got_blob_object *blob_orig, const char *ondisk_path,
845 const char *path, const char *label_orig, const char *deriv_target,
846 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 got_worktree_checkout_cb progress_cb, void *progress_arg)
849 const struct got_error *err = NULL;
850 char *ancestor_target = NULL;
851 struct stat sb;
852 ssize_t ondisk_len, deriv_len;
853 char ondisk_target[PATH_MAX];
854 int have_local_change = 0;
855 int have_incoming_change = 0;
857 if (lstat(ondisk_path, &sb) == -1)
858 return got_error_from_errno2("lstat", ondisk_path);
860 ondisk_len = readlink(ondisk_path, ondisk_target,
861 sizeof(ondisk_target));
862 if (ondisk_len == -1) {
863 err = got_error_from_errno2("readlink",
864 ondisk_path);
865 goto done;
867 ondisk_target[ondisk_len] = '\0';
869 if (blob_orig) {
870 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
871 if (err)
872 goto done;
875 if (ancestor_target == NULL ||
876 (ondisk_len != strlen(ancestor_target) ||
877 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 have_local_change = 1;
880 deriv_len = strlen(deriv_target);
881 if (ancestor_target == NULL ||
882 (deriv_len != strlen(ancestor_target) ||
883 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 have_incoming_change = 1;
886 if (!have_local_change && !have_incoming_change) {
887 if (ancestor_target) {
888 /* Both sides made the same change. */
889 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
890 path);
891 } else if (deriv_len == ondisk_len &&
892 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 /* Both sides added the same symlink. */
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
895 path);
896 } else {
897 /* Both sides added symlinks which don't match. */
898 err = install_symlink_conflict(deriv_target,
899 deriv_base_commit_id, ancestor_target,
900 label_orig, ondisk_target, ondisk_path);
901 if (err)
902 goto done;
903 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
904 path);
906 } else if (!have_local_change && have_incoming_change) {
907 /* Apply the incoming change. */
908 err = update_symlink(ondisk_path, deriv_target,
909 strlen(deriv_target));
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 } else if (have_local_change && have_incoming_change) {
914 if (deriv_len == ondisk_len &&
915 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 /* Both sides made the same change. */
917 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
918 path);
919 } else {
920 err = install_symlink_conflict(deriv_target,
921 deriv_base_commit_id, ancestor_target, label_orig,
922 ondisk_target, ondisk_path);
923 if (err)
924 goto done;
925 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
926 path);
930 done:
931 free(ancestor_target);
932 return err;
935 static const struct got_error *
936 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
938 const struct got_error *err = NULL;
939 char target_path[PATH_MAX];
940 ssize_t target_len;
941 size_t n;
942 FILE *f;
944 *outfile = NULL;
946 f = got_opentemp();
947 if (f == NULL)
948 return got_error_from_errno("got_opentemp");
949 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 if (target_len == -1) {
951 err = got_error_from_errno2("readlink", ondisk_path);
952 goto done;
954 n = fwrite(target_path, 1, target_len, f);
955 if (n != target_len) {
956 err = got_ferror(f, GOT_ERR_IO);
957 goto done;
959 if (fflush(f) == EOF) {
960 err = got_error_from_errno("fflush");
961 goto done;
963 if (fseek(f, 0L, SEEK_SET) == -1) {
964 err = got_ferror(f, GOT_ERR_IO);
965 goto done;
967 done:
968 if (err)
969 fclose(f);
970 else
971 *outfile = f;
972 return err;
975 /*
976 * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 * blob_deriv acts as the first derived version, and the file on disk
978 * acts as the second derived version.
979 */
980 static const struct got_error *
981 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 struct got_blob_object *blob_orig, const char *ondisk_path,
983 const char *path, uint16_t st_mode, const char *label_orig,
984 struct got_blob_object *blob_deriv,
985 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 got_worktree_checkout_cb progress_cb, void *progress_arg)
988 const struct got_error *err = NULL;
989 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 char *blob_orig_path = NULL;
991 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 char *label_deriv = NULL, *parent = NULL;
994 *local_changes_subsumed = 0;
996 err = got_path_dirname(&parent, ondisk_path);
997 if (err)
998 return err;
1000 if (blob_orig) {
1001 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 parent) == -1) {
1003 err = got_error_from_errno("asprintf");
1004 base_path = NULL;
1005 goto done;
1008 err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 base_path, "");
1010 if (err)
1011 goto done;
1012 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 blob_orig);
1014 if (err)
1015 goto done;
1016 free(base_path);
1017 } else {
1019 * No common ancestor exists. This is an "add vs add" conflict
1020 * and we simply use an empty ancestor file to make both files
1021 * appear in the merged result in their entirety.
1023 f_orig = got_opentemp();
1024 if (f_orig == NULL) {
1025 err = got_error_from_errno("got_opentemp");
1026 goto done;
1030 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 err = got_error_from_errno("asprintf");
1032 base_path = NULL;
1033 goto done;
1036 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1037 if (err)
1038 goto done;
1039 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 blob_deriv);
1041 if (err)
1042 goto done;
1044 err = got_object_id_str(&id_str, deriv_base_commit_id);
1045 if (err)
1046 goto done;
1047 if (asprintf(&label_deriv, "%s: commit %s",
1048 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 err = got_error_from_errno("asprintf");
1050 goto done;
1054 * In order the run a 3-way merge with a symlink we copy the symlink's
1055 * target path into a temporary file and use that file with diff3.
1057 if (S_ISLNK(st_mode)) {
1058 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1059 if (err)
1060 goto done;
1061 } else {
1062 int fd;
1063 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 if (fd == -1) {
1065 err = got_error_from_errno2("open", ondisk_path);
1066 goto done;
1068 f_deriv2 = fdopen(fd, "r");
1069 if (f_deriv2 == NULL) {
1070 err = got_error_from_errno2("fdopen", ondisk_path);
1071 close(fd);
1072 goto done;
1076 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1079 done:
1080 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 err = got_error_from_errno("fclose");
1082 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 err = got_error_from_errno("fclose");
1084 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 err = got_error_from_errno("fclose");
1086 free(base_path);
1087 if (blob_orig_path) {
1088 unlink(blob_orig_path);
1089 free(blob_orig_path);
1091 if (blob_deriv_path) {
1092 unlink(blob_deriv_path);
1093 free(blob_deriv_path);
1095 free(id_str);
1096 free(label_deriv);
1097 free(parent);
1098 return err;
1101 static const struct got_error *
1102 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 int wt_fd, const char *path, struct got_object_id *blob_id)
1106 const struct got_error *err = NULL;
1107 struct got_fileindex_entry *new_ie;
1109 *new_iep = NULL;
1111 err = got_fileindex_entry_alloc(&new_ie, path);
1112 if (err)
1113 return err;
1115 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1116 blob_id->sha1, base_commit_id->sha1, 1);
1117 if (err)
1118 goto done;
1120 err = got_fileindex_entry_add(fileindex, new_ie);
1121 done:
1122 if (err)
1123 got_fileindex_entry_free(new_ie);
1124 else
1125 *new_iep = new_ie;
1126 return err;
1129 static mode_t
1130 get_ondisk_perms(int executable, mode_t st_mode)
1132 mode_t xbits = S_IXUSR;
1134 if (executable) {
1135 /* Map read bits to execute bits. */
1136 if (st_mode & S_IRGRP)
1137 xbits |= S_IXGRP;
1138 if (st_mode & S_IROTH)
1139 xbits |= S_IXOTH;
1140 return st_mode | xbits;
1143 return st_mode;
1146 /* forward declaration */
1147 static const struct got_error *
1148 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 const char *path, mode_t te_mode, mode_t st_mode,
1150 struct got_blob_object *blob, int restoring_missing_file,
1151 int reverting_versioned_file, int installing_bad_symlink,
1152 int path_is_unversioned, struct got_repository *repo,
1153 got_worktree_checkout_cb progress_cb, void *progress_arg);
1156 * This function assumes that the provided symlink target points at a
1157 * safe location in the work tree!
1159 static const struct got_error *
1160 replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 const char *target_path, size_t target_len)
1163 const struct got_error *err = NULL;
1164 ssize_t elen;
1165 char etarget[PATH_MAX];
1166 int fd;
1168 *did_something = 0;
1171 * "Bad" symlinks (those pointing outside the work tree or into the
1172 * .got directory) are installed in the work tree as a regular file
1173 * which contains the bad symlink target path.
1174 * The new symlink target has already been checked for safety by our
1175 * caller. If we can successfully open a regular file then we simply
1176 * replace this file with a symlink below.
1178 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1179 if (fd == -1) {
1180 if (!got_err_open_nofollow_on_symlink())
1181 return got_error_from_errno2("open", ondisk_path);
1183 /* We are updating an existing on-disk symlink. */
1184 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 if (elen == -1)
1186 return got_error_from_errno2("readlink", ondisk_path);
1188 if (elen == target_len &&
1189 memcmp(etarget, target_path, target_len) == 0)
1190 return NULL; /* nothing to do */
1193 *did_something = 1;
1194 err = update_symlink(ondisk_path, target_path, target_len);
1195 if (fd != -1 && close(fd) == -1 && err == NULL)
1196 err = got_error_from_errno2("close", ondisk_path);
1197 return err;
1200 static const struct got_error *
1201 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1203 const char *meta_dir)
1205 const struct got_error *err = NULL;
1206 char canonpath[PATH_MAX];
1207 char *path_got = NULL;
1209 *is_bad_symlink = 0;
1211 if (target_len >= sizeof(canonpath)) {
1212 *is_bad_symlink = 1;
1213 return NULL;
1217 * We do not use realpath(3) to resolve the symlink's target
1218 * path because we don't want to resolve symlinks recursively.
1219 * Instead we make the path absolute and then canonicalize it.
1220 * Relative symlink target lookup should begin at the directory
1221 * in which the blob object is being installed.
1223 if (!got_path_is_absolute(target_path)) {
1224 char *abspath, *parent;
1225 err = got_path_dirname(&parent, ondisk_path);
1226 if (err)
1227 return err;
1228 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1229 free(parent);
1230 return got_error_from_errno("asprintf");
1232 free(parent);
1233 if (strlen(abspath) >= sizeof(canonpath)) {
1234 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1235 free(abspath);
1236 return err;
1238 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1239 free(abspath);
1240 if (err)
1241 return err;
1242 } else {
1243 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1244 if (err)
1245 return err;
1248 /* Only allow symlinks pointing at paths within the work tree. */
1249 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1250 *is_bad_symlink = 1;
1251 return NULL;
1254 /* Do not allow symlinks pointing into the meta directory. */
1255 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1256 return got_error_from_errno("asprintf");
1257 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 *is_bad_symlink = 1;
1260 free(path_got);
1261 return NULL;
1264 static const struct got_error *
1265 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 int restoring_missing_file, int reverting_versioned_file,
1268 int path_is_unversioned, int allow_bad_symlinks,
1269 struct got_repository *repo,
1270 got_worktree_checkout_cb progress_cb, void *progress_arg)
1272 const struct got_error *err = NULL;
1273 char target_path[PATH_MAX];
1274 size_t len, target_len = 0;
1275 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1278 *is_bad_symlink = 0;
1281 * Blob object content specifies the target path of the link.
1282 * If a symbolic link cannot be installed we instead create
1283 * a regular file which contains the link target path stored
1284 * in the blob object.
1286 do {
1287 err = got_object_blob_read_block(&len, blob);
1288 if (err)
1289 return err;
1291 if (len + target_len >= sizeof(target_path)) {
1292 /* Path too long; install as a regular file. */
1293 *is_bad_symlink = 1;
1294 got_object_blob_rewind(blob);
1295 return install_blob(worktree, ondisk_path, path,
1296 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 restoring_missing_file, reverting_versioned_file,
1298 1, path_is_unversioned, repo, progress_cb,
1299 progress_arg);
1301 if (len > 0) {
1302 /* Skip blob object header first time around. */
1303 memcpy(target_path + target_len, buf + hdrlen,
1304 len - hdrlen);
1305 target_len += len - hdrlen;
1306 hdrlen = 0;
1308 } while (len != 0);
1309 target_path[target_len] = '\0';
1311 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 ondisk_path, worktree->root_path, worktree->meta_dir);
1313 if (err)
1314 return err;
1316 if (*is_bad_symlink && !allow_bad_symlinks) {
1317 /* install as a regular file */
1318 got_object_blob_rewind(blob);
1319 err = install_blob(worktree, ondisk_path, path,
1320 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 restoring_missing_file, reverting_versioned_file, 1,
1322 path_is_unversioned, repo, progress_cb, progress_arg);
1323 return err;
1326 if (symlink(target_path, ondisk_path) == -1) {
1327 if (errno == EEXIST) {
1328 int symlink_replaced;
1329 if (path_is_unversioned) {
1330 err = (*progress_cb)(progress_arg,
1331 GOT_STATUS_UNVERSIONED, path);
1332 return err;
1334 err = replace_existing_symlink(&symlink_replaced,
1335 ondisk_path, target_path, target_len);
1336 if (err)
1337 return err;
1338 if (progress_cb) {
1339 if (symlink_replaced) {
1340 err = (*progress_cb)(progress_arg,
1341 reverting_versioned_file ?
1342 GOT_STATUS_REVERT :
1343 GOT_STATUS_UPDATE, path);
1344 } else {
1345 err = (*progress_cb)(progress_arg,
1346 GOT_STATUS_EXISTS, path);
1349 return err; /* Nothing else to do. */
1352 if (errno == ENOENT) {
1353 char *parent;
1354 err = got_path_dirname(&parent, ondisk_path);
1355 if (err)
1356 return err;
1357 err = got_path_mkdir(parent);
1358 free(parent);
1359 if (err)
1360 return err;
1362 * Retry, and fall through to error handling
1363 * below if this second attempt fails.
1365 if (symlink(target_path, ondisk_path) != -1) {
1366 err = NULL; /* success */
1367 if (progress_cb) {
1368 err = (*progress_cb)(progress_arg,
1369 reverting_versioned_file ?
1370 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1371 path);
1373 return err;
1377 /* Handle errors from first or second creation attempt. */
1378 if (errno == ENAMETOOLONG) {
1379 /* bad target path; install as a regular file */
1380 *is_bad_symlink = 1;
1381 got_object_blob_rewind(blob);
1382 err = install_blob(worktree, ondisk_path, path,
1383 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 restoring_missing_file, reverting_versioned_file, 1,
1385 path_is_unversioned, repo,
1386 progress_cb, progress_arg);
1387 } else if (errno == ENOTDIR) {
1388 err = got_error_path(ondisk_path,
1389 GOT_ERR_FILE_OBSTRUCTED);
1390 } else {
1391 err = got_error_from_errno3("symlink",
1392 target_path, ondisk_path);
1394 } else if (progress_cb)
1395 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1396 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1397 return err;
1400 static const struct got_error *
1401 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1402 const char *path, mode_t te_mode, mode_t st_mode,
1403 struct got_blob_object *blob, int restoring_missing_file,
1404 int reverting_versioned_file, int installing_bad_symlink,
1405 int path_is_unversioned, struct got_repository *repo,
1406 got_worktree_checkout_cb progress_cb, void *progress_arg)
1408 const struct got_error *err = NULL;
1409 int fd = -1;
1410 size_t len, hdrlen;
1411 int update = 0;
1412 char *tmppath = NULL;
1413 mode_t mode;
1415 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1416 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1417 O_CLOEXEC, mode);
1418 if (fd == -1) {
1419 if (errno == ENOENT || errno == ENOTDIR) {
1420 char *parent;
1421 err = got_path_dirname(&parent, path);
1422 if (err)
1423 return err;
1424 err = add_dir_on_disk(worktree, parent);
1425 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1426 err = got_error_path(path, err->code);
1427 free(parent);
1428 if (err)
1429 return err;
1430 fd = open(ondisk_path,
1431 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1432 mode);
1433 if (fd == -1)
1434 return got_error_from_errno2("open",
1435 ondisk_path);
1436 } else if (errno == EEXIST) {
1437 if (path_is_unversioned) {
1438 err = (*progress_cb)(progress_arg,
1439 GOT_STATUS_UNVERSIONED, path);
1440 goto done;
1442 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1443 !S_ISREG(st_mode) && !installing_bad_symlink) {
1444 /* TODO file is obstructed; do something */
1445 err = got_error_path(ondisk_path,
1446 GOT_ERR_FILE_OBSTRUCTED);
1447 goto done;
1448 } else {
1449 err = got_opentemp_named_fd(&tmppath, &fd,
1450 ondisk_path, "");
1451 if (err)
1452 goto done;
1453 update = 1;
1455 if (fchmod(fd, apply_umask(mode)) == -1) {
1456 err = got_error_from_errno2("fchmod",
1457 tmppath);
1458 goto done;
1461 } else
1462 return got_error_from_errno2("open", ondisk_path);
1465 if (progress_cb) {
1466 if (restoring_missing_file)
1467 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1468 path);
1469 else if (reverting_versioned_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1471 path);
1472 else
1473 err = (*progress_cb)(progress_arg,
1474 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1475 if (err)
1476 goto done;
1479 hdrlen = got_object_blob_get_hdrlen(blob);
1480 do {
1481 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 err = got_object_blob_read_block(&len, blob);
1483 if (err)
1484 break;
1485 if (len > 0) {
1486 /* Skip blob object header first time around. */
1487 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 if (outlen == -1) {
1489 err = got_error_from_errno("write");
1490 goto done;
1491 } else if (outlen != len - hdrlen) {
1492 err = got_error(GOT_ERR_IO);
1493 goto done;
1495 hdrlen = 0;
1497 } while (len != 0);
1499 if (fsync(fd) != 0) {
1500 err = got_error_from_errno("fsync");
1501 goto done;
1504 if (update) {
1505 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1506 err = got_error_from_errno2("unlink", ondisk_path);
1507 goto done;
1509 if (rename(tmppath, ondisk_path) != 0) {
1510 err = got_error_from_errno3("rename", tmppath,
1511 ondisk_path);
1512 goto done;
1514 free(tmppath);
1515 tmppath = NULL;
1518 done:
1519 if (fd != -1 && close(fd) == -1 && err == NULL)
1520 err = got_error_from_errno("close");
1521 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1522 err = got_error_from_errno2("unlink", tmppath);
1523 free(tmppath);
1524 return err;
1528 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1529 * conflict marker is found in newly added lines only.
1531 static const struct got_error *
1532 get_modified_file_content_status(unsigned char *status,
1533 struct got_blob_object *blob, const char *path, struct stat *sb,
1534 FILE *ondisk_file)
1536 const struct got_error *err, *free_err;
1537 const char *markers[3] = {
1538 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1539 GOT_DIFF_CONFLICT_MARKER_SEP,
1540 GOT_DIFF_CONFLICT_MARKER_END
1542 FILE *f1 = NULL;
1543 struct got_diffreg_result *diffreg_result = NULL;
1544 struct diff_result *r;
1545 int nchunks_parsed, n, i = 0, ln = 0;
1546 char *line = NULL;
1547 size_t linesize = 0;
1548 ssize_t linelen;
1550 if (*status != GOT_STATUS_MODIFY)
1551 return NULL;
1553 f1 = got_opentemp();
1554 if (f1 == NULL)
1555 return got_error_from_errno("got_opentemp");
1557 if (blob) {
1558 got_object_blob_rewind(blob);
1559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1560 if (err)
1561 goto done;
1564 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1565 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1566 if (err)
1567 goto done;
1569 r = diffreg_result->result;
1571 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1572 struct diff_chunk *c;
1573 struct diff_chunk_context cc = {};
1574 off_t pos;
1577 * We can optimise a little by advancing straight
1578 * to the next chunk if this one has no added lines.
1580 c = diff_chunk_get(r, n);
1582 if (diff_chunk_type(c) != CHUNK_PLUS) {
1583 nchunks_parsed = 1;
1584 continue; /* removed or unchanged lines */
1587 pos = diff_chunk_get_right_start_pos(c);
1588 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1589 err = got_ferror(ondisk_file, GOT_ERR_IO);
1590 goto done;
1593 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1594 ln = cc.right.start;
1596 while (ln < cc.right.end) {
1597 linelen = getline(&line, &linesize, ondisk_file);
1598 if (linelen == -1) {
1599 if (feof(ondisk_file))
1600 break;
1601 err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 break;
1605 if (line && strncmp(line, markers[i],
1606 strlen(markers[i])) == 0) {
1607 if (strcmp(markers[i],
1608 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1609 *status = GOT_STATUS_CONFLICT;
1610 goto done;
1611 } else
1612 i++;
1614 ++ln;
1618 done:
1619 free(line);
1620 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1621 err = got_error_from_errno("fclose");
1622 free_err = got_diffreg_result_free(diffreg_result);
1623 if (err == NULL)
1624 err = free_err;
1626 return err;
1629 static int
1630 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1632 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1636 static int
1637 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1639 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 ie->size == (sb->st_size & 0xffffffff) &&
1644 !xbit_differs(ie, sb->st_mode));
1647 static unsigned char
1648 get_staged_status(struct got_fileindex_entry *ie)
1650 switch (got_fileindex_entry_stage_get(ie)) {
1651 case GOT_FILEIDX_STAGE_ADD:
1652 return GOT_STATUS_ADD;
1653 case GOT_FILEIDX_STAGE_DELETE:
1654 return GOT_STATUS_DELETE;
1655 case GOT_FILEIDX_STAGE_MODIFY:
1656 return GOT_STATUS_MODIFY;
1657 default:
1658 return GOT_STATUS_NO_CHANGE;
1662 static const struct got_error *
1663 get_symlink_modification_status(unsigned char *status,
1664 struct got_fileindex_entry *ie, const char *abspath,
1665 int dirfd, const char *de_name, struct got_blob_object *blob)
1667 const struct got_error *err = NULL;
1668 char target_path[PATH_MAX];
1669 char etarget[PATH_MAX];
1670 ssize_t elen;
1671 size_t len, target_len = 0;
1672 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1675 *status = GOT_STATUS_NO_CHANGE;
1677 /* Blob object content specifies the target path of the link. */
1678 do {
1679 err = got_object_blob_read_block(&len, blob);
1680 if (err)
1681 return err;
1682 if (len + target_len >= sizeof(target_path)) {
1684 * Should not happen. The blob contents were OK
1685 * when this symlink was installed.
1687 return got_error(GOT_ERR_NO_SPACE);
1689 if (len > 0) {
1690 /* Skip blob object header first time around. */
1691 memcpy(target_path + target_len, buf + hdrlen,
1692 len - hdrlen);
1693 target_len += len - hdrlen;
1694 hdrlen = 0;
1696 } while (len != 0);
1697 target_path[target_len] = '\0';
1699 if (dirfd != -1) {
1700 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 if (elen == -1)
1702 return got_error_from_errno2("readlinkat", abspath);
1703 } else {
1704 elen = readlink(abspath, etarget, sizeof(etarget));
1705 if (elen == -1)
1706 return got_error_from_errno2("readlink", abspath);
1709 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 *status = GOT_STATUS_MODIFY;
1712 return NULL;
1715 static const struct got_error *
1716 get_file_status(unsigned char *status, struct stat *sb,
1717 struct got_fileindex_entry *ie, const char *abspath,
1718 int dirfd, const char *de_name, struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 struct got_object_id id;
1722 size_t hdrlen;
1723 int fd = -1, fd1 = -1;
1724 FILE *f = NULL;
1725 uint8_t fbuf[8192];
1726 struct got_blob_object *blob = NULL;
1727 size_t flen, blen;
1728 unsigned char staged_status;
1730 staged_status = get_staged_status(ie);
1731 *status = GOT_STATUS_NO_CHANGE;
1732 memset(sb, 0, sizeof(*sb));
1735 * Whenever the caller provides a directory descriptor and a
1736 * directory entry name for the file, use them! This prevents
1737 * race conditions if filesystem paths change beneath our feet.
1739 if (dirfd != -1) {
1740 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 if (errno == ENOENT) {
1742 if (got_fileindex_entry_has_file_on_disk(ie))
1743 *status = GOT_STATUS_MISSING;
1744 else
1745 *status = GOT_STATUS_DELETE;
1746 goto done;
1748 err = got_error_from_errno2("fstatat", abspath);
1749 goto done;
1751 } else {
1752 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1753 if (fd == -1 && errno != ENOENT &&
1754 !got_err_open_nofollow_on_symlink())
1755 return got_error_from_errno2("open", abspath);
1756 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1757 if (lstat(abspath, sb) == -1)
1758 return got_error_from_errno2("lstat", abspath);
1759 } else if (fd == -1 || fstat(fd, sb) == -1) {
1760 if (errno == ENOENT) {
1761 if (got_fileindex_entry_has_file_on_disk(ie))
1762 *status = GOT_STATUS_MISSING;
1763 else
1764 *status = GOT_STATUS_DELETE;
1765 goto done;
1767 err = got_error_from_errno2("fstat", abspath);
1768 goto done;
1772 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1773 *status = GOT_STATUS_OBSTRUCTED;
1774 goto done;
1777 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1778 *status = GOT_STATUS_DELETE;
1779 goto done;
1780 } else if (!got_fileindex_entry_has_blob(ie) &&
1781 staged_status != GOT_STATUS_ADD) {
1782 *status = GOT_STATUS_ADD;
1783 goto done;
1786 if (!stat_info_differs(ie, sb))
1787 goto done;
1789 if (S_ISLNK(sb->st_mode) &&
1790 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1791 *status = GOT_STATUS_MODIFY;
1792 goto done;
1795 if (staged_status == GOT_STATUS_MODIFY ||
1796 staged_status == GOT_STATUS_ADD)
1797 got_fileindex_entry_get_staged_blob_id(&id, ie);
1798 else
1799 got_fileindex_entry_get_blob_id(&id, ie);
1801 fd1 = got_opentempfd();
1802 if (fd1 == -1) {
1803 err = got_error_from_errno("got_opentempfd");
1804 goto done;
1806 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1807 if (err)
1808 goto done;
1810 if (S_ISLNK(sb->st_mode)) {
1811 err = get_symlink_modification_status(status, ie,
1812 abspath, dirfd, de_name, blob);
1813 goto done;
1816 if (dirfd != -1) {
1817 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1818 if (fd == -1) {
1819 err = got_error_from_errno2("openat", abspath);
1820 goto done;
1824 f = fdopen(fd, "r");
1825 if (f == NULL) {
1826 err = got_error_from_errno2("fdopen", abspath);
1827 goto done;
1829 fd = -1;
1830 hdrlen = got_object_blob_get_hdrlen(blob);
1831 for (;;) {
1832 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1833 err = got_object_blob_read_block(&blen, blob);
1834 if (err)
1835 goto done;
1836 /* Skip length of blob object header first time around. */
1837 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1838 if (flen == 0 && ferror(f)) {
1839 err = got_error_from_errno("fread");
1840 goto done;
1842 if (blen - hdrlen == 0) {
1843 if (flen != 0)
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1846 } else if (flen == 0) {
1847 if (blen - hdrlen != 0)
1848 *status = GOT_STATUS_MODIFY;
1849 break;
1850 } else if (blen - hdrlen == flen) {
1851 /* Skip blob object header first time around. */
1852 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1853 *status = GOT_STATUS_MODIFY;
1854 break;
1856 } else {
1857 *status = GOT_STATUS_MODIFY;
1858 break;
1860 hdrlen = 0;
1863 if (*status == GOT_STATUS_MODIFY) {
1864 rewind(f);
1865 err = get_modified_file_content_status(status, blob, ie->path,
1866 sb, f);
1867 } else if (xbit_differs(ie, sb->st_mode))
1868 *status = GOT_STATUS_MODE_CHANGE;
1869 done:
1870 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1871 err = got_error_from_errno("close");
1872 if (blob)
1873 got_object_blob_close(blob);
1874 if (f != NULL && fclose(f) == EOF && err == NULL)
1875 err = got_error_from_errno2("fclose", abspath);
1876 if (fd != -1 && close(fd) == -1 && err == NULL)
1877 err = got_error_from_errno2("close", abspath);
1878 return err;
1882 * Update timestamps in the file index if a file is unmodified and
1883 * we had to run a full content comparison to find out.
1885 static const struct got_error *
1886 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1887 struct got_fileindex_entry *ie, struct stat *sb)
1889 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1890 return got_fileindex_entry_update(ie, wt_fd, path,
1891 ie->blob_sha1, ie->commit_sha1, 1);
1893 return NULL;
1896 static const struct got_error *remove_ondisk_file(const char *, const char *);
1898 static const struct got_error *
1899 update_blob(struct got_worktree *worktree,
1900 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1901 struct got_tree_entry *te, const char *path,
1902 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1903 void *progress_arg)
1905 const struct got_error *err = NULL;
1906 struct got_blob_object *blob = NULL;
1907 char *ondisk_path = NULL;
1908 unsigned char status = GOT_STATUS_NO_CHANGE;
1909 struct stat sb;
1910 int fd1 = -1, fd2 = -1;
1912 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1913 return got_error_from_errno("asprintf");
1915 if (ie) {
1916 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1917 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1918 goto done;
1920 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1921 repo);
1922 if (err)
1923 goto done;
1924 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1925 sb.st_mode = got_fileindex_perms_to_st(ie);
1926 } else {
1927 if (stat(ondisk_path, &sb) == -1) {
1928 if (errno != ENOENT && errno != ENOTDIR) {
1929 err = got_error_from_errno2("stat",
1930 ondisk_path);
1931 goto done;
1933 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 status = GOT_STATUS_UNVERSIONED;
1935 } else {
1936 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1937 status = GOT_STATUS_UNVERSIONED;
1938 else
1939 status = GOT_STATUS_OBSTRUCTED;
1943 if (status == GOT_STATUS_OBSTRUCTED) {
1944 if (ie)
1945 got_fileindex_entry_mark_skipped(ie);
1946 err = (*progress_cb)(progress_arg, status, path);
1947 goto done;
1949 if (status == GOT_STATUS_CONFLICT) {
1950 if (ie)
1951 got_fileindex_entry_mark_skipped(ie);
1952 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1953 path);
1954 goto done;
1957 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1958 if (status == GOT_STATUS_UNVERSIONED) {
1959 err = (*progress_cb)(progress_arg, status, path);
1960 } else if (status != GOT_STATUS_NO_CHANGE &&
1961 status != GOT_STATUS_DELETE &&
1962 status != GOT_STATUS_NONEXISTENT &&
1963 status != GOT_STATUS_MISSING) {
1964 err = (*progress_cb)(progress_arg,
1965 GOT_STATUS_CANNOT_DELETE, path);
1966 } else if (ie) {
1967 if (status != GOT_STATUS_DELETE &&
1968 status != GOT_STATUS_NONEXISTENT &&
1969 status != GOT_STATUS_MISSING) {
1970 err = remove_ondisk_file(worktree->root_path,
1971 ie->path);
1972 if (err && !(err->code == GOT_ERR_ERRNO &&
1973 errno == ENOENT))
1974 goto done;
1976 got_fileindex_entry_remove(fileindex, ie);
1977 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1978 ie->path);
1980 goto done; /* nothing else to do */
1983 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1984 (S_ISLNK(te->mode) ||
1985 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1987 * This is a regular file or an installed bad symlink.
1988 * If the file index indicates that this file is already
1989 * up-to-date with respect to the repository we can skip
1990 * updating contents of this file.
1992 if (got_fileindex_entry_has_commit(ie) &&
1993 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1994 SHA1_DIGEST_LENGTH) == 0) {
1995 /* Same commit. */
1996 err = sync_timestamps(worktree->root_fd,
1997 path, status, ie, &sb);
1998 if (err)
1999 goto done;
2000 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2001 path);
2002 goto done;
2004 if (got_fileindex_entry_has_blob(ie) &&
2005 memcmp(ie->blob_sha1, te->id.sha1,
2006 SHA1_DIGEST_LENGTH) == 0) {
2007 /* Different commit but the same blob. */
2008 if (got_fileindex_entry_has_commit(ie)) {
2009 /* Update the base commit ID of this file. */
2010 memcpy(ie->commit_sha1,
2011 worktree->base_commit_id->sha1,
2012 sizeof(ie->commit_sha1));
2014 err = sync_timestamps(worktree->root_fd,
2015 path, status, ie, &sb);
2016 if (err)
2017 goto done;
2018 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2019 path);
2020 goto done;
2024 fd1 = got_opentempfd();
2025 if (fd1 == -1) {
2026 err = got_error_from_errno("got_opentempfd");
2027 goto done;
2029 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2030 if (err)
2031 goto done;
2033 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2034 int update_timestamps;
2035 struct got_blob_object *blob2 = NULL;
2036 char *label_orig = NULL;
2037 if (got_fileindex_entry_has_blob(ie)) {
2038 fd2 = got_opentempfd();
2039 if (fd2 == -1) {
2040 err = got_error_from_errno("got_opentempfd");
2041 goto done;
2043 struct got_object_id id2;
2044 got_fileindex_entry_get_blob_id(&id2, ie);
2045 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2046 fd2);
2047 if (err)
2048 goto done;
2050 if (got_fileindex_entry_has_commit(ie)) {
2051 char id_str[SHA1_DIGEST_STRING_LENGTH];
2052 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2053 sizeof(id_str)) == NULL) {
2054 err = got_error_path(id_str,
2055 GOT_ERR_BAD_OBJ_ID_STR);
2056 goto done;
2058 if (asprintf(&label_orig, "%s: commit %s",
2059 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2060 err = got_error_from_errno("asprintf");
2061 goto done;
2064 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2065 char *link_target;
2066 err = got_object_blob_read_to_str(&link_target, blob);
2067 if (err)
2068 goto done;
2069 err = merge_symlink(worktree, blob2, ondisk_path, path,
2070 label_orig, link_target, worktree->base_commit_id,
2071 repo, progress_cb, progress_arg);
2072 free(link_target);
2073 } else {
2074 err = merge_blob(&update_timestamps, worktree, blob2,
2075 ondisk_path, path, sb.st_mode, label_orig, blob,
2076 worktree->base_commit_id, repo,
2077 progress_cb, progress_arg);
2079 free(label_orig);
2080 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2081 err = got_error_from_errno("close");
2082 goto done;
2084 if (blob2)
2085 got_object_blob_close(blob2);
2086 if (err)
2087 goto done;
2089 * Do not update timestamps of files with local changes.
2090 * Otherwise, a future status walk would treat them as
2091 * unmodified files again.
2093 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2094 blob->id.sha1, worktree->base_commit_id->sha1,
2095 update_timestamps);
2096 } else if (status == GOT_STATUS_MODE_CHANGE) {
2097 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2098 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2099 } else if (status == GOT_STATUS_DELETE) {
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2101 if (err)
2102 goto done;
2103 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2105 if (err)
2106 goto done;
2107 } else {
2108 int is_bad_symlink = 0;
2109 if (S_ISLNK(te->mode)) {
2110 err = install_symlink(&is_bad_symlink, worktree,
2111 ondisk_path, path, blob,
2112 status == GOT_STATUS_MISSING, 0,
2113 status == GOT_STATUS_UNVERSIONED, 0,
2114 repo, progress_cb, progress_arg);
2115 } else {
2116 err = install_blob(worktree, ondisk_path, path,
2117 te->mode, sb.st_mode, blob,
2118 status == GOT_STATUS_MISSING, 0, 0,
2119 status == GOT_STATUS_UNVERSIONED, repo,
2120 progress_cb, progress_arg);
2122 if (err)
2123 goto done;
2125 if (ie) {
2126 err = got_fileindex_entry_update(ie,
2127 worktree->root_fd, path, blob->id.sha1,
2128 worktree->base_commit_id->sha1, 1);
2129 } else {
2130 err = create_fileindex_entry(&ie, fileindex,
2131 worktree->base_commit_id, worktree->root_fd, path,
2132 &blob->id);
2134 if (err)
2135 goto done;
2137 if (is_bad_symlink) {
2138 got_fileindex_entry_filetype_set(ie,
2139 GOT_FILEIDX_MODE_BAD_SYMLINK);
2143 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2144 err = got_error_from_errno("close");
2145 goto done;
2147 got_object_blob_close(blob);
2148 done:
2149 free(ondisk_path);
2150 return err;
2153 static const struct got_error *
2154 remove_ondisk_file(const char *root_path, const char *path)
2156 const struct got_error *err = NULL;
2157 char *ondisk_path = NULL, *parent = NULL;
2159 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2160 return got_error_from_errno("asprintf");
2162 if (unlink(ondisk_path) == -1) {
2163 if (errno != ENOENT)
2164 err = got_error_from_errno2("unlink", ondisk_path);
2165 } else {
2166 size_t root_len = strlen(root_path);
2167 err = got_path_dirname(&parent, ondisk_path);
2168 if (err)
2169 goto done;
2170 while (got_path_cmp(parent, root_path,
2171 strlen(parent), root_len) != 0) {
2172 free(ondisk_path);
2173 ondisk_path = parent;
2174 parent = NULL;
2175 if (rmdir(ondisk_path) == -1) {
2176 if (errno != ENOTEMPTY)
2177 err = got_error_from_errno2("rmdir",
2178 ondisk_path);
2179 break;
2181 err = got_path_dirname(&parent, ondisk_path);
2182 if (err)
2183 break;
2186 done:
2187 free(ondisk_path);
2188 free(parent);
2189 return err;
2192 static const struct got_error *
2193 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2194 struct got_fileindex_entry *ie, struct got_repository *repo,
2195 got_worktree_checkout_cb progress_cb, void *progress_arg)
2197 const struct got_error *err = NULL;
2198 unsigned char status;
2199 struct stat sb;
2200 char *ondisk_path;
2202 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2203 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2205 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2206 == -1)
2207 return got_error_from_errno("asprintf");
2209 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2210 if (err)
2211 goto done;
2213 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2214 char ondisk_target[PATH_MAX];
2215 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2216 sizeof(ondisk_target));
2217 if (ondisk_len == -1) {
2218 err = got_error_from_errno2("readlink", ondisk_path);
2219 goto done;
2221 ondisk_target[ondisk_len] = '\0';
2222 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2223 NULL, NULL, /* XXX pass common ancestor info? */
2224 ondisk_target, ondisk_path);
2225 if (err)
2226 goto done;
2227 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2228 ie->path);
2229 goto done;
2232 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2233 status == GOT_STATUS_ADD) {
2234 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2235 if (err)
2236 goto done;
2238 * Preserve the working file and change the deleted blob's
2239 * entry into a schedule-add entry.
2241 err = got_fileindex_entry_update(ie, worktree->root_fd,
2242 ie->path, NULL, NULL, 0);
2243 } else {
2244 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2245 if (err)
2246 goto done;
2247 if (status == GOT_STATUS_NO_CHANGE) {
2248 err = remove_ondisk_file(worktree->root_path, ie->path);
2249 if (err)
2250 goto done;
2252 got_fileindex_entry_remove(fileindex, ie);
2254 done:
2255 free(ondisk_path);
2256 return err;
2259 struct diff_cb_arg {
2260 struct got_fileindex *fileindex;
2261 struct got_worktree *worktree;
2262 struct got_repository *repo;
2263 got_worktree_checkout_cb progress_cb;
2264 void *progress_arg;
2265 got_cancel_cb cancel_cb;
2266 void *cancel_arg;
2269 static const struct got_error *
2270 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2271 struct got_tree_entry *te, const char *parent_path)
2273 struct diff_cb_arg *a = arg;
2275 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2276 return got_error(GOT_ERR_CANCELLED);
2278 return update_blob(a->worktree, a->fileindex, ie, te,
2279 ie->path, a->repo, a->progress_cb, a->progress_arg);
2282 static const struct got_error *
2283 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2285 struct diff_cb_arg *a = arg;
2287 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 return got_error(GOT_ERR_CANCELLED);
2290 return delete_blob(a->worktree, a->fileindex, ie,
2291 a->repo, a->progress_cb, a->progress_arg);
2294 static const struct got_error *
2295 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2297 struct diff_cb_arg *a = arg;
2298 const struct got_error *err;
2299 char *path;
2301 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2302 return got_error(GOT_ERR_CANCELLED);
2304 if (got_object_tree_entry_is_submodule(te))
2305 return NULL;
2307 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2308 return NULL;
2310 if (asprintf(&path, "%s%s%s", parent_path,
2311 parent_path[0] ? "/" : "", te->name)
2312 == -1)
2313 return got_error_from_errno("asprintf");
2315 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2316 a->repo, a->progress_cb, a->progress_arg);
2318 free(path);
2319 return err;
2322 const struct got_error *
2323 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2325 uint32_t uuid_status;
2327 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2328 if (uuid_status != uuid_s_ok) {
2329 *uuidstr = NULL;
2330 return got_error_uuid(uuid_status, "uuid_to_string");
2333 return NULL;
2336 static const struct got_error *
2337 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2339 const struct got_error *err = NULL;
2340 char *uuidstr = NULL;
2342 *refname = NULL;
2344 err = got_worktree_get_uuid(&uuidstr, worktree);
2345 if (err)
2346 return err;
2348 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2349 err = got_error_from_errno("asprintf");
2350 *refname = NULL;
2352 free(uuidstr);
2353 return err;
2356 const struct got_error *
2357 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2358 const char *prefix)
2360 return get_ref_name(refname, worktree, prefix);
2363 static const struct got_error *
2364 get_base_ref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2369 static const struct got_error *
2370 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2372 return get_ref_name(refname, worktree,
2373 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2376 static const struct got_error *
2377 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2379 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2382 static const struct got_error *
2383 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2385 return get_ref_name(refname, worktree,
2386 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2389 static const struct got_error *
2390 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2396 static const struct got_error *
2397 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2403 static const struct got_error *
2404 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2410 static const struct got_error *
2411 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2424 const struct got_error *
2425 got_worktree_get_histedit_script_path(char **path,
2426 struct got_worktree *worktree)
2428 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2429 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2430 *path = NULL;
2431 return got_error_from_errno("asprintf");
2433 return NULL;
2436 static const struct got_error *
2437 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2439 return get_ref_name(refname, worktree,
2440 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2443 static const struct got_error *
2444 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2446 return get_ref_name(refname, worktree,
2447 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2451 * Prevent Git's garbage collector from deleting our base commit by
2452 * setting a reference to our base commit's ID.
2454 static const struct got_error *
2455 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2457 const struct got_error *err = NULL;
2458 struct got_reference *ref = NULL;
2459 char *refname;
2461 err = get_base_ref_name(&refname, worktree);
2462 if (err)
2463 return err;
2465 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2466 if (err)
2467 goto done;
2469 err = got_ref_write(ref, repo);
2470 done:
2471 free(refname);
2472 if (ref)
2473 got_ref_close(ref);
2474 return err;
2477 static const struct got_error *
2478 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2480 const struct got_error *err = NULL;
2482 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2483 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2484 err = got_error_from_errno("asprintf");
2485 *fileindex_path = NULL;
2487 return err;
2491 static const struct got_error *
2492 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2493 struct got_worktree *worktree)
2495 const struct got_error *err = NULL;
2496 FILE *index = NULL;
2498 *fileindex_path = NULL;
2499 *fileindex = got_fileindex_alloc();
2500 if (*fileindex == NULL)
2501 return got_error_from_errno("got_fileindex_alloc");
2503 err = get_fileindex_path(fileindex_path, worktree);
2504 if (err)
2505 goto done;
2507 index = fopen(*fileindex_path, "rbe");
2508 if (index == NULL) {
2509 if (errno != ENOENT)
2510 err = got_error_from_errno2("fopen", *fileindex_path);
2511 } else {
2512 err = got_fileindex_read(*fileindex, index);
2513 if (fclose(index) == EOF && err == NULL)
2514 err = got_error_from_errno("fclose");
2516 done:
2517 if (err) {
2518 free(*fileindex_path);
2519 *fileindex_path = NULL;
2520 got_fileindex_free(*fileindex);
2521 *fileindex = NULL;
2523 return err;
2526 struct bump_base_commit_id_arg {
2527 struct got_object_id *base_commit_id;
2528 const char *path;
2529 size_t path_len;
2530 const char *entry_name;
2531 got_worktree_checkout_cb progress_cb;
2532 void *progress_arg;
2535 static const struct got_error *
2536 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2538 const struct got_error *err;
2539 struct bump_base_commit_id_arg *a = arg;
2541 if (a->entry_name) {
2542 if (strcmp(ie->path, a->path) != 0)
2543 return NULL;
2544 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2545 return NULL;
2547 if (got_fileindex_entry_was_skipped(ie))
2548 return NULL;
2550 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2551 SHA1_DIGEST_LENGTH) == 0)
2552 return NULL;
2554 if (a->progress_cb) {
2555 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2556 ie->path);
2557 if (err)
2558 return err;
2560 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2561 return NULL;
2564 /* Bump base commit ID of all files within an updated part of the work tree. */
2565 static const struct got_error *
2566 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2567 struct got_fileindex *fileindex,
2568 got_worktree_checkout_cb progress_cb, void *progress_arg)
2570 struct bump_base_commit_id_arg bbc_arg;
2572 bbc_arg.base_commit_id = worktree->base_commit_id;
2573 bbc_arg.entry_name = NULL;
2574 bbc_arg.path = "";
2575 bbc_arg.path_len = 0;
2576 bbc_arg.progress_cb = progress_cb;
2577 bbc_arg.progress_arg = progress_arg;
2579 return got_fileindex_for_each_entry_safe(fileindex,
2580 bump_base_commit_id, &bbc_arg);
2583 static const struct got_error *
2584 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2586 const struct got_error *err = NULL;
2587 char *new_fileindex_path = NULL;
2588 FILE *new_index = NULL;
2589 struct timespec timeout;
2591 err = got_opentemp_named(&new_fileindex_path, &new_index,
2592 fileindex_path, "");
2593 if (err)
2594 goto done;
2596 err = got_fileindex_write(fileindex, new_index);
2597 if (err)
2598 goto done;
2600 if (rename(new_fileindex_path, fileindex_path) != 0) {
2601 err = got_error_from_errno3("rename", new_fileindex_path,
2602 fileindex_path);
2603 unlink(new_fileindex_path);
2607 * Sleep for a short amount of time to ensure that files modified after
2608 * this program exits have a different time stamp from the one which
2609 * was recorded in the file index.
2611 timeout.tv_sec = 0;
2612 timeout.tv_nsec = 1;
2613 nanosleep(&timeout, NULL);
2614 done:
2615 if (new_index)
2616 fclose(new_index);
2617 free(new_fileindex_path);
2618 return err;
2621 static const struct got_error *
2622 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2623 struct got_object_id **tree_id, const char *wt_relpath,
2624 struct got_commit_object *base_commit, struct got_worktree *worktree,
2625 struct got_repository *repo)
2627 const struct got_error *err = NULL;
2628 struct got_object_id *id = NULL;
2629 char *in_repo_path = NULL;
2630 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2632 *entry_type = GOT_OBJ_TYPE_ANY;
2633 *tree_relpath = NULL;
2634 *tree_id = NULL;
2636 if (wt_relpath[0] == '\0') {
2637 /* Check out all files within the work tree. */
2638 *entry_type = GOT_OBJ_TYPE_TREE;
2639 *tree_relpath = strdup("");
2640 if (*tree_relpath == NULL) {
2641 err = got_error_from_errno("strdup");
2642 goto done;
2644 err = got_object_id_by_path(tree_id, repo, base_commit,
2645 worktree->path_prefix);
2646 if (err)
2647 goto done;
2648 return NULL;
2651 /* Check out a subset of files in the work tree. */
2653 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2654 is_root_wt ? "" : "/", wt_relpath) == -1) {
2655 err = got_error_from_errno("asprintf");
2656 goto done;
2659 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2660 if (err)
2661 goto done;
2663 free(in_repo_path);
2664 in_repo_path = NULL;
2666 err = got_object_get_type(entry_type, repo, id);
2667 if (err)
2668 goto done;
2670 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2671 /* Check out a single file. */
2672 if (strchr(wt_relpath, '/') == NULL) {
2673 /* Check out a single file in work tree's root dir. */
2674 in_repo_path = strdup(worktree->path_prefix);
2675 if (in_repo_path == NULL) {
2676 err = got_error_from_errno("strdup");
2677 goto done;
2679 *tree_relpath = strdup("");
2680 if (*tree_relpath == NULL) {
2681 err = got_error_from_errno("strdup");
2682 goto done;
2684 } else {
2685 /* Check out a single file in a subdirectory. */
2686 err = got_path_dirname(tree_relpath, wt_relpath);
2687 if (err)
2688 return err;
2689 if (asprintf(&in_repo_path, "%s%s%s",
2690 worktree->path_prefix, is_root_wt ? "" : "/",
2691 *tree_relpath) == -1) {
2692 err = got_error_from_errno("asprintf");
2693 goto done;
2696 err = got_object_id_by_path(tree_id, repo,
2697 base_commit, in_repo_path);
2698 } else {
2699 /* Check out all files within a subdirectory. */
2700 *tree_id = got_object_id_dup(id);
2701 if (*tree_id == NULL) {
2702 err = got_error_from_errno("got_object_id_dup");
2703 goto done;
2705 *tree_relpath = strdup(wt_relpath);
2706 if (*tree_relpath == NULL) {
2707 err = got_error_from_errno("strdup");
2708 goto done;
2711 done:
2712 free(id);
2713 free(in_repo_path);
2714 if (err) {
2715 *entry_type = GOT_OBJ_TYPE_ANY;
2716 free(*tree_relpath);
2717 *tree_relpath = NULL;
2718 free(*tree_id);
2719 *tree_id = NULL;
2721 return err;
2724 static const struct got_error *
2725 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2726 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2727 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2728 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2730 const struct got_error *err = NULL;
2731 struct got_commit_object *commit = NULL;
2732 struct got_tree_object *tree = NULL;
2733 struct got_fileindex_diff_tree_cb diff_cb;
2734 struct diff_cb_arg arg;
2736 err = ref_base_commit(worktree, repo);
2737 if (err) {
2738 if (!(err->code == GOT_ERR_ERRNO &&
2739 (errno == EACCES || errno == EROFS)))
2740 goto done;
2741 err = (*progress_cb)(progress_arg,
2742 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2743 if (err)
2744 return err;
2747 err = got_object_open_as_commit(&commit, repo,
2748 worktree->base_commit_id);
2749 if (err)
2750 goto done;
2752 err = got_object_open_as_tree(&tree, repo, tree_id);
2753 if (err)
2754 goto done;
2756 if (entry_name &&
2757 got_object_tree_find_entry(tree, entry_name) == NULL) {
2758 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2759 goto done;
2762 diff_cb.diff_old_new = diff_old_new;
2763 diff_cb.diff_old = diff_old;
2764 diff_cb.diff_new = diff_new;
2765 arg.fileindex = fileindex;
2766 arg.worktree = worktree;
2767 arg.repo = repo;
2768 arg.progress_cb = progress_cb;
2769 arg.progress_arg = progress_arg;
2770 arg.cancel_cb = cancel_cb;
2771 arg.cancel_arg = cancel_arg;
2772 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2773 entry_name, repo, &diff_cb, &arg);
2774 done:
2775 if (tree)
2776 got_object_tree_close(tree);
2777 if (commit)
2778 got_object_commit_close(commit);
2779 return err;
2782 const struct got_error *
2783 got_worktree_checkout_files(struct got_worktree *worktree,
2784 struct got_pathlist_head *paths, struct got_repository *repo,
2785 got_worktree_checkout_cb progress_cb, void *progress_arg,
2786 got_cancel_cb cancel_cb, void *cancel_arg)
2788 const struct got_error *err = NULL, *sync_err, *unlockerr;
2789 struct got_commit_object *commit = NULL;
2790 struct got_tree_object *tree = NULL;
2791 struct got_fileindex *fileindex = NULL;
2792 char *fileindex_path = NULL;
2793 struct got_pathlist_entry *pe;
2794 struct tree_path_data {
2795 STAILQ_ENTRY(tree_path_data) entry;
2796 struct got_object_id *tree_id;
2797 int entry_type;
2798 char *relpath;
2799 char *entry_name;
2800 } *tpd = NULL;
2801 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2803 STAILQ_INIT(&tree_paths);
2805 err = lock_worktree(worktree, LOCK_EX);
2806 if (err)
2807 return err;
2809 err = got_object_open_as_commit(&commit, repo,
2810 worktree->base_commit_id);
2811 if (err)
2812 goto done;
2814 /* Map all specified paths to in-repository trees. */
2815 TAILQ_FOREACH(pe, paths, entry) {
2816 tpd = malloc(sizeof(*tpd));
2817 if (tpd == NULL) {
2818 err = got_error_from_errno("malloc");
2819 goto done;
2822 err = find_tree_entry_for_checkout(&tpd->entry_type,
2823 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2824 worktree, repo);
2825 if (err) {
2826 free(tpd);
2827 goto done;
2830 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2831 err = got_path_basename(&tpd->entry_name, pe->path);
2832 if (err) {
2833 free(tpd->relpath);
2834 free(tpd->tree_id);
2835 free(tpd);
2836 goto done;
2838 } else
2839 tpd->entry_name = NULL;
2841 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2845 * Read the file index.
2846 * Checking out files is supposed to be an idempotent operation.
2847 * If the on-disk file index is incomplete we will try to complete it.
2849 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2850 if (err)
2851 goto done;
2853 tpd = STAILQ_FIRST(&tree_paths);
2854 TAILQ_FOREACH(pe, paths, entry) {
2855 struct bump_base_commit_id_arg bbc_arg;
2857 err = checkout_files(worktree, fileindex, tpd->relpath,
2858 tpd->tree_id, tpd->entry_name, repo,
2859 progress_cb, progress_arg, cancel_cb, cancel_arg);
2860 if (err)
2861 break;
2863 bbc_arg.base_commit_id = worktree->base_commit_id;
2864 bbc_arg.entry_name = tpd->entry_name;
2865 bbc_arg.path = pe->path;
2866 bbc_arg.path_len = pe->path_len;
2867 bbc_arg.progress_cb = progress_cb;
2868 bbc_arg.progress_arg = progress_arg;
2869 err = got_fileindex_for_each_entry_safe(fileindex,
2870 bump_base_commit_id, &bbc_arg);
2871 if (err)
2872 break;
2874 tpd = STAILQ_NEXT(tpd, entry);
2876 sync_err = sync_fileindex(fileindex, fileindex_path);
2877 if (sync_err && err == NULL)
2878 err = sync_err;
2879 done:
2880 free(fileindex_path);
2881 if (tree)
2882 got_object_tree_close(tree);
2883 if (commit)
2884 got_object_commit_close(commit);
2885 if (fileindex)
2886 got_fileindex_free(fileindex);
2887 while (!STAILQ_EMPTY(&tree_paths)) {
2888 tpd = STAILQ_FIRST(&tree_paths);
2889 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2890 free(tpd->relpath);
2891 free(tpd->tree_id);
2892 free(tpd);
2894 unlockerr = lock_worktree(worktree, LOCK_SH);
2895 if (unlockerr && err == NULL)
2896 err = unlockerr;
2897 return err;
2900 static const struct got_error *
2901 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2902 struct got_fileindex_entry *ie, const char *ondisk_path,
2903 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2904 int restoring_missing_file, int reverting_versioned_file,
2905 int path_is_unversioned, int allow_bad_symlinks,
2906 struct got_repository *repo,
2907 got_worktree_checkout_cb progress_cb, void *progress_arg)
2909 const struct got_error *err = NULL;
2910 int is_bad_symlink = 0;
2912 if (S_ISLNK(mode2)) {
2913 err = install_symlink(&is_bad_symlink,
2914 worktree, ondisk_path, path2, blob2,
2915 restoring_missing_file,
2916 reverting_versioned_file,
2917 path_is_unversioned, allow_bad_symlinks,
2918 repo, progress_cb, progress_arg);
2919 } else {
2920 err = install_blob(worktree, ondisk_path, path2,
2921 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2922 restoring_missing_file, reverting_versioned_file, 0,
2923 path_is_unversioned, repo, progress_cb, progress_arg);
2925 if (err)
2926 return err;
2927 if (ie == NULL) {
2928 /* Adding an unversioned file. */
2929 err = got_fileindex_entry_alloc(&ie, path2);
2930 if (err)
2931 return err;
2932 err = got_fileindex_entry_update(ie,
2933 worktree->root_fd, path2, NULL, NULL, 1);
2934 if (err) {
2935 got_fileindex_entry_free(ie);
2936 return err;
2938 err = got_fileindex_entry_add(fileindex, ie);
2939 if (err) {
2940 got_fileindex_entry_free(ie);
2941 return err;
2943 } else {
2944 /* Re-adding a locally deleted file. */
2945 err = got_fileindex_entry_update(ie,
2946 worktree->root_fd, path2, ie->blob_sha1,
2947 worktree->base_commit_id->sha1, 0);
2948 if (err)
2949 return err;
2952 if (is_bad_symlink) {
2953 got_fileindex_entry_filetype_set(ie,
2954 GOT_FILEIDX_MODE_BAD_SYMLINK);
2957 return NULL;
2960 struct merge_file_cb_arg {
2961 struct got_worktree *worktree;
2962 struct got_fileindex *fileindex;
2963 got_worktree_checkout_cb progress_cb;
2964 void *progress_arg;
2965 got_cancel_cb cancel_cb;
2966 void *cancel_arg;
2967 const char *label_orig;
2968 struct got_object_id *commit_id2;
2969 int allow_bad_symlinks;
2972 static const struct got_error *
2973 merge_file_cb(void *arg, struct got_blob_object *blob1,
2974 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2975 struct got_object_id *id1, struct got_object_id *id2,
2976 const char *path1, const char *path2,
2977 mode_t mode1, mode_t mode2, struct got_repository *repo)
2979 static const struct got_error *err = NULL;
2980 struct merge_file_cb_arg *a = arg;
2981 struct got_fileindex_entry *ie;
2982 char *ondisk_path = NULL;
2983 struct stat sb;
2984 unsigned char status;
2985 int local_changes_subsumed;
2986 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2987 char *id_str = NULL, *label_deriv2 = NULL;
2989 if (blob1 && blob2) {
2990 ie = got_fileindex_entry_get(a->fileindex, path2,
2991 strlen(path2));
2992 if (ie == NULL)
2993 return (*a->progress_cb)(a->progress_arg,
2994 GOT_STATUS_MISSING, path2);
2996 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2997 path2) == -1)
2998 return got_error_from_errno("asprintf");
3000 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3001 repo);
3002 if (err)
3003 goto done;
3005 if (status == GOT_STATUS_DELETE) {
3006 err = (*a->progress_cb)(a->progress_arg,
3007 GOT_STATUS_MERGE, path2);
3008 goto done;
3010 if (status != GOT_STATUS_NO_CHANGE &&
3011 status != GOT_STATUS_MODIFY &&
3012 status != GOT_STATUS_CONFLICT &&
3013 status != GOT_STATUS_ADD) {
3014 err = (*a->progress_cb)(a->progress_arg, status, path2);
3015 goto done;
3018 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3019 char *link_target2;
3020 err = got_object_blob_read_to_str(&link_target2, blob2);
3021 if (err)
3022 goto done;
3023 err = merge_symlink(a->worktree, blob1, ondisk_path,
3024 path2, a->label_orig, link_target2, a->commit_id2,
3025 repo, a->progress_cb, a->progress_arg);
3026 free(link_target2);
3027 } else {
3028 int fd;
3030 f_orig = got_opentemp();
3031 if (f_orig == NULL) {
3032 err = got_error_from_errno("got_opentemp");
3033 goto done;
3035 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3036 f_orig, blob1);
3037 if (err)
3038 goto done;
3040 f_deriv2 = got_opentemp();
3041 if (f_deriv2 == NULL)
3042 goto done;
3043 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3044 f_deriv2, blob2);
3045 if (err)
3046 goto done;
3048 fd = open(ondisk_path,
3049 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3050 if (fd == -1) {
3051 err = got_error_from_errno2("open",
3052 ondisk_path);
3053 goto done;
3055 f_deriv = fdopen(fd, "r");
3056 if (f_deriv == NULL) {
3057 err = got_error_from_errno2("fdopen",
3058 ondisk_path);
3059 close(fd);
3060 goto done;
3062 err = got_object_id_str(&id_str, a->commit_id2);
3063 if (err)
3064 goto done;
3065 if (asprintf(&label_deriv2, "%s: commit %s",
3066 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3067 err = got_error_from_errno("asprintf");
3068 goto done;
3070 err = merge_file(&local_changes_subsumed, a->worktree,
3071 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3072 mode2, a->label_orig, NULL, label_deriv2,
3073 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3074 a->progress_cb, a->progress_arg);
3076 } else if (blob1) {
3077 ie = got_fileindex_entry_get(a->fileindex, path1,
3078 strlen(path1));
3079 if (ie == NULL)
3080 return (*a->progress_cb)(a->progress_arg,
3081 GOT_STATUS_MISSING, path1);
3083 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3084 path1) == -1)
3085 return got_error_from_errno("asprintf");
3087 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3088 repo);
3089 if (err)
3090 goto done;
3092 switch (status) {
3093 case GOT_STATUS_NO_CHANGE:
3094 err = (*a->progress_cb)(a->progress_arg,
3095 GOT_STATUS_DELETE, path1);
3096 if (err)
3097 goto done;
3098 err = remove_ondisk_file(a->worktree->root_path, path1);
3099 if (err)
3100 goto done;
3101 if (ie)
3102 got_fileindex_entry_mark_deleted_from_disk(ie);
3103 break;
3104 case GOT_STATUS_DELETE:
3105 case GOT_STATUS_MISSING:
3106 err = (*a->progress_cb)(a->progress_arg,
3107 GOT_STATUS_DELETE, path1);
3108 if (err)
3109 goto done;
3110 if (ie)
3111 got_fileindex_entry_mark_deleted_from_disk(ie);
3112 break;
3113 case GOT_STATUS_ADD: {
3114 struct got_object_id *id;
3115 FILE *blob1_f;
3116 off_t blob1_size;
3118 * Delete the added file only if its content already
3119 * exists in the repository.
3121 err = got_object_blob_file_create(&id, &blob1_f,
3122 &blob1_size, path1);
3123 if (err)
3124 goto done;
3125 if (got_object_id_cmp(id, id1) == 0) {
3126 err = (*a->progress_cb)(a->progress_arg,
3127 GOT_STATUS_DELETE, path1);
3128 if (err)
3129 goto done;
3130 err = remove_ondisk_file(a->worktree->root_path,
3131 path1);
3132 if (err)
3133 goto done;
3134 if (ie)
3135 got_fileindex_entry_remove(a->fileindex,
3136 ie);
3137 } else {
3138 err = (*a->progress_cb)(a->progress_arg,
3139 GOT_STATUS_CANNOT_DELETE, path1);
3141 if (fclose(blob1_f) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 free(id);
3144 if (err)
3145 goto done;
3146 break;
3148 case GOT_STATUS_MODIFY:
3149 case GOT_STATUS_CONFLICT:
3150 err = (*a->progress_cb)(a->progress_arg,
3151 GOT_STATUS_CANNOT_DELETE, path1);
3152 if (err)
3153 goto done;
3154 break;
3155 case GOT_STATUS_OBSTRUCTED:
3156 err = (*a->progress_cb)(a->progress_arg, status, path1);
3157 if (err)
3158 goto done;
3159 break;
3160 default:
3161 break;
3163 } else if (blob2) {
3164 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3165 path2) == -1)
3166 return got_error_from_errno("asprintf");
3167 ie = got_fileindex_entry_get(a->fileindex, path2,
3168 strlen(path2));
3169 if (ie) {
3170 err = get_file_status(&status, &sb, ie, ondisk_path,
3171 -1, NULL, repo);
3172 if (err)
3173 goto done;
3174 if (status != GOT_STATUS_NO_CHANGE &&
3175 status != GOT_STATUS_MODIFY &&
3176 status != GOT_STATUS_CONFLICT &&
3177 status != GOT_STATUS_ADD &&
3178 status != GOT_STATUS_DELETE) {
3179 err = (*a->progress_cb)(a->progress_arg,
3180 status, path2);
3181 goto done;
3183 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3184 char *link_target2;
3185 err = got_object_blob_read_to_str(&link_target2,
3186 blob2);
3187 if (err)
3188 goto done;
3189 err = merge_symlink(a->worktree, NULL,
3190 ondisk_path, path2, a->label_orig,
3191 link_target2, a->commit_id2, repo,
3192 a->progress_cb, a->progress_arg);
3193 free(link_target2);
3194 } else if (S_ISREG(sb.st_mode)) {
3195 err = merge_blob(&local_changes_subsumed,
3196 a->worktree, NULL, ondisk_path, path2,
3197 sb.st_mode, a->label_orig, blob2,
3198 a->commit_id2, repo, a->progress_cb,
3199 a->progress_arg);
3200 } else if (status != GOT_STATUS_DELETE) {
3201 err = got_error_path(ondisk_path,
3202 GOT_ERR_FILE_OBSTRUCTED);
3204 if (err)
3205 goto done;
3206 if (status == GOT_STATUS_DELETE) {
3207 /* Re-add file with content from new blob. */
3208 err = add_file(a->worktree, a->fileindex, ie,
3209 ondisk_path, path2, blob2, mode2,
3210 0, 0, 0, a->allow_bad_symlinks,
3211 repo, a->progress_cb, a->progress_arg);
3212 if (err)
3213 goto done;
3215 } else {
3216 err = add_file(a->worktree, a->fileindex, NULL,
3217 ondisk_path, path2, blob2, mode2,
3218 0, 0, 1, a->allow_bad_symlinks,
3219 repo, a->progress_cb, a->progress_arg);
3220 if (err)
3221 goto done;
3224 done:
3225 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3226 err = got_error_from_errno("fclose");
3227 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3228 err = got_error_from_errno("fclose");
3229 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3230 err = got_error_from_errno("fclose");
3231 free(id_str);
3232 free(label_deriv2);
3233 free(ondisk_path);
3234 return err;
3237 static const struct got_error *
3238 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3240 struct got_worktree *worktree = arg;
3242 /* Reject merges into a work tree with mixed base commits. */
3243 if (got_fileindex_entry_has_commit(ie) &&
3244 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3245 SHA1_DIGEST_LENGTH) != 0)
3246 return got_error(GOT_ERR_MIXED_COMMITS);
3248 return NULL;
3251 struct check_merge_conflicts_arg {
3252 struct got_worktree *worktree;
3253 struct got_fileindex *fileindex;
3254 struct got_repository *repo;
3257 static const struct got_error *
3258 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3259 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3260 struct got_object_id *id1, struct got_object_id *id2,
3261 const char *path1, const char *path2,
3262 mode_t mode1, mode_t mode2, struct got_repository *repo)
3264 const struct got_error *err = NULL;
3265 struct check_merge_conflicts_arg *a = arg;
3266 unsigned char status;
3267 struct stat sb;
3268 struct got_fileindex_entry *ie;
3269 const char *path = path2 ? path2 : path1;
3270 struct got_object_id *id = id2 ? id2 : id1;
3271 char *ondisk_path;
3273 if (id == NULL)
3274 return NULL;
3276 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3277 if (ie == NULL)
3278 return NULL;
3280 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3281 == -1)
3282 return got_error_from_errno("asprintf");
3284 /* Reject merges into a work tree with conflicted files. */
3285 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3286 free(ondisk_path);
3287 if (err)
3288 return err;
3289 if (status == GOT_STATUS_CONFLICT)
3290 return got_error(GOT_ERR_CONFLICTS);
3292 return NULL;
3295 static const struct got_error *
3296 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3297 const char *fileindex_path, struct got_object_id *commit_id1,
3298 struct got_object_id *commit_id2, struct got_repository *repo,
3299 got_worktree_checkout_cb progress_cb, void *progress_arg,
3300 got_cancel_cb cancel_cb, void *cancel_arg)
3302 const struct got_error *err = NULL, *sync_err;
3303 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3304 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3305 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3306 struct check_merge_conflicts_arg cmc_arg;
3307 struct merge_file_cb_arg arg;
3308 char *label_orig = NULL;
3309 FILE *f1 = NULL, *f2 = NULL;
3310 int fd1 = -1, fd2 = -1;
3312 if (commit_id1) {
3313 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3314 if (err)
3315 goto done;
3316 err = got_object_id_by_path(&tree_id1, repo, commit1,
3317 worktree->path_prefix);
3318 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3319 goto done;
3321 if (tree_id1) {
3322 char *id_str;
3324 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3325 if (err)
3326 goto done;
3328 err = got_object_id_str(&id_str, commit_id1);
3329 if (err)
3330 goto done;
3332 if (asprintf(&label_orig, "%s: commit %s",
3333 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3334 err = got_error_from_errno("asprintf");
3335 free(id_str);
3336 goto done;
3338 free(id_str);
3340 f1 = got_opentemp();
3341 if (f1 == NULL) {
3342 err = got_error_from_errno("got_opentemp");
3343 goto done;
3347 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3348 if (err)
3349 goto done;
3351 err = got_object_id_by_path(&tree_id2, repo, commit2,
3352 worktree->path_prefix);
3353 if (err)
3354 goto done;
3356 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3357 if (err)
3358 goto done;
3360 f2 = got_opentemp();
3361 if (f2 == NULL) {
3362 err = got_error_from_errno("got_opentemp");
3363 goto done;
3366 fd1 = got_opentempfd();
3367 if (fd1 == -1) {
3368 err = got_error_from_errno("got_opentempfd");
3369 goto done;
3372 fd2 = got_opentempfd();
3373 if (fd2 == -1) {
3374 err = got_error_from_errno("got_opentempfd");
3375 goto done;
3378 cmc_arg.worktree = worktree;
3379 cmc_arg.fileindex = fileindex;
3380 cmc_arg.repo = repo;
3381 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3382 check_merge_conflicts, &cmc_arg, 0);
3383 if (err)
3384 goto done;
3386 arg.worktree = worktree;
3387 arg.fileindex = fileindex;
3388 arg.progress_cb = progress_cb;
3389 arg.progress_arg = progress_arg;
3390 arg.cancel_cb = cancel_cb;
3391 arg.cancel_arg = cancel_arg;
3392 arg.label_orig = label_orig;
3393 arg.commit_id2 = commit_id2;
3394 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3395 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3396 merge_file_cb, &arg, 1);
3397 sync_err = sync_fileindex(fileindex, fileindex_path);
3398 if (sync_err && err == NULL)
3399 err = sync_err;
3400 done:
3401 if (commit1)
3402 got_object_commit_close(commit1);
3403 if (commit2)
3404 got_object_commit_close(commit2);
3405 if (tree1)
3406 got_object_tree_close(tree1);
3407 if (tree2)
3408 got_object_tree_close(tree2);
3409 if (f1 && fclose(f1) == EOF && err == NULL)
3410 err = got_error_from_errno("fclose");
3411 if (f2 && fclose(f2) == EOF && err == NULL)
3412 err = got_error_from_errno("fclose");
3413 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3414 err = got_error_from_errno("close");
3415 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3416 err = got_error_from_errno("close");
3417 free(label_orig);
3418 return err;
3421 const struct got_error *
3422 got_worktree_merge_files(struct got_worktree *worktree,
3423 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3424 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3425 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3427 const struct got_error *err, *unlockerr;
3428 char *fileindex_path = NULL;
3429 struct got_fileindex *fileindex = NULL;
3431 err = lock_worktree(worktree, LOCK_EX);
3432 if (err)
3433 return err;
3435 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3436 if (err)
3437 goto done;
3439 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3440 worktree);
3441 if (err)
3442 goto done;
3444 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3445 commit_id2, repo, progress_cb, progress_arg,
3446 cancel_cb, cancel_arg);
3447 done:
3448 if (fileindex)
3449 got_fileindex_free(fileindex);
3450 free(fileindex_path);
3451 unlockerr = lock_worktree(worktree, LOCK_SH);
3452 if (unlockerr && err == NULL)
3453 err = unlockerr;
3454 return err;
3457 struct diff_dir_cb_arg {
3458 struct got_fileindex *fileindex;
3459 struct got_worktree *worktree;
3460 const char *status_path;
3461 size_t status_path_len;
3462 struct got_repository *repo;
3463 got_worktree_status_cb status_cb;
3464 void *status_arg;
3465 got_cancel_cb cancel_cb;
3466 void *cancel_arg;
3467 /* A pathlist containing per-directory pathlists of ignore patterns. */
3468 struct got_pathlist_head *ignores;
3469 int report_unchanged;
3470 int no_ignores;
3473 static const struct got_error *
3474 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3475 int dirfd, const char *de_name,
3476 got_worktree_status_cb status_cb, void *status_arg,
3477 struct got_repository *repo, int report_unchanged)
3479 const struct got_error *err = NULL;
3480 unsigned char status = GOT_STATUS_NO_CHANGE;
3481 unsigned char staged_status;
3482 struct stat sb;
3483 struct got_object_id blob_id, commit_id, staged_blob_id;
3484 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3485 struct got_object_id *staged_blob_idp = NULL;
3487 staged_status = get_staged_status(ie);
3488 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3489 if (err)
3490 return err;
3492 if (status == GOT_STATUS_NO_CHANGE &&
3493 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3494 return NULL;
3496 if (got_fileindex_entry_has_blob(ie))
3497 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3498 if (got_fileindex_entry_has_commit(ie))
3499 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3500 if (staged_status == GOT_STATUS_ADD ||
3501 staged_status == GOT_STATUS_MODIFY) {
3502 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3503 &staged_blob_id, ie);
3506 return (*status_cb)(status_arg, status, staged_status,
3507 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3510 static const struct got_error *
3511 status_old_new(void *arg, struct got_fileindex_entry *ie,
3512 struct dirent *de, const char *parent_path, int dirfd)
3514 const struct got_error *err = NULL;
3515 struct diff_dir_cb_arg *a = arg;
3516 char *abspath;
3518 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3519 return got_error(GOT_ERR_CANCELLED);
3521 if (got_path_cmp(parent_path, a->status_path,
3522 strlen(parent_path), a->status_path_len) != 0 &&
3523 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3524 return NULL;
3526 if (parent_path[0]) {
3527 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3528 parent_path, de->d_name) == -1)
3529 return got_error_from_errno("asprintf");
3530 } else {
3531 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3532 de->d_name) == -1)
3533 return got_error_from_errno("asprintf");
3536 err = report_file_status(ie, abspath, dirfd, de->d_name,
3537 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3538 free(abspath);
3539 return err;
3542 static const struct got_error *
3543 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3545 struct diff_dir_cb_arg *a = arg;
3546 struct got_object_id blob_id, commit_id;
3547 unsigned char status;
3549 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3550 return got_error(GOT_ERR_CANCELLED);
3552 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3553 return NULL;
3555 got_fileindex_entry_get_blob_id(&blob_id, ie);
3556 got_fileindex_entry_get_commit_id(&commit_id, ie);
3557 if (got_fileindex_entry_has_file_on_disk(ie))
3558 status = GOT_STATUS_MISSING;
3559 else
3560 status = GOT_STATUS_DELETE;
3561 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3562 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3565 static void
3566 free_ignores(struct got_pathlist_head *ignores)
3568 struct got_pathlist_entry *pe;
3570 TAILQ_FOREACH(pe, ignores, entry) {
3571 struct got_pathlist_head *ignorelist = pe->data;
3573 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3575 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3578 static const struct got_error *
3579 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3581 const struct got_error *err = NULL;
3582 struct got_pathlist_entry *pe = NULL;
3583 struct got_pathlist_head *ignorelist;
3584 char *line = NULL, *pattern, *dirpath = NULL;
3585 size_t linesize = 0;
3586 ssize_t linelen;
3588 ignorelist = calloc(1, sizeof(*ignorelist));
3589 if (ignorelist == NULL)
3590 return got_error_from_errno("calloc");
3591 TAILQ_INIT(ignorelist);
3593 while ((linelen = getline(&line, &linesize, f)) != -1) {
3594 if (linelen > 0 && line[linelen - 1] == '\n')
3595 line[linelen - 1] = '\0';
3597 /* Git's ignores may contain comments. */
3598 if (line[0] == '#')
3599 continue;
3601 /* Git's negated patterns are not (yet?) supported. */
3602 if (line[0] == '!')
3603 continue;
3605 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3606 line) == -1) {
3607 err = got_error_from_errno("asprintf");
3608 goto done;
3610 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3611 if (err)
3612 goto done;
3614 if (ferror(f)) {
3615 err = got_error_from_errno("getline");
3616 goto done;
3619 dirpath = strdup(path);
3620 if (dirpath == NULL) {
3621 err = got_error_from_errno("strdup");
3622 goto done;
3624 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3625 done:
3626 free(line);
3627 if (err || pe == NULL) {
3628 free(dirpath);
3629 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3631 return err;
3634 static int
3635 match_path(const char *pattern, size_t pattern_len, const char *path,
3636 int flags)
3638 char buf[PATH_MAX];
3641 * Trailing slashes signify directories.
3642 * Append a * to make such patterns conform to fnmatch rules.
3644 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3645 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3646 return FNM_NOMATCH; /* XXX */
3648 return fnmatch(buf, path, flags);
3651 return fnmatch(pattern, path, flags);
3654 static int
3655 match_ignores(struct got_pathlist_head *ignores, const char *path)
3657 struct got_pathlist_entry *pe;
3659 /* Handle patterns which match in all directories. */
3660 TAILQ_FOREACH(pe, ignores, entry) {
3661 struct got_pathlist_head *ignorelist = pe->data;
3662 struct got_pathlist_entry *pi;
3664 TAILQ_FOREACH(pi, ignorelist, entry) {
3665 const char *p;
3667 if (pi->path_len < 3 ||
3668 strncmp(pi->path, "**/", 3) != 0)
3669 continue;
3670 p = path;
3671 while (*p) {
3672 if (match_path(pi->path + 3,
3673 pi->path_len - 3, p,
3674 FNM_PATHNAME | FNM_LEADING_DIR)) {
3675 /* Retry in next directory. */
3676 while (*p && *p != '/')
3677 p++;
3678 while (*p == '/')
3679 p++;
3680 continue;
3682 return 1;
3688 * The ignores pathlist contains ignore lists from children before
3689 * parents, so we can find the most specific ignorelist by walking
3690 * ignores backwards.
3692 pe = TAILQ_LAST(ignores, got_pathlist_head);
3693 while (pe) {
3694 if (got_path_is_child(path, pe->path, pe->path_len)) {
3695 struct got_pathlist_head *ignorelist = pe->data;
3696 struct got_pathlist_entry *pi;
3697 TAILQ_FOREACH(pi, ignorelist, entry) {
3698 int flags = FNM_LEADING_DIR;
3699 if (strstr(pi->path, "/**/") == NULL)
3700 flags |= FNM_PATHNAME;
3701 if (match_path(pi->path, pi->path_len,
3702 path, flags))
3703 continue;
3704 return 1;
3707 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3710 return 0;
3713 static const struct got_error *
3714 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3715 const char *path, int dirfd, const char *ignores_filename)
3717 const struct got_error *err = NULL;
3718 char *ignorespath;
3719 int fd = -1;
3720 FILE *ignoresfile = NULL;
3722 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3723 path[0] ? "/" : "", ignores_filename) == -1)
3724 return got_error_from_errno("asprintf");
3726 if (dirfd != -1) {
3727 fd = openat(dirfd, ignores_filename,
3728 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3729 if (fd == -1) {
3730 if (errno != ENOENT && errno != EACCES)
3731 err = got_error_from_errno2("openat",
3732 ignorespath);
3733 } else {
3734 ignoresfile = fdopen(fd, "r");
3735 if (ignoresfile == NULL)
3736 err = got_error_from_errno2("fdopen",
3737 ignorespath);
3738 else {
3739 fd = -1;
3740 err = read_ignores(ignores, path, ignoresfile);
3743 } else {
3744 ignoresfile = fopen(ignorespath, "re");
3745 if (ignoresfile == NULL) {
3746 if (errno != ENOENT && errno != EACCES)
3747 err = got_error_from_errno2("fopen",
3748 ignorespath);
3749 } else
3750 err = read_ignores(ignores, path, ignoresfile);
3753 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3754 err = got_error_from_errno2("fclose", path);
3755 if (fd != -1 && close(fd) == -1 && err == NULL)
3756 err = got_error_from_errno2("close", path);
3757 free(ignorespath);
3758 return err;
3761 static const struct got_error *
3762 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3763 int dirfd)
3765 const struct got_error *err = NULL;
3766 struct diff_dir_cb_arg *a = arg;
3767 char *path = NULL;
3769 if (ignore != NULL)
3770 *ignore = 0;
3772 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3773 return got_error(GOT_ERR_CANCELLED);
3775 if (parent_path[0]) {
3776 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3777 return got_error_from_errno("asprintf");
3778 } else {
3779 path = de->d_name;
3782 if (de->d_type == DT_DIR) {
3783 if (!a->no_ignores && ignore != NULL &&
3784 match_ignores(a->ignores, path))
3785 *ignore = 1;
3786 } else if (!match_ignores(a->ignores, path) &&
3787 got_path_is_child(path, a->status_path, a->status_path_len))
3788 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3789 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3790 if (parent_path[0])
3791 free(path);
3792 return err;
3795 static const struct got_error *
3796 status_traverse(void *arg, const char *path, int dirfd)
3798 const struct got_error *err = NULL;
3799 struct diff_dir_cb_arg *a = arg;
3801 if (a->no_ignores)
3802 return NULL;
3804 err = add_ignores(a->ignores, a->worktree->root_path,
3805 path, dirfd, ".cvsignore");
3806 if (err)
3807 return err;
3809 err = add_ignores(a->ignores, a->worktree->root_path, path,
3810 dirfd, ".gitignore");
3812 return err;
3815 static const struct got_error *
3816 report_single_file_status(const char *path, const char *ondisk_path,
3817 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3818 void *status_arg, struct got_repository *repo, int report_unchanged,
3819 struct got_pathlist_head *ignores, int no_ignores)
3821 struct got_fileindex_entry *ie;
3822 struct stat sb;
3824 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3825 if (ie)
3826 return report_file_status(ie, ondisk_path, -1, NULL,
3827 status_cb, status_arg, repo, report_unchanged);
3829 if (lstat(ondisk_path, &sb) == -1) {
3830 if (errno != ENOENT)
3831 return got_error_from_errno2("lstat", ondisk_path);
3832 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3833 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3836 if (!no_ignores && match_ignores(ignores, path))
3837 return NULL;
3839 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3840 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3841 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3843 return NULL;
3846 static const struct got_error *
3847 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3848 const char *root_path, const char *path)
3850 const struct got_error *err;
3851 char *parent_path, *next_parent_path = NULL;
3853 err = add_ignores(ignores, root_path, "", -1,
3854 ".cvsignore");
3855 if (err)
3856 return err;
3858 err = add_ignores(ignores, root_path, "", -1,
3859 ".gitignore");
3860 if (err)
3861 return err;
3863 err = got_path_dirname(&parent_path, path);
3864 if (err) {
3865 if (err->code == GOT_ERR_BAD_PATH)
3866 return NULL; /* cannot traverse parent */
3867 return err;
3869 for (;;) {
3870 err = add_ignores(ignores, root_path, parent_path, -1,
3871 ".cvsignore");
3872 if (err)
3873 break;
3874 err = add_ignores(ignores, root_path, parent_path, -1,
3875 ".gitignore");
3876 if (err)
3877 break;
3878 err = got_path_dirname(&next_parent_path, parent_path);
3879 if (err) {
3880 if (err->code == GOT_ERR_BAD_PATH)
3881 err = NULL; /* traversed everything */
3882 break;
3884 if (got_path_is_root_dir(parent_path))
3885 break;
3886 free(parent_path);
3887 parent_path = next_parent_path;
3888 next_parent_path = NULL;
3891 free(parent_path);
3892 free(next_parent_path);
3893 return err;
3896 struct find_missing_children_args {
3897 const char *parent_path;
3898 size_t parent_len;
3899 struct got_pathlist_head *children;
3900 got_cancel_cb cancel_cb;
3901 void *cancel_arg;
3904 static const struct got_error *
3905 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3907 const struct got_error *err = NULL;
3908 struct find_missing_children_args *a = arg;
3910 if (a->cancel_cb) {
3911 err = a->cancel_cb(a->cancel_arg);
3912 if (err)
3913 return err;
3916 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3917 err = got_pathlist_append(a->children, ie->path, NULL);
3919 return err;
3922 static const struct got_error *
3923 report_children(struct got_pathlist_head *children,
3924 struct got_worktree *worktree, struct got_fileindex *fileindex,
3925 struct got_repository *repo, int is_root_dir, int report_unchanged,
3926 struct got_pathlist_head *ignores, int no_ignores,
3927 got_worktree_status_cb status_cb, void *status_arg,
3928 got_cancel_cb cancel_cb, void *cancel_arg)
3930 const struct got_error *err = NULL;
3931 struct got_pathlist_entry *pe;
3932 char *ondisk_path = NULL;
3934 TAILQ_FOREACH(pe, children, entry) {
3935 if (cancel_cb) {
3936 err = cancel_cb(cancel_arg);
3937 if (err)
3938 break;
3941 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3942 !is_root_dir ? "/" : "", pe->path) == -1) {
3943 err = got_error_from_errno("asprintf");
3944 ondisk_path = NULL;
3945 break;
3948 err = report_single_file_status(pe->path, ondisk_path,
3949 fileindex, status_cb, status_arg, repo, report_unchanged,
3950 ignores, no_ignores);
3951 if (err)
3952 break;
3954 free(ondisk_path);
3955 ondisk_path = NULL;
3958 free(ondisk_path);
3959 return err;
3962 static const struct got_error *
3963 worktree_status(struct got_worktree *worktree, const char *path,
3964 struct got_fileindex *fileindex, struct got_repository *repo,
3965 got_worktree_status_cb status_cb, void *status_arg,
3966 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3967 int report_unchanged)
3969 const struct got_error *err = NULL;
3970 int fd = -1;
3971 struct got_fileindex_diff_dir_cb fdiff_cb;
3972 struct diff_dir_cb_arg arg;
3973 char *ondisk_path = NULL;
3974 struct got_pathlist_head ignores, missing_children;
3975 struct got_fileindex_entry *ie;
3977 TAILQ_INIT(&ignores);
3978 TAILQ_INIT(&missing_children);
3980 if (asprintf(&ondisk_path, "%s%s%s",
3981 worktree->root_path, path[0] ? "/" : "", path) == -1)
3982 return got_error_from_errno("asprintf");
3984 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3985 if (ie) {
3986 err = report_single_file_status(path, ondisk_path,
3987 fileindex, status_cb, status_arg, repo,
3988 report_unchanged, &ignores, no_ignores);
3989 goto done;
3990 } else {
3991 struct find_missing_children_args fmca;
3992 fmca.parent_path = path;
3993 fmca.parent_len = strlen(path);
3994 fmca.children = &missing_children;
3995 fmca.cancel_cb = cancel_cb;
3996 fmca.cancel_arg = cancel_arg;
3997 err = got_fileindex_for_each_entry_safe(fileindex,
3998 find_missing_children, &fmca);
3999 if (err)
4000 goto done;
4003 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4004 if (fd == -1) {
4005 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4006 !got_err_open_nofollow_on_symlink())
4007 err = got_error_from_errno2("open", ondisk_path);
4008 else {
4009 if (!no_ignores) {
4010 err = add_ignores_from_parent_paths(&ignores,
4011 worktree->root_path, ondisk_path);
4012 if (err)
4013 goto done;
4015 if (TAILQ_EMPTY(&missing_children)) {
4016 err = report_single_file_status(path,
4017 ondisk_path, fileindex,
4018 status_cb, status_arg, repo,
4019 report_unchanged, &ignores, no_ignores);
4020 if (err)
4021 goto done;
4022 } else {
4023 err = report_children(&missing_children,
4024 worktree, fileindex, repo,
4025 (path[0] == '\0'), report_unchanged,
4026 &ignores, no_ignores,
4027 status_cb, status_arg,
4028 cancel_cb, cancel_arg);
4029 if (err)
4030 goto done;
4033 } else {
4034 fdiff_cb.diff_old_new = status_old_new;
4035 fdiff_cb.diff_old = status_old;
4036 fdiff_cb.diff_new = status_new;
4037 fdiff_cb.diff_traverse = status_traverse;
4038 arg.fileindex = fileindex;
4039 arg.worktree = worktree;
4040 arg.status_path = path;
4041 arg.status_path_len = strlen(path);
4042 arg.repo = repo;
4043 arg.status_cb = status_cb;
4044 arg.status_arg = status_arg;
4045 arg.cancel_cb = cancel_cb;
4046 arg.cancel_arg = cancel_arg;
4047 arg.report_unchanged = report_unchanged;
4048 arg.no_ignores = no_ignores;
4049 if (!no_ignores) {
4050 err = add_ignores_from_parent_paths(&ignores,
4051 worktree->root_path, path);
4052 if (err)
4053 goto done;
4055 arg.ignores = &ignores;
4056 err = got_fileindex_diff_dir(fileindex, fd,
4057 worktree->root_path, path, repo, &fdiff_cb, &arg);
4059 done:
4060 free_ignores(&ignores);
4061 if (fd != -1 && close(fd) == -1 && err == NULL)
4062 err = got_error_from_errno("close");
4063 free(ondisk_path);
4064 return err;
4067 const struct got_error *
4068 got_worktree_status(struct got_worktree *worktree,
4069 struct got_pathlist_head *paths, struct got_repository *repo,
4070 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4071 got_cancel_cb cancel_cb, void *cancel_arg)
4073 const struct got_error *err = NULL;
4074 char *fileindex_path = NULL;
4075 struct got_fileindex *fileindex = NULL;
4076 struct got_pathlist_entry *pe;
4078 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4079 if (err)
4080 return err;
4082 TAILQ_FOREACH(pe, paths, entry) {
4083 err = worktree_status(worktree, pe->path, fileindex, repo,
4084 status_cb, status_arg, cancel_cb, cancel_arg,
4085 no_ignores, 0);
4086 if (err)
4087 break;
4089 free(fileindex_path);
4090 got_fileindex_free(fileindex);
4091 return err;
4094 const struct got_error *
4095 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4096 const char *arg)
4098 const struct got_error *err = NULL;
4099 char *resolved = NULL, *cwd = NULL, *path = NULL;
4100 size_t len;
4101 struct stat sb;
4102 char *abspath = NULL;
4103 char canonpath[PATH_MAX];
4105 *wt_path = NULL;
4107 cwd = getcwd(NULL, 0);
4108 if (cwd == NULL)
4109 return got_error_from_errno("getcwd");
4111 if (lstat(arg, &sb) == -1) {
4112 if (errno != ENOENT) {
4113 err = got_error_from_errno2("lstat", arg);
4114 goto done;
4116 sb.st_mode = 0;
4118 if (S_ISLNK(sb.st_mode)) {
4120 * We cannot use realpath(3) with symlinks since we want to
4121 * operate on the symlink itself.
4122 * But we can make the path absolute, assuming it is relative
4123 * to the current working directory, and then canonicalize it.
4125 if (!got_path_is_absolute(arg)) {
4126 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4127 err = got_error_from_errno("asprintf");
4128 goto done;
4132 err = got_canonpath(abspath ? abspath : arg, canonpath,
4133 sizeof(canonpath));
4134 if (err)
4135 goto done;
4136 resolved = strdup(canonpath);
4137 if (resolved == NULL) {
4138 err = got_error_from_errno("strdup");
4139 goto done;
4141 } else {
4142 resolved = realpath(arg, NULL);
4143 if (resolved == NULL) {
4144 if (errno != ENOENT) {
4145 err = got_error_from_errno2("realpath", arg);
4146 goto done;
4148 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4149 err = got_error_from_errno("asprintf");
4150 goto done;
4152 err = got_canonpath(abspath, canonpath,
4153 sizeof(canonpath));
4154 if (err)
4155 goto done;
4156 resolved = strdup(canonpath);
4157 if (resolved == NULL) {
4158 err = got_error_from_errno("strdup");
4159 goto done;
4164 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4165 strlen(got_worktree_get_root_path(worktree)))) {
4166 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4167 goto done;
4170 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4171 err = got_path_skip_common_ancestor(&path,
4172 got_worktree_get_root_path(worktree), resolved);
4173 if (err)
4174 goto done;
4175 } else {
4176 path = strdup("");
4177 if (path == NULL) {
4178 err = got_error_from_errno("strdup");
4179 goto done;
4183 /* XXX status walk can't deal with trailing slash! */
4184 len = strlen(path);
4185 while (len > 0 && path[len - 1] == '/') {
4186 path[len - 1] = '\0';
4187 len--;
4189 done:
4190 free(abspath);
4191 free(resolved);
4192 free(cwd);
4193 if (err == NULL)
4194 *wt_path = path;
4195 else
4196 free(path);
4197 return err;
4200 struct schedule_addition_args {
4201 struct got_worktree *worktree;
4202 struct got_fileindex *fileindex;
4203 got_worktree_checkout_cb progress_cb;
4204 void *progress_arg;
4205 struct got_repository *repo;
4208 static int
4209 add_noop_status(unsigned char status)
4211 return (status == GOT_STATUS_ADD ||
4212 status == GOT_STATUS_MODIFY ||
4213 status == GOT_STATUS_CONFLICT ||
4214 status == GOT_STATUS_MODE_CHANGE ||
4215 status == GOT_STATUS_NO_CHANGE);
4218 static const struct got_error *
4219 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4220 const char *relpath, struct got_object_id *blob_id,
4221 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4222 int dirfd, const char *de_name)
4224 struct schedule_addition_args *a = arg;
4225 const struct got_error *err = NULL;
4226 struct got_fileindex_entry *ie;
4227 struct stat sb;
4228 char *ondisk_path;
4230 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4231 relpath) == -1)
4232 return got_error_from_errno("asprintf");
4234 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4235 if (ie) {
4236 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4237 de_name, a->repo);
4238 if (err)
4239 goto done;
4240 /* Re-adding an existing entry is a no-op. */
4241 if (staged_status == GOT_STATUS_NO_CHANGE &&
4242 add_noop_status(status))
4243 goto done;
4244 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4245 if (err)
4246 goto done;
4249 if (status != GOT_STATUS_UNVERSIONED) {
4250 if (status == GOT_STATUS_NONEXISTENT)
4251 err = got_error_set_errno(ENOENT, ondisk_path);
4252 else
4253 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4254 goto done;
4257 err = got_fileindex_entry_alloc(&ie, relpath);
4258 if (err)
4259 goto done;
4260 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4261 relpath, NULL, NULL, 1);
4262 if (err) {
4263 got_fileindex_entry_free(ie);
4264 goto done;
4266 err = got_fileindex_entry_add(a->fileindex, ie);
4267 if (err) {
4268 got_fileindex_entry_free(ie);
4269 goto done;
4271 done:
4272 free(ondisk_path);
4273 if (err)
4274 return err;
4275 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4276 return NULL;
4277 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4280 const struct got_error *
4281 got_worktree_schedule_add(struct got_worktree *worktree,
4282 struct got_pathlist_head *paths,
4283 got_worktree_checkout_cb progress_cb, void *progress_arg,
4284 struct got_repository *repo, int no_ignores)
4286 struct got_fileindex *fileindex = NULL;
4287 char *fileindex_path = NULL;
4288 const struct got_error *err = NULL, *sync_err, *unlockerr;
4289 struct got_pathlist_entry *pe;
4290 struct schedule_addition_args saa;
4292 err = lock_worktree(worktree, LOCK_EX);
4293 if (err)
4294 return err;
4296 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4297 if (err)
4298 goto done;
4300 saa.worktree = worktree;
4301 saa.fileindex = fileindex;
4302 saa.progress_cb = progress_cb;
4303 saa.progress_arg = progress_arg;
4304 saa.repo = repo;
4306 TAILQ_FOREACH(pe, paths, entry) {
4307 err = worktree_status(worktree, pe->path, fileindex, repo,
4308 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4309 if (err)
4310 break;
4312 sync_err = sync_fileindex(fileindex, fileindex_path);
4313 if (sync_err && err == NULL)
4314 err = sync_err;
4315 done:
4316 free(fileindex_path);
4317 if (fileindex)
4318 got_fileindex_free(fileindex);
4319 unlockerr = lock_worktree(worktree, LOCK_SH);
4320 if (unlockerr && err == NULL)
4321 err = unlockerr;
4322 return err;
4325 struct schedule_deletion_args {
4326 struct got_worktree *worktree;
4327 struct got_fileindex *fileindex;
4328 got_worktree_delete_cb progress_cb;
4329 void *progress_arg;
4330 struct got_repository *repo;
4331 int delete_local_mods;
4332 int keep_on_disk;
4333 int ignore_missing_paths;
4334 const char *status_path;
4335 size_t status_path_len;
4336 const char *status_codes;
4339 static const struct got_error *
4340 schedule_for_deletion(void *arg, unsigned char status,
4341 unsigned char staged_status, const char *relpath,
4342 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4343 struct got_object_id *commit_id, int dirfd, const char *de_name)
4345 struct schedule_deletion_args *a = arg;
4346 const struct got_error *err = NULL;
4347 struct got_fileindex_entry *ie = NULL;
4348 struct stat sb;
4349 char *ondisk_path;
4351 if (status == GOT_STATUS_NONEXISTENT) {
4352 if (a->ignore_missing_paths)
4353 return NULL;
4354 return got_error_set_errno(ENOENT, relpath);
4357 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4358 if (ie == NULL)
4359 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4361 staged_status = get_staged_status(ie);
4362 if (staged_status != GOT_STATUS_NO_CHANGE) {
4363 if (staged_status == GOT_STATUS_DELETE)
4364 return NULL;
4365 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4368 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4369 relpath) == -1)
4370 return got_error_from_errno("asprintf");
4372 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4373 a->repo);
4374 if (err)
4375 goto done;
4377 if (a->status_codes) {
4378 size_t ncodes = strlen(a->status_codes);
4379 int i;
4380 for (i = 0; i < ncodes ; i++) {
4381 if (status == a->status_codes[i])
4382 break;
4384 if (i == ncodes) {
4385 /* Do not delete files in non-matching status. */
4386 free(ondisk_path);
4387 return NULL;
4389 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4390 a->status_codes[i] != GOT_STATUS_MISSING) {
4391 static char msg[64];
4392 snprintf(msg, sizeof(msg),
4393 "invalid status code '%c'", a->status_codes[i]);
4394 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4395 goto done;
4399 if (status != GOT_STATUS_NO_CHANGE) {
4400 if (status == GOT_STATUS_DELETE)
4401 goto done;
4402 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4403 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4404 goto done;
4406 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4407 err = got_error_set_errno(ENOENT, relpath);
4408 goto done;
4410 if (status != GOT_STATUS_MODIFY &&
4411 status != GOT_STATUS_MISSING) {
4412 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4413 goto done;
4417 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4418 size_t root_len;
4420 if (dirfd != -1) {
4421 if (unlinkat(dirfd, de_name, 0) == -1) {
4422 err = got_error_from_errno2("unlinkat",
4423 ondisk_path);
4424 goto done;
4426 } else if (unlink(ondisk_path) == -1) {
4427 err = got_error_from_errno2("unlink", ondisk_path);
4428 goto done;
4431 root_len = strlen(a->worktree->root_path);
4432 do {
4433 char *parent;
4435 err = got_path_dirname(&parent, ondisk_path);
4436 if (err)
4437 goto done;
4438 free(ondisk_path);
4439 ondisk_path = parent;
4440 if (got_path_cmp(ondisk_path, a->status_path,
4441 strlen(ondisk_path), a->status_path_len) != 0 &&
4442 !got_path_is_child(ondisk_path, a->status_path,
4443 a->status_path_len))
4444 break;
4445 if (rmdir(ondisk_path) == -1) {
4446 if (errno != ENOTEMPTY)
4447 err = got_error_from_errno2("rmdir",
4448 ondisk_path);
4449 break;
4451 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4452 strlen(ondisk_path), root_len) != 0);
4455 got_fileindex_entry_mark_deleted_from_disk(ie);
4456 done:
4457 free(ondisk_path);
4458 if (err)
4459 return err;
4460 if (status == GOT_STATUS_DELETE)
4461 return NULL;
4462 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4463 staged_status, relpath);
4466 const struct got_error *
4467 got_worktree_schedule_delete(struct got_worktree *worktree,
4468 struct got_pathlist_head *paths, int delete_local_mods,
4469 const char *status_codes,
4470 got_worktree_delete_cb progress_cb, void *progress_arg,
4471 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4473 struct got_fileindex *fileindex = NULL;
4474 char *fileindex_path = NULL;
4475 const struct got_error *err = NULL, *sync_err, *unlockerr;
4476 struct got_pathlist_entry *pe;
4477 struct schedule_deletion_args sda;
4479 err = lock_worktree(worktree, LOCK_EX);
4480 if (err)
4481 return err;
4483 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4484 if (err)
4485 goto done;
4487 sda.worktree = worktree;
4488 sda.fileindex = fileindex;
4489 sda.progress_cb = progress_cb;
4490 sda.progress_arg = progress_arg;
4491 sda.repo = repo;
4492 sda.delete_local_mods = delete_local_mods;
4493 sda.keep_on_disk = keep_on_disk;
4494 sda.ignore_missing_paths = ignore_missing_paths;
4495 sda.status_codes = status_codes;
4497 TAILQ_FOREACH(pe, paths, entry) {
4498 char *ondisk_status_path;
4500 if (asprintf(&ondisk_status_path, "%s%s%s",
4501 got_worktree_get_root_path(worktree),
4502 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4503 err = got_error_from_errno("asprintf");
4504 goto done;
4506 sda.status_path = ondisk_status_path;
4507 sda.status_path_len = strlen(ondisk_status_path);
4508 err = worktree_status(worktree, pe->path, fileindex, repo,
4509 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4510 free(ondisk_status_path);
4511 if (err)
4512 break;
4514 sync_err = sync_fileindex(fileindex, fileindex_path);
4515 if (sync_err && err == NULL)
4516 err = sync_err;
4517 done:
4518 free(fileindex_path);
4519 if (fileindex)
4520 got_fileindex_free(fileindex);
4521 unlockerr = lock_worktree(worktree, LOCK_SH);
4522 if (unlockerr && err == NULL)
4523 err = unlockerr;
4524 return err;
4527 static const struct got_error *
4528 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4530 const struct got_error *err = NULL;
4531 char *line = NULL;
4532 size_t linesize = 0, n;
4533 ssize_t linelen;
4535 linelen = getline(&line, &linesize, infile);
4536 if (linelen == -1) {
4537 if (ferror(infile)) {
4538 err = got_error_from_errno("getline");
4539 goto done;
4541 return NULL;
4543 if (outfile) {
4544 n = fwrite(line, 1, linelen, outfile);
4545 if (n != linelen) {
4546 err = got_ferror(outfile, GOT_ERR_IO);
4547 goto done;
4550 if (rejectfile) {
4551 n = fwrite(line, 1, linelen, rejectfile);
4552 if (n != linelen)
4553 err = got_ferror(rejectfile, GOT_ERR_IO);
4555 done:
4556 free(line);
4557 return err;
4560 static const struct got_error *
4561 skip_one_line(FILE *f)
4563 char *line = NULL;
4564 size_t linesize = 0;
4565 ssize_t linelen;
4567 linelen = getline(&line, &linesize, f);
4568 if (linelen == -1) {
4569 if (ferror(f))
4570 return got_error_from_errno("getline");
4571 return NULL;
4573 free(line);
4574 return NULL;
4577 static const struct got_error *
4578 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4579 int start_old, int end_old, int start_new, int end_new,
4580 FILE *outfile, FILE *rejectfile)
4582 const struct got_error *err;
4584 /* Copy old file's lines leading up to patch. */
4585 while (!feof(f1) && *line_cur1 < start_old) {
4586 err = copy_one_line(f1, outfile, NULL);
4587 if (err)
4588 return err;
4589 (*line_cur1)++;
4591 /* Skip new file's lines leading up to patch. */
4592 while (!feof(f2) && *line_cur2 < start_new) {
4593 if (rejectfile)
4594 err = copy_one_line(f2, NULL, rejectfile);
4595 else
4596 err = skip_one_line(f2);
4597 if (err)
4598 return err;
4599 (*line_cur2)++;
4601 /* Copy patched lines. */
4602 while (!feof(f2) && *line_cur2 <= end_new) {
4603 err = copy_one_line(f2, outfile, NULL);
4604 if (err)
4605 return err;
4606 (*line_cur2)++;
4608 /* Skip over old file's replaced lines. */
4609 while (!feof(f1) && *line_cur1 <= end_old) {
4610 if (rejectfile)
4611 err = copy_one_line(f1, NULL, rejectfile);
4612 else
4613 err = skip_one_line(f1);
4614 if (err)
4615 return err;
4616 (*line_cur1)++;
4619 return NULL;
4622 static const struct got_error *
4623 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4624 FILE *outfile, FILE *rejectfile)
4626 const struct got_error *err;
4628 if (outfile) {
4629 /* Copy old file's lines until EOF. */
4630 while (!feof(f1)) {
4631 err = copy_one_line(f1, outfile, NULL);
4632 if (err)
4633 return err;
4634 (*line_cur1)++;
4637 if (rejectfile) {
4638 /* Copy new file's lines until EOF. */
4639 while (!feof(f2)) {
4640 err = copy_one_line(f2, NULL, rejectfile);
4641 if (err)
4642 return err;
4643 (*line_cur2)++;
4647 return NULL;
4650 static const struct got_error *
4651 apply_or_reject_change(int *choice, int *nchunks_used,
4652 struct diff_result *diff_result, int n,
4653 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4654 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4655 got_worktree_patch_cb patch_cb, void *patch_arg)
4657 const struct got_error *err = NULL;
4658 struct diff_chunk_context cc = {};
4659 int start_old, end_old, start_new, end_new;
4660 FILE *hunkfile;
4661 struct diff_output_unidiff_state *diff_state;
4662 struct diff_input_info diff_info;
4663 int rc;
4665 *choice = GOT_PATCH_CHOICE_NONE;
4667 /* Get changed line numbers without context lines for copy_change(). */
4668 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4669 start_old = cc.left.start;
4670 end_old = cc.left.end;
4671 start_new = cc.right.start;
4672 end_new = cc.right.end;
4674 /* Get the same change with context lines for display. */
4675 memset(&cc, 0, sizeof(cc));
4676 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4678 memset(&diff_info, 0, sizeof(diff_info));
4679 diff_info.left_path = relpath;
4680 diff_info.right_path = relpath;
4682 diff_state = diff_output_unidiff_state_alloc();
4683 if (diff_state == NULL)
4684 return got_error_set_errno(ENOMEM,
4685 "diff_output_unidiff_state_alloc");
4687 hunkfile = got_opentemp();
4688 if (hunkfile == NULL) {
4689 err = got_error_from_errno("got_opentemp");
4690 goto done;
4693 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4694 diff_result, &cc);
4695 if (rc != DIFF_RC_OK) {
4696 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4697 goto done;
4700 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4701 err = got_ferror(hunkfile, GOT_ERR_IO);
4702 goto done;
4705 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4706 hunkfile, changeno, nchanges);
4707 if (err)
4708 goto done;
4710 switch (*choice) {
4711 case GOT_PATCH_CHOICE_YES:
4712 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4713 end_old, start_new, end_new, outfile, rejectfile);
4714 break;
4715 case GOT_PATCH_CHOICE_NO:
4716 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4717 end_old, start_new, end_new, rejectfile, outfile);
4718 break;
4719 case GOT_PATCH_CHOICE_QUIT:
4720 break;
4721 default:
4722 err = got_error(GOT_ERR_PATCH_CHOICE);
4723 break;
4725 done:
4726 diff_output_unidiff_state_free(diff_state);
4727 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4728 err = got_error_from_errno("fclose");
4729 return err;
4732 struct revert_file_args {
4733 struct got_worktree *worktree;
4734 struct got_fileindex *fileindex;
4735 got_worktree_checkout_cb progress_cb;
4736 void *progress_arg;
4737 got_worktree_patch_cb patch_cb;
4738 void *patch_arg;
4739 struct got_repository *repo;
4740 int unlink_added_files;
4741 struct got_pathlist_head *added_files_to_unlink;
4744 static const struct got_error *
4745 create_patched_content(char **path_outfile, int reverse_patch,
4746 struct got_object_id *blob_id, const char *path2,
4747 int dirfd2, const char *de_name2,
4748 const char *relpath, struct got_repository *repo,
4749 got_worktree_patch_cb patch_cb, void *patch_arg)
4751 const struct got_error *err, *free_err;
4752 struct got_blob_object *blob = NULL;
4753 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4754 int fd = -1, fd2 = -1;
4755 char link_target[PATH_MAX];
4756 ssize_t link_len = 0;
4757 char *path1 = NULL, *id_str = NULL;
4758 struct stat sb2;
4759 struct got_diffreg_result *diffreg_result = NULL;
4760 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4761 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4763 *path_outfile = NULL;
4765 err = got_object_id_str(&id_str, blob_id);
4766 if (err)
4767 return err;
4769 if (dirfd2 != -1) {
4770 fd2 = openat(dirfd2, de_name2,
4771 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4772 if (fd2 == -1) {
4773 if (!got_err_open_nofollow_on_symlink()) {
4774 err = got_error_from_errno2("openat", path2);
4775 goto done;
4777 link_len = readlinkat(dirfd2, de_name2,
4778 link_target, sizeof(link_target));
4779 if (link_len == -1) {
4780 return got_error_from_errno2("readlinkat",
4781 path2);
4783 sb2.st_mode = S_IFLNK;
4784 sb2.st_size = link_len;
4786 } else {
4787 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4788 if (fd2 == -1) {
4789 if (!got_err_open_nofollow_on_symlink()) {
4790 err = got_error_from_errno2("open", path2);
4791 goto done;
4793 link_len = readlink(path2, link_target,
4794 sizeof(link_target));
4795 if (link_len == -1)
4796 return got_error_from_errno2("readlink", path2);
4797 sb2.st_mode = S_IFLNK;
4798 sb2.st_size = link_len;
4801 if (fd2 != -1) {
4802 if (fstat(fd2, &sb2) == -1) {
4803 err = got_error_from_errno2("fstat", path2);
4804 goto done;
4807 f2 = fdopen(fd2, "r");
4808 if (f2 == NULL) {
4809 err = got_error_from_errno2("fdopen", path2);
4810 goto done;
4812 fd2 = -1;
4813 } else {
4814 size_t n;
4815 f2 = got_opentemp();
4816 if (f2 == NULL) {
4817 err = got_error_from_errno2("got_opentemp", path2);
4818 goto done;
4820 n = fwrite(link_target, 1, link_len, f2);
4821 if (n != link_len) {
4822 err = got_ferror(f2, GOT_ERR_IO);
4823 goto done;
4825 if (fflush(f2) == EOF) {
4826 err = got_error_from_errno("fflush");
4827 goto done;
4829 rewind(f2);
4832 fd = got_opentempfd();
4833 if (fd == -1) {
4834 err = got_error_from_errno("got_opentempfd");
4835 goto done;
4838 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4839 if (err)
4840 goto done;
4842 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4843 if (err)
4844 goto done;
4846 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4847 if (err)
4848 goto done;
4850 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4851 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4852 if (err)
4853 goto done;
4855 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4856 "");
4857 if (err)
4858 goto done;
4860 if (fseek(f1, 0L, SEEK_SET) == -1)
4861 return got_ferror(f1, GOT_ERR_IO);
4862 if (fseek(f2, 0L, SEEK_SET) == -1)
4863 return got_ferror(f2, GOT_ERR_IO);
4865 /* Count the number of actual changes in the diff result. */
4866 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4867 struct diff_chunk_context cc = {};
4868 diff_chunk_context_load_change(&cc, &nchunks_used,
4869 diffreg_result->result, n, 0);
4870 nchanges++;
4872 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4873 int choice;
4874 err = apply_or_reject_change(&choice, &nchunks_used,
4875 diffreg_result->result, n, relpath, f1, f2,
4876 &line_cur1, &line_cur2,
4877 reverse_patch ? NULL : outfile,
4878 reverse_patch ? outfile : NULL,
4879 ++i, nchanges, patch_cb, patch_arg);
4880 if (err)
4881 goto done;
4882 if (choice == GOT_PATCH_CHOICE_YES)
4883 have_content = 1;
4884 else if (choice == GOT_PATCH_CHOICE_QUIT)
4885 break;
4887 if (have_content) {
4888 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4889 reverse_patch ? NULL : outfile,
4890 reverse_patch ? outfile : NULL);
4891 if (err)
4892 goto done;
4894 if (!S_ISLNK(sb2.st_mode)) {
4895 mode_t mode;
4897 mode = apply_umask(sb2.st_mode);
4898 if (fchmod(fileno(outfile), mode) == -1) {
4899 err = got_error_from_errno2("fchmod", path2);
4900 goto done;
4904 done:
4905 free(id_str);
4906 if (fd != -1 && close(fd) == -1 && err == NULL)
4907 err = got_error_from_errno("close");
4908 if (blob)
4909 got_object_blob_close(blob);
4910 free_err = got_diffreg_result_free(diffreg_result);
4911 if (err == NULL)
4912 err = free_err;
4913 if (f1 && fclose(f1) == EOF && err == NULL)
4914 err = got_error_from_errno2("fclose", path1);
4915 if (f2 && fclose(f2) == EOF && err == NULL)
4916 err = got_error_from_errno2("fclose", path2);
4917 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4918 err = got_error_from_errno2("close", path2);
4919 if (outfile && fclose(outfile) == EOF && err == NULL)
4920 err = got_error_from_errno2("fclose", *path_outfile);
4921 if (path1 && unlink(path1) == -1 && err == NULL)
4922 err = got_error_from_errno2("unlink", path1);
4923 if (err || !have_content) {
4924 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4925 err = got_error_from_errno2("unlink", *path_outfile);
4926 free(*path_outfile);
4927 *path_outfile = NULL;
4929 free(path1);
4930 return err;
4933 static const struct got_error *
4934 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4935 const char *relpath, struct got_object_id *blob_id,
4936 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4937 int dirfd, const char *de_name)
4939 struct revert_file_args *a = arg;
4940 const struct got_error *err = NULL;
4941 char *parent_path = NULL;
4942 struct got_fileindex_entry *ie;
4943 struct got_commit_object *base_commit = NULL;
4944 struct got_tree_object *tree = NULL;
4945 struct got_object_id *tree_id = NULL;
4946 const struct got_tree_entry *te = NULL;
4947 char *tree_path = NULL, *te_name;
4948 char *ondisk_path = NULL, *path_content = NULL;
4949 struct got_blob_object *blob = NULL;
4950 int fd = -1;
4952 /* Reverting a staged deletion is a no-op. */
4953 if (status == GOT_STATUS_DELETE &&
4954 staged_status != GOT_STATUS_NO_CHANGE)
4955 return NULL;
4957 if (status == GOT_STATUS_UNVERSIONED)
4958 return (*a->progress_cb)(a->progress_arg,
4959 GOT_STATUS_UNVERSIONED, relpath);
4961 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4962 if (ie == NULL)
4963 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4965 /* Construct in-repository path of tree which contains this blob. */
4966 err = got_path_dirname(&parent_path, ie->path);
4967 if (err) {
4968 if (err->code != GOT_ERR_BAD_PATH)
4969 goto done;
4970 parent_path = strdup("/");
4971 if (parent_path == NULL) {
4972 err = got_error_from_errno("strdup");
4973 goto done;
4976 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4977 tree_path = strdup(parent_path);
4978 if (tree_path == NULL) {
4979 err = got_error_from_errno("strdup");
4980 goto done;
4982 } else {
4983 if (got_path_is_root_dir(parent_path)) {
4984 tree_path = strdup(a->worktree->path_prefix);
4985 if (tree_path == NULL) {
4986 err = got_error_from_errno("strdup");
4987 goto done;
4989 } else {
4990 if (asprintf(&tree_path, "%s/%s",
4991 a->worktree->path_prefix, parent_path) == -1) {
4992 err = got_error_from_errno("asprintf");
4993 goto done;
4998 err = got_object_open_as_commit(&base_commit, a->repo,
4999 a->worktree->base_commit_id);
5000 if (err)
5001 goto done;
5003 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5004 if (err) {
5005 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5006 (status == GOT_STATUS_ADD ||
5007 staged_status == GOT_STATUS_ADD)))
5008 goto done;
5009 } else {
5010 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5011 if (err)
5012 goto done;
5014 err = got_path_basename(&te_name, ie->path);
5015 if (err)
5016 goto done;
5018 te = got_object_tree_find_entry(tree, te_name);
5019 free(te_name);
5020 if (te == NULL && status != GOT_STATUS_ADD &&
5021 staged_status != GOT_STATUS_ADD) {
5022 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5023 goto done;
5027 switch (status) {
5028 case GOT_STATUS_ADD:
5029 if (a->patch_cb) {
5030 int choice = GOT_PATCH_CHOICE_NONE;
5031 err = (*a->patch_cb)(&choice, a->patch_arg,
5032 status, ie->path, NULL, 1, 1);
5033 if (err)
5034 goto done;
5035 if (choice != GOT_PATCH_CHOICE_YES)
5036 break;
5038 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5039 ie->path);
5040 if (err)
5041 goto done;
5042 got_fileindex_entry_remove(a->fileindex, ie);
5043 if (a->unlink_added_files) {
5044 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5046 if (a->added_files_to_unlink) {
5047 struct got_pathlist_entry *pe;
5049 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5050 entry) {
5051 if (got_path_cmp(pe->path, relpath,
5052 pe->path_len, strlen(relpath)))
5053 continue;
5054 do_unlink = 1;
5055 break;
5059 if (do_unlink) {
5060 if (asprintf(&ondisk_path, "%s/%s",
5061 got_worktree_get_root_path(a->worktree),
5062 relpath) == -1) {
5063 err = got_error_from_errno("asprintf");
5064 goto done;
5066 if (unlink(ondisk_path) == -1) {
5067 err = got_error_from_errno2("unlink",
5068 ondisk_path);
5069 break;
5073 break;
5074 case GOT_STATUS_DELETE:
5075 if (a->patch_cb) {
5076 int choice = GOT_PATCH_CHOICE_NONE;
5077 err = (*a->patch_cb)(&choice, a->patch_arg,
5078 status, ie->path, NULL, 1, 1);
5079 if (err)
5080 goto done;
5081 if (choice != GOT_PATCH_CHOICE_YES)
5082 break;
5084 /* fall through */
5085 case GOT_STATUS_MODIFY:
5086 case GOT_STATUS_MODE_CHANGE:
5087 case GOT_STATUS_CONFLICT:
5088 case GOT_STATUS_MISSING: {
5089 struct got_object_id id;
5090 if (staged_status == GOT_STATUS_ADD ||
5091 staged_status == GOT_STATUS_MODIFY)
5092 got_fileindex_entry_get_staged_blob_id(&id, ie);
5093 else
5094 got_fileindex_entry_get_blob_id(&id, ie);
5095 fd = got_opentempfd();
5096 if (fd == -1) {
5097 err = got_error_from_errno("got_opentempfd");
5098 goto done;
5101 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5102 if (err)
5103 goto done;
5105 if (asprintf(&ondisk_path, "%s/%s",
5106 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5107 err = got_error_from_errno("asprintf");
5108 goto done;
5111 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5112 status == GOT_STATUS_CONFLICT)) {
5113 int is_bad_symlink = 0;
5114 err = create_patched_content(&path_content, 1, &id,
5115 ondisk_path, dirfd, de_name, ie->path, a->repo,
5116 a->patch_cb, a->patch_arg);
5117 if (err || path_content == NULL)
5118 break;
5119 if (te && S_ISLNK(te->mode)) {
5120 if (unlink(path_content) == -1) {
5121 err = got_error_from_errno2("unlink",
5122 path_content);
5123 break;
5125 err = install_symlink(&is_bad_symlink,
5126 a->worktree, ondisk_path, ie->path,
5127 blob, 0, 1, 0, 0, a->repo,
5128 a->progress_cb, a->progress_arg);
5129 } else {
5130 if (rename(path_content, ondisk_path) == -1) {
5131 err = got_error_from_errno3("rename",
5132 path_content, ondisk_path);
5133 goto done;
5136 } else {
5137 int is_bad_symlink = 0;
5138 if (te && S_ISLNK(te->mode)) {
5139 err = install_symlink(&is_bad_symlink,
5140 a->worktree, ondisk_path, ie->path,
5141 blob, 0, 1, 0, 0, a->repo,
5142 a->progress_cb, a->progress_arg);
5143 } else {
5144 err = install_blob(a->worktree, ondisk_path,
5145 ie->path,
5146 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5147 got_fileindex_perms_to_st(ie), blob,
5148 0, 1, 0, 0, a->repo,
5149 a->progress_cb, a->progress_arg);
5151 if (err)
5152 goto done;
5153 if (status == GOT_STATUS_DELETE ||
5154 status == GOT_STATUS_MODE_CHANGE) {
5155 err = got_fileindex_entry_update(ie,
5156 a->worktree->root_fd, relpath,
5157 blob->id.sha1,
5158 a->worktree->base_commit_id->sha1, 1);
5159 if (err)
5160 goto done;
5162 if (is_bad_symlink) {
5163 got_fileindex_entry_filetype_set(ie,
5164 GOT_FILEIDX_MODE_BAD_SYMLINK);
5167 break;
5169 default:
5170 break;
5172 done:
5173 free(ondisk_path);
5174 free(path_content);
5175 free(parent_path);
5176 free(tree_path);
5177 if (fd != -1 && close(fd) == -1 && err == NULL)
5178 err = got_error_from_errno("close");
5179 if (blob)
5180 got_object_blob_close(blob);
5181 if (tree)
5182 got_object_tree_close(tree);
5183 free(tree_id);
5184 if (base_commit)
5185 got_object_commit_close(base_commit);
5186 return err;
5189 const struct got_error *
5190 got_worktree_revert(struct got_worktree *worktree,
5191 struct got_pathlist_head *paths,
5192 got_worktree_checkout_cb progress_cb, void *progress_arg,
5193 got_worktree_patch_cb patch_cb, void *patch_arg,
5194 struct got_repository *repo)
5196 struct got_fileindex *fileindex = NULL;
5197 char *fileindex_path = NULL;
5198 const struct got_error *err = NULL, *unlockerr = NULL;
5199 const struct got_error *sync_err = NULL;
5200 struct got_pathlist_entry *pe;
5201 struct revert_file_args rfa;
5203 err = lock_worktree(worktree, LOCK_EX);
5204 if (err)
5205 return err;
5207 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5208 if (err)
5209 goto done;
5211 rfa.worktree = worktree;
5212 rfa.fileindex = fileindex;
5213 rfa.progress_cb = progress_cb;
5214 rfa.progress_arg = progress_arg;
5215 rfa.patch_cb = patch_cb;
5216 rfa.patch_arg = patch_arg;
5217 rfa.repo = repo;
5218 rfa.unlink_added_files = 0;
5219 TAILQ_FOREACH(pe, paths, entry) {
5220 err = worktree_status(worktree, pe->path, fileindex, repo,
5221 revert_file, &rfa, NULL, NULL, 1, 0);
5222 if (err)
5223 break;
5225 sync_err = sync_fileindex(fileindex, fileindex_path);
5226 if (sync_err && err == NULL)
5227 err = sync_err;
5228 done:
5229 free(fileindex_path);
5230 if (fileindex)
5231 got_fileindex_free(fileindex);
5232 unlockerr = lock_worktree(worktree, LOCK_SH);
5233 if (unlockerr && err == NULL)
5234 err = unlockerr;
5235 return err;
5238 static void
5239 free_commitable(struct got_commitable *ct)
5241 free(ct->path);
5242 free(ct->in_repo_path);
5243 free(ct->ondisk_path);
5244 free(ct->blob_id);
5245 free(ct->base_blob_id);
5246 free(ct->staged_blob_id);
5247 free(ct->base_commit_id);
5248 free(ct);
5251 struct collect_commitables_arg {
5252 struct got_pathlist_head *commitable_paths;
5253 struct got_repository *repo;
5254 struct got_worktree *worktree;
5255 struct got_fileindex *fileindex;
5256 int have_staged_files;
5257 int allow_bad_symlinks;
5258 int diff_header_shown;
5259 int commit_conflicts;
5260 FILE *diff_outfile;
5261 FILE *f1;
5262 FILE *f2;
5266 * Create a file which contains the target path of a symlink so we can feed
5267 * it as content to the diff engine.
5269 static const struct got_error *
5270 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5271 const char *abspath)
5273 const struct got_error *err = NULL;
5274 char target_path[PATH_MAX];
5275 ssize_t target_len, outlen;
5277 *fd = -1;
5279 if (dirfd != -1) {
5280 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5281 if (target_len == -1)
5282 return got_error_from_errno2("readlinkat", abspath);
5283 } else {
5284 target_len = readlink(abspath, target_path, PATH_MAX);
5285 if (target_len == -1)
5286 return got_error_from_errno2("readlink", abspath);
5289 *fd = got_opentempfd();
5290 if (*fd == -1)
5291 return got_error_from_errno("got_opentempfd");
5293 outlen = write(*fd, target_path, target_len);
5294 if (outlen == -1) {
5295 err = got_error_from_errno("got_opentempfd");
5296 goto done;
5299 if (lseek(*fd, 0, SEEK_SET) == -1) {
5300 err = got_error_from_errno2("lseek", abspath);
5301 goto done;
5303 done:
5304 if (err) {
5305 close(*fd);
5306 *fd = -1;
5308 return err;
5311 static const struct got_error *
5312 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5313 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5314 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5316 const struct got_error *err = NULL;
5317 struct got_blob_object *blob1 = NULL;
5318 int fd = -1, fd1 = -1, fd2 = -1;
5319 FILE *ondisk_file = NULL;
5320 char *label1 = NULL;
5321 struct stat sb;
5322 off_t size1 = 0;
5323 int f2_exists = 0;
5324 char *id_str = NULL;
5326 memset(&sb, 0, sizeof(sb));
5328 if (diff_staged) {
5329 if (ct->staged_status != GOT_STATUS_MODIFY &&
5330 ct->staged_status != GOT_STATUS_ADD &&
5331 ct->staged_status != GOT_STATUS_DELETE)
5332 return NULL;
5333 } else {
5334 if (ct->status != GOT_STATUS_MODIFY &&
5335 ct->status != GOT_STATUS_ADD &&
5336 ct->status != GOT_STATUS_DELETE &&
5337 ct->status != GOT_STATUS_CONFLICT)
5338 return NULL;
5341 err = got_opentemp_truncate(f1);
5342 if (err)
5343 return got_error_from_errno("got_opentemp_truncate");
5344 err = got_opentemp_truncate(f2);
5345 if (err)
5346 return got_error_from_errno("got_opentemp_truncate");
5348 if (!*diff_header_shown) {
5349 err = got_object_id_str(&id_str, worktree->base_commit_id);
5350 if (err)
5351 return err;
5352 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5353 got_worktree_get_root_path(worktree));
5354 fprintf(diff_outfile, "commit - %s\n", id_str);
5355 fprintf(diff_outfile, "path + %s%s\n",
5356 got_worktree_get_root_path(worktree),
5357 diff_staged ? " (staged changes)" : "");
5358 *diff_header_shown = 1;
5361 if (diff_staged) {
5362 const char *label1 = NULL, *label2 = NULL;
5363 switch (ct->staged_status) {
5364 case GOT_STATUS_MODIFY:
5365 label1 = ct->path;
5366 label2 = ct->path;
5367 break;
5368 case GOT_STATUS_ADD:
5369 label2 = ct->path;
5370 break;
5371 case GOT_STATUS_DELETE:
5372 label1 = ct->path;
5373 break;
5374 default:
5375 return got_error(GOT_ERR_FILE_STATUS);
5377 fd1 = got_opentempfd();
5378 if (fd1 == -1) {
5379 err = got_error_from_errno("got_opentempfd");
5380 goto done;
5382 fd2 = got_opentempfd();
5383 if (fd2 == -1) {
5384 err = got_error_from_errno("got_opentempfd");
5385 goto done;
5387 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5388 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5389 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5390 NULL, repo, diff_outfile);
5391 goto done;
5394 fd1 = got_opentempfd();
5395 if (fd1 == -1) {
5396 err = got_error_from_errno("got_opentempfd");
5397 goto done;
5400 if (ct->status != GOT_STATUS_ADD) {
5401 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5402 8192, fd1);
5403 if (err)
5404 goto done;
5407 if (ct->status != GOT_STATUS_DELETE) {
5408 if (dirfd != -1) {
5409 fd = openat(dirfd, de_name,
5410 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5411 if (fd == -1) {
5412 if (!got_err_open_nofollow_on_symlink()) {
5413 err = got_error_from_errno2("openat",
5414 ct->ondisk_path);
5415 goto done;
5417 err = get_symlink_target_file(&fd, dirfd,
5418 de_name, ct->ondisk_path);
5419 if (err)
5420 goto done;
5422 } else {
5423 fd = open(ct->ondisk_path,
5424 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5425 if (fd == -1) {
5426 if (!got_err_open_nofollow_on_symlink()) {
5427 err = got_error_from_errno2("open",
5428 ct->ondisk_path);
5429 goto done;
5431 err = get_symlink_target_file(&fd, dirfd,
5432 de_name, ct->ondisk_path);
5433 if (err)
5434 goto done;
5437 if (fstatat(fd, ct->ondisk_path, &sb,
5438 AT_SYMLINK_NOFOLLOW) == -1) {
5439 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5440 goto done;
5442 ondisk_file = fdopen(fd, "r");
5443 if (ondisk_file == NULL) {
5444 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5445 goto done;
5447 fd = -1;
5448 f2_exists = 1;
5451 if (blob1) {
5452 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5453 f1, blob1);
5454 if (err)
5455 goto done;
5458 err = got_diff_blob_file(blob1, f1, size1, label1,
5459 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5460 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5461 done:
5462 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5463 err = got_error_from_errno("close");
5464 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5465 err = got_error_from_errno("close");
5466 if (blob1)
5467 got_object_blob_close(blob1);
5468 if (fd != -1 && close(fd) == -1 && err == NULL)
5469 err = got_error_from_errno("close");
5470 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5471 err = got_error_from_errno("fclose");
5472 return err;
5475 static const struct got_error *
5476 collect_commitables(void *arg, unsigned char status,
5477 unsigned char staged_status, const char *relpath,
5478 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5479 struct got_object_id *commit_id, int dirfd, const char *de_name)
5481 struct collect_commitables_arg *a = arg;
5482 const struct got_error *err = NULL;
5483 struct got_commitable *ct = NULL;
5484 struct got_pathlist_entry *new = NULL;
5485 char *parent_path = NULL, *path = NULL;
5486 struct stat sb;
5488 if (a->have_staged_files) {
5489 if (staged_status != GOT_STATUS_MODIFY &&
5490 staged_status != GOT_STATUS_ADD &&
5491 staged_status != GOT_STATUS_DELETE)
5492 return NULL;
5493 } else {
5494 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5495 printf("C %s\n", relpath);
5496 return got_error(GOT_ERR_COMMIT_CONFLICT);
5499 if (status != GOT_STATUS_MODIFY &&
5500 status != GOT_STATUS_MODE_CHANGE &&
5501 status != GOT_STATUS_ADD &&
5502 status != GOT_STATUS_DELETE &&
5503 status != GOT_STATUS_CONFLICT)
5504 return NULL;
5507 if (asprintf(&path, "/%s", relpath) == -1) {
5508 err = got_error_from_errno("asprintf");
5509 goto done;
5511 if (strcmp(path, "/") == 0) {
5512 parent_path = strdup("");
5513 if (parent_path == NULL)
5514 return got_error_from_errno("strdup");
5515 } else {
5516 err = got_path_dirname(&parent_path, path);
5517 if (err)
5518 return err;
5521 ct = calloc(1, sizeof(*ct));
5522 if (ct == NULL) {
5523 err = got_error_from_errno("calloc");
5524 goto done;
5527 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5528 relpath) == -1) {
5529 err = got_error_from_errno("asprintf");
5530 goto done;
5533 if (staged_status == GOT_STATUS_ADD ||
5534 staged_status == GOT_STATUS_MODIFY) {
5535 struct got_fileindex_entry *ie;
5536 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5537 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5538 case GOT_FILEIDX_MODE_REGULAR_FILE:
5539 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5540 ct->mode = S_IFREG;
5541 break;
5542 case GOT_FILEIDX_MODE_SYMLINK:
5543 ct->mode = S_IFLNK;
5544 break;
5545 default:
5546 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5547 goto done;
5549 ct->mode |= got_fileindex_entry_perms_get(ie);
5550 } else if (status != GOT_STATUS_DELETE &&
5551 staged_status != GOT_STATUS_DELETE) {
5552 if (dirfd != -1) {
5553 if (fstatat(dirfd, de_name, &sb,
5554 AT_SYMLINK_NOFOLLOW) == -1) {
5555 err = got_error_from_errno2("fstatat",
5556 ct->ondisk_path);
5557 goto done;
5559 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5560 err = got_error_from_errno2("lstat", ct->ondisk_path);
5561 goto done;
5563 ct->mode = sb.st_mode;
5566 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5567 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5568 relpath) == -1) {
5569 err = got_error_from_errno("asprintf");
5570 goto done;
5573 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5574 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5575 int is_bad_symlink;
5576 char target_path[PATH_MAX];
5577 ssize_t target_len;
5578 target_len = readlink(ct->ondisk_path, target_path,
5579 sizeof(target_path));
5580 if (target_len == -1) {
5581 err = got_error_from_errno2("readlink",
5582 ct->ondisk_path);
5583 goto done;
5585 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5586 target_len, ct->ondisk_path, a->worktree->root_path,
5587 a->worktree->meta_dir);
5588 if (err)
5589 goto done;
5590 if (is_bad_symlink) {
5591 err = got_error_path(ct->ondisk_path,
5592 GOT_ERR_BAD_SYMLINK);
5593 goto done;
5598 ct->status = status;
5599 ct->staged_status = staged_status;
5600 ct->blob_id = NULL; /* will be filled in when blob gets created */
5601 if (ct->status != GOT_STATUS_ADD &&
5602 ct->staged_status != GOT_STATUS_ADD) {
5603 ct->base_blob_id = got_object_id_dup(blob_id);
5604 if (ct->base_blob_id == NULL) {
5605 err = got_error_from_errno("got_object_id_dup");
5606 goto done;
5608 ct->base_commit_id = got_object_id_dup(commit_id);
5609 if (ct->base_commit_id == NULL) {
5610 err = got_error_from_errno("got_object_id_dup");
5611 goto done;
5614 if (ct->staged_status == GOT_STATUS_ADD ||
5615 ct->staged_status == GOT_STATUS_MODIFY) {
5616 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5617 if (ct->staged_blob_id == NULL) {
5618 err = got_error_from_errno("got_object_id_dup");
5619 goto done;
5622 ct->path = strdup(path);
5623 if (ct->path == NULL) {
5624 err = got_error_from_errno("strdup");
5625 goto done;
5627 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5628 if (err)
5629 goto done;
5631 if (a->diff_outfile && ct && new != NULL) {
5632 err = append_ct_diff(ct, &a->diff_header_shown,
5633 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5634 a->have_staged_files, a->repo, a->worktree);
5635 if (err)
5636 goto done;
5638 done:
5639 if (ct && (err || new == NULL))
5640 free_commitable(ct);
5641 free(parent_path);
5642 free(path);
5643 return err;
5646 static const struct got_error *write_tree(struct got_object_id **, int *,
5647 struct got_tree_object *, const char *, struct got_pathlist_head *,
5648 got_worktree_status_cb status_cb, void *status_arg,
5649 struct got_repository *);
5651 static const struct got_error *
5652 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5653 struct got_tree_entry *te, const char *parent_path,
5654 struct got_pathlist_head *commitable_paths,
5655 got_worktree_status_cb status_cb, void *status_arg,
5656 struct got_repository *repo)
5658 const struct got_error *err = NULL;
5659 struct got_tree_object *subtree;
5660 char *subpath;
5662 if (asprintf(&subpath, "%s%s%s", parent_path,
5663 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5664 return got_error_from_errno("asprintf");
5666 err = got_object_open_as_tree(&subtree, repo, &te->id);
5667 if (err)
5668 return err;
5670 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5671 commitable_paths, status_cb, status_arg, repo);
5672 got_object_tree_close(subtree);
5673 free(subpath);
5674 return err;
5677 static const struct got_error *
5678 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5680 const struct got_error *err = NULL;
5681 char *ct_parent_path = NULL;
5683 *match = 0;
5685 if (strchr(ct->in_repo_path, '/') == NULL) {
5686 *match = got_path_is_root_dir(path);
5687 return NULL;
5690 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5691 if (err)
5692 return err;
5693 *match = (strcmp(path, ct_parent_path) == 0);
5694 free(ct_parent_path);
5695 return err;
5698 static mode_t
5699 get_ct_file_mode(struct got_commitable *ct)
5701 if (S_ISLNK(ct->mode))
5702 return S_IFLNK;
5704 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5707 static const struct got_error *
5708 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5709 struct got_tree_entry *te, struct got_commitable *ct)
5711 const struct got_error *err = NULL;
5713 *new_te = NULL;
5715 err = got_object_tree_entry_dup(new_te, te);
5716 if (err)
5717 goto done;
5719 (*new_te)->mode = get_ct_file_mode(ct);
5721 if (ct->staged_status == GOT_STATUS_MODIFY)
5722 memcpy(&(*new_te)->id, ct->staged_blob_id,
5723 sizeof((*new_te)->id));
5724 else
5725 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5726 done:
5727 if (err && *new_te) {
5728 free(*new_te);
5729 *new_te = NULL;
5731 return err;
5734 static const struct got_error *
5735 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5736 struct got_commitable *ct)
5738 const struct got_error *err = NULL;
5739 char *ct_name = NULL;
5741 *new_te = NULL;
5743 *new_te = calloc(1, sizeof(**new_te));
5744 if (*new_te == NULL)
5745 return got_error_from_errno("calloc");
5747 err = got_path_basename(&ct_name, ct->path);
5748 if (err)
5749 goto done;
5750 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5751 sizeof((*new_te)->name)) {
5752 err = got_error(GOT_ERR_NO_SPACE);
5753 goto done;
5756 (*new_te)->mode = get_ct_file_mode(ct);
5758 if (ct->staged_status == GOT_STATUS_ADD)
5759 memcpy(&(*new_te)->id, ct->staged_blob_id,
5760 sizeof((*new_te)->id));
5761 else
5762 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5763 done:
5764 free(ct_name);
5765 if (err && *new_te) {
5766 free(*new_te);
5767 *new_te = NULL;
5769 return err;
5772 static const struct got_error *
5773 insert_tree_entry(struct got_tree_entry *new_te,
5774 struct got_pathlist_head *paths)
5776 const struct got_error *err = NULL;
5777 struct got_pathlist_entry *new_pe;
5779 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5780 if (err)
5781 return err;
5782 if (new_pe == NULL)
5783 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5784 return NULL;
5787 static const struct got_error *
5788 report_ct_status(struct got_commitable *ct,
5789 got_worktree_status_cb status_cb, void *status_arg)
5791 const char *ct_path = ct->path;
5792 unsigned char status;
5794 if (status_cb == NULL) /* no commit progress output desired */
5795 return NULL;
5797 while (ct_path[0] == '/')
5798 ct_path++;
5800 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5801 status = ct->staged_status;
5802 else
5803 status = ct->status;
5805 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5806 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5809 static const struct got_error *
5810 match_modified_subtree(int *modified, struct got_tree_entry *te,
5811 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5813 const struct got_error *err = NULL;
5814 struct got_pathlist_entry *pe;
5815 char *te_path;
5817 *modified = 0;
5819 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5820 got_path_is_root_dir(base_tree_path) ? "" : "/",
5821 te->name) == -1)
5822 return got_error_from_errno("asprintf");
5824 TAILQ_FOREACH(pe, commitable_paths, entry) {
5825 struct got_commitable *ct = pe->data;
5826 *modified = got_path_is_child(ct->in_repo_path, te_path,
5827 strlen(te_path));
5828 if (*modified)
5829 break;
5832 free(te_path);
5833 return err;
5836 static const struct got_error *
5837 match_deleted_or_modified_ct(struct got_commitable **ctp,
5838 struct got_tree_entry *te, const char *base_tree_path,
5839 struct got_pathlist_head *commitable_paths)
5841 const struct got_error *err = NULL;
5842 struct got_pathlist_entry *pe;
5844 *ctp = NULL;
5846 TAILQ_FOREACH(pe, commitable_paths, entry) {
5847 struct got_commitable *ct = pe->data;
5848 char *ct_name = NULL;
5849 int path_matches;
5851 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5852 if (ct->status != GOT_STATUS_MODIFY &&
5853 ct->status != GOT_STATUS_MODE_CHANGE &&
5854 ct->status != GOT_STATUS_DELETE &&
5855 ct->status != GOT_STATUS_CONFLICT)
5856 continue;
5857 } else {
5858 if (ct->staged_status != GOT_STATUS_MODIFY &&
5859 ct->staged_status != GOT_STATUS_DELETE)
5860 continue;
5863 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5864 continue;
5866 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5867 if (err)
5868 return err;
5869 if (!path_matches)
5870 continue;
5872 err = got_path_basename(&ct_name, pe->path);
5873 if (err)
5874 return err;
5876 if (strcmp(te->name, ct_name) != 0) {
5877 free(ct_name);
5878 continue;
5880 free(ct_name);
5882 *ctp = ct;
5883 break;
5886 return err;
5889 static const struct got_error *
5890 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5891 const char *child_path, const char *path_base_tree,
5892 struct got_pathlist_head *commitable_paths,
5893 got_worktree_status_cb status_cb, void *status_arg,
5894 struct got_repository *repo)
5896 const struct got_error *err = NULL;
5897 struct got_tree_entry *new_te;
5898 char *subtree_path;
5899 struct got_object_id *id = NULL;
5900 int nentries;
5902 *new_tep = NULL;
5904 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5905 got_path_is_root_dir(path_base_tree) ? "" : "/",
5906 child_path) == -1)
5907 return got_error_from_errno("asprintf");
5909 new_te = calloc(1, sizeof(*new_te));
5910 if (new_te == NULL)
5911 return got_error_from_errno("calloc");
5912 new_te->mode = S_IFDIR;
5914 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5915 sizeof(new_te->name)) {
5916 err = got_error(GOT_ERR_NO_SPACE);
5917 goto done;
5919 err = write_tree(&id, &nentries, NULL, subtree_path,
5920 commitable_paths, status_cb, status_arg, repo);
5921 if (err) {
5922 free(new_te);
5923 goto done;
5925 memcpy(&new_te->id, id, sizeof(new_te->id));
5926 done:
5927 free(id);
5928 free(subtree_path);
5929 if (err == NULL)
5930 *new_tep = new_te;
5931 return err;
5934 static const struct got_error *
5935 write_tree(struct got_object_id **new_tree_id, int *nentries,
5936 struct got_tree_object *base_tree, const char *path_base_tree,
5937 struct got_pathlist_head *commitable_paths,
5938 got_worktree_status_cb status_cb, void *status_arg,
5939 struct got_repository *repo)
5941 const struct got_error *err = NULL;
5942 struct got_pathlist_head paths;
5943 struct got_tree_entry *te, *new_te = NULL;
5944 struct got_pathlist_entry *pe;
5946 TAILQ_INIT(&paths);
5947 *nentries = 0;
5949 /* Insert, and recurse into, newly added entries first. */
5950 TAILQ_FOREACH(pe, commitable_paths, entry) {
5951 struct got_commitable *ct = pe->data;
5952 char *child_path = NULL, *slash;
5954 if ((ct->status != GOT_STATUS_ADD &&
5955 ct->staged_status != GOT_STATUS_ADD) ||
5956 (ct->flags & GOT_COMMITABLE_ADDED))
5957 continue;
5959 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5960 strlen(path_base_tree)))
5961 continue;
5963 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5964 ct->in_repo_path);
5965 if (err)
5966 goto done;
5968 slash = strchr(child_path, '/');
5969 if (slash == NULL) {
5970 err = alloc_added_blob_tree_entry(&new_te, ct);
5971 if (err)
5972 goto done;
5973 err = report_ct_status(ct, status_cb, status_arg);
5974 if (err)
5975 goto done;
5976 ct->flags |= GOT_COMMITABLE_ADDED;
5977 err = insert_tree_entry(new_te, &paths);
5978 if (err)
5979 goto done;
5980 (*nentries)++;
5981 } else {
5982 *slash = '\0'; /* trim trailing path components */
5983 if (base_tree == NULL ||
5984 got_object_tree_find_entry(base_tree, child_path)
5985 == NULL) {
5986 err = make_subtree_for_added_blob(&new_te,
5987 child_path, path_base_tree,
5988 commitable_paths, status_cb, status_arg,
5989 repo);
5990 if (err)
5991 goto done;
5992 err = insert_tree_entry(new_te, &paths);
5993 if (err)
5994 goto done;
5995 (*nentries)++;
6000 if (base_tree) {
6001 int i, nbase_entries;
6002 /* Handle modified and deleted entries. */
6003 nbase_entries = got_object_tree_get_nentries(base_tree);
6004 for (i = 0; i < nbase_entries; i++) {
6005 struct got_commitable *ct = NULL;
6007 te = got_object_tree_get_entry(base_tree, i);
6008 if (got_object_tree_entry_is_submodule(te)) {
6009 /* Entry is a submodule; just copy it. */
6010 err = got_object_tree_entry_dup(&new_te, te);
6011 if (err)
6012 goto done;
6013 err = insert_tree_entry(new_te, &paths);
6014 if (err)
6015 goto done;
6016 (*nentries)++;
6017 continue;
6020 if (S_ISDIR(te->mode)) {
6021 int modified;
6022 err = got_object_tree_entry_dup(&new_te, te);
6023 if (err)
6024 goto done;
6025 err = match_modified_subtree(&modified, te,
6026 path_base_tree, commitable_paths);
6027 if (err)
6028 goto done;
6029 /* Avoid recursion into unmodified subtrees. */
6030 if (modified) {
6031 struct got_object_id *new_id;
6032 int nsubentries;
6033 err = write_subtree(&new_id,
6034 &nsubentries, te,
6035 path_base_tree, commitable_paths,
6036 status_cb, status_arg, repo);
6037 if (err)
6038 goto done;
6039 if (nsubentries == 0) {
6040 /* All entries were deleted. */
6041 free(new_id);
6042 continue;
6044 memcpy(&new_te->id, new_id,
6045 sizeof(new_te->id));
6046 free(new_id);
6048 err = insert_tree_entry(new_te, &paths);
6049 if (err)
6050 goto done;
6051 (*nentries)++;
6052 continue;
6055 err = match_deleted_or_modified_ct(&ct, te,
6056 path_base_tree, commitable_paths);
6057 if (err)
6058 goto done;
6059 if (ct) {
6060 /* NB: Deleted entries get dropped here. */
6061 if (ct->status == GOT_STATUS_MODIFY ||
6062 ct->status == GOT_STATUS_MODE_CHANGE ||
6063 ct->status == GOT_STATUS_CONFLICT ||
6064 ct->staged_status == GOT_STATUS_MODIFY) {
6065 err = alloc_modified_blob_tree_entry(
6066 &new_te, te, ct);
6067 if (err)
6068 goto done;
6069 err = insert_tree_entry(new_te, &paths);
6070 if (err)
6071 goto done;
6072 (*nentries)++;
6074 err = report_ct_status(ct, status_cb,
6075 status_arg);
6076 if (err)
6077 goto done;
6078 } else {
6079 /* Entry is unchanged; just copy it. */
6080 err = got_object_tree_entry_dup(&new_te, te);
6081 if (err)
6082 goto done;
6083 err = insert_tree_entry(new_te, &paths);
6084 if (err)
6085 goto done;
6086 (*nentries)++;
6091 /* Write new list of entries; deleted entries have been dropped. */
6092 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6093 done:
6094 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6095 return err;
6098 static const struct got_error *
6099 update_fileindex_after_commit(struct got_worktree *worktree,
6100 struct got_pathlist_head *commitable_paths,
6101 struct got_object_id *new_base_commit_id,
6102 struct got_fileindex *fileindex, int have_staged_files)
6104 const struct got_error *err = NULL;
6105 struct got_pathlist_entry *pe;
6106 char *relpath = NULL;
6108 TAILQ_FOREACH(pe, commitable_paths, entry) {
6109 struct got_fileindex_entry *ie;
6110 struct got_commitable *ct = pe->data;
6112 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6114 err = got_path_skip_common_ancestor(&relpath,
6115 worktree->root_path, ct->ondisk_path);
6116 if (err)
6117 goto done;
6119 if (ie) {
6120 if (ct->status == GOT_STATUS_DELETE ||
6121 ct->staged_status == GOT_STATUS_DELETE) {
6122 got_fileindex_entry_remove(fileindex, ie);
6123 } else if (ct->staged_status == GOT_STATUS_ADD ||
6124 ct->staged_status == GOT_STATUS_MODIFY) {
6125 got_fileindex_entry_stage_set(ie,
6126 GOT_FILEIDX_STAGE_NONE);
6127 got_fileindex_entry_staged_filetype_set(ie, 0);
6129 err = got_fileindex_entry_update(ie,
6130 worktree->root_fd, relpath,
6131 ct->staged_blob_id->sha1,
6132 new_base_commit_id->sha1,
6133 !have_staged_files);
6134 } else
6135 err = got_fileindex_entry_update(ie,
6136 worktree->root_fd, relpath,
6137 ct->blob_id->sha1,
6138 new_base_commit_id->sha1,
6139 !have_staged_files);
6140 } else {
6141 err = got_fileindex_entry_alloc(&ie, pe->path);
6142 if (err)
6143 goto done;
6144 err = got_fileindex_entry_update(ie,
6145 worktree->root_fd, relpath, ct->blob_id->sha1,
6146 new_base_commit_id->sha1, 1);
6147 if (err) {
6148 got_fileindex_entry_free(ie);
6149 goto done;
6151 err = got_fileindex_entry_add(fileindex, ie);
6152 if (err) {
6153 got_fileindex_entry_free(ie);
6154 goto done;
6157 free(relpath);
6158 relpath = NULL;
6160 done:
6161 free(relpath);
6162 return err;
6166 static const struct got_error *
6167 check_out_of_date(const char *in_repo_path, unsigned char status,
6168 unsigned char staged_status, struct got_object_id *base_blob_id,
6169 struct got_object_id *base_commit_id,
6170 struct got_object_id *head_commit_id, struct got_repository *repo,
6171 int ood_errcode)
6173 const struct got_error *err = NULL;
6174 struct got_commit_object *commit = NULL;
6175 struct got_object_id *id = NULL;
6177 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6178 /* Trivial case: base commit == head commit */
6179 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6180 return NULL;
6182 * Ensure file content which local changes were based
6183 * on matches file content in the branch head.
6185 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6186 if (err)
6187 goto done;
6188 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6189 if (err) {
6190 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6191 err = got_error(ood_errcode);
6192 goto done;
6193 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6194 err = got_error(ood_errcode);
6195 } else {
6196 /* Require that added files don't exist in the branch head. */
6197 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6198 if (err)
6199 goto done;
6200 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6201 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6202 goto done;
6203 err = id ? got_error(ood_errcode) : NULL;
6205 done:
6206 free(id);
6207 if (commit)
6208 got_object_commit_close(commit);
6209 return err;
6212 static const struct got_error *
6213 commit_worktree(struct got_object_id **new_commit_id,
6214 struct got_pathlist_head *commitable_paths,
6215 struct got_object_id *head_commit_id,
6216 struct got_object_id *parent_id2,
6217 struct got_worktree *worktree,
6218 const char *author, const char *committer, char *diff_path,
6219 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6220 got_worktree_status_cb status_cb, void *status_arg,
6221 struct got_repository *repo)
6223 const struct got_error *err = NULL, *unlockerr = NULL;
6224 struct got_pathlist_entry *pe;
6225 const char *head_ref_name = NULL;
6226 struct got_commit_object *head_commit = NULL;
6227 struct got_reference *head_ref2 = NULL;
6228 struct got_object_id *head_commit_id2 = NULL;
6229 struct got_tree_object *head_tree = NULL;
6230 struct got_object_id *new_tree_id = NULL;
6231 int nentries, nparents = 0;
6232 struct got_object_id_queue parent_ids;
6233 struct got_object_qid *pid = NULL;
6234 char *logmsg = NULL;
6235 time_t timestamp;
6237 *new_commit_id = NULL;
6239 STAILQ_INIT(&parent_ids);
6241 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6242 if (err)
6243 goto done;
6245 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6246 if (err)
6247 goto done;
6249 if (commit_msg_cb != NULL) {
6250 err = commit_msg_cb(commitable_paths, diff_path,
6251 &logmsg, commit_arg);
6252 if (err)
6253 goto done;
6256 if (logmsg == NULL || strlen(logmsg) == 0) {
6257 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6258 goto done;
6261 /* Create blobs from added and modified files and record their IDs. */
6262 TAILQ_FOREACH(pe, commitable_paths, entry) {
6263 struct got_commitable *ct = pe->data;
6264 char *ondisk_path;
6266 /* Blobs for staged files already exist. */
6267 if (ct->staged_status == GOT_STATUS_ADD ||
6268 ct->staged_status == GOT_STATUS_MODIFY)
6269 continue;
6271 if (ct->status != GOT_STATUS_ADD &&
6272 ct->status != GOT_STATUS_MODIFY &&
6273 ct->status != GOT_STATUS_MODE_CHANGE &&
6274 ct->status != GOT_STATUS_CONFLICT)
6275 continue;
6277 if (asprintf(&ondisk_path, "%s/%s",
6278 worktree->root_path, pe->path) == -1) {
6279 err = got_error_from_errno("asprintf");
6280 goto done;
6282 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6283 free(ondisk_path);
6284 if (err)
6285 goto done;
6288 /* Recursively write new tree objects. */
6289 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6290 commitable_paths, status_cb, status_arg, repo);
6291 if (err)
6292 goto done;
6294 err = got_object_qid_alloc(&pid, head_commit_id);
6295 if (err)
6296 goto done;
6297 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6298 nparents++;
6299 if (parent_id2) {
6300 err = got_object_qid_alloc(&pid, parent_id2);
6301 if (err)
6302 goto done;
6303 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6304 nparents++;
6306 timestamp = time(NULL);
6307 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6308 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6309 if (logmsg != NULL)
6310 free(logmsg);
6311 if (err)
6312 goto done;
6314 /* Check if a concurrent commit to our branch has occurred. */
6315 head_ref_name = got_worktree_get_head_ref_name(worktree);
6316 if (head_ref_name == NULL) {
6317 err = got_error_from_errno("got_worktree_get_head_ref_name");
6318 goto done;
6320 /* Lock the reference here to prevent concurrent modification. */
6321 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6322 if (err)
6323 goto done;
6324 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6325 if (err)
6326 goto done;
6327 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6328 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6329 goto done;
6331 /* Update branch head in repository. */
6332 err = got_ref_change_ref(head_ref2, *new_commit_id);
6333 if (err)
6334 goto done;
6335 err = got_ref_write(head_ref2, repo);
6336 if (err)
6337 goto done;
6339 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6340 if (err)
6341 goto done;
6343 err = ref_base_commit(worktree, repo);
6344 if (err)
6345 goto done;
6346 done:
6347 got_object_id_queue_free(&parent_ids);
6348 if (head_tree)
6349 got_object_tree_close(head_tree);
6350 if (head_commit)
6351 got_object_commit_close(head_commit);
6352 free(head_commit_id2);
6353 if (head_ref2) {
6354 unlockerr = got_ref_unlock(head_ref2);
6355 if (unlockerr && err == NULL)
6356 err = unlockerr;
6357 got_ref_close(head_ref2);
6359 return err;
6362 static const struct got_error *
6363 check_path_is_commitable(const char *path,
6364 struct got_pathlist_head *commitable_paths)
6366 struct got_pathlist_entry *cpe = NULL;
6367 size_t path_len = strlen(path);
6369 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6370 struct got_commitable *ct = cpe->data;
6371 const char *ct_path = ct->path;
6373 while (ct_path[0] == '/')
6374 ct_path++;
6376 if (strcmp(path, ct_path) == 0 ||
6377 got_path_is_child(ct_path, path, path_len))
6378 break;
6381 if (cpe == NULL)
6382 return got_error_path(path, GOT_ERR_BAD_PATH);
6384 return NULL;
6387 static const struct got_error *
6388 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6390 int *have_staged_files = arg;
6392 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6393 *have_staged_files = 1;
6394 return got_error(GOT_ERR_CANCELLED);
6397 return NULL;
6400 static const struct got_error *
6401 check_non_staged_files(struct got_fileindex *fileindex,
6402 struct got_pathlist_head *paths)
6404 struct got_pathlist_entry *pe;
6405 struct got_fileindex_entry *ie;
6407 TAILQ_FOREACH(pe, paths, entry) {
6408 if (pe->path[0] == '\0')
6409 continue;
6410 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6411 if (ie == NULL)
6412 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6413 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6414 return got_error_path(pe->path,
6415 GOT_ERR_FILE_NOT_STAGED);
6418 return NULL;
6421 const struct got_error *
6422 got_worktree_commit(struct got_object_id **new_commit_id,
6423 struct got_worktree *worktree, struct got_pathlist_head *paths,
6424 const char *author, const char *committer, int allow_bad_symlinks,
6425 int show_diff, int commit_conflicts,
6426 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6427 got_worktree_status_cb status_cb, void *status_arg,
6428 struct got_repository *repo)
6430 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6431 struct got_fileindex *fileindex = NULL;
6432 char *fileindex_path = NULL;
6433 struct got_pathlist_head commitable_paths;
6434 struct collect_commitables_arg cc_arg;
6435 struct got_pathlist_entry *pe;
6436 struct got_reference *head_ref = NULL;
6437 struct got_object_id *head_commit_id = NULL;
6438 char *diff_path = NULL;
6439 int have_staged_files = 0;
6441 *new_commit_id = NULL;
6443 memset(&cc_arg, 0, sizeof(cc_arg));
6444 TAILQ_INIT(&commitable_paths);
6446 err = lock_worktree(worktree, LOCK_EX);
6447 if (err)
6448 goto done;
6450 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6451 if (err)
6452 goto done;
6454 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6455 if (err)
6456 goto done;
6458 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6459 if (err)
6460 goto done;
6462 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6463 &have_staged_files);
6464 if (err && err->code != GOT_ERR_CANCELLED)
6465 goto done;
6466 if (have_staged_files) {
6467 err = check_non_staged_files(fileindex, paths);
6468 if (err)
6469 goto done;
6472 cc_arg.commitable_paths = &commitable_paths;
6473 cc_arg.worktree = worktree;
6474 cc_arg.fileindex = fileindex;
6475 cc_arg.repo = repo;
6476 cc_arg.have_staged_files = have_staged_files;
6477 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6478 cc_arg.diff_header_shown = 0;
6479 cc_arg.commit_conflicts = commit_conflicts;
6480 if (show_diff) {
6481 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6482 GOT_TMPDIR_STR "/got", ".diff");
6483 if (err)
6484 goto done;
6485 cc_arg.f1 = got_opentemp();
6486 if (cc_arg.f1 == NULL) {
6487 err = got_error_from_errno("got_opentemp");
6488 goto done;
6490 cc_arg.f2 = got_opentemp();
6491 if (cc_arg.f2 == NULL) {
6492 err = got_error_from_errno("got_opentemp");
6493 goto done;
6497 TAILQ_FOREACH(pe, paths, entry) {
6498 err = worktree_status(worktree, pe->path, fileindex, repo,
6499 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6500 if (err)
6501 goto done;
6504 if (show_diff) {
6505 if (fflush(cc_arg.diff_outfile) == EOF) {
6506 err = got_error_from_errno("fflush");
6507 goto done;
6511 if (TAILQ_EMPTY(&commitable_paths)) {
6512 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6513 goto done;
6516 TAILQ_FOREACH(pe, paths, entry) {
6517 err = check_path_is_commitable(pe->path, &commitable_paths);
6518 if (err)
6519 goto done;
6522 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6523 struct got_commitable *ct = pe->data;
6524 const char *ct_path = ct->in_repo_path;
6526 while (ct_path[0] == '/')
6527 ct_path++;
6528 err = check_out_of_date(ct_path, ct->status,
6529 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6530 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6531 if (err)
6532 goto done;
6536 err = commit_worktree(new_commit_id, &commitable_paths,
6537 head_commit_id, NULL, worktree, author, committer,
6538 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6539 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6540 if (err)
6541 goto done;
6543 err = update_fileindex_after_commit(worktree, &commitable_paths,
6544 *new_commit_id, fileindex, have_staged_files);
6545 sync_err = sync_fileindex(fileindex, fileindex_path);
6546 if (sync_err && err == NULL)
6547 err = sync_err;
6548 done:
6549 if (fileindex)
6550 got_fileindex_free(fileindex);
6551 free(fileindex_path);
6552 unlockerr = lock_worktree(worktree, LOCK_SH);
6553 if (unlockerr && err == NULL)
6554 err = unlockerr;
6555 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6556 struct got_commitable *ct = pe->data;
6558 free_commitable(ct);
6560 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6561 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6562 err = got_error_from_errno2("unlink", diff_path);
6563 free(diff_path);
6564 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6565 err == NULL)
6566 err = got_error_from_errno("fclose");
6567 return err;
6570 const char *
6571 got_commitable_get_path(struct got_commitable *ct)
6573 return ct->path;
6576 unsigned int
6577 got_commitable_get_status(struct got_commitable *ct)
6579 return ct->status;
6582 struct check_rebase_ok_arg {
6583 struct got_worktree *worktree;
6584 struct got_repository *repo;
6587 static const struct got_error *
6588 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6590 const struct got_error *err = NULL;
6591 struct check_rebase_ok_arg *a = arg;
6592 unsigned char status;
6593 struct stat sb;
6594 char *ondisk_path;
6596 /* Reject rebase of a work tree with mixed base commits. */
6597 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6598 SHA1_DIGEST_LENGTH))
6599 return got_error(GOT_ERR_MIXED_COMMITS);
6601 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6602 == -1)
6603 return got_error_from_errno("asprintf");
6605 /* Reject rebase of a work tree with modified or staged files. */
6606 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6607 free(ondisk_path);
6608 if (err)
6609 return err;
6611 if (status != GOT_STATUS_NO_CHANGE)
6612 return got_error(GOT_ERR_MODIFIED);
6613 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6614 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6616 return NULL;
6619 const struct got_error *
6620 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6621 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6622 struct got_worktree *worktree, struct got_reference *branch,
6623 struct got_repository *repo)
6625 const struct got_error *err = NULL;
6626 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6627 char *branch_ref_name = NULL;
6628 char *fileindex_path = NULL;
6629 struct check_rebase_ok_arg ok_arg;
6630 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6631 struct got_object_id *wt_branch_tip = NULL;
6633 *new_base_branch_ref = NULL;
6634 *tmp_branch = NULL;
6635 *fileindex = NULL;
6637 err = lock_worktree(worktree, LOCK_EX);
6638 if (err)
6639 return err;
6641 err = open_fileindex(fileindex, &fileindex_path, worktree);
6642 if (err)
6643 goto done;
6645 ok_arg.worktree = worktree;
6646 ok_arg.repo = repo;
6647 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6648 &ok_arg);
6649 if (err)
6650 goto done;
6652 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6653 if (err)
6654 goto done;
6656 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6657 if (err)
6658 goto done;
6660 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6661 if (err)
6662 goto done;
6664 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6665 0);
6666 if (err)
6667 goto done;
6669 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6670 if (err)
6671 goto done;
6672 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6673 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6674 goto done;
6677 err = got_ref_alloc_symref(new_base_branch_ref,
6678 new_base_branch_ref_name, wt_branch);
6679 if (err)
6680 goto done;
6681 err = got_ref_write(*new_base_branch_ref, repo);
6682 if (err)
6683 goto done;
6685 /* TODO Lock original branch's ref while rebasing? */
6687 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6688 if (err)
6689 goto done;
6691 err = got_ref_write(branch_ref, repo);
6692 if (err)
6693 goto done;
6695 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6696 worktree->base_commit_id);
6697 if (err)
6698 goto done;
6699 err = got_ref_write(*tmp_branch, repo);
6700 if (err)
6701 goto done;
6703 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6704 if (err)
6705 goto done;
6706 done:
6707 free(fileindex_path);
6708 free(tmp_branch_name);
6709 free(new_base_branch_ref_name);
6710 free(branch_ref_name);
6711 if (branch_ref)
6712 got_ref_close(branch_ref);
6713 if (wt_branch)
6714 got_ref_close(wt_branch);
6715 free(wt_branch_tip);
6716 if (err) {
6717 if (*new_base_branch_ref) {
6718 got_ref_close(*new_base_branch_ref);
6719 *new_base_branch_ref = NULL;
6721 if (*tmp_branch) {
6722 got_ref_close(*tmp_branch);
6723 *tmp_branch = NULL;
6725 if (*fileindex) {
6726 got_fileindex_free(*fileindex);
6727 *fileindex = NULL;
6729 lock_worktree(worktree, LOCK_SH);
6731 return err;
6734 const struct got_error *
6735 got_worktree_rebase_continue(struct got_object_id **commit_id,
6736 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6737 struct got_reference **branch, struct got_fileindex **fileindex,
6738 struct got_worktree *worktree, struct got_repository *repo)
6740 const struct got_error *err;
6741 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6742 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6743 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6744 char *fileindex_path = NULL;
6745 int have_staged_files = 0;
6747 *commit_id = NULL;
6748 *new_base_branch = NULL;
6749 *tmp_branch = NULL;
6750 *branch = NULL;
6751 *fileindex = NULL;
6753 err = lock_worktree(worktree, LOCK_EX);
6754 if (err)
6755 return err;
6757 err = open_fileindex(fileindex, &fileindex_path, worktree);
6758 if (err)
6759 goto done;
6761 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6762 &have_staged_files);
6763 if (err && err->code != GOT_ERR_CANCELLED)
6764 goto done;
6765 if (have_staged_files) {
6766 err = got_error(GOT_ERR_STAGED_PATHS);
6767 goto done;
6770 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6771 if (err)
6772 goto done;
6774 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6775 if (err)
6776 goto done;
6778 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6779 if (err)
6780 goto done;
6782 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6783 if (err)
6784 goto done;
6786 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6787 if (err)
6788 goto done;
6790 err = got_ref_open(branch, repo,
6791 got_ref_get_symref_target(branch_ref), 0);
6792 if (err)
6793 goto done;
6795 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6796 if (err)
6797 goto done;
6799 err = got_ref_resolve(commit_id, repo, commit_ref);
6800 if (err)
6801 goto done;
6803 err = got_ref_open(new_base_branch, repo,
6804 new_base_branch_ref_name, 0);
6805 if (err)
6806 goto done;
6808 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6809 if (err)
6810 goto done;
6811 done:
6812 free(commit_ref_name);
6813 free(branch_ref_name);
6814 free(fileindex_path);
6815 if (commit_ref)
6816 got_ref_close(commit_ref);
6817 if (branch_ref)
6818 got_ref_close(branch_ref);
6819 if (err) {
6820 free(*commit_id);
6821 *commit_id = NULL;
6822 if (*tmp_branch) {
6823 got_ref_close(*tmp_branch);
6824 *tmp_branch = NULL;
6826 if (*new_base_branch) {
6827 got_ref_close(*new_base_branch);
6828 *new_base_branch = NULL;
6830 if (*branch) {
6831 got_ref_close(*branch);
6832 *branch = NULL;
6834 if (*fileindex) {
6835 got_fileindex_free(*fileindex);
6836 *fileindex = NULL;
6838 lock_worktree(worktree, LOCK_SH);
6840 return err;
6843 const struct got_error *
6844 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6846 const struct got_error *err;
6847 char *tmp_branch_name = NULL;
6849 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6850 if (err)
6851 return err;
6853 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6854 free(tmp_branch_name);
6855 return NULL;
6858 static const struct got_error *
6859 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6860 const char *diff_path, char **logmsg, void *arg)
6862 *logmsg = arg;
6863 return NULL;
6866 static const struct got_error *
6867 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6868 const char *path, struct got_object_id *blob_id,
6869 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6870 int dirfd, const char *de_name)
6872 return NULL;
6875 struct collect_merged_paths_arg {
6876 got_worktree_checkout_cb progress_cb;
6877 void *progress_arg;
6878 struct got_pathlist_head *merged_paths;
6881 static const struct got_error *
6882 collect_merged_paths(void *arg, unsigned char status, const char *path)
6884 const struct got_error *err;
6885 struct collect_merged_paths_arg *a = arg;
6886 char *p;
6887 struct got_pathlist_entry *new;
6889 err = (*a->progress_cb)(a->progress_arg, status, path);
6890 if (err)
6891 return err;
6893 if (status != GOT_STATUS_MERGE &&
6894 status != GOT_STATUS_ADD &&
6895 status != GOT_STATUS_DELETE &&
6896 status != GOT_STATUS_CONFLICT)
6897 return NULL;
6899 p = strdup(path);
6900 if (p == NULL)
6901 return got_error_from_errno("strdup");
6903 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6904 if (err || new == NULL)
6905 free(p);
6906 return err;
6909 static const struct got_error *
6910 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6911 int is_rebase, struct got_repository *repo)
6913 const struct got_error *err;
6914 struct got_reference *commit_ref = NULL;
6916 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6917 if (err) {
6918 if (err->code != GOT_ERR_NOT_REF)
6919 goto done;
6920 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6921 if (err)
6922 goto done;
6923 err = got_ref_write(commit_ref, repo);
6924 if (err)
6925 goto done;
6926 } else if (is_rebase) {
6927 struct got_object_id *stored_id;
6928 int cmp;
6930 err = got_ref_resolve(&stored_id, repo, commit_ref);
6931 if (err)
6932 goto done;
6933 cmp = got_object_id_cmp(commit_id, stored_id);
6934 free(stored_id);
6935 if (cmp != 0) {
6936 err = got_error(GOT_ERR_REBASE_COMMITID);
6937 goto done;
6940 done:
6941 if (commit_ref)
6942 got_ref_close(commit_ref);
6943 return err;
6946 static const struct got_error *
6947 rebase_merge_files(struct got_pathlist_head *merged_paths,
6948 const char *commit_ref_name, struct got_worktree *worktree,
6949 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6950 struct got_object_id *commit_id, struct got_repository *repo,
6951 got_worktree_checkout_cb progress_cb, void *progress_arg,
6952 got_cancel_cb cancel_cb, void *cancel_arg)
6954 const struct got_error *err;
6955 struct got_reference *commit_ref = NULL;
6956 struct collect_merged_paths_arg cmp_arg;
6957 char *fileindex_path;
6959 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6961 err = get_fileindex_path(&fileindex_path, worktree);
6962 if (err)
6963 return err;
6965 cmp_arg.progress_cb = progress_cb;
6966 cmp_arg.progress_arg = progress_arg;
6967 cmp_arg.merged_paths = merged_paths;
6968 err = merge_files(worktree, fileindex, fileindex_path,
6969 parent_commit_id, commit_id, repo, collect_merged_paths,
6970 &cmp_arg, cancel_cb, cancel_arg);
6971 if (commit_ref)
6972 got_ref_close(commit_ref);
6973 return err;
6976 const struct got_error *
6977 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6978 struct got_worktree *worktree, struct got_fileindex *fileindex,
6979 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6980 struct got_repository *repo,
6981 got_worktree_checkout_cb progress_cb, void *progress_arg,
6982 got_cancel_cb cancel_cb, void *cancel_arg)
6984 const struct got_error *err;
6985 char *commit_ref_name;
6987 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6988 if (err)
6989 return err;
6991 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6992 if (err)
6993 goto done;
6995 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6996 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6997 progress_arg, cancel_cb, cancel_arg);
6998 done:
6999 free(commit_ref_name);
7000 return err;
7003 const struct got_error *
7004 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7005 struct got_worktree *worktree, struct got_fileindex *fileindex,
7006 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7007 struct got_repository *repo,
7008 got_worktree_checkout_cb progress_cb, void *progress_arg,
7009 got_cancel_cb cancel_cb, void *cancel_arg)
7011 const struct got_error *err;
7012 char *commit_ref_name;
7014 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7015 if (err)
7016 return err;
7018 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7019 if (err)
7020 goto done;
7022 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7023 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7024 progress_arg, cancel_cb, cancel_arg);
7025 done:
7026 free(commit_ref_name);
7027 return err;
7030 static const struct got_error *
7031 rebase_commit(struct got_object_id **new_commit_id,
7032 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7033 struct got_worktree *worktree, struct got_fileindex *fileindex,
7034 struct got_reference *tmp_branch, const char *committer,
7035 struct got_commit_object *orig_commit, const char *new_logmsg,
7036 int allow_conflict, struct got_repository *repo)
7038 const struct got_error *err, *sync_err;
7039 struct got_pathlist_head commitable_paths;
7040 struct collect_commitables_arg cc_arg;
7041 char *fileindex_path = NULL;
7042 struct got_reference *head_ref = NULL;
7043 struct got_object_id *head_commit_id = NULL;
7044 char *logmsg = NULL;
7046 memset(&cc_arg, 0, sizeof(cc_arg));
7047 TAILQ_INIT(&commitable_paths);
7048 *new_commit_id = NULL;
7050 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7052 err = get_fileindex_path(&fileindex_path, worktree);
7053 if (err)
7054 return err;
7056 cc_arg.commitable_paths = &commitable_paths;
7057 cc_arg.worktree = worktree;
7058 cc_arg.repo = repo;
7059 cc_arg.have_staged_files = 0;
7060 cc_arg.commit_conflicts = allow_conflict;
7062 * If possible get the status of individual files directly to
7063 * avoid crawling the entire work tree once per rebased commit.
7065 * Ideally, merged_paths would contain a list of commitables
7066 * we could use so we could skip worktree_status() entirely.
7067 * However, we would then need carefully keep track of cumulative
7068 * effects of operations such as file additions and deletions
7069 * in 'got histedit -f' (folding multiple commits into one),
7070 * and this extra complexity is not really worth it.
7072 if (merged_paths) {
7073 struct got_pathlist_entry *pe;
7074 TAILQ_FOREACH(pe, merged_paths, entry) {
7075 err = worktree_status(worktree, pe->path, fileindex,
7076 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7077 0);
7078 if (err)
7079 goto done;
7081 } else {
7082 err = worktree_status(worktree, "", fileindex, repo,
7083 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7084 if (err)
7085 goto done;
7088 if (TAILQ_EMPTY(&commitable_paths)) {
7089 /* No-op change; commit will be elided. */
7090 err = got_ref_delete(commit_ref, repo);
7091 if (err)
7092 goto done;
7093 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7094 goto done;
7097 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7098 if (err)
7099 goto done;
7101 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7102 if (err)
7103 goto done;
7105 if (new_logmsg) {
7106 logmsg = strdup(new_logmsg);
7107 if (logmsg == NULL) {
7108 err = got_error_from_errno("strdup");
7109 goto done;
7111 } else {
7112 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7113 if (err)
7114 goto done;
7117 /* NB: commit_worktree will call free(logmsg) */
7118 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7119 NULL, worktree, got_object_commit_get_author(orig_commit),
7120 committer ? committer :
7121 got_object_commit_get_committer(orig_commit), NULL,
7122 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7123 if (err)
7124 goto done;
7126 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7127 if (err)
7128 goto done;
7130 err = got_ref_delete(commit_ref, repo);
7131 if (err)
7132 goto done;
7134 err = update_fileindex_after_commit(worktree, &commitable_paths,
7135 *new_commit_id, fileindex, 0);
7136 sync_err = sync_fileindex(fileindex, fileindex_path);
7137 if (sync_err && err == NULL)
7138 err = sync_err;
7139 done:
7140 free(fileindex_path);
7141 free(head_commit_id);
7142 if (head_ref)
7143 got_ref_close(head_ref);
7144 if (err) {
7145 free(*new_commit_id);
7146 *new_commit_id = NULL;
7148 return err;
7151 const struct got_error *
7152 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7153 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7154 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7155 const char *committer, struct got_commit_object *orig_commit,
7156 struct got_object_id *orig_commit_id, int allow_conflict,
7157 struct got_repository *repo)
7159 const struct got_error *err;
7160 char *commit_ref_name;
7161 struct got_reference *commit_ref = NULL;
7162 struct got_object_id *commit_id = NULL;
7164 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7165 if (err)
7166 return err;
7168 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7169 if (err)
7170 goto done;
7171 err = got_ref_resolve(&commit_id, repo, commit_ref);
7172 if (err)
7173 goto done;
7174 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7175 err = got_error(GOT_ERR_REBASE_COMMITID);
7176 goto done;
7179 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7180 worktree, fileindex, tmp_branch, committer, orig_commit,
7181 NULL, allow_conflict, repo);
7182 done:
7183 if (commit_ref)
7184 got_ref_close(commit_ref);
7185 free(commit_ref_name);
7186 free(commit_id);
7187 return err;
7190 const struct got_error *
7191 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7192 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7193 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7194 const char *committer, struct got_commit_object *orig_commit,
7195 struct got_object_id *orig_commit_id, const char *new_logmsg,
7196 int allow_conflict, struct got_repository *repo)
7198 const struct got_error *err;
7199 char *commit_ref_name;
7200 struct got_reference *commit_ref = NULL;
7202 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7203 if (err)
7204 return err;
7206 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7207 if (err)
7208 goto done;
7210 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7211 worktree, fileindex, tmp_branch, committer, orig_commit,
7212 new_logmsg, allow_conflict, repo);
7213 done:
7214 if (commit_ref)
7215 got_ref_close(commit_ref);
7216 free(commit_ref_name);
7217 return err;
7220 const struct got_error *
7221 got_worktree_rebase_postpone(struct got_worktree *worktree,
7222 struct got_fileindex *fileindex)
7224 if (fileindex)
7225 got_fileindex_free(fileindex);
7226 return lock_worktree(worktree, LOCK_SH);
7229 static const struct got_error *
7230 delete_ref(const char *name, struct got_repository *repo)
7232 const struct got_error *err;
7233 struct got_reference *ref;
7235 err = got_ref_open(&ref, repo, name, 0);
7236 if (err) {
7237 if (err->code == GOT_ERR_NOT_REF)
7238 return NULL;
7239 return err;
7242 err = got_ref_delete(ref, repo);
7243 got_ref_close(ref);
7244 return err;
7247 static const struct got_error *
7248 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7250 const struct got_error *err;
7251 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7252 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7254 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7255 if (err)
7256 goto done;
7257 err = delete_ref(tmp_branch_name, repo);
7258 if (err)
7259 goto done;
7261 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7262 if (err)
7263 goto done;
7264 err = delete_ref(new_base_branch_ref_name, repo);
7265 if (err)
7266 goto done;
7268 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7269 if (err)
7270 goto done;
7271 err = delete_ref(branch_ref_name, repo);
7272 if (err)
7273 goto done;
7275 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7276 if (err)
7277 goto done;
7278 err = delete_ref(commit_ref_name, repo);
7279 if (err)
7280 goto done;
7282 done:
7283 free(tmp_branch_name);
7284 free(new_base_branch_ref_name);
7285 free(branch_ref_name);
7286 free(commit_ref_name);
7287 return err;
7290 static const struct got_error *
7291 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7292 struct got_object_id *new_commit_id, struct got_repository *repo)
7294 const struct got_error *err;
7295 struct got_reference *ref = NULL;
7296 struct got_object_id *old_commit_id = NULL;
7297 const char *branch_name = NULL;
7298 char *new_id_str = NULL;
7299 char *refname = NULL;
7301 branch_name = got_ref_get_name(branch);
7302 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7303 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7304 branch_name += 11;
7306 err = got_object_id_str(&new_id_str, new_commit_id);
7307 if (err)
7308 return err;
7310 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7311 new_id_str) == -1) {
7312 err = got_error_from_errno("asprintf");
7313 goto done;
7316 err = got_ref_resolve(&old_commit_id, repo, branch);
7317 if (err)
7318 goto done;
7320 err = got_ref_alloc(&ref, refname, old_commit_id);
7321 if (err)
7322 goto done;
7324 err = got_ref_write(ref, repo);
7325 done:
7326 free(new_id_str);
7327 free(refname);
7328 free(old_commit_id);
7329 if (ref)
7330 got_ref_close(ref);
7331 return err;
7334 const struct got_error *
7335 got_worktree_rebase_complete(struct got_worktree *worktree,
7336 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7337 struct got_reference *rebased_branch, struct got_repository *repo,
7338 int create_backup)
7340 const struct got_error *err, *unlockerr, *sync_err;
7341 struct got_object_id *new_head_commit_id = NULL;
7342 char *fileindex_path = NULL;
7344 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7345 if (err)
7346 return err;
7348 if (create_backup) {
7349 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7350 rebased_branch, new_head_commit_id, repo);
7351 if (err)
7352 goto done;
7355 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7356 if (err)
7357 goto done;
7359 err = got_ref_write(rebased_branch, repo);
7360 if (err)
7361 goto done;
7363 err = got_worktree_set_head_ref(worktree, rebased_branch);
7364 if (err)
7365 goto done;
7367 err = delete_rebase_refs(worktree, repo);
7368 if (err)
7369 goto done;
7371 err = get_fileindex_path(&fileindex_path, worktree);
7372 if (err)
7373 goto done;
7374 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7375 sync_err = sync_fileindex(fileindex, fileindex_path);
7376 if (sync_err && err == NULL)
7377 err = sync_err;
7378 done:
7379 got_fileindex_free(fileindex);
7380 free(fileindex_path);
7381 free(new_head_commit_id);
7382 unlockerr = lock_worktree(worktree, LOCK_SH);
7383 if (unlockerr && err == NULL)
7384 err = unlockerr;
7385 return err;
7388 static const struct got_error *
7389 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7390 struct got_object_id *id1, struct got_object_id *id2,
7391 struct got_repository *repo)
7393 const struct got_error *err;
7394 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7395 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7397 if (id1) {
7398 err = got_object_open_as_commit(&commit1, repo, id1);
7399 if (err)
7400 goto done;
7402 err = got_object_open_as_tree(&tree1, repo,
7403 got_object_commit_get_tree_id(commit1));
7404 if (err)
7405 goto done;
7408 if (id2) {
7409 err = got_object_open_as_commit(&commit2, repo, id2);
7410 if (err)
7411 goto done;
7413 err = got_object_open_as_tree(&tree2, repo,
7414 got_object_commit_get_tree_id(commit2));
7415 if (err)
7416 goto done;
7419 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7420 got_diff_tree_collect_changed_paths, paths, 0);
7421 if (err)
7422 goto done;
7423 done:
7424 if (commit1)
7425 got_object_commit_close(commit1);
7426 if (commit2)
7427 got_object_commit_close(commit2);
7428 if (tree1)
7429 got_object_tree_close(tree1);
7430 if (tree2)
7431 got_object_tree_close(tree2);
7432 return err;
7435 static const struct got_error *
7436 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7437 struct got_object_id *id1, struct got_object_id *id2,
7438 const char *path_prefix, struct got_repository *repo)
7440 const struct got_error *err;
7441 struct got_pathlist_head merged_paths;
7442 struct got_pathlist_entry *pe;
7443 char *abspath = NULL, *wt_path = NULL;
7445 TAILQ_INIT(&merged_paths);
7447 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7448 if (err)
7449 goto done;
7451 TAILQ_FOREACH(pe, &merged_paths, entry) {
7452 struct got_diff_changed_path *change = pe->data;
7454 if (change->status != GOT_STATUS_ADD)
7455 continue;
7457 if (got_path_is_root_dir(path_prefix)) {
7458 wt_path = strdup(pe->path);
7459 if (wt_path == NULL) {
7460 err = got_error_from_errno("strdup");
7461 goto done;
7463 } else {
7464 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7465 err = got_error_from_errno("asprintf");
7466 goto done;
7469 err = got_path_skip_common_ancestor(&wt_path,
7470 path_prefix, abspath);
7471 if (err)
7472 goto done;
7473 free(abspath);
7474 abspath = NULL;
7477 err = got_pathlist_append(added_paths, wt_path, NULL);
7478 if (err)
7479 goto done;
7480 wt_path = NULL;
7483 done:
7484 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7485 free(abspath);
7486 free(wt_path);
7487 return err;
7490 static const struct got_error *
7491 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7492 struct got_object_id *id, const char *path_prefix,
7493 struct got_repository *repo)
7495 const struct got_error *err;
7496 struct got_commit_object *commit = NULL;
7497 struct got_object_qid *pid;
7499 err = got_object_open_as_commit(&commit, repo, id);
7500 if (err)
7501 goto done;
7503 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7505 err = get_paths_added_between_commits(added_paths,
7506 pid ? &pid->id : NULL, id, path_prefix, repo);
7507 if (err)
7508 goto done;
7509 done:
7510 if (commit)
7511 got_object_commit_close(commit);
7512 return err;
7515 const struct got_error *
7516 got_worktree_rebase_abort(struct got_worktree *worktree,
7517 struct got_fileindex *fileindex, struct got_repository *repo,
7518 struct got_reference *new_base_branch,
7519 got_worktree_checkout_cb progress_cb, void *progress_arg)
7521 const struct got_error *err, *unlockerr, *sync_err;
7522 struct got_reference *resolved = NULL;
7523 struct got_object_id *commit_id = NULL;
7524 struct got_object_id *merged_commit_id = NULL;
7525 struct got_commit_object *commit = NULL;
7526 char *fileindex_path = NULL;
7527 char *commit_ref_name = NULL;
7528 struct got_reference *commit_ref = NULL;
7529 struct revert_file_args rfa;
7530 struct got_object_id *tree_id = NULL;
7531 struct got_pathlist_head added_paths;
7533 TAILQ_INIT(&added_paths);
7535 err = lock_worktree(worktree, LOCK_EX);
7536 if (err)
7537 return err;
7539 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7540 if (err)
7541 goto done;
7543 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7544 if (err)
7545 goto done;
7547 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7548 if (err)
7549 goto done;
7552 * Determine which files in added status can be safely removed
7553 * from disk while reverting changes in the work tree.
7554 * We want to avoid deleting unrelated files which were added by
7555 * the user for conflict resolution purposes.
7557 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7558 got_worktree_get_path_prefix(worktree), repo);
7559 if (err)
7560 goto done;
7562 err = got_ref_open(&resolved, repo,
7563 got_ref_get_symref_target(new_base_branch), 0);
7564 if (err)
7565 goto done;
7567 err = got_worktree_set_head_ref(worktree, resolved);
7568 if (err)
7569 goto done;
7572 * XXX commits to the base branch could have happened while
7573 * we were busy rebasing; should we store the original commit ID
7574 * when rebase begins and read it back here?
7576 err = got_ref_resolve(&commit_id, repo, resolved);
7577 if (err)
7578 goto done;
7580 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7581 if (err)
7582 goto done;
7584 err = got_object_open_as_commit(&commit, repo,
7585 worktree->base_commit_id);
7586 if (err)
7587 goto done;
7589 err = got_object_id_by_path(&tree_id, repo, commit,
7590 worktree->path_prefix);
7591 if (err)
7592 goto done;
7594 err = delete_rebase_refs(worktree, repo);
7595 if (err)
7596 goto done;
7598 err = get_fileindex_path(&fileindex_path, worktree);
7599 if (err)
7600 goto done;
7602 rfa.worktree = worktree;
7603 rfa.fileindex = fileindex;
7604 rfa.progress_cb = progress_cb;
7605 rfa.progress_arg = progress_arg;
7606 rfa.patch_cb = NULL;
7607 rfa.patch_arg = NULL;
7608 rfa.repo = repo;
7609 rfa.unlink_added_files = 1;
7610 rfa.added_files_to_unlink = &added_paths;
7611 err = worktree_status(worktree, "", fileindex, repo,
7612 revert_file, &rfa, NULL, NULL, 1, 0);
7613 if (err)
7614 goto sync;
7616 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7617 repo, progress_cb, progress_arg, NULL, NULL);
7618 sync:
7619 sync_err = sync_fileindex(fileindex, fileindex_path);
7620 if (sync_err && err == NULL)
7621 err = sync_err;
7622 done:
7623 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7624 got_ref_close(resolved);
7625 free(tree_id);
7626 free(commit_id);
7627 free(merged_commit_id);
7628 if (commit)
7629 got_object_commit_close(commit);
7630 if (fileindex)
7631 got_fileindex_free(fileindex);
7632 free(fileindex_path);
7633 free(commit_ref_name);
7634 if (commit_ref)
7635 got_ref_close(commit_ref);
7637 unlockerr = lock_worktree(worktree, LOCK_SH);
7638 if (unlockerr && err == NULL)
7639 err = unlockerr;
7640 return err;
7643 const struct got_error *
7644 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7645 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7646 struct got_fileindex **fileindex, struct got_worktree *worktree,
7647 struct got_repository *repo)
7649 const struct got_error *err = NULL;
7650 char *tmp_branch_name = NULL;
7651 char *branch_ref_name = NULL;
7652 char *base_commit_ref_name = NULL;
7653 char *fileindex_path = NULL;
7654 struct check_rebase_ok_arg ok_arg;
7655 struct got_reference *wt_branch = NULL;
7656 struct got_reference *base_commit_ref = NULL;
7658 *tmp_branch = NULL;
7659 *branch_ref = NULL;
7660 *base_commit_id = NULL;
7661 *fileindex = NULL;
7663 err = lock_worktree(worktree, LOCK_EX);
7664 if (err)
7665 return err;
7667 err = open_fileindex(fileindex, &fileindex_path, worktree);
7668 if (err)
7669 goto done;
7671 ok_arg.worktree = worktree;
7672 ok_arg.repo = repo;
7673 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7674 &ok_arg);
7675 if (err)
7676 goto done;
7678 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7679 if (err)
7680 goto done;
7682 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7683 if (err)
7684 goto done;
7686 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7687 worktree);
7688 if (err)
7689 goto done;
7691 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7692 0);
7693 if (err)
7694 goto done;
7696 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7697 if (err)
7698 goto done;
7700 err = got_ref_write(*branch_ref, repo);
7701 if (err)
7702 goto done;
7704 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7705 worktree->base_commit_id);
7706 if (err)
7707 goto done;
7708 err = got_ref_write(base_commit_ref, repo);
7709 if (err)
7710 goto done;
7711 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7712 if (*base_commit_id == NULL) {
7713 err = got_error_from_errno("got_object_id_dup");
7714 goto done;
7717 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7718 worktree->base_commit_id);
7719 if (err)
7720 goto done;
7721 err = got_ref_write(*tmp_branch, repo);
7722 if (err)
7723 goto done;
7725 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7726 if (err)
7727 goto done;
7728 done:
7729 free(fileindex_path);
7730 free(tmp_branch_name);
7731 free(branch_ref_name);
7732 free(base_commit_ref_name);
7733 if (wt_branch)
7734 got_ref_close(wt_branch);
7735 if (err) {
7736 if (*branch_ref) {
7737 got_ref_close(*branch_ref);
7738 *branch_ref = NULL;
7740 if (*tmp_branch) {
7741 got_ref_close(*tmp_branch);
7742 *tmp_branch = NULL;
7744 free(*base_commit_id);
7745 if (*fileindex) {
7746 got_fileindex_free(*fileindex);
7747 *fileindex = NULL;
7749 lock_worktree(worktree, LOCK_SH);
7751 return err;
7754 const struct got_error *
7755 got_worktree_histedit_postpone(struct got_worktree *worktree,
7756 struct got_fileindex *fileindex)
7758 if (fileindex)
7759 got_fileindex_free(fileindex);
7760 return lock_worktree(worktree, LOCK_SH);
7763 const struct got_error *
7764 got_worktree_histedit_in_progress(int *in_progress,
7765 struct got_worktree *worktree)
7767 const struct got_error *err;
7768 char *tmp_branch_name = NULL;
7770 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7771 if (err)
7772 return err;
7774 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7775 free(tmp_branch_name);
7776 return NULL;
7779 const struct got_error *
7780 got_worktree_histedit_continue(struct got_object_id **commit_id,
7781 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7782 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7783 struct got_worktree *worktree, struct got_repository *repo)
7785 const struct got_error *err;
7786 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7787 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7788 struct got_reference *commit_ref = NULL;
7789 struct got_reference *base_commit_ref = NULL;
7790 char *fileindex_path = NULL;
7791 int have_staged_files = 0;
7793 *commit_id = NULL;
7794 *tmp_branch = NULL;
7795 *base_commit_id = NULL;
7796 *fileindex = NULL;
7798 err = lock_worktree(worktree, LOCK_EX);
7799 if (err)
7800 return err;
7802 err = open_fileindex(fileindex, &fileindex_path, worktree);
7803 if (err)
7804 goto done;
7806 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7807 &have_staged_files);
7808 if (err && err->code != GOT_ERR_CANCELLED)
7809 goto done;
7810 if (have_staged_files) {
7811 err = got_error(GOT_ERR_STAGED_PATHS);
7812 goto done;
7815 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7816 if (err)
7817 goto done;
7819 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7820 if (err)
7821 goto done;
7823 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7824 if (err)
7825 goto done;
7827 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7828 worktree);
7829 if (err)
7830 goto done;
7832 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7833 if (err)
7834 goto done;
7836 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7837 if (err)
7838 goto done;
7839 err = got_ref_resolve(commit_id, repo, commit_ref);
7840 if (err)
7841 goto done;
7843 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7844 if (err)
7845 goto done;
7846 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7847 if (err)
7848 goto done;
7850 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7851 if (err)
7852 goto done;
7853 done:
7854 free(commit_ref_name);
7855 free(branch_ref_name);
7856 free(fileindex_path);
7857 if (commit_ref)
7858 got_ref_close(commit_ref);
7859 if (base_commit_ref)
7860 got_ref_close(base_commit_ref);
7861 if (err) {
7862 free(*commit_id);
7863 *commit_id = NULL;
7864 free(*base_commit_id);
7865 *base_commit_id = NULL;
7866 if (*tmp_branch) {
7867 got_ref_close(*tmp_branch);
7868 *tmp_branch = NULL;
7870 if (*fileindex) {
7871 got_fileindex_free(*fileindex);
7872 *fileindex = NULL;
7874 lock_worktree(worktree, LOCK_EX);
7876 return err;
7879 static const struct got_error *
7880 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7882 const struct got_error *err;
7883 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7884 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7886 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7887 if (err)
7888 goto done;
7889 err = delete_ref(tmp_branch_name, repo);
7890 if (err)
7891 goto done;
7893 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7894 worktree);
7895 if (err)
7896 goto done;
7897 err = delete_ref(base_commit_ref_name, repo);
7898 if (err)
7899 goto done;
7901 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7902 if (err)
7903 goto done;
7904 err = delete_ref(branch_ref_name, repo);
7905 if (err)
7906 goto done;
7908 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7909 if (err)
7910 goto done;
7911 err = delete_ref(commit_ref_name, repo);
7912 if (err)
7913 goto done;
7914 done:
7915 free(tmp_branch_name);
7916 free(base_commit_ref_name);
7917 free(branch_ref_name);
7918 free(commit_ref_name);
7919 return err;
7922 const struct got_error *
7923 got_worktree_histedit_abort(struct got_worktree *worktree,
7924 struct got_fileindex *fileindex, struct got_repository *repo,
7925 struct got_reference *branch, struct got_object_id *base_commit_id,
7926 got_worktree_checkout_cb progress_cb, void *progress_arg)
7928 const struct got_error *err, *unlockerr, *sync_err;
7929 struct got_reference *resolved = NULL;
7930 char *fileindex_path = NULL;
7931 struct got_object_id *merged_commit_id = NULL;
7932 struct got_commit_object *commit = NULL;
7933 char *commit_ref_name = NULL;
7934 struct got_reference *commit_ref = NULL;
7935 struct got_object_id *tree_id = NULL;
7936 struct revert_file_args rfa;
7937 struct got_pathlist_head added_paths;
7939 TAILQ_INIT(&added_paths);
7941 err = lock_worktree(worktree, LOCK_EX);
7942 if (err)
7943 return err;
7945 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7946 if (err)
7947 goto done;
7949 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7950 if (err) {
7951 if (err->code != GOT_ERR_NOT_REF)
7952 goto done;
7953 /* Can happen on early abort due to invalid histedit script. */
7954 commit_ref = NULL;
7957 if (commit_ref) {
7958 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7959 if (err)
7960 goto done;
7963 * Determine which files in added status can be safely removed
7964 * from disk while reverting changes in the work tree.
7965 * We want to avoid deleting unrelated files added by the
7966 * user during conflict resolution or during histedit -e.
7968 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7969 got_worktree_get_path_prefix(worktree), repo);
7970 if (err)
7971 goto done;
7974 err = got_ref_open(&resolved, repo,
7975 got_ref_get_symref_target(branch), 0);
7976 if (err)
7977 goto done;
7979 err = got_worktree_set_head_ref(worktree, resolved);
7980 if (err)
7981 goto done;
7983 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7984 if (err)
7985 goto done;
7987 err = got_object_open_as_commit(&commit, repo,
7988 worktree->base_commit_id);
7989 if (err)
7990 goto done;
7992 err = got_object_id_by_path(&tree_id, repo, commit,
7993 worktree->path_prefix);
7994 if (err)
7995 goto done;
7997 err = delete_histedit_refs(worktree, repo);
7998 if (err)
7999 goto done;
8001 err = get_fileindex_path(&fileindex_path, worktree);
8002 if (err)
8003 goto done;
8005 rfa.worktree = worktree;
8006 rfa.fileindex = fileindex;
8007 rfa.progress_cb = progress_cb;
8008 rfa.progress_arg = progress_arg;
8009 rfa.patch_cb = NULL;
8010 rfa.patch_arg = NULL;
8011 rfa.repo = repo;
8012 rfa.unlink_added_files = 1;
8013 rfa.added_files_to_unlink = &added_paths;
8014 err = worktree_status(worktree, "", fileindex, repo,
8015 revert_file, &rfa, NULL, NULL, 1, 0);
8016 if (err)
8017 goto sync;
8019 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8020 repo, progress_cb, progress_arg, NULL, NULL);
8021 sync:
8022 sync_err = sync_fileindex(fileindex, fileindex_path);
8023 if (sync_err && err == NULL)
8024 err = sync_err;
8025 done:
8026 if (resolved)
8027 got_ref_close(resolved);
8028 if (commit_ref)
8029 got_ref_close(commit_ref);
8030 free(merged_commit_id);
8031 free(tree_id);
8032 free(fileindex_path);
8033 free(commit_ref_name);
8035 unlockerr = lock_worktree(worktree, LOCK_SH);
8036 if (unlockerr && err == NULL)
8037 err = unlockerr;
8038 return err;
8041 const struct got_error *
8042 got_worktree_histedit_complete(struct got_worktree *worktree,
8043 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8044 struct got_reference *edited_branch, struct got_repository *repo)
8046 const struct got_error *err, *unlockerr, *sync_err;
8047 struct got_object_id *new_head_commit_id = NULL;
8048 struct got_reference *resolved = NULL;
8049 char *fileindex_path = NULL;
8051 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8052 if (err)
8053 return err;
8055 err = got_ref_open(&resolved, repo,
8056 got_ref_get_symref_target(edited_branch), 0);
8057 if (err)
8058 goto done;
8060 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8061 resolved, new_head_commit_id, repo);
8062 if (err)
8063 goto done;
8065 err = got_ref_change_ref(resolved, new_head_commit_id);
8066 if (err)
8067 goto done;
8069 err = got_ref_write(resolved, repo);
8070 if (err)
8071 goto done;
8073 err = got_worktree_set_head_ref(worktree, resolved);
8074 if (err)
8075 goto done;
8077 err = delete_histedit_refs(worktree, repo);
8078 if (err)
8079 goto done;
8081 err = get_fileindex_path(&fileindex_path, worktree);
8082 if (err)
8083 goto done;
8084 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8085 sync_err = sync_fileindex(fileindex, fileindex_path);
8086 if (sync_err && err == NULL)
8087 err = sync_err;
8088 done:
8089 got_fileindex_free(fileindex);
8090 free(fileindex_path);
8091 free(new_head_commit_id);
8092 unlockerr = lock_worktree(worktree, LOCK_SH);
8093 if (unlockerr && err == NULL)
8094 err = unlockerr;
8095 return err;
8098 const struct got_error *
8099 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8100 struct got_object_id *commit_id, struct got_repository *repo)
8102 const struct got_error *err;
8103 char *commit_ref_name;
8105 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8106 if (err)
8107 return err;
8109 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8110 if (err)
8111 goto done;
8113 err = delete_ref(commit_ref_name, repo);
8114 done:
8115 free(commit_ref_name);
8116 return err;
8119 const struct got_error *
8120 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8121 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8122 struct got_worktree *worktree, const char *refname,
8123 struct got_repository *repo)
8125 const struct got_error *err = NULL;
8126 char *fileindex_path = NULL;
8127 struct check_rebase_ok_arg ok_arg;
8129 *fileindex = NULL;
8130 *branch_ref = NULL;
8131 *base_branch_ref = NULL;
8133 err = lock_worktree(worktree, LOCK_EX);
8134 if (err)
8135 return err;
8137 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8138 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8139 "cannot integrate a branch into itself; "
8140 "update -b or different branch name required");
8141 goto done;
8144 err = open_fileindex(fileindex, &fileindex_path, worktree);
8145 if (err)
8146 goto done;
8148 /* Preconditions are the same as for rebase. */
8149 ok_arg.worktree = worktree;
8150 ok_arg.repo = repo;
8151 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8152 &ok_arg);
8153 if (err)
8154 goto done;
8156 err = got_ref_open(branch_ref, repo, refname, 1);
8157 if (err)
8158 goto done;
8160 err = got_ref_open(base_branch_ref, repo,
8161 got_worktree_get_head_ref_name(worktree), 1);
8162 done:
8163 if (err) {
8164 if (*branch_ref) {
8165 got_ref_close(*branch_ref);
8166 *branch_ref = NULL;
8168 if (*base_branch_ref) {
8169 got_ref_close(*base_branch_ref);
8170 *base_branch_ref = NULL;
8172 if (*fileindex) {
8173 got_fileindex_free(*fileindex);
8174 *fileindex = NULL;
8176 lock_worktree(worktree, LOCK_SH);
8178 return err;
8181 const struct got_error *
8182 got_worktree_integrate_continue(struct got_worktree *worktree,
8183 struct got_fileindex *fileindex, struct got_repository *repo,
8184 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8185 got_worktree_checkout_cb progress_cb, void *progress_arg,
8186 got_cancel_cb cancel_cb, void *cancel_arg)
8188 const struct got_error *err = NULL, *sync_err, *unlockerr;
8189 char *fileindex_path = NULL;
8190 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8191 struct got_commit_object *commit = NULL;
8193 err = get_fileindex_path(&fileindex_path, worktree);
8194 if (err)
8195 goto done;
8197 err = got_ref_resolve(&commit_id, repo, branch_ref);
8198 if (err)
8199 goto done;
8201 err = got_object_open_as_commit(&commit, repo, commit_id);
8202 if (err)
8203 goto done;
8205 err = got_object_id_by_path(&tree_id, repo, commit,
8206 worktree->path_prefix);
8207 if (err)
8208 goto done;
8210 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8211 if (err)
8212 goto done;
8214 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8215 progress_cb, progress_arg, cancel_cb, cancel_arg);
8216 if (err)
8217 goto sync;
8219 err = got_ref_change_ref(base_branch_ref, commit_id);
8220 if (err)
8221 goto sync;
8223 err = got_ref_write(base_branch_ref, repo);
8224 if (err)
8225 goto sync;
8227 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8228 sync:
8229 sync_err = sync_fileindex(fileindex, fileindex_path);
8230 if (sync_err && err == NULL)
8231 err = sync_err;
8233 done:
8234 unlockerr = got_ref_unlock(branch_ref);
8235 if (unlockerr && err == NULL)
8236 err = unlockerr;
8237 got_ref_close(branch_ref);
8239 unlockerr = got_ref_unlock(base_branch_ref);
8240 if (unlockerr && err == NULL)
8241 err = unlockerr;
8242 got_ref_close(base_branch_ref);
8244 got_fileindex_free(fileindex);
8245 free(fileindex_path);
8246 free(tree_id);
8247 if (commit)
8248 got_object_commit_close(commit);
8250 unlockerr = lock_worktree(worktree, LOCK_SH);
8251 if (unlockerr && err == NULL)
8252 err = unlockerr;
8253 return err;
8256 const struct got_error *
8257 got_worktree_integrate_abort(struct got_worktree *worktree,
8258 struct got_fileindex *fileindex, struct got_repository *repo,
8259 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8261 const struct got_error *err = NULL, *unlockerr = NULL;
8263 got_fileindex_free(fileindex);
8265 err = lock_worktree(worktree, LOCK_SH);
8267 unlockerr = got_ref_unlock(branch_ref);
8268 if (unlockerr && err == NULL)
8269 err = unlockerr;
8270 got_ref_close(branch_ref);
8272 unlockerr = got_ref_unlock(base_branch_ref);
8273 if (unlockerr && err == NULL)
8274 err = unlockerr;
8275 got_ref_close(base_branch_ref);
8277 return err;
8280 const struct got_error *
8281 got_worktree_merge_postpone(struct got_worktree *worktree,
8282 struct got_fileindex *fileindex)
8284 const struct got_error *err, *sync_err;
8285 char *fileindex_path = NULL;
8287 err = get_fileindex_path(&fileindex_path, worktree);
8288 if (err)
8289 goto done;
8291 sync_err = sync_fileindex(fileindex, fileindex_path);
8293 err = lock_worktree(worktree, LOCK_SH);
8294 if (sync_err && err == NULL)
8295 err = sync_err;
8296 done:
8297 got_fileindex_free(fileindex);
8298 free(fileindex_path);
8299 return err;
8302 static const struct got_error *
8303 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8305 const struct got_error *err;
8306 char *branch_refname = NULL, *commit_refname = NULL;
8308 err = get_merge_branch_ref_name(&branch_refname, worktree);
8309 if (err)
8310 goto done;
8311 err = delete_ref(branch_refname, repo);
8312 if (err)
8313 goto done;
8315 err = get_merge_commit_ref_name(&commit_refname, worktree);
8316 if (err)
8317 goto done;
8318 err = delete_ref(commit_refname, repo);
8319 if (err)
8320 goto done;
8322 done:
8323 free(branch_refname);
8324 free(commit_refname);
8325 return err;
8328 struct merge_commit_msg_arg {
8329 struct got_worktree *worktree;
8330 const char *branch_name;
8333 static const struct got_error *
8334 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8335 const char *diff_path, char **logmsg, void *arg)
8337 struct merge_commit_msg_arg *a = arg;
8339 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8340 got_worktree_get_head_ref_name(a->worktree)) == -1)
8341 return got_error_from_errno("asprintf");
8343 return NULL;
8347 const struct got_error *
8348 got_worktree_merge_branch(struct got_worktree *worktree,
8349 struct got_fileindex *fileindex,
8350 struct got_object_id *yca_commit_id,
8351 struct got_object_id *branch_tip,
8352 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8353 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8355 const struct got_error *err;
8356 char *fileindex_path = NULL;
8358 err = get_fileindex_path(&fileindex_path, worktree);
8359 if (err)
8360 goto done;
8362 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8363 worktree);
8364 if (err)
8365 goto done;
8367 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8368 branch_tip, repo, progress_cb, progress_arg,
8369 cancel_cb, cancel_arg);
8370 done:
8371 free(fileindex_path);
8372 return err;
8375 const struct got_error *
8376 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8377 struct got_worktree *worktree, struct got_fileindex *fileindex,
8378 const char *author, const char *committer, int allow_bad_symlinks,
8379 struct got_object_id *branch_tip, const char *branch_name,
8380 int allow_conflict, struct got_repository *repo,
8381 got_worktree_status_cb status_cb, void *status_arg)
8384 const struct got_error *err = NULL, *sync_err;
8385 struct got_pathlist_head commitable_paths;
8386 struct collect_commitables_arg cc_arg;
8387 struct got_pathlist_entry *pe;
8388 struct got_reference *head_ref = NULL;
8389 struct got_object_id *head_commit_id = NULL;
8390 int have_staged_files = 0;
8391 struct merge_commit_msg_arg mcm_arg;
8392 char *fileindex_path = NULL;
8394 memset(&cc_arg, 0, sizeof(cc_arg));
8395 *new_commit_id = NULL;
8397 TAILQ_INIT(&commitable_paths);
8399 err = get_fileindex_path(&fileindex_path, worktree);
8400 if (err)
8401 goto done;
8403 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8404 if (err)
8405 goto done;
8407 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8408 if (err)
8409 goto done;
8411 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8412 &have_staged_files);
8413 if (err && err->code != GOT_ERR_CANCELLED)
8414 goto done;
8415 if (have_staged_files) {
8416 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8417 goto done;
8420 cc_arg.commitable_paths = &commitable_paths;
8421 cc_arg.worktree = worktree;
8422 cc_arg.fileindex = fileindex;
8423 cc_arg.repo = repo;
8424 cc_arg.have_staged_files = have_staged_files;
8425 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8426 cc_arg.commit_conflicts = allow_conflict;
8427 err = worktree_status(worktree, "", fileindex, repo,
8428 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8429 if (err)
8430 goto done;
8432 mcm_arg.worktree = worktree;
8433 mcm_arg.branch_name = branch_name;
8434 err = commit_worktree(new_commit_id, &commitable_paths,
8435 head_commit_id, branch_tip, worktree, author, committer, NULL,
8436 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8437 if (err)
8438 goto done;
8440 err = update_fileindex_after_commit(worktree, &commitable_paths,
8441 *new_commit_id, fileindex, have_staged_files);
8442 sync_err = sync_fileindex(fileindex, fileindex_path);
8443 if (sync_err && err == NULL)
8444 err = sync_err;
8445 done:
8446 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8447 struct got_commitable *ct = pe->data;
8449 free_commitable(ct);
8451 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8452 free(fileindex_path);
8453 return err;
8456 const struct got_error *
8457 got_worktree_merge_complete(struct got_worktree *worktree,
8458 struct got_fileindex *fileindex, struct got_repository *repo)
8460 const struct got_error *err, *unlockerr, *sync_err;
8461 char *fileindex_path = NULL;
8463 err = delete_merge_refs(worktree, repo);
8464 if (err)
8465 goto done;
8467 err = get_fileindex_path(&fileindex_path, worktree);
8468 if (err)
8469 goto done;
8470 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8471 sync_err = sync_fileindex(fileindex, fileindex_path);
8472 if (sync_err && err == NULL)
8473 err = sync_err;
8474 done:
8475 got_fileindex_free(fileindex);
8476 free(fileindex_path);
8477 unlockerr = lock_worktree(worktree, LOCK_SH);
8478 if (unlockerr && err == NULL)
8479 err = unlockerr;
8480 return err;
8483 const struct got_error *
8484 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8485 struct got_repository *repo)
8487 const struct got_error *err;
8488 char *branch_refname = NULL;
8489 struct got_reference *branch_ref = NULL;
8491 *in_progress = 0;
8493 err = get_merge_branch_ref_name(&branch_refname, worktree);
8494 if (err)
8495 return err;
8496 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8497 free(branch_refname);
8498 if (err) {
8499 if (err->code != GOT_ERR_NOT_REF)
8500 return err;
8501 } else
8502 *in_progress = 1;
8504 return NULL;
8507 const struct got_error *got_worktree_merge_prepare(
8508 struct got_fileindex **fileindex, struct got_worktree *worktree,
8509 struct got_repository *repo)
8511 const struct got_error *err = NULL;
8512 char *fileindex_path = NULL;
8513 struct got_reference *wt_branch = NULL;
8514 struct got_object_id *wt_branch_tip = NULL;
8515 struct check_rebase_ok_arg ok_arg;
8517 *fileindex = NULL;
8519 err = lock_worktree(worktree, LOCK_EX);
8520 if (err)
8521 return err;
8523 err = open_fileindex(fileindex, &fileindex_path, worktree);
8524 if (err)
8525 goto done;
8527 /* Preconditions are the same as for rebase. */
8528 ok_arg.worktree = worktree;
8529 ok_arg.repo = repo;
8530 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8531 &ok_arg);
8532 if (err)
8533 goto done;
8535 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8536 0);
8537 if (err)
8538 goto done;
8540 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8541 if (err)
8542 goto done;
8544 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8545 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8546 goto done;
8549 done:
8550 free(fileindex_path);
8551 if (wt_branch)
8552 got_ref_close(wt_branch);
8553 free(wt_branch_tip);
8554 if (err) {
8555 if (*fileindex) {
8556 got_fileindex_free(*fileindex);
8557 *fileindex = NULL;
8559 lock_worktree(worktree, LOCK_SH);
8561 return err;
8564 const struct got_error *got_worktree_merge_write_refs(
8565 struct got_worktree *worktree, struct got_reference *branch,
8566 struct got_repository *repo)
8568 const struct got_error *err = NULL;
8569 char *branch_refname = NULL, *commit_refname = NULL;
8570 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8571 struct got_object_id *branch_tip = NULL;
8573 err = get_merge_branch_ref_name(&branch_refname, worktree);
8574 if (err)
8575 return err;
8577 err = get_merge_commit_ref_name(&commit_refname, worktree);
8578 if (err)
8579 return err;
8581 err = got_ref_resolve(&branch_tip, repo, branch);
8582 if (err)
8583 goto done;
8585 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8586 if (err)
8587 goto done;
8588 err = got_ref_write(branch_ref, repo);
8589 if (err)
8590 goto done;
8592 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8593 if (err)
8594 goto done;
8595 err = got_ref_write(commit_ref, repo);
8596 if (err)
8597 goto done;
8599 done:
8600 free(branch_refname);
8601 free(commit_refname);
8602 if (branch_ref)
8603 got_ref_close(branch_ref);
8604 if (commit_ref)
8605 got_ref_close(commit_ref);
8606 free(branch_tip);
8607 return err;
8610 const struct got_error *
8611 got_worktree_merge_continue(char **branch_name,
8612 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8613 struct got_worktree *worktree, struct got_repository *repo)
8615 const struct got_error *err;
8616 char *commit_refname = NULL, *branch_refname = NULL;
8617 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8618 char *fileindex_path = NULL;
8619 int have_staged_files = 0;
8621 *branch_name = NULL;
8622 *branch_tip = NULL;
8623 *fileindex = NULL;
8625 err = lock_worktree(worktree, LOCK_EX);
8626 if (err)
8627 return err;
8629 err = open_fileindex(fileindex, &fileindex_path, worktree);
8630 if (err)
8631 goto done;
8633 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8634 &have_staged_files);
8635 if (err && err->code != GOT_ERR_CANCELLED)
8636 goto done;
8637 if (have_staged_files) {
8638 err = got_error(GOT_ERR_STAGED_PATHS);
8639 goto done;
8642 err = get_merge_branch_ref_name(&branch_refname, worktree);
8643 if (err)
8644 goto done;
8646 err = get_merge_commit_ref_name(&commit_refname, worktree);
8647 if (err)
8648 goto done;
8650 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8651 if (err)
8652 goto done;
8654 if (!got_ref_is_symbolic(branch_ref)) {
8655 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8656 "%s is not a symbolic reference",
8657 got_ref_get_name(branch_ref));
8658 goto done;
8660 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8661 if (*branch_name == NULL) {
8662 err = got_error_from_errno("strdup");
8663 goto done;
8666 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8667 if (err)
8668 goto done;
8670 err = got_ref_resolve(branch_tip, repo, commit_ref);
8671 if (err)
8672 goto done;
8673 done:
8674 free(commit_refname);
8675 free(branch_refname);
8676 free(fileindex_path);
8677 if (commit_ref)
8678 got_ref_close(commit_ref);
8679 if (branch_ref)
8680 got_ref_close(branch_ref);
8681 if (err) {
8682 if (*branch_name) {
8683 free(*branch_name);
8684 *branch_name = NULL;
8686 free(*branch_tip);
8687 *branch_tip = NULL;
8688 if (*fileindex) {
8689 got_fileindex_free(*fileindex);
8690 *fileindex = NULL;
8692 lock_worktree(worktree, LOCK_SH);
8694 return err;
8697 const struct got_error *
8698 got_worktree_merge_abort(struct got_worktree *worktree,
8699 struct got_fileindex *fileindex, struct got_repository *repo,
8700 got_worktree_checkout_cb progress_cb, void *progress_arg)
8702 const struct got_error *err, *unlockerr, *sync_err;
8703 struct got_commit_object *commit = NULL;
8704 char *fileindex_path = NULL;
8705 struct revert_file_args rfa;
8706 char *commit_ref_name = NULL;
8707 struct got_reference *commit_ref = NULL;
8708 struct got_object_id *merged_commit_id = NULL;
8709 struct got_object_id *tree_id = NULL;
8710 struct got_pathlist_head added_paths;
8712 TAILQ_INIT(&added_paths);
8714 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8715 if (err)
8716 goto done;
8718 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8719 if (err)
8720 goto done;
8722 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8723 if (err)
8724 goto done;
8727 * Determine which files in added status can be safely removed
8728 * from disk while reverting changes in the work tree.
8729 * We want to avoid deleting unrelated files which were added by
8730 * the user for conflict resolution purposes.
8732 err = get_paths_added_between_commits(&added_paths,
8733 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8734 got_worktree_get_path_prefix(worktree), repo);
8735 if (err)
8736 goto done;
8739 err = got_object_open_as_commit(&commit, repo,
8740 worktree->base_commit_id);
8741 if (err)
8742 goto done;
8744 err = got_object_id_by_path(&tree_id, repo, commit,
8745 worktree->path_prefix);
8746 if (err)
8747 goto done;
8749 err = delete_merge_refs(worktree, repo);
8750 if (err)
8751 goto done;
8753 err = get_fileindex_path(&fileindex_path, worktree);
8754 if (err)
8755 goto done;
8757 rfa.worktree = worktree;
8758 rfa.fileindex = fileindex;
8759 rfa.progress_cb = progress_cb;
8760 rfa.progress_arg = progress_arg;
8761 rfa.patch_cb = NULL;
8762 rfa.patch_arg = NULL;
8763 rfa.repo = repo;
8764 rfa.unlink_added_files = 1;
8765 rfa.added_files_to_unlink = &added_paths;
8766 err = worktree_status(worktree, "", fileindex, repo,
8767 revert_file, &rfa, NULL, NULL, 1, 0);
8768 if (err)
8769 goto sync;
8771 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8772 repo, progress_cb, progress_arg, NULL, NULL);
8773 sync:
8774 sync_err = sync_fileindex(fileindex, fileindex_path);
8775 if (sync_err && err == NULL)
8776 err = sync_err;
8777 done:
8778 free(tree_id);
8779 free(merged_commit_id);
8780 if (commit)
8781 got_object_commit_close(commit);
8782 if (fileindex)
8783 got_fileindex_free(fileindex);
8784 free(fileindex_path);
8785 if (commit_ref)
8786 got_ref_close(commit_ref);
8787 free(commit_ref_name);
8789 unlockerr = lock_worktree(worktree, LOCK_SH);
8790 if (unlockerr && err == NULL)
8791 err = unlockerr;
8792 return err;
8795 struct check_stage_ok_arg {
8796 struct got_object_id *head_commit_id;
8797 struct got_worktree *worktree;
8798 struct got_fileindex *fileindex;
8799 struct got_repository *repo;
8800 int have_changes;
8803 static const struct got_error *
8804 check_stage_ok(void *arg, unsigned char status,
8805 unsigned char staged_status, const char *relpath,
8806 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8807 struct got_object_id *commit_id, int dirfd, const char *de_name)
8809 struct check_stage_ok_arg *a = arg;
8810 const struct got_error *err = NULL;
8811 struct got_fileindex_entry *ie;
8812 struct got_object_id base_commit_id;
8813 struct got_object_id *base_commit_idp = NULL;
8814 char *in_repo_path = NULL, *p;
8816 if (status == GOT_STATUS_UNVERSIONED ||
8817 status == GOT_STATUS_NO_CHANGE)
8818 return NULL;
8819 if (status == GOT_STATUS_NONEXISTENT)
8820 return got_error_set_errno(ENOENT, relpath);
8822 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8823 if (ie == NULL)
8824 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8826 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8827 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8828 relpath) == -1)
8829 return got_error_from_errno("asprintf");
8831 if (got_fileindex_entry_has_commit(ie)) {
8832 base_commit_idp = got_fileindex_entry_get_commit_id(
8833 &base_commit_id, ie);
8836 if (status == GOT_STATUS_CONFLICT) {
8837 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8838 goto done;
8839 } else if (status != GOT_STATUS_ADD &&
8840 status != GOT_STATUS_MODIFY &&
8841 status != GOT_STATUS_DELETE) {
8842 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8843 goto done;
8846 a->have_changes = 1;
8848 p = in_repo_path;
8849 while (p[0] == '/')
8850 p++;
8851 err = check_out_of_date(p, status, staged_status,
8852 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8853 GOT_ERR_STAGE_OUT_OF_DATE);
8854 done:
8855 free(in_repo_path);
8856 return err;
8859 struct stage_path_arg {
8860 struct got_worktree *worktree;
8861 struct got_fileindex *fileindex;
8862 struct got_repository *repo;
8863 got_worktree_status_cb status_cb;
8864 void *status_arg;
8865 got_worktree_patch_cb patch_cb;
8866 void *patch_arg;
8867 int staged_something;
8868 int allow_bad_symlinks;
8871 static const struct got_error *
8872 stage_path(void *arg, unsigned char status,
8873 unsigned char staged_status, const char *relpath,
8874 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8875 struct got_object_id *commit_id, int dirfd, const char *de_name)
8877 struct stage_path_arg *a = arg;
8878 const struct got_error *err = NULL;
8879 struct got_fileindex_entry *ie;
8880 char *ondisk_path = NULL, *path_content = NULL;
8881 uint32_t stage;
8882 struct got_object_id *new_staged_blob_id = NULL;
8883 struct stat sb;
8885 if (status == GOT_STATUS_UNVERSIONED)
8886 return NULL;
8888 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8889 if (ie == NULL)
8890 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8892 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8893 relpath)== -1)
8894 return got_error_from_errno("asprintf");
8896 switch (status) {
8897 case GOT_STATUS_ADD:
8898 case GOT_STATUS_MODIFY:
8899 /* XXX could sb.st_mode be passed in by our caller? */
8900 if (lstat(ondisk_path, &sb) == -1) {
8901 err = got_error_from_errno2("lstat", ondisk_path);
8902 break;
8904 if (a->patch_cb) {
8905 if (status == GOT_STATUS_ADD) {
8906 int choice = GOT_PATCH_CHOICE_NONE;
8907 err = (*a->patch_cb)(&choice, a->patch_arg,
8908 status, ie->path, NULL, 1, 1);
8909 if (err)
8910 break;
8911 if (choice != GOT_PATCH_CHOICE_YES)
8912 break;
8913 } else {
8914 err = create_patched_content(&path_content, 0,
8915 staged_blob_id ? staged_blob_id : blob_id,
8916 ondisk_path, dirfd, de_name, ie->path,
8917 a->repo, a->patch_cb, a->patch_arg);
8918 if (err || path_content == NULL)
8919 break;
8922 err = got_object_blob_create(&new_staged_blob_id,
8923 path_content ? path_content : ondisk_path, a->repo);
8924 if (err)
8925 break;
8926 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8927 SHA1_DIGEST_LENGTH);
8928 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8929 stage = GOT_FILEIDX_STAGE_ADD;
8930 else
8931 stage = GOT_FILEIDX_STAGE_MODIFY;
8932 got_fileindex_entry_stage_set(ie, stage);
8933 if (S_ISLNK(sb.st_mode)) {
8934 int is_bad_symlink = 0;
8935 if (!a->allow_bad_symlinks) {
8936 char target_path[PATH_MAX];
8937 ssize_t target_len;
8938 target_len = readlink(ondisk_path, target_path,
8939 sizeof(target_path));
8940 if (target_len == -1) {
8941 err = got_error_from_errno2("readlink",
8942 ondisk_path);
8943 break;
8945 err = is_bad_symlink_target(&is_bad_symlink,
8946 target_path, target_len, ondisk_path,
8947 a->worktree->root_path,
8948 a->worktree->meta_dir);
8949 if (err)
8950 break;
8951 if (is_bad_symlink) {
8952 err = got_error_path(ondisk_path,
8953 GOT_ERR_BAD_SYMLINK);
8954 break;
8957 if (is_bad_symlink)
8958 got_fileindex_entry_staged_filetype_set(ie,
8959 GOT_FILEIDX_MODE_BAD_SYMLINK);
8960 else
8961 got_fileindex_entry_staged_filetype_set(ie,
8962 GOT_FILEIDX_MODE_SYMLINK);
8963 } else {
8964 got_fileindex_entry_staged_filetype_set(ie,
8965 GOT_FILEIDX_MODE_REGULAR_FILE);
8967 a->staged_something = 1;
8968 if (a->status_cb == NULL)
8969 break;
8970 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8971 get_staged_status(ie), relpath, blob_id,
8972 new_staged_blob_id, NULL, dirfd, de_name);
8973 if (err)
8974 break;
8976 * When staging the reverse of the staged diff,
8977 * implicitly unstage the file.
8979 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8980 sizeof(ie->blob_sha1)) == 0) {
8981 got_fileindex_entry_stage_set(ie,
8982 GOT_FILEIDX_STAGE_NONE);
8984 break;
8985 case GOT_STATUS_DELETE:
8986 if (staged_status == GOT_STATUS_DELETE)
8987 break;
8988 if (a->patch_cb) {
8989 int choice = GOT_PATCH_CHOICE_NONE;
8990 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8991 ie->path, NULL, 1, 1);
8992 if (err)
8993 break;
8994 if (choice == GOT_PATCH_CHOICE_NO)
8995 break;
8996 if (choice != GOT_PATCH_CHOICE_YES) {
8997 err = got_error(GOT_ERR_PATCH_CHOICE);
8998 break;
9001 stage = GOT_FILEIDX_STAGE_DELETE;
9002 got_fileindex_entry_stage_set(ie, stage);
9003 a->staged_something = 1;
9004 if (a->status_cb == NULL)
9005 break;
9006 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9007 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9008 de_name);
9009 break;
9010 case GOT_STATUS_NO_CHANGE:
9011 break;
9012 case GOT_STATUS_CONFLICT:
9013 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9014 break;
9015 case GOT_STATUS_NONEXISTENT:
9016 err = got_error_set_errno(ENOENT, relpath);
9017 break;
9018 default:
9019 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9020 break;
9023 if (path_content && unlink(path_content) == -1 && err == NULL)
9024 err = got_error_from_errno2("unlink", path_content);
9025 free(path_content);
9026 free(ondisk_path);
9027 free(new_staged_blob_id);
9028 return err;
9031 const struct got_error *
9032 got_worktree_stage(struct got_worktree *worktree,
9033 struct got_pathlist_head *paths,
9034 got_worktree_status_cb status_cb, void *status_arg,
9035 got_worktree_patch_cb patch_cb, void *patch_arg,
9036 int allow_bad_symlinks, struct got_repository *repo)
9038 const struct got_error *err = NULL, *sync_err, *unlockerr;
9039 struct got_pathlist_entry *pe;
9040 struct got_fileindex *fileindex = NULL;
9041 char *fileindex_path = NULL;
9042 struct got_reference *head_ref = NULL;
9043 struct got_object_id *head_commit_id = NULL;
9044 struct check_stage_ok_arg oka;
9045 struct stage_path_arg spa;
9047 err = lock_worktree(worktree, LOCK_EX);
9048 if (err)
9049 return err;
9051 err = got_ref_open(&head_ref, repo,
9052 got_worktree_get_head_ref_name(worktree), 0);
9053 if (err)
9054 goto done;
9055 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9056 if (err)
9057 goto done;
9058 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9059 if (err)
9060 goto done;
9062 /* Check pre-conditions before staging anything. */
9063 oka.head_commit_id = head_commit_id;
9064 oka.worktree = worktree;
9065 oka.fileindex = fileindex;
9066 oka.repo = repo;
9067 oka.have_changes = 0;
9068 TAILQ_FOREACH(pe, paths, entry) {
9069 err = worktree_status(worktree, pe->path, fileindex, repo,
9070 check_stage_ok, &oka, NULL, NULL, 1, 0);
9071 if (err)
9072 goto done;
9074 if (!oka.have_changes) {
9075 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9076 goto done;
9079 spa.worktree = worktree;
9080 spa.fileindex = fileindex;
9081 spa.repo = repo;
9082 spa.patch_cb = patch_cb;
9083 spa.patch_arg = patch_arg;
9084 spa.status_cb = status_cb;
9085 spa.status_arg = status_arg;
9086 spa.staged_something = 0;
9087 spa.allow_bad_symlinks = allow_bad_symlinks;
9088 TAILQ_FOREACH(pe, paths, entry) {
9089 err = worktree_status(worktree, pe->path, fileindex, repo,
9090 stage_path, &spa, NULL, NULL, 1, 0);
9091 if (err)
9092 goto done;
9094 if (!spa.staged_something) {
9095 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9096 goto done;
9099 sync_err = sync_fileindex(fileindex, fileindex_path);
9100 if (sync_err && err == NULL)
9101 err = sync_err;
9102 done:
9103 if (head_ref)
9104 got_ref_close(head_ref);
9105 free(head_commit_id);
9106 free(fileindex_path);
9107 if (fileindex)
9108 got_fileindex_free(fileindex);
9109 unlockerr = lock_worktree(worktree, LOCK_SH);
9110 if (unlockerr && err == NULL)
9111 err = unlockerr;
9112 return err;
9115 struct unstage_path_arg {
9116 struct got_worktree *worktree;
9117 struct got_fileindex *fileindex;
9118 struct got_repository *repo;
9119 got_worktree_checkout_cb progress_cb;
9120 void *progress_arg;
9121 got_worktree_patch_cb patch_cb;
9122 void *patch_arg;
9125 static const struct got_error *
9126 create_unstaged_content(char **path_unstaged_content,
9127 char **path_new_staged_content, struct got_object_id *blob_id,
9128 struct got_object_id *staged_blob_id, const char *relpath,
9129 struct got_repository *repo,
9130 got_worktree_patch_cb patch_cb, void *patch_arg)
9132 const struct got_error *err, *free_err;
9133 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9134 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9135 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9136 struct got_diffreg_result *diffreg_result = NULL;
9137 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9138 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9139 int fd1 = -1, fd2 = -1;
9141 *path_unstaged_content = NULL;
9142 *path_new_staged_content = NULL;
9144 err = got_object_id_str(&label1, blob_id);
9145 if (err)
9146 return err;
9148 fd1 = got_opentempfd();
9149 if (fd1 == -1) {
9150 err = got_error_from_errno("got_opentempfd");
9151 goto done;
9153 fd2 = got_opentempfd();
9154 if (fd2 == -1) {
9155 err = got_error_from_errno("got_opentempfd");
9156 goto done;
9159 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9160 if (err)
9161 goto done;
9163 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9164 if (err)
9165 goto done;
9167 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9168 if (err)
9169 goto done;
9171 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9172 fd2);
9173 if (err)
9174 goto done;
9176 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9177 if (err)
9178 goto done;
9180 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9181 if (err)
9182 goto done;
9184 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9185 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9186 if (err)
9187 goto done;
9189 err = got_opentemp_named(path_unstaged_content, &outfile,
9190 "got-unstaged-content", "");
9191 if (err)
9192 goto done;
9193 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9194 "got-new-staged-content", "");
9195 if (err)
9196 goto done;
9198 if (fseek(f1, 0L, SEEK_SET) == -1) {
9199 err = got_ferror(f1, GOT_ERR_IO);
9200 goto done;
9202 if (fseek(f2, 0L, SEEK_SET) == -1) {
9203 err = got_ferror(f2, GOT_ERR_IO);
9204 goto done;
9206 /* Count the number of actual changes in the diff result. */
9207 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9208 struct diff_chunk_context cc = {};
9209 diff_chunk_context_load_change(&cc, &nchunks_used,
9210 diffreg_result->result, n, 0);
9211 nchanges++;
9213 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9214 int choice;
9215 err = apply_or_reject_change(&choice, &nchunks_used,
9216 diffreg_result->result, n, relpath, f1, f2,
9217 &line_cur1, &line_cur2,
9218 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9219 if (err)
9220 goto done;
9221 if (choice == GOT_PATCH_CHOICE_YES)
9222 have_content = 1;
9223 else
9224 have_rejected_content = 1;
9225 if (choice == GOT_PATCH_CHOICE_QUIT)
9226 break;
9228 if (have_content || have_rejected_content)
9229 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9230 outfile, rejectfile);
9231 done:
9232 free(label1);
9233 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9234 err = got_error_from_errno("close");
9235 if (blob)
9236 got_object_blob_close(blob);
9237 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9238 err = got_error_from_errno("close");
9239 if (staged_blob)
9240 got_object_blob_close(staged_blob);
9241 free_err = got_diffreg_result_free(diffreg_result);
9242 if (free_err && err == NULL)
9243 err = free_err;
9244 if (f1 && fclose(f1) == EOF && err == NULL)
9245 err = got_error_from_errno2("fclose", path1);
9246 if (f2 && fclose(f2) == EOF && err == NULL)
9247 err = got_error_from_errno2("fclose", path2);
9248 if (outfile && fclose(outfile) == EOF && err == NULL)
9249 err = got_error_from_errno2("fclose", *path_unstaged_content);
9250 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9251 err = got_error_from_errno2("fclose", *path_new_staged_content);
9252 if (path1 && unlink(path1) == -1 && err == NULL)
9253 err = got_error_from_errno2("unlink", path1);
9254 if (path2 && unlink(path2) == -1 && err == NULL)
9255 err = got_error_from_errno2("unlink", path2);
9256 if (err || !have_content) {
9257 if (*path_unstaged_content &&
9258 unlink(*path_unstaged_content) == -1 && err == NULL)
9259 err = got_error_from_errno2("unlink",
9260 *path_unstaged_content);
9261 free(*path_unstaged_content);
9262 *path_unstaged_content = NULL;
9264 if (err || !have_content || !have_rejected_content) {
9265 if (*path_new_staged_content &&
9266 unlink(*path_new_staged_content) == -1 && err == NULL)
9267 err = got_error_from_errno2("unlink",
9268 *path_new_staged_content);
9269 free(*path_new_staged_content);
9270 *path_new_staged_content = NULL;
9272 free(path1);
9273 free(path2);
9274 return err;
9277 static const struct got_error *
9278 unstage_hunks(struct got_object_id *staged_blob_id,
9279 struct got_blob_object *blob_base,
9280 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9281 const char *ondisk_path, const char *label_orig,
9282 struct got_worktree *worktree, struct got_repository *repo,
9283 got_worktree_patch_cb patch_cb, void *patch_arg,
9284 got_worktree_checkout_cb progress_cb, void *progress_arg)
9286 const struct got_error *err = NULL;
9287 char *path_unstaged_content = NULL;
9288 char *path_new_staged_content = NULL;
9289 char *parent = NULL, *base_path = NULL;
9290 char *blob_base_path = NULL;
9291 struct got_object_id *new_staged_blob_id = NULL;
9292 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9293 struct stat sb;
9295 err = create_unstaged_content(&path_unstaged_content,
9296 &path_new_staged_content, blob_id, staged_blob_id,
9297 ie->path, repo, patch_cb, patch_arg);
9298 if (err)
9299 return err;
9301 if (path_unstaged_content == NULL)
9302 return NULL;
9304 if (path_new_staged_content) {
9305 err = got_object_blob_create(&new_staged_blob_id,
9306 path_new_staged_content, repo);
9307 if (err)
9308 goto done;
9311 f = fopen(path_unstaged_content, "re");
9312 if (f == NULL) {
9313 err = got_error_from_errno2("fopen",
9314 path_unstaged_content);
9315 goto done;
9317 if (fstat(fileno(f), &sb) == -1) {
9318 err = got_error_from_errno2("fstat", path_unstaged_content);
9319 goto done;
9321 if (got_fileindex_entry_staged_filetype_get(ie) ==
9322 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9323 char link_target[PATH_MAX];
9324 size_t r;
9325 r = fread(link_target, 1, sizeof(link_target), f);
9326 if (r == 0 && ferror(f)) {
9327 err = got_error_from_errno("fread");
9328 goto done;
9330 if (r >= sizeof(link_target)) { /* should not happen */
9331 err = got_error(GOT_ERR_NO_SPACE);
9332 goto done;
9334 link_target[r] = '\0';
9335 err = merge_symlink(worktree, blob_base,
9336 ondisk_path, ie->path, label_orig, link_target,
9337 worktree->base_commit_id, repo, progress_cb,
9338 progress_arg);
9339 } else {
9340 int local_changes_subsumed;
9342 err = got_path_dirname(&parent, ondisk_path);
9343 if (err)
9344 return err;
9346 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9347 parent) == -1) {
9348 err = got_error_from_errno("asprintf");
9349 base_path = NULL;
9350 goto done;
9353 err = got_opentemp_named(&blob_base_path, &f_base,
9354 base_path, "");
9355 if (err)
9356 goto done;
9357 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9358 blob_base);
9359 if (err)
9360 goto done;
9363 * In order the run a 3-way merge with a symlink we copy the symlink's
9364 * target path into a temporary file and use that file with diff3.
9366 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9367 err = dump_symlink_target_path_to_file(&f_deriv2,
9368 ondisk_path);
9369 if (err)
9370 goto done;
9371 } else {
9372 int fd;
9373 fd = open(ondisk_path,
9374 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9375 if (fd == -1) {
9376 err = got_error_from_errno2("open", ondisk_path);
9377 goto done;
9379 f_deriv2 = fdopen(fd, "r");
9380 if (f_deriv2 == NULL) {
9381 err = got_error_from_errno2("fdopen", ondisk_path);
9382 close(fd);
9383 goto done;
9387 err = merge_file(&local_changes_subsumed, worktree,
9388 f_base, f, f_deriv2, ondisk_path, ie->path,
9389 got_fileindex_perms_to_st(ie),
9390 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9391 repo, progress_cb, progress_arg);
9393 if (err)
9394 goto done;
9396 if (new_staged_blob_id) {
9397 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9398 SHA1_DIGEST_LENGTH);
9399 } else {
9400 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9401 got_fileindex_entry_staged_filetype_set(ie, 0);
9403 done:
9404 free(new_staged_blob_id);
9405 if (path_unstaged_content &&
9406 unlink(path_unstaged_content) == -1 && err == NULL)
9407 err = got_error_from_errno2("unlink", path_unstaged_content);
9408 if (path_new_staged_content &&
9409 unlink(path_new_staged_content) == -1 && err == NULL)
9410 err = got_error_from_errno2("unlink", path_new_staged_content);
9411 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9412 err = got_error_from_errno2("unlink", blob_base_path);
9413 if (f_base && fclose(f_base) == EOF && err == NULL)
9414 err = got_error_from_errno2("fclose", path_unstaged_content);
9415 if (f && fclose(f) == EOF && err == NULL)
9416 err = got_error_from_errno2("fclose", path_unstaged_content);
9417 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9418 err = got_error_from_errno2("fclose", ondisk_path);
9419 free(path_unstaged_content);
9420 free(path_new_staged_content);
9421 free(blob_base_path);
9422 free(parent);
9423 free(base_path);
9424 return err;
9427 static const struct got_error *
9428 unstage_path(void *arg, unsigned char status,
9429 unsigned char staged_status, const char *relpath,
9430 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9431 struct got_object_id *commit_id, int dirfd, const char *de_name)
9433 const struct got_error *err = NULL;
9434 struct unstage_path_arg *a = arg;
9435 struct got_fileindex_entry *ie;
9436 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9437 char *ondisk_path = NULL;
9438 char *id_str = NULL, *label_orig = NULL;
9439 int local_changes_subsumed;
9440 struct stat sb;
9441 int fd1 = -1, fd2 = -1;
9443 if (staged_status != GOT_STATUS_ADD &&
9444 staged_status != GOT_STATUS_MODIFY &&
9445 staged_status != GOT_STATUS_DELETE)
9446 return NULL;
9448 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9449 if (ie == NULL)
9450 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9452 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9453 == -1)
9454 return got_error_from_errno("asprintf");
9456 err = got_object_id_str(&id_str,
9457 commit_id ? commit_id : a->worktree->base_commit_id);
9458 if (err)
9459 goto done;
9460 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9461 id_str) == -1) {
9462 err = got_error_from_errno("asprintf");
9463 goto done;
9466 fd1 = got_opentempfd();
9467 if (fd1 == -1) {
9468 err = got_error_from_errno("got_opentempfd");
9469 goto done;
9471 fd2 = got_opentempfd();
9472 if (fd2 == -1) {
9473 err = got_error_from_errno("got_opentempfd");
9474 goto done;
9477 switch (staged_status) {
9478 case GOT_STATUS_MODIFY:
9479 err = got_object_open_as_blob(&blob_base, a->repo,
9480 blob_id, 8192, fd1);
9481 if (err)
9482 break;
9483 /* fall through */
9484 case GOT_STATUS_ADD:
9485 if (a->patch_cb) {
9486 if (staged_status == GOT_STATUS_ADD) {
9487 int choice = GOT_PATCH_CHOICE_NONE;
9488 err = (*a->patch_cb)(&choice, a->patch_arg,
9489 staged_status, ie->path, NULL, 1, 1);
9490 if (err)
9491 break;
9492 if (choice != GOT_PATCH_CHOICE_YES)
9493 break;
9494 } else {
9495 err = unstage_hunks(staged_blob_id,
9496 blob_base, blob_id, ie, ondisk_path,
9497 label_orig, a->worktree, a->repo,
9498 a->patch_cb, a->patch_arg,
9499 a->progress_cb, a->progress_arg);
9500 break; /* Done with this file. */
9503 err = got_object_open_as_blob(&blob_staged, a->repo,
9504 staged_blob_id, 8192, fd2);
9505 if (err)
9506 break;
9507 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9508 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9509 case GOT_FILEIDX_MODE_REGULAR_FILE:
9510 err = merge_blob(&local_changes_subsumed, a->worktree,
9511 blob_base, ondisk_path, relpath,
9512 got_fileindex_perms_to_st(ie), label_orig,
9513 blob_staged, commit_id ? commit_id :
9514 a->worktree->base_commit_id, a->repo,
9515 a->progress_cb, a->progress_arg);
9516 break;
9517 case GOT_FILEIDX_MODE_SYMLINK:
9518 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9519 char *staged_target;
9520 err = got_object_blob_read_to_str(
9521 &staged_target, blob_staged);
9522 if (err)
9523 goto done;
9524 err = merge_symlink(a->worktree, blob_base,
9525 ondisk_path, relpath, label_orig,
9526 staged_target, commit_id ? commit_id :
9527 a->worktree->base_commit_id,
9528 a->repo, a->progress_cb, a->progress_arg);
9529 free(staged_target);
9530 } else {
9531 err = merge_blob(&local_changes_subsumed,
9532 a->worktree, blob_base, ondisk_path,
9533 relpath, got_fileindex_perms_to_st(ie),
9534 label_orig, blob_staged,
9535 commit_id ? commit_id :
9536 a->worktree->base_commit_id, a->repo,
9537 a->progress_cb, a->progress_arg);
9539 break;
9540 default:
9541 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9542 break;
9544 if (err == NULL) {
9545 got_fileindex_entry_stage_set(ie,
9546 GOT_FILEIDX_STAGE_NONE);
9547 got_fileindex_entry_staged_filetype_set(ie, 0);
9549 break;
9550 case GOT_STATUS_DELETE:
9551 if (a->patch_cb) {
9552 int choice = GOT_PATCH_CHOICE_NONE;
9553 err = (*a->patch_cb)(&choice, a->patch_arg,
9554 staged_status, ie->path, NULL, 1, 1);
9555 if (err)
9556 break;
9557 if (choice == GOT_PATCH_CHOICE_NO)
9558 break;
9559 if (choice != GOT_PATCH_CHOICE_YES) {
9560 err = got_error(GOT_ERR_PATCH_CHOICE);
9561 break;
9564 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9565 got_fileindex_entry_staged_filetype_set(ie, 0);
9566 err = get_file_status(&status, &sb, ie, ondisk_path,
9567 dirfd, de_name, a->repo);
9568 if (err)
9569 break;
9570 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9571 break;
9573 done:
9574 free(ondisk_path);
9575 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9576 err = got_error_from_errno("close");
9577 if (blob_base)
9578 got_object_blob_close(blob_base);
9579 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9580 err = got_error_from_errno("close");
9581 if (blob_staged)
9582 got_object_blob_close(blob_staged);
9583 free(id_str);
9584 free(label_orig);
9585 return err;
9588 const struct got_error *
9589 got_worktree_unstage(struct got_worktree *worktree,
9590 struct got_pathlist_head *paths,
9591 got_worktree_checkout_cb progress_cb, void *progress_arg,
9592 got_worktree_patch_cb patch_cb, void *patch_arg,
9593 struct got_repository *repo)
9595 const struct got_error *err = NULL, *sync_err, *unlockerr;
9596 struct got_pathlist_entry *pe;
9597 struct got_fileindex *fileindex = NULL;
9598 char *fileindex_path = NULL;
9599 struct unstage_path_arg upa;
9601 err = lock_worktree(worktree, LOCK_EX);
9602 if (err)
9603 return err;
9605 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9606 if (err)
9607 goto done;
9609 upa.worktree = worktree;
9610 upa.fileindex = fileindex;
9611 upa.repo = repo;
9612 upa.progress_cb = progress_cb;
9613 upa.progress_arg = progress_arg;
9614 upa.patch_cb = patch_cb;
9615 upa.patch_arg = patch_arg;
9616 TAILQ_FOREACH(pe, paths, entry) {
9617 err = worktree_status(worktree, pe->path, fileindex, repo,
9618 unstage_path, &upa, NULL, NULL, 1, 0);
9619 if (err)
9620 goto done;
9623 sync_err = sync_fileindex(fileindex, fileindex_path);
9624 if (sync_err && err == NULL)
9625 err = sync_err;
9626 done:
9627 free(fileindex_path);
9628 if (fileindex)
9629 got_fileindex_free(fileindex);
9630 unlockerr = lock_worktree(worktree, LOCK_SH);
9631 if (unlockerr && err == NULL)
9632 err = unlockerr;
9633 return err;
9636 struct report_file_info_arg {
9637 struct got_worktree *worktree;
9638 got_worktree_path_info_cb info_cb;
9639 void *info_arg;
9640 struct got_pathlist_head *paths;
9641 got_cancel_cb cancel_cb;
9642 void *cancel_arg;
9645 static const struct got_error *
9646 report_file_info(void *arg, struct got_fileindex_entry *ie)
9648 struct report_file_info_arg *a = arg;
9649 struct got_pathlist_entry *pe;
9650 struct got_object_id blob_id, staged_blob_id, commit_id;
9651 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9652 struct got_object_id *commit_idp = NULL;
9653 int stage;
9655 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9656 return got_error(GOT_ERR_CANCELLED);
9658 TAILQ_FOREACH(pe, a->paths, entry) {
9659 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9660 got_path_is_child(ie->path, pe->path, pe->path_len))
9661 break;
9663 if (pe == NULL) /* not found */
9664 return NULL;
9666 if (got_fileindex_entry_has_blob(ie))
9667 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9668 stage = got_fileindex_entry_stage_get(ie);
9669 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9670 stage == GOT_FILEIDX_STAGE_ADD) {
9671 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9672 &staged_blob_id, ie);
9675 if (got_fileindex_entry_has_commit(ie))
9676 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9678 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9679 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9682 const struct got_error *
9683 got_worktree_path_info(struct got_worktree *worktree,
9684 struct got_pathlist_head *paths,
9685 got_worktree_path_info_cb info_cb, void *info_arg,
9686 got_cancel_cb cancel_cb, void *cancel_arg)
9689 const struct got_error *err = NULL, *unlockerr;
9690 struct got_fileindex *fileindex = NULL;
9691 char *fileindex_path = NULL;
9692 struct report_file_info_arg arg;
9694 err = lock_worktree(worktree, LOCK_SH);
9695 if (err)
9696 return err;
9698 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9699 if (err)
9700 goto done;
9702 arg.worktree = worktree;
9703 arg.info_cb = info_cb;
9704 arg.info_arg = info_arg;
9705 arg.paths = paths;
9706 arg.cancel_cb = cancel_cb;
9707 arg.cancel_arg = cancel_arg;
9708 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9709 &arg);
9710 done:
9711 free(fileindex_path);
9712 if (fileindex)
9713 got_fileindex_free(fileindex);
9714 unlockerr = lock_worktree(worktree, LOCK_UN);
9715 if (unlockerr && err == NULL)
9716 err = unlockerr;
9717 return err;
9720 static const struct got_error *
9721 patch_check_path(const char *p, char **path, unsigned char *status,
9722 unsigned char *staged_status, struct got_fileindex *fileindex,
9723 struct got_worktree *worktree, struct got_repository *repo)
9725 const struct got_error *err;
9726 struct got_fileindex_entry *ie;
9727 struct stat sb;
9728 char *ondisk_path = NULL;
9730 err = got_worktree_resolve_path(path, worktree, p);
9731 if (err)
9732 return err;
9734 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9735 *path[0] ? "/" : "", *path) == -1)
9736 return got_error_from_errno("asprintf");
9738 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9739 if (ie) {
9740 *staged_status = get_staged_status(ie);
9741 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9742 repo);
9743 if (err)
9744 goto done;
9745 } else {
9746 *staged_status = GOT_STATUS_NO_CHANGE;
9747 *status = GOT_STATUS_UNVERSIONED;
9748 if (lstat(ondisk_path, &sb) == -1) {
9749 if (errno != ENOENT) {
9750 err = got_error_from_errno2("lstat",
9751 ondisk_path);
9752 goto done;
9754 *status = GOT_STATUS_NONEXISTENT;
9758 done:
9759 free(ondisk_path);
9760 return err;
9763 static const struct got_error *
9764 patch_can_rm(const char *path, unsigned char status,
9765 unsigned char staged_status)
9767 if (status == GOT_STATUS_NONEXISTENT)
9768 return got_error_set_errno(ENOENT, path);
9769 if (status != GOT_STATUS_NO_CHANGE &&
9770 status != GOT_STATUS_ADD &&
9771 status != GOT_STATUS_MODIFY &&
9772 status != GOT_STATUS_MODE_CHANGE)
9773 return got_error_path(path, GOT_ERR_FILE_STATUS);
9774 if (staged_status == GOT_STATUS_DELETE)
9775 return got_error_path(path, GOT_ERR_FILE_STATUS);
9776 return NULL;
9779 static const struct got_error *
9780 patch_can_add(const char *path, unsigned char status)
9782 if (status != GOT_STATUS_NONEXISTENT)
9783 return got_error_path(path, GOT_ERR_FILE_STATUS);
9784 return NULL;
9787 static const struct got_error *
9788 patch_can_edit(const char *path, unsigned char status,
9789 unsigned char staged_status)
9791 if (status == GOT_STATUS_NONEXISTENT)
9792 return got_error_set_errno(ENOENT, path);
9793 if (status != GOT_STATUS_NO_CHANGE &&
9794 status != GOT_STATUS_ADD &&
9795 status != GOT_STATUS_MODIFY)
9796 return got_error_path(path, GOT_ERR_FILE_STATUS);
9797 if (staged_status == GOT_STATUS_DELETE)
9798 return got_error_path(path, GOT_ERR_FILE_STATUS);
9799 return NULL;
9802 const struct got_error *
9803 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9804 char **fileindex_path, struct got_worktree *worktree)
9806 return open_fileindex(fileindex, fileindex_path, worktree);
9809 const struct got_error *
9810 got_worktree_patch_check_path(const char *old, const char *new,
9811 char **oldpath, char **newpath, struct got_worktree *worktree,
9812 struct got_repository *repo, struct got_fileindex *fileindex)
9814 const struct got_error *err = NULL;
9815 int file_renamed = 0;
9816 unsigned char status_old, staged_status_old;
9817 unsigned char status_new, staged_status_new;
9819 *oldpath = NULL;
9820 *newpath = NULL;
9822 err = patch_check_path(old != NULL ? old : new, oldpath,
9823 &status_old, &staged_status_old, fileindex, worktree, repo);
9824 if (err)
9825 goto done;
9827 err = patch_check_path(new != NULL ? new : old, newpath,
9828 &status_new, &staged_status_new, fileindex, worktree, repo);
9829 if (err)
9830 goto done;
9832 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9833 file_renamed = 1;
9835 if (old != NULL && new == NULL)
9836 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9837 else if (file_renamed) {
9838 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9839 if (err == NULL)
9840 err = patch_can_add(*newpath, status_new);
9841 } else if (old == NULL)
9842 err = patch_can_add(*newpath, status_new);
9843 else
9844 err = patch_can_edit(*newpath, status_new, staged_status_new);
9846 done:
9847 if (err) {
9848 free(*oldpath);
9849 *oldpath = NULL;
9850 free(*newpath);
9851 *newpath = NULL;
9853 return err;
9856 const struct got_error *
9857 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9858 struct got_worktree *worktree, struct got_fileindex *fileindex,
9859 got_worktree_checkout_cb progress_cb, void *progress_arg)
9861 struct schedule_addition_args saa;
9863 memset(&saa, 0, sizeof(saa));
9864 saa.worktree = worktree;
9865 saa.fileindex = fileindex;
9866 saa.progress_cb = progress_cb;
9867 saa.progress_arg = progress_arg;
9868 saa.repo = repo;
9870 return worktree_status(worktree, path, fileindex, repo,
9871 schedule_addition, &saa, NULL, NULL, 1, 0);
9874 const struct got_error *
9875 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9876 struct got_worktree *worktree, struct got_fileindex *fileindex,
9877 got_worktree_delete_cb progress_cb, void *progress_arg)
9879 const struct got_error *err;
9880 struct schedule_deletion_args sda;
9881 char *ondisk_status_path;
9883 memset(&sda, 0, sizeof(sda));
9884 sda.worktree = worktree;
9885 sda.fileindex = fileindex;
9886 sda.progress_cb = progress_cb;
9887 sda.progress_arg = progress_arg;
9888 sda.repo = repo;
9889 sda.delete_local_mods = 0;
9890 sda.keep_on_disk = 0;
9891 sda.ignore_missing_paths = 0;
9892 sda.status_codes = NULL;
9893 if (asprintf(&ondisk_status_path, "%s/%s",
9894 got_worktree_get_root_path(worktree), path) == -1)
9895 return got_error_from_errno("asprintf");
9896 sda.status_path = ondisk_status_path;
9897 sda.status_path_len = strlen(ondisk_status_path);
9899 err = worktree_status(worktree, path, fileindex, repo,
9900 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9901 free(ondisk_status_path);
9902 return err;
9905 const struct got_error *
9906 got_worktree_patch_complete(struct got_fileindex *fileindex,
9907 const char *fileindex_path)
9909 const struct got_error *err = NULL;
9911 err = sync_fileindex(fileindex, fileindex_path);
9912 got_fileindex_free(fileindex);
9914 return err;