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 const struct got_error *err = NULL;
2274 struct diff_cb_arg *a = arg;
2276 if (a->cancel_cb) {
2277 err = a->cancel_cb(a->cancel_arg);
2278 if (err)
2279 return err;
2282 return update_blob(a->worktree, a->fileindex, ie, te,
2283 ie->path, a->repo, a->progress_cb, a->progress_arg);
2286 static const struct got_error *
2287 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2289 const struct got_error *err = NULL;
2290 struct diff_cb_arg *a = arg;
2292 if (a->cancel_cb) {
2293 err = a->cancel_cb(a->cancel_arg);
2294 if (err)
2295 return err;
2298 return delete_blob(a->worktree, a->fileindex, ie,
2299 a->repo, a->progress_cb, a->progress_arg);
2302 static const struct got_error *
2303 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2305 const struct got_error *err = NULL;
2306 struct diff_cb_arg *a = arg;
2307 char *path;
2309 if (a->cancel_cb) {
2310 err = a->cancel_cb(a->cancel_arg);
2311 if (err)
2312 return err;
2315 if (got_object_tree_entry_is_submodule(te))
2316 return NULL;
2318 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2319 return NULL;
2321 if (asprintf(&path, "%s%s%s", parent_path,
2322 parent_path[0] ? "/" : "", te->name)
2323 == -1)
2324 return got_error_from_errno("asprintf");
2326 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2327 a->repo, a->progress_cb, a->progress_arg);
2329 free(path);
2330 return err;
2333 const struct got_error *
2334 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2336 uint32_t uuid_status;
2338 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2339 if (uuid_status != uuid_s_ok) {
2340 *uuidstr = NULL;
2341 return got_error_uuid(uuid_status, "uuid_to_string");
2344 return NULL;
2347 static const struct got_error *
2348 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2350 const struct got_error *err = NULL;
2351 char *uuidstr = NULL;
2353 *refname = NULL;
2355 err = got_worktree_get_uuid(&uuidstr, worktree);
2356 if (err)
2357 return err;
2359 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2360 err = got_error_from_errno("asprintf");
2361 *refname = NULL;
2363 free(uuidstr);
2364 return err;
2367 const struct got_error *
2368 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2369 const char *prefix)
2371 return get_ref_name(refname, worktree, prefix);
2374 static const struct got_error *
2375 get_base_ref_name(char **refname, struct got_worktree *worktree)
2377 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2380 static const struct got_error *
2381 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2383 return get_ref_name(refname, worktree,
2384 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2387 static const struct got_error *
2388 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2390 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2393 static const struct got_error *
2394 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2396 return get_ref_name(refname, worktree,
2397 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2400 static const struct got_error *
2401 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2403 return get_ref_name(refname, worktree,
2404 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2407 static const struct got_error *
2408 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2410 return get_ref_name(refname, worktree,
2411 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2414 static const struct got_error *
2415 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2417 return get_ref_name(refname, worktree,
2418 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2421 static const struct got_error *
2422 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2424 return get_ref_name(refname, worktree,
2425 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2428 static const struct got_error *
2429 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2431 return get_ref_name(refname, worktree,
2432 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2435 const struct got_error *
2436 got_worktree_get_histedit_script_path(char **path,
2437 struct got_worktree *worktree)
2439 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2440 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2441 *path = NULL;
2442 return got_error_from_errno("asprintf");
2444 return NULL;
2447 static const struct got_error *
2448 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2450 return get_ref_name(refname, worktree,
2451 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2454 static const struct got_error *
2455 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2457 return get_ref_name(refname, worktree,
2458 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2462 * Prevent Git's garbage collector from deleting our base commit by
2463 * setting a reference to our base commit's ID.
2465 static const struct got_error *
2466 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2468 const struct got_error *err = NULL;
2469 struct got_reference *ref = NULL;
2470 char *refname;
2472 err = get_base_ref_name(&refname, worktree);
2473 if (err)
2474 return err;
2476 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2477 if (err)
2478 goto done;
2480 err = got_ref_write(ref, repo);
2481 done:
2482 free(refname);
2483 if (ref)
2484 got_ref_close(ref);
2485 return err;
2488 static const struct got_error *
2489 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2491 const struct got_error *err = NULL;
2493 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2494 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2495 err = got_error_from_errno("asprintf");
2496 *fileindex_path = NULL;
2498 return err;
2502 static const struct got_error *
2503 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2504 struct got_worktree *worktree)
2506 const struct got_error *err = NULL;
2507 FILE *index = NULL;
2509 *fileindex_path = NULL;
2510 *fileindex = got_fileindex_alloc();
2511 if (*fileindex == NULL)
2512 return got_error_from_errno("got_fileindex_alloc");
2514 err = get_fileindex_path(fileindex_path, worktree);
2515 if (err)
2516 goto done;
2518 index = fopen(*fileindex_path, "rbe");
2519 if (index == NULL) {
2520 if (errno != ENOENT)
2521 err = got_error_from_errno2("fopen", *fileindex_path);
2522 } else {
2523 err = got_fileindex_read(*fileindex, index);
2524 if (fclose(index) == EOF && err == NULL)
2525 err = got_error_from_errno("fclose");
2527 done:
2528 if (err) {
2529 free(*fileindex_path);
2530 *fileindex_path = NULL;
2531 got_fileindex_free(*fileindex);
2532 *fileindex = NULL;
2534 return err;
2537 struct bump_base_commit_id_arg {
2538 struct got_object_id *base_commit_id;
2539 const char *path;
2540 size_t path_len;
2541 const char *entry_name;
2542 got_worktree_checkout_cb progress_cb;
2543 void *progress_arg;
2546 static const struct got_error *
2547 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2549 const struct got_error *err;
2550 struct bump_base_commit_id_arg *a = arg;
2552 if (a->entry_name) {
2553 if (strcmp(ie->path, a->path) != 0)
2554 return NULL;
2555 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2556 return NULL;
2558 if (got_fileindex_entry_was_skipped(ie))
2559 return NULL;
2561 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2562 SHA1_DIGEST_LENGTH) == 0)
2563 return NULL;
2565 if (a->progress_cb) {
2566 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2567 ie->path);
2568 if (err)
2569 return err;
2571 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2572 return NULL;
2575 /* Bump base commit ID of all files within an updated part of the work tree. */
2576 static const struct got_error *
2577 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2578 struct got_fileindex *fileindex,
2579 got_worktree_checkout_cb progress_cb, void *progress_arg)
2581 struct bump_base_commit_id_arg bbc_arg;
2583 bbc_arg.base_commit_id = worktree->base_commit_id;
2584 bbc_arg.entry_name = NULL;
2585 bbc_arg.path = "";
2586 bbc_arg.path_len = 0;
2587 bbc_arg.progress_cb = progress_cb;
2588 bbc_arg.progress_arg = progress_arg;
2590 return got_fileindex_for_each_entry_safe(fileindex,
2591 bump_base_commit_id, &bbc_arg);
2594 static const struct got_error *
2595 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2597 const struct got_error *err = NULL;
2598 char *new_fileindex_path = NULL;
2599 FILE *new_index = NULL;
2600 struct timespec timeout;
2602 err = got_opentemp_named(&new_fileindex_path, &new_index,
2603 fileindex_path, "");
2604 if (err)
2605 goto done;
2607 err = got_fileindex_write(fileindex, new_index);
2608 if (err)
2609 goto done;
2611 if (rename(new_fileindex_path, fileindex_path) != 0) {
2612 err = got_error_from_errno3("rename", new_fileindex_path,
2613 fileindex_path);
2614 unlink(new_fileindex_path);
2618 * Sleep for a short amount of time to ensure that files modified after
2619 * this program exits have a different time stamp from the one which
2620 * was recorded in the file index.
2622 timeout.tv_sec = 0;
2623 timeout.tv_nsec = 1;
2624 nanosleep(&timeout, NULL);
2625 done:
2626 if (new_index)
2627 fclose(new_index);
2628 free(new_fileindex_path);
2629 return err;
2632 static const struct got_error *
2633 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2634 struct got_object_id **tree_id, const char *wt_relpath,
2635 struct got_commit_object *base_commit, struct got_worktree *worktree,
2636 struct got_repository *repo)
2638 const struct got_error *err = NULL;
2639 struct got_object_id *id = NULL;
2640 char *in_repo_path = NULL;
2641 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2643 *entry_type = GOT_OBJ_TYPE_ANY;
2644 *tree_relpath = NULL;
2645 *tree_id = NULL;
2647 if (wt_relpath[0] == '\0') {
2648 /* Check out all files within the work tree. */
2649 *entry_type = GOT_OBJ_TYPE_TREE;
2650 *tree_relpath = strdup("");
2651 if (*tree_relpath == NULL) {
2652 err = got_error_from_errno("strdup");
2653 goto done;
2655 err = got_object_id_by_path(tree_id, repo, base_commit,
2656 worktree->path_prefix);
2657 if (err)
2658 goto done;
2659 return NULL;
2662 /* Check out a subset of files in the work tree. */
2664 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2665 is_root_wt ? "" : "/", wt_relpath) == -1) {
2666 err = got_error_from_errno("asprintf");
2667 goto done;
2670 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2671 if (err)
2672 goto done;
2674 free(in_repo_path);
2675 in_repo_path = NULL;
2677 err = got_object_get_type(entry_type, repo, id);
2678 if (err)
2679 goto done;
2681 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2682 /* Check out a single file. */
2683 if (strchr(wt_relpath, '/') == NULL) {
2684 /* Check out a single file in work tree's root dir. */
2685 in_repo_path = strdup(worktree->path_prefix);
2686 if (in_repo_path == NULL) {
2687 err = got_error_from_errno("strdup");
2688 goto done;
2690 *tree_relpath = strdup("");
2691 if (*tree_relpath == NULL) {
2692 err = got_error_from_errno("strdup");
2693 goto done;
2695 } else {
2696 /* Check out a single file in a subdirectory. */
2697 err = got_path_dirname(tree_relpath, wt_relpath);
2698 if (err)
2699 return err;
2700 if (asprintf(&in_repo_path, "%s%s%s",
2701 worktree->path_prefix, is_root_wt ? "" : "/",
2702 *tree_relpath) == -1) {
2703 err = got_error_from_errno("asprintf");
2704 goto done;
2707 err = got_object_id_by_path(tree_id, repo,
2708 base_commit, in_repo_path);
2709 } else {
2710 /* Check out all files within a subdirectory. */
2711 *tree_id = got_object_id_dup(id);
2712 if (*tree_id == NULL) {
2713 err = got_error_from_errno("got_object_id_dup");
2714 goto done;
2716 *tree_relpath = strdup(wt_relpath);
2717 if (*tree_relpath == NULL) {
2718 err = got_error_from_errno("strdup");
2719 goto done;
2722 done:
2723 free(id);
2724 free(in_repo_path);
2725 if (err) {
2726 *entry_type = GOT_OBJ_TYPE_ANY;
2727 free(*tree_relpath);
2728 *tree_relpath = NULL;
2729 free(*tree_id);
2730 *tree_id = NULL;
2732 return err;
2735 static const struct got_error *
2736 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2737 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2738 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2739 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2741 const struct got_error *err = NULL;
2742 struct got_commit_object *commit = NULL;
2743 struct got_tree_object *tree = NULL;
2744 struct got_fileindex_diff_tree_cb diff_cb;
2745 struct diff_cb_arg arg;
2747 err = ref_base_commit(worktree, repo);
2748 if (err) {
2749 if (!(err->code == GOT_ERR_ERRNO &&
2750 (errno == EACCES || errno == EROFS)))
2751 goto done;
2752 err = (*progress_cb)(progress_arg,
2753 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2754 if (err)
2755 return err;
2758 err = got_object_open_as_commit(&commit, repo,
2759 worktree->base_commit_id);
2760 if (err)
2761 goto done;
2763 err = got_object_open_as_tree(&tree, repo, tree_id);
2764 if (err)
2765 goto done;
2767 if (entry_name &&
2768 got_object_tree_find_entry(tree, entry_name) == NULL) {
2769 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2770 goto done;
2773 diff_cb.diff_old_new = diff_old_new;
2774 diff_cb.diff_old = diff_old;
2775 diff_cb.diff_new = diff_new;
2776 arg.fileindex = fileindex;
2777 arg.worktree = worktree;
2778 arg.repo = repo;
2779 arg.progress_cb = progress_cb;
2780 arg.progress_arg = progress_arg;
2781 arg.cancel_cb = cancel_cb;
2782 arg.cancel_arg = cancel_arg;
2783 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2784 entry_name, repo, &diff_cb, &arg);
2785 done:
2786 if (tree)
2787 got_object_tree_close(tree);
2788 if (commit)
2789 got_object_commit_close(commit);
2790 return err;
2793 const struct got_error *
2794 got_worktree_checkout_files(struct got_worktree *worktree,
2795 struct got_pathlist_head *paths, struct got_repository *repo,
2796 got_worktree_checkout_cb progress_cb, void *progress_arg,
2797 got_cancel_cb cancel_cb, void *cancel_arg)
2799 const struct got_error *err = NULL, *sync_err, *unlockerr;
2800 struct got_commit_object *commit = NULL;
2801 struct got_tree_object *tree = NULL;
2802 struct got_fileindex *fileindex = NULL;
2803 char *fileindex_path = NULL;
2804 struct got_pathlist_entry *pe;
2805 struct tree_path_data {
2806 STAILQ_ENTRY(tree_path_data) entry;
2807 struct got_object_id *tree_id;
2808 int entry_type;
2809 char *relpath;
2810 char *entry_name;
2811 } *tpd = NULL;
2812 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2814 STAILQ_INIT(&tree_paths);
2816 err = lock_worktree(worktree, LOCK_EX);
2817 if (err)
2818 return err;
2820 err = got_object_open_as_commit(&commit, repo,
2821 worktree->base_commit_id);
2822 if (err)
2823 goto done;
2825 /* Map all specified paths to in-repository trees. */
2826 TAILQ_FOREACH(pe, paths, entry) {
2827 tpd = malloc(sizeof(*tpd));
2828 if (tpd == NULL) {
2829 err = got_error_from_errno("malloc");
2830 goto done;
2833 err = find_tree_entry_for_checkout(&tpd->entry_type,
2834 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2835 worktree, repo);
2836 if (err) {
2837 free(tpd);
2838 goto done;
2841 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2842 err = got_path_basename(&tpd->entry_name, pe->path);
2843 if (err) {
2844 free(tpd->relpath);
2845 free(tpd->tree_id);
2846 free(tpd);
2847 goto done;
2849 } else
2850 tpd->entry_name = NULL;
2852 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2856 * Read the file index.
2857 * Checking out files is supposed to be an idempotent operation.
2858 * If the on-disk file index is incomplete we will try to complete it.
2860 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2861 if (err)
2862 goto done;
2864 tpd = STAILQ_FIRST(&tree_paths);
2865 TAILQ_FOREACH(pe, paths, entry) {
2866 struct bump_base_commit_id_arg bbc_arg;
2868 err = checkout_files(worktree, fileindex, tpd->relpath,
2869 tpd->tree_id, tpd->entry_name, repo,
2870 progress_cb, progress_arg, cancel_cb, cancel_arg);
2871 if (err)
2872 break;
2874 bbc_arg.base_commit_id = worktree->base_commit_id;
2875 bbc_arg.entry_name = tpd->entry_name;
2876 bbc_arg.path = pe->path;
2877 bbc_arg.path_len = pe->path_len;
2878 bbc_arg.progress_cb = progress_cb;
2879 bbc_arg.progress_arg = progress_arg;
2880 err = got_fileindex_for_each_entry_safe(fileindex,
2881 bump_base_commit_id, &bbc_arg);
2882 if (err)
2883 break;
2885 tpd = STAILQ_NEXT(tpd, entry);
2887 sync_err = sync_fileindex(fileindex, fileindex_path);
2888 if (sync_err && err == NULL)
2889 err = sync_err;
2890 done:
2891 free(fileindex_path);
2892 if (tree)
2893 got_object_tree_close(tree);
2894 if (commit)
2895 got_object_commit_close(commit);
2896 if (fileindex)
2897 got_fileindex_free(fileindex);
2898 while (!STAILQ_EMPTY(&tree_paths)) {
2899 tpd = STAILQ_FIRST(&tree_paths);
2900 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2901 free(tpd->relpath);
2902 free(tpd->tree_id);
2903 free(tpd);
2905 unlockerr = lock_worktree(worktree, LOCK_SH);
2906 if (unlockerr && err == NULL)
2907 err = unlockerr;
2908 return err;
2911 static const struct got_error *
2912 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2913 struct got_fileindex_entry *ie, const char *ondisk_path,
2914 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2915 int restoring_missing_file, int reverting_versioned_file,
2916 int path_is_unversioned, int allow_bad_symlinks,
2917 struct got_repository *repo,
2918 got_worktree_checkout_cb progress_cb, void *progress_arg)
2920 const struct got_error *err = NULL;
2921 int is_bad_symlink = 0;
2923 if (S_ISLNK(mode2)) {
2924 err = install_symlink(&is_bad_symlink,
2925 worktree, ondisk_path, path2, blob2,
2926 restoring_missing_file,
2927 reverting_versioned_file,
2928 path_is_unversioned, allow_bad_symlinks,
2929 repo, progress_cb, progress_arg);
2930 } else {
2931 err = install_blob(worktree, ondisk_path, path2,
2932 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2933 restoring_missing_file, reverting_versioned_file, 0,
2934 path_is_unversioned, repo, progress_cb, progress_arg);
2936 if (err)
2937 return err;
2938 if (ie == NULL) {
2939 /* Adding an unversioned file. */
2940 err = got_fileindex_entry_alloc(&ie, path2);
2941 if (err)
2942 return err;
2943 err = got_fileindex_entry_update(ie,
2944 worktree->root_fd, path2, NULL, NULL, 1);
2945 if (err) {
2946 got_fileindex_entry_free(ie);
2947 return err;
2949 err = got_fileindex_entry_add(fileindex, ie);
2950 if (err) {
2951 got_fileindex_entry_free(ie);
2952 return err;
2954 } else {
2955 /* Re-adding a locally deleted file. */
2956 err = got_fileindex_entry_update(ie,
2957 worktree->root_fd, path2, ie->blob_sha1,
2958 worktree->base_commit_id->sha1, 0);
2959 if (err)
2960 return err;
2963 if (is_bad_symlink) {
2964 got_fileindex_entry_filetype_set(ie,
2965 GOT_FILEIDX_MODE_BAD_SYMLINK);
2968 return NULL;
2971 struct merge_file_cb_arg {
2972 struct got_worktree *worktree;
2973 struct got_fileindex *fileindex;
2974 got_worktree_checkout_cb progress_cb;
2975 void *progress_arg;
2976 got_cancel_cb cancel_cb;
2977 void *cancel_arg;
2978 const char *label_orig;
2979 struct got_object_id *commit_id2;
2980 int allow_bad_symlinks;
2983 static const struct got_error *
2984 merge_file_cb(void *arg, struct got_blob_object *blob1,
2985 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2986 struct got_object_id *id1, struct got_object_id *id2,
2987 const char *path1, const char *path2,
2988 mode_t mode1, mode_t mode2, struct got_repository *repo)
2990 static const struct got_error *err = NULL;
2991 struct merge_file_cb_arg *a = arg;
2992 struct got_fileindex_entry *ie;
2993 char *ondisk_path = NULL;
2994 struct stat sb;
2995 unsigned char status;
2996 int local_changes_subsumed;
2997 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2998 char *id_str = NULL, *label_deriv2 = NULL;
2999 struct got_object_id *id = NULL;
3001 if (blob1 && blob2) {
3002 ie = got_fileindex_entry_get(a->fileindex, path2,
3003 strlen(path2));
3004 if (ie == NULL)
3005 return (*a->progress_cb)(a->progress_arg,
3006 GOT_STATUS_MISSING, path2);
3008 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3009 path2) == -1)
3010 return got_error_from_errno("asprintf");
3012 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3013 repo);
3014 if (err)
3015 goto done;
3017 if (status == GOT_STATUS_DELETE) {
3018 err = (*a->progress_cb)(a->progress_arg,
3019 GOT_STATUS_MERGE, path2);
3020 goto done;
3022 if (status != GOT_STATUS_NO_CHANGE &&
3023 status != GOT_STATUS_MODIFY &&
3024 status != GOT_STATUS_CONFLICT &&
3025 status != GOT_STATUS_ADD) {
3026 err = (*a->progress_cb)(a->progress_arg, status, path2);
3027 goto done;
3030 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3031 char *link_target2;
3032 err = got_object_blob_read_to_str(&link_target2, blob2);
3033 if (err)
3034 goto done;
3035 err = merge_symlink(a->worktree, blob1, ondisk_path,
3036 path2, a->label_orig, link_target2, a->commit_id2,
3037 repo, a->progress_cb, a->progress_arg);
3038 free(link_target2);
3039 } else {
3040 int fd;
3042 f_orig = got_opentemp();
3043 if (f_orig == NULL) {
3044 err = got_error_from_errno("got_opentemp");
3045 goto done;
3047 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3048 f_orig, blob1);
3049 if (err)
3050 goto done;
3052 f_deriv2 = got_opentemp();
3053 if (f_deriv2 == NULL)
3054 goto done;
3055 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3056 f_deriv2, blob2);
3057 if (err)
3058 goto done;
3060 fd = open(ondisk_path,
3061 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3062 if (fd == -1) {
3063 err = got_error_from_errno2("open",
3064 ondisk_path);
3065 goto done;
3067 f_deriv = fdopen(fd, "r");
3068 if (f_deriv == NULL) {
3069 err = got_error_from_errno2("fdopen",
3070 ondisk_path);
3071 close(fd);
3072 goto done;
3074 err = got_object_id_str(&id_str, a->commit_id2);
3075 if (err)
3076 goto done;
3077 if (asprintf(&label_deriv2, "%s: commit %s",
3078 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3079 err = got_error_from_errno("asprintf");
3080 goto done;
3082 err = merge_file(&local_changes_subsumed, a->worktree,
3083 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3084 mode2, a->label_orig, NULL, label_deriv2,
3085 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3086 a->progress_cb, a->progress_arg);
3088 } else if (blob1) {
3089 ie = got_fileindex_entry_get(a->fileindex, path1,
3090 strlen(path1));
3091 if (ie == NULL)
3092 return (*a->progress_cb)(a->progress_arg,
3093 GOT_STATUS_MISSING, path1);
3095 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3096 path1) == -1)
3097 return got_error_from_errno("asprintf");
3099 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3100 repo);
3101 if (err)
3102 goto done;
3104 switch (status) {
3105 case GOT_STATUS_NO_CHANGE:
3106 err = (*a->progress_cb)(a->progress_arg,
3107 GOT_STATUS_DELETE, path1);
3108 if (err)
3109 goto done;
3110 err = remove_ondisk_file(a->worktree->root_path, path1);
3111 if (err)
3112 goto done;
3113 if (ie)
3114 got_fileindex_entry_mark_deleted_from_disk(ie);
3115 break;
3116 case GOT_STATUS_DELETE:
3117 case GOT_STATUS_MISSING:
3118 err = (*a->progress_cb)(a->progress_arg,
3119 GOT_STATUS_DELETE, path1);
3120 if (err)
3121 goto done;
3122 if (ie)
3123 got_fileindex_entry_mark_deleted_from_disk(ie);
3124 break;
3125 case GOT_STATUS_MODIFY: {
3126 FILE *blob1_f;
3127 off_t blob1_size;
3128 int obj_type;
3130 * Delete the file only if its content already
3131 * exists in the repository.
3133 err = got_object_blob_file_create(&id, &blob1_f,
3134 &blob1_size, path1);
3135 if (err)
3136 goto done;
3137 if (fclose(blob1_f) == EOF) {
3138 err = got_error_from_errno("fclose");
3139 goto done;
3142 /* Implied existence check. */
3143 err = got_object_get_type(&obj_type, repo, id);
3144 if (err) {
3145 if (err->code != GOT_ERR_NO_OBJ)
3146 goto done;
3147 err = (*a->progress_cb)(a->progress_arg,
3148 GOT_STATUS_CANNOT_DELETE, path1);
3149 goto done;
3150 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3151 err = (*a->progress_cb)(a->progress_arg,
3152 GOT_STATUS_CANNOT_DELETE, path1);
3153 goto done;
3155 err = (*a->progress_cb)(a->progress_arg,
3156 GOT_STATUS_DELETE, path1);
3157 if (err)
3158 goto done;
3159 err = remove_ondisk_file(a->worktree->root_path,
3160 path1);
3161 if (err)
3162 goto done;
3163 if (ie)
3164 got_fileindex_entry_mark_deleted_from_disk(ie);
3165 break;
3167 case GOT_STATUS_ADD: {
3168 FILE *blob1_f;
3169 off_t blob1_size;
3171 * Delete the file only if its content already
3172 * exists in the repository.
3174 err = got_object_blob_file_create(&id, &blob1_f,
3175 &blob1_size, path1);
3176 if (err)
3177 goto done;
3178 if (fclose(blob1_f) == EOF) {
3179 err = got_error_from_errno("fclose");
3180 goto done;
3182 if (got_object_id_cmp(id, id1) == 0) {
3183 err = (*a->progress_cb)(a->progress_arg,
3184 GOT_STATUS_DELETE, path1);
3185 if (err)
3186 goto done;
3187 err = remove_ondisk_file(a->worktree->root_path,
3188 path1);
3189 if (err)
3190 goto done;
3191 if (ie)
3192 got_fileindex_entry_remove(a->fileindex,
3193 ie);
3194 } else {
3195 err = (*a->progress_cb)(a->progress_arg,
3196 GOT_STATUS_CANNOT_DELETE, path1);
3198 if (err)
3199 goto done;
3200 break;
3202 case GOT_STATUS_CONFLICT:
3203 err = (*a->progress_cb)(a->progress_arg,
3204 GOT_STATUS_CANNOT_DELETE, path1);
3205 if (err)
3206 goto done;
3207 break;
3208 case GOT_STATUS_OBSTRUCTED:
3209 err = (*a->progress_cb)(a->progress_arg, status, path1);
3210 if (err)
3211 goto done;
3212 break;
3213 default:
3214 break;
3216 } else if (blob2) {
3217 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3218 path2) == -1)
3219 return got_error_from_errno("asprintf");
3220 ie = got_fileindex_entry_get(a->fileindex, path2,
3221 strlen(path2));
3222 if (ie) {
3223 err = get_file_status(&status, &sb, ie, ondisk_path,
3224 -1, NULL, repo);
3225 if (err)
3226 goto done;
3227 if (status != GOT_STATUS_NO_CHANGE &&
3228 status != GOT_STATUS_MODIFY &&
3229 status != GOT_STATUS_CONFLICT &&
3230 status != GOT_STATUS_ADD &&
3231 status != GOT_STATUS_DELETE) {
3232 err = (*a->progress_cb)(a->progress_arg,
3233 status, path2);
3234 goto done;
3236 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3237 char *link_target2;
3238 err = got_object_blob_read_to_str(&link_target2,
3239 blob2);
3240 if (err)
3241 goto done;
3242 err = merge_symlink(a->worktree, NULL,
3243 ondisk_path, path2, a->label_orig,
3244 link_target2, a->commit_id2, repo,
3245 a->progress_cb, a->progress_arg);
3246 free(link_target2);
3247 } else if (S_ISREG(sb.st_mode)) {
3248 err = merge_blob(&local_changes_subsumed,
3249 a->worktree, NULL, ondisk_path, path2,
3250 sb.st_mode, a->label_orig, blob2,
3251 a->commit_id2, repo, a->progress_cb,
3252 a->progress_arg);
3253 } else if (status != GOT_STATUS_DELETE) {
3254 err = got_error_path(ondisk_path,
3255 GOT_ERR_FILE_OBSTRUCTED);
3257 if (err)
3258 goto done;
3259 if (status == GOT_STATUS_DELETE) {
3260 /* Re-add file with content from new blob. */
3261 err = add_file(a->worktree, a->fileindex, ie,
3262 ondisk_path, path2, blob2, mode2,
3263 0, 0, 0, a->allow_bad_symlinks,
3264 repo, a->progress_cb, a->progress_arg);
3265 if (err)
3266 goto done;
3268 } else {
3269 err = add_file(a->worktree, a->fileindex, NULL,
3270 ondisk_path, path2, blob2, mode2,
3271 0, 0, 1, a->allow_bad_symlinks,
3272 repo, a->progress_cb, a->progress_arg);
3273 if (err)
3274 goto done;
3277 done:
3278 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3279 err = got_error_from_errno("fclose");
3280 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3281 err = got_error_from_errno("fclose");
3282 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3283 err = got_error_from_errno("fclose");
3284 free(id_str);
3285 free(id);
3286 free(label_deriv2);
3287 free(ondisk_path);
3288 return err;
3291 struct check_mixed_commits_args {
3292 struct got_worktree *worktree;
3293 got_cancel_cb cancel_cb;
3294 void *cancel_arg;
3297 static const struct got_error *
3298 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3300 const struct got_error *err = NULL;
3301 struct check_mixed_commits_args *a = arg;
3303 if (a->cancel_cb) {
3304 err = a->cancel_cb(a->cancel_arg);
3305 if (err)
3306 return err;
3309 /* Reject merges into a work tree with mixed base commits. */
3310 if (got_fileindex_entry_has_commit(ie) &&
3311 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3312 SHA1_DIGEST_LENGTH) != 0)
3313 return got_error(GOT_ERR_MIXED_COMMITS);
3315 return NULL;
3318 const struct got_error *
3319 got_worktree_get_state(char *state, struct got_repository *repo,
3320 struct got_worktree *worktree,
3321 got_cancel_cb cancel_cb, void *cancel_arg)
3323 const struct got_error *err;
3324 struct got_object_id *base_id, *head_id = NULL;
3325 struct got_reference *head_ref;
3326 struct got_fileindex *fileindex = NULL;
3327 char *fileindex_path = NULL;
3328 struct check_mixed_commits_args cma;
3330 if (worktree == NULL)
3331 return got_error(GOT_ERR_NOT_WORKTREE);
3333 err = got_ref_open(&head_ref, repo,
3334 got_worktree_get_head_ref_name(worktree), 0);
3335 if (err)
3336 return err;
3338 err = got_ref_resolve(&head_id, repo, head_ref);
3339 if (err)
3340 goto done;
3342 *state = GOT_WORKTREE_STATE_UNKNOWN;
3343 base_id = got_worktree_get_base_commit_id(worktree);
3345 cma.worktree = worktree;
3346 cma.cancel_cb = cancel_cb;
3347 cma.cancel_arg = cancel_arg;
3349 if (got_object_id_cmp(base_id, head_id) == 0) {
3350 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3351 if (err)
3352 goto done;
3354 err = got_fileindex_for_each_entry_safe(fileindex,
3355 check_mixed_commits, &cma);
3356 if (err == NULL)
3357 *state = GOT_WORKTREE_STATE_UPTODATE;
3358 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3359 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3360 err = NULL;
3362 } else
3363 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3365 done:
3366 free(head_id);
3367 free(fileindex_path);
3368 got_ref_close(head_ref);
3369 if (fileindex != NULL)
3370 got_fileindex_free(fileindex);
3371 return err;
3374 struct check_merge_conflicts_arg {
3375 struct got_worktree *worktree;
3376 struct got_fileindex *fileindex;
3377 struct got_repository *repo;
3380 static const struct got_error *
3381 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3382 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3383 struct got_object_id *id1, struct got_object_id *id2,
3384 const char *path1, const char *path2,
3385 mode_t mode1, mode_t mode2, struct got_repository *repo)
3387 const struct got_error *err = NULL;
3388 struct check_merge_conflicts_arg *a = arg;
3389 unsigned char status;
3390 struct stat sb;
3391 struct got_fileindex_entry *ie;
3392 const char *path = path2 ? path2 : path1;
3393 struct got_object_id *id = id2 ? id2 : id1;
3394 char *ondisk_path;
3396 if (id == NULL)
3397 return NULL;
3399 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3400 if (ie == NULL)
3401 return NULL;
3403 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3404 == -1)
3405 return got_error_from_errno("asprintf");
3407 /* Reject merges into a work tree with conflicted files. */
3408 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3409 free(ondisk_path);
3410 if (err)
3411 return err;
3412 if (status == GOT_STATUS_CONFLICT)
3413 return got_error(GOT_ERR_CONFLICTS);
3415 return NULL;
3418 static const struct got_error *
3419 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3420 const char *fileindex_path, struct got_object_id *commit_id1,
3421 struct got_object_id *commit_id2, struct got_repository *repo,
3422 got_worktree_checkout_cb progress_cb, void *progress_arg,
3423 got_cancel_cb cancel_cb, void *cancel_arg)
3425 const struct got_error *err = NULL, *sync_err;
3426 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3427 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3428 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3429 struct check_merge_conflicts_arg cmc_arg;
3430 struct merge_file_cb_arg arg;
3431 char *label_orig = NULL;
3432 FILE *f1 = NULL, *f2 = NULL;
3433 int fd1 = -1, fd2 = -1;
3435 if (commit_id1) {
3436 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3437 if (err)
3438 goto done;
3439 err = got_object_id_by_path(&tree_id1, repo, commit1,
3440 worktree->path_prefix);
3441 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3442 goto done;
3444 if (tree_id1) {
3445 char *id_str;
3447 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3448 if (err)
3449 goto done;
3451 err = got_object_id_str(&id_str, commit_id1);
3452 if (err)
3453 goto done;
3455 if (asprintf(&label_orig, "%s: commit %s",
3456 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3457 err = got_error_from_errno("asprintf");
3458 free(id_str);
3459 goto done;
3461 free(id_str);
3463 f1 = got_opentemp();
3464 if (f1 == NULL) {
3465 err = got_error_from_errno("got_opentemp");
3466 goto done;
3470 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3471 if (err)
3472 goto done;
3474 err = got_object_id_by_path(&tree_id2, repo, commit2,
3475 worktree->path_prefix);
3476 if (err)
3477 goto done;
3479 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3480 if (err)
3481 goto done;
3483 f2 = got_opentemp();
3484 if (f2 == NULL) {
3485 err = got_error_from_errno("got_opentemp");
3486 goto done;
3489 fd1 = got_opentempfd();
3490 if (fd1 == -1) {
3491 err = got_error_from_errno("got_opentempfd");
3492 goto done;
3495 fd2 = got_opentempfd();
3496 if (fd2 == -1) {
3497 err = got_error_from_errno("got_opentempfd");
3498 goto done;
3501 cmc_arg.worktree = worktree;
3502 cmc_arg.fileindex = fileindex;
3503 cmc_arg.repo = repo;
3504 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3505 check_merge_conflicts, &cmc_arg, 0);
3506 if (err)
3507 goto done;
3509 arg.worktree = worktree;
3510 arg.fileindex = fileindex;
3511 arg.progress_cb = progress_cb;
3512 arg.progress_arg = progress_arg;
3513 arg.cancel_cb = cancel_cb;
3514 arg.cancel_arg = cancel_arg;
3515 arg.label_orig = label_orig;
3516 arg.commit_id2 = commit_id2;
3517 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3518 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3519 merge_file_cb, &arg, 1);
3520 sync_err = sync_fileindex(fileindex, fileindex_path);
3521 if (sync_err && err == NULL)
3522 err = sync_err;
3523 done:
3524 if (commit1)
3525 got_object_commit_close(commit1);
3526 if (commit2)
3527 got_object_commit_close(commit2);
3528 if (tree1)
3529 got_object_tree_close(tree1);
3530 if (tree2)
3531 got_object_tree_close(tree2);
3532 if (f1 && fclose(f1) == EOF && err == NULL)
3533 err = got_error_from_errno("fclose");
3534 if (f2 && fclose(f2) == EOF && err == NULL)
3535 err = got_error_from_errno("fclose");
3536 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3537 err = got_error_from_errno("close");
3538 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3539 err = got_error_from_errno("close");
3540 free(label_orig);
3541 return err;
3544 const struct got_error *
3545 got_worktree_merge_files(struct got_worktree *worktree,
3546 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3547 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3548 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3550 const struct got_error *err, *unlockerr;
3551 char *fileindex_path = NULL;
3552 struct got_fileindex *fileindex = NULL;
3553 struct check_mixed_commits_args cma;
3555 err = lock_worktree(worktree, LOCK_EX);
3556 if (err)
3557 return err;
3559 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3560 if (err)
3561 goto done;
3563 cma.worktree = worktree;
3564 cma.cancel_cb = cancel_cb;
3565 cma.cancel_arg = cancel_arg;
3567 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3568 &cma);
3569 if (err)
3570 goto done;
3572 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3573 commit_id2, repo, progress_cb, progress_arg,
3574 cancel_cb, cancel_arg);
3575 done:
3576 if (fileindex)
3577 got_fileindex_free(fileindex);
3578 free(fileindex_path);
3579 unlockerr = lock_worktree(worktree, LOCK_SH);
3580 if (unlockerr && err == NULL)
3581 err = unlockerr;
3582 return err;
3585 struct diff_dir_cb_arg {
3586 struct got_fileindex *fileindex;
3587 struct got_worktree *worktree;
3588 const char *status_path;
3589 size_t status_path_len;
3590 struct got_repository *repo;
3591 got_worktree_status_cb status_cb;
3592 void *status_arg;
3593 got_cancel_cb cancel_cb;
3594 void *cancel_arg;
3595 /* A pathlist containing per-directory pathlists of ignore patterns. */
3596 struct got_pathlist_head *ignores;
3597 int report_unchanged;
3598 int no_ignores;
3601 static const struct got_error *
3602 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3603 int dirfd, const char *de_name,
3604 got_worktree_status_cb status_cb, void *status_arg,
3605 struct got_repository *repo, int report_unchanged)
3607 const struct got_error *err = NULL;
3608 unsigned char status = GOT_STATUS_NO_CHANGE;
3609 unsigned char staged_status;
3610 struct stat sb;
3611 struct got_object_id blob_id, commit_id, staged_blob_id;
3612 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3613 struct got_object_id *staged_blob_idp = NULL;
3615 staged_status = get_staged_status(ie);
3616 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3617 if (err)
3618 return err;
3620 if (status == GOT_STATUS_NO_CHANGE &&
3621 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3622 return NULL;
3624 if (got_fileindex_entry_has_blob(ie))
3625 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3626 if (got_fileindex_entry_has_commit(ie))
3627 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3628 if (staged_status == GOT_STATUS_ADD ||
3629 staged_status == GOT_STATUS_MODIFY) {
3630 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3631 &staged_blob_id, ie);
3634 return (*status_cb)(status_arg, status, staged_status,
3635 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3638 static const struct got_error *
3639 status_old_new(void *arg, struct got_fileindex_entry *ie,
3640 struct dirent *de, const char *parent_path, int dirfd)
3642 const struct got_error *err = NULL;
3643 struct diff_dir_cb_arg *a = arg;
3644 char *abspath;
3646 if (a->cancel_cb) {
3647 err = a->cancel_cb(a->cancel_arg);
3648 if (err)
3649 return err;
3652 if (got_path_cmp(parent_path, a->status_path,
3653 strlen(parent_path), a->status_path_len) != 0 &&
3654 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3655 return NULL;
3657 if (parent_path[0]) {
3658 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3659 parent_path, de->d_name) == -1)
3660 return got_error_from_errno("asprintf");
3661 } else {
3662 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3663 de->d_name) == -1)
3664 return got_error_from_errno("asprintf");
3667 err = report_file_status(ie, abspath, dirfd, de->d_name,
3668 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3669 free(abspath);
3670 return err;
3673 static const struct got_error *
3674 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3676 const struct got_error *err;
3677 struct diff_dir_cb_arg *a = arg;
3678 struct got_object_id blob_id, commit_id;
3679 unsigned char status;
3681 if (a->cancel_cb) {
3682 err = a->cancel_cb(a->cancel_arg);
3683 if (err)
3684 return err;
3687 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3688 return NULL;
3690 got_fileindex_entry_get_blob_id(&blob_id, ie);
3691 got_fileindex_entry_get_commit_id(&commit_id, ie);
3692 if (got_fileindex_entry_has_file_on_disk(ie))
3693 status = GOT_STATUS_MISSING;
3694 else
3695 status = GOT_STATUS_DELETE;
3696 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3697 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3700 static void
3701 free_ignores(struct got_pathlist_head *ignores)
3703 struct got_pathlist_entry *pe;
3705 TAILQ_FOREACH(pe, ignores, entry) {
3706 struct got_pathlist_head *ignorelist = pe->data;
3708 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3710 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3713 static const struct got_error *
3714 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3716 const struct got_error *err = NULL;
3717 struct got_pathlist_entry *pe = NULL;
3718 struct got_pathlist_head *ignorelist;
3719 char *line = NULL, *pattern, *dirpath = NULL;
3720 size_t linesize = 0;
3721 ssize_t linelen;
3723 ignorelist = calloc(1, sizeof(*ignorelist));
3724 if (ignorelist == NULL)
3725 return got_error_from_errno("calloc");
3726 TAILQ_INIT(ignorelist);
3728 while ((linelen = getline(&line, &linesize, f)) != -1) {
3729 if (linelen > 0 && line[linelen - 1] == '\n')
3730 line[linelen - 1] = '\0';
3732 /* Git's ignores may contain comments. */
3733 if (line[0] == '#')
3734 continue;
3736 /* Git's negated patterns are not (yet?) supported. */
3737 if (line[0] == '!')
3738 continue;
3740 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3741 line) == -1) {
3742 err = got_error_from_errno("asprintf");
3743 goto done;
3745 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3746 if (err)
3747 goto done;
3749 if (ferror(f)) {
3750 err = got_error_from_errno("getline");
3751 goto done;
3754 dirpath = strdup(path);
3755 if (dirpath == NULL) {
3756 err = got_error_from_errno("strdup");
3757 goto done;
3759 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3760 done:
3761 free(line);
3762 if (err || pe == NULL) {
3763 free(dirpath);
3764 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3766 return err;
3769 static int
3770 match_path(const char *pattern, size_t pattern_len, const char *path,
3771 int flags)
3773 char buf[PATH_MAX];
3776 * Trailing slashes signify directories.
3777 * Append a * to make such patterns conform to fnmatch rules.
3779 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3780 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3781 return FNM_NOMATCH; /* XXX */
3783 return fnmatch(buf, path, flags);
3786 return fnmatch(pattern, path, flags);
3789 static int
3790 match_ignores(struct got_pathlist_head *ignores, const char *path)
3792 struct got_pathlist_entry *pe;
3794 /* Handle patterns which match in all directories. */
3795 TAILQ_FOREACH(pe, ignores, entry) {
3796 struct got_pathlist_head *ignorelist = pe->data;
3797 struct got_pathlist_entry *pi;
3799 TAILQ_FOREACH(pi, ignorelist, entry) {
3800 const char *p;
3802 if (pi->path_len < 3 ||
3803 strncmp(pi->path, "**/", 3) != 0)
3804 continue;
3805 p = path;
3806 while (*p) {
3807 if (match_path(pi->path + 3,
3808 pi->path_len - 3, p,
3809 FNM_PATHNAME | FNM_LEADING_DIR)) {
3810 /* Retry in next directory. */
3811 while (*p && *p != '/')
3812 p++;
3813 while (*p == '/')
3814 p++;
3815 continue;
3817 return 1;
3823 * The ignores pathlist contains ignore lists from children before
3824 * parents, so we can find the most specific ignorelist by walking
3825 * ignores backwards.
3827 pe = TAILQ_LAST(ignores, got_pathlist_head);
3828 while (pe) {
3829 if (got_path_is_child(path, pe->path, pe->path_len)) {
3830 struct got_pathlist_head *ignorelist = pe->data;
3831 struct got_pathlist_entry *pi;
3832 TAILQ_FOREACH(pi, ignorelist, entry) {
3833 int flags = FNM_LEADING_DIR;
3834 if (strstr(pi->path, "/**/") == NULL)
3835 flags |= FNM_PATHNAME;
3836 if (match_path(pi->path, pi->path_len,
3837 path, flags))
3838 continue;
3839 return 1;
3842 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3845 return 0;
3848 static const struct got_error *
3849 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3850 const char *path, int dirfd, const char *ignores_filename)
3852 const struct got_error *err = NULL;
3853 char *ignorespath;
3854 int fd = -1;
3855 FILE *ignoresfile = NULL;
3857 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3858 path[0] ? "/" : "", ignores_filename) == -1)
3859 return got_error_from_errno("asprintf");
3861 if (dirfd != -1) {
3862 fd = openat(dirfd, ignores_filename,
3863 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3864 if (fd == -1) {
3865 if (errno != ENOENT && errno != EACCES)
3866 err = got_error_from_errno2("openat",
3867 ignorespath);
3868 } else {
3869 ignoresfile = fdopen(fd, "r");
3870 if (ignoresfile == NULL)
3871 err = got_error_from_errno2("fdopen",
3872 ignorespath);
3873 else {
3874 fd = -1;
3875 err = read_ignores(ignores, path, ignoresfile);
3878 } else {
3879 ignoresfile = fopen(ignorespath, "re");
3880 if (ignoresfile == NULL) {
3881 if (errno != ENOENT && errno != EACCES)
3882 err = got_error_from_errno2("fopen",
3883 ignorespath);
3884 } else
3885 err = read_ignores(ignores, path, ignoresfile);
3888 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3889 err = got_error_from_errno2("fclose", path);
3890 if (fd != -1 && close(fd) == -1 && err == NULL)
3891 err = got_error_from_errno2("close", path);
3892 free(ignorespath);
3893 return err;
3896 static const struct got_error *
3897 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3898 int dirfd)
3900 const struct got_error *err = NULL;
3901 struct diff_dir_cb_arg *a = arg;
3902 char *path = NULL;
3904 if (ignore != NULL)
3905 *ignore = 0;
3907 if (a->cancel_cb) {
3908 err = a->cancel_cb(a->cancel_arg);
3909 if (err)
3910 return err;
3913 if (parent_path[0]) {
3914 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3915 return got_error_from_errno("asprintf");
3916 } else {
3917 path = de->d_name;
3920 if (de->d_type == DT_DIR) {
3921 if (!a->no_ignores && ignore != NULL &&
3922 match_ignores(a->ignores, path))
3923 *ignore = 1;
3924 } else if (!match_ignores(a->ignores, path) &&
3925 got_path_is_child(path, a->status_path, a->status_path_len))
3926 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3927 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3928 if (parent_path[0])
3929 free(path);
3930 return err;
3933 static const struct got_error *
3934 status_traverse(void *arg, const char *path, int dirfd)
3936 const struct got_error *err = NULL;
3937 struct diff_dir_cb_arg *a = arg;
3939 if (a->no_ignores)
3940 return NULL;
3942 err = add_ignores(a->ignores, a->worktree->root_path,
3943 path, dirfd, ".cvsignore");
3944 if (err)
3945 return err;
3947 err = add_ignores(a->ignores, a->worktree->root_path, path,
3948 dirfd, ".gitignore");
3950 return err;
3953 static const struct got_error *
3954 report_single_file_status(const char *path, const char *ondisk_path,
3955 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3956 void *status_arg, struct got_repository *repo, int report_unchanged,
3957 struct got_pathlist_head *ignores, int no_ignores)
3959 struct got_fileindex_entry *ie;
3960 struct stat sb;
3962 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3963 if (ie)
3964 return report_file_status(ie, ondisk_path, -1, NULL,
3965 status_cb, status_arg, repo, report_unchanged);
3967 if (lstat(ondisk_path, &sb) == -1) {
3968 if (errno != ENOENT)
3969 return got_error_from_errno2("lstat", ondisk_path);
3970 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3971 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3974 if (!no_ignores && match_ignores(ignores, path))
3975 return NULL;
3977 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3978 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3979 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3981 return NULL;
3984 static const struct got_error *
3985 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3986 const char *root_path, const char *path)
3988 const struct got_error *err;
3989 char *parent_path, *next_parent_path = NULL;
3991 err = add_ignores(ignores, root_path, "", -1,
3992 ".cvsignore");
3993 if (err)
3994 return err;
3996 err = add_ignores(ignores, root_path, "", -1,
3997 ".gitignore");
3998 if (err)
3999 return err;
4001 err = got_path_dirname(&parent_path, path);
4002 if (err) {
4003 if (err->code == GOT_ERR_BAD_PATH)
4004 return NULL; /* cannot traverse parent */
4005 return err;
4007 for (;;) {
4008 err = add_ignores(ignores, root_path, parent_path, -1,
4009 ".cvsignore");
4010 if (err)
4011 break;
4012 err = add_ignores(ignores, root_path, parent_path, -1,
4013 ".gitignore");
4014 if (err)
4015 break;
4016 err = got_path_dirname(&next_parent_path, parent_path);
4017 if (err) {
4018 if (err->code == GOT_ERR_BAD_PATH)
4019 err = NULL; /* traversed everything */
4020 break;
4022 if (got_path_is_root_dir(parent_path))
4023 break;
4024 free(parent_path);
4025 parent_path = next_parent_path;
4026 next_parent_path = NULL;
4029 free(parent_path);
4030 free(next_parent_path);
4031 return err;
4034 struct find_missing_children_args {
4035 const char *parent_path;
4036 size_t parent_len;
4037 struct got_pathlist_head *children;
4038 got_cancel_cb cancel_cb;
4039 void *cancel_arg;
4042 static const struct got_error *
4043 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4045 const struct got_error *err = NULL;
4046 struct find_missing_children_args *a = arg;
4048 if (a->cancel_cb) {
4049 err = a->cancel_cb(a->cancel_arg);
4050 if (err)
4051 return err;
4054 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4055 err = got_pathlist_append(a->children, ie->path, NULL);
4057 return err;
4060 static const struct got_error *
4061 report_children(struct got_pathlist_head *children,
4062 struct got_worktree *worktree, struct got_fileindex *fileindex,
4063 struct got_repository *repo, int is_root_dir, int report_unchanged,
4064 struct got_pathlist_head *ignores, int no_ignores,
4065 got_worktree_status_cb status_cb, void *status_arg,
4066 got_cancel_cb cancel_cb, void *cancel_arg)
4068 const struct got_error *err = NULL;
4069 struct got_pathlist_entry *pe;
4070 char *ondisk_path = NULL;
4072 TAILQ_FOREACH(pe, children, entry) {
4073 if (cancel_cb) {
4074 err = cancel_cb(cancel_arg);
4075 if (err)
4076 break;
4079 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4080 !is_root_dir ? "/" : "", pe->path) == -1) {
4081 err = got_error_from_errno("asprintf");
4082 ondisk_path = NULL;
4083 break;
4086 err = report_single_file_status(pe->path, ondisk_path,
4087 fileindex, status_cb, status_arg, repo, report_unchanged,
4088 ignores, no_ignores);
4089 if (err)
4090 break;
4092 free(ondisk_path);
4093 ondisk_path = NULL;
4096 free(ondisk_path);
4097 return err;
4100 static const struct got_error *
4101 worktree_status(struct got_worktree *worktree, const char *path,
4102 struct got_fileindex *fileindex, struct got_repository *repo,
4103 got_worktree_status_cb status_cb, void *status_arg,
4104 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4105 int report_unchanged)
4107 const struct got_error *err = NULL;
4108 int fd = -1;
4109 struct got_fileindex_diff_dir_cb fdiff_cb;
4110 struct diff_dir_cb_arg arg;
4111 char *ondisk_path = NULL;
4112 struct got_pathlist_head ignores, missing_children;
4113 struct got_fileindex_entry *ie;
4115 TAILQ_INIT(&ignores);
4116 TAILQ_INIT(&missing_children);
4118 if (asprintf(&ondisk_path, "%s%s%s",
4119 worktree->root_path, path[0] ? "/" : "", path) == -1)
4120 return got_error_from_errno("asprintf");
4122 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4123 if (ie) {
4124 err = report_single_file_status(path, ondisk_path,
4125 fileindex, status_cb, status_arg, repo,
4126 report_unchanged, &ignores, no_ignores);
4127 goto done;
4128 } else {
4129 struct find_missing_children_args fmca;
4130 fmca.parent_path = path;
4131 fmca.parent_len = strlen(path);
4132 fmca.children = &missing_children;
4133 fmca.cancel_cb = cancel_cb;
4134 fmca.cancel_arg = cancel_arg;
4135 err = got_fileindex_for_each_entry_safe(fileindex,
4136 find_missing_children, &fmca);
4137 if (err)
4138 goto done;
4141 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4142 if (fd == -1) {
4143 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4144 !got_err_open_nofollow_on_symlink())
4145 err = got_error_from_errno2("open", ondisk_path);
4146 else {
4147 if (!no_ignores) {
4148 err = add_ignores_from_parent_paths(&ignores,
4149 worktree->root_path, ondisk_path);
4150 if (err)
4151 goto done;
4153 if (TAILQ_EMPTY(&missing_children)) {
4154 err = report_single_file_status(path,
4155 ondisk_path, fileindex,
4156 status_cb, status_arg, repo,
4157 report_unchanged, &ignores, no_ignores);
4158 if (err)
4159 goto done;
4160 } else {
4161 err = report_children(&missing_children,
4162 worktree, fileindex, repo,
4163 (path[0] == '\0'), report_unchanged,
4164 &ignores, no_ignores,
4165 status_cb, status_arg,
4166 cancel_cb, cancel_arg);
4167 if (err)
4168 goto done;
4171 } else {
4172 fdiff_cb.diff_old_new = status_old_new;
4173 fdiff_cb.diff_old = status_old;
4174 fdiff_cb.diff_new = status_new;
4175 fdiff_cb.diff_traverse = status_traverse;
4176 arg.fileindex = fileindex;
4177 arg.worktree = worktree;
4178 arg.status_path = path;
4179 arg.status_path_len = strlen(path);
4180 arg.repo = repo;
4181 arg.status_cb = status_cb;
4182 arg.status_arg = status_arg;
4183 arg.cancel_cb = cancel_cb;
4184 arg.cancel_arg = cancel_arg;
4185 arg.report_unchanged = report_unchanged;
4186 arg.no_ignores = no_ignores;
4187 if (!no_ignores) {
4188 err = add_ignores_from_parent_paths(&ignores,
4189 worktree->root_path, path);
4190 if (err)
4191 goto done;
4193 arg.ignores = &ignores;
4194 err = got_fileindex_diff_dir(fileindex, fd,
4195 worktree->root_path, path, repo, &fdiff_cb, &arg);
4197 done:
4198 free_ignores(&ignores);
4199 if (fd != -1 && close(fd) == -1 && err == NULL)
4200 err = got_error_from_errno("close");
4201 free(ondisk_path);
4202 return err;
4205 const struct got_error *
4206 got_worktree_status(struct got_worktree *worktree,
4207 struct got_pathlist_head *paths, struct got_repository *repo,
4208 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4209 got_cancel_cb cancel_cb, void *cancel_arg)
4211 const struct got_error *err = NULL;
4212 char *fileindex_path = NULL;
4213 struct got_fileindex *fileindex = NULL;
4214 struct got_pathlist_entry *pe;
4216 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4217 if (err)
4218 return err;
4220 TAILQ_FOREACH(pe, paths, entry) {
4221 err = worktree_status(worktree, pe->path, fileindex, repo,
4222 status_cb, status_arg, cancel_cb, cancel_arg,
4223 no_ignores, 0);
4224 if (err)
4225 break;
4227 free(fileindex_path);
4228 got_fileindex_free(fileindex);
4229 return err;
4232 const struct got_error *
4233 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4234 const char *arg)
4236 const struct got_error *err = NULL;
4237 char *resolved = NULL, *cwd = NULL, *path = NULL;
4238 size_t len;
4239 struct stat sb;
4240 char *abspath = NULL;
4241 char canonpath[PATH_MAX];
4243 *wt_path = NULL;
4245 cwd = getcwd(NULL, 0);
4246 if (cwd == NULL)
4247 return got_error_from_errno("getcwd");
4249 if (lstat(arg, &sb) == -1) {
4250 if (errno != ENOENT) {
4251 err = got_error_from_errno2("lstat", arg);
4252 goto done;
4254 sb.st_mode = 0;
4256 if (S_ISLNK(sb.st_mode)) {
4258 * We cannot use realpath(3) with symlinks since we want to
4259 * operate on the symlink itself.
4260 * But we can make the path absolute, assuming it is relative
4261 * to the current working directory, and then canonicalize it.
4263 if (!got_path_is_absolute(arg)) {
4264 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4265 err = got_error_from_errno("asprintf");
4266 goto done;
4270 err = got_canonpath(abspath ? abspath : arg, canonpath,
4271 sizeof(canonpath));
4272 if (err)
4273 goto done;
4274 resolved = strdup(canonpath);
4275 if (resolved == NULL) {
4276 err = got_error_from_errno("strdup");
4277 goto done;
4279 } else {
4280 resolved = realpath(arg, NULL);
4281 if (resolved == NULL) {
4282 if (errno != ENOENT) {
4283 err = got_error_from_errno2("realpath", arg);
4284 goto done;
4286 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4287 err = got_error_from_errno("asprintf");
4288 goto done;
4290 err = got_canonpath(abspath, canonpath,
4291 sizeof(canonpath));
4292 if (err)
4293 goto done;
4294 resolved = strdup(canonpath);
4295 if (resolved == NULL) {
4296 err = got_error_from_errno("strdup");
4297 goto done;
4302 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4303 strlen(got_worktree_get_root_path(worktree)))) {
4304 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4305 goto done;
4308 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4309 err = got_path_skip_common_ancestor(&path,
4310 got_worktree_get_root_path(worktree), resolved);
4311 if (err)
4312 goto done;
4313 } else {
4314 path = strdup("");
4315 if (path == NULL) {
4316 err = got_error_from_errno("strdup");
4317 goto done;
4321 /* XXX status walk can't deal with trailing slash! */
4322 len = strlen(path);
4323 while (len > 0 && path[len - 1] == '/') {
4324 path[len - 1] = '\0';
4325 len--;
4327 done:
4328 free(abspath);
4329 free(resolved);
4330 free(cwd);
4331 if (err == NULL)
4332 *wt_path = path;
4333 else
4334 free(path);
4335 return err;
4338 struct schedule_addition_args {
4339 struct got_worktree *worktree;
4340 struct got_fileindex *fileindex;
4341 got_worktree_checkout_cb progress_cb;
4342 void *progress_arg;
4343 struct got_repository *repo;
4346 static int
4347 add_noop_status(unsigned char status)
4349 return (status == GOT_STATUS_ADD ||
4350 status == GOT_STATUS_MODIFY ||
4351 status == GOT_STATUS_CONFLICT ||
4352 status == GOT_STATUS_MODE_CHANGE ||
4353 status == GOT_STATUS_NO_CHANGE);
4356 static const struct got_error *
4357 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4358 const char *relpath, struct got_object_id *blob_id,
4359 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4360 int dirfd, const char *de_name)
4362 struct schedule_addition_args *a = arg;
4363 const struct got_error *err = NULL;
4364 struct got_fileindex_entry *ie;
4365 struct stat sb;
4366 char *ondisk_path;
4368 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4369 relpath) == -1)
4370 return got_error_from_errno("asprintf");
4372 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4373 if (ie) {
4374 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4375 de_name, a->repo);
4376 if (err)
4377 goto done;
4378 /* Re-adding an existing entry is a no-op. */
4379 if (staged_status == GOT_STATUS_NO_CHANGE &&
4380 add_noop_status(status))
4381 goto done;
4382 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4383 if (err)
4384 goto done;
4387 if (status != GOT_STATUS_UNVERSIONED) {
4388 if (status == GOT_STATUS_NONEXISTENT)
4389 err = got_error_set_errno(ENOENT, ondisk_path);
4390 else
4391 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4392 goto done;
4395 err = got_fileindex_entry_alloc(&ie, relpath);
4396 if (err)
4397 goto done;
4398 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4399 relpath, NULL, NULL, 1);
4400 if (err) {
4401 got_fileindex_entry_free(ie);
4402 goto done;
4404 err = got_fileindex_entry_add(a->fileindex, ie);
4405 if (err) {
4406 got_fileindex_entry_free(ie);
4407 goto done;
4409 done:
4410 free(ondisk_path);
4411 if (err)
4412 return err;
4413 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4414 return NULL;
4415 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4418 const struct got_error *
4419 got_worktree_schedule_add(struct got_worktree *worktree,
4420 struct got_pathlist_head *paths,
4421 got_worktree_checkout_cb progress_cb, void *progress_arg,
4422 struct got_repository *repo, int no_ignores)
4424 struct got_fileindex *fileindex = NULL;
4425 char *fileindex_path = NULL;
4426 const struct got_error *err = NULL, *sync_err, *unlockerr;
4427 struct got_pathlist_entry *pe;
4428 struct schedule_addition_args saa;
4430 err = lock_worktree(worktree, LOCK_EX);
4431 if (err)
4432 return err;
4434 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4435 if (err)
4436 goto done;
4438 saa.worktree = worktree;
4439 saa.fileindex = fileindex;
4440 saa.progress_cb = progress_cb;
4441 saa.progress_arg = progress_arg;
4442 saa.repo = repo;
4444 TAILQ_FOREACH(pe, paths, entry) {
4445 err = worktree_status(worktree, pe->path, fileindex, repo,
4446 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4447 if (err)
4448 break;
4450 sync_err = sync_fileindex(fileindex, fileindex_path);
4451 if (sync_err && err == NULL)
4452 err = sync_err;
4453 done:
4454 free(fileindex_path);
4455 if (fileindex)
4456 got_fileindex_free(fileindex);
4457 unlockerr = lock_worktree(worktree, LOCK_SH);
4458 if (unlockerr && err == NULL)
4459 err = unlockerr;
4460 return err;
4463 struct schedule_deletion_args {
4464 struct got_worktree *worktree;
4465 struct got_fileindex *fileindex;
4466 got_worktree_delete_cb progress_cb;
4467 void *progress_arg;
4468 struct got_repository *repo;
4469 int delete_local_mods;
4470 int keep_on_disk;
4471 int ignore_missing_paths;
4472 const char *status_path;
4473 size_t status_path_len;
4474 const char *status_codes;
4477 static const struct got_error *
4478 schedule_for_deletion(void *arg, unsigned char status,
4479 unsigned char staged_status, const char *relpath,
4480 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4481 struct got_object_id *commit_id, int dirfd, const char *de_name)
4483 struct schedule_deletion_args *a = arg;
4484 const struct got_error *err = NULL;
4485 struct got_fileindex_entry *ie = NULL;
4486 struct stat sb;
4487 char *ondisk_path;
4489 if (status == GOT_STATUS_NONEXISTENT) {
4490 if (a->ignore_missing_paths)
4491 return NULL;
4492 return got_error_set_errno(ENOENT, relpath);
4495 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4496 if (ie == NULL)
4497 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4499 staged_status = get_staged_status(ie);
4500 if (staged_status != GOT_STATUS_NO_CHANGE) {
4501 if (staged_status == GOT_STATUS_DELETE)
4502 return NULL;
4503 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4506 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4507 relpath) == -1)
4508 return got_error_from_errno("asprintf");
4510 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4511 a->repo);
4512 if (err)
4513 goto done;
4515 if (a->status_codes) {
4516 size_t ncodes = strlen(a->status_codes);
4517 int i;
4518 for (i = 0; i < ncodes ; i++) {
4519 if (status == a->status_codes[i])
4520 break;
4522 if (i == ncodes) {
4523 /* Do not delete files in non-matching status. */
4524 free(ondisk_path);
4525 return NULL;
4527 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4528 a->status_codes[i] != GOT_STATUS_MISSING) {
4529 static char msg[64];
4530 snprintf(msg, sizeof(msg),
4531 "invalid status code '%c'", a->status_codes[i]);
4532 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4533 goto done;
4537 if (status != GOT_STATUS_NO_CHANGE) {
4538 if (status == GOT_STATUS_DELETE)
4539 goto done;
4540 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4541 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4542 goto done;
4544 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4545 err = got_error_set_errno(ENOENT, relpath);
4546 goto done;
4548 if (status != GOT_STATUS_MODIFY &&
4549 status != GOT_STATUS_MISSING) {
4550 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4551 goto done;
4555 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4556 size_t root_len;
4558 if (dirfd != -1) {
4559 if (unlinkat(dirfd, de_name, 0) == -1) {
4560 err = got_error_from_errno2("unlinkat",
4561 ondisk_path);
4562 goto done;
4564 } else if (unlink(ondisk_path) == -1) {
4565 err = got_error_from_errno2("unlink", ondisk_path);
4566 goto done;
4569 root_len = strlen(a->worktree->root_path);
4570 do {
4571 char *parent;
4573 err = got_path_dirname(&parent, ondisk_path);
4574 if (err)
4575 goto done;
4576 free(ondisk_path);
4577 ondisk_path = parent;
4578 if (got_path_cmp(ondisk_path, a->status_path,
4579 strlen(ondisk_path), a->status_path_len) != 0 &&
4580 !got_path_is_child(ondisk_path, a->status_path,
4581 a->status_path_len))
4582 break;
4583 if (rmdir(ondisk_path) == -1) {
4584 if (errno != ENOTEMPTY)
4585 err = got_error_from_errno2("rmdir",
4586 ondisk_path);
4587 break;
4589 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4590 strlen(ondisk_path), root_len) != 0);
4593 got_fileindex_entry_mark_deleted_from_disk(ie);
4594 done:
4595 free(ondisk_path);
4596 if (err)
4597 return err;
4598 if (status == GOT_STATUS_DELETE)
4599 return NULL;
4600 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4601 staged_status, relpath);
4604 const struct got_error *
4605 got_worktree_schedule_delete(struct got_worktree *worktree,
4606 struct got_pathlist_head *paths, int delete_local_mods,
4607 const char *status_codes,
4608 got_worktree_delete_cb progress_cb, void *progress_arg,
4609 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4611 struct got_fileindex *fileindex = NULL;
4612 char *fileindex_path = NULL;
4613 const struct got_error *err = NULL, *sync_err, *unlockerr;
4614 struct got_pathlist_entry *pe;
4615 struct schedule_deletion_args sda;
4617 err = lock_worktree(worktree, LOCK_EX);
4618 if (err)
4619 return err;
4621 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4622 if (err)
4623 goto done;
4625 sda.worktree = worktree;
4626 sda.fileindex = fileindex;
4627 sda.progress_cb = progress_cb;
4628 sda.progress_arg = progress_arg;
4629 sda.repo = repo;
4630 sda.delete_local_mods = delete_local_mods;
4631 sda.keep_on_disk = keep_on_disk;
4632 sda.ignore_missing_paths = ignore_missing_paths;
4633 sda.status_codes = status_codes;
4635 TAILQ_FOREACH(pe, paths, entry) {
4636 char *ondisk_status_path;
4638 if (asprintf(&ondisk_status_path, "%s%s%s",
4639 got_worktree_get_root_path(worktree),
4640 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4641 err = got_error_from_errno("asprintf");
4642 goto done;
4644 sda.status_path = ondisk_status_path;
4645 sda.status_path_len = strlen(ondisk_status_path);
4646 err = worktree_status(worktree, pe->path, fileindex, repo,
4647 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4648 free(ondisk_status_path);
4649 if (err)
4650 break;
4652 sync_err = sync_fileindex(fileindex, fileindex_path);
4653 if (sync_err && err == NULL)
4654 err = sync_err;
4655 done:
4656 free(fileindex_path);
4657 if (fileindex)
4658 got_fileindex_free(fileindex);
4659 unlockerr = lock_worktree(worktree, LOCK_SH);
4660 if (unlockerr && err == NULL)
4661 err = unlockerr;
4662 return err;
4665 static const struct got_error *
4666 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4668 const struct got_error *err = NULL;
4669 char *line = NULL;
4670 size_t linesize = 0, n;
4671 ssize_t linelen;
4673 linelen = getline(&line, &linesize, infile);
4674 if (linelen == -1) {
4675 if (ferror(infile)) {
4676 err = got_error_from_errno("getline");
4677 goto done;
4679 return NULL;
4681 if (outfile) {
4682 n = fwrite(line, 1, linelen, outfile);
4683 if (n != linelen) {
4684 err = got_ferror(outfile, GOT_ERR_IO);
4685 goto done;
4688 if (rejectfile) {
4689 n = fwrite(line, 1, linelen, rejectfile);
4690 if (n != linelen)
4691 err = got_ferror(rejectfile, GOT_ERR_IO);
4693 done:
4694 free(line);
4695 return err;
4698 static const struct got_error *
4699 skip_one_line(FILE *f)
4701 char *line = NULL;
4702 size_t linesize = 0;
4703 ssize_t linelen;
4705 linelen = getline(&line, &linesize, f);
4706 if (linelen == -1) {
4707 if (ferror(f))
4708 return got_error_from_errno("getline");
4709 return NULL;
4711 free(line);
4712 return NULL;
4715 static const struct got_error *
4716 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4717 int start_old, int end_old, int start_new, int end_new,
4718 FILE *outfile, FILE *rejectfile)
4720 const struct got_error *err;
4722 /* Copy old file's lines leading up to patch. */
4723 while (!feof(f1) && *line_cur1 < start_old) {
4724 err = copy_one_line(f1, outfile, NULL);
4725 if (err)
4726 return err;
4727 (*line_cur1)++;
4729 /* Skip new file's lines leading up to patch. */
4730 while (!feof(f2) && *line_cur2 < start_new) {
4731 if (rejectfile)
4732 err = copy_one_line(f2, NULL, rejectfile);
4733 else
4734 err = skip_one_line(f2);
4735 if (err)
4736 return err;
4737 (*line_cur2)++;
4739 /* Copy patched lines. */
4740 while (!feof(f2) && *line_cur2 <= end_new) {
4741 err = copy_one_line(f2, outfile, NULL);
4742 if (err)
4743 return err;
4744 (*line_cur2)++;
4746 /* Skip over old file's replaced lines. */
4747 while (!feof(f1) && *line_cur1 <= end_old) {
4748 if (rejectfile)
4749 err = copy_one_line(f1, NULL, rejectfile);
4750 else
4751 err = skip_one_line(f1);
4752 if (err)
4753 return err;
4754 (*line_cur1)++;
4757 return NULL;
4760 static const struct got_error *
4761 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4762 FILE *outfile, FILE *rejectfile)
4764 const struct got_error *err;
4766 if (outfile) {
4767 /* Copy old file's lines until EOF. */
4768 while (!feof(f1)) {
4769 err = copy_one_line(f1, outfile, NULL);
4770 if (err)
4771 return err;
4772 (*line_cur1)++;
4775 if (rejectfile) {
4776 /* Copy new file's lines until EOF. */
4777 while (!feof(f2)) {
4778 err = copy_one_line(f2, NULL, rejectfile);
4779 if (err)
4780 return err;
4781 (*line_cur2)++;
4785 return NULL;
4788 static const struct got_error *
4789 apply_or_reject_change(int *choice, int *nchunks_used,
4790 struct diff_result *diff_result, int n,
4791 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4792 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4793 got_worktree_patch_cb patch_cb, void *patch_arg)
4795 const struct got_error *err = NULL;
4796 struct diff_chunk_context cc = {};
4797 int start_old, end_old, start_new, end_new;
4798 FILE *hunkfile;
4799 struct diff_output_unidiff_state *diff_state;
4800 struct diff_input_info diff_info;
4801 int rc;
4803 *choice = GOT_PATCH_CHOICE_NONE;
4805 /* Get changed line numbers without context lines for copy_change(). */
4806 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4807 start_old = cc.left.start;
4808 end_old = cc.left.end;
4809 start_new = cc.right.start;
4810 end_new = cc.right.end;
4812 /* Get the same change with context lines for display. */
4813 memset(&cc, 0, sizeof(cc));
4814 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4816 memset(&diff_info, 0, sizeof(diff_info));
4817 diff_info.left_path = relpath;
4818 diff_info.right_path = relpath;
4820 diff_state = diff_output_unidiff_state_alloc();
4821 if (diff_state == NULL)
4822 return got_error_set_errno(ENOMEM,
4823 "diff_output_unidiff_state_alloc");
4825 hunkfile = got_opentemp();
4826 if (hunkfile == NULL) {
4827 err = got_error_from_errno("got_opentemp");
4828 goto done;
4831 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4832 diff_result, &cc);
4833 if (rc != DIFF_RC_OK) {
4834 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4835 goto done;
4838 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4839 err = got_ferror(hunkfile, GOT_ERR_IO);
4840 goto done;
4843 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4844 hunkfile, changeno, nchanges);
4845 if (err)
4846 goto done;
4848 switch (*choice) {
4849 case GOT_PATCH_CHOICE_YES:
4850 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4851 end_old, start_new, end_new, outfile, rejectfile);
4852 break;
4853 case GOT_PATCH_CHOICE_NO:
4854 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4855 end_old, start_new, end_new, rejectfile, outfile);
4856 break;
4857 case GOT_PATCH_CHOICE_QUIT:
4858 break;
4859 default:
4860 err = got_error(GOT_ERR_PATCH_CHOICE);
4861 break;
4863 done:
4864 diff_output_unidiff_state_free(diff_state);
4865 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4866 err = got_error_from_errno("fclose");
4867 return err;
4870 struct revert_file_args {
4871 struct got_worktree *worktree;
4872 struct got_fileindex *fileindex;
4873 got_worktree_checkout_cb progress_cb;
4874 void *progress_arg;
4875 got_worktree_patch_cb patch_cb;
4876 void *patch_arg;
4877 struct got_repository *repo;
4878 int unlink_added_files;
4879 struct got_pathlist_head *added_files_to_unlink;
4882 static const struct got_error *
4883 create_patched_content(char **path_outfile, int reverse_patch,
4884 struct got_object_id *blob_id, const char *path2,
4885 int dirfd2, const char *de_name2,
4886 const char *relpath, struct got_repository *repo,
4887 got_worktree_patch_cb patch_cb, void *patch_arg)
4889 const struct got_error *err, *free_err;
4890 struct got_blob_object *blob = NULL;
4891 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4892 int fd = -1, fd2 = -1;
4893 char link_target[PATH_MAX];
4894 ssize_t link_len = 0;
4895 char *path1 = NULL, *id_str = NULL;
4896 struct stat sb2;
4897 struct got_diffreg_result *diffreg_result = NULL;
4898 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4899 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4901 *path_outfile = NULL;
4903 err = got_object_id_str(&id_str, blob_id);
4904 if (err)
4905 return err;
4907 if (dirfd2 != -1) {
4908 fd2 = openat(dirfd2, de_name2,
4909 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4910 if (fd2 == -1) {
4911 if (!got_err_open_nofollow_on_symlink()) {
4912 err = got_error_from_errno2("openat", path2);
4913 goto done;
4915 link_len = readlinkat(dirfd2, de_name2,
4916 link_target, sizeof(link_target));
4917 if (link_len == -1) {
4918 return got_error_from_errno2("readlinkat",
4919 path2);
4921 sb2.st_mode = S_IFLNK;
4922 sb2.st_size = link_len;
4924 } else {
4925 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 if (fd2 == -1) {
4927 if (!got_err_open_nofollow_on_symlink()) {
4928 err = got_error_from_errno2("open", path2);
4929 goto done;
4931 link_len = readlink(path2, link_target,
4932 sizeof(link_target));
4933 if (link_len == -1)
4934 return got_error_from_errno2("readlink", path2);
4935 sb2.st_mode = S_IFLNK;
4936 sb2.st_size = link_len;
4939 if (fd2 != -1) {
4940 if (fstat(fd2, &sb2) == -1) {
4941 err = got_error_from_errno2("fstat", path2);
4942 goto done;
4945 f2 = fdopen(fd2, "r");
4946 if (f2 == NULL) {
4947 err = got_error_from_errno2("fdopen", path2);
4948 goto done;
4950 fd2 = -1;
4951 } else {
4952 size_t n;
4953 f2 = got_opentemp();
4954 if (f2 == NULL) {
4955 err = got_error_from_errno2("got_opentemp", path2);
4956 goto done;
4958 n = fwrite(link_target, 1, link_len, f2);
4959 if (n != link_len) {
4960 err = got_ferror(f2, GOT_ERR_IO);
4961 goto done;
4963 if (fflush(f2) == EOF) {
4964 err = got_error_from_errno("fflush");
4965 goto done;
4967 rewind(f2);
4970 fd = got_opentempfd();
4971 if (fd == -1) {
4972 err = got_error_from_errno("got_opentempfd");
4973 goto done;
4976 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4977 if (err)
4978 goto done;
4980 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4981 if (err)
4982 goto done;
4984 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4985 if (err)
4986 goto done;
4988 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4989 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4990 if (err)
4991 goto done;
4993 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4994 "");
4995 if (err)
4996 goto done;
4998 if (fseek(f1, 0L, SEEK_SET) == -1)
4999 return got_ferror(f1, GOT_ERR_IO);
5000 if (fseek(f2, 0L, SEEK_SET) == -1)
5001 return got_ferror(f2, GOT_ERR_IO);
5003 /* Count the number of actual changes in the diff result. */
5004 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5005 struct diff_chunk_context cc = {};
5006 diff_chunk_context_load_change(&cc, &nchunks_used,
5007 diffreg_result->result, n, 0);
5008 nchanges++;
5010 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5011 int choice;
5012 err = apply_or_reject_change(&choice, &nchunks_used,
5013 diffreg_result->result, n, relpath, f1, f2,
5014 &line_cur1, &line_cur2,
5015 reverse_patch ? NULL : outfile,
5016 reverse_patch ? outfile : NULL,
5017 ++i, nchanges, patch_cb, patch_arg);
5018 if (err)
5019 goto done;
5020 if (choice == GOT_PATCH_CHOICE_YES)
5021 have_content = 1;
5022 else if (choice == GOT_PATCH_CHOICE_QUIT)
5023 break;
5025 if (have_content) {
5026 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5027 reverse_patch ? NULL : outfile,
5028 reverse_patch ? outfile : NULL);
5029 if (err)
5030 goto done;
5032 if (!S_ISLNK(sb2.st_mode)) {
5033 mode_t mode;
5035 mode = apply_umask(sb2.st_mode);
5036 if (fchmod(fileno(outfile), mode) == -1) {
5037 err = got_error_from_errno2("fchmod", path2);
5038 goto done;
5042 done:
5043 free(id_str);
5044 if (fd != -1 && close(fd) == -1 && err == NULL)
5045 err = got_error_from_errno("close");
5046 if (blob)
5047 got_object_blob_close(blob);
5048 free_err = got_diffreg_result_free(diffreg_result);
5049 if (err == NULL)
5050 err = free_err;
5051 if (f1 && fclose(f1) == EOF && err == NULL)
5052 err = got_error_from_errno2("fclose", path1);
5053 if (f2 && fclose(f2) == EOF && err == NULL)
5054 err = got_error_from_errno2("fclose", path2);
5055 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5056 err = got_error_from_errno2("close", path2);
5057 if (outfile && fclose(outfile) == EOF && err == NULL)
5058 err = got_error_from_errno2("fclose", *path_outfile);
5059 if (path1 && unlink(path1) == -1 && err == NULL)
5060 err = got_error_from_errno2("unlink", path1);
5061 if (err || !have_content) {
5062 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5063 err = got_error_from_errno2("unlink", *path_outfile);
5064 free(*path_outfile);
5065 *path_outfile = NULL;
5067 free(path1);
5068 return err;
5071 static const struct got_error *
5072 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5073 const char *relpath, struct got_object_id *blob_id,
5074 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5075 int dirfd, const char *de_name)
5077 struct revert_file_args *a = arg;
5078 const struct got_error *err = NULL;
5079 char *parent_path = NULL;
5080 struct got_fileindex_entry *ie;
5081 struct got_commit_object *base_commit = NULL;
5082 struct got_tree_object *tree = NULL;
5083 struct got_object_id *tree_id = NULL;
5084 const struct got_tree_entry *te = NULL;
5085 char *tree_path = NULL, *te_name;
5086 char *ondisk_path = NULL, *path_content = NULL;
5087 struct got_blob_object *blob = NULL;
5088 int fd = -1;
5090 /* Reverting a staged deletion is a no-op. */
5091 if (status == GOT_STATUS_DELETE &&
5092 staged_status != GOT_STATUS_NO_CHANGE)
5093 return NULL;
5095 if (status == GOT_STATUS_UNVERSIONED)
5096 return (*a->progress_cb)(a->progress_arg,
5097 GOT_STATUS_UNVERSIONED, relpath);
5099 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5100 if (ie == NULL)
5101 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5103 /* Construct in-repository path of tree which contains this blob. */
5104 err = got_path_dirname(&parent_path, ie->path);
5105 if (err) {
5106 if (err->code != GOT_ERR_BAD_PATH)
5107 goto done;
5108 parent_path = strdup("/");
5109 if (parent_path == NULL) {
5110 err = got_error_from_errno("strdup");
5111 goto done;
5114 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5115 tree_path = strdup(parent_path);
5116 if (tree_path == NULL) {
5117 err = got_error_from_errno("strdup");
5118 goto done;
5120 } else {
5121 if (got_path_is_root_dir(parent_path)) {
5122 tree_path = strdup(a->worktree->path_prefix);
5123 if (tree_path == NULL) {
5124 err = got_error_from_errno("strdup");
5125 goto done;
5127 } else {
5128 if (asprintf(&tree_path, "%s/%s",
5129 a->worktree->path_prefix, parent_path) == -1) {
5130 err = got_error_from_errno("asprintf");
5131 goto done;
5136 err = got_object_open_as_commit(&base_commit, a->repo,
5137 a->worktree->base_commit_id);
5138 if (err)
5139 goto done;
5141 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5142 if (err) {
5143 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5144 (status == GOT_STATUS_ADD ||
5145 staged_status == GOT_STATUS_ADD)))
5146 goto done;
5147 } else {
5148 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5149 if (err)
5150 goto done;
5152 err = got_path_basename(&te_name, ie->path);
5153 if (err)
5154 goto done;
5156 te = got_object_tree_find_entry(tree, te_name);
5157 free(te_name);
5158 if (te == NULL && status != GOT_STATUS_ADD &&
5159 staged_status != GOT_STATUS_ADD) {
5160 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5161 goto done;
5165 switch (status) {
5166 case GOT_STATUS_ADD:
5167 if (a->patch_cb) {
5168 int choice = GOT_PATCH_CHOICE_NONE;
5169 err = (*a->patch_cb)(&choice, a->patch_arg,
5170 status, ie->path, NULL, 1, 1);
5171 if (err)
5172 goto done;
5173 if (choice != GOT_PATCH_CHOICE_YES)
5174 break;
5176 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5177 ie->path);
5178 if (err)
5179 goto done;
5180 got_fileindex_entry_remove(a->fileindex, ie);
5181 if (a->unlink_added_files) {
5182 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5184 if (a->added_files_to_unlink) {
5185 struct got_pathlist_entry *pe;
5187 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5188 entry) {
5189 if (got_path_cmp(pe->path, relpath,
5190 pe->path_len, strlen(relpath)))
5191 continue;
5192 do_unlink = 1;
5193 break;
5197 if (do_unlink) {
5198 if (asprintf(&ondisk_path, "%s/%s",
5199 got_worktree_get_root_path(a->worktree),
5200 relpath) == -1) {
5201 err = got_error_from_errno("asprintf");
5202 goto done;
5204 if (unlink(ondisk_path) == -1) {
5205 err = got_error_from_errno2("unlink",
5206 ondisk_path);
5207 break;
5211 break;
5212 case GOT_STATUS_DELETE:
5213 if (a->patch_cb) {
5214 int choice = GOT_PATCH_CHOICE_NONE;
5215 err = (*a->patch_cb)(&choice, a->patch_arg,
5216 status, ie->path, NULL, 1, 1);
5217 if (err)
5218 goto done;
5219 if (choice != GOT_PATCH_CHOICE_YES)
5220 break;
5222 /* fall through */
5223 case GOT_STATUS_MODIFY:
5224 case GOT_STATUS_MODE_CHANGE:
5225 case GOT_STATUS_CONFLICT:
5226 case GOT_STATUS_MISSING: {
5227 struct got_object_id id;
5228 if (staged_status == GOT_STATUS_ADD ||
5229 staged_status == GOT_STATUS_MODIFY)
5230 got_fileindex_entry_get_staged_blob_id(&id, ie);
5231 else
5232 got_fileindex_entry_get_blob_id(&id, ie);
5233 fd = got_opentempfd();
5234 if (fd == -1) {
5235 err = got_error_from_errno("got_opentempfd");
5236 goto done;
5239 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5240 if (err)
5241 goto done;
5243 if (asprintf(&ondisk_path, "%s/%s",
5244 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5245 err = got_error_from_errno("asprintf");
5246 goto done;
5249 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5250 status == GOT_STATUS_CONFLICT)) {
5251 int is_bad_symlink = 0;
5252 err = create_patched_content(&path_content, 1, &id,
5253 ondisk_path, dirfd, de_name, ie->path, a->repo,
5254 a->patch_cb, a->patch_arg);
5255 if (err || path_content == NULL)
5256 break;
5257 if (te && S_ISLNK(te->mode)) {
5258 if (unlink(path_content) == -1) {
5259 err = got_error_from_errno2("unlink",
5260 path_content);
5261 break;
5263 err = install_symlink(&is_bad_symlink,
5264 a->worktree, ondisk_path, ie->path,
5265 blob, 0, 1, 0, 0, a->repo,
5266 a->progress_cb, a->progress_arg);
5267 } else {
5268 if (rename(path_content, ondisk_path) == -1) {
5269 err = got_error_from_errno3("rename",
5270 path_content, ondisk_path);
5271 goto done;
5274 } else {
5275 int is_bad_symlink = 0;
5276 if (te && S_ISLNK(te->mode)) {
5277 err = install_symlink(&is_bad_symlink,
5278 a->worktree, ondisk_path, ie->path,
5279 blob, 0, 1, 0, 0, a->repo,
5280 a->progress_cb, a->progress_arg);
5281 } else {
5282 err = install_blob(a->worktree, ondisk_path,
5283 ie->path,
5284 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5285 got_fileindex_perms_to_st(ie), blob,
5286 0, 1, 0, 0, a->repo,
5287 a->progress_cb, a->progress_arg);
5289 if (err)
5290 goto done;
5291 if (status == GOT_STATUS_DELETE ||
5292 status == GOT_STATUS_MODE_CHANGE) {
5293 err = got_fileindex_entry_update(ie,
5294 a->worktree->root_fd, relpath,
5295 blob->id.sha1,
5296 a->worktree->base_commit_id->sha1, 1);
5297 if (err)
5298 goto done;
5300 if (is_bad_symlink) {
5301 got_fileindex_entry_filetype_set(ie,
5302 GOT_FILEIDX_MODE_BAD_SYMLINK);
5305 break;
5307 default:
5308 break;
5310 done:
5311 free(ondisk_path);
5312 free(path_content);
5313 free(parent_path);
5314 free(tree_path);
5315 if (fd != -1 && close(fd) == -1 && err == NULL)
5316 err = got_error_from_errno("close");
5317 if (blob)
5318 got_object_blob_close(blob);
5319 if (tree)
5320 got_object_tree_close(tree);
5321 free(tree_id);
5322 if (base_commit)
5323 got_object_commit_close(base_commit);
5324 return err;
5327 const struct got_error *
5328 got_worktree_revert(struct got_worktree *worktree,
5329 struct got_pathlist_head *paths,
5330 got_worktree_checkout_cb progress_cb, void *progress_arg,
5331 got_worktree_patch_cb patch_cb, void *patch_arg,
5332 struct got_repository *repo)
5334 struct got_fileindex *fileindex = NULL;
5335 char *fileindex_path = NULL;
5336 const struct got_error *err = NULL, *unlockerr = NULL;
5337 const struct got_error *sync_err = NULL;
5338 struct got_pathlist_entry *pe;
5339 struct revert_file_args rfa;
5341 err = lock_worktree(worktree, LOCK_EX);
5342 if (err)
5343 return err;
5345 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5346 if (err)
5347 goto done;
5349 rfa.worktree = worktree;
5350 rfa.fileindex = fileindex;
5351 rfa.progress_cb = progress_cb;
5352 rfa.progress_arg = progress_arg;
5353 rfa.patch_cb = patch_cb;
5354 rfa.patch_arg = patch_arg;
5355 rfa.repo = repo;
5356 rfa.unlink_added_files = 0;
5357 TAILQ_FOREACH(pe, paths, entry) {
5358 err = worktree_status(worktree, pe->path, fileindex, repo,
5359 revert_file, &rfa, NULL, NULL, 1, 0);
5360 if (err)
5361 break;
5363 sync_err = sync_fileindex(fileindex, fileindex_path);
5364 if (sync_err && err == NULL)
5365 err = sync_err;
5366 done:
5367 free(fileindex_path);
5368 if (fileindex)
5369 got_fileindex_free(fileindex);
5370 unlockerr = lock_worktree(worktree, LOCK_SH);
5371 if (unlockerr && err == NULL)
5372 err = unlockerr;
5373 return err;
5376 static void
5377 free_commitable(struct got_commitable *ct)
5379 free(ct->path);
5380 free(ct->in_repo_path);
5381 free(ct->ondisk_path);
5382 free(ct->blob_id);
5383 free(ct->base_blob_id);
5384 free(ct->staged_blob_id);
5385 free(ct->base_commit_id);
5386 free(ct);
5389 struct collect_commitables_arg {
5390 struct got_pathlist_head *commitable_paths;
5391 struct got_repository *repo;
5392 struct got_worktree *worktree;
5393 struct got_fileindex *fileindex;
5394 int have_staged_files;
5395 int allow_bad_symlinks;
5396 int diff_header_shown;
5397 int commit_conflicts;
5398 FILE *diff_outfile;
5399 FILE *f1;
5400 FILE *f2;
5404 * Create a file which contains the target path of a symlink so we can feed
5405 * it as content to the diff engine.
5407 static const struct got_error *
5408 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5409 const char *abspath)
5411 const struct got_error *err = NULL;
5412 char target_path[PATH_MAX];
5413 ssize_t target_len, outlen;
5415 *fd = -1;
5417 if (dirfd != -1) {
5418 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5419 if (target_len == -1)
5420 return got_error_from_errno2("readlinkat", abspath);
5421 } else {
5422 target_len = readlink(abspath, target_path, PATH_MAX);
5423 if (target_len == -1)
5424 return got_error_from_errno2("readlink", abspath);
5427 *fd = got_opentempfd();
5428 if (*fd == -1)
5429 return got_error_from_errno("got_opentempfd");
5431 outlen = write(*fd, target_path, target_len);
5432 if (outlen == -1) {
5433 err = got_error_from_errno("got_opentempfd");
5434 goto done;
5437 if (lseek(*fd, 0, SEEK_SET) == -1) {
5438 err = got_error_from_errno2("lseek", abspath);
5439 goto done;
5441 done:
5442 if (err) {
5443 close(*fd);
5444 *fd = -1;
5446 return err;
5449 static const struct got_error *
5450 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5451 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5452 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5454 const struct got_error *err = NULL;
5455 struct got_blob_object *blob1 = NULL;
5456 int fd = -1, fd1 = -1, fd2 = -1;
5457 FILE *ondisk_file = NULL;
5458 char *label1 = NULL;
5459 struct stat sb;
5460 off_t size1 = 0;
5461 int f2_exists = 0;
5462 char *id_str = NULL;
5464 memset(&sb, 0, sizeof(sb));
5466 if (diff_staged) {
5467 if (ct->staged_status != GOT_STATUS_MODIFY &&
5468 ct->staged_status != GOT_STATUS_ADD &&
5469 ct->staged_status != GOT_STATUS_DELETE)
5470 return NULL;
5471 } else {
5472 if (ct->status != GOT_STATUS_MODIFY &&
5473 ct->status != GOT_STATUS_ADD &&
5474 ct->status != GOT_STATUS_DELETE &&
5475 ct->status != GOT_STATUS_CONFLICT)
5476 return NULL;
5479 err = got_opentemp_truncate(f1);
5480 if (err)
5481 return got_error_from_errno("got_opentemp_truncate");
5482 err = got_opentemp_truncate(f2);
5483 if (err)
5484 return got_error_from_errno("got_opentemp_truncate");
5486 if (!*diff_header_shown) {
5487 err = got_object_id_str(&id_str, worktree->base_commit_id);
5488 if (err)
5489 return err;
5490 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5491 got_worktree_get_root_path(worktree));
5492 fprintf(diff_outfile, "commit - %s\n", id_str);
5493 fprintf(diff_outfile, "path + %s%s\n",
5494 got_worktree_get_root_path(worktree),
5495 diff_staged ? " (staged changes)" : "");
5496 *diff_header_shown = 1;
5499 if (diff_staged) {
5500 const char *label1 = NULL, *label2 = NULL;
5501 switch (ct->staged_status) {
5502 case GOT_STATUS_MODIFY:
5503 label1 = ct->path;
5504 label2 = ct->path;
5505 break;
5506 case GOT_STATUS_ADD:
5507 label2 = ct->path;
5508 break;
5509 case GOT_STATUS_DELETE:
5510 label1 = ct->path;
5511 break;
5512 default:
5513 return got_error(GOT_ERR_FILE_STATUS);
5515 fd1 = got_opentempfd();
5516 if (fd1 == -1) {
5517 err = got_error_from_errno("got_opentempfd");
5518 goto done;
5520 fd2 = got_opentempfd();
5521 if (fd2 == -1) {
5522 err = got_error_from_errno("got_opentempfd");
5523 goto done;
5525 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5526 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5527 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5528 NULL, repo, diff_outfile);
5529 goto done;
5532 fd1 = got_opentempfd();
5533 if (fd1 == -1) {
5534 err = got_error_from_errno("got_opentempfd");
5535 goto done;
5538 if (ct->status != GOT_STATUS_ADD) {
5539 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5540 8192, fd1);
5541 if (err)
5542 goto done;
5545 if (ct->status != GOT_STATUS_DELETE) {
5546 if (dirfd != -1) {
5547 fd = openat(dirfd, de_name,
5548 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5549 if (fd == -1) {
5550 if (!got_err_open_nofollow_on_symlink()) {
5551 err = got_error_from_errno2("openat",
5552 ct->ondisk_path);
5553 goto done;
5555 err = get_symlink_target_file(&fd, dirfd,
5556 de_name, ct->ondisk_path);
5557 if (err)
5558 goto done;
5560 } else {
5561 fd = open(ct->ondisk_path,
5562 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5563 if (fd == -1) {
5564 if (!got_err_open_nofollow_on_symlink()) {
5565 err = got_error_from_errno2("open",
5566 ct->ondisk_path);
5567 goto done;
5569 err = get_symlink_target_file(&fd, dirfd,
5570 de_name, ct->ondisk_path);
5571 if (err)
5572 goto done;
5575 if (fstatat(fd, ct->ondisk_path, &sb,
5576 AT_SYMLINK_NOFOLLOW) == -1) {
5577 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5578 goto done;
5580 ondisk_file = fdopen(fd, "r");
5581 if (ondisk_file == NULL) {
5582 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5583 goto done;
5585 fd = -1;
5586 f2_exists = 1;
5589 if (blob1) {
5590 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5591 f1, blob1);
5592 if (err)
5593 goto done;
5596 err = got_diff_blob_file(blob1, f1, size1, label1,
5597 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5598 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5599 done:
5600 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5601 err = got_error_from_errno("close");
5602 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5603 err = got_error_from_errno("close");
5604 if (blob1)
5605 got_object_blob_close(blob1);
5606 if (fd != -1 && close(fd) == -1 && err == NULL)
5607 err = got_error_from_errno("close");
5608 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5609 err = got_error_from_errno("fclose");
5610 return err;
5613 static const struct got_error *
5614 collect_commitables(void *arg, unsigned char status,
5615 unsigned char staged_status, const char *relpath,
5616 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5617 struct got_object_id *commit_id, int dirfd, const char *de_name)
5619 struct collect_commitables_arg *a = arg;
5620 const struct got_error *err = NULL;
5621 struct got_commitable *ct = NULL;
5622 struct got_pathlist_entry *new = NULL;
5623 char *parent_path = NULL, *path = NULL;
5624 struct stat sb;
5626 if (a->have_staged_files) {
5627 if (staged_status != GOT_STATUS_MODIFY &&
5628 staged_status != GOT_STATUS_ADD &&
5629 staged_status != GOT_STATUS_DELETE)
5630 return NULL;
5631 } else {
5632 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5633 printf("C %s\n", relpath);
5634 return got_error(GOT_ERR_COMMIT_CONFLICT);
5637 if (status != GOT_STATUS_MODIFY &&
5638 status != GOT_STATUS_MODE_CHANGE &&
5639 status != GOT_STATUS_ADD &&
5640 status != GOT_STATUS_DELETE &&
5641 status != GOT_STATUS_CONFLICT)
5642 return NULL;
5645 if (asprintf(&path, "/%s", relpath) == -1) {
5646 err = got_error_from_errno("asprintf");
5647 goto done;
5649 if (strcmp(path, "/") == 0) {
5650 parent_path = strdup("");
5651 if (parent_path == NULL)
5652 return got_error_from_errno("strdup");
5653 } else {
5654 err = got_path_dirname(&parent_path, path);
5655 if (err)
5656 return err;
5659 ct = calloc(1, sizeof(*ct));
5660 if (ct == NULL) {
5661 err = got_error_from_errno("calloc");
5662 goto done;
5665 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5666 relpath) == -1) {
5667 err = got_error_from_errno("asprintf");
5668 goto done;
5671 if (staged_status == GOT_STATUS_ADD ||
5672 staged_status == GOT_STATUS_MODIFY) {
5673 struct got_fileindex_entry *ie;
5674 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5675 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5676 case GOT_FILEIDX_MODE_REGULAR_FILE:
5677 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5678 ct->mode = S_IFREG;
5679 break;
5680 case GOT_FILEIDX_MODE_SYMLINK:
5681 ct->mode = S_IFLNK;
5682 break;
5683 default:
5684 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5685 goto done;
5687 ct->mode |= got_fileindex_entry_perms_get(ie);
5688 } else if (status != GOT_STATUS_DELETE &&
5689 staged_status != GOT_STATUS_DELETE) {
5690 if (dirfd != -1) {
5691 if (fstatat(dirfd, de_name, &sb,
5692 AT_SYMLINK_NOFOLLOW) == -1) {
5693 err = got_error_from_errno2("fstatat",
5694 ct->ondisk_path);
5695 goto done;
5697 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5698 err = got_error_from_errno2("lstat", ct->ondisk_path);
5699 goto done;
5701 ct->mode = sb.st_mode;
5704 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5705 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5706 relpath) == -1) {
5707 err = got_error_from_errno("asprintf");
5708 goto done;
5711 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5712 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5713 int is_bad_symlink;
5714 char target_path[PATH_MAX];
5715 ssize_t target_len;
5716 target_len = readlink(ct->ondisk_path, target_path,
5717 sizeof(target_path));
5718 if (target_len == -1) {
5719 err = got_error_from_errno2("readlink",
5720 ct->ondisk_path);
5721 goto done;
5723 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5724 target_len, ct->ondisk_path, a->worktree->root_path,
5725 a->worktree->meta_dir);
5726 if (err)
5727 goto done;
5728 if (is_bad_symlink) {
5729 err = got_error_path(ct->ondisk_path,
5730 GOT_ERR_BAD_SYMLINK);
5731 goto done;
5736 ct->status = status;
5737 ct->staged_status = staged_status;
5738 ct->blob_id = NULL; /* will be filled in when blob gets created */
5739 if (ct->status != GOT_STATUS_ADD &&
5740 ct->staged_status != GOT_STATUS_ADD) {
5741 ct->base_blob_id = got_object_id_dup(blob_id);
5742 if (ct->base_blob_id == NULL) {
5743 err = got_error_from_errno("got_object_id_dup");
5744 goto done;
5746 ct->base_commit_id = got_object_id_dup(commit_id);
5747 if (ct->base_commit_id == NULL) {
5748 err = got_error_from_errno("got_object_id_dup");
5749 goto done;
5752 if (ct->staged_status == GOT_STATUS_ADD ||
5753 ct->staged_status == GOT_STATUS_MODIFY) {
5754 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5755 if (ct->staged_blob_id == NULL) {
5756 err = got_error_from_errno("got_object_id_dup");
5757 goto done;
5760 ct->path = strdup(path);
5761 if (ct->path == NULL) {
5762 err = got_error_from_errno("strdup");
5763 goto done;
5765 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5766 if (err)
5767 goto done;
5769 if (a->diff_outfile && ct && new != NULL) {
5770 err = append_ct_diff(ct, &a->diff_header_shown,
5771 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5772 a->have_staged_files, a->repo, a->worktree);
5773 if (err)
5774 goto done;
5776 done:
5777 if (ct && (err || new == NULL))
5778 free_commitable(ct);
5779 free(parent_path);
5780 free(path);
5781 return err;
5784 static const struct got_error *write_tree(struct got_object_id **, int *,
5785 struct got_tree_object *, const char *, struct got_pathlist_head *,
5786 got_worktree_status_cb status_cb, void *status_arg,
5787 struct got_repository *);
5789 static const struct got_error *
5790 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5791 struct got_tree_entry *te, const char *parent_path,
5792 struct got_pathlist_head *commitable_paths,
5793 got_worktree_status_cb status_cb, void *status_arg,
5794 struct got_repository *repo)
5796 const struct got_error *err = NULL;
5797 struct got_tree_object *subtree;
5798 char *subpath;
5800 if (asprintf(&subpath, "%s%s%s", parent_path,
5801 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5802 return got_error_from_errno("asprintf");
5804 err = got_object_open_as_tree(&subtree, repo, &te->id);
5805 if (err)
5806 return err;
5808 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5809 commitable_paths, status_cb, status_arg, repo);
5810 got_object_tree_close(subtree);
5811 free(subpath);
5812 return err;
5815 static const struct got_error *
5816 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5818 const struct got_error *err = NULL;
5819 char *ct_parent_path = NULL;
5821 *match = 0;
5823 if (strchr(ct->in_repo_path, '/') == NULL) {
5824 *match = got_path_is_root_dir(path);
5825 return NULL;
5828 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5829 if (err)
5830 return err;
5831 *match = (strcmp(path, ct_parent_path) == 0);
5832 free(ct_parent_path);
5833 return err;
5836 static mode_t
5837 get_ct_file_mode(struct got_commitable *ct)
5839 if (S_ISLNK(ct->mode))
5840 return S_IFLNK;
5842 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5845 static const struct got_error *
5846 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5847 struct got_tree_entry *te, struct got_commitable *ct)
5849 const struct got_error *err = NULL;
5851 *new_te = NULL;
5853 err = got_object_tree_entry_dup(new_te, te);
5854 if (err)
5855 goto done;
5857 (*new_te)->mode = get_ct_file_mode(ct);
5859 if (ct->staged_status == GOT_STATUS_MODIFY)
5860 memcpy(&(*new_te)->id, ct->staged_blob_id,
5861 sizeof((*new_te)->id));
5862 else
5863 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5864 done:
5865 if (err && *new_te) {
5866 free(*new_te);
5867 *new_te = NULL;
5869 return err;
5872 static const struct got_error *
5873 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5874 struct got_commitable *ct)
5876 const struct got_error *err = NULL;
5877 char *ct_name = NULL;
5879 *new_te = NULL;
5881 *new_te = calloc(1, sizeof(**new_te));
5882 if (*new_te == NULL)
5883 return got_error_from_errno("calloc");
5885 err = got_path_basename(&ct_name, ct->path);
5886 if (err)
5887 goto done;
5888 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5889 sizeof((*new_te)->name)) {
5890 err = got_error(GOT_ERR_NO_SPACE);
5891 goto done;
5894 (*new_te)->mode = get_ct_file_mode(ct);
5896 if (ct->staged_status == GOT_STATUS_ADD)
5897 memcpy(&(*new_te)->id, ct->staged_blob_id,
5898 sizeof((*new_te)->id));
5899 else
5900 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5901 done:
5902 free(ct_name);
5903 if (err && *new_te) {
5904 free(*new_te);
5905 *new_te = NULL;
5907 return err;
5910 static const struct got_error *
5911 insert_tree_entry(struct got_tree_entry *new_te,
5912 struct got_pathlist_head *paths)
5914 const struct got_error *err = NULL;
5915 struct got_pathlist_entry *new_pe;
5917 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5918 if (err)
5919 return err;
5920 if (new_pe == NULL)
5921 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5922 return NULL;
5925 static const struct got_error *
5926 report_ct_status(struct got_commitable *ct,
5927 got_worktree_status_cb status_cb, void *status_arg)
5929 const char *ct_path = ct->path;
5930 unsigned char status;
5932 if (status_cb == NULL) /* no commit progress output desired */
5933 return NULL;
5935 while (ct_path[0] == '/')
5936 ct_path++;
5938 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5939 status = ct->staged_status;
5940 else
5941 status = ct->status;
5943 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5944 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5947 static const struct got_error *
5948 match_modified_subtree(int *modified, struct got_tree_entry *te,
5949 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5951 const struct got_error *err = NULL;
5952 struct got_pathlist_entry *pe;
5953 char *te_path;
5955 *modified = 0;
5957 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5958 got_path_is_root_dir(base_tree_path) ? "" : "/",
5959 te->name) == -1)
5960 return got_error_from_errno("asprintf");
5962 TAILQ_FOREACH(pe, commitable_paths, entry) {
5963 struct got_commitable *ct = pe->data;
5964 *modified = got_path_is_child(ct->in_repo_path, te_path,
5965 strlen(te_path));
5966 if (*modified)
5967 break;
5970 free(te_path);
5971 return err;
5974 static const struct got_error *
5975 match_deleted_or_modified_ct(struct got_commitable **ctp,
5976 struct got_tree_entry *te, const char *base_tree_path,
5977 struct got_pathlist_head *commitable_paths)
5979 const struct got_error *err = NULL;
5980 struct got_pathlist_entry *pe;
5982 *ctp = NULL;
5984 TAILQ_FOREACH(pe, commitable_paths, entry) {
5985 struct got_commitable *ct = pe->data;
5986 char *ct_name = NULL;
5987 int path_matches;
5989 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5990 if (ct->status != GOT_STATUS_MODIFY &&
5991 ct->status != GOT_STATUS_MODE_CHANGE &&
5992 ct->status != GOT_STATUS_DELETE &&
5993 ct->status != GOT_STATUS_CONFLICT)
5994 continue;
5995 } else {
5996 if (ct->staged_status != GOT_STATUS_MODIFY &&
5997 ct->staged_status != GOT_STATUS_DELETE)
5998 continue;
6001 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6002 continue;
6004 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6005 if (err)
6006 return err;
6007 if (!path_matches)
6008 continue;
6010 err = got_path_basename(&ct_name, pe->path);
6011 if (err)
6012 return err;
6014 if (strcmp(te->name, ct_name) != 0) {
6015 free(ct_name);
6016 continue;
6018 free(ct_name);
6020 *ctp = ct;
6021 break;
6024 return err;
6027 static const struct got_error *
6028 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6029 const char *child_path, const char *path_base_tree,
6030 struct got_pathlist_head *commitable_paths,
6031 got_worktree_status_cb status_cb, void *status_arg,
6032 struct got_repository *repo)
6034 const struct got_error *err = NULL;
6035 struct got_tree_entry *new_te;
6036 char *subtree_path;
6037 struct got_object_id *id = NULL;
6038 int nentries;
6040 *new_tep = NULL;
6042 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6043 got_path_is_root_dir(path_base_tree) ? "" : "/",
6044 child_path) == -1)
6045 return got_error_from_errno("asprintf");
6047 new_te = calloc(1, sizeof(*new_te));
6048 if (new_te == NULL)
6049 return got_error_from_errno("calloc");
6050 new_te->mode = S_IFDIR;
6052 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6053 sizeof(new_te->name)) {
6054 err = got_error(GOT_ERR_NO_SPACE);
6055 goto done;
6057 err = write_tree(&id, &nentries, NULL, subtree_path,
6058 commitable_paths, status_cb, status_arg, repo);
6059 if (err) {
6060 free(new_te);
6061 goto done;
6063 memcpy(&new_te->id, id, sizeof(new_te->id));
6064 done:
6065 free(id);
6066 free(subtree_path);
6067 if (err == NULL)
6068 *new_tep = new_te;
6069 return err;
6072 static const struct got_error *
6073 write_tree(struct got_object_id **new_tree_id, int *nentries,
6074 struct got_tree_object *base_tree, const char *path_base_tree,
6075 struct got_pathlist_head *commitable_paths,
6076 got_worktree_status_cb status_cb, void *status_arg,
6077 struct got_repository *repo)
6079 const struct got_error *err = NULL;
6080 struct got_pathlist_head paths;
6081 struct got_tree_entry *te, *new_te = NULL;
6082 struct got_pathlist_entry *pe;
6084 TAILQ_INIT(&paths);
6085 *nentries = 0;
6087 /* Insert, and recurse into, newly added entries first. */
6088 TAILQ_FOREACH(pe, commitable_paths, entry) {
6089 struct got_commitable *ct = pe->data;
6090 char *child_path = NULL, *slash;
6092 if ((ct->status != GOT_STATUS_ADD &&
6093 ct->staged_status != GOT_STATUS_ADD) ||
6094 (ct->flags & GOT_COMMITABLE_ADDED))
6095 continue;
6097 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6098 strlen(path_base_tree)))
6099 continue;
6101 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6102 ct->in_repo_path);
6103 if (err)
6104 goto done;
6106 slash = strchr(child_path, '/');
6107 if (slash == NULL) {
6108 err = alloc_added_blob_tree_entry(&new_te, ct);
6109 if (err)
6110 goto done;
6111 err = report_ct_status(ct, status_cb, status_arg);
6112 if (err)
6113 goto done;
6114 ct->flags |= GOT_COMMITABLE_ADDED;
6115 err = insert_tree_entry(new_te, &paths);
6116 if (err)
6117 goto done;
6118 (*nentries)++;
6119 } else {
6120 *slash = '\0'; /* trim trailing path components */
6121 if (base_tree == NULL ||
6122 got_object_tree_find_entry(base_tree, child_path)
6123 == NULL) {
6124 err = make_subtree_for_added_blob(&new_te,
6125 child_path, path_base_tree,
6126 commitable_paths, status_cb, status_arg,
6127 repo);
6128 if (err)
6129 goto done;
6130 err = insert_tree_entry(new_te, &paths);
6131 if (err)
6132 goto done;
6133 (*nentries)++;
6138 if (base_tree) {
6139 int i, nbase_entries;
6140 /* Handle modified and deleted entries. */
6141 nbase_entries = got_object_tree_get_nentries(base_tree);
6142 for (i = 0; i < nbase_entries; i++) {
6143 struct got_commitable *ct = NULL;
6145 te = got_object_tree_get_entry(base_tree, i);
6146 if (got_object_tree_entry_is_submodule(te)) {
6147 /* Entry is a submodule; just copy it. */
6148 err = got_object_tree_entry_dup(&new_te, te);
6149 if (err)
6150 goto done;
6151 err = insert_tree_entry(new_te, &paths);
6152 if (err)
6153 goto done;
6154 (*nentries)++;
6155 continue;
6158 if (S_ISDIR(te->mode)) {
6159 int modified;
6160 err = got_object_tree_entry_dup(&new_te, te);
6161 if (err)
6162 goto done;
6163 err = match_modified_subtree(&modified, te,
6164 path_base_tree, commitable_paths);
6165 if (err)
6166 goto done;
6167 /* Avoid recursion into unmodified subtrees. */
6168 if (modified) {
6169 struct got_object_id *new_id;
6170 int nsubentries;
6171 err = write_subtree(&new_id,
6172 &nsubentries, te,
6173 path_base_tree, commitable_paths,
6174 status_cb, status_arg, repo);
6175 if (err)
6176 goto done;
6177 if (nsubentries == 0) {
6178 /* All entries were deleted. */
6179 free(new_id);
6180 continue;
6182 memcpy(&new_te->id, new_id,
6183 sizeof(new_te->id));
6184 free(new_id);
6186 err = insert_tree_entry(new_te, &paths);
6187 if (err)
6188 goto done;
6189 (*nentries)++;
6190 continue;
6193 err = match_deleted_or_modified_ct(&ct, te,
6194 path_base_tree, commitable_paths);
6195 if (err)
6196 goto done;
6197 if (ct) {
6198 /* NB: Deleted entries get dropped here. */
6199 if (ct->status == GOT_STATUS_MODIFY ||
6200 ct->status == GOT_STATUS_MODE_CHANGE ||
6201 ct->status == GOT_STATUS_CONFLICT ||
6202 ct->staged_status == GOT_STATUS_MODIFY) {
6203 err = alloc_modified_blob_tree_entry(
6204 &new_te, te, ct);
6205 if (err)
6206 goto done;
6207 err = insert_tree_entry(new_te, &paths);
6208 if (err)
6209 goto done;
6210 (*nentries)++;
6212 err = report_ct_status(ct, status_cb,
6213 status_arg);
6214 if (err)
6215 goto done;
6216 } else {
6217 /* Entry is unchanged; just copy it. */
6218 err = got_object_tree_entry_dup(&new_te, te);
6219 if (err)
6220 goto done;
6221 err = insert_tree_entry(new_te, &paths);
6222 if (err)
6223 goto done;
6224 (*nentries)++;
6229 /* Write new list of entries; deleted entries have been dropped. */
6230 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6231 done:
6232 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6233 return err;
6236 static const struct got_error *
6237 update_fileindex_after_commit(struct got_worktree *worktree,
6238 struct got_pathlist_head *commitable_paths,
6239 struct got_object_id *new_base_commit_id,
6240 struct got_fileindex *fileindex, int have_staged_files)
6242 const struct got_error *err = NULL;
6243 struct got_pathlist_entry *pe;
6244 char *relpath = NULL;
6246 TAILQ_FOREACH(pe, commitable_paths, entry) {
6247 struct got_fileindex_entry *ie;
6248 struct got_commitable *ct = pe->data;
6250 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6252 err = got_path_skip_common_ancestor(&relpath,
6253 worktree->root_path, ct->ondisk_path);
6254 if (err)
6255 goto done;
6257 if (ie) {
6258 if (ct->status == GOT_STATUS_DELETE ||
6259 ct->staged_status == GOT_STATUS_DELETE) {
6260 got_fileindex_entry_remove(fileindex, ie);
6261 } else if (ct->staged_status == GOT_STATUS_ADD ||
6262 ct->staged_status == GOT_STATUS_MODIFY) {
6263 got_fileindex_entry_stage_set(ie,
6264 GOT_FILEIDX_STAGE_NONE);
6265 got_fileindex_entry_staged_filetype_set(ie, 0);
6267 err = got_fileindex_entry_update(ie,
6268 worktree->root_fd, relpath,
6269 ct->staged_blob_id->sha1,
6270 new_base_commit_id->sha1,
6271 !have_staged_files);
6272 } else
6273 err = got_fileindex_entry_update(ie,
6274 worktree->root_fd, relpath,
6275 ct->blob_id->sha1,
6276 new_base_commit_id->sha1,
6277 !have_staged_files);
6278 } else {
6279 err = got_fileindex_entry_alloc(&ie, pe->path);
6280 if (err)
6281 goto done;
6282 err = got_fileindex_entry_update(ie,
6283 worktree->root_fd, relpath, ct->blob_id->sha1,
6284 new_base_commit_id->sha1, 1);
6285 if (err) {
6286 got_fileindex_entry_free(ie);
6287 goto done;
6289 err = got_fileindex_entry_add(fileindex, ie);
6290 if (err) {
6291 got_fileindex_entry_free(ie);
6292 goto done;
6295 free(relpath);
6296 relpath = NULL;
6298 done:
6299 free(relpath);
6300 return err;
6304 static const struct got_error *
6305 check_out_of_date(const char *in_repo_path, unsigned char status,
6306 unsigned char staged_status, struct got_object_id *base_blob_id,
6307 struct got_object_id *base_commit_id,
6308 struct got_object_id *head_commit_id, struct got_repository *repo,
6309 int ood_errcode)
6311 const struct got_error *err = NULL;
6312 struct got_commit_object *commit = NULL;
6313 struct got_object_id *id = NULL;
6315 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6316 /* Trivial case: base commit == head commit */
6317 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6318 return NULL;
6320 * Ensure file content which local changes were based
6321 * on matches file content in the branch head.
6323 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6324 if (err)
6325 goto done;
6326 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6327 if (err) {
6328 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6329 err = got_error(ood_errcode);
6330 goto done;
6331 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6332 err = got_error(ood_errcode);
6333 } else {
6334 /* Require that added files don't exist in the branch head. */
6335 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6336 if (err)
6337 goto done;
6338 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6339 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6340 goto done;
6341 err = id ? got_error(ood_errcode) : NULL;
6343 done:
6344 free(id);
6345 if (commit)
6346 got_object_commit_close(commit);
6347 return err;
6350 static const struct got_error *
6351 commit_worktree(struct got_object_id **new_commit_id,
6352 struct got_pathlist_head *commitable_paths,
6353 struct got_object_id *head_commit_id,
6354 struct got_object_id *parent_id2,
6355 struct got_worktree *worktree,
6356 const char *author, const char *committer, char *diff_path,
6357 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6358 got_worktree_status_cb status_cb, void *status_arg,
6359 struct got_repository *repo)
6361 const struct got_error *err = NULL, *unlockerr = NULL;
6362 struct got_pathlist_entry *pe;
6363 const char *head_ref_name = NULL;
6364 struct got_commit_object *head_commit = NULL;
6365 struct got_reference *head_ref2 = NULL;
6366 struct got_object_id *head_commit_id2 = NULL;
6367 struct got_tree_object *head_tree = NULL;
6368 struct got_object_id *new_tree_id = NULL;
6369 int nentries, nparents = 0;
6370 struct got_object_id_queue parent_ids;
6371 struct got_object_qid *pid = NULL;
6372 char *logmsg = NULL;
6373 time_t timestamp;
6375 *new_commit_id = NULL;
6377 STAILQ_INIT(&parent_ids);
6379 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6380 if (err)
6381 goto done;
6383 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6384 if (err)
6385 goto done;
6387 if (commit_msg_cb != NULL) {
6388 err = commit_msg_cb(commitable_paths, diff_path,
6389 &logmsg, commit_arg);
6390 if (err)
6391 goto done;
6394 if (logmsg == NULL || strlen(logmsg) == 0) {
6395 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6396 goto done;
6399 /* Create blobs from added and modified files and record their IDs. */
6400 TAILQ_FOREACH(pe, commitable_paths, entry) {
6401 struct got_commitable *ct = pe->data;
6402 char *ondisk_path;
6404 /* Blobs for staged files already exist. */
6405 if (ct->staged_status == GOT_STATUS_ADD ||
6406 ct->staged_status == GOT_STATUS_MODIFY)
6407 continue;
6409 if (ct->status != GOT_STATUS_ADD &&
6410 ct->status != GOT_STATUS_MODIFY &&
6411 ct->status != GOT_STATUS_MODE_CHANGE &&
6412 ct->status != GOT_STATUS_CONFLICT)
6413 continue;
6415 if (asprintf(&ondisk_path, "%s/%s",
6416 worktree->root_path, pe->path) == -1) {
6417 err = got_error_from_errno("asprintf");
6418 goto done;
6420 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6421 free(ondisk_path);
6422 if (err)
6423 goto done;
6426 /* Recursively write new tree objects. */
6427 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6428 commitable_paths, status_cb, status_arg, repo);
6429 if (err)
6430 goto done;
6432 err = got_object_qid_alloc(&pid, head_commit_id);
6433 if (err)
6434 goto done;
6435 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6436 nparents++;
6437 if (parent_id2) {
6438 err = got_object_qid_alloc(&pid, parent_id2);
6439 if (err)
6440 goto done;
6441 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6442 nparents++;
6444 timestamp = time(NULL);
6445 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6446 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6447 if (logmsg != NULL)
6448 free(logmsg);
6449 if (err)
6450 goto done;
6452 /* Check if a concurrent commit to our branch has occurred. */
6453 head_ref_name = got_worktree_get_head_ref_name(worktree);
6454 if (head_ref_name == NULL) {
6455 err = got_error_from_errno("got_worktree_get_head_ref_name");
6456 goto done;
6458 /* Lock the reference here to prevent concurrent modification. */
6459 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6460 if (err)
6461 goto done;
6462 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6463 if (err)
6464 goto done;
6465 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6466 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6467 goto done;
6469 /* Update branch head in repository. */
6470 err = got_ref_change_ref(head_ref2, *new_commit_id);
6471 if (err)
6472 goto done;
6473 err = got_ref_write(head_ref2, repo);
6474 if (err)
6475 goto done;
6477 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6478 if (err)
6479 goto done;
6481 err = ref_base_commit(worktree, repo);
6482 if (err)
6483 goto done;
6484 done:
6485 got_object_id_queue_free(&parent_ids);
6486 if (head_tree)
6487 got_object_tree_close(head_tree);
6488 if (head_commit)
6489 got_object_commit_close(head_commit);
6490 free(head_commit_id2);
6491 if (head_ref2) {
6492 unlockerr = got_ref_unlock(head_ref2);
6493 if (unlockerr && err == NULL)
6494 err = unlockerr;
6495 got_ref_close(head_ref2);
6497 return err;
6500 static const struct got_error *
6501 check_path_is_commitable(const char *path,
6502 struct got_pathlist_head *commitable_paths)
6504 struct got_pathlist_entry *cpe = NULL;
6505 size_t path_len = strlen(path);
6507 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6508 struct got_commitable *ct = cpe->data;
6509 const char *ct_path = ct->path;
6511 while (ct_path[0] == '/')
6512 ct_path++;
6514 if (strcmp(path, ct_path) == 0 ||
6515 got_path_is_child(ct_path, path, path_len))
6516 break;
6519 if (cpe == NULL)
6520 return got_error_path(path, GOT_ERR_BAD_PATH);
6522 return NULL;
6525 static const struct got_error *
6526 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6528 int *have_staged_files = arg;
6530 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6531 *have_staged_files = 1;
6532 return got_error(GOT_ERR_CANCELLED);
6535 return NULL;
6538 static const struct got_error *
6539 check_non_staged_files(struct got_fileindex *fileindex,
6540 struct got_pathlist_head *paths)
6542 struct got_pathlist_entry *pe;
6543 struct got_fileindex_entry *ie;
6545 TAILQ_FOREACH(pe, paths, entry) {
6546 if (pe->path[0] == '\0')
6547 continue;
6548 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6549 if (ie == NULL)
6550 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6551 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6552 return got_error_path(pe->path,
6553 GOT_ERR_FILE_NOT_STAGED);
6556 return NULL;
6559 const struct got_error *
6560 got_worktree_commit(struct got_object_id **new_commit_id,
6561 struct got_worktree *worktree, struct got_pathlist_head *paths,
6562 const char *author, const char *committer, int allow_bad_symlinks,
6563 int show_diff, int commit_conflicts,
6564 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6565 got_worktree_status_cb status_cb, void *status_arg,
6566 struct got_repository *repo)
6568 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6569 struct got_fileindex *fileindex = NULL;
6570 char *fileindex_path = NULL;
6571 struct got_pathlist_head commitable_paths;
6572 struct collect_commitables_arg cc_arg;
6573 struct got_pathlist_entry *pe;
6574 struct got_reference *head_ref = NULL;
6575 struct got_object_id *head_commit_id = NULL;
6576 char *diff_path = NULL;
6577 int have_staged_files = 0;
6579 *new_commit_id = NULL;
6581 memset(&cc_arg, 0, sizeof(cc_arg));
6582 TAILQ_INIT(&commitable_paths);
6584 err = lock_worktree(worktree, LOCK_EX);
6585 if (err)
6586 goto done;
6588 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6589 if (err)
6590 goto done;
6592 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6593 if (err)
6594 goto done;
6596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6597 if (err)
6598 goto done;
6600 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6601 &have_staged_files);
6602 if (err && err->code != GOT_ERR_CANCELLED)
6603 goto done;
6604 if (have_staged_files) {
6605 err = check_non_staged_files(fileindex, paths);
6606 if (err)
6607 goto done;
6610 cc_arg.commitable_paths = &commitable_paths;
6611 cc_arg.worktree = worktree;
6612 cc_arg.fileindex = fileindex;
6613 cc_arg.repo = repo;
6614 cc_arg.have_staged_files = have_staged_files;
6615 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6616 cc_arg.diff_header_shown = 0;
6617 cc_arg.commit_conflicts = commit_conflicts;
6618 if (show_diff) {
6619 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6620 GOT_TMPDIR_STR "/got", ".diff");
6621 if (err)
6622 goto done;
6623 cc_arg.f1 = got_opentemp();
6624 if (cc_arg.f1 == NULL) {
6625 err = got_error_from_errno("got_opentemp");
6626 goto done;
6628 cc_arg.f2 = got_opentemp();
6629 if (cc_arg.f2 == NULL) {
6630 err = got_error_from_errno("got_opentemp");
6631 goto done;
6635 TAILQ_FOREACH(pe, paths, entry) {
6636 err = worktree_status(worktree, pe->path, fileindex, repo,
6637 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6638 if (err)
6639 goto done;
6642 if (show_diff) {
6643 if (fflush(cc_arg.diff_outfile) == EOF) {
6644 err = got_error_from_errno("fflush");
6645 goto done;
6649 if (TAILQ_EMPTY(&commitable_paths)) {
6650 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6651 goto done;
6654 TAILQ_FOREACH(pe, paths, entry) {
6655 err = check_path_is_commitable(pe->path, &commitable_paths);
6656 if (err)
6657 goto done;
6660 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6661 struct got_commitable *ct = pe->data;
6662 const char *ct_path = ct->in_repo_path;
6664 while (ct_path[0] == '/')
6665 ct_path++;
6666 err = check_out_of_date(ct_path, ct->status,
6667 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6668 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6669 if (err)
6670 goto done;
6674 err = commit_worktree(new_commit_id, &commitable_paths,
6675 head_commit_id, NULL, worktree, author, committer,
6676 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6677 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6678 if (err)
6679 goto done;
6681 err = update_fileindex_after_commit(worktree, &commitable_paths,
6682 *new_commit_id, fileindex, have_staged_files);
6683 sync_err = sync_fileindex(fileindex, fileindex_path);
6684 if (sync_err && err == NULL)
6685 err = sync_err;
6686 done:
6687 if (fileindex)
6688 got_fileindex_free(fileindex);
6689 free(fileindex_path);
6690 unlockerr = lock_worktree(worktree, LOCK_SH);
6691 if (unlockerr && err == NULL)
6692 err = unlockerr;
6693 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6694 struct got_commitable *ct = pe->data;
6696 free_commitable(ct);
6698 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6699 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6700 err = got_error_from_errno2("unlink", diff_path);
6701 free(diff_path);
6702 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6703 err == NULL)
6704 err = got_error_from_errno("fclose");
6705 return err;
6708 const char *
6709 got_commitable_get_path(struct got_commitable *ct)
6711 return ct->path;
6714 unsigned int
6715 got_commitable_get_status(struct got_commitable *ct)
6717 return ct->status;
6720 struct check_rebase_ok_arg {
6721 struct got_worktree *worktree;
6722 struct got_repository *repo;
6725 static const struct got_error *
6726 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6728 const struct got_error *err = NULL;
6729 struct check_rebase_ok_arg *a = arg;
6730 unsigned char status;
6731 struct stat sb;
6732 char *ondisk_path;
6734 /* Reject rebase of a work tree with mixed base commits. */
6735 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6736 SHA1_DIGEST_LENGTH))
6737 return got_error(GOT_ERR_MIXED_COMMITS);
6739 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6740 == -1)
6741 return got_error_from_errno("asprintf");
6743 /* Reject rebase of a work tree with modified or staged files. */
6744 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6745 free(ondisk_path);
6746 if (err)
6747 return err;
6749 if (status != GOT_STATUS_NO_CHANGE)
6750 return got_error(GOT_ERR_MODIFIED);
6751 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6752 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6754 return NULL;
6757 const struct got_error *
6758 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6759 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6760 struct got_worktree *worktree, struct got_reference *branch,
6761 struct got_repository *repo)
6763 const struct got_error *err = NULL;
6764 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6765 char *branch_ref_name = NULL;
6766 char *fileindex_path = NULL;
6767 struct check_rebase_ok_arg ok_arg;
6768 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6769 struct got_object_id *wt_branch_tip = NULL;
6771 *new_base_branch_ref = NULL;
6772 *tmp_branch = NULL;
6773 *fileindex = NULL;
6775 err = lock_worktree(worktree, LOCK_EX);
6776 if (err)
6777 return err;
6779 err = open_fileindex(fileindex, &fileindex_path, worktree);
6780 if (err)
6781 goto done;
6783 ok_arg.worktree = worktree;
6784 ok_arg.repo = repo;
6785 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6786 &ok_arg);
6787 if (err)
6788 goto done;
6790 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6791 if (err)
6792 goto done;
6794 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6795 if (err)
6796 goto done;
6798 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6799 if (err)
6800 goto done;
6802 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6803 0);
6804 if (err)
6805 goto done;
6807 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6808 if (err)
6809 goto done;
6810 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6811 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6812 goto done;
6815 err = got_ref_alloc_symref(new_base_branch_ref,
6816 new_base_branch_ref_name, wt_branch);
6817 if (err)
6818 goto done;
6819 err = got_ref_write(*new_base_branch_ref, repo);
6820 if (err)
6821 goto done;
6823 /* TODO Lock original branch's ref while rebasing? */
6825 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6826 if (err)
6827 goto done;
6829 err = got_ref_write(branch_ref, repo);
6830 if (err)
6831 goto done;
6833 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6834 worktree->base_commit_id);
6835 if (err)
6836 goto done;
6837 err = got_ref_write(*tmp_branch, repo);
6838 if (err)
6839 goto done;
6841 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6842 if (err)
6843 goto done;
6844 done:
6845 free(fileindex_path);
6846 free(tmp_branch_name);
6847 free(new_base_branch_ref_name);
6848 free(branch_ref_name);
6849 if (branch_ref)
6850 got_ref_close(branch_ref);
6851 if (wt_branch)
6852 got_ref_close(wt_branch);
6853 free(wt_branch_tip);
6854 if (err) {
6855 if (*new_base_branch_ref) {
6856 got_ref_close(*new_base_branch_ref);
6857 *new_base_branch_ref = NULL;
6859 if (*tmp_branch) {
6860 got_ref_close(*tmp_branch);
6861 *tmp_branch = NULL;
6863 if (*fileindex) {
6864 got_fileindex_free(*fileindex);
6865 *fileindex = NULL;
6867 lock_worktree(worktree, LOCK_SH);
6869 return err;
6872 const struct got_error *
6873 got_worktree_rebase_continue(struct got_object_id **commit_id,
6874 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6875 struct got_reference **branch, struct got_fileindex **fileindex,
6876 struct got_worktree *worktree, struct got_repository *repo)
6878 const struct got_error *err;
6879 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6880 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6881 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6882 char *fileindex_path = NULL;
6883 int have_staged_files = 0;
6885 *commit_id = NULL;
6886 *new_base_branch = NULL;
6887 *tmp_branch = NULL;
6888 *branch = NULL;
6889 *fileindex = NULL;
6891 err = lock_worktree(worktree, LOCK_EX);
6892 if (err)
6893 return err;
6895 err = open_fileindex(fileindex, &fileindex_path, worktree);
6896 if (err)
6897 goto done;
6899 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6900 &have_staged_files);
6901 if (err && err->code != GOT_ERR_CANCELLED)
6902 goto done;
6903 if (have_staged_files) {
6904 err = got_error(GOT_ERR_STAGED_PATHS);
6905 goto done;
6908 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6909 if (err)
6910 goto done;
6912 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6913 if (err)
6914 goto done;
6916 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6917 if (err)
6918 goto done;
6920 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6921 if (err)
6922 goto done;
6924 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6925 if (err)
6926 goto done;
6928 err = got_ref_open(branch, repo,
6929 got_ref_get_symref_target(branch_ref), 0);
6930 if (err)
6931 goto done;
6933 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6934 if (err)
6935 goto done;
6937 err = got_ref_resolve(commit_id, repo, commit_ref);
6938 if (err)
6939 goto done;
6941 err = got_ref_open(new_base_branch, repo,
6942 new_base_branch_ref_name, 0);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6947 if (err)
6948 goto done;
6949 done:
6950 free(commit_ref_name);
6951 free(branch_ref_name);
6952 free(fileindex_path);
6953 if (commit_ref)
6954 got_ref_close(commit_ref);
6955 if (branch_ref)
6956 got_ref_close(branch_ref);
6957 if (err) {
6958 free(*commit_id);
6959 *commit_id = NULL;
6960 if (*tmp_branch) {
6961 got_ref_close(*tmp_branch);
6962 *tmp_branch = NULL;
6964 if (*new_base_branch) {
6965 got_ref_close(*new_base_branch);
6966 *new_base_branch = NULL;
6968 if (*branch) {
6969 got_ref_close(*branch);
6970 *branch = NULL;
6972 if (*fileindex) {
6973 got_fileindex_free(*fileindex);
6974 *fileindex = NULL;
6976 lock_worktree(worktree, LOCK_SH);
6978 return err;
6981 const struct got_error *
6982 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6984 const struct got_error *err;
6985 char *tmp_branch_name = NULL;
6987 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6988 if (err)
6989 return err;
6991 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6992 free(tmp_branch_name);
6993 return NULL;
6996 static const struct got_error *
6997 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6998 const char *diff_path, char **logmsg, void *arg)
7000 *logmsg = arg;
7001 return NULL;
7004 static const struct got_error *
7005 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7006 const char *path, struct got_object_id *blob_id,
7007 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7008 int dirfd, const char *de_name)
7010 return NULL;
7013 struct collect_merged_paths_arg {
7014 got_worktree_checkout_cb progress_cb;
7015 void *progress_arg;
7016 struct got_pathlist_head *merged_paths;
7019 static const struct got_error *
7020 collect_merged_paths(void *arg, unsigned char status, const char *path)
7022 const struct got_error *err;
7023 struct collect_merged_paths_arg *a = arg;
7024 char *p;
7025 struct got_pathlist_entry *new;
7027 err = (*a->progress_cb)(a->progress_arg, status, path);
7028 if (err)
7029 return err;
7031 if (status != GOT_STATUS_MERGE &&
7032 status != GOT_STATUS_ADD &&
7033 status != GOT_STATUS_DELETE &&
7034 status != GOT_STATUS_CONFLICT)
7035 return NULL;
7037 p = strdup(path);
7038 if (p == NULL)
7039 return got_error_from_errno("strdup");
7041 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7042 if (err || new == NULL)
7043 free(p);
7044 return err;
7047 static const struct got_error *
7048 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7049 int is_rebase, struct got_repository *repo)
7051 const struct got_error *err;
7052 struct got_reference *commit_ref = NULL;
7054 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7055 if (err) {
7056 if (err->code != GOT_ERR_NOT_REF)
7057 goto done;
7058 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7059 if (err)
7060 goto done;
7061 err = got_ref_write(commit_ref, repo);
7062 if (err)
7063 goto done;
7064 } else if (is_rebase) {
7065 struct got_object_id *stored_id;
7066 int cmp;
7068 err = got_ref_resolve(&stored_id, repo, commit_ref);
7069 if (err)
7070 goto done;
7071 cmp = got_object_id_cmp(commit_id, stored_id);
7072 free(stored_id);
7073 if (cmp != 0) {
7074 err = got_error(GOT_ERR_REBASE_COMMITID);
7075 goto done;
7078 done:
7079 if (commit_ref)
7080 got_ref_close(commit_ref);
7081 return err;
7084 static const struct got_error *
7085 rebase_merge_files(struct got_pathlist_head *merged_paths,
7086 const char *commit_ref_name, struct got_worktree *worktree,
7087 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7088 struct got_object_id *commit_id, struct got_repository *repo,
7089 got_worktree_checkout_cb progress_cb, void *progress_arg,
7090 got_cancel_cb cancel_cb, void *cancel_arg)
7092 const struct got_error *err;
7093 struct got_reference *commit_ref = NULL;
7094 struct collect_merged_paths_arg cmp_arg;
7095 char *fileindex_path;
7097 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7099 err = get_fileindex_path(&fileindex_path, worktree);
7100 if (err)
7101 return err;
7103 cmp_arg.progress_cb = progress_cb;
7104 cmp_arg.progress_arg = progress_arg;
7105 cmp_arg.merged_paths = merged_paths;
7106 err = merge_files(worktree, fileindex, fileindex_path,
7107 parent_commit_id, commit_id, repo, collect_merged_paths,
7108 &cmp_arg, cancel_cb, cancel_arg);
7109 if (commit_ref)
7110 got_ref_close(commit_ref);
7111 return err;
7114 const struct got_error *
7115 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7116 struct got_worktree *worktree, struct got_fileindex *fileindex,
7117 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7118 struct got_repository *repo,
7119 got_worktree_checkout_cb progress_cb, void *progress_arg,
7120 got_cancel_cb cancel_cb, void *cancel_arg)
7122 const struct got_error *err;
7123 char *commit_ref_name;
7125 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7126 if (err)
7127 return err;
7129 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7130 if (err)
7131 goto done;
7133 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7134 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7135 progress_arg, cancel_cb, cancel_arg);
7136 done:
7137 free(commit_ref_name);
7138 return err;
7141 const struct got_error *
7142 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7143 struct got_worktree *worktree, struct got_fileindex *fileindex,
7144 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7145 struct got_repository *repo,
7146 got_worktree_checkout_cb progress_cb, void *progress_arg,
7147 got_cancel_cb cancel_cb, void *cancel_arg)
7149 const struct got_error *err;
7150 char *commit_ref_name;
7152 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7153 if (err)
7154 return err;
7156 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7157 if (err)
7158 goto done;
7160 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7161 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7162 progress_arg, cancel_cb, cancel_arg);
7163 done:
7164 free(commit_ref_name);
7165 return err;
7168 static const struct got_error *
7169 rebase_commit(struct got_object_id **new_commit_id,
7170 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7171 struct got_worktree *worktree, struct got_fileindex *fileindex,
7172 struct got_reference *tmp_branch, const char *committer,
7173 struct got_commit_object *orig_commit, const char *new_logmsg,
7174 int allow_conflict, struct got_repository *repo)
7176 const struct got_error *err, *sync_err;
7177 struct got_pathlist_head commitable_paths;
7178 struct collect_commitables_arg cc_arg;
7179 char *fileindex_path = NULL;
7180 struct got_reference *head_ref = NULL;
7181 struct got_object_id *head_commit_id = NULL;
7182 char *logmsg = NULL;
7184 memset(&cc_arg, 0, sizeof(cc_arg));
7185 TAILQ_INIT(&commitable_paths);
7186 *new_commit_id = NULL;
7188 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7190 err = get_fileindex_path(&fileindex_path, worktree);
7191 if (err)
7192 return err;
7194 cc_arg.commitable_paths = &commitable_paths;
7195 cc_arg.worktree = worktree;
7196 cc_arg.repo = repo;
7197 cc_arg.have_staged_files = 0;
7198 cc_arg.commit_conflicts = allow_conflict;
7200 * If possible get the status of individual files directly to
7201 * avoid crawling the entire work tree once per rebased commit.
7203 * Ideally, merged_paths would contain a list of commitables
7204 * we could use so we could skip worktree_status() entirely.
7205 * However, we would then need carefully keep track of cumulative
7206 * effects of operations such as file additions and deletions
7207 * in 'got histedit -f' (folding multiple commits into one),
7208 * and this extra complexity is not really worth it.
7210 if (merged_paths) {
7211 struct got_pathlist_entry *pe;
7212 TAILQ_FOREACH(pe, merged_paths, entry) {
7213 err = worktree_status(worktree, pe->path, fileindex,
7214 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7215 0);
7216 if (err)
7217 goto done;
7219 } else {
7220 err = worktree_status(worktree, "", fileindex, repo,
7221 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7222 if (err)
7223 goto done;
7226 if (TAILQ_EMPTY(&commitable_paths)) {
7227 /* No-op change; commit will be elided. */
7228 err = got_ref_delete(commit_ref, repo);
7229 if (err)
7230 goto done;
7231 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7232 goto done;
7235 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7236 if (err)
7237 goto done;
7239 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7240 if (err)
7241 goto done;
7243 if (new_logmsg) {
7244 logmsg = strdup(new_logmsg);
7245 if (logmsg == NULL) {
7246 err = got_error_from_errno("strdup");
7247 goto done;
7249 } else {
7250 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7251 if (err)
7252 goto done;
7255 /* NB: commit_worktree will call free(logmsg) */
7256 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7257 NULL, worktree, got_object_commit_get_author(orig_commit),
7258 committer ? committer :
7259 got_object_commit_get_committer(orig_commit), NULL,
7260 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7261 if (err)
7262 goto done;
7264 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7265 if (err)
7266 goto done;
7268 err = got_ref_delete(commit_ref, repo);
7269 if (err)
7270 goto done;
7272 err = update_fileindex_after_commit(worktree, &commitable_paths,
7273 *new_commit_id, fileindex, 0);
7274 sync_err = sync_fileindex(fileindex, fileindex_path);
7275 if (sync_err && err == NULL)
7276 err = sync_err;
7277 done:
7278 free(fileindex_path);
7279 free(head_commit_id);
7280 if (head_ref)
7281 got_ref_close(head_ref);
7282 if (err) {
7283 free(*new_commit_id);
7284 *new_commit_id = NULL;
7286 return err;
7289 const struct got_error *
7290 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7291 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7292 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7293 const char *committer, struct got_commit_object *orig_commit,
7294 struct got_object_id *orig_commit_id, int allow_conflict,
7295 struct got_repository *repo)
7297 const struct got_error *err;
7298 char *commit_ref_name;
7299 struct got_reference *commit_ref = NULL;
7300 struct got_object_id *commit_id = NULL;
7302 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7303 if (err)
7304 return err;
7306 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7307 if (err)
7308 goto done;
7309 err = got_ref_resolve(&commit_id, repo, commit_ref);
7310 if (err)
7311 goto done;
7312 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7313 err = got_error(GOT_ERR_REBASE_COMMITID);
7314 goto done;
7317 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7318 worktree, fileindex, tmp_branch, committer, orig_commit,
7319 NULL, allow_conflict, repo);
7320 done:
7321 if (commit_ref)
7322 got_ref_close(commit_ref);
7323 free(commit_ref_name);
7324 free(commit_id);
7325 return err;
7328 const struct got_error *
7329 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7330 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7331 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7332 const char *committer, struct got_commit_object *orig_commit,
7333 struct got_object_id *orig_commit_id, const char *new_logmsg,
7334 int allow_conflict, struct got_repository *repo)
7336 const struct got_error *err;
7337 char *commit_ref_name;
7338 struct got_reference *commit_ref = NULL;
7340 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7341 if (err)
7342 return err;
7344 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7345 if (err)
7346 goto done;
7348 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7349 worktree, fileindex, tmp_branch, committer, orig_commit,
7350 new_logmsg, allow_conflict, repo);
7351 done:
7352 if (commit_ref)
7353 got_ref_close(commit_ref);
7354 free(commit_ref_name);
7355 return err;
7358 const struct got_error *
7359 got_worktree_rebase_postpone(struct got_worktree *worktree,
7360 struct got_fileindex *fileindex)
7362 if (fileindex)
7363 got_fileindex_free(fileindex);
7364 return lock_worktree(worktree, LOCK_SH);
7367 static const struct got_error *
7368 delete_ref(const char *name, struct got_repository *repo)
7370 const struct got_error *err;
7371 struct got_reference *ref;
7373 err = got_ref_open(&ref, repo, name, 0);
7374 if (err) {
7375 if (err->code == GOT_ERR_NOT_REF)
7376 return NULL;
7377 return err;
7380 err = got_ref_delete(ref, repo);
7381 got_ref_close(ref);
7382 return err;
7385 static const struct got_error *
7386 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7388 const struct got_error *err;
7389 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7390 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7392 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7393 if (err)
7394 goto done;
7395 err = delete_ref(tmp_branch_name, repo);
7396 if (err)
7397 goto done;
7399 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7400 if (err)
7401 goto done;
7402 err = delete_ref(new_base_branch_ref_name, repo);
7403 if (err)
7404 goto done;
7406 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7407 if (err)
7408 goto done;
7409 err = delete_ref(branch_ref_name, repo);
7410 if (err)
7411 goto done;
7413 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7414 if (err)
7415 goto done;
7416 err = delete_ref(commit_ref_name, repo);
7417 if (err)
7418 goto done;
7420 done:
7421 free(tmp_branch_name);
7422 free(new_base_branch_ref_name);
7423 free(branch_ref_name);
7424 free(commit_ref_name);
7425 return err;
7428 static const struct got_error *
7429 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7430 struct got_object_id *new_commit_id, struct got_repository *repo)
7432 const struct got_error *err;
7433 struct got_reference *ref = NULL;
7434 struct got_object_id *old_commit_id = NULL;
7435 const char *branch_name = NULL;
7436 char *new_id_str = NULL;
7437 char *refname = NULL;
7439 branch_name = got_ref_get_name(branch);
7440 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7441 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7442 branch_name += 11;
7444 err = got_object_id_str(&new_id_str, new_commit_id);
7445 if (err)
7446 return err;
7448 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7449 new_id_str) == -1) {
7450 err = got_error_from_errno("asprintf");
7451 goto done;
7454 err = got_ref_resolve(&old_commit_id, repo, branch);
7455 if (err)
7456 goto done;
7458 err = got_ref_alloc(&ref, refname, old_commit_id);
7459 if (err)
7460 goto done;
7462 err = got_ref_write(ref, repo);
7463 done:
7464 free(new_id_str);
7465 free(refname);
7466 free(old_commit_id);
7467 if (ref)
7468 got_ref_close(ref);
7469 return err;
7472 const struct got_error *
7473 got_worktree_rebase_complete(struct got_worktree *worktree,
7474 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7475 struct got_reference *rebased_branch, struct got_repository *repo,
7476 int create_backup)
7478 const struct got_error *err, *unlockerr, *sync_err;
7479 struct got_object_id *new_head_commit_id = NULL;
7480 char *fileindex_path = NULL;
7482 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7483 if (err)
7484 return err;
7486 if (create_backup) {
7487 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7488 rebased_branch, new_head_commit_id, repo);
7489 if (err)
7490 goto done;
7493 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7494 if (err)
7495 goto done;
7497 err = got_ref_write(rebased_branch, repo);
7498 if (err)
7499 goto done;
7501 err = got_worktree_set_head_ref(worktree, rebased_branch);
7502 if (err)
7503 goto done;
7505 err = delete_rebase_refs(worktree, repo);
7506 if (err)
7507 goto done;
7509 err = get_fileindex_path(&fileindex_path, worktree);
7510 if (err)
7511 goto done;
7512 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7513 sync_err = sync_fileindex(fileindex, fileindex_path);
7514 if (sync_err && err == NULL)
7515 err = sync_err;
7516 done:
7517 got_fileindex_free(fileindex);
7518 free(fileindex_path);
7519 free(new_head_commit_id);
7520 unlockerr = lock_worktree(worktree, LOCK_SH);
7521 if (unlockerr && err == NULL)
7522 err = unlockerr;
7523 return err;
7526 static const struct got_error *
7527 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7528 struct got_object_id *id1, struct got_object_id *id2,
7529 struct got_repository *repo)
7531 const struct got_error *err;
7532 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7533 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7535 if (id1) {
7536 err = got_object_open_as_commit(&commit1, repo, id1);
7537 if (err)
7538 goto done;
7540 err = got_object_open_as_tree(&tree1, repo,
7541 got_object_commit_get_tree_id(commit1));
7542 if (err)
7543 goto done;
7546 if (id2) {
7547 err = got_object_open_as_commit(&commit2, repo, id2);
7548 if (err)
7549 goto done;
7551 err = got_object_open_as_tree(&tree2, repo,
7552 got_object_commit_get_tree_id(commit2));
7553 if (err)
7554 goto done;
7557 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7558 got_diff_tree_collect_changed_paths, paths, 0);
7559 if (err)
7560 goto done;
7561 done:
7562 if (commit1)
7563 got_object_commit_close(commit1);
7564 if (commit2)
7565 got_object_commit_close(commit2);
7566 if (tree1)
7567 got_object_tree_close(tree1);
7568 if (tree2)
7569 got_object_tree_close(tree2);
7570 return err;
7573 static const struct got_error *
7574 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7575 struct got_object_id *id1, struct got_object_id *id2,
7576 const char *path_prefix, struct got_repository *repo)
7578 const struct got_error *err;
7579 struct got_pathlist_head merged_paths;
7580 struct got_pathlist_entry *pe;
7581 char *abspath = NULL, *wt_path = NULL;
7583 TAILQ_INIT(&merged_paths);
7585 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7586 if (err)
7587 goto done;
7589 TAILQ_FOREACH(pe, &merged_paths, entry) {
7590 struct got_diff_changed_path *change = pe->data;
7592 if (change->status != GOT_STATUS_ADD)
7593 continue;
7595 if (got_path_is_root_dir(path_prefix)) {
7596 wt_path = strdup(pe->path);
7597 if (wt_path == NULL) {
7598 err = got_error_from_errno("strdup");
7599 goto done;
7601 } else {
7602 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7603 err = got_error_from_errno("asprintf");
7604 goto done;
7607 err = got_path_skip_common_ancestor(&wt_path,
7608 path_prefix, abspath);
7609 if (err)
7610 goto done;
7611 free(abspath);
7612 abspath = NULL;
7615 err = got_pathlist_append(added_paths, wt_path, NULL);
7616 if (err)
7617 goto done;
7618 wt_path = NULL;
7621 done:
7622 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7623 free(abspath);
7624 free(wt_path);
7625 return err;
7628 static const struct got_error *
7629 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7630 struct got_object_id *id, const char *path_prefix,
7631 struct got_repository *repo)
7633 const struct got_error *err;
7634 struct got_commit_object *commit = NULL;
7635 struct got_object_qid *pid;
7637 err = got_object_open_as_commit(&commit, repo, id);
7638 if (err)
7639 goto done;
7641 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7643 err = get_paths_added_between_commits(added_paths,
7644 pid ? &pid->id : NULL, id, path_prefix, repo);
7645 if (err)
7646 goto done;
7647 done:
7648 if (commit)
7649 got_object_commit_close(commit);
7650 return err;
7653 const struct got_error *
7654 got_worktree_rebase_abort(struct got_worktree *worktree,
7655 struct got_fileindex *fileindex, struct got_repository *repo,
7656 struct got_reference *new_base_branch,
7657 got_worktree_checkout_cb progress_cb, void *progress_arg)
7659 const struct got_error *err, *unlockerr, *sync_err;
7660 struct got_reference *resolved = NULL;
7661 struct got_object_id *commit_id = NULL;
7662 struct got_object_id *merged_commit_id = NULL;
7663 struct got_commit_object *commit = NULL;
7664 char *fileindex_path = NULL;
7665 char *commit_ref_name = NULL;
7666 struct got_reference *commit_ref = NULL;
7667 struct revert_file_args rfa;
7668 struct got_object_id *tree_id = NULL;
7669 struct got_pathlist_head added_paths;
7671 TAILQ_INIT(&added_paths);
7673 err = lock_worktree(worktree, LOCK_EX);
7674 if (err)
7675 return err;
7677 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7678 if (err)
7679 goto done;
7681 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7682 if (err)
7683 goto done;
7685 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7686 if (err)
7687 goto done;
7690 * Determine which files in added status can be safely removed
7691 * from disk while reverting changes in the work tree.
7692 * We want to avoid deleting unrelated files which were added by
7693 * the user for conflict resolution purposes.
7695 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7696 got_worktree_get_path_prefix(worktree), repo);
7697 if (err)
7698 goto done;
7700 err = got_ref_open(&resolved, repo,
7701 got_ref_get_symref_target(new_base_branch), 0);
7702 if (err)
7703 goto done;
7705 err = got_worktree_set_head_ref(worktree, resolved);
7706 if (err)
7707 goto done;
7710 * XXX commits to the base branch could have happened while
7711 * we were busy rebasing; should we store the original commit ID
7712 * when rebase begins and read it back here?
7714 err = got_ref_resolve(&commit_id, repo, resolved);
7715 if (err)
7716 goto done;
7718 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7719 if (err)
7720 goto done;
7722 err = got_object_open_as_commit(&commit, repo,
7723 worktree->base_commit_id);
7724 if (err)
7725 goto done;
7727 err = got_object_id_by_path(&tree_id, repo, commit,
7728 worktree->path_prefix);
7729 if (err)
7730 goto done;
7732 err = delete_rebase_refs(worktree, repo);
7733 if (err)
7734 goto done;
7736 err = get_fileindex_path(&fileindex_path, worktree);
7737 if (err)
7738 goto done;
7740 rfa.worktree = worktree;
7741 rfa.fileindex = fileindex;
7742 rfa.progress_cb = progress_cb;
7743 rfa.progress_arg = progress_arg;
7744 rfa.patch_cb = NULL;
7745 rfa.patch_arg = NULL;
7746 rfa.repo = repo;
7747 rfa.unlink_added_files = 1;
7748 rfa.added_files_to_unlink = &added_paths;
7749 err = worktree_status(worktree, "", fileindex, repo,
7750 revert_file, &rfa, NULL, NULL, 1, 0);
7751 if (err)
7752 goto sync;
7754 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7755 repo, progress_cb, progress_arg, NULL, NULL);
7756 sync:
7757 sync_err = sync_fileindex(fileindex, fileindex_path);
7758 if (sync_err && err == NULL)
7759 err = sync_err;
7760 done:
7761 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7762 got_ref_close(resolved);
7763 free(tree_id);
7764 free(commit_id);
7765 free(merged_commit_id);
7766 if (commit)
7767 got_object_commit_close(commit);
7768 if (fileindex)
7769 got_fileindex_free(fileindex);
7770 free(fileindex_path);
7771 free(commit_ref_name);
7772 if (commit_ref)
7773 got_ref_close(commit_ref);
7775 unlockerr = lock_worktree(worktree, LOCK_SH);
7776 if (unlockerr && err == NULL)
7777 err = unlockerr;
7778 return err;
7781 const struct got_error *
7782 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7783 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7784 struct got_fileindex **fileindex, struct got_worktree *worktree,
7785 struct got_repository *repo)
7787 const struct got_error *err = NULL;
7788 char *tmp_branch_name = NULL;
7789 char *branch_ref_name = NULL;
7790 char *base_commit_ref_name = NULL;
7791 char *fileindex_path = NULL;
7792 struct check_rebase_ok_arg ok_arg;
7793 struct got_reference *wt_branch = NULL;
7794 struct got_reference *base_commit_ref = NULL;
7796 *tmp_branch = NULL;
7797 *branch_ref = NULL;
7798 *base_commit_id = NULL;
7799 *fileindex = NULL;
7801 err = lock_worktree(worktree, LOCK_EX);
7802 if (err)
7803 return err;
7805 err = open_fileindex(fileindex, &fileindex_path, worktree);
7806 if (err)
7807 goto done;
7809 ok_arg.worktree = worktree;
7810 ok_arg.repo = repo;
7811 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7812 &ok_arg);
7813 if (err)
7814 goto done;
7816 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7817 if (err)
7818 goto done;
7820 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7821 if (err)
7822 goto done;
7824 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7825 worktree);
7826 if (err)
7827 goto done;
7829 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7830 0);
7831 if (err)
7832 goto done;
7834 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7835 if (err)
7836 goto done;
7838 err = got_ref_write(*branch_ref, repo);
7839 if (err)
7840 goto done;
7842 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7843 worktree->base_commit_id);
7844 if (err)
7845 goto done;
7846 err = got_ref_write(base_commit_ref, repo);
7847 if (err)
7848 goto done;
7849 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7850 if (*base_commit_id == NULL) {
7851 err = got_error_from_errno("got_object_id_dup");
7852 goto done;
7855 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7856 worktree->base_commit_id);
7857 if (err)
7858 goto done;
7859 err = got_ref_write(*tmp_branch, repo);
7860 if (err)
7861 goto done;
7863 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7864 if (err)
7865 goto done;
7866 done:
7867 free(fileindex_path);
7868 free(tmp_branch_name);
7869 free(branch_ref_name);
7870 free(base_commit_ref_name);
7871 if (wt_branch)
7872 got_ref_close(wt_branch);
7873 if (err) {
7874 if (*branch_ref) {
7875 got_ref_close(*branch_ref);
7876 *branch_ref = NULL;
7878 if (*tmp_branch) {
7879 got_ref_close(*tmp_branch);
7880 *tmp_branch = NULL;
7882 free(*base_commit_id);
7883 if (*fileindex) {
7884 got_fileindex_free(*fileindex);
7885 *fileindex = NULL;
7887 lock_worktree(worktree, LOCK_SH);
7889 return err;
7892 const struct got_error *
7893 got_worktree_histedit_postpone(struct got_worktree *worktree,
7894 struct got_fileindex *fileindex)
7896 if (fileindex)
7897 got_fileindex_free(fileindex);
7898 return lock_worktree(worktree, LOCK_SH);
7901 const struct got_error *
7902 got_worktree_histedit_in_progress(int *in_progress,
7903 struct got_worktree *worktree)
7905 const struct got_error *err;
7906 char *tmp_branch_name = NULL;
7908 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7909 if (err)
7910 return err;
7912 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7913 free(tmp_branch_name);
7914 return NULL;
7917 const struct got_error *
7918 got_worktree_histedit_continue(struct got_object_id **commit_id,
7919 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7920 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7921 struct got_worktree *worktree, struct got_repository *repo)
7923 const struct got_error *err;
7924 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7925 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7926 struct got_reference *commit_ref = NULL;
7927 struct got_reference *base_commit_ref = NULL;
7928 char *fileindex_path = NULL;
7929 int have_staged_files = 0;
7931 *commit_id = NULL;
7932 *tmp_branch = NULL;
7933 *base_commit_id = NULL;
7934 *fileindex = NULL;
7936 err = lock_worktree(worktree, LOCK_EX);
7937 if (err)
7938 return err;
7940 err = open_fileindex(fileindex, &fileindex_path, worktree);
7941 if (err)
7942 goto done;
7944 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7945 &have_staged_files);
7946 if (err && err->code != GOT_ERR_CANCELLED)
7947 goto done;
7948 if (have_staged_files) {
7949 err = got_error(GOT_ERR_STAGED_PATHS);
7950 goto done;
7953 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7954 if (err)
7955 goto done;
7957 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7958 if (err)
7959 goto done;
7961 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7962 if (err)
7963 goto done;
7965 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7966 worktree);
7967 if (err)
7968 goto done;
7970 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7971 if (err)
7972 goto done;
7974 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7975 if (err)
7976 goto done;
7977 err = got_ref_resolve(commit_id, repo, commit_ref);
7978 if (err)
7979 goto done;
7981 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7982 if (err)
7983 goto done;
7984 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7985 if (err)
7986 goto done;
7988 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7989 if (err)
7990 goto done;
7991 done:
7992 free(commit_ref_name);
7993 free(branch_ref_name);
7994 free(fileindex_path);
7995 if (commit_ref)
7996 got_ref_close(commit_ref);
7997 if (base_commit_ref)
7998 got_ref_close(base_commit_ref);
7999 if (err) {
8000 free(*commit_id);
8001 *commit_id = NULL;
8002 free(*base_commit_id);
8003 *base_commit_id = NULL;
8004 if (*tmp_branch) {
8005 got_ref_close(*tmp_branch);
8006 *tmp_branch = NULL;
8008 if (*fileindex) {
8009 got_fileindex_free(*fileindex);
8010 *fileindex = NULL;
8012 lock_worktree(worktree, LOCK_EX);
8014 return err;
8017 static const struct got_error *
8018 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8020 const struct got_error *err;
8021 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8022 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8024 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8025 if (err)
8026 goto done;
8027 err = delete_ref(tmp_branch_name, repo);
8028 if (err)
8029 goto done;
8031 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8032 worktree);
8033 if (err)
8034 goto done;
8035 err = delete_ref(base_commit_ref_name, repo);
8036 if (err)
8037 goto done;
8039 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8040 if (err)
8041 goto done;
8042 err = delete_ref(branch_ref_name, repo);
8043 if (err)
8044 goto done;
8046 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8047 if (err)
8048 goto done;
8049 err = delete_ref(commit_ref_name, repo);
8050 if (err)
8051 goto done;
8052 done:
8053 free(tmp_branch_name);
8054 free(base_commit_ref_name);
8055 free(branch_ref_name);
8056 free(commit_ref_name);
8057 return err;
8060 const struct got_error *
8061 got_worktree_histedit_abort(struct got_worktree *worktree,
8062 struct got_fileindex *fileindex, struct got_repository *repo,
8063 struct got_reference *branch, struct got_object_id *base_commit_id,
8064 got_worktree_checkout_cb progress_cb, void *progress_arg)
8066 const struct got_error *err, *unlockerr, *sync_err;
8067 struct got_reference *resolved = NULL;
8068 char *fileindex_path = NULL;
8069 struct got_object_id *merged_commit_id = NULL;
8070 struct got_commit_object *commit = NULL;
8071 char *commit_ref_name = NULL;
8072 struct got_reference *commit_ref = NULL;
8073 struct got_object_id *tree_id = NULL;
8074 struct revert_file_args rfa;
8075 struct got_pathlist_head added_paths;
8077 TAILQ_INIT(&added_paths);
8079 err = lock_worktree(worktree, LOCK_EX);
8080 if (err)
8081 return err;
8083 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8084 if (err)
8085 goto done;
8087 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8088 if (err) {
8089 if (err->code != GOT_ERR_NOT_REF)
8090 goto done;
8091 /* Can happen on early abort due to invalid histedit script. */
8092 commit_ref = NULL;
8095 if (commit_ref) {
8096 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8097 if (err)
8098 goto done;
8101 * Determine which files in added status can be safely removed
8102 * from disk while reverting changes in the work tree.
8103 * We want to avoid deleting unrelated files added by the
8104 * user during conflict resolution or during histedit -e.
8106 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8107 got_worktree_get_path_prefix(worktree), repo);
8108 if (err)
8109 goto done;
8112 err = got_ref_open(&resolved, repo,
8113 got_ref_get_symref_target(branch), 0);
8114 if (err)
8115 goto done;
8117 err = got_worktree_set_head_ref(worktree, resolved);
8118 if (err)
8119 goto done;
8121 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8122 if (err)
8123 goto done;
8125 err = got_object_open_as_commit(&commit, repo,
8126 worktree->base_commit_id);
8127 if (err)
8128 goto done;
8130 err = got_object_id_by_path(&tree_id, repo, commit,
8131 worktree->path_prefix);
8132 if (err)
8133 goto done;
8135 err = delete_histedit_refs(worktree, repo);
8136 if (err)
8137 goto done;
8139 err = get_fileindex_path(&fileindex_path, worktree);
8140 if (err)
8141 goto done;
8143 rfa.worktree = worktree;
8144 rfa.fileindex = fileindex;
8145 rfa.progress_cb = progress_cb;
8146 rfa.progress_arg = progress_arg;
8147 rfa.patch_cb = NULL;
8148 rfa.patch_arg = NULL;
8149 rfa.repo = repo;
8150 rfa.unlink_added_files = 1;
8151 rfa.added_files_to_unlink = &added_paths;
8152 err = worktree_status(worktree, "", fileindex, repo,
8153 revert_file, &rfa, NULL, NULL, 1, 0);
8154 if (err)
8155 goto sync;
8157 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8158 repo, progress_cb, progress_arg, NULL, NULL);
8159 sync:
8160 sync_err = sync_fileindex(fileindex, fileindex_path);
8161 if (sync_err && err == NULL)
8162 err = sync_err;
8163 done:
8164 if (resolved)
8165 got_ref_close(resolved);
8166 if (commit_ref)
8167 got_ref_close(commit_ref);
8168 free(merged_commit_id);
8169 free(tree_id);
8170 free(fileindex_path);
8171 free(commit_ref_name);
8173 unlockerr = lock_worktree(worktree, LOCK_SH);
8174 if (unlockerr && err == NULL)
8175 err = unlockerr;
8176 return err;
8179 const struct got_error *
8180 got_worktree_histedit_complete(struct got_worktree *worktree,
8181 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8182 struct got_reference *edited_branch, struct got_repository *repo)
8184 const struct got_error *err, *unlockerr, *sync_err;
8185 struct got_object_id *new_head_commit_id = NULL;
8186 struct got_reference *resolved = NULL;
8187 char *fileindex_path = NULL;
8189 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8190 if (err)
8191 return err;
8193 err = got_ref_open(&resolved, repo,
8194 got_ref_get_symref_target(edited_branch), 0);
8195 if (err)
8196 goto done;
8198 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8199 resolved, new_head_commit_id, repo);
8200 if (err)
8201 goto done;
8203 err = got_ref_change_ref(resolved, new_head_commit_id);
8204 if (err)
8205 goto done;
8207 err = got_ref_write(resolved, repo);
8208 if (err)
8209 goto done;
8211 err = got_worktree_set_head_ref(worktree, resolved);
8212 if (err)
8213 goto done;
8215 err = delete_histedit_refs(worktree, repo);
8216 if (err)
8217 goto done;
8219 err = get_fileindex_path(&fileindex_path, worktree);
8220 if (err)
8221 goto done;
8222 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8223 sync_err = sync_fileindex(fileindex, fileindex_path);
8224 if (sync_err && err == NULL)
8225 err = sync_err;
8226 done:
8227 got_fileindex_free(fileindex);
8228 free(fileindex_path);
8229 free(new_head_commit_id);
8230 unlockerr = lock_worktree(worktree, LOCK_SH);
8231 if (unlockerr && err == NULL)
8232 err = unlockerr;
8233 return err;
8236 const struct got_error *
8237 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8238 struct got_object_id *commit_id, struct got_repository *repo)
8240 const struct got_error *err;
8241 char *commit_ref_name;
8243 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8244 if (err)
8245 return err;
8247 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8248 if (err)
8249 goto done;
8251 err = delete_ref(commit_ref_name, repo);
8252 done:
8253 free(commit_ref_name);
8254 return err;
8257 const struct got_error *
8258 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8259 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8260 struct got_worktree *worktree, const char *refname,
8261 struct got_repository *repo)
8263 const struct got_error *err = NULL;
8264 char *fileindex_path = NULL;
8265 struct check_rebase_ok_arg ok_arg;
8267 *fileindex = NULL;
8268 *branch_ref = NULL;
8269 *base_branch_ref = NULL;
8271 err = lock_worktree(worktree, LOCK_EX);
8272 if (err)
8273 return err;
8275 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8276 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8277 "cannot integrate a branch into itself; "
8278 "update -b or different branch name required");
8279 goto done;
8282 err = open_fileindex(fileindex, &fileindex_path, worktree);
8283 if (err)
8284 goto done;
8286 /* Preconditions are the same as for rebase. */
8287 ok_arg.worktree = worktree;
8288 ok_arg.repo = repo;
8289 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8290 &ok_arg);
8291 if (err)
8292 goto done;
8294 err = got_ref_open(branch_ref, repo, refname, 1);
8295 if (err)
8296 goto done;
8298 err = got_ref_open(base_branch_ref, repo,
8299 got_worktree_get_head_ref_name(worktree), 1);
8300 done:
8301 if (err) {
8302 if (*branch_ref) {
8303 got_ref_close(*branch_ref);
8304 *branch_ref = NULL;
8306 if (*base_branch_ref) {
8307 got_ref_close(*base_branch_ref);
8308 *base_branch_ref = NULL;
8310 if (*fileindex) {
8311 got_fileindex_free(*fileindex);
8312 *fileindex = NULL;
8314 lock_worktree(worktree, LOCK_SH);
8316 return err;
8319 const struct got_error *
8320 got_worktree_integrate_continue(struct got_worktree *worktree,
8321 struct got_fileindex *fileindex, struct got_repository *repo,
8322 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8323 got_worktree_checkout_cb progress_cb, void *progress_arg,
8324 got_cancel_cb cancel_cb, void *cancel_arg)
8326 const struct got_error *err = NULL, *sync_err, *unlockerr;
8327 char *fileindex_path = NULL;
8328 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8329 struct got_commit_object *commit = NULL;
8331 err = get_fileindex_path(&fileindex_path, worktree);
8332 if (err)
8333 goto done;
8335 err = got_ref_resolve(&commit_id, repo, branch_ref);
8336 if (err)
8337 goto done;
8339 err = got_object_open_as_commit(&commit, repo, commit_id);
8340 if (err)
8341 goto done;
8343 err = got_object_id_by_path(&tree_id, repo, commit,
8344 worktree->path_prefix);
8345 if (err)
8346 goto done;
8348 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8349 if (err)
8350 goto done;
8352 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8353 progress_cb, progress_arg, cancel_cb, cancel_arg);
8354 if (err)
8355 goto sync;
8357 err = got_ref_change_ref(base_branch_ref, commit_id);
8358 if (err)
8359 goto sync;
8361 err = got_ref_write(base_branch_ref, repo);
8362 if (err)
8363 goto sync;
8365 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8366 sync:
8367 sync_err = sync_fileindex(fileindex, fileindex_path);
8368 if (sync_err && err == NULL)
8369 err = sync_err;
8371 done:
8372 unlockerr = got_ref_unlock(branch_ref);
8373 if (unlockerr && err == NULL)
8374 err = unlockerr;
8375 got_ref_close(branch_ref);
8377 unlockerr = got_ref_unlock(base_branch_ref);
8378 if (unlockerr && err == NULL)
8379 err = unlockerr;
8380 got_ref_close(base_branch_ref);
8382 got_fileindex_free(fileindex);
8383 free(fileindex_path);
8384 free(tree_id);
8385 if (commit)
8386 got_object_commit_close(commit);
8388 unlockerr = lock_worktree(worktree, LOCK_SH);
8389 if (unlockerr && err == NULL)
8390 err = unlockerr;
8391 return err;
8394 const struct got_error *
8395 got_worktree_integrate_abort(struct got_worktree *worktree,
8396 struct got_fileindex *fileindex, struct got_repository *repo,
8397 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8399 const struct got_error *err = NULL, *unlockerr = NULL;
8401 got_fileindex_free(fileindex);
8403 err = lock_worktree(worktree, LOCK_SH);
8405 unlockerr = got_ref_unlock(branch_ref);
8406 if (unlockerr && err == NULL)
8407 err = unlockerr;
8408 got_ref_close(branch_ref);
8410 unlockerr = got_ref_unlock(base_branch_ref);
8411 if (unlockerr && err == NULL)
8412 err = unlockerr;
8413 got_ref_close(base_branch_ref);
8415 return err;
8418 const struct got_error *
8419 got_worktree_merge_postpone(struct got_worktree *worktree,
8420 struct got_fileindex *fileindex)
8422 const struct got_error *err, *sync_err;
8423 char *fileindex_path = NULL;
8425 err = get_fileindex_path(&fileindex_path, worktree);
8426 if (err)
8427 goto done;
8429 sync_err = sync_fileindex(fileindex, fileindex_path);
8431 err = lock_worktree(worktree, LOCK_SH);
8432 if (sync_err && err == NULL)
8433 err = sync_err;
8434 done:
8435 got_fileindex_free(fileindex);
8436 free(fileindex_path);
8437 return err;
8440 static const struct got_error *
8441 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8443 const struct got_error *err;
8444 char *branch_refname = NULL, *commit_refname = NULL;
8446 err = get_merge_branch_ref_name(&branch_refname, worktree);
8447 if (err)
8448 goto done;
8449 err = delete_ref(branch_refname, repo);
8450 if (err)
8451 goto done;
8453 err = get_merge_commit_ref_name(&commit_refname, worktree);
8454 if (err)
8455 goto done;
8456 err = delete_ref(commit_refname, repo);
8457 if (err)
8458 goto done;
8460 done:
8461 free(branch_refname);
8462 free(commit_refname);
8463 return err;
8466 struct merge_commit_msg_arg {
8467 struct got_worktree *worktree;
8468 const char *branch_name;
8471 static const struct got_error *
8472 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8473 const char *diff_path, char **logmsg, void *arg)
8475 struct merge_commit_msg_arg *a = arg;
8477 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8478 got_worktree_get_head_ref_name(a->worktree)) == -1)
8479 return got_error_from_errno("asprintf");
8481 return NULL;
8485 const struct got_error *
8486 got_worktree_merge_branch(struct got_worktree *worktree,
8487 struct got_fileindex *fileindex,
8488 struct got_object_id *yca_commit_id,
8489 struct got_object_id *branch_tip,
8490 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8491 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8493 const struct got_error *err;
8494 char *fileindex_path = NULL;
8495 struct check_mixed_commits_args cma;
8497 err = get_fileindex_path(&fileindex_path, worktree);
8498 if (err)
8499 goto done;
8501 cma.worktree = worktree;
8502 cma.cancel_cb = cancel_cb;
8503 cma.cancel_arg = cancel_arg;
8505 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8506 &cma);
8507 if (err)
8508 goto done;
8510 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8511 branch_tip, repo, progress_cb, progress_arg,
8512 cancel_cb, cancel_arg);
8513 done:
8514 free(fileindex_path);
8515 return err;
8518 const struct got_error *
8519 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8520 struct got_worktree *worktree, struct got_fileindex *fileindex,
8521 const char *author, const char *committer, int allow_bad_symlinks,
8522 struct got_object_id *branch_tip, const char *branch_name,
8523 int allow_conflict, struct got_repository *repo,
8524 got_worktree_status_cb status_cb, void *status_arg)
8527 const struct got_error *err = NULL, *sync_err;
8528 struct got_pathlist_head commitable_paths;
8529 struct collect_commitables_arg cc_arg;
8530 struct got_pathlist_entry *pe;
8531 struct got_reference *head_ref = NULL;
8532 struct got_object_id *head_commit_id = NULL;
8533 int have_staged_files = 0;
8534 struct merge_commit_msg_arg mcm_arg;
8535 char *fileindex_path = NULL;
8537 memset(&cc_arg, 0, sizeof(cc_arg));
8538 *new_commit_id = NULL;
8540 TAILQ_INIT(&commitable_paths);
8542 err = get_fileindex_path(&fileindex_path, worktree);
8543 if (err)
8544 goto done;
8546 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8547 if (err)
8548 goto done;
8550 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8551 if (err)
8552 goto done;
8554 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8555 &have_staged_files);
8556 if (err && err->code != GOT_ERR_CANCELLED)
8557 goto done;
8558 if (have_staged_files) {
8559 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8560 goto done;
8563 cc_arg.commitable_paths = &commitable_paths;
8564 cc_arg.worktree = worktree;
8565 cc_arg.fileindex = fileindex;
8566 cc_arg.repo = repo;
8567 cc_arg.have_staged_files = have_staged_files;
8568 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8569 cc_arg.commit_conflicts = allow_conflict;
8570 err = worktree_status(worktree, "", fileindex, repo,
8571 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8572 if (err)
8573 goto done;
8575 mcm_arg.worktree = worktree;
8576 mcm_arg.branch_name = branch_name;
8577 err = commit_worktree(new_commit_id, &commitable_paths,
8578 head_commit_id, branch_tip, worktree, author, committer, NULL,
8579 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8580 if (err)
8581 goto done;
8583 err = update_fileindex_after_commit(worktree, &commitable_paths,
8584 *new_commit_id, fileindex, have_staged_files);
8585 sync_err = sync_fileindex(fileindex, fileindex_path);
8586 if (sync_err && err == NULL)
8587 err = sync_err;
8588 done:
8589 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8590 struct got_commitable *ct = pe->data;
8592 free_commitable(ct);
8594 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8595 free(fileindex_path);
8596 return err;
8599 const struct got_error *
8600 got_worktree_merge_complete(struct got_worktree *worktree,
8601 struct got_fileindex *fileindex, struct got_repository *repo)
8603 const struct got_error *err, *unlockerr, *sync_err;
8604 char *fileindex_path = NULL;
8606 err = delete_merge_refs(worktree, repo);
8607 if (err)
8608 goto done;
8610 err = get_fileindex_path(&fileindex_path, worktree);
8611 if (err)
8612 goto done;
8613 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8614 sync_err = sync_fileindex(fileindex, fileindex_path);
8615 if (sync_err && err == NULL)
8616 err = sync_err;
8617 done:
8618 got_fileindex_free(fileindex);
8619 free(fileindex_path);
8620 unlockerr = lock_worktree(worktree, LOCK_SH);
8621 if (unlockerr && err == NULL)
8622 err = unlockerr;
8623 return err;
8626 const struct got_error *
8627 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8628 struct got_repository *repo)
8630 const struct got_error *err;
8631 char *branch_refname = NULL;
8632 struct got_reference *branch_ref = NULL;
8634 *in_progress = 0;
8636 err = get_merge_branch_ref_name(&branch_refname, worktree);
8637 if (err)
8638 return err;
8639 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8640 free(branch_refname);
8641 if (err) {
8642 if (err->code != GOT_ERR_NOT_REF)
8643 return err;
8644 } else
8645 *in_progress = 1;
8647 return NULL;
8650 const struct got_error *got_worktree_merge_prepare(
8651 struct got_fileindex **fileindex, struct got_worktree *worktree,
8652 struct got_repository *repo)
8654 const struct got_error *err = NULL;
8655 char *fileindex_path = NULL;
8656 struct got_reference *wt_branch = NULL;
8657 struct got_object_id *wt_branch_tip = NULL;
8658 struct check_rebase_ok_arg ok_arg;
8660 *fileindex = NULL;
8662 err = lock_worktree(worktree, LOCK_EX);
8663 if (err)
8664 return err;
8666 err = open_fileindex(fileindex, &fileindex_path, worktree);
8667 if (err)
8668 goto done;
8670 /* Preconditions are the same as for rebase. */
8671 ok_arg.worktree = worktree;
8672 ok_arg.repo = repo;
8673 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8674 &ok_arg);
8675 if (err)
8676 goto done;
8678 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8679 0);
8680 if (err)
8681 goto done;
8683 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8684 if (err)
8685 goto done;
8687 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8688 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8689 goto done;
8692 done:
8693 free(fileindex_path);
8694 if (wt_branch)
8695 got_ref_close(wt_branch);
8696 free(wt_branch_tip);
8697 if (err) {
8698 if (*fileindex) {
8699 got_fileindex_free(*fileindex);
8700 *fileindex = NULL;
8702 lock_worktree(worktree, LOCK_SH);
8704 return err;
8707 const struct got_error *got_worktree_merge_write_refs(
8708 struct got_worktree *worktree, struct got_reference *branch,
8709 struct got_repository *repo)
8711 const struct got_error *err = NULL;
8712 char *branch_refname = NULL, *commit_refname = NULL;
8713 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8714 struct got_object_id *branch_tip = NULL;
8716 err = get_merge_branch_ref_name(&branch_refname, worktree);
8717 if (err)
8718 return err;
8720 err = get_merge_commit_ref_name(&commit_refname, worktree);
8721 if (err)
8722 return err;
8724 err = got_ref_resolve(&branch_tip, repo, branch);
8725 if (err)
8726 goto done;
8728 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8729 if (err)
8730 goto done;
8731 err = got_ref_write(branch_ref, repo);
8732 if (err)
8733 goto done;
8735 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8736 if (err)
8737 goto done;
8738 err = got_ref_write(commit_ref, repo);
8739 if (err)
8740 goto done;
8742 done:
8743 free(branch_refname);
8744 free(commit_refname);
8745 if (branch_ref)
8746 got_ref_close(branch_ref);
8747 if (commit_ref)
8748 got_ref_close(commit_ref);
8749 free(branch_tip);
8750 return err;
8753 const struct got_error *
8754 got_worktree_merge_continue(char **branch_name,
8755 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8756 struct got_worktree *worktree, struct got_repository *repo)
8758 const struct got_error *err;
8759 char *commit_refname = NULL, *branch_refname = NULL;
8760 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8761 char *fileindex_path = NULL;
8762 int have_staged_files = 0;
8764 *branch_name = NULL;
8765 *branch_tip = NULL;
8766 *fileindex = NULL;
8768 err = lock_worktree(worktree, LOCK_EX);
8769 if (err)
8770 return err;
8772 err = open_fileindex(fileindex, &fileindex_path, worktree);
8773 if (err)
8774 goto done;
8776 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8777 &have_staged_files);
8778 if (err && err->code != GOT_ERR_CANCELLED)
8779 goto done;
8780 if (have_staged_files) {
8781 err = got_error(GOT_ERR_STAGED_PATHS);
8782 goto done;
8785 err = get_merge_branch_ref_name(&branch_refname, worktree);
8786 if (err)
8787 goto done;
8789 err = get_merge_commit_ref_name(&commit_refname, worktree);
8790 if (err)
8791 goto done;
8793 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8794 if (err)
8795 goto done;
8797 if (!got_ref_is_symbolic(branch_ref)) {
8798 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8799 "%s is not a symbolic reference",
8800 got_ref_get_name(branch_ref));
8801 goto done;
8803 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8804 if (*branch_name == NULL) {
8805 err = got_error_from_errno("strdup");
8806 goto done;
8809 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8810 if (err)
8811 goto done;
8813 err = got_ref_resolve(branch_tip, repo, commit_ref);
8814 if (err)
8815 goto done;
8816 done:
8817 free(commit_refname);
8818 free(branch_refname);
8819 free(fileindex_path);
8820 if (commit_ref)
8821 got_ref_close(commit_ref);
8822 if (branch_ref)
8823 got_ref_close(branch_ref);
8824 if (err) {
8825 if (*branch_name) {
8826 free(*branch_name);
8827 *branch_name = NULL;
8829 free(*branch_tip);
8830 *branch_tip = NULL;
8831 if (*fileindex) {
8832 got_fileindex_free(*fileindex);
8833 *fileindex = NULL;
8835 lock_worktree(worktree, LOCK_SH);
8837 return err;
8840 const struct got_error *
8841 got_worktree_merge_abort(struct got_worktree *worktree,
8842 struct got_fileindex *fileindex, struct got_repository *repo,
8843 got_worktree_checkout_cb progress_cb, void *progress_arg)
8845 const struct got_error *err, *unlockerr, *sync_err;
8846 struct got_commit_object *commit = NULL;
8847 char *fileindex_path = NULL;
8848 struct revert_file_args rfa;
8849 char *commit_ref_name = NULL;
8850 struct got_reference *commit_ref = NULL;
8851 struct got_object_id *merged_commit_id = NULL;
8852 struct got_object_id *tree_id = NULL;
8853 struct got_pathlist_head added_paths;
8855 TAILQ_INIT(&added_paths);
8857 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8858 if (err)
8859 goto done;
8861 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8862 if (err)
8863 goto done;
8865 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8866 if (err)
8867 goto done;
8870 * Determine which files in added status can be safely removed
8871 * from disk while reverting changes in the work tree.
8872 * We want to avoid deleting unrelated files which were added by
8873 * the user for conflict resolution purposes.
8875 err = get_paths_added_between_commits(&added_paths,
8876 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8877 got_worktree_get_path_prefix(worktree), repo);
8878 if (err)
8879 goto done;
8882 err = got_object_open_as_commit(&commit, repo,
8883 worktree->base_commit_id);
8884 if (err)
8885 goto done;
8887 err = got_object_id_by_path(&tree_id, repo, commit,
8888 worktree->path_prefix);
8889 if (err)
8890 goto done;
8892 err = delete_merge_refs(worktree, repo);
8893 if (err)
8894 goto done;
8896 err = get_fileindex_path(&fileindex_path, worktree);
8897 if (err)
8898 goto done;
8900 rfa.worktree = worktree;
8901 rfa.fileindex = fileindex;
8902 rfa.progress_cb = progress_cb;
8903 rfa.progress_arg = progress_arg;
8904 rfa.patch_cb = NULL;
8905 rfa.patch_arg = NULL;
8906 rfa.repo = repo;
8907 rfa.unlink_added_files = 1;
8908 rfa.added_files_to_unlink = &added_paths;
8909 err = worktree_status(worktree, "", fileindex, repo,
8910 revert_file, &rfa, NULL, NULL, 1, 0);
8911 if (err)
8912 goto sync;
8914 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8915 repo, progress_cb, progress_arg, NULL, NULL);
8916 sync:
8917 sync_err = sync_fileindex(fileindex, fileindex_path);
8918 if (sync_err && err == NULL)
8919 err = sync_err;
8920 done:
8921 free(tree_id);
8922 free(merged_commit_id);
8923 if (commit)
8924 got_object_commit_close(commit);
8925 if (fileindex)
8926 got_fileindex_free(fileindex);
8927 free(fileindex_path);
8928 if (commit_ref)
8929 got_ref_close(commit_ref);
8930 free(commit_ref_name);
8932 unlockerr = lock_worktree(worktree, LOCK_SH);
8933 if (unlockerr && err == NULL)
8934 err = unlockerr;
8935 return err;
8938 struct check_stage_ok_arg {
8939 struct got_object_id *head_commit_id;
8940 struct got_worktree *worktree;
8941 struct got_fileindex *fileindex;
8942 struct got_repository *repo;
8943 int have_changes;
8946 static const struct got_error *
8947 check_stage_ok(void *arg, unsigned char status,
8948 unsigned char staged_status, const char *relpath,
8949 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8950 struct got_object_id *commit_id, int dirfd, const char *de_name)
8952 struct check_stage_ok_arg *a = arg;
8953 const struct got_error *err = NULL;
8954 struct got_fileindex_entry *ie;
8955 struct got_object_id base_commit_id;
8956 struct got_object_id *base_commit_idp = NULL;
8957 char *in_repo_path = NULL, *p;
8959 if (status == GOT_STATUS_UNVERSIONED ||
8960 status == GOT_STATUS_NO_CHANGE)
8961 return NULL;
8962 if (status == GOT_STATUS_NONEXISTENT)
8963 return got_error_set_errno(ENOENT, relpath);
8965 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8966 if (ie == NULL)
8967 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8969 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8970 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8971 relpath) == -1)
8972 return got_error_from_errno("asprintf");
8974 if (got_fileindex_entry_has_commit(ie)) {
8975 base_commit_idp = got_fileindex_entry_get_commit_id(
8976 &base_commit_id, ie);
8979 if (status == GOT_STATUS_CONFLICT) {
8980 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8981 goto done;
8982 } else if (status != GOT_STATUS_ADD &&
8983 status != GOT_STATUS_MODIFY &&
8984 status != GOT_STATUS_DELETE) {
8985 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8986 goto done;
8989 a->have_changes = 1;
8991 p = in_repo_path;
8992 while (p[0] == '/')
8993 p++;
8994 err = check_out_of_date(p, status, staged_status,
8995 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8996 GOT_ERR_STAGE_OUT_OF_DATE);
8997 done:
8998 free(in_repo_path);
8999 return err;
9002 struct stage_path_arg {
9003 struct got_worktree *worktree;
9004 struct got_fileindex *fileindex;
9005 struct got_repository *repo;
9006 got_worktree_status_cb status_cb;
9007 void *status_arg;
9008 got_worktree_patch_cb patch_cb;
9009 void *patch_arg;
9010 int staged_something;
9011 int allow_bad_symlinks;
9014 static const struct got_error *
9015 stage_path(void *arg, unsigned char status,
9016 unsigned char staged_status, const char *relpath,
9017 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9018 struct got_object_id *commit_id, int dirfd, const char *de_name)
9020 struct stage_path_arg *a = arg;
9021 const struct got_error *err = NULL;
9022 struct got_fileindex_entry *ie;
9023 char *ondisk_path = NULL, *path_content = NULL;
9024 uint32_t stage;
9025 struct got_object_id *new_staged_blob_id = NULL;
9026 struct stat sb;
9028 if (status == GOT_STATUS_UNVERSIONED)
9029 return NULL;
9031 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9032 if (ie == NULL)
9033 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9035 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9036 relpath)== -1)
9037 return got_error_from_errno("asprintf");
9039 switch (status) {
9040 case GOT_STATUS_ADD:
9041 case GOT_STATUS_MODIFY:
9042 /* XXX could sb.st_mode be passed in by our caller? */
9043 if (lstat(ondisk_path, &sb) == -1) {
9044 err = got_error_from_errno2("lstat", ondisk_path);
9045 break;
9047 if (a->patch_cb) {
9048 if (status == GOT_STATUS_ADD) {
9049 int choice = GOT_PATCH_CHOICE_NONE;
9050 err = (*a->patch_cb)(&choice, a->patch_arg,
9051 status, ie->path, NULL, 1, 1);
9052 if (err)
9053 break;
9054 if (choice != GOT_PATCH_CHOICE_YES)
9055 break;
9056 } else {
9057 err = create_patched_content(&path_content, 0,
9058 staged_blob_id ? staged_blob_id : blob_id,
9059 ondisk_path, dirfd, de_name, ie->path,
9060 a->repo, a->patch_cb, a->patch_arg);
9061 if (err || path_content == NULL)
9062 break;
9065 err = got_object_blob_create(&new_staged_blob_id,
9066 path_content ? path_content : ondisk_path, a->repo);
9067 if (err)
9068 break;
9069 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9070 SHA1_DIGEST_LENGTH);
9071 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9072 stage = GOT_FILEIDX_STAGE_ADD;
9073 else
9074 stage = GOT_FILEIDX_STAGE_MODIFY;
9075 got_fileindex_entry_stage_set(ie, stage);
9076 if (S_ISLNK(sb.st_mode)) {
9077 int is_bad_symlink = 0;
9078 if (!a->allow_bad_symlinks) {
9079 char target_path[PATH_MAX];
9080 ssize_t target_len;
9081 target_len = readlink(ondisk_path, target_path,
9082 sizeof(target_path));
9083 if (target_len == -1) {
9084 err = got_error_from_errno2("readlink",
9085 ondisk_path);
9086 break;
9088 err = is_bad_symlink_target(&is_bad_symlink,
9089 target_path, target_len, ondisk_path,
9090 a->worktree->root_path,
9091 a->worktree->meta_dir);
9092 if (err)
9093 break;
9094 if (is_bad_symlink) {
9095 err = got_error_path(ondisk_path,
9096 GOT_ERR_BAD_SYMLINK);
9097 break;
9100 if (is_bad_symlink)
9101 got_fileindex_entry_staged_filetype_set(ie,
9102 GOT_FILEIDX_MODE_BAD_SYMLINK);
9103 else
9104 got_fileindex_entry_staged_filetype_set(ie,
9105 GOT_FILEIDX_MODE_SYMLINK);
9106 } else {
9107 got_fileindex_entry_staged_filetype_set(ie,
9108 GOT_FILEIDX_MODE_REGULAR_FILE);
9110 a->staged_something = 1;
9111 if (a->status_cb == NULL)
9112 break;
9113 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9114 get_staged_status(ie), relpath, blob_id,
9115 new_staged_blob_id, NULL, dirfd, de_name);
9116 if (err)
9117 break;
9119 * When staging the reverse of the staged diff,
9120 * implicitly unstage the file.
9122 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9123 sizeof(ie->blob_sha1)) == 0) {
9124 got_fileindex_entry_stage_set(ie,
9125 GOT_FILEIDX_STAGE_NONE);
9127 break;
9128 case GOT_STATUS_DELETE:
9129 if (staged_status == GOT_STATUS_DELETE)
9130 break;
9131 if (a->patch_cb) {
9132 int choice = GOT_PATCH_CHOICE_NONE;
9133 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9134 ie->path, NULL, 1, 1);
9135 if (err)
9136 break;
9137 if (choice == GOT_PATCH_CHOICE_NO)
9138 break;
9139 if (choice != GOT_PATCH_CHOICE_YES) {
9140 err = got_error(GOT_ERR_PATCH_CHOICE);
9141 break;
9144 stage = GOT_FILEIDX_STAGE_DELETE;
9145 got_fileindex_entry_stage_set(ie, stage);
9146 a->staged_something = 1;
9147 if (a->status_cb == NULL)
9148 break;
9149 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9150 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9151 de_name);
9152 break;
9153 case GOT_STATUS_NO_CHANGE:
9154 break;
9155 case GOT_STATUS_CONFLICT:
9156 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9157 break;
9158 case GOT_STATUS_NONEXISTENT:
9159 err = got_error_set_errno(ENOENT, relpath);
9160 break;
9161 default:
9162 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9163 break;
9166 if (path_content && unlink(path_content) == -1 && err == NULL)
9167 err = got_error_from_errno2("unlink", path_content);
9168 free(path_content);
9169 free(ondisk_path);
9170 free(new_staged_blob_id);
9171 return err;
9174 const struct got_error *
9175 got_worktree_stage(struct got_worktree *worktree,
9176 struct got_pathlist_head *paths,
9177 got_worktree_status_cb status_cb, void *status_arg,
9178 got_worktree_patch_cb patch_cb, void *patch_arg,
9179 int allow_bad_symlinks, struct got_repository *repo)
9181 const struct got_error *err = NULL, *sync_err, *unlockerr;
9182 struct got_pathlist_entry *pe;
9183 struct got_fileindex *fileindex = NULL;
9184 char *fileindex_path = NULL;
9185 struct got_reference *head_ref = NULL;
9186 struct got_object_id *head_commit_id = NULL;
9187 struct check_stage_ok_arg oka;
9188 struct stage_path_arg spa;
9190 err = lock_worktree(worktree, LOCK_EX);
9191 if (err)
9192 return err;
9194 err = got_ref_open(&head_ref, repo,
9195 got_worktree_get_head_ref_name(worktree), 0);
9196 if (err)
9197 goto done;
9198 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9199 if (err)
9200 goto done;
9201 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9202 if (err)
9203 goto done;
9205 /* Check pre-conditions before staging anything. */
9206 oka.head_commit_id = head_commit_id;
9207 oka.worktree = worktree;
9208 oka.fileindex = fileindex;
9209 oka.repo = repo;
9210 oka.have_changes = 0;
9211 TAILQ_FOREACH(pe, paths, entry) {
9212 err = worktree_status(worktree, pe->path, fileindex, repo,
9213 check_stage_ok, &oka, NULL, NULL, 1, 0);
9214 if (err)
9215 goto done;
9217 if (!oka.have_changes) {
9218 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9219 goto done;
9222 spa.worktree = worktree;
9223 spa.fileindex = fileindex;
9224 spa.repo = repo;
9225 spa.patch_cb = patch_cb;
9226 spa.patch_arg = patch_arg;
9227 spa.status_cb = status_cb;
9228 spa.status_arg = status_arg;
9229 spa.staged_something = 0;
9230 spa.allow_bad_symlinks = allow_bad_symlinks;
9231 TAILQ_FOREACH(pe, paths, entry) {
9232 err = worktree_status(worktree, pe->path, fileindex, repo,
9233 stage_path, &spa, NULL, NULL, 1, 0);
9234 if (err)
9235 goto done;
9237 if (!spa.staged_something) {
9238 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9239 goto done;
9242 sync_err = sync_fileindex(fileindex, fileindex_path);
9243 if (sync_err && err == NULL)
9244 err = sync_err;
9245 done:
9246 if (head_ref)
9247 got_ref_close(head_ref);
9248 free(head_commit_id);
9249 free(fileindex_path);
9250 if (fileindex)
9251 got_fileindex_free(fileindex);
9252 unlockerr = lock_worktree(worktree, LOCK_SH);
9253 if (unlockerr && err == NULL)
9254 err = unlockerr;
9255 return err;
9258 struct unstage_path_arg {
9259 struct got_worktree *worktree;
9260 struct got_fileindex *fileindex;
9261 struct got_repository *repo;
9262 got_worktree_checkout_cb progress_cb;
9263 void *progress_arg;
9264 got_worktree_patch_cb patch_cb;
9265 void *patch_arg;
9268 static const struct got_error *
9269 create_unstaged_content(char **path_unstaged_content,
9270 char **path_new_staged_content, struct got_object_id *blob_id,
9271 struct got_object_id *staged_blob_id, const char *relpath,
9272 struct got_repository *repo,
9273 got_worktree_patch_cb patch_cb, void *patch_arg)
9275 const struct got_error *err, *free_err;
9276 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9277 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9278 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9279 struct got_diffreg_result *diffreg_result = NULL;
9280 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9281 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9282 int fd1 = -1, fd2 = -1;
9284 *path_unstaged_content = NULL;
9285 *path_new_staged_content = NULL;
9287 err = got_object_id_str(&label1, blob_id);
9288 if (err)
9289 return err;
9291 fd1 = got_opentempfd();
9292 if (fd1 == -1) {
9293 err = got_error_from_errno("got_opentempfd");
9294 goto done;
9296 fd2 = got_opentempfd();
9297 if (fd2 == -1) {
9298 err = got_error_from_errno("got_opentempfd");
9299 goto done;
9302 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9303 if (err)
9304 goto done;
9306 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9307 if (err)
9308 goto done;
9310 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9311 if (err)
9312 goto done;
9314 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9315 fd2);
9316 if (err)
9317 goto done;
9319 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9320 if (err)
9321 goto done;
9323 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9324 if (err)
9325 goto done;
9327 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9328 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9329 if (err)
9330 goto done;
9332 err = got_opentemp_named(path_unstaged_content, &outfile,
9333 "got-unstaged-content", "");
9334 if (err)
9335 goto done;
9336 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9337 "got-new-staged-content", "");
9338 if (err)
9339 goto done;
9341 if (fseek(f1, 0L, SEEK_SET) == -1) {
9342 err = got_ferror(f1, GOT_ERR_IO);
9343 goto done;
9345 if (fseek(f2, 0L, SEEK_SET) == -1) {
9346 err = got_ferror(f2, GOT_ERR_IO);
9347 goto done;
9349 /* Count the number of actual changes in the diff result. */
9350 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9351 struct diff_chunk_context cc = {};
9352 diff_chunk_context_load_change(&cc, &nchunks_used,
9353 diffreg_result->result, n, 0);
9354 nchanges++;
9356 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9357 int choice;
9358 err = apply_or_reject_change(&choice, &nchunks_used,
9359 diffreg_result->result, n, relpath, f1, f2,
9360 &line_cur1, &line_cur2,
9361 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9362 if (err)
9363 goto done;
9364 if (choice == GOT_PATCH_CHOICE_YES)
9365 have_content = 1;
9366 else
9367 have_rejected_content = 1;
9368 if (choice == GOT_PATCH_CHOICE_QUIT)
9369 break;
9371 if (have_content || have_rejected_content)
9372 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9373 outfile, rejectfile);
9374 done:
9375 free(label1);
9376 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9377 err = got_error_from_errno("close");
9378 if (blob)
9379 got_object_blob_close(blob);
9380 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9381 err = got_error_from_errno("close");
9382 if (staged_blob)
9383 got_object_blob_close(staged_blob);
9384 free_err = got_diffreg_result_free(diffreg_result);
9385 if (free_err && err == NULL)
9386 err = free_err;
9387 if (f1 && fclose(f1) == EOF && err == NULL)
9388 err = got_error_from_errno2("fclose", path1);
9389 if (f2 && fclose(f2) == EOF && err == NULL)
9390 err = got_error_from_errno2("fclose", path2);
9391 if (outfile && fclose(outfile) == EOF && err == NULL)
9392 err = got_error_from_errno2("fclose", *path_unstaged_content);
9393 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9394 err = got_error_from_errno2("fclose", *path_new_staged_content);
9395 if (path1 && unlink(path1) == -1 && err == NULL)
9396 err = got_error_from_errno2("unlink", path1);
9397 if (path2 && unlink(path2) == -1 && err == NULL)
9398 err = got_error_from_errno2("unlink", path2);
9399 if (err || !have_content) {
9400 if (*path_unstaged_content &&
9401 unlink(*path_unstaged_content) == -1 && err == NULL)
9402 err = got_error_from_errno2("unlink",
9403 *path_unstaged_content);
9404 free(*path_unstaged_content);
9405 *path_unstaged_content = NULL;
9407 if (err || !have_content || !have_rejected_content) {
9408 if (*path_new_staged_content &&
9409 unlink(*path_new_staged_content) == -1 && err == NULL)
9410 err = got_error_from_errno2("unlink",
9411 *path_new_staged_content);
9412 free(*path_new_staged_content);
9413 *path_new_staged_content = NULL;
9415 free(path1);
9416 free(path2);
9417 return err;
9420 static const struct got_error *
9421 unstage_hunks(struct got_object_id *staged_blob_id,
9422 struct got_blob_object *blob_base,
9423 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9424 const char *ondisk_path, const char *label_orig,
9425 struct got_worktree *worktree, struct got_repository *repo,
9426 got_worktree_patch_cb patch_cb, void *patch_arg,
9427 got_worktree_checkout_cb progress_cb, void *progress_arg)
9429 const struct got_error *err = NULL;
9430 char *path_unstaged_content = NULL;
9431 char *path_new_staged_content = NULL;
9432 char *parent = NULL, *base_path = NULL;
9433 char *blob_base_path = NULL;
9434 struct got_object_id *new_staged_blob_id = NULL;
9435 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9436 struct stat sb;
9438 err = create_unstaged_content(&path_unstaged_content,
9439 &path_new_staged_content, blob_id, staged_blob_id,
9440 ie->path, repo, patch_cb, patch_arg);
9441 if (err)
9442 return err;
9444 if (path_unstaged_content == NULL)
9445 return NULL;
9447 if (path_new_staged_content) {
9448 err = got_object_blob_create(&new_staged_blob_id,
9449 path_new_staged_content, repo);
9450 if (err)
9451 goto done;
9454 f = fopen(path_unstaged_content, "re");
9455 if (f == NULL) {
9456 err = got_error_from_errno2("fopen",
9457 path_unstaged_content);
9458 goto done;
9460 if (fstat(fileno(f), &sb) == -1) {
9461 err = got_error_from_errno2("fstat", path_unstaged_content);
9462 goto done;
9464 if (got_fileindex_entry_staged_filetype_get(ie) ==
9465 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9466 char link_target[PATH_MAX];
9467 size_t r;
9468 r = fread(link_target, 1, sizeof(link_target), f);
9469 if (r == 0 && ferror(f)) {
9470 err = got_error_from_errno("fread");
9471 goto done;
9473 if (r >= sizeof(link_target)) { /* should not happen */
9474 err = got_error(GOT_ERR_NO_SPACE);
9475 goto done;
9477 link_target[r] = '\0';
9478 err = merge_symlink(worktree, blob_base,
9479 ondisk_path, ie->path, label_orig, link_target,
9480 worktree->base_commit_id, repo, progress_cb,
9481 progress_arg);
9482 } else {
9483 int local_changes_subsumed;
9485 err = got_path_dirname(&parent, ondisk_path);
9486 if (err)
9487 return err;
9489 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9490 parent) == -1) {
9491 err = got_error_from_errno("asprintf");
9492 base_path = NULL;
9493 goto done;
9496 err = got_opentemp_named(&blob_base_path, &f_base,
9497 base_path, "");
9498 if (err)
9499 goto done;
9500 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9501 blob_base);
9502 if (err)
9503 goto done;
9506 * In order the run a 3-way merge with a symlink we copy the symlink's
9507 * target path into a temporary file and use that file with diff3.
9509 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9510 err = dump_symlink_target_path_to_file(&f_deriv2,
9511 ondisk_path);
9512 if (err)
9513 goto done;
9514 } else {
9515 int fd;
9516 fd = open(ondisk_path,
9517 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9518 if (fd == -1) {
9519 err = got_error_from_errno2("open", ondisk_path);
9520 goto done;
9522 f_deriv2 = fdopen(fd, "r");
9523 if (f_deriv2 == NULL) {
9524 err = got_error_from_errno2("fdopen", ondisk_path);
9525 close(fd);
9526 goto done;
9530 err = merge_file(&local_changes_subsumed, worktree,
9531 f_base, f, f_deriv2, ondisk_path, ie->path,
9532 got_fileindex_perms_to_st(ie),
9533 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9534 repo, progress_cb, progress_arg);
9536 if (err)
9537 goto done;
9539 if (new_staged_blob_id) {
9540 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9541 SHA1_DIGEST_LENGTH);
9542 } else {
9543 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9544 got_fileindex_entry_staged_filetype_set(ie, 0);
9546 done:
9547 free(new_staged_blob_id);
9548 if (path_unstaged_content &&
9549 unlink(path_unstaged_content) == -1 && err == NULL)
9550 err = got_error_from_errno2("unlink", path_unstaged_content);
9551 if (path_new_staged_content &&
9552 unlink(path_new_staged_content) == -1 && err == NULL)
9553 err = got_error_from_errno2("unlink", path_new_staged_content);
9554 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9555 err = got_error_from_errno2("unlink", blob_base_path);
9556 if (f_base && fclose(f_base) == EOF && err == NULL)
9557 err = got_error_from_errno2("fclose", path_unstaged_content);
9558 if (f && fclose(f) == EOF && err == NULL)
9559 err = got_error_from_errno2("fclose", path_unstaged_content);
9560 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9561 err = got_error_from_errno2("fclose", ondisk_path);
9562 free(path_unstaged_content);
9563 free(path_new_staged_content);
9564 free(blob_base_path);
9565 free(parent);
9566 free(base_path);
9567 return err;
9570 static const struct got_error *
9571 unstage_path(void *arg, unsigned char status,
9572 unsigned char staged_status, const char *relpath,
9573 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9574 struct got_object_id *commit_id, int dirfd, const char *de_name)
9576 const struct got_error *err = NULL;
9577 struct unstage_path_arg *a = arg;
9578 struct got_fileindex_entry *ie;
9579 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9580 char *ondisk_path = NULL;
9581 char *id_str = NULL, *label_orig = NULL;
9582 int local_changes_subsumed;
9583 struct stat sb;
9584 int fd1 = -1, fd2 = -1;
9586 if (staged_status != GOT_STATUS_ADD &&
9587 staged_status != GOT_STATUS_MODIFY &&
9588 staged_status != GOT_STATUS_DELETE)
9589 return NULL;
9591 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9592 if (ie == NULL)
9593 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9595 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9596 == -1)
9597 return got_error_from_errno("asprintf");
9599 err = got_object_id_str(&id_str,
9600 commit_id ? commit_id : a->worktree->base_commit_id);
9601 if (err)
9602 goto done;
9603 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9604 id_str) == -1) {
9605 err = got_error_from_errno("asprintf");
9606 goto done;
9609 fd1 = got_opentempfd();
9610 if (fd1 == -1) {
9611 err = got_error_from_errno("got_opentempfd");
9612 goto done;
9614 fd2 = got_opentempfd();
9615 if (fd2 == -1) {
9616 err = got_error_from_errno("got_opentempfd");
9617 goto done;
9620 switch (staged_status) {
9621 case GOT_STATUS_MODIFY:
9622 err = got_object_open_as_blob(&blob_base, a->repo,
9623 blob_id, 8192, fd1);
9624 if (err)
9625 break;
9626 /* fall through */
9627 case GOT_STATUS_ADD:
9628 if (a->patch_cb) {
9629 if (staged_status == GOT_STATUS_ADD) {
9630 int choice = GOT_PATCH_CHOICE_NONE;
9631 err = (*a->patch_cb)(&choice, a->patch_arg,
9632 staged_status, ie->path, NULL, 1, 1);
9633 if (err)
9634 break;
9635 if (choice != GOT_PATCH_CHOICE_YES)
9636 break;
9637 } else {
9638 err = unstage_hunks(staged_blob_id,
9639 blob_base, blob_id, ie, ondisk_path,
9640 label_orig, a->worktree, a->repo,
9641 a->patch_cb, a->patch_arg,
9642 a->progress_cb, a->progress_arg);
9643 break; /* Done with this file. */
9646 err = got_object_open_as_blob(&blob_staged, a->repo,
9647 staged_blob_id, 8192, fd2);
9648 if (err)
9649 break;
9650 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9651 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9652 case GOT_FILEIDX_MODE_REGULAR_FILE:
9653 err = merge_blob(&local_changes_subsumed, a->worktree,
9654 blob_base, ondisk_path, relpath,
9655 got_fileindex_perms_to_st(ie), label_orig,
9656 blob_staged, commit_id ? commit_id :
9657 a->worktree->base_commit_id, a->repo,
9658 a->progress_cb, a->progress_arg);
9659 break;
9660 case GOT_FILEIDX_MODE_SYMLINK:
9661 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9662 char *staged_target;
9663 err = got_object_blob_read_to_str(
9664 &staged_target, blob_staged);
9665 if (err)
9666 goto done;
9667 err = merge_symlink(a->worktree, blob_base,
9668 ondisk_path, relpath, label_orig,
9669 staged_target, commit_id ? commit_id :
9670 a->worktree->base_commit_id,
9671 a->repo, a->progress_cb, a->progress_arg);
9672 free(staged_target);
9673 } else {
9674 err = merge_blob(&local_changes_subsumed,
9675 a->worktree, blob_base, ondisk_path,
9676 relpath, got_fileindex_perms_to_st(ie),
9677 label_orig, blob_staged,
9678 commit_id ? commit_id :
9679 a->worktree->base_commit_id, a->repo,
9680 a->progress_cb, a->progress_arg);
9682 break;
9683 default:
9684 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9685 break;
9687 if (err == NULL) {
9688 got_fileindex_entry_stage_set(ie,
9689 GOT_FILEIDX_STAGE_NONE);
9690 got_fileindex_entry_staged_filetype_set(ie, 0);
9692 break;
9693 case GOT_STATUS_DELETE:
9694 if (a->patch_cb) {
9695 int choice = GOT_PATCH_CHOICE_NONE;
9696 err = (*a->patch_cb)(&choice, a->patch_arg,
9697 staged_status, ie->path, NULL, 1, 1);
9698 if (err)
9699 break;
9700 if (choice == GOT_PATCH_CHOICE_NO)
9701 break;
9702 if (choice != GOT_PATCH_CHOICE_YES) {
9703 err = got_error(GOT_ERR_PATCH_CHOICE);
9704 break;
9707 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9708 got_fileindex_entry_staged_filetype_set(ie, 0);
9709 err = get_file_status(&status, &sb, ie, ondisk_path,
9710 dirfd, de_name, a->repo);
9711 if (err)
9712 break;
9713 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9714 break;
9716 done:
9717 free(ondisk_path);
9718 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9719 err = got_error_from_errno("close");
9720 if (blob_base)
9721 got_object_blob_close(blob_base);
9722 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9723 err = got_error_from_errno("close");
9724 if (blob_staged)
9725 got_object_blob_close(blob_staged);
9726 free(id_str);
9727 free(label_orig);
9728 return err;
9731 const struct got_error *
9732 got_worktree_unstage(struct got_worktree *worktree,
9733 struct got_pathlist_head *paths,
9734 got_worktree_checkout_cb progress_cb, void *progress_arg,
9735 got_worktree_patch_cb patch_cb, void *patch_arg,
9736 struct got_repository *repo)
9738 const struct got_error *err = NULL, *sync_err, *unlockerr;
9739 struct got_pathlist_entry *pe;
9740 struct got_fileindex *fileindex = NULL;
9741 char *fileindex_path = NULL;
9742 struct unstage_path_arg upa;
9744 err = lock_worktree(worktree, LOCK_EX);
9745 if (err)
9746 return err;
9748 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9749 if (err)
9750 goto done;
9752 upa.worktree = worktree;
9753 upa.fileindex = fileindex;
9754 upa.repo = repo;
9755 upa.progress_cb = progress_cb;
9756 upa.progress_arg = progress_arg;
9757 upa.patch_cb = patch_cb;
9758 upa.patch_arg = patch_arg;
9759 TAILQ_FOREACH(pe, paths, entry) {
9760 err = worktree_status(worktree, pe->path, fileindex, repo,
9761 unstage_path, &upa, NULL, NULL, 1, 0);
9762 if (err)
9763 goto done;
9766 sync_err = sync_fileindex(fileindex, fileindex_path);
9767 if (sync_err && err == NULL)
9768 err = sync_err;
9769 done:
9770 free(fileindex_path);
9771 if (fileindex)
9772 got_fileindex_free(fileindex);
9773 unlockerr = lock_worktree(worktree, LOCK_SH);
9774 if (unlockerr && err == NULL)
9775 err = unlockerr;
9776 return err;
9779 struct report_file_info_arg {
9780 struct got_worktree *worktree;
9781 got_worktree_path_info_cb info_cb;
9782 void *info_arg;
9783 struct got_pathlist_head *paths;
9784 got_cancel_cb cancel_cb;
9785 void *cancel_arg;
9788 static const struct got_error *
9789 report_file_info(void *arg, struct got_fileindex_entry *ie)
9791 const struct got_error *err;
9792 struct report_file_info_arg *a = arg;
9793 struct got_pathlist_entry *pe;
9794 struct got_object_id blob_id, staged_blob_id, commit_id;
9795 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9796 struct got_object_id *commit_idp = NULL;
9797 int stage;
9799 if (a->cancel_cb) {
9800 err = a->cancel_cb(a->cancel_arg);
9801 if (err)
9802 return err;
9805 TAILQ_FOREACH(pe, a->paths, entry) {
9806 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9807 got_path_is_child(ie->path, pe->path, pe->path_len))
9808 break;
9810 if (pe == NULL) /* not found */
9811 return NULL;
9813 if (got_fileindex_entry_has_blob(ie))
9814 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9815 stage = got_fileindex_entry_stage_get(ie);
9816 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9817 stage == GOT_FILEIDX_STAGE_ADD) {
9818 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9819 &staged_blob_id, ie);
9822 if (got_fileindex_entry_has_commit(ie))
9823 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9825 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9826 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9829 const struct got_error *
9830 got_worktree_path_info(struct got_worktree *worktree,
9831 struct got_pathlist_head *paths,
9832 got_worktree_path_info_cb info_cb, void *info_arg,
9833 got_cancel_cb cancel_cb, void *cancel_arg)
9836 const struct got_error *err = NULL, *unlockerr;
9837 struct got_fileindex *fileindex = NULL;
9838 char *fileindex_path = NULL;
9839 struct report_file_info_arg arg;
9841 err = lock_worktree(worktree, LOCK_SH);
9842 if (err)
9843 return err;
9845 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9846 if (err)
9847 goto done;
9849 arg.worktree = worktree;
9850 arg.info_cb = info_cb;
9851 arg.info_arg = info_arg;
9852 arg.paths = paths;
9853 arg.cancel_cb = cancel_cb;
9854 arg.cancel_arg = cancel_arg;
9855 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9856 &arg);
9857 done:
9858 free(fileindex_path);
9859 if (fileindex)
9860 got_fileindex_free(fileindex);
9861 unlockerr = lock_worktree(worktree, LOCK_UN);
9862 if (unlockerr && err == NULL)
9863 err = unlockerr;
9864 return err;
9867 static const struct got_error *
9868 patch_check_path(const char *p, char **path, unsigned char *status,
9869 unsigned char *staged_status, struct got_fileindex *fileindex,
9870 struct got_worktree *worktree, struct got_repository *repo)
9872 const struct got_error *err;
9873 struct got_fileindex_entry *ie;
9874 struct stat sb;
9875 char *ondisk_path = NULL;
9877 err = got_worktree_resolve_path(path, worktree, p);
9878 if (err)
9879 return err;
9881 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9882 *path[0] ? "/" : "", *path) == -1)
9883 return got_error_from_errno("asprintf");
9885 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9886 if (ie) {
9887 *staged_status = get_staged_status(ie);
9888 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9889 repo);
9890 if (err)
9891 goto done;
9892 } else {
9893 *staged_status = GOT_STATUS_NO_CHANGE;
9894 *status = GOT_STATUS_UNVERSIONED;
9895 if (lstat(ondisk_path, &sb) == -1) {
9896 if (errno != ENOENT) {
9897 err = got_error_from_errno2("lstat",
9898 ondisk_path);
9899 goto done;
9901 *status = GOT_STATUS_NONEXISTENT;
9905 done:
9906 free(ondisk_path);
9907 return err;
9910 static const struct got_error *
9911 patch_can_rm(const char *path, unsigned char status,
9912 unsigned char staged_status)
9914 if (status == GOT_STATUS_NONEXISTENT)
9915 return got_error_set_errno(ENOENT, path);
9916 if (status != GOT_STATUS_NO_CHANGE &&
9917 status != GOT_STATUS_ADD &&
9918 status != GOT_STATUS_MODIFY &&
9919 status != GOT_STATUS_MODE_CHANGE)
9920 return got_error_path(path, GOT_ERR_FILE_STATUS);
9921 if (staged_status == GOT_STATUS_DELETE)
9922 return got_error_path(path, GOT_ERR_FILE_STATUS);
9923 return NULL;
9926 static const struct got_error *
9927 patch_can_add(const char *path, unsigned char status)
9929 if (status != GOT_STATUS_NONEXISTENT)
9930 return got_error_path(path, GOT_ERR_FILE_STATUS);
9931 return NULL;
9934 static const struct got_error *
9935 patch_can_edit(const char *path, unsigned char status,
9936 unsigned char staged_status)
9938 if (status == GOT_STATUS_NONEXISTENT)
9939 return got_error_set_errno(ENOENT, path);
9940 if (status != GOT_STATUS_NO_CHANGE &&
9941 status != GOT_STATUS_ADD &&
9942 status != GOT_STATUS_MODIFY)
9943 return got_error_path(path, GOT_ERR_FILE_STATUS);
9944 if (staged_status == GOT_STATUS_DELETE)
9945 return got_error_path(path, GOT_ERR_FILE_STATUS);
9946 return NULL;
9949 const struct got_error *
9950 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9951 char **fileindex_path, struct got_worktree *worktree)
9953 return open_fileindex(fileindex, fileindex_path, worktree);
9956 const struct got_error *
9957 got_worktree_patch_check_path(const char *old, const char *new,
9958 char **oldpath, char **newpath, struct got_worktree *worktree,
9959 struct got_repository *repo, struct got_fileindex *fileindex)
9961 const struct got_error *err = NULL;
9962 int file_renamed = 0;
9963 unsigned char status_old, staged_status_old;
9964 unsigned char status_new, staged_status_new;
9966 *oldpath = NULL;
9967 *newpath = NULL;
9969 err = patch_check_path(old != NULL ? old : new, oldpath,
9970 &status_old, &staged_status_old, fileindex, worktree, repo);
9971 if (err)
9972 goto done;
9974 err = patch_check_path(new != NULL ? new : old, newpath,
9975 &status_new, &staged_status_new, fileindex, worktree, repo);
9976 if (err)
9977 goto done;
9979 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9980 file_renamed = 1;
9982 if (old != NULL && new == NULL)
9983 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9984 else if (file_renamed) {
9985 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9986 if (err == NULL)
9987 err = patch_can_add(*newpath, status_new);
9988 } else if (old == NULL)
9989 err = patch_can_add(*newpath, status_new);
9990 else
9991 err = patch_can_edit(*newpath, status_new, staged_status_new);
9993 done:
9994 if (err) {
9995 free(*oldpath);
9996 *oldpath = NULL;
9997 free(*newpath);
9998 *newpath = NULL;
10000 return err;
10003 const struct got_error *
10004 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10005 struct got_worktree *worktree, struct got_fileindex *fileindex,
10006 got_worktree_checkout_cb progress_cb, void *progress_arg)
10008 struct schedule_addition_args saa;
10010 memset(&saa, 0, sizeof(saa));
10011 saa.worktree = worktree;
10012 saa.fileindex = fileindex;
10013 saa.progress_cb = progress_cb;
10014 saa.progress_arg = progress_arg;
10015 saa.repo = repo;
10017 return worktree_status(worktree, path, fileindex, repo,
10018 schedule_addition, &saa, NULL, NULL, 1, 0);
10021 const struct got_error *
10022 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10023 struct got_worktree *worktree, struct got_fileindex *fileindex,
10024 got_worktree_delete_cb progress_cb, void *progress_arg)
10026 const struct got_error *err;
10027 struct schedule_deletion_args sda;
10028 char *ondisk_status_path;
10030 memset(&sda, 0, sizeof(sda));
10031 sda.worktree = worktree;
10032 sda.fileindex = fileindex;
10033 sda.progress_cb = progress_cb;
10034 sda.progress_arg = progress_arg;
10035 sda.repo = repo;
10036 sda.delete_local_mods = 0;
10037 sda.keep_on_disk = 0;
10038 sda.ignore_missing_paths = 0;
10039 sda.status_codes = NULL;
10040 if (asprintf(&ondisk_status_path, "%s/%s",
10041 got_worktree_get_root_path(worktree), path) == -1)
10042 return got_error_from_errno("asprintf");
10043 sda.status_path = ondisk_status_path;
10044 sda.status_path_len = strlen(ondisk_status_path);
10046 err = worktree_status(worktree, path, fileindex, repo,
10047 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10048 free(ondisk_status_path);
10049 return err;
10052 const struct got_error *
10053 got_worktree_patch_complete(struct got_fileindex *fileindex,
10054 const char *fileindex_path)
10056 const struct got_error *err = NULL;
10058 err = sync_fileindex(fileindex, fileindex_path);
10059 got_fileindex_free(fileindex);
10061 return err;