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,
1105 int update_timestamps)
1107 const struct got_error *err = NULL;
1108 struct got_fileindex_entry *new_ie;
1110 *new_iep = NULL;
1112 err = got_fileindex_entry_alloc(&new_ie, path);
1113 if (err)
1114 return err;
1116 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1117 blob_id->sha1, base_commit_id->sha1, update_timestamps);
1118 if (err)
1119 goto done;
1121 err = got_fileindex_entry_add(fileindex, new_ie);
1122 done:
1123 if (err)
1124 got_fileindex_entry_free(new_ie);
1125 else
1126 *new_iep = new_ie;
1127 return err;
1130 static mode_t
1131 get_ondisk_perms(int executable, mode_t st_mode)
1133 mode_t xbits = S_IXUSR;
1135 if (executable) {
1136 /* Map read bits to execute bits. */
1137 if (st_mode & S_IRGRP)
1138 xbits |= S_IXGRP;
1139 if (st_mode & S_IROTH)
1140 xbits |= S_IXOTH;
1141 return st_mode | xbits;
1144 return st_mode;
1147 /* forward declaration */
1148 static const struct got_error *
1149 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1150 const char *path, mode_t te_mode, mode_t st_mode,
1151 struct got_blob_object *blob, int restoring_missing_file,
1152 int reverting_versioned_file, int installing_bad_symlink,
1153 int path_is_unversioned, int *update_timestamps,
1154 struct got_repository *repo,
1155 got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 * This function assumes that the provided symlink target points at a
1159 * safe location in the work tree!
1161 static const struct got_error *
1162 replace_existing_symlink(int *did_something, const char *ondisk_path,
1163 const char *target_path, size_t target_len)
1165 const struct got_error *err = NULL;
1166 ssize_t elen;
1167 char etarget[PATH_MAX];
1168 int fd;
1170 *did_something = 0;
1173 * "Bad" symlinks (those pointing outside the work tree or into the
1174 * .got directory) are installed in the work tree as a regular file
1175 * which contains the bad symlink target path.
1176 * The new symlink target has already been checked for safety by our
1177 * caller. If we can successfully open a regular file then we simply
1178 * replace this file with a symlink below.
1180 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1181 if (fd == -1) {
1182 if (!got_err_open_nofollow_on_symlink())
1183 return got_error_from_errno2("open", ondisk_path);
1185 /* We are updating an existing on-disk symlink. */
1186 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1187 if (elen == -1)
1188 return got_error_from_errno2("readlink", ondisk_path);
1190 if (elen == target_len &&
1191 memcmp(etarget, target_path, target_len) == 0)
1192 return NULL; /* nothing to do */
1195 *did_something = 1;
1196 err = update_symlink(ondisk_path, target_path, target_len);
1197 if (fd != -1 && close(fd) == -1 && err == NULL)
1198 err = got_error_from_errno2("close", ondisk_path);
1199 return err;
1202 static const struct got_error *
1203 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1204 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1205 const char *meta_dir)
1207 const struct got_error *err = NULL;
1208 char canonpath[PATH_MAX];
1209 char *path_got = NULL;
1211 *is_bad_symlink = 0;
1213 if (target_len >= sizeof(canonpath)) {
1214 *is_bad_symlink = 1;
1215 return NULL;
1219 * We do not use realpath(3) to resolve the symlink's target
1220 * path because we don't want to resolve symlinks recursively.
1221 * Instead we make the path absolute and then canonicalize it.
1222 * Relative symlink target lookup should begin at the directory
1223 * in which the blob object is being installed.
1225 if (!got_path_is_absolute(target_path)) {
1226 char *abspath, *parent;
1227 err = got_path_dirname(&parent, ondisk_path);
1228 if (err)
1229 return err;
1230 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 free(parent);
1232 return got_error_from_errno("asprintf");
1234 free(parent);
1235 if (strlen(abspath) >= sizeof(canonpath)) {
1236 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 free(abspath);
1238 return err;
1240 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1241 free(abspath);
1242 if (err)
1243 return err;
1244 } else {
1245 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1246 if (err)
1247 return err;
1250 /* Only allow symlinks pointing at paths within the work tree. */
1251 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1252 *is_bad_symlink = 1;
1253 return NULL;
1256 /* Do not allow symlinks pointing into the meta directory. */
1257 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1258 return got_error_from_errno("asprintf");
1259 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1260 *is_bad_symlink = 1;
1262 free(path_got);
1263 return NULL;
1266 static const struct got_error *
1267 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1268 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1269 int restoring_missing_file, int reverting_versioned_file,
1270 int path_is_unversioned, int allow_bad_symlinks,
1271 struct got_repository *repo,
1272 got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 const struct got_error *err = NULL;
1275 char target_path[PATH_MAX];
1276 size_t len, target_len = 0;
1277 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1278 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1280 *is_bad_symlink = 0;
1283 * Blob object content specifies the target path of the link.
1284 * If a symbolic link cannot be installed we instead create
1285 * a regular file which contains the link target path stored
1286 * in the blob object.
1288 do {
1289 err = got_object_blob_read_block(&len, blob);
1290 if (err)
1291 return err;
1293 if (len + target_len >= sizeof(target_path)) {
1294 /* Path too long; install as a regular file. */
1295 *is_bad_symlink = 1;
1296 got_object_blob_rewind(blob);
1297 return install_blob(worktree, ondisk_path, path,
1298 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 restoring_missing_file, reverting_versioned_file,
1300 1, path_is_unversioned, NULL, repo, progress_cb,
1301 progress_arg);
1303 if (len > 0) {
1304 /* Skip blob object header first time around. */
1305 memcpy(target_path + target_len, buf + hdrlen,
1306 len - hdrlen);
1307 target_len += len - hdrlen;
1308 hdrlen = 0;
1310 } while (len != 0);
1311 target_path[target_len] = '\0';
1313 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1314 ondisk_path, worktree->root_path, worktree->meta_dir);
1315 if (err)
1316 return err;
1318 if (*is_bad_symlink && !allow_bad_symlinks) {
1319 /* install as a regular file */
1320 got_object_blob_rewind(blob);
1321 err = install_blob(worktree, ondisk_path, path,
1322 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 restoring_missing_file, reverting_versioned_file, 1,
1324 path_is_unversioned, NULL, repo,
1325 progress_cb, progress_arg);
1326 return err;
1329 if (symlink(target_path, ondisk_path) == -1) {
1330 if (errno == EEXIST) {
1331 int symlink_replaced;
1332 if (path_is_unversioned) {
1333 err = (*progress_cb)(progress_arg,
1334 GOT_STATUS_UNVERSIONED, path);
1335 return err;
1337 err = replace_existing_symlink(&symlink_replaced,
1338 ondisk_path, target_path, target_len);
1339 if (err)
1340 return err;
1341 if (progress_cb) {
1342 if (symlink_replaced) {
1343 err = (*progress_cb)(progress_arg,
1344 reverting_versioned_file ?
1345 GOT_STATUS_REVERT :
1346 GOT_STATUS_UPDATE, path);
1347 } else {
1348 err = (*progress_cb)(progress_arg,
1349 GOT_STATUS_EXISTS, path);
1352 return err; /* Nothing else to do. */
1355 if (errno == ENOENT) {
1356 char *parent;
1357 err = got_path_dirname(&parent, ondisk_path);
1358 if (err)
1359 return err;
1360 err = got_path_mkdir(parent);
1361 free(parent);
1362 if (err)
1363 return err;
1365 * Retry, and fall through to error handling
1366 * below if this second attempt fails.
1368 if (symlink(target_path, ondisk_path) != -1) {
1369 err = NULL; /* success */
1370 if (progress_cb) {
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1374 path);
1376 return err;
1380 /* Handle errors from first or second creation attempt. */
1381 if (errno == ENAMETOOLONG) {
1382 /* bad target path; install as a regular file */
1383 *is_bad_symlink = 1;
1384 got_object_blob_rewind(blob);
1385 err = install_blob(worktree, ondisk_path, path,
1386 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 restoring_missing_file, reverting_versioned_file, 1,
1388 path_is_unversioned, NULL, repo,
1389 progress_cb, progress_arg);
1390 } else if (errno == ENOTDIR) {
1391 err = got_error_path(ondisk_path,
1392 GOT_ERR_FILE_OBSTRUCTED);
1393 } else {
1394 err = got_error_from_errno3("symlink",
1395 target_path, ondisk_path);
1397 } else if (progress_cb)
1398 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 return err;
1403 static const struct got_error *
1404 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 const char *path, mode_t te_mode, mode_t st_mode,
1406 struct got_blob_object *blob, int restoring_missing_file,
1407 int reverting_versioned_file, int installing_bad_symlink,
1408 int path_is_unversioned, int *update_timestamps,
1409 struct got_repository *repo,
1410 got_worktree_checkout_cb progress_cb, void *progress_arg)
1412 const struct got_error *err = NULL;
1413 int fd = -1;
1414 size_t len, hdrlen;
1415 int update = 0;
1416 char *tmppath = NULL;
1417 mode_t mode;
1419 if (update_timestamps)
1420 *update_timestamps = 1;
1422 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1423 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1424 O_CLOEXEC, mode);
1425 if (fd == -1) {
1426 if (errno == ENOENT || errno == ENOTDIR) {
1427 char *parent;
1428 err = got_path_dirname(&parent, path);
1429 if (err)
1430 return err;
1431 err = add_dir_on_disk(worktree, parent);
1432 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1433 err = got_error_path(path, err->code);
1434 free(parent);
1435 if (err)
1436 return err;
1437 fd = open(ondisk_path,
1438 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1439 mode);
1440 if (fd == -1)
1441 return got_error_from_errno2("open",
1442 ondisk_path);
1443 } else if (errno == EEXIST) {
1444 if (path_is_unversioned) {
1445 if (update_timestamps)
1446 *update_timestamps = 0;
1447 err = (*progress_cb)(progress_arg,
1448 GOT_STATUS_EXISTS, path);
1449 goto done;
1451 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1452 !S_ISREG(st_mode) && !installing_bad_symlink) {
1453 /* TODO file is obstructed; do something */
1454 err = got_error_path(ondisk_path,
1455 GOT_ERR_FILE_OBSTRUCTED);
1456 goto done;
1457 } else {
1458 err = got_opentemp_named_fd(&tmppath, &fd,
1459 ondisk_path, "");
1460 if (err)
1461 goto done;
1462 update = 1;
1464 if (fchmod(fd, apply_umask(mode)) == -1) {
1465 err = got_error_from_errno2("fchmod",
1466 tmppath);
1467 goto done;
1470 } else
1471 return got_error_from_errno2("open", ondisk_path);
1474 if (progress_cb) {
1475 if (restoring_missing_file)
1476 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1477 path);
1478 else if (reverting_versioned_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1480 path);
1481 else
1482 err = (*progress_cb)(progress_arg,
1483 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1484 if (err)
1485 goto done;
1488 hdrlen = got_object_blob_get_hdrlen(blob);
1489 do {
1490 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 err = got_object_blob_read_block(&len, blob);
1492 if (err)
1493 break;
1494 if (len > 0) {
1495 /* Skip blob object header first time around. */
1496 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 if (outlen == -1) {
1498 err = got_error_from_errno("write");
1499 goto done;
1500 } else if (outlen != len - hdrlen) {
1501 err = got_error(GOT_ERR_IO);
1502 goto done;
1504 hdrlen = 0;
1506 } while (len != 0);
1508 if (fsync(fd) != 0) {
1509 err = got_error_from_errno("fsync");
1510 goto done;
1513 if (update) {
1514 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1515 err = got_error_from_errno2("unlink", ondisk_path);
1516 goto done;
1518 if (rename(tmppath, ondisk_path) != 0) {
1519 err = got_error_from_errno3("rename", tmppath,
1520 ondisk_path);
1521 goto done;
1523 free(tmppath);
1524 tmppath = NULL;
1527 done:
1528 if (fd != -1 && close(fd) == -1 && err == NULL)
1529 err = got_error_from_errno("close");
1530 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1531 err = got_error_from_errno2("unlink", tmppath);
1532 free(tmppath);
1533 return err;
1537 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1538 * conflict marker is found in newly added lines only.
1540 static const struct got_error *
1541 get_modified_file_content_status(unsigned char *status,
1542 struct got_blob_object *blob, const char *path, struct stat *sb,
1543 FILE *ondisk_file)
1545 const struct got_error *err, *free_err;
1546 const char *markers[3] = {
1547 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1548 GOT_DIFF_CONFLICT_MARKER_SEP,
1549 GOT_DIFF_CONFLICT_MARKER_END
1551 FILE *f1 = NULL;
1552 struct got_diffreg_result *diffreg_result = NULL;
1553 struct diff_result *r;
1554 int nchunks_parsed, n, i = 0, ln = 0;
1555 char *line = NULL;
1556 size_t linesize = 0;
1557 ssize_t linelen;
1559 if (*status != GOT_STATUS_MODIFY)
1560 return NULL;
1562 f1 = got_opentemp();
1563 if (f1 == NULL)
1564 return got_error_from_errno("got_opentemp");
1566 if (blob) {
1567 got_object_blob_rewind(blob);
1568 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1569 if (err)
1570 goto done;
1573 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1574 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1575 if (err)
1576 goto done;
1578 r = diffreg_result->result;
1580 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1581 struct diff_chunk *c;
1582 struct diff_chunk_context cc = {};
1583 off_t pos;
1586 * We can optimise a little by advancing straight
1587 * to the next chunk if this one has no added lines.
1589 c = diff_chunk_get(r, n);
1591 if (diff_chunk_type(c) != CHUNK_PLUS) {
1592 nchunks_parsed = 1;
1593 continue; /* removed or unchanged lines */
1596 pos = diff_chunk_get_right_start_pos(c);
1597 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1598 err = got_ferror(ondisk_file, GOT_ERR_IO);
1599 goto done;
1602 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1603 ln = cc.right.start;
1605 while (ln < cc.right.end) {
1606 linelen = getline(&line, &linesize, ondisk_file);
1607 if (linelen == -1) {
1608 if (feof(ondisk_file))
1609 break;
1610 err = got_ferror(ondisk_file, GOT_ERR_IO);
1611 break;
1614 if (line && strncmp(line, markers[i],
1615 strlen(markers[i])) == 0) {
1616 if (strcmp(markers[i],
1617 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1618 *status = GOT_STATUS_CONFLICT;
1619 goto done;
1620 } else
1621 i++;
1623 ++ln;
1627 done:
1628 free(line);
1629 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1630 err = got_error_from_errno("fclose");
1631 free_err = got_diffreg_result_free(diffreg_result);
1632 if (err == NULL)
1633 err = free_err;
1635 return err;
1638 static int
1639 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1641 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1642 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1645 static int
1646 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1648 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1649 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1650 ie->mtime_sec == sb->st_mtim.tv_sec &&
1651 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1652 ie->size == (sb->st_size & 0xffffffff) &&
1653 !xbit_differs(ie, sb->st_mode));
1656 static unsigned char
1657 get_staged_status(struct got_fileindex_entry *ie)
1659 switch (got_fileindex_entry_stage_get(ie)) {
1660 case GOT_FILEIDX_STAGE_ADD:
1661 return GOT_STATUS_ADD;
1662 case GOT_FILEIDX_STAGE_DELETE:
1663 return GOT_STATUS_DELETE;
1664 case GOT_FILEIDX_STAGE_MODIFY:
1665 return GOT_STATUS_MODIFY;
1666 default:
1667 return GOT_STATUS_NO_CHANGE;
1671 static const struct got_error *
1672 get_symlink_modification_status(unsigned char *status,
1673 struct got_fileindex_entry *ie, const char *abspath,
1674 int dirfd, const char *de_name, struct got_blob_object *blob)
1676 const struct got_error *err = NULL;
1677 char target_path[PATH_MAX];
1678 char etarget[PATH_MAX];
1679 ssize_t elen;
1680 size_t len, target_len = 0;
1681 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1682 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1684 *status = GOT_STATUS_NO_CHANGE;
1686 /* Blob object content specifies the target path of the link. */
1687 do {
1688 err = got_object_blob_read_block(&len, blob);
1689 if (err)
1690 return err;
1691 if (len + target_len >= sizeof(target_path)) {
1693 * Should not happen. The blob contents were OK
1694 * when this symlink was installed.
1696 return got_error(GOT_ERR_NO_SPACE);
1698 if (len > 0) {
1699 /* Skip blob object header first time around. */
1700 memcpy(target_path + target_len, buf + hdrlen,
1701 len - hdrlen);
1702 target_len += len - hdrlen;
1703 hdrlen = 0;
1705 } while (len != 0);
1706 target_path[target_len] = '\0';
1708 if (dirfd != -1) {
1709 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1710 if (elen == -1)
1711 return got_error_from_errno2("readlinkat", abspath);
1712 } else {
1713 elen = readlink(abspath, etarget, sizeof(etarget));
1714 if (elen == -1)
1715 return got_error_from_errno2("readlink", abspath);
1718 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1719 *status = GOT_STATUS_MODIFY;
1721 return NULL;
1724 static const struct got_error *
1725 get_file_status(unsigned char *status, struct stat *sb,
1726 struct got_fileindex_entry *ie, const char *abspath,
1727 int dirfd, const char *de_name, struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 struct got_object_id id;
1731 size_t hdrlen;
1732 int fd = -1, fd1 = -1;
1733 FILE *f = NULL;
1734 uint8_t fbuf[8192];
1735 struct got_blob_object *blob = NULL;
1736 size_t flen, blen;
1737 unsigned char staged_status;
1739 staged_status = get_staged_status(ie);
1740 *status = GOT_STATUS_NO_CHANGE;
1741 memset(sb, 0, sizeof(*sb));
1744 * Whenever the caller provides a directory descriptor and a
1745 * directory entry name for the file, use them! This prevents
1746 * race conditions if filesystem paths change beneath our feet.
1748 if (dirfd != -1) {
1749 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1750 if (errno == ENOENT) {
1751 if (got_fileindex_entry_has_file_on_disk(ie))
1752 *status = GOT_STATUS_MISSING;
1753 else
1754 *status = GOT_STATUS_DELETE;
1755 goto done;
1757 err = got_error_from_errno2("fstatat", abspath);
1758 goto done;
1760 } else {
1761 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1762 if (fd == -1 && errno != ENOENT &&
1763 !got_err_open_nofollow_on_symlink())
1764 return got_error_from_errno2("open", abspath);
1765 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1766 if (lstat(abspath, sb) == -1)
1767 return got_error_from_errno2("lstat", abspath);
1768 } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 if (errno == ENOENT) {
1770 if (got_fileindex_entry_has_file_on_disk(ie))
1771 *status = GOT_STATUS_MISSING;
1772 else
1773 *status = GOT_STATUS_DELETE;
1774 goto done;
1776 err = got_error_from_errno2("fstat", abspath);
1777 goto done;
1781 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 *status = GOT_STATUS_OBSTRUCTED;
1783 goto done;
1786 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 *status = GOT_STATUS_DELETE;
1788 goto done;
1789 } else if (!got_fileindex_entry_has_blob(ie) &&
1790 staged_status != GOT_STATUS_ADD) {
1791 *status = GOT_STATUS_ADD;
1792 goto done;
1795 if (!stat_info_differs(ie, sb))
1796 goto done;
1798 if (S_ISLNK(sb->st_mode) &&
1799 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 *status = GOT_STATUS_MODIFY;
1801 goto done;
1804 if (staged_status == GOT_STATUS_MODIFY ||
1805 staged_status == GOT_STATUS_ADD)
1806 got_fileindex_entry_get_staged_blob_id(&id, ie);
1807 else
1808 got_fileindex_entry_get_blob_id(&id, ie);
1810 fd1 = got_opentempfd();
1811 if (fd1 == -1) {
1812 err = got_error_from_errno("got_opentempfd");
1813 goto done;
1815 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1816 if (err)
1817 goto done;
1819 if (S_ISLNK(sb->st_mode)) {
1820 err = get_symlink_modification_status(status, ie,
1821 abspath, dirfd, de_name, blob);
1822 goto done;
1825 if (dirfd != -1) {
1826 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1827 if (fd == -1) {
1828 err = got_error_from_errno2("openat", abspath);
1829 goto done;
1833 f = fdopen(fd, "r");
1834 if (f == NULL) {
1835 err = got_error_from_errno2("fdopen", abspath);
1836 goto done;
1838 fd = -1;
1839 hdrlen = got_object_blob_get_hdrlen(blob);
1840 for (;;) {
1841 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1842 err = got_object_blob_read_block(&blen, blob);
1843 if (err)
1844 goto done;
1845 /* Skip length of blob object header first time around. */
1846 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1847 if (flen == 0 && ferror(f)) {
1848 err = got_error_from_errno("fread");
1849 goto done;
1851 if (blen - hdrlen == 0) {
1852 if (flen != 0)
1853 *status = GOT_STATUS_MODIFY;
1854 break;
1855 } else if (flen == 0) {
1856 if (blen - hdrlen != 0)
1857 *status = GOT_STATUS_MODIFY;
1858 break;
1859 } else if (blen - hdrlen == flen) {
1860 /* Skip blob object header first time around. */
1861 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1862 *status = GOT_STATUS_MODIFY;
1863 break;
1865 } else {
1866 *status = GOT_STATUS_MODIFY;
1867 break;
1869 hdrlen = 0;
1872 if (*status == GOT_STATUS_MODIFY) {
1873 rewind(f);
1874 err = get_modified_file_content_status(status, blob, ie->path,
1875 sb, f);
1876 } else if (xbit_differs(ie, sb->st_mode))
1877 *status = GOT_STATUS_MODE_CHANGE;
1878 done:
1879 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1880 err = got_error_from_errno("close");
1881 if (blob)
1882 got_object_blob_close(blob);
1883 if (f != NULL && fclose(f) == EOF && err == NULL)
1884 err = got_error_from_errno2("fclose", abspath);
1885 if (fd != -1 && close(fd) == -1 && err == NULL)
1886 err = got_error_from_errno2("close", abspath);
1887 return err;
1891 * Update timestamps in the file index if a file is unmodified and
1892 * we had to run a full content comparison to find out.
1894 static const struct got_error *
1895 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1896 struct got_fileindex_entry *ie, struct stat *sb)
1898 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1899 return got_fileindex_entry_update(ie, wt_fd, path,
1900 ie->blob_sha1, ie->commit_sha1, 1);
1902 return NULL;
1905 static const struct got_error *remove_ondisk_file(const char *, const char *);
1907 static const struct got_error *
1908 update_blob(struct got_worktree *worktree,
1909 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 struct got_tree_entry *te, const char *path,
1911 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1912 void *progress_arg)
1914 const struct got_error *err = NULL;
1915 struct got_blob_object *blob = NULL;
1916 char *ondisk_path = NULL;
1917 unsigned char status = GOT_STATUS_NO_CHANGE;
1918 struct stat sb;
1919 int fd1 = -1, fd2 = -1;
1920 int update_timestamps = 0;
1922 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1923 return got_error_from_errno("asprintf");
1925 if (ie) {
1926 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1927 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1928 goto done;
1930 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1931 repo);
1932 if (err)
1933 goto done;
1934 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1935 sb.st_mode = got_fileindex_perms_to_st(ie);
1936 } else {
1937 if (stat(ondisk_path, &sb) == -1) {
1938 if (errno != ENOENT && errno != ENOTDIR) {
1939 err = got_error_from_errno2("stat",
1940 ondisk_path);
1941 goto done;
1943 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 status = GOT_STATUS_UNVERSIONED;
1945 } else {
1946 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1947 status = GOT_STATUS_UNVERSIONED;
1948 else
1949 status = GOT_STATUS_OBSTRUCTED;
1953 if (status == GOT_STATUS_OBSTRUCTED) {
1954 if (ie)
1955 got_fileindex_entry_mark_skipped(ie);
1956 err = (*progress_cb)(progress_arg, status, path);
1957 goto done;
1959 if (status == GOT_STATUS_CONFLICT) {
1960 if (ie)
1961 got_fileindex_entry_mark_skipped(ie);
1962 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1963 path);
1964 goto done;
1967 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1968 if (status == GOT_STATUS_UNVERSIONED) {
1969 err = (*progress_cb)(progress_arg, status, path);
1970 } else if (status != GOT_STATUS_NO_CHANGE &&
1971 status != GOT_STATUS_DELETE &&
1972 status != GOT_STATUS_NONEXISTENT &&
1973 status != GOT_STATUS_MISSING) {
1974 err = (*progress_cb)(progress_arg,
1975 GOT_STATUS_CANNOT_DELETE, path);
1976 } else if (ie) {
1977 if (status != GOT_STATUS_DELETE &&
1978 status != GOT_STATUS_NONEXISTENT &&
1979 status != GOT_STATUS_MISSING) {
1980 err = remove_ondisk_file(worktree->root_path,
1981 ie->path);
1982 if (err && !(err->code == GOT_ERR_ERRNO &&
1983 errno == ENOENT))
1984 goto done;
1986 got_fileindex_entry_remove(fileindex, ie);
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1988 ie->path);
1990 goto done; /* nothing else to do */
1993 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1994 (S_ISLNK(te->mode) ||
1995 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1997 * This is a regular file or an installed bad symlink.
1998 * If the file index indicates that this file is already
1999 * up-to-date with respect to the repository we can skip
2000 * updating contents of this file.
2002 if (got_fileindex_entry_has_commit(ie) &&
2003 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2004 SHA1_DIGEST_LENGTH) == 0) {
2005 /* Same commit. */
2006 err = sync_timestamps(worktree->root_fd,
2007 path, status, ie, &sb);
2008 if (err)
2009 goto done;
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2011 path);
2012 goto done;
2014 if (got_fileindex_entry_has_blob(ie) &&
2015 memcmp(ie->blob_sha1, te->id.sha1,
2016 SHA1_DIGEST_LENGTH) == 0) {
2017 /* Different commit but the same blob. */
2018 if (got_fileindex_entry_has_commit(ie)) {
2019 /* Update the base commit ID of this file. */
2020 memcpy(ie->commit_sha1,
2021 worktree->base_commit_id->sha1,
2022 sizeof(ie->commit_sha1));
2024 err = sync_timestamps(worktree->root_fd,
2025 path, status, ie, &sb);
2026 if (err)
2027 goto done;
2028 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2029 path);
2030 goto done;
2034 fd1 = got_opentempfd();
2035 if (fd1 == -1) {
2036 err = got_error_from_errno("got_opentempfd");
2037 goto done;
2039 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2040 if (err)
2041 goto done;
2043 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2044 struct got_blob_object *blob2 = NULL;
2045 char *label_orig = NULL;
2046 if (got_fileindex_entry_has_blob(ie)) {
2047 fd2 = got_opentempfd();
2048 if (fd2 == -1) {
2049 err = got_error_from_errno("got_opentempfd");
2050 goto done;
2052 struct got_object_id id2;
2053 got_fileindex_entry_get_blob_id(&id2, ie);
2054 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2055 fd2);
2056 if (err)
2057 goto done;
2059 if (got_fileindex_entry_has_commit(ie)) {
2060 char id_str[SHA1_DIGEST_STRING_LENGTH];
2061 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2062 sizeof(id_str)) == NULL) {
2063 err = got_error_path(id_str,
2064 GOT_ERR_BAD_OBJ_ID_STR);
2065 goto done;
2067 if (asprintf(&label_orig, "%s: commit %s",
2068 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2069 err = got_error_from_errno("asprintf");
2070 goto done;
2073 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2074 char *link_target;
2075 err = got_object_blob_read_to_str(&link_target, blob);
2076 if (err)
2077 goto done;
2078 err = merge_symlink(worktree, blob2, ondisk_path, path,
2079 label_orig, link_target, worktree->base_commit_id,
2080 repo, progress_cb, progress_arg);
2081 free(link_target);
2082 } else {
2083 err = merge_blob(&update_timestamps, worktree, blob2,
2084 ondisk_path, path, sb.st_mode, label_orig, blob,
2085 worktree->base_commit_id, repo,
2086 progress_cb, progress_arg);
2088 free(label_orig);
2089 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2090 err = got_error_from_errno("close");
2091 goto done;
2093 if (blob2)
2094 got_object_blob_close(blob2);
2095 if (err)
2096 goto done;
2098 * Do not update timestamps of files with local changes.
2099 * Otherwise, a future status walk would treat them as
2100 * unmodified files again.
2102 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2103 blob->id.sha1, worktree->base_commit_id->sha1,
2104 update_timestamps);
2105 } else if (status == GOT_STATUS_MODE_CHANGE) {
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 } else if (status == GOT_STATUS_DELETE) {
2109 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2110 if (err)
2111 goto done;
2112 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2113 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2114 if (err)
2115 goto done;
2116 } else {
2117 int is_bad_symlink = 0;
2118 if (S_ISLNK(te->mode)) {
2119 err = install_symlink(&is_bad_symlink, worktree,
2120 ondisk_path, path, blob,
2121 status == GOT_STATUS_MISSING, 0,
2122 status == GOT_STATUS_UNVERSIONED, 0,
2123 repo, progress_cb, progress_arg);
2124 } else {
2125 err = install_blob(worktree, ondisk_path, path,
2126 te->mode, sb.st_mode, blob,
2127 status == GOT_STATUS_MISSING, 0, 0,
2128 status == GOT_STATUS_UNVERSIONED,
2129 &update_timestamps,
2130 repo, progress_cb, progress_arg);
2132 if (err)
2133 goto done;
2135 if (ie) {
2136 err = got_fileindex_entry_update(ie,
2137 worktree->root_fd, path, blob->id.sha1,
2138 worktree->base_commit_id->sha1, 1);
2139 } else {
2140 err = create_fileindex_entry(&ie, fileindex,
2141 worktree->base_commit_id, worktree->root_fd, path,
2142 &blob->id, update_timestamps);
2144 if (err)
2145 goto done;
2147 if (is_bad_symlink) {
2148 got_fileindex_entry_filetype_set(ie,
2149 GOT_FILEIDX_MODE_BAD_SYMLINK);
2153 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2154 err = got_error_from_errno("close");
2155 goto done;
2157 got_object_blob_close(blob);
2158 done:
2159 free(ondisk_path);
2160 return err;
2163 static const struct got_error *
2164 remove_ondisk_file(const char *root_path, const char *path)
2166 const struct got_error *err = NULL;
2167 char *ondisk_path = NULL, *parent = NULL;
2169 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2170 return got_error_from_errno("asprintf");
2172 if (unlink(ondisk_path) == -1) {
2173 if (errno != ENOENT)
2174 err = got_error_from_errno2("unlink", ondisk_path);
2175 } else {
2176 size_t root_len = strlen(root_path);
2177 err = got_path_dirname(&parent, ondisk_path);
2178 if (err)
2179 goto done;
2180 while (got_path_cmp(parent, root_path,
2181 strlen(parent), root_len) != 0) {
2182 free(ondisk_path);
2183 ondisk_path = parent;
2184 parent = NULL;
2185 if (rmdir(ondisk_path) == -1) {
2186 if (errno != ENOTEMPTY)
2187 err = got_error_from_errno2("rmdir",
2188 ondisk_path);
2189 break;
2191 err = got_path_dirname(&parent, ondisk_path);
2192 if (err)
2193 break;
2196 done:
2197 free(ondisk_path);
2198 free(parent);
2199 return err;
2202 static const struct got_error *
2203 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2204 struct got_fileindex_entry *ie, struct got_repository *repo,
2205 got_worktree_checkout_cb progress_cb, void *progress_arg)
2207 const struct got_error *err = NULL;
2208 unsigned char status;
2209 struct stat sb;
2210 char *ondisk_path;
2212 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2213 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2215 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2216 == -1)
2217 return got_error_from_errno("asprintf");
2219 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2220 if (err)
2221 goto done;
2223 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2224 char ondisk_target[PATH_MAX];
2225 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2226 sizeof(ondisk_target));
2227 if (ondisk_len == -1) {
2228 err = got_error_from_errno2("readlink", ondisk_path);
2229 goto done;
2231 ondisk_target[ondisk_len] = '\0';
2232 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2233 NULL, NULL, /* XXX pass common ancestor info? */
2234 ondisk_target, ondisk_path);
2235 if (err)
2236 goto done;
2237 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2238 ie->path);
2239 goto done;
2242 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2243 status == GOT_STATUS_ADD) {
2244 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2245 if (err)
2246 goto done;
2248 * Preserve the working file and change the deleted blob's
2249 * entry into a schedule-add entry.
2251 err = got_fileindex_entry_update(ie, worktree->root_fd,
2252 ie->path, NULL, NULL, 0);
2253 } else {
2254 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2255 if (err)
2256 goto done;
2257 if (status == GOT_STATUS_NO_CHANGE) {
2258 err = remove_ondisk_file(worktree->root_path, ie->path);
2259 if (err)
2260 goto done;
2262 got_fileindex_entry_remove(fileindex, ie);
2264 done:
2265 free(ondisk_path);
2266 return err;
2269 struct diff_cb_arg {
2270 struct got_fileindex *fileindex;
2271 struct got_worktree *worktree;
2272 struct got_repository *repo;
2273 got_worktree_checkout_cb progress_cb;
2274 void *progress_arg;
2275 got_cancel_cb cancel_cb;
2276 void *cancel_arg;
2279 static const struct got_error *
2280 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2281 struct got_tree_entry *te, const char *parent_path)
2283 const struct got_error *err = NULL;
2284 struct diff_cb_arg *a = arg;
2286 if (a->cancel_cb) {
2287 err = a->cancel_cb(a->cancel_arg);
2288 if (err)
2289 return err;
2292 return update_blob(a->worktree, a->fileindex, ie, te,
2293 ie->path, a->repo, a->progress_cb, a->progress_arg);
2296 static const struct got_error *
2297 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2299 const struct got_error *err = NULL;
2300 struct diff_cb_arg *a = arg;
2302 if (a->cancel_cb) {
2303 err = a->cancel_cb(a->cancel_arg);
2304 if (err)
2305 return err;
2308 return delete_blob(a->worktree, a->fileindex, ie,
2309 a->repo, a->progress_cb, a->progress_arg);
2312 static const struct got_error *
2313 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2315 const struct got_error *err = NULL;
2316 struct diff_cb_arg *a = arg;
2317 char *path;
2319 if (a->cancel_cb) {
2320 err = a->cancel_cb(a->cancel_arg);
2321 if (err)
2322 return err;
2325 if (got_object_tree_entry_is_submodule(te))
2326 return NULL;
2328 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2329 return NULL;
2331 if (asprintf(&path, "%s%s%s", parent_path,
2332 parent_path[0] ? "/" : "", te->name)
2333 == -1)
2334 return got_error_from_errno("asprintf");
2336 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2337 a->repo, a->progress_cb, a->progress_arg);
2339 free(path);
2340 return err;
2343 const struct got_error *
2344 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2346 uint32_t uuid_status;
2348 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2349 if (uuid_status != uuid_s_ok) {
2350 *uuidstr = NULL;
2351 return got_error_uuid(uuid_status, "uuid_to_string");
2354 return NULL;
2357 static const struct got_error *
2358 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2360 const struct got_error *err = NULL;
2361 char *uuidstr = NULL;
2363 *refname = NULL;
2365 err = got_worktree_get_uuid(&uuidstr, worktree);
2366 if (err)
2367 return err;
2369 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2370 err = got_error_from_errno("asprintf");
2371 *refname = NULL;
2373 free(uuidstr);
2374 return err;
2377 const struct got_error *
2378 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2379 const char *prefix)
2381 return get_ref_name(refname, worktree, prefix);
2384 static const struct got_error *
2385 get_base_ref_name(char **refname, struct got_worktree *worktree)
2387 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2390 static const struct got_error *
2391 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree,
2394 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2397 static const struct got_error *
2398 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2403 static const struct got_error *
2404 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2410 static const struct got_error *
2411 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2424 static const struct got_error *
2425 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2427 return get_ref_name(refname, worktree,
2428 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2431 static const struct got_error *
2432 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 return get_ref_name(refname, worktree,
2435 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2438 static const struct got_error *
2439 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2441 return get_ref_name(refname, worktree,
2442 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2445 const struct got_error *
2446 got_worktree_get_histedit_script_path(char **path,
2447 struct got_worktree *worktree)
2449 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2450 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2451 *path = NULL;
2452 return got_error_from_errno("asprintf");
2454 return NULL;
2457 static const struct got_error *
2458 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2460 return get_ref_name(refname, worktree,
2461 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2464 static const struct got_error *
2465 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2467 return get_ref_name(refname, worktree,
2468 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2472 * Prevent Git's garbage collector from deleting our base commit by
2473 * setting a reference to our base commit's ID.
2475 static const struct got_error *
2476 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2478 const struct got_error *err = NULL;
2479 struct got_reference *ref = NULL;
2480 char *refname;
2482 err = get_base_ref_name(&refname, worktree);
2483 if (err)
2484 return err;
2486 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2487 if (err)
2488 goto done;
2490 err = got_ref_write(ref, repo);
2491 done:
2492 free(refname);
2493 if (ref)
2494 got_ref_close(ref);
2495 return err;
2498 static const struct got_error *
2499 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2501 const struct got_error *err = NULL;
2503 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2504 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2505 err = got_error_from_errno("asprintf");
2506 *fileindex_path = NULL;
2508 return err;
2512 static const struct got_error *
2513 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2514 struct got_worktree *worktree)
2516 const struct got_error *err = NULL;
2517 FILE *index = NULL;
2519 *fileindex_path = NULL;
2520 *fileindex = got_fileindex_alloc();
2521 if (*fileindex == NULL)
2522 return got_error_from_errno("got_fileindex_alloc");
2524 err = get_fileindex_path(fileindex_path, worktree);
2525 if (err)
2526 goto done;
2528 index = fopen(*fileindex_path, "rbe");
2529 if (index == NULL) {
2530 if (errno != ENOENT)
2531 err = got_error_from_errno2("fopen", *fileindex_path);
2532 } else {
2533 err = got_fileindex_read(*fileindex, index);
2534 if (fclose(index) == EOF && err == NULL)
2535 err = got_error_from_errno("fclose");
2537 done:
2538 if (err) {
2539 free(*fileindex_path);
2540 *fileindex_path = NULL;
2541 got_fileindex_free(*fileindex);
2542 *fileindex = NULL;
2544 return err;
2547 struct bump_base_commit_id_arg {
2548 struct got_object_id *base_commit_id;
2549 const char *path;
2550 size_t path_len;
2551 const char *entry_name;
2552 got_worktree_checkout_cb progress_cb;
2553 void *progress_arg;
2556 static const struct got_error *
2557 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2559 const struct got_error *err;
2560 struct bump_base_commit_id_arg *a = arg;
2562 if (a->entry_name) {
2563 if (strcmp(ie->path, a->path) != 0)
2564 return NULL;
2565 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2566 return NULL;
2568 if (got_fileindex_entry_was_skipped(ie))
2569 return NULL;
2571 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2572 SHA1_DIGEST_LENGTH) == 0)
2573 return NULL;
2575 if (a->progress_cb) {
2576 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2577 ie->path);
2578 if (err)
2579 return err;
2581 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2582 return NULL;
2585 /* Bump base commit ID of all files within an updated part of the work tree. */
2586 static const struct got_error *
2587 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2588 struct got_fileindex *fileindex,
2589 got_worktree_checkout_cb progress_cb, void *progress_arg)
2591 struct bump_base_commit_id_arg bbc_arg;
2593 bbc_arg.base_commit_id = worktree->base_commit_id;
2594 bbc_arg.entry_name = NULL;
2595 bbc_arg.path = "";
2596 bbc_arg.path_len = 0;
2597 bbc_arg.progress_cb = progress_cb;
2598 bbc_arg.progress_arg = progress_arg;
2600 return got_fileindex_for_each_entry_safe(fileindex,
2601 bump_base_commit_id, &bbc_arg);
2604 static const struct got_error *
2605 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2607 const struct got_error *err = NULL;
2608 char *new_fileindex_path = NULL;
2609 FILE *new_index = NULL;
2610 struct timespec timeout;
2612 err = got_opentemp_named(&new_fileindex_path, &new_index,
2613 fileindex_path, "");
2614 if (err)
2615 goto done;
2617 err = got_fileindex_write(fileindex, new_index);
2618 if (err)
2619 goto done;
2621 if (rename(new_fileindex_path, fileindex_path) != 0) {
2622 err = got_error_from_errno3("rename", new_fileindex_path,
2623 fileindex_path);
2624 unlink(new_fileindex_path);
2628 * Sleep for a short amount of time to ensure that files modified after
2629 * this program exits have a different time stamp from the one which
2630 * was recorded in the file index.
2632 timeout.tv_sec = 0;
2633 timeout.tv_nsec = 1;
2634 nanosleep(&timeout, NULL);
2635 done:
2636 if (new_index)
2637 fclose(new_index);
2638 free(new_fileindex_path);
2639 return err;
2642 static const struct got_error *
2643 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2644 struct got_object_id **tree_id, const char *wt_relpath,
2645 struct got_commit_object *base_commit, struct got_worktree *worktree,
2646 struct got_repository *repo)
2648 const struct got_error *err = NULL;
2649 struct got_object_id *id = NULL;
2650 char *in_repo_path = NULL;
2651 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2653 *entry_type = GOT_OBJ_TYPE_ANY;
2654 *tree_relpath = NULL;
2655 *tree_id = NULL;
2657 if (wt_relpath[0] == '\0') {
2658 /* Check out all files within the work tree. */
2659 *entry_type = GOT_OBJ_TYPE_TREE;
2660 *tree_relpath = strdup("");
2661 if (*tree_relpath == NULL) {
2662 err = got_error_from_errno("strdup");
2663 goto done;
2665 err = got_object_id_by_path(tree_id, repo, base_commit,
2666 worktree->path_prefix);
2667 if (err)
2668 goto done;
2669 return NULL;
2672 /* Check out a subset of files in the work tree. */
2674 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2675 is_root_wt ? "" : "/", wt_relpath) == -1) {
2676 err = got_error_from_errno("asprintf");
2677 goto done;
2680 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2681 if (err)
2682 goto done;
2684 free(in_repo_path);
2685 in_repo_path = NULL;
2687 err = got_object_get_type(entry_type, repo, id);
2688 if (err)
2689 goto done;
2691 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2692 /* Check out a single file. */
2693 if (strchr(wt_relpath, '/') == NULL) {
2694 /* Check out a single file in work tree's root dir. */
2695 in_repo_path = strdup(worktree->path_prefix);
2696 if (in_repo_path == NULL) {
2697 err = got_error_from_errno("strdup");
2698 goto done;
2700 *tree_relpath = strdup("");
2701 if (*tree_relpath == NULL) {
2702 err = got_error_from_errno("strdup");
2703 goto done;
2705 } else {
2706 /* Check out a single file in a subdirectory. */
2707 err = got_path_dirname(tree_relpath, wt_relpath);
2708 if (err)
2709 return err;
2710 if (asprintf(&in_repo_path, "%s%s%s",
2711 worktree->path_prefix, is_root_wt ? "" : "/",
2712 *tree_relpath) == -1) {
2713 err = got_error_from_errno("asprintf");
2714 goto done;
2717 err = got_object_id_by_path(tree_id, repo,
2718 base_commit, in_repo_path);
2719 } else {
2720 /* Check out all files within a subdirectory. */
2721 *tree_id = got_object_id_dup(id);
2722 if (*tree_id == NULL) {
2723 err = got_error_from_errno("got_object_id_dup");
2724 goto done;
2726 *tree_relpath = strdup(wt_relpath);
2727 if (*tree_relpath == NULL) {
2728 err = got_error_from_errno("strdup");
2729 goto done;
2732 done:
2733 free(id);
2734 free(in_repo_path);
2735 if (err) {
2736 *entry_type = GOT_OBJ_TYPE_ANY;
2737 free(*tree_relpath);
2738 *tree_relpath = NULL;
2739 free(*tree_id);
2740 *tree_id = NULL;
2742 return err;
2745 static const struct got_error *
2746 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2747 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2748 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2749 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2751 const struct got_error *err = NULL;
2752 struct got_commit_object *commit = NULL;
2753 struct got_tree_object *tree = NULL;
2754 struct got_fileindex_diff_tree_cb diff_cb;
2755 struct diff_cb_arg arg;
2757 err = ref_base_commit(worktree, repo);
2758 if (err) {
2759 if (!(err->code == GOT_ERR_ERRNO &&
2760 (errno == EACCES || errno == EROFS)))
2761 goto done;
2762 err = (*progress_cb)(progress_arg,
2763 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2764 if (err)
2765 return err;
2768 err = got_object_open_as_commit(&commit, repo,
2769 worktree->base_commit_id);
2770 if (err)
2771 goto done;
2773 err = got_object_open_as_tree(&tree, repo, tree_id);
2774 if (err)
2775 goto done;
2777 if (entry_name &&
2778 got_object_tree_find_entry(tree, entry_name) == NULL) {
2779 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2780 goto done;
2783 diff_cb.diff_old_new = diff_old_new;
2784 diff_cb.diff_old = diff_old;
2785 diff_cb.diff_new = diff_new;
2786 arg.fileindex = fileindex;
2787 arg.worktree = worktree;
2788 arg.repo = repo;
2789 arg.progress_cb = progress_cb;
2790 arg.progress_arg = progress_arg;
2791 arg.cancel_cb = cancel_cb;
2792 arg.cancel_arg = cancel_arg;
2793 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2794 entry_name, repo, &diff_cb, &arg);
2795 done:
2796 if (tree)
2797 got_object_tree_close(tree);
2798 if (commit)
2799 got_object_commit_close(commit);
2800 return err;
2803 const struct got_error *
2804 got_worktree_checkout_files(struct got_worktree *worktree,
2805 struct got_pathlist_head *paths, struct got_repository *repo,
2806 got_worktree_checkout_cb progress_cb, void *progress_arg,
2807 got_cancel_cb cancel_cb, void *cancel_arg)
2809 const struct got_error *err = NULL, *sync_err, *unlockerr;
2810 struct got_commit_object *commit = NULL;
2811 struct got_tree_object *tree = NULL;
2812 struct got_fileindex *fileindex = NULL;
2813 char *fileindex_path = NULL;
2814 struct got_pathlist_entry *pe;
2815 struct tree_path_data {
2816 STAILQ_ENTRY(tree_path_data) entry;
2817 struct got_object_id *tree_id;
2818 int entry_type;
2819 char *relpath;
2820 char *entry_name;
2821 } *tpd = NULL;
2822 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2824 STAILQ_INIT(&tree_paths);
2826 err = lock_worktree(worktree, LOCK_EX);
2827 if (err)
2828 return err;
2830 err = got_object_open_as_commit(&commit, repo,
2831 worktree->base_commit_id);
2832 if (err)
2833 goto done;
2835 /* Map all specified paths to in-repository trees. */
2836 TAILQ_FOREACH(pe, paths, entry) {
2837 tpd = malloc(sizeof(*tpd));
2838 if (tpd == NULL) {
2839 err = got_error_from_errno("malloc");
2840 goto done;
2843 err = find_tree_entry_for_checkout(&tpd->entry_type,
2844 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2845 worktree, repo);
2846 if (err) {
2847 free(tpd);
2848 goto done;
2851 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2852 err = got_path_basename(&tpd->entry_name, pe->path);
2853 if (err) {
2854 free(tpd->relpath);
2855 free(tpd->tree_id);
2856 free(tpd);
2857 goto done;
2859 } else
2860 tpd->entry_name = NULL;
2862 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2866 * Read the file index.
2867 * Checking out files is supposed to be an idempotent operation.
2868 * If the on-disk file index is incomplete we will try to complete it.
2870 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2871 if (err)
2872 goto done;
2874 tpd = STAILQ_FIRST(&tree_paths);
2875 TAILQ_FOREACH(pe, paths, entry) {
2876 struct bump_base_commit_id_arg bbc_arg;
2878 err = checkout_files(worktree, fileindex, tpd->relpath,
2879 tpd->tree_id, tpd->entry_name, repo,
2880 progress_cb, progress_arg, cancel_cb, cancel_arg);
2881 if (err)
2882 break;
2884 bbc_arg.base_commit_id = worktree->base_commit_id;
2885 bbc_arg.entry_name = tpd->entry_name;
2886 bbc_arg.path = pe->path;
2887 bbc_arg.path_len = pe->path_len;
2888 bbc_arg.progress_cb = progress_cb;
2889 bbc_arg.progress_arg = progress_arg;
2890 err = got_fileindex_for_each_entry_safe(fileindex,
2891 bump_base_commit_id, &bbc_arg);
2892 if (err)
2893 break;
2895 tpd = STAILQ_NEXT(tpd, entry);
2897 sync_err = sync_fileindex(fileindex, fileindex_path);
2898 if (sync_err && err == NULL)
2899 err = sync_err;
2900 done:
2901 free(fileindex_path);
2902 if (tree)
2903 got_object_tree_close(tree);
2904 if (commit)
2905 got_object_commit_close(commit);
2906 if (fileindex)
2907 got_fileindex_free(fileindex);
2908 while (!STAILQ_EMPTY(&tree_paths)) {
2909 tpd = STAILQ_FIRST(&tree_paths);
2910 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2911 free(tpd->relpath);
2912 free(tpd->tree_id);
2913 free(tpd);
2915 unlockerr = lock_worktree(worktree, LOCK_SH);
2916 if (unlockerr && err == NULL)
2917 err = unlockerr;
2918 return err;
2921 static const struct got_error *
2922 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2923 struct got_fileindex_entry *ie, const char *ondisk_path,
2924 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2925 int restoring_missing_file, int reverting_versioned_file,
2926 int path_is_unversioned, int allow_bad_symlinks,
2927 struct got_repository *repo,
2928 got_worktree_checkout_cb progress_cb, void *progress_arg)
2930 const struct got_error *err = NULL;
2931 int is_bad_symlink = 0;
2933 if (S_ISLNK(mode2)) {
2934 err = install_symlink(&is_bad_symlink,
2935 worktree, ondisk_path, path2, blob2,
2936 restoring_missing_file,
2937 reverting_versioned_file,
2938 path_is_unversioned, allow_bad_symlinks,
2939 repo, progress_cb, progress_arg);
2940 } else {
2941 err = install_blob(worktree, ondisk_path, path2,
2942 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2943 restoring_missing_file, reverting_versioned_file, 0,
2944 path_is_unversioned, NULL, repo,
2945 progress_cb, progress_arg);
2947 if (err)
2948 return err;
2949 if (ie == NULL) {
2950 /* Adding an unversioned file. */
2951 err = got_fileindex_entry_alloc(&ie, path2);
2952 if (err)
2953 return err;
2954 err = got_fileindex_entry_update(ie,
2955 worktree->root_fd, path2, NULL, NULL, 1);
2956 if (err) {
2957 got_fileindex_entry_free(ie);
2958 return err;
2960 err = got_fileindex_entry_add(fileindex, ie);
2961 if (err) {
2962 got_fileindex_entry_free(ie);
2963 return err;
2965 } else {
2966 /* Re-adding a locally deleted file. */
2967 err = got_fileindex_entry_update(ie,
2968 worktree->root_fd, path2, ie->blob_sha1,
2969 worktree->base_commit_id->sha1, 0);
2970 if (err)
2971 return err;
2974 if (is_bad_symlink) {
2975 got_fileindex_entry_filetype_set(ie,
2976 GOT_FILEIDX_MODE_BAD_SYMLINK);
2979 return NULL;
2982 struct merge_file_cb_arg {
2983 struct got_worktree *worktree;
2984 struct got_fileindex *fileindex;
2985 got_worktree_checkout_cb progress_cb;
2986 void *progress_arg;
2987 got_cancel_cb cancel_cb;
2988 void *cancel_arg;
2989 const char *label_orig;
2990 struct got_object_id *commit_id2;
2991 int allow_bad_symlinks;
2994 static const struct got_error *
2995 merge_file_cb(void *arg, struct got_blob_object *blob1,
2996 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2997 struct got_object_id *id1, struct got_object_id *id2,
2998 const char *path1, const char *path2,
2999 mode_t mode1, mode_t mode2, struct got_repository *repo)
3001 static const struct got_error *err = NULL;
3002 struct merge_file_cb_arg *a = arg;
3003 struct got_fileindex_entry *ie;
3004 char *ondisk_path = NULL;
3005 struct stat sb;
3006 unsigned char status;
3007 int local_changes_subsumed;
3008 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3009 char *id_str = NULL, *label_deriv2 = NULL;
3010 struct got_object_id *id = NULL;
3012 if (blob1 && blob2) {
3013 ie = got_fileindex_entry_get(a->fileindex, path2,
3014 strlen(path2));
3015 if (ie == NULL)
3016 return (*a->progress_cb)(a->progress_arg,
3017 GOT_STATUS_MISSING, path2);
3019 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3020 path2) == -1)
3021 return got_error_from_errno("asprintf");
3023 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3024 repo);
3025 if (err)
3026 goto done;
3028 if (status == GOT_STATUS_DELETE) {
3029 err = (*a->progress_cb)(a->progress_arg,
3030 GOT_STATUS_MERGE, path2);
3031 goto done;
3033 if (status != GOT_STATUS_NO_CHANGE &&
3034 status != GOT_STATUS_MODIFY &&
3035 status != GOT_STATUS_CONFLICT &&
3036 status != GOT_STATUS_ADD) {
3037 err = (*a->progress_cb)(a->progress_arg, status, path2);
3038 goto done;
3041 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3042 char *link_target2;
3043 err = got_object_blob_read_to_str(&link_target2, blob2);
3044 if (err)
3045 goto done;
3046 err = merge_symlink(a->worktree, blob1, ondisk_path,
3047 path2, a->label_orig, link_target2, a->commit_id2,
3048 repo, a->progress_cb, a->progress_arg);
3049 free(link_target2);
3050 } else {
3051 int fd;
3053 f_orig = got_opentemp();
3054 if (f_orig == NULL) {
3055 err = got_error_from_errno("got_opentemp");
3056 goto done;
3058 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3059 f_orig, blob1);
3060 if (err)
3061 goto done;
3063 f_deriv2 = got_opentemp();
3064 if (f_deriv2 == NULL)
3065 goto done;
3066 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3067 f_deriv2, blob2);
3068 if (err)
3069 goto done;
3071 fd = open(ondisk_path,
3072 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3073 if (fd == -1) {
3074 err = got_error_from_errno2("open",
3075 ondisk_path);
3076 goto done;
3078 f_deriv = fdopen(fd, "r");
3079 if (f_deriv == NULL) {
3080 err = got_error_from_errno2("fdopen",
3081 ondisk_path);
3082 close(fd);
3083 goto done;
3085 err = got_object_id_str(&id_str, a->commit_id2);
3086 if (err)
3087 goto done;
3088 if (asprintf(&label_deriv2, "%s: commit %s",
3089 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3090 err = got_error_from_errno("asprintf");
3091 goto done;
3093 err = merge_file(&local_changes_subsumed, a->worktree,
3094 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3095 mode2, a->label_orig, NULL, label_deriv2,
3096 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3097 a->progress_cb, a->progress_arg);
3099 } else if (blob1) {
3100 ie = got_fileindex_entry_get(a->fileindex, path1,
3101 strlen(path1));
3102 if (ie == NULL)
3103 return (*a->progress_cb)(a->progress_arg,
3104 GOT_STATUS_MISSING, path1);
3106 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3107 path1) == -1)
3108 return got_error_from_errno("asprintf");
3110 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3111 repo);
3112 if (err)
3113 goto done;
3115 switch (status) {
3116 case GOT_STATUS_NO_CHANGE:
3117 err = (*a->progress_cb)(a->progress_arg,
3118 GOT_STATUS_DELETE, path1);
3119 if (err)
3120 goto done;
3121 err = remove_ondisk_file(a->worktree->root_path, path1);
3122 if (err)
3123 goto done;
3124 if (ie)
3125 got_fileindex_entry_mark_deleted_from_disk(ie);
3126 break;
3127 case GOT_STATUS_DELETE:
3128 case GOT_STATUS_MISSING:
3129 err = (*a->progress_cb)(a->progress_arg,
3130 GOT_STATUS_DELETE, path1);
3131 if (err)
3132 goto done;
3133 if (ie)
3134 got_fileindex_entry_mark_deleted_from_disk(ie);
3135 break;
3136 case GOT_STATUS_MODIFY: {
3137 FILE *blob1_f;
3138 off_t blob1_size;
3139 int obj_type;
3141 * Delete the file only if its content already
3142 * exists in the repository.
3144 err = got_object_blob_file_create(&id, &blob1_f,
3145 &blob1_size, path1);
3146 if (err)
3147 goto done;
3148 if (fclose(blob1_f) == EOF) {
3149 err = got_error_from_errno("fclose");
3150 goto done;
3153 /* Implied existence check. */
3154 err = got_object_get_type(&obj_type, repo, id);
3155 if (err) {
3156 if (err->code != GOT_ERR_NO_OBJ)
3157 goto done;
3158 err = (*a->progress_cb)(a->progress_arg,
3159 GOT_STATUS_CANNOT_DELETE, path1);
3160 goto done;
3161 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3162 err = (*a->progress_cb)(a->progress_arg,
3163 GOT_STATUS_CANNOT_DELETE, path1);
3164 goto done;
3166 err = (*a->progress_cb)(a->progress_arg,
3167 GOT_STATUS_DELETE, path1);
3168 if (err)
3169 goto done;
3170 err = remove_ondisk_file(a->worktree->root_path,
3171 path1);
3172 if (err)
3173 goto done;
3174 if (ie)
3175 got_fileindex_entry_mark_deleted_from_disk(ie);
3176 break;
3178 case GOT_STATUS_ADD: {
3179 FILE *blob1_f;
3180 off_t blob1_size;
3182 * Delete the file only if its content already
3183 * exists in the repository.
3185 err = got_object_blob_file_create(&id, &blob1_f,
3186 &blob1_size, path1);
3187 if (err)
3188 goto done;
3189 if (fclose(blob1_f) == EOF) {
3190 err = got_error_from_errno("fclose");
3191 goto done;
3193 if (got_object_id_cmp(id, id1) == 0) {
3194 err = (*a->progress_cb)(a->progress_arg,
3195 GOT_STATUS_DELETE, path1);
3196 if (err)
3197 goto done;
3198 err = remove_ondisk_file(a->worktree->root_path,
3199 path1);
3200 if (err)
3201 goto done;
3202 if (ie)
3203 got_fileindex_entry_remove(a->fileindex,
3204 ie);
3205 } else {
3206 err = (*a->progress_cb)(a->progress_arg,
3207 GOT_STATUS_CANNOT_DELETE, path1);
3209 if (err)
3210 goto done;
3211 break;
3213 case GOT_STATUS_CONFLICT:
3214 err = (*a->progress_cb)(a->progress_arg,
3215 GOT_STATUS_CANNOT_DELETE, path1);
3216 if (err)
3217 goto done;
3218 break;
3219 case GOT_STATUS_OBSTRUCTED:
3220 err = (*a->progress_cb)(a->progress_arg, status, path1);
3221 if (err)
3222 goto done;
3223 break;
3224 default:
3225 break;
3227 } else if (blob2) {
3228 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3229 path2) == -1)
3230 return got_error_from_errno("asprintf");
3231 ie = got_fileindex_entry_get(a->fileindex, path2,
3232 strlen(path2));
3233 if (ie) {
3234 err = get_file_status(&status, &sb, ie, ondisk_path,
3235 -1, NULL, repo);
3236 if (err)
3237 goto done;
3238 if (status != GOT_STATUS_NO_CHANGE &&
3239 status != GOT_STATUS_MODIFY &&
3240 status != GOT_STATUS_CONFLICT &&
3241 status != GOT_STATUS_ADD &&
3242 status != GOT_STATUS_DELETE) {
3243 err = (*a->progress_cb)(a->progress_arg,
3244 status, path2);
3245 goto done;
3247 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3248 char *link_target2;
3249 err = got_object_blob_read_to_str(&link_target2,
3250 blob2);
3251 if (err)
3252 goto done;
3253 err = merge_symlink(a->worktree, NULL,
3254 ondisk_path, path2, a->label_orig,
3255 link_target2, a->commit_id2, repo,
3256 a->progress_cb, a->progress_arg);
3257 free(link_target2);
3258 } else if (S_ISREG(sb.st_mode)) {
3259 err = merge_blob(&local_changes_subsumed,
3260 a->worktree, NULL, ondisk_path, path2,
3261 sb.st_mode, a->label_orig, blob2,
3262 a->commit_id2, repo, a->progress_cb,
3263 a->progress_arg);
3264 } else if (status != GOT_STATUS_DELETE) {
3265 err = got_error_path(ondisk_path,
3266 GOT_ERR_FILE_OBSTRUCTED);
3268 if (err)
3269 goto done;
3270 if (status == GOT_STATUS_DELETE) {
3271 /* Re-add file with content from new blob. */
3272 err = add_file(a->worktree, a->fileindex, ie,
3273 ondisk_path, path2, blob2, mode2,
3274 0, 0, 0, a->allow_bad_symlinks,
3275 repo, a->progress_cb, a->progress_arg);
3276 if (err)
3277 goto done;
3279 } else {
3280 err = add_file(a->worktree, a->fileindex, NULL,
3281 ondisk_path, path2, blob2, mode2,
3282 0, 0, 1, a->allow_bad_symlinks,
3283 repo, a->progress_cb, a->progress_arg);
3284 if (err)
3285 goto done;
3288 done:
3289 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3290 err = got_error_from_errno("fclose");
3291 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3292 err = got_error_from_errno("fclose");
3293 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3294 err = got_error_from_errno("fclose");
3295 free(id_str);
3296 free(id);
3297 free(label_deriv2);
3298 free(ondisk_path);
3299 return err;
3302 struct check_mixed_commits_args {
3303 struct got_worktree *worktree;
3304 got_cancel_cb cancel_cb;
3305 void *cancel_arg;
3308 static const struct got_error *
3309 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3311 const struct got_error *err = NULL;
3312 struct check_mixed_commits_args *a = arg;
3314 if (a->cancel_cb) {
3315 err = a->cancel_cb(a->cancel_arg);
3316 if (err)
3317 return err;
3320 /* Reject merges into a work tree with mixed base commits. */
3321 if (got_fileindex_entry_has_commit(ie) &&
3322 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3323 SHA1_DIGEST_LENGTH) != 0)
3324 return got_error(GOT_ERR_MIXED_COMMITS);
3326 return NULL;
3329 const struct got_error *
3330 got_worktree_get_state(char *state, struct got_repository *repo,
3331 struct got_worktree *worktree,
3332 got_cancel_cb cancel_cb, void *cancel_arg)
3334 const struct got_error *err;
3335 struct got_object_id *base_id, *head_id = NULL;
3336 struct got_reference *head_ref;
3337 struct got_fileindex *fileindex = NULL;
3338 char *fileindex_path = NULL;
3339 struct check_mixed_commits_args cma;
3341 if (worktree == NULL)
3342 return got_error(GOT_ERR_NOT_WORKTREE);
3344 err = got_ref_open(&head_ref, repo,
3345 got_worktree_get_head_ref_name(worktree), 0);
3346 if (err)
3347 return err;
3349 err = got_ref_resolve(&head_id, repo, head_ref);
3350 if (err)
3351 goto done;
3353 *state = GOT_WORKTREE_STATE_UNKNOWN;
3354 base_id = got_worktree_get_base_commit_id(worktree);
3356 cma.worktree = worktree;
3357 cma.cancel_cb = cancel_cb;
3358 cma.cancel_arg = cancel_arg;
3360 if (got_object_id_cmp(base_id, head_id) == 0) {
3361 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3362 if (err)
3363 goto done;
3365 err = got_fileindex_for_each_entry_safe(fileindex,
3366 check_mixed_commits, &cma);
3367 if (err == NULL)
3368 *state = GOT_WORKTREE_STATE_UPTODATE;
3369 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3370 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3371 err = NULL;
3373 } else
3374 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3376 done:
3377 free(head_id);
3378 free(fileindex_path);
3379 got_ref_close(head_ref);
3380 if (fileindex != NULL)
3381 got_fileindex_free(fileindex);
3382 return err;
3385 struct check_merge_conflicts_arg {
3386 struct got_worktree *worktree;
3387 struct got_fileindex *fileindex;
3388 struct got_repository *repo;
3391 static const struct got_error *
3392 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3393 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3394 struct got_object_id *id1, struct got_object_id *id2,
3395 const char *path1, const char *path2,
3396 mode_t mode1, mode_t mode2, struct got_repository *repo)
3398 const struct got_error *err = NULL;
3399 struct check_merge_conflicts_arg *a = arg;
3400 unsigned char status;
3401 struct stat sb;
3402 struct got_fileindex_entry *ie;
3403 const char *path = path2 ? path2 : path1;
3404 struct got_object_id *id = id2 ? id2 : id1;
3405 char *ondisk_path;
3407 if (id == NULL)
3408 return NULL;
3410 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3411 if (ie == NULL)
3412 return NULL;
3414 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3415 == -1)
3416 return got_error_from_errno("asprintf");
3418 /* Reject merges into a work tree with conflicted files. */
3419 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3420 free(ondisk_path);
3421 if (err)
3422 return err;
3423 if (status == GOT_STATUS_CONFLICT)
3424 return got_error(GOT_ERR_CONFLICTS);
3426 return NULL;
3429 static const struct got_error *
3430 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3431 const char *fileindex_path, struct got_object_id *commit_id1,
3432 struct got_object_id *commit_id2, struct got_repository *repo,
3433 got_worktree_checkout_cb progress_cb, void *progress_arg,
3434 got_cancel_cb cancel_cb, void *cancel_arg)
3436 const struct got_error *err = NULL, *sync_err;
3437 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3438 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3439 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3440 struct check_merge_conflicts_arg cmc_arg;
3441 struct merge_file_cb_arg arg;
3442 char *label_orig = NULL;
3443 FILE *f1 = NULL, *f2 = NULL;
3444 int fd1 = -1, fd2 = -1;
3446 if (commit_id1) {
3447 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3448 if (err)
3449 goto done;
3450 err = got_object_id_by_path(&tree_id1, repo, commit1,
3451 worktree->path_prefix);
3452 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3453 goto done;
3455 if (tree_id1) {
3456 char *id_str;
3458 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3459 if (err)
3460 goto done;
3462 err = got_object_id_str(&id_str, commit_id1);
3463 if (err)
3464 goto done;
3466 if (asprintf(&label_orig, "%s: commit %s",
3467 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3468 err = got_error_from_errno("asprintf");
3469 free(id_str);
3470 goto done;
3472 free(id_str);
3474 f1 = got_opentemp();
3475 if (f1 == NULL) {
3476 err = got_error_from_errno("got_opentemp");
3477 goto done;
3481 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3482 if (err)
3483 goto done;
3485 err = got_object_id_by_path(&tree_id2, repo, commit2,
3486 worktree->path_prefix);
3487 if (err)
3488 goto done;
3490 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3491 if (err)
3492 goto done;
3494 f2 = got_opentemp();
3495 if (f2 == NULL) {
3496 err = got_error_from_errno("got_opentemp");
3497 goto done;
3500 fd1 = got_opentempfd();
3501 if (fd1 == -1) {
3502 err = got_error_from_errno("got_opentempfd");
3503 goto done;
3506 fd2 = got_opentempfd();
3507 if (fd2 == -1) {
3508 err = got_error_from_errno("got_opentempfd");
3509 goto done;
3512 cmc_arg.worktree = worktree;
3513 cmc_arg.fileindex = fileindex;
3514 cmc_arg.repo = repo;
3515 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3516 check_merge_conflicts, &cmc_arg, 0);
3517 if (err)
3518 goto done;
3520 arg.worktree = worktree;
3521 arg.fileindex = fileindex;
3522 arg.progress_cb = progress_cb;
3523 arg.progress_arg = progress_arg;
3524 arg.cancel_cb = cancel_cb;
3525 arg.cancel_arg = cancel_arg;
3526 arg.label_orig = label_orig;
3527 arg.commit_id2 = commit_id2;
3528 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3529 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3530 merge_file_cb, &arg, 1);
3531 sync_err = sync_fileindex(fileindex, fileindex_path);
3532 if (sync_err && err == NULL)
3533 err = sync_err;
3534 done:
3535 if (commit1)
3536 got_object_commit_close(commit1);
3537 if (commit2)
3538 got_object_commit_close(commit2);
3539 if (tree1)
3540 got_object_tree_close(tree1);
3541 if (tree2)
3542 got_object_tree_close(tree2);
3543 if (f1 && fclose(f1) == EOF && err == NULL)
3544 err = got_error_from_errno("fclose");
3545 if (f2 && fclose(f2) == EOF && err == NULL)
3546 err = got_error_from_errno("fclose");
3547 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3548 err = got_error_from_errno("close");
3549 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3550 err = got_error_from_errno("close");
3551 free(label_orig);
3552 return err;
3555 const struct got_error *
3556 got_worktree_merge_files(struct got_worktree *worktree,
3557 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3558 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3559 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3561 const struct got_error *err, *unlockerr;
3562 char *fileindex_path = NULL;
3563 struct got_fileindex *fileindex = NULL;
3564 struct check_mixed_commits_args cma;
3566 err = lock_worktree(worktree, LOCK_EX);
3567 if (err)
3568 return err;
3570 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3571 if (err)
3572 goto done;
3574 cma.worktree = worktree;
3575 cma.cancel_cb = cancel_cb;
3576 cma.cancel_arg = cancel_arg;
3578 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3579 &cma);
3580 if (err)
3581 goto done;
3583 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3584 commit_id2, repo, progress_cb, progress_arg,
3585 cancel_cb, cancel_arg);
3586 done:
3587 if (fileindex)
3588 got_fileindex_free(fileindex);
3589 free(fileindex_path);
3590 unlockerr = lock_worktree(worktree, LOCK_SH);
3591 if (unlockerr && err == NULL)
3592 err = unlockerr;
3593 return err;
3596 struct diff_dir_cb_arg {
3597 struct got_fileindex *fileindex;
3598 struct got_worktree *worktree;
3599 const char *status_path;
3600 size_t status_path_len;
3601 struct got_repository *repo;
3602 got_worktree_status_cb status_cb;
3603 void *status_arg;
3604 got_cancel_cb cancel_cb;
3605 void *cancel_arg;
3606 /* A pathlist containing per-directory pathlists of ignore patterns. */
3607 struct got_pathlist_head *ignores;
3608 int report_unchanged;
3609 int no_ignores;
3612 static const struct got_error *
3613 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3614 int dirfd, const char *de_name,
3615 got_worktree_status_cb status_cb, void *status_arg,
3616 struct got_repository *repo, int report_unchanged)
3618 const struct got_error *err = NULL;
3619 unsigned char status = GOT_STATUS_NO_CHANGE;
3620 unsigned char staged_status;
3621 struct stat sb;
3622 struct got_object_id blob_id, commit_id, staged_blob_id;
3623 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3624 struct got_object_id *staged_blob_idp = NULL;
3626 staged_status = get_staged_status(ie);
3627 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3628 if (err)
3629 return err;
3631 if (status == GOT_STATUS_NO_CHANGE &&
3632 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3633 return NULL;
3635 if (got_fileindex_entry_has_blob(ie))
3636 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3637 if (got_fileindex_entry_has_commit(ie))
3638 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3639 if (staged_status == GOT_STATUS_ADD ||
3640 staged_status == GOT_STATUS_MODIFY) {
3641 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3642 &staged_blob_id, ie);
3645 return (*status_cb)(status_arg, status, staged_status,
3646 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3649 static const struct got_error *
3650 status_old_new(void *arg, struct got_fileindex_entry *ie,
3651 struct dirent *de, const char *parent_path, int dirfd)
3653 const struct got_error *err = NULL;
3654 struct diff_dir_cb_arg *a = arg;
3655 char *abspath;
3657 if (a->cancel_cb) {
3658 err = a->cancel_cb(a->cancel_arg);
3659 if (err)
3660 return err;
3663 if (got_path_cmp(parent_path, a->status_path,
3664 strlen(parent_path), a->status_path_len) != 0 &&
3665 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3666 return NULL;
3668 if (parent_path[0]) {
3669 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3670 parent_path, de->d_name) == -1)
3671 return got_error_from_errno("asprintf");
3672 } else {
3673 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3674 de->d_name) == -1)
3675 return got_error_from_errno("asprintf");
3678 err = report_file_status(ie, abspath, dirfd, de->d_name,
3679 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3680 free(abspath);
3681 return err;
3684 static const struct got_error *
3685 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3687 const struct got_error *err;
3688 struct diff_dir_cb_arg *a = arg;
3689 struct got_object_id blob_id, commit_id;
3690 unsigned char status;
3692 if (a->cancel_cb) {
3693 err = a->cancel_cb(a->cancel_arg);
3694 if (err)
3695 return err;
3698 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3699 return NULL;
3701 got_fileindex_entry_get_blob_id(&blob_id, ie);
3702 got_fileindex_entry_get_commit_id(&commit_id, ie);
3703 if (got_fileindex_entry_has_file_on_disk(ie))
3704 status = GOT_STATUS_MISSING;
3705 else
3706 status = GOT_STATUS_DELETE;
3707 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3708 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3711 static void
3712 free_ignores(struct got_pathlist_head *ignores)
3714 struct got_pathlist_entry *pe;
3716 TAILQ_FOREACH(pe, ignores, entry) {
3717 struct got_pathlist_head *ignorelist = pe->data;
3719 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3721 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3724 static const struct got_error *
3725 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3727 const struct got_error *err = NULL;
3728 struct got_pathlist_entry *pe = NULL;
3729 struct got_pathlist_head *ignorelist;
3730 char *line = NULL, *pattern, *dirpath = NULL;
3731 size_t linesize = 0;
3732 ssize_t linelen;
3734 ignorelist = calloc(1, sizeof(*ignorelist));
3735 if (ignorelist == NULL)
3736 return got_error_from_errno("calloc");
3737 TAILQ_INIT(ignorelist);
3739 while ((linelen = getline(&line, &linesize, f)) != -1) {
3740 if (linelen > 0 && line[linelen - 1] == '\n')
3741 line[linelen - 1] = '\0';
3743 /* Git's ignores may contain comments. */
3744 if (line[0] == '#')
3745 continue;
3747 /* Git's negated patterns are not (yet?) supported. */
3748 if (line[0] == '!')
3749 continue;
3751 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3752 line) == -1) {
3753 err = got_error_from_errno("asprintf");
3754 goto done;
3756 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3757 if (err)
3758 goto done;
3760 if (ferror(f)) {
3761 err = got_error_from_errno("getline");
3762 goto done;
3765 dirpath = strdup(path);
3766 if (dirpath == NULL) {
3767 err = got_error_from_errno("strdup");
3768 goto done;
3770 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3771 done:
3772 free(line);
3773 if (err || pe == NULL) {
3774 free(dirpath);
3775 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3776 free(ignorelist);
3778 return err;
3781 static int
3782 match_path(const char *pattern, size_t pattern_len, const char *path,
3783 int flags)
3785 char buf[PATH_MAX];
3788 * Trailing slashes signify directories.
3789 * Append a * to make such patterns conform to fnmatch rules.
3791 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3792 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3793 return FNM_NOMATCH; /* XXX */
3795 return fnmatch(buf, path, flags);
3798 return fnmatch(pattern, path, flags);
3801 static int
3802 match_ignores(struct got_pathlist_head *ignores, const char *path)
3804 struct got_pathlist_entry *pe;
3806 /* Handle patterns which match in all directories. */
3807 TAILQ_FOREACH(pe, ignores, entry) {
3808 struct got_pathlist_head *ignorelist = pe->data;
3809 struct got_pathlist_entry *pi;
3811 TAILQ_FOREACH(pi, ignorelist, entry) {
3812 const char *p;
3814 if (pi->path_len < 3 ||
3815 strncmp(pi->path, "**/", 3) != 0)
3816 continue;
3817 p = path;
3818 while (*p) {
3819 if (match_path(pi->path + 3,
3820 pi->path_len - 3, p,
3821 FNM_PATHNAME | FNM_LEADING_DIR)) {
3822 /* Retry in next directory. */
3823 while (*p && *p != '/')
3824 p++;
3825 while (*p == '/')
3826 p++;
3827 continue;
3829 return 1;
3835 * The ignores pathlist contains ignore lists from children before
3836 * parents, so we can find the most specific ignorelist by walking
3837 * ignores backwards.
3839 pe = TAILQ_LAST(ignores, got_pathlist_head);
3840 while (pe) {
3841 if (got_path_is_child(path, pe->path, pe->path_len)) {
3842 struct got_pathlist_head *ignorelist = pe->data;
3843 struct got_pathlist_entry *pi;
3844 TAILQ_FOREACH(pi, ignorelist, entry) {
3845 int flags = FNM_LEADING_DIR;
3846 if (strstr(pi->path, "/**/") == NULL)
3847 flags |= FNM_PATHNAME;
3848 if (match_path(pi->path, pi->path_len,
3849 path, flags))
3850 continue;
3851 return 1;
3854 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3857 return 0;
3860 static const struct got_error *
3861 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3862 const char *path, int dirfd, const char *ignores_filename)
3864 const struct got_error *err = NULL;
3865 char *ignorespath;
3866 int fd = -1;
3867 FILE *ignoresfile = NULL;
3869 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3870 path[0] ? "/" : "", ignores_filename) == -1)
3871 return got_error_from_errno("asprintf");
3873 if (dirfd != -1) {
3874 fd = openat(dirfd, ignores_filename,
3875 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3876 if (fd == -1) {
3877 if (errno != ENOENT && errno != EACCES)
3878 err = got_error_from_errno2("openat",
3879 ignorespath);
3880 } else {
3881 ignoresfile = fdopen(fd, "r");
3882 if (ignoresfile == NULL)
3883 err = got_error_from_errno2("fdopen",
3884 ignorespath);
3885 else {
3886 fd = -1;
3887 err = read_ignores(ignores, path, ignoresfile);
3890 } else {
3891 ignoresfile = fopen(ignorespath, "re");
3892 if (ignoresfile == NULL) {
3893 if (errno != ENOENT && errno != EACCES)
3894 err = got_error_from_errno2("fopen",
3895 ignorespath);
3896 } else
3897 err = read_ignores(ignores, path, ignoresfile);
3900 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3901 err = got_error_from_errno2("fclose", path);
3902 if (fd != -1 && close(fd) == -1 && err == NULL)
3903 err = got_error_from_errno2("close", path);
3904 free(ignorespath);
3905 return err;
3908 static const struct got_error *
3909 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3910 int dirfd)
3912 const struct got_error *err = NULL;
3913 struct diff_dir_cb_arg *a = arg;
3914 char *path = NULL;
3916 if (ignore != NULL)
3917 *ignore = 0;
3919 if (a->cancel_cb) {
3920 err = a->cancel_cb(a->cancel_arg);
3921 if (err)
3922 return err;
3925 if (parent_path[0]) {
3926 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3927 return got_error_from_errno("asprintf");
3928 } else {
3929 path = de->d_name;
3932 if (de->d_type == DT_DIR) {
3933 if (!a->no_ignores && ignore != NULL &&
3934 match_ignores(a->ignores, path))
3935 *ignore = 1;
3936 } else if (!match_ignores(a->ignores, path) &&
3937 got_path_is_child(path, a->status_path, a->status_path_len))
3938 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3939 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3940 if (parent_path[0])
3941 free(path);
3942 return err;
3945 static const struct got_error *
3946 status_traverse(void *arg, const char *path, int dirfd)
3948 const struct got_error *err = NULL;
3949 struct diff_dir_cb_arg *a = arg;
3951 if (a->no_ignores)
3952 return NULL;
3954 err = add_ignores(a->ignores, a->worktree->root_path,
3955 path, dirfd, ".cvsignore");
3956 if (err)
3957 return err;
3959 err = add_ignores(a->ignores, a->worktree->root_path, path,
3960 dirfd, ".gitignore");
3962 return err;
3965 static const struct got_error *
3966 report_single_file_status(const char *path, const char *ondisk_path,
3967 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3968 void *status_arg, struct got_repository *repo, int report_unchanged,
3969 struct got_pathlist_head *ignores, int no_ignores)
3971 struct got_fileindex_entry *ie;
3972 struct stat sb;
3974 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3975 if (ie)
3976 return report_file_status(ie, ondisk_path, -1, NULL,
3977 status_cb, status_arg, repo, report_unchanged);
3979 if (lstat(ondisk_path, &sb) == -1) {
3980 if (errno != ENOENT)
3981 return got_error_from_errno2("lstat", ondisk_path);
3982 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3983 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3986 if (!no_ignores && match_ignores(ignores, path))
3987 return NULL;
3989 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3990 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3991 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3993 return NULL;
3996 static const struct got_error *
3997 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3998 const char *root_path, const char *path)
4000 const struct got_error *err;
4001 char *parent_path, *next_parent_path = NULL;
4003 err = add_ignores(ignores, root_path, "", -1,
4004 ".cvsignore");
4005 if (err)
4006 return err;
4008 err = add_ignores(ignores, root_path, "", -1,
4009 ".gitignore");
4010 if (err)
4011 return err;
4013 err = got_path_dirname(&parent_path, path);
4014 if (err) {
4015 if (err->code == GOT_ERR_BAD_PATH)
4016 return NULL; /* cannot traverse parent */
4017 return err;
4019 for (;;) {
4020 err = add_ignores(ignores, root_path, parent_path, -1,
4021 ".cvsignore");
4022 if (err)
4023 break;
4024 err = add_ignores(ignores, root_path, parent_path, -1,
4025 ".gitignore");
4026 if (err)
4027 break;
4028 err = got_path_dirname(&next_parent_path, parent_path);
4029 if (err) {
4030 if (err->code == GOT_ERR_BAD_PATH)
4031 err = NULL; /* traversed everything */
4032 break;
4034 if (got_path_is_root_dir(parent_path))
4035 break;
4036 free(parent_path);
4037 parent_path = next_parent_path;
4038 next_parent_path = NULL;
4041 free(parent_path);
4042 free(next_parent_path);
4043 return err;
4046 struct find_missing_children_args {
4047 const char *parent_path;
4048 size_t parent_len;
4049 struct got_pathlist_head *children;
4050 got_cancel_cb cancel_cb;
4051 void *cancel_arg;
4054 static const struct got_error *
4055 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4057 const struct got_error *err = NULL;
4058 struct find_missing_children_args *a = arg;
4060 if (a->cancel_cb) {
4061 err = a->cancel_cb(a->cancel_arg);
4062 if (err)
4063 return err;
4066 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4067 err = got_pathlist_append(a->children, ie->path, NULL);
4069 return err;
4072 static const struct got_error *
4073 report_children(struct got_pathlist_head *children,
4074 struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 struct got_repository *repo, int is_root_dir, int report_unchanged,
4076 struct got_pathlist_head *ignores, int no_ignores,
4077 got_worktree_status_cb status_cb, void *status_arg,
4078 got_cancel_cb cancel_cb, void *cancel_arg)
4080 const struct got_error *err = NULL;
4081 struct got_pathlist_entry *pe;
4082 char *ondisk_path = NULL;
4084 TAILQ_FOREACH(pe, children, entry) {
4085 if (cancel_cb) {
4086 err = cancel_cb(cancel_arg);
4087 if (err)
4088 break;
4091 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4092 !is_root_dir ? "/" : "", pe->path) == -1) {
4093 err = got_error_from_errno("asprintf");
4094 ondisk_path = NULL;
4095 break;
4098 err = report_single_file_status(pe->path, ondisk_path,
4099 fileindex, status_cb, status_arg, repo, report_unchanged,
4100 ignores, no_ignores);
4101 if (err)
4102 break;
4104 free(ondisk_path);
4105 ondisk_path = NULL;
4108 free(ondisk_path);
4109 return err;
4112 static const struct got_error *
4113 worktree_status(struct got_worktree *worktree, const char *path,
4114 struct got_fileindex *fileindex, struct got_repository *repo,
4115 got_worktree_status_cb status_cb, void *status_arg,
4116 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4117 int report_unchanged)
4119 const struct got_error *err = NULL;
4120 int fd = -1;
4121 struct got_fileindex_diff_dir_cb fdiff_cb;
4122 struct diff_dir_cb_arg arg;
4123 char *ondisk_path = NULL;
4124 struct got_pathlist_head ignores, missing_children;
4125 struct got_fileindex_entry *ie;
4127 TAILQ_INIT(&ignores);
4128 TAILQ_INIT(&missing_children);
4130 if (asprintf(&ondisk_path, "%s%s%s",
4131 worktree->root_path, path[0] ? "/" : "", path) == -1)
4132 return got_error_from_errno("asprintf");
4134 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4135 if (ie) {
4136 err = report_single_file_status(path, ondisk_path,
4137 fileindex, status_cb, status_arg, repo,
4138 report_unchanged, &ignores, no_ignores);
4139 goto done;
4140 } else {
4141 struct find_missing_children_args fmca;
4142 fmca.parent_path = path;
4143 fmca.parent_len = strlen(path);
4144 fmca.children = &missing_children;
4145 fmca.cancel_cb = cancel_cb;
4146 fmca.cancel_arg = cancel_arg;
4147 err = got_fileindex_for_each_entry_safe(fileindex,
4148 find_missing_children, &fmca);
4149 if (err)
4150 goto done;
4153 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4154 if (fd == -1) {
4155 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4156 !got_err_open_nofollow_on_symlink())
4157 err = got_error_from_errno2("open", ondisk_path);
4158 else {
4159 if (!no_ignores) {
4160 err = add_ignores_from_parent_paths(&ignores,
4161 worktree->root_path, ondisk_path);
4162 if (err)
4163 goto done;
4165 if (TAILQ_EMPTY(&missing_children)) {
4166 err = report_single_file_status(path,
4167 ondisk_path, fileindex,
4168 status_cb, status_arg, repo,
4169 report_unchanged, &ignores, no_ignores);
4170 if (err)
4171 goto done;
4172 } else {
4173 err = report_children(&missing_children,
4174 worktree, fileindex, repo,
4175 (path[0] == '\0'), report_unchanged,
4176 &ignores, no_ignores,
4177 status_cb, status_arg,
4178 cancel_cb, cancel_arg);
4179 if (err)
4180 goto done;
4183 } else {
4184 fdiff_cb.diff_old_new = status_old_new;
4185 fdiff_cb.diff_old = status_old;
4186 fdiff_cb.diff_new = status_new;
4187 fdiff_cb.diff_traverse = status_traverse;
4188 arg.fileindex = fileindex;
4189 arg.worktree = worktree;
4190 arg.status_path = path;
4191 arg.status_path_len = strlen(path);
4192 arg.repo = repo;
4193 arg.status_cb = status_cb;
4194 arg.status_arg = status_arg;
4195 arg.cancel_cb = cancel_cb;
4196 arg.cancel_arg = cancel_arg;
4197 arg.report_unchanged = report_unchanged;
4198 arg.no_ignores = no_ignores;
4199 if (!no_ignores) {
4200 err = add_ignores_from_parent_paths(&ignores,
4201 worktree->root_path, path);
4202 if (err)
4203 goto done;
4205 arg.ignores = &ignores;
4206 err = got_fileindex_diff_dir(fileindex, fd,
4207 worktree->root_path, path, repo, &fdiff_cb, &arg);
4209 done:
4210 free_ignores(&ignores);
4211 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4212 if (fd != -1 && close(fd) == -1 && err == NULL)
4213 err = got_error_from_errno("close");
4214 free(ondisk_path);
4215 return err;
4218 const struct got_error *
4219 got_worktree_status(struct got_worktree *worktree,
4220 struct got_pathlist_head *paths, struct got_repository *repo,
4221 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4222 got_cancel_cb cancel_cb, void *cancel_arg)
4224 const struct got_error *err = NULL;
4225 char *fileindex_path = NULL;
4226 struct got_fileindex *fileindex = NULL;
4227 struct got_pathlist_entry *pe;
4229 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4230 if (err)
4231 return err;
4233 TAILQ_FOREACH(pe, paths, entry) {
4234 err = worktree_status(worktree, pe->path, fileindex, repo,
4235 status_cb, status_arg, cancel_cb, cancel_arg,
4236 no_ignores, 0);
4237 if (err)
4238 break;
4240 free(fileindex_path);
4241 got_fileindex_free(fileindex);
4242 return err;
4245 const struct got_error *
4246 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4247 const char *arg)
4249 const struct got_error *err = NULL;
4250 char *resolved = NULL, *cwd = NULL, *path = NULL;
4251 size_t len;
4252 struct stat sb;
4253 char *abspath = NULL;
4254 char canonpath[PATH_MAX];
4256 *wt_path = NULL;
4258 cwd = getcwd(NULL, 0);
4259 if (cwd == NULL)
4260 return got_error_from_errno("getcwd");
4262 if (lstat(arg, &sb) == -1) {
4263 if (errno != ENOENT) {
4264 err = got_error_from_errno2("lstat", arg);
4265 goto done;
4267 sb.st_mode = 0;
4269 if (S_ISLNK(sb.st_mode)) {
4271 * We cannot use realpath(3) with symlinks since we want to
4272 * operate on the symlink itself.
4273 * But we can make the path absolute, assuming it is relative
4274 * to the current working directory, and then canonicalize it.
4276 if (!got_path_is_absolute(arg)) {
4277 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4278 err = got_error_from_errno("asprintf");
4279 goto done;
4283 err = got_canonpath(abspath ? abspath : arg, canonpath,
4284 sizeof(canonpath));
4285 if (err)
4286 goto done;
4287 resolved = strdup(canonpath);
4288 if (resolved == NULL) {
4289 err = got_error_from_errno("strdup");
4290 goto done;
4292 } else {
4293 resolved = realpath(arg, NULL);
4294 if (resolved == NULL) {
4295 if (errno != ENOENT) {
4296 err = got_error_from_errno2("realpath", arg);
4297 goto done;
4299 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4300 err = got_error_from_errno("asprintf");
4301 goto done;
4303 err = got_canonpath(abspath, canonpath,
4304 sizeof(canonpath));
4305 if (err)
4306 goto done;
4307 resolved = strdup(canonpath);
4308 if (resolved == NULL) {
4309 err = got_error_from_errno("strdup");
4310 goto done;
4315 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4316 strlen(got_worktree_get_root_path(worktree)))) {
4317 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4318 goto done;
4321 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4322 err = got_path_skip_common_ancestor(&path,
4323 got_worktree_get_root_path(worktree), resolved);
4324 if (err)
4325 goto done;
4326 } else {
4327 path = strdup("");
4328 if (path == NULL) {
4329 err = got_error_from_errno("strdup");
4330 goto done;
4334 /* XXX status walk can't deal with trailing slash! */
4335 len = strlen(path);
4336 while (len > 0 && path[len - 1] == '/') {
4337 path[len - 1] = '\0';
4338 len--;
4340 done:
4341 free(abspath);
4342 free(resolved);
4343 free(cwd);
4344 if (err == NULL)
4345 *wt_path = path;
4346 else
4347 free(path);
4348 return err;
4351 struct schedule_addition_args {
4352 struct got_worktree *worktree;
4353 struct got_fileindex *fileindex;
4354 got_worktree_checkout_cb progress_cb;
4355 void *progress_arg;
4356 struct got_repository *repo;
4359 static int
4360 add_noop_status(unsigned char status)
4362 return (status == GOT_STATUS_ADD ||
4363 status == GOT_STATUS_MODIFY ||
4364 status == GOT_STATUS_CONFLICT ||
4365 status == GOT_STATUS_MODE_CHANGE ||
4366 status == GOT_STATUS_NO_CHANGE);
4369 static const struct got_error *
4370 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4371 const char *relpath, struct got_object_id *blob_id,
4372 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4373 int dirfd, const char *de_name)
4375 struct schedule_addition_args *a = arg;
4376 const struct got_error *err = NULL;
4377 struct got_fileindex_entry *ie;
4378 struct stat sb;
4379 char *ondisk_path;
4381 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4382 relpath) == -1)
4383 return got_error_from_errno("asprintf");
4385 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4386 if (ie) {
4387 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4388 de_name, a->repo);
4389 if (err)
4390 goto done;
4391 /* Re-adding an existing entry is a no-op. */
4392 if (staged_status == GOT_STATUS_NO_CHANGE &&
4393 add_noop_status(status))
4394 goto done;
4395 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4396 if (err)
4397 goto done;
4400 if (status != GOT_STATUS_UNVERSIONED) {
4401 if (status == GOT_STATUS_NONEXISTENT)
4402 err = got_error_set_errno(ENOENT, ondisk_path);
4403 else
4404 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4405 goto done;
4408 err = got_fileindex_entry_alloc(&ie, relpath);
4409 if (err)
4410 goto done;
4411 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4412 relpath, NULL, NULL, 1);
4413 if (err) {
4414 got_fileindex_entry_free(ie);
4415 goto done;
4417 err = got_fileindex_entry_add(a->fileindex, ie);
4418 if (err) {
4419 got_fileindex_entry_free(ie);
4420 goto done;
4422 done:
4423 free(ondisk_path);
4424 if (err)
4425 return err;
4426 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4427 return NULL;
4428 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4431 const struct got_error *
4432 got_worktree_schedule_add(struct got_worktree *worktree,
4433 struct got_pathlist_head *paths,
4434 got_worktree_checkout_cb progress_cb, void *progress_arg,
4435 struct got_repository *repo, int no_ignores)
4437 struct got_fileindex *fileindex = NULL;
4438 char *fileindex_path = NULL;
4439 const struct got_error *err = NULL, *sync_err, *unlockerr;
4440 struct got_pathlist_entry *pe;
4441 struct schedule_addition_args saa;
4443 err = lock_worktree(worktree, LOCK_EX);
4444 if (err)
4445 return err;
4447 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4448 if (err)
4449 goto done;
4451 saa.worktree = worktree;
4452 saa.fileindex = fileindex;
4453 saa.progress_cb = progress_cb;
4454 saa.progress_arg = progress_arg;
4455 saa.repo = repo;
4457 TAILQ_FOREACH(pe, paths, entry) {
4458 err = worktree_status(worktree, pe->path, fileindex, repo,
4459 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4460 if (err)
4461 break;
4463 sync_err = sync_fileindex(fileindex, fileindex_path);
4464 if (sync_err && err == NULL)
4465 err = sync_err;
4466 done:
4467 free(fileindex_path);
4468 if (fileindex)
4469 got_fileindex_free(fileindex);
4470 unlockerr = lock_worktree(worktree, LOCK_SH);
4471 if (unlockerr && err == NULL)
4472 err = unlockerr;
4473 return err;
4476 struct schedule_deletion_args {
4477 struct got_worktree *worktree;
4478 struct got_fileindex *fileindex;
4479 got_worktree_delete_cb progress_cb;
4480 void *progress_arg;
4481 struct got_repository *repo;
4482 int delete_local_mods;
4483 int keep_on_disk;
4484 int ignore_missing_paths;
4485 const char *status_path;
4486 size_t status_path_len;
4487 const char *status_codes;
4490 static const struct got_error *
4491 schedule_for_deletion(void *arg, unsigned char status,
4492 unsigned char staged_status, const char *relpath,
4493 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4494 struct got_object_id *commit_id, int dirfd, const char *de_name)
4496 struct schedule_deletion_args *a = arg;
4497 const struct got_error *err = NULL;
4498 struct got_fileindex_entry *ie = NULL;
4499 struct stat sb;
4500 char *ondisk_path;
4502 if (status == GOT_STATUS_NONEXISTENT) {
4503 if (a->ignore_missing_paths)
4504 return NULL;
4505 return got_error_set_errno(ENOENT, relpath);
4508 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4509 if (ie == NULL)
4510 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4512 staged_status = get_staged_status(ie);
4513 if (staged_status != GOT_STATUS_NO_CHANGE) {
4514 if (staged_status == GOT_STATUS_DELETE)
4515 return NULL;
4516 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4519 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4520 relpath) == -1)
4521 return got_error_from_errno("asprintf");
4523 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4524 a->repo);
4525 if (err)
4526 goto done;
4528 if (a->status_codes) {
4529 size_t ncodes = strlen(a->status_codes);
4530 int i;
4531 for (i = 0; i < ncodes ; i++) {
4532 if (status == a->status_codes[i])
4533 break;
4535 if (i == ncodes) {
4536 /* Do not delete files in non-matching status. */
4537 free(ondisk_path);
4538 return NULL;
4540 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4541 a->status_codes[i] != GOT_STATUS_MISSING) {
4542 static char msg[64];
4543 snprintf(msg, sizeof(msg),
4544 "invalid status code '%c'", a->status_codes[i]);
4545 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4546 goto done;
4550 if (status != GOT_STATUS_NO_CHANGE) {
4551 if (status == GOT_STATUS_DELETE)
4552 goto done;
4553 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4554 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4555 goto done;
4557 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4558 err = got_error_set_errno(ENOENT, relpath);
4559 goto done;
4561 if (status != GOT_STATUS_MODIFY &&
4562 status != GOT_STATUS_MISSING) {
4563 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4564 goto done;
4568 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4569 size_t root_len;
4571 if (dirfd != -1) {
4572 if (unlinkat(dirfd, de_name, 0) == -1) {
4573 err = got_error_from_errno2("unlinkat",
4574 ondisk_path);
4575 goto done;
4577 } else if (unlink(ondisk_path) == -1) {
4578 err = got_error_from_errno2("unlink", ondisk_path);
4579 goto done;
4582 root_len = strlen(a->worktree->root_path);
4583 do {
4584 char *parent;
4586 err = got_path_dirname(&parent, ondisk_path);
4587 if (err)
4588 goto done;
4589 free(ondisk_path);
4590 ondisk_path = parent;
4591 if (got_path_cmp(ondisk_path, a->status_path,
4592 strlen(ondisk_path), a->status_path_len) != 0 &&
4593 !got_path_is_child(ondisk_path, a->status_path,
4594 a->status_path_len))
4595 break;
4596 if (rmdir(ondisk_path) == -1) {
4597 if (errno != ENOTEMPTY)
4598 err = got_error_from_errno2("rmdir",
4599 ondisk_path);
4600 break;
4602 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4603 strlen(ondisk_path), root_len) != 0);
4606 if (got_fileindex_entry_has_blob(ie))
4607 got_fileindex_entry_mark_deleted_from_disk(ie);
4608 else
4609 got_fileindex_entry_remove(a->fileindex, ie);
4610 done:
4611 free(ondisk_path);
4612 if (err)
4613 return err;
4614 if (status == GOT_STATUS_DELETE)
4615 return NULL;
4616 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4617 staged_status, relpath);
4620 const struct got_error *
4621 got_worktree_schedule_delete(struct got_worktree *worktree,
4622 struct got_pathlist_head *paths, int delete_local_mods,
4623 const char *status_codes,
4624 got_worktree_delete_cb progress_cb, void *progress_arg,
4625 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4627 struct got_fileindex *fileindex = NULL;
4628 char *fileindex_path = NULL;
4629 const struct got_error *err = NULL, *sync_err, *unlockerr;
4630 struct got_pathlist_entry *pe;
4631 struct schedule_deletion_args sda;
4633 err = lock_worktree(worktree, LOCK_EX);
4634 if (err)
4635 return err;
4637 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4638 if (err)
4639 goto done;
4641 sda.worktree = worktree;
4642 sda.fileindex = fileindex;
4643 sda.progress_cb = progress_cb;
4644 sda.progress_arg = progress_arg;
4645 sda.repo = repo;
4646 sda.delete_local_mods = delete_local_mods;
4647 sda.keep_on_disk = keep_on_disk;
4648 sda.ignore_missing_paths = ignore_missing_paths;
4649 sda.status_codes = status_codes;
4651 TAILQ_FOREACH(pe, paths, entry) {
4652 char *ondisk_status_path;
4654 if (asprintf(&ondisk_status_path, "%s%s%s",
4655 got_worktree_get_root_path(worktree),
4656 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4657 err = got_error_from_errno("asprintf");
4658 goto done;
4660 sda.status_path = ondisk_status_path;
4661 sda.status_path_len = strlen(ondisk_status_path);
4662 err = worktree_status(worktree, pe->path, fileindex, repo,
4663 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4664 free(ondisk_status_path);
4665 if (err)
4666 break;
4668 sync_err = sync_fileindex(fileindex, fileindex_path);
4669 if (sync_err && err == NULL)
4670 err = sync_err;
4671 done:
4672 free(fileindex_path);
4673 if (fileindex)
4674 got_fileindex_free(fileindex);
4675 unlockerr = lock_worktree(worktree, LOCK_SH);
4676 if (unlockerr && err == NULL)
4677 err = unlockerr;
4678 return err;
4681 static const struct got_error *
4682 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4684 const struct got_error *err = NULL;
4685 char *line = NULL;
4686 size_t linesize = 0, n;
4687 ssize_t linelen;
4689 linelen = getline(&line, &linesize, infile);
4690 if (linelen == -1) {
4691 if (ferror(infile)) {
4692 err = got_error_from_errno("getline");
4693 goto done;
4695 return NULL;
4697 if (outfile) {
4698 n = fwrite(line, 1, linelen, outfile);
4699 if (n != linelen) {
4700 err = got_ferror(outfile, GOT_ERR_IO);
4701 goto done;
4704 if (rejectfile) {
4705 n = fwrite(line, 1, linelen, rejectfile);
4706 if (n != linelen)
4707 err = got_ferror(rejectfile, GOT_ERR_IO);
4709 done:
4710 free(line);
4711 return err;
4714 static const struct got_error *
4715 skip_one_line(FILE *f)
4717 char *line = NULL;
4718 size_t linesize = 0;
4719 ssize_t linelen;
4721 linelen = getline(&line, &linesize, f);
4722 if (linelen == -1) {
4723 if (ferror(f))
4724 return got_error_from_errno("getline");
4725 return NULL;
4727 free(line);
4728 return NULL;
4731 static const struct got_error *
4732 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4733 int start_old, int end_old, int start_new, int end_new,
4734 FILE *outfile, FILE *rejectfile)
4736 const struct got_error *err;
4738 /* Copy old file's lines leading up to patch. */
4739 while (!feof(f1) && *line_cur1 < start_old) {
4740 err = copy_one_line(f1, outfile, NULL);
4741 if (err)
4742 return err;
4743 (*line_cur1)++;
4745 /* Skip new file's lines leading up to patch. */
4746 while (!feof(f2) && *line_cur2 < start_new) {
4747 if (rejectfile)
4748 err = copy_one_line(f2, NULL, rejectfile);
4749 else
4750 err = skip_one_line(f2);
4751 if (err)
4752 return err;
4753 (*line_cur2)++;
4755 /* Copy patched lines. */
4756 while (!feof(f2) && *line_cur2 <= end_new) {
4757 err = copy_one_line(f2, outfile, NULL);
4758 if (err)
4759 return err;
4760 (*line_cur2)++;
4762 /* Skip over old file's replaced lines. */
4763 while (!feof(f1) && *line_cur1 <= end_old) {
4764 if (rejectfile)
4765 err = copy_one_line(f1, NULL, rejectfile);
4766 else
4767 err = skip_one_line(f1);
4768 if (err)
4769 return err;
4770 (*line_cur1)++;
4773 return NULL;
4776 static const struct got_error *
4777 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4778 FILE *outfile, FILE *rejectfile)
4780 const struct got_error *err;
4782 if (outfile) {
4783 /* Copy old file's lines until EOF. */
4784 while (!feof(f1)) {
4785 err = copy_one_line(f1, outfile, NULL);
4786 if (err)
4787 return err;
4788 (*line_cur1)++;
4791 if (rejectfile) {
4792 /* Copy new file's lines until EOF. */
4793 while (!feof(f2)) {
4794 err = copy_one_line(f2, NULL, rejectfile);
4795 if (err)
4796 return err;
4797 (*line_cur2)++;
4801 return NULL;
4804 static const struct got_error *
4805 apply_or_reject_change(int *choice, int *nchunks_used,
4806 struct diff_result *diff_result, int n,
4807 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4808 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4809 got_worktree_patch_cb patch_cb, void *patch_arg)
4811 const struct got_error *err = NULL;
4812 struct diff_chunk_context cc = {};
4813 int start_old, end_old, start_new, end_new;
4814 FILE *hunkfile;
4815 struct diff_output_unidiff_state *diff_state;
4816 struct diff_input_info diff_info;
4817 int rc;
4819 *choice = GOT_PATCH_CHOICE_NONE;
4821 /* Get changed line numbers without context lines for copy_change(). */
4822 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4823 start_old = cc.left.start;
4824 end_old = cc.left.end;
4825 start_new = cc.right.start;
4826 end_new = cc.right.end;
4828 /* Get the same change with context lines for display. */
4829 memset(&cc, 0, sizeof(cc));
4830 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4832 memset(&diff_info, 0, sizeof(diff_info));
4833 diff_info.left_path = relpath;
4834 diff_info.right_path = relpath;
4836 diff_state = diff_output_unidiff_state_alloc();
4837 if (diff_state == NULL)
4838 return got_error_set_errno(ENOMEM,
4839 "diff_output_unidiff_state_alloc");
4841 hunkfile = got_opentemp();
4842 if (hunkfile == NULL) {
4843 err = got_error_from_errno("got_opentemp");
4844 goto done;
4847 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4848 diff_result, &cc);
4849 if (rc != DIFF_RC_OK) {
4850 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4851 goto done;
4854 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4855 err = got_ferror(hunkfile, GOT_ERR_IO);
4856 goto done;
4859 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4860 hunkfile, changeno, nchanges);
4861 if (err)
4862 goto done;
4864 switch (*choice) {
4865 case GOT_PATCH_CHOICE_YES:
4866 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4867 end_old, start_new, end_new, outfile, rejectfile);
4868 break;
4869 case GOT_PATCH_CHOICE_NO:
4870 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 end_old, start_new, end_new, rejectfile, outfile);
4872 break;
4873 case GOT_PATCH_CHOICE_QUIT:
4874 break;
4875 default:
4876 err = got_error(GOT_ERR_PATCH_CHOICE);
4877 break;
4879 done:
4880 diff_output_unidiff_state_free(diff_state);
4881 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4882 err = got_error_from_errno("fclose");
4883 return err;
4886 struct revert_file_args {
4887 struct got_worktree *worktree;
4888 struct got_fileindex *fileindex;
4889 got_worktree_checkout_cb progress_cb;
4890 void *progress_arg;
4891 got_worktree_patch_cb patch_cb;
4892 void *patch_arg;
4893 struct got_repository *repo;
4894 int unlink_added_files;
4895 struct got_pathlist_head *added_files_to_unlink;
4898 static const struct got_error *
4899 create_patched_content(char **path_outfile, int reverse_patch,
4900 struct got_object_id *blob_id, const char *path2,
4901 int dirfd2, const char *de_name2,
4902 const char *relpath, struct got_repository *repo,
4903 got_worktree_patch_cb patch_cb, void *patch_arg)
4905 const struct got_error *err, *free_err;
4906 struct got_blob_object *blob = NULL;
4907 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4908 int fd = -1, fd2 = -1;
4909 char link_target[PATH_MAX];
4910 ssize_t link_len = 0;
4911 char *path1 = NULL, *id_str = NULL;
4912 struct stat sb2;
4913 struct got_diffreg_result *diffreg_result = NULL;
4914 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4915 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4917 *path_outfile = NULL;
4919 err = got_object_id_str(&id_str, blob_id);
4920 if (err)
4921 return err;
4923 if (dirfd2 != -1) {
4924 fd2 = openat(dirfd2, de_name2,
4925 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 if (fd2 == -1) {
4927 if (!got_err_open_nofollow_on_symlink()) {
4928 err = got_error_from_errno2("openat", path2);
4929 goto done;
4931 link_len = readlinkat(dirfd2, de_name2,
4932 link_target, sizeof(link_target));
4933 if (link_len == -1) {
4934 return got_error_from_errno2("readlinkat",
4935 path2);
4937 sb2.st_mode = S_IFLNK;
4938 sb2.st_size = link_len;
4940 } else {
4941 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4942 if (fd2 == -1) {
4943 if (!got_err_open_nofollow_on_symlink()) {
4944 err = got_error_from_errno2("open", path2);
4945 goto done;
4947 link_len = readlink(path2, link_target,
4948 sizeof(link_target));
4949 if (link_len == -1)
4950 return got_error_from_errno2("readlink", path2);
4951 sb2.st_mode = S_IFLNK;
4952 sb2.st_size = link_len;
4955 if (fd2 != -1) {
4956 if (fstat(fd2, &sb2) == -1) {
4957 err = got_error_from_errno2("fstat", path2);
4958 goto done;
4961 f2 = fdopen(fd2, "r");
4962 if (f2 == NULL) {
4963 err = got_error_from_errno2("fdopen", path2);
4964 goto done;
4966 fd2 = -1;
4967 } else {
4968 size_t n;
4969 f2 = got_opentemp();
4970 if (f2 == NULL) {
4971 err = got_error_from_errno2("got_opentemp", path2);
4972 goto done;
4974 n = fwrite(link_target, 1, link_len, f2);
4975 if (n != link_len) {
4976 err = got_ferror(f2, GOT_ERR_IO);
4977 goto done;
4979 if (fflush(f2) == EOF) {
4980 err = got_error_from_errno("fflush");
4981 goto done;
4983 rewind(f2);
4986 fd = got_opentempfd();
4987 if (fd == -1) {
4988 err = got_error_from_errno("got_opentempfd");
4989 goto done;
4992 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4993 if (err)
4994 goto done;
4996 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4997 if (err)
4998 goto done;
5000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5001 if (err)
5002 goto done;
5004 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5005 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5006 if (err)
5007 goto done;
5009 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5010 "");
5011 if (err)
5012 goto done;
5014 if (fseek(f1, 0L, SEEK_SET) == -1)
5015 return got_ferror(f1, GOT_ERR_IO);
5016 if (fseek(f2, 0L, SEEK_SET) == -1)
5017 return got_ferror(f2, GOT_ERR_IO);
5019 /* Count the number of actual changes in the diff result. */
5020 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5021 struct diff_chunk_context cc = {};
5022 diff_chunk_context_load_change(&cc, &nchunks_used,
5023 diffreg_result->result, n, 0);
5024 nchanges++;
5026 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5027 int choice;
5028 err = apply_or_reject_change(&choice, &nchunks_used,
5029 diffreg_result->result, n, relpath, f1, f2,
5030 &line_cur1, &line_cur2,
5031 reverse_patch ? NULL : outfile,
5032 reverse_patch ? outfile : NULL,
5033 ++i, nchanges, patch_cb, patch_arg);
5034 if (err)
5035 goto done;
5036 if (choice == GOT_PATCH_CHOICE_YES)
5037 have_content = 1;
5038 else if (choice == GOT_PATCH_CHOICE_QUIT)
5039 break;
5041 if (have_content) {
5042 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5043 reverse_patch ? NULL : outfile,
5044 reverse_patch ? outfile : NULL);
5045 if (err)
5046 goto done;
5048 if (!S_ISLNK(sb2.st_mode)) {
5049 mode_t mode;
5051 mode = apply_umask(sb2.st_mode);
5052 if (fchmod(fileno(outfile), mode) == -1) {
5053 err = got_error_from_errno2("fchmod", path2);
5054 goto done;
5058 done:
5059 free(id_str);
5060 if (fd != -1 && close(fd) == -1 && err == NULL)
5061 err = got_error_from_errno("close");
5062 if (blob)
5063 got_object_blob_close(blob);
5064 free_err = got_diffreg_result_free(diffreg_result);
5065 if (err == NULL)
5066 err = free_err;
5067 if (f1 && fclose(f1) == EOF && err == NULL)
5068 err = got_error_from_errno2("fclose", path1);
5069 if (f2 && fclose(f2) == EOF && err == NULL)
5070 err = got_error_from_errno2("fclose", path2);
5071 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5072 err = got_error_from_errno2("close", path2);
5073 if (outfile && fclose(outfile) == EOF && err == NULL)
5074 err = got_error_from_errno2("fclose", *path_outfile);
5075 if (path1 && unlink(path1) == -1 && err == NULL)
5076 err = got_error_from_errno2("unlink", path1);
5077 if (err || !have_content) {
5078 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5079 err = got_error_from_errno2("unlink", *path_outfile);
5080 free(*path_outfile);
5081 *path_outfile = NULL;
5083 free(path1);
5084 return err;
5087 static const struct got_error *
5088 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5089 const char *relpath, struct got_object_id *blob_id,
5090 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5091 int dirfd, const char *de_name)
5093 struct revert_file_args *a = arg;
5094 const struct got_error *err = NULL;
5095 char *parent_path = NULL;
5096 struct got_fileindex_entry *ie;
5097 struct got_commit_object *base_commit = NULL;
5098 struct got_tree_object *tree = NULL;
5099 struct got_object_id *tree_id = NULL;
5100 const struct got_tree_entry *te = NULL;
5101 char *tree_path = NULL, *te_name;
5102 char *ondisk_path = NULL, *path_content = NULL;
5103 struct got_blob_object *blob = NULL;
5104 int fd = -1;
5106 /* Reverting a staged deletion is a no-op. */
5107 if (status == GOT_STATUS_DELETE &&
5108 staged_status != GOT_STATUS_NO_CHANGE)
5109 return NULL;
5111 if (status == GOT_STATUS_UNVERSIONED)
5112 return (*a->progress_cb)(a->progress_arg,
5113 GOT_STATUS_UNVERSIONED, relpath);
5115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5116 if (ie == NULL)
5117 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5119 /* Construct in-repository path of tree which contains this blob. */
5120 err = got_path_dirname(&parent_path, ie->path);
5121 if (err) {
5122 if (err->code != GOT_ERR_BAD_PATH)
5123 goto done;
5124 parent_path = strdup("/");
5125 if (parent_path == NULL) {
5126 err = got_error_from_errno("strdup");
5127 goto done;
5130 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5131 tree_path = strdup(parent_path);
5132 if (tree_path == NULL) {
5133 err = got_error_from_errno("strdup");
5134 goto done;
5136 } else {
5137 if (got_path_is_root_dir(parent_path)) {
5138 tree_path = strdup(a->worktree->path_prefix);
5139 if (tree_path == NULL) {
5140 err = got_error_from_errno("strdup");
5141 goto done;
5143 } else {
5144 if (asprintf(&tree_path, "%s/%s",
5145 a->worktree->path_prefix, parent_path) == -1) {
5146 err = got_error_from_errno("asprintf");
5147 goto done;
5152 err = got_object_open_as_commit(&base_commit, a->repo,
5153 a->worktree->base_commit_id);
5154 if (err)
5155 goto done;
5157 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5158 if (err) {
5159 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5160 (status == GOT_STATUS_ADD ||
5161 staged_status == GOT_STATUS_ADD)))
5162 goto done;
5163 } else {
5164 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5165 if (err)
5166 goto done;
5168 err = got_path_basename(&te_name, ie->path);
5169 if (err)
5170 goto done;
5172 te = got_object_tree_find_entry(tree, te_name);
5173 free(te_name);
5174 if (te == NULL && status != GOT_STATUS_ADD &&
5175 staged_status != GOT_STATUS_ADD) {
5176 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5177 goto done;
5181 switch (status) {
5182 case GOT_STATUS_ADD:
5183 if (a->patch_cb) {
5184 int choice = GOT_PATCH_CHOICE_NONE;
5185 err = (*a->patch_cb)(&choice, a->patch_arg,
5186 status, ie->path, NULL, 1, 1);
5187 if (err)
5188 goto done;
5189 if (choice != GOT_PATCH_CHOICE_YES)
5190 break;
5192 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5193 ie->path);
5194 if (err)
5195 goto done;
5196 got_fileindex_entry_remove(a->fileindex, ie);
5197 if (a->unlink_added_files) {
5198 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5200 if (a->added_files_to_unlink) {
5201 struct got_pathlist_entry *pe;
5203 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5204 entry) {
5205 if (got_path_cmp(pe->path, relpath,
5206 pe->path_len, strlen(relpath)))
5207 continue;
5208 do_unlink = 1;
5209 break;
5213 if (do_unlink) {
5214 if (asprintf(&ondisk_path, "%s/%s",
5215 got_worktree_get_root_path(a->worktree),
5216 relpath) == -1) {
5217 err = got_error_from_errno("asprintf");
5218 goto done;
5220 if (unlink(ondisk_path) == -1) {
5221 err = got_error_from_errno2("unlink",
5222 ondisk_path);
5223 break;
5227 break;
5228 case GOT_STATUS_DELETE:
5229 if (a->patch_cb) {
5230 int choice = GOT_PATCH_CHOICE_NONE;
5231 err = (*a->patch_cb)(&choice, a->patch_arg,
5232 status, ie->path, NULL, 1, 1);
5233 if (err)
5234 goto done;
5235 if (choice != GOT_PATCH_CHOICE_YES)
5236 break;
5238 /* fall through */
5239 case GOT_STATUS_MODIFY:
5240 case GOT_STATUS_MODE_CHANGE:
5241 case GOT_STATUS_CONFLICT:
5242 case GOT_STATUS_MISSING: {
5243 struct got_object_id id;
5244 if (staged_status == GOT_STATUS_ADD ||
5245 staged_status == GOT_STATUS_MODIFY)
5246 got_fileindex_entry_get_staged_blob_id(&id, ie);
5247 else
5248 got_fileindex_entry_get_blob_id(&id, ie);
5249 fd = got_opentempfd();
5250 if (fd == -1) {
5251 err = got_error_from_errno("got_opentempfd");
5252 goto done;
5255 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5256 if (err)
5257 goto done;
5259 if (asprintf(&ondisk_path, "%s/%s",
5260 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5261 err = got_error_from_errno("asprintf");
5262 goto done;
5265 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5266 status == GOT_STATUS_CONFLICT)) {
5267 int is_bad_symlink = 0;
5268 err = create_patched_content(&path_content, 1, &id,
5269 ondisk_path, dirfd, de_name, ie->path, a->repo,
5270 a->patch_cb, a->patch_arg);
5271 if (err || path_content == NULL)
5272 break;
5273 if (te && S_ISLNK(te->mode)) {
5274 if (unlink(path_content) == -1) {
5275 err = got_error_from_errno2("unlink",
5276 path_content);
5277 break;
5279 err = install_symlink(&is_bad_symlink,
5280 a->worktree, ondisk_path, ie->path,
5281 blob, 0, 1, 0, 0, a->repo,
5282 a->progress_cb, a->progress_arg);
5283 } else {
5284 if (rename(path_content, ondisk_path) == -1) {
5285 err = got_error_from_errno3("rename",
5286 path_content, ondisk_path);
5287 goto done;
5290 } else {
5291 int is_bad_symlink = 0;
5292 if (te && S_ISLNK(te->mode)) {
5293 err = install_symlink(&is_bad_symlink,
5294 a->worktree, ondisk_path, ie->path,
5295 blob, 0, 1, 0, 0, a->repo,
5296 a->progress_cb, a->progress_arg);
5297 } else {
5298 err = install_blob(a->worktree, ondisk_path,
5299 ie->path,
5300 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5301 got_fileindex_perms_to_st(ie), blob,
5302 0, 1, 0, 0, NULL, a->repo,
5303 a->progress_cb, a->progress_arg);
5305 if (err)
5306 goto done;
5307 if (status == GOT_STATUS_DELETE ||
5308 status == GOT_STATUS_MODE_CHANGE) {
5309 err = got_fileindex_entry_update(ie,
5310 a->worktree->root_fd, relpath,
5311 blob->id.sha1,
5312 a->worktree->base_commit_id->sha1, 1);
5313 if (err)
5314 goto done;
5316 if (is_bad_symlink) {
5317 got_fileindex_entry_filetype_set(ie,
5318 GOT_FILEIDX_MODE_BAD_SYMLINK);
5321 break;
5323 default:
5324 break;
5326 done:
5327 free(ondisk_path);
5328 free(path_content);
5329 free(parent_path);
5330 free(tree_path);
5331 if (fd != -1 && close(fd) == -1 && err == NULL)
5332 err = got_error_from_errno("close");
5333 if (blob)
5334 got_object_blob_close(blob);
5335 if (tree)
5336 got_object_tree_close(tree);
5337 free(tree_id);
5338 if (base_commit)
5339 got_object_commit_close(base_commit);
5340 return err;
5343 const struct got_error *
5344 got_worktree_revert(struct got_worktree *worktree,
5345 struct got_pathlist_head *paths,
5346 got_worktree_checkout_cb progress_cb, void *progress_arg,
5347 got_worktree_patch_cb patch_cb, void *patch_arg,
5348 struct got_repository *repo)
5350 struct got_fileindex *fileindex = NULL;
5351 char *fileindex_path = NULL;
5352 const struct got_error *err = NULL, *unlockerr = NULL;
5353 const struct got_error *sync_err = NULL;
5354 struct got_pathlist_entry *pe;
5355 struct revert_file_args rfa;
5357 err = lock_worktree(worktree, LOCK_EX);
5358 if (err)
5359 return err;
5361 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5362 if (err)
5363 goto done;
5365 rfa.worktree = worktree;
5366 rfa.fileindex = fileindex;
5367 rfa.progress_cb = progress_cb;
5368 rfa.progress_arg = progress_arg;
5369 rfa.patch_cb = patch_cb;
5370 rfa.patch_arg = patch_arg;
5371 rfa.repo = repo;
5372 rfa.unlink_added_files = 0;
5373 TAILQ_FOREACH(pe, paths, entry) {
5374 err = worktree_status(worktree, pe->path, fileindex, repo,
5375 revert_file, &rfa, NULL, NULL, 1, 0);
5376 if (err)
5377 break;
5379 sync_err = sync_fileindex(fileindex, fileindex_path);
5380 if (sync_err && err == NULL)
5381 err = sync_err;
5382 done:
5383 free(fileindex_path);
5384 if (fileindex)
5385 got_fileindex_free(fileindex);
5386 unlockerr = lock_worktree(worktree, LOCK_SH);
5387 if (unlockerr && err == NULL)
5388 err = unlockerr;
5389 return err;
5392 static void
5393 free_commitable(struct got_commitable *ct)
5395 free(ct->path);
5396 free(ct->in_repo_path);
5397 free(ct->ondisk_path);
5398 free(ct->blob_id);
5399 free(ct->base_blob_id);
5400 free(ct->staged_blob_id);
5401 free(ct->base_commit_id);
5402 free(ct);
5405 struct collect_commitables_arg {
5406 struct got_pathlist_head *commitable_paths;
5407 struct got_repository *repo;
5408 struct got_worktree *worktree;
5409 struct got_fileindex *fileindex;
5410 int have_staged_files;
5411 int allow_bad_symlinks;
5412 int diff_header_shown;
5413 int commit_conflicts;
5414 FILE *diff_outfile;
5415 FILE *f1;
5416 FILE *f2;
5420 * Create a file which contains the target path of a symlink so we can feed
5421 * it as content to the diff engine.
5423 static const struct got_error *
5424 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5425 const char *abspath)
5427 const struct got_error *err = NULL;
5428 char target_path[PATH_MAX];
5429 ssize_t target_len, outlen;
5431 *fd = -1;
5433 if (dirfd != -1) {
5434 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5435 if (target_len == -1)
5436 return got_error_from_errno2("readlinkat", abspath);
5437 } else {
5438 target_len = readlink(abspath, target_path, PATH_MAX);
5439 if (target_len == -1)
5440 return got_error_from_errno2("readlink", abspath);
5443 *fd = got_opentempfd();
5444 if (*fd == -1)
5445 return got_error_from_errno("got_opentempfd");
5447 outlen = write(*fd, target_path, target_len);
5448 if (outlen == -1) {
5449 err = got_error_from_errno("got_opentempfd");
5450 goto done;
5453 if (lseek(*fd, 0, SEEK_SET) == -1) {
5454 err = got_error_from_errno2("lseek", abspath);
5455 goto done;
5457 done:
5458 if (err) {
5459 close(*fd);
5460 *fd = -1;
5462 return err;
5465 static const struct got_error *
5466 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5467 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5468 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5470 const struct got_error *err = NULL;
5471 struct got_blob_object *blob1 = NULL;
5472 int fd = -1, fd1 = -1, fd2 = -1;
5473 FILE *ondisk_file = NULL;
5474 char *label1 = NULL;
5475 struct stat sb;
5476 off_t size1 = 0;
5477 int f2_exists = 0;
5478 char *id_str = NULL;
5480 memset(&sb, 0, sizeof(sb));
5482 if (diff_staged) {
5483 if (ct->staged_status != GOT_STATUS_MODIFY &&
5484 ct->staged_status != GOT_STATUS_ADD &&
5485 ct->staged_status != GOT_STATUS_DELETE)
5486 return NULL;
5487 } else {
5488 if (ct->status != GOT_STATUS_MODIFY &&
5489 ct->status != GOT_STATUS_ADD &&
5490 ct->status != GOT_STATUS_DELETE &&
5491 ct->status != GOT_STATUS_CONFLICT)
5492 return NULL;
5495 err = got_opentemp_truncate(f1);
5496 if (err)
5497 return got_error_from_errno("got_opentemp_truncate");
5498 err = got_opentemp_truncate(f2);
5499 if (err)
5500 return got_error_from_errno("got_opentemp_truncate");
5502 if (!*diff_header_shown) {
5503 err = got_object_id_str(&id_str, worktree->base_commit_id);
5504 if (err)
5505 return err;
5506 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5507 got_worktree_get_root_path(worktree));
5508 fprintf(diff_outfile, "commit - %s\n", id_str);
5509 fprintf(diff_outfile, "path + %s%s\n",
5510 got_worktree_get_root_path(worktree),
5511 diff_staged ? " (staged changes)" : "");
5512 *diff_header_shown = 1;
5515 if (diff_staged) {
5516 const char *label1 = NULL, *label2 = NULL;
5517 switch (ct->staged_status) {
5518 case GOT_STATUS_MODIFY:
5519 label1 = ct->path;
5520 label2 = ct->path;
5521 break;
5522 case GOT_STATUS_ADD:
5523 label2 = ct->path;
5524 break;
5525 case GOT_STATUS_DELETE:
5526 label1 = ct->path;
5527 break;
5528 default:
5529 return got_error(GOT_ERR_FILE_STATUS);
5531 fd1 = got_opentempfd();
5532 if (fd1 == -1) {
5533 err = got_error_from_errno("got_opentempfd");
5534 goto done;
5536 fd2 = got_opentempfd();
5537 if (fd2 == -1) {
5538 err = got_error_from_errno("got_opentempfd");
5539 goto done;
5541 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5542 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5543 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5544 NULL, repo, diff_outfile);
5545 goto done;
5548 fd1 = got_opentempfd();
5549 if (fd1 == -1) {
5550 err = got_error_from_errno("got_opentempfd");
5551 goto done;
5554 if (ct->status != GOT_STATUS_ADD) {
5555 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5556 8192, fd1);
5557 if (err)
5558 goto done;
5561 if (ct->status != GOT_STATUS_DELETE) {
5562 if (dirfd != -1) {
5563 fd = openat(dirfd, de_name,
5564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5565 if (fd == -1) {
5566 if (!got_err_open_nofollow_on_symlink()) {
5567 err = got_error_from_errno2("openat",
5568 ct->ondisk_path);
5569 goto done;
5571 err = get_symlink_target_file(&fd, dirfd,
5572 de_name, ct->ondisk_path);
5573 if (err)
5574 goto done;
5576 } else {
5577 fd = open(ct->ondisk_path,
5578 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5579 if (fd == -1) {
5580 if (!got_err_open_nofollow_on_symlink()) {
5581 err = got_error_from_errno2("open",
5582 ct->ondisk_path);
5583 goto done;
5585 err = get_symlink_target_file(&fd, dirfd,
5586 de_name, ct->ondisk_path);
5587 if (err)
5588 goto done;
5591 if (fstatat(fd, ct->ondisk_path, &sb,
5592 AT_SYMLINK_NOFOLLOW) == -1) {
5593 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5594 goto done;
5596 ondisk_file = fdopen(fd, "r");
5597 if (ondisk_file == NULL) {
5598 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5599 goto done;
5601 fd = -1;
5602 f2_exists = 1;
5605 if (blob1) {
5606 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5607 f1, blob1);
5608 if (err)
5609 goto done;
5612 err = got_diff_blob_file(blob1, f1, size1, label1,
5613 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5614 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5615 done:
5616 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5617 err = got_error_from_errno("close");
5618 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5619 err = got_error_from_errno("close");
5620 if (blob1)
5621 got_object_blob_close(blob1);
5622 if (fd != -1 && close(fd) == -1 && err == NULL)
5623 err = got_error_from_errno("close");
5624 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5625 err = got_error_from_errno("fclose");
5626 return err;
5629 static const struct got_error *
5630 collect_commitables(void *arg, unsigned char status,
5631 unsigned char staged_status, const char *relpath,
5632 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5633 struct got_object_id *commit_id, int dirfd, const char *de_name)
5635 struct collect_commitables_arg *a = arg;
5636 const struct got_error *err = NULL;
5637 struct got_commitable *ct = NULL;
5638 struct got_pathlist_entry *new = NULL;
5639 char *parent_path = NULL, *path = NULL;
5640 struct stat sb;
5642 if (a->have_staged_files) {
5643 if (staged_status != GOT_STATUS_MODIFY &&
5644 staged_status != GOT_STATUS_ADD &&
5645 staged_status != GOT_STATUS_DELETE)
5646 return NULL;
5647 } else {
5648 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5649 printf("C %s\n", relpath);
5650 return got_error(GOT_ERR_COMMIT_CONFLICT);
5653 if (status != GOT_STATUS_MODIFY &&
5654 status != GOT_STATUS_MODE_CHANGE &&
5655 status != GOT_STATUS_ADD &&
5656 status != GOT_STATUS_DELETE &&
5657 status != GOT_STATUS_CONFLICT)
5658 return NULL;
5661 if (asprintf(&path, "/%s", relpath) == -1) {
5662 err = got_error_from_errno("asprintf");
5663 goto done;
5665 if (strcmp(path, "/") == 0) {
5666 parent_path = strdup("");
5667 if (parent_path == NULL)
5668 return got_error_from_errno("strdup");
5669 } else {
5670 err = got_path_dirname(&parent_path, path);
5671 if (err)
5672 return err;
5675 ct = calloc(1, sizeof(*ct));
5676 if (ct == NULL) {
5677 err = got_error_from_errno("calloc");
5678 goto done;
5681 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5682 relpath) == -1) {
5683 err = got_error_from_errno("asprintf");
5684 goto done;
5687 if (staged_status == GOT_STATUS_ADD ||
5688 staged_status == GOT_STATUS_MODIFY) {
5689 struct got_fileindex_entry *ie;
5690 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5691 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5692 case GOT_FILEIDX_MODE_REGULAR_FILE:
5693 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5694 ct->mode = S_IFREG;
5695 break;
5696 case GOT_FILEIDX_MODE_SYMLINK:
5697 ct->mode = S_IFLNK;
5698 break;
5699 default:
5700 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5701 goto done;
5703 ct->mode |= got_fileindex_entry_perms_get(ie);
5704 } else if (status != GOT_STATUS_DELETE &&
5705 staged_status != GOT_STATUS_DELETE) {
5706 if (dirfd != -1) {
5707 if (fstatat(dirfd, de_name, &sb,
5708 AT_SYMLINK_NOFOLLOW) == -1) {
5709 err = got_error_from_errno2("fstatat",
5710 ct->ondisk_path);
5711 goto done;
5713 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5714 err = got_error_from_errno2("lstat", ct->ondisk_path);
5715 goto done;
5717 ct->mode = sb.st_mode;
5720 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5721 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5722 relpath) == -1) {
5723 err = got_error_from_errno("asprintf");
5724 goto done;
5727 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5728 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5729 int is_bad_symlink;
5730 char target_path[PATH_MAX];
5731 ssize_t target_len;
5732 target_len = readlink(ct->ondisk_path, target_path,
5733 sizeof(target_path));
5734 if (target_len == -1) {
5735 err = got_error_from_errno2("readlink",
5736 ct->ondisk_path);
5737 goto done;
5739 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5740 target_len, ct->ondisk_path, a->worktree->root_path,
5741 a->worktree->meta_dir);
5742 if (err)
5743 goto done;
5744 if (is_bad_symlink) {
5745 err = got_error_path(ct->ondisk_path,
5746 GOT_ERR_BAD_SYMLINK);
5747 goto done;
5752 ct->status = status;
5753 ct->staged_status = staged_status;
5754 ct->blob_id = NULL; /* will be filled in when blob gets created */
5755 if (ct->status != GOT_STATUS_ADD &&
5756 ct->staged_status != GOT_STATUS_ADD) {
5757 ct->base_blob_id = got_object_id_dup(blob_id);
5758 if (ct->base_blob_id == NULL) {
5759 err = got_error_from_errno("got_object_id_dup");
5760 goto done;
5762 ct->base_commit_id = got_object_id_dup(commit_id);
5763 if (ct->base_commit_id == NULL) {
5764 err = got_error_from_errno("got_object_id_dup");
5765 goto done;
5768 if (ct->staged_status == GOT_STATUS_ADD ||
5769 ct->staged_status == GOT_STATUS_MODIFY) {
5770 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5771 if (ct->staged_blob_id == NULL) {
5772 err = got_error_from_errno("got_object_id_dup");
5773 goto done;
5776 ct->path = strdup(path);
5777 if (ct->path == NULL) {
5778 err = got_error_from_errno("strdup");
5779 goto done;
5782 if (a->diff_outfile) {
5783 err = append_ct_diff(ct, &a->diff_header_shown,
5784 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5785 a->have_staged_files, a->repo, a->worktree);
5786 if (err)
5787 goto done;
5790 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5791 done:
5792 if (ct && (err || new == NULL))
5793 free_commitable(ct);
5794 free(parent_path);
5795 free(path);
5796 return err;
5799 static const struct got_error *write_tree(struct got_object_id **, int *,
5800 struct got_tree_object *, const char *, struct got_pathlist_head *,
5801 got_worktree_status_cb status_cb, void *status_arg,
5802 struct got_repository *);
5804 static const struct got_error *
5805 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5806 struct got_tree_entry *te, const char *parent_path,
5807 struct got_pathlist_head *commitable_paths,
5808 got_worktree_status_cb status_cb, void *status_arg,
5809 struct got_repository *repo)
5811 const struct got_error *err = NULL;
5812 struct got_tree_object *subtree;
5813 char *subpath;
5815 if (asprintf(&subpath, "%s%s%s", parent_path,
5816 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5817 return got_error_from_errno("asprintf");
5819 err = got_object_open_as_tree(&subtree, repo, &te->id);
5820 if (err)
5821 return err;
5823 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5824 commitable_paths, status_cb, status_arg, repo);
5825 got_object_tree_close(subtree);
5826 free(subpath);
5827 return err;
5830 static const struct got_error *
5831 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5833 const struct got_error *err = NULL;
5834 char *ct_parent_path = NULL;
5836 *match = 0;
5838 if (strchr(ct->in_repo_path, '/') == NULL) {
5839 *match = got_path_is_root_dir(path);
5840 return NULL;
5843 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5844 if (err)
5845 return err;
5846 *match = (strcmp(path, ct_parent_path) == 0);
5847 free(ct_parent_path);
5848 return err;
5851 static mode_t
5852 get_ct_file_mode(struct got_commitable *ct)
5854 if (S_ISLNK(ct->mode))
5855 return S_IFLNK;
5857 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5860 static const struct got_error *
5861 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5862 struct got_tree_entry *te, struct got_commitable *ct)
5864 const struct got_error *err = NULL;
5866 *new_te = NULL;
5868 err = got_object_tree_entry_dup(new_te, te);
5869 if (err)
5870 goto done;
5872 (*new_te)->mode = get_ct_file_mode(ct);
5874 if (ct->staged_status == GOT_STATUS_MODIFY)
5875 memcpy(&(*new_te)->id, ct->staged_blob_id,
5876 sizeof((*new_te)->id));
5877 else
5878 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5879 done:
5880 if (err && *new_te) {
5881 free(*new_te);
5882 *new_te = NULL;
5884 return err;
5887 static const struct got_error *
5888 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5889 struct got_commitable *ct)
5891 const struct got_error *err = NULL;
5892 char *ct_name = NULL;
5894 *new_te = NULL;
5896 *new_te = calloc(1, sizeof(**new_te));
5897 if (*new_te == NULL)
5898 return got_error_from_errno("calloc");
5900 err = got_path_basename(&ct_name, ct->path);
5901 if (err)
5902 goto done;
5903 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5904 sizeof((*new_te)->name)) {
5905 err = got_error(GOT_ERR_NO_SPACE);
5906 goto done;
5909 (*new_te)->mode = get_ct_file_mode(ct);
5911 if (ct->staged_status == GOT_STATUS_ADD)
5912 memcpy(&(*new_te)->id, ct->staged_blob_id,
5913 sizeof((*new_te)->id));
5914 else
5915 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5916 done:
5917 free(ct_name);
5918 if (err && *new_te) {
5919 free(*new_te);
5920 *new_te = NULL;
5922 return err;
5925 static const struct got_error *
5926 insert_tree_entry(struct got_tree_entry *new_te,
5927 struct got_pathlist_head *paths)
5929 const struct got_error *err = NULL;
5930 struct got_pathlist_entry *new_pe;
5932 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5933 if (err)
5934 return err;
5935 if (new_pe == NULL)
5936 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5937 return NULL;
5940 static const struct got_error *
5941 report_ct_status(struct got_commitable *ct,
5942 got_worktree_status_cb status_cb, void *status_arg)
5944 const char *ct_path = ct->path;
5945 unsigned char status;
5947 if (status_cb == NULL) /* no commit progress output desired */
5948 return NULL;
5950 while (ct_path[0] == '/')
5951 ct_path++;
5953 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5954 status = ct->staged_status;
5955 else
5956 status = ct->status;
5958 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5959 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5962 static const struct got_error *
5963 match_modified_subtree(int *modified, struct got_tree_entry *te,
5964 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5966 const struct got_error *err = NULL;
5967 struct got_pathlist_entry *pe;
5968 char *te_path;
5970 *modified = 0;
5972 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5973 got_path_is_root_dir(base_tree_path) ? "" : "/",
5974 te->name) == -1)
5975 return got_error_from_errno("asprintf");
5977 TAILQ_FOREACH(pe, commitable_paths, entry) {
5978 struct got_commitable *ct = pe->data;
5979 *modified = got_path_is_child(ct->in_repo_path, te_path,
5980 strlen(te_path));
5981 if (*modified)
5982 break;
5985 free(te_path);
5986 return err;
5989 static const struct got_error *
5990 match_deleted_or_modified_ct(struct got_commitable **ctp,
5991 struct got_tree_entry *te, const char *base_tree_path,
5992 struct got_pathlist_head *commitable_paths)
5994 const struct got_error *err = NULL;
5995 struct got_pathlist_entry *pe;
5997 *ctp = NULL;
5999 TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 struct got_commitable *ct = pe->data;
6001 char *ct_name = NULL;
6002 int path_matches;
6004 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6005 if (ct->status != GOT_STATUS_MODIFY &&
6006 ct->status != GOT_STATUS_MODE_CHANGE &&
6007 ct->status != GOT_STATUS_DELETE &&
6008 ct->status != GOT_STATUS_CONFLICT)
6009 continue;
6010 } else {
6011 if (ct->staged_status != GOT_STATUS_MODIFY &&
6012 ct->staged_status != GOT_STATUS_DELETE)
6013 continue;
6016 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6017 continue;
6019 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6020 if (err)
6021 return err;
6022 if (!path_matches)
6023 continue;
6025 err = got_path_basename(&ct_name, pe->path);
6026 if (err)
6027 return err;
6029 if (strcmp(te->name, ct_name) != 0) {
6030 free(ct_name);
6031 continue;
6033 free(ct_name);
6035 *ctp = ct;
6036 break;
6039 return err;
6042 static const struct got_error *
6043 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6044 const char *child_path, const char *path_base_tree,
6045 struct got_pathlist_head *commitable_paths,
6046 got_worktree_status_cb status_cb, void *status_arg,
6047 struct got_repository *repo)
6049 const struct got_error *err = NULL;
6050 struct got_tree_entry *new_te;
6051 char *subtree_path;
6052 struct got_object_id *id = NULL;
6053 int nentries;
6055 *new_tep = NULL;
6057 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6058 got_path_is_root_dir(path_base_tree) ? "" : "/",
6059 child_path) == -1)
6060 return got_error_from_errno("asprintf");
6062 new_te = calloc(1, sizeof(*new_te));
6063 if (new_te == NULL)
6064 return got_error_from_errno("calloc");
6065 new_te->mode = S_IFDIR;
6067 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6068 sizeof(new_te->name)) {
6069 err = got_error(GOT_ERR_NO_SPACE);
6070 goto done;
6072 err = write_tree(&id, &nentries, NULL, subtree_path,
6073 commitable_paths, status_cb, status_arg, repo);
6074 if (err) {
6075 free(new_te);
6076 goto done;
6078 memcpy(&new_te->id, id, sizeof(new_te->id));
6079 done:
6080 free(id);
6081 free(subtree_path);
6082 if (err == NULL)
6083 *new_tep = new_te;
6084 return err;
6087 static const struct got_error *
6088 write_tree(struct got_object_id **new_tree_id, int *nentries,
6089 struct got_tree_object *base_tree, const char *path_base_tree,
6090 struct got_pathlist_head *commitable_paths,
6091 got_worktree_status_cb status_cb, void *status_arg,
6092 struct got_repository *repo)
6094 const struct got_error *err = NULL;
6095 struct got_pathlist_head paths;
6096 struct got_tree_entry *te, *new_te = NULL;
6097 struct got_pathlist_entry *pe;
6099 TAILQ_INIT(&paths);
6100 *nentries = 0;
6102 /* Insert, and recurse into, newly added entries first. */
6103 TAILQ_FOREACH(pe, commitable_paths, entry) {
6104 struct got_commitable *ct = pe->data;
6105 char *child_path = NULL, *slash;
6107 if ((ct->status != GOT_STATUS_ADD &&
6108 ct->staged_status != GOT_STATUS_ADD) ||
6109 (ct->flags & GOT_COMMITABLE_ADDED))
6110 continue;
6112 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6113 strlen(path_base_tree)))
6114 continue;
6116 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6117 ct->in_repo_path);
6118 if (err)
6119 goto done;
6121 slash = strchr(child_path, '/');
6122 if (slash == NULL) {
6123 err = alloc_added_blob_tree_entry(&new_te, ct);
6124 if (err)
6125 goto done;
6126 err = report_ct_status(ct, status_cb, status_arg);
6127 if (err)
6128 goto done;
6129 ct->flags |= GOT_COMMITABLE_ADDED;
6130 err = insert_tree_entry(new_te, &paths);
6131 if (err)
6132 goto done;
6133 (*nentries)++;
6134 } else {
6135 *slash = '\0'; /* trim trailing path components */
6136 if (base_tree == NULL ||
6137 got_object_tree_find_entry(base_tree, child_path)
6138 == NULL) {
6139 err = make_subtree_for_added_blob(&new_te,
6140 child_path, path_base_tree,
6141 commitable_paths, status_cb, status_arg,
6142 repo);
6143 if (err)
6144 goto done;
6145 err = insert_tree_entry(new_te, &paths);
6146 if (err)
6147 goto done;
6148 (*nentries)++;
6153 if (base_tree) {
6154 int i, nbase_entries;
6155 /* Handle modified and deleted entries. */
6156 nbase_entries = got_object_tree_get_nentries(base_tree);
6157 for (i = 0; i < nbase_entries; i++) {
6158 struct got_commitable *ct = NULL;
6160 te = got_object_tree_get_entry(base_tree, i);
6161 if (got_object_tree_entry_is_submodule(te)) {
6162 /* Entry is a submodule; just copy it. */
6163 err = got_object_tree_entry_dup(&new_te, te);
6164 if (err)
6165 goto done;
6166 err = insert_tree_entry(new_te, &paths);
6167 if (err)
6168 goto done;
6169 (*nentries)++;
6170 continue;
6173 if (S_ISDIR(te->mode)) {
6174 int modified;
6175 err = got_object_tree_entry_dup(&new_te, te);
6176 if (err)
6177 goto done;
6178 err = match_modified_subtree(&modified, te,
6179 path_base_tree, commitable_paths);
6180 if (err)
6181 goto done;
6182 /* Avoid recursion into unmodified subtrees. */
6183 if (modified) {
6184 struct got_object_id *new_id;
6185 int nsubentries;
6186 err = write_subtree(&new_id,
6187 &nsubentries, te,
6188 path_base_tree, commitable_paths,
6189 status_cb, status_arg, repo);
6190 if (err)
6191 goto done;
6192 if (nsubentries == 0) {
6193 /* All entries were deleted. */
6194 free(new_id);
6195 continue;
6197 memcpy(&new_te->id, new_id,
6198 sizeof(new_te->id));
6199 free(new_id);
6201 err = insert_tree_entry(new_te, &paths);
6202 if (err)
6203 goto done;
6204 (*nentries)++;
6205 continue;
6208 err = match_deleted_or_modified_ct(&ct, te,
6209 path_base_tree, commitable_paths);
6210 if (err)
6211 goto done;
6212 if (ct) {
6213 /* NB: Deleted entries get dropped here. */
6214 if (ct->status == GOT_STATUS_MODIFY ||
6215 ct->status == GOT_STATUS_MODE_CHANGE ||
6216 ct->status == GOT_STATUS_CONFLICT ||
6217 ct->staged_status == GOT_STATUS_MODIFY) {
6218 err = alloc_modified_blob_tree_entry(
6219 &new_te, te, ct);
6220 if (err)
6221 goto done;
6222 err = insert_tree_entry(new_te, &paths);
6223 if (err)
6224 goto done;
6225 (*nentries)++;
6227 err = report_ct_status(ct, status_cb,
6228 status_arg);
6229 if (err)
6230 goto done;
6231 } else {
6232 /* Entry is unchanged; just copy it. */
6233 err = got_object_tree_entry_dup(&new_te, te);
6234 if (err)
6235 goto done;
6236 err = insert_tree_entry(new_te, &paths);
6237 if (err)
6238 goto done;
6239 (*nentries)++;
6244 /* Write new list of entries; deleted entries have been dropped. */
6245 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6246 done:
6247 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6248 return err;
6251 static const struct got_error *
6252 update_fileindex_after_commit(struct got_worktree *worktree,
6253 struct got_pathlist_head *commitable_paths,
6254 struct got_object_id *new_base_commit_id,
6255 struct got_fileindex *fileindex, int have_staged_files)
6257 const struct got_error *err = NULL;
6258 struct got_pathlist_entry *pe;
6259 char *relpath = NULL;
6261 TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 struct got_fileindex_entry *ie;
6263 struct got_commitable *ct = pe->data;
6265 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6267 err = got_path_skip_common_ancestor(&relpath,
6268 worktree->root_path, ct->ondisk_path);
6269 if (err)
6270 goto done;
6272 if (ie) {
6273 if (ct->status == GOT_STATUS_DELETE ||
6274 ct->staged_status == GOT_STATUS_DELETE) {
6275 got_fileindex_entry_remove(fileindex, ie);
6276 } else if (ct->staged_status == GOT_STATUS_ADD ||
6277 ct->staged_status == GOT_STATUS_MODIFY) {
6278 got_fileindex_entry_stage_set(ie,
6279 GOT_FILEIDX_STAGE_NONE);
6280 got_fileindex_entry_staged_filetype_set(ie, 0);
6282 err = got_fileindex_entry_update(ie,
6283 worktree->root_fd, relpath,
6284 ct->staged_blob_id->sha1,
6285 new_base_commit_id->sha1,
6286 !have_staged_files);
6287 } else
6288 err = got_fileindex_entry_update(ie,
6289 worktree->root_fd, relpath,
6290 ct->blob_id->sha1,
6291 new_base_commit_id->sha1,
6292 !have_staged_files);
6293 } else {
6294 err = got_fileindex_entry_alloc(&ie, pe->path);
6295 if (err)
6296 goto done;
6297 err = got_fileindex_entry_update(ie,
6298 worktree->root_fd, relpath, ct->blob_id->sha1,
6299 new_base_commit_id->sha1, 1);
6300 if (err) {
6301 got_fileindex_entry_free(ie);
6302 goto done;
6304 err = got_fileindex_entry_add(fileindex, ie);
6305 if (err) {
6306 got_fileindex_entry_free(ie);
6307 goto done;
6310 free(relpath);
6311 relpath = NULL;
6313 done:
6314 free(relpath);
6315 return err;
6319 static const struct got_error *
6320 check_out_of_date(const char *in_repo_path, unsigned char status,
6321 unsigned char staged_status, struct got_object_id *base_blob_id,
6322 struct got_object_id *base_commit_id,
6323 struct got_object_id *head_commit_id, struct got_repository *repo,
6324 int ood_errcode)
6326 const struct got_error *err = NULL;
6327 struct got_commit_object *commit = NULL;
6328 struct got_object_id *id = NULL;
6330 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6331 /* Trivial case: base commit == head commit */
6332 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6333 return NULL;
6335 * Ensure file content which local changes were based
6336 * on matches file content in the branch head.
6338 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6339 if (err)
6340 goto done;
6341 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6342 if (err) {
6343 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6344 err = got_error(ood_errcode);
6345 goto done;
6346 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6347 err = got_error(ood_errcode);
6348 } else {
6349 /* Require that added files don't exist in the branch head. */
6350 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6351 if (err)
6352 goto done;
6353 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6354 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6355 goto done;
6356 err = id ? got_error(ood_errcode) : NULL;
6358 done:
6359 free(id);
6360 if (commit)
6361 got_object_commit_close(commit);
6362 return err;
6365 static const struct got_error *
6366 commit_worktree(struct got_object_id **new_commit_id,
6367 struct got_pathlist_head *commitable_paths,
6368 struct got_object_id *head_commit_id,
6369 struct got_object_id *parent_id2,
6370 struct got_worktree *worktree,
6371 const char *author, const char *committer, char *diff_path,
6372 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6373 got_worktree_status_cb status_cb, void *status_arg,
6374 struct got_repository *repo)
6376 const struct got_error *err = NULL, *unlockerr = NULL;
6377 struct got_pathlist_entry *pe;
6378 const char *head_ref_name = NULL;
6379 struct got_commit_object *head_commit = NULL;
6380 struct got_reference *head_ref2 = NULL;
6381 struct got_object_id *head_commit_id2 = NULL;
6382 struct got_tree_object *head_tree = NULL;
6383 struct got_object_id *new_tree_id = NULL;
6384 int nentries, nparents = 0;
6385 struct got_object_id_queue parent_ids;
6386 struct got_object_qid *pid = NULL;
6387 char *logmsg = NULL;
6388 time_t timestamp;
6390 *new_commit_id = NULL;
6392 STAILQ_INIT(&parent_ids);
6394 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6395 if (err)
6396 goto done;
6398 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6399 if (err)
6400 goto done;
6402 if (commit_msg_cb != NULL) {
6403 err = commit_msg_cb(commitable_paths, diff_path,
6404 &logmsg, commit_arg);
6405 if (err)
6406 goto done;
6409 if (logmsg == NULL || strlen(logmsg) == 0) {
6410 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6411 goto done;
6414 /* Create blobs from added and modified files and record their IDs. */
6415 TAILQ_FOREACH(pe, commitable_paths, entry) {
6416 struct got_commitable *ct = pe->data;
6417 char *ondisk_path;
6419 /* Blobs for staged files already exist. */
6420 if (ct->staged_status == GOT_STATUS_ADD ||
6421 ct->staged_status == GOT_STATUS_MODIFY)
6422 continue;
6424 if (ct->status != GOT_STATUS_ADD &&
6425 ct->status != GOT_STATUS_MODIFY &&
6426 ct->status != GOT_STATUS_MODE_CHANGE &&
6427 ct->status != GOT_STATUS_CONFLICT)
6428 continue;
6430 if (asprintf(&ondisk_path, "%s/%s",
6431 worktree->root_path, pe->path) == -1) {
6432 err = got_error_from_errno("asprintf");
6433 goto done;
6435 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6436 free(ondisk_path);
6437 if (err)
6438 goto done;
6441 /* Recursively write new tree objects. */
6442 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6443 commitable_paths, status_cb, status_arg, repo);
6444 if (err)
6445 goto done;
6447 err = got_object_qid_alloc(&pid, head_commit_id);
6448 if (err)
6449 goto done;
6450 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6451 nparents++;
6452 if (parent_id2) {
6453 err = got_object_qid_alloc(&pid, parent_id2);
6454 if (err)
6455 goto done;
6456 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6457 nparents++;
6459 timestamp = time(NULL);
6460 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6461 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6462 if (logmsg != NULL)
6463 free(logmsg);
6464 if (err)
6465 goto done;
6467 /* Check if a concurrent commit to our branch has occurred. */
6468 head_ref_name = got_worktree_get_head_ref_name(worktree);
6469 if (head_ref_name == NULL) {
6470 err = got_error_from_errno("got_worktree_get_head_ref_name");
6471 goto done;
6473 /* Lock the reference here to prevent concurrent modification. */
6474 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6475 if (err)
6476 goto done;
6477 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6478 if (err)
6479 goto done;
6480 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6481 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6482 goto done;
6484 /* Update branch head in repository. */
6485 err = got_ref_change_ref(head_ref2, *new_commit_id);
6486 if (err)
6487 goto done;
6488 err = got_ref_write(head_ref2, repo);
6489 if (err)
6490 goto done;
6492 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6493 if (err)
6494 goto done;
6496 err = ref_base_commit(worktree, repo);
6497 if (err)
6498 goto done;
6499 done:
6500 got_object_id_queue_free(&parent_ids);
6501 if (head_tree)
6502 got_object_tree_close(head_tree);
6503 if (head_commit)
6504 got_object_commit_close(head_commit);
6505 free(head_commit_id2);
6506 if (head_ref2) {
6507 unlockerr = got_ref_unlock(head_ref2);
6508 if (unlockerr && err == NULL)
6509 err = unlockerr;
6510 got_ref_close(head_ref2);
6512 return err;
6515 static const struct got_error *
6516 check_path_is_commitable(const char *path,
6517 struct got_pathlist_head *commitable_paths)
6519 struct got_pathlist_entry *cpe = NULL;
6520 size_t path_len = strlen(path);
6522 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6523 struct got_commitable *ct = cpe->data;
6524 const char *ct_path = ct->path;
6526 while (ct_path[0] == '/')
6527 ct_path++;
6529 if (strcmp(path, ct_path) == 0 ||
6530 got_path_is_child(ct_path, path, path_len))
6531 break;
6534 if (cpe == NULL)
6535 return got_error_path(path, GOT_ERR_BAD_PATH);
6537 return NULL;
6540 static const struct got_error *
6541 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6543 int *have_staged_files = arg;
6545 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6546 *have_staged_files = 1;
6547 return got_error(GOT_ERR_CANCELLED);
6550 return NULL;
6553 static const struct got_error *
6554 check_non_staged_files(struct got_fileindex *fileindex,
6555 struct got_pathlist_head *paths)
6557 struct got_pathlist_entry *pe;
6558 struct got_fileindex_entry *ie;
6560 TAILQ_FOREACH(pe, paths, entry) {
6561 if (pe->path[0] == '\0')
6562 continue;
6563 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6564 if (ie == NULL)
6565 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6566 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6567 return got_error_path(pe->path,
6568 GOT_ERR_FILE_NOT_STAGED);
6571 return NULL;
6574 const struct got_error *
6575 got_worktree_commit(struct got_object_id **new_commit_id,
6576 struct got_worktree *worktree, struct got_pathlist_head *paths,
6577 const char *author, const char *committer, int allow_bad_symlinks,
6578 int show_diff, int commit_conflicts,
6579 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6580 got_worktree_status_cb status_cb, void *status_arg,
6581 struct got_repository *repo)
6583 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6584 struct got_fileindex *fileindex = NULL;
6585 char *fileindex_path = NULL;
6586 struct got_pathlist_head commitable_paths;
6587 struct collect_commitables_arg cc_arg;
6588 struct got_pathlist_entry *pe;
6589 struct got_reference *head_ref = NULL;
6590 struct got_object_id *head_commit_id = NULL;
6591 char *diff_path = NULL;
6592 int have_staged_files = 0;
6594 *new_commit_id = NULL;
6596 memset(&cc_arg, 0, sizeof(cc_arg));
6597 TAILQ_INIT(&commitable_paths);
6599 err = lock_worktree(worktree, LOCK_EX);
6600 if (err)
6601 goto done;
6603 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6604 if (err)
6605 goto done;
6607 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6608 if (err)
6609 goto done;
6611 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6612 if (err)
6613 goto done;
6615 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6616 &have_staged_files);
6617 if (err && err->code != GOT_ERR_CANCELLED)
6618 goto done;
6619 if (have_staged_files) {
6620 err = check_non_staged_files(fileindex, paths);
6621 if (err)
6622 goto done;
6625 cc_arg.commitable_paths = &commitable_paths;
6626 cc_arg.worktree = worktree;
6627 cc_arg.fileindex = fileindex;
6628 cc_arg.repo = repo;
6629 cc_arg.have_staged_files = have_staged_files;
6630 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6631 cc_arg.diff_header_shown = 0;
6632 cc_arg.commit_conflicts = commit_conflicts;
6633 if (show_diff) {
6634 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6635 GOT_TMPDIR_STR "/got", ".diff");
6636 if (err)
6637 goto done;
6638 cc_arg.f1 = got_opentemp();
6639 if (cc_arg.f1 == NULL) {
6640 err = got_error_from_errno("got_opentemp");
6641 goto done;
6643 cc_arg.f2 = got_opentemp();
6644 if (cc_arg.f2 == NULL) {
6645 err = got_error_from_errno("got_opentemp");
6646 goto done;
6650 TAILQ_FOREACH(pe, paths, entry) {
6651 err = worktree_status(worktree, pe->path, fileindex, repo,
6652 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6653 if (err)
6654 goto done;
6657 if (show_diff) {
6658 if (fflush(cc_arg.diff_outfile) == EOF) {
6659 err = got_error_from_errno("fflush");
6660 goto done;
6664 if (TAILQ_EMPTY(&commitable_paths)) {
6665 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6666 goto done;
6669 TAILQ_FOREACH(pe, paths, entry) {
6670 err = check_path_is_commitable(pe->path, &commitable_paths);
6671 if (err)
6672 goto done;
6675 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6676 struct got_commitable *ct = pe->data;
6677 const char *ct_path = ct->in_repo_path;
6679 while (ct_path[0] == '/')
6680 ct_path++;
6681 err = check_out_of_date(ct_path, ct->status,
6682 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6683 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6684 if (err)
6685 goto done;
6689 err = commit_worktree(new_commit_id, &commitable_paths,
6690 head_commit_id, NULL, worktree, author, committer,
6691 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6692 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6693 if (err)
6694 goto done;
6696 err = update_fileindex_after_commit(worktree, &commitable_paths,
6697 *new_commit_id, fileindex, have_staged_files);
6698 sync_err = sync_fileindex(fileindex, fileindex_path);
6699 if (sync_err && err == NULL)
6700 err = sync_err;
6701 done:
6702 if (fileindex)
6703 got_fileindex_free(fileindex);
6704 free(fileindex_path);
6705 unlockerr = lock_worktree(worktree, LOCK_SH);
6706 if (unlockerr && err == NULL)
6707 err = unlockerr;
6708 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6709 struct got_commitable *ct = pe->data;
6711 free_commitable(ct);
6713 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6714 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6715 err = got_error_from_errno2("unlink", diff_path);
6716 free(diff_path);
6717 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6718 err == NULL)
6719 err = got_error_from_errno("fclose");
6720 return err;
6723 const char *
6724 got_commitable_get_path(struct got_commitable *ct)
6726 return ct->path;
6729 unsigned int
6730 got_commitable_get_status(struct got_commitable *ct)
6732 return ct->status;
6735 struct check_rebase_ok_arg {
6736 struct got_worktree *worktree;
6737 struct got_repository *repo;
6740 static const struct got_error *
6741 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6743 const struct got_error *err = NULL;
6744 struct check_rebase_ok_arg *a = arg;
6745 unsigned char status;
6746 struct stat sb;
6747 char *ondisk_path;
6749 /* Reject rebase of a work tree with mixed base commits. */
6750 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6751 SHA1_DIGEST_LENGTH))
6752 return got_error(GOT_ERR_MIXED_COMMITS);
6754 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6755 == -1)
6756 return got_error_from_errno("asprintf");
6758 /* Reject rebase of a work tree with modified or staged files. */
6759 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6760 free(ondisk_path);
6761 if (err)
6762 return err;
6764 if (status != GOT_STATUS_NO_CHANGE)
6765 return got_error(GOT_ERR_MODIFIED);
6766 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6767 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6769 return NULL;
6772 const struct got_error *
6773 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6774 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6775 struct got_worktree *worktree, struct got_reference *branch,
6776 struct got_repository *repo)
6778 const struct got_error *err = NULL;
6779 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6780 char *branch_ref_name = NULL;
6781 char *fileindex_path = NULL;
6782 struct check_rebase_ok_arg ok_arg;
6783 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6784 struct got_object_id *wt_branch_tip = NULL;
6786 *new_base_branch_ref = NULL;
6787 *tmp_branch = NULL;
6788 *fileindex = NULL;
6790 err = lock_worktree(worktree, LOCK_EX);
6791 if (err)
6792 return err;
6794 err = open_fileindex(fileindex, &fileindex_path, worktree);
6795 if (err)
6796 goto done;
6798 ok_arg.worktree = worktree;
6799 ok_arg.repo = repo;
6800 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6801 &ok_arg);
6802 if (err)
6803 goto done;
6805 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6806 if (err)
6807 goto done;
6809 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6810 if (err)
6811 goto done;
6813 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6814 if (err)
6815 goto done;
6817 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6818 0);
6819 if (err)
6820 goto done;
6822 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6823 if (err)
6824 goto done;
6825 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6826 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6827 goto done;
6830 err = got_ref_alloc_symref(new_base_branch_ref,
6831 new_base_branch_ref_name, wt_branch);
6832 if (err)
6833 goto done;
6834 err = got_ref_write(*new_base_branch_ref, repo);
6835 if (err)
6836 goto done;
6838 /* TODO Lock original branch's ref while rebasing? */
6840 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6841 if (err)
6842 goto done;
6844 err = got_ref_write(branch_ref, repo);
6845 if (err)
6846 goto done;
6848 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6849 worktree->base_commit_id);
6850 if (err)
6851 goto done;
6852 err = got_ref_write(*tmp_branch, repo);
6853 if (err)
6854 goto done;
6856 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6857 if (err)
6858 goto done;
6859 done:
6860 free(fileindex_path);
6861 free(tmp_branch_name);
6862 free(new_base_branch_ref_name);
6863 free(branch_ref_name);
6864 if (branch_ref)
6865 got_ref_close(branch_ref);
6866 if (wt_branch)
6867 got_ref_close(wt_branch);
6868 free(wt_branch_tip);
6869 if (err) {
6870 if (*new_base_branch_ref) {
6871 got_ref_close(*new_base_branch_ref);
6872 *new_base_branch_ref = NULL;
6874 if (*tmp_branch) {
6875 got_ref_close(*tmp_branch);
6876 *tmp_branch = NULL;
6878 if (*fileindex) {
6879 got_fileindex_free(*fileindex);
6880 *fileindex = NULL;
6882 lock_worktree(worktree, LOCK_SH);
6884 return err;
6887 const struct got_error *
6888 got_worktree_rebase_continue(struct got_object_id **commit_id,
6889 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6890 struct got_reference **branch, struct got_fileindex **fileindex,
6891 struct got_worktree *worktree, struct got_repository *repo)
6893 const struct got_error *err;
6894 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6895 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6896 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6897 char *fileindex_path = NULL;
6898 int have_staged_files = 0;
6900 *commit_id = NULL;
6901 *new_base_branch = NULL;
6902 *tmp_branch = NULL;
6903 *branch = NULL;
6904 *fileindex = NULL;
6906 err = lock_worktree(worktree, LOCK_EX);
6907 if (err)
6908 return err;
6910 err = open_fileindex(fileindex, &fileindex_path, worktree);
6911 if (err)
6912 goto done;
6914 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6915 &have_staged_files);
6916 if (err && err->code != GOT_ERR_CANCELLED)
6917 goto done;
6918 if (have_staged_files) {
6919 err = got_error(GOT_ERR_STAGED_PATHS);
6920 goto done;
6923 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6924 if (err)
6925 goto done;
6927 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6928 if (err)
6929 goto done;
6931 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6932 if (err)
6933 goto done;
6935 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6936 if (err)
6937 goto done;
6939 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6940 if (err)
6941 goto done;
6943 err = got_ref_open(branch, repo,
6944 got_ref_get_symref_target(branch_ref), 0);
6945 if (err)
6946 goto done;
6948 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6949 if (err)
6950 goto done;
6952 err = got_ref_resolve(commit_id, repo, commit_ref);
6953 if (err)
6954 goto done;
6956 err = got_ref_open(new_base_branch, repo,
6957 new_base_branch_ref_name, 0);
6958 if (err)
6959 goto done;
6961 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6962 if (err)
6963 goto done;
6964 done:
6965 free(commit_ref_name);
6966 free(branch_ref_name);
6967 free(fileindex_path);
6968 if (commit_ref)
6969 got_ref_close(commit_ref);
6970 if (branch_ref)
6971 got_ref_close(branch_ref);
6972 if (err) {
6973 free(*commit_id);
6974 *commit_id = NULL;
6975 if (*tmp_branch) {
6976 got_ref_close(*tmp_branch);
6977 *tmp_branch = NULL;
6979 if (*new_base_branch) {
6980 got_ref_close(*new_base_branch);
6981 *new_base_branch = NULL;
6983 if (*branch) {
6984 got_ref_close(*branch);
6985 *branch = NULL;
6987 if (*fileindex) {
6988 got_fileindex_free(*fileindex);
6989 *fileindex = NULL;
6991 lock_worktree(worktree, LOCK_SH);
6993 return err;
6996 const struct got_error *
6997 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6999 const struct got_error *err;
7000 char *tmp_branch_name = NULL;
7002 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7003 if (err)
7004 return err;
7006 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7007 free(tmp_branch_name);
7008 return NULL;
7011 static const struct got_error *
7012 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7013 const char *diff_path, char **logmsg, void *arg)
7015 *logmsg = arg;
7016 return NULL;
7019 static const struct got_error *
7020 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7021 const char *path, struct got_object_id *blob_id,
7022 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7023 int dirfd, const char *de_name)
7025 return NULL;
7028 struct collect_merged_paths_arg {
7029 got_worktree_checkout_cb progress_cb;
7030 void *progress_arg;
7031 struct got_pathlist_head *merged_paths;
7034 static const struct got_error *
7035 collect_merged_paths(void *arg, unsigned char status, const char *path)
7037 const struct got_error *err;
7038 struct collect_merged_paths_arg *a = arg;
7039 char *p;
7040 struct got_pathlist_entry *new;
7042 err = (*a->progress_cb)(a->progress_arg, status, path);
7043 if (err)
7044 return err;
7046 if (status != GOT_STATUS_MERGE &&
7047 status != GOT_STATUS_ADD &&
7048 status != GOT_STATUS_DELETE &&
7049 status != GOT_STATUS_CONFLICT)
7050 return NULL;
7052 p = strdup(path);
7053 if (p == NULL)
7054 return got_error_from_errno("strdup");
7056 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7057 if (err || new == NULL)
7058 free(p);
7059 return err;
7062 static const struct got_error *
7063 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7064 int is_rebase, struct got_repository *repo)
7066 const struct got_error *err;
7067 struct got_reference *commit_ref = NULL;
7069 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7070 if (err) {
7071 if (err->code != GOT_ERR_NOT_REF)
7072 goto done;
7073 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7074 if (err)
7075 goto done;
7076 err = got_ref_write(commit_ref, repo);
7077 if (err)
7078 goto done;
7079 } else if (is_rebase) {
7080 struct got_object_id *stored_id;
7081 int cmp;
7083 err = got_ref_resolve(&stored_id, repo, commit_ref);
7084 if (err)
7085 goto done;
7086 cmp = got_object_id_cmp(commit_id, stored_id);
7087 free(stored_id);
7088 if (cmp != 0) {
7089 err = got_error(GOT_ERR_REBASE_COMMITID);
7090 goto done;
7093 done:
7094 if (commit_ref)
7095 got_ref_close(commit_ref);
7096 return err;
7099 static const struct got_error *
7100 rebase_merge_files(struct got_pathlist_head *merged_paths,
7101 const char *commit_ref_name, struct got_worktree *worktree,
7102 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7103 struct got_object_id *commit_id, struct got_repository *repo,
7104 got_worktree_checkout_cb progress_cb, void *progress_arg,
7105 got_cancel_cb cancel_cb, void *cancel_arg)
7107 const struct got_error *err;
7108 struct got_reference *commit_ref = NULL;
7109 struct collect_merged_paths_arg cmp_arg;
7110 char *fileindex_path;
7112 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7114 err = get_fileindex_path(&fileindex_path, worktree);
7115 if (err)
7116 return err;
7118 cmp_arg.progress_cb = progress_cb;
7119 cmp_arg.progress_arg = progress_arg;
7120 cmp_arg.merged_paths = merged_paths;
7121 err = merge_files(worktree, fileindex, fileindex_path,
7122 parent_commit_id, commit_id, repo, collect_merged_paths,
7123 &cmp_arg, cancel_cb, cancel_arg);
7124 if (commit_ref)
7125 got_ref_close(commit_ref);
7126 return err;
7129 const struct got_error *
7130 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7131 struct got_worktree *worktree, struct got_fileindex *fileindex,
7132 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7133 struct got_repository *repo,
7134 got_worktree_checkout_cb progress_cb, void *progress_arg,
7135 got_cancel_cb cancel_cb, void *cancel_arg)
7137 const struct got_error *err;
7138 char *commit_ref_name;
7140 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7141 if (err)
7142 return err;
7144 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7145 if (err)
7146 goto done;
7148 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7149 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7150 progress_arg, cancel_cb, cancel_arg);
7151 done:
7152 free(commit_ref_name);
7153 return err;
7156 const struct got_error *
7157 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7158 struct got_worktree *worktree, struct got_fileindex *fileindex,
7159 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7160 struct got_repository *repo,
7161 got_worktree_checkout_cb progress_cb, void *progress_arg,
7162 got_cancel_cb cancel_cb, void *cancel_arg)
7164 const struct got_error *err;
7165 char *commit_ref_name;
7167 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7168 if (err)
7169 return err;
7171 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7172 if (err)
7173 goto done;
7175 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7176 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7177 progress_arg, cancel_cb, cancel_arg);
7178 done:
7179 free(commit_ref_name);
7180 return err;
7183 static const struct got_error *
7184 rebase_commit(struct got_object_id **new_commit_id,
7185 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7186 struct got_worktree *worktree, struct got_fileindex *fileindex,
7187 struct got_reference *tmp_branch, const char *committer,
7188 struct got_commit_object *orig_commit, const char *new_logmsg,
7189 int allow_conflict, struct got_repository *repo)
7191 const struct got_error *err, *sync_err;
7192 struct got_pathlist_head commitable_paths;
7193 struct collect_commitables_arg cc_arg;
7194 char *fileindex_path = NULL;
7195 struct got_reference *head_ref = NULL;
7196 struct got_object_id *head_commit_id = NULL;
7197 char *logmsg = NULL;
7199 memset(&cc_arg, 0, sizeof(cc_arg));
7200 TAILQ_INIT(&commitable_paths);
7201 *new_commit_id = NULL;
7203 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7205 err = get_fileindex_path(&fileindex_path, worktree);
7206 if (err)
7207 return err;
7209 cc_arg.commitable_paths = &commitable_paths;
7210 cc_arg.worktree = worktree;
7211 cc_arg.repo = repo;
7212 cc_arg.have_staged_files = 0;
7213 cc_arg.commit_conflicts = allow_conflict;
7215 * If possible get the status of individual files directly to
7216 * avoid crawling the entire work tree once per rebased commit.
7218 * Ideally, merged_paths would contain a list of commitables
7219 * we could use so we could skip worktree_status() entirely.
7220 * However, we would then need carefully keep track of cumulative
7221 * effects of operations such as file additions and deletions
7222 * in 'got histedit -f' (folding multiple commits into one),
7223 * and this extra complexity is not really worth it.
7225 if (merged_paths) {
7226 struct got_pathlist_entry *pe;
7227 TAILQ_FOREACH(pe, merged_paths, entry) {
7228 err = worktree_status(worktree, pe->path, fileindex,
7229 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7230 0);
7231 if (err)
7232 goto done;
7234 } else {
7235 err = worktree_status(worktree, "", fileindex, repo,
7236 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7237 if (err)
7238 goto done;
7241 if (TAILQ_EMPTY(&commitable_paths)) {
7242 /* No-op change; commit will be elided. */
7243 err = got_ref_delete(commit_ref, repo);
7244 if (err)
7245 goto done;
7246 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7247 goto done;
7250 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7251 if (err)
7252 goto done;
7254 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7255 if (err)
7256 goto done;
7258 if (new_logmsg) {
7259 logmsg = strdup(new_logmsg);
7260 if (logmsg == NULL) {
7261 err = got_error_from_errno("strdup");
7262 goto done;
7264 } else {
7265 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7266 if (err)
7267 goto done;
7270 /* NB: commit_worktree will call free(logmsg) */
7271 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7272 NULL, worktree, got_object_commit_get_author(orig_commit),
7273 committer ? committer :
7274 got_object_commit_get_committer(orig_commit), NULL,
7275 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7276 if (err)
7277 goto done;
7279 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7280 if (err)
7281 goto done;
7283 err = got_ref_delete(commit_ref, repo);
7284 if (err)
7285 goto done;
7287 err = update_fileindex_after_commit(worktree, &commitable_paths,
7288 *new_commit_id, fileindex, 0);
7289 sync_err = sync_fileindex(fileindex, fileindex_path);
7290 if (sync_err && err == NULL)
7291 err = sync_err;
7292 done:
7293 free(fileindex_path);
7294 free(head_commit_id);
7295 if (head_ref)
7296 got_ref_close(head_ref);
7297 if (err) {
7298 free(*new_commit_id);
7299 *new_commit_id = NULL;
7301 return err;
7304 const struct got_error *
7305 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7306 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7307 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7308 const char *committer, struct got_commit_object *orig_commit,
7309 struct got_object_id *orig_commit_id, int allow_conflict,
7310 struct got_repository *repo)
7312 const struct got_error *err;
7313 char *commit_ref_name;
7314 struct got_reference *commit_ref = NULL;
7315 struct got_object_id *commit_id = NULL;
7317 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7318 if (err)
7319 return err;
7321 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7322 if (err)
7323 goto done;
7324 err = got_ref_resolve(&commit_id, repo, commit_ref);
7325 if (err)
7326 goto done;
7327 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7328 err = got_error(GOT_ERR_REBASE_COMMITID);
7329 goto done;
7332 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7333 worktree, fileindex, tmp_branch, committer, orig_commit,
7334 NULL, allow_conflict, repo);
7335 done:
7336 if (commit_ref)
7337 got_ref_close(commit_ref);
7338 free(commit_ref_name);
7339 free(commit_id);
7340 return err;
7343 const struct got_error *
7344 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7345 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7346 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7347 const char *committer, struct got_commit_object *orig_commit,
7348 struct got_object_id *orig_commit_id, const char *new_logmsg,
7349 int allow_conflict, struct got_repository *repo)
7351 const struct got_error *err;
7352 char *commit_ref_name;
7353 struct got_reference *commit_ref = NULL;
7355 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7356 if (err)
7357 return err;
7359 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7360 if (err)
7361 goto done;
7363 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7364 worktree, fileindex, tmp_branch, committer, orig_commit,
7365 new_logmsg, allow_conflict, repo);
7366 done:
7367 if (commit_ref)
7368 got_ref_close(commit_ref);
7369 free(commit_ref_name);
7370 return err;
7373 const struct got_error *
7374 got_worktree_rebase_postpone(struct got_worktree *worktree,
7375 struct got_fileindex *fileindex)
7377 if (fileindex)
7378 got_fileindex_free(fileindex);
7379 return lock_worktree(worktree, LOCK_SH);
7382 static const struct got_error *
7383 delete_ref(const char *name, struct got_repository *repo)
7385 const struct got_error *err;
7386 struct got_reference *ref;
7388 err = got_ref_open(&ref, repo, name, 0);
7389 if (err) {
7390 if (err->code == GOT_ERR_NOT_REF)
7391 return NULL;
7392 return err;
7395 err = got_ref_delete(ref, repo);
7396 got_ref_close(ref);
7397 return err;
7400 static const struct got_error *
7401 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7403 const struct got_error *err;
7404 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7405 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7407 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7408 if (err)
7409 goto done;
7410 err = delete_ref(tmp_branch_name, repo);
7411 if (err)
7412 goto done;
7414 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7415 if (err)
7416 goto done;
7417 err = delete_ref(new_base_branch_ref_name, repo);
7418 if (err)
7419 goto done;
7421 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7422 if (err)
7423 goto done;
7424 err = delete_ref(branch_ref_name, repo);
7425 if (err)
7426 goto done;
7428 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7429 if (err)
7430 goto done;
7431 err = delete_ref(commit_ref_name, repo);
7432 if (err)
7433 goto done;
7435 done:
7436 free(tmp_branch_name);
7437 free(new_base_branch_ref_name);
7438 free(branch_ref_name);
7439 free(commit_ref_name);
7440 return err;
7443 static const struct got_error *
7444 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7445 struct got_object_id *new_commit_id, struct got_repository *repo)
7447 const struct got_error *err;
7448 struct got_reference *ref = NULL;
7449 struct got_object_id *old_commit_id = NULL;
7450 const char *branch_name = NULL;
7451 char *new_id_str = NULL;
7452 char *refname = NULL;
7454 branch_name = got_ref_get_name(branch);
7455 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7456 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7457 branch_name += 11;
7459 err = got_object_id_str(&new_id_str, new_commit_id);
7460 if (err)
7461 return err;
7463 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7464 new_id_str) == -1) {
7465 err = got_error_from_errno("asprintf");
7466 goto done;
7469 err = got_ref_resolve(&old_commit_id, repo, branch);
7470 if (err)
7471 goto done;
7473 err = got_ref_alloc(&ref, refname, old_commit_id);
7474 if (err)
7475 goto done;
7477 err = got_ref_write(ref, repo);
7478 done:
7479 free(new_id_str);
7480 free(refname);
7481 free(old_commit_id);
7482 if (ref)
7483 got_ref_close(ref);
7484 return err;
7487 const struct got_error *
7488 got_worktree_rebase_complete(struct got_worktree *worktree,
7489 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7490 struct got_reference *rebased_branch, struct got_repository *repo,
7491 int create_backup)
7493 const struct got_error *err, *unlockerr, *sync_err;
7494 struct got_object_id *new_head_commit_id = NULL;
7495 char *fileindex_path = NULL;
7497 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7498 if (err)
7499 return err;
7501 if (create_backup) {
7502 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7503 rebased_branch, new_head_commit_id, repo);
7504 if (err)
7505 goto done;
7508 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7509 if (err)
7510 goto done;
7512 err = got_ref_write(rebased_branch, repo);
7513 if (err)
7514 goto done;
7516 err = got_worktree_set_head_ref(worktree, rebased_branch);
7517 if (err)
7518 goto done;
7520 err = delete_rebase_refs(worktree, repo);
7521 if (err)
7522 goto done;
7524 err = get_fileindex_path(&fileindex_path, worktree);
7525 if (err)
7526 goto done;
7527 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7528 sync_err = sync_fileindex(fileindex, fileindex_path);
7529 if (sync_err && err == NULL)
7530 err = sync_err;
7531 done:
7532 got_fileindex_free(fileindex);
7533 free(fileindex_path);
7534 free(new_head_commit_id);
7535 unlockerr = lock_worktree(worktree, LOCK_SH);
7536 if (unlockerr && err == NULL)
7537 err = unlockerr;
7538 return err;
7541 static const struct got_error *
7542 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7543 struct got_object_id *id1, struct got_object_id *id2,
7544 struct got_repository *repo)
7546 const struct got_error *err;
7547 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7548 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7550 if (id1) {
7551 err = got_object_open_as_commit(&commit1, repo, id1);
7552 if (err)
7553 goto done;
7555 err = got_object_open_as_tree(&tree1, repo,
7556 got_object_commit_get_tree_id(commit1));
7557 if (err)
7558 goto done;
7561 if (id2) {
7562 err = got_object_open_as_commit(&commit2, repo, id2);
7563 if (err)
7564 goto done;
7566 err = got_object_open_as_tree(&tree2, repo,
7567 got_object_commit_get_tree_id(commit2));
7568 if (err)
7569 goto done;
7572 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7573 got_diff_tree_collect_changed_paths, paths, 0);
7574 if (err)
7575 goto done;
7576 done:
7577 if (commit1)
7578 got_object_commit_close(commit1);
7579 if (commit2)
7580 got_object_commit_close(commit2);
7581 if (tree1)
7582 got_object_tree_close(tree1);
7583 if (tree2)
7584 got_object_tree_close(tree2);
7585 return err;
7588 static const struct got_error *
7589 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7590 struct got_object_id *id1, struct got_object_id *id2,
7591 const char *path_prefix, struct got_repository *repo)
7593 const struct got_error *err;
7594 struct got_pathlist_head merged_paths;
7595 struct got_pathlist_entry *pe;
7596 char *abspath = NULL, *wt_path = NULL;
7598 TAILQ_INIT(&merged_paths);
7600 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7601 if (err)
7602 goto done;
7604 TAILQ_FOREACH(pe, &merged_paths, entry) {
7605 struct got_diff_changed_path *change = pe->data;
7607 if (change->status != GOT_STATUS_ADD)
7608 continue;
7610 if (got_path_is_root_dir(path_prefix)) {
7611 wt_path = strdup(pe->path);
7612 if (wt_path == NULL) {
7613 err = got_error_from_errno("strdup");
7614 goto done;
7616 } else {
7617 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7618 err = got_error_from_errno("asprintf");
7619 goto done;
7622 err = got_path_skip_common_ancestor(&wt_path,
7623 path_prefix, abspath);
7624 if (err)
7625 goto done;
7626 free(abspath);
7627 abspath = NULL;
7630 err = got_pathlist_append(added_paths, wt_path, NULL);
7631 if (err)
7632 goto done;
7633 wt_path = NULL;
7636 done:
7637 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7638 free(abspath);
7639 free(wt_path);
7640 return err;
7643 static const struct got_error *
7644 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7645 struct got_object_id *id, const char *path_prefix,
7646 struct got_repository *repo)
7648 const struct got_error *err;
7649 struct got_commit_object *commit = NULL;
7650 struct got_object_qid *pid;
7652 err = got_object_open_as_commit(&commit, repo, id);
7653 if (err)
7654 goto done;
7656 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7658 err = get_paths_added_between_commits(added_paths,
7659 pid ? &pid->id : NULL, id, path_prefix, repo);
7660 if (err)
7661 goto done;
7662 done:
7663 if (commit)
7664 got_object_commit_close(commit);
7665 return err;
7668 const struct got_error *
7669 got_worktree_rebase_abort(struct got_worktree *worktree,
7670 struct got_fileindex *fileindex, struct got_repository *repo,
7671 struct got_reference *new_base_branch,
7672 got_worktree_checkout_cb progress_cb, void *progress_arg)
7674 const struct got_error *err, *unlockerr, *sync_err;
7675 struct got_reference *resolved = NULL;
7676 struct got_object_id *commit_id = NULL;
7677 struct got_object_id *merged_commit_id = NULL;
7678 struct got_commit_object *commit = NULL;
7679 char *fileindex_path = NULL;
7680 char *commit_ref_name = NULL;
7681 struct got_reference *commit_ref = NULL;
7682 struct revert_file_args rfa;
7683 struct got_object_id *tree_id = NULL;
7684 struct got_pathlist_head added_paths;
7686 TAILQ_INIT(&added_paths);
7688 err = lock_worktree(worktree, LOCK_EX);
7689 if (err)
7690 return err;
7692 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7693 if (err)
7694 goto done;
7696 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7697 if (err)
7698 goto done;
7700 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7701 if (err)
7702 goto done;
7705 * Determine which files in added status can be safely removed
7706 * from disk while reverting changes in the work tree.
7707 * We want to avoid deleting unrelated files which were added by
7708 * the user for conflict resolution purposes.
7710 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7711 got_worktree_get_path_prefix(worktree), repo);
7712 if (err)
7713 goto done;
7715 err = got_ref_open(&resolved, repo,
7716 got_ref_get_symref_target(new_base_branch), 0);
7717 if (err)
7718 goto done;
7720 err = got_worktree_set_head_ref(worktree, resolved);
7721 if (err)
7722 goto done;
7725 * XXX commits to the base branch could have happened while
7726 * we were busy rebasing; should we store the original commit ID
7727 * when rebase begins and read it back here?
7729 err = got_ref_resolve(&commit_id, repo, resolved);
7730 if (err)
7731 goto done;
7733 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7734 if (err)
7735 goto done;
7737 err = got_object_open_as_commit(&commit, repo,
7738 worktree->base_commit_id);
7739 if (err)
7740 goto done;
7742 err = got_object_id_by_path(&tree_id, repo, commit,
7743 worktree->path_prefix);
7744 if (err)
7745 goto done;
7747 err = delete_rebase_refs(worktree, repo);
7748 if (err)
7749 goto done;
7751 err = get_fileindex_path(&fileindex_path, worktree);
7752 if (err)
7753 goto done;
7755 rfa.worktree = worktree;
7756 rfa.fileindex = fileindex;
7757 rfa.progress_cb = progress_cb;
7758 rfa.progress_arg = progress_arg;
7759 rfa.patch_cb = NULL;
7760 rfa.patch_arg = NULL;
7761 rfa.repo = repo;
7762 rfa.unlink_added_files = 1;
7763 rfa.added_files_to_unlink = &added_paths;
7764 err = worktree_status(worktree, "", fileindex, repo,
7765 revert_file, &rfa, NULL, NULL, 1, 0);
7766 if (err)
7767 goto sync;
7769 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7770 repo, progress_cb, progress_arg, NULL, NULL);
7771 sync:
7772 sync_err = sync_fileindex(fileindex, fileindex_path);
7773 if (sync_err && err == NULL)
7774 err = sync_err;
7775 done:
7776 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7777 got_ref_close(resolved);
7778 free(tree_id);
7779 free(commit_id);
7780 free(merged_commit_id);
7781 if (commit)
7782 got_object_commit_close(commit);
7783 if (fileindex)
7784 got_fileindex_free(fileindex);
7785 free(fileindex_path);
7786 free(commit_ref_name);
7787 if (commit_ref)
7788 got_ref_close(commit_ref);
7790 unlockerr = lock_worktree(worktree, LOCK_SH);
7791 if (unlockerr && err == NULL)
7792 err = unlockerr;
7793 return err;
7796 const struct got_error *
7797 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7798 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7799 struct got_fileindex **fileindex, struct got_worktree *worktree,
7800 struct got_repository *repo)
7802 const struct got_error *err = NULL;
7803 char *tmp_branch_name = NULL;
7804 char *branch_ref_name = NULL;
7805 char *base_commit_ref_name = NULL;
7806 char *fileindex_path = NULL;
7807 struct check_rebase_ok_arg ok_arg;
7808 struct got_reference *wt_branch = NULL;
7809 struct got_reference *base_commit_ref = NULL;
7811 *tmp_branch = NULL;
7812 *branch_ref = NULL;
7813 *base_commit_id = NULL;
7814 *fileindex = NULL;
7816 err = lock_worktree(worktree, LOCK_EX);
7817 if (err)
7818 return err;
7820 err = open_fileindex(fileindex, &fileindex_path, worktree);
7821 if (err)
7822 goto done;
7824 ok_arg.worktree = worktree;
7825 ok_arg.repo = repo;
7826 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7827 &ok_arg);
7828 if (err)
7829 goto done;
7831 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7832 if (err)
7833 goto done;
7835 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7836 if (err)
7837 goto done;
7839 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7840 worktree);
7841 if (err)
7842 goto done;
7844 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7845 0);
7846 if (err)
7847 goto done;
7849 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7850 if (err)
7851 goto done;
7853 err = got_ref_write(*branch_ref, repo);
7854 if (err)
7855 goto done;
7857 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7858 worktree->base_commit_id);
7859 if (err)
7860 goto done;
7861 err = got_ref_write(base_commit_ref, repo);
7862 if (err)
7863 goto done;
7864 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7865 if (*base_commit_id == NULL) {
7866 err = got_error_from_errno("got_object_id_dup");
7867 goto done;
7870 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7871 worktree->base_commit_id);
7872 if (err)
7873 goto done;
7874 err = got_ref_write(*tmp_branch, repo);
7875 if (err)
7876 goto done;
7878 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7879 if (err)
7880 goto done;
7881 done:
7882 free(fileindex_path);
7883 free(tmp_branch_name);
7884 free(branch_ref_name);
7885 free(base_commit_ref_name);
7886 if (wt_branch)
7887 got_ref_close(wt_branch);
7888 if (err) {
7889 if (*branch_ref) {
7890 got_ref_close(*branch_ref);
7891 *branch_ref = NULL;
7893 if (*tmp_branch) {
7894 got_ref_close(*tmp_branch);
7895 *tmp_branch = NULL;
7897 free(*base_commit_id);
7898 if (*fileindex) {
7899 got_fileindex_free(*fileindex);
7900 *fileindex = NULL;
7902 lock_worktree(worktree, LOCK_SH);
7904 return err;
7907 const struct got_error *
7908 got_worktree_histedit_postpone(struct got_worktree *worktree,
7909 struct got_fileindex *fileindex)
7911 if (fileindex)
7912 got_fileindex_free(fileindex);
7913 return lock_worktree(worktree, LOCK_SH);
7916 const struct got_error *
7917 got_worktree_histedit_in_progress(int *in_progress,
7918 struct got_worktree *worktree)
7920 const struct got_error *err;
7921 char *tmp_branch_name = NULL;
7923 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7924 if (err)
7925 return err;
7927 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7928 free(tmp_branch_name);
7929 return NULL;
7932 const struct got_error *
7933 got_worktree_histedit_continue(struct got_object_id **commit_id,
7934 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7935 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7936 struct got_worktree *worktree, struct got_repository *repo)
7938 const struct got_error *err;
7939 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7940 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7941 struct got_reference *commit_ref = NULL;
7942 struct got_reference *base_commit_ref = NULL;
7943 char *fileindex_path = NULL;
7944 int have_staged_files = 0;
7946 *commit_id = NULL;
7947 *tmp_branch = NULL;
7948 *branch_ref = NULL;
7949 *base_commit_id = NULL;
7950 *fileindex = NULL;
7952 err = lock_worktree(worktree, LOCK_EX);
7953 if (err)
7954 return err;
7956 err = open_fileindex(fileindex, &fileindex_path, worktree);
7957 if (err)
7958 goto done;
7960 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7961 &have_staged_files);
7962 if (err && err->code != GOT_ERR_CANCELLED)
7963 goto done;
7964 if (have_staged_files) {
7965 err = got_error(GOT_ERR_STAGED_PATHS);
7966 goto done;
7969 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7970 if (err)
7971 goto done;
7973 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7974 if (err)
7975 goto done;
7977 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7978 if (err)
7979 goto done;
7981 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7982 worktree);
7983 if (err)
7984 goto done;
7986 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7987 if (err)
7988 goto done;
7990 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7991 if (err)
7992 goto done;
7993 err = got_ref_resolve(commit_id, repo, commit_ref);
7994 if (err)
7995 goto done;
7997 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7998 if (err)
7999 goto done;
8000 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8001 if (err)
8002 goto done;
8004 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8005 if (err)
8006 goto done;
8007 done:
8008 free(commit_ref_name);
8009 free(branch_ref_name);
8010 free(fileindex_path);
8011 if (commit_ref)
8012 got_ref_close(commit_ref);
8013 if (base_commit_ref)
8014 got_ref_close(base_commit_ref);
8015 if (err) {
8016 free(*commit_id);
8017 *commit_id = NULL;
8018 free(*base_commit_id);
8019 *base_commit_id = NULL;
8020 if (*tmp_branch) {
8021 got_ref_close(*tmp_branch);
8022 *tmp_branch = NULL;
8024 if (*branch_ref) {
8025 got_ref_close(*branch_ref);
8026 *branch_ref = NULL;
8028 if (*fileindex) {
8029 got_fileindex_free(*fileindex);
8030 *fileindex = NULL;
8032 lock_worktree(worktree, LOCK_EX);
8034 return err;
8037 static const struct got_error *
8038 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8040 const struct got_error *err;
8041 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8042 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8044 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8045 if (err)
8046 goto done;
8047 err = delete_ref(tmp_branch_name, repo);
8048 if (err)
8049 goto done;
8051 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8052 worktree);
8053 if (err)
8054 goto done;
8055 err = delete_ref(base_commit_ref_name, repo);
8056 if (err)
8057 goto done;
8059 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8060 if (err)
8061 goto done;
8062 err = delete_ref(branch_ref_name, repo);
8063 if (err)
8064 goto done;
8066 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8067 if (err)
8068 goto done;
8069 err = delete_ref(commit_ref_name, repo);
8070 if (err)
8071 goto done;
8072 done:
8073 free(tmp_branch_name);
8074 free(base_commit_ref_name);
8075 free(branch_ref_name);
8076 free(commit_ref_name);
8077 return err;
8080 const struct got_error *
8081 got_worktree_histedit_abort(struct got_worktree *worktree,
8082 struct got_fileindex *fileindex, struct got_repository *repo,
8083 struct got_reference *branch, struct got_object_id *base_commit_id,
8084 got_worktree_checkout_cb progress_cb, void *progress_arg)
8086 const struct got_error *err, *unlockerr, *sync_err;
8087 struct got_reference *resolved = NULL;
8088 char *fileindex_path = NULL;
8089 struct got_object_id *merged_commit_id = NULL;
8090 struct got_commit_object *commit = NULL;
8091 char *commit_ref_name = NULL;
8092 struct got_reference *commit_ref = NULL;
8093 struct got_object_id *tree_id = NULL;
8094 struct revert_file_args rfa;
8095 struct got_pathlist_head added_paths;
8097 TAILQ_INIT(&added_paths);
8099 err = lock_worktree(worktree, LOCK_EX);
8100 if (err)
8101 return err;
8103 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8104 if (err)
8105 goto done;
8107 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8108 if (err) {
8109 if (err->code != GOT_ERR_NOT_REF)
8110 goto done;
8111 /* Can happen on early abort due to invalid histedit script. */
8112 commit_ref = NULL;
8115 if (commit_ref) {
8116 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8117 if (err)
8118 goto done;
8121 * Determine which files in added status can be safely removed
8122 * from disk while reverting changes in the work tree.
8123 * We want to avoid deleting unrelated files added by the
8124 * user during conflict resolution or during histedit -e.
8126 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8127 got_worktree_get_path_prefix(worktree), repo);
8128 if (err)
8129 goto done;
8132 err = got_ref_open(&resolved, repo,
8133 got_ref_get_symref_target(branch), 0);
8134 if (err)
8135 goto done;
8137 err = got_worktree_set_head_ref(worktree, resolved);
8138 if (err)
8139 goto done;
8141 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8142 if (err)
8143 goto done;
8145 err = got_object_open_as_commit(&commit, repo,
8146 worktree->base_commit_id);
8147 if (err)
8148 goto done;
8150 err = got_object_id_by_path(&tree_id, repo, commit,
8151 worktree->path_prefix);
8152 if (err)
8153 goto done;
8155 err = delete_histedit_refs(worktree, repo);
8156 if (err)
8157 goto done;
8159 err = get_fileindex_path(&fileindex_path, worktree);
8160 if (err)
8161 goto done;
8163 rfa.worktree = worktree;
8164 rfa.fileindex = fileindex;
8165 rfa.progress_cb = progress_cb;
8166 rfa.progress_arg = progress_arg;
8167 rfa.patch_cb = NULL;
8168 rfa.patch_arg = NULL;
8169 rfa.repo = repo;
8170 rfa.unlink_added_files = 1;
8171 rfa.added_files_to_unlink = &added_paths;
8172 err = worktree_status(worktree, "", fileindex, repo,
8173 revert_file, &rfa, NULL, NULL, 1, 0);
8174 if (err)
8175 goto sync;
8177 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8178 repo, progress_cb, progress_arg, NULL, NULL);
8179 sync:
8180 sync_err = sync_fileindex(fileindex, fileindex_path);
8181 if (sync_err && err == NULL)
8182 err = sync_err;
8183 done:
8184 if (resolved)
8185 got_ref_close(resolved);
8186 if (commit_ref)
8187 got_ref_close(commit_ref);
8188 free(merged_commit_id);
8189 free(tree_id);
8190 free(fileindex_path);
8191 free(commit_ref_name);
8193 unlockerr = lock_worktree(worktree, LOCK_SH);
8194 if (unlockerr && err == NULL)
8195 err = unlockerr;
8196 return err;
8199 const struct got_error *
8200 got_worktree_histedit_complete(struct got_worktree *worktree,
8201 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8202 struct got_reference *edited_branch, struct got_repository *repo)
8204 const struct got_error *err, *unlockerr, *sync_err;
8205 struct got_object_id *new_head_commit_id = NULL;
8206 struct got_reference *resolved = NULL;
8207 char *fileindex_path = NULL;
8209 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8210 if (err)
8211 return err;
8213 err = got_ref_open(&resolved, repo,
8214 got_ref_get_symref_target(edited_branch), 0);
8215 if (err)
8216 goto done;
8218 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8219 resolved, new_head_commit_id, repo);
8220 if (err)
8221 goto done;
8223 err = got_ref_change_ref(resolved, new_head_commit_id);
8224 if (err)
8225 goto done;
8227 err = got_ref_write(resolved, repo);
8228 if (err)
8229 goto done;
8231 err = got_worktree_set_head_ref(worktree, resolved);
8232 if (err)
8233 goto done;
8235 err = delete_histedit_refs(worktree, repo);
8236 if (err)
8237 goto done;
8239 err = get_fileindex_path(&fileindex_path, worktree);
8240 if (err)
8241 goto done;
8242 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8243 sync_err = sync_fileindex(fileindex, fileindex_path);
8244 if (sync_err && err == NULL)
8245 err = sync_err;
8246 done:
8247 got_fileindex_free(fileindex);
8248 free(fileindex_path);
8249 free(new_head_commit_id);
8250 unlockerr = lock_worktree(worktree, LOCK_SH);
8251 if (unlockerr && err == NULL)
8252 err = unlockerr;
8253 return err;
8256 const struct got_error *
8257 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8258 struct got_object_id *commit_id, struct got_repository *repo)
8260 const struct got_error *err;
8261 char *commit_ref_name;
8263 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8264 if (err)
8265 return err;
8267 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8268 if (err)
8269 goto done;
8271 err = delete_ref(commit_ref_name, repo);
8272 done:
8273 free(commit_ref_name);
8274 return err;
8277 const struct got_error *
8278 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8279 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8280 struct got_worktree *worktree, const char *refname,
8281 struct got_repository *repo)
8283 const struct got_error *err = NULL;
8284 char *fileindex_path = NULL;
8285 struct check_rebase_ok_arg ok_arg;
8287 *fileindex = NULL;
8288 *branch_ref = NULL;
8289 *base_branch_ref = NULL;
8291 err = lock_worktree(worktree, LOCK_EX);
8292 if (err)
8293 return err;
8295 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8296 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8297 "cannot integrate a branch into itself; "
8298 "update -b or different branch name required");
8299 goto done;
8302 err = open_fileindex(fileindex, &fileindex_path, worktree);
8303 if (err)
8304 goto done;
8306 /* Preconditions are the same as for rebase. */
8307 ok_arg.worktree = worktree;
8308 ok_arg.repo = repo;
8309 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8310 &ok_arg);
8311 if (err)
8312 goto done;
8314 err = got_ref_open(branch_ref, repo, refname, 1);
8315 if (err)
8316 goto done;
8318 err = got_ref_open(base_branch_ref, repo,
8319 got_worktree_get_head_ref_name(worktree), 1);
8320 done:
8321 if (err) {
8322 if (*branch_ref) {
8323 got_ref_close(*branch_ref);
8324 *branch_ref = NULL;
8326 if (*base_branch_ref) {
8327 got_ref_close(*base_branch_ref);
8328 *base_branch_ref = NULL;
8330 if (*fileindex) {
8331 got_fileindex_free(*fileindex);
8332 *fileindex = NULL;
8334 lock_worktree(worktree, LOCK_SH);
8336 return err;
8339 const struct got_error *
8340 got_worktree_integrate_continue(struct got_worktree *worktree,
8341 struct got_fileindex *fileindex, struct got_repository *repo,
8342 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8343 got_worktree_checkout_cb progress_cb, void *progress_arg,
8344 got_cancel_cb cancel_cb, void *cancel_arg)
8346 const struct got_error *err = NULL, *sync_err, *unlockerr;
8347 char *fileindex_path = NULL;
8348 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8349 struct got_commit_object *commit = NULL;
8351 err = get_fileindex_path(&fileindex_path, worktree);
8352 if (err)
8353 goto done;
8355 err = got_ref_resolve(&commit_id, repo, branch_ref);
8356 if (err)
8357 goto done;
8359 err = got_object_open_as_commit(&commit, repo, commit_id);
8360 if (err)
8361 goto done;
8363 err = got_object_id_by_path(&tree_id, repo, commit,
8364 worktree->path_prefix);
8365 if (err)
8366 goto done;
8368 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8369 if (err)
8370 goto done;
8372 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8373 progress_cb, progress_arg, cancel_cb, cancel_arg);
8374 if (err)
8375 goto sync;
8377 err = got_ref_change_ref(base_branch_ref, commit_id);
8378 if (err)
8379 goto sync;
8381 err = got_ref_write(base_branch_ref, repo);
8382 if (err)
8383 goto sync;
8385 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8386 sync:
8387 sync_err = sync_fileindex(fileindex, fileindex_path);
8388 if (sync_err && err == NULL)
8389 err = sync_err;
8391 done:
8392 unlockerr = got_ref_unlock(branch_ref);
8393 if (unlockerr && err == NULL)
8394 err = unlockerr;
8395 got_ref_close(branch_ref);
8397 unlockerr = got_ref_unlock(base_branch_ref);
8398 if (unlockerr && err == NULL)
8399 err = unlockerr;
8400 got_ref_close(base_branch_ref);
8402 got_fileindex_free(fileindex);
8403 free(fileindex_path);
8404 free(tree_id);
8405 if (commit)
8406 got_object_commit_close(commit);
8408 unlockerr = lock_worktree(worktree, LOCK_SH);
8409 if (unlockerr && err == NULL)
8410 err = unlockerr;
8411 return err;
8414 const struct got_error *
8415 got_worktree_integrate_abort(struct got_worktree *worktree,
8416 struct got_fileindex *fileindex, struct got_repository *repo,
8417 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8419 const struct got_error *err = NULL, *unlockerr = NULL;
8421 got_fileindex_free(fileindex);
8423 err = lock_worktree(worktree, LOCK_SH);
8425 unlockerr = got_ref_unlock(branch_ref);
8426 if (unlockerr && err == NULL)
8427 err = unlockerr;
8428 got_ref_close(branch_ref);
8430 unlockerr = got_ref_unlock(base_branch_ref);
8431 if (unlockerr && err == NULL)
8432 err = unlockerr;
8433 got_ref_close(base_branch_ref);
8435 return err;
8438 const struct got_error *
8439 got_worktree_merge_postpone(struct got_worktree *worktree,
8440 struct got_fileindex *fileindex)
8442 const struct got_error *err, *sync_err;
8443 char *fileindex_path = NULL;
8445 err = get_fileindex_path(&fileindex_path, worktree);
8446 if (err)
8447 goto done;
8449 sync_err = sync_fileindex(fileindex, fileindex_path);
8451 err = lock_worktree(worktree, LOCK_SH);
8452 if (sync_err && err == NULL)
8453 err = sync_err;
8454 done:
8455 got_fileindex_free(fileindex);
8456 free(fileindex_path);
8457 return err;
8460 static const struct got_error *
8461 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8463 const struct got_error *err;
8464 char *branch_refname = NULL, *commit_refname = NULL;
8466 err = get_merge_branch_ref_name(&branch_refname, worktree);
8467 if (err)
8468 goto done;
8469 err = delete_ref(branch_refname, repo);
8470 if (err)
8471 goto done;
8473 err = get_merge_commit_ref_name(&commit_refname, worktree);
8474 if (err)
8475 goto done;
8476 err = delete_ref(commit_refname, repo);
8477 if (err)
8478 goto done;
8480 done:
8481 free(branch_refname);
8482 free(commit_refname);
8483 return err;
8486 struct merge_commit_msg_arg {
8487 struct got_worktree *worktree;
8488 const char *branch_name;
8491 static const struct got_error *
8492 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8493 const char *diff_path, char **logmsg, void *arg)
8495 struct merge_commit_msg_arg *a = arg;
8497 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8498 got_worktree_get_head_ref_name(a->worktree)) == -1)
8499 return got_error_from_errno("asprintf");
8501 return NULL;
8505 const struct got_error *
8506 got_worktree_merge_branch(struct got_worktree *worktree,
8507 struct got_fileindex *fileindex,
8508 struct got_object_id *yca_commit_id,
8509 struct got_object_id *branch_tip,
8510 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8511 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8513 const struct got_error *err;
8514 char *fileindex_path = NULL;
8515 struct check_mixed_commits_args cma;
8517 err = get_fileindex_path(&fileindex_path, worktree);
8518 if (err)
8519 goto done;
8521 cma.worktree = worktree;
8522 cma.cancel_cb = cancel_cb;
8523 cma.cancel_arg = cancel_arg;
8525 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8526 &cma);
8527 if (err)
8528 goto done;
8530 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8531 branch_tip, repo, progress_cb, progress_arg,
8532 cancel_cb, cancel_arg);
8533 done:
8534 free(fileindex_path);
8535 return err;
8538 const struct got_error *
8539 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8540 struct got_worktree *worktree, struct got_fileindex *fileindex,
8541 const char *author, const char *committer, int allow_bad_symlinks,
8542 struct got_object_id *branch_tip, const char *branch_name,
8543 int allow_conflict, struct got_repository *repo,
8544 got_worktree_status_cb status_cb, void *status_arg)
8547 const struct got_error *err = NULL, *sync_err;
8548 struct got_pathlist_head commitable_paths;
8549 struct collect_commitables_arg cc_arg;
8550 struct got_pathlist_entry *pe;
8551 struct got_reference *head_ref = NULL;
8552 struct got_object_id *head_commit_id = NULL;
8553 int have_staged_files = 0;
8554 struct merge_commit_msg_arg mcm_arg;
8555 char *fileindex_path = NULL;
8557 memset(&cc_arg, 0, sizeof(cc_arg));
8558 *new_commit_id = NULL;
8560 TAILQ_INIT(&commitable_paths);
8562 err = get_fileindex_path(&fileindex_path, worktree);
8563 if (err)
8564 goto done;
8566 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8567 if (err)
8568 goto done;
8570 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8571 if (err)
8572 goto done;
8574 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8575 &have_staged_files);
8576 if (err && err->code != GOT_ERR_CANCELLED)
8577 goto done;
8578 if (have_staged_files) {
8579 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8580 goto done;
8583 cc_arg.commitable_paths = &commitable_paths;
8584 cc_arg.worktree = worktree;
8585 cc_arg.fileindex = fileindex;
8586 cc_arg.repo = repo;
8587 cc_arg.have_staged_files = have_staged_files;
8588 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8589 cc_arg.commit_conflicts = allow_conflict;
8590 err = worktree_status(worktree, "", fileindex, repo,
8591 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8592 if (err)
8593 goto done;
8595 mcm_arg.worktree = worktree;
8596 mcm_arg.branch_name = branch_name;
8597 err = commit_worktree(new_commit_id, &commitable_paths,
8598 head_commit_id, branch_tip, worktree, author, committer, NULL,
8599 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8600 if (err)
8601 goto done;
8603 err = update_fileindex_after_commit(worktree, &commitable_paths,
8604 *new_commit_id, fileindex, have_staged_files);
8605 sync_err = sync_fileindex(fileindex, fileindex_path);
8606 if (sync_err && err == NULL)
8607 err = sync_err;
8608 done:
8609 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8610 struct got_commitable *ct = pe->data;
8612 free_commitable(ct);
8614 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8615 free(fileindex_path);
8616 return err;
8619 const struct got_error *
8620 got_worktree_merge_complete(struct got_worktree *worktree,
8621 struct got_fileindex *fileindex, struct got_repository *repo)
8623 const struct got_error *err, *unlockerr, *sync_err;
8624 char *fileindex_path = NULL;
8626 err = delete_merge_refs(worktree, repo);
8627 if (err)
8628 goto done;
8630 err = get_fileindex_path(&fileindex_path, worktree);
8631 if (err)
8632 goto done;
8633 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8634 sync_err = sync_fileindex(fileindex, fileindex_path);
8635 if (sync_err && err == NULL)
8636 err = sync_err;
8637 done:
8638 got_fileindex_free(fileindex);
8639 free(fileindex_path);
8640 unlockerr = lock_worktree(worktree, LOCK_SH);
8641 if (unlockerr && err == NULL)
8642 err = unlockerr;
8643 return err;
8646 const struct got_error *
8647 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8648 struct got_repository *repo)
8650 const struct got_error *err;
8651 char *branch_refname = NULL;
8652 struct got_reference *branch_ref = NULL;
8654 *in_progress = 0;
8656 err = get_merge_branch_ref_name(&branch_refname, worktree);
8657 if (err)
8658 return err;
8659 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8660 free(branch_refname);
8661 if (err) {
8662 if (err->code != GOT_ERR_NOT_REF)
8663 return err;
8664 } else
8665 *in_progress = 1;
8667 return NULL;
8670 const struct got_error *got_worktree_merge_prepare(
8671 struct got_fileindex **fileindex, struct got_worktree *worktree,
8672 struct got_repository *repo)
8674 const struct got_error *err = NULL;
8675 char *fileindex_path = NULL;
8676 struct got_reference *wt_branch = NULL;
8677 struct got_object_id *wt_branch_tip = NULL;
8678 struct check_rebase_ok_arg ok_arg;
8680 *fileindex = NULL;
8682 err = lock_worktree(worktree, LOCK_EX);
8683 if (err)
8684 return err;
8686 err = open_fileindex(fileindex, &fileindex_path, worktree);
8687 if (err)
8688 goto done;
8690 /* Preconditions are the same as for rebase. */
8691 ok_arg.worktree = worktree;
8692 ok_arg.repo = repo;
8693 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8694 &ok_arg);
8695 if (err)
8696 goto done;
8698 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8699 0);
8700 if (err)
8701 goto done;
8703 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8704 if (err)
8705 goto done;
8707 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8708 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8709 goto done;
8712 done:
8713 free(fileindex_path);
8714 if (wt_branch)
8715 got_ref_close(wt_branch);
8716 free(wt_branch_tip);
8717 if (err) {
8718 if (*fileindex) {
8719 got_fileindex_free(*fileindex);
8720 *fileindex = NULL;
8722 lock_worktree(worktree, LOCK_SH);
8724 return err;
8727 const struct got_error *got_worktree_merge_write_refs(
8728 struct got_worktree *worktree, struct got_reference *branch,
8729 struct got_repository *repo)
8731 const struct got_error *err = NULL;
8732 char *branch_refname = NULL, *commit_refname = NULL;
8733 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8734 struct got_object_id *branch_tip = NULL;
8736 err = get_merge_branch_ref_name(&branch_refname, worktree);
8737 if (err)
8738 return err;
8740 err = get_merge_commit_ref_name(&commit_refname, worktree);
8741 if (err)
8742 return err;
8744 err = got_ref_resolve(&branch_tip, repo, branch);
8745 if (err)
8746 goto done;
8748 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8749 if (err)
8750 goto done;
8751 err = got_ref_write(branch_ref, repo);
8752 if (err)
8753 goto done;
8755 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8756 if (err)
8757 goto done;
8758 err = got_ref_write(commit_ref, repo);
8759 if (err)
8760 goto done;
8762 done:
8763 free(branch_refname);
8764 free(commit_refname);
8765 if (branch_ref)
8766 got_ref_close(branch_ref);
8767 if (commit_ref)
8768 got_ref_close(commit_ref);
8769 free(branch_tip);
8770 return err;
8773 const struct got_error *
8774 got_worktree_merge_continue(char **branch_name,
8775 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8776 struct got_worktree *worktree, struct got_repository *repo)
8778 const struct got_error *err;
8779 char *commit_refname = NULL, *branch_refname = NULL;
8780 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8781 char *fileindex_path = NULL;
8782 int have_staged_files = 0;
8784 *branch_name = NULL;
8785 *branch_tip = NULL;
8786 *fileindex = NULL;
8788 err = lock_worktree(worktree, LOCK_EX);
8789 if (err)
8790 return err;
8792 err = open_fileindex(fileindex, &fileindex_path, worktree);
8793 if (err)
8794 goto done;
8796 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8797 &have_staged_files);
8798 if (err && err->code != GOT_ERR_CANCELLED)
8799 goto done;
8800 if (have_staged_files) {
8801 err = got_error(GOT_ERR_STAGED_PATHS);
8802 goto done;
8805 err = get_merge_branch_ref_name(&branch_refname, worktree);
8806 if (err)
8807 goto done;
8809 err = get_merge_commit_ref_name(&commit_refname, worktree);
8810 if (err)
8811 goto done;
8813 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8814 if (err)
8815 goto done;
8817 if (!got_ref_is_symbolic(branch_ref)) {
8818 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8819 "%s is not a symbolic reference",
8820 got_ref_get_name(branch_ref));
8821 goto done;
8823 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8824 if (*branch_name == NULL) {
8825 err = got_error_from_errno("strdup");
8826 goto done;
8829 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8830 if (err)
8831 goto done;
8833 err = got_ref_resolve(branch_tip, repo, commit_ref);
8834 if (err)
8835 goto done;
8836 done:
8837 free(commit_refname);
8838 free(branch_refname);
8839 free(fileindex_path);
8840 if (commit_ref)
8841 got_ref_close(commit_ref);
8842 if (branch_ref)
8843 got_ref_close(branch_ref);
8844 if (err) {
8845 if (*branch_name) {
8846 free(*branch_name);
8847 *branch_name = NULL;
8849 free(*branch_tip);
8850 *branch_tip = NULL;
8851 if (*fileindex) {
8852 got_fileindex_free(*fileindex);
8853 *fileindex = NULL;
8855 lock_worktree(worktree, LOCK_SH);
8857 return err;
8860 const struct got_error *
8861 got_worktree_merge_abort(struct got_worktree *worktree,
8862 struct got_fileindex *fileindex, struct got_repository *repo,
8863 got_worktree_checkout_cb progress_cb, void *progress_arg)
8865 const struct got_error *err, *unlockerr, *sync_err;
8866 struct got_commit_object *commit = NULL;
8867 char *fileindex_path = NULL;
8868 struct revert_file_args rfa;
8869 char *commit_ref_name = NULL;
8870 struct got_reference *commit_ref = NULL;
8871 struct got_object_id *merged_commit_id = NULL;
8872 struct got_object_id *tree_id = NULL;
8873 struct got_pathlist_head added_paths;
8875 TAILQ_INIT(&added_paths);
8877 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8878 if (err)
8879 goto done;
8881 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8882 if (err)
8883 goto done;
8885 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8886 if (err)
8887 goto done;
8890 * Determine which files in added status can be safely removed
8891 * from disk while reverting changes in the work tree.
8892 * We want to avoid deleting unrelated files which were added by
8893 * the user for conflict resolution purposes.
8895 err = get_paths_added_between_commits(&added_paths,
8896 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8897 got_worktree_get_path_prefix(worktree), repo);
8898 if (err)
8899 goto done;
8902 err = got_object_open_as_commit(&commit, repo,
8903 worktree->base_commit_id);
8904 if (err)
8905 goto done;
8907 err = got_object_id_by_path(&tree_id, repo, commit,
8908 worktree->path_prefix);
8909 if (err)
8910 goto done;
8912 err = delete_merge_refs(worktree, repo);
8913 if (err)
8914 goto done;
8916 err = get_fileindex_path(&fileindex_path, worktree);
8917 if (err)
8918 goto done;
8920 rfa.worktree = worktree;
8921 rfa.fileindex = fileindex;
8922 rfa.progress_cb = progress_cb;
8923 rfa.progress_arg = progress_arg;
8924 rfa.patch_cb = NULL;
8925 rfa.patch_arg = NULL;
8926 rfa.repo = repo;
8927 rfa.unlink_added_files = 1;
8928 rfa.added_files_to_unlink = &added_paths;
8929 err = worktree_status(worktree, "", fileindex, repo,
8930 revert_file, &rfa, NULL, NULL, 1, 0);
8931 if (err)
8932 goto sync;
8934 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8935 repo, progress_cb, progress_arg, NULL, NULL);
8936 sync:
8937 sync_err = sync_fileindex(fileindex, fileindex_path);
8938 if (sync_err && err == NULL)
8939 err = sync_err;
8940 done:
8941 free(tree_id);
8942 free(merged_commit_id);
8943 if (commit)
8944 got_object_commit_close(commit);
8945 if (fileindex)
8946 got_fileindex_free(fileindex);
8947 free(fileindex_path);
8948 if (commit_ref)
8949 got_ref_close(commit_ref);
8950 free(commit_ref_name);
8952 unlockerr = lock_worktree(worktree, LOCK_SH);
8953 if (unlockerr && err == NULL)
8954 err = unlockerr;
8955 return err;
8958 struct check_stage_ok_arg {
8959 struct got_object_id *head_commit_id;
8960 struct got_worktree *worktree;
8961 struct got_fileindex *fileindex;
8962 struct got_repository *repo;
8963 int have_changes;
8966 static const struct got_error *
8967 check_stage_ok(void *arg, unsigned char status,
8968 unsigned char staged_status, const char *relpath,
8969 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8970 struct got_object_id *commit_id, int dirfd, const char *de_name)
8972 struct check_stage_ok_arg *a = arg;
8973 const struct got_error *err = NULL;
8974 struct got_fileindex_entry *ie;
8975 struct got_object_id base_commit_id;
8976 struct got_object_id *base_commit_idp = NULL;
8977 char *in_repo_path = NULL, *p;
8979 if (status == GOT_STATUS_UNVERSIONED ||
8980 status == GOT_STATUS_NO_CHANGE)
8981 return NULL;
8982 if (status == GOT_STATUS_NONEXISTENT)
8983 return got_error_set_errno(ENOENT, relpath);
8985 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8986 if (ie == NULL)
8987 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8989 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8990 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8991 relpath) == -1)
8992 return got_error_from_errno("asprintf");
8994 if (got_fileindex_entry_has_commit(ie)) {
8995 base_commit_idp = got_fileindex_entry_get_commit_id(
8996 &base_commit_id, ie);
8999 if (status == GOT_STATUS_CONFLICT) {
9000 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9001 goto done;
9002 } else if (status != GOT_STATUS_ADD &&
9003 status != GOT_STATUS_MODIFY &&
9004 status != GOT_STATUS_DELETE) {
9005 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9006 goto done;
9009 a->have_changes = 1;
9011 p = in_repo_path;
9012 while (p[0] == '/')
9013 p++;
9014 err = check_out_of_date(p, status, staged_status,
9015 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9016 GOT_ERR_STAGE_OUT_OF_DATE);
9017 done:
9018 free(in_repo_path);
9019 return err;
9022 struct stage_path_arg {
9023 struct got_worktree *worktree;
9024 struct got_fileindex *fileindex;
9025 struct got_repository *repo;
9026 got_worktree_status_cb status_cb;
9027 void *status_arg;
9028 got_worktree_patch_cb patch_cb;
9029 void *patch_arg;
9030 int staged_something;
9031 int allow_bad_symlinks;
9034 static const struct got_error *
9035 stage_path(void *arg, unsigned char status,
9036 unsigned char staged_status, const char *relpath,
9037 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9038 struct got_object_id *commit_id, int dirfd, const char *de_name)
9040 struct stage_path_arg *a = arg;
9041 const struct got_error *err = NULL;
9042 struct got_fileindex_entry *ie;
9043 char *ondisk_path = NULL, *path_content = NULL;
9044 uint32_t stage;
9045 struct got_object_id *new_staged_blob_id = NULL;
9046 struct stat sb;
9048 if (status == GOT_STATUS_UNVERSIONED)
9049 return NULL;
9051 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9052 if (ie == NULL)
9053 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9055 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9056 relpath)== -1)
9057 return got_error_from_errno("asprintf");
9059 switch (status) {
9060 case GOT_STATUS_ADD:
9061 case GOT_STATUS_MODIFY:
9062 /* XXX could sb.st_mode be passed in by our caller? */
9063 if (lstat(ondisk_path, &sb) == -1) {
9064 err = got_error_from_errno2("lstat", ondisk_path);
9065 break;
9067 if (a->patch_cb) {
9068 if (status == GOT_STATUS_ADD) {
9069 int choice = GOT_PATCH_CHOICE_NONE;
9070 err = (*a->patch_cb)(&choice, a->patch_arg,
9071 status, ie->path, NULL, 1, 1);
9072 if (err)
9073 break;
9074 if (choice != GOT_PATCH_CHOICE_YES)
9075 break;
9076 } else {
9077 err = create_patched_content(&path_content, 0,
9078 staged_blob_id ? staged_blob_id : blob_id,
9079 ondisk_path, dirfd, de_name, ie->path,
9080 a->repo, a->patch_cb, a->patch_arg);
9081 if (err || path_content == NULL)
9082 break;
9085 err = got_object_blob_create(&new_staged_blob_id,
9086 path_content ? path_content : ondisk_path, a->repo);
9087 if (err)
9088 break;
9089 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9090 SHA1_DIGEST_LENGTH);
9091 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9092 stage = GOT_FILEIDX_STAGE_ADD;
9093 else
9094 stage = GOT_FILEIDX_STAGE_MODIFY;
9095 got_fileindex_entry_stage_set(ie, stage);
9096 if (S_ISLNK(sb.st_mode)) {
9097 int is_bad_symlink = 0;
9098 if (!a->allow_bad_symlinks) {
9099 char target_path[PATH_MAX];
9100 ssize_t target_len;
9101 target_len = readlink(ondisk_path, target_path,
9102 sizeof(target_path));
9103 if (target_len == -1) {
9104 err = got_error_from_errno2("readlink",
9105 ondisk_path);
9106 break;
9108 err = is_bad_symlink_target(&is_bad_symlink,
9109 target_path, target_len, ondisk_path,
9110 a->worktree->root_path,
9111 a->worktree->meta_dir);
9112 if (err)
9113 break;
9114 if (is_bad_symlink) {
9115 err = got_error_path(ondisk_path,
9116 GOT_ERR_BAD_SYMLINK);
9117 break;
9120 if (is_bad_symlink)
9121 got_fileindex_entry_staged_filetype_set(ie,
9122 GOT_FILEIDX_MODE_BAD_SYMLINK);
9123 else
9124 got_fileindex_entry_staged_filetype_set(ie,
9125 GOT_FILEIDX_MODE_SYMLINK);
9126 } else {
9127 got_fileindex_entry_staged_filetype_set(ie,
9128 GOT_FILEIDX_MODE_REGULAR_FILE);
9130 a->staged_something = 1;
9131 if (a->status_cb == NULL)
9132 break;
9133 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9134 get_staged_status(ie), relpath, blob_id,
9135 new_staged_blob_id, NULL, dirfd, de_name);
9136 if (err)
9137 break;
9139 * When staging the reverse of the staged diff,
9140 * implicitly unstage the file.
9142 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9143 sizeof(ie->blob_sha1)) == 0) {
9144 got_fileindex_entry_stage_set(ie,
9145 GOT_FILEIDX_STAGE_NONE);
9147 break;
9148 case GOT_STATUS_DELETE:
9149 if (staged_status == GOT_STATUS_DELETE)
9150 break;
9151 if (a->patch_cb) {
9152 int choice = GOT_PATCH_CHOICE_NONE;
9153 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9154 ie->path, NULL, 1, 1);
9155 if (err)
9156 break;
9157 if (choice == GOT_PATCH_CHOICE_NO)
9158 break;
9159 if (choice != GOT_PATCH_CHOICE_YES) {
9160 err = got_error(GOT_ERR_PATCH_CHOICE);
9161 break;
9164 stage = GOT_FILEIDX_STAGE_DELETE;
9165 got_fileindex_entry_stage_set(ie, stage);
9166 a->staged_something = 1;
9167 if (a->status_cb == NULL)
9168 break;
9169 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9170 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9171 de_name);
9172 break;
9173 case GOT_STATUS_NO_CHANGE:
9174 break;
9175 case GOT_STATUS_CONFLICT:
9176 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9177 break;
9178 case GOT_STATUS_NONEXISTENT:
9179 err = got_error_set_errno(ENOENT, relpath);
9180 break;
9181 default:
9182 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9183 break;
9186 if (path_content && unlink(path_content) == -1 && err == NULL)
9187 err = got_error_from_errno2("unlink", path_content);
9188 free(path_content);
9189 free(ondisk_path);
9190 free(new_staged_blob_id);
9191 return err;
9194 const struct got_error *
9195 got_worktree_stage(struct got_worktree *worktree,
9196 struct got_pathlist_head *paths,
9197 got_worktree_status_cb status_cb, void *status_arg,
9198 got_worktree_patch_cb patch_cb, void *patch_arg,
9199 int allow_bad_symlinks, struct got_repository *repo)
9201 const struct got_error *err = NULL, *sync_err, *unlockerr;
9202 struct got_pathlist_entry *pe;
9203 struct got_fileindex *fileindex = NULL;
9204 char *fileindex_path = NULL;
9205 struct got_reference *head_ref = NULL;
9206 struct got_object_id *head_commit_id = NULL;
9207 struct check_stage_ok_arg oka;
9208 struct stage_path_arg spa;
9210 err = lock_worktree(worktree, LOCK_EX);
9211 if (err)
9212 return err;
9214 err = got_ref_open(&head_ref, repo,
9215 got_worktree_get_head_ref_name(worktree), 0);
9216 if (err)
9217 goto done;
9218 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9219 if (err)
9220 goto done;
9221 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9222 if (err)
9223 goto done;
9225 /* Check pre-conditions before staging anything. */
9226 oka.head_commit_id = head_commit_id;
9227 oka.worktree = worktree;
9228 oka.fileindex = fileindex;
9229 oka.repo = repo;
9230 oka.have_changes = 0;
9231 TAILQ_FOREACH(pe, paths, entry) {
9232 err = worktree_status(worktree, pe->path, fileindex, repo,
9233 check_stage_ok, &oka, NULL, NULL, 1, 0);
9234 if (err)
9235 goto done;
9237 if (!oka.have_changes) {
9238 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9239 goto done;
9242 spa.worktree = worktree;
9243 spa.fileindex = fileindex;
9244 spa.repo = repo;
9245 spa.patch_cb = patch_cb;
9246 spa.patch_arg = patch_arg;
9247 spa.status_cb = status_cb;
9248 spa.status_arg = status_arg;
9249 spa.staged_something = 0;
9250 spa.allow_bad_symlinks = allow_bad_symlinks;
9251 TAILQ_FOREACH(pe, paths, entry) {
9252 err = worktree_status(worktree, pe->path, fileindex, repo,
9253 stage_path, &spa, NULL, NULL, 1, 0);
9254 if (err)
9255 goto done;
9257 if (!spa.staged_something) {
9258 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9259 goto done;
9262 sync_err = sync_fileindex(fileindex, fileindex_path);
9263 if (sync_err && err == NULL)
9264 err = sync_err;
9265 done:
9266 if (head_ref)
9267 got_ref_close(head_ref);
9268 free(head_commit_id);
9269 free(fileindex_path);
9270 if (fileindex)
9271 got_fileindex_free(fileindex);
9272 unlockerr = lock_worktree(worktree, LOCK_SH);
9273 if (unlockerr && err == NULL)
9274 err = unlockerr;
9275 return err;
9278 struct unstage_path_arg {
9279 struct got_worktree *worktree;
9280 struct got_fileindex *fileindex;
9281 struct got_repository *repo;
9282 got_worktree_checkout_cb progress_cb;
9283 void *progress_arg;
9284 got_worktree_patch_cb patch_cb;
9285 void *patch_arg;
9288 static const struct got_error *
9289 create_unstaged_content(char **path_unstaged_content,
9290 char **path_new_staged_content, struct got_object_id *blob_id,
9291 struct got_object_id *staged_blob_id, const char *relpath,
9292 struct got_repository *repo,
9293 got_worktree_patch_cb patch_cb, void *patch_arg)
9295 const struct got_error *err, *free_err;
9296 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9297 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9298 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9299 struct got_diffreg_result *diffreg_result = NULL;
9300 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9301 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9302 int fd1 = -1, fd2 = -1;
9304 *path_unstaged_content = NULL;
9305 *path_new_staged_content = NULL;
9307 err = got_object_id_str(&label1, blob_id);
9308 if (err)
9309 return err;
9311 fd1 = got_opentempfd();
9312 if (fd1 == -1) {
9313 err = got_error_from_errno("got_opentempfd");
9314 goto done;
9316 fd2 = got_opentempfd();
9317 if (fd2 == -1) {
9318 err = got_error_from_errno("got_opentempfd");
9319 goto done;
9322 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9323 if (err)
9324 goto done;
9326 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9327 if (err)
9328 goto done;
9330 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9331 if (err)
9332 goto done;
9334 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9335 fd2);
9336 if (err)
9337 goto done;
9339 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9340 if (err)
9341 goto done;
9343 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9344 if (err)
9345 goto done;
9347 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9348 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9349 if (err)
9350 goto done;
9352 err = got_opentemp_named(path_unstaged_content, &outfile,
9353 "got-unstaged-content", "");
9354 if (err)
9355 goto done;
9356 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9357 "got-new-staged-content", "");
9358 if (err)
9359 goto done;
9361 if (fseek(f1, 0L, SEEK_SET) == -1) {
9362 err = got_ferror(f1, GOT_ERR_IO);
9363 goto done;
9365 if (fseek(f2, 0L, SEEK_SET) == -1) {
9366 err = got_ferror(f2, GOT_ERR_IO);
9367 goto done;
9369 /* Count the number of actual changes in the diff result. */
9370 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9371 struct diff_chunk_context cc = {};
9372 diff_chunk_context_load_change(&cc, &nchunks_used,
9373 diffreg_result->result, n, 0);
9374 nchanges++;
9376 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9377 int choice;
9378 err = apply_or_reject_change(&choice, &nchunks_used,
9379 diffreg_result->result, n, relpath, f1, f2,
9380 &line_cur1, &line_cur2,
9381 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9382 if (err)
9383 goto done;
9384 if (choice == GOT_PATCH_CHOICE_YES)
9385 have_content = 1;
9386 else
9387 have_rejected_content = 1;
9388 if (choice == GOT_PATCH_CHOICE_QUIT)
9389 break;
9391 if (have_content || have_rejected_content)
9392 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9393 outfile, rejectfile);
9394 done:
9395 free(label1);
9396 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9397 err = got_error_from_errno("close");
9398 if (blob)
9399 got_object_blob_close(blob);
9400 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9401 err = got_error_from_errno("close");
9402 if (staged_blob)
9403 got_object_blob_close(staged_blob);
9404 free_err = got_diffreg_result_free(diffreg_result);
9405 if (free_err && err == NULL)
9406 err = free_err;
9407 if (f1 && fclose(f1) == EOF && err == NULL)
9408 err = got_error_from_errno2("fclose", path1);
9409 if (f2 && fclose(f2) == EOF && err == NULL)
9410 err = got_error_from_errno2("fclose", path2);
9411 if (outfile && fclose(outfile) == EOF && err == NULL)
9412 err = got_error_from_errno2("fclose", *path_unstaged_content);
9413 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9414 err = got_error_from_errno2("fclose", *path_new_staged_content);
9415 if (path1 && unlink(path1) == -1 && err == NULL)
9416 err = got_error_from_errno2("unlink", path1);
9417 if (path2 && unlink(path2) == -1 && err == NULL)
9418 err = got_error_from_errno2("unlink", path2);
9419 if (err || !have_content) {
9420 if (*path_unstaged_content &&
9421 unlink(*path_unstaged_content) == -1 && err == NULL)
9422 err = got_error_from_errno2("unlink",
9423 *path_unstaged_content);
9424 free(*path_unstaged_content);
9425 *path_unstaged_content = NULL;
9427 if (err || !have_content || !have_rejected_content) {
9428 if (*path_new_staged_content &&
9429 unlink(*path_new_staged_content) == -1 && err == NULL)
9430 err = got_error_from_errno2("unlink",
9431 *path_new_staged_content);
9432 free(*path_new_staged_content);
9433 *path_new_staged_content = NULL;
9435 free(path1);
9436 free(path2);
9437 return err;
9440 static const struct got_error *
9441 unstage_hunks(struct got_object_id *staged_blob_id,
9442 struct got_blob_object *blob_base,
9443 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9444 const char *ondisk_path, const char *label_orig,
9445 struct got_worktree *worktree, struct got_repository *repo,
9446 got_worktree_patch_cb patch_cb, void *patch_arg,
9447 got_worktree_checkout_cb progress_cb, void *progress_arg)
9449 const struct got_error *err = NULL;
9450 char *path_unstaged_content = NULL;
9451 char *path_new_staged_content = NULL;
9452 char *parent = NULL, *base_path = NULL;
9453 char *blob_base_path = NULL;
9454 struct got_object_id *new_staged_blob_id = NULL;
9455 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9456 struct stat sb;
9458 err = create_unstaged_content(&path_unstaged_content,
9459 &path_new_staged_content, blob_id, staged_blob_id,
9460 ie->path, repo, patch_cb, patch_arg);
9461 if (err)
9462 return err;
9464 if (path_unstaged_content == NULL)
9465 return NULL;
9467 if (path_new_staged_content) {
9468 err = got_object_blob_create(&new_staged_blob_id,
9469 path_new_staged_content, repo);
9470 if (err)
9471 goto done;
9474 f = fopen(path_unstaged_content, "re");
9475 if (f == NULL) {
9476 err = got_error_from_errno2("fopen",
9477 path_unstaged_content);
9478 goto done;
9480 if (fstat(fileno(f), &sb) == -1) {
9481 err = got_error_from_errno2("fstat", path_unstaged_content);
9482 goto done;
9484 if (got_fileindex_entry_staged_filetype_get(ie) ==
9485 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9486 char link_target[PATH_MAX];
9487 size_t r;
9488 r = fread(link_target, 1, sizeof(link_target), f);
9489 if (r == 0 && ferror(f)) {
9490 err = got_error_from_errno("fread");
9491 goto done;
9493 if (r >= sizeof(link_target)) { /* should not happen */
9494 err = got_error(GOT_ERR_NO_SPACE);
9495 goto done;
9497 link_target[r] = '\0';
9498 err = merge_symlink(worktree, blob_base,
9499 ondisk_path, ie->path, label_orig, link_target,
9500 worktree->base_commit_id, repo, progress_cb,
9501 progress_arg);
9502 } else {
9503 int local_changes_subsumed;
9505 err = got_path_dirname(&parent, ondisk_path);
9506 if (err)
9507 return err;
9509 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9510 parent) == -1) {
9511 err = got_error_from_errno("asprintf");
9512 base_path = NULL;
9513 goto done;
9516 err = got_opentemp_named(&blob_base_path, &f_base,
9517 base_path, "");
9518 if (err)
9519 goto done;
9520 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9521 blob_base);
9522 if (err)
9523 goto done;
9526 * In order the run a 3-way merge with a symlink we copy the symlink's
9527 * target path into a temporary file and use that file with diff3.
9529 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9530 err = dump_symlink_target_path_to_file(&f_deriv2,
9531 ondisk_path);
9532 if (err)
9533 goto done;
9534 } else {
9535 int fd;
9536 fd = open(ondisk_path,
9537 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9538 if (fd == -1) {
9539 err = got_error_from_errno2("open", ondisk_path);
9540 goto done;
9542 f_deriv2 = fdopen(fd, "r");
9543 if (f_deriv2 == NULL) {
9544 err = got_error_from_errno2("fdopen", ondisk_path);
9545 close(fd);
9546 goto done;
9550 err = merge_file(&local_changes_subsumed, worktree,
9551 f_base, f, f_deriv2, ondisk_path, ie->path,
9552 got_fileindex_perms_to_st(ie),
9553 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9554 repo, progress_cb, progress_arg);
9556 if (err)
9557 goto done;
9559 if (new_staged_blob_id) {
9560 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9561 SHA1_DIGEST_LENGTH);
9562 } else {
9563 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9564 got_fileindex_entry_staged_filetype_set(ie, 0);
9566 done:
9567 free(new_staged_blob_id);
9568 if (path_unstaged_content &&
9569 unlink(path_unstaged_content) == -1 && err == NULL)
9570 err = got_error_from_errno2("unlink", path_unstaged_content);
9571 if (path_new_staged_content &&
9572 unlink(path_new_staged_content) == -1 && err == NULL)
9573 err = got_error_from_errno2("unlink", path_new_staged_content);
9574 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9575 err = got_error_from_errno2("unlink", blob_base_path);
9576 if (f_base && fclose(f_base) == EOF && err == NULL)
9577 err = got_error_from_errno2("fclose", path_unstaged_content);
9578 if (f && fclose(f) == EOF && err == NULL)
9579 err = got_error_from_errno2("fclose", path_unstaged_content);
9580 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9581 err = got_error_from_errno2("fclose", ondisk_path);
9582 free(path_unstaged_content);
9583 free(path_new_staged_content);
9584 free(blob_base_path);
9585 free(parent);
9586 free(base_path);
9587 return err;
9590 static const struct got_error *
9591 unstage_path(void *arg, unsigned char status,
9592 unsigned char staged_status, const char *relpath,
9593 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9594 struct got_object_id *commit_id, int dirfd, const char *de_name)
9596 const struct got_error *err = NULL;
9597 struct unstage_path_arg *a = arg;
9598 struct got_fileindex_entry *ie;
9599 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9600 char *ondisk_path = NULL;
9601 char *id_str = NULL, *label_orig = NULL;
9602 int local_changes_subsumed;
9603 struct stat sb;
9604 int fd1 = -1, fd2 = -1;
9606 if (staged_status != GOT_STATUS_ADD &&
9607 staged_status != GOT_STATUS_MODIFY &&
9608 staged_status != GOT_STATUS_DELETE)
9609 return NULL;
9611 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9612 if (ie == NULL)
9613 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9615 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9616 == -1)
9617 return got_error_from_errno("asprintf");
9619 err = got_object_id_str(&id_str,
9620 commit_id ? commit_id : a->worktree->base_commit_id);
9621 if (err)
9622 goto done;
9623 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9624 id_str) == -1) {
9625 err = got_error_from_errno("asprintf");
9626 goto done;
9629 fd1 = got_opentempfd();
9630 if (fd1 == -1) {
9631 err = got_error_from_errno("got_opentempfd");
9632 goto done;
9634 fd2 = got_opentempfd();
9635 if (fd2 == -1) {
9636 err = got_error_from_errno("got_opentempfd");
9637 goto done;
9640 switch (staged_status) {
9641 case GOT_STATUS_MODIFY:
9642 err = got_object_open_as_blob(&blob_base, a->repo,
9643 blob_id, 8192, fd1);
9644 if (err)
9645 break;
9646 /* fall through */
9647 case GOT_STATUS_ADD:
9648 if (a->patch_cb) {
9649 if (staged_status == GOT_STATUS_ADD) {
9650 int choice = GOT_PATCH_CHOICE_NONE;
9651 err = (*a->patch_cb)(&choice, a->patch_arg,
9652 staged_status, ie->path, NULL, 1, 1);
9653 if (err)
9654 break;
9655 if (choice != GOT_PATCH_CHOICE_YES)
9656 break;
9657 } else {
9658 err = unstage_hunks(staged_blob_id,
9659 blob_base, blob_id, ie, ondisk_path,
9660 label_orig, a->worktree, a->repo,
9661 a->patch_cb, a->patch_arg,
9662 a->progress_cb, a->progress_arg);
9663 break; /* Done with this file. */
9666 err = got_object_open_as_blob(&blob_staged, a->repo,
9667 staged_blob_id, 8192, fd2);
9668 if (err)
9669 break;
9670 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9671 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9672 case GOT_FILEIDX_MODE_REGULAR_FILE:
9673 err = merge_blob(&local_changes_subsumed, a->worktree,
9674 blob_base, ondisk_path, relpath,
9675 got_fileindex_perms_to_st(ie), label_orig,
9676 blob_staged, commit_id ? commit_id :
9677 a->worktree->base_commit_id, a->repo,
9678 a->progress_cb, a->progress_arg);
9679 break;
9680 case GOT_FILEIDX_MODE_SYMLINK:
9681 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9682 char *staged_target;
9683 err = got_object_blob_read_to_str(
9684 &staged_target, blob_staged);
9685 if (err)
9686 goto done;
9687 err = merge_symlink(a->worktree, blob_base,
9688 ondisk_path, relpath, label_orig,
9689 staged_target, commit_id ? commit_id :
9690 a->worktree->base_commit_id,
9691 a->repo, a->progress_cb, a->progress_arg);
9692 free(staged_target);
9693 } else {
9694 err = merge_blob(&local_changes_subsumed,
9695 a->worktree, blob_base, ondisk_path,
9696 relpath, got_fileindex_perms_to_st(ie),
9697 label_orig, blob_staged,
9698 commit_id ? commit_id :
9699 a->worktree->base_commit_id, a->repo,
9700 a->progress_cb, a->progress_arg);
9702 break;
9703 default:
9704 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9705 break;
9707 if (err == NULL) {
9708 got_fileindex_entry_stage_set(ie,
9709 GOT_FILEIDX_STAGE_NONE);
9710 got_fileindex_entry_staged_filetype_set(ie, 0);
9712 break;
9713 case GOT_STATUS_DELETE:
9714 if (a->patch_cb) {
9715 int choice = GOT_PATCH_CHOICE_NONE;
9716 err = (*a->patch_cb)(&choice, a->patch_arg,
9717 staged_status, ie->path, NULL, 1, 1);
9718 if (err)
9719 break;
9720 if (choice == GOT_PATCH_CHOICE_NO)
9721 break;
9722 if (choice != GOT_PATCH_CHOICE_YES) {
9723 err = got_error(GOT_ERR_PATCH_CHOICE);
9724 break;
9727 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9728 got_fileindex_entry_staged_filetype_set(ie, 0);
9729 err = get_file_status(&status, &sb, ie, ondisk_path,
9730 dirfd, de_name, a->repo);
9731 if (err)
9732 break;
9733 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9734 break;
9736 done:
9737 free(ondisk_path);
9738 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9739 err = got_error_from_errno("close");
9740 if (blob_base)
9741 got_object_blob_close(blob_base);
9742 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9743 err = got_error_from_errno("close");
9744 if (blob_staged)
9745 got_object_blob_close(blob_staged);
9746 free(id_str);
9747 free(label_orig);
9748 return err;
9751 const struct got_error *
9752 got_worktree_unstage(struct got_worktree *worktree,
9753 struct got_pathlist_head *paths,
9754 got_worktree_checkout_cb progress_cb, void *progress_arg,
9755 got_worktree_patch_cb patch_cb, void *patch_arg,
9756 struct got_repository *repo)
9758 const struct got_error *err = NULL, *sync_err, *unlockerr;
9759 struct got_pathlist_entry *pe;
9760 struct got_fileindex *fileindex = NULL;
9761 char *fileindex_path = NULL;
9762 struct unstage_path_arg upa;
9764 err = lock_worktree(worktree, LOCK_EX);
9765 if (err)
9766 return err;
9768 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9769 if (err)
9770 goto done;
9772 upa.worktree = worktree;
9773 upa.fileindex = fileindex;
9774 upa.repo = repo;
9775 upa.progress_cb = progress_cb;
9776 upa.progress_arg = progress_arg;
9777 upa.patch_cb = patch_cb;
9778 upa.patch_arg = patch_arg;
9779 TAILQ_FOREACH(pe, paths, entry) {
9780 err = worktree_status(worktree, pe->path, fileindex, repo,
9781 unstage_path, &upa, NULL, NULL, 1, 0);
9782 if (err)
9783 goto done;
9786 sync_err = sync_fileindex(fileindex, fileindex_path);
9787 if (sync_err && err == NULL)
9788 err = sync_err;
9789 done:
9790 free(fileindex_path);
9791 if (fileindex)
9792 got_fileindex_free(fileindex);
9793 unlockerr = lock_worktree(worktree, LOCK_SH);
9794 if (unlockerr && err == NULL)
9795 err = unlockerr;
9796 return err;
9799 struct report_file_info_arg {
9800 struct got_worktree *worktree;
9801 got_worktree_path_info_cb info_cb;
9802 void *info_arg;
9803 struct got_pathlist_head *paths;
9804 got_cancel_cb cancel_cb;
9805 void *cancel_arg;
9808 static const struct got_error *
9809 report_file_info(void *arg, struct got_fileindex_entry *ie)
9811 const struct got_error *err;
9812 struct report_file_info_arg *a = arg;
9813 struct got_pathlist_entry *pe;
9814 struct got_object_id blob_id, staged_blob_id, commit_id;
9815 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9816 struct got_object_id *commit_idp = NULL;
9817 int stage;
9819 if (a->cancel_cb) {
9820 err = a->cancel_cb(a->cancel_arg);
9821 if (err)
9822 return err;
9825 TAILQ_FOREACH(pe, a->paths, entry) {
9826 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9827 got_path_is_child(ie->path, pe->path, pe->path_len))
9828 break;
9830 if (pe == NULL) /* not found */
9831 return NULL;
9833 if (got_fileindex_entry_has_blob(ie))
9834 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9835 stage = got_fileindex_entry_stage_get(ie);
9836 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9837 stage == GOT_FILEIDX_STAGE_ADD) {
9838 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9839 &staged_blob_id, ie);
9842 if (got_fileindex_entry_has_commit(ie))
9843 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9845 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9846 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9849 const struct got_error *
9850 got_worktree_path_info(struct got_worktree *worktree,
9851 struct got_pathlist_head *paths,
9852 got_worktree_path_info_cb info_cb, void *info_arg,
9853 got_cancel_cb cancel_cb, void *cancel_arg)
9856 const struct got_error *err = NULL, *unlockerr;
9857 struct got_fileindex *fileindex = NULL;
9858 char *fileindex_path = NULL;
9859 struct report_file_info_arg arg;
9861 err = lock_worktree(worktree, LOCK_SH);
9862 if (err)
9863 return err;
9865 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9866 if (err)
9867 goto done;
9869 arg.worktree = worktree;
9870 arg.info_cb = info_cb;
9871 arg.info_arg = info_arg;
9872 arg.paths = paths;
9873 arg.cancel_cb = cancel_cb;
9874 arg.cancel_arg = cancel_arg;
9875 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9876 &arg);
9877 done:
9878 free(fileindex_path);
9879 if (fileindex)
9880 got_fileindex_free(fileindex);
9881 unlockerr = lock_worktree(worktree, LOCK_UN);
9882 if (unlockerr && err == NULL)
9883 err = unlockerr;
9884 return err;
9887 static const struct got_error *
9888 patch_check_path(const char *p, char **path, unsigned char *status,
9889 unsigned char *staged_status, struct got_fileindex *fileindex,
9890 struct got_worktree *worktree, struct got_repository *repo)
9892 const struct got_error *err;
9893 struct got_fileindex_entry *ie;
9894 struct stat sb;
9895 char *ondisk_path = NULL;
9897 err = got_worktree_resolve_path(path, worktree, p);
9898 if (err)
9899 return err;
9901 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9902 *path[0] ? "/" : "", *path) == -1)
9903 return got_error_from_errno("asprintf");
9905 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9906 if (ie) {
9907 *staged_status = get_staged_status(ie);
9908 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9909 repo);
9910 if (err)
9911 goto done;
9912 } else {
9913 *staged_status = GOT_STATUS_NO_CHANGE;
9914 *status = GOT_STATUS_UNVERSIONED;
9915 if (lstat(ondisk_path, &sb) == -1) {
9916 if (errno != ENOENT) {
9917 err = got_error_from_errno2("lstat",
9918 ondisk_path);
9919 goto done;
9921 *status = GOT_STATUS_NONEXISTENT;
9925 done:
9926 free(ondisk_path);
9927 return err;
9930 static const struct got_error *
9931 patch_can_rm(const char *path, unsigned char status,
9932 unsigned char staged_status)
9934 if (status == GOT_STATUS_NONEXISTENT)
9935 return got_error_set_errno(ENOENT, path);
9936 if (status != GOT_STATUS_NO_CHANGE &&
9937 status != GOT_STATUS_ADD &&
9938 status != GOT_STATUS_MODIFY &&
9939 status != GOT_STATUS_MODE_CHANGE)
9940 return got_error_path(path, GOT_ERR_FILE_STATUS);
9941 if (staged_status == GOT_STATUS_DELETE)
9942 return got_error_path(path, GOT_ERR_FILE_STATUS);
9943 return NULL;
9946 static const struct got_error *
9947 patch_can_add(const char *path, unsigned char status)
9949 if (status != GOT_STATUS_NONEXISTENT)
9950 return got_error_path(path, GOT_ERR_FILE_STATUS);
9951 return NULL;
9954 static const struct got_error *
9955 patch_can_edit(const char *path, unsigned char status,
9956 unsigned char staged_status)
9958 if (status == GOT_STATUS_NONEXISTENT)
9959 return got_error_set_errno(ENOENT, path);
9960 if (status != GOT_STATUS_NO_CHANGE &&
9961 status != GOT_STATUS_ADD &&
9962 status != GOT_STATUS_MODIFY)
9963 return got_error_path(path, GOT_ERR_FILE_STATUS);
9964 if (staged_status == GOT_STATUS_DELETE)
9965 return got_error_path(path, GOT_ERR_FILE_STATUS);
9966 return NULL;
9969 const struct got_error *
9970 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9971 char **fileindex_path, struct got_worktree *worktree)
9973 return open_fileindex(fileindex, fileindex_path, worktree);
9976 const struct got_error *
9977 got_worktree_patch_check_path(const char *old, const char *new,
9978 char **oldpath, char **newpath, struct got_worktree *worktree,
9979 struct got_repository *repo, struct got_fileindex *fileindex)
9981 const struct got_error *err = NULL;
9982 int file_renamed = 0;
9983 unsigned char status_old, staged_status_old;
9984 unsigned char status_new, staged_status_new;
9986 *oldpath = NULL;
9987 *newpath = NULL;
9989 err = patch_check_path(old != NULL ? old : new, oldpath,
9990 &status_old, &staged_status_old, fileindex, worktree, repo);
9991 if (err)
9992 goto done;
9994 err = patch_check_path(new != NULL ? new : old, newpath,
9995 &status_new, &staged_status_new, fileindex, worktree, repo);
9996 if (err)
9997 goto done;
9999 if (old != NULL && new != NULL && strcmp(old, new) != 0)
10000 file_renamed = 1;
10002 if (old != NULL && new == NULL)
10003 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10004 else if (file_renamed) {
10005 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10006 if (err == NULL)
10007 err = patch_can_add(*newpath, status_new);
10008 } else if (old == NULL)
10009 err = patch_can_add(*newpath, status_new);
10010 else
10011 err = patch_can_edit(*newpath, status_new, staged_status_new);
10013 done:
10014 if (err) {
10015 free(*oldpath);
10016 *oldpath = NULL;
10017 free(*newpath);
10018 *newpath = NULL;
10020 return err;
10023 const struct got_error *
10024 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10025 struct got_worktree *worktree, struct got_fileindex *fileindex,
10026 got_worktree_checkout_cb progress_cb, void *progress_arg)
10028 struct schedule_addition_args saa;
10030 memset(&saa, 0, sizeof(saa));
10031 saa.worktree = worktree;
10032 saa.fileindex = fileindex;
10033 saa.progress_cb = progress_cb;
10034 saa.progress_arg = progress_arg;
10035 saa.repo = repo;
10037 return worktree_status(worktree, path, fileindex, repo,
10038 schedule_addition, &saa, NULL, NULL, 1, 0);
10041 const struct got_error *
10042 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10043 struct got_worktree *worktree, struct got_fileindex *fileindex,
10044 got_worktree_delete_cb progress_cb, void *progress_arg)
10046 const struct got_error *err;
10047 struct schedule_deletion_args sda;
10048 char *ondisk_status_path;
10050 memset(&sda, 0, sizeof(sda));
10051 sda.worktree = worktree;
10052 sda.fileindex = fileindex;
10053 sda.progress_cb = progress_cb;
10054 sda.progress_arg = progress_arg;
10055 sda.repo = repo;
10056 sda.delete_local_mods = 0;
10057 sda.keep_on_disk = 0;
10058 sda.ignore_missing_paths = 0;
10059 sda.status_codes = NULL;
10060 if (asprintf(&ondisk_status_path, "%s/%s",
10061 got_worktree_get_root_path(worktree), path) == -1)
10062 return got_error_from_errno("asprintf");
10063 sda.status_path = ondisk_status_path;
10064 sda.status_path_len = strlen(ondisk_status_path);
10066 err = worktree_status(worktree, path, fileindex, repo,
10067 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10068 free(ondisk_status_path);
10069 return err;
10072 const struct got_error *
10073 got_worktree_patch_complete(struct got_fileindex *fileindex,
10074 const char *fileindex_path)
10076 const struct got_error *err = NULL;
10078 err = sync_fileindex(fileindex, fileindex_path);
10079 got_fileindex_free(fileindex);
10081 return err;