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_PATH);
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);
3778 return err;
3781 static int
3782 match_path(const char *pattern, size_t pattern_len, const char *path,
3783 int flags)
3785 char buf[PATH_MAX];
3788 * Trailing slashes signify directories.
3789 * Append a * to make such patterns conform to fnmatch rules.
3791 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3792 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3793 return FNM_NOMATCH; /* XXX */
3795 return fnmatch(buf, path, flags);
3798 return fnmatch(pattern, path, flags);
3801 static int
3802 match_ignores(struct got_pathlist_head *ignores, const char *path)
3804 struct got_pathlist_entry *pe;
3806 /* Handle patterns which match in all directories. */
3807 TAILQ_FOREACH(pe, ignores, entry) {
3808 struct got_pathlist_head *ignorelist = pe->data;
3809 struct got_pathlist_entry *pi;
3811 TAILQ_FOREACH(pi, ignorelist, entry) {
3812 const char *p;
3814 if (pi->path_len < 3 ||
3815 strncmp(pi->path, "**/", 3) != 0)
3816 continue;
3817 p = path;
3818 while (*p) {
3819 if (match_path(pi->path + 3,
3820 pi->path_len - 3, p,
3821 FNM_PATHNAME | FNM_LEADING_DIR)) {
3822 /* Retry in next directory. */
3823 while (*p && *p != '/')
3824 p++;
3825 while (*p == '/')
3826 p++;
3827 continue;
3829 return 1;
3835 * The ignores pathlist contains ignore lists from children before
3836 * parents, so we can find the most specific ignorelist by walking
3837 * ignores backwards.
3839 pe = TAILQ_LAST(ignores, got_pathlist_head);
3840 while (pe) {
3841 if (got_path_is_child(path, pe->path, pe->path_len)) {
3842 struct got_pathlist_head *ignorelist = pe->data;
3843 struct got_pathlist_entry *pi;
3844 TAILQ_FOREACH(pi, ignorelist, entry) {
3845 int flags = FNM_LEADING_DIR;
3846 if (strstr(pi->path, "/**/") == NULL)
3847 flags |= FNM_PATHNAME;
3848 if (match_path(pi->path, pi->path_len,
3849 path, flags))
3850 continue;
3851 return 1;
3854 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3857 return 0;
3860 static const struct got_error *
3861 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3862 const char *path, int dirfd, const char *ignores_filename)
3864 const struct got_error *err = NULL;
3865 char *ignorespath;
3866 int fd = -1;
3867 FILE *ignoresfile = NULL;
3869 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3870 path[0] ? "/" : "", ignores_filename) == -1)
3871 return got_error_from_errno("asprintf");
3873 if (dirfd != -1) {
3874 fd = openat(dirfd, ignores_filename,
3875 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3876 if (fd == -1) {
3877 if (errno != ENOENT && errno != EACCES)
3878 err = got_error_from_errno2("openat",
3879 ignorespath);
3880 } else {
3881 ignoresfile = fdopen(fd, "r");
3882 if (ignoresfile == NULL)
3883 err = got_error_from_errno2("fdopen",
3884 ignorespath);
3885 else {
3886 fd = -1;
3887 err = read_ignores(ignores, path, ignoresfile);
3890 } else {
3891 ignoresfile = fopen(ignorespath, "re");
3892 if (ignoresfile == NULL) {
3893 if (errno != ENOENT && errno != EACCES)
3894 err = got_error_from_errno2("fopen",
3895 ignorespath);
3896 } else
3897 err = read_ignores(ignores, path, ignoresfile);
3900 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3901 err = got_error_from_errno2("fclose", path);
3902 if (fd != -1 && close(fd) == -1 && err == NULL)
3903 err = got_error_from_errno2("close", path);
3904 free(ignorespath);
3905 return err;
3908 static const struct got_error *
3909 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3910 int dirfd)
3912 const struct got_error *err = NULL;
3913 struct diff_dir_cb_arg *a = arg;
3914 char *path = NULL;
3916 if (ignore != NULL)
3917 *ignore = 0;
3919 if (a->cancel_cb) {
3920 err = a->cancel_cb(a->cancel_arg);
3921 if (err)
3922 return err;
3925 if (parent_path[0]) {
3926 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3927 return got_error_from_errno("asprintf");
3928 } else {
3929 path = de->d_name;
3932 if (de->d_type == DT_DIR) {
3933 if (!a->no_ignores && ignore != NULL &&
3934 match_ignores(a->ignores, path))
3935 *ignore = 1;
3936 } else if (!match_ignores(a->ignores, path) &&
3937 got_path_is_child(path, a->status_path, a->status_path_len))
3938 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3939 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3940 if (parent_path[0])
3941 free(path);
3942 return err;
3945 static const struct got_error *
3946 status_traverse(void *arg, const char *path, int dirfd)
3948 const struct got_error *err = NULL;
3949 struct diff_dir_cb_arg *a = arg;
3951 if (a->no_ignores)
3952 return NULL;
3954 err = add_ignores(a->ignores, a->worktree->root_path,
3955 path, dirfd, ".cvsignore");
3956 if (err)
3957 return err;
3959 err = add_ignores(a->ignores, a->worktree->root_path, path,
3960 dirfd, ".gitignore");
3962 return err;
3965 static const struct got_error *
3966 report_single_file_status(const char *path, const char *ondisk_path,
3967 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3968 void *status_arg, struct got_repository *repo, int report_unchanged,
3969 struct got_pathlist_head *ignores, int no_ignores)
3971 struct got_fileindex_entry *ie;
3972 struct stat sb;
3974 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3975 if (ie)
3976 return report_file_status(ie, ondisk_path, -1, NULL,
3977 status_cb, status_arg, repo, report_unchanged);
3979 if (lstat(ondisk_path, &sb) == -1) {
3980 if (errno != ENOENT)
3981 return got_error_from_errno2("lstat", ondisk_path);
3982 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3983 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3986 if (!no_ignores && match_ignores(ignores, path))
3987 return NULL;
3989 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3990 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3991 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3993 return NULL;
3996 static const struct got_error *
3997 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3998 const char *root_path, const char *path)
4000 const struct got_error *err;
4001 char *parent_path, *next_parent_path = NULL;
4003 err = add_ignores(ignores, root_path, "", -1,
4004 ".cvsignore");
4005 if (err)
4006 return err;
4008 err = add_ignores(ignores, root_path, "", -1,
4009 ".gitignore");
4010 if (err)
4011 return err;
4013 err = got_path_dirname(&parent_path, path);
4014 if (err) {
4015 if (err->code == GOT_ERR_BAD_PATH)
4016 return NULL; /* cannot traverse parent */
4017 return err;
4019 for (;;) {
4020 err = add_ignores(ignores, root_path, parent_path, -1,
4021 ".cvsignore");
4022 if (err)
4023 break;
4024 err = add_ignores(ignores, root_path, parent_path, -1,
4025 ".gitignore");
4026 if (err)
4027 break;
4028 err = got_path_dirname(&next_parent_path, parent_path);
4029 if (err) {
4030 if (err->code == GOT_ERR_BAD_PATH)
4031 err = NULL; /* traversed everything */
4032 break;
4034 if (got_path_is_root_dir(parent_path))
4035 break;
4036 free(parent_path);
4037 parent_path = next_parent_path;
4038 next_parent_path = NULL;
4041 free(parent_path);
4042 free(next_parent_path);
4043 return err;
4046 struct find_missing_children_args {
4047 const char *parent_path;
4048 size_t parent_len;
4049 struct got_pathlist_head *children;
4050 got_cancel_cb cancel_cb;
4051 void *cancel_arg;
4054 static const struct got_error *
4055 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4057 const struct got_error *err = NULL;
4058 struct find_missing_children_args *a = arg;
4060 if (a->cancel_cb) {
4061 err = a->cancel_cb(a->cancel_arg);
4062 if (err)
4063 return err;
4066 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4067 err = got_pathlist_append(a->children, ie->path, NULL);
4069 return err;
4072 static const struct got_error *
4073 report_children(struct got_pathlist_head *children,
4074 struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 struct got_repository *repo, int is_root_dir, int report_unchanged,
4076 struct got_pathlist_head *ignores, int no_ignores,
4077 got_worktree_status_cb status_cb, void *status_arg,
4078 got_cancel_cb cancel_cb, void *cancel_arg)
4080 const struct got_error *err = NULL;
4081 struct got_pathlist_entry *pe;
4082 char *ondisk_path = NULL;
4084 TAILQ_FOREACH(pe, children, entry) {
4085 if (cancel_cb) {
4086 err = cancel_cb(cancel_arg);
4087 if (err)
4088 break;
4091 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4092 !is_root_dir ? "/" : "", pe->path) == -1) {
4093 err = got_error_from_errno("asprintf");
4094 ondisk_path = NULL;
4095 break;
4098 err = report_single_file_status(pe->path, ondisk_path,
4099 fileindex, status_cb, status_arg, repo, report_unchanged,
4100 ignores, no_ignores);
4101 if (err)
4102 break;
4104 free(ondisk_path);
4105 ondisk_path = NULL;
4108 free(ondisk_path);
4109 return err;
4112 static const struct got_error *
4113 worktree_status(struct got_worktree *worktree, const char *path,
4114 struct got_fileindex *fileindex, struct got_repository *repo,
4115 got_worktree_status_cb status_cb, void *status_arg,
4116 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4117 int report_unchanged)
4119 const struct got_error *err = NULL;
4120 int fd = -1;
4121 struct got_fileindex_diff_dir_cb fdiff_cb;
4122 struct diff_dir_cb_arg arg;
4123 char *ondisk_path = NULL;
4124 struct got_pathlist_head ignores, missing_children;
4125 struct got_fileindex_entry *ie;
4127 TAILQ_INIT(&ignores);
4128 TAILQ_INIT(&missing_children);
4130 if (asprintf(&ondisk_path, "%s%s%s",
4131 worktree->root_path, path[0] ? "/" : "", path) == -1)
4132 return got_error_from_errno("asprintf");
4134 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4135 if (ie) {
4136 err = report_single_file_status(path, ondisk_path,
4137 fileindex, status_cb, status_arg, repo,
4138 report_unchanged, &ignores, no_ignores);
4139 goto done;
4140 } else {
4141 struct find_missing_children_args fmca;
4142 fmca.parent_path = path;
4143 fmca.parent_len = strlen(path);
4144 fmca.children = &missing_children;
4145 fmca.cancel_cb = cancel_cb;
4146 fmca.cancel_arg = cancel_arg;
4147 err = got_fileindex_for_each_entry_safe(fileindex,
4148 find_missing_children, &fmca);
4149 if (err)
4150 goto done;
4153 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4154 if (fd == -1) {
4155 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4156 !got_err_open_nofollow_on_symlink())
4157 err = got_error_from_errno2("open", ondisk_path);
4158 else {
4159 if (!no_ignores) {
4160 err = add_ignores_from_parent_paths(&ignores,
4161 worktree->root_path, ondisk_path);
4162 if (err)
4163 goto done;
4165 if (TAILQ_EMPTY(&missing_children)) {
4166 err = report_single_file_status(path,
4167 ondisk_path, fileindex,
4168 status_cb, status_arg, repo,
4169 report_unchanged, &ignores, no_ignores);
4170 if (err)
4171 goto done;
4172 } else {
4173 err = report_children(&missing_children,
4174 worktree, fileindex, repo,
4175 (path[0] == '\0'), report_unchanged,
4176 &ignores, no_ignores,
4177 status_cb, status_arg,
4178 cancel_cb, cancel_arg);
4179 if (err)
4180 goto done;
4183 } else {
4184 fdiff_cb.diff_old_new = status_old_new;
4185 fdiff_cb.diff_old = status_old;
4186 fdiff_cb.diff_new = status_new;
4187 fdiff_cb.diff_traverse = status_traverse;
4188 arg.fileindex = fileindex;
4189 arg.worktree = worktree;
4190 arg.status_path = path;
4191 arg.status_path_len = strlen(path);
4192 arg.repo = repo;
4193 arg.status_cb = status_cb;
4194 arg.status_arg = status_arg;
4195 arg.cancel_cb = cancel_cb;
4196 arg.cancel_arg = cancel_arg;
4197 arg.report_unchanged = report_unchanged;
4198 arg.no_ignores = no_ignores;
4199 if (!no_ignores) {
4200 err = add_ignores_from_parent_paths(&ignores,
4201 worktree->root_path, path);
4202 if (err)
4203 goto done;
4205 arg.ignores = &ignores;
4206 err = got_fileindex_diff_dir(fileindex, fd,
4207 worktree->root_path, path, repo, &fdiff_cb, &arg);
4209 done:
4210 free_ignores(&ignores);
4211 if (fd != -1 && close(fd) == -1 && err == NULL)
4212 err = got_error_from_errno("close");
4213 free(ondisk_path);
4214 return err;
4217 const struct got_error *
4218 got_worktree_status(struct got_worktree *worktree,
4219 struct got_pathlist_head *paths, struct got_repository *repo,
4220 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4221 got_cancel_cb cancel_cb, void *cancel_arg)
4223 const struct got_error *err = NULL;
4224 char *fileindex_path = NULL;
4225 struct got_fileindex *fileindex = NULL;
4226 struct got_pathlist_entry *pe;
4228 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4229 if (err)
4230 return err;
4232 TAILQ_FOREACH(pe, paths, entry) {
4233 err = worktree_status(worktree, pe->path, fileindex, repo,
4234 status_cb, status_arg, cancel_cb, cancel_arg,
4235 no_ignores, 0);
4236 if (err)
4237 break;
4239 free(fileindex_path);
4240 got_fileindex_free(fileindex);
4241 return err;
4244 const struct got_error *
4245 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4246 const char *arg)
4248 const struct got_error *err = NULL;
4249 char *resolved = NULL, *cwd = NULL, *path = NULL;
4250 size_t len;
4251 struct stat sb;
4252 char *abspath = NULL;
4253 char canonpath[PATH_MAX];
4255 *wt_path = NULL;
4257 cwd = getcwd(NULL, 0);
4258 if (cwd == NULL)
4259 return got_error_from_errno("getcwd");
4261 if (lstat(arg, &sb) == -1) {
4262 if (errno != ENOENT) {
4263 err = got_error_from_errno2("lstat", arg);
4264 goto done;
4266 sb.st_mode = 0;
4268 if (S_ISLNK(sb.st_mode)) {
4270 * We cannot use realpath(3) with symlinks since we want to
4271 * operate on the symlink itself.
4272 * But we can make the path absolute, assuming it is relative
4273 * to the current working directory, and then canonicalize it.
4275 if (!got_path_is_absolute(arg)) {
4276 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4277 err = got_error_from_errno("asprintf");
4278 goto done;
4282 err = got_canonpath(abspath ? abspath : arg, canonpath,
4283 sizeof(canonpath));
4284 if (err)
4285 goto done;
4286 resolved = strdup(canonpath);
4287 if (resolved == NULL) {
4288 err = got_error_from_errno("strdup");
4289 goto done;
4291 } else {
4292 resolved = realpath(arg, NULL);
4293 if (resolved == NULL) {
4294 if (errno != ENOENT) {
4295 err = got_error_from_errno2("realpath", arg);
4296 goto done;
4298 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4299 err = got_error_from_errno("asprintf");
4300 goto done;
4302 err = got_canonpath(abspath, canonpath,
4303 sizeof(canonpath));
4304 if (err)
4305 goto done;
4306 resolved = strdup(canonpath);
4307 if (resolved == NULL) {
4308 err = got_error_from_errno("strdup");
4309 goto done;
4314 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4315 strlen(got_worktree_get_root_path(worktree)))) {
4316 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4317 goto done;
4320 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4321 err = got_path_skip_common_ancestor(&path,
4322 got_worktree_get_root_path(worktree), resolved);
4323 if (err)
4324 goto done;
4325 } else {
4326 path = strdup("");
4327 if (path == NULL) {
4328 err = got_error_from_errno("strdup");
4329 goto done;
4333 /* XXX status walk can't deal with trailing slash! */
4334 len = strlen(path);
4335 while (len > 0 && path[len - 1] == '/') {
4336 path[len - 1] = '\0';
4337 len--;
4339 done:
4340 free(abspath);
4341 free(resolved);
4342 free(cwd);
4343 if (err == NULL)
4344 *wt_path = path;
4345 else
4346 free(path);
4347 return err;
4350 struct schedule_addition_args {
4351 struct got_worktree *worktree;
4352 struct got_fileindex *fileindex;
4353 got_worktree_checkout_cb progress_cb;
4354 void *progress_arg;
4355 struct got_repository *repo;
4358 static int
4359 add_noop_status(unsigned char status)
4361 return (status == GOT_STATUS_ADD ||
4362 status == GOT_STATUS_MODIFY ||
4363 status == GOT_STATUS_CONFLICT ||
4364 status == GOT_STATUS_MODE_CHANGE ||
4365 status == GOT_STATUS_NO_CHANGE);
4368 static const struct got_error *
4369 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4370 const char *relpath, struct got_object_id *blob_id,
4371 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4372 int dirfd, const char *de_name)
4374 struct schedule_addition_args *a = arg;
4375 const struct got_error *err = NULL;
4376 struct got_fileindex_entry *ie;
4377 struct stat sb;
4378 char *ondisk_path;
4380 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4381 relpath) == -1)
4382 return got_error_from_errno("asprintf");
4384 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4385 if (ie) {
4386 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4387 de_name, a->repo);
4388 if (err)
4389 goto done;
4390 /* Re-adding an existing entry is a no-op. */
4391 if (staged_status == GOT_STATUS_NO_CHANGE &&
4392 add_noop_status(status))
4393 goto done;
4394 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4395 if (err)
4396 goto done;
4399 if (status != GOT_STATUS_UNVERSIONED) {
4400 if (status == GOT_STATUS_NONEXISTENT)
4401 err = got_error_set_errno(ENOENT, ondisk_path);
4402 else
4403 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4404 goto done;
4407 err = got_fileindex_entry_alloc(&ie, relpath);
4408 if (err)
4409 goto done;
4410 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4411 relpath, NULL, NULL, 1);
4412 if (err) {
4413 got_fileindex_entry_free(ie);
4414 goto done;
4416 err = got_fileindex_entry_add(a->fileindex, ie);
4417 if (err) {
4418 got_fileindex_entry_free(ie);
4419 goto done;
4421 done:
4422 free(ondisk_path);
4423 if (err)
4424 return err;
4425 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4426 return NULL;
4427 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4430 const struct got_error *
4431 got_worktree_schedule_add(struct got_worktree *worktree,
4432 struct got_pathlist_head *paths,
4433 got_worktree_checkout_cb progress_cb, void *progress_arg,
4434 struct got_repository *repo, int no_ignores)
4436 struct got_fileindex *fileindex = NULL;
4437 char *fileindex_path = NULL;
4438 const struct got_error *err = NULL, *sync_err, *unlockerr;
4439 struct got_pathlist_entry *pe;
4440 struct schedule_addition_args saa;
4442 err = lock_worktree(worktree, LOCK_EX);
4443 if (err)
4444 return err;
4446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4447 if (err)
4448 goto done;
4450 saa.worktree = worktree;
4451 saa.fileindex = fileindex;
4452 saa.progress_cb = progress_cb;
4453 saa.progress_arg = progress_arg;
4454 saa.repo = repo;
4456 TAILQ_FOREACH(pe, paths, entry) {
4457 err = worktree_status(worktree, pe->path, fileindex, repo,
4458 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4459 if (err)
4460 break;
4462 sync_err = sync_fileindex(fileindex, fileindex_path);
4463 if (sync_err && err == NULL)
4464 err = sync_err;
4465 done:
4466 free(fileindex_path);
4467 if (fileindex)
4468 got_fileindex_free(fileindex);
4469 unlockerr = lock_worktree(worktree, LOCK_SH);
4470 if (unlockerr && err == NULL)
4471 err = unlockerr;
4472 return err;
4475 struct schedule_deletion_args {
4476 struct got_worktree *worktree;
4477 struct got_fileindex *fileindex;
4478 got_worktree_delete_cb progress_cb;
4479 void *progress_arg;
4480 struct got_repository *repo;
4481 int delete_local_mods;
4482 int keep_on_disk;
4483 int ignore_missing_paths;
4484 const char *status_path;
4485 size_t status_path_len;
4486 const char *status_codes;
4489 static const struct got_error *
4490 schedule_for_deletion(void *arg, unsigned char status,
4491 unsigned char staged_status, const char *relpath,
4492 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4493 struct got_object_id *commit_id, int dirfd, const char *de_name)
4495 struct schedule_deletion_args *a = arg;
4496 const struct got_error *err = NULL;
4497 struct got_fileindex_entry *ie = NULL;
4498 struct stat sb;
4499 char *ondisk_path;
4501 if (status == GOT_STATUS_NONEXISTENT) {
4502 if (a->ignore_missing_paths)
4503 return NULL;
4504 return got_error_set_errno(ENOENT, relpath);
4507 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4508 if (ie == NULL)
4509 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4511 staged_status = get_staged_status(ie);
4512 if (staged_status != GOT_STATUS_NO_CHANGE) {
4513 if (staged_status == GOT_STATUS_DELETE)
4514 return NULL;
4515 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4518 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4519 relpath) == -1)
4520 return got_error_from_errno("asprintf");
4522 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4523 a->repo);
4524 if (err)
4525 goto done;
4527 if (a->status_codes) {
4528 size_t ncodes = strlen(a->status_codes);
4529 int i;
4530 for (i = 0; i < ncodes ; i++) {
4531 if (status == a->status_codes[i])
4532 break;
4534 if (i == ncodes) {
4535 /* Do not delete files in non-matching status. */
4536 free(ondisk_path);
4537 return NULL;
4539 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4540 a->status_codes[i] != GOT_STATUS_MISSING) {
4541 static char msg[64];
4542 snprintf(msg, sizeof(msg),
4543 "invalid status code '%c'", a->status_codes[i]);
4544 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4545 goto done;
4549 if (status != GOT_STATUS_NO_CHANGE) {
4550 if (status == GOT_STATUS_DELETE)
4551 goto done;
4552 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4553 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4554 goto done;
4556 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4557 err = got_error_set_errno(ENOENT, relpath);
4558 goto done;
4560 if (status != GOT_STATUS_MODIFY &&
4561 status != GOT_STATUS_MISSING) {
4562 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4563 goto done;
4567 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4568 size_t root_len;
4570 if (dirfd != -1) {
4571 if (unlinkat(dirfd, de_name, 0) == -1) {
4572 err = got_error_from_errno2("unlinkat",
4573 ondisk_path);
4574 goto done;
4576 } else if (unlink(ondisk_path) == -1) {
4577 err = got_error_from_errno2("unlink", ondisk_path);
4578 goto done;
4581 root_len = strlen(a->worktree->root_path);
4582 do {
4583 char *parent;
4585 err = got_path_dirname(&parent, ondisk_path);
4586 if (err)
4587 goto done;
4588 free(ondisk_path);
4589 ondisk_path = parent;
4590 if (got_path_cmp(ondisk_path, a->status_path,
4591 strlen(ondisk_path), a->status_path_len) != 0 &&
4592 !got_path_is_child(ondisk_path, a->status_path,
4593 a->status_path_len))
4594 break;
4595 if (rmdir(ondisk_path) == -1) {
4596 if (errno != ENOTEMPTY)
4597 err = got_error_from_errno2("rmdir",
4598 ondisk_path);
4599 break;
4601 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4602 strlen(ondisk_path), root_len) != 0);
4605 if (got_fileindex_entry_has_blob(ie))
4606 got_fileindex_entry_mark_deleted_from_disk(ie);
4607 else
4608 got_fileindex_entry_remove(a->fileindex, ie);
4609 done:
4610 free(ondisk_path);
4611 if (err)
4612 return err;
4613 if (status == GOT_STATUS_DELETE)
4614 return NULL;
4615 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4616 staged_status, relpath);
4619 const struct got_error *
4620 got_worktree_schedule_delete(struct got_worktree *worktree,
4621 struct got_pathlist_head *paths, int delete_local_mods,
4622 const char *status_codes,
4623 got_worktree_delete_cb progress_cb, void *progress_arg,
4624 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4626 struct got_fileindex *fileindex = NULL;
4627 char *fileindex_path = NULL;
4628 const struct got_error *err = NULL, *sync_err, *unlockerr;
4629 struct got_pathlist_entry *pe;
4630 struct schedule_deletion_args sda;
4632 err = lock_worktree(worktree, LOCK_EX);
4633 if (err)
4634 return err;
4636 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4637 if (err)
4638 goto done;
4640 sda.worktree = worktree;
4641 sda.fileindex = fileindex;
4642 sda.progress_cb = progress_cb;
4643 sda.progress_arg = progress_arg;
4644 sda.repo = repo;
4645 sda.delete_local_mods = delete_local_mods;
4646 sda.keep_on_disk = keep_on_disk;
4647 sda.ignore_missing_paths = ignore_missing_paths;
4648 sda.status_codes = status_codes;
4650 TAILQ_FOREACH(pe, paths, entry) {
4651 char *ondisk_status_path;
4653 if (asprintf(&ondisk_status_path, "%s%s%s",
4654 got_worktree_get_root_path(worktree),
4655 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4656 err = got_error_from_errno("asprintf");
4657 goto done;
4659 sda.status_path = ondisk_status_path;
4660 sda.status_path_len = strlen(ondisk_status_path);
4661 err = worktree_status(worktree, pe->path, fileindex, repo,
4662 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4663 free(ondisk_status_path);
4664 if (err)
4665 break;
4667 sync_err = sync_fileindex(fileindex, fileindex_path);
4668 if (sync_err && err == NULL)
4669 err = sync_err;
4670 done:
4671 free(fileindex_path);
4672 if (fileindex)
4673 got_fileindex_free(fileindex);
4674 unlockerr = lock_worktree(worktree, LOCK_SH);
4675 if (unlockerr && err == NULL)
4676 err = unlockerr;
4677 return err;
4680 static const struct got_error *
4681 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4683 const struct got_error *err = NULL;
4684 char *line = NULL;
4685 size_t linesize = 0, n;
4686 ssize_t linelen;
4688 linelen = getline(&line, &linesize, infile);
4689 if (linelen == -1) {
4690 if (ferror(infile)) {
4691 err = got_error_from_errno("getline");
4692 goto done;
4694 return NULL;
4696 if (outfile) {
4697 n = fwrite(line, 1, linelen, outfile);
4698 if (n != linelen) {
4699 err = got_ferror(outfile, GOT_ERR_IO);
4700 goto done;
4703 if (rejectfile) {
4704 n = fwrite(line, 1, linelen, rejectfile);
4705 if (n != linelen)
4706 err = got_ferror(rejectfile, GOT_ERR_IO);
4708 done:
4709 free(line);
4710 return err;
4713 static const struct got_error *
4714 skip_one_line(FILE *f)
4716 char *line = NULL;
4717 size_t linesize = 0;
4718 ssize_t linelen;
4720 linelen = getline(&line, &linesize, f);
4721 if (linelen == -1) {
4722 if (ferror(f))
4723 return got_error_from_errno("getline");
4724 return NULL;
4726 free(line);
4727 return NULL;
4730 static const struct got_error *
4731 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4732 int start_old, int end_old, int start_new, int end_new,
4733 FILE *outfile, FILE *rejectfile)
4735 const struct got_error *err;
4737 /* Copy old file's lines leading up to patch. */
4738 while (!feof(f1) && *line_cur1 < start_old) {
4739 err = copy_one_line(f1, outfile, NULL);
4740 if (err)
4741 return err;
4742 (*line_cur1)++;
4744 /* Skip new file's lines leading up to patch. */
4745 while (!feof(f2) && *line_cur2 < start_new) {
4746 if (rejectfile)
4747 err = copy_one_line(f2, NULL, rejectfile);
4748 else
4749 err = skip_one_line(f2);
4750 if (err)
4751 return err;
4752 (*line_cur2)++;
4754 /* Copy patched lines. */
4755 while (!feof(f2) && *line_cur2 <= end_new) {
4756 err = copy_one_line(f2, outfile, NULL);
4757 if (err)
4758 return err;
4759 (*line_cur2)++;
4761 /* Skip over old file's replaced lines. */
4762 while (!feof(f1) && *line_cur1 <= end_old) {
4763 if (rejectfile)
4764 err = copy_one_line(f1, NULL, rejectfile);
4765 else
4766 err = skip_one_line(f1);
4767 if (err)
4768 return err;
4769 (*line_cur1)++;
4772 return NULL;
4775 static const struct got_error *
4776 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4777 FILE *outfile, FILE *rejectfile)
4779 const struct got_error *err;
4781 if (outfile) {
4782 /* Copy old file's lines until EOF. */
4783 while (!feof(f1)) {
4784 err = copy_one_line(f1, outfile, NULL);
4785 if (err)
4786 return err;
4787 (*line_cur1)++;
4790 if (rejectfile) {
4791 /* Copy new file's lines until EOF. */
4792 while (!feof(f2)) {
4793 err = copy_one_line(f2, NULL, rejectfile);
4794 if (err)
4795 return err;
4796 (*line_cur2)++;
4800 return NULL;
4803 static const struct got_error *
4804 apply_or_reject_change(int *choice, int *nchunks_used,
4805 struct diff_result *diff_result, int n,
4806 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4807 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4808 got_worktree_patch_cb patch_cb, void *patch_arg)
4810 const struct got_error *err = NULL;
4811 struct diff_chunk_context cc = {};
4812 int start_old, end_old, start_new, end_new;
4813 FILE *hunkfile;
4814 struct diff_output_unidiff_state *diff_state;
4815 struct diff_input_info diff_info;
4816 int rc;
4818 *choice = GOT_PATCH_CHOICE_NONE;
4820 /* Get changed line numbers without context lines for copy_change(). */
4821 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4822 start_old = cc.left.start;
4823 end_old = cc.left.end;
4824 start_new = cc.right.start;
4825 end_new = cc.right.end;
4827 /* Get the same change with context lines for display. */
4828 memset(&cc, 0, sizeof(cc));
4829 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4831 memset(&diff_info, 0, sizeof(diff_info));
4832 diff_info.left_path = relpath;
4833 diff_info.right_path = relpath;
4835 diff_state = diff_output_unidiff_state_alloc();
4836 if (diff_state == NULL)
4837 return got_error_set_errno(ENOMEM,
4838 "diff_output_unidiff_state_alloc");
4840 hunkfile = got_opentemp();
4841 if (hunkfile == NULL) {
4842 err = got_error_from_errno("got_opentemp");
4843 goto done;
4846 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4847 diff_result, &cc);
4848 if (rc != DIFF_RC_OK) {
4849 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4850 goto done;
4853 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4854 err = got_ferror(hunkfile, GOT_ERR_IO);
4855 goto done;
4858 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4859 hunkfile, changeno, nchanges);
4860 if (err)
4861 goto done;
4863 switch (*choice) {
4864 case GOT_PATCH_CHOICE_YES:
4865 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4866 end_old, start_new, end_new, outfile, rejectfile);
4867 break;
4868 case GOT_PATCH_CHOICE_NO:
4869 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4870 end_old, start_new, end_new, rejectfile, outfile);
4871 break;
4872 case GOT_PATCH_CHOICE_QUIT:
4873 break;
4874 default:
4875 err = got_error(GOT_ERR_PATCH_CHOICE);
4876 break;
4878 done:
4879 diff_output_unidiff_state_free(diff_state);
4880 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4881 err = got_error_from_errno("fclose");
4882 return err;
4885 struct revert_file_args {
4886 struct got_worktree *worktree;
4887 struct got_fileindex *fileindex;
4888 got_worktree_checkout_cb progress_cb;
4889 void *progress_arg;
4890 got_worktree_patch_cb patch_cb;
4891 void *patch_arg;
4892 struct got_repository *repo;
4893 int unlink_added_files;
4894 struct got_pathlist_head *added_files_to_unlink;
4897 static const struct got_error *
4898 create_patched_content(char **path_outfile, int reverse_patch,
4899 struct got_object_id *blob_id, const char *path2,
4900 int dirfd2, const char *de_name2,
4901 const char *relpath, struct got_repository *repo,
4902 got_worktree_patch_cb patch_cb, void *patch_arg)
4904 const struct got_error *err, *free_err;
4905 struct got_blob_object *blob = NULL;
4906 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4907 int fd = -1, fd2 = -1;
4908 char link_target[PATH_MAX];
4909 ssize_t link_len = 0;
4910 char *path1 = NULL, *id_str = NULL;
4911 struct stat sb2;
4912 struct got_diffreg_result *diffreg_result = NULL;
4913 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4914 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4916 *path_outfile = NULL;
4918 err = got_object_id_str(&id_str, blob_id);
4919 if (err)
4920 return err;
4922 if (dirfd2 != -1) {
4923 fd2 = openat(dirfd2, de_name2,
4924 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4925 if (fd2 == -1) {
4926 if (!got_err_open_nofollow_on_symlink()) {
4927 err = got_error_from_errno2("openat", path2);
4928 goto done;
4930 link_len = readlinkat(dirfd2, de_name2,
4931 link_target, sizeof(link_target));
4932 if (link_len == -1) {
4933 return got_error_from_errno2("readlinkat",
4934 path2);
4936 sb2.st_mode = S_IFLNK;
4937 sb2.st_size = link_len;
4939 } else {
4940 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4941 if (fd2 == -1) {
4942 if (!got_err_open_nofollow_on_symlink()) {
4943 err = got_error_from_errno2("open", path2);
4944 goto done;
4946 link_len = readlink(path2, link_target,
4947 sizeof(link_target));
4948 if (link_len == -1)
4949 return got_error_from_errno2("readlink", path2);
4950 sb2.st_mode = S_IFLNK;
4951 sb2.st_size = link_len;
4954 if (fd2 != -1) {
4955 if (fstat(fd2, &sb2) == -1) {
4956 err = got_error_from_errno2("fstat", path2);
4957 goto done;
4960 f2 = fdopen(fd2, "r");
4961 if (f2 == NULL) {
4962 err = got_error_from_errno2("fdopen", path2);
4963 goto done;
4965 fd2 = -1;
4966 } else {
4967 size_t n;
4968 f2 = got_opentemp();
4969 if (f2 == NULL) {
4970 err = got_error_from_errno2("got_opentemp", path2);
4971 goto done;
4973 n = fwrite(link_target, 1, link_len, f2);
4974 if (n != link_len) {
4975 err = got_ferror(f2, GOT_ERR_IO);
4976 goto done;
4978 if (fflush(f2) == EOF) {
4979 err = got_error_from_errno("fflush");
4980 goto done;
4982 rewind(f2);
4985 fd = got_opentempfd();
4986 if (fd == -1) {
4987 err = got_error_from_errno("got_opentempfd");
4988 goto done;
4991 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4992 if (err)
4993 goto done;
4995 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4996 if (err)
4997 goto done;
4999 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5000 if (err)
5001 goto done;
5003 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5004 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5005 if (err)
5006 goto done;
5008 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5009 "");
5010 if (err)
5011 goto done;
5013 if (fseek(f1, 0L, SEEK_SET) == -1)
5014 return got_ferror(f1, GOT_ERR_IO);
5015 if (fseek(f2, 0L, SEEK_SET) == -1)
5016 return got_ferror(f2, GOT_ERR_IO);
5018 /* Count the number of actual changes in the diff result. */
5019 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5020 struct diff_chunk_context cc = {};
5021 diff_chunk_context_load_change(&cc, &nchunks_used,
5022 diffreg_result->result, n, 0);
5023 nchanges++;
5025 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5026 int choice;
5027 err = apply_or_reject_change(&choice, &nchunks_used,
5028 diffreg_result->result, n, relpath, f1, f2,
5029 &line_cur1, &line_cur2,
5030 reverse_patch ? NULL : outfile,
5031 reverse_patch ? outfile : NULL,
5032 ++i, nchanges, patch_cb, patch_arg);
5033 if (err)
5034 goto done;
5035 if (choice == GOT_PATCH_CHOICE_YES)
5036 have_content = 1;
5037 else if (choice == GOT_PATCH_CHOICE_QUIT)
5038 break;
5040 if (have_content) {
5041 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5042 reverse_patch ? NULL : outfile,
5043 reverse_patch ? outfile : NULL);
5044 if (err)
5045 goto done;
5047 if (!S_ISLNK(sb2.st_mode)) {
5048 mode_t mode;
5050 mode = apply_umask(sb2.st_mode);
5051 if (fchmod(fileno(outfile), mode) == -1) {
5052 err = got_error_from_errno2("fchmod", path2);
5053 goto done;
5057 done:
5058 free(id_str);
5059 if (fd != -1 && close(fd) == -1 && err == NULL)
5060 err = got_error_from_errno("close");
5061 if (blob)
5062 got_object_blob_close(blob);
5063 free_err = got_diffreg_result_free(diffreg_result);
5064 if (err == NULL)
5065 err = free_err;
5066 if (f1 && fclose(f1) == EOF && err == NULL)
5067 err = got_error_from_errno2("fclose", path1);
5068 if (f2 && fclose(f2) == EOF && err == NULL)
5069 err = got_error_from_errno2("fclose", path2);
5070 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5071 err = got_error_from_errno2("close", path2);
5072 if (outfile && fclose(outfile) == EOF && err == NULL)
5073 err = got_error_from_errno2("fclose", *path_outfile);
5074 if (path1 && unlink(path1) == -1 && err == NULL)
5075 err = got_error_from_errno2("unlink", path1);
5076 if (err || !have_content) {
5077 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5078 err = got_error_from_errno2("unlink", *path_outfile);
5079 free(*path_outfile);
5080 *path_outfile = NULL;
5082 free(path1);
5083 return err;
5086 static const struct got_error *
5087 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5088 const char *relpath, struct got_object_id *blob_id,
5089 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5090 int dirfd, const char *de_name)
5092 struct revert_file_args *a = arg;
5093 const struct got_error *err = NULL;
5094 char *parent_path = NULL;
5095 struct got_fileindex_entry *ie;
5096 struct got_commit_object *base_commit = NULL;
5097 struct got_tree_object *tree = NULL;
5098 struct got_object_id *tree_id = NULL;
5099 const struct got_tree_entry *te = NULL;
5100 char *tree_path = NULL, *te_name;
5101 char *ondisk_path = NULL, *path_content = NULL;
5102 struct got_blob_object *blob = NULL;
5103 int fd = -1;
5105 /* Reverting a staged deletion is a no-op. */
5106 if (status == GOT_STATUS_DELETE &&
5107 staged_status != GOT_STATUS_NO_CHANGE)
5108 return NULL;
5110 if (status == GOT_STATUS_UNVERSIONED)
5111 return (*a->progress_cb)(a->progress_arg,
5112 GOT_STATUS_UNVERSIONED, relpath);
5114 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5115 if (ie == NULL)
5116 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5118 /* Construct in-repository path of tree which contains this blob. */
5119 err = got_path_dirname(&parent_path, ie->path);
5120 if (err) {
5121 if (err->code != GOT_ERR_BAD_PATH)
5122 goto done;
5123 parent_path = strdup("/");
5124 if (parent_path == NULL) {
5125 err = got_error_from_errno("strdup");
5126 goto done;
5129 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5130 tree_path = strdup(parent_path);
5131 if (tree_path == NULL) {
5132 err = got_error_from_errno("strdup");
5133 goto done;
5135 } else {
5136 if (got_path_is_root_dir(parent_path)) {
5137 tree_path = strdup(a->worktree->path_prefix);
5138 if (tree_path == NULL) {
5139 err = got_error_from_errno("strdup");
5140 goto done;
5142 } else {
5143 if (asprintf(&tree_path, "%s/%s",
5144 a->worktree->path_prefix, parent_path) == -1) {
5145 err = got_error_from_errno("asprintf");
5146 goto done;
5151 err = got_object_open_as_commit(&base_commit, a->repo,
5152 a->worktree->base_commit_id);
5153 if (err)
5154 goto done;
5156 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5157 if (err) {
5158 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5159 (status == GOT_STATUS_ADD ||
5160 staged_status == GOT_STATUS_ADD)))
5161 goto done;
5162 } else {
5163 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5164 if (err)
5165 goto done;
5167 err = got_path_basename(&te_name, ie->path);
5168 if (err)
5169 goto done;
5171 te = got_object_tree_find_entry(tree, te_name);
5172 free(te_name);
5173 if (te == NULL && status != GOT_STATUS_ADD &&
5174 staged_status != GOT_STATUS_ADD) {
5175 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5176 goto done;
5180 switch (status) {
5181 case GOT_STATUS_ADD:
5182 if (a->patch_cb) {
5183 int choice = GOT_PATCH_CHOICE_NONE;
5184 err = (*a->patch_cb)(&choice, a->patch_arg,
5185 status, ie->path, NULL, 1, 1);
5186 if (err)
5187 goto done;
5188 if (choice != GOT_PATCH_CHOICE_YES)
5189 break;
5191 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5192 ie->path);
5193 if (err)
5194 goto done;
5195 got_fileindex_entry_remove(a->fileindex, ie);
5196 if (a->unlink_added_files) {
5197 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5199 if (a->added_files_to_unlink) {
5200 struct got_pathlist_entry *pe;
5202 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5203 entry) {
5204 if (got_path_cmp(pe->path, relpath,
5205 pe->path_len, strlen(relpath)))
5206 continue;
5207 do_unlink = 1;
5208 break;
5212 if (do_unlink) {
5213 if (asprintf(&ondisk_path, "%s/%s",
5214 got_worktree_get_root_path(a->worktree),
5215 relpath) == -1) {
5216 err = got_error_from_errno("asprintf");
5217 goto done;
5219 if (unlink(ondisk_path) == -1) {
5220 err = got_error_from_errno2("unlink",
5221 ondisk_path);
5222 break;
5226 break;
5227 case GOT_STATUS_DELETE:
5228 if (a->patch_cb) {
5229 int choice = GOT_PATCH_CHOICE_NONE;
5230 err = (*a->patch_cb)(&choice, a->patch_arg,
5231 status, ie->path, NULL, 1, 1);
5232 if (err)
5233 goto done;
5234 if (choice != GOT_PATCH_CHOICE_YES)
5235 break;
5237 /* fall through */
5238 case GOT_STATUS_MODIFY:
5239 case GOT_STATUS_MODE_CHANGE:
5240 case GOT_STATUS_CONFLICT:
5241 case GOT_STATUS_MISSING: {
5242 struct got_object_id id;
5243 if (staged_status == GOT_STATUS_ADD ||
5244 staged_status == GOT_STATUS_MODIFY)
5245 got_fileindex_entry_get_staged_blob_id(&id, ie);
5246 else
5247 got_fileindex_entry_get_blob_id(&id, ie);
5248 fd = got_opentempfd();
5249 if (fd == -1) {
5250 err = got_error_from_errno("got_opentempfd");
5251 goto done;
5254 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5255 if (err)
5256 goto done;
5258 if (asprintf(&ondisk_path, "%s/%s",
5259 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5260 err = got_error_from_errno("asprintf");
5261 goto done;
5264 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5265 status == GOT_STATUS_CONFLICT)) {
5266 int is_bad_symlink = 0;
5267 err = create_patched_content(&path_content, 1, &id,
5268 ondisk_path, dirfd, de_name, ie->path, a->repo,
5269 a->patch_cb, a->patch_arg);
5270 if (err || path_content == NULL)
5271 break;
5272 if (te && S_ISLNK(te->mode)) {
5273 if (unlink(path_content) == -1) {
5274 err = got_error_from_errno2("unlink",
5275 path_content);
5276 break;
5278 err = install_symlink(&is_bad_symlink,
5279 a->worktree, ondisk_path, ie->path,
5280 blob, 0, 1, 0, 0, a->repo,
5281 a->progress_cb, a->progress_arg);
5282 } else {
5283 if (rename(path_content, ondisk_path) == -1) {
5284 err = got_error_from_errno3("rename",
5285 path_content, ondisk_path);
5286 goto done;
5289 } else {
5290 int is_bad_symlink = 0;
5291 if (te && S_ISLNK(te->mode)) {
5292 err = install_symlink(&is_bad_symlink,
5293 a->worktree, ondisk_path, ie->path,
5294 blob, 0, 1, 0, 0, a->repo,
5295 a->progress_cb, a->progress_arg);
5296 } else {
5297 err = install_blob(a->worktree, ondisk_path,
5298 ie->path,
5299 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5300 got_fileindex_perms_to_st(ie), blob,
5301 0, 1, 0, 0, NULL, a->repo,
5302 a->progress_cb, a->progress_arg);
5304 if (err)
5305 goto done;
5306 if (status == GOT_STATUS_DELETE ||
5307 status == GOT_STATUS_MODE_CHANGE) {
5308 err = got_fileindex_entry_update(ie,
5309 a->worktree->root_fd, relpath,
5310 blob->id.sha1,
5311 a->worktree->base_commit_id->sha1, 1);
5312 if (err)
5313 goto done;
5315 if (is_bad_symlink) {
5316 got_fileindex_entry_filetype_set(ie,
5317 GOT_FILEIDX_MODE_BAD_SYMLINK);
5320 break;
5322 default:
5323 break;
5325 done:
5326 free(ondisk_path);
5327 free(path_content);
5328 free(parent_path);
5329 free(tree_path);
5330 if (fd != -1 && close(fd) == -1 && err == NULL)
5331 err = got_error_from_errno("close");
5332 if (blob)
5333 got_object_blob_close(blob);
5334 if (tree)
5335 got_object_tree_close(tree);
5336 free(tree_id);
5337 if (base_commit)
5338 got_object_commit_close(base_commit);
5339 return err;
5342 const struct got_error *
5343 got_worktree_revert(struct got_worktree *worktree,
5344 struct got_pathlist_head *paths,
5345 got_worktree_checkout_cb progress_cb, void *progress_arg,
5346 got_worktree_patch_cb patch_cb, void *patch_arg,
5347 struct got_repository *repo)
5349 struct got_fileindex *fileindex = NULL;
5350 char *fileindex_path = NULL;
5351 const struct got_error *err = NULL, *unlockerr = NULL;
5352 const struct got_error *sync_err = NULL;
5353 struct got_pathlist_entry *pe;
5354 struct revert_file_args rfa;
5356 err = lock_worktree(worktree, LOCK_EX);
5357 if (err)
5358 return err;
5360 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5361 if (err)
5362 goto done;
5364 rfa.worktree = worktree;
5365 rfa.fileindex = fileindex;
5366 rfa.progress_cb = progress_cb;
5367 rfa.progress_arg = progress_arg;
5368 rfa.patch_cb = patch_cb;
5369 rfa.patch_arg = patch_arg;
5370 rfa.repo = repo;
5371 rfa.unlink_added_files = 0;
5372 TAILQ_FOREACH(pe, paths, entry) {
5373 err = worktree_status(worktree, pe->path, fileindex, repo,
5374 revert_file, &rfa, NULL, NULL, 1, 0);
5375 if (err)
5376 break;
5378 sync_err = sync_fileindex(fileindex, fileindex_path);
5379 if (sync_err && err == NULL)
5380 err = sync_err;
5381 done:
5382 free(fileindex_path);
5383 if (fileindex)
5384 got_fileindex_free(fileindex);
5385 unlockerr = lock_worktree(worktree, LOCK_SH);
5386 if (unlockerr && err == NULL)
5387 err = unlockerr;
5388 return err;
5391 static void
5392 free_commitable(struct got_commitable *ct)
5394 free(ct->path);
5395 free(ct->in_repo_path);
5396 free(ct->ondisk_path);
5397 free(ct->blob_id);
5398 free(ct->base_blob_id);
5399 free(ct->staged_blob_id);
5400 free(ct->base_commit_id);
5401 free(ct);
5404 struct collect_commitables_arg {
5405 struct got_pathlist_head *commitable_paths;
5406 struct got_repository *repo;
5407 struct got_worktree *worktree;
5408 struct got_fileindex *fileindex;
5409 int have_staged_files;
5410 int allow_bad_symlinks;
5411 int diff_header_shown;
5412 int commit_conflicts;
5413 FILE *diff_outfile;
5414 FILE *f1;
5415 FILE *f2;
5419 * Create a file which contains the target path of a symlink so we can feed
5420 * it as content to the diff engine.
5422 static const struct got_error *
5423 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5424 const char *abspath)
5426 const struct got_error *err = NULL;
5427 char target_path[PATH_MAX];
5428 ssize_t target_len, outlen;
5430 *fd = -1;
5432 if (dirfd != -1) {
5433 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5434 if (target_len == -1)
5435 return got_error_from_errno2("readlinkat", abspath);
5436 } else {
5437 target_len = readlink(abspath, target_path, PATH_MAX);
5438 if (target_len == -1)
5439 return got_error_from_errno2("readlink", abspath);
5442 *fd = got_opentempfd();
5443 if (*fd == -1)
5444 return got_error_from_errno("got_opentempfd");
5446 outlen = write(*fd, target_path, target_len);
5447 if (outlen == -1) {
5448 err = got_error_from_errno("got_opentempfd");
5449 goto done;
5452 if (lseek(*fd, 0, SEEK_SET) == -1) {
5453 err = got_error_from_errno2("lseek", abspath);
5454 goto done;
5456 done:
5457 if (err) {
5458 close(*fd);
5459 *fd = -1;
5461 return err;
5464 static const struct got_error *
5465 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5466 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5467 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5469 const struct got_error *err = NULL;
5470 struct got_blob_object *blob1 = NULL;
5471 int fd = -1, fd1 = -1, fd2 = -1;
5472 FILE *ondisk_file = NULL;
5473 char *label1 = NULL;
5474 struct stat sb;
5475 off_t size1 = 0;
5476 int f2_exists = 0;
5477 char *id_str = NULL;
5479 memset(&sb, 0, sizeof(sb));
5481 if (diff_staged) {
5482 if (ct->staged_status != GOT_STATUS_MODIFY &&
5483 ct->staged_status != GOT_STATUS_ADD &&
5484 ct->staged_status != GOT_STATUS_DELETE)
5485 return NULL;
5486 } else {
5487 if (ct->status != GOT_STATUS_MODIFY &&
5488 ct->status != GOT_STATUS_ADD &&
5489 ct->status != GOT_STATUS_DELETE &&
5490 ct->status != GOT_STATUS_CONFLICT)
5491 return NULL;
5494 err = got_opentemp_truncate(f1);
5495 if (err)
5496 return got_error_from_errno("got_opentemp_truncate");
5497 err = got_opentemp_truncate(f2);
5498 if (err)
5499 return got_error_from_errno("got_opentemp_truncate");
5501 if (!*diff_header_shown) {
5502 err = got_object_id_str(&id_str, worktree->base_commit_id);
5503 if (err)
5504 return err;
5505 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5506 got_worktree_get_root_path(worktree));
5507 fprintf(diff_outfile, "commit - %s\n", id_str);
5508 fprintf(diff_outfile, "path + %s%s\n",
5509 got_worktree_get_root_path(worktree),
5510 diff_staged ? " (staged changes)" : "");
5511 *diff_header_shown = 1;
5514 if (diff_staged) {
5515 const char *label1 = NULL, *label2 = NULL;
5516 switch (ct->staged_status) {
5517 case GOT_STATUS_MODIFY:
5518 label1 = ct->path;
5519 label2 = ct->path;
5520 break;
5521 case GOT_STATUS_ADD:
5522 label2 = ct->path;
5523 break;
5524 case GOT_STATUS_DELETE:
5525 label1 = ct->path;
5526 break;
5527 default:
5528 return got_error(GOT_ERR_FILE_STATUS);
5530 fd1 = got_opentempfd();
5531 if (fd1 == -1) {
5532 err = got_error_from_errno("got_opentempfd");
5533 goto done;
5535 fd2 = got_opentempfd();
5536 if (fd2 == -1) {
5537 err = got_error_from_errno("got_opentempfd");
5538 goto done;
5540 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5541 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5542 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5543 NULL, repo, diff_outfile);
5544 goto done;
5547 fd1 = got_opentempfd();
5548 if (fd1 == -1) {
5549 err = got_error_from_errno("got_opentempfd");
5550 goto done;
5553 if (ct->status != GOT_STATUS_ADD) {
5554 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5555 8192, fd1);
5556 if (err)
5557 goto done;
5560 if (ct->status != GOT_STATUS_DELETE) {
5561 if (dirfd != -1) {
5562 fd = openat(dirfd, de_name,
5563 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5564 if (fd == -1) {
5565 if (!got_err_open_nofollow_on_symlink()) {
5566 err = got_error_from_errno2("openat",
5567 ct->ondisk_path);
5568 goto done;
5570 err = get_symlink_target_file(&fd, dirfd,
5571 de_name, ct->ondisk_path);
5572 if (err)
5573 goto done;
5575 } else {
5576 fd = open(ct->ondisk_path,
5577 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5578 if (fd == -1) {
5579 if (!got_err_open_nofollow_on_symlink()) {
5580 err = got_error_from_errno2("open",
5581 ct->ondisk_path);
5582 goto done;
5584 err = get_symlink_target_file(&fd, dirfd,
5585 de_name, ct->ondisk_path);
5586 if (err)
5587 goto done;
5590 if (fstatat(fd, ct->ondisk_path, &sb,
5591 AT_SYMLINK_NOFOLLOW) == -1) {
5592 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5593 goto done;
5595 ondisk_file = fdopen(fd, "r");
5596 if (ondisk_file == NULL) {
5597 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5598 goto done;
5600 fd = -1;
5601 f2_exists = 1;
5604 if (blob1) {
5605 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5606 f1, blob1);
5607 if (err)
5608 goto done;
5611 err = got_diff_blob_file(blob1, f1, size1, label1,
5612 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5613 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5614 done:
5615 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5616 err = got_error_from_errno("close");
5617 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5618 err = got_error_from_errno("close");
5619 if (blob1)
5620 got_object_blob_close(blob1);
5621 if (fd != -1 && close(fd) == -1 && err == NULL)
5622 err = got_error_from_errno("close");
5623 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5624 err = got_error_from_errno("fclose");
5625 return err;
5628 static const struct got_error *
5629 collect_commitables(void *arg, unsigned char status,
5630 unsigned char staged_status, const char *relpath,
5631 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5632 struct got_object_id *commit_id, int dirfd, const char *de_name)
5634 struct collect_commitables_arg *a = arg;
5635 const struct got_error *err = NULL;
5636 struct got_commitable *ct = NULL;
5637 struct got_pathlist_entry *new = NULL;
5638 char *parent_path = NULL, *path = NULL;
5639 struct stat sb;
5641 if (a->have_staged_files) {
5642 if (staged_status != GOT_STATUS_MODIFY &&
5643 staged_status != GOT_STATUS_ADD &&
5644 staged_status != GOT_STATUS_DELETE)
5645 return NULL;
5646 } else {
5647 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5648 printf("C %s\n", relpath);
5649 return got_error(GOT_ERR_COMMIT_CONFLICT);
5652 if (status != GOT_STATUS_MODIFY &&
5653 status != GOT_STATUS_MODE_CHANGE &&
5654 status != GOT_STATUS_ADD &&
5655 status != GOT_STATUS_DELETE &&
5656 status != GOT_STATUS_CONFLICT)
5657 return NULL;
5660 if (asprintf(&path, "/%s", relpath) == -1) {
5661 err = got_error_from_errno("asprintf");
5662 goto done;
5664 if (strcmp(path, "/") == 0) {
5665 parent_path = strdup("");
5666 if (parent_path == NULL)
5667 return got_error_from_errno("strdup");
5668 } else {
5669 err = got_path_dirname(&parent_path, path);
5670 if (err)
5671 return err;
5674 ct = calloc(1, sizeof(*ct));
5675 if (ct == NULL) {
5676 err = got_error_from_errno("calloc");
5677 goto done;
5680 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5681 relpath) == -1) {
5682 err = got_error_from_errno("asprintf");
5683 goto done;
5686 if (staged_status == GOT_STATUS_ADD ||
5687 staged_status == GOT_STATUS_MODIFY) {
5688 struct got_fileindex_entry *ie;
5689 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5690 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5691 case GOT_FILEIDX_MODE_REGULAR_FILE:
5692 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5693 ct->mode = S_IFREG;
5694 break;
5695 case GOT_FILEIDX_MODE_SYMLINK:
5696 ct->mode = S_IFLNK;
5697 break;
5698 default:
5699 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5700 goto done;
5702 ct->mode |= got_fileindex_entry_perms_get(ie);
5703 } else if (status != GOT_STATUS_DELETE &&
5704 staged_status != GOT_STATUS_DELETE) {
5705 if (dirfd != -1) {
5706 if (fstatat(dirfd, de_name, &sb,
5707 AT_SYMLINK_NOFOLLOW) == -1) {
5708 err = got_error_from_errno2("fstatat",
5709 ct->ondisk_path);
5710 goto done;
5712 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5713 err = got_error_from_errno2("lstat", ct->ondisk_path);
5714 goto done;
5716 ct->mode = sb.st_mode;
5719 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5720 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5721 relpath) == -1) {
5722 err = got_error_from_errno("asprintf");
5723 goto done;
5726 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5727 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5728 int is_bad_symlink;
5729 char target_path[PATH_MAX];
5730 ssize_t target_len;
5731 target_len = readlink(ct->ondisk_path, target_path,
5732 sizeof(target_path));
5733 if (target_len == -1) {
5734 err = got_error_from_errno2("readlink",
5735 ct->ondisk_path);
5736 goto done;
5738 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5739 target_len, ct->ondisk_path, a->worktree->root_path,
5740 a->worktree->meta_dir);
5741 if (err)
5742 goto done;
5743 if (is_bad_symlink) {
5744 err = got_error_path(ct->ondisk_path,
5745 GOT_ERR_BAD_SYMLINK);
5746 goto done;
5751 ct->status = status;
5752 ct->staged_status = staged_status;
5753 ct->blob_id = NULL; /* will be filled in when blob gets created */
5754 if (ct->status != GOT_STATUS_ADD &&
5755 ct->staged_status != GOT_STATUS_ADD) {
5756 ct->base_blob_id = got_object_id_dup(blob_id);
5757 if (ct->base_blob_id == NULL) {
5758 err = got_error_from_errno("got_object_id_dup");
5759 goto done;
5761 ct->base_commit_id = got_object_id_dup(commit_id);
5762 if (ct->base_commit_id == NULL) {
5763 err = got_error_from_errno("got_object_id_dup");
5764 goto done;
5767 if (ct->staged_status == GOT_STATUS_ADD ||
5768 ct->staged_status == GOT_STATUS_MODIFY) {
5769 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5770 if (ct->staged_blob_id == NULL) {
5771 err = got_error_from_errno("got_object_id_dup");
5772 goto done;
5775 ct->path = strdup(path);
5776 if (ct->path == NULL) {
5777 err = got_error_from_errno("strdup");
5778 goto done;
5781 if (a->diff_outfile) {
5782 err = append_ct_diff(ct, &a->diff_header_shown,
5783 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5784 a->have_staged_files, a->repo, a->worktree);
5785 if (err)
5786 goto done;
5789 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5790 done:
5791 if (ct && (err || new == NULL))
5792 free_commitable(ct);
5793 free(parent_path);
5794 free(path);
5795 return err;
5798 static const struct got_error *write_tree(struct got_object_id **, int *,
5799 struct got_tree_object *, const char *, struct got_pathlist_head *,
5800 got_worktree_status_cb status_cb, void *status_arg,
5801 struct got_repository *);
5803 static const struct got_error *
5804 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5805 struct got_tree_entry *te, const char *parent_path,
5806 struct got_pathlist_head *commitable_paths,
5807 got_worktree_status_cb status_cb, void *status_arg,
5808 struct got_repository *repo)
5810 const struct got_error *err = NULL;
5811 struct got_tree_object *subtree;
5812 char *subpath;
5814 if (asprintf(&subpath, "%s%s%s", parent_path,
5815 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5816 return got_error_from_errno("asprintf");
5818 err = got_object_open_as_tree(&subtree, repo, &te->id);
5819 if (err)
5820 return err;
5822 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5823 commitable_paths, status_cb, status_arg, repo);
5824 got_object_tree_close(subtree);
5825 free(subpath);
5826 return err;
5829 static const struct got_error *
5830 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5832 const struct got_error *err = NULL;
5833 char *ct_parent_path = NULL;
5835 *match = 0;
5837 if (strchr(ct->in_repo_path, '/') == NULL) {
5838 *match = got_path_is_root_dir(path);
5839 return NULL;
5842 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5843 if (err)
5844 return err;
5845 *match = (strcmp(path, ct_parent_path) == 0);
5846 free(ct_parent_path);
5847 return err;
5850 static mode_t
5851 get_ct_file_mode(struct got_commitable *ct)
5853 if (S_ISLNK(ct->mode))
5854 return S_IFLNK;
5856 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5859 static const struct got_error *
5860 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5861 struct got_tree_entry *te, struct got_commitable *ct)
5863 const struct got_error *err = NULL;
5865 *new_te = NULL;
5867 err = got_object_tree_entry_dup(new_te, te);
5868 if (err)
5869 goto done;
5871 (*new_te)->mode = get_ct_file_mode(ct);
5873 if (ct->staged_status == GOT_STATUS_MODIFY)
5874 memcpy(&(*new_te)->id, ct->staged_blob_id,
5875 sizeof((*new_te)->id));
5876 else
5877 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5878 done:
5879 if (err && *new_te) {
5880 free(*new_te);
5881 *new_te = NULL;
5883 return err;
5886 static const struct got_error *
5887 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5888 struct got_commitable *ct)
5890 const struct got_error *err = NULL;
5891 char *ct_name = NULL;
5893 *new_te = NULL;
5895 *new_te = calloc(1, sizeof(**new_te));
5896 if (*new_te == NULL)
5897 return got_error_from_errno("calloc");
5899 err = got_path_basename(&ct_name, ct->path);
5900 if (err)
5901 goto done;
5902 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5903 sizeof((*new_te)->name)) {
5904 err = got_error(GOT_ERR_NO_SPACE);
5905 goto done;
5908 (*new_te)->mode = get_ct_file_mode(ct);
5910 if (ct->staged_status == GOT_STATUS_ADD)
5911 memcpy(&(*new_te)->id, ct->staged_blob_id,
5912 sizeof((*new_te)->id));
5913 else
5914 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5915 done:
5916 free(ct_name);
5917 if (err && *new_te) {
5918 free(*new_te);
5919 *new_te = NULL;
5921 return err;
5924 static const struct got_error *
5925 insert_tree_entry(struct got_tree_entry *new_te,
5926 struct got_pathlist_head *paths)
5928 const struct got_error *err = NULL;
5929 struct got_pathlist_entry *new_pe;
5931 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5932 if (err)
5933 return err;
5934 if (new_pe == NULL)
5935 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5936 return NULL;
5939 static const struct got_error *
5940 report_ct_status(struct got_commitable *ct,
5941 got_worktree_status_cb status_cb, void *status_arg)
5943 const char *ct_path = ct->path;
5944 unsigned char status;
5946 if (status_cb == NULL) /* no commit progress output desired */
5947 return NULL;
5949 while (ct_path[0] == '/')
5950 ct_path++;
5952 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5953 status = ct->staged_status;
5954 else
5955 status = ct->status;
5957 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5958 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5961 static const struct got_error *
5962 match_modified_subtree(int *modified, struct got_tree_entry *te,
5963 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5965 const struct got_error *err = NULL;
5966 struct got_pathlist_entry *pe;
5967 char *te_path;
5969 *modified = 0;
5971 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5972 got_path_is_root_dir(base_tree_path) ? "" : "/",
5973 te->name) == -1)
5974 return got_error_from_errno("asprintf");
5976 TAILQ_FOREACH(pe, commitable_paths, entry) {
5977 struct got_commitable *ct = pe->data;
5978 *modified = got_path_is_child(ct->in_repo_path, te_path,
5979 strlen(te_path));
5980 if (*modified)
5981 break;
5984 free(te_path);
5985 return err;
5988 static const struct got_error *
5989 match_deleted_or_modified_ct(struct got_commitable **ctp,
5990 struct got_tree_entry *te, const char *base_tree_path,
5991 struct got_pathlist_head *commitable_paths)
5993 const struct got_error *err = NULL;
5994 struct got_pathlist_entry *pe;
5996 *ctp = NULL;
5998 TAILQ_FOREACH(pe, commitable_paths, entry) {
5999 struct got_commitable *ct = pe->data;
6000 char *ct_name = NULL;
6001 int path_matches;
6003 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6004 if (ct->status != GOT_STATUS_MODIFY &&
6005 ct->status != GOT_STATUS_MODE_CHANGE &&
6006 ct->status != GOT_STATUS_DELETE &&
6007 ct->status != GOT_STATUS_CONFLICT)
6008 continue;
6009 } else {
6010 if (ct->staged_status != GOT_STATUS_MODIFY &&
6011 ct->staged_status != GOT_STATUS_DELETE)
6012 continue;
6015 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6016 continue;
6018 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6019 if (err)
6020 return err;
6021 if (!path_matches)
6022 continue;
6024 err = got_path_basename(&ct_name, pe->path);
6025 if (err)
6026 return err;
6028 if (strcmp(te->name, ct_name) != 0) {
6029 free(ct_name);
6030 continue;
6032 free(ct_name);
6034 *ctp = ct;
6035 break;
6038 return err;
6041 static const struct got_error *
6042 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6043 const char *child_path, const char *path_base_tree,
6044 struct got_pathlist_head *commitable_paths,
6045 got_worktree_status_cb status_cb, void *status_arg,
6046 struct got_repository *repo)
6048 const struct got_error *err = NULL;
6049 struct got_tree_entry *new_te;
6050 char *subtree_path;
6051 struct got_object_id *id = NULL;
6052 int nentries;
6054 *new_tep = NULL;
6056 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6057 got_path_is_root_dir(path_base_tree) ? "" : "/",
6058 child_path) == -1)
6059 return got_error_from_errno("asprintf");
6061 new_te = calloc(1, sizeof(*new_te));
6062 if (new_te == NULL)
6063 return got_error_from_errno("calloc");
6064 new_te->mode = S_IFDIR;
6066 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6067 sizeof(new_te->name)) {
6068 err = got_error(GOT_ERR_NO_SPACE);
6069 goto done;
6071 err = write_tree(&id, &nentries, NULL, subtree_path,
6072 commitable_paths, status_cb, status_arg, repo);
6073 if (err) {
6074 free(new_te);
6075 goto done;
6077 memcpy(&new_te->id, id, sizeof(new_te->id));
6078 done:
6079 free(id);
6080 free(subtree_path);
6081 if (err == NULL)
6082 *new_tep = new_te;
6083 return err;
6086 static const struct got_error *
6087 write_tree(struct got_object_id **new_tree_id, int *nentries,
6088 struct got_tree_object *base_tree, const char *path_base_tree,
6089 struct got_pathlist_head *commitable_paths,
6090 got_worktree_status_cb status_cb, void *status_arg,
6091 struct got_repository *repo)
6093 const struct got_error *err = NULL;
6094 struct got_pathlist_head paths;
6095 struct got_tree_entry *te, *new_te = NULL;
6096 struct got_pathlist_entry *pe;
6098 TAILQ_INIT(&paths);
6099 *nentries = 0;
6101 /* Insert, and recurse into, newly added entries first. */
6102 TAILQ_FOREACH(pe, commitable_paths, entry) {
6103 struct got_commitable *ct = pe->data;
6104 char *child_path = NULL, *slash;
6106 if ((ct->status != GOT_STATUS_ADD &&
6107 ct->staged_status != GOT_STATUS_ADD) ||
6108 (ct->flags & GOT_COMMITABLE_ADDED))
6109 continue;
6111 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6112 strlen(path_base_tree)))
6113 continue;
6115 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6116 ct->in_repo_path);
6117 if (err)
6118 goto done;
6120 slash = strchr(child_path, '/');
6121 if (slash == NULL) {
6122 err = alloc_added_blob_tree_entry(&new_te, ct);
6123 if (err)
6124 goto done;
6125 err = report_ct_status(ct, status_cb, status_arg);
6126 if (err)
6127 goto done;
6128 ct->flags |= GOT_COMMITABLE_ADDED;
6129 err = insert_tree_entry(new_te, &paths);
6130 if (err)
6131 goto done;
6132 (*nentries)++;
6133 } else {
6134 *slash = '\0'; /* trim trailing path components */
6135 if (base_tree == NULL ||
6136 got_object_tree_find_entry(base_tree, child_path)
6137 == NULL) {
6138 err = make_subtree_for_added_blob(&new_te,
6139 child_path, path_base_tree,
6140 commitable_paths, status_cb, status_arg,
6141 repo);
6142 if (err)
6143 goto done;
6144 err = insert_tree_entry(new_te, &paths);
6145 if (err)
6146 goto done;
6147 (*nentries)++;
6152 if (base_tree) {
6153 int i, nbase_entries;
6154 /* Handle modified and deleted entries. */
6155 nbase_entries = got_object_tree_get_nentries(base_tree);
6156 for (i = 0; i < nbase_entries; i++) {
6157 struct got_commitable *ct = NULL;
6159 te = got_object_tree_get_entry(base_tree, i);
6160 if (got_object_tree_entry_is_submodule(te)) {
6161 /* Entry is a submodule; just copy it. */
6162 err = got_object_tree_entry_dup(&new_te, te);
6163 if (err)
6164 goto done;
6165 err = insert_tree_entry(new_te, &paths);
6166 if (err)
6167 goto done;
6168 (*nentries)++;
6169 continue;
6172 if (S_ISDIR(te->mode)) {
6173 int modified;
6174 err = got_object_tree_entry_dup(&new_te, te);
6175 if (err)
6176 goto done;
6177 err = match_modified_subtree(&modified, te,
6178 path_base_tree, commitable_paths);
6179 if (err)
6180 goto done;
6181 /* Avoid recursion into unmodified subtrees. */
6182 if (modified) {
6183 struct got_object_id *new_id;
6184 int nsubentries;
6185 err = write_subtree(&new_id,
6186 &nsubentries, te,
6187 path_base_tree, commitable_paths,
6188 status_cb, status_arg, repo);
6189 if (err)
6190 goto done;
6191 if (nsubentries == 0) {
6192 /* All entries were deleted. */
6193 free(new_id);
6194 continue;
6196 memcpy(&new_te->id, new_id,
6197 sizeof(new_te->id));
6198 free(new_id);
6200 err = insert_tree_entry(new_te, &paths);
6201 if (err)
6202 goto done;
6203 (*nentries)++;
6204 continue;
6207 err = match_deleted_or_modified_ct(&ct, te,
6208 path_base_tree, commitable_paths);
6209 if (err)
6210 goto done;
6211 if (ct) {
6212 /* NB: Deleted entries get dropped here. */
6213 if (ct->status == GOT_STATUS_MODIFY ||
6214 ct->status == GOT_STATUS_MODE_CHANGE ||
6215 ct->status == GOT_STATUS_CONFLICT ||
6216 ct->staged_status == GOT_STATUS_MODIFY) {
6217 err = alloc_modified_blob_tree_entry(
6218 &new_te, te, ct);
6219 if (err)
6220 goto done;
6221 err = insert_tree_entry(new_te, &paths);
6222 if (err)
6223 goto done;
6224 (*nentries)++;
6226 err = report_ct_status(ct, status_cb,
6227 status_arg);
6228 if (err)
6229 goto done;
6230 } else {
6231 /* Entry is unchanged; just copy it. */
6232 err = got_object_tree_entry_dup(&new_te, te);
6233 if (err)
6234 goto done;
6235 err = insert_tree_entry(new_te, &paths);
6236 if (err)
6237 goto done;
6238 (*nentries)++;
6243 /* Write new list of entries; deleted entries have been dropped. */
6244 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6245 done:
6246 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6247 return err;
6250 static const struct got_error *
6251 update_fileindex_after_commit(struct got_worktree *worktree,
6252 struct got_pathlist_head *commitable_paths,
6253 struct got_object_id *new_base_commit_id,
6254 struct got_fileindex *fileindex, int have_staged_files)
6256 const struct got_error *err = NULL;
6257 struct got_pathlist_entry *pe;
6258 char *relpath = NULL;
6260 TAILQ_FOREACH(pe, commitable_paths, entry) {
6261 struct got_fileindex_entry *ie;
6262 struct got_commitable *ct = pe->data;
6264 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6266 err = got_path_skip_common_ancestor(&relpath,
6267 worktree->root_path, ct->ondisk_path);
6268 if (err)
6269 goto done;
6271 if (ie) {
6272 if (ct->status == GOT_STATUS_DELETE ||
6273 ct->staged_status == GOT_STATUS_DELETE) {
6274 got_fileindex_entry_remove(fileindex, ie);
6275 } else if (ct->staged_status == GOT_STATUS_ADD ||
6276 ct->staged_status == GOT_STATUS_MODIFY) {
6277 got_fileindex_entry_stage_set(ie,
6278 GOT_FILEIDX_STAGE_NONE);
6279 got_fileindex_entry_staged_filetype_set(ie, 0);
6281 err = got_fileindex_entry_update(ie,
6282 worktree->root_fd, relpath,
6283 ct->staged_blob_id->sha1,
6284 new_base_commit_id->sha1,
6285 !have_staged_files);
6286 } else
6287 err = got_fileindex_entry_update(ie,
6288 worktree->root_fd, relpath,
6289 ct->blob_id->sha1,
6290 new_base_commit_id->sha1,
6291 !have_staged_files);
6292 } else {
6293 err = got_fileindex_entry_alloc(&ie, pe->path);
6294 if (err)
6295 goto done;
6296 err = got_fileindex_entry_update(ie,
6297 worktree->root_fd, relpath, ct->blob_id->sha1,
6298 new_base_commit_id->sha1, 1);
6299 if (err) {
6300 got_fileindex_entry_free(ie);
6301 goto done;
6303 err = got_fileindex_entry_add(fileindex, ie);
6304 if (err) {
6305 got_fileindex_entry_free(ie);
6306 goto done;
6309 free(relpath);
6310 relpath = NULL;
6312 done:
6313 free(relpath);
6314 return err;
6318 static const struct got_error *
6319 check_out_of_date(const char *in_repo_path, unsigned char status,
6320 unsigned char staged_status, struct got_object_id *base_blob_id,
6321 struct got_object_id *base_commit_id,
6322 struct got_object_id *head_commit_id, struct got_repository *repo,
6323 int ood_errcode)
6325 const struct got_error *err = NULL;
6326 struct got_commit_object *commit = NULL;
6327 struct got_object_id *id = NULL;
6329 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6330 /* Trivial case: base commit == head commit */
6331 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6332 return NULL;
6334 * Ensure file content which local changes were based
6335 * on matches file content in the branch head.
6337 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6338 if (err)
6339 goto done;
6340 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6341 if (err) {
6342 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6343 err = got_error(ood_errcode);
6344 goto done;
6345 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6346 err = got_error(ood_errcode);
6347 } else {
6348 /* Require that added files don't exist in the branch head. */
6349 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6350 if (err)
6351 goto done;
6352 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6353 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6354 goto done;
6355 err = id ? got_error(ood_errcode) : NULL;
6357 done:
6358 free(id);
6359 if (commit)
6360 got_object_commit_close(commit);
6361 return err;
6364 static const struct got_error *
6365 commit_worktree(struct got_object_id **new_commit_id,
6366 struct got_pathlist_head *commitable_paths,
6367 struct got_object_id *head_commit_id,
6368 struct got_object_id *parent_id2,
6369 struct got_worktree *worktree,
6370 const char *author, const char *committer, char *diff_path,
6371 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6372 got_worktree_status_cb status_cb, void *status_arg,
6373 struct got_repository *repo)
6375 const struct got_error *err = NULL, *unlockerr = NULL;
6376 struct got_pathlist_entry *pe;
6377 const char *head_ref_name = NULL;
6378 struct got_commit_object *head_commit = NULL;
6379 struct got_reference *head_ref2 = NULL;
6380 struct got_object_id *head_commit_id2 = NULL;
6381 struct got_tree_object *head_tree = NULL;
6382 struct got_object_id *new_tree_id = NULL;
6383 int nentries, nparents = 0;
6384 struct got_object_id_queue parent_ids;
6385 struct got_object_qid *pid = NULL;
6386 char *logmsg = NULL;
6387 time_t timestamp;
6389 *new_commit_id = NULL;
6391 STAILQ_INIT(&parent_ids);
6393 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6394 if (err)
6395 goto done;
6397 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6398 if (err)
6399 goto done;
6401 if (commit_msg_cb != NULL) {
6402 err = commit_msg_cb(commitable_paths, diff_path,
6403 &logmsg, commit_arg);
6404 if (err)
6405 goto done;
6408 if (logmsg == NULL || strlen(logmsg) == 0) {
6409 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6410 goto done;
6413 /* Create blobs from added and modified files and record their IDs. */
6414 TAILQ_FOREACH(pe, commitable_paths, entry) {
6415 struct got_commitable *ct = pe->data;
6416 char *ondisk_path;
6418 /* Blobs for staged files already exist. */
6419 if (ct->staged_status == GOT_STATUS_ADD ||
6420 ct->staged_status == GOT_STATUS_MODIFY)
6421 continue;
6423 if (ct->status != GOT_STATUS_ADD &&
6424 ct->status != GOT_STATUS_MODIFY &&
6425 ct->status != GOT_STATUS_MODE_CHANGE &&
6426 ct->status != GOT_STATUS_CONFLICT)
6427 continue;
6429 if (asprintf(&ondisk_path, "%s/%s",
6430 worktree->root_path, pe->path) == -1) {
6431 err = got_error_from_errno("asprintf");
6432 goto done;
6434 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6435 free(ondisk_path);
6436 if (err)
6437 goto done;
6440 /* Recursively write new tree objects. */
6441 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6442 commitable_paths, status_cb, status_arg, repo);
6443 if (err)
6444 goto done;
6446 err = got_object_qid_alloc(&pid, head_commit_id);
6447 if (err)
6448 goto done;
6449 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6450 nparents++;
6451 if (parent_id2) {
6452 err = got_object_qid_alloc(&pid, parent_id2);
6453 if (err)
6454 goto done;
6455 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6456 nparents++;
6458 timestamp = time(NULL);
6459 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6460 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6461 if (logmsg != NULL)
6462 free(logmsg);
6463 if (err)
6464 goto done;
6466 /* Check if a concurrent commit to our branch has occurred. */
6467 head_ref_name = got_worktree_get_head_ref_name(worktree);
6468 if (head_ref_name == NULL) {
6469 err = got_error_from_errno("got_worktree_get_head_ref_name");
6470 goto done;
6472 /* Lock the reference here to prevent concurrent modification. */
6473 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6474 if (err)
6475 goto done;
6476 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6477 if (err)
6478 goto done;
6479 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6480 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6481 goto done;
6483 /* Update branch head in repository. */
6484 err = got_ref_change_ref(head_ref2, *new_commit_id);
6485 if (err)
6486 goto done;
6487 err = got_ref_write(head_ref2, repo);
6488 if (err)
6489 goto done;
6491 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6492 if (err)
6493 goto done;
6495 err = ref_base_commit(worktree, repo);
6496 if (err)
6497 goto done;
6498 done:
6499 got_object_id_queue_free(&parent_ids);
6500 if (head_tree)
6501 got_object_tree_close(head_tree);
6502 if (head_commit)
6503 got_object_commit_close(head_commit);
6504 free(head_commit_id2);
6505 if (head_ref2) {
6506 unlockerr = got_ref_unlock(head_ref2);
6507 if (unlockerr && err == NULL)
6508 err = unlockerr;
6509 got_ref_close(head_ref2);
6511 return err;
6514 static const struct got_error *
6515 check_path_is_commitable(const char *path,
6516 struct got_pathlist_head *commitable_paths)
6518 struct got_pathlist_entry *cpe = NULL;
6519 size_t path_len = strlen(path);
6521 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6522 struct got_commitable *ct = cpe->data;
6523 const char *ct_path = ct->path;
6525 while (ct_path[0] == '/')
6526 ct_path++;
6528 if (strcmp(path, ct_path) == 0 ||
6529 got_path_is_child(ct_path, path, path_len))
6530 break;
6533 if (cpe == NULL)
6534 return got_error_path(path, GOT_ERR_BAD_PATH);
6536 return NULL;
6539 static const struct got_error *
6540 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6542 int *have_staged_files = arg;
6544 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6545 *have_staged_files = 1;
6546 return got_error(GOT_ERR_CANCELLED);
6549 return NULL;
6552 static const struct got_error *
6553 check_non_staged_files(struct got_fileindex *fileindex,
6554 struct got_pathlist_head *paths)
6556 struct got_pathlist_entry *pe;
6557 struct got_fileindex_entry *ie;
6559 TAILQ_FOREACH(pe, paths, entry) {
6560 if (pe->path[0] == '\0')
6561 continue;
6562 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6563 if (ie == NULL)
6564 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6565 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6566 return got_error_path(pe->path,
6567 GOT_ERR_FILE_NOT_STAGED);
6570 return NULL;
6573 const struct got_error *
6574 got_worktree_commit(struct got_object_id **new_commit_id,
6575 struct got_worktree *worktree, struct got_pathlist_head *paths,
6576 const char *author, const char *committer, int allow_bad_symlinks,
6577 int show_diff, int commit_conflicts,
6578 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6579 got_worktree_status_cb status_cb, void *status_arg,
6580 struct got_repository *repo)
6582 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6583 struct got_fileindex *fileindex = NULL;
6584 char *fileindex_path = NULL;
6585 struct got_pathlist_head commitable_paths;
6586 struct collect_commitables_arg cc_arg;
6587 struct got_pathlist_entry *pe;
6588 struct got_reference *head_ref = NULL;
6589 struct got_object_id *head_commit_id = NULL;
6590 char *diff_path = NULL;
6591 int have_staged_files = 0;
6593 *new_commit_id = NULL;
6595 memset(&cc_arg, 0, sizeof(cc_arg));
6596 TAILQ_INIT(&commitable_paths);
6598 err = lock_worktree(worktree, LOCK_EX);
6599 if (err)
6600 goto done;
6602 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6603 if (err)
6604 goto done;
6606 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6607 if (err)
6608 goto done;
6610 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6611 if (err)
6612 goto done;
6614 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6615 &have_staged_files);
6616 if (err && err->code != GOT_ERR_CANCELLED)
6617 goto done;
6618 if (have_staged_files) {
6619 err = check_non_staged_files(fileindex, paths);
6620 if (err)
6621 goto done;
6624 cc_arg.commitable_paths = &commitable_paths;
6625 cc_arg.worktree = worktree;
6626 cc_arg.fileindex = fileindex;
6627 cc_arg.repo = repo;
6628 cc_arg.have_staged_files = have_staged_files;
6629 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6630 cc_arg.diff_header_shown = 0;
6631 cc_arg.commit_conflicts = commit_conflicts;
6632 if (show_diff) {
6633 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6634 GOT_TMPDIR_STR "/got", ".diff");
6635 if (err)
6636 goto done;
6637 cc_arg.f1 = got_opentemp();
6638 if (cc_arg.f1 == NULL) {
6639 err = got_error_from_errno("got_opentemp");
6640 goto done;
6642 cc_arg.f2 = got_opentemp();
6643 if (cc_arg.f2 == NULL) {
6644 err = got_error_from_errno("got_opentemp");
6645 goto done;
6649 TAILQ_FOREACH(pe, paths, entry) {
6650 err = worktree_status(worktree, pe->path, fileindex, repo,
6651 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6652 if (err)
6653 goto done;
6656 if (show_diff) {
6657 if (fflush(cc_arg.diff_outfile) == EOF) {
6658 err = got_error_from_errno("fflush");
6659 goto done;
6663 if (TAILQ_EMPTY(&commitable_paths)) {
6664 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6665 goto done;
6668 TAILQ_FOREACH(pe, paths, entry) {
6669 err = check_path_is_commitable(pe->path, &commitable_paths);
6670 if (err)
6671 goto done;
6674 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6675 struct got_commitable *ct = pe->data;
6676 const char *ct_path = ct->in_repo_path;
6678 while (ct_path[0] == '/')
6679 ct_path++;
6680 err = check_out_of_date(ct_path, ct->status,
6681 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6682 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6683 if (err)
6684 goto done;
6688 err = commit_worktree(new_commit_id, &commitable_paths,
6689 head_commit_id, NULL, worktree, author, committer,
6690 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6691 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6692 if (err)
6693 goto done;
6695 err = update_fileindex_after_commit(worktree, &commitable_paths,
6696 *new_commit_id, fileindex, have_staged_files);
6697 sync_err = sync_fileindex(fileindex, fileindex_path);
6698 if (sync_err && err == NULL)
6699 err = sync_err;
6700 done:
6701 if (fileindex)
6702 got_fileindex_free(fileindex);
6703 free(fileindex_path);
6704 unlockerr = lock_worktree(worktree, LOCK_SH);
6705 if (unlockerr && err == NULL)
6706 err = unlockerr;
6707 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6708 struct got_commitable *ct = pe->data;
6710 free_commitable(ct);
6712 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6713 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6714 err = got_error_from_errno2("unlink", diff_path);
6715 free(diff_path);
6716 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6717 err == NULL)
6718 err = got_error_from_errno("fclose");
6719 return err;
6722 const char *
6723 got_commitable_get_path(struct got_commitable *ct)
6725 return ct->path;
6728 unsigned int
6729 got_commitable_get_status(struct got_commitable *ct)
6731 return ct->status;
6734 struct check_rebase_ok_arg {
6735 struct got_worktree *worktree;
6736 struct got_repository *repo;
6739 static const struct got_error *
6740 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6742 const struct got_error *err = NULL;
6743 struct check_rebase_ok_arg *a = arg;
6744 unsigned char status;
6745 struct stat sb;
6746 char *ondisk_path;
6748 /* Reject rebase of a work tree with mixed base commits. */
6749 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6750 SHA1_DIGEST_LENGTH))
6751 return got_error(GOT_ERR_MIXED_COMMITS);
6753 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6754 == -1)
6755 return got_error_from_errno("asprintf");
6757 /* Reject rebase of a work tree with modified or staged files. */
6758 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6759 free(ondisk_path);
6760 if (err)
6761 return err;
6763 if (status != GOT_STATUS_NO_CHANGE)
6764 return got_error(GOT_ERR_MODIFIED);
6765 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6766 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6768 return NULL;
6771 const struct got_error *
6772 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6773 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6774 struct got_worktree *worktree, struct got_reference *branch,
6775 struct got_repository *repo)
6777 const struct got_error *err = NULL;
6778 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6779 char *branch_ref_name = NULL;
6780 char *fileindex_path = NULL;
6781 struct check_rebase_ok_arg ok_arg;
6782 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6783 struct got_object_id *wt_branch_tip = NULL;
6785 *new_base_branch_ref = NULL;
6786 *tmp_branch = NULL;
6787 *fileindex = NULL;
6789 err = lock_worktree(worktree, LOCK_EX);
6790 if (err)
6791 return err;
6793 err = open_fileindex(fileindex, &fileindex_path, worktree);
6794 if (err)
6795 goto done;
6797 ok_arg.worktree = worktree;
6798 ok_arg.repo = repo;
6799 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6800 &ok_arg);
6801 if (err)
6802 goto done;
6804 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6805 if (err)
6806 goto done;
6808 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6809 if (err)
6810 goto done;
6812 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6813 if (err)
6814 goto done;
6816 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6817 0);
6818 if (err)
6819 goto done;
6821 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6822 if (err)
6823 goto done;
6824 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6825 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6826 goto done;
6829 err = got_ref_alloc_symref(new_base_branch_ref,
6830 new_base_branch_ref_name, wt_branch);
6831 if (err)
6832 goto done;
6833 err = got_ref_write(*new_base_branch_ref, repo);
6834 if (err)
6835 goto done;
6837 /* TODO Lock original branch's ref while rebasing? */
6839 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6840 if (err)
6841 goto done;
6843 err = got_ref_write(branch_ref, repo);
6844 if (err)
6845 goto done;
6847 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6848 worktree->base_commit_id);
6849 if (err)
6850 goto done;
6851 err = got_ref_write(*tmp_branch, repo);
6852 if (err)
6853 goto done;
6855 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6856 if (err)
6857 goto done;
6858 done:
6859 free(fileindex_path);
6860 free(tmp_branch_name);
6861 free(new_base_branch_ref_name);
6862 free(branch_ref_name);
6863 if (branch_ref)
6864 got_ref_close(branch_ref);
6865 if (wt_branch)
6866 got_ref_close(wt_branch);
6867 free(wt_branch_tip);
6868 if (err) {
6869 if (*new_base_branch_ref) {
6870 got_ref_close(*new_base_branch_ref);
6871 *new_base_branch_ref = NULL;
6873 if (*tmp_branch) {
6874 got_ref_close(*tmp_branch);
6875 *tmp_branch = NULL;
6877 if (*fileindex) {
6878 got_fileindex_free(*fileindex);
6879 *fileindex = NULL;
6881 lock_worktree(worktree, LOCK_SH);
6883 return err;
6886 const struct got_error *
6887 got_worktree_rebase_continue(struct got_object_id **commit_id,
6888 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6889 struct got_reference **branch, struct got_fileindex **fileindex,
6890 struct got_worktree *worktree, struct got_repository *repo)
6892 const struct got_error *err;
6893 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6894 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6895 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6896 char *fileindex_path = NULL;
6897 int have_staged_files = 0;
6899 *commit_id = NULL;
6900 *new_base_branch = NULL;
6901 *tmp_branch = NULL;
6902 *branch = NULL;
6903 *fileindex = NULL;
6905 err = lock_worktree(worktree, LOCK_EX);
6906 if (err)
6907 return err;
6909 err = open_fileindex(fileindex, &fileindex_path, worktree);
6910 if (err)
6911 goto done;
6913 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6914 &have_staged_files);
6915 if (err && err->code != GOT_ERR_CANCELLED)
6916 goto done;
6917 if (have_staged_files) {
6918 err = got_error(GOT_ERR_STAGED_PATHS);
6919 goto done;
6922 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6923 if (err)
6924 goto done;
6926 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6927 if (err)
6928 goto done;
6930 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6931 if (err)
6932 goto done;
6934 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6935 if (err)
6936 goto done;
6938 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6939 if (err)
6940 goto done;
6942 err = got_ref_open(branch, repo,
6943 got_ref_get_symref_target(branch_ref), 0);
6944 if (err)
6945 goto done;
6947 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6948 if (err)
6949 goto done;
6951 err = got_ref_resolve(commit_id, repo, commit_ref);
6952 if (err)
6953 goto done;
6955 err = got_ref_open(new_base_branch, repo,
6956 new_base_branch_ref_name, 0);
6957 if (err)
6958 goto done;
6960 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6961 if (err)
6962 goto done;
6963 done:
6964 free(commit_ref_name);
6965 free(branch_ref_name);
6966 free(fileindex_path);
6967 if (commit_ref)
6968 got_ref_close(commit_ref);
6969 if (branch_ref)
6970 got_ref_close(branch_ref);
6971 if (err) {
6972 free(*commit_id);
6973 *commit_id = NULL;
6974 if (*tmp_branch) {
6975 got_ref_close(*tmp_branch);
6976 *tmp_branch = NULL;
6978 if (*new_base_branch) {
6979 got_ref_close(*new_base_branch);
6980 *new_base_branch = NULL;
6982 if (*branch) {
6983 got_ref_close(*branch);
6984 *branch = NULL;
6986 if (*fileindex) {
6987 got_fileindex_free(*fileindex);
6988 *fileindex = NULL;
6990 lock_worktree(worktree, LOCK_SH);
6992 return err;
6995 const struct got_error *
6996 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6998 const struct got_error *err;
6999 char *tmp_branch_name = NULL;
7001 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7002 if (err)
7003 return err;
7005 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7006 free(tmp_branch_name);
7007 return NULL;
7010 static const struct got_error *
7011 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7012 const char *diff_path, char **logmsg, void *arg)
7014 *logmsg = arg;
7015 return NULL;
7018 static const struct got_error *
7019 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7020 const char *path, struct got_object_id *blob_id,
7021 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7022 int dirfd, const char *de_name)
7024 return NULL;
7027 struct collect_merged_paths_arg {
7028 got_worktree_checkout_cb progress_cb;
7029 void *progress_arg;
7030 struct got_pathlist_head *merged_paths;
7033 static const struct got_error *
7034 collect_merged_paths(void *arg, unsigned char status, const char *path)
7036 const struct got_error *err;
7037 struct collect_merged_paths_arg *a = arg;
7038 char *p;
7039 struct got_pathlist_entry *new;
7041 err = (*a->progress_cb)(a->progress_arg, status, path);
7042 if (err)
7043 return err;
7045 if (status != GOT_STATUS_MERGE &&
7046 status != GOT_STATUS_ADD &&
7047 status != GOT_STATUS_DELETE &&
7048 status != GOT_STATUS_CONFLICT)
7049 return NULL;
7051 p = strdup(path);
7052 if (p == NULL)
7053 return got_error_from_errno("strdup");
7055 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7056 if (err || new == NULL)
7057 free(p);
7058 return err;
7061 static const struct got_error *
7062 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7063 int is_rebase, struct got_repository *repo)
7065 const struct got_error *err;
7066 struct got_reference *commit_ref = NULL;
7068 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7069 if (err) {
7070 if (err->code != GOT_ERR_NOT_REF)
7071 goto done;
7072 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7073 if (err)
7074 goto done;
7075 err = got_ref_write(commit_ref, repo);
7076 if (err)
7077 goto done;
7078 } else if (is_rebase) {
7079 struct got_object_id *stored_id;
7080 int cmp;
7082 err = got_ref_resolve(&stored_id, repo, commit_ref);
7083 if (err)
7084 goto done;
7085 cmp = got_object_id_cmp(commit_id, stored_id);
7086 free(stored_id);
7087 if (cmp != 0) {
7088 err = got_error(GOT_ERR_REBASE_COMMITID);
7089 goto done;
7092 done:
7093 if (commit_ref)
7094 got_ref_close(commit_ref);
7095 return err;
7098 static const struct got_error *
7099 rebase_merge_files(struct got_pathlist_head *merged_paths,
7100 const char *commit_ref_name, struct got_worktree *worktree,
7101 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7102 struct got_object_id *commit_id, struct got_repository *repo,
7103 got_worktree_checkout_cb progress_cb, void *progress_arg,
7104 got_cancel_cb cancel_cb, void *cancel_arg)
7106 const struct got_error *err;
7107 struct got_reference *commit_ref = NULL;
7108 struct collect_merged_paths_arg cmp_arg;
7109 char *fileindex_path;
7111 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7113 err = get_fileindex_path(&fileindex_path, worktree);
7114 if (err)
7115 return err;
7117 cmp_arg.progress_cb = progress_cb;
7118 cmp_arg.progress_arg = progress_arg;
7119 cmp_arg.merged_paths = merged_paths;
7120 err = merge_files(worktree, fileindex, fileindex_path,
7121 parent_commit_id, commit_id, repo, collect_merged_paths,
7122 &cmp_arg, cancel_cb, cancel_arg);
7123 if (commit_ref)
7124 got_ref_close(commit_ref);
7125 return err;
7128 const struct got_error *
7129 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7130 struct got_worktree *worktree, struct got_fileindex *fileindex,
7131 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7132 struct got_repository *repo,
7133 got_worktree_checkout_cb progress_cb, void *progress_arg,
7134 got_cancel_cb cancel_cb, void *cancel_arg)
7136 const struct got_error *err;
7137 char *commit_ref_name;
7139 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7140 if (err)
7141 return err;
7143 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7144 if (err)
7145 goto done;
7147 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7148 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7149 progress_arg, cancel_cb, cancel_arg);
7150 done:
7151 free(commit_ref_name);
7152 return err;
7155 const struct got_error *
7156 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7157 struct got_worktree *worktree, struct got_fileindex *fileindex,
7158 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7159 struct got_repository *repo,
7160 got_worktree_checkout_cb progress_cb, void *progress_arg,
7161 got_cancel_cb cancel_cb, void *cancel_arg)
7163 const struct got_error *err;
7164 char *commit_ref_name;
7166 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7167 if (err)
7168 return err;
7170 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7171 if (err)
7172 goto done;
7174 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7175 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7176 progress_arg, cancel_cb, cancel_arg);
7177 done:
7178 free(commit_ref_name);
7179 return err;
7182 static const struct got_error *
7183 rebase_commit(struct got_object_id **new_commit_id,
7184 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7185 struct got_worktree *worktree, struct got_fileindex *fileindex,
7186 struct got_reference *tmp_branch, const char *committer,
7187 struct got_commit_object *orig_commit, const char *new_logmsg,
7188 int allow_conflict, struct got_repository *repo)
7190 const struct got_error *err, *sync_err;
7191 struct got_pathlist_head commitable_paths;
7192 struct collect_commitables_arg cc_arg;
7193 char *fileindex_path = NULL;
7194 struct got_reference *head_ref = NULL;
7195 struct got_object_id *head_commit_id = NULL;
7196 char *logmsg = NULL;
7198 memset(&cc_arg, 0, sizeof(cc_arg));
7199 TAILQ_INIT(&commitable_paths);
7200 *new_commit_id = NULL;
7202 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7204 err = get_fileindex_path(&fileindex_path, worktree);
7205 if (err)
7206 return err;
7208 cc_arg.commitable_paths = &commitable_paths;
7209 cc_arg.worktree = worktree;
7210 cc_arg.repo = repo;
7211 cc_arg.have_staged_files = 0;
7212 cc_arg.commit_conflicts = allow_conflict;
7214 * If possible get the status of individual files directly to
7215 * avoid crawling the entire work tree once per rebased commit.
7217 * Ideally, merged_paths would contain a list of commitables
7218 * we could use so we could skip worktree_status() entirely.
7219 * However, we would then need carefully keep track of cumulative
7220 * effects of operations such as file additions and deletions
7221 * in 'got histedit -f' (folding multiple commits into one),
7222 * and this extra complexity is not really worth it.
7224 if (merged_paths) {
7225 struct got_pathlist_entry *pe;
7226 TAILQ_FOREACH(pe, merged_paths, entry) {
7227 err = worktree_status(worktree, pe->path, fileindex,
7228 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7229 0);
7230 if (err)
7231 goto done;
7233 } else {
7234 err = worktree_status(worktree, "", fileindex, repo,
7235 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7236 if (err)
7237 goto done;
7240 if (TAILQ_EMPTY(&commitable_paths)) {
7241 /* No-op change; commit will be elided. */
7242 err = got_ref_delete(commit_ref, repo);
7243 if (err)
7244 goto done;
7245 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7246 goto done;
7249 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7250 if (err)
7251 goto done;
7253 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7254 if (err)
7255 goto done;
7257 if (new_logmsg) {
7258 logmsg = strdup(new_logmsg);
7259 if (logmsg == NULL) {
7260 err = got_error_from_errno("strdup");
7261 goto done;
7263 } else {
7264 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7265 if (err)
7266 goto done;
7269 /* NB: commit_worktree will call free(logmsg) */
7270 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7271 NULL, worktree, got_object_commit_get_author(orig_commit),
7272 committer ? committer :
7273 got_object_commit_get_committer(orig_commit), NULL,
7274 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7275 if (err)
7276 goto done;
7278 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7279 if (err)
7280 goto done;
7282 err = got_ref_delete(commit_ref, repo);
7283 if (err)
7284 goto done;
7286 err = update_fileindex_after_commit(worktree, &commitable_paths,
7287 *new_commit_id, fileindex, 0);
7288 sync_err = sync_fileindex(fileindex, fileindex_path);
7289 if (sync_err && err == NULL)
7290 err = sync_err;
7291 done:
7292 free(fileindex_path);
7293 free(head_commit_id);
7294 if (head_ref)
7295 got_ref_close(head_ref);
7296 if (err) {
7297 free(*new_commit_id);
7298 *new_commit_id = NULL;
7300 return err;
7303 const struct got_error *
7304 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7305 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7306 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7307 const char *committer, struct got_commit_object *orig_commit,
7308 struct got_object_id *orig_commit_id, int allow_conflict,
7309 struct got_repository *repo)
7311 const struct got_error *err;
7312 char *commit_ref_name;
7313 struct got_reference *commit_ref = NULL;
7314 struct got_object_id *commit_id = NULL;
7316 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7317 if (err)
7318 return err;
7320 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7321 if (err)
7322 goto done;
7323 err = got_ref_resolve(&commit_id, repo, commit_ref);
7324 if (err)
7325 goto done;
7326 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7327 err = got_error(GOT_ERR_REBASE_COMMITID);
7328 goto done;
7331 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7332 worktree, fileindex, tmp_branch, committer, orig_commit,
7333 NULL, allow_conflict, repo);
7334 done:
7335 if (commit_ref)
7336 got_ref_close(commit_ref);
7337 free(commit_ref_name);
7338 free(commit_id);
7339 return err;
7342 const struct got_error *
7343 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7344 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7345 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7346 const char *committer, struct got_commit_object *orig_commit,
7347 struct got_object_id *orig_commit_id, const char *new_logmsg,
7348 int allow_conflict, struct got_repository *repo)
7350 const struct got_error *err;
7351 char *commit_ref_name;
7352 struct got_reference *commit_ref = NULL;
7354 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7355 if (err)
7356 return err;
7358 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7359 if (err)
7360 goto done;
7362 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7363 worktree, fileindex, tmp_branch, committer, orig_commit,
7364 new_logmsg, allow_conflict, repo);
7365 done:
7366 if (commit_ref)
7367 got_ref_close(commit_ref);
7368 free(commit_ref_name);
7369 return err;
7372 const struct got_error *
7373 got_worktree_rebase_postpone(struct got_worktree *worktree,
7374 struct got_fileindex *fileindex)
7376 if (fileindex)
7377 got_fileindex_free(fileindex);
7378 return lock_worktree(worktree, LOCK_SH);
7381 static const struct got_error *
7382 delete_ref(const char *name, struct got_repository *repo)
7384 const struct got_error *err;
7385 struct got_reference *ref;
7387 err = got_ref_open(&ref, repo, name, 0);
7388 if (err) {
7389 if (err->code == GOT_ERR_NOT_REF)
7390 return NULL;
7391 return err;
7394 err = got_ref_delete(ref, repo);
7395 got_ref_close(ref);
7396 return err;
7399 static const struct got_error *
7400 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7402 const struct got_error *err;
7403 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7404 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7406 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7407 if (err)
7408 goto done;
7409 err = delete_ref(tmp_branch_name, repo);
7410 if (err)
7411 goto done;
7413 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7414 if (err)
7415 goto done;
7416 err = delete_ref(new_base_branch_ref_name, repo);
7417 if (err)
7418 goto done;
7420 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7421 if (err)
7422 goto done;
7423 err = delete_ref(branch_ref_name, repo);
7424 if (err)
7425 goto done;
7427 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7428 if (err)
7429 goto done;
7430 err = delete_ref(commit_ref_name, repo);
7431 if (err)
7432 goto done;
7434 done:
7435 free(tmp_branch_name);
7436 free(new_base_branch_ref_name);
7437 free(branch_ref_name);
7438 free(commit_ref_name);
7439 return err;
7442 static const struct got_error *
7443 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7444 struct got_object_id *new_commit_id, struct got_repository *repo)
7446 const struct got_error *err;
7447 struct got_reference *ref = NULL;
7448 struct got_object_id *old_commit_id = NULL;
7449 const char *branch_name = NULL;
7450 char *new_id_str = NULL;
7451 char *refname = NULL;
7453 branch_name = got_ref_get_name(branch);
7454 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7455 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7456 branch_name += 11;
7458 err = got_object_id_str(&new_id_str, new_commit_id);
7459 if (err)
7460 return err;
7462 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7463 new_id_str) == -1) {
7464 err = got_error_from_errno("asprintf");
7465 goto done;
7468 err = got_ref_resolve(&old_commit_id, repo, branch);
7469 if (err)
7470 goto done;
7472 err = got_ref_alloc(&ref, refname, old_commit_id);
7473 if (err)
7474 goto done;
7476 err = got_ref_write(ref, repo);
7477 done:
7478 free(new_id_str);
7479 free(refname);
7480 free(old_commit_id);
7481 if (ref)
7482 got_ref_close(ref);
7483 return err;
7486 const struct got_error *
7487 got_worktree_rebase_complete(struct got_worktree *worktree,
7488 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7489 struct got_reference *rebased_branch, struct got_repository *repo,
7490 int create_backup)
7492 const struct got_error *err, *unlockerr, *sync_err;
7493 struct got_object_id *new_head_commit_id = NULL;
7494 char *fileindex_path = NULL;
7496 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7497 if (err)
7498 return err;
7500 if (create_backup) {
7501 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7502 rebased_branch, new_head_commit_id, repo);
7503 if (err)
7504 goto done;
7507 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7508 if (err)
7509 goto done;
7511 err = got_ref_write(rebased_branch, repo);
7512 if (err)
7513 goto done;
7515 err = got_worktree_set_head_ref(worktree, rebased_branch);
7516 if (err)
7517 goto done;
7519 err = delete_rebase_refs(worktree, repo);
7520 if (err)
7521 goto done;
7523 err = get_fileindex_path(&fileindex_path, worktree);
7524 if (err)
7525 goto done;
7526 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7527 sync_err = sync_fileindex(fileindex, fileindex_path);
7528 if (sync_err && err == NULL)
7529 err = sync_err;
7530 done:
7531 got_fileindex_free(fileindex);
7532 free(fileindex_path);
7533 free(new_head_commit_id);
7534 unlockerr = lock_worktree(worktree, LOCK_SH);
7535 if (unlockerr && err == NULL)
7536 err = unlockerr;
7537 return err;
7540 static const struct got_error *
7541 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7542 struct got_object_id *id1, struct got_object_id *id2,
7543 struct got_repository *repo)
7545 const struct got_error *err;
7546 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7547 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7549 if (id1) {
7550 err = got_object_open_as_commit(&commit1, repo, id1);
7551 if (err)
7552 goto done;
7554 err = got_object_open_as_tree(&tree1, repo,
7555 got_object_commit_get_tree_id(commit1));
7556 if (err)
7557 goto done;
7560 if (id2) {
7561 err = got_object_open_as_commit(&commit2, repo, id2);
7562 if (err)
7563 goto done;
7565 err = got_object_open_as_tree(&tree2, repo,
7566 got_object_commit_get_tree_id(commit2));
7567 if (err)
7568 goto done;
7571 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7572 got_diff_tree_collect_changed_paths, paths, 0);
7573 if (err)
7574 goto done;
7575 done:
7576 if (commit1)
7577 got_object_commit_close(commit1);
7578 if (commit2)
7579 got_object_commit_close(commit2);
7580 if (tree1)
7581 got_object_tree_close(tree1);
7582 if (tree2)
7583 got_object_tree_close(tree2);
7584 return err;
7587 static const struct got_error *
7588 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7589 struct got_object_id *id1, struct got_object_id *id2,
7590 const char *path_prefix, struct got_repository *repo)
7592 const struct got_error *err;
7593 struct got_pathlist_head merged_paths;
7594 struct got_pathlist_entry *pe;
7595 char *abspath = NULL, *wt_path = NULL;
7597 TAILQ_INIT(&merged_paths);
7599 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7600 if (err)
7601 goto done;
7603 TAILQ_FOREACH(pe, &merged_paths, entry) {
7604 struct got_diff_changed_path *change = pe->data;
7606 if (change->status != GOT_STATUS_ADD)
7607 continue;
7609 if (got_path_is_root_dir(path_prefix)) {
7610 wt_path = strdup(pe->path);
7611 if (wt_path == NULL) {
7612 err = got_error_from_errno("strdup");
7613 goto done;
7615 } else {
7616 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7617 err = got_error_from_errno("asprintf");
7618 goto done;
7621 err = got_path_skip_common_ancestor(&wt_path,
7622 path_prefix, abspath);
7623 if (err)
7624 goto done;
7625 free(abspath);
7626 abspath = NULL;
7629 err = got_pathlist_append(added_paths, wt_path, NULL);
7630 if (err)
7631 goto done;
7632 wt_path = NULL;
7635 done:
7636 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7637 free(abspath);
7638 free(wt_path);
7639 return err;
7642 static const struct got_error *
7643 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7644 struct got_object_id *id, const char *path_prefix,
7645 struct got_repository *repo)
7647 const struct got_error *err;
7648 struct got_commit_object *commit = NULL;
7649 struct got_object_qid *pid;
7651 err = got_object_open_as_commit(&commit, repo, id);
7652 if (err)
7653 goto done;
7655 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7657 err = get_paths_added_between_commits(added_paths,
7658 pid ? &pid->id : NULL, id, path_prefix, repo);
7659 if (err)
7660 goto done;
7661 done:
7662 if (commit)
7663 got_object_commit_close(commit);
7664 return err;
7667 const struct got_error *
7668 got_worktree_rebase_abort(struct got_worktree *worktree,
7669 struct got_fileindex *fileindex, struct got_repository *repo,
7670 struct got_reference *new_base_branch,
7671 got_worktree_checkout_cb progress_cb, void *progress_arg)
7673 const struct got_error *err, *unlockerr, *sync_err;
7674 struct got_reference *resolved = NULL;
7675 struct got_object_id *commit_id = NULL;
7676 struct got_object_id *merged_commit_id = NULL;
7677 struct got_commit_object *commit = NULL;
7678 char *fileindex_path = NULL;
7679 char *commit_ref_name = NULL;
7680 struct got_reference *commit_ref = NULL;
7681 struct revert_file_args rfa;
7682 struct got_object_id *tree_id = NULL;
7683 struct got_pathlist_head added_paths;
7685 TAILQ_INIT(&added_paths);
7687 err = lock_worktree(worktree, LOCK_EX);
7688 if (err)
7689 return err;
7691 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7692 if (err)
7693 goto done;
7695 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7696 if (err)
7697 goto done;
7699 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7700 if (err)
7701 goto done;
7704 * Determine which files in added status can be safely removed
7705 * from disk while reverting changes in the work tree.
7706 * We want to avoid deleting unrelated files which were added by
7707 * the user for conflict resolution purposes.
7709 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7710 got_worktree_get_path_prefix(worktree), repo);
7711 if (err)
7712 goto done;
7714 err = got_ref_open(&resolved, repo,
7715 got_ref_get_symref_target(new_base_branch), 0);
7716 if (err)
7717 goto done;
7719 err = got_worktree_set_head_ref(worktree, resolved);
7720 if (err)
7721 goto done;
7724 * XXX commits to the base branch could have happened while
7725 * we were busy rebasing; should we store the original commit ID
7726 * when rebase begins and read it back here?
7728 err = got_ref_resolve(&commit_id, repo, resolved);
7729 if (err)
7730 goto done;
7732 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7733 if (err)
7734 goto done;
7736 err = got_object_open_as_commit(&commit, repo,
7737 worktree->base_commit_id);
7738 if (err)
7739 goto done;
7741 err = got_object_id_by_path(&tree_id, repo, commit,
7742 worktree->path_prefix);
7743 if (err)
7744 goto done;
7746 err = delete_rebase_refs(worktree, repo);
7747 if (err)
7748 goto done;
7750 err = get_fileindex_path(&fileindex_path, worktree);
7751 if (err)
7752 goto done;
7754 rfa.worktree = worktree;
7755 rfa.fileindex = fileindex;
7756 rfa.progress_cb = progress_cb;
7757 rfa.progress_arg = progress_arg;
7758 rfa.patch_cb = NULL;
7759 rfa.patch_arg = NULL;
7760 rfa.repo = repo;
7761 rfa.unlink_added_files = 1;
7762 rfa.added_files_to_unlink = &added_paths;
7763 err = worktree_status(worktree, "", fileindex, repo,
7764 revert_file, &rfa, NULL, NULL, 1, 0);
7765 if (err)
7766 goto sync;
7768 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7769 repo, progress_cb, progress_arg, NULL, NULL);
7770 sync:
7771 sync_err = sync_fileindex(fileindex, fileindex_path);
7772 if (sync_err && err == NULL)
7773 err = sync_err;
7774 done:
7775 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7776 got_ref_close(resolved);
7777 free(tree_id);
7778 free(commit_id);
7779 free(merged_commit_id);
7780 if (commit)
7781 got_object_commit_close(commit);
7782 if (fileindex)
7783 got_fileindex_free(fileindex);
7784 free(fileindex_path);
7785 free(commit_ref_name);
7786 if (commit_ref)
7787 got_ref_close(commit_ref);
7789 unlockerr = lock_worktree(worktree, LOCK_SH);
7790 if (unlockerr && err == NULL)
7791 err = unlockerr;
7792 return err;
7795 const struct got_error *
7796 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7797 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7798 struct got_fileindex **fileindex, struct got_worktree *worktree,
7799 struct got_repository *repo)
7801 const struct got_error *err = NULL;
7802 char *tmp_branch_name = NULL;
7803 char *branch_ref_name = NULL;
7804 char *base_commit_ref_name = NULL;
7805 char *fileindex_path = NULL;
7806 struct check_rebase_ok_arg ok_arg;
7807 struct got_reference *wt_branch = NULL;
7808 struct got_reference *base_commit_ref = NULL;
7810 *tmp_branch = NULL;
7811 *branch_ref = NULL;
7812 *base_commit_id = NULL;
7813 *fileindex = NULL;
7815 err = lock_worktree(worktree, LOCK_EX);
7816 if (err)
7817 return err;
7819 err = open_fileindex(fileindex, &fileindex_path, worktree);
7820 if (err)
7821 goto done;
7823 ok_arg.worktree = worktree;
7824 ok_arg.repo = repo;
7825 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7826 &ok_arg);
7827 if (err)
7828 goto done;
7830 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7831 if (err)
7832 goto done;
7834 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7835 if (err)
7836 goto done;
7838 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7839 worktree);
7840 if (err)
7841 goto done;
7843 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7844 0);
7845 if (err)
7846 goto done;
7848 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7849 if (err)
7850 goto done;
7852 err = got_ref_write(*branch_ref, repo);
7853 if (err)
7854 goto done;
7856 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7857 worktree->base_commit_id);
7858 if (err)
7859 goto done;
7860 err = got_ref_write(base_commit_ref, repo);
7861 if (err)
7862 goto done;
7863 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7864 if (*base_commit_id == NULL) {
7865 err = got_error_from_errno("got_object_id_dup");
7866 goto done;
7869 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7870 worktree->base_commit_id);
7871 if (err)
7872 goto done;
7873 err = got_ref_write(*tmp_branch, repo);
7874 if (err)
7875 goto done;
7877 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7878 if (err)
7879 goto done;
7880 done:
7881 free(fileindex_path);
7882 free(tmp_branch_name);
7883 free(branch_ref_name);
7884 free(base_commit_ref_name);
7885 if (wt_branch)
7886 got_ref_close(wt_branch);
7887 if (err) {
7888 if (*branch_ref) {
7889 got_ref_close(*branch_ref);
7890 *branch_ref = NULL;
7892 if (*tmp_branch) {
7893 got_ref_close(*tmp_branch);
7894 *tmp_branch = NULL;
7896 free(*base_commit_id);
7897 if (*fileindex) {
7898 got_fileindex_free(*fileindex);
7899 *fileindex = NULL;
7901 lock_worktree(worktree, LOCK_SH);
7903 return err;
7906 const struct got_error *
7907 got_worktree_histedit_postpone(struct got_worktree *worktree,
7908 struct got_fileindex *fileindex)
7910 if (fileindex)
7911 got_fileindex_free(fileindex);
7912 return lock_worktree(worktree, LOCK_SH);
7915 const struct got_error *
7916 got_worktree_histedit_in_progress(int *in_progress,
7917 struct got_worktree *worktree)
7919 const struct got_error *err;
7920 char *tmp_branch_name = NULL;
7922 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7923 if (err)
7924 return err;
7926 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7927 free(tmp_branch_name);
7928 return NULL;
7931 const struct got_error *
7932 got_worktree_histedit_continue(struct got_object_id **commit_id,
7933 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7934 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7935 struct got_worktree *worktree, struct got_repository *repo)
7937 const struct got_error *err;
7938 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7939 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7940 struct got_reference *commit_ref = NULL;
7941 struct got_reference *base_commit_ref = NULL;
7942 char *fileindex_path = NULL;
7943 int have_staged_files = 0;
7945 *commit_id = NULL;
7946 *tmp_branch = NULL;
7947 *base_commit_id = NULL;
7948 *fileindex = NULL;
7950 err = lock_worktree(worktree, LOCK_EX);
7951 if (err)
7952 return err;
7954 err = open_fileindex(fileindex, &fileindex_path, worktree);
7955 if (err)
7956 goto done;
7958 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7959 &have_staged_files);
7960 if (err && err->code != GOT_ERR_CANCELLED)
7961 goto done;
7962 if (have_staged_files) {
7963 err = got_error(GOT_ERR_STAGED_PATHS);
7964 goto done;
7967 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7968 if (err)
7969 goto done;
7971 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7972 if (err)
7973 goto done;
7975 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7976 if (err)
7977 goto done;
7979 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7980 worktree);
7981 if (err)
7982 goto done;
7984 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7985 if (err)
7986 goto done;
7988 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7989 if (err)
7990 goto done;
7991 err = got_ref_resolve(commit_id, repo, commit_ref);
7992 if (err)
7993 goto done;
7995 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7996 if (err)
7997 goto done;
7998 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7999 if (err)
8000 goto done;
8002 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8003 if (err)
8004 goto done;
8005 done:
8006 free(commit_ref_name);
8007 free(branch_ref_name);
8008 free(fileindex_path);
8009 if (commit_ref)
8010 got_ref_close(commit_ref);
8011 if (base_commit_ref)
8012 got_ref_close(base_commit_ref);
8013 if (err) {
8014 free(*commit_id);
8015 *commit_id = NULL;
8016 free(*base_commit_id);
8017 *base_commit_id = NULL;
8018 if (*tmp_branch) {
8019 got_ref_close(*tmp_branch);
8020 *tmp_branch = NULL;
8022 if (*fileindex) {
8023 got_fileindex_free(*fileindex);
8024 *fileindex = NULL;
8026 lock_worktree(worktree, LOCK_EX);
8028 return err;
8031 static const struct got_error *
8032 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8034 const struct got_error *err;
8035 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8036 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8038 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8039 if (err)
8040 goto done;
8041 err = delete_ref(tmp_branch_name, repo);
8042 if (err)
8043 goto done;
8045 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8046 worktree);
8047 if (err)
8048 goto done;
8049 err = delete_ref(base_commit_ref_name, repo);
8050 if (err)
8051 goto done;
8053 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8054 if (err)
8055 goto done;
8056 err = delete_ref(branch_ref_name, repo);
8057 if (err)
8058 goto done;
8060 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8061 if (err)
8062 goto done;
8063 err = delete_ref(commit_ref_name, repo);
8064 if (err)
8065 goto done;
8066 done:
8067 free(tmp_branch_name);
8068 free(base_commit_ref_name);
8069 free(branch_ref_name);
8070 free(commit_ref_name);
8071 return err;
8074 const struct got_error *
8075 got_worktree_histedit_abort(struct got_worktree *worktree,
8076 struct got_fileindex *fileindex, struct got_repository *repo,
8077 struct got_reference *branch, struct got_object_id *base_commit_id,
8078 got_worktree_checkout_cb progress_cb, void *progress_arg)
8080 const struct got_error *err, *unlockerr, *sync_err;
8081 struct got_reference *resolved = NULL;
8082 char *fileindex_path = NULL;
8083 struct got_object_id *merged_commit_id = NULL;
8084 struct got_commit_object *commit = NULL;
8085 char *commit_ref_name = NULL;
8086 struct got_reference *commit_ref = NULL;
8087 struct got_object_id *tree_id = NULL;
8088 struct revert_file_args rfa;
8089 struct got_pathlist_head added_paths;
8091 TAILQ_INIT(&added_paths);
8093 err = lock_worktree(worktree, LOCK_EX);
8094 if (err)
8095 return err;
8097 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8098 if (err)
8099 goto done;
8101 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8102 if (err) {
8103 if (err->code != GOT_ERR_NOT_REF)
8104 goto done;
8105 /* Can happen on early abort due to invalid histedit script. */
8106 commit_ref = NULL;
8109 if (commit_ref) {
8110 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8111 if (err)
8112 goto done;
8115 * Determine which files in added status can be safely removed
8116 * from disk while reverting changes in the work tree.
8117 * We want to avoid deleting unrelated files added by the
8118 * user during conflict resolution or during histedit -e.
8120 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8121 got_worktree_get_path_prefix(worktree), repo);
8122 if (err)
8123 goto done;
8126 err = got_ref_open(&resolved, repo,
8127 got_ref_get_symref_target(branch), 0);
8128 if (err)
8129 goto done;
8131 err = got_worktree_set_head_ref(worktree, resolved);
8132 if (err)
8133 goto done;
8135 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8136 if (err)
8137 goto done;
8139 err = got_object_open_as_commit(&commit, repo,
8140 worktree->base_commit_id);
8141 if (err)
8142 goto done;
8144 err = got_object_id_by_path(&tree_id, repo, commit,
8145 worktree->path_prefix);
8146 if (err)
8147 goto done;
8149 err = delete_histedit_refs(worktree, repo);
8150 if (err)
8151 goto done;
8153 err = get_fileindex_path(&fileindex_path, worktree);
8154 if (err)
8155 goto done;
8157 rfa.worktree = worktree;
8158 rfa.fileindex = fileindex;
8159 rfa.progress_cb = progress_cb;
8160 rfa.progress_arg = progress_arg;
8161 rfa.patch_cb = NULL;
8162 rfa.patch_arg = NULL;
8163 rfa.repo = repo;
8164 rfa.unlink_added_files = 1;
8165 rfa.added_files_to_unlink = &added_paths;
8166 err = worktree_status(worktree, "", fileindex, repo,
8167 revert_file, &rfa, NULL, NULL, 1, 0);
8168 if (err)
8169 goto sync;
8171 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8172 repo, progress_cb, progress_arg, NULL, NULL);
8173 sync:
8174 sync_err = sync_fileindex(fileindex, fileindex_path);
8175 if (sync_err && err == NULL)
8176 err = sync_err;
8177 done:
8178 if (resolved)
8179 got_ref_close(resolved);
8180 if (commit_ref)
8181 got_ref_close(commit_ref);
8182 free(merged_commit_id);
8183 free(tree_id);
8184 free(fileindex_path);
8185 free(commit_ref_name);
8187 unlockerr = lock_worktree(worktree, LOCK_SH);
8188 if (unlockerr && err == NULL)
8189 err = unlockerr;
8190 return err;
8193 const struct got_error *
8194 got_worktree_histedit_complete(struct got_worktree *worktree,
8195 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8196 struct got_reference *edited_branch, struct got_repository *repo)
8198 const struct got_error *err, *unlockerr, *sync_err;
8199 struct got_object_id *new_head_commit_id = NULL;
8200 struct got_reference *resolved = NULL;
8201 char *fileindex_path = NULL;
8203 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8204 if (err)
8205 return err;
8207 err = got_ref_open(&resolved, repo,
8208 got_ref_get_symref_target(edited_branch), 0);
8209 if (err)
8210 goto done;
8212 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8213 resolved, new_head_commit_id, repo);
8214 if (err)
8215 goto done;
8217 err = got_ref_change_ref(resolved, new_head_commit_id);
8218 if (err)
8219 goto done;
8221 err = got_ref_write(resolved, repo);
8222 if (err)
8223 goto done;
8225 err = got_worktree_set_head_ref(worktree, resolved);
8226 if (err)
8227 goto done;
8229 err = delete_histedit_refs(worktree, repo);
8230 if (err)
8231 goto done;
8233 err = get_fileindex_path(&fileindex_path, worktree);
8234 if (err)
8235 goto done;
8236 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8237 sync_err = sync_fileindex(fileindex, fileindex_path);
8238 if (sync_err && err == NULL)
8239 err = sync_err;
8240 done:
8241 got_fileindex_free(fileindex);
8242 free(fileindex_path);
8243 free(new_head_commit_id);
8244 unlockerr = lock_worktree(worktree, LOCK_SH);
8245 if (unlockerr && err == NULL)
8246 err = unlockerr;
8247 return err;
8250 const struct got_error *
8251 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8252 struct got_object_id *commit_id, struct got_repository *repo)
8254 const struct got_error *err;
8255 char *commit_ref_name;
8257 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8258 if (err)
8259 return err;
8261 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8262 if (err)
8263 goto done;
8265 err = delete_ref(commit_ref_name, repo);
8266 done:
8267 free(commit_ref_name);
8268 return err;
8271 const struct got_error *
8272 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8273 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8274 struct got_worktree *worktree, const char *refname,
8275 struct got_repository *repo)
8277 const struct got_error *err = NULL;
8278 char *fileindex_path = NULL;
8279 struct check_rebase_ok_arg ok_arg;
8281 *fileindex = NULL;
8282 *branch_ref = NULL;
8283 *base_branch_ref = NULL;
8285 err = lock_worktree(worktree, LOCK_EX);
8286 if (err)
8287 return err;
8289 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8290 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8291 "cannot integrate a branch into itself; "
8292 "update -b or different branch name required");
8293 goto done;
8296 err = open_fileindex(fileindex, &fileindex_path, worktree);
8297 if (err)
8298 goto done;
8300 /* Preconditions are the same as for rebase. */
8301 ok_arg.worktree = worktree;
8302 ok_arg.repo = repo;
8303 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8304 &ok_arg);
8305 if (err)
8306 goto done;
8308 err = got_ref_open(branch_ref, repo, refname, 1);
8309 if (err)
8310 goto done;
8312 err = got_ref_open(base_branch_ref, repo,
8313 got_worktree_get_head_ref_name(worktree), 1);
8314 done:
8315 if (err) {
8316 if (*branch_ref) {
8317 got_ref_close(*branch_ref);
8318 *branch_ref = NULL;
8320 if (*base_branch_ref) {
8321 got_ref_close(*base_branch_ref);
8322 *base_branch_ref = NULL;
8324 if (*fileindex) {
8325 got_fileindex_free(*fileindex);
8326 *fileindex = NULL;
8328 lock_worktree(worktree, LOCK_SH);
8330 return err;
8333 const struct got_error *
8334 got_worktree_integrate_continue(struct got_worktree *worktree,
8335 struct got_fileindex *fileindex, struct got_repository *repo,
8336 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8337 got_worktree_checkout_cb progress_cb, void *progress_arg,
8338 got_cancel_cb cancel_cb, void *cancel_arg)
8340 const struct got_error *err = NULL, *sync_err, *unlockerr;
8341 char *fileindex_path = NULL;
8342 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8343 struct got_commit_object *commit = NULL;
8345 err = get_fileindex_path(&fileindex_path, worktree);
8346 if (err)
8347 goto done;
8349 err = got_ref_resolve(&commit_id, repo, branch_ref);
8350 if (err)
8351 goto done;
8353 err = got_object_open_as_commit(&commit, repo, commit_id);
8354 if (err)
8355 goto done;
8357 err = got_object_id_by_path(&tree_id, repo, commit,
8358 worktree->path_prefix);
8359 if (err)
8360 goto done;
8362 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8363 if (err)
8364 goto done;
8366 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8367 progress_cb, progress_arg, cancel_cb, cancel_arg);
8368 if (err)
8369 goto sync;
8371 err = got_ref_change_ref(base_branch_ref, commit_id);
8372 if (err)
8373 goto sync;
8375 err = got_ref_write(base_branch_ref, repo);
8376 if (err)
8377 goto sync;
8379 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8380 sync:
8381 sync_err = sync_fileindex(fileindex, fileindex_path);
8382 if (sync_err && err == NULL)
8383 err = sync_err;
8385 done:
8386 unlockerr = got_ref_unlock(branch_ref);
8387 if (unlockerr && err == NULL)
8388 err = unlockerr;
8389 got_ref_close(branch_ref);
8391 unlockerr = got_ref_unlock(base_branch_ref);
8392 if (unlockerr && err == NULL)
8393 err = unlockerr;
8394 got_ref_close(base_branch_ref);
8396 got_fileindex_free(fileindex);
8397 free(fileindex_path);
8398 free(tree_id);
8399 if (commit)
8400 got_object_commit_close(commit);
8402 unlockerr = lock_worktree(worktree, LOCK_SH);
8403 if (unlockerr && err == NULL)
8404 err = unlockerr;
8405 return err;
8408 const struct got_error *
8409 got_worktree_integrate_abort(struct got_worktree *worktree,
8410 struct got_fileindex *fileindex, struct got_repository *repo,
8411 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8413 const struct got_error *err = NULL, *unlockerr = NULL;
8415 got_fileindex_free(fileindex);
8417 err = lock_worktree(worktree, LOCK_SH);
8419 unlockerr = got_ref_unlock(branch_ref);
8420 if (unlockerr && err == NULL)
8421 err = unlockerr;
8422 got_ref_close(branch_ref);
8424 unlockerr = got_ref_unlock(base_branch_ref);
8425 if (unlockerr && err == NULL)
8426 err = unlockerr;
8427 got_ref_close(base_branch_ref);
8429 return err;
8432 const struct got_error *
8433 got_worktree_merge_postpone(struct got_worktree *worktree,
8434 struct got_fileindex *fileindex)
8436 const struct got_error *err, *sync_err;
8437 char *fileindex_path = NULL;
8439 err = get_fileindex_path(&fileindex_path, worktree);
8440 if (err)
8441 goto done;
8443 sync_err = sync_fileindex(fileindex, fileindex_path);
8445 err = lock_worktree(worktree, LOCK_SH);
8446 if (sync_err && err == NULL)
8447 err = sync_err;
8448 done:
8449 got_fileindex_free(fileindex);
8450 free(fileindex_path);
8451 return err;
8454 static const struct got_error *
8455 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8457 const struct got_error *err;
8458 char *branch_refname = NULL, *commit_refname = NULL;
8460 err = get_merge_branch_ref_name(&branch_refname, worktree);
8461 if (err)
8462 goto done;
8463 err = delete_ref(branch_refname, repo);
8464 if (err)
8465 goto done;
8467 err = get_merge_commit_ref_name(&commit_refname, worktree);
8468 if (err)
8469 goto done;
8470 err = delete_ref(commit_refname, repo);
8471 if (err)
8472 goto done;
8474 done:
8475 free(branch_refname);
8476 free(commit_refname);
8477 return err;
8480 struct merge_commit_msg_arg {
8481 struct got_worktree *worktree;
8482 const char *branch_name;
8485 static const struct got_error *
8486 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8487 const char *diff_path, char **logmsg, void *arg)
8489 struct merge_commit_msg_arg *a = arg;
8491 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8492 got_worktree_get_head_ref_name(a->worktree)) == -1)
8493 return got_error_from_errno("asprintf");
8495 return NULL;
8499 const struct got_error *
8500 got_worktree_merge_branch(struct got_worktree *worktree,
8501 struct got_fileindex *fileindex,
8502 struct got_object_id *yca_commit_id,
8503 struct got_object_id *branch_tip,
8504 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8505 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8507 const struct got_error *err;
8508 char *fileindex_path = NULL;
8509 struct check_mixed_commits_args cma;
8511 err = get_fileindex_path(&fileindex_path, worktree);
8512 if (err)
8513 goto done;
8515 cma.worktree = worktree;
8516 cma.cancel_cb = cancel_cb;
8517 cma.cancel_arg = cancel_arg;
8519 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8520 &cma);
8521 if (err)
8522 goto done;
8524 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8525 branch_tip, repo, progress_cb, progress_arg,
8526 cancel_cb, cancel_arg);
8527 done:
8528 free(fileindex_path);
8529 return err;
8532 const struct got_error *
8533 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8534 struct got_worktree *worktree, struct got_fileindex *fileindex,
8535 const char *author, const char *committer, int allow_bad_symlinks,
8536 struct got_object_id *branch_tip, const char *branch_name,
8537 int allow_conflict, struct got_repository *repo,
8538 got_worktree_status_cb status_cb, void *status_arg)
8541 const struct got_error *err = NULL, *sync_err;
8542 struct got_pathlist_head commitable_paths;
8543 struct collect_commitables_arg cc_arg;
8544 struct got_pathlist_entry *pe;
8545 struct got_reference *head_ref = NULL;
8546 struct got_object_id *head_commit_id = NULL;
8547 int have_staged_files = 0;
8548 struct merge_commit_msg_arg mcm_arg;
8549 char *fileindex_path = NULL;
8551 memset(&cc_arg, 0, sizeof(cc_arg));
8552 *new_commit_id = NULL;
8554 TAILQ_INIT(&commitable_paths);
8556 err = get_fileindex_path(&fileindex_path, worktree);
8557 if (err)
8558 goto done;
8560 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8561 if (err)
8562 goto done;
8564 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8565 if (err)
8566 goto done;
8568 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8569 &have_staged_files);
8570 if (err && err->code != GOT_ERR_CANCELLED)
8571 goto done;
8572 if (have_staged_files) {
8573 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8574 goto done;
8577 cc_arg.commitable_paths = &commitable_paths;
8578 cc_arg.worktree = worktree;
8579 cc_arg.fileindex = fileindex;
8580 cc_arg.repo = repo;
8581 cc_arg.have_staged_files = have_staged_files;
8582 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8583 cc_arg.commit_conflicts = allow_conflict;
8584 err = worktree_status(worktree, "", fileindex, repo,
8585 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8586 if (err)
8587 goto done;
8589 mcm_arg.worktree = worktree;
8590 mcm_arg.branch_name = branch_name;
8591 err = commit_worktree(new_commit_id, &commitable_paths,
8592 head_commit_id, branch_tip, worktree, author, committer, NULL,
8593 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8594 if (err)
8595 goto done;
8597 err = update_fileindex_after_commit(worktree, &commitable_paths,
8598 *new_commit_id, fileindex, have_staged_files);
8599 sync_err = sync_fileindex(fileindex, fileindex_path);
8600 if (sync_err && err == NULL)
8601 err = sync_err;
8602 done:
8603 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8604 struct got_commitable *ct = pe->data;
8606 free_commitable(ct);
8608 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8609 free(fileindex_path);
8610 return err;
8613 const struct got_error *
8614 got_worktree_merge_complete(struct got_worktree *worktree,
8615 struct got_fileindex *fileindex, struct got_repository *repo)
8617 const struct got_error *err, *unlockerr, *sync_err;
8618 char *fileindex_path = NULL;
8620 err = delete_merge_refs(worktree, repo);
8621 if (err)
8622 goto done;
8624 err = get_fileindex_path(&fileindex_path, worktree);
8625 if (err)
8626 goto done;
8627 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8628 sync_err = sync_fileindex(fileindex, fileindex_path);
8629 if (sync_err && err == NULL)
8630 err = sync_err;
8631 done:
8632 got_fileindex_free(fileindex);
8633 free(fileindex_path);
8634 unlockerr = lock_worktree(worktree, LOCK_SH);
8635 if (unlockerr && err == NULL)
8636 err = unlockerr;
8637 return err;
8640 const struct got_error *
8641 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8642 struct got_repository *repo)
8644 const struct got_error *err;
8645 char *branch_refname = NULL;
8646 struct got_reference *branch_ref = NULL;
8648 *in_progress = 0;
8650 err = get_merge_branch_ref_name(&branch_refname, worktree);
8651 if (err)
8652 return err;
8653 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8654 free(branch_refname);
8655 if (err) {
8656 if (err->code != GOT_ERR_NOT_REF)
8657 return err;
8658 } else
8659 *in_progress = 1;
8661 return NULL;
8664 const struct got_error *got_worktree_merge_prepare(
8665 struct got_fileindex **fileindex, struct got_worktree *worktree,
8666 struct got_repository *repo)
8668 const struct got_error *err = NULL;
8669 char *fileindex_path = NULL;
8670 struct got_reference *wt_branch = NULL;
8671 struct got_object_id *wt_branch_tip = NULL;
8672 struct check_rebase_ok_arg ok_arg;
8674 *fileindex = NULL;
8676 err = lock_worktree(worktree, LOCK_EX);
8677 if (err)
8678 return err;
8680 err = open_fileindex(fileindex, &fileindex_path, worktree);
8681 if (err)
8682 goto done;
8684 /* Preconditions are the same as for rebase. */
8685 ok_arg.worktree = worktree;
8686 ok_arg.repo = repo;
8687 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8688 &ok_arg);
8689 if (err)
8690 goto done;
8692 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8693 0);
8694 if (err)
8695 goto done;
8697 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8698 if (err)
8699 goto done;
8701 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8702 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8703 goto done;
8706 done:
8707 free(fileindex_path);
8708 if (wt_branch)
8709 got_ref_close(wt_branch);
8710 free(wt_branch_tip);
8711 if (err) {
8712 if (*fileindex) {
8713 got_fileindex_free(*fileindex);
8714 *fileindex = NULL;
8716 lock_worktree(worktree, LOCK_SH);
8718 return err;
8721 const struct got_error *got_worktree_merge_write_refs(
8722 struct got_worktree *worktree, struct got_reference *branch,
8723 struct got_repository *repo)
8725 const struct got_error *err = NULL;
8726 char *branch_refname = NULL, *commit_refname = NULL;
8727 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8728 struct got_object_id *branch_tip = NULL;
8730 err = get_merge_branch_ref_name(&branch_refname, worktree);
8731 if (err)
8732 return err;
8734 err = get_merge_commit_ref_name(&commit_refname, worktree);
8735 if (err)
8736 return err;
8738 err = got_ref_resolve(&branch_tip, repo, branch);
8739 if (err)
8740 goto done;
8742 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8743 if (err)
8744 goto done;
8745 err = got_ref_write(branch_ref, repo);
8746 if (err)
8747 goto done;
8749 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8750 if (err)
8751 goto done;
8752 err = got_ref_write(commit_ref, repo);
8753 if (err)
8754 goto done;
8756 done:
8757 free(branch_refname);
8758 free(commit_refname);
8759 if (branch_ref)
8760 got_ref_close(branch_ref);
8761 if (commit_ref)
8762 got_ref_close(commit_ref);
8763 free(branch_tip);
8764 return err;
8767 const struct got_error *
8768 got_worktree_merge_continue(char **branch_name,
8769 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8770 struct got_worktree *worktree, struct got_repository *repo)
8772 const struct got_error *err;
8773 char *commit_refname = NULL, *branch_refname = NULL;
8774 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8775 char *fileindex_path = NULL;
8776 int have_staged_files = 0;
8778 *branch_name = NULL;
8779 *branch_tip = NULL;
8780 *fileindex = NULL;
8782 err = lock_worktree(worktree, LOCK_EX);
8783 if (err)
8784 return err;
8786 err = open_fileindex(fileindex, &fileindex_path, worktree);
8787 if (err)
8788 goto done;
8790 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8791 &have_staged_files);
8792 if (err && err->code != GOT_ERR_CANCELLED)
8793 goto done;
8794 if (have_staged_files) {
8795 err = got_error(GOT_ERR_STAGED_PATHS);
8796 goto done;
8799 err = get_merge_branch_ref_name(&branch_refname, worktree);
8800 if (err)
8801 goto done;
8803 err = get_merge_commit_ref_name(&commit_refname, worktree);
8804 if (err)
8805 goto done;
8807 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8808 if (err)
8809 goto done;
8811 if (!got_ref_is_symbolic(branch_ref)) {
8812 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8813 "%s is not a symbolic reference",
8814 got_ref_get_name(branch_ref));
8815 goto done;
8817 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8818 if (*branch_name == NULL) {
8819 err = got_error_from_errno("strdup");
8820 goto done;
8823 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8824 if (err)
8825 goto done;
8827 err = got_ref_resolve(branch_tip, repo, commit_ref);
8828 if (err)
8829 goto done;
8830 done:
8831 free(commit_refname);
8832 free(branch_refname);
8833 free(fileindex_path);
8834 if (commit_ref)
8835 got_ref_close(commit_ref);
8836 if (branch_ref)
8837 got_ref_close(branch_ref);
8838 if (err) {
8839 if (*branch_name) {
8840 free(*branch_name);
8841 *branch_name = NULL;
8843 free(*branch_tip);
8844 *branch_tip = NULL;
8845 if (*fileindex) {
8846 got_fileindex_free(*fileindex);
8847 *fileindex = NULL;
8849 lock_worktree(worktree, LOCK_SH);
8851 return err;
8854 const struct got_error *
8855 got_worktree_merge_abort(struct got_worktree *worktree,
8856 struct got_fileindex *fileindex, struct got_repository *repo,
8857 got_worktree_checkout_cb progress_cb, void *progress_arg)
8859 const struct got_error *err, *unlockerr, *sync_err;
8860 struct got_commit_object *commit = NULL;
8861 char *fileindex_path = NULL;
8862 struct revert_file_args rfa;
8863 char *commit_ref_name = NULL;
8864 struct got_reference *commit_ref = NULL;
8865 struct got_object_id *merged_commit_id = NULL;
8866 struct got_object_id *tree_id = NULL;
8867 struct got_pathlist_head added_paths;
8869 TAILQ_INIT(&added_paths);
8871 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8872 if (err)
8873 goto done;
8875 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8876 if (err)
8877 goto done;
8879 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8880 if (err)
8881 goto done;
8884 * Determine which files in added status can be safely removed
8885 * from disk while reverting changes in the work tree.
8886 * We want to avoid deleting unrelated files which were added by
8887 * the user for conflict resolution purposes.
8889 err = get_paths_added_between_commits(&added_paths,
8890 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8891 got_worktree_get_path_prefix(worktree), repo);
8892 if (err)
8893 goto done;
8896 err = got_object_open_as_commit(&commit, repo,
8897 worktree->base_commit_id);
8898 if (err)
8899 goto done;
8901 err = got_object_id_by_path(&tree_id, repo, commit,
8902 worktree->path_prefix);
8903 if (err)
8904 goto done;
8906 err = delete_merge_refs(worktree, repo);
8907 if (err)
8908 goto done;
8910 err = get_fileindex_path(&fileindex_path, worktree);
8911 if (err)
8912 goto done;
8914 rfa.worktree = worktree;
8915 rfa.fileindex = fileindex;
8916 rfa.progress_cb = progress_cb;
8917 rfa.progress_arg = progress_arg;
8918 rfa.patch_cb = NULL;
8919 rfa.patch_arg = NULL;
8920 rfa.repo = repo;
8921 rfa.unlink_added_files = 1;
8922 rfa.added_files_to_unlink = &added_paths;
8923 err = worktree_status(worktree, "", fileindex, repo,
8924 revert_file, &rfa, NULL, NULL, 1, 0);
8925 if (err)
8926 goto sync;
8928 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8929 repo, progress_cb, progress_arg, NULL, NULL);
8930 sync:
8931 sync_err = sync_fileindex(fileindex, fileindex_path);
8932 if (sync_err && err == NULL)
8933 err = sync_err;
8934 done:
8935 free(tree_id);
8936 free(merged_commit_id);
8937 if (commit)
8938 got_object_commit_close(commit);
8939 if (fileindex)
8940 got_fileindex_free(fileindex);
8941 free(fileindex_path);
8942 if (commit_ref)
8943 got_ref_close(commit_ref);
8944 free(commit_ref_name);
8946 unlockerr = lock_worktree(worktree, LOCK_SH);
8947 if (unlockerr && err == NULL)
8948 err = unlockerr;
8949 return err;
8952 struct check_stage_ok_arg {
8953 struct got_object_id *head_commit_id;
8954 struct got_worktree *worktree;
8955 struct got_fileindex *fileindex;
8956 struct got_repository *repo;
8957 int have_changes;
8960 static const struct got_error *
8961 check_stage_ok(void *arg, unsigned char status,
8962 unsigned char staged_status, const char *relpath,
8963 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8964 struct got_object_id *commit_id, int dirfd, const char *de_name)
8966 struct check_stage_ok_arg *a = arg;
8967 const struct got_error *err = NULL;
8968 struct got_fileindex_entry *ie;
8969 struct got_object_id base_commit_id;
8970 struct got_object_id *base_commit_idp = NULL;
8971 char *in_repo_path = NULL, *p;
8973 if (status == GOT_STATUS_UNVERSIONED ||
8974 status == GOT_STATUS_NO_CHANGE)
8975 return NULL;
8976 if (status == GOT_STATUS_NONEXISTENT)
8977 return got_error_set_errno(ENOENT, relpath);
8979 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8980 if (ie == NULL)
8981 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8983 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8984 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8985 relpath) == -1)
8986 return got_error_from_errno("asprintf");
8988 if (got_fileindex_entry_has_commit(ie)) {
8989 base_commit_idp = got_fileindex_entry_get_commit_id(
8990 &base_commit_id, ie);
8993 if (status == GOT_STATUS_CONFLICT) {
8994 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8995 goto done;
8996 } else if (status != GOT_STATUS_ADD &&
8997 status != GOT_STATUS_MODIFY &&
8998 status != GOT_STATUS_DELETE) {
8999 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9000 goto done;
9003 a->have_changes = 1;
9005 p = in_repo_path;
9006 while (p[0] == '/')
9007 p++;
9008 err = check_out_of_date(p, status, staged_status,
9009 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9010 GOT_ERR_STAGE_OUT_OF_DATE);
9011 done:
9012 free(in_repo_path);
9013 return err;
9016 struct stage_path_arg {
9017 struct got_worktree *worktree;
9018 struct got_fileindex *fileindex;
9019 struct got_repository *repo;
9020 got_worktree_status_cb status_cb;
9021 void *status_arg;
9022 got_worktree_patch_cb patch_cb;
9023 void *patch_arg;
9024 int staged_something;
9025 int allow_bad_symlinks;
9028 static const struct got_error *
9029 stage_path(void *arg, unsigned char status,
9030 unsigned char staged_status, const char *relpath,
9031 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9032 struct got_object_id *commit_id, int dirfd, const char *de_name)
9034 struct stage_path_arg *a = arg;
9035 const struct got_error *err = NULL;
9036 struct got_fileindex_entry *ie;
9037 char *ondisk_path = NULL, *path_content = NULL;
9038 uint32_t stage;
9039 struct got_object_id *new_staged_blob_id = NULL;
9040 struct stat sb;
9042 if (status == GOT_STATUS_UNVERSIONED)
9043 return NULL;
9045 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9046 if (ie == NULL)
9047 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9049 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9050 relpath)== -1)
9051 return got_error_from_errno("asprintf");
9053 switch (status) {
9054 case GOT_STATUS_ADD:
9055 case GOT_STATUS_MODIFY:
9056 /* XXX could sb.st_mode be passed in by our caller? */
9057 if (lstat(ondisk_path, &sb) == -1) {
9058 err = got_error_from_errno2("lstat", ondisk_path);
9059 break;
9061 if (a->patch_cb) {
9062 if (status == GOT_STATUS_ADD) {
9063 int choice = GOT_PATCH_CHOICE_NONE;
9064 err = (*a->patch_cb)(&choice, a->patch_arg,
9065 status, ie->path, NULL, 1, 1);
9066 if (err)
9067 break;
9068 if (choice != GOT_PATCH_CHOICE_YES)
9069 break;
9070 } else {
9071 err = create_patched_content(&path_content, 0,
9072 staged_blob_id ? staged_blob_id : blob_id,
9073 ondisk_path, dirfd, de_name, ie->path,
9074 a->repo, a->patch_cb, a->patch_arg);
9075 if (err || path_content == NULL)
9076 break;
9079 err = got_object_blob_create(&new_staged_blob_id,
9080 path_content ? path_content : ondisk_path, a->repo);
9081 if (err)
9082 break;
9083 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9084 SHA1_DIGEST_LENGTH);
9085 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9086 stage = GOT_FILEIDX_STAGE_ADD;
9087 else
9088 stage = GOT_FILEIDX_STAGE_MODIFY;
9089 got_fileindex_entry_stage_set(ie, stage);
9090 if (S_ISLNK(sb.st_mode)) {
9091 int is_bad_symlink = 0;
9092 if (!a->allow_bad_symlinks) {
9093 char target_path[PATH_MAX];
9094 ssize_t target_len;
9095 target_len = readlink(ondisk_path, target_path,
9096 sizeof(target_path));
9097 if (target_len == -1) {
9098 err = got_error_from_errno2("readlink",
9099 ondisk_path);
9100 break;
9102 err = is_bad_symlink_target(&is_bad_symlink,
9103 target_path, target_len, ondisk_path,
9104 a->worktree->root_path,
9105 a->worktree->meta_dir);
9106 if (err)
9107 break;
9108 if (is_bad_symlink) {
9109 err = got_error_path(ondisk_path,
9110 GOT_ERR_BAD_SYMLINK);
9111 break;
9114 if (is_bad_symlink)
9115 got_fileindex_entry_staged_filetype_set(ie,
9116 GOT_FILEIDX_MODE_BAD_SYMLINK);
9117 else
9118 got_fileindex_entry_staged_filetype_set(ie,
9119 GOT_FILEIDX_MODE_SYMLINK);
9120 } else {
9121 got_fileindex_entry_staged_filetype_set(ie,
9122 GOT_FILEIDX_MODE_REGULAR_FILE);
9124 a->staged_something = 1;
9125 if (a->status_cb == NULL)
9126 break;
9127 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9128 get_staged_status(ie), relpath, blob_id,
9129 new_staged_blob_id, NULL, dirfd, de_name);
9130 if (err)
9131 break;
9133 * When staging the reverse of the staged diff,
9134 * implicitly unstage the file.
9136 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9137 sizeof(ie->blob_sha1)) == 0) {
9138 got_fileindex_entry_stage_set(ie,
9139 GOT_FILEIDX_STAGE_NONE);
9141 break;
9142 case GOT_STATUS_DELETE:
9143 if (staged_status == GOT_STATUS_DELETE)
9144 break;
9145 if (a->patch_cb) {
9146 int choice = GOT_PATCH_CHOICE_NONE;
9147 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9148 ie->path, NULL, 1, 1);
9149 if (err)
9150 break;
9151 if (choice == GOT_PATCH_CHOICE_NO)
9152 break;
9153 if (choice != GOT_PATCH_CHOICE_YES) {
9154 err = got_error(GOT_ERR_PATCH_CHOICE);
9155 break;
9158 stage = GOT_FILEIDX_STAGE_DELETE;
9159 got_fileindex_entry_stage_set(ie, stage);
9160 a->staged_something = 1;
9161 if (a->status_cb == NULL)
9162 break;
9163 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9164 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9165 de_name);
9166 break;
9167 case GOT_STATUS_NO_CHANGE:
9168 break;
9169 case GOT_STATUS_CONFLICT:
9170 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9171 break;
9172 case GOT_STATUS_NONEXISTENT:
9173 err = got_error_set_errno(ENOENT, relpath);
9174 break;
9175 default:
9176 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9177 break;
9180 if (path_content && unlink(path_content) == -1 && err == NULL)
9181 err = got_error_from_errno2("unlink", path_content);
9182 free(path_content);
9183 free(ondisk_path);
9184 free(new_staged_blob_id);
9185 return err;
9188 const struct got_error *
9189 got_worktree_stage(struct got_worktree *worktree,
9190 struct got_pathlist_head *paths,
9191 got_worktree_status_cb status_cb, void *status_arg,
9192 got_worktree_patch_cb patch_cb, void *patch_arg,
9193 int allow_bad_symlinks, struct got_repository *repo)
9195 const struct got_error *err = NULL, *sync_err, *unlockerr;
9196 struct got_pathlist_entry *pe;
9197 struct got_fileindex *fileindex = NULL;
9198 char *fileindex_path = NULL;
9199 struct got_reference *head_ref = NULL;
9200 struct got_object_id *head_commit_id = NULL;
9201 struct check_stage_ok_arg oka;
9202 struct stage_path_arg spa;
9204 err = lock_worktree(worktree, LOCK_EX);
9205 if (err)
9206 return err;
9208 err = got_ref_open(&head_ref, repo,
9209 got_worktree_get_head_ref_name(worktree), 0);
9210 if (err)
9211 goto done;
9212 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9213 if (err)
9214 goto done;
9215 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9216 if (err)
9217 goto done;
9219 /* Check pre-conditions before staging anything. */
9220 oka.head_commit_id = head_commit_id;
9221 oka.worktree = worktree;
9222 oka.fileindex = fileindex;
9223 oka.repo = repo;
9224 oka.have_changes = 0;
9225 TAILQ_FOREACH(pe, paths, entry) {
9226 err = worktree_status(worktree, pe->path, fileindex, repo,
9227 check_stage_ok, &oka, NULL, NULL, 1, 0);
9228 if (err)
9229 goto done;
9231 if (!oka.have_changes) {
9232 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9233 goto done;
9236 spa.worktree = worktree;
9237 spa.fileindex = fileindex;
9238 spa.repo = repo;
9239 spa.patch_cb = patch_cb;
9240 spa.patch_arg = patch_arg;
9241 spa.status_cb = status_cb;
9242 spa.status_arg = status_arg;
9243 spa.staged_something = 0;
9244 spa.allow_bad_symlinks = allow_bad_symlinks;
9245 TAILQ_FOREACH(pe, paths, entry) {
9246 err = worktree_status(worktree, pe->path, fileindex, repo,
9247 stage_path, &spa, NULL, NULL, 1, 0);
9248 if (err)
9249 goto done;
9251 if (!spa.staged_something) {
9252 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9253 goto done;
9256 sync_err = sync_fileindex(fileindex, fileindex_path);
9257 if (sync_err && err == NULL)
9258 err = sync_err;
9259 done:
9260 if (head_ref)
9261 got_ref_close(head_ref);
9262 free(head_commit_id);
9263 free(fileindex_path);
9264 if (fileindex)
9265 got_fileindex_free(fileindex);
9266 unlockerr = lock_worktree(worktree, LOCK_SH);
9267 if (unlockerr && err == NULL)
9268 err = unlockerr;
9269 return err;
9272 struct unstage_path_arg {
9273 struct got_worktree *worktree;
9274 struct got_fileindex *fileindex;
9275 struct got_repository *repo;
9276 got_worktree_checkout_cb progress_cb;
9277 void *progress_arg;
9278 got_worktree_patch_cb patch_cb;
9279 void *patch_arg;
9282 static const struct got_error *
9283 create_unstaged_content(char **path_unstaged_content,
9284 char **path_new_staged_content, struct got_object_id *blob_id,
9285 struct got_object_id *staged_blob_id, const char *relpath,
9286 struct got_repository *repo,
9287 got_worktree_patch_cb patch_cb, void *patch_arg)
9289 const struct got_error *err, *free_err;
9290 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9291 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9292 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9293 struct got_diffreg_result *diffreg_result = NULL;
9294 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9295 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9296 int fd1 = -1, fd2 = -1;
9298 *path_unstaged_content = NULL;
9299 *path_new_staged_content = NULL;
9301 err = got_object_id_str(&label1, blob_id);
9302 if (err)
9303 return err;
9305 fd1 = got_opentempfd();
9306 if (fd1 == -1) {
9307 err = got_error_from_errno("got_opentempfd");
9308 goto done;
9310 fd2 = got_opentempfd();
9311 if (fd2 == -1) {
9312 err = got_error_from_errno("got_opentempfd");
9313 goto done;
9316 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9317 if (err)
9318 goto done;
9320 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9321 if (err)
9322 goto done;
9324 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9325 if (err)
9326 goto done;
9328 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9329 fd2);
9330 if (err)
9331 goto done;
9333 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9334 if (err)
9335 goto done;
9337 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9338 if (err)
9339 goto done;
9341 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9342 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9343 if (err)
9344 goto done;
9346 err = got_opentemp_named(path_unstaged_content, &outfile,
9347 "got-unstaged-content", "");
9348 if (err)
9349 goto done;
9350 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9351 "got-new-staged-content", "");
9352 if (err)
9353 goto done;
9355 if (fseek(f1, 0L, SEEK_SET) == -1) {
9356 err = got_ferror(f1, GOT_ERR_IO);
9357 goto done;
9359 if (fseek(f2, 0L, SEEK_SET) == -1) {
9360 err = got_ferror(f2, GOT_ERR_IO);
9361 goto done;
9363 /* Count the number of actual changes in the diff result. */
9364 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9365 struct diff_chunk_context cc = {};
9366 diff_chunk_context_load_change(&cc, &nchunks_used,
9367 diffreg_result->result, n, 0);
9368 nchanges++;
9370 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9371 int choice;
9372 err = apply_or_reject_change(&choice, &nchunks_used,
9373 diffreg_result->result, n, relpath, f1, f2,
9374 &line_cur1, &line_cur2,
9375 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9376 if (err)
9377 goto done;
9378 if (choice == GOT_PATCH_CHOICE_YES)
9379 have_content = 1;
9380 else
9381 have_rejected_content = 1;
9382 if (choice == GOT_PATCH_CHOICE_QUIT)
9383 break;
9385 if (have_content || have_rejected_content)
9386 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9387 outfile, rejectfile);
9388 done:
9389 free(label1);
9390 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9391 err = got_error_from_errno("close");
9392 if (blob)
9393 got_object_blob_close(blob);
9394 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9395 err = got_error_from_errno("close");
9396 if (staged_blob)
9397 got_object_blob_close(staged_blob);
9398 free_err = got_diffreg_result_free(diffreg_result);
9399 if (free_err && err == NULL)
9400 err = free_err;
9401 if (f1 && fclose(f1) == EOF && err == NULL)
9402 err = got_error_from_errno2("fclose", path1);
9403 if (f2 && fclose(f2) == EOF && err == NULL)
9404 err = got_error_from_errno2("fclose", path2);
9405 if (outfile && fclose(outfile) == EOF && err == NULL)
9406 err = got_error_from_errno2("fclose", *path_unstaged_content);
9407 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9408 err = got_error_from_errno2("fclose", *path_new_staged_content);
9409 if (path1 && unlink(path1) == -1 && err == NULL)
9410 err = got_error_from_errno2("unlink", path1);
9411 if (path2 && unlink(path2) == -1 && err == NULL)
9412 err = got_error_from_errno2("unlink", path2);
9413 if (err || !have_content) {
9414 if (*path_unstaged_content &&
9415 unlink(*path_unstaged_content) == -1 && err == NULL)
9416 err = got_error_from_errno2("unlink",
9417 *path_unstaged_content);
9418 free(*path_unstaged_content);
9419 *path_unstaged_content = NULL;
9421 if (err || !have_content || !have_rejected_content) {
9422 if (*path_new_staged_content &&
9423 unlink(*path_new_staged_content) == -1 && err == NULL)
9424 err = got_error_from_errno2("unlink",
9425 *path_new_staged_content);
9426 free(*path_new_staged_content);
9427 *path_new_staged_content = NULL;
9429 free(path1);
9430 free(path2);
9431 return err;
9434 static const struct got_error *
9435 unstage_hunks(struct got_object_id *staged_blob_id,
9436 struct got_blob_object *blob_base,
9437 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9438 const char *ondisk_path, const char *label_orig,
9439 struct got_worktree *worktree, struct got_repository *repo,
9440 got_worktree_patch_cb patch_cb, void *patch_arg,
9441 got_worktree_checkout_cb progress_cb, void *progress_arg)
9443 const struct got_error *err = NULL;
9444 char *path_unstaged_content = NULL;
9445 char *path_new_staged_content = NULL;
9446 char *parent = NULL, *base_path = NULL;
9447 char *blob_base_path = NULL;
9448 struct got_object_id *new_staged_blob_id = NULL;
9449 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9450 struct stat sb;
9452 err = create_unstaged_content(&path_unstaged_content,
9453 &path_new_staged_content, blob_id, staged_blob_id,
9454 ie->path, repo, patch_cb, patch_arg);
9455 if (err)
9456 return err;
9458 if (path_unstaged_content == NULL)
9459 return NULL;
9461 if (path_new_staged_content) {
9462 err = got_object_blob_create(&new_staged_blob_id,
9463 path_new_staged_content, repo);
9464 if (err)
9465 goto done;
9468 f = fopen(path_unstaged_content, "re");
9469 if (f == NULL) {
9470 err = got_error_from_errno2("fopen",
9471 path_unstaged_content);
9472 goto done;
9474 if (fstat(fileno(f), &sb) == -1) {
9475 err = got_error_from_errno2("fstat", path_unstaged_content);
9476 goto done;
9478 if (got_fileindex_entry_staged_filetype_get(ie) ==
9479 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9480 char link_target[PATH_MAX];
9481 size_t r;
9482 r = fread(link_target, 1, sizeof(link_target), f);
9483 if (r == 0 && ferror(f)) {
9484 err = got_error_from_errno("fread");
9485 goto done;
9487 if (r >= sizeof(link_target)) { /* should not happen */
9488 err = got_error(GOT_ERR_NO_SPACE);
9489 goto done;
9491 link_target[r] = '\0';
9492 err = merge_symlink(worktree, blob_base,
9493 ondisk_path, ie->path, label_orig, link_target,
9494 worktree->base_commit_id, repo, progress_cb,
9495 progress_arg);
9496 } else {
9497 int local_changes_subsumed;
9499 err = got_path_dirname(&parent, ondisk_path);
9500 if (err)
9501 return err;
9503 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9504 parent) == -1) {
9505 err = got_error_from_errno("asprintf");
9506 base_path = NULL;
9507 goto done;
9510 err = got_opentemp_named(&blob_base_path, &f_base,
9511 base_path, "");
9512 if (err)
9513 goto done;
9514 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9515 blob_base);
9516 if (err)
9517 goto done;
9520 * In order the run a 3-way merge with a symlink we copy the symlink's
9521 * target path into a temporary file and use that file with diff3.
9523 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9524 err = dump_symlink_target_path_to_file(&f_deriv2,
9525 ondisk_path);
9526 if (err)
9527 goto done;
9528 } else {
9529 int fd;
9530 fd = open(ondisk_path,
9531 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9532 if (fd == -1) {
9533 err = got_error_from_errno2("open", ondisk_path);
9534 goto done;
9536 f_deriv2 = fdopen(fd, "r");
9537 if (f_deriv2 == NULL) {
9538 err = got_error_from_errno2("fdopen", ondisk_path);
9539 close(fd);
9540 goto done;
9544 err = merge_file(&local_changes_subsumed, worktree,
9545 f_base, f, f_deriv2, ondisk_path, ie->path,
9546 got_fileindex_perms_to_st(ie),
9547 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9548 repo, progress_cb, progress_arg);
9550 if (err)
9551 goto done;
9553 if (new_staged_blob_id) {
9554 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9555 SHA1_DIGEST_LENGTH);
9556 } else {
9557 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9558 got_fileindex_entry_staged_filetype_set(ie, 0);
9560 done:
9561 free(new_staged_blob_id);
9562 if (path_unstaged_content &&
9563 unlink(path_unstaged_content) == -1 && err == NULL)
9564 err = got_error_from_errno2("unlink", path_unstaged_content);
9565 if (path_new_staged_content &&
9566 unlink(path_new_staged_content) == -1 && err == NULL)
9567 err = got_error_from_errno2("unlink", path_new_staged_content);
9568 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9569 err = got_error_from_errno2("unlink", blob_base_path);
9570 if (f_base && fclose(f_base) == EOF && err == NULL)
9571 err = got_error_from_errno2("fclose", path_unstaged_content);
9572 if (f && fclose(f) == EOF && err == NULL)
9573 err = got_error_from_errno2("fclose", path_unstaged_content);
9574 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9575 err = got_error_from_errno2("fclose", ondisk_path);
9576 free(path_unstaged_content);
9577 free(path_new_staged_content);
9578 free(blob_base_path);
9579 free(parent);
9580 free(base_path);
9581 return err;
9584 static const struct got_error *
9585 unstage_path(void *arg, unsigned char status,
9586 unsigned char staged_status, const char *relpath,
9587 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9588 struct got_object_id *commit_id, int dirfd, const char *de_name)
9590 const struct got_error *err = NULL;
9591 struct unstage_path_arg *a = arg;
9592 struct got_fileindex_entry *ie;
9593 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9594 char *ondisk_path = NULL;
9595 char *id_str = NULL, *label_orig = NULL;
9596 int local_changes_subsumed;
9597 struct stat sb;
9598 int fd1 = -1, fd2 = -1;
9600 if (staged_status != GOT_STATUS_ADD &&
9601 staged_status != GOT_STATUS_MODIFY &&
9602 staged_status != GOT_STATUS_DELETE)
9603 return NULL;
9605 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9606 if (ie == NULL)
9607 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9609 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9610 == -1)
9611 return got_error_from_errno("asprintf");
9613 err = got_object_id_str(&id_str,
9614 commit_id ? commit_id : a->worktree->base_commit_id);
9615 if (err)
9616 goto done;
9617 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9618 id_str) == -1) {
9619 err = got_error_from_errno("asprintf");
9620 goto done;
9623 fd1 = got_opentempfd();
9624 if (fd1 == -1) {
9625 err = got_error_from_errno("got_opentempfd");
9626 goto done;
9628 fd2 = got_opentempfd();
9629 if (fd2 == -1) {
9630 err = got_error_from_errno("got_opentempfd");
9631 goto done;
9634 switch (staged_status) {
9635 case GOT_STATUS_MODIFY:
9636 err = got_object_open_as_blob(&blob_base, a->repo,
9637 blob_id, 8192, fd1);
9638 if (err)
9639 break;
9640 /* fall through */
9641 case GOT_STATUS_ADD:
9642 if (a->patch_cb) {
9643 if (staged_status == GOT_STATUS_ADD) {
9644 int choice = GOT_PATCH_CHOICE_NONE;
9645 err = (*a->patch_cb)(&choice, a->patch_arg,
9646 staged_status, ie->path, NULL, 1, 1);
9647 if (err)
9648 break;
9649 if (choice != GOT_PATCH_CHOICE_YES)
9650 break;
9651 } else {
9652 err = unstage_hunks(staged_blob_id,
9653 blob_base, blob_id, ie, ondisk_path,
9654 label_orig, a->worktree, a->repo,
9655 a->patch_cb, a->patch_arg,
9656 a->progress_cb, a->progress_arg);
9657 break; /* Done with this file. */
9660 err = got_object_open_as_blob(&blob_staged, a->repo,
9661 staged_blob_id, 8192, fd2);
9662 if (err)
9663 break;
9664 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9665 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9666 case GOT_FILEIDX_MODE_REGULAR_FILE:
9667 err = merge_blob(&local_changes_subsumed, a->worktree,
9668 blob_base, ondisk_path, relpath,
9669 got_fileindex_perms_to_st(ie), label_orig,
9670 blob_staged, commit_id ? commit_id :
9671 a->worktree->base_commit_id, a->repo,
9672 a->progress_cb, a->progress_arg);
9673 break;
9674 case GOT_FILEIDX_MODE_SYMLINK:
9675 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9676 char *staged_target;
9677 err = got_object_blob_read_to_str(
9678 &staged_target, blob_staged);
9679 if (err)
9680 goto done;
9681 err = merge_symlink(a->worktree, blob_base,
9682 ondisk_path, relpath, label_orig,
9683 staged_target, commit_id ? commit_id :
9684 a->worktree->base_commit_id,
9685 a->repo, a->progress_cb, a->progress_arg);
9686 free(staged_target);
9687 } else {
9688 err = merge_blob(&local_changes_subsumed,
9689 a->worktree, blob_base, ondisk_path,
9690 relpath, got_fileindex_perms_to_st(ie),
9691 label_orig, blob_staged,
9692 commit_id ? commit_id :
9693 a->worktree->base_commit_id, a->repo,
9694 a->progress_cb, a->progress_arg);
9696 break;
9697 default:
9698 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9699 break;
9701 if (err == NULL) {
9702 got_fileindex_entry_stage_set(ie,
9703 GOT_FILEIDX_STAGE_NONE);
9704 got_fileindex_entry_staged_filetype_set(ie, 0);
9706 break;
9707 case GOT_STATUS_DELETE:
9708 if (a->patch_cb) {
9709 int choice = GOT_PATCH_CHOICE_NONE;
9710 err = (*a->patch_cb)(&choice, a->patch_arg,
9711 staged_status, ie->path, NULL, 1, 1);
9712 if (err)
9713 break;
9714 if (choice == GOT_PATCH_CHOICE_NO)
9715 break;
9716 if (choice != GOT_PATCH_CHOICE_YES) {
9717 err = got_error(GOT_ERR_PATCH_CHOICE);
9718 break;
9721 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9722 got_fileindex_entry_staged_filetype_set(ie, 0);
9723 err = get_file_status(&status, &sb, ie, ondisk_path,
9724 dirfd, de_name, a->repo);
9725 if (err)
9726 break;
9727 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9728 break;
9730 done:
9731 free(ondisk_path);
9732 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9733 err = got_error_from_errno("close");
9734 if (blob_base)
9735 got_object_blob_close(blob_base);
9736 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9737 err = got_error_from_errno("close");
9738 if (blob_staged)
9739 got_object_blob_close(blob_staged);
9740 free(id_str);
9741 free(label_orig);
9742 return err;
9745 const struct got_error *
9746 got_worktree_unstage(struct got_worktree *worktree,
9747 struct got_pathlist_head *paths,
9748 got_worktree_checkout_cb progress_cb, void *progress_arg,
9749 got_worktree_patch_cb patch_cb, void *patch_arg,
9750 struct got_repository *repo)
9752 const struct got_error *err = NULL, *sync_err, *unlockerr;
9753 struct got_pathlist_entry *pe;
9754 struct got_fileindex *fileindex = NULL;
9755 char *fileindex_path = NULL;
9756 struct unstage_path_arg upa;
9758 err = lock_worktree(worktree, LOCK_EX);
9759 if (err)
9760 return err;
9762 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9763 if (err)
9764 goto done;
9766 upa.worktree = worktree;
9767 upa.fileindex = fileindex;
9768 upa.repo = repo;
9769 upa.progress_cb = progress_cb;
9770 upa.progress_arg = progress_arg;
9771 upa.patch_cb = patch_cb;
9772 upa.patch_arg = patch_arg;
9773 TAILQ_FOREACH(pe, paths, entry) {
9774 err = worktree_status(worktree, pe->path, fileindex, repo,
9775 unstage_path, &upa, NULL, NULL, 1, 0);
9776 if (err)
9777 goto done;
9780 sync_err = sync_fileindex(fileindex, fileindex_path);
9781 if (sync_err && err == NULL)
9782 err = sync_err;
9783 done:
9784 free(fileindex_path);
9785 if (fileindex)
9786 got_fileindex_free(fileindex);
9787 unlockerr = lock_worktree(worktree, LOCK_SH);
9788 if (unlockerr && err == NULL)
9789 err = unlockerr;
9790 return err;
9793 struct report_file_info_arg {
9794 struct got_worktree *worktree;
9795 got_worktree_path_info_cb info_cb;
9796 void *info_arg;
9797 struct got_pathlist_head *paths;
9798 got_cancel_cb cancel_cb;
9799 void *cancel_arg;
9802 static const struct got_error *
9803 report_file_info(void *arg, struct got_fileindex_entry *ie)
9805 const struct got_error *err;
9806 struct report_file_info_arg *a = arg;
9807 struct got_pathlist_entry *pe;
9808 struct got_object_id blob_id, staged_blob_id, commit_id;
9809 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9810 struct got_object_id *commit_idp = NULL;
9811 int stage;
9813 if (a->cancel_cb) {
9814 err = a->cancel_cb(a->cancel_arg);
9815 if (err)
9816 return err;
9819 TAILQ_FOREACH(pe, a->paths, entry) {
9820 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9821 got_path_is_child(ie->path, pe->path, pe->path_len))
9822 break;
9824 if (pe == NULL) /* not found */
9825 return NULL;
9827 if (got_fileindex_entry_has_blob(ie))
9828 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9829 stage = got_fileindex_entry_stage_get(ie);
9830 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9831 stage == GOT_FILEIDX_STAGE_ADD) {
9832 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9833 &staged_blob_id, ie);
9836 if (got_fileindex_entry_has_commit(ie))
9837 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9839 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9840 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9843 const struct got_error *
9844 got_worktree_path_info(struct got_worktree *worktree,
9845 struct got_pathlist_head *paths,
9846 got_worktree_path_info_cb info_cb, void *info_arg,
9847 got_cancel_cb cancel_cb, void *cancel_arg)
9850 const struct got_error *err = NULL, *unlockerr;
9851 struct got_fileindex *fileindex = NULL;
9852 char *fileindex_path = NULL;
9853 struct report_file_info_arg arg;
9855 err = lock_worktree(worktree, LOCK_SH);
9856 if (err)
9857 return err;
9859 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9860 if (err)
9861 goto done;
9863 arg.worktree = worktree;
9864 arg.info_cb = info_cb;
9865 arg.info_arg = info_arg;
9866 arg.paths = paths;
9867 arg.cancel_cb = cancel_cb;
9868 arg.cancel_arg = cancel_arg;
9869 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9870 &arg);
9871 done:
9872 free(fileindex_path);
9873 if (fileindex)
9874 got_fileindex_free(fileindex);
9875 unlockerr = lock_worktree(worktree, LOCK_UN);
9876 if (unlockerr && err == NULL)
9877 err = unlockerr;
9878 return err;
9881 static const struct got_error *
9882 patch_check_path(const char *p, char **path, unsigned char *status,
9883 unsigned char *staged_status, struct got_fileindex *fileindex,
9884 struct got_worktree *worktree, struct got_repository *repo)
9886 const struct got_error *err;
9887 struct got_fileindex_entry *ie;
9888 struct stat sb;
9889 char *ondisk_path = NULL;
9891 err = got_worktree_resolve_path(path, worktree, p);
9892 if (err)
9893 return err;
9895 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9896 *path[0] ? "/" : "", *path) == -1)
9897 return got_error_from_errno("asprintf");
9899 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9900 if (ie) {
9901 *staged_status = get_staged_status(ie);
9902 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9903 repo);
9904 if (err)
9905 goto done;
9906 } else {
9907 *staged_status = GOT_STATUS_NO_CHANGE;
9908 *status = GOT_STATUS_UNVERSIONED;
9909 if (lstat(ondisk_path, &sb) == -1) {
9910 if (errno != ENOENT) {
9911 err = got_error_from_errno2("lstat",
9912 ondisk_path);
9913 goto done;
9915 *status = GOT_STATUS_NONEXISTENT;
9919 done:
9920 free(ondisk_path);
9921 return err;
9924 static const struct got_error *
9925 patch_can_rm(const char *path, unsigned char status,
9926 unsigned char staged_status)
9928 if (status == GOT_STATUS_NONEXISTENT)
9929 return got_error_set_errno(ENOENT, path);
9930 if (status != GOT_STATUS_NO_CHANGE &&
9931 status != GOT_STATUS_ADD &&
9932 status != GOT_STATUS_MODIFY &&
9933 status != GOT_STATUS_MODE_CHANGE)
9934 return got_error_path(path, GOT_ERR_FILE_STATUS);
9935 if (staged_status == GOT_STATUS_DELETE)
9936 return got_error_path(path, GOT_ERR_FILE_STATUS);
9937 return NULL;
9940 static const struct got_error *
9941 patch_can_add(const char *path, unsigned char status)
9943 if (status != GOT_STATUS_NONEXISTENT)
9944 return got_error_path(path, GOT_ERR_FILE_STATUS);
9945 return NULL;
9948 static const struct got_error *
9949 patch_can_edit(const char *path, unsigned char status,
9950 unsigned char staged_status)
9952 if (status == GOT_STATUS_NONEXISTENT)
9953 return got_error_set_errno(ENOENT, path);
9954 if (status != GOT_STATUS_NO_CHANGE &&
9955 status != GOT_STATUS_ADD &&
9956 status != GOT_STATUS_MODIFY)
9957 return got_error_path(path, GOT_ERR_FILE_STATUS);
9958 if (staged_status == GOT_STATUS_DELETE)
9959 return got_error_path(path, GOT_ERR_FILE_STATUS);
9960 return NULL;
9963 const struct got_error *
9964 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9965 char **fileindex_path, struct got_worktree *worktree)
9967 return open_fileindex(fileindex, fileindex_path, worktree);
9970 const struct got_error *
9971 got_worktree_patch_check_path(const char *old, const char *new,
9972 char **oldpath, char **newpath, struct got_worktree *worktree,
9973 struct got_repository *repo, struct got_fileindex *fileindex)
9975 const struct got_error *err = NULL;
9976 int file_renamed = 0;
9977 unsigned char status_old, staged_status_old;
9978 unsigned char status_new, staged_status_new;
9980 *oldpath = NULL;
9981 *newpath = NULL;
9983 err = patch_check_path(old != NULL ? old : new, oldpath,
9984 &status_old, &staged_status_old, fileindex, worktree, repo);
9985 if (err)
9986 goto done;
9988 err = patch_check_path(new != NULL ? new : old, newpath,
9989 &status_new, &staged_status_new, fileindex, worktree, repo);
9990 if (err)
9991 goto done;
9993 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9994 file_renamed = 1;
9996 if (old != NULL && new == NULL)
9997 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9998 else if (file_renamed) {
9999 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10000 if (err == NULL)
10001 err = patch_can_add(*newpath, status_new);
10002 } else if (old == NULL)
10003 err = patch_can_add(*newpath, status_new);
10004 else
10005 err = patch_can_edit(*newpath, status_new, staged_status_new);
10007 done:
10008 if (err) {
10009 free(*oldpath);
10010 *oldpath = NULL;
10011 free(*newpath);
10012 *newpath = NULL;
10014 return err;
10017 const struct got_error *
10018 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10019 struct got_worktree *worktree, struct got_fileindex *fileindex,
10020 got_worktree_checkout_cb progress_cb, void *progress_arg)
10022 struct schedule_addition_args saa;
10024 memset(&saa, 0, sizeof(saa));
10025 saa.worktree = worktree;
10026 saa.fileindex = fileindex;
10027 saa.progress_cb = progress_cb;
10028 saa.progress_arg = progress_arg;
10029 saa.repo = repo;
10031 return worktree_status(worktree, path, fileindex, repo,
10032 schedule_addition, &saa, NULL, NULL, 1, 0);
10035 const struct got_error *
10036 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10037 struct got_worktree *worktree, struct got_fileindex *fileindex,
10038 got_worktree_delete_cb progress_cb, void *progress_arg)
10040 const struct got_error *err;
10041 struct schedule_deletion_args sda;
10042 char *ondisk_status_path;
10044 memset(&sda, 0, sizeof(sda));
10045 sda.worktree = worktree;
10046 sda.fileindex = fileindex;
10047 sda.progress_cb = progress_cb;
10048 sda.progress_arg = progress_arg;
10049 sda.repo = repo;
10050 sda.delete_local_mods = 0;
10051 sda.keep_on_disk = 0;
10052 sda.ignore_missing_paths = 0;
10053 sda.status_codes = NULL;
10054 if (asprintf(&ondisk_status_path, "%s/%s",
10055 got_worktree_get_root_path(worktree), path) == -1)
10056 return got_error_from_errno("asprintf");
10057 sda.status_path = ondisk_status_path;
10058 sda.status_path_len = strlen(ondisk_status_path);
10060 err = worktree_status(worktree, path, fileindex, repo,
10061 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10062 free(ondisk_status_path);
10063 return err;
10066 const struct got_error *
10067 got_worktree_patch_complete(struct got_fileindex *fileindex,
10068 const char *fileindex_path)
10070 const struct got_error *err = NULL;
10072 err = sync_fileindex(fileindex, fileindex_path);
10073 got_fileindex_free(fileindex);
10075 return err;