Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t
66 apply_umask(mode_t mode)
67 {
68 mode_t um;
70 um = umask(000);
71 umask(um);
72 return mode & ~um;
73 }
75 static const struct got_error *
76 create_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 char *path;
81 if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 return got_error_from_errno("asprintf");
84 err = got_path_create_file(path, content);
85 free(path);
86 return err;
87 }
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
91 {
92 const struct got_error *err = NULL;
93 FILE *tmpfile = NULL;
94 char *tmppath = NULL;
95 char *path = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno("asprintf");
99 path = NULL;
100 goto done;
103 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
104 if (err)
105 goto done;
107 if (content) {
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno2("fprintf", tmppath);
111 goto done;
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno3("rename", tmppath, path);
117 unlink(tmppath);
118 goto done;
121 done:
122 if (fclose(tmpfile) == EOF && err == NULL)
123 err = got_error_from_errno2("fclose", tmppath);
124 free(tmppath);
125 return err;
128 static const struct got_error *
129 write_head_ref(const char *path_got, struct got_reference *head_ref)
131 const struct got_error *err = NULL;
132 char *refstr = NULL;
134 if (got_ref_is_symbolic(head_ref)) {
135 refstr = got_ref_to_str(head_ref);
136 if (refstr == NULL)
137 return got_error_from_errno("got_ref_to_str");
138 } else {
139 refstr = strdup(got_ref_get_name(head_ref));
140 if (refstr == NULL)
141 return got_error_from_errno("strdup");
143 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 free(refstr);
145 return err;
148 const struct got_error *
149 got_worktree_init(const char *path, struct got_reference *head_ref,
150 const char *prefix, const char *meta_dir, struct got_repository *repo)
152 const struct got_error *err = NULL;
153 struct got_object_id *commit_id = NULL;
154 uuid_t uuid;
155 uint32_t uuid_status;
156 int obj_type;
157 char *path_got = NULL;
158 char *formatstr = NULL;
159 char *absprefix = NULL;
160 char *basestr = NULL;
161 char *uuidstr = NULL;
163 if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 err = got_error(GOT_ERR_WORKTREE_REPO);
165 goto done;
168 err = got_ref_resolve(&commit_id, repo, head_ref);
169 if (err)
170 return err;
171 err = got_object_get_type(&obj_type, repo, commit_id);
172 if (err)
173 return err;
174 if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 return got_error(GOT_ERR_OBJ_TYPE);
177 if (!got_path_is_absolute(prefix)) {
178 if (asprintf(&absprefix, "/%s", prefix) == -1)
179 return got_error_from_errno("asprintf");
182 /* Create top-level directory (may already exist). */
183 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path);
185 goto done;
188 /* Create .got/.cvg directory (may already exist). */
189 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
190 err = got_error_from_errno("asprintf");
191 goto done;
193 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 err = got_error_from_errno2("mkdir", path_got);
195 goto done;
198 /* Create an empty lock file. */
199 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
200 if (err)
201 goto done;
203 /* Create an empty file index. */
204 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
205 if (err)
206 goto done;
208 /* Write the HEAD reference. */
209 err = write_head_ref(path_got, head_ref);
210 if (err)
211 goto done;
213 /* Record our base commit. */
214 err = got_object_id_str(&basestr, commit_id);
215 if (err)
216 goto done;
217 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
218 if (err)
219 goto done;
221 /* Store path to repository. */
222 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 got_repo_get_path(repo));
224 if (err)
225 goto done;
227 /* Store in-repository path prefix. */
228 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 absprefix ? absprefix : prefix);
230 if (err)
231 goto done;
233 /* Generate UUID. */
234 uuid_create(&uuid, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_create");
237 goto done;
239 uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 if (uuid_status != uuid_s_ok) {
241 err = got_error_uuid(uuid_status, "uuid_to_string");
242 goto done;
244 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
245 if (err)
246 goto done;
248 /* Stamp work tree with format file. */
249 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 err = got_error_from_errno("asprintf");
251 goto done;
253 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
254 if (err)
255 goto done;
257 done:
258 free(commit_id);
259 free(path_got);
260 free(formatstr);
261 free(absprefix);
262 free(basestr);
263 free(uuidstr);
264 return err;
267 const struct got_error *
268 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 const char *path_prefix)
271 char *absprefix = NULL;
273 if (!got_path_is_absolute(path_prefix)) {
274 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 return got_error_from_errno("asprintf");
277 *match = (strcmp(absprefix ? absprefix : path_prefix,
278 worktree->path_prefix) == 0);
279 free(absprefix);
280 return NULL;
283 const char *
284 got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 return worktree->head_ref_name;
289 const struct got_error *
290 got_worktree_set_head_ref(struct got_worktree *worktree,
291 struct got_reference *head_ref)
293 const struct got_error *err = NULL;
294 char *path_got = NULL, *head_ref_name = NULL;
296 if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 worktree->meta_dir) == -1) {
298 err = got_error_from_errno("asprintf");
299 path_got = NULL;
300 goto done;
303 head_ref_name = strdup(got_ref_get_name(head_ref));
304 if (head_ref_name == NULL) {
305 err = got_error_from_errno("strdup");
306 goto done;
309 err = write_head_ref(path_got, head_ref);
310 if (err)
311 goto done;
313 free(worktree->head_ref_name);
314 worktree->head_ref_name = head_ref_name;
315 done:
316 free(path_got);
317 if (err)
318 free(head_ref_name);
319 return err;
322 struct got_object_id *
323 got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 return worktree->base_commit_id;
328 const struct got_error *
329 got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 struct got_repository *repo, struct got_object_id *commit_id)
332 const struct got_error *err;
333 struct got_object *obj = NULL;
334 char *id_str = NULL;
335 char *path_got = NULL;
337 if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 worktree->meta_dir) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 err = got_object_open(&obj, repo, commit_id);
345 if (err)
346 return err;
348 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 err = got_error(GOT_ERR_OBJ_TYPE);
350 goto done;
353 /* Record our base commit. */
354 err = got_object_id_str(&id_str, commit_id);
355 if (err)
356 goto done;
357 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
358 if (err)
359 goto done;
361 free(worktree->base_commit_id);
362 worktree->base_commit_id = got_object_id_dup(commit_id);
363 if (worktree->base_commit_id == NULL) {
364 err = got_error_from_errno("got_object_id_dup");
365 goto done;
367 done:
368 if (obj)
369 got_object_close(obj);
370 free(id_str);
371 free(path_got);
372 return err;
375 const struct got_gotconfig *
376 got_worktree_get_gotconfig(struct got_worktree *worktree)
378 return worktree->gotconfig;
381 static const struct got_error *
382 lock_worktree(struct got_worktree *worktree, int operation)
384 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 : got_error_from_errno2("flock",
387 got_worktree_get_root_path(worktree)));
388 return NULL;
391 static const struct got_error *
392 add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 const struct got_error *err = NULL;
395 char *abspath;
397 /* We only accept worktree-relative paths here. */
398 if (got_path_is_absolute(path)) {
399 return got_error_fmt(GOT_ERR_BAD_PATH,
400 "%s does not accept absolute paths: %s",
401 __func__, path);
404 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
405 return got_error_from_errno("asprintf");
407 err = got_path_mkdir(abspath);
408 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
409 struct stat sb;
410 err = NULL;
411 if (lstat(abspath, &sb) == -1) {
412 err = got_error_from_errno2("lstat", abspath);
413 } else if (!S_ISDIR(sb.st_mode)) {
414 /* TODO directory is obstructed; do something */
415 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
418 free(abspath);
419 return err;
422 static const struct got_error *
423 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
425 const struct got_error *err = NULL;
426 uint8_t fbuf1[8192];
427 uint8_t fbuf2[8192];
428 size_t flen1 = 0, flen2 = 0;
430 *same = 1;
432 for (;;) {
433 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
434 if (flen1 == 0 && ferror(f1)) {
435 err = got_error_from_errno("fread");
436 break;
438 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
439 if (flen2 == 0 && ferror(f2)) {
440 err = got_error_from_errno("fread");
441 break;
443 if (flen1 == 0) {
444 if (flen2 != 0)
445 *same = 0;
446 break;
447 } else if (flen2 == 0) {
448 if (flen1 != 0)
449 *same = 0;
450 break;
451 } else if (flen1 == flen2) {
452 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
453 *same = 0;
454 break;
456 } else {
457 *same = 0;
458 break;
462 return err;
465 static const struct got_error *
466 check_files_equal(int *same, FILE *f1, FILE *f2)
468 struct stat sb;
469 size_t size1, size2;
471 *same = 1;
473 if (fstat(fileno(f1), &sb) != 0)
474 return got_error_from_errno("fstat");
475 size1 = sb.st_size;
477 if (fstat(fileno(f2), &sb) != 0)
478 return got_error_from_errno("fstat");
479 size2 = sb.st_size;
481 if (size1 != size2) {
482 *same = 0;
483 return NULL;
486 if (fseek(f1, 0L, SEEK_SET) == -1)
487 return got_ferror(f1, GOT_ERR_IO);
488 if (fseek(f2, 0L, SEEK_SET) == -1)
489 return got_ferror(f2, GOT_ERR_IO);
491 return check_file_contents_equal(same, f1, f2);
494 static const struct got_error *
495 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
497 uint8_t fbuf[65536];
498 size_t flen;
499 ssize_t outlen;
501 *outsize = 0;
503 if (fseek(f, 0L, SEEK_SET) == -1)
504 return got_ferror(f, GOT_ERR_IO);
506 for (;;) {
507 flen = fread(fbuf, 1, sizeof(fbuf), f);
508 if (flen == 0) {
509 if (ferror(f))
510 return got_error_from_errno("fread");
511 if (feof(f))
512 break;
514 outlen = write(outfd, fbuf, flen);
515 if (outlen == -1)
516 return got_error_from_errno("write");
517 if (outlen != flen)
518 return got_error(GOT_ERR_IO);
519 *outsize += outlen;
522 return NULL;
525 static const struct got_error *
526 merge_binary_file(int *overlapcnt, int merged_fd,
527 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
528 const char *label_deriv, const char *label_orig, const char *label_deriv2,
529 const char *ondisk_path)
531 const struct got_error *err = NULL;
532 int same_content, changed_deriv, changed_deriv2;
533 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
534 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
535 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
536 char *base_path_orig = NULL, *base_path_deriv = NULL;
537 char *base_path_deriv2 = NULL;
539 *overlapcnt = 0;
541 err = check_files_equal(&same_content, f_deriv, f_deriv2);
542 if (err)
543 return err;
545 if (same_content)
546 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
548 err = check_files_equal(&same_content, f_deriv, f_orig);
549 if (err)
550 return err;
551 changed_deriv = !same_content;
552 err = check_files_equal(&same_content, f_deriv2, f_orig);
553 if (err)
554 return err;
555 changed_deriv2 = !same_content;
557 if (changed_deriv && changed_deriv2) {
558 *overlapcnt = 1;
559 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
560 err = got_error_from_errno("asprintf");
561 goto done;
563 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
564 err = got_error_from_errno("asprintf");
565 goto done;
567 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
568 err = got_error_from_errno("asprintf");
569 goto done;
571 err = got_opentemp_named_fd(&path_orig, &fd_orig,
572 base_path_orig, "");
573 if (err)
574 goto done;
575 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
576 base_path_deriv, "");
577 if (err)
578 goto done;
579 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
580 base_path_deriv2, "");
581 if (err)
582 goto done;
583 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
590 if (err)
591 goto done;
592 if (dprintf(merged_fd, "Binary files differ and cannot be "
593 "merged automatically:\n") < 0) {
594 err = got_error_from_errno("dprintf");
595 goto done;
597 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
598 GOT_DIFF_CONFLICT_MARKER_BEGIN,
599 label_deriv ? " " : "",
600 label_deriv ? label_deriv : "",
601 path_deriv) < 0) {
602 err = got_error_from_errno("dprintf");
603 goto done;
605 if (size_orig > 0) {
606 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
607 GOT_DIFF_CONFLICT_MARKER_ORIG,
608 label_orig ? " " : "",
609 label_orig ? label_orig : "",
610 path_orig) < 0) {
611 err = got_error_from_errno("dprintf");
612 goto done;
615 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
616 GOT_DIFF_CONFLICT_MARKER_SEP,
617 path_deriv2,
618 GOT_DIFF_CONFLICT_MARKER_END,
619 label_deriv2 ? " " : "",
620 label_deriv2 ? label_deriv2 : "") < 0) {
621 err = got_error_from_errno("dprintf");
622 goto done;
624 } else if (changed_deriv)
625 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
626 else if (changed_deriv2)
627 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
628 done:
629 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
630 err == NULL)
631 err = got_error_from_errno2("unlink", path_orig);
632 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
633 err = got_error_from_errno2("close", path_orig);
634 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
635 err = got_error_from_errno2("close", path_deriv);
636 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
637 err = got_error_from_errno2("close", path_deriv2);
638 free(path_orig);
639 free(path_deriv);
640 free(path_deriv2);
641 free(base_path_orig);
642 free(base_path_deriv);
643 free(base_path_deriv2);
644 return err;
647 /*
648 * Perform a 3-way merge where the file f_orig acts as the common
649 * ancestor, the file f_deriv acts as the first derived version,
650 * and the file f_deriv2 acts as the second derived version.
651 * The merge result will be written to a new file at ondisk_path; any
652 * existing file at this path will be replaced.
653 */
654 static const struct got_error *
655 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
656 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
657 const char *path, uint16_t st_mode,
658 const char *label_orig, const char *label_deriv, const char *label_deriv2,
659 enum got_diff_algorithm diff_algo, struct got_repository *repo,
660 got_worktree_checkout_cb progress_cb, void *progress_arg)
662 const struct got_error *err = NULL;
663 int merged_fd = -1;
664 FILE *f_merged = NULL;
665 char *merged_path = NULL, *base_path = NULL;
666 int overlapcnt = 0;
667 char *parent = NULL;
669 *local_changes_subsumed = 0;
671 err = got_path_dirname(&parent, ondisk_path);
672 if (err)
673 return err;
675 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
676 err = got_error_from_errno("asprintf");
677 goto done;
680 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
681 if (err)
682 goto done;
684 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
685 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
686 if (err) {
687 if (err->code != GOT_ERR_FILE_BINARY)
688 goto done;
689 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
690 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
691 ondisk_path);
692 if (err)
693 goto done;
696 err = (*progress_cb)(progress_arg,
697 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
698 if (err)
699 goto done;
701 if (fsync(merged_fd) != 0) {
702 err = got_error_from_errno("fsync");
703 goto done;
706 f_merged = fdopen(merged_fd, "r");
707 if (f_merged == NULL) {
708 err = got_error_from_errno("fdopen");
709 goto done;
711 merged_fd = -1;
713 /* Check if a clean merge has subsumed all local changes. */
714 if (overlapcnt == 0) {
715 err = check_files_equal(local_changes_subsumed, f_deriv,
716 f_merged);
717 if (err)
718 goto done;
721 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
722 err = got_error_from_errno2("fchmod", merged_path);
723 goto done;
726 if (rename(merged_path, ondisk_path) != 0) {
727 err = got_error_from_errno3("rename", merged_path,
728 ondisk_path);
729 goto done;
731 done:
732 if (err) {
733 if (merged_path)
734 unlink(merged_path);
736 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
737 err = got_error_from_errno("close");
738 if (f_merged && fclose(f_merged) == EOF && err == NULL)
739 err = got_error_from_errno("fclose");
740 free(merged_path);
741 free(base_path);
742 free(parent);
743 return err;
746 static const struct got_error *
747 update_symlink(const char *ondisk_path, const char *target_path,
748 size_t target_len)
750 /* This is not atomic but matches what 'ln -sf' does. */
751 if (unlink(ondisk_path) == -1)
752 return got_error_from_errno2("unlink", ondisk_path);
753 if (symlink(target_path, ondisk_path) == -1)
754 return got_error_from_errno3("symlink", target_path,
755 ondisk_path);
756 return NULL;
759 /*
760 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
761 * in the work tree with a file that contains conflict markers and the
762 * conflicting target paths of the original version, a "derived version"
763 * of a symlink from an incoming change, and a local version of the symlink.
765 * The original versions's target path can be NULL if it is not available,
766 * such as if both derived versions added a new symlink at the same path.
768 * The incoming derived symlink target is NULL in case the incoming change
769 * has deleted this symlink.
770 */
771 static const struct got_error *
772 install_symlink_conflict(const char *deriv_target,
773 struct got_object_id *deriv_base_commit_id, const char *orig_target,
774 const char *label_orig, const char *local_target, const char *ondisk_path)
776 const struct got_error *err;
777 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
778 FILE *f = NULL;
780 err = got_object_id_str(&id_str, deriv_base_commit_id);
781 if (err)
782 return got_error_from_errno("asprintf");
784 if (asprintf(&label_deriv, "%s: commit %s",
785 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
786 err = got_error_from_errno("asprintf");
787 goto done;
790 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
791 if (err)
792 goto done;
794 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
795 err = got_error_from_errno2("fchmod", path);
796 goto done;
799 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
800 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
801 deriv_target ? deriv_target : "(symlink was deleted)",
802 orig_target ? label_orig : "",
803 orig_target ? "\n" : "",
804 orig_target ? orig_target : "",
805 orig_target ? "\n" : "",
806 GOT_DIFF_CONFLICT_MARKER_SEP,
807 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
808 err = got_error_from_errno2("fprintf", path);
809 goto done;
812 if (unlink(ondisk_path) == -1) {
813 err = got_error_from_errno2("unlink", ondisk_path);
814 goto done;
816 if (rename(path, ondisk_path) == -1) {
817 err = got_error_from_errno3("rename", path, ondisk_path);
818 goto done;
820 done:
821 if (f != NULL && fclose(f) == EOF && err == NULL)
822 err = got_error_from_errno2("fclose", path);
823 free(path);
824 free(id_str);
825 free(label_deriv);
826 return err;
829 /* forward declaration */
830 static const struct got_error *
831 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
832 const char *, const char *, uint16_t, const char *,
833 struct got_blob_object *, struct got_object_id *,
834 struct got_repository *, got_worktree_checkout_cb, void *);
836 /*
837 * Merge a symlink into the work tree, where blob_orig acts as the common
838 * ancestor, deriv_target is the link target of the first derived version,
839 * and the symlink on disk acts as the second derived version.
840 * Assume that contents of both blobs represent symlinks.
841 */
842 static const struct got_error *
843 merge_symlink(struct got_worktree *worktree,
844 struct got_blob_object *blob_orig, const char *ondisk_path,
845 const char *path, const char *label_orig, const char *deriv_target,
846 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
847 got_worktree_checkout_cb progress_cb, void *progress_arg)
849 const struct got_error *err = NULL;
850 char *ancestor_target = NULL;
851 struct stat sb;
852 ssize_t ondisk_len, deriv_len;
853 char ondisk_target[PATH_MAX];
854 int have_local_change = 0;
855 int have_incoming_change = 0;
857 if (lstat(ondisk_path, &sb) == -1)
858 return got_error_from_errno2("lstat", ondisk_path);
860 ondisk_len = readlink(ondisk_path, ondisk_target,
861 sizeof(ondisk_target));
862 if (ondisk_len == -1) {
863 err = got_error_from_errno2("readlink",
864 ondisk_path);
865 goto done;
867 ondisk_target[ondisk_len] = '\0';
869 if (blob_orig) {
870 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
871 if (err)
872 goto done;
875 if (ancestor_target == NULL ||
876 (ondisk_len != strlen(ancestor_target) ||
877 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
878 have_local_change = 1;
880 deriv_len = strlen(deriv_target);
881 if (ancestor_target == NULL ||
882 (deriv_len != strlen(ancestor_target) ||
883 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
884 have_incoming_change = 1;
886 if (!have_local_change && !have_incoming_change) {
887 if (ancestor_target) {
888 /* Both sides made the same change. */
889 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
890 path);
891 } else if (deriv_len == ondisk_len &&
892 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
893 /* Both sides added the same symlink. */
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
895 path);
896 } else {
897 /* Both sides added symlinks which don't match. */
898 err = install_symlink_conflict(deriv_target,
899 deriv_base_commit_id, ancestor_target,
900 label_orig, ondisk_target, ondisk_path);
901 if (err)
902 goto done;
903 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
904 path);
906 } else if (!have_local_change && have_incoming_change) {
907 /* Apply the incoming change. */
908 err = update_symlink(ondisk_path, deriv_target,
909 strlen(deriv_target));
910 if (err)
911 goto done;
912 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
913 } else if (have_local_change && have_incoming_change) {
914 if (deriv_len == ondisk_len &&
915 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
916 /* Both sides made the same change. */
917 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
918 path);
919 } else {
920 err = install_symlink_conflict(deriv_target,
921 deriv_base_commit_id, ancestor_target, label_orig,
922 ondisk_target, ondisk_path);
923 if (err)
924 goto done;
925 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
926 path);
930 done:
931 free(ancestor_target);
932 return err;
935 static const struct got_error *
936 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
938 const struct got_error *err = NULL;
939 char target_path[PATH_MAX];
940 ssize_t target_len;
941 size_t n;
942 FILE *f;
944 *outfile = NULL;
946 f = got_opentemp();
947 if (f == NULL)
948 return got_error_from_errno("got_opentemp");
949 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
950 if (target_len == -1) {
951 err = got_error_from_errno2("readlink", ondisk_path);
952 goto done;
954 n = fwrite(target_path, 1, target_len, f);
955 if (n != target_len) {
956 err = got_ferror(f, GOT_ERR_IO);
957 goto done;
959 if (fflush(f) == EOF) {
960 err = got_error_from_errno("fflush");
961 goto done;
963 if (fseek(f, 0L, SEEK_SET) == -1) {
964 err = got_ferror(f, GOT_ERR_IO);
965 goto done;
967 done:
968 if (err)
969 fclose(f);
970 else
971 *outfile = f;
972 return err;
975 /*
976 * Perform a 3-way merge where blob_orig acts as the common ancestor,
977 * blob_deriv acts as the first derived version, and the file on disk
978 * acts as the second derived version.
979 */
980 static const struct got_error *
981 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
982 struct got_blob_object *blob_orig, const char *ondisk_path,
983 const char *path, uint16_t st_mode, const char *label_orig,
984 struct got_blob_object *blob_deriv,
985 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
986 got_worktree_checkout_cb progress_cb, void *progress_arg)
988 const struct got_error *err = NULL;
989 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
990 char *blob_orig_path = NULL;
991 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
992 char *label_deriv = NULL, *parent = NULL;
994 *local_changes_subsumed = 0;
996 err = got_path_dirname(&parent, ondisk_path);
997 if (err)
998 return err;
1000 if (blob_orig) {
1001 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1002 parent) == -1) {
1003 err = got_error_from_errno("asprintf");
1004 base_path = NULL;
1005 goto done;
1008 err = got_opentemp_named(&blob_orig_path, &f_orig,
1009 base_path, "");
1010 if (err)
1011 goto done;
1012 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1013 blob_orig);
1014 if (err)
1015 goto done;
1016 free(base_path);
1017 } else {
1019 * No common ancestor exists. This is an "add vs add" conflict
1020 * and we simply use an empty ancestor file to make both files
1021 * appear in the merged result in their entirety.
1023 f_orig = got_opentemp();
1024 if (f_orig == NULL) {
1025 err = got_error_from_errno("got_opentemp");
1026 goto done;
1030 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1031 err = got_error_from_errno("asprintf");
1032 base_path = NULL;
1033 goto done;
1036 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1037 if (err)
1038 goto done;
1039 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1040 blob_deriv);
1041 if (err)
1042 goto done;
1044 err = got_object_id_str(&id_str, deriv_base_commit_id);
1045 if (err)
1046 goto done;
1047 if (asprintf(&label_deriv, "%s: commit %s",
1048 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1049 err = got_error_from_errno("asprintf");
1050 goto done;
1054 * In order the run a 3-way merge with a symlink we copy the symlink's
1055 * target path into a temporary file and use that file with diff3.
1057 if (S_ISLNK(st_mode)) {
1058 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1059 if (err)
1060 goto done;
1061 } else {
1062 int fd;
1063 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1064 if (fd == -1) {
1065 err = got_error_from_errno2("open", ondisk_path);
1066 goto done;
1068 f_deriv2 = fdopen(fd, "r");
1069 if (f_deriv2 == NULL) {
1070 err = got_error_from_errno2("fdopen", ondisk_path);
1071 close(fd);
1072 goto done;
1076 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1077 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1078 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1079 done:
1080 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1081 err = got_error_from_errno("fclose");
1082 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1083 err = got_error_from_errno("fclose");
1084 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1085 err = got_error_from_errno("fclose");
1086 free(base_path);
1087 if (blob_orig_path) {
1088 unlink(blob_orig_path);
1089 free(blob_orig_path);
1091 if (blob_deriv_path) {
1092 unlink(blob_deriv_path);
1093 free(blob_deriv_path);
1095 free(id_str);
1096 free(label_deriv);
1097 free(parent);
1098 return err;
1101 static const struct got_error *
1102 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1103 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1104 int wt_fd, const char *path, struct got_object_id *blob_id,
1105 int update_timestamps)
1107 const struct got_error *err = NULL;
1108 struct got_fileindex_entry *new_ie;
1110 *new_iep = NULL;
1112 err = got_fileindex_entry_alloc(&new_ie, path);
1113 if (err)
1114 return err;
1116 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1117 blob_id->sha1, base_commit_id->sha1, update_timestamps);
1118 if (err)
1119 goto done;
1121 err = got_fileindex_entry_add(fileindex, new_ie);
1122 done:
1123 if (err)
1124 got_fileindex_entry_free(new_ie);
1125 else
1126 *new_iep = new_ie;
1127 return err;
1130 static mode_t
1131 get_ondisk_perms(int executable, mode_t st_mode)
1133 mode_t xbits = S_IXUSR;
1135 if (executable) {
1136 /* Map read bits to execute bits. */
1137 if (st_mode & S_IRGRP)
1138 xbits |= S_IXGRP;
1139 if (st_mode & S_IROTH)
1140 xbits |= S_IXOTH;
1141 return st_mode | xbits;
1144 return st_mode;
1147 /* forward declaration */
1148 static const struct got_error *
1149 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1150 const char *path, mode_t te_mode, mode_t st_mode,
1151 struct got_blob_object *blob, int restoring_missing_file,
1152 int reverting_versioned_file, int installing_bad_symlink,
1153 int path_is_unversioned, int *update_timestamps,
1154 struct got_repository *repo,
1155 got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 * This function assumes that the provided symlink target points at a
1159 * safe location in the work tree!
1161 static const struct got_error *
1162 replace_existing_symlink(int *did_something, const char *ondisk_path,
1163 const char *target_path, size_t target_len)
1165 const struct got_error *err = NULL;
1166 ssize_t elen;
1167 char etarget[PATH_MAX];
1168 int fd;
1170 *did_something = 0;
1173 * "Bad" symlinks (those pointing outside the work tree or into the
1174 * .got directory) are installed in the work tree as a regular file
1175 * which contains the bad symlink target path.
1176 * The new symlink target has already been checked for safety by our
1177 * caller. If we can successfully open a regular file then we simply
1178 * replace this file with a symlink below.
1180 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1181 if (fd == -1) {
1182 if (!got_err_open_nofollow_on_symlink())
1183 return got_error_from_errno2("open", ondisk_path);
1185 /* We are updating an existing on-disk symlink. */
1186 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1187 if (elen == -1)
1188 return got_error_from_errno2("readlink", ondisk_path);
1190 if (elen == target_len &&
1191 memcmp(etarget, target_path, target_len) == 0)
1192 return NULL; /* nothing to do */
1195 *did_something = 1;
1196 err = update_symlink(ondisk_path, target_path, target_len);
1197 if (fd != -1 && close(fd) == -1 && err == NULL)
1198 err = got_error_from_errno2("close", ondisk_path);
1199 return err;
1202 static const struct got_error *
1203 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1204 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1205 const char *meta_dir)
1207 const struct got_error *err = NULL;
1208 char canonpath[PATH_MAX];
1209 char *path_got = NULL;
1211 *is_bad_symlink = 0;
1213 if (target_len >= sizeof(canonpath)) {
1214 *is_bad_symlink = 1;
1215 return NULL;
1219 * We do not use realpath(3) to resolve the symlink's target
1220 * path because we don't want to resolve symlinks recursively.
1221 * Instead we make the path absolute and then canonicalize it.
1222 * Relative symlink target lookup should begin at the directory
1223 * in which the blob object is being installed.
1225 if (!got_path_is_absolute(target_path)) {
1226 char *abspath, *parent;
1227 err = got_path_dirname(&parent, ondisk_path);
1228 if (err)
1229 return err;
1230 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 free(parent);
1232 return got_error_from_errno("asprintf");
1234 free(parent);
1235 if (strlen(abspath) >= sizeof(canonpath)) {
1236 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1237 free(abspath);
1238 return err;
1240 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1241 free(abspath);
1242 if (err)
1243 return err;
1244 } else {
1245 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1246 if (err)
1247 return err;
1250 /* Only allow symlinks pointing at paths within the work tree. */
1251 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1252 *is_bad_symlink = 1;
1253 return NULL;
1256 /* Do not allow symlinks pointing into the meta directory. */
1257 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1258 return got_error_from_errno("asprintf");
1259 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1260 *is_bad_symlink = 1;
1262 free(path_got);
1263 return NULL;
1266 static const struct got_error *
1267 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1268 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1269 int restoring_missing_file, int reverting_versioned_file,
1270 int path_is_unversioned, int allow_bad_symlinks,
1271 struct got_repository *repo,
1272 got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 const struct got_error *err = NULL;
1275 char target_path[PATH_MAX];
1276 size_t len, target_len = 0;
1277 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1278 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1280 *is_bad_symlink = 0;
1283 * Blob object content specifies the target path of the link.
1284 * If a symbolic link cannot be installed we instead create
1285 * a regular file which contains the link target path stored
1286 * in the blob object.
1288 do {
1289 err = got_object_blob_read_block(&len, blob);
1290 if (err)
1291 return err;
1293 if (len + target_len >= sizeof(target_path)) {
1294 /* Path too long; install as a regular file. */
1295 *is_bad_symlink = 1;
1296 got_object_blob_rewind(blob);
1297 return install_blob(worktree, ondisk_path, path,
1298 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 restoring_missing_file, reverting_versioned_file,
1300 1, path_is_unversioned, NULL, repo, progress_cb,
1301 progress_arg);
1303 if (len > 0) {
1304 /* Skip blob object header first time around. */
1305 memcpy(target_path + target_len, buf + hdrlen,
1306 len - hdrlen);
1307 target_len += len - hdrlen;
1308 hdrlen = 0;
1310 } while (len != 0);
1311 target_path[target_len] = '\0';
1313 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1314 ondisk_path, worktree->root_path, worktree->meta_dir);
1315 if (err)
1316 return err;
1318 if (*is_bad_symlink && !allow_bad_symlinks) {
1319 /* install as a regular file */
1320 got_object_blob_rewind(blob);
1321 err = install_blob(worktree, ondisk_path, path,
1322 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1323 restoring_missing_file, reverting_versioned_file, 1,
1324 path_is_unversioned, NULL, repo,
1325 progress_cb, progress_arg);
1326 return err;
1329 if (symlink(target_path, ondisk_path) == -1) {
1330 if (errno == EEXIST) {
1331 int symlink_replaced;
1332 if (path_is_unversioned) {
1333 err = (*progress_cb)(progress_arg,
1334 GOT_STATUS_UNVERSIONED, path);
1335 return err;
1337 err = replace_existing_symlink(&symlink_replaced,
1338 ondisk_path, target_path, target_len);
1339 if (err)
1340 return err;
1341 if (progress_cb) {
1342 if (symlink_replaced) {
1343 err = (*progress_cb)(progress_arg,
1344 reverting_versioned_file ?
1345 GOT_STATUS_REVERT :
1346 GOT_STATUS_UPDATE, path);
1347 } else {
1348 err = (*progress_cb)(progress_arg,
1349 GOT_STATUS_EXISTS, path);
1352 return err; /* Nothing else to do. */
1355 if (errno == ENOENT) {
1356 char *parent;
1357 err = got_path_dirname(&parent, ondisk_path);
1358 if (err)
1359 return err;
1360 err = got_path_mkdir(parent);
1361 free(parent);
1362 if (err)
1363 return err;
1365 * Retry, and fall through to error handling
1366 * below if this second attempt fails.
1368 if (symlink(target_path, ondisk_path) != -1) {
1369 err = NULL; /* success */
1370 if (progress_cb) {
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1374 path);
1376 return err;
1380 /* Handle errors from first or second creation attempt. */
1381 if (errno == ENAMETOOLONG) {
1382 /* bad target path; install as a regular file */
1383 *is_bad_symlink = 1;
1384 got_object_blob_rewind(blob);
1385 err = install_blob(worktree, ondisk_path, path,
1386 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 restoring_missing_file, reverting_versioned_file, 1,
1388 path_is_unversioned, NULL, repo,
1389 progress_cb, progress_arg);
1390 } else if (errno == ENOTDIR) {
1391 err = got_error_path(ondisk_path,
1392 GOT_ERR_FILE_OBSTRUCTED);
1393 } else {
1394 err = got_error_from_errno3("symlink",
1395 target_path, ondisk_path);
1397 } else if (progress_cb)
1398 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 return err;
1403 static const struct got_error *
1404 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 const char *path, mode_t te_mode, mode_t st_mode,
1406 struct got_blob_object *blob, int restoring_missing_file,
1407 int reverting_versioned_file, int installing_bad_symlink,
1408 int path_is_unversioned, int *update_timestamps,
1409 struct got_repository *repo,
1410 got_worktree_checkout_cb progress_cb, void *progress_arg)
1412 const struct got_error *err = NULL;
1413 int fd = -1;
1414 size_t len, hdrlen;
1415 int update = 0;
1416 char *tmppath = NULL;
1417 mode_t mode;
1419 if (update_timestamps)
1420 *update_timestamps = 1;
1422 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1423 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1424 O_CLOEXEC, mode);
1425 if (fd == -1) {
1426 if (errno == ENOENT || errno == ENOTDIR) {
1427 char *parent;
1428 err = got_path_dirname(&parent, path);
1429 if (err)
1430 return err;
1431 err = add_dir_on_disk(worktree, parent);
1432 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1433 err = got_error_path(path, err->code);
1434 free(parent);
1435 if (err)
1436 return err;
1437 fd = open(ondisk_path,
1438 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1439 mode);
1440 if (fd == -1)
1441 return got_error_from_errno2("open",
1442 ondisk_path);
1443 } else if (errno == EEXIST) {
1444 if (path_is_unversioned) {
1445 if (update_timestamps)
1446 *update_timestamps = 0;
1447 err = (*progress_cb)(progress_arg,
1448 GOT_STATUS_EXISTS, path);
1449 goto done;
1451 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1452 !S_ISREG(st_mode) && !installing_bad_symlink) {
1453 /* TODO file is obstructed; do something */
1454 err = got_error_path(ondisk_path,
1455 GOT_ERR_FILE_OBSTRUCTED);
1456 goto done;
1457 } else {
1458 err = got_opentemp_named_fd(&tmppath, &fd,
1459 ondisk_path, "");
1460 if (err)
1461 goto done;
1462 update = 1;
1464 if (fchmod(fd, apply_umask(mode)) == -1) {
1465 err = got_error_from_errno2("fchmod",
1466 tmppath);
1467 goto done;
1470 } else
1471 return got_error_from_errno2("open", ondisk_path);
1474 if (progress_cb) {
1475 if (restoring_missing_file)
1476 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1477 path);
1478 else if (reverting_versioned_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1480 path);
1481 else
1482 err = (*progress_cb)(progress_arg,
1483 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1484 if (err)
1485 goto done;
1488 hdrlen = got_object_blob_get_hdrlen(blob);
1489 do {
1490 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 err = got_object_blob_read_block(&len, blob);
1492 if (err)
1493 break;
1494 if (len > 0) {
1495 /* Skip blob object header first time around. */
1496 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 if (outlen == -1) {
1498 err = got_error_from_errno("write");
1499 goto done;
1500 } else if (outlen != len - hdrlen) {
1501 err = got_error(GOT_ERR_IO);
1502 goto done;
1504 hdrlen = 0;
1506 } while (len != 0);
1508 if (fsync(fd) != 0) {
1509 err = got_error_from_errno("fsync");
1510 goto done;
1513 if (update) {
1514 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1515 err = got_error_from_errno2("unlink", ondisk_path);
1516 goto done;
1518 if (rename(tmppath, ondisk_path) != 0) {
1519 err = got_error_from_errno3("rename", tmppath,
1520 ondisk_path);
1521 goto done;
1523 free(tmppath);
1524 tmppath = NULL;
1527 done:
1528 if (fd != -1 && close(fd) == -1 && err == NULL)
1529 err = got_error_from_errno("close");
1530 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1531 err = got_error_from_errno2("unlink", tmppath);
1532 free(tmppath);
1533 return err;
1537 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1538 * conflict marker is found in newly added lines only.
1540 static const struct got_error *
1541 get_modified_file_content_status(unsigned char *status,
1542 struct got_blob_object *blob, const char *path, struct stat *sb,
1543 FILE *ondisk_file)
1545 const struct got_error *err, *free_err;
1546 const char *markers[3] = {
1547 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1548 GOT_DIFF_CONFLICT_MARKER_SEP,
1549 GOT_DIFF_CONFLICT_MARKER_END
1551 FILE *f1 = NULL;
1552 struct got_diffreg_result *diffreg_result = NULL;
1553 struct diff_result *r;
1554 int nchunks_parsed, n, i = 0, ln = 0;
1555 char *line = NULL;
1556 size_t linesize = 0;
1557 ssize_t linelen;
1559 if (*status != GOT_STATUS_MODIFY)
1560 return NULL;
1562 f1 = got_opentemp();
1563 if (f1 == NULL)
1564 return got_error_from_errno("got_opentemp");
1566 if (blob) {
1567 got_object_blob_rewind(blob);
1568 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1569 if (err)
1570 goto done;
1573 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1574 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1575 if (err)
1576 goto done;
1578 r = diffreg_result->result;
1580 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1581 struct diff_chunk *c;
1582 struct diff_chunk_context cc = {};
1583 off_t pos;
1586 * We can optimise a little by advancing straight
1587 * to the next chunk if this one has no added lines.
1589 c = diff_chunk_get(r, n);
1591 if (diff_chunk_type(c) != CHUNK_PLUS) {
1592 nchunks_parsed = 1;
1593 continue; /* removed or unchanged lines */
1596 pos = diff_chunk_get_right_start_pos(c);
1597 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1598 err = got_ferror(ondisk_file, GOT_ERR_IO);
1599 goto done;
1602 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1603 ln = cc.right.start;
1605 while (ln < cc.right.end) {
1606 linelen = getline(&line, &linesize, ondisk_file);
1607 if (linelen == -1) {
1608 if (feof(ondisk_file))
1609 break;
1610 err = got_ferror(ondisk_file, GOT_ERR_IO);
1611 break;
1614 if (line && strncmp(line, markers[i],
1615 strlen(markers[i])) == 0) {
1616 if (strcmp(markers[i],
1617 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1618 *status = GOT_STATUS_CONFLICT;
1619 goto done;
1620 } else
1621 i++;
1623 ++ln;
1627 done:
1628 free(line);
1629 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1630 err = got_error_from_errno("fclose");
1631 free_err = got_diffreg_result_free(diffreg_result);
1632 if (err == NULL)
1633 err = free_err;
1635 return err;
1638 static int
1639 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1641 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1642 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1645 static int
1646 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1648 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1649 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1650 ie->mtime_sec == sb->st_mtim.tv_sec &&
1651 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1652 ie->size == (sb->st_size & 0xffffffff) &&
1653 !xbit_differs(ie, sb->st_mode));
1656 static unsigned char
1657 get_staged_status(struct got_fileindex_entry *ie)
1659 switch (got_fileindex_entry_stage_get(ie)) {
1660 case GOT_FILEIDX_STAGE_ADD:
1661 return GOT_STATUS_ADD;
1662 case GOT_FILEIDX_STAGE_DELETE:
1663 return GOT_STATUS_DELETE;
1664 case GOT_FILEIDX_STAGE_MODIFY:
1665 return GOT_STATUS_MODIFY;
1666 default:
1667 return GOT_STATUS_NO_CHANGE;
1671 static const struct got_error *
1672 get_symlink_modification_status(unsigned char *status,
1673 struct got_fileindex_entry *ie, const char *abspath,
1674 int dirfd, const char *de_name, struct got_blob_object *blob)
1676 const struct got_error *err = NULL;
1677 char target_path[PATH_MAX];
1678 char etarget[PATH_MAX];
1679 ssize_t elen;
1680 size_t len, target_len = 0;
1681 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1682 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1684 *status = GOT_STATUS_NO_CHANGE;
1686 /* Blob object content specifies the target path of the link. */
1687 do {
1688 err = got_object_blob_read_block(&len, blob);
1689 if (err)
1690 return err;
1691 if (len + target_len >= sizeof(target_path)) {
1693 * Should not happen. The blob contents were OK
1694 * when this symlink was installed.
1696 return got_error(GOT_ERR_NO_SPACE);
1698 if (len > 0) {
1699 /* Skip blob object header first time around. */
1700 memcpy(target_path + target_len, buf + hdrlen,
1701 len - hdrlen);
1702 target_len += len - hdrlen;
1703 hdrlen = 0;
1705 } while (len != 0);
1706 target_path[target_len] = '\0';
1708 if (dirfd != -1) {
1709 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1710 if (elen == -1)
1711 return got_error_from_errno2("readlinkat", abspath);
1712 } else {
1713 elen = readlink(abspath, etarget, sizeof(etarget));
1714 if (elen == -1)
1715 return got_error_from_errno2("readlink", abspath);
1718 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1719 *status = GOT_STATUS_MODIFY;
1721 return NULL;
1724 static const struct got_error *
1725 get_file_status(unsigned char *status, struct stat *sb,
1726 struct got_fileindex_entry *ie, const char *abspath,
1727 int dirfd, const char *de_name, struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 struct got_object_id id;
1731 size_t hdrlen;
1732 int fd = -1, fd1 = -1;
1733 FILE *f = NULL;
1734 uint8_t fbuf[8192];
1735 struct got_blob_object *blob = NULL;
1736 size_t flen, blen;
1737 unsigned char staged_status;
1739 staged_status = get_staged_status(ie);
1740 *status = GOT_STATUS_NO_CHANGE;
1741 memset(sb, 0, sizeof(*sb));
1744 * Whenever the caller provides a directory descriptor and a
1745 * directory entry name for the file, use them! This prevents
1746 * race conditions if filesystem paths change beneath our feet.
1748 if (dirfd != -1) {
1749 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1750 if (errno == ENOENT) {
1751 if (got_fileindex_entry_has_file_on_disk(ie))
1752 *status = GOT_STATUS_MISSING;
1753 else
1754 *status = GOT_STATUS_DELETE;
1755 goto done;
1757 err = got_error_from_errno2("fstatat", abspath);
1758 goto done;
1760 } else {
1761 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1762 if (fd == -1 && errno != ENOENT &&
1763 !got_err_open_nofollow_on_symlink())
1764 return got_error_from_errno2("open", abspath);
1765 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1766 if (lstat(abspath, sb) == -1)
1767 return got_error_from_errno2("lstat", abspath);
1768 } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 if (errno == ENOENT) {
1770 if (got_fileindex_entry_has_file_on_disk(ie))
1771 *status = GOT_STATUS_MISSING;
1772 else
1773 *status = GOT_STATUS_DELETE;
1774 goto done;
1776 err = got_error_from_errno2("fstat", abspath);
1777 goto done;
1781 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 *status = GOT_STATUS_OBSTRUCTED;
1783 goto done;
1786 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 *status = GOT_STATUS_DELETE;
1788 goto done;
1789 } else if (!got_fileindex_entry_has_blob(ie) &&
1790 staged_status != GOT_STATUS_ADD) {
1791 *status = GOT_STATUS_ADD;
1792 goto done;
1795 if (!stat_info_differs(ie, sb))
1796 goto done;
1798 if (S_ISLNK(sb->st_mode) &&
1799 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 *status = GOT_STATUS_MODIFY;
1801 goto done;
1804 if (staged_status == GOT_STATUS_MODIFY ||
1805 staged_status == GOT_STATUS_ADD)
1806 got_fileindex_entry_get_staged_blob_id(&id, ie);
1807 else
1808 got_fileindex_entry_get_blob_id(&id, ie);
1810 fd1 = got_opentempfd();
1811 if (fd1 == -1) {
1812 err = got_error_from_errno("got_opentempfd");
1813 goto done;
1815 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1816 if (err)
1817 goto done;
1819 if (S_ISLNK(sb->st_mode)) {
1820 err = get_symlink_modification_status(status, ie,
1821 abspath, dirfd, de_name, blob);
1822 goto done;
1825 if (dirfd != -1) {
1826 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1827 if (fd == -1) {
1828 err = got_error_from_errno2("openat", abspath);
1829 goto done;
1833 f = fdopen(fd, "r");
1834 if (f == NULL) {
1835 err = got_error_from_errno2("fdopen", abspath);
1836 goto done;
1838 fd = -1;
1839 hdrlen = got_object_blob_get_hdrlen(blob);
1840 for (;;) {
1841 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1842 err = got_object_blob_read_block(&blen, blob);
1843 if (err)
1844 goto done;
1845 /* Skip length of blob object header first time around. */
1846 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1847 if (flen == 0 && ferror(f)) {
1848 err = got_error_from_errno("fread");
1849 goto done;
1851 if (blen - hdrlen == 0) {
1852 if (flen != 0)
1853 *status = GOT_STATUS_MODIFY;
1854 break;
1855 } else if (flen == 0) {
1856 if (blen - hdrlen != 0)
1857 *status = GOT_STATUS_MODIFY;
1858 break;
1859 } else if (blen - hdrlen == flen) {
1860 /* Skip blob object header first time around. */
1861 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1862 *status = GOT_STATUS_MODIFY;
1863 break;
1865 } else {
1866 *status = GOT_STATUS_MODIFY;
1867 break;
1869 hdrlen = 0;
1872 if (*status == GOT_STATUS_MODIFY) {
1873 rewind(f);
1874 err = get_modified_file_content_status(status, blob, ie->path,
1875 sb, f);
1876 } else if (xbit_differs(ie, sb->st_mode))
1877 *status = GOT_STATUS_MODE_CHANGE;
1878 done:
1879 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1880 err = got_error_from_errno("close");
1881 if (blob)
1882 got_object_blob_close(blob);
1883 if (f != NULL && fclose(f) == EOF && err == NULL)
1884 err = got_error_from_errno2("fclose", abspath);
1885 if (fd != -1 && close(fd) == -1 && err == NULL)
1886 err = got_error_from_errno2("close", abspath);
1887 return err;
1891 * Update timestamps in the file index if a file is unmodified and
1892 * we had to run a full content comparison to find out.
1894 static const struct got_error *
1895 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1896 struct got_fileindex_entry *ie, struct stat *sb)
1898 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1899 return got_fileindex_entry_update(ie, wt_fd, path,
1900 ie->blob_sha1, ie->commit_sha1, 1);
1902 return NULL;
1905 static const struct got_error *remove_ondisk_file(const char *, const char *);
1907 static const struct got_error *
1908 update_blob(struct got_worktree *worktree,
1909 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 struct got_tree_entry *te, const char *path,
1911 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1912 void *progress_arg)
1914 const struct got_error *err = NULL;
1915 struct got_blob_object *blob = NULL;
1916 char *ondisk_path = NULL;
1917 unsigned char status = GOT_STATUS_NO_CHANGE;
1918 struct stat sb;
1919 int fd1 = -1, fd2 = -1;
1920 int update_timestamps = 0;
1922 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1923 return got_error_from_errno("asprintf");
1925 if (ie) {
1926 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1927 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1928 goto done;
1930 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1931 repo);
1932 if (err)
1933 goto done;
1934 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1935 sb.st_mode = got_fileindex_perms_to_st(ie);
1936 } else {
1937 if (stat(ondisk_path, &sb) == -1) {
1938 if (errno != ENOENT && errno != ENOTDIR) {
1939 err = got_error_from_errno2("stat",
1940 ondisk_path);
1941 goto done;
1943 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 status = GOT_STATUS_UNVERSIONED;
1945 } else {
1946 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1947 status = GOT_STATUS_UNVERSIONED;
1948 else
1949 status = GOT_STATUS_OBSTRUCTED;
1953 if (status == GOT_STATUS_OBSTRUCTED) {
1954 if (ie)
1955 got_fileindex_entry_mark_skipped(ie);
1956 err = (*progress_cb)(progress_arg, status, path);
1957 goto done;
1959 if (status == GOT_STATUS_CONFLICT) {
1960 if (ie)
1961 got_fileindex_entry_mark_skipped(ie);
1962 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1963 path);
1964 goto done;
1967 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1968 if (status == GOT_STATUS_UNVERSIONED) {
1969 err = (*progress_cb)(progress_arg, status, path);
1970 } else if (status != GOT_STATUS_NO_CHANGE &&
1971 status != GOT_STATUS_DELETE &&
1972 status != GOT_STATUS_NONEXISTENT &&
1973 status != GOT_STATUS_MISSING) {
1974 err = (*progress_cb)(progress_arg,
1975 GOT_STATUS_CANNOT_DELETE, path);
1976 } else if (ie) {
1977 if (status != GOT_STATUS_DELETE &&
1978 status != GOT_STATUS_NONEXISTENT &&
1979 status != GOT_STATUS_MISSING) {
1980 err = remove_ondisk_file(worktree->root_path,
1981 ie->path);
1982 if (err && !(err->code == GOT_ERR_ERRNO &&
1983 errno == ENOENT))
1984 goto done;
1986 got_fileindex_entry_remove(fileindex, ie);
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1988 ie->path);
1990 goto done; /* nothing else to do */
1993 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1994 (S_ISLNK(te->mode) ||
1995 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1997 * This is a regular file or an installed bad symlink.
1998 * If the file index indicates that this file is already
1999 * up-to-date with respect to the repository we can skip
2000 * updating contents of this file.
2002 if (got_fileindex_entry_has_commit(ie) &&
2003 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2004 SHA1_DIGEST_LENGTH) == 0) {
2005 /* Same commit. */
2006 err = sync_timestamps(worktree->root_fd,
2007 path, status, ie, &sb);
2008 if (err)
2009 goto done;
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2011 path);
2012 goto done;
2014 if (got_fileindex_entry_has_blob(ie) &&
2015 memcmp(ie->blob_sha1, te->id.sha1,
2016 SHA1_DIGEST_LENGTH) == 0) {
2017 /* Different commit but the same blob. */
2018 if (got_fileindex_entry_has_commit(ie)) {
2019 /* Update the base commit ID of this file. */
2020 memcpy(ie->commit_sha1,
2021 worktree->base_commit_id->sha1,
2022 sizeof(ie->commit_sha1));
2024 err = sync_timestamps(worktree->root_fd,
2025 path, status, ie, &sb);
2026 if (err)
2027 goto done;
2028 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2029 path);
2030 goto done;
2034 fd1 = got_opentempfd();
2035 if (fd1 == -1) {
2036 err = got_error_from_errno("got_opentempfd");
2037 goto done;
2039 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2040 if (err)
2041 goto done;
2043 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2044 struct got_blob_object *blob2 = NULL;
2045 char *label_orig = NULL;
2046 if (got_fileindex_entry_has_blob(ie)) {
2047 fd2 = got_opentempfd();
2048 if (fd2 == -1) {
2049 err = got_error_from_errno("got_opentempfd");
2050 goto done;
2052 struct got_object_id id2;
2053 got_fileindex_entry_get_blob_id(&id2, ie);
2054 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2055 fd2);
2056 if (err)
2057 goto done;
2059 if (got_fileindex_entry_has_commit(ie)) {
2060 char id_str[SHA1_DIGEST_STRING_LENGTH];
2061 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2062 sizeof(id_str)) == NULL) {
2063 err = got_error_path(id_str,
2064 GOT_ERR_BAD_OBJ_ID_STR);
2065 goto done;
2067 if (asprintf(&label_orig, "%s: commit %s",
2068 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2069 err = got_error_from_errno("asprintf");
2070 goto done;
2073 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2074 char *link_target;
2075 err = got_object_blob_read_to_str(&link_target, blob);
2076 if (err)
2077 goto done;
2078 err = merge_symlink(worktree, blob2, ondisk_path, path,
2079 label_orig, link_target, worktree->base_commit_id,
2080 repo, progress_cb, progress_arg);
2081 free(link_target);
2082 } else {
2083 err = merge_blob(&update_timestamps, worktree, blob2,
2084 ondisk_path, path, sb.st_mode, label_orig, blob,
2085 worktree->base_commit_id, repo,
2086 progress_cb, progress_arg);
2088 free(label_orig);
2089 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2090 err = got_error_from_errno("close");
2091 goto done;
2093 if (blob2)
2094 got_object_blob_close(blob2);
2095 if (err)
2096 goto done;
2098 * Do not update timestamps of files with local changes.
2099 * Otherwise, a future status walk would treat them as
2100 * unmodified files again.
2102 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2103 blob->id.sha1, worktree->base_commit_id->sha1,
2104 update_timestamps);
2105 } else if (status == GOT_STATUS_MODE_CHANGE) {
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 } else if (status == GOT_STATUS_DELETE) {
2109 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2110 if (err)
2111 goto done;
2112 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2113 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2114 if (err)
2115 goto done;
2116 } else {
2117 int is_bad_symlink = 0;
2118 if (S_ISLNK(te->mode)) {
2119 err = install_symlink(&is_bad_symlink, worktree,
2120 ondisk_path, path, blob,
2121 status == GOT_STATUS_MISSING, 0,
2122 status == GOT_STATUS_UNVERSIONED, 0,
2123 repo, progress_cb, progress_arg);
2124 } else {
2125 err = install_blob(worktree, ondisk_path, path,
2126 te->mode, sb.st_mode, blob,
2127 status == GOT_STATUS_MISSING, 0, 0,
2128 status == GOT_STATUS_UNVERSIONED,
2129 &update_timestamps,
2130 repo, progress_cb, progress_arg);
2132 if (err)
2133 goto done;
2135 if (ie) {
2136 err = got_fileindex_entry_update(ie,
2137 worktree->root_fd, path, blob->id.sha1,
2138 worktree->base_commit_id->sha1, 1);
2139 } else {
2140 err = create_fileindex_entry(&ie, fileindex,
2141 worktree->base_commit_id, worktree->root_fd, path,
2142 &blob->id, update_timestamps);
2144 if (err)
2145 goto done;
2147 if (is_bad_symlink) {
2148 got_fileindex_entry_filetype_set(ie,
2149 GOT_FILEIDX_MODE_BAD_SYMLINK);
2153 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2154 err = got_error_from_errno("close");
2155 goto done;
2157 got_object_blob_close(blob);
2158 done:
2159 free(ondisk_path);
2160 return err;
2163 static const struct got_error *
2164 remove_ondisk_file(const char *root_path, const char *path)
2166 const struct got_error *err = NULL;
2167 char *ondisk_path = NULL, *parent = NULL;
2169 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2170 return got_error_from_errno("asprintf");
2172 if (unlink(ondisk_path) == -1) {
2173 if (errno != ENOENT)
2174 err = got_error_from_errno2("unlink", ondisk_path);
2175 } else {
2176 size_t root_len = strlen(root_path);
2177 err = got_path_dirname(&parent, ondisk_path);
2178 if (err)
2179 goto done;
2180 while (got_path_cmp(parent, root_path,
2181 strlen(parent), root_len) != 0) {
2182 free(ondisk_path);
2183 ondisk_path = parent;
2184 parent = NULL;
2185 if (rmdir(ondisk_path) == -1) {
2186 if (errno != ENOTEMPTY)
2187 err = got_error_from_errno2("rmdir",
2188 ondisk_path);
2189 break;
2191 err = got_path_dirname(&parent, ondisk_path);
2192 if (err)
2193 break;
2196 done:
2197 free(ondisk_path);
2198 free(parent);
2199 return err;
2202 static const struct got_error *
2203 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2204 struct got_fileindex_entry *ie, struct got_repository *repo,
2205 got_worktree_checkout_cb progress_cb, void *progress_arg)
2207 const struct got_error *err = NULL;
2208 unsigned char status;
2209 struct stat sb;
2210 char *ondisk_path;
2212 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2213 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2215 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2216 == -1)
2217 return got_error_from_errno("asprintf");
2219 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2220 if (err)
2221 goto done;
2223 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2224 char ondisk_target[PATH_MAX];
2225 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2226 sizeof(ondisk_target));
2227 if (ondisk_len == -1) {
2228 err = got_error_from_errno2("readlink", ondisk_path);
2229 goto done;
2231 ondisk_target[ondisk_len] = '\0';
2232 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2233 NULL, NULL, /* XXX pass common ancestor info? */
2234 ondisk_target, ondisk_path);
2235 if (err)
2236 goto done;
2237 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2238 ie->path);
2239 goto done;
2242 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2243 status == GOT_STATUS_ADD) {
2244 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2245 if (err)
2246 goto done;
2248 * Preserve the working file and change the deleted blob's
2249 * entry into a schedule-add entry.
2251 err = got_fileindex_entry_update(ie, worktree->root_fd,
2252 ie->path, NULL, NULL, 0);
2253 } else {
2254 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2255 if (err)
2256 goto done;
2257 if (status == GOT_STATUS_NO_CHANGE) {
2258 err = remove_ondisk_file(worktree->root_path, ie->path);
2259 if (err)
2260 goto done;
2262 got_fileindex_entry_remove(fileindex, ie);
2264 done:
2265 free(ondisk_path);
2266 return err;
2269 struct diff_cb_arg {
2270 struct got_fileindex *fileindex;
2271 struct got_worktree *worktree;
2272 struct got_repository *repo;
2273 got_worktree_checkout_cb progress_cb;
2274 void *progress_arg;
2275 got_cancel_cb cancel_cb;
2276 void *cancel_arg;
2279 static const struct got_error *
2280 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2281 struct got_tree_entry *te, const char *parent_path)
2283 const struct got_error *err = NULL;
2284 struct diff_cb_arg *a = arg;
2286 if (a->cancel_cb) {
2287 err = a->cancel_cb(a->cancel_arg);
2288 if (err)
2289 return err;
2292 return update_blob(a->worktree, a->fileindex, ie, te,
2293 ie->path, a->repo, a->progress_cb, a->progress_arg);
2296 static const struct got_error *
2297 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2299 const struct got_error *err = NULL;
2300 struct diff_cb_arg *a = arg;
2302 if (a->cancel_cb) {
2303 err = a->cancel_cb(a->cancel_arg);
2304 if (err)
2305 return err;
2308 return delete_blob(a->worktree, a->fileindex, ie,
2309 a->repo, a->progress_cb, a->progress_arg);
2312 static const struct got_error *
2313 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2315 const struct got_error *err = NULL;
2316 struct diff_cb_arg *a = arg;
2317 char *path;
2319 if (a->cancel_cb) {
2320 err = a->cancel_cb(a->cancel_arg);
2321 if (err)
2322 return err;
2325 if (got_object_tree_entry_is_submodule(te))
2326 return NULL;
2328 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2329 return NULL;
2331 if (asprintf(&path, "%s%s%s", parent_path,
2332 parent_path[0] ? "/" : "", te->name)
2333 == -1)
2334 return got_error_from_errno("asprintf");
2336 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2337 a->repo, a->progress_cb, a->progress_arg);
2339 free(path);
2340 return err;
2343 const struct got_error *
2344 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2346 uint32_t uuid_status;
2348 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2349 if (uuid_status != uuid_s_ok) {
2350 *uuidstr = NULL;
2351 return got_error_uuid(uuid_status, "uuid_to_string");
2354 return NULL;
2357 static const struct got_error *
2358 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2360 const struct got_error *err = NULL;
2361 char *uuidstr = NULL;
2363 *refname = NULL;
2365 err = got_worktree_get_uuid(&uuidstr, worktree);
2366 if (err)
2367 return err;
2369 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2370 err = got_error_from_errno("asprintf");
2371 *refname = NULL;
2373 free(uuidstr);
2374 return err;
2377 const struct got_error *
2378 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2379 const char *prefix)
2381 return get_ref_name(refname, worktree, prefix);
2384 static const struct got_error *
2385 get_base_ref_name(char **refname, struct got_worktree *worktree)
2387 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2390 static const struct got_error *
2391 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree,
2394 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2397 static const struct got_error *
2398 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2403 static const struct got_error *
2404 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2410 static const struct got_error *
2411 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2424 static const struct got_error *
2425 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2427 return get_ref_name(refname, worktree,
2428 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2431 static const struct got_error *
2432 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 return get_ref_name(refname, worktree,
2435 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2438 static const struct got_error *
2439 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2441 return get_ref_name(refname, worktree,
2442 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2445 const struct got_error *
2446 got_worktree_get_histedit_script_path(char **path,
2447 struct got_worktree *worktree)
2449 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2450 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2451 *path = NULL;
2452 return got_error_from_errno("asprintf");
2454 return NULL;
2457 static const struct got_error *
2458 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2460 return get_ref_name(refname, worktree,
2461 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2464 static const struct got_error *
2465 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2467 return get_ref_name(refname, worktree,
2468 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2472 * Prevent Git's garbage collector from deleting our base commit by
2473 * setting a reference to our base commit's ID.
2475 static const struct got_error *
2476 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2478 const struct got_error *err = NULL;
2479 struct got_reference *ref = NULL;
2480 char *refname;
2482 err = get_base_ref_name(&refname, worktree);
2483 if (err)
2484 return err;
2486 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2487 if (err)
2488 goto done;
2490 err = got_ref_write(ref, repo);
2491 done:
2492 free(refname);
2493 if (ref)
2494 got_ref_close(ref);
2495 return err;
2498 static const struct got_error *
2499 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2501 const struct got_error *err = NULL;
2503 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2504 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2505 err = got_error_from_errno("asprintf");
2506 *fileindex_path = NULL;
2508 return err;
2512 static const struct got_error *
2513 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2514 struct got_worktree *worktree)
2516 const struct got_error *err = NULL;
2517 FILE *index = NULL;
2519 *fileindex_path = NULL;
2520 *fileindex = got_fileindex_alloc();
2521 if (*fileindex == NULL)
2522 return got_error_from_errno("got_fileindex_alloc");
2524 err = get_fileindex_path(fileindex_path, worktree);
2525 if (err)
2526 goto done;
2528 index = fopen(*fileindex_path, "rbe");
2529 if (index == NULL) {
2530 if (errno != ENOENT)
2531 err = got_error_from_errno2("fopen", *fileindex_path);
2532 } else {
2533 err = got_fileindex_read(*fileindex, index);
2534 if (fclose(index) == EOF && err == NULL)
2535 err = got_error_from_errno("fclose");
2537 done:
2538 if (err) {
2539 free(*fileindex_path);
2540 *fileindex_path = NULL;
2541 got_fileindex_free(*fileindex);
2542 *fileindex = NULL;
2544 return err;
2547 struct bump_base_commit_id_arg {
2548 struct got_object_id *base_commit_id;
2549 const char *path;
2550 size_t path_len;
2551 const char *entry_name;
2552 got_worktree_checkout_cb progress_cb;
2553 void *progress_arg;
2556 static const struct got_error *
2557 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2559 const struct got_error *err;
2560 struct bump_base_commit_id_arg *a = arg;
2562 if (a->entry_name) {
2563 if (strcmp(ie->path, a->path) != 0)
2564 return NULL;
2565 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2566 return NULL;
2568 if (got_fileindex_entry_was_skipped(ie))
2569 return NULL;
2571 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2572 SHA1_DIGEST_LENGTH) == 0)
2573 return NULL;
2575 if (a->progress_cb) {
2576 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2577 ie->path);
2578 if (err)
2579 return err;
2581 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2582 return NULL;
2585 /* Bump base commit ID of all files within an updated part of the work tree. */
2586 static const struct got_error *
2587 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2588 struct got_fileindex *fileindex,
2589 got_worktree_checkout_cb progress_cb, void *progress_arg)
2591 struct bump_base_commit_id_arg bbc_arg;
2593 bbc_arg.base_commit_id = worktree->base_commit_id;
2594 bbc_arg.entry_name = NULL;
2595 bbc_arg.path = "";
2596 bbc_arg.path_len = 0;
2597 bbc_arg.progress_cb = progress_cb;
2598 bbc_arg.progress_arg = progress_arg;
2600 return got_fileindex_for_each_entry_safe(fileindex,
2601 bump_base_commit_id, &bbc_arg);
2604 static const struct got_error *
2605 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2607 const struct got_error *err = NULL;
2608 char *new_fileindex_path = NULL;
2609 FILE *new_index = NULL;
2610 struct timespec timeout;
2612 err = got_opentemp_named(&new_fileindex_path, &new_index,
2613 fileindex_path, "");
2614 if (err)
2615 goto done;
2617 err = got_fileindex_write(fileindex, new_index);
2618 if (err)
2619 goto done;
2621 if (rename(new_fileindex_path, fileindex_path) != 0) {
2622 err = got_error_from_errno3("rename", new_fileindex_path,
2623 fileindex_path);
2624 unlink(new_fileindex_path);
2628 * Sleep for a short amount of time to ensure that files modified after
2629 * this program exits have a different time stamp from the one which
2630 * was recorded in the file index.
2632 timeout.tv_sec = 0;
2633 timeout.tv_nsec = 1;
2634 nanosleep(&timeout, NULL);
2635 done:
2636 if (new_index)
2637 fclose(new_index);
2638 free(new_fileindex_path);
2639 return err;
2642 static const struct got_error *
2643 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2644 struct got_object_id **tree_id, const char *wt_relpath,
2645 struct got_commit_object *base_commit, struct got_worktree *worktree,
2646 struct got_repository *repo)
2648 const struct got_error *err = NULL;
2649 struct got_object_id *id = NULL;
2650 char *in_repo_path = NULL;
2651 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2653 *entry_type = GOT_OBJ_TYPE_ANY;
2654 *tree_relpath = NULL;
2655 *tree_id = NULL;
2657 if (wt_relpath[0] == '\0') {
2658 /* Check out all files within the work tree. */
2659 *entry_type = GOT_OBJ_TYPE_TREE;
2660 *tree_relpath = strdup("");
2661 if (*tree_relpath == NULL) {
2662 err = got_error_from_errno("strdup");
2663 goto done;
2665 err = got_object_id_by_path(tree_id, repo, base_commit,
2666 worktree->path_prefix);
2667 if (err)
2668 goto done;
2669 return NULL;
2672 /* Check out a subset of files in the work tree. */
2674 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2675 is_root_wt ? "" : "/", wt_relpath) == -1) {
2676 err = got_error_from_errno("asprintf");
2677 goto done;
2680 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2681 if (err)
2682 goto done;
2684 free(in_repo_path);
2685 in_repo_path = NULL;
2687 err = got_object_get_type(entry_type, repo, id);
2688 if (err)
2689 goto done;
2691 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2692 /* Check out a single file. */
2693 if (strchr(wt_relpath, '/') == NULL) {
2694 /* Check out a single file in work tree's root dir. */
2695 in_repo_path = strdup(worktree->path_prefix);
2696 if (in_repo_path == NULL) {
2697 err = got_error_from_errno("strdup");
2698 goto done;
2700 *tree_relpath = strdup("");
2701 if (*tree_relpath == NULL) {
2702 err = got_error_from_errno("strdup");
2703 goto done;
2705 } else {
2706 /* Check out a single file in a subdirectory. */
2707 err = got_path_dirname(tree_relpath, wt_relpath);
2708 if (err)
2709 return err;
2710 if (asprintf(&in_repo_path, "%s%s%s",
2711 worktree->path_prefix, is_root_wt ? "" : "/",
2712 *tree_relpath) == -1) {
2713 err = got_error_from_errno("asprintf");
2714 goto done;
2717 err = got_object_id_by_path(tree_id, repo,
2718 base_commit, in_repo_path);
2719 } else {
2720 /* Check out all files within a subdirectory. */
2721 *tree_id = got_object_id_dup(id);
2722 if (*tree_id == NULL) {
2723 err = got_error_from_errno("got_object_id_dup");
2724 goto done;
2726 *tree_relpath = strdup(wt_relpath);
2727 if (*tree_relpath == NULL) {
2728 err = got_error_from_errno("strdup");
2729 goto done;
2732 done:
2733 free(id);
2734 free(in_repo_path);
2735 if (err) {
2736 *entry_type = GOT_OBJ_TYPE_ANY;
2737 free(*tree_relpath);
2738 *tree_relpath = NULL;
2739 free(*tree_id);
2740 *tree_id = NULL;
2742 return err;
2745 static const struct got_error *
2746 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2747 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2748 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2749 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2751 const struct got_error *err = NULL;
2752 struct got_commit_object *commit = NULL;
2753 struct got_tree_object *tree = NULL;
2754 struct got_fileindex_diff_tree_cb diff_cb;
2755 struct diff_cb_arg arg;
2757 err = ref_base_commit(worktree, repo);
2758 if (err) {
2759 if (!(err->code == GOT_ERR_ERRNO &&
2760 (errno == EACCES || errno == EROFS)))
2761 goto done;
2762 err = (*progress_cb)(progress_arg,
2763 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2764 if (err)
2765 return err;
2768 err = got_object_open_as_commit(&commit, repo,
2769 worktree->base_commit_id);
2770 if (err)
2771 goto done;
2773 err = got_object_open_as_tree(&tree, repo, tree_id);
2774 if (err)
2775 goto done;
2777 if (entry_name &&
2778 got_object_tree_find_entry(tree, entry_name) == NULL) {
2779 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2780 goto done;
2783 diff_cb.diff_old_new = diff_old_new;
2784 diff_cb.diff_old = diff_old;
2785 diff_cb.diff_new = diff_new;
2786 arg.fileindex = fileindex;
2787 arg.worktree = worktree;
2788 arg.repo = repo;
2789 arg.progress_cb = progress_cb;
2790 arg.progress_arg = progress_arg;
2791 arg.cancel_cb = cancel_cb;
2792 arg.cancel_arg = cancel_arg;
2793 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2794 entry_name, repo, &diff_cb, &arg);
2795 done:
2796 if (tree)
2797 got_object_tree_close(tree);
2798 if (commit)
2799 got_object_commit_close(commit);
2800 return err;
2803 const struct got_error *
2804 got_worktree_checkout_files(struct got_worktree *worktree,
2805 struct got_pathlist_head *paths, struct got_repository *repo,
2806 got_worktree_checkout_cb progress_cb, void *progress_arg,
2807 got_cancel_cb cancel_cb, void *cancel_arg)
2809 const struct got_error *err = NULL, *sync_err, *unlockerr;
2810 struct got_commit_object *commit = NULL;
2811 struct got_tree_object *tree = NULL;
2812 struct got_fileindex *fileindex = NULL;
2813 char *fileindex_path = NULL;
2814 struct got_pathlist_entry *pe;
2815 struct tree_path_data {
2816 STAILQ_ENTRY(tree_path_data) entry;
2817 struct got_object_id *tree_id;
2818 int entry_type;
2819 char *relpath;
2820 char *entry_name;
2821 } *tpd = NULL;
2822 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2824 STAILQ_INIT(&tree_paths);
2826 err = lock_worktree(worktree, LOCK_EX);
2827 if (err)
2828 return err;
2830 err = got_object_open_as_commit(&commit, repo,
2831 worktree->base_commit_id);
2832 if (err)
2833 goto done;
2835 /* Map all specified paths to in-repository trees. */
2836 TAILQ_FOREACH(pe, paths, entry) {
2837 tpd = malloc(sizeof(*tpd));
2838 if (tpd == NULL) {
2839 err = got_error_from_errno("malloc");
2840 goto done;
2843 err = find_tree_entry_for_checkout(&tpd->entry_type,
2844 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2845 worktree, repo);
2846 if (err) {
2847 free(tpd);
2848 goto done;
2851 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2852 err = got_path_basename(&tpd->entry_name, pe->path);
2853 if (err) {
2854 free(tpd->relpath);
2855 free(tpd->tree_id);
2856 free(tpd);
2857 goto done;
2859 } else
2860 tpd->entry_name = NULL;
2862 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2866 * Read the file index.
2867 * Checking out files is supposed to be an idempotent operation.
2868 * If the on-disk file index is incomplete we will try to complete it.
2870 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2871 if (err)
2872 goto done;
2874 tpd = STAILQ_FIRST(&tree_paths);
2875 TAILQ_FOREACH(pe, paths, entry) {
2876 struct bump_base_commit_id_arg bbc_arg;
2878 err = checkout_files(worktree, fileindex, tpd->relpath,
2879 tpd->tree_id, tpd->entry_name, repo,
2880 progress_cb, progress_arg, cancel_cb, cancel_arg);
2881 if (err)
2882 break;
2884 bbc_arg.base_commit_id = worktree->base_commit_id;
2885 bbc_arg.entry_name = tpd->entry_name;
2886 bbc_arg.path = pe->path;
2887 bbc_arg.path_len = pe->path_len;
2888 bbc_arg.progress_cb = progress_cb;
2889 bbc_arg.progress_arg = progress_arg;
2890 err = got_fileindex_for_each_entry_safe(fileindex,
2891 bump_base_commit_id, &bbc_arg);
2892 if (err)
2893 break;
2895 tpd = STAILQ_NEXT(tpd, entry);
2897 sync_err = sync_fileindex(fileindex, fileindex_path);
2898 if (sync_err && err == NULL)
2899 err = sync_err;
2900 done:
2901 free(fileindex_path);
2902 if (tree)
2903 got_object_tree_close(tree);
2904 if (commit)
2905 got_object_commit_close(commit);
2906 if (fileindex)
2907 got_fileindex_free(fileindex);
2908 while (!STAILQ_EMPTY(&tree_paths)) {
2909 tpd = STAILQ_FIRST(&tree_paths);
2910 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2911 free(tpd->relpath);
2912 free(tpd->tree_id);
2913 free(tpd);
2915 unlockerr = lock_worktree(worktree, LOCK_SH);
2916 if (unlockerr && err == NULL)
2917 err = unlockerr;
2918 return err;
2921 static const struct got_error *
2922 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2923 struct got_fileindex_entry *ie, const char *ondisk_path,
2924 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2925 int restoring_missing_file, int reverting_versioned_file,
2926 int path_is_unversioned, int allow_bad_symlinks,
2927 struct got_repository *repo,
2928 got_worktree_checkout_cb progress_cb, void *progress_arg)
2930 const struct got_error *err = NULL;
2931 int is_bad_symlink = 0;
2933 if (S_ISLNK(mode2)) {
2934 err = install_symlink(&is_bad_symlink,
2935 worktree, ondisk_path, path2, blob2,
2936 restoring_missing_file,
2937 reverting_versioned_file,
2938 path_is_unversioned, allow_bad_symlinks,
2939 repo, progress_cb, progress_arg);
2940 } else {
2941 err = install_blob(worktree, ondisk_path, path2,
2942 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2943 restoring_missing_file, reverting_versioned_file, 0,
2944 path_is_unversioned, NULL, repo,
2945 progress_cb, progress_arg);
2947 if (err)
2948 return err;
2949 if (ie == NULL) {
2950 /* Adding an unversioned file. */
2951 err = got_fileindex_entry_alloc(&ie, path2);
2952 if (err)
2953 return err;
2954 err = got_fileindex_entry_update(ie,
2955 worktree->root_fd, path2, NULL, NULL, 1);
2956 if (err) {
2957 got_fileindex_entry_free(ie);
2958 return err;
2960 err = got_fileindex_entry_add(fileindex, ie);
2961 if (err) {
2962 got_fileindex_entry_free(ie);
2963 return err;
2965 } else {
2966 /* Re-adding a locally deleted file. */
2967 err = got_fileindex_entry_update(ie,
2968 worktree->root_fd, path2, ie->blob_sha1,
2969 worktree->base_commit_id->sha1, 0);
2970 if (err)
2971 return err;
2974 if (is_bad_symlink) {
2975 got_fileindex_entry_filetype_set(ie,
2976 GOT_FILEIDX_MODE_BAD_SYMLINK);
2979 return NULL;
2982 struct merge_file_cb_arg {
2983 struct got_worktree *worktree;
2984 struct got_fileindex *fileindex;
2985 got_worktree_checkout_cb progress_cb;
2986 void *progress_arg;
2987 got_cancel_cb cancel_cb;
2988 void *cancel_arg;
2989 const char *label_orig;
2990 struct got_object_id *commit_id2;
2991 int allow_bad_symlinks;
2994 static const struct got_error *
2995 merge_file_cb(void *arg, struct got_blob_object *blob1,
2996 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2997 struct got_object_id *id1, struct got_object_id *id2,
2998 const char *path1, const char *path2,
2999 mode_t mode1, mode_t mode2, struct got_repository *repo)
3001 static const struct got_error *err = NULL;
3002 struct merge_file_cb_arg *a = arg;
3003 struct got_fileindex_entry *ie;
3004 char *ondisk_path = NULL;
3005 struct stat sb;
3006 unsigned char status;
3007 int local_changes_subsumed;
3008 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3009 char *id_str = NULL, *label_deriv2 = NULL;
3010 struct got_object_id *id = NULL;
3012 if (blob1 && blob2) {
3013 ie = got_fileindex_entry_get(a->fileindex, path2,
3014 strlen(path2));
3015 if (ie == NULL)
3016 return (*a->progress_cb)(a->progress_arg,
3017 GOT_STATUS_MISSING, path2);
3019 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3020 path2) == -1)
3021 return got_error_from_errno("asprintf");
3023 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3024 repo);
3025 if (err)
3026 goto done;
3028 if (status == GOT_STATUS_DELETE) {
3029 err = (*a->progress_cb)(a->progress_arg,
3030 GOT_STATUS_MERGE, path2);
3031 goto done;
3033 if (status != GOT_STATUS_NO_CHANGE &&
3034 status != GOT_STATUS_MODIFY &&
3035 status != GOT_STATUS_CONFLICT &&
3036 status != GOT_STATUS_ADD) {
3037 err = (*a->progress_cb)(a->progress_arg, status, path2);
3038 goto done;
3041 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3042 char *link_target2;
3043 err = got_object_blob_read_to_str(&link_target2, blob2);
3044 if (err)
3045 goto done;
3046 err = merge_symlink(a->worktree, blob1, ondisk_path,
3047 path2, a->label_orig, link_target2, a->commit_id2,
3048 repo, a->progress_cb, a->progress_arg);
3049 free(link_target2);
3050 } else {
3051 int fd;
3053 f_orig = got_opentemp();
3054 if (f_orig == NULL) {
3055 err = got_error_from_errno("got_opentemp");
3056 goto done;
3058 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3059 f_orig, blob1);
3060 if (err)
3061 goto done;
3063 f_deriv2 = got_opentemp();
3064 if (f_deriv2 == NULL)
3065 goto done;
3066 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3067 f_deriv2, blob2);
3068 if (err)
3069 goto done;
3071 fd = open(ondisk_path,
3072 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3073 if (fd == -1) {
3074 err = got_error_from_errno2("open",
3075 ondisk_path);
3076 goto done;
3078 f_deriv = fdopen(fd, "r");
3079 if (f_deriv == NULL) {
3080 err = got_error_from_errno2("fdopen",
3081 ondisk_path);
3082 close(fd);
3083 goto done;
3085 err = got_object_id_str(&id_str, a->commit_id2);
3086 if (err)
3087 goto done;
3088 if (asprintf(&label_deriv2, "%s: commit %s",
3089 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3090 err = got_error_from_errno("asprintf");
3091 goto done;
3093 err = merge_file(&local_changes_subsumed, a->worktree,
3094 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3095 mode2, a->label_orig, NULL, label_deriv2,
3096 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3097 a->progress_cb, a->progress_arg);
3099 } else if (blob1) {
3100 ie = got_fileindex_entry_get(a->fileindex, path1,
3101 strlen(path1));
3102 if (ie == NULL)
3103 return (*a->progress_cb)(a->progress_arg,
3104 GOT_STATUS_MISSING, path1);
3106 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3107 path1) == -1)
3108 return got_error_from_errno("asprintf");
3110 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3111 repo);
3112 if (err)
3113 goto done;
3115 switch (status) {
3116 case GOT_STATUS_NO_CHANGE:
3117 err = (*a->progress_cb)(a->progress_arg,
3118 GOT_STATUS_DELETE, path1);
3119 if (err)
3120 goto done;
3121 err = remove_ondisk_file(a->worktree->root_path, path1);
3122 if (err)
3123 goto done;
3124 if (ie)
3125 got_fileindex_entry_mark_deleted_from_disk(ie);
3126 break;
3127 case GOT_STATUS_DELETE:
3128 case GOT_STATUS_MISSING:
3129 err = (*a->progress_cb)(a->progress_arg,
3130 GOT_STATUS_DELETE, path1);
3131 if (err)
3132 goto done;
3133 if (ie)
3134 got_fileindex_entry_mark_deleted_from_disk(ie);
3135 break;
3136 case GOT_STATUS_MODIFY: {
3137 FILE *blob1_f;
3138 off_t blob1_size;
3139 int obj_type;
3141 * Delete the file only if its content already
3142 * exists in the repository.
3144 err = got_object_blob_file_create(&id, &blob1_f,
3145 &blob1_size, path1);
3146 if (err)
3147 goto done;
3148 if (fclose(blob1_f) == EOF) {
3149 err = got_error_from_errno("fclose");
3150 goto done;
3153 /* Implied existence check. */
3154 err = got_object_get_type(&obj_type, repo, id);
3155 if (err) {
3156 if (err->code != GOT_ERR_NO_OBJ)
3157 goto done;
3158 err = (*a->progress_cb)(a->progress_arg,
3159 GOT_STATUS_CANNOT_DELETE, path1);
3160 goto done;
3161 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3162 err = (*a->progress_cb)(a->progress_arg,
3163 GOT_STATUS_CANNOT_DELETE, path1);
3164 goto done;
3166 err = (*a->progress_cb)(a->progress_arg,
3167 GOT_STATUS_DELETE, path1);
3168 if (err)
3169 goto done;
3170 err = remove_ondisk_file(a->worktree->root_path,
3171 path1);
3172 if (err)
3173 goto done;
3174 if (ie)
3175 got_fileindex_entry_mark_deleted_from_disk(ie);
3176 break;
3178 case GOT_STATUS_ADD: {
3179 FILE *blob1_f;
3180 off_t blob1_size;
3182 * Delete the file only if its content already
3183 * exists in the repository.
3185 err = got_object_blob_file_create(&id, &blob1_f,
3186 &blob1_size, path1);
3187 if (err)
3188 goto done;
3189 if (fclose(blob1_f) == EOF) {
3190 err = got_error_from_errno("fclose");
3191 goto done;
3193 if (got_object_id_cmp(id, id1) == 0) {
3194 err = (*a->progress_cb)(a->progress_arg,
3195 GOT_STATUS_DELETE, path1);
3196 if (err)
3197 goto done;
3198 err = remove_ondisk_file(a->worktree->root_path,
3199 path1);
3200 if (err)
3201 goto done;
3202 if (ie)
3203 got_fileindex_entry_remove(a->fileindex,
3204 ie);
3205 } else {
3206 err = (*a->progress_cb)(a->progress_arg,
3207 GOT_STATUS_CANNOT_DELETE, path1);
3209 if (err)
3210 goto done;
3211 break;
3213 case GOT_STATUS_CONFLICT:
3214 err = (*a->progress_cb)(a->progress_arg,
3215 GOT_STATUS_CANNOT_DELETE, path1);
3216 if (err)
3217 goto done;
3218 break;
3219 case GOT_STATUS_OBSTRUCTED:
3220 err = (*a->progress_cb)(a->progress_arg, status, path1);
3221 if (err)
3222 goto done;
3223 break;
3224 default:
3225 break;
3227 } else if (blob2) {
3228 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3229 path2) == -1)
3230 return got_error_from_errno("asprintf");
3231 ie = got_fileindex_entry_get(a->fileindex, path2,
3232 strlen(path2));
3233 if (ie) {
3234 err = get_file_status(&status, &sb, ie, ondisk_path,
3235 -1, NULL, repo);
3236 if (err)
3237 goto done;
3238 if (status != GOT_STATUS_NO_CHANGE &&
3239 status != GOT_STATUS_MODIFY &&
3240 status != GOT_STATUS_CONFLICT &&
3241 status != GOT_STATUS_ADD &&
3242 status != GOT_STATUS_DELETE) {
3243 err = (*a->progress_cb)(a->progress_arg,
3244 status, path2);
3245 goto done;
3247 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3248 char *link_target2;
3249 err = got_object_blob_read_to_str(&link_target2,
3250 blob2);
3251 if (err)
3252 goto done;
3253 err = merge_symlink(a->worktree, NULL,
3254 ondisk_path, path2, a->label_orig,
3255 link_target2, a->commit_id2, repo,
3256 a->progress_cb, a->progress_arg);
3257 free(link_target2);
3258 } else if (S_ISREG(sb.st_mode)) {
3259 err = merge_blob(&local_changes_subsumed,
3260 a->worktree, NULL, ondisk_path, path2,
3261 sb.st_mode, a->label_orig, blob2,
3262 a->commit_id2, repo, a->progress_cb,
3263 a->progress_arg);
3264 } else if (status != GOT_STATUS_DELETE) {
3265 err = got_error_path(ondisk_path,
3266 GOT_ERR_FILE_OBSTRUCTED);
3268 if (err)
3269 goto done;
3270 if (status == GOT_STATUS_DELETE) {
3271 /* Re-add file with content from new blob. */
3272 err = add_file(a->worktree, a->fileindex, ie,
3273 ondisk_path, path2, blob2, mode2,
3274 0, 0, 0, a->allow_bad_symlinks,
3275 repo, a->progress_cb, a->progress_arg);
3276 if (err)
3277 goto done;
3279 } else {
3280 err = add_file(a->worktree, a->fileindex, NULL,
3281 ondisk_path, path2, blob2, mode2,
3282 0, 0, 1, a->allow_bad_symlinks,
3283 repo, a->progress_cb, a->progress_arg);
3284 if (err)
3285 goto done;
3288 done:
3289 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3290 err = got_error_from_errno("fclose");
3291 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3292 err = got_error_from_errno("fclose");
3293 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3294 err = got_error_from_errno("fclose");
3295 free(id_str);
3296 free(id);
3297 free(label_deriv2);
3298 free(ondisk_path);
3299 return err;
3302 struct check_mixed_commits_args {
3303 struct got_worktree *worktree;
3304 got_cancel_cb cancel_cb;
3305 void *cancel_arg;
3308 static const struct got_error *
3309 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3311 const struct got_error *err = NULL;
3312 struct check_mixed_commits_args *a = arg;
3314 if (a->cancel_cb) {
3315 err = a->cancel_cb(a->cancel_arg);
3316 if (err)
3317 return err;
3320 /* Reject merges into a work tree with mixed base commits. */
3321 if (got_fileindex_entry_has_commit(ie) &&
3322 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3323 SHA1_DIGEST_LENGTH) != 0)
3324 return got_error(GOT_ERR_MIXED_COMMITS);
3326 return NULL;
3329 const struct got_error *
3330 got_worktree_get_state(char *state, struct got_repository *repo,
3331 struct got_worktree *worktree,
3332 got_cancel_cb cancel_cb, void *cancel_arg)
3334 const struct got_error *err;
3335 struct got_object_id *base_id, *head_id = NULL;
3336 struct got_reference *head_ref;
3337 struct got_fileindex *fileindex = NULL;
3338 char *fileindex_path = NULL;
3339 struct check_mixed_commits_args cma;
3341 if (worktree == NULL)
3342 return got_error(GOT_ERR_NOT_WORKTREE);
3344 err = got_ref_open(&head_ref, repo,
3345 got_worktree_get_head_ref_name(worktree), 0);
3346 if (err)
3347 return err;
3349 err = got_ref_resolve(&head_id, repo, head_ref);
3350 if (err)
3351 goto done;
3353 *state = GOT_WORKTREE_STATE_UNKNOWN;
3354 base_id = got_worktree_get_base_commit_id(worktree);
3356 cma.worktree = worktree;
3357 cma.cancel_cb = cancel_cb;
3358 cma.cancel_arg = cancel_arg;
3360 if (got_object_id_cmp(base_id, head_id) == 0) {
3361 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3362 if (err)
3363 goto done;
3365 err = got_fileindex_for_each_entry_safe(fileindex,
3366 check_mixed_commits, &cma);
3367 if (err == NULL)
3368 *state = GOT_WORKTREE_STATE_UPTODATE;
3369 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3370 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3371 err = NULL;
3373 } else
3374 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3376 done:
3377 free(head_id);
3378 free(fileindex_path);
3379 got_ref_close(head_ref);
3380 if (fileindex != NULL)
3381 got_fileindex_free(fileindex);
3382 return err;
3385 struct check_merge_conflicts_arg {
3386 struct got_worktree *worktree;
3387 struct got_fileindex *fileindex;
3388 struct got_repository *repo;
3391 static const struct got_error *
3392 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3393 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3394 struct got_object_id *id1, struct got_object_id *id2,
3395 const char *path1, const char *path2,
3396 mode_t mode1, mode_t mode2, struct got_repository *repo)
3398 const struct got_error *err = NULL;
3399 struct check_merge_conflicts_arg *a = arg;
3400 unsigned char status;
3401 struct stat sb;
3402 struct got_fileindex_entry *ie;
3403 const char *path = path2 ? path2 : path1;
3404 struct got_object_id *id = id2 ? id2 : id1;
3405 char *ondisk_path;
3407 if (id == NULL)
3408 return NULL;
3410 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3411 if (ie == NULL)
3412 return NULL;
3414 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3415 == -1)
3416 return got_error_from_errno("asprintf");
3418 /* Reject merges into a work tree with conflicted files. */
3419 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3420 free(ondisk_path);
3421 if (err)
3422 return err;
3423 if (status == GOT_STATUS_CONFLICT)
3424 return got_error(GOT_ERR_CONFLICTS);
3426 return NULL;
3429 static const struct got_error *
3430 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3431 const char *fileindex_path, struct got_object_id *commit_id1,
3432 struct got_object_id *commit_id2, struct got_repository *repo,
3433 got_worktree_checkout_cb progress_cb, void *progress_arg,
3434 got_cancel_cb cancel_cb, void *cancel_arg)
3436 const struct got_error *err = NULL, *sync_err;
3437 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3438 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3439 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3440 struct check_merge_conflicts_arg cmc_arg;
3441 struct merge_file_cb_arg arg;
3442 char *label_orig = NULL;
3443 FILE *f1 = NULL, *f2 = NULL;
3444 int fd1 = -1, fd2 = -1;
3446 if (commit_id1) {
3447 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3448 if (err)
3449 goto done;
3450 err = got_object_id_by_path(&tree_id1, repo, commit1,
3451 worktree->path_prefix);
3452 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3453 goto done;
3455 if (tree_id1) {
3456 char *id_str;
3458 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3459 if (err)
3460 goto done;
3462 err = got_object_id_str(&id_str, commit_id1);
3463 if (err)
3464 goto done;
3466 if (asprintf(&label_orig, "%s: commit %s",
3467 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3468 err = got_error_from_errno("asprintf");
3469 free(id_str);
3470 goto done;
3472 free(id_str);
3474 f1 = got_opentemp();
3475 if (f1 == NULL) {
3476 err = got_error_from_errno("got_opentemp");
3477 goto done;
3481 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3482 if (err)
3483 goto done;
3485 err = got_object_id_by_path(&tree_id2, repo, commit2,
3486 worktree->path_prefix);
3487 if (err)
3488 goto done;
3490 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3491 if (err)
3492 goto done;
3494 f2 = got_opentemp();
3495 if (f2 == NULL) {
3496 err = got_error_from_errno("got_opentemp");
3497 goto done;
3500 fd1 = got_opentempfd();
3501 if (fd1 == -1) {
3502 err = got_error_from_errno("got_opentempfd");
3503 goto done;
3506 fd2 = got_opentempfd();
3507 if (fd2 == -1) {
3508 err = got_error_from_errno("got_opentempfd");
3509 goto done;
3512 cmc_arg.worktree = worktree;
3513 cmc_arg.fileindex = fileindex;
3514 cmc_arg.repo = repo;
3515 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3516 check_merge_conflicts, &cmc_arg, 0);
3517 if (err)
3518 goto done;
3520 arg.worktree = worktree;
3521 arg.fileindex = fileindex;
3522 arg.progress_cb = progress_cb;
3523 arg.progress_arg = progress_arg;
3524 arg.cancel_cb = cancel_cb;
3525 arg.cancel_arg = cancel_arg;
3526 arg.label_orig = label_orig;
3527 arg.commit_id2 = commit_id2;
3528 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3529 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3530 merge_file_cb, &arg, 1);
3531 sync_err = sync_fileindex(fileindex, fileindex_path);
3532 if (sync_err && err == NULL)
3533 err = sync_err;
3534 done:
3535 if (commit1)
3536 got_object_commit_close(commit1);
3537 if (commit2)
3538 got_object_commit_close(commit2);
3539 if (tree1)
3540 got_object_tree_close(tree1);
3541 if (tree2)
3542 got_object_tree_close(tree2);
3543 if (f1 && fclose(f1) == EOF && err == NULL)
3544 err = got_error_from_errno("fclose");
3545 if (f2 && fclose(f2) == EOF && err == NULL)
3546 err = got_error_from_errno("fclose");
3547 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3548 err = got_error_from_errno("close");
3549 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3550 err = got_error_from_errno("close");
3551 free(label_orig);
3552 return err;
3555 const struct got_error *
3556 got_worktree_merge_files(struct got_worktree *worktree,
3557 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3558 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3559 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3561 const struct got_error *err, *unlockerr;
3562 char *fileindex_path = NULL;
3563 struct got_fileindex *fileindex = NULL;
3564 struct check_mixed_commits_args cma;
3566 err = lock_worktree(worktree, LOCK_EX);
3567 if (err)
3568 return err;
3570 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3571 if (err)
3572 goto done;
3574 cma.worktree = worktree;
3575 cma.cancel_cb = cancel_cb;
3576 cma.cancel_arg = cancel_arg;
3578 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3579 &cma);
3580 if (err)
3581 goto done;
3583 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3584 commit_id2, repo, progress_cb, progress_arg,
3585 cancel_cb, cancel_arg);
3586 done:
3587 if (fileindex)
3588 got_fileindex_free(fileindex);
3589 free(fileindex_path);
3590 unlockerr = lock_worktree(worktree, LOCK_SH);
3591 if (unlockerr && err == NULL)
3592 err = unlockerr;
3593 return err;
3596 struct diff_dir_cb_arg {
3597 struct got_fileindex *fileindex;
3598 struct got_worktree *worktree;
3599 const char *status_path;
3600 size_t status_path_len;
3601 struct got_repository *repo;
3602 got_worktree_status_cb status_cb;
3603 void *status_arg;
3604 got_cancel_cb cancel_cb;
3605 void *cancel_arg;
3606 /* A pathlist containing per-directory pathlists of ignore patterns. */
3607 struct got_pathlist_head *ignores;
3608 int report_unchanged;
3609 int no_ignores;
3612 static const struct got_error *
3613 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3614 int dirfd, const char *de_name,
3615 got_worktree_status_cb status_cb, void *status_arg,
3616 struct got_repository *repo, int report_unchanged)
3618 const struct got_error *err = NULL;
3619 unsigned char status = GOT_STATUS_NO_CHANGE;
3620 unsigned char staged_status;
3621 struct stat sb;
3622 struct got_object_id blob_id, commit_id, staged_blob_id;
3623 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3624 struct got_object_id *staged_blob_idp = NULL;
3626 staged_status = get_staged_status(ie);
3627 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3628 if (err)
3629 return err;
3631 if (status == GOT_STATUS_NO_CHANGE &&
3632 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3633 return NULL;
3635 if (got_fileindex_entry_has_blob(ie))
3636 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3637 if (got_fileindex_entry_has_commit(ie))
3638 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3639 if (staged_status == GOT_STATUS_ADD ||
3640 staged_status == GOT_STATUS_MODIFY) {
3641 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3642 &staged_blob_id, ie);
3645 return (*status_cb)(status_arg, status, staged_status,
3646 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3649 static const struct got_error *
3650 status_old_new(void *arg, struct got_fileindex_entry *ie,
3651 struct dirent *de, const char *parent_path, int dirfd)
3653 const struct got_error *err = NULL;
3654 struct diff_dir_cb_arg *a = arg;
3655 char *abspath;
3657 if (a->cancel_cb) {
3658 err = a->cancel_cb(a->cancel_arg);
3659 if (err)
3660 return err;
3663 if (got_path_cmp(parent_path, a->status_path,
3664 strlen(parent_path), a->status_path_len) != 0 &&
3665 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3666 return NULL;
3668 if (parent_path[0]) {
3669 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3670 parent_path, de->d_name) == -1)
3671 return got_error_from_errno("asprintf");
3672 } else {
3673 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3674 de->d_name) == -1)
3675 return got_error_from_errno("asprintf");
3678 err = report_file_status(ie, abspath, dirfd, de->d_name,
3679 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3680 free(abspath);
3681 return err;
3684 static const struct got_error *
3685 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3687 const struct got_error *err;
3688 struct diff_dir_cb_arg *a = arg;
3689 struct got_object_id blob_id, commit_id;
3690 unsigned char status;
3692 if (a->cancel_cb) {
3693 err = a->cancel_cb(a->cancel_arg);
3694 if (err)
3695 return err;
3698 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3699 return NULL;
3701 got_fileindex_entry_get_blob_id(&blob_id, ie);
3702 got_fileindex_entry_get_commit_id(&commit_id, ie);
3703 if (got_fileindex_entry_has_file_on_disk(ie))
3704 status = GOT_STATUS_MISSING;
3705 else
3706 status = GOT_STATUS_DELETE;
3707 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3708 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3711 static void
3712 free_ignores(struct got_pathlist_head *ignores)
3714 struct got_pathlist_entry *pe;
3716 TAILQ_FOREACH(pe, ignores, entry) {
3717 struct got_pathlist_head *ignorelist = pe->data;
3719 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3721 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3724 static const struct got_error *
3725 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3727 const struct got_error *err = NULL;
3728 struct got_pathlist_entry *pe = NULL;
3729 struct got_pathlist_head *ignorelist;
3730 char *line = NULL, *pattern, *dirpath = NULL;
3731 size_t linesize = 0;
3732 ssize_t linelen;
3734 ignorelist = calloc(1, sizeof(*ignorelist));
3735 if (ignorelist == NULL)
3736 return got_error_from_errno("calloc");
3737 TAILQ_INIT(ignorelist);
3739 while ((linelen = getline(&line, &linesize, f)) != -1) {
3740 if (linelen > 0 && line[linelen - 1] == '\n')
3741 line[linelen - 1] = '\0';
3743 /* Git's ignores may contain comments. */
3744 if (line[0] == '#')
3745 continue;
3747 /* Git's negated patterns are not (yet?) supported. */
3748 if (line[0] == '!')
3749 continue;
3751 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3752 line) == -1) {
3753 err = got_error_from_errno("asprintf");
3754 goto done;
3756 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3757 if (err)
3758 goto done;
3760 if (ferror(f)) {
3761 err = got_error_from_errno("getline");
3762 goto done;
3765 dirpath = strdup(path);
3766 if (dirpath == NULL) {
3767 err = got_error_from_errno("strdup");
3768 goto done;
3770 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3771 done:
3772 free(line);
3773 if (err || pe == NULL) {
3774 free(dirpath);
3775 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3776 free(ignorelist);
3778 return err;
3781 static int
3782 match_path(const char *pattern, size_t pattern_len, const char *path,
3783 int flags)
3785 char buf[PATH_MAX];
3788 * Trailing slashes signify directories.
3789 * Append a * to make such patterns conform to fnmatch rules.
3791 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3792 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3793 return FNM_NOMATCH; /* XXX */
3795 return fnmatch(buf, path, flags);
3798 return fnmatch(pattern, path, flags);
3801 static int
3802 match_ignores(struct got_pathlist_head *ignores, const char *path)
3804 struct got_pathlist_entry *pe;
3806 /* Handle patterns which match in all directories. */
3807 TAILQ_FOREACH(pe, ignores, entry) {
3808 struct got_pathlist_head *ignorelist = pe->data;
3809 struct got_pathlist_entry *pi;
3811 TAILQ_FOREACH(pi, ignorelist, entry) {
3812 const char *p;
3814 if (pi->path_len < 3 ||
3815 strncmp(pi->path, "**/", 3) != 0)
3816 continue;
3817 p = path;
3818 while (*p) {
3819 if (match_path(pi->path + 3,
3820 pi->path_len - 3, p,
3821 FNM_PATHNAME | FNM_LEADING_DIR)) {
3822 /* Retry in next directory. */
3823 while (*p && *p != '/')
3824 p++;
3825 while (*p == '/')
3826 p++;
3827 continue;
3829 return 1;
3835 * The ignores pathlist contains ignore lists from children before
3836 * parents, so we can find the most specific ignorelist by walking
3837 * ignores backwards.
3839 pe = TAILQ_LAST(ignores, got_pathlist_head);
3840 while (pe) {
3841 if (got_path_is_child(path, pe->path, pe->path_len)) {
3842 struct got_pathlist_head *ignorelist = pe->data;
3843 struct got_pathlist_entry *pi;
3844 TAILQ_FOREACH(pi, ignorelist, entry) {
3845 int flags = FNM_LEADING_DIR;
3846 if (strstr(pi->path, "/**/") == NULL)
3847 flags |= FNM_PATHNAME;
3848 if (match_path(pi->path, pi->path_len,
3849 path, flags))
3850 continue;
3851 return 1;
3854 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3857 return 0;
3860 static const struct got_error *
3861 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3862 const char *path, int dirfd, const char *ignores_filename)
3864 const struct got_error *err = NULL;
3865 char *ignorespath;
3866 int fd = -1;
3867 FILE *ignoresfile = NULL;
3869 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3870 path[0] ? "/" : "", ignores_filename) == -1)
3871 return got_error_from_errno("asprintf");
3873 if (dirfd != -1) {
3874 fd = openat(dirfd, ignores_filename,
3875 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3876 if (fd == -1) {
3877 if (errno != ENOENT && errno != EACCES)
3878 err = got_error_from_errno2("openat",
3879 ignorespath);
3880 } else {
3881 ignoresfile = fdopen(fd, "r");
3882 if (ignoresfile == NULL)
3883 err = got_error_from_errno2("fdopen",
3884 ignorespath);
3885 else {
3886 fd = -1;
3887 err = read_ignores(ignores, path, ignoresfile);
3890 } else {
3891 ignoresfile = fopen(ignorespath, "re");
3892 if (ignoresfile == NULL) {
3893 if (errno != ENOENT && errno != EACCES)
3894 err = got_error_from_errno2("fopen",
3895 ignorespath);
3896 } else
3897 err = read_ignores(ignores, path, ignoresfile);
3900 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3901 err = got_error_from_errno2("fclose", path);
3902 if (fd != -1 && close(fd) == -1 && err == NULL)
3903 err = got_error_from_errno2("close", path);
3904 free(ignorespath);
3905 return err;
3908 static const struct got_error *
3909 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3910 int dirfd)
3912 const struct got_error *err = NULL;
3913 struct diff_dir_cb_arg *a = arg;
3914 char *path = NULL;
3916 if (ignore != NULL)
3917 *ignore = 0;
3919 if (a->cancel_cb) {
3920 err = a->cancel_cb(a->cancel_arg);
3921 if (err)
3922 return err;
3925 if (parent_path[0]) {
3926 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3927 return got_error_from_errno("asprintf");
3928 } else {
3929 path = de->d_name;
3932 if (de->d_type == DT_DIR) {
3933 if (!a->no_ignores && ignore != NULL &&
3934 match_ignores(a->ignores, path))
3935 *ignore = 1;
3936 } else if (!match_ignores(a->ignores, path) &&
3937 got_path_is_child(path, a->status_path, a->status_path_len))
3938 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3939 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3940 if (parent_path[0])
3941 free(path);
3942 return err;
3945 static const struct got_error *
3946 status_traverse(void *arg, const char *path, int dirfd)
3948 const struct got_error *err = NULL;
3949 struct diff_dir_cb_arg *a = arg;
3951 if (a->no_ignores)
3952 return NULL;
3954 err = add_ignores(a->ignores, a->worktree->root_path,
3955 path, dirfd, ".cvsignore");
3956 if (err)
3957 return err;
3959 err = add_ignores(a->ignores, a->worktree->root_path, path,
3960 dirfd, ".gitignore");
3962 return err;
3965 static const struct got_error *
3966 report_single_file_status(const char *path, const char *ondisk_path,
3967 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3968 void *status_arg, struct got_repository *repo, int report_unchanged,
3969 struct got_pathlist_head *ignores, int no_ignores)
3971 struct got_fileindex_entry *ie;
3972 struct stat sb;
3974 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3975 if (ie)
3976 return report_file_status(ie, ondisk_path, -1, NULL,
3977 status_cb, status_arg, repo, report_unchanged);
3979 if (lstat(ondisk_path, &sb) == -1) {
3980 if (errno != ENOENT)
3981 return got_error_from_errno2("lstat", ondisk_path);
3982 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3983 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3986 if (!no_ignores && match_ignores(ignores, path))
3987 return NULL;
3989 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3990 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3991 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3993 return NULL;
3996 static const struct got_error *
3997 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3998 const char *root_path, const char *path)
4000 const struct got_error *err;
4001 char *parent_path, *next_parent_path = NULL;
4003 err = add_ignores(ignores, root_path, "", -1,
4004 ".cvsignore");
4005 if (err)
4006 return err;
4008 err = add_ignores(ignores, root_path, "", -1,
4009 ".gitignore");
4010 if (err)
4011 return err;
4013 err = got_path_dirname(&parent_path, path);
4014 if (err) {
4015 if (err->code == GOT_ERR_BAD_PATH)
4016 return NULL; /* cannot traverse parent */
4017 return err;
4019 for (;;) {
4020 err = add_ignores(ignores, root_path, parent_path, -1,
4021 ".cvsignore");
4022 if (err)
4023 break;
4024 err = add_ignores(ignores, root_path, parent_path, -1,
4025 ".gitignore");
4026 if (err)
4027 break;
4028 err = got_path_dirname(&next_parent_path, parent_path);
4029 if (err) {
4030 if (err->code == GOT_ERR_BAD_PATH)
4031 err = NULL; /* traversed everything */
4032 break;
4034 if (got_path_is_root_dir(parent_path))
4035 break;
4036 free(parent_path);
4037 parent_path = next_parent_path;
4038 next_parent_path = NULL;
4041 free(parent_path);
4042 free(next_parent_path);
4043 return err;
4046 struct find_missing_children_args {
4047 const char *parent_path;
4048 size_t parent_len;
4049 struct got_pathlist_head *children;
4050 got_cancel_cb cancel_cb;
4051 void *cancel_arg;
4054 static const struct got_error *
4055 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4057 const struct got_error *err = NULL;
4058 struct find_missing_children_args *a = arg;
4060 if (a->cancel_cb) {
4061 err = a->cancel_cb(a->cancel_arg);
4062 if (err)
4063 return err;
4066 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4067 err = got_pathlist_append(a->children, ie->path, NULL);
4069 return err;
4072 static const struct got_error *
4073 report_children(struct got_pathlist_head *children,
4074 struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 struct got_repository *repo, int is_root_dir, int report_unchanged,
4076 struct got_pathlist_head *ignores, int no_ignores,
4077 got_worktree_status_cb status_cb, void *status_arg,
4078 got_cancel_cb cancel_cb, void *cancel_arg)
4080 const struct got_error *err = NULL;
4081 struct got_pathlist_entry *pe;
4082 char *ondisk_path = NULL;
4084 TAILQ_FOREACH(pe, children, entry) {
4085 if (cancel_cb) {
4086 err = cancel_cb(cancel_arg);
4087 if (err)
4088 break;
4091 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4092 !is_root_dir ? "/" : "", pe->path) == -1) {
4093 err = got_error_from_errno("asprintf");
4094 ondisk_path = NULL;
4095 break;
4098 err = report_single_file_status(pe->path, ondisk_path,
4099 fileindex, status_cb, status_arg, repo, report_unchanged,
4100 ignores, no_ignores);
4101 if (err)
4102 break;
4104 free(ondisk_path);
4105 ondisk_path = NULL;
4108 free(ondisk_path);
4109 return err;
4112 static const struct got_error *
4113 worktree_status(struct got_worktree *worktree, const char *path,
4114 struct got_fileindex *fileindex, struct got_repository *repo,
4115 got_worktree_status_cb status_cb, void *status_arg,
4116 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4117 int report_unchanged)
4119 const struct got_error *err = NULL;
4120 int fd = -1;
4121 struct got_fileindex_diff_dir_cb fdiff_cb;
4122 struct diff_dir_cb_arg arg;
4123 char *ondisk_path = NULL;
4124 struct got_pathlist_head ignores, missing_children;
4125 struct got_fileindex_entry *ie;
4127 TAILQ_INIT(&ignores);
4128 TAILQ_INIT(&missing_children);
4130 if (asprintf(&ondisk_path, "%s%s%s",
4131 worktree->root_path, path[0] ? "/" : "", path) == -1)
4132 return got_error_from_errno("asprintf");
4134 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4135 if (ie) {
4136 err = report_single_file_status(path, ondisk_path,
4137 fileindex, status_cb, status_arg, repo,
4138 report_unchanged, &ignores, no_ignores);
4139 goto done;
4140 } else {
4141 struct find_missing_children_args fmca;
4142 fmca.parent_path = path;
4143 fmca.parent_len = strlen(path);
4144 fmca.children = &missing_children;
4145 fmca.cancel_cb = cancel_cb;
4146 fmca.cancel_arg = cancel_arg;
4147 err = got_fileindex_for_each_entry_safe(fileindex,
4148 find_missing_children, &fmca);
4149 if (err)
4150 goto done;
4153 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4154 if (fd == -1) {
4155 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4156 !got_err_open_nofollow_on_symlink())
4157 err = got_error_from_errno2("open", ondisk_path);
4158 else {
4159 if (!no_ignores) {
4160 err = add_ignores_from_parent_paths(&ignores,
4161 worktree->root_path, ondisk_path);
4162 if (err)
4163 goto done;
4165 if (TAILQ_EMPTY(&missing_children)) {
4166 err = report_single_file_status(path,
4167 ondisk_path, fileindex,
4168 status_cb, status_arg, repo,
4169 report_unchanged, &ignores, no_ignores);
4170 if (err)
4171 goto done;
4172 } else {
4173 err = report_children(&missing_children,
4174 worktree, fileindex, repo,
4175 (path[0] == '\0'), report_unchanged,
4176 &ignores, no_ignores,
4177 status_cb, status_arg,
4178 cancel_cb, cancel_arg);
4179 if (err)
4180 goto done;
4183 } else {
4184 fdiff_cb.diff_old_new = status_old_new;
4185 fdiff_cb.diff_old = status_old;
4186 fdiff_cb.diff_new = status_new;
4187 fdiff_cb.diff_traverse = status_traverse;
4188 arg.fileindex = fileindex;
4189 arg.worktree = worktree;
4190 arg.status_path = path;
4191 arg.status_path_len = strlen(path);
4192 arg.repo = repo;
4193 arg.status_cb = status_cb;
4194 arg.status_arg = status_arg;
4195 arg.cancel_cb = cancel_cb;
4196 arg.cancel_arg = cancel_arg;
4197 arg.report_unchanged = report_unchanged;
4198 arg.no_ignores = no_ignores;
4199 if (!no_ignores) {
4200 err = add_ignores_from_parent_paths(&ignores,
4201 worktree->root_path, path);
4202 if (err)
4203 goto done;
4205 arg.ignores = &ignores;
4206 err = got_fileindex_diff_dir(fileindex, fd,
4207 worktree->root_path, path, repo, &fdiff_cb, &arg);
4209 done:
4210 free_ignores(&ignores);
4211 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4212 if (fd != -1 && close(fd) == -1 && err == NULL)
4213 err = got_error_from_errno("close");
4214 free(ondisk_path);
4215 return err;
4218 const struct got_error *
4219 got_worktree_status(struct got_worktree *worktree,
4220 struct got_pathlist_head *paths, struct got_repository *repo,
4221 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4222 got_cancel_cb cancel_cb, void *cancel_arg)
4224 const struct got_error *err = NULL;
4225 char *fileindex_path = NULL;
4226 struct got_fileindex *fileindex = NULL;
4227 struct got_pathlist_entry *pe;
4229 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4230 if (err)
4231 return err;
4233 TAILQ_FOREACH(pe, paths, entry) {
4234 err = worktree_status(worktree, pe->path, fileindex, repo,
4235 status_cb, status_arg, cancel_cb, cancel_arg,
4236 no_ignores, 0);
4237 if (err)
4238 break;
4240 free(fileindex_path);
4241 got_fileindex_free(fileindex);
4242 return err;
4245 const struct got_error *
4246 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4247 const char *arg)
4249 const struct got_error *err = NULL;
4250 char *resolved = NULL, *cwd = NULL, *path = NULL;
4251 size_t len;
4252 struct stat sb;
4253 char *abspath = NULL;
4254 char canonpath[PATH_MAX];
4256 *wt_path = NULL;
4258 cwd = getcwd(NULL, 0);
4259 if (cwd == NULL)
4260 return got_error_from_errno("getcwd");
4262 if (lstat(arg, &sb) == -1) {
4263 if (errno != ENOENT) {
4264 err = got_error_from_errno2("lstat", arg);
4265 goto done;
4267 sb.st_mode = 0;
4269 if (S_ISLNK(sb.st_mode)) {
4271 * We cannot use realpath(3) with symlinks since we want to
4272 * operate on the symlink itself.
4273 * But we can make the path absolute, assuming it is relative
4274 * to the current working directory, and then canonicalize it.
4276 if (!got_path_is_absolute(arg)) {
4277 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4278 err = got_error_from_errno("asprintf");
4279 goto done;
4283 err = got_canonpath(abspath ? abspath : arg, canonpath,
4284 sizeof(canonpath));
4285 if (err)
4286 goto done;
4287 resolved = strdup(canonpath);
4288 if (resolved == NULL) {
4289 err = got_error_from_errno("strdup");
4290 goto done;
4292 } else {
4293 resolved = realpath(arg, NULL);
4294 if (resolved == NULL) {
4295 if (errno != ENOENT) {
4296 err = got_error_from_errno2("realpath", arg);
4297 goto done;
4299 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4300 err = got_error_from_errno("asprintf");
4301 goto done;
4303 err = got_canonpath(abspath, canonpath,
4304 sizeof(canonpath));
4305 if (err)
4306 goto done;
4307 resolved = strdup(canonpath);
4308 if (resolved == NULL) {
4309 err = got_error_from_errno("strdup");
4310 goto done;
4315 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4316 strlen(got_worktree_get_root_path(worktree)))) {
4317 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4318 goto done;
4321 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4322 err = got_path_skip_common_ancestor(&path,
4323 got_worktree_get_root_path(worktree), resolved);
4324 if (err)
4325 goto done;
4326 } else {
4327 path = strdup("");
4328 if (path == NULL) {
4329 err = got_error_from_errno("strdup");
4330 goto done;
4334 /* XXX status walk can't deal with trailing slash! */
4335 len = strlen(path);
4336 while (len > 0 && path[len - 1] == '/') {
4337 path[len - 1] = '\0';
4338 len--;
4340 done:
4341 free(abspath);
4342 free(resolved);
4343 free(cwd);
4344 if (err == NULL)
4345 *wt_path = path;
4346 else
4347 free(path);
4348 return err;
4351 struct schedule_addition_args {
4352 struct got_worktree *worktree;
4353 struct got_fileindex *fileindex;
4354 got_worktree_checkout_cb progress_cb;
4355 void *progress_arg;
4356 struct got_repository *repo;
4359 static int
4360 add_noop_status(unsigned char status)
4362 return (status == GOT_STATUS_ADD ||
4363 status == GOT_STATUS_MODIFY ||
4364 status == GOT_STATUS_CONFLICT ||
4365 status == GOT_STATUS_MODE_CHANGE ||
4366 status == GOT_STATUS_NO_CHANGE);
4369 static const struct got_error *
4370 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4371 const char *relpath, struct got_object_id *blob_id,
4372 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4373 int dirfd, const char *de_name)
4375 struct schedule_addition_args *a = arg;
4376 const struct got_error *err = NULL;
4377 struct got_fileindex_entry *ie;
4378 struct stat sb;
4379 char *ondisk_path;
4381 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4382 relpath) == -1)
4383 return got_error_from_errno("asprintf");
4385 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4386 if (ie) {
4387 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4388 de_name, a->repo);
4389 if (err)
4390 goto done;
4391 /* Re-adding an existing entry is a no-op. */
4392 if (staged_status == GOT_STATUS_NO_CHANGE &&
4393 add_noop_status(status))
4394 goto done;
4395 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4396 if (err)
4397 goto done;
4400 if (status != GOT_STATUS_UNVERSIONED) {
4401 if (status == GOT_STATUS_NONEXISTENT)
4402 err = got_error_set_errno(ENOENT, ondisk_path);
4403 else
4404 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4405 goto done;
4408 err = got_fileindex_entry_alloc(&ie, relpath);
4409 if (err)
4410 goto done;
4411 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4412 relpath, NULL, NULL, 1);
4413 if (err) {
4414 got_fileindex_entry_free(ie);
4415 goto done;
4417 err = got_fileindex_entry_add(a->fileindex, ie);
4418 if (err) {
4419 got_fileindex_entry_free(ie);
4420 goto done;
4422 done:
4423 free(ondisk_path);
4424 if (err)
4425 return err;
4426 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4427 return NULL;
4428 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4431 const struct got_error *
4432 got_worktree_schedule_add(struct got_worktree *worktree,
4433 struct got_pathlist_head *paths,
4434 got_worktree_checkout_cb progress_cb, void *progress_arg,
4435 struct got_repository *repo, int no_ignores)
4437 struct got_fileindex *fileindex = NULL;
4438 char *fileindex_path = NULL;
4439 const struct got_error *err = NULL, *sync_err, *unlockerr;
4440 struct got_pathlist_entry *pe;
4441 struct schedule_addition_args saa;
4443 err = lock_worktree(worktree, LOCK_EX);
4444 if (err)
4445 return err;
4447 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4448 if (err)
4449 goto done;
4451 saa.worktree = worktree;
4452 saa.fileindex = fileindex;
4453 saa.progress_cb = progress_cb;
4454 saa.progress_arg = progress_arg;
4455 saa.repo = repo;
4457 TAILQ_FOREACH(pe, paths, entry) {
4458 err = worktree_status(worktree, pe->path, fileindex, repo,
4459 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4460 if (err)
4461 break;
4463 sync_err = sync_fileindex(fileindex, fileindex_path);
4464 if (sync_err && err == NULL)
4465 err = sync_err;
4466 done:
4467 free(fileindex_path);
4468 if (fileindex)
4469 got_fileindex_free(fileindex);
4470 unlockerr = lock_worktree(worktree, LOCK_SH);
4471 if (unlockerr && err == NULL)
4472 err = unlockerr;
4473 return err;
4476 struct schedule_deletion_args {
4477 struct got_worktree *worktree;
4478 struct got_fileindex *fileindex;
4479 got_worktree_delete_cb progress_cb;
4480 void *progress_arg;
4481 struct got_repository *repo;
4482 int delete_local_mods;
4483 int keep_on_disk;
4484 int ignore_missing_paths;
4485 const char *status_path;
4486 size_t status_path_len;
4487 const char *status_codes;
4490 static const struct got_error *
4491 schedule_for_deletion(void *arg, unsigned char status,
4492 unsigned char staged_status, const char *relpath,
4493 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4494 struct got_object_id *commit_id, int dirfd, const char *de_name)
4496 struct schedule_deletion_args *a = arg;
4497 const struct got_error *err = NULL;
4498 struct got_fileindex_entry *ie = NULL;
4499 struct stat sb;
4500 char *ondisk_path;
4502 if (status == GOT_STATUS_NONEXISTENT) {
4503 if (a->ignore_missing_paths)
4504 return NULL;
4505 return got_error_set_errno(ENOENT, relpath);
4508 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4509 if (ie == NULL)
4510 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4512 staged_status = get_staged_status(ie);
4513 if (staged_status != GOT_STATUS_NO_CHANGE) {
4514 if (staged_status == GOT_STATUS_DELETE)
4515 return NULL;
4516 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4519 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4520 relpath) == -1)
4521 return got_error_from_errno("asprintf");
4523 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4524 a->repo);
4525 if (err)
4526 goto done;
4528 if (a->status_codes) {
4529 size_t ncodes = strlen(a->status_codes);
4530 int i;
4531 for (i = 0; i < ncodes ; i++) {
4532 if (status == a->status_codes[i])
4533 break;
4535 if (i == ncodes) {
4536 /* Do not delete files in non-matching status. */
4537 free(ondisk_path);
4538 return NULL;
4540 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4541 a->status_codes[i] != GOT_STATUS_MISSING) {
4542 static char msg[64];
4543 snprintf(msg, sizeof(msg),
4544 "invalid status code '%c'", a->status_codes[i]);
4545 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4546 goto done;
4550 if (status != GOT_STATUS_NO_CHANGE) {
4551 if (status == GOT_STATUS_DELETE)
4552 goto done;
4553 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4554 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4555 goto done;
4557 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4558 err = got_error_set_errno(ENOENT, relpath);
4559 goto done;
4561 if (status != GOT_STATUS_MODIFY &&
4562 status != GOT_STATUS_MISSING) {
4563 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4564 goto done;
4568 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4569 size_t root_len;
4571 if (dirfd != -1) {
4572 if (unlinkat(dirfd, de_name, 0) == -1) {
4573 err = got_error_from_errno2("unlinkat",
4574 ondisk_path);
4575 goto done;
4577 } else if (unlink(ondisk_path) == -1) {
4578 err = got_error_from_errno2("unlink", ondisk_path);
4579 goto done;
4582 root_len = strlen(a->worktree->root_path);
4583 do {
4584 char *parent;
4586 err = got_path_dirname(&parent, ondisk_path);
4587 if (err)
4588 goto done;
4589 free(ondisk_path);
4590 ondisk_path = parent;
4591 if (got_path_cmp(ondisk_path, a->status_path,
4592 strlen(ondisk_path), a->status_path_len) != 0 &&
4593 !got_path_is_child(ondisk_path, a->status_path,
4594 a->status_path_len))
4595 break;
4596 if (rmdir(ondisk_path) == -1) {
4597 if (errno != ENOTEMPTY)
4598 err = got_error_from_errno2("rmdir",
4599 ondisk_path);
4600 break;
4602 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4603 strlen(ondisk_path), root_len) != 0);
4606 if (got_fileindex_entry_has_blob(ie))
4607 got_fileindex_entry_mark_deleted_from_disk(ie);
4608 else
4609 got_fileindex_entry_remove(a->fileindex, ie);
4610 done:
4611 free(ondisk_path);
4612 if (err)
4613 return err;
4614 if (status == GOT_STATUS_DELETE)
4615 return NULL;
4616 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4617 staged_status, relpath);
4620 const struct got_error *
4621 got_worktree_schedule_delete(struct got_worktree *worktree,
4622 struct got_pathlist_head *paths, int delete_local_mods,
4623 const char *status_codes,
4624 got_worktree_delete_cb progress_cb, void *progress_arg,
4625 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4627 struct got_fileindex *fileindex = NULL;
4628 char *fileindex_path = NULL;
4629 const struct got_error *err = NULL, *sync_err, *unlockerr;
4630 struct got_pathlist_entry *pe;
4631 struct schedule_deletion_args sda;
4633 err = lock_worktree(worktree, LOCK_EX);
4634 if (err)
4635 return err;
4637 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4638 if (err)
4639 goto done;
4641 sda.worktree = worktree;
4642 sda.fileindex = fileindex;
4643 sda.progress_cb = progress_cb;
4644 sda.progress_arg = progress_arg;
4645 sda.repo = repo;
4646 sda.delete_local_mods = delete_local_mods;
4647 sda.keep_on_disk = keep_on_disk;
4648 sda.ignore_missing_paths = ignore_missing_paths;
4649 sda.status_codes = status_codes;
4651 TAILQ_FOREACH(pe, paths, entry) {
4652 char *ondisk_status_path;
4654 if (asprintf(&ondisk_status_path, "%s%s%s",
4655 got_worktree_get_root_path(worktree),
4656 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4657 err = got_error_from_errno("asprintf");
4658 goto done;
4660 sda.status_path = ondisk_status_path;
4661 sda.status_path_len = strlen(ondisk_status_path);
4662 err = worktree_status(worktree, pe->path, fileindex, repo,
4663 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4664 free(ondisk_status_path);
4665 if (err)
4666 break;
4668 sync_err = sync_fileindex(fileindex, fileindex_path);
4669 if (sync_err && err == NULL)
4670 err = sync_err;
4671 done:
4672 free(fileindex_path);
4673 if (fileindex)
4674 got_fileindex_free(fileindex);
4675 unlockerr = lock_worktree(worktree, LOCK_SH);
4676 if (unlockerr && err == NULL)
4677 err = unlockerr;
4678 return err;
4681 static const struct got_error *
4682 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4684 const struct got_error *err = NULL;
4685 char *line = NULL;
4686 size_t linesize = 0, n;
4687 ssize_t linelen;
4689 linelen = getline(&line, &linesize, infile);
4690 if (linelen == -1) {
4691 if (ferror(infile)) {
4692 err = got_error_from_errno("getline");
4693 goto done;
4695 return NULL;
4697 if (outfile) {
4698 n = fwrite(line, 1, linelen, outfile);
4699 if (n != linelen) {
4700 err = got_ferror(outfile, GOT_ERR_IO);
4701 goto done;
4704 if (rejectfile) {
4705 n = fwrite(line, 1, linelen, rejectfile);
4706 if (n != linelen)
4707 err = got_ferror(rejectfile, GOT_ERR_IO);
4709 done:
4710 free(line);
4711 return err;
4714 static const struct got_error *
4715 skip_one_line(FILE *f)
4717 char *line = NULL;
4718 size_t linesize = 0;
4719 ssize_t linelen;
4721 linelen = getline(&line, &linesize, f);
4722 if (linelen == -1) {
4723 if (ferror(f))
4724 return got_error_from_errno("getline");
4725 return NULL;
4727 free(line);
4728 return NULL;
4731 static const struct got_error *
4732 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4733 int start_old, int end_old, int start_new, int end_new,
4734 FILE *outfile, FILE *rejectfile)
4736 const struct got_error *err;
4738 /* Copy old file's lines leading up to patch. */
4739 while (!feof(f1) && *line_cur1 < start_old) {
4740 err = copy_one_line(f1, outfile, NULL);
4741 if (err)
4742 return err;
4743 (*line_cur1)++;
4745 /* Skip new file's lines leading up to patch. */
4746 while (!feof(f2) && *line_cur2 < start_new) {
4747 if (rejectfile)
4748 err = copy_one_line(f2, NULL, rejectfile);
4749 else
4750 err = skip_one_line(f2);
4751 if (err)
4752 return err;
4753 (*line_cur2)++;
4755 /* Copy patched lines. */
4756 while (!feof(f2) && *line_cur2 <= end_new) {
4757 err = copy_one_line(f2, outfile, NULL);
4758 if (err)
4759 return err;
4760 (*line_cur2)++;
4762 /* Skip over old file's replaced lines. */
4763 while (!feof(f1) && *line_cur1 <= end_old) {
4764 if (rejectfile)
4765 err = copy_one_line(f1, NULL, rejectfile);
4766 else
4767 err = skip_one_line(f1);
4768 if (err)
4769 return err;
4770 (*line_cur1)++;
4773 return NULL;
4776 static const struct got_error *
4777 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4778 FILE *outfile, FILE *rejectfile)
4780 const struct got_error *err;
4782 if (outfile) {
4783 /* Copy old file's lines until EOF. */
4784 while (!feof(f1)) {
4785 err = copy_one_line(f1, outfile, NULL);
4786 if (err)
4787 return err;
4788 (*line_cur1)++;
4791 if (rejectfile) {
4792 /* Copy new file's lines until EOF. */
4793 while (!feof(f2)) {
4794 err = copy_one_line(f2, NULL, rejectfile);
4795 if (err)
4796 return err;
4797 (*line_cur2)++;
4801 return NULL;
4804 static const struct got_error *
4805 apply_or_reject_change(int *choice, int *nchunks_used,
4806 struct diff_result *diff_result, int n,
4807 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4808 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4809 got_worktree_patch_cb patch_cb, void *patch_arg)
4811 const struct got_error *err = NULL;
4812 struct diff_chunk_context cc = {};
4813 int start_old, end_old, start_new, end_new;
4814 FILE *hunkfile;
4815 struct diff_output_unidiff_state *diff_state;
4816 struct diff_input_info diff_info;
4817 int rc;
4819 *choice = GOT_PATCH_CHOICE_NONE;
4821 /* Get changed line numbers without context lines for copy_change(). */
4822 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4823 start_old = cc.left.start;
4824 end_old = cc.left.end;
4825 start_new = cc.right.start;
4826 end_new = cc.right.end;
4828 /* Get the same change with context lines for display. */
4829 memset(&cc, 0, sizeof(cc));
4830 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4832 memset(&diff_info, 0, sizeof(diff_info));
4833 diff_info.left_path = relpath;
4834 diff_info.right_path = relpath;
4836 diff_state = diff_output_unidiff_state_alloc();
4837 if (diff_state == NULL)
4838 return got_error_set_errno(ENOMEM,
4839 "diff_output_unidiff_state_alloc");
4841 hunkfile = got_opentemp();
4842 if (hunkfile == NULL) {
4843 err = got_error_from_errno("got_opentemp");
4844 goto done;
4847 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4848 diff_result, &cc);
4849 if (rc != DIFF_RC_OK) {
4850 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4851 goto done;
4854 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4855 err = got_ferror(hunkfile, GOT_ERR_IO);
4856 goto done;
4859 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4860 hunkfile, changeno, nchanges);
4861 if (err)
4862 goto done;
4864 switch (*choice) {
4865 case GOT_PATCH_CHOICE_YES:
4866 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4867 end_old, start_new, end_new, outfile, rejectfile);
4868 break;
4869 case GOT_PATCH_CHOICE_NO:
4870 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 end_old, start_new, end_new, rejectfile, outfile);
4872 break;
4873 case GOT_PATCH_CHOICE_QUIT:
4874 break;
4875 default:
4876 err = got_error(GOT_ERR_PATCH_CHOICE);
4877 break;
4879 done:
4880 diff_output_unidiff_state_free(diff_state);
4881 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4882 err = got_error_from_errno("fclose");
4883 return err;
4886 struct revert_file_args {
4887 struct got_worktree *worktree;
4888 struct got_fileindex *fileindex;
4889 got_worktree_checkout_cb progress_cb;
4890 void *progress_arg;
4891 got_worktree_patch_cb patch_cb;
4892 void *patch_arg;
4893 struct got_repository *repo;
4894 int unlink_added_files;
4895 struct got_pathlist_head *added_files_to_unlink;
4898 static const struct got_error *
4899 create_patched_content(char **path_outfile, int reverse_patch,
4900 struct got_object_id *blob_id, const char *path2,
4901 int dirfd2, const char *de_name2,
4902 const char *relpath, struct got_repository *repo,
4903 got_worktree_patch_cb patch_cb, void *patch_arg)
4905 const struct got_error *err, *free_err;
4906 struct got_blob_object *blob = NULL;
4907 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4908 int fd = -1, fd2 = -1;
4909 char link_target[PATH_MAX];
4910 ssize_t link_len = 0;
4911 char *path1 = NULL, *id_str = NULL;
4912 struct stat sb2;
4913 struct got_diffreg_result *diffreg_result = NULL;
4914 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4915 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4917 *path_outfile = NULL;
4919 err = got_object_id_str(&id_str, blob_id);
4920 if (err)
4921 return err;
4923 if (dirfd2 != -1) {
4924 fd2 = openat(dirfd2, de_name2,
4925 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4926 if (fd2 == -1) {
4927 if (!got_err_open_nofollow_on_symlink()) {
4928 err = got_error_from_errno2("openat", path2);
4929 goto done;
4931 link_len = readlinkat(dirfd2, de_name2,
4932 link_target, sizeof(link_target));
4933 if (link_len == -1) {
4934 return got_error_from_errno2("readlinkat",
4935 path2);
4937 sb2.st_mode = S_IFLNK;
4938 sb2.st_size = link_len;
4940 } else {
4941 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4942 if (fd2 == -1) {
4943 if (!got_err_open_nofollow_on_symlink()) {
4944 err = got_error_from_errno2("open", path2);
4945 goto done;
4947 link_len = readlink(path2, link_target,
4948 sizeof(link_target));
4949 if (link_len == -1)
4950 return got_error_from_errno2("readlink", path2);
4951 sb2.st_mode = S_IFLNK;
4952 sb2.st_size = link_len;
4955 if (fd2 != -1) {
4956 if (fstat(fd2, &sb2) == -1) {
4957 err = got_error_from_errno2("fstat", path2);
4958 goto done;
4961 f2 = fdopen(fd2, "r");
4962 if (f2 == NULL) {
4963 err = got_error_from_errno2("fdopen", path2);
4964 goto done;
4966 fd2 = -1;
4967 } else {
4968 size_t n;
4969 f2 = got_opentemp();
4970 if (f2 == NULL) {
4971 err = got_error_from_errno2("got_opentemp", path2);
4972 goto done;
4974 n = fwrite(link_target, 1, link_len, f2);
4975 if (n != link_len) {
4976 err = got_ferror(f2, GOT_ERR_IO);
4977 goto done;
4979 if (fflush(f2) == EOF) {
4980 err = got_error_from_errno("fflush");
4981 goto done;
4983 rewind(f2);
4986 fd = got_opentempfd();
4987 if (fd == -1) {
4988 err = got_error_from_errno("got_opentempfd");
4989 goto done;
4992 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4993 if (err)
4994 goto done;
4996 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4997 if (err)
4998 goto done;
5000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5001 if (err)
5002 goto done;
5004 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5005 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5006 if (err)
5007 goto done;
5009 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5010 "");
5011 if (err)
5012 goto done;
5014 if (fseek(f1, 0L, SEEK_SET) == -1)
5015 return got_ferror(f1, GOT_ERR_IO);
5016 if (fseek(f2, 0L, SEEK_SET) == -1)
5017 return got_ferror(f2, GOT_ERR_IO);
5019 /* Count the number of actual changes in the diff result. */
5020 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5021 struct diff_chunk_context cc = {};
5022 diff_chunk_context_load_change(&cc, &nchunks_used,
5023 diffreg_result->result, n, 0);
5024 nchanges++;
5026 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5027 int choice;
5028 err = apply_or_reject_change(&choice, &nchunks_used,
5029 diffreg_result->result, n, relpath, f1, f2,
5030 &line_cur1, &line_cur2,
5031 reverse_patch ? NULL : outfile,
5032 reverse_patch ? outfile : NULL,
5033 ++i, nchanges, patch_cb, patch_arg);
5034 if (err)
5035 goto done;
5036 if (choice == GOT_PATCH_CHOICE_YES)
5037 have_content = 1;
5038 else if (choice == GOT_PATCH_CHOICE_QUIT)
5039 break;
5041 if (have_content) {
5042 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5043 reverse_patch ? NULL : outfile,
5044 reverse_patch ? outfile : NULL);
5045 if (err)
5046 goto done;
5048 if (!S_ISLNK(sb2.st_mode)) {
5049 mode_t mode;
5051 mode = apply_umask(sb2.st_mode);
5052 if (fchmod(fileno(outfile), mode) == -1) {
5053 err = got_error_from_errno2("fchmod", path2);
5054 goto done;
5058 done:
5059 free(id_str);
5060 if (fd != -1 && close(fd) == -1 && err == NULL)
5061 err = got_error_from_errno("close");
5062 if (blob)
5063 got_object_blob_close(blob);
5064 free_err = got_diffreg_result_free(diffreg_result);
5065 if (err == NULL)
5066 err = free_err;
5067 if (f1 && fclose(f1) == EOF && err == NULL)
5068 err = got_error_from_errno2("fclose", path1);
5069 if (f2 && fclose(f2) == EOF && err == NULL)
5070 err = got_error_from_errno2("fclose", path2);
5071 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5072 err = got_error_from_errno2("close", path2);
5073 if (outfile && fclose(outfile) == EOF && err == NULL)
5074 err = got_error_from_errno2("fclose", *path_outfile);
5075 if (path1 && unlink(path1) == -1 && err == NULL)
5076 err = got_error_from_errno2("unlink", path1);
5077 if (err || !have_content) {
5078 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5079 err = got_error_from_errno2("unlink", *path_outfile);
5080 free(*path_outfile);
5081 *path_outfile = NULL;
5083 free(path1);
5084 return err;
5087 static const struct got_error *
5088 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5089 const char *relpath, struct got_object_id *blob_id,
5090 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5091 int dirfd, const char *de_name)
5093 struct revert_file_args *a = arg;
5094 const struct got_error *err = NULL;
5095 char *parent_path = NULL;
5096 struct got_fileindex_entry *ie;
5097 struct got_commit_object *base_commit = NULL;
5098 struct got_tree_object *tree = NULL;
5099 struct got_object_id *tree_id = NULL;
5100 const struct got_tree_entry *te = NULL;
5101 char *tree_path = NULL, *te_name;
5102 char *ondisk_path = NULL, *path_content = NULL;
5103 struct got_blob_object *blob = NULL;
5104 int fd = -1;
5106 /* Reverting a staged deletion is a no-op. */
5107 if (status == GOT_STATUS_DELETE &&
5108 staged_status != GOT_STATUS_NO_CHANGE)
5109 return NULL;
5111 if (status == GOT_STATUS_UNVERSIONED)
5112 return (*a->progress_cb)(a->progress_arg,
5113 GOT_STATUS_UNVERSIONED, relpath);
5115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5116 if (ie == NULL)
5117 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5119 /* Construct in-repository path of tree which contains this blob. */
5120 err = got_path_dirname(&parent_path, ie->path);
5121 if (err) {
5122 if (err->code != GOT_ERR_BAD_PATH)
5123 goto done;
5124 parent_path = strdup("/");
5125 if (parent_path == NULL) {
5126 err = got_error_from_errno("strdup");
5127 goto done;
5130 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5131 tree_path = strdup(parent_path);
5132 if (tree_path == NULL) {
5133 err = got_error_from_errno("strdup");
5134 goto done;
5136 } else {
5137 if (got_path_is_root_dir(parent_path)) {
5138 tree_path = strdup(a->worktree->path_prefix);
5139 if (tree_path == NULL) {
5140 err = got_error_from_errno("strdup");
5141 goto done;
5143 } else {
5144 if (asprintf(&tree_path, "%s/%s",
5145 a->worktree->path_prefix, parent_path) == -1) {
5146 err = got_error_from_errno("asprintf");
5147 goto done;
5152 err = got_object_open_as_commit(&base_commit, a->repo,
5153 a->worktree->base_commit_id);
5154 if (err)
5155 goto done;
5157 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5158 if (err) {
5159 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5160 (status == GOT_STATUS_ADD ||
5161 staged_status == GOT_STATUS_ADD)))
5162 goto done;
5163 } else {
5164 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5165 if (err)
5166 goto done;
5168 err = got_path_basename(&te_name, ie->path);
5169 if (err)
5170 goto done;
5172 te = got_object_tree_find_entry(tree, te_name);
5173 free(te_name);
5174 if (te == NULL && status != GOT_STATUS_ADD &&
5175 staged_status != GOT_STATUS_ADD) {
5176 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5177 goto done;
5181 switch (status) {
5182 case GOT_STATUS_ADD:
5183 if (a->patch_cb) {
5184 int choice = GOT_PATCH_CHOICE_NONE;
5185 err = (*a->patch_cb)(&choice, a->patch_arg,
5186 status, ie->path, NULL, 1, 1);
5187 if (err)
5188 goto done;
5189 if (choice != GOT_PATCH_CHOICE_YES)
5190 break;
5192 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5193 ie->path);
5194 if (err)
5195 goto done;
5196 got_fileindex_entry_remove(a->fileindex, ie);
5197 if (a->unlink_added_files) {
5198 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5200 if (a->added_files_to_unlink) {
5201 struct got_pathlist_entry *pe;
5203 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5204 entry) {
5205 if (got_path_cmp(pe->path, relpath,
5206 pe->path_len, strlen(relpath)))
5207 continue;
5208 do_unlink = 1;
5209 break;
5213 if (do_unlink) {
5214 if (asprintf(&ondisk_path, "%s/%s",
5215 got_worktree_get_root_path(a->worktree),
5216 relpath) == -1) {
5217 err = got_error_from_errno("asprintf");
5218 goto done;
5220 if (unlink(ondisk_path) == -1) {
5221 err = got_error_from_errno2("unlink",
5222 ondisk_path);
5223 break;
5227 break;
5228 case GOT_STATUS_DELETE:
5229 if (a->patch_cb) {
5230 int choice = GOT_PATCH_CHOICE_NONE;
5231 err = (*a->patch_cb)(&choice, a->patch_arg,
5232 status, ie->path, NULL, 1, 1);
5233 if (err)
5234 goto done;
5235 if (choice != GOT_PATCH_CHOICE_YES)
5236 break;
5238 /* fall through */
5239 case GOT_STATUS_MODIFY:
5240 case GOT_STATUS_MODE_CHANGE:
5241 case GOT_STATUS_CONFLICT:
5242 case GOT_STATUS_MISSING: {
5243 struct got_object_id id;
5244 if (staged_status == GOT_STATUS_ADD ||
5245 staged_status == GOT_STATUS_MODIFY)
5246 got_fileindex_entry_get_staged_blob_id(&id, ie);
5247 else
5248 got_fileindex_entry_get_blob_id(&id, ie);
5249 fd = got_opentempfd();
5250 if (fd == -1) {
5251 err = got_error_from_errno("got_opentempfd");
5252 goto done;
5255 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5256 if (err)
5257 goto done;
5259 if (asprintf(&ondisk_path, "%s/%s",
5260 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5261 err = got_error_from_errno("asprintf");
5262 goto done;
5265 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5266 status == GOT_STATUS_CONFLICT)) {
5267 int is_bad_symlink = 0;
5268 err = create_patched_content(&path_content, 1, &id,
5269 ondisk_path, dirfd, de_name, ie->path, a->repo,
5270 a->patch_cb, a->patch_arg);
5271 if (err || path_content == NULL)
5272 break;
5273 if (te && S_ISLNK(te->mode)) {
5274 if (unlink(path_content) == -1) {
5275 err = got_error_from_errno2("unlink",
5276 path_content);
5277 break;
5279 err = install_symlink(&is_bad_symlink,
5280 a->worktree, ondisk_path, ie->path,
5281 blob, 0, 1, 0, 0, a->repo,
5282 a->progress_cb, a->progress_arg);
5283 } else {
5284 if (rename(path_content, ondisk_path) == -1) {
5285 err = got_error_from_errno3("rename",
5286 path_content, ondisk_path);
5287 goto done;
5290 } else {
5291 int is_bad_symlink = 0;
5292 if (te && S_ISLNK(te->mode)) {
5293 err = install_symlink(&is_bad_symlink,
5294 a->worktree, ondisk_path, ie->path,
5295 blob, 0, 1, 0, 0, a->repo,
5296 a->progress_cb, a->progress_arg);
5297 } else {
5298 err = install_blob(a->worktree, ondisk_path,
5299 ie->path,
5300 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5301 got_fileindex_perms_to_st(ie), blob,
5302 0, 1, 0, 0, NULL, a->repo,
5303 a->progress_cb, a->progress_arg);
5305 if (err)
5306 goto done;
5307 if (status == GOT_STATUS_DELETE ||
5308 status == GOT_STATUS_MODE_CHANGE) {
5309 err = got_fileindex_entry_update(ie,
5310 a->worktree->root_fd, relpath,
5311 blob->id.sha1,
5312 a->worktree->base_commit_id->sha1, 1);
5313 if (err)
5314 goto done;
5316 if (is_bad_symlink) {
5317 got_fileindex_entry_filetype_set(ie,
5318 GOT_FILEIDX_MODE_BAD_SYMLINK);
5321 break;
5323 default:
5324 break;
5326 done:
5327 free(ondisk_path);
5328 free(path_content);
5329 free(parent_path);
5330 free(tree_path);
5331 if (fd != -1 && close(fd) == -1 && err == NULL)
5332 err = got_error_from_errno("close");
5333 if (blob)
5334 got_object_blob_close(blob);
5335 if (tree)
5336 got_object_tree_close(tree);
5337 free(tree_id);
5338 if (base_commit)
5339 got_object_commit_close(base_commit);
5340 return err;
5343 const struct got_error *
5344 got_worktree_revert(struct got_worktree *worktree,
5345 struct got_pathlist_head *paths,
5346 got_worktree_checkout_cb progress_cb, void *progress_arg,
5347 got_worktree_patch_cb patch_cb, void *patch_arg,
5348 struct got_repository *repo)
5350 struct got_fileindex *fileindex = NULL;
5351 char *fileindex_path = NULL;
5352 const struct got_error *err = NULL, *unlockerr = NULL;
5353 const struct got_error *sync_err = NULL;
5354 struct got_pathlist_entry *pe;
5355 struct revert_file_args rfa;
5357 err = lock_worktree(worktree, LOCK_EX);
5358 if (err)
5359 return err;
5361 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5362 if (err)
5363 goto done;
5365 rfa.worktree = worktree;
5366 rfa.fileindex = fileindex;
5367 rfa.progress_cb = progress_cb;
5368 rfa.progress_arg = progress_arg;
5369 rfa.patch_cb = patch_cb;
5370 rfa.patch_arg = patch_arg;
5371 rfa.repo = repo;
5372 rfa.unlink_added_files = 0;
5373 TAILQ_FOREACH(pe, paths, entry) {
5374 err = worktree_status(worktree, pe->path, fileindex, repo,
5375 revert_file, &rfa, NULL, NULL, 1, 0);
5376 if (err)
5377 break;
5379 sync_err = sync_fileindex(fileindex, fileindex_path);
5380 if (sync_err && err == NULL)
5381 err = sync_err;
5382 done:
5383 free(fileindex_path);
5384 if (fileindex)
5385 got_fileindex_free(fileindex);
5386 unlockerr = lock_worktree(worktree, LOCK_SH);
5387 if (unlockerr && err == NULL)
5388 err = unlockerr;
5389 return err;
5392 static void
5393 free_commitable(struct got_commitable *ct)
5395 free(ct->path);
5396 free(ct->in_repo_path);
5397 free(ct->ondisk_path);
5398 free(ct->blob_id);
5399 free(ct->base_blob_id);
5400 free(ct->staged_blob_id);
5401 free(ct->base_commit_id);
5402 free(ct);
5405 struct collect_commitables_arg {
5406 struct got_pathlist_head *commitable_paths;
5407 struct got_repository *repo;
5408 struct got_worktree *worktree;
5409 struct got_fileindex *fileindex;
5410 int have_staged_files;
5411 int allow_bad_symlinks;
5412 int diff_header_shown;
5413 int commit_conflicts;
5414 FILE *diff_outfile;
5415 FILE *f1;
5416 FILE *f2;
5420 * Create a file which contains the target path of a symlink so we can feed
5421 * it as content to the diff engine.
5423 static const struct got_error *
5424 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5425 const char *abspath)
5427 const struct got_error *err = NULL;
5428 char target_path[PATH_MAX];
5429 ssize_t target_len, outlen;
5431 *fd = -1;
5433 if (dirfd != -1) {
5434 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5435 if (target_len == -1)
5436 return got_error_from_errno2("readlinkat", abspath);
5437 } else {
5438 target_len = readlink(abspath, target_path, PATH_MAX);
5439 if (target_len == -1)
5440 return got_error_from_errno2("readlink", abspath);
5443 *fd = got_opentempfd();
5444 if (*fd == -1)
5445 return got_error_from_errno("got_opentempfd");
5447 outlen = write(*fd, target_path, target_len);
5448 if (outlen == -1) {
5449 err = got_error_from_errno("got_opentempfd");
5450 goto done;
5453 if (lseek(*fd, 0, SEEK_SET) == -1) {
5454 err = got_error_from_errno2("lseek", abspath);
5455 goto done;
5457 done:
5458 if (err) {
5459 close(*fd);
5460 *fd = -1;
5462 return err;
5465 static const struct got_error *
5466 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5467 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5468 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5470 const struct got_error *err = NULL;
5471 struct got_blob_object *blob1 = NULL;
5472 int fd = -1, fd1 = -1, fd2 = -1;
5473 FILE *ondisk_file = NULL;
5474 char *label1 = NULL;
5475 struct stat sb;
5476 off_t size1 = 0;
5477 int f2_exists = 0;
5478 char *id_str = NULL;
5480 memset(&sb, 0, sizeof(sb));
5482 if (diff_staged) {
5483 if (ct->staged_status != GOT_STATUS_MODIFY &&
5484 ct->staged_status != GOT_STATUS_ADD &&
5485 ct->staged_status != GOT_STATUS_DELETE)
5486 return NULL;
5487 } else {
5488 if (ct->status != GOT_STATUS_MODIFY &&
5489 ct->status != GOT_STATUS_ADD &&
5490 ct->status != GOT_STATUS_DELETE &&
5491 ct->status != GOT_STATUS_CONFLICT)
5492 return NULL;
5495 err = got_opentemp_truncate(f1);
5496 if (err)
5497 return got_error_from_errno("got_opentemp_truncate");
5498 err = got_opentemp_truncate(f2);
5499 if (err)
5500 return got_error_from_errno("got_opentemp_truncate");
5502 if (!*diff_header_shown) {
5503 err = got_object_id_str(&id_str, worktree->base_commit_id);
5504 if (err)
5505 return err;
5506 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5507 got_worktree_get_root_path(worktree));
5508 fprintf(diff_outfile, "commit - %s\n", id_str);
5509 fprintf(diff_outfile, "path + %s%s\n",
5510 got_worktree_get_root_path(worktree),
5511 diff_staged ? " (staged changes)" : "");
5512 *diff_header_shown = 1;
5515 if (diff_staged) {
5516 const char *label1 = NULL, *label2 = NULL;
5517 switch (ct->staged_status) {
5518 case GOT_STATUS_MODIFY:
5519 label1 = ct->path;
5520 label2 = ct->path;
5521 break;
5522 case GOT_STATUS_ADD:
5523 label2 = ct->path;
5524 break;
5525 case GOT_STATUS_DELETE:
5526 label1 = ct->path;
5527 break;
5528 default:
5529 return got_error(GOT_ERR_FILE_STATUS);
5531 fd1 = got_opentempfd();
5532 if (fd1 == -1) {
5533 err = got_error_from_errno("got_opentempfd");
5534 goto done;
5536 fd2 = got_opentempfd();
5537 if (fd2 == -1) {
5538 err = got_error_from_errno("got_opentempfd");
5539 goto done;
5541 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5542 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5543 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5544 NULL, repo, diff_outfile);
5545 goto done;
5548 fd1 = got_opentempfd();
5549 if (fd1 == -1) {
5550 err = got_error_from_errno("got_opentempfd");
5551 goto done;
5554 if (ct->status != GOT_STATUS_ADD) {
5555 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5556 8192, fd1);
5557 if (err)
5558 goto done;
5561 if (ct->status != GOT_STATUS_DELETE) {
5562 if (dirfd != -1) {
5563 fd = openat(dirfd, de_name,
5564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5565 if (fd == -1) {
5566 if (!got_err_open_nofollow_on_symlink()) {
5567 err = got_error_from_errno2("openat",
5568 ct->ondisk_path);
5569 goto done;
5571 err = get_symlink_target_file(&fd, dirfd,
5572 de_name, ct->ondisk_path);
5573 if (err)
5574 goto done;
5576 } else {
5577 fd = open(ct->ondisk_path,
5578 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5579 if (fd == -1) {
5580 if (!got_err_open_nofollow_on_symlink()) {
5581 err = got_error_from_errno2("open",
5582 ct->ondisk_path);
5583 goto done;
5585 err = get_symlink_target_file(&fd, dirfd,
5586 de_name, ct->ondisk_path);
5587 if (err)
5588 goto done;
5591 if (fstatat(fd, ct->ondisk_path, &sb,
5592 AT_SYMLINK_NOFOLLOW) == -1) {
5593 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5594 goto done;
5596 ondisk_file = fdopen(fd, "r");
5597 if (ondisk_file == NULL) {
5598 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5599 goto done;
5601 fd = -1;
5602 f2_exists = 1;
5605 if (blob1) {
5606 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5607 f1, blob1);
5608 if (err)
5609 goto done;
5612 err = got_diff_blob_file(blob1, f1, size1, label1,
5613 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5614 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5615 done:
5616 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5617 err = got_error_from_errno("close");
5618 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5619 err = got_error_from_errno("close");
5620 if (blob1)
5621 got_object_blob_close(blob1);
5622 if (fd != -1 && close(fd) == -1 && err == NULL)
5623 err = got_error_from_errno("close");
5624 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5625 err = got_error_from_errno("fclose");
5626 return err;
5629 static const struct got_error *
5630 collect_commitables(void *arg, unsigned char status,
5631 unsigned char staged_status, const char *relpath,
5632 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5633 struct got_object_id *commit_id, int dirfd, const char *de_name)
5635 struct collect_commitables_arg *a = arg;
5636 const struct got_error *err = NULL;
5637 struct got_commitable *ct = NULL;
5638 struct got_pathlist_entry *new = NULL;
5639 char *parent_path = NULL, *path = NULL;
5640 struct stat sb;
5642 if (a->have_staged_files) {
5643 if (staged_status != GOT_STATUS_MODIFY &&
5644 staged_status != GOT_STATUS_ADD &&
5645 staged_status != GOT_STATUS_DELETE)
5646 return NULL;
5647 } else {
5648 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5649 printf("C %s\n", relpath);
5650 return got_error(GOT_ERR_COMMIT_CONFLICT);
5653 if (status != GOT_STATUS_MODIFY &&
5654 status != GOT_STATUS_MODE_CHANGE &&
5655 status != GOT_STATUS_ADD &&
5656 status != GOT_STATUS_DELETE &&
5657 status != GOT_STATUS_CONFLICT)
5658 return NULL;
5661 if (asprintf(&path, "/%s", relpath) == -1) {
5662 err = got_error_from_errno("asprintf");
5663 goto done;
5665 if (strcmp(path, "/") == 0) {
5666 parent_path = strdup("");
5667 if (parent_path == NULL)
5668 return got_error_from_errno("strdup");
5669 } else {
5670 err = got_path_dirname(&parent_path, path);
5671 if (err)
5672 return err;
5675 ct = calloc(1, sizeof(*ct));
5676 if (ct == NULL) {
5677 err = got_error_from_errno("calloc");
5678 goto done;
5681 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5682 relpath) == -1) {
5683 err = got_error_from_errno("asprintf");
5684 goto done;
5687 if (staged_status == GOT_STATUS_ADD ||
5688 staged_status == GOT_STATUS_MODIFY) {
5689 struct got_fileindex_entry *ie;
5690 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5691 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5692 case GOT_FILEIDX_MODE_REGULAR_FILE:
5693 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5694 ct->mode = S_IFREG;
5695 break;
5696 case GOT_FILEIDX_MODE_SYMLINK:
5697 ct->mode = S_IFLNK;
5698 break;
5699 default:
5700 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5701 goto done;
5703 ct->mode |= got_fileindex_entry_perms_get(ie);
5704 } else if (status != GOT_STATUS_DELETE &&
5705 staged_status != GOT_STATUS_DELETE) {
5706 if (dirfd != -1) {
5707 if (fstatat(dirfd, de_name, &sb,
5708 AT_SYMLINK_NOFOLLOW) == -1) {
5709 err = got_error_from_errno2("fstatat",
5710 ct->ondisk_path);
5711 goto done;
5713 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5714 err = got_error_from_errno2("lstat", ct->ondisk_path);
5715 goto done;
5717 ct->mode = sb.st_mode;
5720 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5721 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5722 relpath) == -1) {
5723 err = got_error_from_errno("asprintf");
5724 goto done;
5727 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5728 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5729 int is_bad_symlink;
5730 char target_path[PATH_MAX];
5731 ssize_t target_len;
5732 target_len = readlink(ct->ondisk_path, target_path,
5733 sizeof(target_path));
5734 if (target_len == -1) {
5735 err = got_error_from_errno2("readlink",
5736 ct->ondisk_path);
5737 goto done;
5739 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5740 target_len, ct->ondisk_path, a->worktree->root_path,
5741 a->worktree->meta_dir);
5742 if (err)
5743 goto done;
5744 if (is_bad_symlink) {
5745 err = got_error_path(ct->ondisk_path,
5746 GOT_ERR_BAD_SYMLINK);
5747 goto done;
5752 ct->status = status;
5753 ct->staged_status = staged_status;
5754 ct->blob_id = NULL; /* will be filled in when blob gets created */
5755 if (ct->status != GOT_STATUS_ADD &&
5756 ct->staged_status != GOT_STATUS_ADD) {
5757 ct->base_blob_id = got_object_id_dup(blob_id);
5758 if (ct->base_blob_id == NULL) {
5759 err = got_error_from_errno("got_object_id_dup");
5760 goto done;
5762 ct->base_commit_id = got_object_id_dup(commit_id);
5763 if (ct->base_commit_id == NULL) {
5764 err = got_error_from_errno("got_object_id_dup");
5765 goto done;
5768 if (ct->staged_status == GOT_STATUS_ADD ||
5769 ct->staged_status == GOT_STATUS_MODIFY) {
5770 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5771 if (ct->staged_blob_id == NULL) {
5772 err = got_error_from_errno("got_object_id_dup");
5773 goto done;
5776 ct->path = strdup(path);
5777 if (ct->path == NULL) {
5778 err = got_error_from_errno("strdup");
5779 goto done;
5782 if (a->diff_outfile) {
5783 err = append_ct_diff(ct, &a->diff_header_shown,
5784 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5785 a->have_staged_files, a->repo, a->worktree);
5786 if (err)
5787 goto done;
5790 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5791 done:
5792 if (ct && (err || new == NULL))
5793 free_commitable(ct);
5794 free(parent_path);
5795 free(path);
5796 return err;
5799 static const struct got_error *write_tree(struct got_object_id **, int *,
5800 struct got_tree_object *, const char *, struct got_pathlist_head *,
5801 got_worktree_status_cb status_cb, void *status_arg,
5802 struct got_repository *);
5804 static const struct got_error *
5805 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5806 struct got_tree_entry *te, const char *parent_path,
5807 struct got_pathlist_head *commitable_paths,
5808 got_worktree_status_cb status_cb, void *status_arg,
5809 struct got_repository *repo)
5811 const struct got_error *err = NULL;
5812 struct got_tree_object *subtree;
5813 char *subpath;
5815 if (asprintf(&subpath, "%s%s%s", parent_path,
5816 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5817 return got_error_from_errno("asprintf");
5819 err = got_object_open_as_tree(&subtree, repo, &te->id);
5820 if (err)
5821 return err;
5823 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5824 commitable_paths, status_cb, status_arg, repo);
5825 got_object_tree_close(subtree);
5826 free(subpath);
5827 return err;
5830 static const struct got_error *
5831 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5833 const struct got_error *err = NULL;
5834 char *ct_parent_path = NULL;
5836 *match = 0;
5838 if (strchr(ct->in_repo_path, '/') == NULL) {
5839 *match = got_path_is_root_dir(path);
5840 return NULL;
5843 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5844 if (err)
5845 return err;
5846 *match = (strcmp(path, ct_parent_path) == 0);
5847 free(ct_parent_path);
5848 return err;
5851 static mode_t
5852 get_ct_file_mode(struct got_commitable *ct)
5854 if (S_ISLNK(ct->mode))
5855 return S_IFLNK;
5857 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5860 static const struct got_error *
5861 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5862 struct got_tree_entry *te, struct got_commitable *ct)
5864 const struct got_error *err = NULL;
5866 *new_te = NULL;
5868 err = got_object_tree_entry_dup(new_te, te);
5869 if (err)
5870 goto done;
5872 (*new_te)->mode = get_ct_file_mode(ct);
5874 if (ct->staged_status == GOT_STATUS_MODIFY)
5875 memcpy(&(*new_te)->id, ct->staged_blob_id,
5876 sizeof((*new_te)->id));
5877 else
5878 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5879 done:
5880 if (err && *new_te) {
5881 free(*new_te);
5882 *new_te = NULL;
5884 return err;
5887 static const struct got_error *
5888 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5889 struct got_commitable *ct)
5891 const struct got_error *err = NULL;
5892 char *ct_name = NULL;
5894 *new_te = NULL;
5896 *new_te = calloc(1, sizeof(**new_te));
5897 if (*new_te == NULL)
5898 return got_error_from_errno("calloc");
5900 err = got_path_basename(&ct_name, ct->path);
5901 if (err)
5902 goto done;
5903 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5904 sizeof((*new_te)->name)) {
5905 err = got_error(GOT_ERR_NO_SPACE);
5906 goto done;
5909 (*new_te)->mode = get_ct_file_mode(ct);
5911 if (ct->staged_status == GOT_STATUS_ADD)
5912 memcpy(&(*new_te)->id, ct->staged_blob_id,
5913 sizeof((*new_te)->id));
5914 else
5915 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5916 done:
5917 free(ct_name);
5918 if (err && *new_te) {
5919 free(*new_te);
5920 *new_te = NULL;
5922 return err;
5925 static const struct got_error *
5926 insert_tree_entry(struct got_tree_entry *new_te,
5927 struct got_pathlist_head *paths)
5929 const struct got_error *err = NULL;
5930 struct got_pathlist_entry *new_pe;
5932 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5933 if (err)
5934 return err;
5935 if (new_pe == NULL)
5936 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5937 return NULL;
5940 static const struct got_error *
5941 report_ct_status(struct got_commitable *ct,
5942 got_worktree_status_cb status_cb, void *status_arg)
5944 const char *ct_path = ct->path;
5945 unsigned char status;
5947 if (status_cb == NULL) /* no commit progress output desired */
5948 return NULL;
5950 while (ct_path[0] == '/')
5951 ct_path++;
5953 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5954 status = ct->staged_status;
5955 else
5956 status = ct->status;
5958 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5959 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5962 static const struct got_error *
5963 match_modified_subtree(int *modified, struct got_tree_entry *te,
5964 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5966 const struct got_error *err = NULL;
5967 struct got_pathlist_entry *pe;
5968 char *te_path;
5970 *modified = 0;
5972 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5973 got_path_is_root_dir(base_tree_path) ? "" : "/",
5974 te->name) == -1)
5975 return got_error_from_errno("asprintf");
5977 TAILQ_FOREACH(pe, commitable_paths, entry) {
5978 struct got_commitable *ct = pe->data;
5979 *modified = got_path_is_child(ct->in_repo_path, te_path,
5980 strlen(te_path));
5981 if (*modified)
5982 break;
5985 free(te_path);
5986 return err;
5989 static const struct got_error *
5990 match_deleted_or_modified_ct(struct got_commitable **ctp,
5991 struct got_tree_entry *te, const char *base_tree_path,
5992 struct got_pathlist_head *commitable_paths)
5994 const struct got_error *err = NULL;
5995 struct got_pathlist_entry *pe;
5997 *ctp = NULL;
5999 TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 struct got_commitable *ct = pe->data;
6001 char *ct_name = NULL;
6002 int path_matches;
6004 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6005 if (ct->status != GOT_STATUS_MODIFY &&
6006 ct->status != GOT_STATUS_MODE_CHANGE &&
6007 ct->status != GOT_STATUS_DELETE &&
6008 ct->status != GOT_STATUS_CONFLICT)
6009 continue;
6010 } else {
6011 if (ct->staged_status != GOT_STATUS_MODIFY &&
6012 ct->staged_status != GOT_STATUS_DELETE)
6013 continue;
6016 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6017 continue;
6019 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6020 if (err)
6021 return err;
6022 if (!path_matches)
6023 continue;
6025 err = got_path_basename(&ct_name, pe->path);
6026 if (err)
6027 return err;
6029 if (strcmp(te->name, ct_name) != 0) {
6030 free(ct_name);
6031 continue;
6033 free(ct_name);
6035 *ctp = ct;
6036 break;
6039 return err;
6042 static const struct got_error *
6043 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6044 const char *child_path, const char *path_base_tree,
6045 struct got_pathlist_head *commitable_paths,
6046 got_worktree_status_cb status_cb, void *status_arg,
6047 struct got_repository *repo)
6049 const struct got_error *err = NULL;
6050 struct got_tree_entry *new_te;
6051 char *subtree_path;
6052 struct got_object_id *id = NULL;
6053 int nentries;
6055 *new_tep = NULL;
6057 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6058 got_path_is_root_dir(path_base_tree) ? "" : "/",
6059 child_path) == -1)
6060 return got_error_from_errno("asprintf");
6062 new_te = calloc(1, sizeof(*new_te));
6063 if (new_te == NULL)
6064 return got_error_from_errno("calloc");
6065 new_te->mode = S_IFDIR;
6067 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6068 sizeof(new_te->name)) {
6069 err = got_error(GOT_ERR_NO_SPACE);
6070 goto done;
6072 err = write_tree(&id, &nentries, NULL, subtree_path,
6073 commitable_paths, status_cb, status_arg, repo);
6074 if (err) {
6075 free(new_te);
6076 goto done;
6078 memcpy(&new_te->id, id, sizeof(new_te->id));
6079 done:
6080 free(id);
6081 free(subtree_path);
6082 if (err == NULL)
6083 *new_tep = new_te;
6084 return err;
6087 static const struct got_error *
6088 write_tree(struct got_object_id **new_tree_id, int *nentries,
6089 struct got_tree_object *base_tree, const char *path_base_tree,
6090 struct got_pathlist_head *commitable_paths,
6091 got_worktree_status_cb status_cb, void *status_arg,
6092 struct got_repository *repo)
6094 const struct got_error *err = NULL;
6095 struct got_pathlist_head paths;
6096 struct got_tree_entry *te, *new_te = NULL;
6097 struct got_pathlist_entry *pe;
6099 TAILQ_INIT(&paths);
6100 *nentries = 0;
6102 /* Insert, and recurse into, newly added entries first. */
6103 TAILQ_FOREACH(pe, commitable_paths, entry) {
6104 struct got_commitable *ct = pe->data;
6105 char *child_path = NULL, *slash;
6107 if ((ct->status != GOT_STATUS_ADD &&
6108 ct->staged_status != GOT_STATUS_ADD) ||
6109 (ct->flags & GOT_COMMITABLE_ADDED))
6110 continue;
6112 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6113 strlen(path_base_tree)))
6114 continue;
6116 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6117 ct->in_repo_path);
6118 if (err)
6119 goto done;
6121 slash = strchr(child_path, '/');
6122 if (slash == NULL) {
6123 err = alloc_added_blob_tree_entry(&new_te, ct);
6124 if (err)
6125 goto done;
6126 err = report_ct_status(ct, status_cb, status_arg);
6127 if (err)
6128 goto done;
6129 ct->flags |= GOT_COMMITABLE_ADDED;
6130 err = insert_tree_entry(new_te, &paths);
6131 if (err)
6132 goto done;
6133 (*nentries)++;
6134 } else {
6135 *slash = '\0'; /* trim trailing path components */
6136 if (base_tree == NULL ||
6137 got_object_tree_find_entry(base_tree, child_path)
6138 == NULL) {
6139 err = make_subtree_for_added_blob(&new_te,
6140 child_path, path_base_tree,
6141 commitable_paths, status_cb, status_arg,
6142 repo);
6143 if (err)
6144 goto done;
6145 err = insert_tree_entry(new_te, &paths);
6146 if (err)
6147 goto done;
6148 (*nentries)++;
6153 if (base_tree) {
6154 int i, nbase_entries;
6155 /* Handle modified and deleted entries. */
6156 nbase_entries = got_object_tree_get_nentries(base_tree);
6157 for (i = 0; i < nbase_entries; i++) {
6158 struct got_commitable *ct = NULL;
6160 te = got_object_tree_get_entry(base_tree, i);
6161 if (got_object_tree_entry_is_submodule(te)) {
6162 /* Entry is a submodule; just copy it. */
6163 err = got_object_tree_entry_dup(&new_te, te);
6164 if (err)
6165 goto done;
6166 err = insert_tree_entry(new_te, &paths);
6167 if (err)
6168 goto done;
6169 (*nentries)++;
6170 continue;
6173 if (S_ISDIR(te->mode)) {
6174 int modified;
6175 err = got_object_tree_entry_dup(&new_te, te);
6176 if (err)
6177 goto done;
6178 err = match_modified_subtree(&modified, te,
6179 path_base_tree, commitable_paths);
6180 if (err)
6181 goto done;
6182 /* Avoid recursion into unmodified subtrees. */
6183 if (modified) {
6184 struct got_object_id *new_id;
6185 int nsubentries;
6186 err = write_subtree(&new_id,
6187 &nsubentries, te,
6188 path_base_tree, commitable_paths,
6189 status_cb, status_arg, repo);
6190 if (err)
6191 goto done;
6192 if (nsubentries == 0) {
6193 /* All entries were deleted. */
6194 free(new_id);
6195 continue;
6197 memcpy(&new_te->id, new_id,
6198 sizeof(new_te->id));
6199 free(new_id);
6201 err = insert_tree_entry(new_te, &paths);
6202 if (err)
6203 goto done;
6204 (*nentries)++;
6205 continue;
6208 err = match_deleted_or_modified_ct(&ct, te,
6209 path_base_tree, commitable_paths);
6210 if (err)
6211 goto done;
6212 if (ct) {
6213 /* NB: Deleted entries get dropped here. */
6214 if (ct->status == GOT_STATUS_MODIFY ||
6215 ct->status == GOT_STATUS_MODE_CHANGE ||
6216 ct->status == GOT_STATUS_CONFLICT ||
6217 ct->staged_status == GOT_STATUS_MODIFY) {
6218 err = alloc_modified_blob_tree_entry(
6219 &new_te, te, ct);
6220 if (err)
6221 goto done;
6222 err = insert_tree_entry(new_te, &paths);
6223 if (err)
6224 goto done;
6225 (*nentries)++;
6227 err = report_ct_status(ct, status_cb,
6228 status_arg);
6229 if (err)
6230 goto done;
6231 } else {
6232 /* Entry is unchanged; just copy it. */
6233 err = got_object_tree_entry_dup(&new_te, te);
6234 if (err)
6235 goto done;
6236 err = insert_tree_entry(new_te, &paths);
6237 if (err)
6238 goto done;
6239 (*nentries)++;
6244 /* Write new list of entries; deleted entries have been dropped. */
6245 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6246 done:
6247 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6248 return err;
6251 static const struct got_error *
6252 update_fileindex_after_commit(struct got_worktree *worktree,
6253 struct got_pathlist_head *commitable_paths,
6254 struct got_object_id *new_base_commit_id,
6255 struct got_fileindex *fileindex, int have_staged_files)
6257 const struct got_error *err = NULL;
6258 struct got_pathlist_entry *pe;
6259 char *relpath = NULL;
6261 TAILQ_FOREACH(pe, commitable_paths, entry) {
6262 struct got_fileindex_entry *ie;
6263 struct got_commitable *ct = pe->data;
6265 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6267 err = got_path_skip_common_ancestor(&relpath,
6268 worktree->root_path, ct->ondisk_path);
6269 if (err)
6270 goto done;
6272 if (ie) {
6273 if (ct->status == GOT_STATUS_DELETE ||
6274 ct->staged_status == GOT_STATUS_DELETE) {
6275 got_fileindex_entry_remove(fileindex, ie);
6276 } else if (ct->staged_status == GOT_STATUS_ADD ||
6277 ct->staged_status == GOT_STATUS_MODIFY) {
6278 got_fileindex_entry_stage_set(ie,
6279 GOT_FILEIDX_STAGE_NONE);
6280 got_fileindex_entry_staged_filetype_set(ie, 0);
6282 err = got_fileindex_entry_update(ie,
6283 worktree->root_fd, relpath,
6284 ct->staged_blob_id->sha1,
6285 new_base_commit_id->sha1,
6286 !have_staged_files);
6287 } else
6288 err = got_fileindex_entry_update(ie,
6289 worktree->root_fd, relpath,
6290 ct->blob_id->sha1,
6291 new_base_commit_id->sha1,
6292 !have_staged_files);
6293 } else {
6294 err = got_fileindex_entry_alloc(&ie, pe->path);
6295 if (err)
6296 goto done;
6297 err = got_fileindex_entry_update(ie,
6298 worktree->root_fd, relpath, ct->blob_id->sha1,
6299 new_base_commit_id->sha1, 1);
6300 if (err) {
6301 got_fileindex_entry_free(ie);
6302 goto done;
6304 err = got_fileindex_entry_add(fileindex, ie);
6305 if (err) {
6306 got_fileindex_entry_free(ie);
6307 goto done;
6310 free(relpath);
6311 relpath = NULL;
6313 done:
6314 free(relpath);
6315 return err;
6319 static const struct got_error *
6320 check_out_of_date(const char *in_repo_path, unsigned char status,
6321 unsigned char staged_status, struct got_object_id *base_blob_id,
6322 struct got_object_id *base_commit_id,
6323 struct got_object_id *head_commit_id, struct got_repository *repo,
6324 int ood_errcode)
6326 const struct got_error *err = NULL;
6327 struct got_commit_object *commit = NULL;
6328 struct got_object_id *id = NULL;
6330 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6331 /* Trivial case: base commit == head commit */
6332 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6333 return NULL;
6335 * Ensure file content which local changes were based
6336 * on matches file content in the branch head.
6338 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6339 if (err)
6340 goto done;
6341 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6342 if (err) {
6343 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6344 err = got_error(ood_errcode);
6345 goto done;
6346 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6347 err = got_error(ood_errcode);
6348 } else {
6349 /* Require that added files don't exist in the branch head. */
6350 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6351 if (err)
6352 goto done;
6353 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6354 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6355 goto done;
6356 err = id ? got_error(ood_errcode) : NULL;
6358 done:
6359 free(id);
6360 if (commit)
6361 got_object_commit_close(commit);
6362 return err;
6365 static const struct got_error *
6366 commit_worktree(struct got_object_id **new_commit_id,
6367 struct got_pathlist_head *commitable_paths,
6368 struct got_object_id *head_commit_id,
6369 struct got_object_id *parent_id2,
6370 struct got_worktree *worktree,
6371 const char *author, const char *committer, char *diff_path,
6372 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6373 got_worktree_status_cb status_cb, void *status_arg,
6374 struct got_repository *repo)
6376 const struct got_error *err = NULL, *unlockerr = NULL;
6377 struct got_pathlist_entry *pe;
6378 const char *head_ref_name = NULL;
6379 struct got_commit_object *head_commit = NULL;
6380 struct got_reference *head_ref2 = NULL;
6381 struct got_object_id *head_commit_id2 = NULL;
6382 struct got_tree_object *head_tree = NULL;
6383 struct got_object_id *new_tree_id = NULL;
6384 int nentries, nparents = 0;
6385 struct got_object_id_queue parent_ids;
6386 struct got_object_qid *pid = NULL;
6387 char *logmsg = NULL;
6388 time_t timestamp;
6390 *new_commit_id = NULL;
6392 STAILQ_INIT(&parent_ids);
6394 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6395 if (err)
6396 goto done;
6398 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6399 if (err)
6400 goto done;
6402 if (commit_msg_cb != NULL) {
6403 err = commit_msg_cb(commitable_paths, diff_path,
6404 &logmsg, commit_arg);
6405 if (err)
6406 goto done;
6409 if (logmsg == NULL || strlen(logmsg) == 0) {
6410 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6411 goto done;
6414 /* Create blobs from added and modified files and record their IDs. */
6415 TAILQ_FOREACH(pe, commitable_paths, entry) {
6416 struct got_commitable *ct = pe->data;
6417 char *ondisk_path;
6419 /* Blobs for staged files already exist. */
6420 if (ct->staged_status == GOT_STATUS_ADD ||
6421 ct->staged_status == GOT_STATUS_MODIFY)
6422 continue;
6424 if (ct->status != GOT_STATUS_ADD &&
6425 ct->status != GOT_STATUS_MODIFY &&
6426 ct->status != GOT_STATUS_MODE_CHANGE &&
6427 ct->status != GOT_STATUS_CONFLICT)
6428 continue;
6430 if (asprintf(&ondisk_path, "%s/%s",
6431 worktree->root_path, pe->path) == -1) {
6432 err = got_error_from_errno("asprintf");
6433 goto done;
6435 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6436 free(ondisk_path);
6437 if (err)
6438 goto done;
6441 /* Recursively write new tree objects. */
6442 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6443 commitable_paths, status_cb, status_arg, repo);
6444 if (err)
6445 goto done;
6447 err = got_object_qid_alloc(&pid, head_commit_id);
6448 if (err)
6449 goto done;
6450 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6451 nparents++;
6452 if (parent_id2) {
6453 err = got_object_qid_alloc(&pid, parent_id2);
6454 if (err)
6455 goto done;
6456 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6457 nparents++;
6459 timestamp = time(NULL);
6460 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6461 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6462 if (logmsg != NULL)
6463 free(logmsg);
6464 if (err)
6465 goto done;
6467 /* Check if a concurrent commit to our branch has occurred. */
6468 head_ref_name = got_worktree_get_head_ref_name(worktree);
6469 if (head_ref_name == NULL) {
6470 err = got_error_from_errno("got_worktree_get_head_ref_name");
6471 goto done;
6473 /* Lock the reference here to prevent concurrent modification. */
6474 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6475 if (err)
6476 goto done;
6477 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6478 if (err)
6479 goto done;
6480 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6481 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6482 goto done;
6484 /* Update branch head in repository. */
6485 err = got_ref_change_ref(head_ref2, *new_commit_id);
6486 if (err)
6487 goto done;
6488 err = got_ref_write(head_ref2, repo);
6489 if (err)
6490 goto done;
6492 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6493 if (err)
6494 goto done;
6496 err = ref_base_commit(worktree, repo);
6497 if (err)
6498 goto done;
6499 done:
6500 got_object_id_queue_free(&parent_ids);
6501 if (head_tree)
6502 got_object_tree_close(head_tree);
6503 if (head_commit)
6504 got_object_commit_close(head_commit);
6505 free(head_commit_id2);
6506 if (head_ref2) {
6507 unlockerr = got_ref_unlock(head_ref2);
6508 if (unlockerr && err == NULL)
6509 err = unlockerr;
6510 got_ref_close(head_ref2);
6512 return err;
6515 static const struct got_error *
6516 check_path_is_commitable(const char *path,
6517 struct got_pathlist_head *commitable_paths)
6519 struct got_pathlist_entry *cpe = NULL;
6520 size_t path_len = strlen(path);
6522 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6523 struct got_commitable *ct = cpe->data;
6524 const char *ct_path = ct->path;
6526 while (ct_path[0] == '/')
6527 ct_path++;
6529 if (strcmp(path, ct_path) == 0 ||
6530 got_path_is_child(ct_path, path, path_len))
6531 break;
6534 if (cpe == NULL)
6535 return got_error_path(path, GOT_ERR_BAD_PATH);
6537 return NULL;
6540 static const struct got_error *
6541 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6543 int *have_staged_files = arg;
6545 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6546 *have_staged_files = 1;
6547 return got_error(GOT_ERR_CANCELLED);
6550 return NULL;
6553 static const struct got_error *
6554 check_non_staged_files(struct got_fileindex *fileindex,
6555 struct got_pathlist_head *paths)
6557 struct got_pathlist_entry *pe;
6558 struct got_fileindex_entry *ie;
6560 TAILQ_FOREACH(pe, paths, entry) {
6561 if (pe->path[0] == '\0')
6562 continue;
6563 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6564 if (ie == NULL)
6565 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6566 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6567 return got_error_path(pe->path,
6568 GOT_ERR_FILE_NOT_STAGED);
6571 return NULL;
6574 const struct got_error *
6575 got_worktree_commit(struct got_object_id **new_commit_id,
6576 struct got_worktree *worktree, struct got_pathlist_head *paths,
6577 const char *author, const char *committer, int allow_bad_symlinks,
6578 int show_diff, int commit_conflicts,
6579 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6580 got_worktree_status_cb status_cb, void *status_arg,
6581 struct got_repository *repo)
6583 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6584 struct got_fileindex *fileindex = NULL;
6585 char *fileindex_path = NULL;
6586 struct got_pathlist_head commitable_paths;
6587 struct collect_commitables_arg cc_arg;
6588 struct got_pathlist_entry *pe;
6589 struct got_reference *head_ref = NULL;
6590 struct got_object_id *head_commit_id = NULL;
6591 char *diff_path = NULL;
6592 int have_staged_files = 0;
6594 *new_commit_id = NULL;
6596 memset(&cc_arg, 0, sizeof(cc_arg));
6597 TAILQ_INIT(&commitable_paths);
6599 err = lock_worktree(worktree, LOCK_EX);
6600 if (err)
6601 goto done;
6603 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6604 if (err)
6605 goto done;
6607 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6608 if (err)
6609 goto done;
6611 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6612 if (err)
6613 goto done;
6615 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6616 &have_staged_files);
6617 if (err && err->code != GOT_ERR_CANCELLED)
6618 goto done;
6619 if (have_staged_files) {
6620 err = check_non_staged_files(fileindex, paths);
6621 if (err)
6622 goto done;
6625 cc_arg.commitable_paths = &commitable_paths;
6626 cc_arg.worktree = worktree;
6627 cc_arg.fileindex = fileindex;
6628 cc_arg.repo = repo;
6629 cc_arg.have_staged_files = have_staged_files;
6630 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6631 cc_arg.diff_header_shown = 0;
6632 cc_arg.commit_conflicts = commit_conflicts;
6633 if (show_diff) {
6634 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6635 GOT_TMPDIR_STR "/got", ".diff");
6636 if (err)
6637 goto done;
6638 cc_arg.f1 = got_opentemp();
6639 if (cc_arg.f1 == NULL) {
6640 err = got_error_from_errno("got_opentemp");
6641 goto done;
6643 cc_arg.f2 = got_opentemp();
6644 if (cc_arg.f2 == NULL) {
6645 err = got_error_from_errno("got_opentemp");
6646 goto done;
6650 TAILQ_FOREACH(pe, paths, entry) {
6651 err = worktree_status(worktree, pe->path, fileindex, repo,
6652 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6653 if (err)
6654 goto done;
6657 if (show_diff) {
6658 if (fflush(cc_arg.diff_outfile) == EOF) {
6659 err = got_error_from_errno("fflush");
6660 goto done;
6664 if (TAILQ_EMPTY(&commitable_paths)) {
6665 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6666 goto done;
6669 TAILQ_FOREACH(pe, paths, entry) {
6670 err = check_path_is_commitable(pe->path, &commitable_paths);
6671 if (err)
6672 goto done;
6675 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6676 struct got_commitable *ct = pe->data;
6677 const char *ct_path = ct->in_repo_path;
6679 while (ct_path[0] == '/')
6680 ct_path++;
6681 err = check_out_of_date(ct_path, ct->status,
6682 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6683 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6684 if (err)
6685 goto done;
6689 err = commit_worktree(new_commit_id, &commitable_paths,
6690 head_commit_id, NULL, worktree, author, committer,
6691 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6692 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6693 if (err)
6694 goto done;
6696 err = update_fileindex_after_commit(worktree, &commitable_paths,
6697 *new_commit_id, fileindex, have_staged_files);
6698 sync_err = sync_fileindex(fileindex, fileindex_path);
6699 if (sync_err && err == NULL)
6700 err = sync_err;
6701 done:
6702 if (fileindex)
6703 got_fileindex_free(fileindex);
6704 free(fileindex_path);
6705 unlockerr = lock_worktree(worktree, LOCK_SH);
6706 if (unlockerr && err == NULL)
6707 err = unlockerr;
6708 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6709 struct got_commitable *ct = pe->data;
6711 free_commitable(ct);
6713 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6714 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6715 err = got_error_from_errno2("unlink", diff_path);
6716 free(diff_path);
6717 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6718 err == NULL)
6719 err = got_error_from_errno("fclose");
6720 return err;
6723 const char *
6724 got_commitable_get_path(struct got_commitable *ct)
6726 return ct->path;
6729 unsigned int
6730 got_commitable_get_status(struct got_commitable *ct)
6732 return ct->status;
6735 struct check_rebase_ok_arg {
6736 struct got_worktree *worktree;
6737 struct got_repository *repo;
6740 static const struct got_error *
6741 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6743 const struct got_error *err = NULL;
6744 struct check_rebase_ok_arg *a = arg;
6745 unsigned char status;
6746 struct stat sb;
6747 char *ondisk_path;
6749 /* Reject rebase of a work tree with mixed base commits. */
6750 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6751 SHA1_DIGEST_LENGTH))
6752 return got_error(GOT_ERR_MIXED_COMMITS);
6754 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6755 == -1)
6756 return got_error_from_errno("asprintf");
6758 /* Reject rebase of a work tree with modified or staged files. */
6759 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6760 free(ondisk_path);
6761 if (err)
6762 return err;
6764 if (status != GOT_STATUS_NO_CHANGE)
6765 return got_error(GOT_ERR_MODIFIED);
6766 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6767 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6769 return NULL;
6772 const struct got_error *
6773 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6774 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6775 struct got_worktree *worktree, struct got_reference *branch,
6776 struct got_repository *repo)
6778 const struct got_error *err = NULL;
6779 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6780 char *branch_ref_name = NULL;
6781 char *fileindex_path = NULL;
6782 struct check_rebase_ok_arg ok_arg;
6783 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6784 struct got_object_id *wt_branch_tip = NULL;
6786 *new_base_branch_ref = NULL;
6787 *tmp_branch = NULL;
6788 *fileindex = NULL;
6790 err = lock_worktree(worktree, LOCK_EX);
6791 if (err)
6792 return err;
6794 err = open_fileindex(fileindex, &fileindex_path, worktree);
6795 if (err)
6796 goto done;
6798 ok_arg.worktree = worktree;
6799 ok_arg.repo = repo;
6800 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6801 &ok_arg);
6802 if (err)
6803 goto done;
6805 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6806 if (err)
6807 goto done;
6809 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6810 if (err)
6811 goto done;
6813 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6814 if (err)
6815 goto done;
6817 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6818 0);
6819 if (err)
6820 goto done;
6822 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6823 if (err)
6824 goto done;
6825 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6826 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6827 goto done;
6830 err = got_ref_alloc_symref(new_base_branch_ref,
6831 new_base_branch_ref_name, wt_branch);
6832 if (err)
6833 goto done;
6834 err = got_ref_write(*new_base_branch_ref, repo);
6835 if (err)
6836 goto done;
6838 /* TODO Lock original branch's ref while rebasing? */
6840 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6841 if (err)
6842 goto done;
6844 err = got_ref_write(branch_ref, repo);
6845 if (err)
6846 goto done;
6848 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6849 worktree->base_commit_id);
6850 if (err)
6851 goto done;
6852 err = got_ref_write(*tmp_branch, repo);
6853 if (err)
6854 goto done;
6856 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6857 if (err)
6858 goto done;
6859 done:
6860 free(fileindex_path);
6861 free(tmp_branch_name);
6862 free(new_base_branch_ref_name);
6863 free(branch_ref_name);
6864 if (branch_ref)
6865 got_ref_close(branch_ref);
6866 if (wt_branch)
6867 got_ref_close(wt_branch);
6868 free(wt_branch_tip);
6869 if (err) {
6870 if (*new_base_branch_ref) {
6871 got_ref_close(*new_base_branch_ref);
6872 *new_base_branch_ref = NULL;
6874 if (*tmp_branch) {
6875 got_ref_close(*tmp_branch);
6876 *tmp_branch = NULL;
6878 if (*fileindex) {
6879 got_fileindex_free(*fileindex);
6880 *fileindex = NULL;
6882 lock_worktree(worktree, LOCK_SH);
6884 return err;
6887 const struct got_error *
6888 got_worktree_rebase_continue(struct got_object_id **commit_id,
6889 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6890 struct got_reference **branch, struct got_fileindex **fileindex,
6891 struct got_worktree *worktree, struct got_repository *repo)
6893 const struct got_error *err;
6894 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6895 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6896 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6897 char *fileindex_path = NULL;
6898 int have_staged_files = 0;
6900 *commit_id = NULL;
6901 *new_base_branch = NULL;
6902 *tmp_branch = NULL;
6903 *branch = NULL;
6904 *fileindex = NULL;
6906 err = lock_worktree(worktree, LOCK_EX);
6907 if (err)
6908 return err;
6910 err = open_fileindex(fileindex, &fileindex_path, worktree);
6911 if (err)
6912 goto done;
6914 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6915 &have_staged_files);
6916 if (err && err->code != GOT_ERR_CANCELLED)
6917 goto done;
6918 if (have_staged_files) {
6919 err = got_error(GOT_ERR_STAGED_PATHS);
6920 goto done;
6923 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6924 if (err)
6925 goto done;
6927 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6928 if (err)
6929 goto done;
6931 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6932 if (err)
6933 goto done;
6935 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6936 if (err)
6937 goto done;
6939 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6940 if (err)
6941 goto done;
6943 err = got_ref_open(branch, repo,
6944 got_ref_get_symref_target(branch_ref), 0);
6945 if (err)
6946 goto done;
6948 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6949 if (err)
6950 goto done;
6952 err = got_ref_resolve(commit_id, repo, commit_ref);
6953 if (err)
6954 goto done;
6956 err = got_ref_open(new_base_branch, repo,
6957 new_base_branch_ref_name, 0);
6958 if (err)
6959 goto done;
6961 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6962 if (err)
6963 goto done;
6964 done:
6965 free(commit_ref_name);
6966 free(branch_ref_name);
6967 free(fileindex_path);
6968 if (commit_ref)
6969 got_ref_close(commit_ref);
6970 if (branch_ref)
6971 got_ref_close(branch_ref);
6972 if (err) {
6973 free(*commit_id);
6974 *commit_id = NULL;
6975 if (*tmp_branch) {
6976 got_ref_close(*tmp_branch);
6977 *tmp_branch = NULL;
6979 if (*new_base_branch) {
6980 got_ref_close(*new_base_branch);
6981 *new_base_branch = NULL;
6983 if (*branch) {
6984 got_ref_close(*branch);
6985 *branch = NULL;
6987 if (*fileindex) {
6988 got_fileindex_free(*fileindex);
6989 *fileindex = NULL;
6991 lock_worktree(worktree, LOCK_SH);
6993 return err;
6996 const struct got_error *
6997 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6999 const struct got_error *err;
7000 char *tmp_branch_name = NULL;
7002 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7003 if (err)
7004 return err;
7006 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7007 free(tmp_branch_name);
7008 return NULL;
7011 const struct got_error *
7012 got_worktree_rebase_info(char **new_base_branch_name, char **branch_name,
7013 struct got_worktree *worktree, struct got_repository *repo)
7015 const struct got_error *err;
7016 char *new_base_branch_ref_name = NULL;
7017 char *branch_ref_name = NULL;
7018 struct got_reference *branch_ref = NULL, *branch = NULL;
7019 struct got_reference *new_base_branch = NULL;
7021 *new_base_branch_name = NULL;
7022 *branch_name = NULL;
7024 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7025 if (err)
7026 goto done;
7028 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7029 if (err)
7030 goto done;
7032 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
7033 if (err)
7034 goto done;
7036 err = got_ref_open(&branch, repo,
7037 got_ref_get_symref_target(branch_ref), 0);
7038 if (err)
7039 goto done;
7041 err = got_ref_open(&new_base_branch, repo,
7042 new_base_branch_ref_name, 0);
7043 if (err)
7044 goto done;
7046 if (!got_ref_is_symbolic(new_base_branch)) {
7047 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7048 "%s is not a symbolic reference",
7049 got_ref_get_name(branch_ref));
7050 goto done;
7053 *new_base_branch_name = strdup(got_ref_get_symref_target(
7054 new_base_branch));
7055 if (*new_base_branch_name == NULL) {
7056 err = got_error_from_errno("strdup");
7057 goto done;
7059 *branch_name = strdup(got_ref_get_name(branch));
7060 if (*branch_name == NULL) {
7061 err = got_error_from_errno("strdup");
7062 goto done;
7064 done:
7065 free(branch_ref_name);
7066 if (branch_ref)
7067 got_ref_close(branch_ref);
7068 if (new_base_branch)
7069 got_ref_close(new_base_branch);
7070 if (branch)
7071 got_ref_close(branch);
7072 return err;
7075 static const struct got_error *
7076 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7077 const char *diff_path, char **logmsg, void *arg)
7079 *logmsg = arg;
7080 return NULL;
7083 static const struct got_error *
7084 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7085 const char *path, struct got_object_id *blob_id,
7086 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7087 int dirfd, const char *de_name)
7089 return NULL;
7092 struct collect_merged_paths_arg {
7093 got_worktree_checkout_cb progress_cb;
7094 void *progress_arg;
7095 struct got_pathlist_head *merged_paths;
7098 static const struct got_error *
7099 collect_merged_paths(void *arg, unsigned char status, const char *path)
7101 const struct got_error *err;
7102 struct collect_merged_paths_arg *a = arg;
7103 char *p;
7104 struct got_pathlist_entry *new;
7106 err = (*a->progress_cb)(a->progress_arg, status, path);
7107 if (err)
7108 return err;
7110 if (status != GOT_STATUS_MERGE &&
7111 status != GOT_STATUS_ADD &&
7112 status != GOT_STATUS_DELETE &&
7113 status != GOT_STATUS_CONFLICT)
7114 return NULL;
7116 p = strdup(path);
7117 if (p == NULL)
7118 return got_error_from_errno("strdup");
7120 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7121 if (err || new == NULL)
7122 free(p);
7123 return err;
7126 static const struct got_error *
7127 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7128 int is_rebase, struct got_repository *repo)
7130 const struct got_error *err;
7131 struct got_reference *commit_ref = NULL;
7133 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7134 if (err) {
7135 if (err->code != GOT_ERR_NOT_REF)
7136 goto done;
7137 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7138 if (err)
7139 goto done;
7140 err = got_ref_write(commit_ref, repo);
7141 if (err)
7142 goto done;
7143 } else if (is_rebase) {
7144 struct got_object_id *stored_id;
7145 int cmp;
7147 err = got_ref_resolve(&stored_id, repo, commit_ref);
7148 if (err)
7149 goto done;
7150 cmp = got_object_id_cmp(commit_id, stored_id);
7151 free(stored_id);
7152 if (cmp != 0) {
7153 err = got_error(GOT_ERR_REBASE_COMMITID);
7154 goto done;
7157 done:
7158 if (commit_ref)
7159 got_ref_close(commit_ref);
7160 return err;
7163 static const struct got_error *
7164 rebase_merge_files(struct got_pathlist_head *merged_paths,
7165 const char *commit_ref_name, struct got_worktree *worktree,
7166 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7167 struct got_object_id *commit_id, struct got_repository *repo,
7168 got_worktree_checkout_cb progress_cb, void *progress_arg,
7169 got_cancel_cb cancel_cb, void *cancel_arg)
7171 const struct got_error *err;
7172 struct got_reference *commit_ref = NULL;
7173 struct collect_merged_paths_arg cmp_arg;
7174 char *fileindex_path;
7176 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7178 err = get_fileindex_path(&fileindex_path, worktree);
7179 if (err)
7180 return err;
7182 cmp_arg.progress_cb = progress_cb;
7183 cmp_arg.progress_arg = progress_arg;
7184 cmp_arg.merged_paths = merged_paths;
7185 err = merge_files(worktree, fileindex, fileindex_path,
7186 parent_commit_id, commit_id, repo, collect_merged_paths,
7187 &cmp_arg, cancel_cb, cancel_arg);
7188 if (commit_ref)
7189 got_ref_close(commit_ref);
7190 return err;
7193 const struct got_error *
7194 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7195 struct got_worktree *worktree, struct got_fileindex *fileindex,
7196 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7197 struct got_repository *repo,
7198 got_worktree_checkout_cb progress_cb, void *progress_arg,
7199 got_cancel_cb cancel_cb, void *cancel_arg)
7201 const struct got_error *err;
7202 char *commit_ref_name;
7204 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7205 if (err)
7206 return err;
7208 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7209 if (err)
7210 goto done;
7212 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7213 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7214 progress_arg, cancel_cb, cancel_arg);
7215 done:
7216 free(commit_ref_name);
7217 return err;
7220 const struct got_error *
7221 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7222 struct got_worktree *worktree, struct got_fileindex *fileindex,
7223 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7224 struct got_repository *repo,
7225 got_worktree_checkout_cb progress_cb, void *progress_arg,
7226 got_cancel_cb cancel_cb, void *cancel_arg)
7228 const struct got_error *err;
7229 char *commit_ref_name;
7231 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7232 if (err)
7233 return err;
7235 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7236 if (err)
7237 goto done;
7239 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7240 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7241 progress_arg, cancel_cb, cancel_arg);
7242 done:
7243 free(commit_ref_name);
7244 return err;
7247 static const struct got_error *
7248 rebase_commit(struct got_object_id **new_commit_id,
7249 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7250 struct got_worktree *worktree, struct got_fileindex *fileindex,
7251 struct got_reference *tmp_branch, const char *committer,
7252 struct got_commit_object *orig_commit, const char *new_logmsg,
7253 int allow_conflict, struct got_repository *repo)
7255 const struct got_error *err, *sync_err;
7256 struct got_pathlist_head commitable_paths;
7257 struct collect_commitables_arg cc_arg;
7258 char *fileindex_path = NULL;
7259 struct got_reference *head_ref = NULL;
7260 struct got_object_id *head_commit_id = NULL;
7261 char *logmsg = NULL;
7263 memset(&cc_arg, 0, sizeof(cc_arg));
7264 TAILQ_INIT(&commitable_paths);
7265 *new_commit_id = NULL;
7267 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7269 err = get_fileindex_path(&fileindex_path, worktree);
7270 if (err)
7271 return err;
7273 cc_arg.commitable_paths = &commitable_paths;
7274 cc_arg.worktree = worktree;
7275 cc_arg.repo = repo;
7276 cc_arg.have_staged_files = 0;
7277 cc_arg.commit_conflicts = allow_conflict;
7279 * If possible get the status of individual files directly to
7280 * avoid crawling the entire work tree once per rebased commit.
7282 * Ideally, merged_paths would contain a list of commitables
7283 * we could use so we could skip worktree_status() entirely.
7284 * However, we would then need carefully keep track of cumulative
7285 * effects of operations such as file additions and deletions
7286 * in 'got histedit -f' (folding multiple commits into one),
7287 * and this extra complexity is not really worth it.
7289 if (merged_paths) {
7290 struct got_pathlist_entry *pe;
7291 TAILQ_FOREACH(pe, merged_paths, entry) {
7292 err = worktree_status(worktree, pe->path, fileindex,
7293 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7294 0);
7295 if (err)
7296 goto done;
7298 } else {
7299 err = worktree_status(worktree, "", fileindex, repo,
7300 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7301 if (err)
7302 goto done;
7305 if (TAILQ_EMPTY(&commitable_paths)) {
7306 /* No-op change; commit will be elided. */
7307 err = got_ref_delete(commit_ref, repo);
7308 if (err)
7309 goto done;
7310 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7311 goto done;
7314 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7315 if (err)
7316 goto done;
7318 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7319 if (err)
7320 goto done;
7322 if (new_logmsg) {
7323 logmsg = strdup(new_logmsg);
7324 if (logmsg == NULL) {
7325 err = got_error_from_errno("strdup");
7326 goto done;
7328 } else {
7329 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7330 if (err)
7331 goto done;
7334 /* NB: commit_worktree will call free(logmsg) */
7335 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7336 NULL, worktree, got_object_commit_get_author(orig_commit),
7337 committer ? committer :
7338 got_object_commit_get_committer(orig_commit), NULL,
7339 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7340 if (err)
7341 goto done;
7343 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7344 if (err)
7345 goto done;
7347 err = got_ref_delete(commit_ref, repo);
7348 if (err)
7349 goto done;
7351 err = update_fileindex_after_commit(worktree, &commitable_paths,
7352 *new_commit_id, fileindex, 0);
7353 sync_err = sync_fileindex(fileindex, fileindex_path);
7354 if (sync_err && err == NULL)
7355 err = sync_err;
7356 done:
7357 free(fileindex_path);
7358 free(head_commit_id);
7359 if (head_ref)
7360 got_ref_close(head_ref);
7361 if (err) {
7362 free(*new_commit_id);
7363 *new_commit_id = NULL;
7365 return err;
7368 const struct got_error *
7369 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7370 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7371 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7372 const char *committer, struct got_commit_object *orig_commit,
7373 struct got_object_id *orig_commit_id, int allow_conflict,
7374 struct got_repository *repo)
7376 const struct got_error *err;
7377 char *commit_ref_name;
7378 struct got_reference *commit_ref = NULL;
7379 struct got_object_id *commit_id = NULL;
7381 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7382 if (err)
7383 return err;
7385 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7386 if (err)
7387 goto done;
7388 err = got_ref_resolve(&commit_id, repo, commit_ref);
7389 if (err)
7390 goto done;
7391 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7392 err = got_error(GOT_ERR_REBASE_COMMITID);
7393 goto done;
7396 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7397 worktree, fileindex, tmp_branch, committer, orig_commit,
7398 NULL, allow_conflict, repo);
7399 done:
7400 if (commit_ref)
7401 got_ref_close(commit_ref);
7402 free(commit_ref_name);
7403 free(commit_id);
7404 return err;
7407 const struct got_error *
7408 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7409 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7410 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7411 const char *committer, struct got_commit_object *orig_commit,
7412 struct got_object_id *orig_commit_id, const char *new_logmsg,
7413 int allow_conflict, struct got_repository *repo)
7415 const struct got_error *err;
7416 char *commit_ref_name;
7417 struct got_reference *commit_ref = NULL;
7419 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7420 if (err)
7421 return err;
7423 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7424 if (err)
7425 goto done;
7427 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7428 worktree, fileindex, tmp_branch, committer, orig_commit,
7429 new_logmsg, allow_conflict, repo);
7430 done:
7431 if (commit_ref)
7432 got_ref_close(commit_ref);
7433 free(commit_ref_name);
7434 return err;
7437 const struct got_error *
7438 got_worktree_rebase_postpone(struct got_worktree *worktree,
7439 struct got_fileindex *fileindex)
7441 if (fileindex)
7442 got_fileindex_free(fileindex);
7443 return lock_worktree(worktree, LOCK_SH);
7446 static const struct got_error *
7447 delete_ref(const char *name, struct got_repository *repo)
7449 const struct got_error *err;
7450 struct got_reference *ref;
7452 err = got_ref_open(&ref, repo, name, 0);
7453 if (err) {
7454 if (err->code == GOT_ERR_NOT_REF)
7455 return NULL;
7456 return err;
7459 err = got_ref_delete(ref, repo);
7460 got_ref_close(ref);
7461 return err;
7464 static const struct got_error *
7465 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7467 const struct got_error *err;
7468 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7469 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7471 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7472 if (err)
7473 goto done;
7474 err = delete_ref(tmp_branch_name, repo);
7475 if (err)
7476 goto done;
7478 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7479 if (err)
7480 goto done;
7481 err = delete_ref(new_base_branch_ref_name, repo);
7482 if (err)
7483 goto done;
7485 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7486 if (err)
7487 goto done;
7488 err = delete_ref(branch_ref_name, repo);
7489 if (err)
7490 goto done;
7492 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7493 if (err)
7494 goto done;
7495 err = delete_ref(commit_ref_name, repo);
7496 if (err)
7497 goto done;
7499 done:
7500 free(tmp_branch_name);
7501 free(new_base_branch_ref_name);
7502 free(branch_ref_name);
7503 free(commit_ref_name);
7504 return err;
7507 static const struct got_error *
7508 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7509 struct got_object_id *new_commit_id, struct got_repository *repo)
7511 const struct got_error *err;
7512 struct got_reference *ref = NULL;
7513 struct got_object_id *old_commit_id = NULL;
7514 const char *branch_name = NULL;
7515 char *new_id_str = NULL;
7516 char *refname = NULL;
7518 branch_name = got_ref_get_name(branch);
7519 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7520 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7521 branch_name += 11;
7523 err = got_object_id_str(&new_id_str, new_commit_id);
7524 if (err)
7525 return err;
7527 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7528 new_id_str) == -1) {
7529 err = got_error_from_errno("asprintf");
7530 goto done;
7533 err = got_ref_resolve(&old_commit_id, repo, branch);
7534 if (err)
7535 goto done;
7537 err = got_ref_alloc(&ref, refname, old_commit_id);
7538 if (err)
7539 goto done;
7541 err = got_ref_write(ref, repo);
7542 done:
7543 free(new_id_str);
7544 free(refname);
7545 free(old_commit_id);
7546 if (ref)
7547 got_ref_close(ref);
7548 return err;
7551 const struct got_error *
7552 got_worktree_rebase_complete(struct got_worktree *worktree,
7553 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7554 struct got_reference *rebased_branch, struct got_repository *repo,
7555 int create_backup)
7557 const struct got_error *err, *unlockerr, *sync_err;
7558 struct got_object_id *new_head_commit_id = NULL;
7559 char *fileindex_path = NULL;
7561 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7562 if (err)
7563 return err;
7565 if (create_backup) {
7566 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7567 rebased_branch, new_head_commit_id, repo);
7568 if (err)
7569 goto done;
7572 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7573 if (err)
7574 goto done;
7576 err = got_ref_write(rebased_branch, repo);
7577 if (err)
7578 goto done;
7580 err = got_worktree_set_head_ref(worktree, rebased_branch);
7581 if (err)
7582 goto done;
7584 err = delete_rebase_refs(worktree, repo);
7585 if (err)
7586 goto done;
7588 err = get_fileindex_path(&fileindex_path, worktree);
7589 if (err)
7590 goto done;
7591 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7592 sync_err = sync_fileindex(fileindex, fileindex_path);
7593 if (sync_err && err == NULL)
7594 err = sync_err;
7595 done:
7596 got_fileindex_free(fileindex);
7597 free(fileindex_path);
7598 free(new_head_commit_id);
7599 unlockerr = lock_worktree(worktree, LOCK_SH);
7600 if (unlockerr && err == NULL)
7601 err = unlockerr;
7602 return err;
7605 static const struct got_error *
7606 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7607 struct got_object_id *id1, struct got_object_id *id2,
7608 struct got_repository *repo)
7610 const struct got_error *err;
7611 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7612 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7614 if (id1) {
7615 err = got_object_open_as_commit(&commit1, repo, id1);
7616 if (err)
7617 goto done;
7619 err = got_object_open_as_tree(&tree1, repo,
7620 got_object_commit_get_tree_id(commit1));
7621 if (err)
7622 goto done;
7625 if (id2) {
7626 err = got_object_open_as_commit(&commit2, repo, id2);
7627 if (err)
7628 goto done;
7630 err = got_object_open_as_tree(&tree2, repo,
7631 got_object_commit_get_tree_id(commit2));
7632 if (err)
7633 goto done;
7636 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7637 got_diff_tree_collect_changed_paths, paths, 0);
7638 if (err)
7639 goto done;
7640 done:
7641 if (commit1)
7642 got_object_commit_close(commit1);
7643 if (commit2)
7644 got_object_commit_close(commit2);
7645 if (tree1)
7646 got_object_tree_close(tree1);
7647 if (tree2)
7648 got_object_tree_close(tree2);
7649 return err;
7652 static const struct got_error *
7653 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7654 struct got_object_id *id1, struct got_object_id *id2,
7655 const char *path_prefix, struct got_repository *repo)
7657 const struct got_error *err;
7658 struct got_pathlist_head merged_paths;
7659 struct got_pathlist_entry *pe;
7660 char *abspath = NULL, *wt_path = NULL;
7662 TAILQ_INIT(&merged_paths);
7664 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7665 if (err)
7666 goto done;
7668 TAILQ_FOREACH(pe, &merged_paths, entry) {
7669 struct got_diff_changed_path *change = pe->data;
7671 if (change->status != GOT_STATUS_ADD)
7672 continue;
7674 if (got_path_is_root_dir(path_prefix)) {
7675 wt_path = strdup(pe->path);
7676 if (wt_path == NULL) {
7677 err = got_error_from_errno("strdup");
7678 goto done;
7680 } else {
7681 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7682 err = got_error_from_errno("asprintf");
7683 goto done;
7686 err = got_path_skip_common_ancestor(&wt_path,
7687 path_prefix, abspath);
7688 if (err)
7689 goto done;
7690 free(abspath);
7691 abspath = NULL;
7694 err = got_pathlist_append(added_paths, wt_path, NULL);
7695 if (err)
7696 goto done;
7697 wt_path = NULL;
7700 done:
7701 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7702 free(abspath);
7703 free(wt_path);
7704 return err;
7707 static const struct got_error *
7708 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7709 struct got_object_id *id, const char *path_prefix,
7710 struct got_repository *repo)
7712 const struct got_error *err;
7713 struct got_commit_object *commit = NULL;
7714 struct got_object_qid *pid;
7716 err = got_object_open_as_commit(&commit, repo, id);
7717 if (err)
7718 goto done;
7720 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7722 err = get_paths_added_between_commits(added_paths,
7723 pid ? &pid->id : NULL, id, path_prefix, repo);
7724 if (err)
7725 goto done;
7726 done:
7727 if (commit)
7728 got_object_commit_close(commit);
7729 return err;
7732 const struct got_error *
7733 got_worktree_rebase_abort(struct got_worktree *worktree,
7734 struct got_fileindex *fileindex, struct got_repository *repo,
7735 struct got_reference *new_base_branch,
7736 got_worktree_checkout_cb progress_cb, void *progress_arg)
7738 const struct got_error *err, *unlockerr, *sync_err;
7739 struct got_reference *resolved = NULL;
7740 struct got_object_id *commit_id = NULL;
7741 struct got_object_id *merged_commit_id = NULL;
7742 struct got_commit_object *commit = NULL;
7743 char *fileindex_path = NULL;
7744 char *commit_ref_name = NULL;
7745 struct got_reference *commit_ref = NULL;
7746 struct revert_file_args rfa;
7747 struct got_object_id *tree_id = NULL;
7748 struct got_pathlist_head added_paths;
7750 TAILQ_INIT(&added_paths);
7752 err = lock_worktree(worktree, LOCK_EX);
7753 if (err)
7754 return err;
7756 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7757 if (err)
7758 goto done;
7760 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7761 if (err)
7762 goto done;
7764 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7765 if (err)
7766 goto done;
7769 * Determine which files in added status can be safely removed
7770 * from disk while reverting changes in the work tree.
7771 * We want to avoid deleting unrelated files which were added by
7772 * the user for conflict resolution purposes.
7774 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7775 got_worktree_get_path_prefix(worktree), repo);
7776 if (err)
7777 goto done;
7779 err = got_ref_open(&resolved, repo,
7780 got_ref_get_symref_target(new_base_branch), 0);
7781 if (err)
7782 goto done;
7784 err = got_worktree_set_head_ref(worktree, resolved);
7785 if (err)
7786 goto done;
7789 * XXX commits to the base branch could have happened while
7790 * we were busy rebasing; should we store the original commit ID
7791 * when rebase begins and read it back here?
7793 err = got_ref_resolve(&commit_id, repo, resolved);
7794 if (err)
7795 goto done;
7797 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7798 if (err)
7799 goto done;
7801 err = got_object_open_as_commit(&commit, repo,
7802 worktree->base_commit_id);
7803 if (err)
7804 goto done;
7806 err = got_object_id_by_path(&tree_id, repo, commit,
7807 worktree->path_prefix);
7808 if (err)
7809 goto done;
7811 err = delete_rebase_refs(worktree, repo);
7812 if (err)
7813 goto done;
7815 err = get_fileindex_path(&fileindex_path, worktree);
7816 if (err)
7817 goto done;
7819 rfa.worktree = worktree;
7820 rfa.fileindex = fileindex;
7821 rfa.progress_cb = progress_cb;
7822 rfa.progress_arg = progress_arg;
7823 rfa.patch_cb = NULL;
7824 rfa.patch_arg = NULL;
7825 rfa.repo = repo;
7826 rfa.unlink_added_files = 1;
7827 rfa.added_files_to_unlink = &added_paths;
7828 err = worktree_status(worktree, "", fileindex, repo,
7829 revert_file, &rfa, NULL, NULL, 1, 0);
7830 if (err)
7831 goto sync;
7833 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7834 repo, progress_cb, progress_arg, NULL, NULL);
7835 sync:
7836 sync_err = sync_fileindex(fileindex, fileindex_path);
7837 if (sync_err && err == NULL)
7838 err = sync_err;
7839 done:
7840 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7841 got_ref_close(resolved);
7842 free(tree_id);
7843 free(commit_id);
7844 free(merged_commit_id);
7845 if (commit)
7846 got_object_commit_close(commit);
7847 if (fileindex)
7848 got_fileindex_free(fileindex);
7849 free(fileindex_path);
7850 free(commit_ref_name);
7851 if (commit_ref)
7852 got_ref_close(commit_ref);
7854 unlockerr = lock_worktree(worktree, LOCK_SH);
7855 if (unlockerr && err == NULL)
7856 err = unlockerr;
7857 return err;
7860 const struct got_error *
7861 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7862 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7863 struct got_fileindex **fileindex, struct got_worktree *worktree,
7864 struct got_repository *repo)
7866 const struct got_error *err = NULL;
7867 char *tmp_branch_name = NULL;
7868 char *branch_ref_name = NULL;
7869 char *base_commit_ref_name = NULL;
7870 char *fileindex_path = NULL;
7871 struct check_rebase_ok_arg ok_arg;
7872 struct got_reference *wt_branch = NULL;
7873 struct got_reference *base_commit_ref = NULL;
7875 *tmp_branch = NULL;
7876 *branch_ref = NULL;
7877 *base_commit_id = NULL;
7878 *fileindex = NULL;
7880 err = lock_worktree(worktree, LOCK_EX);
7881 if (err)
7882 return err;
7884 err = open_fileindex(fileindex, &fileindex_path, worktree);
7885 if (err)
7886 goto done;
7888 ok_arg.worktree = worktree;
7889 ok_arg.repo = repo;
7890 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7891 &ok_arg);
7892 if (err)
7893 goto done;
7895 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7896 if (err)
7897 goto done;
7899 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7900 if (err)
7901 goto done;
7903 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7904 worktree);
7905 if (err)
7906 goto done;
7908 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7909 0);
7910 if (err)
7911 goto done;
7913 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7914 if (err)
7915 goto done;
7917 err = got_ref_write(*branch_ref, repo);
7918 if (err)
7919 goto done;
7921 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7922 worktree->base_commit_id);
7923 if (err)
7924 goto done;
7925 err = got_ref_write(base_commit_ref, repo);
7926 if (err)
7927 goto done;
7928 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7929 if (*base_commit_id == NULL) {
7930 err = got_error_from_errno("got_object_id_dup");
7931 goto done;
7934 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7935 worktree->base_commit_id);
7936 if (err)
7937 goto done;
7938 err = got_ref_write(*tmp_branch, repo);
7939 if (err)
7940 goto done;
7942 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7943 if (err)
7944 goto done;
7945 done:
7946 free(fileindex_path);
7947 free(tmp_branch_name);
7948 free(branch_ref_name);
7949 free(base_commit_ref_name);
7950 if (wt_branch)
7951 got_ref_close(wt_branch);
7952 if (err) {
7953 if (*branch_ref) {
7954 got_ref_close(*branch_ref);
7955 *branch_ref = NULL;
7957 if (*tmp_branch) {
7958 got_ref_close(*tmp_branch);
7959 *tmp_branch = NULL;
7961 free(*base_commit_id);
7962 if (*fileindex) {
7963 got_fileindex_free(*fileindex);
7964 *fileindex = NULL;
7966 lock_worktree(worktree, LOCK_SH);
7968 return err;
7971 const struct got_error *
7972 got_worktree_histedit_postpone(struct got_worktree *worktree,
7973 struct got_fileindex *fileindex)
7975 if (fileindex)
7976 got_fileindex_free(fileindex);
7977 return lock_worktree(worktree, LOCK_SH);
7980 const struct got_error *
7981 got_worktree_histedit_in_progress(int *in_progress,
7982 struct got_worktree *worktree)
7984 const struct got_error *err;
7985 char *tmp_branch_name = NULL;
7987 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7988 if (err)
7989 return err;
7991 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7992 free(tmp_branch_name);
7993 return NULL;
7996 const struct got_error *
7997 got_worktree_histedit_continue(struct got_object_id **commit_id,
7998 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7999 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
8000 struct got_worktree *worktree, struct got_repository *repo)
8002 const struct got_error *err;
8003 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
8004 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
8005 struct got_reference *commit_ref = NULL;
8006 struct got_reference *base_commit_ref = NULL;
8007 char *fileindex_path = NULL;
8008 int have_staged_files = 0;
8010 *commit_id = NULL;
8011 *tmp_branch = NULL;
8012 *branch_ref = NULL;
8013 *base_commit_id = NULL;
8014 *fileindex = NULL;
8016 err = lock_worktree(worktree, LOCK_EX);
8017 if (err)
8018 return err;
8020 err = open_fileindex(fileindex, &fileindex_path, worktree);
8021 if (err)
8022 goto done;
8024 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8025 &have_staged_files);
8026 if (err && err->code != GOT_ERR_CANCELLED)
8027 goto done;
8028 if (have_staged_files) {
8029 err = got_error(GOT_ERR_STAGED_PATHS);
8030 goto done;
8033 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8034 if (err)
8035 goto done;
8037 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8038 if (err)
8039 goto done;
8041 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
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;
8050 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
8051 if (err)
8052 goto done;
8054 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8055 if (err)
8056 goto done;
8057 err = got_ref_resolve(commit_id, repo, commit_ref);
8058 if (err)
8059 goto done;
8061 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8062 if (err)
8063 goto done;
8064 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8065 if (err)
8066 goto done;
8068 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8069 if (err)
8070 goto done;
8071 done:
8072 free(commit_ref_name);
8073 free(branch_ref_name);
8074 free(fileindex_path);
8075 if (commit_ref)
8076 got_ref_close(commit_ref);
8077 if (base_commit_ref)
8078 got_ref_close(base_commit_ref);
8079 if (err) {
8080 free(*commit_id);
8081 *commit_id = NULL;
8082 free(*base_commit_id);
8083 *base_commit_id = NULL;
8084 if (*tmp_branch) {
8085 got_ref_close(*tmp_branch);
8086 *tmp_branch = NULL;
8088 if (*branch_ref) {
8089 got_ref_close(*branch_ref);
8090 *branch_ref = NULL;
8092 if (*fileindex) {
8093 got_fileindex_free(*fileindex);
8094 *fileindex = NULL;
8096 lock_worktree(worktree, LOCK_EX);
8098 return err;
8101 const struct got_error *
8102 got_worktree_histedit_info(char **branch_name,
8103 struct got_worktree *worktree, struct got_repository *repo)
8105 const struct got_error *err;
8106 struct got_reference *branch_ref = NULL;
8107 char *branch_ref_name = NULL;
8109 *branch_name = NULL;
8111 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8112 if (err)
8113 goto done;
8115 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
8116 if (err)
8117 goto done;
8119 if (!got_ref_is_symbolic(branch_ref)) {
8120 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8121 "%s is not a symbolic reference",
8122 got_ref_get_name(branch_ref));
8123 goto done;
8126 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8127 if (*branch_name == NULL) {
8128 err = got_error_from_errno("strdup");
8129 goto done;
8131 done:
8132 free(branch_ref_name);
8133 if (branch_ref)
8134 got_ref_close(branch_ref);
8135 if (err) {
8136 free(*branch_name);
8137 *branch_name = NULL;
8139 return err;
8142 static const struct got_error *
8143 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8145 const struct got_error *err;
8146 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8147 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8149 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8150 if (err)
8151 goto done;
8152 err = delete_ref(tmp_branch_name, repo);
8153 if (err)
8154 goto done;
8156 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8157 worktree);
8158 if (err)
8159 goto done;
8160 err = delete_ref(base_commit_ref_name, repo);
8161 if (err)
8162 goto done;
8164 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8165 if (err)
8166 goto done;
8167 err = delete_ref(branch_ref_name, repo);
8168 if (err)
8169 goto done;
8171 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8172 if (err)
8173 goto done;
8174 err = delete_ref(commit_ref_name, repo);
8175 if (err)
8176 goto done;
8177 done:
8178 free(tmp_branch_name);
8179 free(base_commit_ref_name);
8180 free(branch_ref_name);
8181 free(commit_ref_name);
8182 return err;
8185 const struct got_error *
8186 got_worktree_histedit_abort(struct got_worktree *worktree,
8187 struct got_fileindex *fileindex, struct got_repository *repo,
8188 struct got_reference *branch, struct got_object_id *base_commit_id,
8189 got_worktree_checkout_cb progress_cb, void *progress_arg)
8191 const struct got_error *err, *unlockerr, *sync_err;
8192 struct got_reference *resolved = NULL;
8193 char *fileindex_path = NULL;
8194 struct got_object_id *merged_commit_id = NULL;
8195 struct got_commit_object *commit = NULL;
8196 char *commit_ref_name = NULL;
8197 struct got_reference *commit_ref = NULL;
8198 struct got_object_id *tree_id = NULL;
8199 struct revert_file_args rfa;
8200 struct got_pathlist_head added_paths;
8202 TAILQ_INIT(&added_paths);
8204 err = lock_worktree(worktree, LOCK_EX);
8205 if (err)
8206 return err;
8208 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8209 if (err)
8210 goto done;
8212 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8213 if (err) {
8214 if (err->code != GOT_ERR_NOT_REF)
8215 goto done;
8216 /* Can happen on early abort due to invalid histedit script. */
8217 commit_ref = NULL;
8220 if (commit_ref) {
8221 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8222 if (err)
8223 goto done;
8226 * Determine which files in added status can be safely removed
8227 * from disk while reverting changes in the work tree.
8228 * We want to avoid deleting unrelated files added by the
8229 * user during conflict resolution or during histedit -e.
8231 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8232 got_worktree_get_path_prefix(worktree), repo);
8233 if (err)
8234 goto done;
8237 err = got_ref_open(&resolved, repo,
8238 got_ref_get_symref_target(branch), 0);
8239 if (err)
8240 goto done;
8242 err = got_worktree_set_head_ref(worktree, resolved);
8243 if (err)
8244 goto done;
8246 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8247 if (err)
8248 goto done;
8250 err = got_object_open_as_commit(&commit, repo,
8251 worktree->base_commit_id);
8252 if (err)
8253 goto done;
8255 err = got_object_id_by_path(&tree_id, repo, commit,
8256 worktree->path_prefix);
8257 if (err)
8258 goto done;
8260 err = delete_histedit_refs(worktree, repo);
8261 if (err)
8262 goto done;
8264 err = get_fileindex_path(&fileindex_path, worktree);
8265 if (err)
8266 goto done;
8268 rfa.worktree = worktree;
8269 rfa.fileindex = fileindex;
8270 rfa.progress_cb = progress_cb;
8271 rfa.progress_arg = progress_arg;
8272 rfa.patch_cb = NULL;
8273 rfa.patch_arg = NULL;
8274 rfa.repo = repo;
8275 rfa.unlink_added_files = 1;
8276 rfa.added_files_to_unlink = &added_paths;
8277 err = worktree_status(worktree, "", fileindex, repo,
8278 revert_file, &rfa, NULL, NULL, 1, 0);
8279 if (err)
8280 goto sync;
8282 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8283 repo, progress_cb, progress_arg, NULL, NULL);
8284 sync:
8285 sync_err = sync_fileindex(fileindex, fileindex_path);
8286 if (sync_err && err == NULL)
8287 err = sync_err;
8288 done:
8289 if (resolved)
8290 got_ref_close(resolved);
8291 if (commit_ref)
8292 got_ref_close(commit_ref);
8293 free(merged_commit_id);
8294 free(tree_id);
8295 free(fileindex_path);
8296 free(commit_ref_name);
8298 unlockerr = lock_worktree(worktree, LOCK_SH);
8299 if (unlockerr && err == NULL)
8300 err = unlockerr;
8301 return err;
8304 const struct got_error *
8305 got_worktree_histedit_complete(struct got_worktree *worktree,
8306 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8307 struct got_reference *edited_branch, struct got_repository *repo)
8309 const struct got_error *err, *unlockerr, *sync_err;
8310 struct got_object_id *new_head_commit_id = NULL;
8311 struct got_reference *resolved = NULL;
8312 char *fileindex_path = NULL;
8314 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8315 if (err)
8316 return err;
8318 err = got_ref_open(&resolved, repo,
8319 got_ref_get_symref_target(edited_branch), 0);
8320 if (err)
8321 goto done;
8323 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8324 resolved, new_head_commit_id, repo);
8325 if (err)
8326 goto done;
8328 err = got_ref_change_ref(resolved, new_head_commit_id);
8329 if (err)
8330 goto done;
8332 err = got_ref_write(resolved, repo);
8333 if (err)
8334 goto done;
8336 err = got_worktree_set_head_ref(worktree, resolved);
8337 if (err)
8338 goto done;
8340 err = delete_histedit_refs(worktree, repo);
8341 if (err)
8342 goto done;
8344 err = get_fileindex_path(&fileindex_path, worktree);
8345 if (err)
8346 goto done;
8347 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8348 sync_err = sync_fileindex(fileindex, fileindex_path);
8349 if (sync_err && err == NULL)
8350 err = sync_err;
8351 done:
8352 got_fileindex_free(fileindex);
8353 free(fileindex_path);
8354 free(new_head_commit_id);
8355 unlockerr = lock_worktree(worktree, LOCK_SH);
8356 if (unlockerr && err == NULL)
8357 err = unlockerr;
8358 return err;
8361 const struct got_error *
8362 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8363 struct got_object_id *commit_id, struct got_repository *repo)
8365 const struct got_error *err;
8366 char *commit_ref_name;
8368 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8369 if (err)
8370 return err;
8372 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8373 if (err)
8374 goto done;
8376 err = delete_ref(commit_ref_name, repo);
8377 done:
8378 free(commit_ref_name);
8379 return err;
8382 const struct got_error *
8383 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8384 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8385 struct got_worktree *worktree, const char *refname,
8386 struct got_repository *repo)
8388 const struct got_error *err = NULL;
8389 char *fileindex_path = NULL;
8390 struct check_rebase_ok_arg ok_arg;
8392 *fileindex = NULL;
8393 *branch_ref = NULL;
8394 *base_branch_ref = NULL;
8396 err = lock_worktree(worktree, LOCK_EX);
8397 if (err)
8398 return err;
8400 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8401 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8402 "cannot integrate a branch into itself; "
8403 "update -b or different branch name required");
8404 goto done;
8407 err = open_fileindex(fileindex, &fileindex_path, worktree);
8408 if (err)
8409 goto done;
8411 /* Preconditions are the same as for rebase. */
8412 ok_arg.worktree = worktree;
8413 ok_arg.repo = repo;
8414 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8415 &ok_arg);
8416 if (err)
8417 goto done;
8419 err = got_ref_open(branch_ref, repo, refname, 1);
8420 if (err)
8421 goto done;
8423 err = got_ref_open(base_branch_ref, repo,
8424 got_worktree_get_head_ref_name(worktree), 1);
8425 done:
8426 if (err) {
8427 if (*branch_ref) {
8428 got_ref_close(*branch_ref);
8429 *branch_ref = NULL;
8431 if (*base_branch_ref) {
8432 got_ref_close(*base_branch_ref);
8433 *base_branch_ref = NULL;
8435 if (*fileindex) {
8436 got_fileindex_free(*fileindex);
8437 *fileindex = NULL;
8439 lock_worktree(worktree, LOCK_SH);
8441 return err;
8444 const struct got_error *
8445 got_worktree_integrate_continue(struct got_worktree *worktree,
8446 struct got_fileindex *fileindex, struct got_repository *repo,
8447 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8448 got_worktree_checkout_cb progress_cb, void *progress_arg,
8449 got_cancel_cb cancel_cb, void *cancel_arg)
8451 const struct got_error *err = NULL, *sync_err, *unlockerr;
8452 char *fileindex_path = NULL;
8453 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8454 struct got_commit_object *commit = NULL;
8456 err = get_fileindex_path(&fileindex_path, worktree);
8457 if (err)
8458 goto done;
8460 err = got_ref_resolve(&commit_id, repo, branch_ref);
8461 if (err)
8462 goto done;
8464 err = got_object_open_as_commit(&commit, repo, commit_id);
8465 if (err)
8466 goto done;
8468 err = got_object_id_by_path(&tree_id, repo, commit,
8469 worktree->path_prefix);
8470 if (err)
8471 goto done;
8473 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8474 if (err)
8475 goto done;
8477 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8478 progress_cb, progress_arg, cancel_cb, cancel_arg);
8479 if (err)
8480 goto sync;
8482 err = got_ref_change_ref(base_branch_ref, commit_id);
8483 if (err)
8484 goto sync;
8486 err = got_ref_write(base_branch_ref, repo);
8487 if (err)
8488 goto sync;
8490 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8491 sync:
8492 sync_err = sync_fileindex(fileindex, fileindex_path);
8493 if (sync_err && err == NULL)
8494 err = sync_err;
8496 done:
8497 unlockerr = got_ref_unlock(branch_ref);
8498 if (unlockerr && err == NULL)
8499 err = unlockerr;
8500 got_ref_close(branch_ref);
8502 unlockerr = got_ref_unlock(base_branch_ref);
8503 if (unlockerr && err == NULL)
8504 err = unlockerr;
8505 got_ref_close(base_branch_ref);
8507 got_fileindex_free(fileindex);
8508 free(fileindex_path);
8509 free(tree_id);
8510 if (commit)
8511 got_object_commit_close(commit);
8513 unlockerr = lock_worktree(worktree, LOCK_SH);
8514 if (unlockerr && err == NULL)
8515 err = unlockerr;
8516 return err;
8519 const struct got_error *
8520 got_worktree_integrate_abort(struct got_worktree *worktree,
8521 struct got_fileindex *fileindex, struct got_repository *repo,
8522 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8524 const struct got_error *err = NULL, *unlockerr = NULL;
8526 got_fileindex_free(fileindex);
8528 err = lock_worktree(worktree, LOCK_SH);
8530 unlockerr = got_ref_unlock(branch_ref);
8531 if (unlockerr && err == NULL)
8532 err = unlockerr;
8533 got_ref_close(branch_ref);
8535 unlockerr = got_ref_unlock(base_branch_ref);
8536 if (unlockerr && err == NULL)
8537 err = unlockerr;
8538 got_ref_close(base_branch_ref);
8540 return err;
8543 const struct got_error *
8544 got_worktree_merge_postpone(struct got_worktree *worktree,
8545 struct got_fileindex *fileindex)
8547 const struct got_error *err, *sync_err;
8548 char *fileindex_path = NULL;
8550 err = get_fileindex_path(&fileindex_path, worktree);
8551 if (err)
8552 goto done;
8554 sync_err = sync_fileindex(fileindex, fileindex_path);
8556 err = lock_worktree(worktree, LOCK_SH);
8557 if (sync_err && err == NULL)
8558 err = sync_err;
8559 done:
8560 got_fileindex_free(fileindex);
8561 free(fileindex_path);
8562 return err;
8565 static const struct got_error *
8566 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8568 const struct got_error *err;
8569 char *branch_refname = NULL, *commit_refname = NULL;
8571 err = get_merge_branch_ref_name(&branch_refname, worktree);
8572 if (err)
8573 goto done;
8574 err = delete_ref(branch_refname, repo);
8575 if (err)
8576 goto done;
8578 err = get_merge_commit_ref_name(&commit_refname, worktree);
8579 if (err)
8580 goto done;
8581 err = delete_ref(commit_refname, repo);
8582 if (err)
8583 goto done;
8585 done:
8586 free(branch_refname);
8587 free(commit_refname);
8588 return err;
8591 struct merge_commit_msg_arg {
8592 struct got_worktree *worktree;
8593 const char *branch_name;
8596 static const struct got_error *
8597 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8598 const char *diff_path, char **logmsg, void *arg)
8600 struct merge_commit_msg_arg *a = arg;
8602 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8603 got_worktree_get_head_ref_name(a->worktree)) == -1)
8604 return got_error_from_errno("asprintf");
8606 return NULL;
8610 const struct got_error *
8611 got_worktree_merge_branch(struct got_worktree *worktree,
8612 struct got_fileindex *fileindex,
8613 struct got_object_id *yca_commit_id,
8614 struct got_object_id *branch_tip,
8615 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8616 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8618 const struct got_error *err;
8619 char *fileindex_path = NULL;
8620 struct check_mixed_commits_args cma;
8622 err = get_fileindex_path(&fileindex_path, worktree);
8623 if (err)
8624 goto done;
8626 cma.worktree = worktree;
8627 cma.cancel_cb = cancel_cb;
8628 cma.cancel_arg = cancel_arg;
8630 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8631 &cma);
8632 if (err)
8633 goto done;
8635 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8636 branch_tip, repo, progress_cb, progress_arg,
8637 cancel_cb, cancel_arg);
8638 done:
8639 free(fileindex_path);
8640 return err;
8643 const struct got_error *
8644 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8645 struct got_worktree *worktree, struct got_fileindex *fileindex,
8646 const char *author, const char *committer, int allow_bad_symlinks,
8647 struct got_object_id *branch_tip, const char *branch_name,
8648 int allow_conflict, struct got_repository *repo,
8649 got_worktree_status_cb status_cb, void *status_arg)
8652 const struct got_error *err = NULL, *sync_err;
8653 struct got_pathlist_head commitable_paths;
8654 struct collect_commitables_arg cc_arg;
8655 struct got_pathlist_entry *pe;
8656 struct got_reference *head_ref = NULL;
8657 struct got_object_id *head_commit_id = NULL;
8658 int have_staged_files = 0;
8659 struct merge_commit_msg_arg mcm_arg;
8660 char *fileindex_path = NULL;
8662 memset(&cc_arg, 0, sizeof(cc_arg));
8663 *new_commit_id = NULL;
8665 TAILQ_INIT(&commitable_paths);
8667 err = get_fileindex_path(&fileindex_path, worktree);
8668 if (err)
8669 goto done;
8671 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8672 if (err)
8673 goto done;
8675 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8676 if (err)
8677 goto done;
8679 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8680 &have_staged_files);
8681 if (err && err->code != GOT_ERR_CANCELLED)
8682 goto done;
8683 if (have_staged_files) {
8684 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8685 goto done;
8688 cc_arg.commitable_paths = &commitable_paths;
8689 cc_arg.worktree = worktree;
8690 cc_arg.fileindex = fileindex;
8691 cc_arg.repo = repo;
8692 cc_arg.have_staged_files = have_staged_files;
8693 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8694 cc_arg.commit_conflicts = allow_conflict;
8695 err = worktree_status(worktree, "", fileindex, repo,
8696 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8697 if (err)
8698 goto done;
8700 mcm_arg.worktree = worktree;
8701 mcm_arg.branch_name = branch_name;
8702 err = commit_worktree(new_commit_id, &commitable_paths,
8703 head_commit_id, branch_tip, worktree, author, committer, NULL,
8704 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8705 if (err)
8706 goto done;
8708 err = update_fileindex_after_commit(worktree, &commitable_paths,
8709 *new_commit_id, fileindex, have_staged_files);
8710 sync_err = sync_fileindex(fileindex, fileindex_path);
8711 if (sync_err && err == NULL)
8712 err = sync_err;
8713 done:
8714 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8715 struct got_commitable *ct = pe->data;
8717 free_commitable(ct);
8719 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8720 free(fileindex_path);
8721 return err;
8724 const struct got_error *
8725 got_worktree_merge_complete(struct got_worktree *worktree,
8726 struct got_fileindex *fileindex, struct got_repository *repo)
8728 const struct got_error *err, *unlockerr, *sync_err;
8729 char *fileindex_path = NULL;
8731 err = delete_merge_refs(worktree, repo);
8732 if (err)
8733 goto done;
8735 err = get_fileindex_path(&fileindex_path, worktree);
8736 if (err)
8737 goto done;
8738 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8739 sync_err = sync_fileindex(fileindex, fileindex_path);
8740 if (sync_err && err == NULL)
8741 err = sync_err;
8742 done:
8743 got_fileindex_free(fileindex);
8744 free(fileindex_path);
8745 unlockerr = lock_worktree(worktree, LOCK_SH);
8746 if (unlockerr && err == NULL)
8747 err = unlockerr;
8748 return err;
8751 const struct got_error *
8752 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8753 struct got_repository *repo)
8755 const struct got_error *err;
8756 char *branch_refname = NULL;
8757 struct got_reference *branch_ref = NULL;
8759 *in_progress = 0;
8761 err = get_merge_branch_ref_name(&branch_refname, worktree);
8762 if (err)
8763 return err;
8764 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8765 free(branch_refname);
8766 if (err) {
8767 if (err->code != GOT_ERR_NOT_REF)
8768 return err;
8769 } else
8770 *in_progress = 1;
8772 return NULL;
8775 const struct got_error *got_worktree_merge_prepare(
8776 struct got_fileindex **fileindex, struct got_worktree *worktree,
8777 struct got_repository *repo)
8779 const struct got_error *err = NULL;
8780 char *fileindex_path = NULL;
8781 struct got_reference *wt_branch = NULL;
8782 struct got_object_id *wt_branch_tip = NULL;
8783 struct check_rebase_ok_arg ok_arg;
8785 *fileindex = NULL;
8787 err = lock_worktree(worktree, LOCK_EX);
8788 if (err)
8789 return err;
8791 err = open_fileindex(fileindex, &fileindex_path, worktree);
8792 if (err)
8793 goto done;
8795 /* Preconditions are the same as for rebase. */
8796 ok_arg.worktree = worktree;
8797 ok_arg.repo = repo;
8798 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8799 &ok_arg);
8800 if (err)
8801 goto done;
8803 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8804 0);
8805 if (err)
8806 goto done;
8808 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8809 if (err)
8810 goto done;
8812 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8813 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8814 goto done;
8817 done:
8818 free(fileindex_path);
8819 if (wt_branch)
8820 got_ref_close(wt_branch);
8821 free(wt_branch_tip);
8822 if (err) {
8823 if (*fileindex) {
8824 got_fileindex_free(*fileindex);
8825 *fileindex = NULL;
8827 lock_worktree(worktree, LOCK_SH);
8829 return err;
8832 const struct got_error *got_worktree_merge_write_refs(
8833 struct got_worktree *worktree, struct got_reference *branch,
8834 struct got_repository *repo)
8836 const struct got_error *err = NULL;
8837 char *branch_refname = NULL, *commit_refname = NULL;
8838 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8839 struct got_object_id *branch_tip = NULL;
8841 err = get_merge_branch_ref_name(&branch_refname, worktree);
8842 if (err)
8843 return err;
8845 err = get_merge_commit_ref_name(&commit_refname, worktree);
8846 if (err)
8847 return err;
8849 err = got_ref_resolve(&branch_tip, repo, branch);
8850 if (err)
8851 goto done;
8853 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8854 if (err)
8855 goto done;
8856 err = got_ref_write(branch_ref, repo);
8857 if (err)
8858 goto done;
8860 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8861 if (err)
8862 goto done;
8863 err = got_ref_write(commit_ref, repo);
8864 if (err)
8865 goto done;
8867 done:
8868 free(branch_refname);
8869 free(commit_refname);
8870 if (branch_ref)
8871 got_ref_close(branch_ref);
8872 if (commit_ref)
8873 got_ref_close(commit_ref);
8874 free(branch_tip);
8875 return err;
8878 const struct got_error *
8879 got_worktree_merge_continue(char **branch_name,
8880 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8881 struct got_worktree *worktree, struct got_repository *repo)
8883 const struct got_error *err;
8884 char *commit_refname = NULL, *branch_refname = NULL;
8885 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8886 char *fileindex_path = NULL;
8887 int have_staged_files = 0;
8889 *branch_name = NULL;
8890 *branch_tip = NULL;
8891 *fileindex = NULL;
8893 err = lock_worktree(worktree, LOCK_EX);
8894 if (err)
8895 return err;
8897 err = open_fileindex(fileindex, &fileindex_path, worktree);
8898 if (err)
8899 goto done;
8901 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8902 &have_staged_files);
8903 if (err && err->code != GOT_ERR_CANCELLED)
8904 goto done;
8905 if (have_staged_files) {
8906 err = got_error(GOT_ERR_STAGED_PATHS);
8907 goto done;
8910 err = get_merge_branch_ref_name(&branch_refname, worktree);
8911 if (err)
8912 goto done;
8914 err = get_merge_commit_ref_name(&commit_refname, worktree);
8915 if (err)
8916 goto done;
8918 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8919 if (err)
8920 goto done;
8922 if (!got_ref_is_symbolic(branch_ref)) {
8923 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8924 "%s is not a symbolic reference",
8925 got_ref_get_name(branch_ref));
8926 goto done;
8928 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8929 if (*branch_name == NULL) {
8930 err = got_error_from_errno("strdup");
8931 goto done;
8934 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8935 if (err)
8936 goto done;
8938 err = got_ref_resolve(branch_tip, repo, commit_ref);
8939 if (err)
8940 goto done;
8941 done:
8942 free(commit_refname);
8943 free(branch_refname);
8944 free(fileindex_path);
8945 if (commit_ref)
8946 got_ref_close(commit_ref);
8947 if (branch_ref)
8948 got_ref_close(branch_ref);
8949 if (err) {
8950 if (*branch_name) {
8951 free(*branch_name);
8952 *branch_name = NULL;
8954 free(*branch_tip);
8955 *branch_tip = NULL;
8956 if (*fileindex) {
8957 got_fileindex_free(*fileindex);
8958 *fileindex = NULL;
8960 lock_worktree(worktree, LOCK_SH);
8962 return err;
8965 const struct got_error *
8966 got_worktree_merge_info(char **branch_name, struct got_worktree *worktree,
8967 struct got_repository *repo)
8969 const struct got_error *err;
8970 char *branch_refname = NULL;
8971 struct got_reference *branch_ref = NULL;
8973 *branch_name = NULL;
8975 err = get_merge_branch_ref_name(&branch_refname, worktree);
8976 if (err)
8977 goto done;
8979 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8980 if (err)
8981 goto done;
8983 if (!got_ref_is_symbolic(branch_ref)) {
8984 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8985 "%s is not a symbolic reference",
8986 got_ref_get_name(branch_ref));
8987 goto done;
8989 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8990 if (*branch_name == NULL) {
8991 err = got_error_from_errno("strdup");
8992 goto done;
8995 done:
8996 free(branch_refname);
8997 if (branch_ref)
8998 got_ref_close(branch_ref);
8999 if (err) {
9000 if (*branch_name) {
9001 free(*branch_name);
9002 *branch_name = NULL;
9005 return err;
9008 const struct got_error *
9009 got_worktree_merge_abort(struct got_worktree *worktree,
9010 struct got_fileindex *fileindex, struct got_repository *repo,
9011 got_worktree_checkout_cb progress_cb, void *progress_arg)
9013 const struct got_error *err, *unlockerr, *sync_err;
9014 struct got_commit_object *commit = NULL;
9015 char *fileindex_path = NULL;
9016 struct revert_file_args rfa;
9017 char *commit_ref_name = NULL;
9018 struct got_reference *commit_ref = NULL;
9019 struct got_object_id *merged_commit_id = NULL;
9020 struct got_object_id *tree_id = NULL;
9021 struct got_pathlist_head added_paths;
9023 TAILQ_INIT(&added_paths);
9025 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
9026 if (err)
9027 goto done;
9029 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
9030 if (err)
9031 goto done;
9033 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
9034 if (err)
9035 goto done;
9038 * Determine which files in added status can be safely removed
9039 * from disk while reverting changes in the work tree.
9040 * We want to avoid deleting unrelated files which were added by
9041 * the user for conflict resolution purposes.
9043 err = get_paths_added_between_commits(&added_paths,
9044 got_worktree_get_base_commit_id(worktree), merged_commit_id,
9045 got_worktree_get_path_prefix(worktree), repo);
9046 if (err)
9047 goto done;
9050 err = got_object_open_as_commit(&commit, repo,
9051 worktree->base_commit_id);
9052 if (err)
9053 goto done;
9055 err = got_object_id_by_path(&tree_id, repo, commit,
9056 worktree->path_prefix);
9057 if (err)
9058 goto done;
9060 err = delete_merge_refs(worktree, repo);
9061 if (err)
9062 goto done;
9064 err = get_fileindex_path(&fileindex_path, worktree);
9065 if (err)
9066 goto done;
9068 rfa.worktree = worktree;
9069 rfa.fileindex = fileindex;
9070 rfa.progress_cb = progress_cb;
9071 rfa.progress_arg = progress_arg;
9072 rfa.patch_cb = NULL;
9073 rfa.patch_arg = NULL;
9074 rfa.repo = repo;
9075 rfa.unlink_added_files = 1;
9076 rfa.added_files_to_unlink = &added_paths;
9077 err = worktree_status(worktree, "", fileindex, repo,
9078 revert_file, &rfa, NULL, NULL, 1, 0);
9079 if (err)
9080 goto sync;
9082 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
9083 repo, progress_cb, progress_arg, NULL, NULL);
9084 sync:
9085 sync_err = sync_fileindex(fileindex, fileindex_path);
9086 if (sync_err && err == NULL)
9087 err = sync_err;
9088 done:
9089 free(tree_id);
9090 free(merged_commit_id);
9091 if (commit)
9092 got_object_commit_close(commit);
9093 if (fileindex)
9094 got_fileindex_free(fileindex);
9095 free(fileindex_path);
9096 if (commit_ref)
9097 got_ref_close(commit_ref);
9098 free(commit_ref_name);
9100 unlockerr = lock_worktree(worktree, LOCK_SH);
9101 if (unlockerr && err == NULL)
9102 err = unlockerr;
9103 return err;
9106 struct check_stage_ok_arg {
9107 struct got_object_id *head_commit_id;
9108 struct got_worktree *worktree;
9109 struct got_fileindex *fileindex;
9110 struct got_repository *repo;
9111 int have_changes;
9114 static const struct got_error *
9115 check_stage_ok(void *arg, unsigned char status,
9116 unsigned char staged_status, const char *relpath,
9117 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9118 struct got_object_id *commit_id, int dirfd, const char *de_name)
9120 struct check_stage_ok_arg *a = arg;
9121 const struct got_error *err = NULL;
9122 struct got_fileindex_entry *ie;
9123 struct got_object_id base_commit_id;
9124 struct got_object_id *base_commit_idp = NULL;
9125 char *in_repo_path = NULL, *p;
9127 if (status == GOT_STATUS_UNVERSIONED ||
9128 status == GOT_STATUS_NO_CHANGE)
9129 return NULL;
9130 if (status == GOT_STATUS_NONEXISTENT)
9131 return got_error_set_errno(ENOENT, relpath);
9133 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9134 if (ie == NULL)
9135 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9137 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
9138 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
9139 relpath) == -1)
9140 return got_error_from_errno("asprintf");
9142 if (got_fileindex_entry_has_commit(ie)) {
9143 base_commit_idp = got_fileindex_entry_get_commit_id(
9144 &base_commit_id, ie);
9147 if (status == GOT_STATUS_CONFLICT) {
9148 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9149 goto done;
9150 } else if (status != GOT_STATUS_ADD &&
9151 status != GOT_STATUS_MODIFY &&
9152 status != GOT_STATUS_DELETE) {
9153 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9154 goto done;
9157 a->have_changes = 1;
9159 p = in_repo_path;
9160 while (p[0] == '/')
9161 p++;
9162 err = check_out_of_date(p, status, staged_status,
9163 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9164 GOT_ERR_STAGE_OUT_OF_DATE);
9165 done:
9166 free(in_repo_path);
9167 return err;
9170 struct stage_path_arg {
9171 struct got_worktree *worktree;
9172 struct got_fileindex *fileindex;
9173 struct got_repository *repo;
9174 got_worktree_status_cb status_cb;
9175 void *status_arg;
9176 got_worktree_patch_cb patch_cb;
9177 void *patch_arg;
9178 int staged_something;
9179 int allow_bad_symlinks;
9182 static const struct got_error *
9183 stage_path(void *arg, unsigned char status,
9184 unsigned char staged_status, const char *relpath,
9185 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9186 struct got_object_id *commit_id, int dirfd, const char *de_name)
9188 struct stage_path_arg *a = arg;
9189 const struct got_error *err = NULL;
9190 struct got_fileindex_entry *ie;
9191 char *ondisk_path = NULL, *path_content = NULL;
9192 uint32_t stage;
9193 struct got_object_id *new_staged_blob_id = NULL;
9194 struct stat sb;
9196 if (status == GOT_STATUS_UNVERSIONED)
9197 return NULL;
9199 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9200 if (ie == NULL)
9201 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9203 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9204 relpath)== -1)
9205 return got_error_from_errno("asprintf");
9207 switch (status) {
9208 case GOT_STATUS_ADD:
9209 case GOT_STATUS_MODIFY:
9210 /* XXX could sb.st_mode be passed in by our caller? */
9211 if (lstat(ondisk_path, &sb) == -1) {
9212 err = got_error_from_errno2("lstat", ondisk_path);
9213 break;
9215 if (a->patch_cb) {
9216 if (status == GOT_STATUS_ADD) {
9217 int choice = GOT_PATCH_CHOICE_NONE;
9218 err = (*a->patch_cb)(&choice, a->patch_arg,
9219 status, ie->path, NULL, 1, 1);
9220 if (err)
9221 break;
9222 if (choice != GOT_PATCH_CHOICE_YES)
9223 break;
9224 } else {
9225 err = create_patched_content(&path_content, 0,
9226 staged_blob_id ? staged_blob_id : blob_id,
9227 ondisk_path, dirfd, de_name, ie->path,
9228 a->repo, a->patch_cb, a->patch_arg);
9229 if (err || path_content == NULL)
9230 break;
9233 err = got_object_blob_create(&new_staged_blob_id,
9234 path_content ? path_content : ondisk_path, a->repo);
9235 if (err)
9236 break;
9237 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9238 SHA1_DIGEST_LENGTH);
9239 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9240 stage = GOT_FILEIDX_STAGE_ADD;
9241 else
9242 stage = GOT_FILEIDX_STAGE_MODIFY;
9243 got_fileindex_entry_stage_set(ie, stage);
9244 if (S_ISLNK(sb.st_mode)) {
9245 int is_bad_symlink = 0;
9246 if (!a->allow_bad_symlinks) {
9247 char target_path[PATH_MAX];
9248 ssize_t target_len;
9249 target_len = readlink(ondisk_path, target_path,
9250 sizeof(target_path));
9251 if (target_len == -1) {
9252 err = got_error_from_errno2("readlink",
9253 ondisk_path);
9254 break;
9256 err = is_bad_symlink_target(&is_bad_symlink,
9257 target_path, target_len, ondisk_path,
9258 a->worktree->root_path,
9259 a->worktree->meta_dir);
9260 if (err)
9261 break;
9262 if (is_bad_symlink) {
9263 err = got_error_path(ondisk_path,
9264 GOT_ERR_BAD_SYMLINK);
9265 break;
9268 if (is_bad_symlink)
9269 got_fileindex_entry_staged_filetype_set(ie,
9270 GOT_FILEIDX_MODE_BAD_SYMLINK);
9271 else
9272 got_fileindex_entry_staged_filetype_set(ie,
9273 GOT_FILEIDX_MODE_SYMLINK);
9274 } else {
9275 got_fileindex_entry_staged_filetype_set(ie,
9276 GOT_FILEIDX_MODE_REGULAR_FILE);
9278 a->staged_something = 1;
9279 if (a->status_cb == NULL)
9280 break;
9281 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9282 get_staged_status(ie), relpath, blob_id,
9283 new_staged_blob_id, NULL, dirfd, de_name);
9284 if (err)
9285 break;
9287 * When staging the reverse of the staged diff,
9288 * implicitly unstage the file.
9290 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9291 sizeof(ie->blob_sha1)) == 0) {
9292 got_fileindex_entry_stage_set(ie,
9293 GOT_FILEIDX_STAGE_NONE);
9295 break;
9296 case GOT_STATUS_DELETE:
9297 if (staged_status == GOT_STATUS_DELETE)
9298 break;
9299 if (a->patch_cb) {
9300 int choice = GOT_PATCH_CHOICE_NONE;
9301 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9302 ie->path, NULL, 1, 1);
9303 if (err)
9304 break;
9305 if (choice == GOT_PATCH_CHOICE_NO)
9306 break;
9307 if (choice != GOT_PATCH_CHOICE_YES) {
9308 err = got_error(GOT_ERR_PATCH_CHOICE);
9309 break;
9312 stage = GOT_FILEIDX_STAGE_DELETE;
9313 got_fileindex_entry_stage_set(ie, stage);
9314 a->staged_something = 1;
9315 if (a->status_cb == NULL)
9316 break;
9317 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9318 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9319 de_name);
9320 break;
9321 case GOT_STATUS_NO_CHANGE:
9322 break;
9323 case GOT_STATUS_CONFLICT:
9324 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9325 break;
9326 case GOT_STATUS_NONEXISTENT:
9327 err = got_error_set_errno(ENOENT, relpath);
9328 break;
9329 default:
9330 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9331 break;
9334 if (path_content && unlink(path_content) == -1 && err == NULL)
9335 err = got_error_from_errno2("unlink", path_content);
9336 free(path_content);
9337 free(ondisk_path);
9338 free(new_staged_blob_id);
9339 return err;
9342 const struct got_error *
9343 got_worktree_stage(struct got_worktree *worktree,
9344 struct got_pathlist_head *paths,
9345 got_worktree_status_cb status_cb, void *status_arg,
9346 got_worktree_patch_cb patch_cb, void *patch_arg,
9347 int allow_bad_symlinks, struct got_repository *repo)
9349 const struct got_error *err = NULL, *sync_err, *unlockerr;
9350 struct got_pathlist_entry *pe;
9351 struct got_fileindex *fileindex = NULL;
9352 char *fileindex_path = NULL;
9353 struct got_reference *head_ref = NULL;
9354 struct got_object_id *head_commit_id = NULL;
9355 struct check_stage_ok_arg oka;
9356 struct stage_path_arg spa;
9358 err = lock_worktree(worktree, LOCK_EX);
9359 if (err)
9360 return err;
9362 err = got_ref_open(&head_ref, repo,
9363 got_worktree_get_head_ref_name(worktree), 0);
9364 if (err)
9365 goto done;
9366 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9367 if (err)
9368 goto done;
9369 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9370 if (err)
9371 goto done;
9373 /* Check pre-conditions before staging anything. */
9374 oka.head_commit_id = head_commit_id;
9375 oka.worktree = worktree;
9376 oka.fileindex = fileindex;
9377 oka.repo = repo;
9378 oka.have_changes = 0;
9379 TAILQ_FOREACH(pe, paths, entry) {
9380 err = worktree_status(worktree, pe->path, fileindex, repo,
9381 check_stage_ok, &oka, NULL, NULL, 1, 0);
9382 if (err)
9383 goto done;
9385 if (!oka.have_changes) {
9386 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9387 goto done;
9390 spa.worktree = worktree;
9391 spa.fileindex = fileindex;
9392 spa.repo = repo;
9393 spa.patch_cb = patch_cb;
9394 spa.patch_arg = patch_arg;
9395 spa.status_cb = status_cb;
9396 spa.status_arg = status_arg;
9397 spa.staged_something = 0;
9398 spa.allow_bad_symlinks = allow_bad_symlinks;
9399 TAILQ_FOREACH(pe, paths, entry) {
9400 err = worktree_status(worktree, pe->path, fileindex, repo,
9401 stage_path, &spa, NULL, NULL, 1, 0);
9402 if (err)
9403 goto done;
9405 if (!spa.staged_something) {
9406 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9407 goto done;
9410 sync_err = sync_fileindex(fileindex, fileindex_path);
9411 if (sync_err && err == NULL)
9412 err = sync_err;
9413 done:
9414 if (head_ref)
9415 got_ref_close(head_ref);
9416 free(head_commit_id);
9417 free(fileindex_path);
9418 if (fileindex)
9419 got_fileindex_free(fileindex);
9420 unlockerr = lock_worktree(worktree, LOCK_SH);
9421 if (unlockerr && err == NULL)
9422 err = unlockerr;
9423 return err;
9426 struct unstage_path_arg {
9427 struct got_worktree *worktree;
9428 struct got_fileindex *fileindex;
9429 struct got_repository *repo;
9430 got_worktree_checkout_cb progress_cb;
9431 void *progress_arg;
9432 got_worktree_patch_cb patch_cb;
9433 void *patch_arg;
9436 static const struct got_error *
9437 create_unstaged_content(char **path_unstaged_content,
9438 char **path_new_staged_content, struct got_object_id *blob_id,
9439 struct got_object_id *staged_blob_id, const char *relpath,
9440 struct got_repository *repo,
9441 got_worktree_patch_cb patch_cb, void *patch_arg)
9443 const struct got_error *err, *free_err;
9444 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9445 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9446 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9447 struct got_diffreg_result *diffreg_result = NULL;
9448 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9449 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9450 int fd1 = -1, fd2 = -1;
9452 *path_unstaged_content = NULL;
9453 *path_new_staged_content = NULL;
9455 err = got_object_id_str(&label1, blob_id);
9456 if (err)
9457 return err;
9459 fd1 = got_opentempfd();
9460 if (fd1 == -1) {
9461 err = got_error_from_errno("got_opentempfd");
9462 goto done;
9464 fd2 = got_opentempfd();
9465 if (fd2 == -1) {
9466 err = got_error_from_errno("got_opentempfd");
9467 goto done;
9470 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9471 if (err)
9472 goto done;
9474 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9475 if (err)
9476 goto done;
9478 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9479 if (err)
9480 goto done;
9482 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9483 fd2);
9484 if (err)
9485 goto done;
9487 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9488 if (err)
9489 goto done;
9491 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9492 if (err)
9493 goto done;
9495 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9496 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9497 if (err)
9498 goto done;
9500 err = got_opentemp_named(path_unstaged_content, &outfile,
9501 "got-unstaged-content", "");
9502 if (err)
9503 goto done;
9504 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9505 "got-new-staged-content", "");
9506 if (err)
9507 goto done;
9509 if (fseek(f1, 0L, SEEK_SET) == -1) {
9510 err = got_ferror(f1, GOT_ERR_IO);
9511 goto done;
9513 if (fseek(f2, 0L, SEEK_SET) == -1) {
9514 err = got_ferror(f2, GOT_ERR_IO);
9515 goto done;
9517 /* Count the number of actual changes in the diff result. */
9518 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9519 struct diff_chunk_context cc = {};
9520 diff_chunk_context_load_change(&cc, &nchunks_used,
9521 diffreg_result->result, n, 0);
9522 nchanges++;
9524 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9525 int choice;
9526 err = apply_or_reject_change(&choice, &nchunks_used,
9527 diffreg_result->result, n, relpath, f1, f2,
9528 &line_cur1, &line_cur2,
9529 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9530 if (err)
9531 goto done;
9532 if (choice == GOT_PATCH_CHOICE_YES)
9533 have_content = 1;
9534 else
9535 have_rejected_content = 1;
9536 if (choice == GOT_PATCH_CHOICE_QUIT)
9537 break;
9539 if (have_content || have_rejected_content)
9540 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9541 outfile, rejectfile);
9542 done:
9543 free(label1);
9544 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9545 err = got_error_from_errno("close");
9546 if (blob)
9547 got_object_blob_close(blob);
9548 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9549 err = got_error_from_errno("close");
9550 if (staged_blob)
9551 got_object_blob_close(staged_blob);
9552 free_err = got_diffreg_result_free(diffreg_result);
9553 if (free_err && err == NULL)
9554 err = free_err;
9555 if (f1 && fclose(f1) == EOF && err == NULL)
9556 err = got_error_from_errno2("fclose", path1);
9557 if (f2 && fclose(f2) == EOF && err == NULL)
9558 err = got_error_from_errno2("fclose", path2);
9559 if (outfile && fclose(outfile) == EOF && err == NULL)
9560 err = got_error_from_errno2("fclose", *path_unstaged_content);
9561 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9562 err = got_error_from_errno2("fclose", *path_new_staged_content);
9563 if (path1 && unlink(path1) == -1 && err == NULL)
9564 err = got_error_from_errno2("unlink", path1);
9565 if (path2 && unlink(path2) == -1 && err == NULL)
9566 err = got_error_from_errno2("unlink", path2);
9567 if (err || !have_content) {
9568 if (*path_unstaged_content &&
9569 unlink(*path_unstaged_content) == -1 && err == NULL)
9570 err = got_error_from_errno2("unlink",
9571 *path_unstaged_content);
9572 free(*path_unstaged_content);
9573 *path_unstaged_content = NULL;
9575 if (err || !have_content || !have_rejected_content) {
9576 if (*path_new_staged_content &&
9577 unlink(*path_new_staged_content) == -1 && err == NULL)
9578 err = got_error_from_errno2("unlink",
9579 *path_new_staged_content);
9580 free(*path_new_staged_content);
9581 *path_new_staged_content = NULL;
9583 free(path1);
9584 free(path2);
9585 return err;
9588 static const struct got_error *
9589 unstage_hunks(struct got_object_id *staged_blob_id,
9590 struct got_blob_object *blob_base,
9591 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9592 const char *ondisk_path, const char *label_orig,
9593 struct got_worktree *worktree, struct got_repository *repo,
9594 got_worktree_patch_cb patch_cb, void *patch_arg,
9595 got_worktree_checkout_cb progress_cb, void *progress_arg)
9597 const struct got_error *err = NULL;
9598 char *path_unstaged_content = NULL;
9599 char *path_new_staged_content = NULL;
9600 char *parent = NULL, *base_path = NULL;
9601 char *blob_base_path = NULL;
9602 struct got_object_id *new_staged_blob_id = NULL;
9603 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9604 struct stat sb;
9606 err = create_unstaged_content(&path_unstaged_content,
9607 &path_new_staged_content, blob_id, staged_blob_id,
9608 ie->path, repo, patch_cb, patch_arg);
9609 if (err)
9610 return err;
9612 if (path_unstaged_content == NULL)
9613 return NULL;
9615 if (path_new_staged_content) {
9616 err = got_object_blob_create(&new_staged_blob_id,
9617 path_new_staged_content, repo);
9618 if (err)
9619 goto done;
9622 f = fopen(path_unstaged_content, "re");
9623 if (f == NULL) {
9624 err = got_error_from_errno2("fopen",
9625 path_unstaged_content);
9626 goto done;
9628 if (fstat(fileno(f), &sb) == -1) {
9629 err = got_error_from_errno2("fstat", path_unstaged_content);
9630 goto done;
9632 if (got_fileindex_entry_staged_filetype_get(ie) ==
9633 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9634 char link_target[PATH_MAX];
9635 size_t r;
9636 r = fread(link_target, 1, sizeof(link_target), f);
9637 if (r == 0 && ferror(f)) {
9638 err = got_error_from_errno("fread");
9639 goto done;
9641 if (r >= sizeof(link_target)) { /* should not happen */
9642 err = got_error(GOT_ERR_NO_SPACE);
9643 goto done;
9645 link_target[r] = '\0';
9646 err = merge_symlink(worktree, blob_base,
9647 ondisk_path, ie->path, label_orig, link_target,
9648 worktree->base_commit_id, repo, progress_cb,
9649 progress_arg);
9650 } else {
9651 int local_changes_subsumed;
9653 err = got_path_dirname(&parent, ondisk_path);
9654 if (err)
9655 return err;
9657 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9658 parent) == -1) {
9659 err = got_error_from_errno("asprintf");
9660 base_path = NULL;
9661 goto done;
9664 err = got_opentemp_named(&blob_base_path, &f_base,
9665 base_path, "");
9666 if (err)
9667 goto done;
9668 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9669 blob_base);
9670 if (err)
9671 goto done;
9674 * In order the run a 3-way merge with a symlink we copy the symlink's
9675 * target path into a temporary file and use that file with diff3.
9677 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9678 err = dump_symlink_target_path_to_file(&f_deriv2,
9679 ondisk_path);
9680 if (err)
9681 goto done;
9682 } else {
9683 int fd;
9684 fd = open(ondisk_path,
9685 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9686 if (fd == -1) {
9687 err = got_error_from_errno2("open", ondisk_path);
9688 goto done;
9690 f_deriv2 = fdopen(fd, "r");
9691 if (f_deriv2 == NULL) {
9692 err = got_error_from_errno2("fdopen", ondisk_path);
9693 close(fd);
9694 goto done;
9698 err = merge_file(&local_changes_subsumed, worktree,
9699 f_base, f, f_deriv2, ondisk_path, ie->path,
9700 got_fileindex_perms_to_st(ie),
9701 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9702 repo, progress_cb, progress_arg);
9704 if (err)
9705 goto done;
9707 if (new_staged_blob_id) {
9708 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9709 SHA1_DIGEST_LENGTH);
9710 } else {
9711 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9712 got_fileindex_entry_staged_filetype_set(ie, 0);
9714 done:
9715 free(new_staged_blob_id);
9716 if (path_unstaged_content &&
9717 unlink(path_unstaged_content) == -1 && err == NULL)
9718 err = got_error_from_errno2("unlink", path_unstaged_content);
9719 if (path_new_staged_content &&
9720 unlink(path_new_staged_content) == -1 && err == NULL)
9721 err = got_error_from_errno2("unlink", path_new_staged_content);
9722 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9723 err = got_error_from_errno2("unlink", blob_base_path);
9724 if (f_base && fclose(f_base) == EOF && err == NULL)
9725 err = got_error_from_errno2("fclose", path_unstaged_content);
9726 if (f && fclose(f) == EOF && err == NULL)
9727 err = got_error_from_errno2("fclose", path_unstaged_content);
9728 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9729 err = got_error_from_errno2("fclose", ondisk_path);
9730 free(path_unstaged_content);
9731 free(path_new_staged_content);
9732 free(blob_base_path);
9733 free(parent);
9734 free(base_path);
9735 return err;
9738 static const struct got_error *
9739 unstage_path(void *arg, unsigned char status,
9740 unsigned char staged_status, const char *relpath,
9741 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9742 struct got_object_id *commit_id, int dirfd, const char *de_name)
9744 const struct got_error *err = NULL;
9745 struct unstage_path_arg *a = arg;
9746 struct got_fileindex_entry *ie;
9747 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9748 char *ondisk_path = NULL;
9749 char *id_str = NULL, *label_orig = NULL;
9750 int local_changes_subsumed;
9751 struct stat sb;
9752 int fd1 = -1, fd2 = -1;
9754 if (staged_status != GOT_STATUS_ADD &&
9755 staged_status != GOT_STATUS_MODIFY &&
9756 staged_status != GOT_STATUS_DELETE)
9757 return NULL;
9759 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9760 if (ie == NULL)
9761 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9763 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9764 == -1)
9765 return got_error_from_errno("asprintf");
9767 err = got_object_id_str(&id_str,
9768 commit_id ? commit_id : a->worktree->base_commit_id);
9769 if (err)
9770 goto done;
9771 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9772 id_str) == -1) {
9773 err = got_error_from_errno("asprintf");
9774 goto done;
9777 fd1 = got_opentempfd();
9778 if (fd1 == -1) {
9779 err = got_error_from_errno("got_opentempfd");
9780 goto done;
9782 fd2 = got_opentempfd();
9783 if (fd2 == -1) {
9784 err = got_error_from_errno("got_opentempfd");
9785 goto done;
9788 switch (staged_status) {
9789 case GOT_STATUS_MODIFY:
9790 err = got_object_open_as_blob(&blob_base, a->repo,
9791 blob_id, 8192, fd1);
9792 if (err)
9793 break;
9794 /* fall through */
9795 case GOT_STATUS_ADD:
9796 if (a->patch_cb) {
9797 if (staged_status == GOT_STATUS_ADD) {
9798 int choice = GOT_PATCH_CHOICE_NONE;
9799 err = (*a->patch_cb)(&choice, a->patch_arg,
9800 staged_status, ie->path, NULL, 1, 1);
9801 if (err)
9802 break;
9803 if (choice != GOT_PATCH_CHOICE_YES)
9804 break;
9805 } else {
9806 err = unstage_hunks(staged_blob_id,
9807 blob_base, blob_id, ie, ondisk_path,
9808 label_orig, a->worktree, a->repo,
9809 a->patch_cb, a->patch_arg,
9810 a->progress_cb, a->progress_arg);
9811 break; /* Done with this file. */
9814 err = got_object_open_as_blob(&blob_staged, a->repo,
9815 staged_blob_id, 8192, fd2);
9816 if (err)
9817 break;
9818 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9819 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9820 case GOT_FILEIDX_MODE_REGULAR_FILE:
9821 err = merge_blob(&local_changes_subsumed, a->worktree,
9822 blob_base, ondisk_path, relpath,
9823 got_fileindex_perms_to_st(ie), label_orig,
9824 blob_staged, commit_id ? commit_id :
9825 a->worktree->base_commit_id, a->repo,
9826 a->progress_cb, a->progress_arg);
9827 break;
9828 case GOT_FILEIDX_MODE_SYMLINK:
9829 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9830 char *staged_target;
9831 err = got_object_blob_read_to_str(
9832 &staged_target, blob_staged);
9833 if (err)
9834 goto done;
9835 err = merge_symlink(a->worktree, blob_base,
9836 ondisk_path, relpath, label_orig,
9837 staged_target, commit_id ? commit_id :
9838 a->worktree->base_commit_id,
9839 a->repo, a->progress_cb, a->progress_arg);
9840 free(staged_target);
9841 } else {
9842 err = merge_blob(&local_changes_subsumed,
9843 a->worktree, blob_base, ondisk_path,
9844 relpath, got_fileindex_perms_to_st(ie),
9845 label_orig, blob_staged,
9846 commit_id ? commit_id :
9847 a->worktree->base_commit_id, a->repo,
9848 a->progress_cb, a->progress_arg);
9850 break;
9851 default:
9852 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9853 break;
9855 if (err == NULL) {
9856 got_fileindex_entry_stage_set(ie,
9857 GOT_FILEIDX_STAGE_NONE);
9858 got_fileindex_entry_staged_filetype_set(ie, 0);
9860 break;
9861 case GOT_STATUS_DELETE:
9862 if (a->patch_cb) {
9863 int choice = GOT_PATCH_CHOICE_NONE;
9864 err = (*a->patch_cb)(&choice, a->patch_arg,
9865 staged_status, ie->path, NULL, 1, 1);
9866 if (err)
9867 break;
9868 if (choice == GOT_PATCH_CHOICE_NO)
9869 break;
9870 if (choice != GOT_PATCH_CHOICE_YES) {
9871 err = got_error(GOT_ERR_PATCH_CHOICE);
9872 break;
9875 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9876 got_fileindex_entry_staged_filetype_set(ie, 0);
9877 err = get_file_status(&status, &sb, ie, ondisk_path,
9878 dirfd, de_name, a->repo);
9879 if (err)
9880 break;
9881 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9882 break;
9884 done:
9885 free(ondisk_path);
9886 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9887 err = got_error_from_errno("close");
9888 if (blob_base)
9889 got_object_blob_close(blob_base);
9890 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9891 err = got_error_from_errno("close");
9892 if (blob_staged)
9893 got_object_blob_close(blob_staged);
9894 free(id_str);
9895 free(label_orig);
9896 return err;
9899 const struct got_error *
9900 got_worktree_unstage(struct got_worktree *worktree,
9901 struct got_pathlist_head *paths,
9902 got_worktree_checkout_cb progress_cb, void *progress_arg,
9903 got_worktree_patch_cb patch_cb, void *patch_arg,
9904 struct got_repository *repo)
9906 const struct got_error *err = NULL, *sync_err, *unlockerr;
9907 struct got_pathlist_entry *pe;
9908 struct got_fileindex *fileindex = NULL;
9909 char *fileindex_path = NULL;
9910 struct unstage_path_arg upa;
9912 err = lock_worktree(worktree, LOCK_EX);
9913 if (err)
9914 return err;
9916 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9917 if (err)
9918 goto done;
9920 upa.worktree = worktree;
9921 upa.fileindex = fileindex;
9922 upa.repo = repo;
9923 upa.progress_cb = progress_cb;
9924 upa.progress_arg = progress_arg;
9925 upa.patch_cb = patch_cb;
9926 upa.patch_arg = patch_arg;
9927 TAILQ_FOREACH(pe, paths, entry) {
9928 err = worktree_status(worktree, pe->path, fileindex, repo,
9929 unstage_path, &upa, NULL, NULL, 1, 0);
9930 if (err)
9931 goto done;
9934 sync_err = sync_fileindex(fileindex, fileindex_path);
9935 if (sync_err && err == NULL)
9936 err = sync_err;
9937 done:
9938 free(fileindex_path);
9939 if (fileindex)
9940 got_fileindex_free(fileindex);
9941 unlockerr = lock_worktree(worktree, LOCK_SH);
9942 if (unlockerr && err == NULL)
9943 err = unlockerr;
9944 return err;
9947 struct report_file_info_arg {
9948 struct got_worktree *worktree;
9949 got_worktree_path_info_cb info_cb;
9950 void *info_arg;
9951 struct got_pathlist_head *paths;
9952 got_cancel_cb cancel_cb;
9953 void *cancel_arg;
9956 static const struct got_error *
9957 report_file_info(void *arg, struct got_fileindex_entry *ie)
9959 const struct got_error *err;
9960 struct report_file_info_arg *a = arg;
9961 struct got_pathlist_entry *pe;
9962 struct got_object_id blob_id, staged_blob_id, commit_id;
9963 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9964 struct got_object_id *commit_idp = NULL;
9965 int stage;
9967 if (a->cancel_cb) {
9968 err = a->cancel_cb(a->cancel_arg);
9969 if (err)
9970 return err;
9973 TAILQ_FOREACH(pe, a->paths, entry) {
9974 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9975 got_path_is_child(ie->path, pe->path, pe->path_len))
9976 break;
9978 if (pe == NULL) /* not found */
9979 return NULL;
9981 if (got_fileindex_entry_has_blob(ie))
9982 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9983 stage = got_fileindex_entry_stage_get(ie);
9984 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9985 stage == GOT_FILEIDX_STAGE_ADD) {
9986 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9987 &staged_blob_id, ie);
9990 if (got_fileindex_entry_has_commit(ie))
9991 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9993 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9994 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9997 const struct got_error *
9998 got_worktree_path_info(struct got_worktree *worktree,
9999 struct got_pathlist_head *paths,
10000 got_worktree_path_info_cb info_cb, void *info_arg,
10001 got_cancel_cb cancel_cb, void *cancel_arg)
10004 const struct got_error *err = NULL, *unlockerr;
10005 struct got_fileindex *fileindex = NULL;
10006 char *fileindex_path = NULL;
10007 struct report_file_info_arg arg;
10009 err = lock_worktree(worktree, LOCK_SH);
10010 if (err)
10011 return err;
10013 err = open_fileindex(&fileindex, &fileindex_path, worktree);
10014 if (err)
10015 goto done;
10017 arg.worktree = worktree;
10018 arg.info_cb = info_cb;
10019 arg.info_arg = info_arg;
10020 arg.paths = paths;
10021 arg.cancel_cb = cancel_cb;
10022 arg.cancel_arg = cancel_arg;
10023 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
10024 &arg);
10025 done:
10026 free(fileindex_path);
10027 if (fileindex)
10028 got_fileindex_free(fileindex);
10029 unlockerr = lock_worktree(worktree, LOCK_UN);
10030 if (unlockerr && err == NULL)
10031 err = unlockerr;
10032 return err;
10035 static const struct got_error *
10036 patch_check_path(const char *p, char **path, unsigned char *status,
10037 unsigned char *staged_status, struct got_fileindex *fileindex,
10038 struct got_worktree *worktree, struct got_repository *repo)
10040 const struct got_error *err;
10041 struct got_fileindex_entry *ie;
10042 struct stat sb;
10043 char *ondisk_path = NULL;
10045 err = got_worktree_resolve_path(path, worktree, p);
10046 if (err)
10047 return err;
10049 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
10050 *path[0] ? "/" : "", *path) == -1)
10051 return got_error_from_errno("asprintf");
10053 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
10054 if (ie) {
10055 *staged_status = get_staged_status(ie);
10056 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
10057 repo);
10058 if (err)
10059 goto done;
10060 } else {
10061 *staged_status = GOT_STATUS_NO_CHANGE;
10062 *status = GOT_STATUS_UNVERSIONED;
10063 if (lstat(ondisk_path, &sb) == -1) {
10064 if (errno != ENOENT) {
10065 err = got_error_from_errno2("lstat",
10066 ondisk_path);
10067 goto done;
10069 *status = GOT_STATUS_NONEXISTENT;
10073 done:
10074 free(ondisk_path);
10075 return err;
10078 static const struct got_error *
10079 patch_can_rm(const char *path, unsigned char status,
10080 unsigned char staged_status)
10082 if (status == GOT_STATUS_NONEXISTENT)
10083 return got_error_set_errno(ENOENT, path);
10084 if (status != GOT_STATUS_NO_CHANGE &&
10085 status != GOT_STATUS_ADD &&
10086 status != GOT_STATUS_MODIFY &&
10087 status != GOT_STATUS_MODE_CHANGE)
10088 return got_error_path(path, GOT_ERR_FILE_STATUS);
10089 if (staged_status == GOT_STATUS_DELETE)
10090 return got_error_path(path, GOT_ERR_FILE_STATUS);
10091 return NULL;
10094 static const struct got_error *
10095 patch_can_add(const char *path, unsigned char status)
10097 if (status != GOT_STATUS_NONEXISTENT)
10098 return got_error_path(path, GOT_ERR_FILE_STATUS);
10099 return NULL;
10102 static const struct got_error *
10103 patch_can_edit(const char *path, unsigned char status,
10104 unsigned char staged_status)
10106 if (status == GOT_STATUS_NONEXISTENT)
10107 return got_error_set_errno(ENOENT, path);
10108 if (status != GOT_STATUS_NO_CHANGE &&
10109 status != GOT_STATUS_ADD &&
10110 status != GOT_STATUS_MODIFY)
10111 return got_error_path(path, GOT_ERR_FILE_STATUS);
10112 if (staged_status == GOT_STATUS_DELETE)
10113 return got_error_path(path, GOT_ERR_FILE_STATUS);
10114 return NULL;
10117 const struct got_error *
10118 got_worktree_patch_prepare(struct got_fileindex **fileindex,
10119 char **fileindex_path, struct got_worktree *worktree)
10121 return open_fileindex(fileindex, fileindex_path, worktree);
10124 const struct got_error *
10125 got_worktree_patch_check_path(const char *old, const char *new,
10126 char **oldpath, char **newpath, struct got_worktree *worktree,
10127 struct got_repository *repo, struct got_fileindex *fileindex)
10129 const struct got_error *err = NULL;
10130 int file_renamed = 0;
10131 unsigned char status_old, staged_status_old;
10132 unsigned char status_new, staged_status_new;
10134 *oldpath = NULL;
10135 *newpath = NULL;
10137 err = patch_check_path(old != NULL ? old : new, oldpath,
10138 &status_old, &staged_status_old, fileindex, worktree, repo);
10139 if (err)
10140 goto done;
10142 err = patch_check_path(new != NULL ? new : old, newpath,
10143 &status_new, &staged_status_new, fileindex, worktree, repo);
10144 if (err)
10145 goto done;
10147 if (old != NULL && new != NULL && strcmp(old, new) != 0)
10148 file_renamed = 1;
10150 if (old != NULL && new == NULL)
10151 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10152 else if (file_renamed) {
10153 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10154 if (err == NULL)
10155 err = patch_can_add(*newpath, status_new);
10156 } else if (old == NULL)
10157 err = patch_can_add(*newpath, status_new);
10158 else
10159 err = patch_can_edit(*newpath, status_new, staged_status_new);
10161 done:
10162 if (err) {
10163 free(*oldpath);
10164 *oldpath = NULL;
10165 free(*newpath);
10166 *newpath = NULL;
10168 return err;
10171 const struct got_error *
10172 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10173 struct got_worktree *worktree, struct got_fileindex *fileindex,
10174 got_worktree_checkout_cb progress_cb, void *progress_arg)
10176 struct schedule_addition_args saa;
10178 memset(&saa, 0, sizeof(saa));
10179 saa.worktree = worktree;
10180 saa.fileindex = fileindex;
10181 saa.progress_cb = progress_cb;
10182 saa.progress_arg = progress_arg;
10183 saa.repo = repo;
10185 return worktree_status(worktree, path, fileindex, repo,
10186 schedule_addition, &saa, NULL, NULL, 1, 0);
10189 const struct got_error *
10190 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10191 struct got_worktree *worktree, struct got_fileindex *fileindex,
10192 got_worktree_delete_cb progress_cb, void *progress_arg)
10194 const struct got_error *err;
10195 struct schedule_deletion_args sda;
10196 char *ondisk_status_path;
10198 memset(&sda, 0, sizeof(sda));
10199 sda.worktree = worktree;
10200 sda.fileindex = fileindex;
10201 sda.progress_cb = progress_cb;
10202 sda.progress_arg = progress_arg;
10203 sda.repo = repo;
10204 sda.delete_local_mods = 0;
10205 sda.keep_on_disk = 0;
10206 sda.ignore_missing_paths = 0;
10207 sda.status_codes = NULL;
10208 if (asprintf(&ondisk_status_path, "%s/%s",
10209 got_worktree_get_root_path(worktree), path) == -1)
10210 return got_error_from_errno("asprintf");
10211 sda.status_path = ondisk_status_path;
10212 sda.status_path_len = strlen(ondisk_status_path);
10214 err = worktree_status(worktree, path, fileindex, repo,
10215 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10216 free(ondisk_status_path);
10217 return err;
10220 const struct got_error *
10221 got_worktree_patch_complete(struct got_fileindex *fileindex,
10222 const char *fileindex_path)
10224 const struct got_error *err = NULL;
10226 err = sync_fileindex(fileindex, fileindex_path);
10227 got_fileindex_free(fileindex);
10229 return err;