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;
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 int update_timestamps;
2045 struct got_blob_object *blob2 = NULL;
2046 char *label_orig = NULL;
2047 if (got_fileindex_entry_has_blob(ie)) {
2048 fd2 = got_opentempfd();
2049 if (fd2 == -1) {
2050 err = got_error_from_errno("got_opentempfd");
2051 goto done;
2053 struct got_object_id id2;
2054 got_fileindex_entry_get_blob_id(&id2, ie);
2055 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2056 fd2);
2057 if (err)
2058 goto done;
2060 if (got_fileindex_entry_has_commit(ie)) {
2061 char id_str[SHA1_DIGEST_STRING_LENGTH];
2062 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2063 sizeof(id_str)) == NULL) {
2064 err = got_error_path(id_str,
2065 GOT_ERR_BAD_OBJ_ID_STR);
2066 goto done;
2068 if (asprintf(&label_orig, "%s: commit %s",
2069 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2070 err = got_error_from_errno("asprintf");
2071 goto done;
2074 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2075 char *link_target;
2076 err = got_object_blob_read_to_str(&link_target, blob);
2077 if (err)
2078 goto done;
2079 err = merge_symlink(worktree, blob2, ondisk_path, path,
2080 label_orig, link_target, worktree->base_commit_id,
2081 repo, progress_cb, progress_arg);
2082 free(link_target);
2083 } else {
2084 err = merge_blob(&update_timestamps, worktree, blob2,
2085 ondisk_path, path, sb.st_mode, label_orig, blob,
2086 worktree->base_commit_id, repo,
2087 progress_cb, progress_arg);
2089 free(label_orig);
2090 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2091 err = got_error_from_errno("close");
2092 goto done;
2094 if (blob2)
2095 got_object_blob_close(blob2);
2096 if (err)
2097 goto done;
2099 * Do not update timestamps of files with local changes.
2100 * Otherwise, a future status walk would treat them as
2101 * unmodified files again.
2103 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2104 blob->id.sha1, worktree->base_commit_id->sha1,
2105 update_timestamps);
2106 } else if (status == GOT_STATUS_MODE_CHANGE) {
2107 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2108 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2109 } else if (status == GOT_STATUS_DELETE) {
2110 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2111 if (err)
2112 goto done;
2113 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2114 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2115 if (err)
2116 goto done;
2117 } else {
2118 int is_bad_symlink = 0;
2119 if (S_ISLNK(te->mode)) {
2120 err = install_symlink(&is_bad_symlink, worktree,
2121 ondisk_path, path, blob,
2122 status == GOT_STATUS_MISSING, 0,
2123 status == GOT_STATUS_UNVERSIONED, 0,
2124 repo, progress_cb, progress_arg);
2125 } else {
2126 err = install_blob(worktree, ondisk_path, path,
2127 te->mode, sb.st_mode, blob,
2128 status == GOT_STATUS_MISSING, 0, 0,
2129 status == GOT_STATUS_UNVERSIONED,
2130 &update_timestamps,
2131 repo, progress_cb, progress_arg);
2133 if (err)
2134 goto done;
2136 if (ie) {
2137 err = got_fileindex_entry_update(ie,
2138 worktree->root_fd, path, blob->id.sha1,
2139 worktree->base_commit_id->sha1, 1);
2140 } else {
2141 err = create_fileindex_entry(&ie, fileindex,
2142 worktree->base_commit_id, worktree->root_fd, path,
2143 &blob->id, update_timestamps);
2145 if (err)
2146 goto done;
2148 if (is_bad_symlink) {
2149 got_fileindex_entry_filetype_set(ie,
2150 GOT_FILEIDX_MODE_BAD_SYMLINK);
2154 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2155 err = got_error_from_errno("close");
2156 goto done;
2158 got_object_blob_close(blob);
2159 done:
2160 free(ondisk_path);
2161 return err;
2164 static const struct got_error *
2165 remove_ondisk_file(const char *root_path, const char *path)
2167 const struct got_error *err = NULL;
2168 char *ondisk_path = NULL, *parent = NULL;
2170 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2171 return got_error_from_errno("asprintf");
2173 if (unlink(ondisk_path) == -1) {
2174 if (errno != ENOENT)
2175 err = got_error_from_errno2("unlink", ondisk_path);
2176 } else {
2177 size_t root_len = strlen(root_path);
2178 err = got_path_dirname(&parent, ondisk_path);
2179 if (err)
2180 goto done;
2181 while (got_path_cmp(parent, root_path,
2182 strlen(parent), root_len) != 0) {
2183 free(ondisk_path);
2184 ondisk_path = parent;
2185 parent = NULL;
2186 if (rmdir(ondisk_path) == -1) {
2187 if (errno != ENOTEMPTY)
2188 err = got_error_from_errno2("rmdir",
2189 ondisk_path);
2190 break;
2192 err = got_path_dirname(&parent, ondisk_path);
2193 if (err)
2194 break;
2197 done:
2198 free(ondisk_path);
2199 free(parent);
2200 return err;
2203 static const struct got_error *
2204 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2205 struct got_fileindex_entry *ie, struct got_repository *repo,
2206 got_worktree_checkout_cb progress_cb, void *progress_arg)
2208 const struct got_error *err = NULL;
2209 unsigned char status;
2210 struct stat sb;
2211 char *ondisk_path;
2213 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2214 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2216 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2217 == -1)
2218 return got_error_from_errno("asprintf");
2220 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2221 if (err)
2222 goto done;
2224 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2225 char ondisk_target[PATH_MAX];
2226 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2227 sizeof(ondisk_target));
2228 if (ondisk_len == -1) {
2229 err = got_error_from_errno2("readlink", ondisk_path);
2230 goto done;
2232 ondisk_target[ondisk_len] = '\0';
2233 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2234 NULL, NULL, /* XXX pass common ancestor info? */
2235 ondisk_target, ondisk_path);
2236 if (err)
2237 goto done;
2238 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2239 ie->path);
2240 goto done;
2243 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2244 status == GOT_STATUS_ADD) {
2245 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2246 if (err)
2247 goto done;
2249 * Preserve the working file and change the deleted blob's
2250 * entry into a schedule-add entry.
2252 err = got_fileindex_entry_update(ie, worktree->root_fd,
2253 ie->path, NULL, NULL, 0);
2254 } else {
2255 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2256 if (err)
2257 goto done;
2258 if (status == GOT_STATUS_NO_CHANGE) {
2259 err = remove_ondisk_file(worktree->root_path, ie->path);
2260 if (err)
2261 goto done;
2263 got_fileindex_entry_remove(fileindex, ie);
2265 done:
2266 free(ondisk_path);
2267 return err;
2270 struct diff_cb_arg {
2271 struct got_fileindex *fileindex;
2272 struct got_worktree *worktree;
2273 struct got_repository *repo;
2274 got_worktree_checkout_cb progress_cb;
2275 void *progress_arg;
2276 got_cancel_cb cancel_cb;
2277 void *cancel_arg;
2280 static const struct got_error *
2281 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2282 struct got_tree_entry *te, const char *parent_path)
2284 const struct got_error *err = NULL;
2285 struct diff_cb_arg *a = arg;
2287 if (a->cancel_cb) {
2288 err = a->cancel_cb(a->cancel_arg);
2289 if (err)
2290 return err;
2293 return update_blob(a->worktree, a->fileindex, ie, te,
2294 ie->path, a->repo, a->progress_cb, a->progress_arg);
2297 static const struct got_error *
2298 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2300 const struct got_error *err = NULL;
2301 struct diff_cb_arg *a = arg;
2303 if (a->cancel_cb) {
2304 err = a->cancel_cb(a->cancel_arg);
2305 if (err)
2306 return err;
2309 return delete_blob(a->worktree, a->fileindex, ie,
2310 a->repo, a->progress_cb, a->progress_arg);
2313 static const struct got_error *
2314 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2316 const struct got_error *err = NULL;
2317 struct diff_cb_arg *a = arg;
2318 char *path;
2320 if (a->cancel_cb) {
2321 err = a->cancel_cb(a->cancel_arg);
2322 if (err)
2323 return err;
2326 if (got_object_tree_entry_is_submodule(te))
2327 return NULL;
2329 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2330 return NULL;
2332 if (asprintf(&path, "%s%s%s", parent_path,
2333 parent_path[0] ? "/" : "", te->name)
2334 == -1)
2335 return got_error_from_errno("asprintf");
2337 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2338 a->repo, a->progress_cb, a->progress_arg);
2340 free(path);
2341 return err;
2344 const struct got_error *
2345 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2347 uint32_t uuid_status;
2349 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2350 if (uuid_status != uuid_s_ok) {
2351 *uuidstr = NULL;
2352 return got_error_uuid(uuid_status, "uuid_to_string");
2355 return NULL;
2358 static const struct got_error *
2359 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2361 const struct got_error *err = NULL;
2362 char *uuidstr = NULL;
2364 *refname = NULL;
2366 err = got_worktree_get_uuid(&uuidstr, worktree);
2367 if (err)
2368 return err;
2370 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2371 err = got_error_from_errno("asprintf");
2372 *refname = NULL;
2374 free(uuidstr);
2375 return err;
2378 const struct got_error *
2379 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2380 const char *prefix)
2382 return get_ref_name(refname, worktree, prefix);
2385 static const struct got_error *
2386 get_base_ref_name(char **refname, struct got_worktree *worktree)
2388 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2391 static const struct got_error *
2392 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2394 return get_ref_name(refname, worktree,
2395 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2398 static const struct got_error *
2399 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2401 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2404 static const struct got_error *
2405 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2407 return get_ref_name(refname, worktree,
2408 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2411 static const struct got_error *
2412 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2414 return get_ref_name(refname, worktree,
2415 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2418 static const struct got_error *
2419 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2421 return get_ref_name(refname, worktree,
2422 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2425 static const struct got_error *
2426 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2428 return get_ref_name(refname, worktree,
2429 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2432 static const struct got_error *
2433 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2435 return get_ref_name(refname, worktree,
2436 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2439 static const struct got_error *
2440 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2442 return get_ref_name(refname, worktree,
2443 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2446 const struct got_error *
2447 got_worktree_get_histedit_script_path(char **path,
2448 struct got_worktree *worktree)
2450 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2451 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2452 *path = NULL;
2453 return got_error_from_errno("asprintf");
2455 return NULL;
2458 static const struct got_error *
2459 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2461 return get_ref_name(refname, worktree,
2462 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2465 static const struct got_error *
2466 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2468 return get_ref_name(refname, worktree,
2469 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2473 * Prevent Git's garbage collector from deleting our base commit by
2474 * setting a reference to our base commit's ID.
2476 static const struct got_error *
2477 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2479 const struct got_error *err = NULL;
2480 struct got_reference *ref = NULL;
2481 char *refname;
2483 err = get_base_ref_name(&refname, worktree);
2484 if (err)
2485 return err;
2487 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2488 if (err)
2489 goto done;
2491 err = got_ref_write(ref, repo);
2492 done:
2493 free(refname);
2494 if (ref)
2495 got_ref_close(ref);
2496 return err;
2499 static const struct got_error *
2500 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2502 const struct got_error *err = NULL;
2504 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2505 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2506 err = got_error_from_errno("asprintf");
2507 *fileindex_path = NULL;
2509 return err;
2513 static const struct got_error *
2514 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2515 struct got_worktree *worktree)
2517 const struct got_error *err = NULL;
2518 FILE *index = NULL;
2520 *fileindex_path = NULL;
2521 *fileindex = got_fileindex_alloc();
2522 if (*fileindex == NULL)
2523 return got_error_from_errno("got_fileindex_alloc");
2525 err = get_fileindex_path(fileindex_path, worktree);
2526 if (err)
2527 goto done;
2529 index = fopen(*fileindex_path, "rbe");
2530 if (index == NULL) {
2531 if (errno != ENOENT)
2532 err = got_error_from_errno2("fopen", *fileindex_path);
2533 } else {
2534 err = got_fileindex_read(*fileindex, index);
2535 if (fclose(index) == EOF && err == NULL)
2536 err = got_error_from_errno("fclose");
2538 done:
2539 if (err) {
2540 free(*fileindex_path);
2541 *fileindex_path = NULL;
2542 got_fileindex_free(*fileindex);
2543 *fileindex = NULL;
2545 return err;
2548 struct bump_base_commit_id_arg {
2549 struct got_object_id *base_commit_id;
2550 const char *path;
2551 size_t path_len;
2552 const char *entry_name;
2553 got_worktree_checkout_cb progress_cb;
2554 void *progress_arg;
2557 static const struct got_error *
2558 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2560 const struct got_error *err;
2561 struct bump_base_commit_id_arg *a = arg;
2563 if (a->entry_name) {
2564 if (strcmp(ie->path, a->path) != 0)
2565 return NULL;
2566 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2567 return NULL;
2569 if (got_fileindex_entry_was_skipped(ie))
2570 return NULL;
2572 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2573 SHA1_DIGEST_LENGTH) == 0)
2574 return NULL;
2576 if (a->progress_cb) {
2577 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2578 ie->path);
2579 if (err)
2580 return err;
2582 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2583 return NULL;
2586 /* Bump base commit ID of all files within an updated part of the work tree. */
2587 static const struct got_error *
2588 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2589 struct got_fileindex *fileindex,
2590 got_worktree_checkout_cb progress_cb, void *progress_arg)
2592 struct bump_base_commit_id_arg bbc_arg;
2594 bbc_arg.base_commit_id = worktree->base_commit_id;
2595 bbc_arg.entry_name = NULL;
2596 bbc_arg.path = "";
2597 bbc_arg.path_len = 0;
2598 bbc_arg.progress_cb = progress_cb;
2599 bbc_arg.progress_arg = progress_arg;
2601 return got_fileindex_for_each_entry_safe(fileindex,
2602 bump_base_commit_id, &bbc_arg);
2605 static const struct got_error *
2606 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2608 const struct got_error *err = NULL;
2609 char *new_fileindex_path = NULL;
2610 FILE *new_index = NULL;
2611 struct timespec timeout;
2613 err = got_opentemp_named(&new_fileindex_path, &new_index,
2614 fileindex_path, "");
2615 if (err)
2616 goto done;
2618 err = got_fileindex_write(fileindex, new_index);
2619 if (err)
2620 goto done;
2622 if (rename(new_fileindex_path, fileindex_path) != 0) {
2623 err = got_error_from_errno3("rename", new_fileindex_path,
2624 fileindex_path);
2625 unlink(new_fileindex_path);
2629 * Sleep for a short amount of time to ensure that files modified after
2630 * this program exits have a different time stamp from the one which
2631 * was recorded in the file index.
2633 timeout.tv_sec = 0;
2634 timeout.tv_nsec = 1;
2635 nanosleep(&timeout, NULL);
2636 done:
2637 if (new_index)
2638 fclose(new_index);
2639 free(new_fileindex_path);
2640 return err;
2643 static const struct got_error *
2644 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2645 struct got_object_id **tree_id, const char *wt_relpath,
2646 struct got_commit_object *base_commit, struct got_worktree *worktree,
2647 struct got_repository *repo)
2649 const struct got_error *err = NULL;
2650 struct got_object_id *id = NULL;
2651 char *in_repo_path = NULL;
2652 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2654 *entry_type = GOT_OBJ_TYPE_ANY;
2655 *tree_relpath = NULL;
2656 *tree_id = NULL;
2658 if (wt_relpath[0] == '\0') {
2659 /* Check out all files within the work tree. */
2660 *entry_type = GOT_OBJ_TYPE_TREE;
2661 *tree_relpath = strdup("");
2662 if (*tree_relpath == NULL) {
2663 err = got_error_from_errno("strdup");
2664 goto done;
2666 err = got_object_id_by_path(tree_id, repo, base_commit,
2667 worktree->path_prefix);
2668 if (err)
2669 goto done;
2670 return NULL;
2673 /* Check out a subset of files in the work tree. */
2675 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2676 is_root_wt ? "" : "/", wt_relpath) == -1) {
2677 err = got_error_from_errno("asprintf");
2678 goto done;
2681 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2682 if (err)
2683 goto done;
2685 free(in_repo_path);
2686 in_repo_path = NULL;
2688 err = got_object_get_type(entry_type, repo, id);
2689 if (err)
2690 goto done;
2692 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2693 /* Check out a single file. */
2694 if (strchr(wt_relpath, '/') == NULL) {
2695 /* Check out a single file in work tree's root dir. */
2696 in_repo_path = strdup(worktree->path_prefix);
2697 if (in_repo_path == NULL) {
2698 err = got_error_from_errno("strdup");
2699 goto done;
2701 *tree_relpath = strdup("");
2702 if (*tree_relpath == NULL) {
2703 err = got_error_from_errno("strdup");
2704 goto done;
2706 } else {
2707 /* Check out a single file in a subdirectory. */
2708 err = got_path_dirname(tree_relpath, wt_relpath);
2709 if (err)
2710 return err;
2711 if (asprintf(&in_repo_path, "%s%s%s",
2712 worktree->path_prefix, is_root_wt ? "" : "/",
2713 *tree_relpath) == -1) {
2714 err = got_error_from_errno("asprintf");
2715 goto done;
2718 err = got_object_id_by_path(tree_id, repo,
2719 base_commit, in_repo_path);
2720 } else {
2721 /* Check out all files within a subdirectory. */
2722 *tree_id = got_object_id_dup(id);
2723 if (*tree_id == NULL) {
2724 err = got_error_from_errno("got_object_id_dup");
2725 goto done;
2727 *tree_relpath = strdup(wt_relpath);
2728 if (*tree_relpath == NULL) {
2729 err = got_error_from_errno("strdup");
2730 goto done;
2733 done:
2734 free(id);
2735 free(in_repo_path);
2736 if (err) {
2737 *entry_type = GOT_OBJ_TYPE_ANY;
2738 free(*tree_relpath);
2739 *tree_relpath = NULL;
2740 free(*tree_id);
2741 *tree_id = NULL;
2743 return err;
2746 static const struct got_error *
2747 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2748 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2749 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2750 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2752 const struct got_error *err = NULL;
2753 struct got_commit_object *commit = NULL;
2754 struct got_tree_object *tree = NULL;
2755 struct got_fileindex_diff_tree_cb diff_cb;
2756 struct diff_cb_arg arg;
2758 err = ref_base_commit(worktree, repo);
2759 if (err) {
2760 if (!(err->code == GOT_ERR_ERRNO &&
2761 (errno == EACCES || errno == EROFS)))
2762 goto done;
2763 err = (*progress_cb)(progress_arg,
2764 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2765 if (err)
2766 return err;
2769 err = got_object_open_as_commit(&commit, repo,
2770 worktree->base_commit_id);
2771 if (err)
2772 goto done;
2774 err = got_object_open_as_tree(&tree, repo, tree_id);
2775 if (err)
2776 goto done;
2778 if (entry_name &&
2779 got_object_tree_find_entry(tree, entry_name) == NULL) {
2780 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2781 goto done;
2784 diff_cb.diff_old_new = diff_old_new;
2785 diff_cb.diff_old = diff_old;
2786 diff_cb.diff_new = diff_new;
2787 arg.fileindex = fileindex;
2788 arg.worktree = worktree;
2789 arg.repo = repo;
2790 arg.progress_cb = progress_cb;
2791 arg.progress_arg = progress_arg;
2792 arg.cancel_cb = cancel_cb;
2793 arg.cancel_arg = cancel_arg;
2794 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2795 entry_name, repo, &diff_cb, &arg);
2796 done:
2797 if (tree)
2798 got_object_tree_close(tree);
2799 if (commit)
2800 got_object_commit_close(commit);
2801 return err;
2804 const struct got_error *
2805 got_worktree_checkout_files(struct got_worktree *worktree,
2806 struct got_pathlist_head *paths, struct got_repository *repo,
2807 got_worktree_checkout_cb progress_cb, void *progress_arg,
2808 got_cancel_cb cancel_cb, void *cancel_arg)
2810 const struct got_error *err = NULL, *sync_err, *unlockerr;
2811 struct got_commit_object *commit = NULL;
2812 struct got_tree_object *tree = NULL;
2813 struct got_fileindex *fileindex = NULL;
2814 char *fileindex_path = NULL;
2815 struct got_pathlist_entry *pe;
2816 struct tree_path_data {
2817 STAILQ_ENTRY(tree_path_data) entry;
2818 struct got_object_id *tree_id;
2819 int entry_type;
2820 char *relpath;
2821 char *entry_name;
2822 } *tpd = NULL;
2823 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2825 STAILQ_INIT(&tree_paths);
2827 err = lock_worktree(worktree, LOCK_EX);
2828 if (err)
2829 return err;
2831 err = got_object_open_as_commit(&commit, repo,
2832 worktree->base_commit_id);
2833 if (err)
2834 goto done;
2836 /* Map all specified paths to in-repository trees. */
2837 TAILQ_FOREACH(pe, paths, entry) {
2838 tpd = malloc(sizeof(*tpd));
2839 if (tpd == NULL) {
2840 err = got_error_from_errno("malloc");
2841 goto done;
2844 err = find_tree_entry_for_checkout(&tpd->entry_type,
2845 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2846 worktree, repo);
2847 if (err) {
2848 free(tpd);
2849 goto done;
2852 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2853 err = got_path_basename(&tpd->entry_name, pe->path);
2854 if (err) {
2855 free(tpd->relpath);
2856 free(tpd->tree_id);
2857 free(tpd);
2858 goto done;
2860 } else
2861 tpd->entry_name = NULL;
2863 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2867 * Read the file index.
2868 * Checking out files is supposed to be an idempotent operation.
2869 * If the on-disk file index is incomplete we will try to complete it.
2871 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2872 if (err)
2873 goto done;
2875 tpd = STAILQ_FIRST(&tree_paths);
2876 TAILQ_FOREACH(pe, paths, entry) {
2877 struct bump_base_commit_id_arg bbc_arg;
2879 err = checkout_files(worktree, fileindex, tpd->relpath,
2880 tpd->tree_id, tpd->entry_name, repo,
2881 progress_cb, progress_arg, cancel_cb, cancel_arg);
2882 if (err)
2883 break;
2885 bbc_arg.base_commit_id = worktree->base_commit_id;
2886 bbc_arg.entry_name = tpd->entry_name;
2887 bbc_arg.path = pe->path;
2888 bbc_arg.path_len = pe->path_len;
2889 bbc_arg.progress_cb = progress_cb;
2890 bbc_arg.progress_arg = progress_arg;
2891 err = got_fileindex_for_each_entry_safe(fileindex,
2892 bump_base_commit_id, &bbc_arg);
2893 if (err)
2894 break;
2896 tpd = STAILQ_NEXT(tpd, entry);
2898 sync_err = sync_fileindex(fileindex, fileindex_path);
2899 if (sync_err && err == NULL)
2900 err = sync_err;
2901 done:
2902 free(fileindex_path);
2903 if (tree)
2904 got_object_tree_close(tree);
2905 if (commit)
2906 got_object_commit_close(commit);
2907 if (fileindex)
2908 got_fileindex_free(fileindex);
2909 while (!STAILQ_EMPTY(&tree_paths)) {
2910 tpd = STAILQ_FIRST(&tree_paths);
2911 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2912 free(tpd->relpath);
2913 free(tpd->tree_id);
2914 free(tpd);
2916 unlockerr = lock_worktree(worktree, LOCK_SH);
2917 if (unlockerr && err == NULL)
2918 err = unlockerr;
2919 return err;
2922 static const struct got_error *
2923 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2924 struct got_fileindex_entry *ie, const char *ondisk_path,
2925 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2926 int restoring_missing_file, int reverting_versioned_file,
2927 int path_is_unversioned, int allow_bad_symlinks,
2928 struct got_repository *repo,
2929 got_worktree_checkout_cb progress_cb, void *progress_arg)
2931 const struct got_error *err = NULL;
2932 int is_bad_symlink = 0;
2934 if (S_ISLNK(mode2)) {
2935 err = install_symlink(&is_bad_symlink,
2936 worktree, ondisk_path, path2, blob2,
2937 restoring_missing_file,
2938 reverting_versioned_file,
2939 path_is_unversioned, allow_bad_symlinks,
2940 repo, progress_cb, progress_arg);
2941 } else {
2942 err = install_blob(worktree, ondisk_path, path2,
2943 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2944 restoring_missing_file, reverting_versioned_file, 0,
2945 path_is_unversioned, NULL, repo,
2946 progress_cb, progress_arg);
2948 if (err)
2949 return err;
2950 if (ie == NULL) {
2951 /* Adding an unversioned file. */
2952 err = got_fileindex_entry_alloc(&ie, path2);
2953 if (err)
2954 return err;
2955 err = got_fileindex_entry_update(ie,
2956 worktree->root_fd, path2, NULL, NULL, 1);
2957 if (err) {
2958 got_fileindex_entry_free(ie);
2959 return err;
2961 err = got_fileindex_entry_add(fileindex, ie);
2962 if (err) {
2963 got_fileindex_entry_free(ie);
2964 return err;
2966 } else {
2967 /* Re-adding a locally deleted file. */
2968 err = got_fileindex_entry_update(ie,
2969 worktree->root_fd, path2, ie->blob_sha1,
2970 worktree->base_commit_id->sha1, 0);
2971 if (err)
2972 return err;
2975 if (is_bad_symlink) {
2976 got_fileindex_entry_filetype_set(ie,
2977 GOT_FILEIDX_MODE_BAD_SYMLINK);
2980 return NULL;
2983 struct merge_file_cb_arg {
2984 struct got_worktree *worktree;
2985 struct got_fileindex *fileindex;
2986 got_worktree_checkout_cb progress_cb;
2987 void *progress_arg;
2988 got_cancel_cb cancel_cb;
2989 void *cancel_arg;
2990 const char *label_orig;
2991 struct got_object_id *commit_id2;
2992 int allow_bad_symlinks;
2995 static const struct got_error *
2996 merge_file_cb(void *arg, struct got_blob_object *blob1,
2997 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2998 struct got_object_id *id1, struct got_object_id *id2,
2999 const char *path1, const char *path2,
3000 mode_t mode1, mode_t mode2, struct got_repository *repo)
3002 static const struct got_error *err = NULL;
3003 struct merge_file_cb_arg *a = arg;
3004 struct got_fileindex_entry *ie;
3005 char *ondisk_path = NULL;
3006 struct stat sb;
3007 unsigned char status;
3008 int local_changes_subsumed;
3009 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3010 char *id_str = NULL, *label_deriv2 = NULL;
3011 struct got_object_id *id = NULL;
3013 if (blob1 && blob2) {
3014 ie = got_fileindex_entry_get(a->fileindex, path2,
3015 strlen(path2));
3016 if (ie == NULL)
3017 return (*a->progress_cb)(a->progress_arg,
3018 GOT_STATUS_MISSING, path2);
3020 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3021 path2) == -1)
3022 return got_error_from_errno("asprintf");
3024 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3025 repo);
3026 if (err)
3027 goto done;
3029 if (status == GOT_STATUS_DELETE) {
3030 err = (*a->progress_cb)(a->progress_arg,
3031 GOT_STATUS_MERGE, path2);
3032 goto done;
3034 if (status != GOT_STATUS_NO_CHANGE &&
3035 status != GOT_STATUS_MODIFY &&
3036 status != GOT_STATUS_CONFLICT &&
3037 status != GOT_STATUS_ADD) {
3038 err = (*a->progress_cb)(a->progress_arg, status, path2);
3039 goto done;
3042 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3043 char *link_target2;
3044 err = got_object_blob_read_to_str(&link_target2, blob2);
3045 if (err)
3046 goto done;
3047 err = merge_symlink(a->worktree, blob1, ondisk_path,
3048 path2, a->label_orig, link_target2, a->commit_id2,
3049 repo, a->progress_cb, a->progress_arg);
3050 free(link_target2);
3051 } else {
3052 int fd;
3054 f_orig = got_opentemp();
3055 if (f_orig == NULL) {
3056 err = got_error_from_errno("got_opentemp");
3057 goto done;
3059 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3060 f_orig, blob1);
3061 if (err)
3062 goto done;
3064 f_deriv2 = got_opentemp();
3065 if (f_deriv2 == NULL)
3066 goto done;
3067 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3068 f_deriv2, blob2);
3069 if (err)
3070 goto done;
3072 fd = open(ondisk_path,
3073 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3074 if (fd == -1) {
3075 err = got_error_from_errno2("open",
3076 ondisk_path);
3077 goto done;
3079 f_deriv = fdopen(fd, "r");
3080 if (f_deriv == NULL) {
3081 err = got_error_from_errno2("fdopen",
3082 ondisk_path);
3083 close(fd);
3084 goto done;
3086 err = got_object_id_str(&id_str, a->commit_id2);
3087 if (err)
3088 goto done;
3089 if (asprintf(&label_deriv2, "%s: commit %s",
3090 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3091 err = got_error_from_errno("asprintf");
3092 goto done;
3094 err = merge_file(&local_changes_subsumed, a->worktree,
3095 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3096 mode2, a->label_orig, NULL, label_deriv2,
3097 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3098 a->progress_cb, a->progress_arg);
3100 } else if (blob1) {
3101 ie = got_fileindex_entry_get(a->fileindex, path1,
3102 strlen(path1));
3103 if (ie == NULL)
3104 return (*a->progress_cb)(a->progress_arg,
3105 GOT_STATUS_MISSING, path1);
3107 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3108 path1) == -1)
3109 return got_error_from_errno("asprintf");
3111 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3112 repo);
3113 if (err)
3114 goto done;
3116 switch (status) {
3117 case GOT_STATUS_NO_CHANGE:
3118 err = (*a->progress_cb)(a->progress_arg,
3119 GOT_STATUS_DELETE, path1);
3120 if (err)
3121 goto done;
3122 err = remove_ondisk_file(a->worktree->root_path, path1);
3123 if (err)
3124 goto done;
3125 if (ie)
3126 got_fileindex_entry_mark_deleted_from_disk(ie);
3127 break;
3128 case GOT_STATUS_DELETE:
3129 case GOT_STATUS_MISSING:
3130 err = (*a->progress_cb)(a->progress_arg,
3131 GOT_STATUS_DELETE, path1);
3132 if (err)
3133 goto done;
3134 if (ie)
3135 got_fileindex_entry_mark_deleted_from_disk(ie);
3136 break;
3137 case GOT_STATUS_MODIFY: {
3138 FILE *blob1_f;
3139 off_t blob1_size;
3140 int obj_type;
3142 * Delete the file only if its content already
3143 * exists in the repository.
3145 err = got_object_blob_file_create(&id, &blob1_f,
3146 &blob1_size, path1);
3147 if (err)
3148 goto done;
3149 if (fclose(blob1_f) == EOF) {
3150 err = got_error_from_errno("fclose");
3151 goto done;
3154 /* Implied existence check. */
3155 err = got_object_get_type(&obj_type, repo, id);
3156 if (err) {
3157 if (err->code != GOT_ERR_NO_OBJ)
3158 goto done;
3159 err = (*a->progress_cb)(a->progress_arg,
3160 GOT_STATUS_CANNOT_DELETE, path1);
3161 goto done;
3162 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3163 err = (*a->progress_cb)(a->progress_arg,
3164 GOT_STATUS_CANNOT_DELETE, path1);
3165 goto done;
3167 err = (*a->progress_cb)(a->progress_arg,
3168 GOT_STATUS_DELETE, path1);
3169 if (err)
3170 goto done;
3171 err = remove_ondisk_file(a->worktree->root_path,
3172 path1);
3173 if (err)
3174 goto done;
3175 if (ie)
3176 got_fileindex_entry_mark_deleted_from_disk(ie);
3177 break;
3179 case GOT_STATUS_ADD: {
3180 FILE *blob1_f;
3181 off_t blob1_size;
3183 * Delete the file only if its content already
3184 * exists in the repository.
3186 err = got_object_blob_file_create(&id, &blob1_f,
3187 &blob1_size, path1);
3188 if (err)
3189 goto done;
3190 if (fclose(blob1_f) == EOF) {
3191 err = got_error_from_errno("fclose");
3192 goto done;
3194 if (got_object_id_cmp(id, id1) == 0) {
3195 err = (*a->progress_cb)(a->progress_arg,
3196 GOT_STATUS_DELETE, path1);
3197 if (err)
3198 goto done;
3199 err = remove_ondisk_file(a->worktree->root_path,
3200 path1);
3201 if (err)
3202 goto done;
3203 if (ie)
3204 got_fileindex_entry_remove(a->fileindex,
3205 ie);
3206 } else {
3207 err = (*a->progress_cb)(a->progress_arg,
3208 GOT_STATUS_CANNOT_DELETE, path1);
3210 if (err)
3211 goto done;
3212 break;
3214 case GOT_STATUS_CONFLICT:
3215 err = (*a->progress_cb)(a->progress_arg,
3216 GOT_STATUS_CANNOT_DELETE, path1);
3217 if (err)
3218 goto done;
3219 break;
3220 case GOT_STATUS_OBSTRUCTED:
3221 err = (*a->progress_cb)(a->progress_arg, status, path1);
3222 if (err)
3223 goto done;
3224 break;
3225 default:
3226 break;
3228 } else if (blob2) {
3229 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3230 path2) == -1)
3231 return got_error_from_errno("asprintf");
3232 ie = got_fileindex_entry_get(a->fileindex, path2,
3233 strlen(path2));
3234 if (ie) {
3235 err = get_file_status(&status, &sb, ie, ondisk_path,
3236 -1, NULL, repo);
3237 if (err)
3238 goto done;
3239 if (status != GOT_STATUS_NO_CHANGE &&
3240 status != GOT_STATUS_MODIFY &&
3241 status != GOT_STATUS_CONFLICT &&
3242 status != GOT_STATUS_ADD &&
3243 status != GOT_STATUS_DELETE) {
3244 err = (*a->progress_cb)(a->progress_arg,
3245 status, path2);
3246 goto done;
3248 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3249 char *link_target2;
3250 err = got_object_blob_read_to_str(&link_target2,
3251 blob2);
3252 if (err)
3253 goto done;
3254 err = merge_symlink(a->worktree, NULL,
3255 ondisk_path, path2, a->label_orig,
3256 link_target2, a->commit_id2, repo,
3257 a->progress_cb, a->progress_arg);
3258 free(link_target2);
3259 } else if (S_ISREG(sb.st_mode)) {
3260 err = merge_blob(&local_changes_subsumed,
3261 a->worktree, NULL, ondisk_path, path2,
3262 sb.st_mode, a->label_orig, blob2,
3263 a->commit_id2, repo, a->progress_cb,
3264 a->progress_arg);
3265 } else if (status != GOT_STATUS_DELETE) {
3266 err = got_error_path(ondisk_path,
3267 GOT_ERR_FILE_OBSTRUCTED);
3269 if (err)
3270 goto done;
3271 if (status == GOT_STATUS_DELETE) {
3272 /* Re-add file with content from new blob. */
3273 err = add_file(a->worktree, a->fileindex, ie,
3274 ondisk_path, path2, blob2, mode2,
3275 0, 0, 0, a->allow_bad_symlinks,
3276 repo, a->progress_cb, a->progress_arg);
3277 if (err)
3278 goto done;
3280 } else {
3281 err = add_file(a->worktree, a->fileindex, NULL,
3282 ondisk_path, path2, blob2, mode2,
3283 0, 0, 1, a->allow_bad_symlinks,
3284 repo, a->progress_cb, a->progress_arg);
3285 if (err)
3286 goto done;
3289 done:
3290 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3291 err = got_error_from_errno("fclose");
3292 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3293 err = got_error_from_errno("fclose");
3294 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3295 err = got_error_from_errno("fclose");
3296 free(id_str);
3297 free(id);
3298 free(label_deriv2);
3299 free(ondisk_path);
3300 return err;
3303 struct check_mixed_commits_args {
3304 struct got_worktree *worktree;
3305 got_cancel_cb cancel_cb;
3306 void *cancel_arg;
3309 static const struct got_error *
3310 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3312 const struct got_error *err = NULL;
3313 struct check_mixed_commits_args *a = arg;
3315 if (a->cancel_cb) {
3316 err = a->cancel_cb(a->cancel_arg);
3317 if (err)
3318 return err;
3321 /* Reject merges into a work tree with mixed base commits. */
3322 if (got_fileindex_entry_has_commit(ie) &&
3323 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3324 SHA1_DIGEST_LENGTH) != 0)
3325 return got_error(GOT_ERR_MIXED_COMMITS);
3327 return NULL;
3330 const struct got_error *
3331 got_worktree_get_state(char *state, struct got_repository *repo,
3332 struct got_worktree *worktree,
3333 got_cancel_cb cancel_cb, void *cancel_arg)
3335 const struct got_error *err;
3336 struct got_object_id *base_id, *head_id = NULL;
3337 struct got_reference *head_ref;
3338 struct got_fileindex *fileindex = NULL;
3339 char *fileindex_path = NULL;
3340 struct check_mixed_commits_args cma;
3342 if (worktree == NULL)
3343 return got_error(GOT_ERR_NOT_WORKTREE);
3345 err = got_ref_open(&head_ref, repo,
3346 got_worktree_get_head_ref_name(worktree), 0);
3347 if (err)
3348 return err;
3350 err = got_ref_resolve(&head_id, repo, head_ref);
3351 if (err)
3352 goto done;
3354 *state = GOT_WORKTREE_STATE_UNKNOWN;
3355 base_id = got_worktree_get_base_commit_id(worktree);
3357 cma.worktree = worktree;
3358 cma.cancel_cb = cancel_cb;
3359 cma.cancel_arg = cancel_arg;
3361 if (got_object_id_cmp(base_id, head_id) == 0) {
3362 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3363 if (err)
3364 goto done;
3366 err = got_fileindex_for_each_entry_safe(fileindex,
3367 check_mixed_commits, &cma);
3368 if (err == NULL)
3369 *state = GOT_WORKTREE_STATE_UPTODATE;
3370 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3371 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3372 err = NULL;
3374 } else
3375 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3377 done:
3378 free(head_id);
3379 free(fileindex_path);
3380 got_ref_close(head_ref);
3381 if (fileindex != NULL)
3382 got_fileindex_free(fileindex);
3383 return err;
3386 struct check_merge_conflicts_arg {
3387 struct got_worktree *worktree;
3388 struct got_fileindex *fileindex;
3389 struct got_repository *repo;
3392 static const struct got_error *
3393 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3394 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3395 struct got_object_id *id1, struct got_object_id *id2,
3396 const char *path1, const char *path2,
3397 mode_t mode1, mode_t mode2, struct got_repository *repo)
3399 const struct got_error *err = NULL;
3400 struct check_merge_conflicts_arg *a = arg;
3401 unsigned char status;
3402 struct stat sb;
3403 struct got_fileindex_entry *ie;
3404 const char *path = path2 ? path2 : path1;
3405 struct got_object_id *id = id2 ? id2 : id1;
3406 char *ondisk_path;
3408 if (id == NULL)
3409 return NULL;
3411 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3412 if (ie == NULL)
3413 return NULL;
3415 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3416 == -1)
3417 return got_error_from_errno("asprintf");
3419 /* Reject merges into a work tree with conflicted files. */
3420 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3421 free(ondisk_path);
3422 if (err)
3423 return err;
3424 if (status == GOT_STATUS_CONFLICT)
3425 return got_error(GOT_ERR_CONFLICTS);
3427 return NULL;
3430 static const struct got_error *
3431 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3432 const char *fileindex_path, struct got_object_id *commit_id1,
3433 struct got_object_id *commit_id2, struct got_repository *repo,
3434 got_worktree_checkout_cb progress_cb, void *progress_arg,
3435 got_cancel_cb cancel_cb, void *cancel_arg)
3437 const struct got_error *err = NULL, *sync_err;
3438 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3439 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3440 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3441 struct check_merge_conflicts_arg cmc_arg;
3442 struct merge_file_cb_arg arg;
3443 char *label_orig = NULL;
3444 FILE *f1 = NULL, *f2 = NULL;
3445 int fd1 = -1, fd2 = -1;
3447 if (commit_id1) {
3448 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3449 if (err)
3450 goto done;
3451 err = got_object_id_by_path(&tree_id1, repo, commit1,
3452 worktree->path_prefix);
3453 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3454 goto done;
3456 if (tree_id1) {
3457 char *id_str;
3459 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3460 if (err)
3461 goto done;
3463 err = got_object_id_str(&id_str, commit_id1);
3464 if (err)
3465 goto done;
3467 if (asprintf(&label_orig, "%s: commit %s",
3468 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3469 err = got_error_from_errno("asprintf");
3470 free(id_str);
3471 goto done;
3473 free(id_str);
3475 f1 = got_opentemp();
3476 if (f1 == NULL) {
3477 err = got_error_from_errno("got_opentemp");
3478 goto done;
3482 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3483 if (err)
3484 goto done;
3486 err = got_object_id_by_path(&tree_id2, repo, commit2,
3487 worktree->path_prefix);
3488 if (err)
3489 goto done;
3491 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3492 if (err)
3493 goto done;
3495 f2 = got_opentemp();
3496 if (f2 == NULL) {
3497 err = got_error_from_errno("got_opentemp");
3498 goto done;
3501 fd1 = got_opentempfd();
3502 if (fd1 == -1) {
3503 err = got_error_from_errno("got_opentempfd");
3504 goto done;
3507 fd2 = got_opentempfd();
3508 if (fd2 == -1) {
3509 err = got_error_from_errno("got_opentempfd");
3510 goto done;
3513 cmc_arg.worktree = worktree;
3514 cmc_arg.fileindex = fileindex;
3515 cmc_arg.repo = repo;
3516 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3517 check_merge_conflicts, &cmc_arg, 0);
3518 if (err)
3519 goto done;
3521 arg.worktree = worktree;
3522 arg.fileindex = fileindex;
3523 arg.progress_cb = progress_cb;
3524 arg.progress_arg = progress_arg;
3525 arg.cancel_cb = cancel_cb;
3526 arg.cancel_arg = cancel_arg;
3527 arg.label_orig = label_orig;
3528 arg.commit_id2 = commit_id2;
3529 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3530 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3531 merge_file_cb, &arg, 1);
3532 sync_err = sync_fileindex(fileindex, fileindex_path);
3533 if (sync_err && err == NULL)
3534 err = sync_err;
3535 done:
3536 if (commit1)
3537 got_object_commit_close(commit1);
3538 if (commit2)
3539 got_object_commit_close(commit2);
3540 if (tree1)
3541 got_object_tree_close(tree1);
3542 if (tree2)
3543 got_object_tree_close(tree2);
3544 if (f1 && fclose(f1) == EOF && err == NULL)
3545 err = got_error_from_errno("fclose");
3546 if (f2 && fclose(f2) == EOF && err == NULL)
3547 err = got_error_from_errno("fclose");
3548 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3549 err = got_error_from_errno("close");
3550 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3551 err = got_error_from_errno("close");
3552 free(label_orig);
3553 return err;
3556 const struct got_error *
3557 got_worktree_merge_files(struct got_worktree *worktree,
3558 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3559 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3560 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3562 const struct got_error *err, *unlockerr;
3563 char *fileindex_path = NULL;
3564 struct got_fileindex *fileindex = NULL;
3565 struct check_mixed_commits_args cma;
3567 err = lock_worktree(worktree, LOCK_EX);
3568 if (err)
3569 return err;
3571 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3572 if (err)
3573 goto done;
3575 cma.worktree = worktree;
3576 cma.cancel_cb = cancel_cb;
3577 cma.cancel_arg = cancel_arg;
3579 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3580 &cma);
3581 if (err)
3582 goto done;
3584 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3585 commit_id2, repo, progress_cb, progress_arg,
3586 cancel_cb, cancel_arg);
3587 done:
3588 if (fileindex)
3589 got_fileindex_free(fileindex);
3590 free(fileindex_path);
3591 unlockerr = lock_worktree(worktree, LOCK_SH);
3592 if (unlockerr && err == NULL)
3593 err = unlockerr;
3594 return err;
3597 struct diff_dir_cb_arg {
3598 struct got_fileindex *fileindex;
3599 struct got_worktree *worktree;
3600 const char *status_path;
3601 size_t status_path_len;
3602 struct got_repository *repo;
3603 got_worktree_status_cb status_cb;
3604 void *status_arg;
3605 got_cancel_cb cancel_cb;
3606 void *cancel_arg;
3607 /* A pathlist containing per-directory pathlists of ignore patterns. */
3608 struct got_pathlist_head *ignores;
3609 int report_unchanged;
3610 int no_ignores;
3613 static const struct got_error *
3614 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3615 int dirfd, const char *de_name,
3616 got_worktree_status_cb status_cb, void *status_arg,
3617 struct got_repository *repo, int report_unchanged)
3619 const struct got_error *err = NULL;
3620 unsigned char status = GOT_STATUS_NO_CHANGE;
3621 unsigned char staged_status;
3622 struct stat sb;
3623 struct got_object_id blob_id, commit_id, staged_blob_id;
3624 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3625 struct got_object_id *staged_blob_idp = NULL;
3627 staged_status = get_staged_status(ie);
3628 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3629 if (err)
3630 return err;
3632 if (status == GOT_STATUS_NO_CHANGE &&
3633 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3634 return NULL;
3636 if (got_fileindex_entry_has_blob(ie))
3637 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3638 if (got_fileindex_entry_has_commit(ie))
3639 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3640 if (staged_status == GOT_STATUS_ADD ||
3641 staged_status == GOT_STATUS_MODIFY) {
3642 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3643 &staged_blob_id, ie);
3646 return (*status_cb)(status_arg, status, staged_status,
3647 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3650 static const struct got_error *
3651 status_old_new(void *arg, struct got_fileindex_entry *ie,
3652 struct dirent *de, const char *parent_path, int dirfd)
3654 const struct got_error *err = NULL;
3655 struct diff_dir_cb_arg *a = arg;
3656 char *abspath;
3658 if (a->cancel_cb) {
3659 err = a->cancel_cb(a->cancel_arg);
3660 if (err)
3661 return err;
3664 if (got_path_cmp(parent_path, a->status_path,
3665 strlen(parent_path), a->status_path_len) != 0 &&
3666 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3667 return NULL;
3669 if (parent_path[0]) {
3670 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3671 parent_path, de->d_name) == -1)
3672 return got_error_from_errno("asprintf");
3673 } else {
3674 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3675 de->d_name) == -1)
3676 return got_error_from_errno("asprintf");
3679 err = report_file_status(ie, abspath, dirfd, de->d_name,
3680 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3681 free(abspath);
3682 return err;
3685 static const struct got_error *
3686 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3688 const struct got_error *err;
3689 struct diff_dir_cb_arg *a = arg;
3690 struct got_object_id blob_id, commit_id;
3691 unsigned char status;
3693 if (a->cancel_cb) {
3694 err = a->cancel_cb(a->cancel_arg);
3695 if (err)
3696 return err;
3699 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3700 return NULL;
3702 got_fileindex_entry_get_blob_id(&blob_id, ie);
3703 got_fileindex_entry_get_commit_id(&commit_id, ie);
3704 if (got_fileindex_entry_has_file_on_disk(ie))
3705 status = GOT_STATUS_MISSING;
3706 else
3707 status = GOT_STATUS_DELETE;
3708 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3709 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3712 static void
3713 free_ignores(struct got_pathlist_head *ignores)
3715 struct got_pathlist_entry *pe;
3717 TAILQ_FOREACH(pe, ignores, entry) {
3718 struct got_pathlist_head *ignorelist = pe->data;
3720 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3722 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3725 static const struct got_error *
3726 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3728 const struct got_error *err = NULL;
3729 struct got_pathlist_entry *pe = NULL;
3730 struct got_pathlist_head *ignorelist;
3731 char *line = NULL, *pattern, *dirpath = NULL;
3732 size_t linesize = 0;
3733 ssize_t linelen;
3735 ignorelist = calloc(1, sizeof(*ignorelist));
3736 if (ignorelist == NULL)
3737 return got_error_from_errno("calloc");
3738 TAILQ_INIT(ignorelist);
3740 while ((linelen = getline(&line, &linesize, f)) != -1) {
3741 if (linelen > 0 && line[linelen - 1] == '\n')
3742 line[linelen - 1] = '\0';
3744 /* Git's ignores may contain comments. */
3745 if (line[0] == '#')
3746 continue;
3748 /* Git's negated patterns are not (yet?) supported. */
3749 if (line[0] == '!')
3750 continue;
3752 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3753 line) == -1) {
3754 err = got_error_from_errno("asprintf");
3755 goto done;
3757 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3758 if (err)
3759 goto done;
3761 if (ferror(f)) {
3762 err = got_error_from_errno("getline");
3763 goto done;
3766 dirpath = strdup(path);
3767 if (dirpath == NULL) {
3768 err = got_error_from_errno("strdup");
3769 goto done;
3771 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3772 done:
3773 free(line);
3774 if (err || pe == NULL) {
3775 free(dirpath);
3776 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3777 free(ignorelist);
3779 return err;
3782 static int
3783 match_path(const char *pattern, size_t pattern_len, const char *path,
3784 int flags)
3786 char buf[PATH_MAX];
3789 * Trailing slashes signify directories.
3790 * Append a * to make such patterns conform to fnmatch rules.
3792 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3793 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3794 return FNM_NOMATCH; /* XXX */
3796 return fnmatch(buf, path, flags);
3799 return fnmatch(pattern, path, flags);
3802 static int
3803 match_ignores(struct got_pathlist_head *ignores, const char *path)
3805 struct got_pathlist_entry *pe;
3807 /* Handle patterns which match in all directories. */
3808 TAILQ_FOREACH(pe, ignores, entry) {
3809 struct got_pathlist_head *ignorelist = pe->data;
3810 struct got_pathlist_entry *pi;
3812 TAILQ_FOREACH(pi, ignorelist, entry) {
3813 const char *p;
3815 if (pi->path_len < 3 ||
3816 strncmp(pi->path, "**/", 3) != 0)
3817 continue;
3818 p = path;
3819 while (*p) {
3820 if (match_path(pi->path + 3,
3821 pi->path_len - 3, p,
3822 FNM_PATHNAME | FNM_LEADING_DIR)) {
3823 /* Retry in next directory. */
3824 while (*p && *p != '/')
3825 p++;
3826 while (*p == '/')
3827 p++;
3828 continue;
3830 return 1;
3836 * The ignores pathlist contains ignore lists from children before
3837 * parents, so we can find the most specific ignorelist by walking
3838 * ignores backwards.
3840 pe = TAILQ_LAST(ignores, got_pathlist_head);
3841 while (pe) {
3842 if (got_path_is_child(path, pe->path, pe->path_len)) {
3843 struct got_pathlist_head *ignorelist = pe->data;
3844 struct got_pathlist_entry *pi;
3845 TAILQ_FOREACH(pi, ignorelist, entry) {
3846 int flags = FNM_LEADING_DIR;
3847 if (strstr(pi->path, "/**/") == NULL)
3848 flags |= FNM_PATHNAME;
3849 if (match_path(pi->path, pi->path_len,
3850 path, flags))
3851 continue;
3852 return 1;
3855 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3858 return 0;
3861 static const struct got_error *
3862 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3863 const char *path, int dirfd, const char *ignores_filename)
3865 const struct got_error *err = NULL;
3866 char *ignorespath;
3867 int fd = -1;
3868 FILE *ignoresfile = NULL;
3870 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3871 path[0] ? "/" : "", ignores_filename) == -1)
3872 return got_error_from_errno("asprintf");
3874 if (dirfd != -1) {
3875 fd = openat(dirfd, ignores_filename,
3876 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3877 if (fd == -1) {
3878 if (errno != ENOENT && errno != EACCES)
3879 err = got_error_from_errno2("openat",
3880 ignorespath);
3881 } else {
3882 ignoresfile = fdopen(fd, "r");
3883 if (ignoresfile == NULL)
3884 err = got_error_from_errno2("fdopen",
3885 ignorespath);
3886 else {
3887 fd = -1;
3888 err = read_ignores(ignores, path, ignoresfile);
3891 } else {
3892 ignoresfile = fopen(ignorespath, "re");
3893 if (ignoresfile == NULL) {
3894 if (errno != ENOENT && errno != EACCES)
3895 err = got_error_from_errno2("fopen",
3896 ignorespath);
3897 } else
3898 err = read_ignores(ignores, path, ignoresfile);
3901 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3902 err = got_error_from_errno2("fclose", path);
3903 if (fd != -1 && close(fd) == -1 && err == NULL)
3904 err = got_error_from_errno2("close", path);
3905 free(ignorespath);
3906 return err;
3909 static const struct got_error *
3910 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3911 int dirfd)
3913 const struct got_error *err = NULL;
3914 struct diff_dir_cb_arg *a = arg;
3915 char *path = NULL;
3917 if (ignore != NULL)
3918 *ignore = 0;
3920 if (a->cancel_cb) {
3921 err = a->cancel_cb(a->cancel_arg);
3922 if (err)
3923 return err;
3926 if (parent_path[0]) {
3927 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3928 return got_error_from_errno("asprintf");
3929 } else {
3930 path = de->d_name;
3933 if (de->d_type == DT_DIR) {
3934 if (!a->no_ignores && ignore != NULL &&
3935 match_ignores(a->ignores, path))
3936 *ignore = 1;
3937 } else if (!match_ignores(a->ignores, path) &&
3938 got_path_is_child(path, a->status_path, a->status_path_len))
3939 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3940 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3941 if (parent_path[0])
3942 free(path);
3943 return err;
3946 static const struct got_error *
3947 status_traverse(void *arg, const char *path, int dirfd)
3949 const struct got_error *err = NULL;
3950 struct diff_dir_cb_arg *a = arg;
3952 if (a->no_ignores)
3953 return NULL;
3955 err = add_ignores(a->ignores, a->worktree->root_path,
3956 path, dirfd, ".cvsignore");
3957 if (err)
3958 return err;
3960 err = add_ignores(a->ignores, a->worktree->root_path, path,
3961 dirfd, ".gitignore");
3963 return err;
3966 static const struct got_error *
3967 report_single_file_status(const char *path, const char *ondisk_path,
3968 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3969 void *status_arg, struct got_repository *repo, int report_unchanged,
3970 struct got_pathlist_head *ignores, int no_ignores)
3972 struct got_fileindex_entry *ie;
3973 struct stat sb;
3975 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3976 if (ie)
3977 return report_file_status(ie, ondisk_path, -1, NULL,
3978 status_cb, status_arg, repo, report_unchanged);
3980 if (lstat(ondisk_path, &sb) == -1) {
3981 if (errno != ENOENT)
3982 return got_error_from_errno2("lstat", ondisk_path);
3983 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3984 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3987 if (!no_ignores && match_ignores(ignores, path))
3988 return NULL;
3990 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3991 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3992 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3994 return NULL;
3997 static const struct got_error *
3998 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3999 const char *root_path, const char *path)
4001 const struct got_error *err;
4002 char *parent_path, *next_parent_path = NULL;
4004 err = add_ignores(ignores, root_path, "", -1,
4005 ".cvsignore");
4006 if (err)
4007 return err;
4009 err = add_ignores(ignores, root_path, "", -1,
4010 ".gitignore");
4011 if (err)
4012 return err;
4014 err = got_path_dirname(&parent_path, path);
4015 if (err) {
4016 if (err->code == GOT_ERR_BAD_PATH)
4017 return NULL; /* cannot traverse parent */
4018 return err;
4020 for (;;) {
4021 err = add_ignores(ignores, root_path, parent_path, -1,
4022 ".cvsignore");
4023 if (err)
4024 break;
4025 err = add_ignores(ignores, root_path, parent_path, -1,
4026 ".gitignore");
4027 if (err)
4028 break;
4029 err = got_path_dirname(&next_parent_path, parent_path);
4030 if (err) {
4031 if (err->code == GOT_ERR_BAD_PATH)
4032 err = NULL; /* traversed everything */
4033 break;
4035 if (got_path_is_root_dir(parent_path))
4036 break;
4037 free(parent_path);
4038 parent_path = next_parent_path;
4039 next_parent_path = NULL;
4042 free(parent_path);
4043 free(next_parent_path);
4044 return err;
4047 struct find_missing_children_args {
4048 const char *parent_path;
4049 size_t parent_len;
4050 struct got_pathlist_head *children;
4051 got_cancel_cb cancel_cb;
4052 void *cancel_arg;
4055 static const struct got_error *
4056 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4058 const struct got_error *err = NULL;
4059 struct find_missing_children_args *a = arg;
4061 if (a->cancel_cb) {
4062 err = a->cancel_cb(a->cancel_arg);
4063 if (err)
4064 return err;
4067 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4068 err = got_pathlist_append(a->children, ie->path, NULL);
4070 return err;
4073 static const struct got_error *
4074 report_children(struct got_pathlist_head *children,
4075 struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 struct got_repository *repo, int is_root_dir, int report_unchanged,
4077 struct got_pathlist_head *ignores, int no_ignores,
4078 got_worktree_status_cb status_cb, void *status_arg,
4079 got_cancel_cb cancel_cb, void *cancel_arg)
4081 const struct got_error *err = NULL;
4082 struct got_pathlist_entry *pe;
4083 char *ondisk_path = NULL;
4085 TAILQ_FOREACH(pe, children, entry) {
4086 if (cancel_cb) {
4087 err = cancel_cb(cancel_arg);
4088 if (err)
4089 break;
4092 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4093 !is_root_dir ? "/" : "", pe->path) == -1) {
4094 err = got_error_from_errno("asprintf");
4095 ondisk_path = NULL;
4096 break;
4099 err = report_single_file_status(pe->path, ondisk_path,
4100 fileindex, status_cb, status_arg, repo, report_unchanged,
4101 ignores, no_ignores);
4102 if (err)
4103 break;
4105 free(ondisk_path);
4106 ondisk_path = NULL;
4109 free(ondisk_path);
4110 return err;
4113 static const struct got_error *
4114 worktree_status(struct got_worktree *worktree, const char *path,
4115 struct got_fileindex *fileindex, struct got_repository *repo,
4116 got_worktree_status_cb status_cb, void *status_arg,
4117 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4118 int report_unchanged)
4120 const struct got_error *err = NULL;
4121 int fd = -1;
4122 struct got_fileindex_diff_dir_cb fdiff_cb;
4123 struct diff_dir_cb_arg arg;
4124 char *ondisk_path = NULL;
4125 struct got_pathlist_head ignores, missing_children;
4126 struct got_fileindex_entry *ie;
4128 TAILQ_INIT(&ignores);
4129 TAILQ_INIT(&missing_children);
4131 if (asprintf(&ondisk_path, "%s%s%s",
4132 worktree->root_path, path[0] ? "/" : "", path) == -1)
4133 return got_error_from_errno("asprintf");
4135 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4136 if (ie) {
4137 err = report_single_file_status(path, ondisk_path,
4138 fileindex, status_cb, status_arg, repo,
4139 report_unchanged, &ignores, no_ignores);
4140 goto done;
4141 } else {
4142 struct find_missing_children_args fmca;
4143 fmca.parent_path = path;
4144 fmca.parent_len = strlen(path);
4145 fmca.children = &missing_children;
4146 fmca.cancel_cb = cancel_cb;
4147 fmca.cancel_arg = cancel_arg;
4148 err = got_fileindex_for_each_entry_safe(fileindex,
4149 find_missing_children, &fmca);
4150 if (err)
4151 goto done;
4154 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4155 if (fd == -1) {
4156 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4157 !got_err_open_nofollow_on_symlink())
4158 err = got_error_from_errno2("open", ondisk_path);
4159 else {
4160 if (!no_ignores) {
4161 err = add_ignores_from_parent_paths(&ignores,
4162 worktree->root_path, ondisk_path);
4163 if (err)
4164 goto done;
4166 if (TAILQ_EMPTY(&missing_children)) {
4167 err = report_single_file_status(path,
4168 ondisk_path, fileindex,
4169 status_cb, status_arg, repo,
4170 report_unchanged, &ignores, no_ignores);
4171 if (err)
4172 goto done;
4173 } else {
4174 err = report_children(&missing_children,
4175 worktree, fileindex, repo,
4176 (path[0] == '\0'), report_unchanged,
4177 &ignores, no_ignores,
4178 status_cb, status_arg,
4179 cancel_cb, cancel_arg);
4180 if (err)
4181 goto done;
4184 } else {
4185 fdiff_cb.diff_old_new = status_old_new;
4186 fdiff_cb.diff_old = status_old;
4187 fdiff_cb.diff_new = status_new;
4188 fdiff_cb.diff_traverse = status_traverse;
4189 arg.fileindex = fileindex;
4190 arg.worktree = worktree;
4191 arg.status_path = path;
4192 arg.status_path_len = strlen(path);
4193 arg.repo = repo;
4194 arg.status_cb = status_cb;
4195 arg.status_arg = status_arg;
4196 arg.cancel_cb = cancel_cb;
4197 arg.cancel_arg = cancel_arg;
4198 arg.report_unchanged = report_unchanged;
4199 arg.no_ignores = no_ignores;
4200 if (!no_ignores) {
4201 err = add_ignores_from_parent_paths(&ignores,
4202 worktree->root_path, path);
4203 if (err)
4204 goto done;
4206 arg.ignores = &ignores;
4207 err = got_fileindex_diff_dir(fileindex, fd,
4208 worktree->root_path, path, repo, &fdiff_cb, &arg);
4210 done:
4211 free_ignores(&ignores);
4212 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4213 if (fd != -1 && close(fd) == -1 && err == NULL)
4214 err = got_error_from_errno("close");
4215 free(ondisk_path);
4216 return err;
4219 const struct got_error *
4220 got_worktree_status(struct got_worktree *worktree,
4221 struct got_pathlist_head *paths, struct got_repository *repo,
4222 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4223 got_cancel_cb cancel_cb, void *cancel_arg)
4225 const struct got_error *err = NULL;
4226 char *fileindex_path = NULL;
4227 struct got_fileindex *fileindex = NULL;
4228 struct got_pathlist_entry *pe;
4230 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4231 if (err)
4232 return err;
4234 TAILQ_FOREACH(pe, paths, entry) {
4235 err = worktree_status(worktree, pe->path, fileindex, repo,
4236 status_cb, status_arg, cancel_cb, cancel_arg,
4237 no_ignores, 0);
4238 if (err)
4239 break;
4241 free(fileindex_path);
4242 got_fileindex_free(fileindex);
4243 return err;
4246 const struct got_error *
4247 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4248 const char *arg)
4250 const struct got_error *err = NULL;
4251 char *resolved = NULL, *cwd = NULL, *path = NULL;
4252 size_t len;
4253 struct stat sb;
4254 char *abspath = NULL;
4255 char canonpath[PATH_MAX];
4257 *wt_path = NULL;
4259 cwd = getcwd(NULL, 0);
4260 if (cwd == NULL)
4261 return got_error_from_errno("getcwd");
4263 if (lstat(arg, &sb) == -1) {
4264 if (errno != ENOENT) {
4265 err = got_error_from_errno2("lstat", arg);
4266 goto done;
4268 sb.st_mode = 0;
4270 if (S_ISLNK(sb.st_mode)) {
4272 * We cannot use realpath(3) with symlinks since we want to
4273 * operate on the symlink itself.
4274 * But we can make the path absolute, assuming it is relative
4275 * to the current working directory, and then canonicalize it.
4277 if (!got_path_is_absolute(arg)) {
4278 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4279 err = got_error_from_errno("asprintf");
4280 goto done;
4284 err = got_canonpath(abspath ? abspath : arg, canonpath,
4285 sizeof(canonpath));
4286 if (err)
4287 goto done;
4288 resolved = strdup(canonpath);
4289 if (resolved == NULL) {
4290 err = got_error_from_errno("strdup");
4291 goto done;
4293 } else {
4294 resolved = realpath(arg, NULL);
4295 if (resolved == NULL) {
4296 if (errno != ENOENT) {
4297 err = got_error_from_errno2("realpath", arg);
4298 goto done;
4300 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4301 err = got_error_from_errno("asprintf");
4302 goto done;
4304 err = got_canonpath(abspath, canonpath,
4305 sizeof(canonpath));
4306 if (err)
4307 goto done;
4308 resolved = strdup(canonpath);
4309 if (resolved == NULL) {
4310 err = got_error_from_errno("strdup");
4311 goto done;
4316 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4317 strlen(got_worktree_get_root_path(worktree)))) {
4318 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4319 goto done;
4322 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4323 err = got_path_skip_common_ancestor(&path,
4324 got_worktree_get_root_path(worktree), resolved);
4325 if (err)
4326 goto done;
4327 } else {
4328 path = strdup("");
4329 if (path == NULL) {
4330 err = got_error_from_errno("strdup");
4331 goto done;
4335 /* XXX status walk can't deal with trailing slash! */
4336 len = strlen(path);
4337 while (len > 0 && path[len - 1] == '/') {
4338 path[len - 1] = '\0';
4339 len--;
4341 done:
4342 free(abspath);
4343 free(resolved);
4344 free(cwd);
4345 if (err == NULL)
4346 *wt_path = path;
4347 else
4348 free(path);
4349 return err;
4352 struct schedule_addition_args {
4353 struct got_worktree *worktree;
4354 struct got_fileindex *fileindex;
4355 got_worktree_checkout_cb progress_cb;
4356 void *progress_arg;
4357 struct got_repository *repo;
4360 static int
4361 add_noop_status(unsigned char status)
4363 return (status == GOT_STATUS_ADD ||
4364 status == GOT_STATUS_MODIFY ||
4365 status == GOT_STATUS_CONFLICT ||
4366 status == GOT_STATUS_MODE_CHANGE ||
4367 status == GOT_STATUS_NO_CHANGE);
4370 static const struct got_error *
4371 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4372 const char *relpath, struct got_object_id *blob_id,
4373 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4374 int dirfd, const char *de_name)
4376 struct schedule_addition_args *a = arg;
4377 const struct got_error *err = NULL;
4378 struct got_fileindex_entry *ie;
4379 struct stat sb;
4380 char *ondisk_path;
4382 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4383 relpath) == -1)
4384 return got_error_from_errno("asprintf");
4386 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4387 if (ie) {
4388 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4389 de_name, a->repo);
4390 if (err)
4391 goto done;
4392 /* Re-adding an existing entry is a no-op. */
4393 if (staged_status == GOT_STATUS_NO_CHANGE &&
4394 add_noop_status(status))
4395 goto done;
4396 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4397 if (err)
4398 goto done;
4401 if (status != GOT_STATUS_UNVERSIONED) {
4402 if (status == GOT_STATUS_NONEXISTENT)
4403 err = got_error_set_errno(ENOENT, ondisk_path);
4404 else
4405 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4406 goto done;
4409 err = got_fileindex_entry_alloc(&ie, relpath);
4410 if (err)
4411 goto done;
4412 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4413 relpath, NULL, NULL, 1);
4414 if (err) {
4415 got_fileindex_entry_free(ie);
4416 goto done;
4418 err = got_fileindex_entry_add(a->fileindex, ie);
4419 if (err) {
4420 got_fileindex_entry_free(ie);
4421 goto done;
4423 done:
4424 free(ondisk_path);
4425 if (err)
4426 return err;
4427 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4428 return NULL;
4429 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4432 const struct got_error *
4433 got_worktree_schedule_add(struct got_worktree *worktree,
4434 struct got_pathlist_head *paths,
4435 got_worktree_checkout_cb progress_cb, void *progress_arg,
4436 struct got_repository *repo, int no_ignores)
4438 struct got_fileindex *fileindex = NULL;
4439 char *fileindex_path = NULL;
4440 const struct got_error *err = NULL, *sync_err, *unlockerr;
4441 struct got_pathlist_entry *pe;
4442 struct schedule_addition_args saa;
4444 err = lock_worktree(worktree, LOCK_EX);
4445 if (err)
4446 return err;
4448 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4449 if (err)
4450 goto done;
4452 saa.worktree = worktree;
4453 saa.fileindex = fileindex;
4454 saa.progress_cb = progress_cb;
4455 saa.progress_arg = progress_arg;
4456 saa.repo = repo;
4458 TAILQ_FOREACH(pe, paths, entry) {
4459 err = worktree_status(worktree, pe->path, fileindex, repo,
4460 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4461 if (err)
4462 break;
4464 sync_err = sync_fileindex(fileindex, fileindex_path);
4465 if (sync_err && err == NULL)
4466 err = sync_err;
4467 done:
4468 free(fileindex_path);
4469 if (fileindex)
4470 got_fileindex_free(fileindex);
4471 unlockerr = lock_worktree(worktree, LOCK_SH);
4472 if (unlockerr && err == NULL)
4473 err = unlockerr;
4474 return err;
4477 struct schedule_deletion_args {
4478 struct got_worktree *worktree;
4479 struct got_fileindex *fileindex;
4480 got_worktree_delete_cb progress_cb;
4481 void *progress_arg;
4482 struct got_repository *repo;
4483 int delete_local_mods;
4484 int keep_on_disk;
4485 int ignore_missing_paths;
4486 const char *status_path;
4487 size_t status_path_len;
4488 const char *status_codes;
4491 static const struct got_error *
4492 schedule_for_deletion(void *arg, unsigned char status,
4493 unsigned char staged_status, const char *relpath,
4494 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4495 struct got_object_id *commit_id, int dirfd, const char *de_name)
4497 struct schedule_deletion_args *a = arg;
4498 const struct got_error *err = NULL;
4499 struct got_fileindex_entry *ie = NULL;
4500 struct stat sb;
4501 char *ondisk_path;
4503 if (status == GOT_STATUS_NONEXISTENT) {
4504 if (a->ignore_missing_paths)
4505 return NULL;
4506 return got_error_set_errno(ENOENT, relpath);
4509 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4510 if (ie == NULL)
4511 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4513 staged_status = get_staged_status(ie);
4514 if (staged_status != GOT_STATUS_NO_CHANGE) {
4515 if (staged_status == GOT_STATUS_DELETE)
4516 return NULL;
4517 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4520 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4521 relpath) == -1)
4522 return got_error_from_errno("asprintf");
4524 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4525 a->repo);
4526 if (err)
4527 goto done;
4529 if (a->status_codes) {
4530 size_t ncodes = strlen(a->status_codes);
4531 int i;
4532 for (i = 0; i < ncodes ; i++) {
4533 if (status == a->status_codes[i])
4534 break;
4536 if (i == ncodes) {
4537 /* Do not delete files in non-matching status. */
4538 free(ondisk_path);
4539 return NULL;
4541 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4542 a->status_codes[i] != GOT_STATUS_MISSING) {
4543 static char msg[64];
4544 snprintf(msg, sizeof(msg),
4545 "invalid status code '%c'", a->status_codes[i]);
4546 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4547 goto done;
4551 if (status != GOT_STATUS_NO_CHANGE) {
4552 if (status == GOT_STATUS_DELETE)
4553 goto done;
4554 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4555 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4556 goto done;
4558 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4559 err = got_error_set_errno(ENOENT, relpath);
4560 goto done;
4562 if (status != GOT_STATUS_MODIFY &&
4563 status != GOT_STATUS_MISSING) {
4564 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4565 goto done;
4569 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4570 size_t root_len;
4572 if (dirfd != -1) {
4573 if (unlinkat(dirfd, de_name, 0) == -1) {
4574 err = got_error_from_errno2("unlinkat",
4575 ondisk_path);
4576 goto done;
4578 } else if (unlink(ondisk_path) == -1) {
4579 err = got_error_from_errno2("unlink", ondisk_path);
4580 goto done;
4583 root_len = strlen(a->worktree->root_path);
4584 do {
4585 char *parent;
4587 err = got_path_dirname(&parent, ondisk_path);
4588 if (err)
4589 goto done;
4590 free(ondisk_path);
4591 ondisk_path = parent;
4592 if (got_path_cmp(ondisk_path, a->status_path,
4593 strlen(ondisk_path), a->status_path_len) != 0 &&
4594 !got_path_is_child(ondisk_path, a->status_path,
4595 a->status_path_len))
4596 break;
4597 if (rmdir(ondisk_path) == -1) {
4598 if (errno != ENOTEMPTY)
4599 err = got_error_from_errno2("rmdir",
4600 ondisk_path);
4601 break;
4603 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4604 strlen(ondisk_path), root_len) != 0);
4607 if (got_fileindex_entry_has_blob(ie))
4608 got_fileindex_entry_mark_deleted_from_disk(ie);
4609 else
4610 got_fileindex_entry_remove(a->fileindex, ie);
4611 done:
4612 free(ondisk_path);
4613 if (err)
4614 return err;
4615 if (status == GOT_STATUS_DELETE)
4616 return NULL;
4617 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4618 staged_status, relpath);
4621 const struct got_error *
4622 got_worktree_schedule_delete(struct got_worktree *worktree,
4623 struct got_pathlist_head *paths, int delete_local_mods,
4624 const char *status_codes,
4625 got_worktree_delete_cb progress_cb, void *progress_arg,
4626 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4628 struct got_fileindex *fileindex = NULL;
4629 char *fileindex_path = NULL;
4630 const struct got_error *err = NULL, *sync_err, *unlockerr;
4631 struct got_pathlist_entry *pe;
4632 struct schedule_deletion_args sda;
4634 err = lock_worktree(worktree, LOCK_EX);
4635 if (err)
4636 return err;
4638 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4639 if (err)
4640 goto done;
4642 sda.worktree = worktree;
4643 sda.fileindex = fileindex;
4644 sda.progress_cb = progress_cb;
4645 sda.progress_arg = progress_arg;
4646 sda.repo = repo;
4647 sda.delete_local_mods = delete_local_mods;
4648 sda.keep_on_disk = keep_on_disk;
4649 sda.ignore_missing_paths = ignore_missing_paths;
4650 sda.status_codes = status_codes;
4652 TAILQ_FOREACH(pe, paths, entry) {
4653 char *ondisk_status_path;
4655 if (asprintf(&ondisk_status_path, "%s%s%s",
4656 got_worktree_get_root_path(worktree),
4657 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4658 err = got_error_from_errno("asprintf");
4659 goto done;
4661 sda.status_path = ondisk_status_path;
4662 sda.status_path_len = strlen(ondisk_status_path);
4663 err = worktree_status(worktree, pe->path, fileindex, repo,
4664 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4665 free(ondisk_status_path);
4666 if (err)
4667 break;
4669 sync_err = sync_fileindex(fileindex, fileindex_path);
4670 if (sync_err && err == NULL)
4671 err = sync_err;
4672 done:
4673 free(fileindex_path);
4674 if (fileindex)
4675 got_fileindex_free(fileindex);
4676 unlockerr = lock_worktree(worktree, LOCK_SH);
4677 if (unlockerr && err == NULL)
4678 err = unlockerr;
4679 return err;
4682 static const struct got_error *
4683 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4685 const struct got_error *err = NULL;
4686 char *line = NULL;
4687 size_t linesize = 0, n;
4688 ssize_t linelen;
4690 linelen = getline(&line, &linesize, infile);
4691 if (linelen == -1) {
4692 if (ferror(infile)) {
4693 err = got_error_from_errno("getline");
4694 goto done;
4696 return NULL;
4698 if (outfile) {
4699 n = fwrite(line, 1, linelen, outfile);
4700 if (n != linelen) {
4701 err = got_ferror(outfile, GOT_ERR_IO);
4702 goto done;
4705 if (rejectfile) {
4706 n = fwrite(line, 1, linelen, rejectfile);
4707 if (n != linelen)
4708 err = got_ferror(rejectfile, GOT_ERR_IO);
4710 done:
4711 free(line);
4712 return err;
4715 static const struct got_error *
4716 skip_one_line(FILE *f)
4718 char *line = NULL;
4719 size_t linesize = 0;
4720 ssize_t linelen;
4722 linelen = getline(&line, &linesize, f);
4723 if (linelen == -1) {
4724 if (ferror(f))
4725 return got_error_from_errno("getline");
4726 return NULL;
4728 free(line);
4729 return NULL;
4732 static const struct got_error *
4733 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4734 int start_old, int end_old, int start_new, int end_new,
4735 FILE *outfile, FILE *rejectfile)
4737 const struct got_error *err;
4739 /* Copy old file's lines leading up to patch. */
4740 while (!feof(f1) && *line_cur1 < start_old) {
4741 err = copy_one_line(f1, outfile, NULL);
4742 if (err)
4743 return err;
4744 (*line_cur1)++;
4746 /* Skip new file's lines leading up to patch. */
4747 while (!feof(f2) && *line_cur2 < start_new) {
4748 if (rejectfile)
4749 err = copy_one_line(f2, NULL, rejectfile);
4750 else
4751 err = skip_one_line(f2);
4752 if (err)
4753 return err;
4754 (*line_cur2)++;
4756 /* Copy patched lines. */
4757 while (!feof(f2) && *line_cur2 <= end_new) {
4758 err = copy_one_line(f2, outfile, NULL);
4759 if (err)
4760 return err;
4761 (*line_cur2)++;
4763 /* Skip over old file's replaced lines. */
4764 while (!feof(f1) && *line_cur1 <= end_old) {
4765 if (rejectfile)
4766 err = copy_one_line(f1, NULL, rejectfile);
4767 else
4768 err = skip_one_line(f1);
4769 if (err)
4770 return err;
4771 (*line_cur1)++;
4774 return NULL;
4777 static const struct got_error *
4778 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4779 FILE *outfile, FILE *rejectfile)
4781 const struct got_error *err;
4783 if (outfile) {
4784 /* Copy old file's lines until EOF. */
4785 while (!feof(f1)) {
4786 err = copy_one_line(f1, outfile, NULL);
4787 if (err)
4788 return err;
4789 (*line_cur1)++;
4792 if (rejectfile) {
4793 /* Copy new file's lines until EOF. */
4794 while (!feof(f2)) {
4795 err = copy_one_line(f2, NULL, rejectfile);
4796 if (err)
4797 return err;
4798 (*line_cur2)++;
4802 return NULL;
4805 static const struct got_error *
4806 apply_or_reject_change(int *choice, int *nchunks_used,
4807 struct diff_result *diff_result, int n,
4808 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4809 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4810 got_worktree_patch_cb patch_cb, void *patch_arg)
4812 const struct got_error *err = NULL;
4813 struct diff_chunk_context cc = {};
4814 int start_old, end_old, start_new, end_new;
4815 FILE *hunkfile;
4816 struct diff_output_unidiff_state *diff_state;
4817 struct diff_input_info diff_info;
4818 int rc;
4820 *choice = GOT_PATCH_CHOICE_NONE;
4822 /* Get changed line numbers without context lines for copy_change(). */
4823 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4824 start_old = cc.left.start;
4825 end_old = cc.left.end;
4826 start_new = cc.right.start;
4827 end_new = cc.right.end;
4829 /* Get the same change with context lines for display. */
4830 memset(&cc, 0, sizeof(cc));
4831 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4833 memset(&diff_info, 0, sizeof(diff_info));
4834 diff_info.left_path = relpath;
4835 diff_info.right_path = relpath;
4837 diff_state = diff_output_unidiff_state_alloc();
4838 if (diff_state == NULL)
4839 return got_error_set_errno(ENOMEM,
4840 "diff_output_unidiff_state_alloc");
4842 hunkfile = got_opentemp();
4843 if (hunkfile == NULL) {
4844 err = got_error_from_errno("got_opentemp");
4845 goto done;
4848 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4849 diff_result, &cc);
4850 if (rc != DIFF_RC_OK) {
4851 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4852 goto done;
4855 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4856 err = got_ferror(hunkfile, GOT_ERR_IO);
4857 goto done;
4860 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4861 hunkfile, changeno, nchanges);
4862 if (err)
4863 goto done;
4865 switch (*choice) {
4866 case GOT_PATCH_CHOICE_YES:
4867 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4868 end_old, start_new, end_new, outfile, rejectfile);
4869 break;
4870 case GOT_PATCH_CHOICE_NO:
4871 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4872 end_old, start_new, end_new, rejectfile, outfile);
4873 break;
4874 case GOT_PATCH_CHOICE_QUIT:
4875 break;
4876 default:
4877 err = got_error(GOT_ERR_PATCH_CHOICE);
4878 break;
4880 done:
4881 diff_output_unidiff_state_free(diff_state);
4882 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4883 err = got_error_from_errno("fclose");
4884 return err;
4887 struct revert_file_args {
4888 struct got_worktree *worktree;
4889 struct got_fileindex *fileindex;
4890 got_worktree_checkout_cb progress_cb;
4891 void *progress_arg;
4892 got_worktree_patch_cb patch_cb;
4893 void *patch_arg;
4894 struct got_repository *repo;
4895 int unlink_added_files;
4896 struct got_pathlist_head *added_files_to_unlink;
4899 static const struct got_error *
4900 create_patched_content(char **path_outfile, int reverse_patch,
4901 struct got_object_id *blob_id, const char *path2,
4902 int dirfd2, const char *de_name2,
4903 const char *relpath, struct got_repository *repo,
4904 got_worktree_patch_cb patch_cb, void *patch_arg)
4906 const struct got_error *err, *free_err;
4907 struct got_blob_object *blob = NULL;
4908 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4909 int fd = -1, fd2 = -1;
4910 char link_target[PATH_MAX];
4911 ssize_t link_len = 0;
4912 char *path1 = NULL, *id_str = NULL;
4913 struct stat sb2;
4914 struct got_diffreg_result *diffreg_result = NULL;
4915 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4916 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4918 *path_outfile = NULL;
4920 err = got_object_id_str(&id_str, blob_id);
4921 if (err)
4922 return err;
4924 if (dirfd2 != -1) {
4925 fd2 = openat(dirfd2, de_name2,
4926 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4927 if (fd2 == -1) {
4928 if (!got_err_open_nofollow_on_symlink()) {
4929 err = got_error_from_errno2("openat", path2);
4930 goto done;
4932 link_len = readlinkat(dirfd2, de_name2,
4933 link_target, sizeof(link_target));
4934 if (link_len == -1) {
4935 return got_error_from_errno2("readlinkat",
4936 path2);
4938 sb2.st_mode = S_IFLNK;
4939 sb2.st_size = link_len;
4941 } else {
4942 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4943 if (fd2 == -1) {
4944 if (!got_err_open_nofollow_on_symlink()) {
4945 err = got_error_from_errno2("open", path2);
4946 goto done;
4948 link_len = readlink(path2, link_target,
4949 sizeof(link_target));
4950 if (link_len == -1)
4951 return got_error_from_errno2("readlink", path2);
4952 sb2.st_mode = S_IFLNK;
4953 sb2.st_size = link_len;
4956 if (fd2 != -1) {
4957 if (fstat(fd2, &sb2) == -1) {
4958 err = got_error_from_errno2("fstat", path2);
4959 goto done;
4962 f2 = fdopen(fd2, "r");
4963 if (f2 == NULL) {
4964 err = got_error_from_errno2("fdopen", path2);
4965 goto done;
4967 fd2 = -1;
4968 } else {
4969 size_t n;
4970 f2 = got_opentemp();
4971 if (f2 == NULL) {
4972 err = got_error_from_errno2("got_opentemp", path2);
4973 goto done;
4975 n = fwrite(link_target, 1, link_len, f2);
4976 if (n != link_len) {
4977 err = got_ferror(f2, GOT_ERR_IO);
4978 goto done;
4980 if (fflush(f2) == EOF) {
4981 err = got_error_from_errno("fflush");
4982 goto done;
4984 rewind(f2);
4987 fd = got_opentempfd();
4988 if (fd == -1) {
4989 err = got_error_from_errno("got_opentempfd");
4990 goto done;
4993 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4994 if (err)
4995 goto done;
4997 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4998 if (err)
4999 goto done;
5001 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5002 if (err)
5003 goto done;
5005 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5006 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5007 if (err)
5008 goto done;
5010 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5011 "");
5012 if (err)
5013 goto done;
5015 if (fseek(f1, 0L, SEEK_SET) == -1)
5016 return got_ferror(f1, GOT_ERR_IO);
5017 if (fseek(f2, 0L, SEEK_SET) == -1)
5018 return got_ferror(f2, GOT_ERR_IO);
5020 /* Count the number of actual changes in the diff result. */
5021 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5022 struct diff_chunk_context cc = {};
5023 diff_chunk_context_load_change(&cc, &nchunks_used,
5024 diffreg_result->result, n, 0);
5025 nchanges++;
5027 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5028 int choice;
5029 err = apply_or_reject_change(&choice, &nchunks_used,
5030 diffreg_result->result, n, relpath, f1, f2,
5031 &line_cur1, &line_cur2,
5032 reverse_patch ? NULL : outfile,
5033 reverse_patch ? outfile : NULL,
5034 ++i, nchanges, patch_cb, patch_arg);
5035 if (err)
5036 goto done;
5037 if (choice == GOT_PATCH_CHOICE_YES)
5038 have_content = 1;
5039 else if (choice == GOT_PATCH_CHOICE_QUIT)
5040 break;
5042 if (have_content) {
5043 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5044 reverse_patch ? NULL : outfile,
5045 reverse_patch ? outfile : NULL);
5046 if (err)
5047 goto done;
5049 if (!S_ISLNK(sb2.st_mode)) {
5050 mode_t mode;
5052 mode = apply_umask(sb2.st_mode);
5053 if (fchmod(fileno(outfile), mode) == -1) {
5054 err = got_error_from_errno2("fchmod", path2);
5055 goto done;
5059 done:
5060 free(id_str);
5061 if (fd != -1 && close(fd) == -1 && err == NULL)
5062 err = got_error_from_errno("close");
5063 if (blob)
5064 got_object_blob_close(blob);
5065 free_err = got_diffreg_result_free(diffreg_result);
5066 if (err == NULL)
5067 err = free_err;
5068 if (f1 && fclose(f1) == EOF && err == NULL)
5069 err = got_error_from_errno2("fclose", path1);
5070 if (f2 && fclose(f2) == EOF && err == NULL)
5071 err = got_error_from_errno2("fclose", path2);
5072 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5073 err = got_error_from_errno2("close", path2);
5074 if (outfile && fclose(outfile) == EOF && err == NULL)
5075 err = got_error_from_errno2("fclose", *path_outfile);
5076 if (path1 && unlink(path1) == -1 && err == NULL)
5077 err = got_error_from_errno2("unlink", path1);
5078 if (err || !have_content) {
5079 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5080 err = got_error_from_errno2("unlink", *path_outfile);
5081 free(*path_outfile);
5082 *path_outfile = NULL;
5084 free(path1);
5085 return err;
5088 static const struct got_error *
5089 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5090 const char *relpath, struct got_object_id *blob_id,
5091 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5092 int dirfd, const char *de_name)
5094 struct revert_file_args *a = arg;
5095 const struct got_error *err = NULL;
5096 char *parent_path = NULL;
5097 struct got_fileindex_entry *ie;
5098 struct got_commit_object *base_commit = NULL;
5099 struct got_tree_object *tree = NULL;
5100 struct got_object_id *tree_id = NULL;
5101 const struct got_tree_entry *te = NULL;
5102 char *tree_path = NULL, *te_name;
5103 char *ondisk_path = NULL, *path_content = NULL;
5104 struct got_blob_object *blob = NULL;
5105 int fd = -1;
5107 /* Reverting a staged deletion is a no-op. */
5108 if (status == GOT_STATUS_DELETE &&
5109 staged_status != GOT_STATUS_NO_CHANGE)
5110 return NULL;
5112 if (status == GOT_STATUS_UNVERSIONED)
5113 return (*a->progress_cb)(a->progress_arg,
5114 GOT_STATUS_UNVERSIONED, relpath);
5116 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5117 if (ie == NULL)
5118 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5120 /* Construct in-repository path of tree which contains this blob. */
5121 err = got_path_dirname(&parent_path, ie->path);
5122 if (err) {
5123 if (err->code != GOT_ERR_BAD_PATH)
5124 goto done;
5125 parent_path = strdup("/");
5126 if (parent_path == NULL) {
5127 err = got_error_from_errno("strdup");
5128 goto done;
5131 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5132 tree_path = strdup(parent_path);
5133 if (tree_path == NULL) {
5134 err = got_error_from_errno("strdup");
5135 goto done;
5137 } else {
5138 if (got_path_is_root_dir(parent_path)) {
5139 tree_path = strdup(a->worktree->path_prefix);
5140 if (tree_path == NULL) {
5141 err = got_error_from_errno("strdup");
5142 goto done;
5144 } else {
5145 if (asprintf(&tree_path, "%s/%s",
5146 a->worktree->path_prefix, parent_path) == -1) {
5147 err = got_error_from_errno("asprintf");
5148 goto done;
5153 err = got_object_open_as_commit(&base_commit, a->repo,
5154 a->worktree->base_commit_id);
5155 if (err)
5156 goto done;
5158 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5159 if (err) {
5160 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5161 (status == GOT_STATUS_ADD ||
5162 staged_status == GOT_STATUS_ADD)))
5163 goto done;
5164 } else {
5165 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5166 if (err)
5167 goto done;
5169 err = got_path_basename(&te_name, ie->path);
5170 if (err)
5171 goto done;
5173 te = got_object_tree_find_entry(tree, te_name);
5174 free(te_name);
5175 if (te == NULL && status != GOT_STATUS_ADD &&
5176 staged_status != GOT_STATUS_ADD) {
5177 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5178 goto done;
5182 switch (status) {
5183 case GOT_STATUS_ADD:
5184 if (a->patch_cb) {
5185 int choice = GOT_PATCH_CHOICE_NONE;
5186 err = (*a->patch_cb)(&choice, a->patch_arg,
5187 status, ie->path, NULL, 1, 1);
5188 if (err)
5189 goto done;
5190 if (choice != GOT_PATCH_CHOICE_YES)
5191 break;
5193 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5194 ie->path);
5195 if (err)
5196 goto done;
5197 got_fileindex_entry_remove(a->fileindex, ie);
5198 if (a->unlink_added_files) {
5199 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5201 if (a->added_files_to_unlink) {
5202 struct got_pathlist_entry *pe;
5204 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5205 entry) {
5206 if (got_path_cmp(pe->path, relpath,
5207 pe->path_len, strlen(relpath)))
5208 continue;
5209 do_unlink = 1;
5210 break;
5214 if (do_unlink) {
5215 if (asprintf(&ondisk_path, "%s/%s",
5216 got_worktree_get_root_path(a->worktree),
5217 relpath) == -1) {
5218 err = got_error_from_errno("asprintf");
5219 goto done;
5221 if (unlink(ondisk_path) == -1) {
5222 err = got_error_from_errno2("unlink",
5223 ondisk_path);
5224 break;
5228 break;
5229 case GOT_STATUS_DELETE:
5230 if (a->patch_cb) {
5231 int choice = GOT_PATCH_CHOICE_NONE;
5232 err = (*a->patch_cb)(&choice, a->patch_arg,
5233 status, ie->path, NULL, 1, 1);
5234 if (err)
5235 goto done;
5236 if (choice != GOT_PATCH_CHOICE_YES)
5237 break;
5239 /* fall through */
5240 case GOT_STATUS_MODIFY:
5241 case GOT_STATUS_MODE_CHANGE:
5242 case GOT_STATUS_CONFLICT:
5243 case GOT_STATUS_MISSING: {
5244 struct got_object_id id;
5245 if (staged_status == GOT_STATUS_ADD ||
5246 staged_status == GOT_STATUS_MODIFY)
5247 got_fileindex_entry_get_staged_blob_id(&id, ie);
5248 else
5249 got_fileindex_entry_get_blob_id(&id, ie);
5250 fd = got_opentempfd();
5251 if (fd == -1) {
5252 err = got_error_from_errno("got_opentempfd");
5253 goto done;
5256 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5257 if (err)
5258 goto done;
5260 if (asprintf(&ondisk_path, "%s/%s",
5261 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5262 err = got_error_from_errno("asprintf");
5263 goto done;
5266 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5267 status == GOT_STATUS_CONFLICT)) {
5268 int is_bad_symlink = 0;
5269 err = create_patched_content(&path_content, 1, &id,
5270 ondisk_path, dirfd, de_name, ie->path, a->repo,
5271 a->patch_cb, a->patch_arg);
5272 if (err || path_content == NULL)
5273 break;
5274 if (te && S_ISLNK(te->mode)) {
5275 if (unlink(path_content) == -1) {
5276 err = got_error_from_errno2("unlink",
5277 path_content);
5278 break;
5280 err = install_symlink(&is_bad_symlink,
5281 a->worktree, ondisk_path, ie->path,
5282 blob, 0, 1, 0, 0, a->repo,
5283 a->progress_cb, a->progress_arg);
5284 } else {
5285 if (rename(path_content, ondisk_path) == -1) {
5286 err = got_error_from_errno3("rename",
5287 path_content, ondisk_path);
5288 goto done;
5291 } else {
5292 int is_bad_symlink = 0;
5293 if (te && S_ISLNK(te->mode)) {
5294 err = install_symlink(&is_bad_symlink,
5295 a->worktree, ondisk_path, ie->path,
5296 blob, 0, 1, 0, 0, a->repo,
5297 a->progress_cb, a->progress_arg);
5298 } else {
5299 err = install_blob(a->worktree, ondisk_path,
5300 ie->path,
5301 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5302 got_fileindex_perms_to_st(ie), blob,
5303 0, 1, 0, 0, NULL, a->repo,
5304 a->progress_cb, a->progress_arg);
5306 if (err)
5307 goto done;
5308 if (status == GOT_STATUS_DELETE ||
5309 status == GOT_STATUS_MODE_CHANGE) {
5310 err = got_fileindex_entry_update(ie,
5311 a->worktree->root_fd, relpath,
5312 blob->id.sha1,
5313 a->worktree->base_commit_id->sha1, 1);
5314 if (err)
5315 goto done;
5317 if (is_bad_symlink) {
5318 got_fileindex_entry_filetype_set(ie,
5319 GOT_FILEIDX_MODE_BAD_SYMLINK);
5322 break;
5324 default:
5325 break;
5327 done:
5328 free(ondisk_path);
5329 free(path_content);
5330 free(parent_path);
5331 free(tree_path);
5332 if (fd != -1 && close(fd) == -1 && err == NULL)
5333 err = got_error_from_errno("close");
5334 if (blob)
5335 got_object_blob_close(blob);
5336 if (tree)
5337 got_object_tree_close(tree);
5338 free(tree_id);
5339 if (base_commit)
5340 got_object_commit_close(base_commit);
5341 return err;
5344 const struct got_error *
5345 got_worktree_revert(struct got_worktree *worktree,
5346 struct got_pathlist_head *paths,
5347 got_worktree_checkout_cb progress_cb, void *progress_arg,
5348 got_worktree_patch_cb patch_cb, void *patch_arg,
5349 struct got_repository *repo)
5351 struct got_fileindex *fileindex = NULL;
5352 char *fileindex_path = NULL;
5353 const struct got_error *err = NULL, *unlockerr = NULL;
5354 const struct got_error *sync_err = NULL;
5355 struct got_pathlist_entry *pe;
5356 struct revert_file_args rfa;
5358 err = lock_worktree(worktree, LOCK_EX);
5359 if (err)
5360 return err;
5362 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5363 if (err)
5364 goto done;
5366 rfa.worktree = worktree;
5367 rfa.fileindex = fileindex;
5368 rfa.progress_cb = progress_cb;
5369 rfa.progress_arg = progress_arg;
5370 rfa.patch_cb = patch_cb;
5371 rfa.patch_arg = patch_arg;
5372 rfa.repo = repo;
5373 rfa.unlink_added_files = 0;
5374 TAILQ_FOREACH(pe, paths, entry) {
5375 err = worktree_status(worktree, pe->path, fileindex, repo,
5376 revert_file, &rfa, NULL, NULL, 1, 0);
5377 if (err)
5378 break;
5380 sync_err = sync_fileindex(fileindex, fileindex_path);
5381 if (sync_err && err == NULL)
5382 err = sync_err;
5383 done:
5384 free(fileindex_path);
5385 if (fileindex)
5386 got_fileindex_free(fileindex);
5387 unlockerr = lock_worktree(worktree, LOCK_SH);
5388 if (unlockerr && err == NULL)
5389 err = unlockerr;
5390 return err;
5393 static void
5394 free_commitable(struct got_commitable *ct)
5396 free(ct->path);
5397 free(ct->in_repo_path);
5398 free(ct->ondisk_path);
5399 free(ct->blob_id);
5400 free(ct->base_blob_id);
5401 free(ct->staged_blob_id);
5402 free(ct->base_commit_id);
5403 free(ct);
5406 struct collect_commitables_arg {
5407 struct got_pathlist_head *commitable_paths;
5408 struct got_repository *repo;
5409 struct got_worktree *worktree;
5410 struct got_fileindex *fileindex;
5411 int have_staged_files;
5412 int allow_bad_symlinks;
5413 int diff_header_shown;
5414 int commit_conflicts;
5415 FILE *diff_outfile;
5416 FILE *f1;
5417 FILE *f2;
5421 * Create a file which contains the target path of a symlink so we can feed
5422 * it as content to the diff engine.
5424 static const struct got_error *
5425 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5426 const char *abspath)
5428 const struct got_error *err = NULL;
5429 char target_path[PATH_MAX];
5430 ssize_t target_len, outlen;
5432 *fd = -1;
5434 if (dirfd != -1) {
5435 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5436 if (target_len == -1)
5437 return got_error_from_errno2("readlinkat", abspath);
5438 } else {
5439 target_len = readlink(abspath, target_path, PATH_MAX);
5440 if (target_len == -1)
5441 return got_error_from_errno2("readlink", abspath);
5444 *fd = got_opentempfd();
5445 if (*fd == -1)
5446 return got_error_from_errno("got_opentempfd");
5448 outlen = write(*fd, target_path, target_len);
5449 if (outlen == -1) {
5450 err = got_error_from_errno("got_opentempfd");
5451 goto done;
5454 if (lseek(*fd, 0, SEEK_SET) == -1) {
5455 err = got_error_from_errno2("lseek", abspath);
5456 goto done;
5458 done:
5459 if (err) {
5460 close(*fd);
5461 *fd = -1;
5463 return err;
5466 static const struct got_error *
5467 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5468 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5469 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5471 const struct got_error *err = NULL;
5472 struct got_blob_object *blob1 = NULL;
5473 int fd = -1, fd1 = -1, fd2 = -1;
5474 FILE *ondisk_file = NULL;
5475 char *label1 = NULL;
5476 struct stat sb;
5477 off_t size1 = 0;
5478 int f2_exists = 0;
5479 char *id_str = NULL;
5481 memset(&sb, 0, sizeof(sb));
5483 if (diff_staged) {
5484 if (ct->staged_status != GOT_STATUS_MODIFY &&
5485 ct->staged_status != GOT_STATUS_ADD &&
5486 ct->staged_status != GOT_STATUS_DELETE)
5487 return NULL;
5488 } else {
5489 if (ct->status != GOT_STATUS_MODIFY &&
5490 ct->status != GOT_STATUS_ADD &&
5491 ct->status != GOT_STATUS_DELETE &&
5492 ct->status != GOT_STATUS_CONFLICT)
5493 return NULL;
5496 err = got_opentemp_truncate(f1);
5497 if (err)
5498 return got_error_from_errno("got_opentemp_truncate");
5499 err = got_opentemp_truncate(f2);
5500 if (err)
5501 return got_error_from_errno("got_opentemp_truncate");
5503 if (!*diff_header_shown) {
5504 err = got_object_id_str(&id_str, worktree->base_commit_id);
5505 if (err)
5506 return err;
5507 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5508 got_worktree_get_root_path(worktree));
5509 fprintf(diff_outfile, "commit - %s\n", id_str);
5510 fprintf(diff_outfile, "path + %s%s\n",
5511 got_worktree_get_root_path(worktree),
5512 diff_staged ? " (staged changes)" : "");
5513 *diff_header_shown = 1;
5516 if (diff_staged) {
5517 const char *label1 = NULL, *label2 = NULL;
5518 switch (ct->staged_status) {
5519 case GOT_STATUS_MODIFY:
5520 label1 = ct->path;
5521 label2 = ct->path;
5522 break;
5523 case GOT_STATUS_ADD:
5524 label2 = ct->path;
5525 break;
5526 case GOT_STATUS_DELETE:
5527 label1 = ct->path;
5528 break;
5529 default:
5530 return got_error(GOT_ERR_FILE_STATUS);
5532 fd1 = got_opentempfd();
5533 if (fd1 == -1) {
5534 err = got_error_from_errno("got_opentempfd");
5535 goto done;
5537 fd2 = got_opentempfd();
5538 if (fd2 == -1) {
5539 err = got_error_from_errno("got_opentempfd");
5540 goto done;
5542 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5543 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5544 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5545 NULL, repo, diff_outfile);
5546 goto done;
5549 fd1 = got_opentempfd();
5550 if (fd1 == -1) {
5551 err = got_error_from_errno("got_opentempfd");
5552 goto done;
5555 if (ct->status != GOT_STATUS_ADD) {
5556 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5557 8192, fd1);
5558 if (err)
5559 goto done;
5562 if (ct->status != GOT_STATUS_DELETE) {
5563 if (dirfd != -1) {
5564 fd = openat(dirfd, de_name,
5565 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5566 if (fd == -1) {
5567 if (!got_err_open_nofollow_on_symlink()) {
5568 err = got_error_from_errno2("openat",
5569 ct->ondisk_path);
5570 goto done;
5572 err = get_symlink_target_file(&fd, dirfd,
5573 de_name, ct->ondisk_path);
5574 if (err)
5575 goto done;
5577 } else {
5578 fd = open(ct->ondisk_path,
5579 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5580 if (fd == -1) {
5581 if (!got_err_open_nofollow_on_symlink()) {
5582 err = got_error_from_errno2("open",
5583 ct->ondisk_path);
5584 goto done;
5586 err = get_symlink_target_file(&fd, dirfd,
5587 de_name, ct->ondisk_path);
5588 if (err)
5589 goto done;
5592 if (fstatat(fd, ct->ondisk_path, &sb,
5593 AT_SYMLINK_NOFOLLOW) == -1) {
5594 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5595 goto done;
5597 ondisk_file = fdopen(fd, "r");
5598 if (ondisk_file == NULL) {
5599 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5600 goto done;
5602 fd = -1;
5603 f2_exists = 1;
5606 if (blob1) {
5607 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5608 f1, blob1);
5609 if (err)
5610 goto done;
5613 err = got_diff_blob_file(blob1, f1, size1, label1,
5614 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5615 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5616 done:
5617 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5618 err = got_error_from_errno("close");
5619 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5620 err = got_error_from_errno("close");
5621 if (blob1)
5622 got_object_blob_close(blob1);
5623 if (fd != -1 && close(fd) == -1 && err == NULL)
5624 err = got_error_from_errno("close");
5625 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5626 err = got_error_from_errno("fclose");
5627 return err;
5630 static const struct got_error *
5631 collect_commitables(void *arg, unsigned char status,
5632 unsigned char staged_status, const char *relpath,
5633 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5634 struct got_object_id *commit_id, int dirfd, const char *de_name)
5636 struct collect_commitables_arg *a = arg;
5637 const struct got_error *err = NULL;
5638 struct got_commitable *ct = NULL;
5639 struct got_pathlist_entry *new = NULL;
5640 char *parent_path = NULL, *path = NULL;
5641 struct stat sb;
5643 if (a->have_staged_files) {
5644 if (staged_status != GOT_STATUS_MODIFY &&
5645 staged_status != GOT_STATUS_ADD &&
5646 staged_status != GOT_STATUS_DELETE)
5647 return NULL;
5648 } else {
5649 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5650 printf("C %s\n", relpath);
5651 return got_error(GOT_ERR_COMMIT_CONFLICT);
5654 if (status != GOT_STATUS_MODIFY &&
5655 status != GOT_STATUS_MODE_CHANGE &&
5656 status != GOT_STATUS_ADD &&
5657 status != GOT_STATUS_DELETE &&
5658 status != GOT_STATUS_CONFLICT)
5659 return NULL;
5662 if (asprintf(&path, "/%s", relpath) == -1) {
5663 err = got_error_from_errno("asprintf");
5664 goto done;
5666 if (strcmp(path, "/") == 0) {
5667 parent_path = strdup("");
5668 if (parent_path == NULL)
5669 return got_error_from_errno("strdup");
5670 } else {
5671 err = got_path_dirname(&parent_path, path);
5672 if (err)
5673 return err;
5676 ct = calloc(1, sizeof(*ct));
5677 if (ct == NULL) {
5678 err = got_error_from_errno("calloc");
5679 goto done;
5682 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5683 relpath) == -1) {
5684 err = got_error_from_errno("asprintf");
5685 goto done;
5688 if (staged_status == GOT_STATUS_ADD ||
5689 staged_status == GOT_STATUS_MODIFY) {
5690 struct got_fileindex_entry *ie;
5691 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5692 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5693 case GOT_FILEIDX_MODE_REGULAR_FILE:
5694 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5695 ct->mode = S_IFREG;
5696 break;
5697 case GOT_FILEIDX_MODE_SYMLINK:
5698 ct->mode = S_IFLNK;
5699 break;
5700 default:
5701 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5702 goto done;
5704 ct->mode |= got_fileindex_entry_perms_get(ie);
5705 } else if (status != GOT_STATUS_DELETE &&
5706 staged_status != GOT_STATUS_DELETE) {
5707 if (dirfd != -1) {
5708 if (fstatat(dirfd, de_name, &sb,
5709 AT_SYMLINK_NOFOLLOW) == -1) {
5710 err = got_error_from_errno2("fstatat",
5711 ct->ondisk_path);
5712 goto done;
5714 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5715 err = got_error_from_errno2("lstat", ct->ondisk_path);
5716 goto done;
5718 ct->mode = sb.st_mode;
5721 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5722 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5723 relpath) == -1) {
5724 err = got_error_from_errno("asprintf");
5725 goto done;
5728 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5729 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5730 int is_bad_symlink;
5731 char target_path[PATH_MAX];
5732 ssize_t target_len;
5733 target_len = readlink(ct->ondisk_path, target_path,
5734 sizeof(target_path));
5735 if (target_len == -1) {
5736 err = got_error_from_errno2("readlink",
5737 ct->ondisk_path);
5738 goto done;
5740 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5741 target_len, ct->ondisk_path, a->worktree->root_path,
5742 a->worktree->meta_dir);
5743 if (err)
5744 goto done;
5745 if (is_bad_symlink) {
5746 err = got_error_path(ct->ondisk_path,
5747 GOT_ERR_BAD_SYMLINK);
5748 goto done;
5753 ct->status = status;
5754 ct->staged_status = staged_status;
5755 ct->blob_id = NULL; /* will be filled in when blob gets created */
5756 if (ct->status != GOT_STATUS_ADD &&
5757 ct->staged_status != GOT_STATUS_ADD) {
5758 ct->base_blob_id = got_object_id_dup(blob_id);
5759 if (ct->base_blob_id == NULL) {
5760 err = got_error_from_errno("got_object_id_dup");
5761 goto done;
5763 ct->base_commit_id = got_object_id_dup(commit_id);
5764 if (ct->base_commit_id == NULL) {
5765 err = got_error_from_errno("got_object_id_dup");
5766 goto done;
5769 if (ct->staged_status == GOT_STATUS_ADD ||
5770 ct->staged_status == GOT_STATUS_MODIFY) {
5771 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5772 if (ct->staged_blob_id == NULL) {
5773 err = got_error_from_errno("got_object_id_dup");
5774 goto done;
5777 ct->path = strdup(path);
5778 if (ct->path == NULL) {
5779 err = got_error_from_errno("strdup");
5780 goto done;
5783 if (a->diff_outfile) {
5784 err = append_ct_diff(ct, &a->diff_header_shown,
5785 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5786 a->have_staged_files, a->repo, a->worktree);
5787 if (err)
5788 goto done;
5791 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5792 done:
5793 if (ct && (err || new == NULL))
5794 free_commitable(ct);
5795 free(parent_path);
5796 free(path);
5797 return err;
5800 static const struct got_error *write_tree(struct got_object_id **, int *,
5801 struct got_tree_object *, const char *, struct got_pathlist_head *,
5802 got_worktree_status_cb status_cb, void *status_arg,
5803 struct got_repository *);
5805 static const struct got_error *
5806 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5807 struct got_tree_entry *te, const char *parent_path,
5808 struct got_pathlist_head *commitable_paths,
5809 got_worktree_status_cb status_cb, void *status_arg,
5810 struct got_repository *repo)
5812 const struct got_error *err = NULL;
5813 struct got_tree_object *subtree;
5814 char *subpath;
5816 if (asprintf(&subpath, "%s%s%s", parent_path,
5817 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5818 return got_error_from_errno("asprintf");
5820 err = got_object_open_as_tree(&subtree, repo, &te->id);
5821 if (err)
5822 return err;
5824 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5825 commitable_paths, status_cb, status_arg, repo);
5826 got_object_tree_close(subtree);
5827 free(subpath);
5828 return err;
5831 static const struct got_error *
5832 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5834 const struct got_error *err = NULL;
5835 char *ct_parent_path = NULL;
5837 *match = 0;
5839 if (strchr(ct->in_repo_path, '/') == NULL) {
5840 *match = got_path_is_root_dir(path);
5841 return NULL;
5844 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5845 if (err)
5846 return err;
5847 *match = (strcmp(path, ct_parent_path) == 0);
5848 free(ct_parent_path);
5849 return err;
5852 static mode_t
5853 get_ct_file_mode(struct got_commitable *ct)
5855 if (S_ISLNK(ct->mode))
5856 return S_IFLNK;
5858 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5861 static const struct got_error *
5862 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5863 struct got_tree_entry *te, struct got_commitable *ct)
5865 const struct got_error *err = NULL;
5867 *new_te = NULL;
5869 err = got_object_tree_entry_dup(new_te, te);
5870 if (err)
5871 goto done;
5873 (*new_te)->mode = get_ct_file_mode(ct);
5875 if (ct->staged_status == GOT_STATUS_MODIFY)
5876 memcpy(&(*new_te)->id, ct->staged_blob_id,
5877 sizeof((*new_te)->id));
5878 else
5879 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5880 done:
5881 if (err && *new_te) {
5882 free(*new_te);
5883 *new_te = NULL;
5885 return err;
5888 static const struct got_error *
5889 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5890 struct got_commitable *ct)
5892 const struct got_error *err = NULL;
5893 char *ct_name = NULL;
5895 *new_te = NULL;
5897 *new_te = calloc(1, sizeof(**new_te));
5898 if (*new_te == NULL)
5899 return got_error_from_errno("calloc");
5901 err = got_path_basename(&ct_name, ct->path);
5902 if (err)
5903 goto done;
5904 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5905 sizeof((*new_te)->name)) {
5906 err = got_error(GOT_ERR_NO_SPACE);
5907 goto done;
5910 (*new_te)->mode = get_ct_file_mode(ct);
5912 if (ct->staged_status == GOT_STATUS_ADD)
5913 memcpy(&(*new_te)->id, ct->staged_blob_id,
5914 sizeof((*new_te)->id));
5915 else
5916 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5917 done:
5918 free(ct_name);
5919 if (err && *new_te) {
5920 free(*new_te);
5921 *new_te = NULL;
5923 return err;
5926 static const struct got_error *
5927 insert_tree_entry(struct got_tree_entry *new_te,
5928 struct got_pathlist_head *paths)
5930 const struct got_error *err = NULL;
5931 struct got_pathlist_entry *new_pe;
5933 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5934 if (err)
5935 return err;
5936 if (new_pe == NULL)
5937 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5938 return NULL;
5941 static const struct got_error *
5942 report_ct_status(struct got_commitable *ct,
5943 got_worktree_status_cb status_cb, void *status_arg)
5945 const char *ct_path = ct->path;
5946 unsigned char status;
5948 if (status_cb == NULL) /* no commit progress output desired */
5949 return NULL;
5951 while (ct_path[0] == '/')
5952 ct_path++;
5954 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5955 status = ct->staged_status;
5956 else
5957 status = ct->status;
5959 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5960 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5963 static const struct got_error *
5964 match_modified_subtree(int *modified, struct got_tree_entry *te,
5965 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5967 const struct got_error *err = NULL;
5968 struct got_pathlist_entry *pe;
5969 char *te_path;
5971 *modified = 0;
5973 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5974 got_path_is_root_dir(base_tree_path) ? "" : "/",
5975 te->name) == -1)
5976 return got_error_from_errno("asprintf");
5978 TAILQ_FOREACH(pe, commitable_paths, entry) {
5979 struct got_commitable *ct = pe->data;
5980 *modified = got_path_is_child(ct->in_repo_path, te_path,
5981 strlen(te_path));
5982 if (*modified)
5983 break;
5986 free(te_path);
5987 return err;
5990 static const struct got_error *
5991 match_deleted_or_modified_ct(struct got_commitable **ctp,
5992 struct got_tree_entry *te, const char *base_tree_path,
5993 struct got_pathlist_head *commitable_paths)
5995 const struct got_error *err = NULL;
5996 struct got_pathlist_entry *pe;
5998 *ctp = NULL;
6000 TAILQ_FOREACH(pe, commitable_paths, entry) {
6001 struct got_commitable *ct = pe->data;
6002 char *ct_name = NULL;
6003 int path_matches;
6005 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6006 if (ct->status != GOT_STATUS_MODIFY &&
6007 ct->status != GOT_STATUS_MODE_CHANGE &&
6008 ct->status != GOT_STATUS_DELETE &&
6009 ct->status != GOT_STATUS_CONFLICT)
6010 continue;
6011 } else {
6012 if (ct->staged_status != GOT_STATUS_MODIFY &&
6013 ct->staged_status != GOT_STATUS_DELETE)
6014 continue;
6017 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6018 continue;
6020 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6021 if (err)
6022 return err;
6023 if (!path_matches)
6024 continue;
6026 err = got_path_basename(&ct_name, pe->path);
6027 if (err)
6028 return err;
6030 if (strcmp(te->name, ct_name) != 0) {
6031 free(ct_name);
6032 continue;
6034 free(ct_name);
6036 *ctp = ct;
6037 break;
6040 return err;
6043 static const struct got_error *
6044 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6045 const char *child_path, const char *path_base_tree,
6046 struct got_pathlist_head *commitable_paths,
6047 got_worktree_status_cb status_cb, void *status_arg,
6048 struct got_repository *repo)
6050 const struct got_error *err = NULL;
6051 struct got_tree_entry *new_te;
6052 char *subtree_path;
6053 struct got_object_id *id = NULL;
6054 int nentries;
6056 *new_tep = NULL;
6058 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6059 got_path_is_root_dir(path_base_tree) ? "" : "/",
6060 child_path) == -1)
6061 return got_error_from_errno("asprintf");
6063 new_te = calloc(1, sizeof(*new_te));
6064 if (new_te == NULL)
6065 return got_error_from_errno("calloc");
6066 new_te->mode = S_IFDIR;
6068 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6069 sizeof(new_te->name)) {
6070 err = got_error(GOT_ERR_NO_SPACE);
6071 goto done;
6073 err = write_tree(&id, &nentries, NULL, subtree_path,
6074 commitable_paths, status_cb, status_arg, repo);
6075 if (err) {
6076 free(new_te);
6077 goto done;
6079 memcpy(&new_te->id, id, sizeof(new_te->id));
6080 done:
6081 free(id);
6082 free(subtree_path);
6083 if (err == NULL)
6084 *new_tep = new_te;
6085 return err;
6088 static const struct got_error *
6089 write_tree(struct got_object_id **new_tree_id, int *nentries,
6090 struct got_tree_object *base_tree, const char *path_base_tree,
6091 struct got_pathlist_head *commitable_paths,
6092 got_worktree_status_cb status_cb, void *status_arg,
6093 struct got_repository *repo)
6095 const struct got_error *err = NULL;
6096 struct got_pathlist_head paths;
6097 struct got_tree_entry *te, *new_te = NULL;
6098 struct got_pathlist_entry *pe;
6100 TAILQ_INIT(&paths);
6101 *nentries = 0;
6103 /* Insert, and recurse into, newly added entries first. */
6104 TAILQ_FOREACH(pe, commitable_paths, entry) {
6105 struct got_commitable *ct = pe->data;
6106 char *child_path = NULL, *slash;
6108 if ((ct->status != GOT_STATUS_ADD &&
6109 ct->staged_status != GOT_STATUS_ADD) ||
6110 (ct->flags & GOT_COMMITABLE_ADDED))
6111 continue;
6113 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6114 strlen(path_base_tree)))
6115 continue;
6117 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6118 ct->in_repo_path);
6119 if (err)
6120 goto done;
6122 slash = strchr(child_path, '/');
6123 if (slash == NULL) {
6124 err = alloc_added_blob_tree_entry(&new_te, ct);
6125 if (err)
6126 goto done;
6127 err = report_ct_status(ct, status_cb, status_arg);
6128 if (err)
6129 goto done;
6130 ct->flags |= GOT_COMMITABLE_ADDED;
6131 err = insert_tree_entry(new_te, &paths);
6132 if (err)
6133 goto done;
6134 (*nentries)++;
6135 } else {
6136 *slash = '\0'; /* trim trailing path components */
6137 if (base_tree == NULL ||
6138 got_object_tree_find_entry(base_tree, child_path)
6139 == NULL) {
6140 err = make_subtree_for_added_blob(&new_te,
6141 child_path, path_base_tree,
6142 commitable_paths, status_cb, status_arg,
6143 repo);
6144 if (err)
6145 goto done;
6146 err = insert_tree_entry(new_te, &paths);
6147 if (err)
6148 goto done;
6149 (*nentries)++;
6154 if (base_tree) {
6155 int i, nbase_entries;
6156 /* Handle modified and deleted entries. */
6157 nbase_entries = got_object_tree_get_nentries(base_tree);
6158 for (i = 0; i < nbase_entries; i++) {
6159 struct got_commitable *ct = NULL;
6161 te = got_object_tree_get_entry(base_tree, i);
6162 if (got_object_tree_entry_is_submodule(te)) {
6163 /* Entry is a submodule; just copy it. */
6164 err = got_object_tree_entry_dup(&new_te, te);
6165 if (err)
6166 goto done;
6167 err = insert_tree_entry(new_te, &paths);
6168 if (err)
6169 goto done;
6170 (*nentries)++;
6171 continue;
6174 if (S_ISDIR(te->mode)) {
6175 int modified;
6176 err = got_object_tree_entry_dup(&new_te, te);
6177 if (err)
6178 goto done;
6179 err = match_modified_subtree(&modified, te,
6180 path_base_tree, commitable_paths);
6181 if (err)
6182 goto done;
6183 /* Avoid recursion into unmodified subtrees. */
6184 if (modified) {
6185 struct got_object_id *new_id;
6186 int nsubentries;
6187 err = write_subtree(&new_id,
6188 &nsubentries, te,
6189 path_base_tree, commitable_paths,
6190 status_cb, status_arg, repo);
6191 if (err)
6192 goto done;
6193 if (nsubentries == 0) {
6194 /* All entries were deleted. */
6195 free(new_id);
6196 continue;
6198 memcpy(&new_te->id, new_id,
6199 sizeof(new_te->id));
6200 free(new_id);
6202 err = insert_tree_entry(new_te, &paths);
6203 if (err)
6204 goto done;
6205 (*nentries)++;
6206 continue;
6209 err = match_deleted_or_modified_ct(&ct, te,
6210 path_base_tree, commitable_paths);
6211 if (err)
6212 goto done;
6213 if (ct) {
6214 /* NB: Deleted entries get dropped here. */
6215 if (ct->status == GOT_STATUS_MODIFY ||
6216 ct->status == GOT_STATUS_MODE_CHANGE ||
6217 ct->status == GOT_STATUS_CONFLICT ||
6218 ct->staged_status == GOT_STATUS_MODIFY) {
6219 err = alloc_modified_blob_tree_entry(
6220 &new_te, te, ct);
6221 if (err)
6222 goto done;
6223 err = insert_tree_entry(new_te, &paths);
6224 if (err)
6225 goto done;
6226 (*nentries)++;
6228 err = report_ct_status(ct, status_cb,
6229 status_arg);
6230 if (err)
6231 goto done;
6232 } else {
6233 /* Entry is unchanged; just copy it. */
6234 err = got_object_tree_entry_dup(&new_te, te);
6235 if (err)
6236 goto done;
6237 err = insert_tree_entry(new_te, &paths);
6238 if (err)
6239 goto done;
6240 (*nentries)++;
6245 /* Write new list of entries; deleted entries have been dropped. */
6246 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6247 done:
6248 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6249 return err;
6252 static const struct got_error *
6253 update_fileindex_after_commit(struct got_worktree *worktree,
6254 struct got_pathlist_head *commitable_paths,
6255 struct got_object_id *new_base_commit_id,
6256 struct got_fileindex *fileindex, int have_staged_files)
6258 const struct got_error *err = NULL;
6259 struct got_pathlist_entry *pe;
6260 char *relpath = NULL;
6262 TAILQ_FOREACH(pe, commitable_paths, entry) {
6263 struct got_fileindex_entry *ie;
6264 struct got_commitable *ct = pe->data;
6266 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6268 err = got_path_skip_common_ancestor(&relpath,
6269 worktree->root_path, ct->ondisk_path);
6270 if (err)
6271 goto done;
6273 if (ie) {
6274 if (ct->status == GOT_STATUS_DELETE ||
6275 ct->staged_status == GOT_STATUS_DELETE) {
6276 got_fileindex_entry_remove(fileindex, ie);
6277 } else if (ct->staged_status == GOT_STATUS_ADD ||
6278 ct->staged_status == GOT_STATUS_MODIFY) {
6279 got_fileindex_entry_stage_set(ie,
6280 GOT_FILEIDX_STAGE_NONE);
6281 got_fileindex_entry_staged_filetype_set(ie, 0);
6283 err = got_fileindex_entry_update(ie,
6284 worktree->root_fd, relpath,
6285 ct->staged_blob_id->sha1,
6286 new_base_commit_id->sha1,
6287 !have_staged_files);
6288 } else
6289 err = got_fileindex_entry_update(ie,
6290 worktree->root_fd, relpath,
6291 ct->blob_id->sha1,
6292 new_base_commit_id->sha1,
6293 !have_staged_files);
6294 } else {
6295 err = got_fileindex_entry_alloc(&ie, pe->path);
6296 if (err)
6297 goto done;
6298 err = got_fileindex_entry_update(ie,
6299 worktree->root_fd, relpath, ct->blob_id->sha1,
6300 new_base_commit_id->sha1, 1);
6301 if (err) {
6302 got_fileindex_entry_free(ie);
6303 goto done;
6305 err = got_fileindex_entry_add(fileindex, ie);
6306 if (err) {
6307 got_fileindex_entry_free(ie);
6308 goto done;
6311 free(relpath);
6312 relpath = NULL;
6314 done:
6315 free(relpath);
6316 return err;
6320 static const struct got_error *
6321 check_out_of_date(const char *in_repo_path, unsigned char status,
6322 unsigned char staged_status, struct got_object_id *base_blob_id,
6323 struct got_object_id *base_commit_id,
6324 struct got_object_id *head_commit_id, struct got_repository *repo,
6325 int ood_errcode)
6327 const struct got_error *err = NULL;
6328 struct got_commit_object *commit = NULL;
6329 struct got_object_id *id = NULL;
6331 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6332 /* Trivial case: base commit == head commit */
6333 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6334 return NULL;
6336 * Ensure file content which local changes were based
6337 * on matches file content in the branch head.
6339 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6340 if (err)
6341 goto done;
6342 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6343 if (err) {
6344 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6345 err = got_error(ood_errcode);
6346 goto done;
6347 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6348 err = got_error(ood_errcode);
6349 } else {
6350 /* Require that added files don't exist in the branch head. */
6351 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6352 if (err)
6353 goto done;
6354 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6355 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6356 goto done;
6357 err = id ? got_error(ood_errcode) : NULL;
6359 done:
6360 free(id);
6361 if (commit)
6362 got_object_commit_close(commit);
6363 return err;
6366 static const struct got_error *
6367 commit_worktree(struct got_object_id **new_commit_id,
6368 struct got_pathlist_head *commitable_paths,
6369 struct got_object_id *head_commit_id,
6370 struct got_object_id *parent_id2,
6371 struct got_worktree *worktree,
6372 const char *author, const char *committer, char *diff_path,
6373 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6374 got_worktree_status_cb status_cb, void *status_arg,
6375 struct got_repository *repo)
6377 const struct got_error *err = NULL, *unlockerr = NULL;
6378 struct got_pathlist_entry *pe;
6379 const char *head_ref_name = NULL;
6380 struct got_commit_object *head_commit = NULL;
6381 struct got_reference *head_ref2 = NULL;
6382 struct got_object_id *head_commit_id2 = NULL;
6383 struct got_tree_object *head_tree = NULL;
6384 struct got_object_id *new_tree_id = NULL;
6385 int nentries, nparents = 0;
6386 struct got_object_id_queue parent_ids;
6387 struct got_object_qid *pid = NULL;
6388 char *logmsg = NULL;
6389 time_t timestamp;
6391 *new_commit_id = NULL;
6393 STAILQ_INIT(&parent_ids);
6395 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6396 if (err)
6397 goto done;
6399 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6400 if (err)
6401 goto done;
6403 if (commit_msg_cb != NULL) {
6404 err = commit_msg_cb(commitable_paths, diff_path,
6405 &logmsg, commit_arg);
6406 if (err)
6407 goto done;
6410 if (logmsg == NULL || strlen(logmsg) == 0) {
6411 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6412 goto done;
6415 /* Create blobs from added and modified files and record their IDs. */
6416 TAILQ_FOREACH(pe, commitable_paths, entry) {
6417 struct got_commitable *ct = pe->data;
6418 char *ondisk_path;
6420 /* Blobs for staged files already exist. */
6421 if (ct->staged_status == GOT_STATUS_ADD ||
6422 ct->staged_status == GOT_STATUS_MODIFY)
6423 continue;
6425 if (ct->status != GOT_STATUS_ADD &&
6426 ct->status != GOT_STATUS_MODIFY &&
6427 ct->status != GOT_STATUS_MODE_CHANGE &&
6428 ct->status != GOT_STATUS_CONFLICT)
6429 continue;
6431 if (asprintf(&ondisk_path, "%s/%s",
6432 worktree->root_path, pe->path) == -1) {
6433 err = got_error_from_errno("asprintf");
6434 goto done;
6436 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6437 free(ondisk_path);
6438 if (err)
6439 goto done;
6442 /* Recursively write new tree objects. */
6443 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6444 commitable_paths, status_cb, status_arg, repo);
6445 if (err)
6446 goto done;
6448 err = got_object_qid_alloc(&pid, head_commit_id);
6449 if (err)
6450 goto done;
6451 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6452 nparents++;
6453 if (parent_id2) {
6454 err = got_object_qid_alloc(&pid, parent_id2);
6455 if (err)
6456 goto done;
6457 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6458 nparents++;
6460 timestamp = time(NULL);
6461 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6462 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6463 if (logmsg != NULL)
6464 free(logmsg);
6465 if (err)
6466 goto done;
6468 /* Check if a concurrent commit to our branch has occurred. */
6469 head_ref_name = got_worktree_get_head_ref_name(worktree);
6470 if (head_ref_name == NULL) {
6471 err = got_error_from_errno("got_worktree_get_head_ref_name");
6472 goto done;
6474 /* Lock the reference here to prevent concurrent modification. */
6475 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6476 if (err)
6477 goto done;
6478 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6479 if (err)
6480 goto done;
6481 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6482 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6483 goto done;
6485 /* Update branch head in repository. */
6486 err = got_ref_change_ref(head_ref2, *new_commit_id);
6487 if (err)
6488 goto done;
6489 err = got_ref_write(head_ref2, repo);
6490 if (err)
6491 goto done;
6493 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6494 if (err)
6495 goto done;
6497 err = ref_base_commit(worktree, repo);
6498 if (err)
6499 goto done;
6500 done:
6501 got_object_id_queue_free(&parent_ids);
6502 if (head_tree)
6503 got_object_tree_close(head_tree);
6504 if (head_commit)
6505 got_object_commit_close(head_commit);
6506 free(head_commit_id2);
6507 if (head_ref2) {
6508 unlockerr = got_ref_unlock(head_ref2);
6509 if (unlockerr && err == NULL)
6510 err = unlockerr;
6511 got_ref_close(head_ref2);
6513 return err;
6516 static const struct got_error *
6517 check_path_is_commitable(const char *path,
6518 struct got_pathlist_head *commitable_paths)
6520 struct got_pathlist_entry *cpe = NULL;
6521 size_t path_len = strlen(path);
6523 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6524 struct got_commitable *ct = cpe->data;
6525 const char *ct_path = ct->path;
6527 while (ct_path[0] == '/')
6528 ct_path++;
6530 if (strcmp(path, ct_path) == 0 ||
6531 got_path_is_child(ct_path, path, path_len))
6532 break;
6535 if (cpe == NULL)
6536 return got_error_path(path, GOT_ERR_BAD_PATH);
6538 return NULL;
6541 static const struct got_error *
6542 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6544 int *have_staged_files = arg;
6546 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6547 *have_staged_files = 1;
6548 return got_error(GOT_ERR_CANCELLED);
6551 return NULL;
6554 static const struct got_error *
6555 check_non_staged_files(struct got_fileindex *fileindex,
6556 struct got_pathlist_head *paths)
6558 struct got_pathlist_entry *pe;
6559 struct got_fileindex_entry *ie;
6561 TAILQ_FOREACH(pe, paths, entry) {
6562 if (pe->path[0] == '\0')
6563 continue;
6564 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6565 if (ie == NULL)
6566 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6567 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6568 return got_error_path(pe->path,
6569 GOT_ERR_FILE_NOT_STAGED);
6572 return NULL;
6575 const struct got_error *
6576 got_worktree_commit(struct got_object_id **new_commit_id,
6577 struct got_worktree *worktree, struct got_pathlist_head *paths,
6578 const char *author, const char *committer, int allow_bad_symlinks,
6579 int show_diff, int commit_conflicts,
6580 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6581 got_worktree_status_cb status_cb, void *status_arg,
6582 struct got_repository *repo)
6584 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6585 struct got_fileindex *fileindex = NULL;
6586 char *fileindex_path = NULL;
6587 struct got_pathlist_head commitable_paths;
6588 struct collect_commitables_arg cc_arg;
6589 struct got_pathlist_entry *pe;
6590 struct got_reference *head_ref = NULL;
6591 struct got_object_id *head_commit_id = NULL;
6592 char *diff_path = NULL;
6593 int have_staged_files = 0;
6595 *new_commit_id = NULL;
6597 memset(&cc_arg, 0, sizeof(cc_arg));
6598 TAILQ_INIT(&commitable_paths);
6600 err = lock_worktree(worktree, LOCK_EX);
6601 if (err)
6602 goto done;
6604 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6605 if (err)
6606 goto done;
6608 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6609 if (err)
6610 goto done;
6612 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6613 if (err)
6614 goto done;
6616 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6617 &have_staged_files);
6618 if (err && err->code != GOT_ERR_CANCELLED)
6619 goto done;
6620 if (have_staged_files) {
6621 err = check_non_staged_files(fileindex, paths);
6622 if (err)
6623 goto done;
6626 cc_arg.commitable_paths = &commitable_paths;
6627 cc_arg.worktree = worktree;
6628 cc_arg.fileindex = fileindex;
6629 cc_arg.repo = repo;
6630 cc_arg.have_staged_files = have_staged_files;
6631 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6632 cc_arg.diff_header_shown = 0;
6633 cc_arg.commit_conflicts = commit_conflicts;
6634 if (show_diff) {
6635 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6636 GOT_TMPDIR_STR "/got", ".diff");
6637 if (err)
6638 goto done;
6639 cc_arg.f1 = got_opentemp();
6640 if (cc_arg.f1 == NULL) {
6641 err = got_error_from_errno("got_opentemp");
6642 goto done;
6644 cc_arg.f2 = got_opentemp();
6645 if (cc_arg.f2 == NULL) {
6646 err = got_error_from_errno("got_opentemp");
6647 goto done;
6651 TAILQ_FOREACH(pe, paths, entry) {
6652 err = worktree_status(worktree, pe->path, fileindex, repo,
6653 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6654 if (err)
6655 goto done;
6658 if (show_diff) {
6659 if (fflush(cc_arg.diff_outfile) == EOF) {
6660 err = got_error_from_errno("fflush");
6661 goto done;
6665 if (TAILQ_EMPTY(&commitable_paths)) {
6666 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6667 goto done;
6670 TAILQ_FOREACH(pe, paths, entry) {
6671 err = check_path_is_commitable(pe->path, &commitable_paths);
6672 if (err)
6673 goto done;
6676 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6677 struct got_commitable *ct = pe->data;
6678 const char *ct_path = ct->in_repo_path;
6680 while (ct_path[0] == '/')
6681 ct_path++;
6682 err = check_out_of_date(ct_path, ct->status,
6683 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6684 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6685 if (err)
6686 goto done;
6690 err = commit_worktree(new_commit_id, &commitable_paths,
6691 head_commit_id, NULL, worktree, author, committer,
6692 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6693 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6694 if (err)
6695 goto done;
6697 err = update_fileindex_after_commit(worktree, &commitable_paths,
6698 *new_commit_id, fileindex, have_staged_files);
6699 sync_err = sync_fileindex(fileindex, fileindex_path);
6700 if (sync_err && err == NULL)
6701 err = sync_err;
6702 done:
6703 if (fileindex)
6704 got_fileindex_free(fileindex);
6705 free(fileindex_path);
6706 unlockerr = lock_worktree(worktree, LOCK_SH);
6707 if (unlockerr && err == NULL)
6708 err = unlockerr;
6709 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6710 struct got_commitable *ct = pe->data;
6712 free_commitable(ct);
6714 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6715 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6716 err = got_error_from_errno2("unlink", diff_path);
6717 free(diff_path);
6718 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6719 err == NULL)
6720 err = got_error_from_errno("fclose");
6721 return err;
6724 const char *
6725 got_commitable_get_path(struct got_commitable *ct)
6727 return ct->path;
6730 unsigned int
6731 got_commitable_get_status(struct got_commitable *ct)
6733 return ct->status;
6736 struct check_rebase_ok_arg {
6737 struct got_worktree *worktree;
6738 struct got_repository *repo;
6741 static const struct got_error *
6742 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6744 const struct got_error *err = NULL;
6745 struct check_rebase_ok_arg *a = arg;
6746 unsigned char status;
6747 struct stat sb;
6748 char *ondisk_path;
6750 /* Reject rebase of a work tree with mixed base commits. */
6751 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6752 SHA1_DIGEST_LENGTH))
6753 return got_error(GOT_ERR_MIXED_COMMITS);
6755 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6756 == -1)
6757 return got_error_from_errno("asprintf");
6759 /* Reject rebase of a work tree with modified or staged files. */
6760 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6761 free(ondisk_path);
6762 if (err)
6763 return err;
6765 if (status != GOT_STATUS_NO_CHANGE)
6766 return got_error(GOT_ERR_MODIFIED);
6767 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6768 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6770 return NULL;
6773 const struct got_error *
6774 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6775 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6776 struct got_worktree *worktree, struct got_reference *branch,
6777 struct got_repository *repo)
6779 const struct got_error *err = NULL;
6780 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6781 char *branch_ref_name = NULL;
6782 char *fileindex_path = NULL;
6783 struct check_rebase_ok_arg ok_arg;
6784 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6785 struct got_object_id *wt_branch_tip = NULL;
6787 *new_base_branch_ref = NULL;
6788 *tmp_branch = NULL;
6789 *fileindex = NULL;
6791 err = lock_worktree(worktree, LOCK_EX);
6792 if (err)
6793 return err;
6795 err = open_fileindex(fileindex, &fileindex_path, worktree);
6796 if (err)
6797 goto done;
6799 ok_arg.worktree = worktree;
6800 ok_arg.repo = repo;
6801 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6802 &ok_arg);
6803 if (err)
6804 goto done;
6806 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6807 if (err)
6808 goto done;
6810 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6811 if (err)
6812 goto done;
6814 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6815 if (err)
6816 goto done;
6818 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6819 0);
6820 if (err)
6821 goto done;
6823 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6824 if (err)
6825 goto done;
6826 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6827 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6828 goto done;
6831 err = got_ref_alloc_symref(new_base_branch_ref,
6832 new_base_branch_ref_name, wt_branch);
6833 if (err)
6834 goto done;
6835 err = got_ref_write(*new_base_branch_ref, repo);
6836 if (err)
6837 goto done;
6839 /* TODO Lock original branch's ref while rebasing? */
6841 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6842 if (err)
6843 goto done;
6845 err = got_ref_write(branch_ref, repo);
6846 if (err)
6847 goto done;
6849 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6850 worktree->base_commit_id);
6851 if (err)
6852 goto done;
6853 err = got_ref_write(*tmp_branch, repo);
6854 if (err)
6855 goto done;
6857 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6858 if (err)
6859 goto done;
6860 done:
6861 free(fileindex_path);
6862 free(tmp_branch_name);
6863 free(new_base_branch_ref_name);
6864 free(branch_ref_name);
6865 if (branch_ref)
6866 got_ref_close(branch_ref);
6867 if (wt_branch)
6868 got_ref_close(wt_branch);
6869 free(wt_branch_tip);
6870 if (err) {
6871 if (*new_base_branch_ref) {
6872 got_ref_close(*new_base_branch_ref);
6873 *new_base_branch_ref = NULL;
6875 if (*tmp_branch) {
6876 got_ref_close(*tmp_branch);
6877 *tmp_branch = NULL;
6879 if (*fileindex) {
6880 got_fileindex_free(*fileindex);
6881 *fileindex = NULL;
6883 lock_worktree(worktree, LOCK_SH);
6885 return err;
6888 const struct got_error *
6889 got_worktree_rebase_continue(struct got_object_id **commit_id,
6890 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6891 struct got_reference **branch, struct got_fileindex **fileindex,
6892 struct got_worktree *worktree, struct got_repository *repo)
6894 const struct got_error *err;
6895 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6896 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6897 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6898 char *fileindex_path = NULL;
6899 int have_staged_files = 0;
6901 *commit_id = NULL;
6902 *new_base_branch = NULL;
6903 *tmp_branch = NULL;
6904 *branch = NULL;
6905 *fileindex = NULL;
6907 err = lock_worktree(worktree, LOCK_EX);
6908 if (err)
6909 return err;
6911 err = open_fileindex(fileindex, &fileindex_path, worktree);
6912 if (err)
6913 goto done;
6915 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6916 &have_staged_files);
6917 if (err && err->code != GOT_ERR_CANCELLED)
6918 goto done;
6919 if (have_staged_files) {
6920 err = got_error(GOT_ERR_STAGED_PATHS);
6921 goto done;
6924 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6925 if (err)
6926 goto done;
6928 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6929 if (err)
6930 goto done;
6932 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6933 if (err)
6934 goto done;
6936 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6937 if (err)
6938 goto done;
6940 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6941 if (err)
6942 goto done;
6944 err = got_ref_open(branch, repo,
6945 got_ref_get_symref_target(branch_ref), 0);
6946 if (err)
6947 goto done;
6949 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6950 if (err)
6951 goto done;
6953 err = got_ref_resolve(commit_id, repo, commit_ref);
6954 if (err)
6955 goto done;
6957 err = got_ref_open(new_base_branch, repo,
6958 new_base_branch_ref_name, 0);
6959 if (err)
6960 goto done;
6962 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6963 if (err)
6964 goto done;
6965 done:
6966 free(commit_ref_name);
6967 free(branch_ref_name);
6968 free(fileindex_path);
6969 if (commit_ref)
6970 got_ref_close(commit_ref);
6971 if (branch_ref)
6972 got_ref_close(branch_ref);
6973 if (err) {
6974 free(*commit_id);
6975 *commit_id = NULL;
6976 if (*tmp_branch) {
6977 got_ref_close(*tmp_branch);
6978 *tmp_branch = NULL;
6980 if (*new_base_branch) {
6981 got_ref_close(*new_base_branch);
6982 *new_base_branch = NULL;
6984 if (*branch) {
6985 got_ref_close(*branch);
6986 *branch = NULL;
6988 if (*fileindex) {
6989 got_fileindex_free(*fileindex);
6990 *fileindex = NULL;
6992 lock_worktree(worktree, LOCK_SH);
6994 return err;
6997 const struct got_error *
6998 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7000 const struct got_error *err;
7001 char *tmp_branch_name = NULL;
7003 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7004 if (err)
7005 return err;
7007 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7008 free(tmp_branch_name);
7009 return NULL;
7012 static const struct got_error *
7013 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7014 const char *diff_path, char **logmsg, void *arg)
7016 *logmsg = arg;
7017 return NULL;
7020 static const struct got_error *
7021 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7022 const char *path, struct got_object_id *blob_id,
7023 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7024 int dirfd, const char *de_name)
7026 return NULL;
7029 struct collect_merged_paths_arg {
7030 got_worktree_checkout_cb progress_cb;
7031 void *progress_arg;
7032 struct got_pathlist_head *merged_paths;
7035 static const struct got_error *
7036 collect_merged_paths(void *arg, unsigned char status, const char *path)
7038 const struct got_error *err;
7039 struct collect_merged_paths_arg *a = arg;
7040 char *p;
7041 struct got_pathlist_entry *new;
7043 err = (*a->progress_cb)(a->progress_arg, status, path);
7044 if (err)
7045 return err;
7047 if (status != GOT_STATUS_MERGE &&
7048 status != GOT_STATUS_ADD &&
7049 status != GOT_STATUS_DELETE &&
7050 status != GOT_STATUS_CONFLICT)
7051 return NULL;
7053 p = strdup(path);
7054 if (p == NULL)
7055 return got_error_from_errno("strdup");
7057 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7058 if (err || new == NULL)
7059 free(p);
7060 return err;
7063 static const struct got_error *
7064 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7065 int is_rebase, struct got_repository *repo)
7067 const struct got_error *err;
7068 struct got_reference *commit_ref = NULL;
7070 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7071 if (err) {
7072 if (err->code != GOT_ERR_NOT_REF)
7073 goto done;
7074 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7075 if (err)
7076 goto done;
7077 err = got_ref_write(commit_ref, repo);
7078 if (err)
7079 goto done;
7080 } else if (is_rebase) {
7081 struct got_object_id *stored_id;
7082 int cmp;
7084 err = got_ref_resolve(&stored_id, repo, commit_ref);
7085 if (err)
7086 goto done;
7087 cmp = got_object_id_cmp(commit_id, stored_id);
7088 free(stored_id);
7089 if (cmp != 0) {
7090 err = got_error(GOT_ERR_REBASE_COMMITID);
7091 goto done;
7094 done:
7095 if (commit_ref)
7096 got_ref_close(commit_ref);
7097 return err;
7100 static const struct got_error *
7101 rebase_merge_files(struct got_pathlist_head *merged_paths,
7102 const char *commit_ref_name, struct got_worktree *worktree,
7103 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7104 struct got_object_id *commit_id, struct got_repository *repo,
7105 got_worktree_checkout_cb progress_cb, void *progress_arg,
7106 got_cancel_cb cancel_cb, void *cancel_arg)
7108 const struct got_error *err;
7109 struct got_reference *commit_ref = NULL;
7110 struct collect_merged_paths_arg cmp_arg;
7111 char *fileindex_path;
7113 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7115 err = get_fileindex_path(&fileindex_path, worktree);
7116 if (err)
7117 return err;
7119 cmp_arg.progress_cb = progress_cb;
7120 cmp_arg.progress_arg = progress_arg;
7121 cmp_arg.merged_paths = merged_paths;
7122 err = merge_files(worktree, fileindex, fileindex_path,
7123 parent_commit_id, commit_id, repo, collect_merged_paths,
7124 &cmp_arg, cancel_cb, cancel_arg);
7125 if (commit_ref)
7126 got_ref_close(commit_ref);
7127 return err;
7130 const struct got_error *
7131 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7132 struct got_worktree *worktree, struct got_fileindex *fileindex,
7133 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7134 struct got_repository *repo,
7135 got_worktree_checkout_cb progress_cb, void *progress_arg,
7136 got_cancel_cb cancel_cb, void *cancel_arg)
7138 const struct got_error *err;
7139 char *commit_ref_name;
7141 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7142 if (err)
7143 return err;
7145 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7146 if (err)
7147 goto done;
7149 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7150 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7151 progress_arg, cancel_cb, cancel_arg);
7152 done:
7153 free(commit_ref_name);
7154 return err;
7157 const struct got_error *
7158 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7159 struct got_worktree *worktree, struct got_fileindex *fileindex,
7160 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7161 struct got_repository *repo,
7162 got_worktree_checkout_cb progress_cb, void *progress_arg,
7163 got_cancel_cb cancel_cb, void *cancel_arg)
7165 const struct got_error *err;
7166 char *commit_ref_name;
7168 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7169 if (err)
7170 return err;
7172 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7173 if (err)
7174 goto done;
7176 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7177 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7178 progress_arg, cancel_cb, cancel_arg);
7179 done:
7180 free(commit_ref_name);
7181 return err;
7184 static const struct got_error *
7185 rebase_commit(struct got_object_id **new_commit_id,
7186 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7187 struct got_worktree *worktree, struct got_fileindex *fileindex,
7188 struct got_reference *tmp_branch, const char *committer,
7189 struct got_commit_object *orig_commit, const char *new_logmsg,
7190 int allow_conflict, struct got_repository *repo)
7192 const struct got_error *err, *sync_err;
7193 struct got_pathlist_head commitable_paths;
7194 struct collect_commitables_arg cc_arg;
7195 char *fileindex_path = NULL;
7196 struct got_reference *head_ref = NULL;
7197 struct got_object_id *head_commit_id = NULL;
7198 char *logmsg = NULL;
7200 memset(&cc_arg, 0, sizeof(cc_arg));
7201 TAILQ_INIT(&commitable_paths);
7202 *new_commit_id = NULL;
7204 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7206 err = get_fileindex_path(&fileindex_path, worktree);
7207 if (err)
7208 return err;
7210 cc_arg.commitable_paths = &commitable_paths;
7211 cc_arg.worktree = worktree;
7212 cc_arg.repo = repo;
7213 cc_arg.have_staged_files = 0;
7214 cc_arg.commit_conflicts = allow_conflict;
7216 * If possible get the status of individual files directly to
7217 * avoid crawling the entire work tree once per rebased commit.
7219 * Ideally, merged_paths would contain a list of commitables
7220 * we could use so we could skip worktree_status() entirely.
7221 * However, we would then need carefully keep track of cumulative
7222 * effects of operations such as file additions and deletions
7223 * in 'got histedit -f' (folding multiple commits into one),
7224 * and this extra complexity is not really worth it.
7226 if (merged_paths) {
7227 struct got_pathlist_entry *pe;
7228 TAILQ_FOREACH(pe, merged_paths, entry) {
7229 err = worktree_status(worktree, pe->path, fileindex,
7230 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7231 0);
7232 if (err)
7233 goto done;
7235 } else {
7236 err = worktree_status(worktree, "", fileindex, repo,
7237 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7238 if (err)
7239 goto done;
7242 if (TAILQ_EMPTY(&commitable_paths)) {
7243 /* No-op change; commit will be elided. */
7244 err = got_ref_delete(commit_ref, repo);
7245 if (err)
7246 goto done;
7247 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7248 goto done;
7251 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7252 if (err)
7253 goto done;
7255 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7256 if (err)
7257 goto done;
7259 if (new_logmsg) {
7260 logmsg = strdup(new_logmsg);
7261 if (logmsg == NULL) {
7262 err = got_error_from_errno("strdup");
7263 goto done;
7265 } else {
7266 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7267 if (err)
7268 goto done;
7271 /* NB: commit_worktree will call free(logmsg) */
7272 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7273 NULL, worktree, got_object_commit_get_author(orig_commit),
7274 committer ? committer :
7275 got_object_commit_get_committer(orig_commit), NULL,
7276 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7277 if (err)
7278 goto done;
7280 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7281 if (err)
7282 goto done;
7284 err = got_ref_delete(commit_ref, repo);
7285 if (err)
7286 goto done;
7288 err = update_fileindex_after_commit(worktree, &commitable_paths,
7289 *new_commit_id, fileindex, 0);
7290 sync_err = sync_fileindex(fileindex, fileindex_path);
7291 if (sync_err && err == NULL)
7292 err = sync_err;
7293 done:
7294 free(fileindex_path);
7295 free(head_commit_id);
7296 if (head_ref)
7297 got_ref_close(head_ref);
7298 if (err) {
7299 free(*new_commit_id);
7300 *new_commit_id = NULL;
7302 return err;
7305 const struct got_error *
7306 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7307 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7308 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7309 const char *committer, struct got_commit_object *orig_commit,
7310 struct got_object_id *orig_commit_id, int allow_conflict,
7311 struct got_repository *repo)
7313 const struct got_error *err;
7314 char *commit_ref_name;
7315 struct got_reference *commit_ref = NULL;
7316 struct got_object_id *commit_id = NULL;
7318 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7319 if (err)
7320 return err;
7322 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7323 if (err)
7324 goto done;
7325 err = got_ref_resolve(&commit_id, repo, commit_ref);
7326 if (err)
7327 goto done;
7328 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7329 err = got_error(GOT_ERR_REBASE_COMMITID);
7330 goto done;
7333 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7334 worktree, fileindex, tmp_branch, committer, orig_commit,
7335 NULL, allow_conflict, repo);
7336 done:
7337 if (commit_ref)
7338 got_ref_close(commit_ref);
7339 free(commit_ref_name);
7340 free(commit_id);
7341 return err;
7344 const struct got_error *
7345 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7346 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7347 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7348 const char *committer, struct got_commit_object *orig_commit,
7349 struct got_object_id *orig_commit_id, const char *new_logmsg,
7350 int allow_conflict, struct got_repository *repo)
7352 const struct got_error *err;
7353 char *commit_ref_name;
7354 struct got_reference *commit_ref = NULL;
7356 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7357 if (err)
7358 return err;
7360 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7361 if (err)
7362 goto done;
7364 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7365 worktree, fileindex, tmp_branch, committer, orig_commit,
7366 new_logmsg, allow_conflict, repo);
7367 done:
7368 if (commit_ref)
7369 got_ref_close(commit_ref);
7370 free(commit_ref_name);
7371 return err;
7374 const struct got_error *
7375 got_worktree_rebase_postpone(struct got_worktree *worktree,
7376 struct got_fileindex *fileindex)
7378 if (fileindex)
7379 got_fileindex_free(fileindex);
7380 return lock_worktree(worktree, LOCK_SH);
7383 static const struct got_error *
7384 delete_ref(const char *name, struct got_repository *repo)
7386 const struct got_error *err;
7387 struct got_reference *ref;
7389 err = got_ref_open(&ref, repo, name, 0);
7390 if (err) {
7391 if (err->code == GOT_ERR_NOT_REF)
7392 return NULL;
7393 return err;
7396 err = got_ref_delete(ref, repo);
7397 got_ref_close(ref);
7398 return err;
7401 static const struct got_error *
7402 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7404 const struct got_error *err;
7405 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7406 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7408 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7409 if (err)
7410 goto done;
7411 err = delete_ref(tmp_branch_name, repo);
7412 if (err)
7413 goto done;
7415 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7416 if (err)
7417 goto done;
7418 err = delete_ref(new_base_branch_ref_name, repo);
7419 if (err)
7420 goto done;
7422 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7423 if (err)
7424 goto done;
7425 err = delete_ref(branch_ref_name, repo);
7426 if (err)
7427 goto done;
7429 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7430 if (err)
7431 goto done;
7432 err = delete_ref(commit_ref_name, repo);
7433 if (err)
7434 goto done;
7436 done:
7437 free(tmp_branch_name);
7438 free(new_base_branch_ref_name);
7439 free(branch_ref_name);
7440 free(commit_ref_name);
7441 return err;
7444 static const struct got_error *
7445 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7446 struct got_object_id *new_commit_id, struct got_repository *repo)
7448 const struct got_error *err;
7449 struct got_reference *ref = NULL;
7450 struct got_object_id *old_commit_id = NULL;
7451 const char *branch_name = NULL;
7452 char *new_id_str = NULL;
7453 char *refname = NULL;
7455 branch_name = got_ref_get_name(branch);
7456 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7457 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7458 branch_name += 11;
7460 err = got_object_id_str(&new_id_str, new_commit_id);
7461 if (err)
7462 return err;
7464 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7465 new_id_str) == -1) {
7466 err = got_error_from_errno("asprintf");
7467 goto done;
7470 err = got_ref_resolve(&old_commit_id, repo, branch);
7471 if (err)
7472 goto done;
7474 err = got_ref_alloc(&ref, refname, old_commit_id);
7475 if (err)
7476 goto done;
7478 err = got_ref_write(ref, repo);
7479 done:
7480 free(new_id_str);
7481 free(refname);
7482 free(old_commit_id);
7483 if (ref)
7484 got_ref_close(ref);
7485 return err;
7488 const struct got_error *
7489 got_worktree_rebase_complete(struct got_worktree *worktree,
7490 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7491 struct got_reference *rebased_branch, struct got_repository *repo,
7492 int create_backup)
7494 const struct got_error *err, *unlockerr, *sync_err;
7495 struct got_object_id *new_head_commit_id = NULL;
7496 char *fileindex_path = NULL;
7498 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7499 if (err)
7500 return err;
7502 if (create_backup) {
7503 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7504 rebased_branch, new_head_commit_id, repo);
7505 if (err)
7506 goto done;
7509 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7510 if (err)
7511 goto done;
7513 err = got_ref_write(rebased_branch, repo);
7514 if (err)
7515 goto done;
7517 err = got_worktree_set_head_ref(worktree, rebased_branch);
7518 if (err)
7519 goto done;
7521 err = delete_rebase_refs(worktree, repo);
7522 if (err)
7523 goto done;
7525 err = get_fileindex_path(&fileindex_path, worktree);
7526 if (err)
7527 goto done;
7528 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7529 sync_err = sync_fileindex(fileindex, fileindex_path);
7530 if (sync_err && err == NULL)
7531 err = sync_err;
7532 done:
7533 got_fileindex_free(fileindex);
7534 free(fileindex_path);
7535 free(new_head_commit_id);
7536 unlockerr = lock_worktree(worktree, LOCK_SH);
7537 if (unlockerr && err == NULL)
7538 err = unlockerr;
7539 return err;
7542 static const struct got_error *
7543 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7544 struct got_object_id *id1, struct got_object_id *id2,
7545 struct got_repository *repo)
7547 const struct got_error *err;
7548 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7549 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7551 if (id1) {
7552 err = got_object_open_as_commit(&commit1, repo, id1);
7553 if (err)
7554 goto done;
7556 err = got_object_open_as_tree(&tree1, repo,
7557 got_object_commit_get_tree_id(commit1));
7558 if (err)
7559 goto done;
7562 if (id2) {
7563 err = got_object_open_as_commit(&commit2, repo, id2);
7564 if (err)
7565 goto done;
7567 err = got_object_open_as_tree(&tree2, repo,
7568 got_object_commit_get_tree_id(commit2));
7569 if (err)
7570 goto done;
7573 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7574 got_diff_tree_collect_changed_paths, paths, 0);
7575 if (err)
7576 goto done;
7577 done:
7578 if (commit1)
7579 got_object_commit_close(commit1);
7580 if (commit2)
7581 got_object_commit_close(commit2);
7582 if (tree1)
7583 got_object_tree_close(tree1);
7584 if (tree2)
7585 got_object_tree_close(tree2);
7586 return err;
7589 static const struct got_error *
7590 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7591 struct got_object_id *id1, struct got_object_id *id2,
7592 const char *path_prefix, struct got_repository *repo)
7594 const struct got_error *err;
7595 struct got_pathlist_head merged_paths;
7596 struct got_pathlist_entry *pe;
7597 char *abspath = NULL, *wt_path = NULL;
7599 TAILQ_INIT(&merged_paths);
7601 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7602 if (err)
7603 goto done;
7605 TAILQ_FOREACH(pe, &merged_paths, entry) {
7606 struct got_diff_changed_path *change = pe->data;
7608 if (change->status != GOT_STATUS_ADD)
7609 continue;
7611 if (got_path_is_root_dir(path_prefix)) {
7612 wt_path = strdup(pe->path);
7613 if (wt_path == NULL) {
7614 err = got_error_from_errno("strdup");
7615 goto done;
7617 } else {
7618 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7619 err = got_error_from_errno("asprintf");
7620 goto done;
7623 err = got_path_skip_common_ancestor(&wt_path,
7624 path_prefix, abspath);
7625 if (err)
7626 goto done;
7627 free(abspath);
7628 abspath = NULL;
7631 err = got_pathlist_append(added_paths, wt_path, NULL);
7632 if (err)
7633 goto done;
7634 wt_path = NULL;
7637 done:
7638 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7639 free(abspath);
7640 free(wt_path);
7641 return err;
7644 static const struct got_error *
7645 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7646 struct got_object_id *id, const char *path_prefix,
7647 struct got_repository *repo)
7649 const struct got_error *err;
7650 struct got_commit_object *commit = NULL;
7651 struct got_object_qid *pid;
7653 err = got_object_open_as_commit(&commit, repo, id);
7654 if (err)
7655 goto done;
7657 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7659 err = get_paths_added_between_commits(added_paths,
7660 pid ? &pid->id : NULL, id, path_prefix, repo);
7661 if (err)
7662 goto done;
7663 done:
7664 if (commit)
7665 got_object_commit_close(commit);
7666 return err;
7669 const struct got_error *
7670 got_worktree_rebase_abort(struct got_worktree *worktree,
7671 struct got_fileindex *fileindex, struct got_repository *repo,
7672 struct got_reference *new_base_branch,
7673 got_worktree_checkout_cb progress_cb, void *progress_arg)
7675 const struct got_error *err, *unlockerr, *sync_err;
7676 struct got_reference *resolved = NULL;
7677 struct got_object_id *commit_id = NULL;
7678 struct got_object_id *merged_commit_id = NULL;
7679 struct got_commit_object *commit = NULL;
7680 char *fileindex_path = NULL;
7681 char *commit_ref_name = NULL;
7682 struct got_reference *commit_ref = NULL;
7683 struct revert_file_args rfa;
7684 struct got_object_id *tree_id = NULL;
7685 struct got_pathlist_head added_paths;
7687 TAILQ_INIT(&added_paths);
7689 err = lock_worktree(worktree, LOCK_EX);
7690 if (err)
7691 return err;
7693 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7694 if (err)
7695 goto done;
7697 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7698 if (err)
7699 goto done;
7701 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7702 if (err)
7703 goto done;
7706 * Determine which files in added status can be safely removed
7707 * from disk while reverting changes in the work tree.
7708 * We want to avoid deleting unrelated files which were added by
7709 * the user for conflict resolution purposes.
7711 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7712 got_worktree_get_path_prefix(worktree), repo);
7713 if (err)
7714 goto done;
7716 err = got_ref_open(&resolved, repo,
7717 got_ref_get_symref_target(new_base_branch), 0);
7718 if (err)
7719 goto done;
7721 err = got_worktree_set_head_ref(worktree, resolved);
7722 if (err)
7723 goto done;
7726 * XXX commits to the base branch could have happened while
7727 * we were busy rebasing; should we store the original commit ID
7728 * when rebase begins and read it back here?
7730 err = got_ref_resolve(&commit_id, repo, resolved);
7731 if (err)
7732 goto done;
7734 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7735 if (err)
7736 goto done;
7738 err = got_object_open_as_commit(&commit, repo,
7739 worktree->base_commit_id);
7740 if (err)
7741 goto done;
7743 err = got_object_id_by_path(&tree_id, repo, commit,
7744 worktree->path_prefix);
7745 if (err)
7746 goto done;
7748 err = delete_rebase_refs(worktree, repo);
7749 if (err)
7750 goto done;
7752 err = get_fileindex_path(&fileindex_path, worktree);
7753 if (err)
7754 goto done;
7756 rfa.worktree = worktree;
7757 rfa.fileindex = fileindex;
7758 rfa.progress_cb = progress_cb;
7759 rfa.progress_arg = progress_arg;
7760 rfa.patch_cb = NULL;
7761 rfa.patch_arg = NULL;
7762 rfa.repo = repo;
7763 rfa.unlink_added_files = 1;
7764 rfa.added_files_to_unlink = &added_paths;
7765 err = worktree_status(worktree, "", fileindex, repo,
7766 revert_file, &rfa, NULL, NULL, 1, 0);
7767 if (err)
7768 goto sync;
7770 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7771 repo, progress_cb, progress_arg, NULL, NULL);
7772 sync:
7773 sync_err = sync_fileindex(fileindex, fileindex_path);
7774 if (sync_err && err == NULL)
7775 err = sync_err;
7776 done:
7777 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7778 got_ref_close(resolved);
7779 free(tree_id);
7780 free(commit_id);
7781 free(merged_commit_id);
7782 if (commit)
7783 got_object_commit_close(commit);
7784 if (fileindex)
7785 got_fileindex_free(fileindex);
7786 free(fileindex_path);
7787 free(commit_ref_name);
7788 if (commit_ref)
7789 got_ref_close(commit_ref);
7791 unlockerr = lock_worktree(worktree, LOCK_SH);
7792 if (unlockerr && err == NULL)
7793 err = unlockerr;
7794 return err;
7797 const struct got_error *
7798 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7799 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7800 struct got_fileindex **fileindex, struct got_worktree *worktree,
7801 struct got_repository *repo)
7803 const struct got_error *err = NULL;
7804 char *tmp_branch_name = NULL;
7805 char *branch_ref_name = NULL;
7806 char *base_commit_ref_name = NULL;
7807 char *fileindex_path = NULL;
7808 struct check_rebase_ok_arg ok_arg;
7809 struct got_reference *wt_branch = NULL;
7810 struct got_reference *base_commit_ref = NULL;
7812 *tmp_branch = NULL;
7813 *branch_ref = NULL;
7814 *base_commit_id = NULL;
7815 *fileindex = NULL;
7817 err = lock_worktree(worktree, LOCK_EX);
7818 if (err)
7819 return err;
7821 err = open_fileindex(fileindex, &fileindex_path, worktree);
7822 if (err)
7823 goto done;
7825 ok_arg.worktree = worktree;
7826 ok_arg.repo = repo;
7827 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7828 &ok_arg);
7829 if (err)
7830 goto done;
7832 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7833 if (err)
7834 goto done;
7836 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7837 if (err)
7838 goto done;
7840 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7841 worktree);
7842 if (err)
7843 goto done;
7845 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7846 0);
7847 if (err)
7848 goto done;
7850 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7851 if (err)
7852 goto done;
7854 err = got_ref_write(*branch_ref, repo);
7855 if (err)
7856 goto done;
7858 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7859 worktree->base_commit_id);
7860 if (err)
7861 goto done;
7862 err = got_ref_write(base_commit_ref, repo);
7863 if (err)
7864 goto done;
7865 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7866 if (*base_commit_id == NULL) {
7867 err = got_error_from_errno("got_object_id_dup");
7868 goto done;
7871 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7872 worktree->base_commit_id);
7873 if (err)
7874 goto done;
7875 err = got_ref_write(*tmp_branch, repo);
7876 if (err)
7877 goto done;
7879 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7880 if (err)
7881 goto done;
7882 done:
7883 free(fileindex_path);
7884 free(tmp_branch_name);
7885 free(branch_ref_name);
7886 free(base_commit_ref_name);
7887 if (wt_branch)
7888 got_ref_close(wt_branch);
7889 if (err) {
7890 if (*branch_ref) {
7891 got_ref_close(*branch_ref);
7892 *branch_ref = NULL;
7894 if (*tmp_branch) {
7895 got_ref_close(*tmp_branch);
7896 *tmp_branch = NULL;
7898 free(*base_commit_id);
7899 if (*fileindex) {
7900 got_fileindex_free(*fileindex);
7901 *fileindex = NULL;
7903 lock_worktree(worktree, LOCK_SH);
7905 return err;
7908 const struct got_error *
7909 got_worktree_histedit_postpone(struct got_worktree *worktree,
7910 struct got_fileindex *fileindex)
7912 if (fileindex)
7913 got_fileindex_free(fileindex);
7914 return lock_worktree(worktree, LOCK_SH);
7917 const struct got_error *
7918 got_worktree_histedit_in_progress(int *in_progress,
7919 struct got_worktree *worktree)
7921 const struct got_error *err;
7922 char *tmp_branch_name = NULL;
7924 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7925 if (err)
7926 return err;
7928 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7929 free(tmp_branch_name);
7930 return NULL;
7933 const struct got_error *
7934 got_worktree_histedit_continue(struct got_object_id **commit_id,
7935 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7936 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7937 struct got_worktree *worktree, struct got_repository *repo)
7939 const struct got_error *err;
7940 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7941 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7942 struct got_reference *commit_ref = NULL;
7943 struct got_reference *base_commit_ref = NULL;
7944 char *fileindex_path = NULL;
7945 int have_staged_files = 0;
7947 *commit_id = NULL;
7948 *tmp_branch = 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 (*fileindex) {
8025 got_fileindex_free(*fileindex);
8026 *fileindex = NULL;
8028 lock_worktree(worktree, LOCK_EX);
8030 return err;
8033 static const struct got_error *
8034 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8036 const struct got_error *err;
8037 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8038 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8040 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8041 if (err)
8042 goto done;
8043 err = delete_ref(tmp_branch_name, repo);
8044 if (err)
8045 goto done;
8047 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8048 worktree);
8049 if (err)
8050 goto done;
8051 err = delete_ref(base_commit_ref_name, repo);
8052 if (err)
8053 goto done;
8055 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8056 if (err)
8057 goto done;
8058 err = delete_ref(branch_ref_name, repo);
8059 if (err)
8060 goto done;
8062 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8063 if (err)
8064 goto done;
8065 err = delete_ref(commit_ref_name, repo);
8066 if (err)
8067 goto done;
8068 done:
8069 free(tmp_branch_name);
8070 free(base_commit_ref_name);
8071 free(branch_ref_name);
8072 free(commit_ref_name);
8073 return err;
8076 const struct got_error *
8077 got_worktree_histedit_abort(struct got_worktree *worktree,
8078 struct got_fileindex *fileindex, struct got_repository *repo,
8079 struct got_reference *branch, struct got_object_id *base_commit_id,
8080 got_worktree_checkout_cb progress_cb, void *progress_arg)
8082 const struct got_error *err, *unlockerr, *sync_err;
8083 struct got_reference *resolved = NULL;
8084 char *fileindex_path = NULL;
8085 struct got_object_id *merged_commit_id = NULL;
8086 struct got_commit_object *commit = NULL;
8087 char *commit_ref_name = NULL;
8088 struct got_reference *commit_ref = NULL;
8089 struct got_object_id *tree_id = NULL;
8090 struct revert_file_args rfa;
8091 struct got_pathlist_head added_paths;
8093 TAILQ_INIT(&added_paths);
8095 err = lock_worktree(worktree, LOCK_EX);
8096 if (err)
8097 return err;
8099 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8100 if (err)
8101 goto done;
8103 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8104 if (err) {
8105 if (err->code != GOT_ERR_NOT_REF)
8106 goto done;
8107 /* Can happen on early abort due to invalid histedit script. */
8108 commit_ref = NULL;
8111 if (commit_ref) {
8112 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8113 if (err)
8114 goto done;
8117 * Determine which files in added status can be safely removed
8118 * from disk while reverting changes in the work tree.
8119 * We want to avoid deleting unrelated files added by the
8120 * user during conflict resolution or during histedit -e.
8122 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8123 got_worktree_get_path_prefix(worktree), repo);
8124 if (err)
8125 goto done;
8128 err = got_ref_open(&resolved, repo,
8129 got_ref_get_symref_target(branch), 0);
8130 if (err)
8131 goto done;
8133 err = got_worktree_set_head_ref(worktree, resolved);
8134 if (err)
8135 goto done;
8137 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8138 if (err)
8139 goto done;
8141 err = got_object_open_as_commit(&commit, repo,
8142 worktree->base_commit_id);
8143 if (err)
8144 goto done;
8146 err = got_object_id_by_path(&tree_id, repo, commit,
8147 worktree->path_prefix);
8148 if (err)
8149 goto done;
8151 err = delete_histedit_refs(worktree, repo);
8152 if (err)
8153 goto done;
8155 err = get_fileindex_path(&fileindex_path, worktree);
8156 if (err)
8157 goto done;
8159 rfa.worktree = worktree;
8160 rfa.fileindex = fileindex;
8161 rfa.progress_cb = progress_cb;
8162 rfa.progress_arg = progress_arg;
8163 rfa.patch_cb = NULL;
8164 rfa.patch_arg = NULL;
8165 rfa.repo = repo;
8166 rfa.unlink_added_files = 1;
8167 rfa.added_files_to_unlink = &added_paths;
8168 err = worktree_status(worktree, "", fileindex, repo,
8169 revert_file, &rfa, NULL, NULL, 1, 0);
8170 if (err)
8171 goto sync;
8173 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8174 repo, progress_cb, progress_arg, NULL, NULL);
8175 sync:
8176 sync_err = sync_fileindex(fileindex, fileindex_path);
8177 if (sync_err && err == NULL)
8178 err = sync_err;
8179 done:
8180 if (resolved)
8181 got_ref_close(resolved);
8182 if (commit_ref)
8183 got_ref_close(commit_ref);
8184 free(merged_commit_id);
8185 free(tree_id);
8186 free(fileindex_path);
8187 free(commit_ref_name);
8189 unlockerr = lock_worktree(worktree, LOCK_SH);
8190 if (unlockerr && err == NULL)
8191 err = unlockerr;
8192 return err;
8195 const struct got_error *
8196 got_worktree_histedit_complete(struct got_worktree *worktree,
8197 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8198 struct got_reference *edited_branch, struct got_repository *repo)
8200 const struct got_error *err, *unlockerr, *sync_err;
8201 struct got_object_id *new_head_commit_id = NULL;
8202 struct got_reference *resolved = NULL;
8203 char *fileindex_path = NULL;
8205 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8206 if (err)
8207 return err;
8209 err = got_ref_open(&resolved, repo,
8210 got_ref_get_symref_target(edited_branch), 0);
8211 if (err)
8212 goto done;
8214 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8215 resolved, new_head_commit_id, repo);
8216 if (err)
8217 goto done;
8219 err = got_ref_change_ref(resolved, new_head_commit_id);
8220 if (err)
8221 goto done;
8223 err = got_ref_write(resolved, repo);
8224 if (err)
8225 goto done;
8227 err = got_worktree_set_head_ref(worktree, resolved);
8228 if (err)
8229 goto done;
8231 err = delete_histedit_refs(worktree, repo);
8232 if (err)
8233 goto done;
8235 err = get_fileindex_path(&fileindex_path, worktree);
8236 if (err)
8237 goto done;
8238 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8239 sync_err = sync_fileindex(fileindex, fileindex_path);
8240 if (sync_err && err == NULL)
8241 err = sync_err;
8242 done:
8243 got_fileindex_free(fileindex);
8244 free(fileindex_path);
8245 free(new_head_commit_id);
8246 unlockerr = lock_worktree(worktree, LOCK_SH);
8247 if (unlockerr && err == NULL)
8248 err = unlockerr;
8249 return err;
8252 const struct got_error *
8253 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8254 struct got_object_id *commit_id, struct got_repository *repo)
8256 const struct got_error *err;
8257 char *commit_ref_name;
8259 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8260 if (err)
8261 return err;
8263 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8264 if (err)
8265 goto done;
8267 err = delete_ref(commit_ref_name, repo);
8268 done:
8269 free(commit_ref_name);
8270 return err;
8273 const struct got_error *
8274 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8275 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8276 struct got_worktree *worktree, const char *refname,
8277 struct got_repository *repo)
8279 const struct got_error *err = NULL;
8280 char *fileindex_path = NULL;
8281 struct check_rebase_ok_arg ok_arg;
8283 *fileindex = NULL;
8284 *branch_ref = NULL;
8285 *base_branch_ref = NULL;
8287 err = lock_worktree(worktree, LOCK_EX);
8288 if (err)
8289 return err;
8291 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8292 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8293 "cannot integrate a branch into itself; "
8294 "update -b or different branch name required");
8295 goto done;
8298 err = open_fileindex(fileindex, &fileindex_path, worktree);
8299 if (err)
8300 goto done;
8302 /* Preconditions are the same as for rebase. */
8303 ok_arg.worktree = worktree;
8304 ok_arg.repo = repo;
8305 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8306 &ok_arg);
8307 if (err)
8308 goto done;
8310 err = got_ref_open(branch_ref, repo, refname, 1);
8311 if (err)
8312 goto done;
8314 err = got_ref_open(base_branch_ref, repo,
8315 got_worktree_get_head_ref_name(worktree), 1);
8316 done:
8317 if (err) {
8318 if (*branch_ref) {
8319 got_ref_close(*branch_ref);
8320 *branch_ref = NULL;
8322 if (*base_branch_ref) {
8323 got_ref_close(*base_branch_ref);
8324 *base_branch_ref = NULL;
8326 if (*fileindex) {
8327 got_fileindex_free(*fileindex);
8328 *fileindex = NULL;
8330 lock_worktree(worktree, LOCK_SH);
8332 return err;
8335 const struct got_error *
8336 got_worktree_integrate_continue(struct got_worktree *worktree,
8337 struct got_fileindex *fileindex, struct got_repository *repo,
8338 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8339 got_worktree_checkout_cb progress_cb, void *progress_arg,
8340 got_cancel_cb cancel_cb, void *cancel_arg)
8342 const struct got_error *err = NULL, *sync_err, *unlockerr;
8343 char *fileindex_path = NULL;
8344 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8345 struct got_commit_object *commit = NULL;
8347 err = get_fileindex_path(&fileindex_path, worktree);
8348 if (err)
8349 goto done;
8351 err = got_ref_resolve(&commit_id, repo, branch_ref);
8352 if (err)
8353 goto done;
8355 err = got_object_open_as_commit(&commit, repo, commit_id);
8356 if (err)
8357 goto done;
8359 err = got_object_id_by_path(&tree_id, repo, commit,
8360 worktree->path_prefix);
8361 if (err)
8362 goto done;
8364 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8365 if (err)
8366 goto done;
8368 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8369 progress_cb, progress_arg, cancel_cb, cancel_arg);
8370 if (err)
8371 goto sync;
8373 err = got_ref_change_ref(base_branch_ref, commit_id);
8374 if (err)
8375 goto sync;
8377 err = got_ref_write(base_branch_ref, repo);
8378 if (err)
8379 goto sync;
8381 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8382 sync:
8383 sync_err = sync_fileindex(fileindex, fileindex_path);
8384 if (sync_err && err == NULL)
8385 err = sync_err;
8387 done:
8388 unlockerr = got_ref_unlock(branch_ref);
8389 if (unlockerr && err == NULL)
8390 err = unlockerr;
8391 got_ref_close(branch_ref);
8393 unlockerr = got_ref_unlock(base_branch_ref);
8394 if (unlockerr && err == NULL)
8395 err = unlockerr;
8396 got_ref_close(base_branch_ref);
8398 got_fileindex_free(fileindex);
8399 free(fileindex_path);
8400 free(tree_id);
8401 if (commit)
8402 got_object_commit_close(commit);
8404 unlockerr = lock_worktree(worktree, LOCK_SH);
8405 if (unlockerr && err == NULL)
8406 err = unlockerr;
8407 return err;
8410 const struct got_error *
8411 got_worktree_integrate_abort(struct got_worktree *worktree,
8412 struct got_fileindex *fileindex, struct got_repository *repo,
8413 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8415 const struct got_error *err = NULL, *unlockerr = NULL;
8417 got_fileindex_free(fileindex);
8419 err = lock_worktree(worktree, LOCK_SH);
8421 unlockerr = got_ref_unlock(branch_ref);
8422 if (unlockerr && err == NULL)
8423 err = unlockerr;
8424 got_ref_close(branch_ref);
8426 unlockerr = got_ref_unlock(base_branch_ref);
8427 if (unlockerr && err == NULL)
8428 err = unlockerr;
8429 got_ref_close(base_branch_ref);
8431 return err;
8434 const struct got_error *
8435 got_worktree_merge_postpone(struct got_worktree *worktree,
8436 struct got_fileindex *fileindex)
8438 const struct got_error *err, *sync_err;
8439 char *fileindex_path = NULL;
8441 err = get_fileindex_path(&fileindex_path, worktree);
8442 if (err)
8443 goto done;
8445 sync_err = sync_fileindex(fileindex, fileindex_path);
8447 err = lock_worktree(worktree, LOCK_SH);
8448 if (sync_err && err == NULL)
8449 err = sync_err;
8450 done:
8451 got_fileindex_free(fileindex);
8452 free(fileindex_path);
8453 return err;
8456 static const struct got_error *
8457 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8459 const struct got_error *err;
8460 char *branch_refname = NULL, *commit_refname = NULL;
8462 err = get_merge_branch_ref_name(&branch_refname, worktree);
8463 if (err)
8464 goto done;
8465 err = delete_ref(branch_refname, repo);
8466 if (err)
8467 goto done;
8469 err = get_merge_commit_ref_name(&commit_refname, worktree);
8470 if (err)
8471 goto done;
8472 err = delete_ref(commit_refname, repo);
8473 if (err)
8474 goto done;
8476 done:
8477 free(branch_refname);
8478 free(commit_refname);
8479 return err;
8482 struct merge_commit_msg_arg {
8483 struct got_worktree *worktree;
8484 const char *branch_name;
8487 static const struct got_error *
8488 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8489 const char *diff_path, char **logmsg, void *arg)
8491 struct merge_commit_msg_arg *a = arg;
8493 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8494 got_worktree_get_head_ref_name(a->worktree)) == -1)
8495 return got_error_from_errno("asprintf");
8497 return NULL;
8501 const struct got_error *
8502 got_worktree_merge_branch(struct got_worktree *worktree,
8503 struct got_fileindex *fileindex,
8504 struct got_object_id *yca_commit_id,
8505 struct got_object_id *branch_tip,
8506 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8507 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8509 const struct got_error *err;
8510 char *fileindex_path = NULL;
8511 struct check_mixed_commits_args cma;
8513 err = get_fileindex_path(&fileindex_path, worktree);
8514 if (err)
8515 goto done;
8517 cma.worktree = worktree;
8518 cma.cancel_cb = cancel_cb;
8519 cma.cancel_arg = cancel_arg;
8521 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8522 &cma);
8523 if (err)
8524 goto done;
8526 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8527 branch_tip, repo, progress_cb, progress_arg,
8528 cancel_cb, cancel_arg);
8529 done:
8530 free(fileindex_path);
8531 return err;
8534 const struct got_error *
8535 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8536 struct got_worktree *worktree, struct got_fileindex *fileindex,
8537 const char *author, const char *committer, int allow_bad_symlinks,
8538 struct got_object_id *branch_tip, const char *branch_name,
8539 int allow_conflict, struct got_repository *repo,
8540 got_worktree_status_cb status_cb, void *status_arg)
8543 const struct got_error *err = NULL, *sync_err;
8544 struct got_pathlist_head commitable_paths;
8545 struct collect_commitables_arg cc_arg;
8546 struct got_pathlist_entry *pe;
8547 struct got_reference *head_ref = NULL;
8548 struct got_object_id *head_commit_id = NULL;
8549 int have_staged_files = 0;
8550 struct merge_commit_msg_arg mcm_arg;
8551 char *fileindex_path = NULL;
8553 memset(&cc_arg, 0, sizeof(cc_arg));
8554 *new_commit_id = NULL;
8556 TAILQ_INIT(&commitable_paths);
8558 err = get_fileindex_path(&fileindex_path, worktree);
8559 if (err)
8560 goto done;
8562 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8563 if (err)
8564 goto done;
8566 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8567 if (err)
8568 goto done;
8570 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8571 &have_staged_files);
8572 if (err && err->code != GOT_ERR_CANCELLED)
8573 goto done;
8574 if (have_staged_files) {
8575 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8576 goto done;
8579 cc_arg.commitable_paths = &commitable_paths;
8580 cc_arg.worktree = worktree;
8581 cc_arg.fileindex = fileindex;
8582 cc_arg.repo = repo;
8583 cc_arg.have_staged_files = have_staged_files;
8584 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8585 cc_arg.commit_conflicts = allow_conflict;
8586 err = worktree_status(worktree, "", fileindex, repo,
8587 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8588 if (err)
8589 goto done;
8591 mcm_arg.worktree = worktree;
8592 mcm_arg.branch_name = branch_name;
8593 err = commit_worktree(new_commit_id, &commitable_paths,
8594 head_commit_id, branch_tip, worktree, author, committer, NULL,
8595 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8596 if (err)
8597 goto done;
8599 err = update_fileindex_after_commit(worktree, &commitable_paths,
8600 *new_commit_id, fileindex, have_staged_files);
8601 sync_err = sync_fileindex(fileindex, fileindex_path);
8602 if (sync_err && err == NULL)
8603 err = sync_err;
8604 done:
8605 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8606 struct got_commitable *ct = pe->data;
8608 free_commitable(ct);
8610 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8611 free(fileindex_path);
8612 return err;
8615 const struct got_error *
8616 got_worktree_merge_complete(struct got_worktree *worktree,
8617 struct got_fileindex *fileindex, struct got_repository *repo)
8619 const struct got_error *err, *unlockerr, *sync_err;
8620 char *fileindex_path = NULL;
8622 err = delete_merge_refs(worktree, repo);
8623 if (err)
8624 goto done;
8626 err = get_fileindex_path(&fileindex_path, worktree);
8627 if (err)
8628 goto done;
8629 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8630 sync_err = sync_fileindex(fileindex, fileindex_path);
8631 if (sync_err && err == NULL)
8632 err = sync_err;
8633 done:
8634 got_fileindex_free(fileindex);
8635 free(fileindex_path);
8636 unlockerr = lock_worktree(worktree, LOCK_SH);
8637 if (unlockerr && err == NULL)
8638 err = unlockerr;
8639 return err;
8642 const struct got_error *
8643 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8644 struct got_repository *repo)
8646 const struct got_error *err;
8647 char *branch_refname = NULL;
8648 struct got_reference *branch_ref = NULL;
8650 *in_progress = 0;
8652 err = get_merge_branch_ref_name(&branch_refname, worktree);
8653 if (err)
8654 return err;
8655 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8656 free(branch_refname);
8657 if (err) {
8658 if (err->code != GOT_ERR_NOT_REF)
8659 return err;
8660 } else
8661 *in_progress = 1;
8663 return NULL;
8666 const struct got_error *got_worktree_merge_prepare(
8667 struct got_fileindex **fileindex, struct got_worktree *worktree,
8668 struct got_repository *repo)
8670 const struct got_error *err = NULL;
8671 char *fileindex_path = NULL;
8672 struct got_reference *wt_branch = NULL;
8673 struct got_object_id *wt_branch_tip = NULL;
8674 struct check_rebase_ok_arg ok_arg;
8676 *fileindex = NULL;
8678 err = lock_worktree(worktree, LOCK_EX);
8679 if (err)
8680 return err;
8682 err = open_fileindex(fileindex, &fileindex_path, worktree);
8683 if (err)
8684 goto done;
8686 /* Preconditions are the same as for rebase. */
8687 ok_arg.worktree = worktree;
8688 ok_arg.repo = repo;
8689 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8690 &ok_arg);
8691 if (err)
8692 goto done;
8694 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8695 0);
8696 if (err)
8697 goto done;
8699 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8700 if (err)
8701 goto done;
8703 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8704 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8705 goto done;
8708 done:
8709 free(fileindex_path);
8710 if (wt_branch)
8711 got_ref_close(wt_branch);
8712 free(wt_branch_tip);
8713 if (err) {
8714 if (*fileindex) {
8715 got_fileindex_free(*fileindex);
8716 *fileindex = NULL;
8718 lock_worktree(worktree, LOCK_SH);
8720 return err;
8723 const struct got_error *got_worktree_merge_write_refs(
8724 struct got_worktree *worktree, struct got_reference *branch,
8725 struct got_repository *repo)
8727 const struct got_error *err = NULL;
8728 char *branch_refname = NULL, *commit_refname = NULL;
8729 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8730 struct got_object_id *branch_tip = NULL;
8732 err = get_merge_branch_ref_name(&branch_refname, worktree);
8733 if (err)
8734 return err;
8736 err = get_merge_commit_ref_name(&commit_refname, worktree);
8737 if (err)
8738 return err;
8740 err = got_ref_resolve(&branch_tip, repo, branch);
8741 if (err)
8742 goto done;
8744 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8745 if (err)
8746 goto done;
8747 err = got_ref_write(branch_ref, repo);
8748 if (err)
8749 goto done;
8751 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8752 if (err)
8753 goto done;
8754 err = got_ref_write(commit_ref, repo);
8755 if (err)
8756 goto done;
8758 done:
8759 free(branch_refname);
8760 free(commit_refname);
8761 if (branch_ref)
8762 got_ref_close(branch_ref);
8763 if (commit_ref)
8764 got_ref_close(commit_ref);
8765 free(branch_tip);
8766 return err;
8769 const struct got_error *
8770 got_worktree_merge_continue(char **branch_name,
8771 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8772 struct got_worktree *worktree, struct got_repository *repo)
8774 const struct got_error *err;
8775 char *commit_refname = NULL, *branch_refname = NULL;
8776 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8777 char *fileindex_path = NULL;
8778 int have_staged_files = 0;
8780 *branch_name = NULL;
8781 *branch_tip = NULL;
8782 *fileindex = NULL;
8784 err = lock_worktree(worktree, LOCK_EX);
8785 if (err)
8786 return err;
8788 err = open_fileindex(fileindex, &fileindex_path, worktree);
8789 if (err)
8790 goto done;
8792 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8793 &have_staged_files);
8794 if (err && err->code != GOT_ERR_CANCELLED)
8795 goto done;
8796 if (have_staged_files) {
8797 err = got_error(GOT_ERR_STAGED_PATHS);
8798 goto done;
8801 err = get_merge_branch_ref_name(&branch_refname, worktree);
8802 if (err)
8803 goto done;
8805 err = get_merge_commit_ref_name(&commit_refname, worktree);
8806 if (err)
8807 goto done;
8809 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8810 if (err)
8811 goto done;
8813 if (!got_ref_is_symbolic(branch_ref)) {
8814 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8815 "%s is not a symbolic reference",
8816 got_ref_get_name(branch_ref));
8817 goto done;
8819 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8820 if (*branch_name == NULL) {
8821 err = got_error_from_errno("strdup");
8822 goto done;
8825 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8826 if (err)
8827 goto done;
8829 err = got_ref_resolve(branch_tip, repo, commit_ref);
8830 if (err)
8831 goto done;
8832 done:
8833 free(commit_refname);
8834 free(branch_refname);
8835 free(fileindex_path);
8836 if (commit_ref)
8837 got_ref_close(commit_ref);
8838 if (branch_ref)
8839 got_ref_close(branch_ref);
8840 if (err) {
8841 if (*branch_name) {
8842 free(*branch_name);
8843 *branch_name = NULL;
8845 free(*branch_tip);
8846 *branch_tip = NULL;
8847 if (*fileindex) {
8848 got_fileindex_free(*fileindex);
8849 *fileindex = NULL;
8851 lock_worktree(worktree, LOCK_SH);
8853 return err;
8856 const struct got_error *
8857 got_worktree_merge_abort(struct got_worktree *worktree,
8858 struct got_fileindex *fileindex, struct got_repository *repo,
8859 got_worktree_checkout_cb progress_cb, void *progress_arg)
8861 const struct got_error *err, *unlockerr, *sync_err;
8862 struct got_commit_object *commit = NULL;
8863 char *fileindex_path = NULL;
8864 struct revert_file_args rfa;
8865 char *commit_ref_name = NULL;
8866 struct got_reference *commit_ref = NULL;
8867 struct got_object_id *merged_commit_id = NULL;
8868 struct got_object_id *tree_id = NULL;
8869 struct got_pathlist_head added_paths;
8871 TAILQ_INIT(&added_paths);
8873 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8874 if (err)
8875 goto done;
8877 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8878 if (err)
8879 goto done;
8881 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8882 if (err)
8883 goto done;
8886 * Determine which files in added status can be safely removed
8887 * from disk while reverting changes in the work tree.
8888 * We want to avoid deleting unrelated files which were added by
8889 * the user for conflict resolution purposes.
8891 err = get_paths_added_between_commits(&added_paths,
8892 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8893 got_worktree_get_path_prefix(worktree), repo);
8894 if (err)
8895 goto done;
8898 err = got_object_open_as_commit(&commit, repo,
8899 worktree->base_commit_id);
8900 if (err)
8901 goto done;
8903 err = got_object_id_by_path(&tree_id, repo, commit,
8904 worktree->path_prefix);
8905 if (err)
8906 goto done;
8908 err = delete_merge_refs(worktree, repo);
8909 if (err)
8910 goto done;
8912 err = get_fileindex_path(&fileindex_path, worktree);
8913 if (err)
8914 goto done;
8916 rfa.worktree = worktree;
8917 rfa.fileindex = fileindex;
8918 rfa.progress_cb = progress_cb;
8919 rfa.progress_arg = progress_arg;
8920 rfa.patch_cb = NULL;
8921 rfa.patch_arg = NULL;
8922 rfa.repo = repo;
8923 rfa.unlink_added_files = 1;
8924 rfa.added_files_to_unlink = &added_paths;
8925 err = worktree_status(worktree, "", fileindex, repo,
8926 revert_file, &rfa, NULL, NULL, 1, 0);
8927 if (err)
8928 goto sync;
8930 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8931 repo, progress_cb, progress_arg, NULL, NULL);
8932 sync:
8933 sync_err = sync_fileindex(fileindex, fileindex_path);
8934 if (sync_err && err == NULL)
8935 err = sync_err;
8936 done:
8937 free(tree_id);
8938 free(merged_commit_id);
8939 if (commit)
8940 got_object_commit_close(commit);
8941 if (fileindex)
8942 got_fileindex_free(fileindex);
8943 free(fileindex_path);
8944 if (commit_ref)
8945 got_ref_close(commit_ref);
8946 free(commit_ref_name);
8948 unlockerr = lock_worktree(worktree, LOCK_SH);
8949 if (unlockerr && err == NULL)
8950 err = unlockerr;
8951 return err;
8954 struct check_stage_ok_arg {
8955 struct got_object_id *head_commit_id;
8956 struct got_worktree *worktree;
8957 struct got_fileindex *fileindex;
8958 struct got_repository *repo;
8959 int have_changes;
8962 static const struct got_error *
8963 check_stage_ok(void *arg, unsigned char status,
8964 unsigned char staged_status, const char *relpath,
8965 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8966 struct got_object_id *commit_id, int dirfd, const char *de_name)
8968 struct check_stage_ok_arg *a = arg;
8969 const struct got_error *err = NULL;
8970 struct got_fileindex_entry *ie;
8971 struct got_object_id base_commit_id;
8972 struct got_object_id *base_commit_idp = NULL;
8973 char *in_repo_path = NULL, *p;
8975 if (status == GOT_STATUS_UNVERSIONED ||
8976 status == GOT_STATUS_NO_CHANGE)
8977 return NULL;
8978 if (status == GOT_STATUS_NONEXISTENT)
8979 return got_error_set_errno(ENOENT, relpath);
8981 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8982 if (ie == NULL)
8983 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8985 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8986 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8987 relpath) == -1)
8988 return got_error_from_errno("asprintf");
8990 if (got_fileindex_entry_has_commit(ie)) {
8991 base_commit_idp = got_fileindex_entry_get_commit_id(
8992 &base_commit_id, ie);
8995 if (status == GOT_STATUS_CONFLICT) {
8996 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8997 goto done;
8998 } else if (status != GOT_STATUS_ADD &&
8999 status != GOT_STATUS_MODIFY &&
9000 status != GOT_STATUS_DELETE) {
9001 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9002 goto done;
9005 a->have_changes = 1;
9007 p = in_repo_path;
9008 while (p[0] == '/')
9009 p++;
9010 err = check_out_of_date(p, status, staged_status,
9011 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9012 GOT_ERR_STAGE_OUT_OF_DATE);
9013 done:
9014 free(in_repo_path);
9015 return err;
9018 struct stage_path_arg {
9019 struct got_worktree *worktree;
9020 struct got_fileindex *fileindex;
9021 struct got_repository *repo;
9022 got_worktree_status_cb status_cb;
9023 void *status_arg;
9024 got_worktree_patch_cb patch_cb;
9025 void *patch_arg;
9026 int staged_something;
9027 int allow_bad_symlinks;
9030 static const struct got_error *
9031 stage_path(void *arg, unsigned char status,
9032 unsigned char staged_status, const char *relpath,
9033 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9034 struct got_object_id *commit_id, int dirfd, const char *de_name)
9036 struct stage_path_arg *a = arg;
9037 const struct got_error *err = NULL;
9038 struct got_fileindex_entry *ie;
9039 char *ondisk_path = NULL, *path_content = NULL;
9040 uint32_t stage;
9041 struct got_object_id *new_staged_blob_id = NULL;
9042 struct stat sb;
9044 if (status == GOT_STATUS_UNVERSIONED)
9045 return NULL;
9047 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9048 if (ie == NULL)
9049 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9051 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9052 relpath)== -1)
9053 return got_error_from_errno("asprintf");
9055 switch (status) {
9056 case GOT_STATUS_ADD:
9057 case GOT_STATUS_MODIFY:
9058 /* XXX could sb.st_mode be passed in by our caller? */
9059 if (lstat(ondisk_path, &sb) == -1) {
9060 err = got_error_from_errno2("lstat", ondisk_path);
9061 break;
9063 if (a->patch_cb) {
9064 if (status == GOT_STATUS_ADD) {
9065 int choice = GOT_PATCH_CHOICE_NONE;
9066 err = (*a->patch_cb)(&choice, a->patch_arg,
9067 status, ie->path, NULL, 1, 1);
9068 if (err)
9069 break;
9070 if (choice != GOT_PATCH_CHOICE_YES)
9071 break;
9072 } else {
9073 err = create_patched_content(&path_content, 0,
9074 staged_blob_id ? staged_blob_id : blob_id,
9075 ondisk_path, dirfd, de_name, ie->path,
9076 a->repo, a->patch_cb, a->patch_arg);
9077 if (err || path_content == NULL)
9078 break;
9081 err = got_object_blob_create(&new_staged_blob_id,
9082 path_content ? path_content : ondisk_path, a->repo);
9083 if (err)
9084 break;
9085 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9086 SHA1_DIGEST_LENGTH);
9087 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9088 stage = GOT_FILEIDX_STAGE_ADD;
9089 else
9090 stage = GOT_FILEIDX_STAGE_MODIFY;
9091 got_fileindex_entry_stage_set(ie, stage);
9092 if (S_ISLNK(sb.st_mode)) {
9093 int is_bad_symlink = 0;
9094 if (!a->allow_bad_symlinks) {
9095 char target_path[PATH_MAX];
9096 ssize_t target_len;
9097 target_len = readlink(ondisk_path, target_path,
9098 sizeof(target_path));
9099 if (target_len == -1) {
9100 err = got_error_from_errno2("readlink",
9101 ondisk_path);
9102 break;
9104 err = is_bad_symlink_target(&is_bad_symlink,
9105 target_path, target_len, ondisk_path,
9106 a->worktree->root_path,
9107 a->worktree->meta_dir);
9108 if (err)
9109 break;
9110 if (is_bad_symlink) {
9111 err = got_error_path(ondisk_path,
9112 GOT_ERR_BAD_SYMLINK);
9113 break;
9116 if (is_bad_symlink)
9117 got_fileindex_entry_staged_filetype_set(ie,
9118 GOT_FILEIDX_MODE_BAD_SYMLINK);
9119 else
9120 got_fileindex_entry_staged_filetype_set(ie,
9121 GOT_FILEIDX_MODE_SYMLINK);
9122 } else {
9123 got_fileindex_entry_staged_filetype_set(ie,
9124 GOT_FILEIDX_MODE_REGULAR_FILE);
9126 a->staged_something = 1;
9127 if (a->status_cb == NULL)
9128 break;
9129 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9130 get_staged_status(ie), relpath, blob_id,
9131 new_staged_blob_id, NULL, dirfd, de_name);
9132 if (err)
9133 break;
9135 * When staging the reverse of the staged diff,
9136 * implicitly unstage the file.
9138 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9139 sizeof(ie->blob_sha1)) == 0) {
9140 got_fileindex_entry_stage_set(ie,
9141 GOT_FILEIDX_STAGE_NONE);
9143 break;
9144 case GOT_STATUS_DELETE:
9145 if (staged_status == GOT_STATUS_DELETE)
9146 break;
9147 if (a->patch_cb) {
9148 int choice = GOT_PATCH_CHOICE_NONE;
9149 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9150 ie->path, NULL, 1, 1);
9151 if (err)
9152 break;
9153 if (choice == GOT_PATCH_CHOICE_NO)
9154 break;
9155 if (choice != GOT_PATCH_CHOICE_YES) {
9156 err = got_error(GOT_ERR_PATCH_CHOICE);
9157 break;
9160 stage = GOT_FILEIDX_STAGE_DELETE;
9161 got_fileindex_entry_stage_set(ie, stage);
9162 a->staged_something = 1;
9163 if (a->status_cb == NULL)
9164 break;
9165 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9166 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9167 de_name);
9168 break;
9169 case GOT_STATUS_NO_CHANGE:
9170 break;
9171 case GOT_STATUS_CONFLICT:
9172 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9173 break;
9174 case GOT_STATUS_NONEXISTENT:
9175 err = got_error_set_errno(ENOENT, relpath);
9176 break;
9177 default:
9178 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9179 break;
9182 if (path_content && unlink(path_content) == -1 && err == NULL)
9183 err = got_error_from_errno2("unlink", path_content);
9184 free(path_content);
9185 free(ondisk_path);
9186 free(new_staged_blob_id);
9187 return err;
9190 const struct got_error *
9191 got_worktree_stage(struct got_worktree *worktree,
9192 struct got_pathlist_head *paths,
9193 got_worktree_status_cb status_cb, void *status_arg,
9194 got_worktree_patch_cb patch_cb, void *patch_arg,
9195 int allow_bad_symlinks, struct got_repository *repo)
9197 const struct got_error *err = NULL, *sync_err, *unlockerr;
9198 struct got_pathlist_entry *pe;
9199 struct got_fileindex *fileindex = NULL;
9200 char *fileindex_path = NULL;
9201 struct got_reference *head_ref = NULL;
9202 struct got_object_id *head_commit_id = NULL;
9203 struct check_stage_ok_arg oka;
9204 struct stage_path_arg spa;
9206 err = lock_worktree(worktree, LOCK_EX);
9207 if (err)
9208 return err;
9210 err = got_ref_open(&head_ref, repo,
9211 got_worktree_get_head_ref_name(worktree), 0);
9212 if (err)
9213 goto done;
9214 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9215 if (err)
9216 goto done;
9217 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9218 if (err)
9219 goto done;
9221 /* Check pre-conditions before staging anything. */
9222 oka.head_commit_id = head_commit_id;
9223 oka.worktree = worktree;
9224 oka.fileindex = fileindex;
9225 oka.repo = repo;
9226 oka.have_changes = 0;
9227 TAILQ_FOREACH(pe, paths, entry) {
9228 err = worktree_status(worktree, pe->path, fileindex, repo,
9229 check_stage_ok, &oka, NULL, NULL, 1, 0);
9230 if (err)
9231 goto done;
9233 if (!oka.have_changes) {
9234 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9235 goto done;
9238 spa.worktree = worktree;
9239 spa.fileindex = fileindex;
9240 spa.repo = repo;
9241 spa.patch_cb = patch_cb;
9242 spa.patch_arg = patch_arg;
9243 spa.status_cb = status_cb;
9244 spa.status_arg = status_arg;
9245 spa.staged_something = 0;
9246 spa.allow_bad_symlinks = allow_bad_symlinks;
9247 TAILQ_FOREACH(pe, paths, entry) {
9248 err = worktree_status(worktree, pe->path, fileindex, repo,
9249 stage_path, &spa, NULL, NULL, 1, 0);
9250 if (err)
9251 goto done;
9253 if (!spa.staged_something) {
9254 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9255 goto done;
9258 sync_err = sync_fileindex(fileindex, fileindex_path);
9259 if (sync_err && err == NULL)
9260 err = sync_err;
9261 done:
9262 if (head_ref)
9263 got_ref_close(head_ref);
9264 free(head_commit_id);
9265 free(fileindex_path);
9266 if (fileindex)
9267 got_fileindex_free(fileindex);
9268 unlockerr = lock_worktree(worktree, LOCK_SH);
9269 if (unlockerr && err == NULL)
9270 err = unlockerr;
9271 return err;
9274 struct unstage_path_arg {
9275 struct got_worktree *worktree;
9276 struct got_fileindex *fileindex;
9277 struct got_repository *repo;
9278 got_worktree_checkout_cb progress_cb;
9279 void *progress_arg;
9280 got_worktree_patch_cb patch_cb;
9281 void *patch_arg;
9284 static const struct got_error *
9285 create_unstaged_content(char **path_unstaged_content,
9286 char **path_new_staged_content, struct got_object_id *blob_id,
9287 struct got_object_id *staged_blob_id, const char *relpath,
9288 struct got_repository *repo,
9289 got_worktree_patch_cb patch_cb, void *patch_arg)
9291 const struct got_error *err, *free_err;
9292 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9293 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9294 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9295 struct got_diffreg_result *diffreg_result = NULL;
9296 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9297 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9298 int fd1 = -1, fd2 = -1;
9300 *path_unstaged_content = NULL;
9301 *path_new_staged_content = NULL;
9303 err = got_object_id_str(&label1, blob_id);
9304 if (err)
9305 return err;
9307 fd1 = got_opentempfd();
9308 if (fd1 == -1) {
9309 err = got_error_from_errno("got_opentempfd");
9310 goto done;
9312 fd2 = got_opentempfd();
9313 if (fd2 == -1) {
9314 err = got_error_from_errno("got_opentempfd");
9315 goto done;
9318 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9319 if (err)
9320 goto done;
9322 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9323 if (err)
9324 goto done;
9326 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9327 if (err)
9328 goto done;
9330 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9331 fd2);
9332 if (err)
9333 goto done;
9335 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9336 if (err)
9337 goto done;
9339 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9340 if (err)
9341 goto done;
9343 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9344 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9345 if (err)
9346 goto done;
9348 err = got_opentemp_named(path_unstaged_content, &outfile,
9349 "got-unstaged-content", "");
9350 if (err)
9351 goto done;
9352 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9353 "got-new-staged-content", "");
9354 if (err)
9355 goto done;
9357 if (fseek(f1, 0L, SEEK_SET) == -1) {
9358 err = got_ferror(f1, GOT_ERR_IO);
9359 goto done;
9361 if (fseek(f2, 0L, SEEK_SET) == -1) {
9362 err = got_ferror(f2, GOT_ERR_IO);
9363 goto done;
9365 /* Count the number of actual changes in the diff result. */
9366 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9367 struct diff_chunk_context cc = {};
9368 diff_chunk_context_load_change(&cc, &nchunks_used,
9369 diffreg_result->result, n, 0);
9370 nchanges++;
9372 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9373 int choice;
9374 err = apply_or_reject_change(&choice, &nchunks_used,
9375 diffreg_result->result, n, relpath, f1, f2,
9376 &line_cur1, &line_cur2,
9377 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9378 if (err)
9379 goto done;
9380 if (choice == GOT_PATCH_CHOICE_YES)
9381 have_content = 1;
9382 else
9383 have_rejected_content = 1;
9384 if (choice == GOT_PATCH_CHOICE_QUIT)
9385 break;
9387 if (have_content || have_rejected_content)
9388 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9389 outfile, rejectfile);
9390 done:
9391 free(label1);
9392 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9393 err = got_error_from_errno("close");
9394 if (blob)
9395 got_object_blob_close(blob);
9396 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9397 err = got_error_from_errno("close");
9398 if (staged_blob)
9399 got_object_blob_close(staged_blob);
9400 free_err = got_diffreg_result_free(diffreg_result);
9401 if (free_err && err == NULL)
9402 err = free_err;
9403 if (f1 && fclose(f1) == EOF && err == NULL)
9404 err = got_error_from_errno2("fclose", path1);
9405 if (f2 && fclose(f2) == EOF && err == NULL)
9406 err = got_error_from_errno2("fclose", path2);
9407 if (outfile && fclose(outfile) == EOF && err == NULL)
9408 err = got_error_from_errno2("fclose", *path_unstaged_content);
9409 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9410 err = got_error_from_errno2("fclose", *path_new_staged_content);
9411 if (path1 && unlink(path1) == -1 && err == NULL)
9412 err = got_error_from_errno2("unlink", path1);
9413 if (path2 && unlink(path2) == -1 && err == NULL)
9414 err = got_error_from_errno2("unlink", path2);
9415 if (err || !have_content) {
9416 if (*path_unstaged_content &&
9417 unlink(*path_unstaged_content) == -1 && err == NULL)
9418 err = got_error_from_errno2("unlink",
9419 *path_unstaged_content);
9420 free(*path_unstaged_content);
9421 *path_unstaged_content = NULL;
9423 if (err || !have_content || !have_rejected_content) {
9424 if (*path_new_staged_content &&
9425 unlink(*path_new_staged_content) == -1 && err == NULL)
9426 err = got_error_from_errno2("unlink",
9427 *path_new_staged_content);
9428 free(*path_new_staged_content);
9429 *path_new_staged_content = NULL;
9431 free(path1);
9432 free(path2);
9433 return err;
9436 static const struct got_error *
9437 unstage_hunks(struct got_object_id *staged_blob_id,
9438 struct got_blob_object *blob_base,
9439 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9440 const char *ondisk_path, const char *label_orig,
9441 struct got_worktree *worktree, struct got_repository *repo,
9442 got_worktree_patch_cb patch_cb, void *patch_arg,
9443 got_worktree_checkout_cb progress_cb, void *progress_arg)
9445 const struct got_error *err = NULL;
9446 char *path_unstaged_content = NULL;
9447 char *path_new_staged_content = NULL;
9448 char *parent = NULL, *base_path = NULL;
9449 char *blob_base_path = NULL;
9450 struct got_object_id *new_staged_blob_id = NULL;
9451 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9452 struct stat sb;
9454 err = create_unstaged_content(&path_unstaged_content,
9455 &path_new_staged_content, blob_id, staged_blob_id,
9456 ie->path, repo, patch_cb, patch_arg);
9457 if (err)
9458 return err;
9460 if (path_unstaged_content == NULL)
9461 return NULL;
9463 if (path_new_staged_content) {
9464 err = got_object_blob_create(&new_staged_blob_id,
9465 path_new_staged_content, repo);
9466 if (err)
9467 goto done;
9470 f = fopen(path_unstaged_content, "re");
9471 if (f == NULL) {
9472 err = got_error_from_errno2("fopen",
9473 path_unstaged_content);
9474 goto done;
9476 if (fstat(fileno(f), &sb) == -1) {
9477 err = got_error_from_errno2("fstat", path_unstaged_content);
9478 goto done;
9480 if (got_fileindex_entry_staged_filetype_get(ie) ==
9481 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9482 char link_target[PATH_MAX];
9483 size_t r;
9484 r = fread(link_target, 1, sizeof(link_target), f);
9485 if (r == 0 && ferror(f)) {
9486 err = got_error_from_errno("fread");
9487 goto done;
9489 if (r >= sizeof(link_target)) { /* should not happen */
9490 err = got_error(GOT_ERR_NO_SPACE);
9491 goto done;
9493 link_target[r] = '\0';
9494 err = merge_symlink(worktree, blob_base,
9495 ondisk_path, ie->path, label_orig, link_target,
9496 worktree->base_commit_id, repo, progress_cb,
9497 progress_arg);
9498 } else {
9499 int local_changes_subsumed;
9501 err = got_path_dirname(&parent, ondisk_path);
9502 if (err)
9503 return err;
9505 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9506 parent) == -1) {
9507 err = got_error_from_errno("asprintf");
9508 base_path = NULL;
9509 goto done;
9512 err = got_opentemp_named(&blob_base_path, &f_base,
9513 base_path, "");
9514 if (err)
9515 goto done;
9516 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9517 blob_base);
9518 if (err)
9519 goto done;
9522 * In order the run a 3-way merge with a symlink we copy the symlink's
9523 * target path into a temporary file and use that file with diff3.
9525 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9526 err = dump_symlink_target_path_to_file(&f_deriv2,
9527 ondisk_path);
9528 if (err)
9529 goto done;
9530 } else {
9531 int fd;
9532 fd = open(ondisk_path,
9533 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9534 if (fd == -1) {
9535 err = got_error_from_errno2("open", ondisk_path);
9536 goto done;
9538 f_deriv2 = fdopen(fd, "r");
9539 if (f_deriv2 == NULL) {
9540 err = got_error_from_errno2("fdopen", ondisk_path);
9541 close(fd);
9542 goto done;
9546 err = merge_file(&local_changes_subsumed, worktree,
9547 f_base, f, f_deriv2, ondisk_path, ie->path,
9548 got_fileindex_perms_to_st(ie),
9549 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9550 repo, progress_cb, progress_arg);
9552 if (err)
9553 goto done;
9555 if (new_staged_blob_id) {
9556 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9557 SHA1_DIGEST_LENGTH);
9558 } else {
9559 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9560 got_fileindex_entry_staged_filetype_set(ie, 0);
9562 done:
9563 free(new_staged_blob_id);
9564 if (path_unstaged_content &&
9565 unlink(path_unstaged_content) == -1 && err == NULL)
9566 err = got_error_from_errno2("unlink", path_unstaged_content);
9567 if (path_new_staged_content &&
9568 unlink(path_new_staged_content) == -1 && err == NULL)
9569 err = got_error_from_errno2("unlink", path_new_staged_content);
9570 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9571 err = got_error_from_errno2("unlink", blob_base_path);
9572 if (f_base && fclose(f_base) == EOF && err == NULL)
9573 err = got_error_from_errno2("fclose", path_unstaged_content);
9574 if (f && fclose(f) == EOF && err == NULL)
9575 err = got_error_from_errno2("fclose", path_unstaged_content);
9576 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9577 err = got_error_from_errno2("fclose", ondisk_path);
9578 free(path_unstaged_content);
9579 free(path_new_staged_content);
9580 free(blob_base_path);
9581 free(parent);
9582 free(base_path);
9583 return err;
9586 static const struct got_error *
9587 unstage_path(void *arg, unsigned char status,
9588 unsigned char staged_status, const char *relpath,
9589 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9590 struct got_object_id *commit_id, int dirfd, const char *de_name)
9592 const struct got_error *err = NULL;
9593 struct unstage_path_arg *a = arg;
9594 struct got_fileindex_entry *ie;
9595 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9596 char *ondisk_path = NULL;
9597 char *id_str = NULL, *label_orig = NULL;
9598 int local_changes_subsumed;
9599 struct stat sb;
9600 int fd1 = -1, fd2 = -1;
9602 if (staged_status != GOT_STATUS_ADD &&
9603 staged_status != GOT_STATUS_MODIFY &&
9604 staged_status != GOT_STATUS_DELETE)
9605 return NULL;
9607 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9608 if (ie == NULL)
9609 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9611 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9612 == -1)
9613 return got_error_from_errno("asprintf");
9615 err = got_object_id_str(&id_str,
9616 commit_id ? commit_id : a->worktree->base_commit_id);
9617 if (err)
9618 goto done;
9619 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9620 id_str) == -1) {
9621 err = got_error_from_errno("asprintf");
9622 goto done;
9625 fd1 = got_opentempfd();
9626 if (fd1 == -1) {
9627 err = got_error_from_errno("got_opentempfd");
9628 goto done;
9630 fd2 = got_opentempfd();
9631 if (fd2 == -1) {
9632 err = got_error_from_errno("got_opentempfd");
9633 goto done;
9636 switch (staged_status) {
9637 case GOT_STATUS_MODIFY:
9638 err = got_object_open_as_blob(&blob_base, a->repo,
9639 blob_id, 8192, fd1);
9640 if (err)
9641 break;
9642 /* fall through */
9643 case GOT_STATUS_ADD:
9644 if (a->patch_cb) {
9645 if (staged_status == GOT_STATUS_ADD) {
9646 int choice = GOT_PATCH_CHOICE_NONE;
9647 err = (*a->patch_cb)(&choice, a->patch_arg,
9648 staged_status, ie->path, NULL, 1, 1);
9649 if (err)
9650 break;
9651 if (choice != GOT_PATCH_CHOICE_YES)
9652 break;
9653 } else {
9654 err = unstage_hunks(staged_blob_id,
9655 blob_base, blob_id, ie, ondisk_path,
9656 label_orig, a->worktree, a->repo,
9657 a->patch_cb, a->patch_arg,
9658 a->progress_cb, a->progress_arg);
9659 break; /* Done with this file. */
9662 err = got_object_open_as_blob(&blob_staged, a->repo,
9663 staged_blob_id, 8192, fd2);
9664 if (err)
9665 break;
9666 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9667 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9668 case GOT_FILEIDX_MODE_REGULAR_FILE:
9669 err = merge_blob(&local_changes_subsumed, a->worktree,
9670 blob_base, ondisk_path, relpath,
9671 got_fileindex_perms_to_st(ie), label_orig,
9672 blob_staged, commit_id ? commit_id :
9673 a->worktree->base_commit_id, a->repo,
9674 a->progress_cb, a->progress_arg);
9675 break;
9676 case GOT_FILEIDX_MODE_SYMLINK:
9677 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9678 char *staged_target;
9679 err = got_object_blob_read_to_str(
9680 &staged_target, blob_staged);
9681 if (err)
9682 goto done;
9683 err = merge_symlink(a->worktree, blob_base,
9684 ondisk_path, relpath, label_orig,
9685 staged_target, commit_id ? commit_id :
9686 a->worktree->base_commit_id,
9687 a->repo, a->progress_cb, a->progress_arg);
9688 free(staged_target);
9689 } else {
9690 err = merge_blob(&local_changes_subsumed,
9691 a->worktree, blob_base, ondisk_path,
9692 relpath, got_fileindex_perms_to_st(ie),
9693 label_orig, blob_staged,
9694 commit_id ? commit_id :
9695 a->worktree->base_commit_id, a->repo,
9696 a->progress_cb, a->progress_arg);
9698 break;
9699 default:
9700 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9701 break;
9703 if (err == NULL) {
9704 got_fileindex_entry_stage_set(ie,
9705 GOT_FILEIDX_STAGE_NONE);
9706 got_fileindex_entry_staged_filetype_set(ie, 0);
9708 break;
9709 case GOT_STATUS_DELETE:
9710 if (a->patch_cb) {
9711 int choice = GOT_PATCH_CHOICE_NONE;
9712 err = (*a->patch_cb)(&choice, a->patch_arg,
9713 staged_status, ie->path, NULL, 1, 1);
9714 if (err)
9715 break;
9716 if (choice == GOT_PATCH_CHOICE_NO)
9717 break;
9718 if (choice != GOT_PATCH_CHOICE_YES) {
9719 err = got_error(GOT_ERR_PATCH_CHOICE);
9720 break;
9723 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9724 got_fileindex_entry_staged_filetype_set(ie, 0);
9725 err = get_file_status(&status, &sb, ie, ondisk_path,
9726 dirfd, de_name, a->repo);
9727 if (err)
9728 break;
9729 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9730 break;
9732 done:
9733 free(ondisk_path);
9734 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9735 err = got_error_from_errno("close");
9736 if (blob_base)
9737 got_object_blob_close(blob_base);
9738 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9739 err = got_error_from_errno("close");
9740 if (blob_staged)
9741 got_object_blob_close(blob_staged);
9742 free(id_str);
9743 free(label_orig);
9744 return err;
9747 const struct got_error *
9748 got_worktree_unstage(struct got_worktree *worktree,
9749 struct got_pathlist_head *paths,
9750 got_worktree_checkout_cb progress_cb, void *progress_arg,
9751 got_worktree_patch_cb patch_cb, void *patch_arg,
9752 struct got_repository *repo)
9754 const struct got_error *err = NULL, *sync_err, *unlockerr;
9755 struct got_pathlist_entry *pe;
9756 struct got_fileindex *fileindex = NULL;
9757 char *fileindex_path = NULL;
9758 struct unstage_path_arg upa;
9760 err = lock_worktree(worktree, LOCK_EX);
9761 if (err)
9762 return err;
9764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9765 if (err)
9766 goto done;
9768 upa.worktree = worktree;
9769 upa.fileindex = fileindex;
9770 upa.repo = repo;
9771 upa.progress_cb = progress_cb;
9772 upa.progress_arg = progress_arg;
9773 upa.patch_cb = patch_cb;
9774 upa.patch_arg = patch_arg;
9775 TAILQ_FOREACH(pe, paths, entry) {
9776 err = worktree_status(worktree, pe->path, fileindex, repo,
9777 unstage_path, &upa, NULL, NULL, 1, 0);
9778 if (err)
9779 goto done;
9782 sync_err = sync_fileindex(fileindex, fileindex_path);
9783 if (sync_err && err == NULL)
9784 err = sync_err;
9785 done:
9786 free(fileindex_path);
9787 if (fileindex)
9788 got_fileindex_free(fileindex);
9789 unlockerr = lock_worktree(worktree, LOCK_SH);
9790 if (unlockerr && err == NULL)
9791 err = unlockerr;
9792 return err;
9795 struct report_file_info_arg {
9796 struct got_worktree *worktree;
9797 got_worktree_path_info_cb info_cb;
9798 void *info_arg;
9799 struct got_pathlist_head *paths;
9800 got_cancel_cb cancel_cb;
9801 void *cancel_arg;
9804 static const struct got_error *
9805 report_file_info(void *arg, struct got_fileindex_entry *ie)
9807 const struct got_error *err;
9808 struct report_file_info_arg *a = arg;
9809 struct got_pathlist_entry *pe;
9810 struct got_object_id blob_id, staged_blob_id, commit_id;
9811 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9812 struct got_object_id *commit_idp = NULL;
9813 int stage;
9815 if (a->cancel_cb) {
9816 err = a->cancel_cb(a->cancel_arg);
9817 if (err)
9818 return err;
9821 TAILQ_FOREACH(pe, a->paths, entry) {
9822 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9823 got_path_is_child(ie->path, pe->path, pe->path_len))
9824 break;
9826 if (pe == NULL) /* not found */
9827 return NULL;
9829 if (got_fileindex_entry_has_blob(ie))
9830 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9831 stage = got_fileindex_entry_stage_get(ie);
9832 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9833 stage == GOT_FILEIDX_STAGE_ADD) {
9834 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9835 &staged_blob_id, ie);
9838 if (got_fileindex_entry_has_commit(ie))
9839 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9841 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9842 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9845 const struct got_error *
9846 got_worktree_path_info(struct got_worktree *worktree,
9847 struct got_pathlist_head *paths,
9848 got_worktree_path_info_cb info_cb, void *info_arg,
9849 got_cancel_cb cancel_cb, void *cancel_arg)
9852 const struct got_error *err = NULL, *unlockerr;
9853 struct got_fileindex *fileindex = NULL;
9854 char *fileindex_path = NULL;
9855 struct report_file_info_arg arg;
9857 err = lock_worktree(worktree, LOCK_SH);
9858 if (err)
9859 return err;
9861 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9862 if (err)
9863 goto done;
9865 arg.worktree = worktree;
9866 arg.info_cb = info_cb;
9867 arg.info_arg = info_arg;
9868 arg.paths = paths;
9869 arg.cancel_cb = cancel_cb;
9870 arg.cancel_arg = cancel_arg;
9871 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9872 &arg);
9873 done:
9874 free(fileindex_path);
9875 if (fileindex)
9876 got_fileindex_free(fileindex);
9877 unlockerr = lock_worktree(worktree, LOCK_UN);
9878 if (unlockerr && err == NULL)
9879 err = unlockerr;
9880 return err;
9883 static const struct got_error *
9884 patch_check_path(const char *p, char **path, unsigned char *status,
9885 unsigned char *staged_status, struct got_fileindex *fileindex,
9886 struct got_worktree *worktree, struct got_repository *repo)
9888 const struct got_error *err;
9889 struct got_fileindex_entry *ie;
9890 struct stat sb;
9891 char *ondisk_path = NULL;
9893 err = got_worktree_resolve_path(path, worktree, p);
9894 if (err)
9895 return err;
9897 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9898 *path[0] ? "/" : "", *path) == -1)
9899 return got_error_from_errno("asprintf");
9901 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9902 if (ie) {
9903 *staged_status = get_staged_status(ie);
9904 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9905 repo);
9906 if (err)
9907 goto done;
9908 } else {
9909 *staged_status = GOT_STATUS_NO_CHANGE;
9910 *status = GOT_STATUS_UNVERSIONED;
9911 if (lstat(ondisk_path, &sb) == -1) {
9912 if (errno != ENOENT) {
9913 err = got_error_from_errno2("lstat",
9914 ondisk_path);
9915 goto done;
9917 *status = GOT_STATUS_NONEXISTENT;
9921 done:
9922 free(ondisk_path);
9923 return err;
9926 static const struct got_error *
9927 patch_can_rm(const char *path, unsigned char status,
9928 unsigned char staged_status)
9930 if (status == GOT_STATUS_NONEXISTENT)
9931 return got_error_set_errno(ENOENT, path);
9932 if (status != GOT_STATUS_NO_CHANGE &&
9933 status != GOT_STATUS_ADD &&
9934 status != GOT_STATUS_MODIFY &&
9935 status != GOT_STATUS_MODE_CHANGE)
9936 return got_error_path(path, GOT_ERR_FILE_STATUS);
9937 if (staged_status == GOT_STATUS_DELETE)
9938 return got_error_path(path, GOT_ERR_FILE_STATUS);
9939 return NULL;
9942 static const struct got_error *
9943 patch_can_add(const char *path, unsigned char status)
9945 if (status != GOT_STATUS_NONEXISTENT)
9946 return got_error_path(path, GOT_ERR_FILE_STATUS);
9947 return NULL;
9950 static const struct got_error *
9951 patch_can_edit(const char *path, unsigned char status,
9952 unsigned char staged_status)
9954 if (status == GOT_STATUS_NONEXISTENT)
9955 return got_error_set_errno(ENOENT, path);
9956 if (status != GOT_STATUS_NO_CHANGE &&
9957 status != GOT_STATUS_ADD &&
9958 status != GOT_STATUS_MODIFY)
9959 return got_error_path(path, GOT_ERR_FILE_STATUS);
9960 if (staged_status == GOT_STATUS_DELETE)
9961 return got_error_path(path, GOT_ERR_FILE_STATUS);
9962 return NULL;
9965 const struct got_error *
9966 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9967 char **fileindex_path, struct got_worktree *worktree)
9969 return open_fileindex(fileindex, fileindex_path, worktree);
9972 const struct got_error *
9973 got_worktree_patch_check_path(const char *old, const char *new,
9974 char **oldpath, char **newpath, struct got_worktree *worktree,
9975 struct got_repository *repo, struct got_fileindex *fileindex)
9977 const struct got_error *err = NULL;
9978 int file_renamed = 0;
9979 unsigned char status_old, staged_status_old;
9980 unsigned char status_new, staged_status_new;
9982 *oldpath = NULL;
9983 *newpath = NULL;
9985 err = patch_check_path(old != NULL ? old : new, oldpath,
9986 &status_old, &staged_status_old, fileindex, worktree, repo);
9987 if (err)
9988 goto done;
9990 err = patch_check_path(new != NULL ? new : old, newpath,
9991 &status_new, &staged_status_new, fileindex, worktree, repo);
9992 if (err)
9993 goto done;
9995 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9996 file_renamed = 1;
9998 if (old != NULL && new == NULL)
9999 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10000 else if (file_renamed) {
10001 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10002 if (err == NULL)
10003 err = patch_can_add(*newpath, status_new);
10004 } else if (old == NULL)
10005 err = patch_can_add(*newpath, status_new);
10006 else
10007 err = patch_can_edit(*newpath, status_new, staged_status_new);
10009 done:
10010 if (err) {
10011 free(*oldpath);
10012 *oldpath = NULL;
10013 free(*newpath);
10014 *newpath = NULL;
10016 return err;
10019 const struct got_error *
10020 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10021 struct got_worktree *worktree, struct got_fileindex *fileindex,
10022 got_worktree_checkout_cb progress_cb, void *progress_arg)
10024 struct schedule_addition_args saa;
10026 memset(&saa, 0, sizeof(saa));
10027 saa.worktree = worktree;
10028 saa.fileindex = fileindex;
10029 saa.progress_cb = progress_cb;
10030 saa.progress_arg = progress_arg;
10031 saa.repo = repo;
10033 return worktree_status(worktree, path, fileindex, repo,
10034 schedule_addition, &saa, NULL, NULL, 1, 0);
10037 const struct got_error *
10038 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10039 struct got_worktree *worktree, struct got_fileindex *fileindex,
10040 got_worktree_delete_cb progress_cb, void *progress_arg)
10042 const struct got_error *err;
10043 struct schedule_deletion_args sda;
10044 char *ondisk_status_path;
10046 memset(&sda, 0, sizeof(sda));
10047 sda.worktree = worktree;
10048 sda.fileindex = fileindex;
10049 sda.progress_cb = progress_cb;
10050 sda.progress_arg = progress_arg;
10051 sda.repo = repo;
10052 sda.delete_local_mods = 0;
10053 sda.keep_on_disk = 0;
10054 sda.ignore_missing_paths = 0;
10055 sda.status_codes = NULL;
10056 if (asprintf(&ondisk_status_path, "%s/%s",
10057 got_worktree_get_root_path(worktree), path) == -1)
10058 return got_error_from_errno("asprintf");
10059 sda.status_path = ondisk_status_path;
10060 sda.status_path_len = strlen(ondisk_status_path);
10062 err = worktree_status(worktree, path, fileindex, repo,
10063 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10064 free(ondisk_status_path);
10065 return err;
10068 const struct got_error *
10069 got_worktree_patch_complete(struct got_fileindex *fileindex,
10070 const char *fileindex_path)
10072 const struct got_error *err = NULL;
10074 err = sync_fileindex(fileindex, fileindex_path);
10075 got_fileindex_free(fileindex);
10077 return err;