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 <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t
69 apply_umask(mode_t mode)
70 {
71 mode_t um;
73 um = umask(000);
74 umask(um);
75 return mode & ~um;
76 }
78 static const struct got_error *
79 create_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 char *path;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 return got_error_from_errno("asprintf");
87 err = got_path_create_file(path, content);
88 free(path);
89 return err;
90 }
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
94 {
95 const struct got_error *err = NULL;
96 FILE *tmpfile = NULL;
97 char *tmppath = NULL;
98 char *path = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno("asprintf");
102 path = NULL;
103 goto done;
106 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 if (err)
108 goto done;
110 if (content) {
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno2("fprintf", tmppath);
114 goto done;
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno3("rename", tmppath, path);
120 unlink(tmppath);
121 goto done;
124 done:
125 if (fclose(tmpfile) == EOF && err == NULL)
126 err = got_error_from_errno2("fclose", tmppath);
127 free(tmppath);
128 return err;
131 static const struct got_error *
132 write_head_ref(const char *path_got, struct got_reference *head_ref)
134 const struct got_error *err = NULL;
135 char *refstr = NULL;
137 if (got_ref_is_symbolic(head_ref)) {
138 refstr = got_ref_to_str(head_ref);
139 if (refstr == NULL)
140 return got_error_from_errno("got_ref_to_str");
141 } else {
142 refstr = strdup(got_ref_get_name(head_ref));
143 if (refstr == NULL)
144 return got_error_from_errno("strdup");
146 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
147 free(refstr);
148 return err;
151 const struct got_error *
152 got_worktree_init(const char *path, struct got_reference *head_ref,
153 const char *prefix, const char *meta_dir, struct got_repository *repo)
155 const struct got_error *err = NULL;
156 struct got_object_id *commit_id = NULL;
157 uuid_t uuid;
158 uint32_t uuid_status;
159 int obj_type;
160 char *path_got = NULL;
161 char *formatstr = NULL;
162 char *absprefix = NULL;
163 char *basestr = NULL;
164 char *uuidstr = NULL;
166 if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 err = got_error(GOT_ERR_WORKTREE_REPO);
168 goto done;
171 err = got_ref_resolve(&commit_id, repo, head_ref);
172 if (err)
173 return err;
174 err = got_object_get_type(&obj_type, repo, commit_id);
175 if (err)
176 return err;
177 if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 return got_error(GOT_ERR_OBJ_TYPE);
180 if (!got_path_is_absolute(prefix)) {
181 if (asprintf(&absprefix, "/%s", prefix) == -1)
182 return got_error_from_errno("asprintf");
185 /* Create top-level directory (may already exist). */
186 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path);
188 goto done;
191 /* Create .got/.cvg directory (may already exist). */
192 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 err = got_error_from_errno("asprintf");
194 goto done;
196 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 err = got_error_from_errno2("mkdir", path_got);
198 goto done;
201 /* Create an empty lock file. */
202 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 if (err)
204 goto done;
206 /* Create an empty file index. */
207 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 if (err)
209 goto done;
211 /* Write the HEAD reference. */
212 err = write_head_ref(path_got, head_ref);
213 if (err)
214 goto done;
216 /* Record our base commit. */
217 err = got_object_id_str(&basestr, commit_id);
218 if (err)
219 goto done;
220 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 if (err)
222 goto done;
224 /* Store path to repository. */
225 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 got_repo_get_path(repo));
227 if (err)
228 goto done;
230 /* Store in-repository path prefix. */
231 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 absprefix ? absprefix : prefix);
233 if (err)
234 goto done;
236 /* Generate UUID. */
237 uuid_create(&uuid, &uuid_status);
238 if (uuid_status != uuid_s_ok) {
239 err = got_error_uuid(uuid_status, "uuid_create");
240 goto done;
242 uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 if (uuid_status != uuid_s_ok) {
244 err = got_error_uuid(uuid_status, "uuid_to_string");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 if (err)
249 goto done;
251 /* Stamp work tree with format file. */
252 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 err = got_error_from_errno("asprintf");
254 goto done;
256 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 if (err)
258 goto done;
260 done:
261 free(commit_id);
262 free(path_got);
263 free(formatstr);
264 free(absprefix);
265 free(basestr);
266 free(uuidstr);
267 return err;
270 const struct got_error *
271 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 const char *path_prefix)
274 char *absprefix = NULL;
276 if (!got_path_is_absolute(path_prefix)) {
277 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 return got_error_from_errno("asprintf");
280 *match = (strcmp(absprefix ? absprefix : path_prefix,
281 worktree->path_prefix) == 0);
282 free(absprefix);
283 return NULL;
286 const char *
287 got_worktree_get_head_ref_name(struct got_worktree *worktree)
289 return worktree->head_ref_name;
292 const struct got_error *
293 got_worktree_set_head_ref(struct got_worktree *worktree,
294 struct got_reference *head_ref)
296 const struct got_error *err = NULL;
297 char *path_got = NULL, *head_ref_name = NULL;
299 if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 worktree->meta_dir) == -1) {
301 err = got_error_from_errno("asprintf");
302 path_got = NULL;
303 goto done;
306 head_ref_name = strdup(got_ref_get_name(head_ref));
307 if (head_ref_name == NULL) {
308 err = got_error_from_errno("strdup");
309 goto done;
312 err = write_head_ref(path_got, head_ref);
313 if (err)
314 goto done;
316 free(worktree->head_ref_name);
317 worktree->head_ref_name = head_ref_name;
318 done:
319 free(path_got);
320 if (err)
321 free(head_ref_name);
322 return err;
325 struct got_object_id *
326 got_worktree_get_base_commit_id(struct got_worktree *worktree)
328 return worktree->base_commit_id;
331 const struct got_error *
332 got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 struct got_repository *repo, struct got_object_id *commit_id)
335 const struct got_error *err;
336 struct got_object *obj = NULL;
337 char *id_str = NULL;
338 char *path_got = NULL;
340 if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 worktree->meta_dir) == -1) {
342 err = got_error_from_errno("asprintf");
343 path_got = NULL;
344 goto done;
347 err = got_object_open(&obj, repo, commit_id);
348 if (err)
349 return err;
351 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
353 goto done;
356 /* Record our base commit. */
357 err = got_object_id_str(&id_str, commit_id);
358 if (err)
359 goto done;
360 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 if (err)
362 goto done;
364 free(worktree->base_commit_id);
365 worktree->base_commit_id = got_object_id_dup(commit_id);
366 if (worktree->base_commit_id == NULL) {
367 err = got_error_from_errno("got_object_id_dup");
368 goto done;
370 done:
371 if (obj)
372 got_object_close(obj);
373 free(id_str);
374 free(path_got);
375 return err;
378 const struct got_gotconfig *
379 got_worktree_get_gotconfig(struct got_worktree *worktree)
381 return worktree->gotconfig;
384 static const struct got_error *
385 lock_worktree(struct got_worktree *worktree, int operation)
387 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 : got_error_from_errno2("flock",
390 got_worktree_get_root_path(worktree)));
391 return NULL;
394 static const struct got_error *
395 add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 const struct got_error *err = NULL;
398 char *abspath;
400 /* We only accept worktree-relative paths here. */
401 if (got_path_is_absolute(path)) {
402 return got_error_fmt(GOT_ERR_BAD_PATH,
403 "%s does not accept absolute paths: %s",
404 __func__, path);
407 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 return got_error_from_errno("asprintf");
410 err = got_path_mkdir(abspath);
411 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 struct stat sb;
413 err = NULL;
414 if (lstat(abspath, &sb) == -1) {
415 err = got_error_from_errno2("lstat", abspath);
416 } else if (!S_ISDIR(sb.st_mode)) {
417 /* TODO directory is obstructed; do something */
418 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
421 free(abspath);
422 return err;
425 static const struct got_error *
426 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 const struct got_error *err = NULL;
429 uint8_t fbuf1[8192];
430 uint8_t fbuf2[8192];
431 size_t flen1 = 0, flen2 = 0;
433 *same = 1;
435 for (;;) {
436 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 if (flen1 == 0 && ferror(f1)) {
438 err = got_error_from_errno("fread");
439 break;
441 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 if (flen2 == 0 && ferror(f2)) {
443 err = got_error_from_errno("fread");
444 break;
446 if (flen1 == 0) {
447 if (flen2 != 0)
448 *same = 0;
449 break;
450 } else if (flen2 == 0) {
451 if (flen1 != 0)
452 *same = 0;
453 break;
454 } else if (flen1 == flen2) {
455 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
456 *same = 0;
457 break;
459 } else {
460 *same = 0;
461 break;
465 return err;
468 static const struct got_error *
469 check_files_equal(int *same, FILE *f1, FILE *f2)
471 struct stat sb;
472 size_t size1, size2;
474 *same = 1;
476 if (fstat(fileno(f1), &sb) != 0)
477 return got_error_from_errno("fstat");
478 size1 = sb.st_size;
480 if (fstat(fileno(f2), &sb) != 0)
481 return got_error_from_errno("fstat");
482 size2 = sb.st_size;
484 if (size1 != size2) {
485 *same = 0;
486 return NULL;
489 if (fseek(f1, 0L, SEEK_SET) == -1)
490 return got_ferror(f1, GOT_ERR_IO);
491 if (fseek(f2, 0L, SEEK_SET) == -1)
492 return got_ferror(f2, GOT_ERR_IO);
494 return check_file_contents_equal(same, f1, f2);
497 static const struct got_error *
498 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
500 uint8_t fbuf[65536];
501 size_t flen;
502 ssize_t outlen;
504 *outsize = 0;
506 if (fseek(f, 0L, SEEK_SET) == -1)
507 return got_ferror(f, GOT_ERR_IO);
509 for (;;) {
510 flen = fread(fbuf, 1, sizeof(fbuf), f);
511 if (flen == 0) {
512 if (ferror(f))
513 return got_error_from_errno("fread");
514 if (feof(f))
515 break;
517 outlen = write(outfd, fbuf, flen);
518 if (outlen == -1)
519 return got_error_from_errno("write");
520 if (outlen != flen)
521 return got_error(GOT_ERR_IO);
522 *outsize += outlen;
525 return NULL;
528 static const struct got_error *
529 merge_binary_file(int *overlapcnt, int merged_fd,
530 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 const char *ondisk_path)
534 const struct got_error *err = NULL;
535 int same_content, changed_deriv, changed_deriv2;
536 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 char *base_path_orig = NULL, *base_path_deriv = NULL;
540 char *base_path_deriv2 = NULL;
542 *overlapcnt = 0;
544 err = check_files_equal(&same_content, f_deriv, f_deriv2);
545 if (err)
546 return err;
548 if (same_content)
549 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 err = check_files_equal(&same_content, f_deriv, f_orig);
552 if (err)
553 return err;
554 changed_deriv = !same_content;
555 err = check_files_equal(&same_content, f_deriv2, f_orig);
556 if (err)
557 return err;
558 changed_deriv2 = !same_content;
560 if (changed_deriv && changed_deriv2) {
561 *overlapcnt = 1;
562 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 err = got_error_from_errno("asprintf");
564 goto done;
566 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 err = got_error_from_errno("asprintf");
568 goto done;
570 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 err = got_error_from_errno("asprintf");
572 goto done;
574 err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 base_path_orig, "");
576 if (err)
577 goto done;
578 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 base_path_deriv, "");
580 if (err)
581 goto done;
582 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 base_path_deriv2, "");
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
590 if (err)
591 goto done;
592 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
593 if (err)
594 goto done;
595 if (dprintf(merged_fd, "Binary files differ and cannot be "
596 "merged automatically:\n") < 0) {
597 err = got_error_from_errno("dprintf");
598 goto done;
600 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 label_deriv ? " " : "",
603 label_deriv ? label_deriv : "",
604 path_deriv) < 0) {
605 err = got_error_from_errno("dprintf");
606 goto done;
608 if (size_orig > 0) {
609 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 GOT_DIFF_CONFLICT_MARKER_ORIG,
611 label_orig ? " " : "",
612 label_orig ? label_orig : "",
613 path_orig) < 0) {
614 err = got_error_from_errno("dprintf");
615 goto done;
618 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 GOT_DIFF_CONFLICT_MARKER_SEP,
620 path_deriv2,
621 GOT_DIFF_CONFLICT_MARKER_END,
622 label_deriv2 ? " " : "",
623 label_deriv2 ? label_deriv2 : "") < 0) {
624 err = got_error_from_errno("dprintf");
625 goto done;
627 } else if (changed_deriv)
628 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 else if (changed_deriv2)
630 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
631 done:
632 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 err == NULL)
634 err = got_error_from_errno2("unlink", path_orig);
635 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 err = got_error_from_errno2("close", path_orig);
637 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 err = got_error_from_errno2("close", path_deriv);
639 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 err = got_error_from_errno2("close", path_deriv2);
641 free(path_orig);
642 free(path_deriv);
643 free(path_deriv2);
644 free(base_path_orig);
645 free(base_path_deriv);
646 free(base_path_deriv2);
647 return err;
650 /*
651 * Perform a 3-way merge where the file f_orig acts as the common
652 * ancestor, the file f_deriv acts as the first derived version,
653 * and the file f_deriv2 acts as the second derived version.
654 * The merge result will be written to a new file at ondisk_path; any
655 * existing file at this path will be replaced.
656 */
657 static const struct got_error *
658 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 const char *path, uint16_t st_mode,
661 const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg)
665 const struct got_error *err = NULL;
666 int merged_fd = -1;
667 FILE *f_merged = NULL;
668 char *merged_path = NULL, *base_path = NULL;
669 int overlapcnt = 0;
670 char *parent = NULL;
672 *local_changes_subsumed = 0;
674 err = got_path_dirname(&parent, ondisk_path);
675 if (err)
676 return err;
678 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 err = got_error_from_errno("asprintf");
680 goto done;
683 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 if (err)
685 goto done;
687 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
689 if (err) {
690 if (err->code != GOT_ERR_FILE_BINARY)
691 goto done;
692 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 ondisk_path);
695 if (err)
696 goto done;
699 err = (*progress_cb)(progress_arg,
700 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 if (err)
702 goto done;
704 if (fsync(merged_fd) != 0) {
705 err = got_error_from_errno("fsync");
706 goto done;
709 f_merged = fdopen(merged_fd, "r");
710 if (f_merged == NULL) {
711 err = got_error_from_errno("fdopen");
712 goto done;
714 merged_fd = -1;
716 /* Check if a clean merge has subsumed all local changes. */
717 if (overlapcnt == 0) {
718 err = check_files_equal(local_changes_subsumed, f_deriv,
719 f_merged);
720 if (err)
721 goto done;
724 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 err = got_error_from_errno2("fchmod", merged_path);
726 goto done;
729 if (rename(merged_path, ondisk_path) != 0) {
730 err = got_error_from_errno3("rename", merged_path,
731 ondisk_path);
732 goto done;
734 done:
735 if (err) {
736 if (merged_path)
737 unlink(merged_path);
739 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 err = got_error_from_errno("fclose");
743 free(merged_path);
744 free(base_path);
745 free(parent);
746 return err;
749 static const struct got_error *
750 update_symlink(const char *ondisk_path, const char *target_path,
751 size_t target_len)
753 /* This is not atomic but matches what 'ln -sf' does. */
754 if (unlink(ondisk_path) == -1)
755 return got_error_from_errno2("unlink", ondisk_path);
756 if (symlink(target_path, ondisk_path) == -1)
757 return got_error_from_errno3("symlink", target_path,
758 ondisk_path);
759 return NULL;
762 /*
763 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 * in the work tree with a file that contains conflict markers and the
765 * conflicting target paths of the original version, a "derived version"
766 * of a symlink from an incoming change, and a local version of the symlink.
768 * The original versions's target path can be NULL if it is not available,
769 * such as if both derived versions added a new symlink at the same path.
771 * The incoming derived symlink target is NULL in case the incoming change
772 * has deleted this symlink.
773 */
774 static const struct got_error *
775 install_symlink_conflict(const char *deriv_target,
776 struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 const char *label_orig, const char *local_target, const char *ondisk_path)
779 const struct got_error *err;
780 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 FILE *f = NULL;
783 err = got_object_id_str(&id_str, deriv_base_commit_id);
784 if (err)
785 return got_error_from_errno("asprintf");
787 if (asprintf(&label_deriv, "%s: commit %s",
788 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
793 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 if (err)
795 goto done;
797 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 err = got_error_from_errno2("fchmod", path);
799 goto done;
802 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 deriv_target ? deriv_target : "(symlink was deleted)",
805 orig_target ? label_orig : "",
806 orig_target ? "\n" : "",
807 orig_target ? orig_target : "",
808 orig_target ? "\n" : "",
809 GOT_DIFF_CONFLICT_MARKER_SEP,
810 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 err = got_error_from_errno2("fprintf", path);
812 goto done;
815 if (unlink(ondisk_path) == -1) {
816 err = got_error_from_errno2("unlink", ondisk_path);
817 goto done;
819 if (rename(path, ondisk_path) == -1) {
820 err = got_error_from_errno3("rename", path, ondisk_path);
821 goto done;
823 done:
824 if (f != NULL && fclose(f) == EOF && err == NULL)
825 err = got_error_from_errno2("fclose", path);
826 free(path);
827 free(id_str);
828 free(label_deriv);
829 return err;
832 /* forward declaration */
833 static const struct got_error *
834 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 const char *, const char *, uint16_t, const char *,
836 struct got_blob_object *, struct got_object_id *,
837 struct got_repository *, got_worktree_checkout_cb, void *);
839 /*
840 * Merge a symlink into the work tree, where blob_orig acts as the common
841 * ancestor, deriv_target is the link target of the first derived version,
842 * and the symlink on disk acts as the second derived version.
843 * Assume that contents of both blobs represent symlinks.
844 */
845 static const struct got_error *
846 merge_symlink(struct got_worktree *worktree,
847 struct got_blob_object *blob_orig, const char *ondisk_path,
848 const char *path, const char *label_orig, const char *deriv_target,
849 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 got_worktree_checkout_cb progress_cb, void *progress_arg)
852 const struct got_error *err = NULL;
853 char *ancestor_target = NULL;
854 struct stat sb;
855 ssize_t ondisk_len, deriv_len;
856 char ondisk_target[PATH_MAX];
857 int have_local_change = 0;
858 int have_incoming_change = 0;
860 if (lstat(ondisk_path, &sb) == -1)
861 return got_error_from_errno2("lstat", ondisk_path);
863 ondisk_len = readlink(ondisk_path, ondisk_target,
864 sizeof(ondisk_target));
865 if (ondisk_len == -1) {
866 err = got_error_from_errno2("readlink",
867 ondisk_path);
868 goto done;
870 ondisk_target[ondisk_len] = '\0';
872 if (blob_orig) {
873 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
874 if (err)
875 goto done;
878 if (ancestor_target == NULL ||
879 (ondisk_len != strlen(ancestor_target) ||
880 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 have_local_change = 1;
883 deriv_len = strlen(deriv_target);
884 if (ancestor_target == NULL ||
885 (deriv_len != strlen(ancestor_target) ||
886 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 have_incoming_change = 1;
889 if (!have_local_change && !have_incoming_change) {
890 if (ancestor_target) {
891 /* Both sides made the same change. */
892 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
893 path);
894 } else if (deriv_len == ondisk_len &&
895 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 /* Both sides added the same symlink. */
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
898 path);
899 } else {
900 /* Both sides added symlinks which don't match. */
901 err = install_symlink_conflict(deriv_target,
902 deriv_base_commit_id, ancestor_target,
903 label_orig, ondisk_target, ondisk_path);
904 if (err)
905 goto done;
906 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
907 path);
909 } else if (!have_local_change && have_incoming_change) {
910 /* Apply the incoming change. */
911 err = update_symlink(ondisk_path, deriv_target,
912 strlen(deriv_target));
913 if (err)
914 goto done;
915 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 } else if (have_local_change && have_incoming_change) {
917 if (deriv_len == ondisk_len &&
918 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 /* Both sides made the same change. */
920 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
921 path);
922 } else {
923 err = install_symlink_conflict(deriv_target,
924 deriv_base_commit_id, ancestor_target, label_orig,
925 ondisk_target, ondisk_path);
926 if (err)
927 goto done;
928 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
929 path);
933 done:
934 free(ancestor_target);
935 return err;
938 static const struct got_error *
939 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 const struct got_error *err = NULL;
942 char target_path[PATH_MAX];
943 ssize_t target_len;
944 size_t n;
945 FILE *f;
947 *outfile = NULL;
949 f = got_opentemp();
950 if (f == NULL)
951 return got_error_from_errno("got_opentemp");
952 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 if (target_len == -1) {
954 err = got_error_from_errno2("readlink", ondisk_path);
955 goto done;
957 n = fwrite(target_path, 1, target_len, f);
958 if (n != target_len) {
959 err = got_ferror(f, GOT_ERR_IO);
960 goto done;
962 if (fflush(f) == EOF) {
963 err = got_error_from_errno("fflush");
964 goto done;
966 if (fseek(f, 0L, SEEK_SET) == -1) {
967 err = got_ferror(f, GOT_ERR_IO);
968 goto done;
970 done:
971 if (err)
972 fclose(f);
973 else
974 *outfile = f;
975 return err;
978 /*
979 * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 * blob_deriv acts as the first derived version, and the file on disk
981 * acts as the second derived version.
982 */
983 static const struct got_error *
984 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 struct got_blob_object *blob_orig, const char *ondisk_path,
986 const char *path, uint16_t st_mode, const char *label_orig,
987 struct got_blob_object *blob_deriv,
988 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 got_worktree_checkout_cb progress_cb, void *progress_arg)
991 const struct got_error *err = NULL;
992 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 char *blob_orig_path = NULL;
994 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 char *label_deriv = NULL, *parent = NULL;
997 *local_changes_subsumed = 0;
999 err = got_path_dirname(&parent, ondisk_path);
1000 if (err)
1001 return err;
1003 if (blob_orig) {
1004 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 parent) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 base_path = NULL;
1008 goto done;
1011 err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 base_path, "");
1013 if (err)
1014 goto done;
1015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 blob_orig);
1017 if (err)
1018 goto done;
1019 free(base_path);
1020 } else {
1022 * No common ancestor exists. This is an "add vs add" conflict
1023 * and we simply use an empty ancestor file to make both files
1024 * appear in the merged result in their entirety.
1026 f_orig = got_opentemp();
1027 if (f_orig == NULL) {
1028 err = got_error_from_errno("got_opentemp");
1029 goto done;
1033 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 base_path = NULL;
1036 goto done;
1039 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1040 if (err)
1041 goto done;
1042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 blob_deriv);
1044 if (err)
1045 goto done;
1047 err = got_object_id_str(&id_str, deriv_base_commit_id);
1048 if (err)
1049 goto done;
1050 if (asprintf(&label_deriv, "%s: commit %s",
1051 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 err = got_error_from_errno("asprintf");
1053 goto done;
1057 * In order the run a 3-way merge with a symlink we copy the symlink's
1058 * target path into a temporary file and use that file with diff3.
1060 if (S_ISLNK(st_mode)) {
1061 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1062 if (err)
1063 goto done;
1064 } else {
1065 int fd;
1066 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 if (fd == -1) {
1068 err = got_error_from_errno2("open", ondisk_path);
1069 goto done;
1071 f_deriv2 = fdopen(fd, "r");
1072 if (f_deriv2 == NULL) {
1073 err = got_error_from_errno2("fdopen", ondisk_path);
1074 close(fd);
1075 goto done;
1079 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1082 done:
1083 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 err = got_error_from_errno("fclose");
1085 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 err = got_error_from_errno("fclose");
1087 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1089 free(base_path);
1090 if (blob_orig_path) {
1091 unlink(blob_orig_path);
1092 free(blob_orig_path);
1094 if (blob_deriv_path) {
1095 unlink(blob_deriv_path);
1096 free(blob_deriv_path);
1098 free(id_str);
1099 free(label_deriv);
1100 free(parent);
1101 return err;
1104 static const struct got_error *
1105 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 int wt_fd, const char *path, struct got_object_id *blob_id)
1109 const struct got_error *err = NULL;
1110 struct got_fileindex_entry *new_ie;
1112 *new_iep = NULL;
1114 err = got_fileindex_entry_alloc(&new_ie, path);
1115 if (err)
1116 return err;
1118 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1119 blob_id->sha1, base_commit_id->sha1, 1);
1120 if (err)
1121 goto done;
1123 err = got_fileindex_entry_add(fileindex, new_ie);
1124 done:
1125 if (err)
1126 got_fileindex_entry_free(new_ie);
1127 else
1128 *new_iep = new_ie;
1129 return err;
1132 static mode_t
1133 get_ondisk_perms(int executable, mode_t st_mode)
1135 mode_t xbits = S_IXUSR;
1137 if (executable) {
1138 /* Map read bits to execute bits. */
1139 if (st_mode & S_IRGRP)
1140 xbits |= S_IXGRP;
1141 if (st_mode & S_IROTH)
1142 xbits |= S_IXOTH;
1143 return st_mode | xbits;
1146 return st_mode;
1149 /* forward declaration */
1150 static const struct got_error *
1151 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1152 const char *path, mode_t te_mode, mode_t st_mode,
1153 struct got_blob_object *blob, int restoring_missing_file,
1154 int reverting_versioned_file, int installing_bad_symlink,
1155 int path_is_unversioned, struct got_repository *repo,
1156 got_worktree_checkout_cb progress_cb, void *progress_arg);
1159 * This function assumes that the provided symlink target points at a
1160 * safe location in the work tree!
1162 static const struct got_error *
1163 replace_existing_symlink(int *did_something, const char *ondisk_path,
1164 const char *target_path, size_t target_len)
1166 const struct got_error *err = NULL;
1167 ssize_t elen;
1168 char etarget[PATH_MAX];
1169 int fd;
1171 *did_something = 0;
1174 * "Bad" symlinks (those pointing outside the work tree or into the
1175 * .got directory) are installed in the work tree as a regular file
1176 * which contains the bad symlink target path.
1177 * The new symlink target has already been checked for safety by our
1178 * caller. If we can successfully open a regular file then we simply
1179 * replace this file with a symlink below.
1181 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1182 if (fd == -1) {
1183 if (!got_err_open_nofollow_on_symlink())
1184 return got_error_from_errno2("open", ondisk_path);
1186 /* We are updating an existing on-disk symlink. */
1187 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1188 if (elen == -1)
1189 return got_error_from_errno2("readlink", ondisk_path);
1191 if (elen == target_len &&
1192 memcmp(etarget, target_path, target_len) == 0)
1193 return NULL; /* nothing to do */
1196 *did_something = 1;
1197 err = update_symlink(ondisk_path, target_path, target_len);
1198 if (fd != -1 && close(fd) == -1 && err == NULL)
1199 err = got_error_from_errno2("close", ondisk_path);
1200 return err;
1203 static const struct got_error *
1204 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1205 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1206 const char *meta_dir)
1208 const struct got_error *err = NULL;
1209 char canonpath[PATH_MAX];
1210 char *path_got = NULL;
1212 *is_bad_symlink = 0;
1214 if (target_len >= sizeof(canonpath)) {
1215 *is_bad_symlink = 1;
1216 return NULL;
1220 * We do not use realpath(3) to resolve the symlink's target
1221 * path because we don't want to resolve symlinks recursively.
1222 * Instead we make the path absolute and then canonicalize it.
1223 * Relative symlink target lookup should begin at the directory
1224 * in which the blob object is being installed.
1226 if (!got_path_is_absolute(target_path)) {
1227 char *abspath, *parent;
1228 err = got_path_dirname(&parent, ondisk_path);
1229 if (err)
1230 return err;
1231 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1232 free(parent);
1233 return got_error_from_errno("asprintf");
1235 free(parent);
1236 if (strlen(abspath) >= sizeof(canonpath)) {
1237 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1238 free(abspath);
1239 return err;
1241 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1242 free(abspath);
1243 if (err)
1244 return err;
1245 } else {
1246 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1247 if (err)
1248 return err;
1251 /* Only allow symlinks pointing at paths within the work tree. */
1252 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1253 *is_bad_symlink = 1;
1254 return NULL;
1257 /* Do not allow symlinks pointing into the meta directory. */
1258 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1259 return got_error_from_errno("asprintf");
1260 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1261 *is_bad_symlink = 1;
1263 free(path_got);
1264 return NULL;
1267 static const struct got_error *
1268 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1269 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1270 int restoring_missing_file, int reverting_versioned_file,
1271 int path_is_unversioned, int allow_bad_symlinks,
1272 struct got_repository *repo,
1273 got_worktree_checkout_cb progress_cb, void *progress_arg)
1275 const struct got_error *err = NULL;
1276 char target_path[PATH_MAX];
1277 size_t len, target_len = 0;
1278 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1279 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1281 *is_bad_symlink = 0;
1284 * Blob object content specifies the target path of the link.
1285 * If a symbolic link cannot be installed we instead create
1286 * a regular file which contains the link target path stored
1287 * in the blob object.
1289 do {
1290 err = got_object_blob_read_block(&len, blob);
1291 if (err)
1292 return err;
1294 if (len + target_len >= sizeof(target_path)) {
1295 /* Path too long; install as a regular file. */
1296 *is_bad_symlink = 1;
1297 got_object_blob_rewind(blob);
1298 return install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file,
1301 1, path_is_unversioned, repo, progress_cb,
1302 progress_arg);
1304 if (len > 0) {
1305 /* Skip blob object header first time around. */
1306 memcpy(target_path + target_len, buf + hdrlen,
1307 len - hdrlen);
1308 target_len += len - hdrlen;
1309 hdrlen = 0;
1311 } while (len != 0);
1312 target_path[target_len] = '\0';
1314 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1315 ondisk_path, worktree->root_path, worktree->meta_dir);
1316 if (err)
1317 return err;
1319 if (*is_bad_symlink && !allow_bad_symlinks) {
1320 /* install as a regular file */
1321 got_object_blob_rewind(blob);
1322 err = install_blob(worktree, ondisk_path, path,
1323 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1324 restoring_missing_file, reverting_versioned_file, 1,
1325 path_is_unversioned, repo, 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, 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, struct got_repository *repo,
1409 got_worktree_checkout_cb progress_cb, void *progress_arg)
1411 const struct got_error *err = NULL;
1412 int fd = -1;
1413 size_t len, hdrlen;
1414 int update = 0;
1415 char *tmppath = NULL;
1416 mode_t mode;
1418 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1419 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1420 O_CLOEXEC, mode);
1421 if (fd == -1) {
1422 if (errno == ENOENT || errno == ENOTDIR) {
1423 char *parent;
1424 err = got_path_dirname(&parent, path);
1425 if (err)
1426 return err;
1427 err = add_dir_on_disk(worktree, parent);
1428 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1429 err = got_error_path(path, err->code);
1430 free(parent);
1431 if (err)
1432 return err;
1433 fd = open(ondisk_path,
1434 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1435 mode);
1436 if (fd == -1)
1437 return got_error_from_errno2("open",
1438 ondisk_path);
1439 } else if (errno == EEXIST) {
1440 if (path_is_unversioned) {
1441 err = (*progress_cb)(progress_arg,
1442 GOT_STATUS_UNVERSIONED, path);
1443 goto done;
1445 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1446 !S_ISREG(st_mode) && !installing_bad_symlink) {
1447 /* TODO file is obstructed; do something */
1448 err = got_error_path(ondisk_path,
1449 GOT_ERR_FILE_OBSTRUCTED);
1450 goto done;
1451 } else {
1452 err = got_opentemp_named_fd(&tmppath, &fd,
1453 ondisk_path, "");
1454 if (err)
1455 goto done;
1456 update = 1;
1458 if (fchmod(fd, apply_umask(mode)) == -1) {
1459 err = got_error_from_errno2("fchmod",
1460 tmppath);
1461 goto done;
1464 } else
1465 return got_error_from_errno2("open", ondisk_path);
1468 if (progress_cb) {
1469 if (restoring_missing_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1471 path);
1472 else if (reverting_versioned_file)
1473 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1474 path);
1475 else
1476 err = (*progress_cb)(progress_arg,
1477 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1478 if (err)
1479 goto done;
1482 hdrlen = got_object_blob_get_hdrlen(blob);
1483 do {
1484 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1485 err = got_object_blob_read_block(&len, blob);
1486 if (err)
1487 break;
1488 if (len > 0) {
1489 /* Skip blob object header first time around. */
1490 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1491 if (outlen == -1) {
1492 err = got_error_from_errno("write");
1493 goto done;
1494 } else if (outlen != len - hdrlen) {
1495 err = got_error(GOT_ERR_IO);
1496 goto done;
1498 hdrlen = 0;
1500 } while (len != 0);
1502 if (fsync(fd) != 0) {
1503 err = got_error_from_errno("fsync");
1504 goto done;
1507 if (update) {
1508 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1509 err = got_error_from_errno2("unlink", ondisk_path);
1510 goto done;
1512 if (rename(tmppath, ondisk_path) != 0) {
1513 err = got_error_from_errno3("rename", tmppath,
1514 ondisk_path);
1515 goto done;
1517 free(tmppath);
1518 tmppath = NULL;
1521 done:
1522 if (fd != -1 && close(fd) == -1 && err == NULL)
1523 err = got_error_from_errno("close");
1524 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1525 err = got_error_from_errno2("unlink", tmppath);
1526 free(tmppath);
1527 return err;
1531 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1532 * conflict marker is found in newly added lines only.
1534 static const struct got_error *
1535 get_modified_file_content_status(unsigned char *status,
1536 struct got_blob_object *blob, const char *path, struct stat *sb,
1537 FILE *ondisk_file)
1539 const struct got_error *err, *free_err;
1540 const char *markers[3] = {
1541 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 GOT_DIFF_CONFLICT_MARKER_SEP,
1543 GOT_DIFF_CONFLICT_MARKER_END
1545 FILE *f1 = NULL;
1546 struct got_diffreg_result *diffreg_result = NULL;
1547 struct diff_result *r;
1548 int nchunks_parsed, n, i = 0, ln = 0;
1549 char *line = NULL;
1550 size_t linesize = 0;
1551 ssize_t linelen;
1553 if (*status != GOT_STATUS_MODIFY)
1554 return NULL;
1556 f1 = got_opentemp();
1557 if (f1 == NULL)
1558 return got_error_from_errno("got_opentemp");
1560 if (blob) {
1561 got_object_blob_rewind(blob);
1562 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1563 if (err)
1564 goto done;
1567 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1568 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1569 if (err)
1570 goto done;
1572 r = diffreg_result->result;
1574 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1575 struct diff_chunk *c;
1576 struct diff_chunk_context cc = {};
1577 off_t pos;
1580 * We can optimise a little by advancing straight
1581 * to the next chunk if this one has no added lines.
1583 c = diff_chunk_get(r, n);
1585 if (diff_chunk_type(c) != CHUNK_PLUS) {
1586 nchunks_parsed = 1;
1587 continue; /* removed or unchanged lines */
1590 pos = diff_chunk_get_right_start_pos(c);
1591 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1592 err = got_ferror(ondisk_file, GOT_ERR_IO);
1593 goto done;
1596 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1597 ln = cc.right.start;
1599 while (ln < cc.right.end) {
1600 linelen = getline(&line, &linesize, ondisk_file);
1601 if (linelen == -1) {
1602 if (feof(ondisk_file))
1603 break;
1604 err = got_ferror(ondisk_file, GOT_ERR_IO);
1605 break;
1608 if (line && strncmp(line, markers[i],
1609 strlen(markers[i])) == 0) {
1610 if (strcmp(markers[i],
1611 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1612 *status = GOT_STATUS_CONFLICT;
1613 goto done;
1614 } else
1615 i++;
1617 ++ln;
1621 done:
1622 free(line);
1623 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1624 err = got_error_from_errno("fclose");
1625 free_err = got_diffreg_result_free(diffreg_result);
1626 if (err == NULL)
1627 err = free_err;
1629 return err;
1632 static int
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1639 static int
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1660 default:
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1673 ssize_t elen;
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1681 do {
1682 err = got_object_blob_read_block(&len, blob);
1683 if (err)
1684 return err;
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1692 if (len > 0) {
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1695 len - hdrlen);
1696 target_len += len - hdrlen;
1697 hdrlen = 0;
1699 } while (len != 0);
1700 target_path[target_len] = '\0';
1702 if (dirfd != -1) {
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 if (elen == -1)
1705 return got_error_from_errno2("readlinkat", abspath);
1706 } else {
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1708 if (elen == -1)
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1715 return NULL;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1725 size_t hdrlen;
1726 int fd = -1, fd1 = -1;
1727 FILE *f = NULL;
1728 uint8_t fbuf[8192];
1729 struct got_blob_object *blob = NULL;
1730 size_t flen, blen;
1731 unsigned char staged_status;
1733 staged_status = get_staged_status(ie);
1734 *status = GOT_STATUS_NO_CHANGE;
1735 memset(sb, 0, sizeof(*sb));
1738 * Whenever the caller provides a directory descriptor and a
1739 * directory entry name for the file, use them! This prevents
1740 * race conditions if filesystem paths change beneath our feet.
1742 if (dirfd != -1) {
1743 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1744 if (errno == ENOENT) {
1745 if (got_fileindex_entry_has_file_on_disk(ie))
1746 *status = GOT_STATUS_MISSING;
1747 else
1748 *status = GOT_STATUS_DELETE;
1749 goto done;
1751 err = got_error_from_errno2("fstatat", abspath);
1752 goto done;
1754 } else {
1755 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1756 if (fd == -1 && errno != ENOENT &&
1757 !got_err_open_nofollow_on_symlink())
1758 return got_error_from_errno2("open", abspath);
1759 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1760 if (lstat(abspath, sb) == -1)
1761 return got_error_from_errno2("lstat", abspath);
1762 } else if (fd == -1 || fstat(fd, sb) == -1) {
1763 if (errno == ENOENT) {
1764 if (got_fileindex_entry_has_file_on_disk(ie))
1765 *status = GOT_STATUS_MISSING;
1766 else
1767 *status = GOT_STATUS_DELETE;
1768 goto done;
1770 err = got_error_from_errno2("fstat", abspath);
1771 goto done;
1775 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1776 *status = GOT_STATUS_OBSTRUCTED;
1777 goto done;
1780 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1781 *status = GOT_STATUS_DELETE;
1782 goto done;
1783 } else if (!got_fileindex_entry_has_blob(ie) &&
1784 staged_status != GOT_STATUS_ADD) {
1785 *status = GOT_STATUS_ADD;
1786 goto done;
1789 if (!stat_info_differs(ie, sb))
1790 goto done;
1792 if (S_ISLNK(sb->st_mode) &&
1793 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1794 *status = GOT_STATUS_MODIFY;
1795 goto done;
1798 if (staged_status == GOT_STATUS_MODIFY ||
1799 staged_status == GOT_STATUS_ADD)
1800 got_fileindex_entry_get_staged_blob_id(&id, ie);
1801 else
1802 got_fileindex_entry_get_blob_id(&id, ie);
1804 fd1 = got_opentempfd();
1805 if (fd1 == -1) {
1806 err = got_error_from_errno("got_opentempfd");
1807 goto done;
1809 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1810 if (err)
1811 goto done;
1813 if (S_ISLNK(sb->st_mode)) {
1814 err = get_symlink_modification_status(status, ie,
1815 abspath, dirfd, de_name, blob);
1816 goto done;
1819 if (dirfd != -1) {
1820 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1821 if (fd == -1) {
1822 err = got_error_from_errno2("openat", abspath);
1823 goto done;
1827 f = fdopen(fd, "r");
1828 if (f == NULL) {
1829 err = got_error_from_errno2("fdopen", abspath);
1830 goto done;
1832 fd = -1;
1833 hdrlen = got_object_blob_get_hdrlen(blob);
1834 for (;;) {
1835 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1836 err = got_object_blob_read_block(&blen, blob);
1837 if (err)
1838 goto done;
1839 /* Skip length of blob object header first time around. */
1840 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1841 if (flen == 0 && ferror(f)) {
1842 err = got_error_from_errno("fread");
1843 goto done;
1845 if (blen - hdrlen == 0) {
1846 if (flen != 0)
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1849 } else if (flen == 0) {
1850 if (blen - hdrlen != 0)
1851 *status = GOT_STATUS_MODIFY;
1852 break;
1853 } else if (blen - hdrlen == flen) {
1854 /* Skip blob object header first time around. */
1855 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1856 *status = GOT_STATUS_MODIFY;
1857 break;
1859 } else {
1860 *status = GOT_STATUS_MODIFY;
1861 break;
1863 hdrlen = 0;
1866 if (*status == GOT_STATUS_MODIFY) {
1867 rewind(f);
1868 err = get_modified_file_content_status(status, blob, ie->path,
1869 sb, f);
1870 } else if (xbit_differs(ie, sb->st_mode))
1871 *status = GOT_STATUS_MODE_CHANGE;
1872 done:
1873 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1874 err = got_error_from_errno("close");
1875 if (blob)
1876 got_object_blob_close(blob);
1877 if (f != NULL && fclose(f) == EOF && err == NULL)
1878 err = got_error_from_errno2("fclose", abspath);
1879 if (fd != -1 && close(fd) == -1 && err == NULL)
1880 err = got_error_from_errno2("close", abspath);
1881 return err;
1885 * Update timestamps in the file index if a file is unmodified and
1886 * we had to run a full content comparison to find out.
1888 static const struct got_error *
1889 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1890 struct got_fileindex_entry *ie, struct stat *sb)
1892 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1893 return got_fileindex_entry_update(ie, wt_fd, path,
1894 ie->blob_sha1, ie->commit_sha1, 1);
1896 return NULL;
1899 static const struct got_error *remove_ondisk_file(const char *, const char *);
1901 static const struct got_error *
1902 update_blob(struct got_worktree *worktree,
1903 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1904 struct got_tree_entry *te, const char *path,
1905 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1906 void *progress_arg)
1908 const struct got_error *err = NULL;
1909 struct got_blob_object *blob = NULL;
1910 char *ondisk_path = NULL;
1911 unsigned char status = GOT_STATUS_NO_CHANGE;
1912 struct stat sb;
1913 int fd1 = -1, fd2 = -1;
1915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1916 return got_error_from_errno("asprintf");
1918 if (ie) {
1919 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1920 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1921 goto done;
1923 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1924 repo);
1925 if (err)
1926 goto done;
1927 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1928 sb.st_mode = got_fileindex_perms_to_st(ie);
1929 } else {
1930 if (stat(ondisk_path, &sb) == -1) {
1931 if (errno != ENOENT && errno != ENOTDIR) {
1932 err = got_error_from_errno2("stat",
1933 ondisk_path);
1934 goto done;
1936 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1937 status = GOT_STATUS_UNVERSIONED;
1938 } else {
1939 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1940 status = GOT_STATUS_UNVERSIONED;
1941 else
1942 status = GOT_STATUS_OBSTRUCTED;
1946 if (status == GOT_STATUS_OBSTRUCTED) {
1947 if (ie)
1948 got_fileindex_entry_mark_skipped(ie);
1949 err = (*progress_cb)(progress_arg, status, path);
1950 goto done;
1952 if (status == GOT_STATUS_CONFLICT) {
1953 if (ie)
1954 got_fileindex_entry_mark_skipped(ie);
1955 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1956 path);
1957 goto done;
1960 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1961 if (status == GOT_STATUS_UNVERSIONED) {
1962 err = (*progress_cb)(progress_arg, status, path);
1963 } else if (status != GOT_STATUS_NO_CHANGE &&
1964 status != GOT_STATUS_DELETE &&
1965 status != GOT_STATUS_NONEXISTENT &&
1966 status != GOT_STATUS_MISSING) {
1967 err = (*progress_cb)(progress_arg,
1968 GOT_STATUS_CANNOT_DELETE, path);
1969 } else if (ie) {
1970 if (status != GOT_STATUS_DELETE &&
1971 status != GOT_STATUS_NONEXISTENT &&
1972 status != GOT_STATUS_MISSING) {
1973 err = remove_ondisk_file(worktree->root_path,
1974 ie->path);
1975 if (err && !(err->code == GOT_ERR_ERRNO &&
1976 errno == ENOENT))
1977 goto done;
1979 got_fileindex_entry_remove(fileindex, ie);
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1981 ie->path);
1983 goto done; /* nothing else to do */
1986 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1987 (S_ISLNK(te->mode) ||
1988 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1990 * This is a regular file or an installed bad symlink.
1991 * If the file index indicates that this file is already
1992 * up-to-date with respect to the repository we can skip
1993 * updating contents of this file.
1995 if (got_fileindex_entry_has_commit(ie) &&
1996 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1997 SHA1_DIGEST_LENGTH) == 0) {
1998 /* Same commit. */
1999 err = sync_timestamps(worktree->root_fd,
2000 path, status, ie, &sb);
2001 if (err)
2002 goto done;
2003 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2004 path);
2005 goto done;
2007 if (got_fileindex_entry_has_blob(ie) &&
2008 memcmp(ie->blob_sha1, te->id.sha1,
2009 SHA1_DIGEST_LENGTH) == 0) {
2010 /* Different commit but the same blob. */
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 /* Update the base commit ID of this file. */
2013 memcpy(ie->commit_sha1,
2014 worktree->base_commit_id->sha1,
2015 sizeof(ie->commit_sha1));
2017 err = sync_timestamps(worktree->root_fd,
2018 path, status, ie, &sb);
2019 if (err)
2020 goto done;
2021 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2022 path);
2023 goto done;
2027 fd1 = got_opentempfd();
2028 if (fd1 == -1) {
2029 err = got_error_from_errno("got_opentempfd");
2030 goto done;
2032 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2033 if (err)
2034 goto done;
2036 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2037 int update_timestamps;
2038 struct got_blob_object *blob2 = NULL;
2039 char *label_orig = NULL;
2040 if (got_fileindex_entry_has_blob(ie)) {
2041 fd2 = got_opentempfd();
2042 if (fd2 == -1) {
2043 err = got_error_from_errno("got_opentempfd");
2044 goto done;
2046 struct got_object_id id2;
2047 got_fileindex_entry_get_blob_id(&id2, ie);
2048 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2049 fd2);
2050 if (err)
2051 goto done;
2053 if (got_fileindex_entry_has_commit(ie)) {
2054 char id_str[SHA1_DIGEST_STRING_LENGTH];
2055 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2056 sizeof(id_str)) == NULL) {
2057 err = got_error_path(id_str,
2058 GOT_ERR_BAD_OBJ_ID_STR);
2059 goto done;
2061 if (asprintf(&label_orig, "%s: commit %s",
2062 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2063 err = got_error_from_errno("asprintf");
2064 goto done;
2067 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2068 char *link_target;
2069 err = got_object_blob_read_to_str(&link_target, blob);
2070 if (err)
2071 goto done;
2072 err = merge_symlink(worktree, blob2, ondisk_path, path,
2073 label_orig, link_target, worktree->base_commit_id,
2074 repo, progress_cb, progress_arg);
2075 free(link_target);
2076 } else {
2077 err = merge_blob(&update_timestamps, worktree, blob2,
2078 ondisk_path, path, sb.st_mode, label_orig, blob,
2079 worktree->base_commit_id, repo,
2080 progress_cb, progress_arg);
2082 free(label_orig);
2083 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2084 err = got_error_from_errno("close");
2085 goto done;
2087 if (blob2)
2088 got_object_blob_close(blob2);
2089 if (err)
2090 goto done;
2092 * Do not update timestamps of files with local changes.
2093 * Otherwise, a future status walk would treat them as
2094 * unmodified files again.
2096 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2097 blob->id.sha1, worktree->base_commit_id->sha1,
2098 update_timestamps);
2099 } else if (status == GOT_STATUS_MODE_CHANGE) {
2100 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2101 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2102 } else if (status == GOT_STATUS_DELETE) {
2103 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2104 if (err)
2105 goto done;
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 if (err)
2109 goto done;
2110 } else {
2111 int is_bad_symlink = 0;
2112 if (S_ISLNK(te->mode)) {
2113 err = install_symlink(&is_bad_symlink, worktree,
2114 ondisk_path, path, blob,
2115 status == GOT_STATUS_MISSING, 0,
2116 status == GOT_STATUS_UNVERSIONED, 0,
2117 repo, progress_cb, progress_arg);
2118 } else {
2119 err = install_blob(worktree, ondisk_path, path,
2120 te->mode, sb.st_mode, blob,
2121 status == GOT_STATUS_MISSING, 0, 0,
2122 status == GOT_STATUS_UNVERSIONED, repo,
2123 progress_cb, progress_arg);
2125 if (err)
2126 goto done;
2128 if (ie) {
2129 err = got_fileindex_entry_update(ie,
2130 worktree->root_fd, path, blob->id.sha1,
2131 worktree->base_commit_id->sha1, 1);
2132 } else {
2133 err = create_fileindex_entry(&ie, fileindex,
2134 worktree->base_commit_id, worktree->root_fd, path,
2135 &blob->id);
2137 if (err)
2138 goto done;
2140 if (is_bad_symlink) {
2141 got_fileindex_entry_filetype_set(ie,
2142 GOT_FILEIDX_MODE_BAD_SYMLINK);
2146 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2147 err = got_error_from_errno("close");
2148 goto done;
2150 got_object_blob_close(blob);
2151 done:
2152 free(ondisk_path);
2153 return err;
2156 static const struct got_error *
2157 remove_ondisk_file(const char *root_path, const char *path)
2159 const struct got_error *err = NULL;
2160 char *ondisk_path = NULL, *parent = NULL;
2162 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2163 return got_error_from_errno("asprintf");
2165 if (unlink(ondisk_path) == -1) {
2166 if (errno != ENOENT)
2167 err = got_error_from_errno2("unlink", ondisk_path);
2168 } else {
2169 size_t root_len = strlen(root_path);
2170 err = got_path_dirname(&parent, ondisk_path);
2171 if (err)
2172 goto done;
2173 while (got_path_cmp(parent, root_path,
2174 strlen(parent), root_len) != 0) {
2175 free(ondisk_path);
2176 ondisk_path = parent;
2177 parent = NULL;
2178 if (rmdir(ondisk_path) == -1) {
2179 if (errno != ENOTEMPTY)
2180 err = got_error_from_errno2("rmdir",
2181 ondisk_path);
2182 break;
2184 err = got_path_dirname(&parent, ondisk_path);
2185 if (err)
2186 break;
2189 done:
2190 free(ondisk_path);
2191 free(parent);
2192 return err;
2195 static const struct got_error *
2196 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2197 struct got_fileindex_entry *ie, struct got_repository *repo,
2198 got_worktree_checkout_cb progress_cb, void *progress_arg)
2200 const struct got_error *err = NULL;
2201 unsigned char status;
2202 struct stat sb;
2203 char *ondisk_path;
2205 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2206 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2208 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2209 == -1)
2210 return got_error_from_errno("asprintf");
2212 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2213 if (err)
2214 goto done;
2216 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2217 char ondisk_target[PATH_MAX];
2218 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2219 sizeof(ondisk_target));
2220 if (ondisk_len == -1) {
2221 err = got_error_from_errno2("readlink", ondisk_path);
2222 goto done;
2224 ondisk_target[ondisk_len] = '\0';
2225 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2226 NULL, NULL, /* XXX pass common ancestor info? */
2227 ondisk_target, ondisk_path);
2228 if (err)
2229 goto done;
2230 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2231 ie->path);
2232 goto done;
2235 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2236 status == GOT_STATUS_ADD) {
2237 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2238 if (err)
2239 goto done;
2241 * Preserve the working file and change the deleted blob's
2242 * entry into a schedule-add entry.
2244 err = got_fileindex_entry_update(ie, worktree->root_fd,
2245 ie->path, NULL, NULL, 0);
2246 } else {
2247 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2248 if (err)
2249 goto done;
2250 if (status == GOT_STATUS_NO_CHANGE) {
2251 err = remove_ondisk_file(worktree->root_path, ie->path);
2252 if (err)
2253 goto done;
2255 got_fileindex_entry_remove(fileindex, ie);
2257 done:
2258 free(ondisk_path);
2259 return err;
2262 struct diff_cb_arg {
2263 struct got_fileindex *fileindex;
2264 struct got_worktree *worktree;
2265 struct got_repository *repo;
2266 got_worktree_checkout_cb progress_cb;
2267 void *progress_arg;
2268 got_cancel_cb cancel_cb;
2269 void *cancel_arg;
2272 static const struct got_error *
2273 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2274 struct got_tree_entry *te, const char *parent_path)
2276 const struct got_error *err = NULL;
2277 struct diff_cb_arg *a = arg;
2279 if (a->cancel_cb) {
2280 err = a->cancel_cb(a->cancel_arg);
2281 if (err)
2282 return err;
2285 return update_blob(a->worktree, a->fileindex, ie, te,
2286 ie->path, a->repo, a->progress_cb, a->progress_arg);
2289 static const struct got_error *
2290 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2292 const struct got_error *err = NULL;
2293 struct diff_cb_arg *a = arg;
2295 if (a->cancel_cb) {
2296 err = a->cancel_cb(a->cancel_arg);
2297 if (err)
2298 return err;
2301 return delete_blob(a->worktree, a->fileindex, ie,
2302 a->repo, a->progress_cb, a->progress_arg);
2305 static const struct got_error *
2306 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2308 const struct got_error *err = NULL;
2309 struct diff_cb_arg *a = arg;
2310 char *path;
2312 if (a->cancel_cb) {
2313 err = a->cancel_cb(a->cancel_arg);
2314 if (err)
2315 return err;
2318 if (got_object_tree_entry_is_submodule(te))
2319 return NULL;
2321 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2322 return NULL;
2324 if (asprintf(&path, "%s%s%s", parent_path,
2325 parent_path[0] ? "/" : "", te->name)
2326 == -1)
2327 return got_error_from_errno("asprintf");
2329 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2330 a->repo, a->progress_cb, a->progress_arg);
2332 free(path);
2333 return err;
2336 const struct got_error *
2337 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2339 uint32_t uuid_status;
2341 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2342 if (uuid_status != uuid_s_ok) {
2343 *uuidstr = NULL;
2344 return got_error_uuid(uuid_status, "uuid_to_string");
2347 return NULL;
2350 static const struct got_error *
2351 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2353 const struct got_error *err = NULL;
2354 char *uuidstr = NULL;
2356 *refname = NULL;
2358 err = got_worktree_get_uuid(&uuidstr, worktree);
2359 if (err)
2360 return err;
2362 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2363 err = got_error_from_errno("asprintf");
2364 *refname = NULL;
2366 free(uuidstr);
2367 return err;
2370 const struct got_error *
2371 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2372 const char *prefix)
2374 return get_ref_name(refname, worktree, prefix);
2377 static const struct got_error *
2378 get_base_ref_name(char **refname, struct got_worktree *worktree)
2380 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2383 static const struct got_error *
2384 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2386 return get_ref_name(refname, worktree,
2387 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2390 static const struct got_error *
2391 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2396 static const struct got_error *
2397 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2403 static const struct got_error *
2404 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2410 static const struct got_error *
2411 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2424 static const struct got_error *
2425 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2427 return get_ref_name(refname, worktree,
2428 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2431 static const struct got_error *
2432 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 return get_ref_name(refname, worktree,
2435 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2438 const struct got_error *
2439 got_worktree_get_histedit_script_path(char **path,
2440 struct got_worktree *worktree)
2442 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2443 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2444 *path = NULL;
2445 return got_error_from_errno("asprintf");
2447 return NULL;
2450 static const struct got_error *
2451 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2453 return get_ref_name(refname, worktree,
2454 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2457 static const struct got_error *
2458 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2460 return get_ref_name(refname, worktree,
2461 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2465 * Prevent Git's garbage collector from deleting our base commit by
2466 * setting a reference to our base commit's ID.
2468 static const struct got_error *
2469 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2471 const struct got_error *err = NULL;
2472 struct got_reference *ref = NULL;
2473 char *refname;
2475 err = get_base_ref_name(&refname, worktree);
2476 if (err)
2477 return err;
2479 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2480 if (err)
2481 goto done;
2483 err = got_ref_write(ref, repo);
2484 done:
2485 free(refname);
2486 if (ref)
2487 got_ref_close(ref);
2488 return err;
2491 static const struct got_error *
2492 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2494 const struct got_error *err = NULL;
2496 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2497 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2498 err = got_error_from_errno("asprintf");
2499 *fileindex_path = NULL;
2501 return err;
2505 static const struct got_error *
2506 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2507 struct got_worktree *worktree)
2509 const struct got_error *err = NULL;
2510 FILE *index = NULL;
2512 *fileindex_path = NULL;
2513 *fileindex = got_fileindex_alloc();
2514 if (*fileindex == NULL)
2515 return got_error_from_errno("got_fileindex_alloc");
2517 err = get_fileindex_path(fileindex_path, worktree);
2518 if (err)
2519 goto done;
2521 index = fopen(*fileindex_path, "rbe");
2522 if (index == NULL) {
2523 if (errno != ENOENT)
2524 err = got_error_from_errno2("fopen", *fileindex_path);
2525 } else {
2526 err = got_fileindex_read(*fileindex, index);
2527 if (fclose(index) == EOF && err == NULL)
2528 err = got_error_from_errno("fclose");
2530 done:
2531 if (err) {
2532 free(*fileindex_path);
2533 *fileindex_path = NULL;
2534 got_fileindex_free(*fileindex);
2535 *fileindex = NULL;
2537 return err;
2540 struct bump_base_commit_id_arg {
2541 struct got_object_id *base_commit_id;
2542 const char *path;
2543 size_t path_len;
2544 const char *entry_name;
2545 got_worktree_checkout_cb progress_cb;
2546 void *progress_arg;
2549 static const struct got_error *
2550 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2552 const struct got_error *err;
2553 struct bump_base_commit_id_arg *a = arg;
2555 if (a->entry_name) {
2556 if (strcmp(ie->path, a->path) != 0)
2557 return NULL;
2558 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2559 return NULL;
2561 if (got_fileindex_entry_was_skipped(ie))
2562 return NULL;
2564 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2565 SHA1_DIGEST_LENGTH) == 0)
2566 return NULL;
2568 if (a->progress_cb) {
2569 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2570 ie->path);
2571 if (err)
2572 return err;
2574 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2575 return NULL;
2578 /* Bump base commit ID of all files within an updated part of the work tree. */
2579 static const struct got_error *
2580 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2581 struct got_fileindex *fileindex,
2582 got_worktree_checkout_cb progress_cb, void *progress_arg)
2584 struct bump_base_commit_id_arg bbc_arg;
2586 bbc_arg.base_commit_id = worktree->base_commit_id;
2587 bbc_arg.entry_name = NULL;
2588 bbc_arg.path = "";
2589 bbc_arg.path_len = 0;
2590 bbc_arg.progress_cb = progress_cb;
2591 bbc_arg.progress_arg = progress_arg;
2593 return got_fileindex_for_each_entry_safe(fileindex,
2594 bump_base_commit_id, &bbc_arg);
2597 static const struct got_error *
2598 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2600 const struct got_error *err = NULL;
2601 char *new_fileindex_path = NULL;
2602 FILE *new_index = NULL;
2603 struct timespec timeout;
2605 err = got_opentemp_named(&new_fileindex_path, &new_index,
2606 fileindex_path, "");
2607 if (err)
2608 goto done;
2610 err = got_fileindex_write(fileindex, new_index);
2611 if (err)
2612 goto done;
2614 if (rename(new_fileindex_path, fileindex_path) != 0) {
2615 err = got_error_from_errno3("rename", new_fileindex_path,
2616 fileindex_path);
2617 unlink(new_fileindex_path);
2621 * Sleep for a short amount of time to ensure that files modified after
2622 * this program exits have a different time stamp from the one which
2623 * was recorded in the file index.
2625 timeout.tv_sec = 0;
2626 timeout.tv_nsec = 1;
2627 nanosleep(&timeout, NULL);
2628 done:
2629 if (new_index)
2630 fclose(new_index);
2631 free(new_fileindex_path);
2632 return err;
2635 static const struct got_error *
2636 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2637 struct got_object_id **tree_id, const char *wt_relpath,
2638 struct got_commit_object *base_commit, struct got_worktree *worktree,
2639 struct got_repository *repo)
2641 const struct got_error *err = NULL;
2642 struct got_object_id *id = NULL;
2643 char *in_repo_path = NULL;
2644 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2646 *entry_type = GOT_OBJ_TYPE_ANY;
2647 *tree_relpath = NULL;
2648 *tree_id = NULL;
2650 if (wt_relpath[0] == '\0') {
2651 /* Check out all files within the work tree. */
2652 *entry_type = GOT_OBJ_TYPE_TREE;
2653 *tree_relpath = strdup("");
2654 if (*tree_relpath == NULL) {
2655 err = got_error_from_errno("strdup");
2656 goto done;
2658 err = got_object_id_by_path(tree_id, repo, base_commit,
2659 worktree->path_prefix);
2660 if (err)
2661 goto done;
2662 return NULL;
2665 /* Check out a subset of files in the work tree. */
2667 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2668 is_root_wt ? "" : "/", wt_relpath) == -1) {
2669 err = got_error_from_errno("asprintf");
2670 goto done;
2673 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2674 if (err)
2675 goto done;
2677 free(in_repo_path);
2678 in_repo_path = NULL;
2680 err = got_object_get_type(entry_type, repo, id);
2681 if (err)
2682 goto done;
2684 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2685 /* Check out a single file. */
2686 if (strchr(wt_relpath, '/') == NULL) {
2687 /* Check out a single file in work tree's root dir. */
2688 in_repo_path = strdup(worktree->path_prefix);
2689 if (in_repo_path == NULL) {
2690 err = got_error_from_errno("strdup");
2691 goto done;
2693 *tree_relpath = strdup("");
2694 if (*tree_relpath == NULL) {
2695 err = got_error_from_errno("strdup");
2696 goto done;
2698 } else {
2699 /* Check out a single file in a subdirectory. */
2700 err = got_path_dirname(tree_relpath, wt_relpath);
2701 if (err)
2702 return err;
2703 if (asprintf(&in_repo_path, "%s%s%s",
2704 worktree->path_prefix, is_root_wt ? "" : "/",
2705 *tree_relpath) == -1) {
2706 err = got_error_from_errno("asprintf");
2707 goto done;
2710 err = got_object_id_by_path(tree_id, repo,
2711 base_commit, in_repo_path);
2712 } else {
2713 /* Check out all files within a subdirectory. */
2714 *tree_id = got_object_id_dup(id);
2715 if (*tree_id == NULL) {
2716 err = got_error_from_errno("got_object_id_dup");
2717 goto done;
2719 *tree_relpath = strdup(wt_relpath);
2720 if (*tree_relpath == NULL) {
2721 err = got_error_from_errno("strdup");
2722 goto done;
2725 done:
2726 free(id);
2727 free(in_repo_path);
2728 if (err) {
2729 *entry_type = GOT_OBJ_TYPE_ANY;
2730 free(*tree_relpath);
2731 *tree_relpath = NULL;
2732 free(*tree_id);
2733 *tree_id = NULL;
2735 return err;
2738 static const struct got_error *
2739 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2740 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2741 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2742 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2744 const struct got_error *err = NULL;
2745 struct got_commit_object *commit = NULL;
2746 struct got_tree_object *tree = NULL;
2747 struct got_fileindex_diff_tree_cb diff_cb;
2748 struct diff_cb_arg arg;
2750 err = ref_base_commit(worktree, repo);
2751 if (err) {
2752 if (!(err->code == GOT_ERR_ERRNO &&
2753 (errno == EACCES || errno == EROFS)))
2754 goto done;
2755 err = (*progress_cb)(progress_arg,
2756 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2757 if (err)
2758 return err;
2761 err = got_object_open_as_commit(&commit, repo,
2762 worktree->base_commit_id);
2763 if (err)
2764 goto done;
2766 err = got_object_open_as_tree(&tree, repo, tree_id);
2767 if (err)
2768 goto done;
2770 if (entry_name &&
2771 got_object_tree_find_entry(tree, entry_name) == NULL) {
2772 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2773 goto done;
2776 diff_cb.diff_old_new = diff_old_new;
2777 diff_cb.diff_old = diff_old;
2778 diff_cb.diff_new = diff_new;
2779 arg.fileindex = fileindex;
2780 arg.worktree = worktree;
2781 arg.repo = repo;
2782 arg.progress_cb = progress_cb;
2783 arg.progress_arg = progress_arg;
2784 arg.cancel_cb = cancel_cb;
2785 arg.cancel_arg = cancel_arg;
2786 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2787 entry_name, repo, &diff_cb, &arg);
2788 done:
2789 if (tree)
2790 got_object_tree_close(tree);
2791 if (commit)
2792 got_object_commit_close(commit);
2793 return err;
2796 const struct got_error *
2797 got_worktree_checkout_files(struct got_worktree *worktree,
2798 struct got_pathlist_head *paths, struct got_repository *repo,
2799 got_worktree_checkout_cb progress_cb, void *progress_arg,
2800 got_cancel_cb cancel_cb, void *cancel_arg)
2802 const struct got_error *err = NULL, *sync_err, *unlockerr;
2803 struct got_commit_object *commit = NULL;
2804 struct got_tree_object *tree = NULL;
2805 struct got_fileindex *fileindex = NULL;
2806 char *fileindex_path = NULL;
2807 struct got_pathlist_entry *pe;
2808 struct tree_path_data {
2809 STAILQ_ENTRY(tree_path_data) entry;
2810 struct got_object_id *tree_id;
2811 int entry_type;
2812 char *relpath;
2813 char *entry_name;
2814 } *tpd = NULL;
2815 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2817 STAILQ_INIT(&tree_paths);
2819 err = lock_worktree(worktree, LOCK_EX);
2820 if (err)
2821 return err;
2823 err = got_object_open_as_commit(&commit, repo,
2824 worktree->base_commit_id);
2825 if (err)
2826 goto done;
2828 /* Map all specified paths to in-repository trees. */
2829 TAILQ_FOREACH(pe, paths, entry) {
2830 tpd = malloc(sizeof(*tpd));
2831 if (tpd == NULL) {
2832 err = got_error_from_errno("malloc");
2833 goto done;
2836 err = find_tree_entry_for_checkout(&tpd->entry_type,
2837 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2838 worktree, repo);
2839 if (err) {
2840 free(tpd);
2841 goto done;
2844 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2845 err = got_path_basename(&tpd->entry_name, pe->path);
2846 if (err) {
2847 free(tpd->relpath);
2848 free(tpd->tree_id);
2849 free(tpd);
2850 goto done;
2852 } else
2853 tpd->entry_name = NULL;
2855 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2859 * Read the file index.
2860 * Checking out files is supposed to be an idempotent operation.
2861 * If the on-disk file index is incomplete we will try to complete it.
2863 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2864 if (err)
2865 goto done;
2867 tpd = STAILQ_FIRST(&tree_paths);
2868 TAILQ_FOREACH(pe, paths, entry) {
2869 struct bump_base_commit_id_arg bbc_arg;
2871 err = checkout_files(worktree, fileindex, tpd->relpath,
2872 tpd->tree_id, tpd->entry_name, repo,
2873 progress_cb, progress_arg, cancel_cb, cancel_arg);
2874 if (err)
2875 break;
2877 bbc_arg.base_commit_id = worktree->base_commit_id;
2878 bbc_arg.entry_name = tpd->entry_name;
2879 bbc_arg.path = pe->path;
2880 bbc_arg.path_len = pe->path_len;
2881 bbc_arg.progress_cb = progress_cb;
2882 bbc_arg.progress_arg = progress_arg;
2883 err = got_fileindex_for_each_entry_safe(fileindex,
2884 bump_base_commit_id, &bbc_arg);
2885 if (err)
2886 break;
2888 tpd = STAILQ_NEXT(tpd, entry);
2890 sync_err = sync_fileindex(fileindex, fileindex_path);
2891 if (sync_err && err == NULL)
2892 err = sync_err;
2893 done:
2894 free(fileindex_path);
2895 if (tree)
2896 got_object_tree_close(tree);
2897 if (commit)
2898 got_object_commit_close(commit);
2899 if (fileindex)
2900 got_fileindex_free(fileindex);
2901 while (!STAILQ_EMPTY(&tree_paths)) {
2902 tpd = STAILQ_FIRST(&tree_paths);
2903 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2904 free(tpd->relpath);
2905 free(tpd->tree_id);
2906 free(tpd);
2908 unlockerr = lock_worktree(worktree, LOCK_SH);
2909 if (unlockerr && err == NULL)
2910 err = unlockerr;
2911 return err;
2914 static const struct got_error *
2915 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2916 struct got_fileindex_entry *ie, const char *ondisk_path,
2917 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2918 int restoring_missing_file, int reverting_versioned_file,
2919 int path_is_unversioned, int allow_bad_symlinks,
2920 struct got_repository *repo,
2921 got_worktree_checkout_cb progress_cb, void *progress_arg)
2923 const struct got_error *err = NULL;
2924 int is_bad_symlink = 0;
2926 if (S_ISLNK(mode2)) {
2927 err = install_symlink(&is_bad_symlink,
2928 worktree, ondisk_path, path2, blob2,
2929 restoring_missing_file,
2930 reverting_versioned_file,
2931 path_is_unversioned, allow_bad_symlinks,
2932 repo, progress_cb, progress_arg);
2933 } else {
2934 err = install_blob(worktree, ondisk_path, path2,
2935 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2936 restoring_missing_file, reverting_versioned_file, 0,
2937 path_is_unversioned, repo, progress_cb, progress_arg);
2939 if (err)
2940 return err;
2941 if (ie == NULL) {
2942 /* Adding an unversioned file. */
2943 err = got_fileindex_entry_alloc(&ie, path2);
2944 if (err)
2945 return err;
2946 err = got_fileindex_entry_update(ie,
2947 worktree->root_fd, path2, NULL, NULL, 1);
2948 if (err) {
2949 got_fileindex_entry_free(ie);
2950 return err;
2952 err = got_fileindex_entry_add(fileindex, ie);
2953 if (err) {
2954 got_fileindex_entry_free(ie);
2955 return err;
2957 } else {
2958 /* Re-adding a locally deleted file. */
2959 err = got_fileindex_entry_update(ie,
2960 worktree->root_fd, path2, ie->blob_sha1,
2961 worktree->base_commit_id->sha1, 0);
2962 if (err)
2963 return err;
2966 if (is_bad_symlink) {
2967 got_fileindex_entry_filetype_set(ie,
2968 GOT_FILEIDX_MODE_BAD_SYMLINK);
2971 return NULL;
2974 struct merge_file_cb_arg {
2975 struct got_worktree *worktree;
2976 struct got_fileindex *fileindex;
2977 got_worktree_checkout_cb progress_cb;
2978 void *progress_arg;
2979 got_cancel_cb cancel_cb;
2980 void *cancel_arg;
2981 const char *label_orig;
2982 struct got_object_id *commit_id2;
2983 int allow_bad_symlinks;
2986 static const struct got_error *
2987 merge_file_cb(void *arg, struct got_blob_object *blob1,
2988 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2989 struct got_object_id *id1, struct got_object_id *id2,
2990 const char *path1, const char *path2,
2991 mode_t mode1, mode_t mode2, struct got_repository *repo)
2993 static const struct got_error *err = NULL;
2994 struct merge_file_cb_arg *a = arg;
2995 struct got_fileindex_entry *ie;
2996 char *ondisk_path = NULL;
2997 struct stat sb;
2998 unsigned char status;
2999 int local_changes_subsumed;
3000 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3001 char *id_str = NULL, *label_deriv2 = NULL;
3003 if (blob1 && blob2) {
3004 ie = got_fileindex_entry_get(a->fileindex, path2,
3005 strlen(path2));
3006 if (ie == NULL)
3007 return (*a->progress_cb)(a->progress_arg,
3008 GOT_STATUS_MISSING, path2);
3010 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3011 path2) == -1)
3012 return got_error_from_errno("asprintf");
3014 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3015 repo);
3016 if (err)
3017 goto done;
3019 if (status == GOT_STATUS_DELETE) {
3020 err = (*a->progress_cb)(a->progress_arg,
3021 GOT_STATUS_MERGE, path2);
3022 goto done;
3024 if (status != GOT_STATUS_NO_CHANGE &&
3025 status != GOT_STATUS_MODIFY &&
3026 status != GOT_STATUS_CONFLICT &&
3027 status != GOT_STATUS_ADD) {
3028 err = (*a->progress_cb)(a->progress_arg, status, path2);
3029 goto done;
3032 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3033 char *link_target2;
3034 err = got_object_blob_read_to_str(&link_target2, blob2);
3035 if (err)
3036 goto done;
3037 err = merge_symlink(a->worktree, blob1, ondisk_path,
3038 path2, a->label_orig, link_target2, a->commit_id2,
3039 repo, a->progress_cb, a->progress_arg);
3040 free(link_target2);
3041 } else {
3042 int fd;
3044 f_orig = got_opentemp();
3045 if (f_orig == NULL) {
3046 err = got_error_from_errno("got_opentemp");
3047 goto done;
3049 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3050 f_orig, blob1);
3051 if (err)
3052 goto done;
3054 f_deriv2 = got_opentemp();
3055 if (f_deriv2 == NULL)
3056 goto done;
3057 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3058 f_deriv2, blob2);
3059 if (err)
3060 goto done;
3062 fd = open(ondisk_path,
3063 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3064 if (fd == -1) {
3065 err = got_error_from_errno2("open",
3066 ondisk_path);
3067 goto done;
3069 f_deriv = fdopen(fd, "r");
3070 if (f_deriv == NULL) {
3071 err = got_error_from_errno2("fdopen",
3072 ondisk_path);
3073 close(fd);
3074 goto done;
3076 err = got_object_id_str(&id_str, a->commit_id2);
3077 if (err)
3078 goto done;
3079 if (asprintf(&label_deriv2, "%s: commit %s",
3080 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3081 err = got_error_from_errno("asprintf");
3082 goto done;
3084 err = merge_file(&local_changes_subsumed, a->worktree,
3085 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3086 mode2, a->label_orig, NULL, label_deriv2,
3087 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3088 a->progress_cb, a->progress_arg);
3090 } else if (blob1) {
3091 ie = got_fileindex_entry_get(a->fileindex, path1,
3092 strlen(path1));
3093 if (ie == NULL)
3094 return (*a->progress_cb)(a->progress_arg,
3095 GOT_STATUS_MISSING, path1);
3097 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3098 path1) == -1)
3099 return got_error_from_errno("asprintf");
3101 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3102 repo);
3103 if (err)
3104 goto done;
3106 switch (status) {
3107 case GOT_STATUS_NO_CHANGE:
3108 err = (*a->progress_cb)(a->progress_arg,
3109 GOT_STATUS_DELETE, path1);
3110 if (err)
3111 goto done;
3112 err = remove_ondisk_file(a->worktree->root_path, path1);
3113 if (err)
3114 goto done;
3115 if (ie)
3116 got_fileindex_entry_mark_deleted_from_disk(ie);
3117 break;
3118 case GOT_STATUS_DELETE:
3119 case GOT_STATUS_MISSING:
3120 err = (*a->progress_cb)(a->progress_arg,
3121 GOT_STATUS_DELETE, 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_ADD: {
3128 struct got_object_id *id;
3129 FILE *blob1_f;
3130 off_t blob1_size;
3132 * Delete the added file only if its content already
3133 * exists in the repository.
3135 err = got_object_blob_file_create(&id, &blob1_f,
3136 &blob1_size, path1);
3137 if (err)
3138 goto done;
3139 if (got_object_id_cmp(id, id1) == 0) {
3140 err = (*a->progress_cb)(a->progress_arg,
3141 GOT_STATUS_DELETE, path1);
3142 if (err)
3143 goto done;
3144 err = remove_ondisk_file(a->worktree->root_path,
3145 path1);
3146 if (err)
3147 goto done;
3148 if (ie)
3149 got_fileindex_entry_remove(a->fileindex,
3150 ie);
3151 } else {
3152 err = (*a->progress_cb)(a->progress_arg,
3153 GOT_STATUS_CANNOT_DELETE, path1);
3155 if (fclose(blob1_f) == EOF && err == NULL)
3156 err = got_error_from_errno("fclose");
3157 free(id);
3158 if (err)
3159 goto done;
3160 break;
3162 case GOT_STATUS_MODIFY:
3163 case GOT_STATUS_CONFLICT:
3164 err = (*a->progress_cb)(a->progress_arg,
3165 GOT_STATUS_CANNOT_DELETE, path1);
3166 if (err)
3167 goto done;
3168 break;
3169 case GOT_STATUS_OBSTRUCTED:
3170 err = (*a->progress_cb)(a->progress_arg, status, path1);
3171 if (err)
3172 goto done;
3173 break;
3174 default:
3175 break;
3177 } else if (blob2) {
3178 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3179 path2) == -1)
3180 return got_error_from_errno("asprintf");
3181 ie = got_fileindex_entry_get(a->fileindex, path2,
3182 strlen(path2));
3183 if (ie) {
3184 err = get_file_status(&status, &sb, ie, ondisk_path,
3185 -1, NULL, repo);
3186 if (err)
3187 goto done;
3188 if (status != GOT_STATUS_NO_CHANGE &&
3189 status != GOT_STATUS_MODIFY &&
3190 status != GOT_STATUS_CONFLICT &&
3191 status != GOT_STATUS_ADD &&
3192 status != GOT_STATUS_DELETE) {
3193 err = (*a->progress_cb)(a->progress_arg,
3194 status, path2);
3195 goto done;
3197 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3198 char *link_target2;
3199 err = got_object_blob_read_to_str(&link_target2,
3200 blob2);
3201 if (err)
3202 goto done;
3203 err = merge_symlink(a->worktree, NULL,
3204 ondisk_path, path2, a->label_orig,
3205 link_target2, a->commit_id2, repo,
3206 a->progress_cb, a->progress_arg);
3207 free(link_target2);
3208 } else if (S_ISREG(sb.st_mode)) {
3209 err = merge_blob(&local_changes_subsumed,
3210 a->worktree, NULL, ondisk_path, path2,
3211 sb.st_mode, a->label_orig, blob2,
3212 a->commit_id2, repo, a->progress_cb,
3213 a->progress_arg);
3214 } else if (status != GOT_STATUS_DELETE) {
3215 err = got_error_path(ondisk_path,
3216 GOT_ERR_FILE_OBSTRUCTED);
3218 if (err)
3219 goto done;
3220 if (status == GOT_STATUS_DELETE) {
3221 /* Re-add file with content from new blob. */
3222 err = add_file(a->worktree, a->fileindex, ie,
3223 ondisk_path, path2, blob2, mode2,
3224 0, 0, 0, a->allow_bad_symlinks,
3225 repo, a->progress_cb, a->progress_arg);
3226 if (err)
3227 goto done;
3229 } else {
3230 err = add_file(a->worktree, a->fileindex, NULL,
3231 ondisk_path, path2, blob2, mode2,
3232 0, 0, 1, a->allow_bad_symlinks,
3233 repo, a->progress_cb, a->progress_arg);
3234 if (err)
3235 goto done;
3238 done:
3239 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3240 err = got_error_from_errno("fclose");
3241 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3242 err = got_error_from_errno("fclose");
3243 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3244 err = got_error_from_errno("fclose");
3245 free(id_str);
3246 free(label_deriv2);
3247 free(ondisk_path);
3248 return err;
3251 struct check_mixed_commits_args {
3252 struct got_worktree *worktree;
3253 got_cancel_cb cancel_cb;
3254 void *cancel_arg;
3257 static const struct got_error *
3258 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3260 const struct got_error *err = NULL;
3261 struct check_mixed_commits_args *a = arg;
3263 if (a->cancel_cb) {
3264 err = a->cancel_cb(a->cancel_arg);
3265 if (err)
3266 return err;
3269 /* Reject merges into a work tree with mixed base commits. */
3270 if (got_fileindex_entry_has_commit(ie) &&
3271 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3272 SHA1_DIGEST_LENGTH) != 0)
3273 return got_error(GOT_ERR_MIXED_COMMITS);
3275 return NULL;
3278 const struct got_error *
3279 got_worktree_get_state(char *state, struct got_repository *repo,
3280 struct got_worktree *worktree,
3281 got_cancel_cb cancel_cb, void *cancel_arg)
3283 const struct got_error *err;
3284 struct got_object_id *base_id, *head_id = NULL;
3285 struct got_reference *head_ref;
3286 struct got_fileindex *fileindex = NULL;
3287 char *fileindex_path = NULL;
3288 struct check_mixed_commits_args cma;
3290 if (worktree == NULL)
3291 return got_error(GOT_ERR_NOT_WORKTREE);
3293 err = got_ref_open(&head_ref, repo,
3294 got_worktree_get_head_ref_name(worktree), 0);
3295 if (err)
3296 return err;
3298 err = got_ref_resolve(&head_id, repo, head_ref);
3299 if (err)
3300 goto done;
3302 *state = GOT_WORKTREE_STATE_UNKNOWN;
3303 base_id = got_worktree_get_base_commit_id(worktree);
3305 cma.worktree = worktree;
3306 cma.cancel_cb = cancel_cb;
3307 cma.cancel_arg = cancel_arg;
3309 if (got_object_id_cmp(base_id, head_id) == 0) {
3310 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3311 if (err)
3312 goto done;
3314 err = got_fileindex_for_each_entry_safe(fileindex,
3315 check_mixed_commits, &cma);
3316 if (err == NULL)
3317 *state = GOT_WORKTREE_STATE_UPTODATE;
3318 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3319 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3320 err = NULL;
3322 } else
3323 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3325 done:
3326 free(head_id);
3327 free(fileindex_path);
3328 got_ref_close(head_ref);
3329 if (fileindex != NULL)
3330 got_fileindex_free(fileindex);
3331 return err;
3334 struct check_merge_conflicts_arg {
3335 struct got_worktree *worktree;
3336 struct got_fileindex *fileindex;
3337 struct got_repository *repo;
3340 static const struct got_error *
3341 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3342 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3343 struct got_object_id *id1, struct got_object_id *id2,
3344 const char *path1, const char *path2,
3345 mode_t mode1, mode_t mode2, struct got_repository *repo)
3347 const struct got_error *err = NULL;
3348 struct check_merge_conflicts_arg *a = arg;
3349 unsigned char status;
3350 struct stat sb;
3351 struct got_fileindex_entry *ie;
3352 const char *path = path2 ? path2 : path1;
3353 struct got_object_id *id = id2 ? id2 : id1;
3354 char *ondisk_path;
3356 if (id == NULL)
3357 return NULL;
3359 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3360 if (ie == NULL)
3361 return NULL;
3363 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3364 == -1)
3365 return got_error_from_errno("asprintf");
3367 /* Reject merges into a work tree with conflicted files. */
3368 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3369 free(ondisk_path);
3370 if (err)
3371 return err;
3372 if (status == GOT_STATUS_CONFLICT)
3373 return got_error(GOT_ERR_CONFLICTS);
3375 return NULL;
3378 static const struct got_error *
3379 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3380 const char *fileindex_path, struct got_object_id *commit_id1,
3381 struct got_object_id *commit_id2, struct got_repository *repo,
3382 got_worktree_checkout_cb progress_cb, void *progress_arg,
3383 got_cancel_cb cancel_cb, void *cancel_arg)
3385 const struct got_error *err = NULL, *sync_err;
3386 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3387 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3388 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3389 struct check_merge_conflicts_arg cmc_arg;
3390 struct merge_file_cb_arg arg;
3391 char *label_orig = NULL;
3392 FILE *f1 = NULL, *f2 = NULL;
3393 int fd1 = -1, fd2 = -1;
3395 if (commit_id1) {
3396 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3397 if (err)
3398 goto done;
3399 err = got_object_id_by_path(&tree_id1, repo, commit1,
3400 worktree->path_prefix);
3401 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3402 goto done;
3404 if (tree_id1) {
3405 char *id_str;
3407 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3408 if (err)
3409 goto done;
3411 err = got_object_id_str(&id_str, commit_id1);
3412 if (err)
3413 goto done;
3415 if (asprintf(&label_orig, "%s: commit %s",
3416 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3417 err = got_error_from_errno("asprintf");
3418 free(id_str);
3419 goto done;
3421 free(id_str);
3423 f1 = got_opentemp();
3424 if (f1 == NULL) {
3425 err = got_error_from_errno("got_opentemp");
3426 goto done;
3430 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3431 if (err)
3432 goto done;
3434 err = got_object_id_by_path(&tree_id2, repo, commit2,
3435 worktree->path_prefix);
3436 if (err)
3437 goto done;
3439 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3440 if (err)
3441 goto done;
3443 f2 = got_opentemp();
3444 if (f2 == NULL) {
3445 err = got_error_from_errno("got_opentemp");
3446 goto done;
3449 fd1 = got_opentempfd();
3450 if (fd1 == -1) {
3451 err = got_error_from_errno("got_opentempfd");
3452 goto done;
3455 fd2 = got_opentempfd();
3456 if (fd2 == -1) {
3457 err = got_error_from_errno("got_opentempfd");
3458 goto done;
3461 cmc_arg.worktree = worktree;
3462 cmc_arg.fileindex = fileindex;
3463 cmc_arg.repo = repo;
3464 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3465 check_merge_conflicts, &cmc_arg, 0);
3466 if (err)
3467 goto done;
3469 arg.worktree = worktree;
3470 arg.fileindex = fileindex;
3471 arg.progress_cb = progress_cb;
3472 arg.progress_arg = progress_arg;
3473 arg.cancel_cb = cancel_cb;
3474 arg.cancel_arg = cancel_arg;
3475 arg.label_orig = label_orig;
3476 arg.commit_id2 = commit_id2;
3477 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3478 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3479 merge_file_cb, &arg, 1);
3480 sync_err = sync_fileindex(fileindex, fileindex_path);
3481 if (sync_err && err == NULL)
3482 err = sync_err;
3483 done:
3484 if (commit1)
3485 got_object_commit_close(commit1);
3486 if (commit2)
3487 got_object_commit_close(commit2);
3488 if (tree1)
3489 got_object_tree_close(tree1);
3490 if (tree2)
3491 got_object_tree_close(tree2);
3492 if (f1 && fclose(f1) == EOF && err == NULL)
3493 err = got_error_from_errno("fclose");
3494 if (f2 && fclose(f2) == EOF && err == NULL)
3495 err = got_error_from_errno("fclose");
3496 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3497 err = got_error_from_errno("close");
3498 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3499 err = got_error_from_errno("close");
3500 free(label_orig);
3501 return err;
3504 const struct got_error *
3505 got_worktree_merge_files(struct got_worktree *worktree,
3506 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3507 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3508 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3510 const struct got_error *err, *unlockerr;
3511 char *fileindex_path = NULL;
3512 struct got_fileindex *fileindex = NULL;
3513 struct check_mixed_commits_args cma;
3515 err = lock_worktree(worktree, LOCK_EX);
3516 if (err)
3517 return err;
3519 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3520 if (err)
3521 goto done;
3523 cma.worktree = worktree;
3524 cma.cancel_cb = cancel_cb;
3525 cma.cancel_arg = cancel_arg;
3527 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3528 &cma);
3529 if (err)
3530 goto done;
3532 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3533 commit_id2, repo, progress_cb, progress_arg,
3534 cancel_cb, cancel_arg);
3535 done:
3536 if (fileindex)
3537 got_fileindex_free(fileindex);
3538 free(fileindex_path);
3539 unlockerr = lock_worktree(worktree, LOCK_SH);
3540 if (unlockerr && err == NULL)
3541 err = unlockerr;
3542 return err;
3545 struct diff_dir_cb_arg {
3546 struct got_fileindex *fileindex;
3547 struct got_worktree *worktree;
3548 const char *status_path;
3549 size_t status_path_len;
3550 struct got_repository *repo;
3551 got_worktree_status_cb status_cb;
3552 void *status_arg;
3553 got_cancel_cb cancel_cb;
3554 void *cancel_arg;
3555 /* A pathlist containing per-directory pathlists of ignore patterns. */
3556 struct got_pathlist_head *ignores;
3557 int report_unchanged;
3558 int no_ignores;
3561 static const struct got_error *
3562 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3563 int dirfd, const char *de_name,
3564 got_worktree_status_cb status_cb, void *status_arg,
3565 struct got_repository *repo, int report_unchanged)
3567 const struct got_error *err = NULL;
3568 unsigned char status = GOT_STATUS_NO_CHANGE;
3569 unsigned char staged_status;
3570 struct stat sb;
3571 struct got_object_id blob_id, commit_id, staged_blob_id;
3572 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3573 struct got_object_id *staged_blob_idp = NULL;
3575 staged_status = get_staged_status(ie);
3576 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3577 if (err)
3578 return err;
3580 if (status == GOT_STATUS_NO_CHANGE &&
3581 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3582 return NULL;
3584 if (got_fileindex_entry_has_blob(ie))
3585 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3586 if (got_fileindex_entry_has_commit(ie))
3587 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3588 if (staged_status == GOT_STATUS_ADD ||
3589 staged_status == GOT_STATUS_MODIFY) {
3590 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3591 &staged_blob_id, ie);
3594 return (*status_cb)(status_arg, status, staged_status,
3595 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3598 static const struct got_error *
3599 status_old_new(void *arg, struct got_fileindex_entry *ie,
3600 struct dirent *de, const char *parent_path, int dirfd)
3602 const struct got_error *err = NULL;
3603 struct diff_dir_cb_arg *a = arg;
3604 char *abspath;
3606 if (a->cancel_cb) {
3607 err = a->cancel_cb(a->cancel_arg);
3608 if (err)
3609 return err;
3612 if (got_path_cmp(parent_path, a->status_path,
3613 strlen(parent_path), a->status_path_len) != 0 &&
3614 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3615 return NULL;
3617 if (parent_path[0]) {
3618 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3619 parent_path, de->d_name) == -1)
3620 return got_error_from_errno("asprintf");
3621 } else {
3622 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3623 de->d_name) == -1)
3624 return got_error_from_errno("asprintf");
3627 err = report_file_status(ie, abspath, dirfd, de->d_name,
3628 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3629 free(abspath);
3630 return err;
3633 static const struct got_error *
3634 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3636 const struct got_error *err;
3637 struct diff_dir_cb_arg *a = arg;
3638 struct got_object_id blob_id, commit_id;
3639 unsigned char status;
3641 if (a->cancel_cb) {
3642 err = a->cancel_cb(a->cancel_arg);
3643 if (err)
3644 return err;
3647 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3648 return NULL;
3650 got_fileindex_entry_get_blob_id(&blob_id, ie);
3651 got_fileindex_entry_get_commit_id(&commit_id, ie);
3652 if (got_fileindex_entry_has_file_on_disk(ie))
3653 status = GOT_STATUS_MISSING;
3654 else
3655 status = GOT_STATUS_DELETE;
3656 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3657 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3660 static void
3661 free_ignores(struct got_pathlist_head *ignores)
3663 struct got_pathlist_entry *pe;
3665 TAILQ_FOREACH(pe, ignores, entry) {
3666 struct got_pathlist_head *ignorelist = pe->data;
3668 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3670 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3673 static const struct got_error *
3674 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3676 const struct got_error *err = NULL;
3677 struct got_pathlist_entry *pe = NULL;
3678 struct got_pathlist_head *ignorelist;
3679 char *line = NULL, *pattern, *dirpath = NULL;
3680 size_t linesize = 0;
3681 ssize_t linelen;
3683 ignorelist = calloc(1, sizeof(*ignorelist));
3684 if (ignorelist == NULL)
3685 return got_error_from_errno("calloc");
3686 TAILQ_INIT(ignorelist);
3688 while ((linelen = getline(&line, &linesize, f)) != -1) {
3689 if (linelen > 0 && line[linelen - 1] == '\n')
3690 line[linelen - 1] = '\0';
3692 /* Git's ignores may contain comments. */
3693 if (line[0] == '#')
3694 continue;
3696 /* Git's negated patterns are not (yet?) supported. */
3697 if (line[0] == '!')
3698 continue;
3700 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3701 line) == -1) {
3702 err = got_error_from_errno("asprintf");
3703 goto done;
3705 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3706 if (err)
3707 goto done;
3709 if (ferror(f)) {
3710 err = got_error_from_errno("getline");
3711 goto done;
3714 dirpath = strdup(path);
3715 if (dirpath == NULL) {
3716 err = got_error_from_errno("strdup");
3717 goto done;
3719 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3720 done:
3721 free(line);
3722 if (err || pe == NULL) {
3723 free(dirpath);
3724 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3726 return err;
3729 static int
3730 match_path(const char *pattern, size_t pattern_len, const char *path,
3731 int flags)
3733 char buf[PATH_MAX];
3736 * Trailing slashes signify directories.
3737 * Append a * to make such patterns conform to fnmatch rules.
3739 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3740 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3741 return FNM_NOMATCH; /* XXX */
3743 return fnmatch(buf, path, flags);
3746 return fnmatch(pattern, path, flags);
3749 static int
3750 match_ignores(struct got_pathlist_head *ignores, const char *path)
3752 struct got_pathlist_entry *pe;
3754 /* Handle patterns which match in all directories. */
3755 TAILQ_FOREACH(pe, ignores, entry) {
3756 struct got_pathlist_head *ignorelist = pe->data;
3757 struct got_pathlist_entry *pi;
3759 TAILQ_FOREACH(pi, ignorelist, entry) {
3760 const char *p;
3762 if (pi->path_len < 3 ||
3763 strncmp(pi->path, "**/", 3) != 0)
3764 continue;
3765 p = path;
3766 while (*p) {
3767 if (match_path(pi->path + 3,
3768 pi->path_len - 3, p,
3769 FNM_PATHNAME | FNM_LEADING_DIR)) {
3770 /* Retry in next directory. */
3771 while (*p && *p != '/')
3772 p++;
3773 while (*p == '/')
3774 p++;
3775 continue;
3777 return 1;
3783 * The ignores pathlist contains ignore lists from children before
3784 * parents, so we can find the most specific ignorelist by walking
3785 * ignores backwards.
3787 pe = TAILQ_LAST(ignores, got_pathlist_head);
3788 while (pe) {
3789 if (got_path_is_child(path, pe->path, pe->path_len)) {
3790 struct got_pathlist_head *ignorelist = pe->data;
3791 struct got_pathlist_entry *pi;
3792 TAILQ_FOREACH(pi, ignorelist, entry) {
3793 int flags = FNM_LEADING_DIR;
3794 if (strstr(pi->path, "/**/") == NULL)
3795 flags |= FNM_PATHNAME;
3796 if (match_path(pi->path, pi->path_len,
3797 path, flags))
3798 continue;
3799 return 1;
3802 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3805 return 0;
3808 static const struct got_error *
3809 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3810 const char *path, int dirfd, const char *ignores_filename)
3812 const struct got_error *err = NULL;
3813 char *ignorespath;
3814 int fd = -1;
3815 FILE *ignoresfile = NULL;
3817 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3818 path[0] ? "/" : "", ignores_filename) == -1)
3819 return got_error_from_errno("asprintf");
3821 if (dirfd != -1) {
3822 fd = openat(dirfd, ignores_filename,
3823 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3824 if (fd == -1) {
3825 if (errno != ENOENT && errno != EACCES)
3826 err = got_error_from_errno2("openat",
3827 ignorespath);
3828 } else {
3829 ignoresfile = fdopen(fd, "r");
3830 if (ignoresfile == NULL)
3831 err = got_error_from_errno2("fdopen",
3832 ignorespath);
3833 else {
3834 fd = -1;
3835 err = read_ignores(ignores, path, ignoresfile);
3838 } else {
3839 ignoresfile = fopen(ignorespath, "re");
3840 if (ignoresfile == NULL) {
3841 if (errno != ENOENT && errno != EACCES)
3842 err = got_error_from_errno2("fopen",
3843 ignorespath);
3844 } else
3845 err = read_ignores(ignores, path, ignoresfile);
3848 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3849 err = got_error_from_errno2("fclose", path);
3850 if (fd != -1 && close(fd) == -1 && err == NULL)
3851 err = got_error_from_errno2("close", path);
3852 free(ignorespath);
3853 return err;
3856 static const struct got_error *
3857 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3858 int dirfd)
3860 const struct got_error *err = NULL;
3861 struct diff_dir_cb_arg *a = arg;
3862 char *path = NULL;
3864 if (ignore != NULL)
3865 *ignore = 0;
3867 if (a->cancel_cb) {
3868 err = a->cancel_cb(a->cancel_arg);
3869 if (err)
3870 return err;
3873 if (parent_path[0]) {
3874 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3875 return got_error_from_errno("asprintf");
3876 } else {
3877 path = de->d_name;
3880 if (de->d_type == DT_DIR) {
3881 if (!a->no_ignores && ignore != NULL &&
3882 match_ignores(a->ignores, path))
3883 *ignore = 1;
3884 } else if (!match_ignores(a->ignores, path) &&
3885 got_path_is_child(path, a->status_path, a->status_path_len))
3886 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3887 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3888 if (parent_path[0])
3889 free(path);
3890 return err;
3893 static const struct got_error *
3894 status_traverse(void *arg, const char *path, int dirfd)
3896 const struct got_error *err = NULL;
3897 struct diff_dir_cb_arg *a = arg;
3899 if (a->no_ignores)
3900 return NULL;
3902 err = add_ignores(a->ignores, a->worktree->root_path,
3903 path, dirfd, ".cvsignore");
3904 if (err)
3905 return err;
3907 err = add_ignores(a->ignores, a->worktree->root_path, path,
3908 dirfd, ".gitignore");
3910 return err;
3913 static const struct got_error *
3914 report_single_file_status(const char *path, const char *ondisk_path,
3915 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3916 void *status_arg, struct got_repository *repo, int report_unchanged,
3917 struct got_pathlist_head *ignores, int no_ignores)
3919 struct got_fileindex_entry *ie;
3920 struct stat sb;
3922 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3923 if (ie)
3924 return report_file_status(ie, ondisk_path, -1, NULL,
3925 status_cb, status_arg, repo, report_unchanged);
3927 if (lstat(ondisk_path, &sb) == -1) {
3928 if (errno != ENOENT)
3929 return got_error_from_errno2("lstat", ondisk_path);
3930 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3931 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3934 if (!no_ignores && match_ignores(ignores, path))
3935 return NULL;
3937 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3938 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3939 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3941 return NULL;
3944 static const struct got_error *
3945 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3946 const char *root_path, const char *path)
3948 const struct got_error *err;
3949 char *parent_path, *next_parent_path = NULL;
3951 err = add_ignores(ignores, root_path, "", -1,
3952 ".cvsignore");
3953 if (err)
3954 return err;
3956 err = add_ignores(ignores, root_path, "", -1,
3957 ".gitignore");
3958 if (err)
3959 return err;
3961 err = got_path_dirname(&parent_path, path);
3962 if (err) {
3963 if (err->code == GOT_ERR_BAD_PATH)
3964 return NULL; /* cannot traverse parent */
3965 return err;
3967 for (;;) {
3968 err = add_ignores(ignores, root_path, parent_path, -1,
3969 ".cvsignore");
3970 if (err)
3971 break;
3972 err = add_ignores(ignores, root_path, parent_path, -1,
3973 ".gitignore");
3974 if (err)
3975 break;
3976 err = got_path_dirname(&next_parent_path, parent_path);
3977 if (err) {
3978 if (err->code == GOT_ERR_BAD_PATH)
3979 err = NULL; /* traversed everything */
3980 break;
3982 if (got_path_is_root_dir(parent_path))
3983 break;
3984 free(parent_path);
3985 parent_path = next_parent_path;
3986 next_parent_path = NULL;
3989 free(parent_path);
3990 free(next_parent_path);
3991 return err;
3994 struct find_missing_children_args {
3995 const char *parent_path;
3996 size_t parent_len;
3997 struct got_pathlist_head *children;
3998 got_cancel_cb cancel_cb;
3999 void *cancel_arg;
4002 static const struct got_error *
4003 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4005 const struct got_error *err = NULL;
4006 struct find_missing_children_args *a = arg;
4008 if (a->cancel_cb) {
4009 err = a->cancel_cb(a->cancel_arg);
4010 if (err)
4011 return err;
4014 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4015 err = got_pathlist_append(a->children, ie->path, NULL);
4017 return err;
4020 static const struct got_error *
4021 report_children(struct got_pathlist_head *children,
4022 struct got_worktree *worktree, struct got_fileindex *fileindex,
4023 struct got_repository *repo, int is_root_dir, int report_unchanged,
4024 struct got_pathlist_head *ignores, int no_ignores,
4025 got_worktree_status_cb status_cb, void *status_arg,
4026 got_cancel_cb cancel_cb, void *cancel_arg)
4028 const struct got_error *err = NULL;
4029 struct got_pathlist_entry *pe;
4030 char *ondisk_path = NULL;
4032 TAILQ_FOREACH(pe, children, entry) {
4033 if (cancel_cb) {
4034 err = cancel_cb(cancel_arg);
4035 if (err)
4036 break;
4039 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4040 !is_root_dir ? "/" : "", pe->path) == -1) {
4041 err = got_error_from_errno("asprintf");
4042 ondisk_path = NULL;
4043 break;
4046 err = report_single_file_status(pe->path, ondisk_path,
4047 fileindex, status_cb, status_arg, repo, report_unchanged,
4048 ignores, no_ignores);
4049 if (err)
4050 break;
4052 free(ondisk_path);
4053 ondisk_path = NULL;
4056 free(ondisk_path);
4057 return err;
4060 static const struct got_error *
4061 worktree_status(struct got_worktree *worktree, const char *path,
4062 struct got_fileindex *fileindex, struct got_repository *repo,
4063 got_worktree_status_cb status_cb, void *status_arg,
4064 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4065 int report_unchanged)
4067 const struct got_error *err = NULL;
4068 int fd = -1;
4069 struct got_fileindex_diff_dir_cb fdiff_cb;
4070 struct diff_dir_cb_arg arg;
4071 char *ondisk_path = NULL;
4072 struct got_pathlist_head ignores, missing_children;
4073 struct got_fileindex_entry *ie;
4075 TAILQ_INIT(&ignores);
4076 TAILQ_INIT(&missing_children);
4078 if (asprintf(&ondisk_path, "%s%s%s",
4079 worktree->root_path, path[0] ? "/" : "", path) == -1)
4080 return got_error_from_errno("asprintf");
4082 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4083 if (ie) {
4084 err = report_single_file_status(path, ondisk_path,
4085 fileindex, status_cb, status_arg, repo,
4086 report_unchanged, &ignores, no_ignores);
4087 goto done;
4088 } else {
4089 struct find_missing_children_args fmca;
4090 fmca.parent_path = path;
4091 fmca.parent_len = strlen(path);
4092 fmca.children = &missing_children;
4093 fmca.cancel_cb = cancel_cb;
4094 fmca.cancel_arg = cancel_arg;
4095 err = got_fileindex_for_each_entry_safe(fileindex,
4096 find_missing_children, &fmca);
4097 if (err)
4098 goto done;
4101 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4102 if (fd == -1) {
4103 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4104 !got_err_open_nofollow_on_symlink())
4105 err = got_error_from_errno2("open", ondisk_path);
4106 else {
4107 if (!no_ignores) {
4108 err = add_ignores_from_parent_paths(&ignores,
4109 worktree->root_path, ondisk_path);
4110 if (err)
4111 goto done;
4113 if (TAILQ_EMPTY(&missing_children)) {
4114 err = report_single_file_status(path,
4115 ondisk_path, fileindex,
4116 status_cb, status_arg, repo,
4117 report_unchanged, &ignores, no_ignores);
4118 if (err)
4119 goto done;
4120 } else {
4121 err = report_children(&missing_children,
4122 worktree, fileindex, repo,
4123 (path[0] == '\0'), report_unchanged,
4124 &ignores, no_ignores,
4125 status_cb, status_arg,
4126 cancel_cb, cancel_arg);
4127 if (err)
4128 goto done;
4131 } else {
4132 fdiff_cb.diff_old_new = status_old_new;
4133 fdiff_cb.diff_old = status_old;
4134 fdiff_cb.diff_new = status_new;
4135 fdiff_cb.diff_traverse = status_traverse;
4136 arg.fileindex = fileindex;
4137 arg.worktree = worktree;
4138 arg.status_path = path;
4139 arg.status_path_len = strlen(path);
4140 arg.repo = repo;
4141 arg.status_cb = status_cb;
4142 arg.status_arg = status_arg;
4143 arg.cancel_cb = cancel_cb;
4144 arg.cancel_arg = cancel_arg;
4145 arg.report_unchanged = report_unchanged;
4146 arg.no_ignores = no_ignores;
4147 if (!no_ignores) {
4148 err = add_ignores_from_parent_paths(&ignores,
4149 worktree->root_path, path);
4150 if (err)
4151 goto done;
4153 arg.ignores = &ignores;
4154 err = got_fileindex_diff_dir(fileindex, fd,
4155 worktree->root_path, path, repo, &fdiff_cb, &arg);
4157 done:
4158 free_ignores(&ignores);
4159 if (fd != -1 && close(fd) == -1 && err == NULL)
4160 err = got_error_from_errno("close");
4161 free(ondisk_path);
4162 return err;
4165 const struct got_error *
4166 got_worktree_status(struct got_worktree *worktree,
4167 struct got_pathlist_head *paths, struct got_repository *repo,
4168 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4169 got_cancel_cb cancel_cb, void *cancel_arg)
4171 const struct got_error *err = NULL;
4172 char *fileindex_path = NULL;
4173 struct got_fileindex *fileindex = NULL;
4174 struct got_pathlist_entry *pe;
4176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4177 if (err)
4178 return err;
4180 TAILQ_FOREACH(pe, paths, entry) {
4181 err = worktree_status(worktree, pe->path, fileindex, repo,
4182 status_cb, status_arg, cancel_cb, cancel_arg,
4183 no_ignores, 0);
4184 if (err)
4185 break;
4187 free(fileindex_path);
4188 got_fileindex_free(fileindex);
4189 return err;
4192 const struct got_error *
4193 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4194 const char *arg)
4196 const struct got_error *err = NULL;
4197 char *resolved = NULL, *cwd = NULL, *path = NULL;
4198 size_t len;
4199 struct stat sb;
4200 char *abspath = NULL;
4201 char canonpath[PATH_MAX];
4203 *wt_path = NULL;
4205 cwd = getcwd(NULL, 0);
4206 if (cwd == NULL)
4207 return got_error_from_errno("getcwd");
4209 if (lstat(arg, &sb) == -1) {
4210 if (errno != ENOENT) {
4211 err = got_error_from_errno2("lstat", arg);
4212 goto done;
4214 sb.st_mode = 0;
4216 if (S_ISLNK(sb.st_mode)) {
4218 * We cannot use realpath(3) with symlinks since we want to
4219 * operate on the symlink itself.
4220 * But we can make the path absolute, assuming it is relative
4221 * to the current working directory, and then canonicalize it.
4223 if (!got_path_is_absolute(arg)) {
4224 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4225 err = got_error_from_errno("asprintf");
4226 goto done;
4230 err = got_canonpath(abspath ? abspath : arg, canonpath,
4231 sizeof(canonpath));
4232 if (err)
4233 goto done;
4234 resolved = strdup(canonpath);
4235 if (resolved == NULL) {
4236 err = got_error_from_errno("strdup");
4237 goto done;
4239 } else {
4240 resolved = realpath(arg, NULL);
4241 if (resolved == NULL) {
4242 if (errno != ENOENT) {
4243 err = got_error_from_errno2("realpath", arg);
4244 goto done;
4246 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4247 err = got_error_from_errno("asprintf");
4248 goto done;
4250 err = got_canonpath(abspath, canonpath,
4251 sizeof(canonpath));
4252 if (err)
4253 goto done;
4254 resolved = strdup(canonpath);
4255 if (resolved == NULL) {
4256 err = got_error_from_errno("strdup");
4257 goto done;
4262 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4263 strlen(got_worktree_get_root_path(worktree)))) {
4264 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4265 goto done;
4268 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4269 err = got_path_skip_common_ancestor(&path,
4270 got_worktree_get_root_path(worktree), resolved);
4271 if (err)
4272 goto done;
4273 } else {
4274 path = strdup("");
4275 if (path == NULL) {
4276 err = got_error_from_errno("strdup");
4277 goto done;
4281 /* XXX status walk can't deal with trailing slash! */
4282 len = strlen(path);
4283 while (len > 0 && path[len - 1] == '/') {
4284 path[len - 1] = '\0';
4285 len--;
4287 done:
4288 free(abspath);
4289 free(resolved);
4290 free(cwd);
4291 if (err == NULL)
4292 *wt_path = path;
4293 else
4294 free(path);
4295 return err;
4298 struct schedule_addition_args {
4299 struct got_worktree *worktree;
4300 struct got_fileindex *fileindex;
4301 got_worktree_checkout_cb progress_cb;
4302 void *progress_arg;
4303 struct got_repository *repo;
4306 static int
4307 add_noop_status(unsigned char status)
4309 return (status == GOT_STATUS_ADD ||
4310 status == GOT_STATUS_MODIFY ||
4311 status == GOT_STATUS_CONFLICT ||
4312 status == GOT_STATUS_MODE_CHANGE ||
4313 status == GOT_STATUS_NO_CHANGE);
4316 static const struct got_error *
4317 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4318 const char *relpath, struct got_object_id *blob_id,
4319 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4320 int dirfd, const char *de_name)
4322 struct schedule_addition_args *a = arg;
4323 const struct got_error *err = NULL;
4324 struct got_fileindex_entry *ie;
4325 struct stat sb;
4326 char *ondisk_path;
4328 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4329 relpath) == -1)
4330 return got_error_from_errno("asprintf");
4332 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4333 if (ie) {
4334 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4335 de_name, a->repo);
4336 if (err)
4337 goto done;
4338 /* Re-adding an existing entry is a no-op. */
4339 if (staged_status == GOT_STATUS_NO_CHANGE &&
4340 add_noop_status(status))
4341 goto done;
4342 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4343 if (err)
4344 goto done;
4347 if (status != GOT_STATUS_UNVERSIONED) {
4348 if (status == GOT_STATUS_NONEXISTENT)
4349 err = got_error_set_errno(ENOENT, ondisk_path);
4350 else
4351 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4352 goto done;
4355 err = got_fileindex_entry_alloc(&ie, relpath);
4356 if (err)
4357 goto done;
4358 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4359 relpath, NULL, NULL, 1);
4360 if (err) {
4361 got_fileindex_entry_free(ie);
4362 goto done;
4364 err = got_fileindex_entry_add(a->fileindex, ie);
4365 if (err) {
4366 got_fileindex_entry_free(ie);
4367 goto done;
4369 done:
4370 free(ondisk_path);
4371 if (err)
4372 return err;
4373 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4374 return NULL;
4375 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4378 const struct got_error *
4379 got_worktree_schedule_add(struct got_worktree *worktree,
4380 struct got_pathlist_head *paths,
4381 got_worktree_checkout_cb progress_cb, void *progress_arg,
4382 struct got_repository *repo, int no_ignores)
4384 struct got_fileindex *fileindex = NULL;
4385 char *fileindex_path = NULL;
4386 const struct got_error *err = NULL, *sync_err, *unlockerr;
4387 struct got_pathlist_entry *pe;
4388 struct schedule_addition_args saa;
4390 err = lock_worktree(worktree, LOCK_EX);
4391 if (err)
4392 return err;
4394 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4395 if (err)
4396 goto done;
4398 saa.worktree = worktree;
4399 saa.fileindex = fileindex;
4400 saa.progress_cb = progress_cb;
4401 saa.progress_arg = progress_arg;
4402 saa.repo = repo;
4404 TAILQ_FOREACH(pe, paths, entry) {
4405 err = worktree_status(worktree, pe->path, fileindex, repo,
4406 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4407 if (err)
4408 break;
4410 sync_err = sync_fileindex(fileindex, fileindex_path);
4411 if (sync_err && err == NULL)
4412 err = sync_err;
4413 done:
4414 free(fileindex_path);
4415 if (fileindex)
4416 got_fileindex_free(fileindex);
4417 unlockerr = lock_worktree(worktree, LOCK_SH);
4418 if (unlockerr && err == NULL)
4419 err = unlockerr;
4420 return err;
4423 struct schedule_deletion_args {
4424 struct got_worktree *worktree;
4425 struct got_fileindex *fileindex;
4426 got_worktree_delete_cb progress_cb;
4427 void *progress_arg;
4428 struct got_repository *repo;
4429 int delete_local_mods;
4430 int keep_on_disk;
4431 int ignore_missing_paths;
4432 const char *status_path;
4433 size_t status_path_len;
4434 const char *status_codes;
4437 static const struct got_error *
4438 schedule_for_deletion(void *arg, unsigned char status,
4439 unsigned char staged_status, const char *relpath,
4440 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4441 struct got_object_id *commit_id, int dirfd, const char *de_name)
4443 struct schedule_deletion_args *a = arg;
4444 const struct got_error *err = NULL;
4445 struct got_fileindex_entry *ie = NULL;
4446 struct stat sb;
4447 char *ondisk_path;
4449 if (status == GOT_STATUS_NONEXISTENT) {
4450 if (a->ignore_missing_paths)
4451 return NULL;
4452 return got_error_set_errno(ENOENT, relpath);
4455 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4456 if (ie == NULL)
4457 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4459 staged_status = get_staged_status(ie);
4460 if (staged_status != GOT_STATUS_NO_CHANGE) {
4461 if (staged_status == GOT_STATUS_DELETE)
4462 return NULL;
4463 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4466 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4467 relpath) == -1)
4468 return got_error_from_errno("asprintf");
4470 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4471 a->repo);
4472 if (err)
4473 goto done;
4475 if (a->status_codes) {
4476 size_t ncodes = strlen(a->status_codes);
4477 int i;
4478 for (i = 0; i < ncodes ; i++) {
4479 if (status == a->status_codes[i])
4480 break;
4482 if (i == ncodes) {
4483 /* Do not delete files in non-matching status. */
4484 free(ondisk_path);
4485 return NULL;
4487 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4488 a->status_codes[i] != GOT_STATUS_MISSING) {
4489 static char msg[64];
4490 snprintf(msg, sizeof(msg),
4491 "invalid status code '%c'", a->status_codes[i]);
4492 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4493 goto done;
4497 if (status != GOT_STATUS_NO_CHANGE) {
4498 if (status == GOT_STATUS_DELETE)
4499 goto done;
4500 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4501 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4502 goto done;
4504 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4505 err = got_error_set_errno(ENOENT, relpath);
4506 goto done;
4508 if (status != GOT_STATUS_MODIFY &&
4509 status != GOT_STATUS_MISSING) {
4510 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4511 goto done;
4515 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4516 size_t root_len;
4518 if (dirfd != -1) {
4519 if (unlinkat(dirfd, de_name, 0) == -1) {
4520 err = got_error_from_errno2("unlinkat",
4521 ondisk_path);
4522 goto done;
4524 } else if (unlink(ondisk_path) == -1) {
4525 err = got_error_from_errno2("unlink", ondisk_path);
4526 goto done;
4529 root_len = strlen(a->worktree->root_path);
4530 do {
4531 char *parent;
4533 err = got_path_dirname(&parent, ondisk_path);
4534 if (err)
4535 goto done;
4536 free(ondisk_path);
4537 ondisk_path = parent;
4538 if (got_path_cmp(ondisk_path, a->status_path,
4539 strlen(ondisk_path), a->status_path_len) != 0 &&
4540 !got_path_is_child(ondisk_path, a->status_path,
4541 a->status_path_len))
4542 break;
4543 if (rmdir(ondisk_path) == -1) {
4544 if (errno != ENOTEMPTY)
4545 err = got_error_from_errno2("rmdir",
4546 ondisk_path);
4547 break;
4549 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4550 strlen(ondisk_path), root_len) != 0);
4553 got_fileindex_entry_mark_deleted_from_disk(ie);
4554 done:
4555 free(ondisk_path);
4556 if (err)
4557 return err;
4558 if (status == GOT_STATUS_DELETE)
4559 return NULL;
4560 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4561 staged_status, relpath);
4564 const struct got_error *
4565 got_worktree_schedule_delete(struct got_worktree *worktree,
4566 struct got_pathlist_head *paths, int delete_local_mods,
4567 const char *status_codes,
4568 got_worktree_delete_cb progress_cb, void *progress_arg,
4569 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4571 struct got_fileindex *fileindex = NULL;
4572 char *fileindex_path = NULL;
4573 const struct got_error *err = NULL, *sync_err, *unlockerr;
4574 struct got_pathlist_entry *pe;
4575 struct schedule_deletion_args sda;
4577 err = lock_worktree(worktree, LOCK_EX);
4578 if (err)
4579 return err;
4581 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4582 if (err)
4583 goto done;
4585 sda.worktree = worktree;
4586 sda.fileindex = fileindex;
4587 sda.progress_cb = progress_cb;
4588 sda.progress_arg = progress_arg;
4589 sda.repo = repo;
4590 sda.delete_local_mods = delete_local_mods;
4591 sda.keep_on_disk = keep_on_disk;
4592 sda.ignore_missing_paths = ignore_missing_paths;
4593 sda.status_codes = status_codes;
4595 TAILQ_FOREACH(pe, paths, entry) {
4596 char *ondisk_status_path;
4598 if (asprintf(&ondisk_status_path, "%s%s%s",
4599 got_worktree_get_root_path(worktree),
4600 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4601 err = got_error_from_errno("asprintf");
4602 goto done;
4604 sda.status_path = ondisk_status_path;
4605 sda.status_path_len = strlen(ondisk_status_path);
4606 err = worktree_status(worktree, pe->path, fileindex, repo,
4607 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4608 free(ondisk_status_path);
4609 if (err)
4610 break;
4612 sync_err = sync_fileindex(fileindex, fileindex_path);
4613 if (sync_err && err == NULL)
4614 err = sync_err;
4615 done:
4616 free(fileindex_path);
4617 if (fileindex)
4618 got_fileindex_free(fileindex);
4619 unlockerr = lock_worktree(worktree, LOCK_SH);
4620 if (unlockerr && err == NULL)
4621 err = unlockerr;
4622 return err;
4625 static const struct got_error *
4626 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4628 const struct got_error *err = NULL;
4629 char *line = NULL;
4630 size_t linesize = 0, n;
4631 ssize_t linelen;
4633 linelen = getline(&line, &linesize, infile);
4634 if (linelen == -1) {
4635 if (ferror(infile)) {
4636 err = got_error_from_errno("getline");
4637 goto done;
4639 return NULL;
4641 if (outfile) {
4642 n = fwrite(line, 1, linelen, outfile);
4643 if (n != linelen) {
4644 err = got_ferror(outfile, GOT_ERR_IO);
4645 goto done;
4648 if (rejectfile) {
4649 n = fwrite(line, 1, linelen, rejectfile);
4650 if (n != linelen)
4651 err = got_ferror(rejectfile, GOT_ERR_IO);
4653 done:
4654 free(line);
4655 return err;
4658 static const struct got_error *
4659 skip_one_line(FILE *f)
4661 char *line = NULL;
4662 size_t linesize = 0;
4663 ssize_t linelen;
4665 linelen = getline(&line, &linesize, f);
4666 if (linelen == -1) {
4667 if (ferror(f))
4668 return got_error_from_errno("getline");
4669 return NULL;
4671 free(line);
4672 return NULL;
4675 static const struct got_error *
4676 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4677 int start_old, int end_old, int start_new, int end_new,
4678 FILE *outfile, FILE *rejectfile)
4680 const struct got_error *err;
4682 /* Copy old file's lines leading up to patch. */
4683 while (!feof(f1) && *line_cur1 < start_old) {
4684 err = copy_one_line(f1, outfile, NULL);
4685 if (err)
4686 return err;
4687 (*line_cur1)++;
4689 /* Skip new file's lines leading up to patch. */
4690 while (!feof(f2) && *line_cur2 < start_new) {
4691 if (rejectfile)
4692 err = copy_one_line(f2, NULL, rejectfile);
4693 else
4694 err = skip_one_line(f2);
4695 if (err)
4696 return err;
4697 (*line_cur2)++;
4699 /* Copy patched lines. */
4700 while (!feof(f2) && *line_cur2 <= end_new) {
4701 err = copy_one_line(f2, outfile, NULL);
4702 if (err)
4703 return err;
4704 (*line_cur2)++;
4706 /* Skip over old file's replaced lines. */
4707 while (!feof(f1) && *line_cur1 <= end_old) {
4708 if (rejectfile)
4709 err = copy_one_line(f1, NULL, rejectfile);
4710 else
4711 err = skip_one_line(f1);
4712 if (err)
4713 return err;
4714 (*line_cur1)++;
4717 return NULL;
4720 static const struct got_error *
4721 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4722 FILE *outfile, FILE *rejectfile)
4724 const struct got_error *err;
4726 if (outfile) {
4727 /* Copy old file's lines until EOF. */
4728 while (!feof(f1)) {
4729 err = copy_one_line(f1, outfile, NULL);
4730 if (err)
4731 return err;
4732 (*line_cur1)++;
4735 if (rejectfile) {
4736 /* Copy new file's lines until EOF. */
4737 while (!feof(f2)) {
4738 err = copy_one_line(f2, NULL, rejectfile);
4739 if (err)
4740 return err;
4741 (*line_cur2)++;
4745 return NULL;
4748 static const struct got_error *
4749 apply_or_reject_change(int *choice, int *nchunks_used,
4750 struct diff_result *diff_result, int n,
4751 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4752 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4753 got_worktree_patch_cb patch_cb, void *patch_arg)
4755 const struct got_error *err = NULL;
4756 struct diff_chunk_context cc = {};
4757 int start_old, end_old, start_new, end_new;
4758 FILE *hunkfile;
4759 struct diff_output_unidiff_state *diff_state;
4760 struct diff_input_info diff_info;
4761 int rc;
4763 *choice = GOT_PATCH_CHOICE_NONE;
4765 /* Get changed line numbers without context lines for copy_change(). */
4766 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4767 start_old = cc.left.start;
4768 end_old = cc.left.end;
4769 start_new = cc.right.start;
4770 end_new = cc.right.end;
4772 /* Get the same change with context lines for display. */
4773 memset(&cc, 0, sizeof(cc));
4774 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4776 memset(&diff_info, 0, sizeof(diff_info));
4777 diff_info.left_path = relpath;
4778 diff_info.right_path = relpath;
4780 diff_state = diff_output_unidiff_state_alloc();
4781 if (diff_state == NULL)
4782 return got_error_set_errno(ENOMEM,
4783 "diff_output_unidiff_state_alloc");
4785 hunkfile = got_opentemp();
4786 if (hunkfile == NULL) {
4787 err = got_error_from_errno("got_opentemp");
4788 goto done;
4791 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4792 diff_result, &cc);
4793 if (rc != DIFF_RC_OK) {
4794 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4795 goto done;
4798 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4799 err = got_ferror(hunkfile, GOT_ERR_IO);
4800 goto done;
4803 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4804 hunkfile, changeno, nchanges);
4805 if (err)
4806 goto done;
4808 switch (*choice) {
4809 case GOT_PATCH_CHOICE_YES:
4810 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4811 end_old, start_new, end_new, outfile, rejectfile);
4812 break;
4813 case GOT_PATCH_CHOICE_NO:
4814 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4815 end_old, start_new, end_new, rejectfile, outfile);
4816 break;
4817 case GOT_PATCH_CHOICE_QUIT:
4818 break;
4819 default:
4820 err = got_error(GOT_ERR_PATCH_CHOICE);
4821 break;
4823 done:
4824 diff_output_unidiff_state_free(diff_state);
4825 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4826 err = got_error_from_errno("fclose");
4827 return err;
4830 struct revert_file_args {
4831 struct got_worktree *worktree;
4832 struct got_fileindex *fileindex;
4833 got_worktree_checkout_cb progress_cb;
4834 void *progress_arg;
4835 got_worktree_patch_cb patch_cb;
4836 void *patch_arg;
4837 struct got_repository *repo;
4838 int unlink_added_files;
4839 struct got_pathlist_head *added_files_to_unlink;
4842 static const struct got_error *
4843 create_patched_content(char **path_outfile, int reverse_patch,
4844 struct got_object_id *blob_id, const char *path2,
4845 int dirfd2, const char *de_name2,
4846 const char *relpath, struct got_repository *repo,
4847 got_worktree_patch_cb patch_cb, void *patch_arg)
4849 const struct got_error *err, *free_err;
4850 struct got_blob_object *blob = NULL;
4851 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4852 int fd = -1, fd2 = -1;
4853 char link_target[PATH_MAX];
4854 ssize_t link_len = 0;
4855 char *path1 = NULL, *id_str = NULL;
4856 struct stat sb2;
4857 struct got_diffreg_result *diffreg_result = NULL;
4858 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4859 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4861 *path_outfile = NULL;
4863 err = got_object_id_str(&id_str, blob_id);
4864 if (err)
4865 return err;
4867 if (dirfd2 != -1) {
4868 fd2 = openat(dirfd2, de_name2,
4869 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4870 if (fd2 == -1) {
4871 if (!got_err_open_nofollow_on_symlink()) {
4872 err = got_error_from_errno2("openat", path2);
4873 goto done;
4875 link_len = readlinkat(dirfd2, de_name2,
4876 link_target, sizeof(link_target));
4877 if (link_len == -1) {
4878 return got_error_from_errno2("readlinkat",
4879 path2);
4881 sb2.st_mode = S_IFLNK;
4882 sb2.st_size = link_len;
4884 } else {
4885 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4886 if (fd2 == -1) {
4887 if (!got_err_open_nofollow_on_symlink()) {
4888 err = got_error_from_errno2("open", path2);
4889 goto done;
4891 link_len = readlink(path2, link_target,
4892 sizeof(link_target));
4893 if (link_len == -1)
4894 return got_error_from_errno2("readlink", path2);
4895 sb2.st_mode = S_IFLNK;
4896 sb2.st_size = link_len;
4899 if (fd2 != -1) {
4900 if (fstat(fd2, &sb2) == -1) {
4901 err = got_error_from_errno2("fstat", path2);
4902 goto done;
4905 f2 = fdopen(fd2, "r");
4906 if (f2 == NULL) {
4907 err = got_error_from_errno2("fdopen", path2);
4908 goto done;
4910 fd2 = -1;
4911 } else {
4912 size_t n;
4913 f2 = got_opentemp();
4914 if (f2 == NULL) {
4915 err = got_error_from_errno2("got_opentemp", path2);
4916 goto done;
4918 n = fwrite(link_target, 1, link_len, f2);
4919 if (n != link_len) {
4920 err = got_ferror(f2, GOT_ERR_IO);
4921 goto done;
4923 if (fflush(f2) == EOF) {
4924 err = got_error_from_errno("fflush");
4925 goto done;
4927 rewind(f2);
4930 fd = got_opentempfd();
4931 if (fd == -1) {
4932 err = got_error_from_errno("got_opentempfd");
4933 goto done;
4936 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4937 if (err)
4938 goto done;
4940 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4941 if (err)
4942 goto done;
4944 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4945 if (err)
4946 goto done;
4948 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4949 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4950 if (err)
4951 goto done;
4953 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4954 "");
4955 if (err)
4956 goto done;
4958 if (fseek(f1, 0L, SEEK_SET) == -1)
4959 return got_ferror(f1, GOT_ERR_IO);
4960 if (fseek(f2, 0L, SEEK_SET) == -1)
4961 return got_ferror(f2, GOT_ERR_IO);
4963 /* Count the number of actual changes in the diff result. */
4964 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4965 struct diff_chunk_context cc = {};
4966 diff_chunk_context_load_change(&cc, &nchunks_used,
4967 diffreg_result->result, n, 0);
4968 nchanges++;
4970 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4971 int choice;
4972 err = apply_or_reject_change(&choice, &nchunks_used,
4973 diffreg_result->result, n, relpath, f1, f2,
4974 &line_cur1, &line_cur2,
4975 reverse_patch ? NULL : outfile,
4976 reverse_patch ? outfile : NULL,
4977 ++i, nchanges, patch_cb, patch_arg);
4978 if (err)
4979 goto done;
4980 if (choice == GOT_PATCH_CHOICE_YES)
4981 have_content = 1;
4982 else if (choice == GOT_PATCH_CHOICE_QUIT)
4983 break;
4985 if (have_content) {
4986 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4987 reverse_patch ? NULL : outfile,
4988 reverse_patch ? outfile : NULL);
4989 if (err)
4990 goto done;
4992 if (!S_ISLNK(sb2.st_mode)) {
4993 mode_t mode;
4995 mode = apply_umask(sb2.st_mode);
4996 if (fchmod(fileno(outfile), mode) == -1) {
4997 err = got_error_from_errno2("fchmod", path2);
4998 goto done;
5002 done:
5003 free(id_str);
5004 if (fd != -1 && close(fd) == -1 && err == NULL)
5005 err = got_error_from_errno("close");
5006 if (blob)
5007 got_object_blob_close(blob);
5008 free_err = got_diffreg_result_free(diffreg_result);
5009 if (err == NULL)
5010 err = free_err;
5011 if (f1 && fclose(f1) == EOF && err == NULL)
5012 err = got_error_from_errno2("fclose", path1);
5013 if (f2 && fclose(f2) == EOF && err == NULL)
5014 err = got_error_from_errno2("fclose", path2);
5015 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5016 err = got_error_from_errno2("close", path2);
5017 if (outfile && fclose(outfile) == EOF && err == NULL)
5018 err = got_error_from_errno2("fclose", *path_outfile);
5019 if (path1 && unlink(path1) == -1 && err == NULL)
5020 err = got_error_from_errno2("unlink", path1);
5021 if (err || !have_content) {
5022 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5023 err = got_error_from_errno2("unlink", *path_outfile);
5024 free(*path_outfile);
5025 *path_outfile = NULL;
5027 free(path1);
5028 return err;
5031 static const struct got_error *
5032 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5033 const char *relpath, struct got_object_id *blob_id,
5034 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5035 int dirfd, const char *de_name)
5037 struct revert_file_args *a = arg;
5038 const struct got_error *err = NULL;
5039 char *parent_path = NULL;
5040 struct got_fileindex_entry *ie;
5041 struct got_commit_object *base_commit = NULL;
5042 struct got_tree_object *tree = NULL;
5043 struct got_object_id *tree_id = NULL;
5044 const struct got_tree_entry *te = NULL;
5045 char *tree_path = NULL, *te_name;
5046 char *ondisk_path = NULL, *path_content = NULL;
5047 struct got_blob_object *blob = NULL;
5048 int fd = -1;
5050 /* Reverting a staged deletion is a no-op. */
5051 if (status == GOT_STATUS_DELETE &&
5052 staged_status != GOT_STATUS_NO_CHANGE)
5053 return NULL;
5055 if (status == GOT_STATUS_UNVERSIONED)
5056 return (*a->progress_cb)(a->progress_arg,
5057 GOT_STATUS_UNVERSIONED, relpath);
5059 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5060 if (ie == NULL)
5061 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5063 /* Construct in-repository path of tree which contains this blob. */
5064 err = got_path_dirname(&parent_path, ie->path);
5065 if (err) {
5066 if (err->code != GOT_ERR_BAD_PATH)
5067 goto done;
5068 parent_path = strdup("/");
5069 if (parent_path == NULL) {
5070 err = got_error_from_errno("strdup");
5071 goto done;
5074 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5075 tree_path = strdup(parent_path);
5076 if (tree_path == NULL) {
5077 err = got_error_from_errno("strdup");
5078 goto done;
5080 } else {
5081 if (got_path_is_root_dir(parent_path)) {
5082 tree_path = strdup(a->worktree->path_prefix);
5083 if (tree_path == NULL) {
5084 err = got_error_from_errno("strdup");
5085 goto done;
5087 } else {
5088 if (asprintf(&tree_path, "%s/%s",
5089 a->worktree->path_prefix, parent_path) == -1) {
5090 err = got_error_from_errno("asprintf");
5091 goto done;
5096 err = got_object_open_as_commit(&base_commit, a->repo,
5097 a->worktree->base_commit_id);
5098 if (err)
5099 goto done;
5101 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5102 if (err) {
5103 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5104 (status == GOT_STATUS_ADD ||
5105 staged_status == GOT_STATUS_ADD)))
5106 goto done;
5107 } else {
5108 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5109 if (err)
5110 goto done;
5112 err = got_path_basename(&te_name, ie->path);
5113 if (err)
5114 goto done;
5116 te = got_object_tree_find_entry(tree, te_name);
5117 free(te_name);
5118 if (te == NULL && status != GOT_STATUS_ADD &&
5119 staged_status != GOT_STATUS_ADD) {
5120 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5121 goto done;
5125 switch (status) {
5126 case GOT_STATUS_ADD:
5127 if (a->patch_cb) {
5128 int choice = GOT_PATCH_CHOICE_NONE;
5129 err = (*a->patch_cb)(&choice, a->patch_arg,
5130 status, ie->path, NULL, 1, 1);
5131 if (err)
5132 goto done;
5133 if (choice != GOT_PATCH_CHOICE_YES)
5134 break;
5136 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5137 ie->path);
5138 if (err)
5139 goto done;
5140 got_fileindex_entry_remove(a->fileindex, ie);
5141 if (a->unlink_added_files) {
5142 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5144 if (a->added_files_to_unlink) {
5145 struct got_pathlist_entry *pe;
5147 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5148 entry) {
5149 if (got_path_cmp(pe->path, relpath,
5150 pe->path_len, strlen(relpath)))
5151 continue;
5152 do_unlink = 1;
5153 break;
5157 if (do_unlink) {
5158 if (asprintf(&ondisk_path, "%s/%s",
5159 got_worktree_get_root_path(a->worktree),
5160 relpath) == -1) {
5161 err = got_error_from_errno("asprintf");
5162 goto done;
5164 if (unlink(ondisk_path) == -1) {
5165 err = got_error_from_errno2("unlink",
5166 ondisk_path);
5167 break;
5171 break;
5172 case GOT_STATUS_DELETE:
5173 if (a->patch_cb) {
5174 int choice = GOT_PATCH_CHOICE_NONE;
5175 err = (*a->patch_cb)(&choice, a->patch_arg,
5176 status, ie->path, NULL, 1, 1);
5177 if (err)
5178 goto done;
5179 if (choice != GOT_PATCH_CHOICE_YES)
5180 break;
5182 /* fall through */
5183 case GOT_STATUS_MODIFY:
5184 case GOT_STATUS_MODE_CHANGE:
5185 case GOT_STATUS_CONFLICT:
5186 case GOT_STATUS_MISSING: {
5187 struct got_object_id id;
5188 if (staged_status == GOT_STATUS_ADD ||
5189 staged_status == GOT_STATUS_MODIFY)
5190 got_fileindex_entry_get_staged_blob_id(&id, ie);
5191 else
5192 got_fileindex_entry_get_blob_id(&id, ie);
5193 fd = got_opentempfd();
5194 if (fd == -1) {
5195 err = got_error_from_errno("got_opentempfd");
5196 goto done;
5199 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5200 if (err)
5201 goto done;
5203 if (asprintf(&ondisk_path, "%s/%s",
5204 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5205 err = got_error_from_errno("asprintf");
5206 goto done;
5209 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5210 status == GOT_STATUS_CONFLICT)) {
5211 int is_bad_symlink = 0;
5212 err = create_patched_content(&path_content, 1, &id,
5213 ondisk_path, dirfd, de_name, ie->path, a->repo,
5214 a->patch_cb, a->patch_arg);
5215 if (err || path_content == NULL)
5216 break;
5217 if (te && S_ISLNK(te->mode)) {
5218 if (unlink(path_content) == -1) {
5219 err = got_error_from_errno2("unlink",
5220 path_content);
5221 break;
5223 err = install_symlink(&is_bad_symlink,
5224 a->worktree, ondisk_path, ie->path,
5225 blob, 0, 1, 0, 0, a->repo,
5226 a->progress_cb, a->progress_arg);
5227 } else {
5228 if (rename(path_content, ondisk_path) == -1) {
5229 err = got_error_from_errno3("rename",
5230 path_content, ondisk_path);
5231 goto done;
5234 } else {
5235 int is_bad_symlink = 0;
5236 if (te && S_ISLNK(te->mode)) {
5237 err = install_symlink(&is_bad_symlink,
5238 a->worktree, ondisk_path, ie->path,
5239 blob, 0, 1, 0, 0, a->repo,
5240 a->progress_cb, a->progress_arg);
5241 } else {
5242 err = install_blob(a->worktree, ondisk_path,
5243 ie->path,
5244 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5245 got_fileindex_perms_to_st(ie), blob,
5246 0, 1, 0, 0, a->repo,
5247 a->progress_cb, a->progress_arg);
5249 if (err)
5250 goto done;
5251 if (status == GOT_STATUS_DELETE ||
5252 status == GOT_STATUS_MODE_CHANGE) {
5253 err = got_fileindex_entry_update(ie,
5254 a->worktree->root_fd, relpath,
5255 blob->id.sha1,
5256 a->worktree->base_commit_id->sha1, 1);
5257 if (err)
5258 goto done;
5260 if (is_bad_symlink) {
5261 got_fileindex_entry_filetype_set(ie,
5262 GOT_FILEIDX_MODE_BAD_SYMLINK);
5265 break;
5267 default:
5268 break;
5270 done:
5271 free(ondisk_path);
5272 free(path_content);
5273 free(parent_path);
5274 free(tree_path);
5275 if (fd != -1 && close(fd) == -1 && err == NULL)
5276 err = got_error_from_errno("close");
5277 if (blob)
5278 got_object_blob_close(blob);
5279 if (tree)
5280 got_object_tree_close(tree);
5281 free(tree_id);
5282 if (base_commit)
5283 got_object_commit_close(base_commit);
5284 return err;
5287 const struct got_error *
5288 got_worktree_revert(struct got_worktree *worktree,
5289 struct got_pathlist_head *paths,
5290 got_worktree_checkout_cb progress_cb, void *progress_arg,
5291 got_worktree_patch_cb patch_cb, void *patch_arg,
5292 struct got_repository *repo)
5294 struct got_fileindex *fileindex = NULL;
5295 char *fileindex_path = NULL;
5296 const struct got_error *err = NULL, *unlockerr = NULL;
5297 const struct got_error *sync_err = NULL;
5298 struct got_pathlist_entry *pe;
5299 struct revert_file_args rfa;
5301 err = lock_worktree(worktree, LOCK_EX);
5302 if (err)
5303 return err;
5305 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5306 if (err)
5307 goto done;
5309 rfa.worktree = worktree;
5310 rfa.fileindex = fileindex;
5311 rfa.progress_cb = progress_cb;
5312 rfa.progress_arg = progress_arg;
5313 rfa.patch_cb = patch_cb;
5314 rfa.patch_arg = patch_arg;
5315 rfa.repo = repo;
5316 rfa.unlink_added_files = 0;
5317 TAILQ_FOREACH(pe, paths, entry) {
5318 err = worktree_status(worktree, pe->path, fileindex, repo,
5319 revert_file, &rfa, NULL, NULL, 1, 0);
5320 if (err)
5321 break;
5323 sync_err = sync_fileindex(fileindex, fileindex_path);
5324 if (sync_err && err == NULL)
5325 err = sync_err;
5326 done:
5327 free(fileindex_path);
5328 if (fileindex)
5329 got_fileindex_free(fileindex);
5330 unlockerr = lock_worktree(worktree, LOCK_SH);
5331 if (unlockerr && err == NULL)
5332 err = unlockerr;
5333 return err;
5336 static void
5337 free_commitable(struct got_commitable *ct)
5339 free(ct->path);
5340 free(ct->in_repo_path);
5341 free(ct->ondisk_path);
5342 free(ct->blob_id);
5343 free(ct->base_blob_id);
5344 free(ct->staged_blob_id);
5345 free(ct->base_commit_id);
5346 free(ct);
5349 struct collect_commitables_arg {
5350 struct got_pathlist_head *commitable_paths;
5351 struct got_repository *repo;
5352 struct got_worktree *worktree;
5353 struct got_fileindex *fileindex;
5354 int have_staged_files;
5355 int allow_bad_symlinks;
5356 int diff_header_shown;
5357 int commit_conflicts;
5358 FILE *diff_outfile;
5359 FILE *f1;
5360 FILE *f2;
5364 * Create a file which contains the target path of a symlink so we can feed
5365 * it as content to the diff engine.
5367 static const struct got_error *
5368 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5369 const char *abspath)
5371 const struct got_error *err = NULL;
5372 char target_path[PATH_MAX];
5373 ssize_t target_len, outlen;
5375 *fd = -1;
5377 if (dirfd != -1) {
5378 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5379 if (target_len == -1)
5380 return got_error_from_errno2("readlinkat", abspath);
5381 } else {
5382 target_len = readlink(abspath, target_path, PATH_MAX);
5383 if (target_len == -1)
5384 return got_error_from_errno2("readlink", abspath);
5387 *fd = got_opentempfd();
5388 if (*fd == -1)
5389 return got_error_from_errno("got_opentempfd");
5391 outlen = write(*fd, target_path, target_len);
5392 if (outlen == -1) {
5393 err = got_error_from_errno("got_opentempfd");
5394 goto done;
5397 if (lseek(*fd, 0, SEEK_SET) == -1) {
5398 err = got_error_from_errno2("lseek", abspath);
5399 goto done;
5401 done:
5402 if (err) {
5403 close(*fd);
5404 *fd = -1;
5406 return err;
5409 static const struct got_error *
5410 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5411 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5412 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5414 const struct got_error *err = NULL;
5415 struct got_blob_object *blob1 = NULL;
5416 int fd = -1, fd1 = -1, fd2 = -1;
5417 FILE *ondisk_file = NULL;
5418 char *label1 = NULL;
5419 struct stat sb;
5420 off_t size1 = 0;
5421 int f2_exists = 0;
5422 char *id_str = NULL;
5424 memset(&sb, 0, sizeof(sb));
5426 if (diff_staged) {
5427 if (ct->staged_status != GOT_STATUS_MODIFY &&
5428 ct->staged_status != GOT_STATUS_ADD &&
5429 ct->staged_status != GOT_STATUS_DELETE)
5430 return NULL;
5431 } else {
5432 if (ct->status != GOT_STATUS_MODIFY &&
5433 ct->status != GOT_STATUS_ADD &&
5434 ct->status != GOT_STATUS_DELETE &&
5435 ct->status != GOT_STATUS_CONFLICT)
5436 return NULL;
5439 err = got_opentemp_truncate(f1);
5440 if (err)
5441 return got_error_from_errno("got_opentemp_truncate");
5442 err = got_opentemp_truncate(f2);
5443 if (err)
5444 return got_error_from_errno("got_opentemp_truncate");
5446 if (!*diff_header_shown) {
5447 err = got_object_id_str(&id_str, worktree->base_commit_id);
5448 if (err)
5449 return err;
5450 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5451 got_worktree_get_root_path(worktree));
5452 fprintf(diff_outfile, "commit - %s\n", id_str);
5453 fprintf(diff_outfile, "path + %s%s\n",
5454 got_worktree_get_root_path(worktree),
5455 diff_staged ? " (staged changes)" : "");
5456 *diff_header_shown = 1;
5459 if (diff_staged) {
5460 const char *label1 = NULL, *label2 = NULL;
5461 switch (ct->staged_status) {
5462 case GOT_STATUS_MODIFY:
5463 label1 = ct->path;
5464 label2 = ct->path;
5465 break;
5466 case GOT_STATUS_ADD:
5467 label2 = ct->path;
5468 break;
5469 case GOT_STATUS_DELETE:
5470 label1 = ct->path;
5471 break;
5472 default:
5473 return got_error(GOT_ERR_FILE_STATUS);
5475 fd1 = got_opentempfd();
5476 if (fd1 == -1) {
5477 err = got_error_from_errno("got_opentempfd");
5478 goto done;
5480 fd2 = got_opentempfd();
5481 if (fd2 == -1) {
5482 err = got_error_from_errno("got_opentempfd");
5483 goto done;
5485 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5486 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5487 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5488 NULL, repo, diff_outfile);
5489 goto done;
5492 fd1 = got_opentempfd();
5493 if (fd1 == -1) {
5494 err = got_error_from_errno("got_opentempfd");
5495 goto done;
5498 if (ct->status != GOT_STATUS_ADD) {
5499 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5500 8192, fd1);
5501 if (err)
5502 goto done;
5505 if (ct->status != GOT_STATUS_DELETE) {
5506 if (dirfd != -1) {
5507 fd = openat(dirfd, de_name,
5508 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5509 if (fd == -1) {
5510 if (!got_err_open_nofollow_on_symlink()) {
5511 err = got_error_from_errno2("openat",
5512 ct->ondisk_path);
5513 goto done;
5515 err = get_symlink_target_file(&fd, dirfd,
5516 de_name, ct->ondisk_path);
5517 if (err)
5518 goto done;
5520 } else {
5521 fd = open(ct->ondisk_path,
5522 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5523 if (fd == -1) {
5524 if (!got_err_open_nofollow_on_symlink()) {
5525 err = got_error_from_errno2("open",
5526 ct->ondisk_path);
5527 goto done;
5529 err = get_symlink_target_file(&fd, dirfd,
5530 de_name, ct->ondisk_path);
5531 if (err)
5532 goto done;
5535 if (fstatat(fd, ct->ondisk_path, &sb,
5536 AT_SYMLINK_NOFOLLOW) == -1) {
5537 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5538 goto done;
5540 ondisk_file = fdopen(fd, "r");
5541 if (ondisk_file == NULL) {
5542 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5543 goto done;
5545 fd = -1;
5546 f2_exists = 1;
5549 if (blob1) {
5550 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5551 f1, blob1);
5552 if (err)
5553 goto done;
5556 err = got_diff_blob_file(blob1, f1, size1, label1,
5557 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5558 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5559 done:
5560 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5561 err = got_error_from_errno("close");
5562 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5563 err = got_error_from_errno("close");
5564 if (blob1)
5565 got_object_blob_close(blob1);
5566 if (fd != -1 && close(fd) == -1 && err == NULL)
5567 err = got_error_from_errno("close");
5568 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5569 err = got_error_from_errno("fclose");
5570 return err;
5573 static const struct got_error *
5574 collect_commitables(void *arg, unsigned char status,
5575 unsigned char staged_status, const char *relpath,
5576 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5577 struct got_object_id *commit_id, int dirfd, const char *de_name)
5579 struct collect_commitables_arg *a = arg;
5580 const struct got_error *err = NULL;
5581 struct got_commitable *ct = NULL;
5582 struct got_pathlist_entry *new = NULL;
5583 char *parent_path = NULL, *path = NULL;
5584 struct stat sb;
5586 if (a->have_staged_files) {
5587 if (staged_status != GOT_STATUS_MODIFY &&
5588 staged_status != GOT_STATUS_ADD &&
5589 staged_status != GOT_STATUS_DELETE)
5590 return NULL;
5591 } else {
5592 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5593 printf("C %s\n", relpath);
5594 return got_error(GOT_ERR_COMMIT_CONFLICT);
5597 if (status != GOT_STATUS_MODIFY &&
5598 status != GOT_STATUS_MODE_CHANGE &&
5599 status != GOT_STATUS_ADD &&
5600 status != GOT_STATUS_DELETE &&
5601 status != GOT_STATUS_CONFLICT)
5602 return NULL;
5605 if (asprintf(&path, "/%s", relpath) == -1) {
5606 err = got_error_from_errno("asprintf");
5607 goto done;
5609 if (strcmp(path, "/") == 0) {
5610 parent_path = strdup("");
5611 if (parent_path == NULL)
5612 return got_error_from_errno("strdup");
5613 } else {
5614 err = got_path_dirname(&parent_path, path);
5615 if (err)
5616 return err;
5619 ct = calloc(1, sizeof(*ct));
5620 if (ct == NULL) {
5621 err = got_error_from_errno("calloc");
5622 goto done;
5625 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5626 relpath) == -1) {
5627 err = got_error_from_errno("asprintf");
5628 goto done;
5631 if (staged_status == GOT_STATUS_ADD ||
5632 staged_status == GOT_STATUS_MODIFY) {
5633 struct got_fileindex_entry *ie;
5634 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5635 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5636 case GOT_FILEIDX_MODE_REGULAR_FILE:
5637 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5638 ct->mode = S_IFREG;
5639 break;
5640 case GOT_FILEIDX_MODE_SYMLINK:
5641 ct->mode = S_IFLNK;
5642 break;
5643 default:
5644 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5645 goto done;
5647 ct->mode |= got_fileindex_entry_perms_get(ie);
5648 } else if (status != GOT_STATUS_DELETE &&
5649 staged_status != GOT_STATUS_DELETE) {
5650 if (dirfd != -1) {
5651 if (fstatat(dirfd, de_name, &sb,
5652 AT_SYMLINK_NOFOLLOW) == -1) {
5653 err = got_error_from_errno2("fstatat",
5654 ct->ondisk_path);
5655 goto done;
5657 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5658 err = got_error_from_errno2("lstat", ct->ondisk_path);
5659 goto done;
5661 ct->mode = sb.st_mode;
5664 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5665 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5666 relpath) == -1) {
5667 err = got_error_from_errno("asprintf");
5668 goto done;
5671 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5672 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5673 int is_bad_symlink;
5674 char target_path[PATH_MAX];
5675 ssize_t target_len;
5676 target_len = readlink(ct->ondisk_path, target_path,
5677 sizeof(target_path));
5678 if (target_len == -1) {
5679 err = got_error_from_errno2("readlink",
5680 ct->ondisk_path);
5681 goto done;
5683 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5684 target_len, ct->ondisk_path, a->worktree->root_path,
5685 a->worktree->meta_dir);
5686 if (err)
5687 goto done;
5688 if (is_bad_symlink) {
5689 err = got_error_path(ct->ondisk_path,
5690 GOT_ERR_BAD_SYMLINK);
5691 goto done;
5696 ct->status = status;
5697 ct->staged_status = staged_status;
5698 ct->blob_id = NULL; /* will be filled in when blob gets created */
5699 if (ct->status != GOT_STATUS_ADD &&
5700 ct->staged_status != GOT_STATUS_ADD) {
5701 ct->base_blob_id = got_object_id_dup(blob_id);
5702 if (ct->base_blob_id == NULL) {
5703 err = got_error_from_errno("got_object_id_dup");
5704 goto done;
5706 ct->base_commit_id = got_object_id_dup(commit_id);
5707 if (ct->base_commit_id == NULL) {
5708 err = got_error_from_errno("got_object_id_dup");
5709 goto done;
5712 if (ct->staged_status == GOT_STATUS_ADD ||
5713 ct->staged_status == GOT_STATUS_MODIFY) {
5714 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5715 if (ct->staged_blob_id == NULL) {
5716 err = got_error_from_errno("got_object_id_dup");
5717 goto done;
5720 ct->path = strdup(path);
5721 if (ct->path == NULL) {
5722 err = got_error_from_errno("strdup");
5723 goto done;
5725 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5726 if (err)
5727 goto done;
5729 if (a->diff_outfile && ct && new != NULL) {
5730 err = append_ct_diff(ct, &a->diff_header_shown,
5731 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5732 a->have_staged_files, a->repo, a->worktree);
5733 if (err)
5734 goto done;
5736 done:
5737 if (ct && (err || new == NULL))
5738 free_commitable(ct);
5739 free(parent_path);
5740 free(path);
5741 return err;
5744 static const struct got_error *write_tree(struct got_object_id **, int *,
5745 struct got_tree_object *, const char *, struct got_pathlist_head *,
5746 got_worktree_status_cb status_cb, void *status_arg,
5747 struct got_repository *);
5749 static const struct got_error *
5750 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5751 struct got_tree_entry *te, const char *parent_path,
5752 struct got_pathlist_head *commitable_paths,
5753 got_worktree_status_cb status_cb, void *status_arg,
5754 struct got_repository *repo)
5756 const struct got_error *err = NULL;
5757 struct got_tree_object *subtree;
5758 char *subpath;
5760 if (asprintf(&subpath, "%s%s%s", parent_path,
5761 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5762 return got_error_from_errno("asprintf");
5764 err = got_object_open_as_tree(&subtree, repo, &te->id);
5765 if (err)
5766 return err;
5768 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5769 commitable_paths, status_cb, status_arg, repo);
5770 got_object_tree_close(subtree);
5771 free(subpath);
5772 return err;
5775 static const struct got_error *
5776 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5778 const struct got_error *err = NULL;
5779 char *ct_parent_path = NULL;
5781 *match = 0;
5783 if (strchr(ct->in_repo_path, '/') == NULL) {
5784 *match = got_path_is_root_dir(path);
5785 return NULL;
5788 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5789 if (err)
5790 return err;
5791 *match = (strcmp(path, ct_parent_path) == 0);
5792 free(ct_parent_path);
5793 return err;
5796 static mode_t
5797 get_ct_file_mode(struct got_commitable *ct)
5799 if (S_ISLNK(ct->mode))
5800 return S_IFLNK;
5802 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5805 static const struct got_error *
5806 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5807 struct got_tree_entry *te, struct got_commitable *ct)
5809 const struct got_error *err = NULL;
5811 *new_te = NULL;
5813 err = got_object_tree_entry_dup(new_te, te);
5814 if (err)
5815 goto done;
5817 (*new_te)->mode = get_ct_file_mode(ct);
5819 if (ct->staged_status == GOT_STATUS_MODIFY)
5820 memcpy(&(*new_te)->id, ct->staged_blob_id,
5821 sizeof((*new_te)->id));
5822 else
5823 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5824 done:
5825 if (err && *new_te) {
5826 free(*new_te);
5827 *new_te = NULL;
5829 return err;
5832 static const struct got_error *
5833 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5834 struct got_commitable *ct)
5836 const struct got_error *err = NULL;
5837 char *ct_name = NULL;
5839 *new_te = NULL;
5841 *new_te = calloc(1, sizeof(**new_te));
5842 if (*new_te == NULL)
5843 return got_error_from_errno("calloc");
5845 err = got_path_basename(&ct_name, ct->path);
5846 if (err)
5847 goto done;
5848 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5849 sizeof((*new_te)->name)) {
5850 err = got_error(GOT_ERR_NO_SPACE);
5851 goto done;
5854 (*new_te)->mode = get_ct_file_mode(ct);
5856 if (ct->staged_status == GOT_STATUS_ADD)
5857 memcpy(&(*new_te)->id, ct->staged_blob_id,
5858 sizeof((*new_te)->id));
5859 else
5860 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5861 done:
5862 free(ct_name);
5863 if (err && *new_te) {
5864 free(*new_te);
5865 *new_te = NULL;
5867 return err;
5870 static const struct got_error *
5871 insert_tree_entry(struct got_tree_entry *new_te,
5872 struct got_pathlist_head *paths)
5874 const struct got_error *err = NULL;
5875 struct got_pathlist_entry *new_pe;
5877 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5878 if (err)
5879 return err;
5880 if (new_pe == NULL)
5881 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5882 return NULL;
5885 static const struct got_error *
5886 report_ct_status(struct got_commitable *ct,
5887 got_worktree_status_cb status_cb, void *status_arg)
5889 const char *ct_path = ct->path;
5890 unsigned char status;
5892 if (status_cb == NULL) /* no commit progress output desired */
5893 return NULL;
5895 while (ct_path[0] == '/')
5896 ct_path++;
5898 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5899 status = ct->staged_status;
5900 else
5901 status = ct->status;
5903 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5904 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5907 static const struct got_error *
5908 match_modified_subtree(int *modified, struct got_tree_entry *te,
5909 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5911 const struct got_error *err = NULL;
5912 struct got_pathlist_entry *pe;
5913 char *te_path;
5915 *modified = 0;
5917 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5918 got_path_is_root_dir(base_tree_path) ? "" : "/",
5919 te->name) == -1)
5920 return got_error_from_errno("asprintf");
5922 TAILQ_FOREACH(pe, commitable_paths, entry) {
5923 struct got_commitable *ct = pe->data;
5924 *modified = got_path_is_child(ct->in_repo_path, te_path,
5925 strlen(te_path));
5926 if (*modified)
5927 break;
5930 free(te_path);
5931 return err;
5934 static const struct got_error *
5935 match_deleted_or_modified_ct(struct got_commitable **ctp,
5936 struct got_tree_entry *te, const char *base_tree_path,
5937 struct got_pathlist_head *commitable_paths)
5939 const struct got_error *err = NULL;
5940 struct got_pathlist_entry *pe;
5942 *ctp = NULL;
5944 TAILQ_FOREACH(pe, commitable_paths, entry) {
5945 struct got_commitable *ct = pe->data;
5946 char *ct_name = NULL;
5947 int path_matches;
5949 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5950 if (ct->status != GOT_STATUS_MODIFY &&
5951 ct->status != GOT_STATUS_MODE_CHANGE &&
5952 ct->status != GOT_STATUS_DELETE &&
5953 ct->status != GOT_STATUS_CONFLICT)
5954 continue;
5955 } else {
5956 if (ct->staged_status != GOT_STATUS_MODIFY &&
5957 ct->staged_status != GOT_STATUS_DELETE)
5958 continue;
5961 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5962 continue;
5964 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5965 if (err)
5966 return err;
5967 if (!path_matches)
5968 continue;
5970 err = got_path_basename(&ct_name, pe->path);
5971 if (err)
5972 return err;
5974 if (strcmp(te->name, ct_name) != 0) {
5975 free(ct_name);
5976 continue;
5978 free(ct_name);
5980 *ctp = ct;
5981 break;
5984 return err;
5987 static const struct got_error *
5988 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5989 const char *child_path, const char *path_base_tree,
5990 struct got_pathlist_head *commitable_paths,
5991 got_worktree_status_cb status_cb, void *status_arg,
5992 struct got_repository *repo)
5994 const struct got_error *err = NULL;
5995 struct got_tree_entry *new_te;
5996 char *subtree_path;
5997 struct got_object_id *id = NULL;
5998 int nentries;
6000 *new_tep = NULL;
6002 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6003 got_path_is_root_dir(path_base_tree) ? "" : "/",
6004 child_path) == -1)
6005 return got_error_from_errno("asprintf");
6007 new_te = calloc(1, sizeof(*new_te));
6008 if (new_te == NULL)
6009 return got_error_from_errno("calloc");
6010 new_te->mode = S_IFDIR;
6012 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6013 sizeof(new_te->name)) {
6014 err = got_error(GOT_ERR_NO_SPACE);
6015 goto done;
6017 err = write_tree(&id, &nentries, NULL, subtree_path,
6018 commitable_paths, status_cb, status_arg, repo);
6019 if (err) {
6020 free(new_te);
6021 goto done;
6023 memcpy(&new_te->id, id, sizeof(new_te->id));
6024 done:
6025 free(id);
6026 free(subtree_path);
6027 if (err == NULL)
6028 *new_tep = new_te;
6029 return err;
6032 static const struct got_error *
6033 write_tree(struct got_object_id **new_tree_id, int *nentries,
6034 struct got_tree_object *base_tree, const char *path_base_tree,
6035 struct got_pathlist_head *commitable_paths,
6036 got_worktree_status_cb status_cb, void *status_arg,
6037 struct got_repository *repo)
6039 const struct got_error *err = NULL;
6040 struct got_pathlist_head paths;
6041 struct got_tree_entry *te, *new_te = NULL;
6042 struct got_pathlist_entry *pe;
6044 TAILQ_INIT(&paths);
6045 *nentries = 0;
6047 /* Insert, and recurse into, newly added entries first. */
6048 TAILQ_FOREACH(pe, commitable_paths, entry) {
6049 struct got_commitable *ct = pe->data;
6050 char *child_path = NULL, *slash;
6052 if ((ct->status != GOT_STATUS_ADD &&
6053 ct->staged_status != GOT_STATUS_ADD) ||
6054 (ct->flags & GOT_COMMITABLE_ADDED))
6055 continue;
6057 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6058 strlen(path_base_tree)))
6059 continue;
6061 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6062 ct->in_repo_path);
6063 if (err)
6064 goto done;
6066 slash = strchr(child_path, '/');
6067 if (slash == NULL) {
6068 err = alloc_added_blob_tree_entry(&new_te, ct);
6069 if (err)
6070 goto done;
6071 err = report_ct_status(ct, status_cb, status_arg);
6072 if (err)
6073 goto done;
6074 ct->flags |= GOT_COMMITABLE_ADDED;
6075 err = insert_tree_entry(new_te, &paths);
6076 if (err)
6077 goto done;
6078 (*nentries)++;
6079 } else {
6080 *slash = '\0'; /* trim trailing path components */
6081 if (base_tree == NULL ||
6082 got_object_tree_find_entry(base_tree, child_path)
6083 == NULL) {
6084 err = make_subtree_for_added_blob(&new_te,
6085 child_path, path_base_tree,
6086 commitable_paths, status_cb, status_arg,
6087 repo);
6088 if (err)
6089 goto done;
6090 err = insert_tree_entry(new_te, &paths);
6091 if (err)
6092 goto done;
6093 (*nentries)++;
6098 if (base_tree) {
6099 int i, nbase_entries;
6100 /* Handle modified and deleted entries. */
6101 nbase_entries = got_object_tree_get_nentries(base_tree);
6102 for (i = 0; i < nbase_entries; i++) {
6103 struct got_commitable *ct = NULL;
6105 te = got_object_tree_get_entry(base_tree, i);
6106 if (got_object_tree_entry_is_submodule(te)) {
6107 /* Entry is a submodule; just copy it. */
6108 err = got_object_tree_entry_dup(&new_te, te);
6109 if (err)
6110 goto done;
6111 err = insert_tree_entry(new_te, &paths);
6112 if (err)
6113 goto done;
6114 (*nentries)++;
6115 continue;
6118 if (S_ISDIR(te->mode)) {
6119 int modified;
6120 err = got_object_tree_entry_dup(&new_te, te);
6121 if (err)
6122 goto done;
6123 err = match_modified_subtree(&modified, te,
6124 path_base_tree, commitable_paths);
6125 if (err)
6126 goto done;
6127 /* Avoid recursion into unmodified subtrees. */
6128 if (modified) {
6129 struct got_object_id *new_id;
6130 int nsubentries;
6131 err = write_subtree(&new_id,
6132 &nsubentries, te,
6133 path_base_tree, commitable_paths,
6134 status_cb, status_arg, repo);
6135 if (err)
6136 goto done;
6137 if (nsubentries == 0) {
6138 /* All entries were deleted. */
6139 free(new_id);
6140 continue;
6142 memcpy(&new_te->id, new_id,
6143 sizeof(new_te->id));
6144 free(new_id);
6146 err = insert_tree_entry(new_te, &paths);
6147 if (err)
6148 goto done;
6149 (*nentries)++;
6150 continue;
6153 err = match_deleted_or_modified_ct(&ct, te,
6154 path_base_tree, commitable_paths);
6155 if (err)
6156 goto done;
6157 if (ct) {
6158 /* NB: Deleted entries get dropped here. */
6159 if (ct->status == GOT_STATUS_MODIFY ||
6160 ct->status == GOT_STATUS_MODE_CHANGE ||
6161 ct->status == GOT_STATUS_CONFLICT ||
6162 ct->staged_status == GOT_STATUS_MODIFY) {
6163 err = alloc_modified_blob_tree_entry(
6164 &new_te, te, ct);
6165 if (err)
6166 goto done;
6167 err = insert_tree_entry(new_te, &paths);
6168 if (err)
6169 goto done;
6170 (*nentries)++;
6172 err = report_ct_status(ct, status_cb,
6173 status_arg);
6174 if (err)
6175 goto done;
6176 } else {
6177 /* Entry is unchanged; just copy it. */
6178 err = got_object_tree_entry_dup(&new_te, te);
6179 if (err)
6180 goto done;
6181 err = insert_tree_entry(new_te, &paths);
6182 if (err)
6183 goto done;
6184 (*nentries)++;
6189 /* Write new list of entries; deleted entries have been dropped. */
6190 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6191 done:
6192 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6193 return err;
6196 static const struct got_error *
6197 update_fileindex_after_commit(struct got_worktree *worktree,
6198 struct got_pathlist_head *commitable_paths,
6199 struct got_object_id *new_base_commit_id,
6200 struct got_fileindex *fileindex, int have_staged_files)
6202 const struct got_error *err = NULL;
6203 struct got_pathlist_entry *pe;
6204 char *relpath = NULL;
6206 TAILQ_FOREACH(pe, commitable_paths, entry) {
6207 struct got_fileindex_entry *ie;
6208 struct got_commitable *ct = pe->data;
6210 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6212 err = got_path_skip_common_ancestor(&relpath,
6213 worktree->root_path, ct->ondisk_path);
6214 if (err)
6215 goto done;
6217 if (ie) {
6218 if (ct->status == GOT_STATUS_DELETE ||
6219 ct->staged_status == GOT_STATUS_DELETE) {
6220 got_fileindex_entry_remove(fileindex, ie);
6221 } else if (ct->staged_status == GOT_STATUS_ADD ||
6222 ct->staged_status == GOT_STATUS_MODIFY) {
6223 got_fileindex_entry_stage_set(ie,
6224 GOT_FILEIDX_STAGE_NONE);
6225 got_fileindex_entry_staged_filetype_set(ie, 0);
6227 err = got_fileindex_entry_update(ie,
6228 worktree->root_fd, relpath,
6229 ct->staged_blob_id->sha1,
6230 new_base_commit_id->sha1,
6231 !have_staged_files);
6232 } else
6233 err = got_fileindex_entry_update(ie,
6234 worktree->root_fd, relpath,
6235 ct->blob_id->sha1,
6236 new_base_commit_id->sha1,
6237 !have_staged_files);
6238 } else {
6239 err = got_fileindex_entry_alloc(&ie, pe->path);
6240 if (err)
6241 goto done;
6242 err = got_fileindex_entry_update(ie,
6243 worktree->root_fd, relpath, ct->blob_id->sha1,
6244 new_base_commit_id->sha1, 1);
6245 if (err) {
6246 got_fileindex_entry_free(ie);
6247 goto done;
6249 err = got_fileindex_entry_add(fileindex, ie);
6250 if (err) {
6251 got_fileindex_entry_free(ie);
6252 goto done;
6255 free(relpath);
6256 relpath = NULL;
6258 done:
6259 free(relpath);
6260 return err;
6264 static const struct got_error *
6265 check_out_of_date(const char *in_repo_path, unsigned char status,
6266 unsigned char staged_status, struct got_object_id *base_blob_id,
6267 struct got_object_id *base_commit_id,
6268 struct got_object_id *head_commit_id, struct got_repository *repo,
6269 int ood_errcode)
6271 const struct got_error *err = NULL;
6272 struct got_commit_object *commit = NULL;
6273 struct got_object_id *id = NULL;
6275 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6276 /* Trivial case: base commit == head commit */
6277 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6278 return NULL;
6280 * Ensure file content which local changes were based
6281 * on matches file content in the branch head.
6283 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6284 if (err)
6285 goto done;
6286 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6287 if (err) {
6288 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6289 err = got_error(ood_errcode);
6290 goto done;
6291 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6292 err = got_error(ood_errcode);
6293 } else {
6294 /* Require that added files don't exist in the branch head. */
6295 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6296 if (err)
6297 goto done;
6298 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6299 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6300 goto done;
6301 err = id ? got_error(ood_errcode) : NULL;
6303 done:
6304 free(id);
6305 if (commit)
6306 got_object_commit_close(commit);
6307 return err;
6310 static const struct got_error *
6311 commit_worktree(struct got_object_id **new_commit_id,
6312 struct got_pathlist_head *commitable_paths,
6313 struct got_object_id *head_commit_id,
6314 struct got_object_id *parent_id2,
6315 struct got_worktree *worktree,
6316 const char *author, const char *committer, char *diff_path,
6317 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6318 got_worktree_status_cb status_cb, void *status_arg,
6319 struct got_repository *repo)
6321 const struct got_error *err = NULL, *unlockerr = NULL;
6322 struct got_pathlist_entry *pe;
6323 const char *head_ref_name = NULL;
6324 struct got_commit_object *head_commit = NULL;
6325 struct got_reference *head_ref2 = NULL;
6326 struct got_object_id *head_commit_id2 = NULL;
6327 struct got_tree_object *head_tree = NULL;
6328 struct got_object_id *new_tree_id = NULL;
6329 int nentries, nparents = 0;
6330 struct got_object_id_queue parent_ids;
6331 struct got_object_qid *pid = NULL;
6332 char *logmsg = NULL;
6333 time_t timestamp;
6335 *new_commit_id = NULL;
6337 STAILQ_INIT(&parent_ids);
6339 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6340 if (err)
6341 goto done;
6343 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6344 if (err)
6345 goto done;
6347 if (commit_msg_cb != NULL) {
6348 err = commit_msg_cb(commitable_paths, diff_path,
6349 &logmsg, commit_arg);
6350 if (err)
6351 goto done;
6354 if (logmsg == NULL || strlen(logmsg) == 0) {
6355 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6356 goto done;
6359 /* Create blobs from added and modified files and record their IDs. */
6360 TAILQ_FOREACH(pe, commitable_paths, entry) {
6361 struct got_commitable *ct = pe->data;
6362 char *ondisk_path;
6364 /* Blobs for staged files already exist. */
6365 if (ct->staged_status == GOT_STATUS_ADD ||
6366 ct->staged_status == GOT_STATUS_MODIFY)
6367 continue;
6369 if (ct->status != GOT_STATUS_ADD &&
6370 ct->status != GOT_STATUS_MODIFY &&
6371 ct->status != GOT_STATUS_MODE_CHANGE &&
6372 ct->status != GOT_STATUS_CONFLICT)
6373 continue;
6375 if (asprintf(&ondisk_path, "%s/%s",
6376 worktree->root_path, pe->path) == -1) {
6377 err = got_error_from_errno("asprintf");
6378 goto done;
6380 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6381 free(ondisk_path);
6382 if (err)
6383 goto done;
6386 /* Recursively write new tree objects. */
6387 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6388 commitable_paths, status_cb, status_arg, repo);
6389 if (err)
6390 goto done;
6392 err = got_object_qid_alloc(&pid, head_commit_id);
6393 if (err)
6394 goto done;
6395 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6396 nparents++;
6397 if (parent_id2) {
6398 err = got_object_qid_alloc(&pid, parent_id2);
6399 if (err)
6400 goto done;
6401 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6402 nparents++;
6404 timestamp = time(NULL);
6405 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6406 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6407 if (logmsg != NULL)
6408 free(logmsg);
6409 if (err)
6410 goto done;
6412 /* Check if a concurrent commit to our branch has occurred. */
6413 head_ref_name = got_worktree_get_head_ref_name(worktree);
6414 if (head_ref_name == NULL) {
6415 err = got_error_from_errno("got_worktree_get_head_ref_name");
6416 goto done;
6418 /* Lock the reference here to prevent concurrent modification. */
6419 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6420 if (err)
6421 goto done;
6422 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6423 if (err)
6424 goto done;
6425 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6426 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6427 goto done;
6429 /* Update branch head in repository. */
6430 err = got_ref_change_ref(head_ref2, *new_commit_id);
6431 if (err)
6432 goto done;
6433 err = got_ref_write(head_ref2, repo);
6434 if (err)
6435 goto done;
6437 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6438 if (err)
6439 goto done;
6441 err = ref_base_commit(worktree, repo);
6442 if (err)
6443 goto done;
6444 done:
6445 got_object_id_queue_free(&parent_ids);
6446 if (head_tree)
6447 got_object_tree_close(head_tree);
6448 if (head_commit)
6449 got_object_commit_close(head_commit);
6450 free(head_commit_id2);
6451 if (head_ref2) {
6452 unlockerr = got_ref_unlock(head_ref2);
6453 if (unlockerr && err == NULL)
6454 err = unlockerr;
6455 got_ref_close(head_ref2);
6457 return err;
6460 static const struct got_error *
6461 check_path_is_commitable(const char *path,
6462 struct got_pathlist_head *commitable_paths)
6464 struct got_pathlist_entry *cpe = NULL;
6465 size_t path_len = strlen(path);
6467 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6468 struct got_commitable *ct = cpe->data;
6469 const char *ct_path = ct->path;
6471 while (ct_path[0] == '/')
6472 ct_path++;
6474 if (strcmp(path, ct_path) == 0 ||
6475 got_path_is_child(ct_path, path, path_len))
6476 break;
6479 if (cpe == NULL)
6480 return got_error_path(path, GOT_ERR_BAD_PATH);
6482 return NULL;
6485 static const struct got_error *
6486 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6488 int *have_staged_files = arg;
6490 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6491 *have_staged_files = 1;
6492 return got_error(GOT_ERR_CANCELLED);
6495 return NULL;
6498 static const struct got_error *
6499 check_non_staged_files(struct got_fileindex *fileindex,
6500 struct got_pathlist_head *paths)
6502 struct got_pathlist_entry *pe;
6503 struct got_fileindex_entry *ie;
6505 TAILQ_FOREACH(pe, paths, entry) {
6506 if (pe->path[0] == '\0')
6507 continue;
6508 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6509 if (ie == NULL)
6510 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6511 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6512 return got_error_path(pe->path,
6513 GOT_ERR_FILE_NOT_STAGED);
6516 return NULL;
6519 const struct got_error *
6520 got_worktree_commit(struct got_object_id **new_commit_id,
6521 struct got_worktree *worktree, struct got_pathlist_head *paths,
6522 const char *author, const char *committer, int allow_bad_symlinks,
6523 int show_diff, int commit_conflicts,
6524 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6525 got_worktree_status_cb status_cb, void *status_arg,
6526 struct got_repository *repo)
6528 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6529 struct got_fileindex *fileindex = NULL;
6530 char *fileindex_path = NULL;
6531 struct got_pathlist_head commitable_paths;
6532 struct collect_commitables_arg cc_arg;
6533 struct got_pathlist_entry *pe;
6534 struct got_reference *head_ref = NULL;
6535 struct got_object_id *head_commit_id = NULL;
6536 char *diff_path = NULL;
6537 int have_staged_files = 0;
6539 *new_commit_id = NULL;
6541 memset(&cc_arg, 0, sizeof(cc_arg));
6542 TAILQ_INIT(&commitable_paths);
6544 err = lock_worktree(worktree, LOCK_EX);
6545 if (err)
6546 goto done;
6548 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6549 if (err)
6550 goto done;
6552 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6553 if (err)
6554 goto done;
6556 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6557 if (err)
6558 goto done;
6560 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6561 &have_staged_files);
6562 if (err && err->code != GOT_ERR_CANCELLED)
6563 goto done;
6564 if (have_staged_files) {
6565 err = check_non_staged_files(fileindex, paths);
6566 if (err)
6567 goto done;
6570 cc_arg.commitable_paths = &commitable_paths;
6571 cc_arg.worktree = worktree;
6572 cc_arg.fileindex = fileindex;
6573 cc_arg.repo = repo;
6574 cc_arg.have_staged_files = have_staged_files;
6575 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6576 cc_arg.diff_header_shown = 0;
6577 cc_arg.commit_conflicts = commit_conflicts;
6578 if (show_diff) {
6579 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6580 GOT_TMPDIR_STR "/got", ".diff");
6581 if (err)
6582 goto done;
6583 cc_arg.f1 = got_opentemp();
6584 if (cc_arg.f1 == NULL) {
6585 err = got_error_from_errno("got_opentemp");
6586 goto done;
6588 cc_arg.f2 = got_opentemp();
6589 if (cc_arg.f2 == NULL) {
6590 err = got_error_from_errno("got_opentemp");
6591 goto done;
6595 TAILQ_FOREACH(pe, paths, entry) {
6596 err = worktree_status(worktree, pe->path, fileindex, repo,
6597 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6598 if (err)
6599 goto done;
6602 if (show_diff) {
6603 if (fflush(cc_arg.diff_outfile) == EOF) {
6604 err = got_error_from_errno("fflush");
6605 goto done;
6609 if (TAILQ_EMPTY(&commitable_paths)) {
6610 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6611 goto done;
6614 TAILQ_FOREACH(pe, paths, entry) {
6615 err = check_path_is_commitable(pe->path, &commitable_paths);
6616 if (err)
6617 goto done;
6620 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6621 struct got_commitable *ct = pe->data;
6622 const char *ct_path = ct->in_repo_path;
6624 while (ct_path[0] == '/')
6625 ct_path++;
6626 err = check_out_of_date(ct_path, ct->status,
6627 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6628 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6629 if (err)
6630 goto done;
6634 err = commit_worktree(new_commit_id, &commitable_paths,
6635 head_commit_id, NULL, worktree, author, committer,
6636 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6637 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6638 if (err)
6639 goto done;
6641 err = update_fileindex_after_commit(worktree, &commitable_paths,
6642 *new_commit_id, fileindex, have_staged_files);
6643 sync_err = sync_fileindex(fileindex, fileindex_path);
6644 if (sync_err && err == NULL)
6645 err = sync_err;
6646 done:
6647 if (fileindex)
6648 got_fileindex_free(fileindex);
6649 free(fileindex_path);
6650 unlockerr = lock_worktree(worktree, LOCK_SH);
6651 if (unlockerr && err == NULL)
6652 err = unlockerr;
6653 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6654 struct got_commitable *ct = pe->data;
6656 free_commitable(ct);
6658 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6659 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6660 err = got_error_from_errno2("unlink", diff_path);
6661 free(diff_path);
6662 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6663 err == NULL)
6664 err = got_error_from_errno("fclose");
6665 return err;
6668 const char *
6669 got_commitable_get_path(struct got_commitable *ct)
6671 return ct->path;
6674 unsigned int
6675 got_commitable_get_status(struct got_commitable *ct)
6677 return ct->status;
6680 struct check_rebase_ok_arg {
6681 struct got_worktree *worktree;
6682 struct got_repository *repo;
6685 static const struct got_error *
6686 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6688 const struct got_error *err = NULL;
6689 struct check_rebase_ok_arg *a = arg;
6690 unsigned char status;
6691 struct stat sb;
6692 char *ondisk_path;
6694 /* Reject rebase of a work tree with mixed base commits. */
6695 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6696 SHA1_DIGEST_LENGTH))
6697 return got_error(GOT_ERR_MIXED_COMMITS);
6699 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6700 == -1)
6701 return got_error_from_errno("asprintf");
6703 /* Reject rebase of a work tree with modified or staged files. */
6704 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6705 free(ondisk_path);
6706 if (err)
6707 return err;
6709 if (status != GOT_STATUS_NO_CHANGE)
6710 return got_error(GOT_ERR_MODIFIED);
6711 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6712 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6714 return NULL;
6717 const struct got_error *
6718 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6719 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6720 struct got_worktree *worktree, struct got_reference *branch,
6721 struct got_repository *repo)
6723 const struct got_error *err = NULL;
6724 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6725 char *branch_ref_name = NULL;
6726 char *fileindex_path = NULL;
6727 struct check_rebase_ok_arg ok_arg;
6728 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6729 struct got_object_id *wt_branch_tip = NULL;
6731 *new_base_branch_ref = NULL;
6732 *tmp_branch = NULL;
6733 *fileindex = NULL;
6735 err = lock_worktree(worktree, LOCK_EX);
6736 if (err)
6737 return err;
6739 err = open_fileindex(fileindex, &fileindex_path, worktree);
6740 if (err)
6741 goto done;
6743 ok_arg.worktree = worktree;
6744 ok_arg.repo = repo;
6745 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6746 &ok_arg);
6747 if (err)
6748 goto done;
6750 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6751 if (err)
6752 goto done;
6754 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6755 if (err)
6756 goto done;
6758 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6759 if (err)
6760 goto done;
6762 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6763 0);
6764 if (err)
6765 goto done;
6767 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6768 if (err)
6769 goto done;
6770 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6771 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6772 goto done;
6775 err = got_ref_alloc_symref(new_base_branch_ref,
6776 new_base_branch_ref_name, wt_branch);
6777 if (err)
6778 goto done;
6779 err = got_ref_write(*new_base_branch_ref, repo);
6780 if (err)
6781 goto done;
6783 /* TODO Lock original branch's ref while rebasing? */
6785 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6786 if (err)
6787 goto done;
6789 err = got_ref_write(branch_ref, repo);
6790 if (err)
6791 goto done;
6793 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6794 worktree->base_commit_id);
6795 if (err)
6796 goto done;
6797 err = got_ref_write(*tmp_branch, repo);
6798 if (err)
6799 goto done;
6801 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6802 if (err)
6803 goto done;
6804 done:
6805 free(fileindex_path);
6806 free(tmp_branch_name);
6807 free(new_base_branch_ref_name);
6808 free(branch_ref_name);
6809 if (branch_ref)
6810 got_ref_close(branch_ref);
6811 if (wt_branch)
6812 got_ref_close(wt_branch);
6813 free(wt_branch_tip);
6814 if (err) {
6815 if (*new_base_branch_ref) {
6816 got_ref_close(*new_base_branch_ref);
6817 *new_base_branch_ref = NULL;
6819 if (*tmp_branch) {
6820 got_ref_close(*tmp_branch);
6821 *tmp_branch = NULL;
6823 if (*fileindex) {
6824 got_fileindex_free(*fileindex);
6825 *fileindex = NULL;
6827 lock_worktree(worktree, LOCK_SH);
6829 return err;
6832 const struct got_error *
6833 got_worktree_rebase_continue(struct got_object_id **commit_id,
6834 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6835 struct got_reference **branch, struct got_fileindex **fileindex,
6836 struct got_worktree *worktree, struct got_repository *repo)
6838 const struct got_error *err;
6839 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6840 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6841 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6842 char *fileindex_path = NULL;
6843 int have_staged_files = 0;
6845 *commit_id = NULL;
6846 *new_base_branch = NULL;
6847 *tmp_branch = NULL;
6848 *branch = NULL;
6849 *fileindex = NULL;
6851 err = lock_worktree(worktree, LOCK_EX);
6852 if (err)
6853 return err;
6855 err = open_fileindex(fileindex, &fileindex_path, worktree);
6856 if (err)
6857 goto done;
6859 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6860 &have_staged_files);
6861 if (err && err->code != GOT_ERR_CANCELLED)
6862 goto done;
6863 if (have_staged_files) {
6864 err = got_error(GOT_ERR_STAGED_PATHS);
6865 goto done;
6868 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6869 if (err)
6870 goto done;
6872 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6873 if (err)
6874 goto done;
6876 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6877 if (err)
6878 goto done;
6880 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6881 if (err)
6882 goto done;
6884 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6885 if (err)
6886 goto done;
6888 err = got_ref_open(branch, repo,
6889 got_ref_get_symref_target(branch_ref), 0);
6890 if (err)
6891 goto done;
6893 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6894 if (err)
6895 goto done;
6897 err = got_ref_resolve(commit_id, repo, commit_ref);
6898 if (err)
6899 goto done;
6901 err = got_ref_open(new_base_branch, repo,
6902 new_base_branch_ref_name, 0);
6903 if (err)
6904 goto done;
6906 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6907 if (err)
6908 goto done;
6909 done:
6910 free(commit_ref_name);
6911 free(branch_ref_name);
6912 free(fileindex_path);
6913 if (commit_ref)
6914 got_ref_close(commit_ref);
6915 if (branch_ref)
6916 got_ref_close(branch_ref);
6917 if (err) {
6918 free(*commit_id);
6919 *commit_id = NULL;
6920 if (*tmp_branch) {
6921 got_ref_close(*tmp_branch);
6922 *tmp_branch = NULL;
6924 if (*new_base_branch) {
6925 got_ref_close(*new_base_branch);
6926 *new_base_branch = NULL;
6928 if (*branch) {
6929 got_ref_close(*branch);
6930 *branch = NULL;
6932 if (*fileindex) {
6933 got_fileindex_free(*fileindex);
6934 *fileindex = NULL;
6936 lock_worktree(worktree, LOCK_SH);
6938 return err;
6941 const struct got_error *
6942 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6944 const struct got_error *err;
6945 char *tmp_branch_name = NULL;
6947 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6948 if (err)
6949 return err;
6951 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6952 free(tmp_branch_name);
6953 return NULL;
6956 static const struct got_error *
6957 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6958 const char *diff_path, char **logmsg, void *arg)
6960 *logmsg = arg;
6961 return NULL;
6964 static const struct got_error *
6965 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6966 const char *path, struct got_object_id *blob_id,
6967 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6968 int dirfd, const char *de_name)
6970 return NULL;
6973 struct collect_merged_paths_arg {
6974 got_worktree_checkout_cb progress_cb;
6975 void *progress_arg;
6976 struct got_pathlist_head *merged_paths;
6979 static const struct got_error *
6980 collect_merged_paths(void *arg, unsigned char status, const char *path)
6982 const struct got_error *err;
6983 struct collect_merged_paths_arg *a = arg;
6984 char *p;
6985 struct got_pathlist_entry *new;
6987 err = (*a->progress_cb)(a->progress_arg, status, path);
6988 if (err)
6989 return err;
6991 if (status != GOT_STATUS_MERGE &&
6992 status != GOT_STATUS_ADD &&
6993 status != GOT_STATUS_DELETE &&
6994 status != GOT_STATUS_CONFLICT)
6995 return NULL;
6997 p = strdup(path);
6998 if (p == NULL)
6999 return got_error_from_errno("strdup");
7001 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7002 if (err || new == NULL)
7003 free(p);
7004 return err;
7007 static const struct got_error *
7008 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7009 int is_rebase, struct got_repository *repo)
7011 const struct got_error *err;
7012 struct got_reference *commit_ref = NULL;
7014 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7015 if (err) {
7016 if (err->code != GOT_ERR_NOT_REF)
7017 goto done;
7018 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7019 if (err)
7020 goto done;
7021 err = got_ref_write(commit_ref, repo);
7022 if (err)
7023 goto done;
7024 } else if (is_rebase) {
7025 struct got_object_id *stored_id;
7026 int cmp;
7028 err = got_ref_resolve(&stored_id, repo, commit_ref);
7029 if (err)
7030 goto done;
7031 cmp = got_object_id_cmp(commit_id, stored_id);
7032 free(stored_id);
7033 if (cmp != 0) {
7034 err = got_error(GOT_ERR_REBASE_COMMITID);
7035 goto done;
7038 done:
7039 if (commit_ref)
7040 got_ref_close(commit_ref);
7041 return err;
7044 static const struct got_error *
7045 rebase_merge_files(struct got_pathlist_head *merged_paths,
7046 const char *commit_ref_name, struct got_worktree *worktree,
7047 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7048 struct got_object_id *commit_id, struct got_repository *repo,
7049 got_worktree_checkout_cb progress_cb, void *progress_arg,
7050 got_cancel_cb cancel_cb, void *cancel_arg)
7052 const struct got_error *err;
7053 struct got_reference *commit_ref = NULL;
7054 struct collect_merged_paths_arg cmp_arg;
7055 char *fileindex_path;
7057 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7059 err = get_fileindex_path(&fileindex_path, worktree);
7060 if (err)
7061 return err;
7063 cmp_arg.progress_cb = progress_cb;
7064 cmp_arg.progress_arg = progress_arg;
7065 cmp_arg.merged_paths = merged_paths;
7066 err = merge_files(worktree, fileindex, fileindex_path,
7067 parent_commit_id, commit_id, repo, collect_merged_paths,
7068 &cmp_arg, cancel_cb, cancel_arg);
7069 if (commit_ref)
7070 got_ref_close(commit_ref);
7071 return err;
7074 const struct got_error *
7075 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7076 struct got_worktree *worktree, struct got_fileindex *fileindex,
7077 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7078 struct got_repository *repo,
7079 got_worktree_checkout_cb progress_cb, void *progress_arg,
7080 got_cancel_cb cancel_cb, void *cancel_arg)
7082 const struct got_error *err;
7083 char *commit_ref_name;
7085 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7086 if (err)
7087 return err;
7089 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7090 if (err)
7091 goto done;
7093 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7094 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7095 progress_arg, cancel_cb, cancel_arg);
7096 done:
7097 free(commit_ref_name);
7098 return err;
7101 const struct got_error *
7102 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7103 struct got_worktree *worktree, struct got_fileindex *fileindex,
7104 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7105 struct got_repository *repo,
7106 got_worktree_checkout_cb progress_cb, void *progress_arg,
7107 got_cancel_cb cancel_cb, void *cancel_arg)
7109 const struct got_error *err;
7110 char *commit_ref_name;
7112 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7113 if (err)
7114 return err;
7116 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7117 if (err)
7118 goto done;
7120 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7121 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7122 progress_arg, cancel_cb, cancel_arg);
7123 done:
7124 free(commit_ref_name);
7125 return err;
7128 static const struct got_error *
7129 rebase_commit(struct got_object_id **new_commit_id,
7130 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7131 struct got_worktree *worktree, struct got_fileindex *fileindex,
7132 struct got_reference *tmp_branch, const char *committer,
7133 struct got_commit_object *orig_commit, const char *new_logmsg,
7134 int allow_conflict, struct got_repository *repo)
7136 const struct got_error *err, *sync_err;
7137 struct got_pathlist_head commitable_paths;
7138 struct collect_commitables_arg cc_arg;
7139 char *fileindex_path = NULL;
7140 struct got_reference *head_ref = NULL;
7141 struct got_object_id *head_commit_id = NULL;
7142 char *logmsg = NULL;
7144 memset(&cc_arg, 0, sizeof(cc_arg));
7145 TAILQ_INIT(&commitable_paths);
7146 *new_commit_id = NULL;
7148 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7150 err = get_fileindex_path(&fileindex_path, worktree);
7151 if (err)
7152 return err;
7154 cc_arg.commitable_paths = &commitable_paths;
7155 cc_arg.worktree = worktree;
7156 cc_arg.repo = repo;
7157 cc_arg.have_staged_files = 0;
7158 cc_arg.commit_conflicts = allow_conflict;
7160 * If possible get the status of individual files directly to
7161 * avoid crawling the entire work tree once per rebased commit.
7163 * Ideally, merged_paths would contain a list of commitables
7164 * we could use so we could skip worktree_status() entirely.
7165 * However, we would then need carefully keep track of cumulative
7166 * effects of operations such as file additions and deletions
7167 * in 'got histedit -f' (folding multiple commits into one),
7168 * and this extra complexity is not really worth it.
7170 if (merged_paths) {
7171 struct got_pathlist_entry *pe;
7172 TAILQ_FOREACH(pe, merged_paths, entry) {
7173 err = worktree_status(worktree, pe->path, fileindex,
7174 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7175 0);
7176 if (err)
7177 goto done;
7179 } else {
7180 err = worktree_status(worktree, "", fileindex, repo,
7181 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7182 if (err)
7183 goto done;
7186 if (TAILQ_EMPTY(&commitable_paths)) {
7187 /* No-op change; commit will be elided. */
7188 err = got_ref_delete(commit_ref, repo);
7189 if (err)
7190 goto done;
7191 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7192 goto done;
7195 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7196 if (err)
7197 goto done;
7199 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7200 if (err)
7201 goto done;
7203 if (new_logmsg) {
7204 logmsg = strdup(new_logmsg);
7205 if (logmsg == NULL) {
7206 err = got_error_from_errno("strdup");
7207 goto done;
7209 } else {
7210 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7211 if (err)
7212 goto done;
7215 /* NB: commit_worktree will call free(logmsg) */
7216 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7217 NULL, worktree, got_object_commit_get_author(orig_commit),
7218 committer ? committer :
7219 got_object_commit_get_committer(orig_commit), NULL,
7220 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7221 if (err)
7222 goto done;
7224 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7225 if (err)
7226 goto done;
7228 err = got_ref_delete(commit_ref, repo);
7229 if (err)
7230 goto done;
7232 err = update_fileindex_after_commit(worktree, &commitable_paths,
7233 *new_commit_id, fileindex, 0);
7234 sync_err = sync_fileindex(fileindex, fileindex_path);
7235 if (sync_err && err == NULL)
7236 err = sync_err;
7237 done:
7238 free(fileindex_path);
7239 free(head_commit_id);
7240 if (head_ref)
7241 got_ref_close(head_ref);
7242 if (err) {
7243 free(*new_commit_id);
7244 *new_commit_id = NULL;
7246 return err;
7249 const struct got_error *
7250 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7251 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7252 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7253 const char *committer, struct got_commit_object *orig_commit,
7254 struct got_object_id *orig_commit_id, int allow_conflict,
7255 struct got_repository *repo)
7257 const struct got_error *err;
7258 char *commit_ref_name;
7259 struct got_reference *commit_ref = NULL;
7260 struct got_object_id *commit_id = NULL;
7262 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7263 if (err)
7264 return err;
7266 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7267 if (err)
7268 goto done;
7269 err = got_ref_resolve(&commit_id, repo, commit_ref);
7270 if (err)
7271 goto done;
7272 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7273 err = got_error(GOT_ERR_REBASE_COMMITID);
7274 goto done;
7277 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7278 worktree, fileindex, tmp_branch, committer, orig_commit,
7279 NULL, allow_conflict, repo);
7280 done:
7281 if (commit_ref)
7282 got_ref_close(commit_ref);
7283 free(commit_ref_name);
7284 free(commit_id);
7285 return err;
7288 const struct got_error *
7289 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7290 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7291 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7292 const char *committer, struct got_commit_object *orig_commit,
7293 struct got_object_id *orig_commit_id, const char *new_logmsg,
7294 int allow_conflict, struct got_repository *repo)
7296 const struct got_error *err;
7297 char *commit_ref_name;
7298 struct got_reference *commit_ref = NULL;
7300 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7301 if (err)
7302 return err;
7304 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7305 if (err)
7306 goto done;
7308 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7309 worktree, fileindex, tmp_branch, committer, orig_commit,
7310 new_logmsg, allow_conflict, repo);
7311 done:
7312 if (commit_ref)
7313 got_ref_close(commit_ref);
7314 free(commit_ref_name);
7315 return err;
7318 const struct got_error *
7319 got_worktree_rebase_postpone(struct got_worktree *worktree,
7320 struct got_fileindex *fileindex)
7322 if (fileindex)
7323 got_fileindex_free(fileindex);
7324 return lock_worktree(worktree, LOCK_SH);
7327 static const struct got_error *
7328 delete_ref(const char *name, struct got_repository *repo)
7330 const struct got_error *err;
7331 struct got_reference *ref;
7333 err = got_ref_open(&ref, repo, name, 0);
7334 if (err) {
7335 if (err->code == GOT_ERR_NOT_REF)
7336 return NULL;
7337 return err;
7340 err = got_ref_delete(ref, repo);
7341 got_ref_close(ref);
7342 return err;
7345 static const struct got_error *
7346 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7348 const struct got_error *err;
7349 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7350 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7352 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7353 if (err)
7354 goto done;
7355 err = delete_ref(tmp_branch_name, repo);
7356 if (err)
7357 goto done;
7359 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7360 if (err)
7361 goto done;
7362 err = delete_ref(new_base_branch_ref_name, repo);
7363 if (err)
7364 goto done;
7366 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7367 if (err)
7368 goto done;
7369 err = delete_ref(branch_ref_name, repo);
7370 if (err)
7371 goto done;
7373 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7374 if (err)
7375 goto done;
7376 err = delete_ref(commit_ref_name, repo);
7377 if (err)
7378 goto done;
7380 done:
7381 free(tmp_branch_name);
7382 free(new_base_branch_ref_name);
7383 free(branch_ref_name);
7384 free(commit_ref_name);
7385 return err;
7388 static const struct got_error *
7389 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7390 struct got_object_id *new_commit_id, struct got_repository *repo)
7392 const struct got_error *err;
7393 struct got_reference *ref = NULL;
7394 struct got_object_id *old_commit_id = NULL;
7395 const char *branch_name = NULL;
7396 char *new_id_str = NULL;
7397 char *refname = NULL;
7399 branch_name = got_ref_get_name(branch);
7400 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7401 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7402 branch_name += 11;
7404 err = got_object_id_str(&new_id_str, new_commit_id);
7405 if (err)
7406 return err;
7408 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7409 new_id_str) == -1) {
7410 err = got_error_from_errno("asprintf");
7411 goto done;
7414 err = got_ref_resolve(&old_commit_id, repo, branch);
7415 if (err)
7416 goto done;
7418 err = got_ref_alloc(&ref, refname, old_commit_id);
7419 if (err)
7420 goto done;
7422 err = got_ref_write(ref, repo);
7423 done:
7424 free(new_id_str);
7425 free(refname);
7426 free(old_commit_id);
7427 if (ref)
7428 got_ref_close(ref);
7429 return err;
7432 const struct got_error *
7433 got_worktree_rebase_complete(struct got_worktree *worktree,
7434 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7435 struct got_reference *rebased_branch, struct got_repository *repo,
7436 int create_backup)
7438 const struct got_error *err, *unlockerr, *sync_err;
7439 struct got_object_id *new_head_commit_id = NULL;
7440 char *fileindex_path = NULL;
7442 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7443 if (err)
7444 return err;
7446 if (create_backup) {
7447 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7448 rebased_branch, new_head_commit_id, repo);
7449 if (err)
7450 goto done;
7453 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7454 if (err)
7455 goto done;
7457 err = got_ref_write(rebased_branch, repo);
7458 if (err)
7459 goto done;
7461 err = got_worktree_set_head_ref(worktree, rebased_branch);
7462 if (err)
7463 goto done;
7465 err = delete_rebase_refs(worktree, repo);
7466 if (err)
7467 goto done;
7469 err = get_fileindex_path(&fileindex_path, worktree);
7470 if (err)
7471 goto done;
7472 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7473 sync_err = sync_fileindex(fileindex, fileindex_path);
7474 if (sync_err && err == NULL)
7475 err = sync_err;
7476 done:
7477 got_fileindex_free(fileindex);
7478 free(fileindex_path);
7479 free(new_head_commit_id);
7480 unlockerr = lock_worktree(worktree, LOCK_SH);
7481 if (unlockerr && err == NULL)
7482 err = unlockerr;
7483 return err;
7486 static const struct got_error *
7487 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7488 struct got_object_id *id1, struct got_object_id *id2,
7489 struct got_repository *repo)
7491 const struct got_error *err;
7492 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7493 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7495 if (id1) {
7496 err = got_object_open_as_commit(&commit1, repo, id1);
7497 if (err)
7498 goto done;
7500 err = got_object_open_as_tree(&tree1, repo,
7501 got_object_commit_get_tree_id(commit1));
7502 if (err)
7503 goto done;
7506 if (id2) {
7507 err = got_object_open_as_commit(&commit2, repo, id2);
7508 if (err)
7509 goto done;
7511 err = got_object_open_as_tree(&tree2, repo,
7512 got_object_commit_get_tree_id(commit2));
7513 if (err)
7514 goto done;
7517 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7518 got_diff_tree_collect_changed_paths, paths, 0);
7519 if (err)
7520 goto done;
7521 done:
7522 if (commit1)
7523 got_object_commit_close(commit1);
7524 if (commit2)
7525 got_object_commit_close(commit2);
7526 if (tree1)
7527 got_object_tree_close(tree1);
7528 if (tree2)
7529 got_object_tree_close(tree2);
7530 return err;
7533 static const struct got_error *
7534 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7535 struct got_object_id *id1, struct got_object_id *id2,
7536 const char *path_prefix, struct got_repository *repo)
7538 const struct got_error *err;
7539 struct got_pathlist_head merged_paths;
7540 struct got_pathlist_entry *pe;
7541 char *abspath = NULL, *wt_path = NULL;
7543 TAILQ_INIT(&merged_paths);
7545 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7546 if (err)
7547 goto done;
7549 TAILQ_FOREACH(pe, &merged_paths, entry) {
7550 struct got_diff_changed_path *change = pe->data;
7552 if (change->status != GOT_STATUS_ADD)
7553 continue;
7555 if (got_path_is_root_dir(path_prefix)) {
7556 wt_path = strdup(pe->path);
7557 if (wt_path == NULL) {
7558 err = got_error_from_errno("strdup");
7559 goto done;
7561 } else {
7562 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7563 err = got_error_from_errno("asprintf");
7564 goto done;
7567 err = got_path_skip_common_ancestor(&wt_path,
7568 path_prefix, abspath);
7569 if (err)
7570 goto done;
7571 free(abspath);
7572 abspath = NULL;
7575 err = got_pathlist_append(added_paths, wt_path, NULL);
7576 if (err)
7577 goto done;
7578 wt_path = NULL;
7581 done:
7582 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7583 free(abspath);
7584 free(wt_path);
7585 return err;
7588 static const struct got_error *
7589 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7590 struct got_object_id *id, const char *path_prefix,
7591 struct got_repository *repo)
7593 const struct got_error *err;
7594 struct got_commit_object *commit = NULL;
7595 struct got_object_qid *pid;
7597 err = got_object_open_as_commit(&commit, repo, id);
7598 if (err)
7599 goto done;
7601 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7603 err = get_paths_added_between_commits(added_paths,
7604 pid ? &pid->id : NULL, id, path_prefix, repo);
7605 if (err)
7606 goto done;
7607 done:
7608 if (commit)
7609 got_object_commit_close(commit);
7610 return err;
7613 const struct got_error *
7614 got_worktree_rebase_abort(struct got_worktree *worktree,
7615 struct got_fileindex *fileindex, struct got_repository *repo,
7616 struct got_reference *new_base_branch,
7617 got_worktree_checkout_cb progress_cb, void *progress_arg)
7619 const struct got_error *err, *unlockerr, *sync_err;
7620 struct got_reference *resolved = NULL;
7621 struct got_object_id *commit_id = NULL;
7622 struct got_object_id *merged_commit_id = NULL;
7623 struct got_commit_object *commit = NULL;
7624 char *fileindex_path = NULL;
7625 char *commit_ref_name = NULL;
7626 struct got_reference *commit_ref = NULL;
7627 struct revert_file_args rfa;
7628 struct got_object_id *tree_id = NULL;
7629 struct got_pathlist_head added_paths;
7631 TAILQ_INIT(&added_paths);
7633 err = lock_worktree(worktree, LOCK_EX);
7634 if (err)
7635 return err;
7637 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7638 if (err)
7639 goto done;
7641 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7642 if (err)
7643 goto done;
7645 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7646 if (err)
7647 goto done;
7650 * Determine which files in added status can be safely removed
7651 * from disk while reverting changes in the work tree.
7652 * We want to avoid deleting unrelated files which were added by
7653 * the user for conflict resolution purposes.
7655 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7656 got_worktree_get_path_prefix(worktree), repo);
7657 if (err)
7658 goto done;
7660 err = got_ref_open(&resolved, repo,
7661 got_ref_get_symref_target(new_base_branch), 0);
7662 if (err)
7663 goto done;
7665 err = got_worktree_set_head_ref(worktree, resolved);
7666 if (err)
7667 goto done;
7670 * XXX commits to the base branch could have happened while
7671 * we were busy rebasing; should we store the original commit ID
7672 * when rebase begins and read it back here?
7674 err = got_ref_resolve(&commit_id, repo, resolved);
7675 if (err)
7676 goto done;
7678 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7679 if (err)
7680 goto done;
7682 err = got_object_open_as_commit(&commit, repo,
7683 worktree->base_commit_id);
7684 if (err)
7685 goto done;
7687 err = got_object_id_by_path(&tree_id, repo, commit,
7688 worktree->path_prefix);
7689 if (err)
7690 goto done;
7692 err = delete_rebase_refs(worktree, repo);
7693 if (err)
7694 goto done;
7696 err = get_fileindex_path(&fileindex_path, worktree);
7697 if (err)
7698 goto done;
7700 rfa.worktree = worktree;
7701 rfa.fileindex = fileindex;
7702 rfa.progress_cb = progress_cb;
7703 rfa.progress_arg = progress_arg;
7704 rfa.patch_cb = NULL;
7705 rfa.patch_arg = NULL;
7706 rfa.repo = repo;
7707 rfa.unlink_added_files = 1;
7708 rfa.added_files_to_unlink = &added_paths;
7709 err = worktree_status(worktree, "", fileindex, repo,
7710 revert_file, &rfa, NULL, NULL, 1, 0);
7711 if (err)
7712 goto sync;
7714 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7715 repo, progress_cb, progress_arg, NULL, NULL);
7716 sync:
7717 sync_err = sync_fileindex(fileindex, fileindex_path);
7718 if (sync_err && err == NULL)
7719 err = sync_err;
7720 done:
7721 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7722 got_ref_close(resolved);
7723 free(tree_id);
7724 free(commit_id);
7725 free(merged_commit_id);
7726 if (commit)
7727 got_object_commit_close(commit);
7728 if (fileindex)
7729 got_fileindex_free(fileindex);
7730 free(fileindex_path);
7731 free(commit_ref_name);
7732 if (commit_ref)
7733 got_ref_close(commit_ref);
7735 unlockerr = lock_worktree(worktree, LOCK_SH);
7736 if (unlockerr && err == NULL)
7737 err = unlockerr;
7738 return err;
7741 const struct got_error *
7742 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7743 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7744 struct got_fileindex **fileindex, struct got_worktree *worktree,
7745 struct got_repository *repo)
7747 const struct got_error *err = NULL;
7748 char *tmp_branch_name = NULL;
7749 char *branch_ref_name = NULL;
7750 char *base_commit_ref_name = NULL;
7751 char *fileindex_path = NULL;
7752 struct check_rebase_ok_arg ok_arg;
7753 struct got_reference *wt_branch = NULL;
7754 struct got_reference *base_commit_ref = NULL;
7756 *tmp_branch = NULL;
7757 *branch_ref = NULL;
7758 *base_commit_id = NULL;
7759 *fileindex = NULL;
7761 err = lock_worktree(worktree, LOCK_EX);
7762 if (err)
7763 return err;
7765 err = open_fileindex(fileindex, &fileindex_path, worktree);
7766 if (err)
7767 goto done;
7769 ok_arg.worktree = worktree;
7770 ok_arg.repo = repo;
7771 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7772 &ok_arg);
7773 if (err)
7774 goto done;
7776 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7777 if (err)
7778 goto done;
7780 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7781 if (err)
7782 goto done;
7784 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7785 worktree);
7786 if (err)
7787 goto done;
7789 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7790 0);
7791 if (err)
7792 goto done;
7794 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7795 if (err)
7796 goto done;
7798 err = got_ref_write(*branch_ref, repo);
7799 if (err)
7800 goto done;
7802 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7803 worktree->base_commit_id);
7804 if (err)
7805 goto done;
7806 err = got_ref_write(base_commit_ref, repo);
7807 if (err)
7808 goto done;
7809 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7810 if (*base_commit_id == NULL) {
7811 err = got_error_from_errno("got_object_id_dup");
7812 goto done;
7815 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7816 worktree->base_commit_id);
7817 if (err)
7818 goto done;
7819 err = got_ref_write(*tmp_branch, repo);
7820 if (err)
7821 goto done;
7823 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7824 if (err)
7825 goto done;
7826 done:
7827 free(fileindex_path);
7828 free(tmp_branch_name);
7829 free(branch_ref_name);
7830 free(base_commit_ref_name);
7831 if (wt_branch)
7832 got_ref_close(wt_branch);
7833 if (err) {
7834 if (*branch_ref) {
7835 got_ref_close(*branch_ref);
7836 *branch_ref = NULL;
7838 if (*tmp_branch) {
7839 got_ref_close(*tmp_branch);
7840 *tmp_branch = NULL;
7842 free(*base_commit_id);
7843 if (*fileindex) {
7844 got_fileindex_free(*fileindex);
7845 *fileindex = NULL;
7847 lock_worktree(worktree, LOCK_SH);
7849 return err;
7852 const struct got_error *
7853 got_worktree_histedit_postpone(struct got_worktree *worktree,
7854 struct got_fileindex *fileindex)
7856 if (fileindex)
7857 got_fileindex_free(fileindex);
7858 return lock_worktree(worktree, LOCK_SH);
7861 const struct got_error *
7862 got_worktree_histedit_in_progress(int *in_progress,
7863 struct got_worktree *worktree)
7865 const struct got_error *err;
7866 char *tmp_branch_name = NULL;
7868 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7869 if (err)
7870 return err;
7872 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7873 free(tmp_branch_name);
7874 return NULL;
7877 const struct got_error *
7878 got_worktree_histedit_continue(struct got_object_id **commit_id,
7879 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7880 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7881 struct got_worktree *worktree, struct got_repository *repo)
7883 const struct got_error *err;
7884 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7885 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7886 struct got_reference *commit_ref = NULL;
7887 struct got_reference *base_commit_ref = NULL;
7888 char *fileindex_path = NULL;
7889 int have_staged_files = 0;
7891 *commit_id = NULL;
7892 *tmp_branch = NULL;
7893 *base_commit_id = NULL;
7894 *fileindex = NULL;
7896 err = lock_worktree(worktree, LOCK_EX);
7897 if (err)
7898 return err;
7900 err = open_fileindex(fileindex, &fileindex_path, worktree);
7901 if (err)
7902 goto done;
7904 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7905 &have_staged_files);
7906 if (err && err->code != GOT_ERR_CANCELLED)
7907 goto done;
7908 if (have_staged_files) {
7909 err = got_error(GOT_ERR_STAGED_PATHS);
7910 goto done;
7913 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7914 if (err)
7915 goto done;
7917 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7918 if (err)
7919 goto done;
7921 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7922 if (err)
7923 goto done;
7925 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7926 worktree);
7927 if (err)
7928 goto done;
7930 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7931 if (err)
7932 goto done;
7934 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7935 if (err)
7936 goto done;
7937 err = got_ref_resolve(commit_id, repo, commit_ref);
7938 if (err)
7939 goto done;
7941 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7942 if (err)
7943 goto done;
7944 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7945 if (err)
7946 goto done;
7948 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7949 if (err)
7950 goto done;
7951 done:
7952 free(commit_ref_name);
7953 free(branch_ref_name);
7954 free(fileindex_path);
7955 if (commit_ref)
7956 got_ref_close(commit_ref);
7957 if (base_commit_ref)
7958 got_ref_close(base_commit_ref);
7959 if (err) {
7960 free(*commit_id);
7961 *commit_id = NULL;
7962 free(*base_commit_id);
7963 *base_commit_id = NULL;
7964 if (*tmp_branch) {
7965 got_ref_close(*tmp_branch);
7966 *tmp_branch = NULL;
7968 if (*fileindex) {
7969 got_fileindex_free(*fileindex);
7970 *fileindex = NULL;
7972 lock_worktree(worktree, LOCK_EX);
7974 return err;
7977 static const struct got_error *
7978 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7980 const struct got_error *err;
7981 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7982 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7984 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7985 if (err)
7986 goto done;
7987 err = delete_ref(tmp_branch_name, repo);
7988 if (err)
7989 goto done;
7991 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7992 worktree);
7993 if (err)
7994 goto done;
7995 err = delete_ref(base_commit_ref_name, repo);
7996 if (err)
7997 goto done;
7999 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8000 if (err)
8001 goto done;
8002 err = delete_ref(branch_ref_name, repo);
8003 if (err)
8004 goto done;
8006 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8007 if (err)
8008 goto done;
8009 err = delete_ref(commit_ref_name, repo);
8010 if (err)
8011 goto done;
8012 done:
8013 free(tmp_branch_name);
8014 free(base_commit_ref_name);
8015 free(branch_ref_name);
8016 free(commit_ref_name);
8017 return err;
8020 const struct got_error *
8021 got_worktree_histedit_abort(struct got_worktree *worktree,
8022 struct got_fileindex *fileindex, struct got_repository *repo,
8023 struct got_reference *branch, struct got_object_id *base_commit_id,
8024 got_worktree_checkout_cb progress_cb, void *progress_arg)
8026 const struct got_error *err, *unlockerr, *sync_err;
8027 struct got_reference *resolved = NULL;
8028 char *fileindex_path = NULL;
8029 struct got_object_id *merged_commit_id = NULL;
8030 struct got_commit_object *commit = NULL;
8031 char *commit_ref_name = NULL;
8032 struct got_reference *commit_ref = NULL;
8033 struct got_object_id *tree_id = NULL;
8034 struct revert_file_args rfa;
8035 struct got_pathlist_head added_paths;
8037 TAILQ_INIT(&added_paths);
8039 err = lock_worktree(worktree, LOCK_EX);
8040 if (err)
8041 return err;
8043 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8044 if (err)
8045 goto done;
8047 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8048 if (err) {
8049 if (err->code != GOT_ERR_NOT_REF)
8050 goto done;
8051 /* Can happen on early abort due to invalid histedit script. */
8052 commit_ref = NULL;
8055 if (commit_ref) {
8056 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8057 if (err)
8058 goto done;
8061 * Determine which files in added status can be safely removed
8062 * from disk while reverting changes in the work tree.
8063 * We want to avoid deleting unrelated files added by the
8064 * user during conflict resolution or during histedit -e.
8066 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8067 got_worktree_get_path_prefix(worktree), repo);
8068 if (err)
8069 goto done;
8072 err = got_ref_open(&resolved, repo,
8073 got_ref_get_symref_target(branch), 0);
8074 if (err)
8075 goto done;
8077 err = got_worktree_set_head_ref(worktree, resolved);
8078 if (err)
8079 goto done;
8081 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8082 if (err)
8083 goto done;
8085 err = got_object_open_as_commit(&commit, repo,
8086 worktree->base_commit_id);
8087 if (err)
8088 goto done;
8090 err = got_object_id_by_path(&tree_id, repo, commit,
8091 worktree->path_prefix);
8092 if (err)
8093 goto done;
8095 err = delete_histedit_refs(worktree, repo);
8096 if (err)
8097 goto done;
8099 err = get_fileindex_path(&fileindex_path, worktree);
8100 if (err)
8101 goto done;
8103 rfa.worktree = worktree;
8104 rfa.fileindex = fileindex;
8105 rfa.progress_cb = progress_cb;
8106 rfa.progress_arg = progress_arg;
8107 rfa.patch_cb = NULL;
8108 rfa.patch_arg = NULL;
8109 rfa.repo = repo;
8110 rfa.unlink_added_files = 1;
8111 rfa.added_files_to_unlink = &added_paths;
8112 err = worktree_status(worktree, "", fileindex, repo,
8113 revert_file, &rfa, NULL, NULL, 1, 0);
8114 if (err)
8115 goto sync;
8117 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8118 repo, progress_cb, progress_arg, NULL, NULL);
8119 sync:
8120 sync_err = sync_fileindex(fileindex, fileindex_path);
8121 if (sync_err && err == NULL)
8122 err = sync_err;
8123 done:
8124 if (resolved)
8125 got_ref_close(resolved);
8126 if (commit_ref)
8127 got_ref_close(commit_ref);
8128 free(merged_commit_id);
8129 free(tree_id);
8130 free(fileindex_path);
8131 free(commit_ref_name);
8133 unlockerr = lock_worktree(worktree, LOCK_SH);
8134 if (unlockerr && err == NULL)
8135 err = unlockerr;
8136 return err;
8139 const struct got_error *
8140 got_worktree_histedit_complete(struct got_worktree *worktree,
8141 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8142 struct got_reference *edited_branch, struct got_repository *repo)
8144 const struct got_error *err, *unlockerr, *sync_err;
8145 struct got_object_id *new_head_commit_id = NULL;
8146 struct got_reference *resolved = NULL;
8147 char *fileindex_path = NULL;
8149 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8150 if (err)
8151 return err;
8153 err = got_ref_open(&resolved, repo,
8154 got_ref_get_symref_target(edited_branch), 0);
8155 if (err)
8156 goto done;
8158 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8159 resolved, new_head_commit_id, repo);
8160 if (err)
8161 goto done;
8163 err = got_ref_change_ref(resolved, new_head_commit_id);
8164 if (err)
8165 goto done;
8167 err = got_ref_write(resolved, repo);
8168 if (err)
8169 goto done;
8171 err = got_worktree_set_head_ref(worktree, resolved);
8172 if (err)
8173 goto done;
8175 err = delete_histedit_refs(worktree, repo);
8176 if (err)
8177 goto done;
8179 err = get_fileindex_path(&fileindex_path, worktree);
8180 if (err)
8181 goto done;
8182 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8183 sync_err = sync_fileindex(fileindex, fileindex_path);
8184 if (sync_err && err == NULL)
8185 err = sync_err;
8186 done:
8187 got_fileindex_free(fileindex);
8188 free(fileindex_path);
8189 free(new_head_commit_id);
8190 unlockerr = lock_worktree(worktree, LOCK_SH);
8191 if (unlockerr && err == NULL)
8192 err = unlockerr;
8193 return err;
8196 const struct got_error *
8197 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8198 struct got_object_id *commit_id, struct got_repository *repo)
8200 const struct got_error *err;
8201 char *commit_ref_name;
8203 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8204 if (err)
8205 return err;
8207 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8208 if (err)
8209 goto done;
8211 err = delete_ref(commit_ref_name, repo);
8212 done:
8213 free(commit_ref_name);
8214 return err;
8217 const struct got_error *
8218 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8219 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8220 struct got_worktree *worktree, const char *refname,
8221 struct got_repository *repo)
8223 const struct got_error *err = NULL;
8224 char *fileindex_path = NULL;
8225 struct check_rebase_ok_arg ok_arg;
8227 *fileindex = NULL;
8228 *branch_ref = NULL;
8229 *base_branch_ref = NULL;
8231 err = lock_worktree(worktree, LOCK_EX);
8232 if (err)
8233 return err;
8235 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8236 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8237 "cannot integrate a branch into itself; "
8238 "update -b or different branch name required");
8239 goto done;
8242 err = open_fileindex(fileindex, &fileindex_path, worktree);
8243 if (err)
8244 goto done;
8246 /* Preconditions are the same as for rebase. */
8247 ok_arg.worktree = worktree;
8248 ok_arg.repo = repo;
8249 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8250 &ok_arg);
8251 if (err)
8252 goto done;
8254 err = got_ref_open(branch_ref, repo, refname, 1);
8255 if (err)
8256 goto done;
8258 err = got_ref_open(base_branch_ref, repo,
8259 got_worktree_get_head_ref_name(worktree), 1);
8260 done:
8261 if (err) {
8262 if (*branch_ref) {
8263 got_ref_close(*branch_ref);
8264 *branch_ref = NULL;
8266 if (*base_branch_ref) {
8267 got_ref_close(*base_branch_ref);
8268 *base_branch_ref = NULL;
8270 if (*fileindex) {
8271 got_fileindex_free(*fileindex);
8272 *fileindex = NULL;
8274 lock_worktree(worktree, LOCK_SH);
8276 return err;
8279 const struct got_error *
8280 got_worktree_integrate_continue(struct got_worktree *worktree,
8281 struct got_fileindex *fileindex, struct got_repository *repo,
8282 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8283 got_worktree_checkout_cb progress_cb, void *progress_arg,
8284 got_cancel_cb cancel_cb, void *cancel_arg)
8286 const struct got_error *err = NULL, *sync_err, *unlockerr;
8287 char *fileindex_path = NULL;
8288 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8289 struct got_commit_object *commit = NULL;
8291 err = get_fileindex_path(&fileindex_path, worktree);
8292 if (err)
8293 goto done;
8295 err = got_ref_resolve(&commit_id, repo, branch_ref);
8296 if (err)
8297 goto done;
8299 err = got_object_open_as_commit(&commit, repo, commit_id);
8300 if (err)
8301 goto done;
8303 err = got_object_id_by_path(&tree_id, repo, commit,
8304 worktree->path_prefix);
8305 if (err)
8306 goto done;
8308 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8309 if (err)
8310 goto done;
8312 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8313 progress_cb, progress_arg, cancel_cb, cancel_arg);
8314 if (err)
8315 goto sync;
8317 err = got_ref_change_ref(base_branch_ref, commit_id);
8318 if (err)
8319 goto sync;
8321 err = got_ref_write(base_branch_ref, repo);
8322 if (err)
8323 goto sync;
8325 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8326 sync:
8327 sync_err = sync_fileindex(fileindex, fileindex_path);
8328 if (sync_err && err == NULL)
8329 err = sync_err;
8331 done:
8332 unlockerr = got_ref_unlock(branch_ref);
8333 if (unlockerr && err == NULL)
8334 err = unlockerr;
8335 got_ref_close(branch_ref);
8337 unlockerr = got_ref_unlock(base_branch_ref);
8338 if (unlockerr && err == NULL)
8339 err = unlockerr;
8340 got_ref_close(base_branch_ref);
8342 got_fileindex_free(fileindex);
8343 free(fileindex_path);
8344 free(tree_id);
8345 if (commit)
8346 got_object_commit_close(commit);
8348 unlockerr = lock_worktree(worktree, LOCK_SH);
8349 if (unlockerr && err == NULL)
8350 err = unlockerr;
8351 return err;
8354 const struct got_error *
8355 got_worktree_integrate_abort(struct got_worktree *worktree,
8356 struct got_fileindex *fileindex, struct got_repository *repo,
8357 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8359 const struct got_error *err = NULL, *unlockerr = NULL;
8361 got_fileindex_free(fileindex);
8363 err = lock_worktree(worktree, LOCK_SH);
8365 unlockerr = got_ref_unlock(branch_ref);
8366 if (unlockerr && err == NULL)
8367 err = unlockerr;
8368 got_ref_close(branch_ref);
8370 unlockerr = got_ref_unlock(base_branch_ref);
8371 if (unlockerr && err == NULL)
8372 err = unlockerr;
8373 got_ref_close(base_branch_ref);
8375 return err;
8378 const struct got_error *
8379 got_worktree_merge_postpone(struct got_worktree *worktree,
8380 struct got_fileindex *fileindex)
8382 const struct got_error *err, *sync_err;
8383 char *fileindex_path = NULL;
8385 err = get_fileindex_path(&fileindex_path, worktree);
8386 if (err)
8387 goto done;
8389 sync_err = sync_fileindex(fileindex, fileindex_path);
8391 err = lock_worktree(worktree, LOCK_SH);
8392 if (sync_err && err == NULL)
8393 err = sync_err;
8394 done:
8395 got_fileindex_free(fileindex);
8396 free(fileindex_path);
8397 return err;
8400 static const struct got_error *
8401 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8403 const struct got_error *err;
8404 char *branch_refname = NULL, *commit_refname = NULL;
8406 err = get_merge_branch_ref_name(&branch_refname, worktree);
8407 if (err)
8408 goto done;
8409 err = delete_ref(branch_refname, repo);
8410 if (err)
8411 goto done;
8413 err = get_merge_commit_ref_name(&commit_refname, worktree);
8414 if (err)
8415 goto done;
8416 err = delete_ref(commit_refname, repo);
8417 if (err)
8418 goto done;
8420 done:
8421 free(branch_refname);
8422 free(commit_refname);
8423 return err;
8426 struct merge_commit_msg_arg {
8427 struct got_worktree *worktree;
8428 const char *branch_name;
8431 static const struct got_error *
8432 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8433 const char *diff_path, char **logmsg, void *arg)
8435 struct merge_commit_msg_arg *a = arg;
8437 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8438 got_worktree_get_head_ref_name(a->worktree)) == -1)
8439 return got_error_from_errno("asprintf");
8441 return NULL;
8445 const struct got_error *
8446 got_worktree_merge_branch(struct got_worktree *worktree,
8447 struct got_fileindex *fileindex,
8448 struct got_object_id *yca_commit_id,
8449 struct got_object_id *branch_tip,
8450 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8451 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8453 const struct got_error *err;
8454 char *fileindex_path = NULL;
8455 struct check_mixed_commits_args cma;
8457 err = get_fileindex_path(&fileindex_path, worktree);
8458 if (err)
8459 goto done;
8461 cma.worktree = worktree;
8462 cma.cancel_cb = cancel_cb;
8463 cma.cancel_arg = cancel_arg;
8465 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8466 &cma);
8467 if (err)
8468 goto done;
8470 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8471 branch_tip, repo, progress_cb, progress_arg,
8472 cancel_cb, cancel_arg);
8473 done:
8474 free(fileindex_path);
8475 return err;
8478 const struct got_error *
8479 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8480 struct got_worktree *worktree, struct got_fileindex *fileindex,
8481 const char *author, const char *committer, int allow_bad_symlinks,
8482 struct got_object_id *branch_tip, const char *branch_name,
8483 int allow_conflict, struct got_repository *repo,
8484 got_worktree_status_cb status_cb, void *status_arg)
8487 const struct got_error *err = NULL, *sync_err;
8488 struct got_pathlist_head commitable_paths;
8489 struct collect_commitables_arg cc_arg;
8490 struct got_pathlist_entry *pe;
8491 struct got_reference *head_ref = NULL;
8492 struct got_object_id *head_commit_id = NULL;
8493 int have_staged_files = 0;
8494 struct merge_commit_msg_arg mcm_arg;
8495 char *fileindex_path = NULL;
8497 memset(&cc_arg, 0, sizeof(cc_arg));
8498 *new_commit_id = NULL;
8500 TAILQ_INIT(&commitable_paths);
8502 err = get_fileindex_path(&fileindex_path, worktree);
8503 if (err)
8504 goto done;
8506 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8507 if (err)
8508 goto done;
8510 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8511 if (err)
8512 goto done;
8514 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8515 &have_staged_files);
8516 if (err && err->code != GOT_ERR_CANCELLED)
8517 goto done;
8518 if (have_staged_files) {
8519 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8520 goto done;
8523 cc_arg.commitable_paths = &commitable_paths;
8524 cc_arg.worktree = worktree;
8525 cc_arg.fileindex = fileindex;
8526 cc_arg.repo = repo;
8527 cc_arg.have_staged_files = have_staged_files;
8528 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8529 cc_arg.commit_conflicts = allow_conflict;
8530 err = worktree_status(worktree, "", fileindex, repo,
8531 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8532 if (err)
8533 goto done;
8535 mcm_arg.worktree = worktree;
8536 mcm_arg.branch_name = branch_name;
8537 err = commit_worktree(new_commit_id, &commitable_paths,
8538 head_commit_id, branch_tip, worktree, author, committer, NULL,
8539 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8540 if (err)
8541 goto done;
8543 err = update_fileindex_after_commit(worktree, &commitable_paths,
8544 *new_commit_id, fileindex, have_staged_files);
8545 sync_err = sync_fileindex(fileindex, fileindex_path);
8546 if (sync_err && err == NULL)
8547 err = sync_err;
8548 done:
8549 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8550 struct got_commitable *ct = pe->data;
8552 free_commitable(ct);
8554 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8555 free(fileindex_path);
8556 return err;
8559 const struct got_error *
8560 got_worktree_merge_complete(struct got_worktree *worktree,
8561 struct got_fileindex *fileindex, struct got_repository *repo)
8563 const struct got_error *err, *unlockerr, *sync_err;
8564 char *fileindex_path = NULL;
8566 err = delete_merge_refs(worktree, repo);
8567 if (err)
8568 goto done;
8570 err = get_fileindex_path(&fileindex_path, worktree);
8571 if (err)
8572 goto done;
8573 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8574 sync_err = sync_fileindex(fileindex, fileindex_path);
8575 if (sync_err && err == NULL)
8576 err = sync_err;
8577 done:
8578 got_fileindex_free(fileindex);
8579 free(fileindex_path);
8580 unlockerr = lock_worktree(worktree, LOCK_SH);
8581 if (unlockerr && err == NULL)
8582 err = unlockerr;
8583 return err;
8586 const struct got_error *
8587 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8588 struct got_repository *repo)
8590 const struct got_error *err;
8591 char *branch_refname = NULL;
8592 struct got_reference *branch_ref = NULL;
8594 *in_progress = 0;
8596 err = get_merge_branch_ref_name(&branch_refname, worktree);
8597 if (err)
8598 return err;
8599 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8600 free(branch_refname);
8601 if (err) {
8602 if (err->code != GOT_ERR_NOT_REF)
8603 return err;
8604 } else
8605 *in_progress = 1;
8607 return NULL;
8610 const struct got_error *got_worktree_merge_prepare(
8611 struct got_fileindex **fileindex, struct got_worktree *worktree,
8612 struct got_repository *repo)
8614 const struct got_error *err = NULL;
8615 char *fileindex_path = NULL;
8616 struct got_reference *wt_branch = NULL;
8617 struct got_object_id *wt_branch_tip = NULL;
8618 struct check_rebase_ok_arg ok_arg;
8620 *fileindex = NULL;
8622 err = lock_worktree(worktree, LOCK_EX);
8623 if (err)
8624 return err;
8626 err = open_fileindex(fileindex, &fileindex_path, worktree);
8627 if (err)
8628 goto done;
8630 /* Preconditions are the same as for rebase. */
8631 ok_arg.worktree = worktree;
8632 ok_arg.repo = repo;
8633 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8634 &ok_arg);
8635 if (err)
8636 goto done;
8638 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8639 0);
8640 if (err)
8641 goto done;
8643 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8644 if (err)
8645 goto done;
8647 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8648 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8649 goto done;
8652 done:
8653 free(fileindex_path);
8654 if (wt_branch)
8655 got_ref_close(wt_branch);
8656 free(wt_branch_tip);
8657 if (err) {
8658 if (*fileindex) {
8659 got_fileindex_free(*fileindex);
8660 *fileindex = NULL;
8662 lock_worktree(worktree, LOCK_SH);
8664 return err;
8667 const struct got_error *got_worktree_merge_write_refs(
8668 struct got_worktree *worktree, struct got_reference *branch,
8669 struct got_repository *repo)
8671 const struct got_error *err = NULL;
8672 char *branch_refname = NULL, *commit_refname = NULL;
8673 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8674 struct got_object_id *branch_tip = NULL;
8676 err = get_merge_branch_ref_name(&branch_refname, worktree);
8677 if (err)
8678 return err;
8680 err = get_merge_commit_ref_name(&commit_refname, worktree);
8681 if (err)
8682 return err;
8684 err = got_ref_resolve(&branch_tip, repo, branch);
8685 if (err)
8686 goto done;
8688 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8689 if (err)
8690 goto done;
8691 err = got_ref_write(branch_ref, repo);
8692 if (err)
8693 goto done;
8695 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8696 if (err)
8697 goto done;
8698 err = got_ref_write(commit_ref, repo);
8699 if (err)
8700 goto done;
8702 done:
8703 free(branch_refname);
8704 free(commit_refname);
8705 if (branch_ref)
8706 got_ref_close(branch_ref);
8707 if (commit_ref)
8708 got_ref_close(commit_ref);
8709 free(branch_tip);
8710 return err;
8713 const struct got_error *
8714 got_worktree_merge_continue(char **branch_name,
8715 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8716 struct got_worktree *worktree, struct got_repository *repo)
8718 const struct got_error *err;
8719 char *commit_refname = NULL, *branch_refname = NULL;
8720 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8721 char *fileindex_path = NULL;
8722 int have_staged_files = 0;
8724 *branch_name = NULL;
8725 *branch_tip = NULL;
8726 *fileindex = NULL;
8728 err = lock_worktree(worktree, LOCK_EX);
8729 if (err)
8730 return err;
8732 err = open_fileindex(fileindex, &fileindex_path, worktree);
8733 if (err)
8734 goto done;
8736 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8737 &have_staged_files);
8738 if (err && err->code != GOT_ERR_CANCELLED)
8739 goto done;
8740 if (have_staged_files) {
8741 err = got_error(GOT_ERR_STAGED_PATHS);
8742 goto done;
8745 err = get_merge_branch_ref_name(&branch_refname, worktree);
8746 if (err)
8747 goto done;
8749 err = get_merge_commit_ref_name(&commit_refname, worktree);
8750 if (err)
8751 goto done;
8753 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8754 if (err)
8755 goto done;
8757 if (!got_ref_is_symbolic(branch_ref)) {
8758 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8759 "%s is not a symbolic reference",
8760 got_ref_get_name(branch_ref));
8761 goto done;
8763 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8764 if (*branch_name == NULL) {
8765 err = got_error_from_errno("strdup");
8766 goto done;
8769 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8770 if (err)
8771 goto done;
8773 err = got_ref_resolve(branch_tip, repo, commit_ref);
8774 if (err)
8775 goto done;
8776 done:
8777 free(commit_refname);
8778 free(branch_refname);
8779 free(fileindex_path);
8780 if (commit_ref)
8781 got_ref_close(commit_ref);
8782 if (branch_ref)
8783 got_ref_close(branch_ref);
8784 if (err) {
8785 if (*branch_name) {
8786 free(*branch_name);
8787 *branch_name = NULL;
8789 free(*branch_tip);
8790 *branch_tip = NULL;
8791 if (*fileindex) {
8792 got_fileindex_free(*fileindex);
8793 *fileindex = NULL;
8795 lock_worktree(worktree, LOCK_SH);
8797 return err;
8800 const struct got_error *
8801 got_worktree_merge_abort(struct got_worktree *worktree,
8802 struct got_fileindex *fileindex, struct got_repository *repo,
8803 got_worktree_checkout_cb progress_cb, void *progress_arg)
8805 const struct got_error *err, *unlockerr, *sync_err;
8806 struct got_commit_object *commit = NULL;
8807 char *fileindex_path = NULL;
8808 struct revert_file_args rfa;
8809 char *commit_ref_name = NULL;
8810 struct got_reference *commit_ref = NULL;
8811 struct got_object_id *merged_commit_id = NULL;
8812 struct got_object_id *tree_id = NULL;
8813 struct got_pathlist_head added_paths;
8815 TAILQ_INIT(&added_paths);
8817 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8818 if (err)
8819 goto done;
8821 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8822 if (err)
8823 goto done;
8825 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8826 if (err)
8827 goto done;
8830 * Determine which files in added status can be safely removed
8831 * from disk while reverting changes in the work tree.
8832 * We want to avoid deleting unrelated files which were added by
8833 * the user for conflict resolution purposes.
8835 err = get_paths_added_between_commits(&added_paths,
8836 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8837 got_worktree_get_path_prefix(worktree), repo);
8838 if (err)
8839 goto done;
8842 err = got_object_open_as_commit(&commit, repo,
8843 worktree->base_commit_id);
8844 if (err)
8845 goto done;
8847 err = got_object_id_by_path(&tree_id, repo, commit,
8848 worktree->path_prefix);
8849 if (err)
8850 goto done;
8852 err = delete_merge_refs(worktree, repo);
8853 if (err)
8854 goto done;
8856 err = get_fileindex_path(&fileindex_path, worktree);
8857 if (err)
8858 goto done;
8860 rfa.worktree = worktree;
8861 rfa.fileindex = fileindex;
8862 rfa.progress_cb = progress_cb;
8863 rfa.progress_arg = progress_arg;
8864 rfa.patch_cb = NULL;
8865 rfa.patch_arg = NULL;
8866 rfa.repo = repo;
8867 rfa.unlink_added_files = 1;
8868 rfa.added_files_to_unlink = &added_paths;
8869 err = worktree_status(worktree, "", fileindex, repo,
8870 revert_file, &rfa, NULL, NULL, 1, 0);
8871 if (err)
8872 goto sync;
8874 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8875 repo, progress_cb, progress_arg, NULL, NULL);
8876 sync:
8877 sync_err = sync_fileindex(fileindex, fileindex_path);
8878 if (sync_err && err == NULL)
8879 err = sync_err;
8880 done:
8881 free(tree_id);
8882 free(merged_commit_id);
8883 if (commit)
8884 got_object_commit_close(commit);
8885 if (fileindex)
8886 got_fileindex_free(fileindex);
8887 free(fileindex_path);
8888 if (commit_ref)
8889 got_ref_close(commit_ref);
8890 free(commit_ref_name);
8892 unlockerr = lock_worktree(worktree, LOCK_SH);
8893 if (unlockerr && err == NULL)
8894 err = unlockerr;
8895 return err;
8898 struct check_stage_ok_arg {
8899 struct got_object_id *head_commit_id;
8900 struct got_worktree *worktree;
8901 struct got_fileindex *fileindex;
8902 struct got_repository *repo;
8903 int have_changes;
8906 static const struct got_error *
8907 check_stage_ok(void *arg, unsigned char status,
8908 unsigned char staged_status, const char *relpath,
8909 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8910 struct got_object_id *commit_id, int dirfd, const char *de_name)
8912 struct check_stage_ok_arg *a = arg;
8913 const struct got_error *err = NULL;
8914 struct got_fileindex_entry *ie;
8915 struct got_object_id base_commit_id;
8916 struct got_object_id *base_commit_idp = NULL;
8917 char *in_repo_path = NULL, *p;
8919 if (status == GOT_STATUS_UNVERSIONED ||
8920 status == GOT_STATUS_NO_CHANGE)
8921 return NULL;
8922 if (status == GOT_STATUS_NONEXISTENT)
8923 return got_error_set_errno(ENOENT, relpath);
8925 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8926 if (ie == NULL)
8927 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8929 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8930 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8931 relpath) == -1)
8932 return got_error_from_errno("asprintf");
8934 if (got_fileindex_entry_has_commit(ie)) {
8935 base_commit_idp = got_fileindex_entry_get_commit_id(
8936 &base_commit_id, ie);
8939 if (status == GOT_STATUS_CONFLICT) {
8940 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8941 goto done;
8942 } else if (status != GOT_STATUS_ADD &&
8943 status != GOT_STATUS_MODIFY &&
8944 status != GOT_STATUS_DELETE) {
8945 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8946 goto done;
8949 a->have_changes = 1;
8951 p = in_repo_path;
8952 while (p[0] == '/')
8953 p++;
8954 err = check_out_of_date(p, status, staged_status,
8955 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8956 GOT_ERR_STAGE_OUT_OF_DATE);
8957 done:
8958 free(in_repo_path);
8959 return err;
8962 struct stage_path_arg {
8963 struct got_worktree *worktree;
8964 struct got_fileindex *fileindex;
8965 struct got_repository *repo;
8966 got_worktree_status_cb status_cb;
8967 void *status_arg;
8968 got_worktree_patch_cb patch_cb;
8969 void *patch_arg;
8970 int staged_something;
8971 int allow_bad_symlinks;
8974 static const struct got_error *
8975 stage_path(void *arg, unsigned char status,
8976 unsigned char staged_status, const char *relpath,
8977 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8978 struct got_object_id *commit_id, int dirfd, const char *de_name)
8980 struct stage_path_arg *a = arg;
8981 const struct got_error *err = NULL;
8982 struct got_fileindex_entry *ie;
8983 char *ondisk_path = NULL, *path_content = NULL;
8984 uint32_t stage;
8985 struct got_object_id *new_staged_blob_id = NULL;
8986 struct stat sb;
8988 if (status == GOT_STATUS_UNVERSIONED)
8989 return NULL;
8991 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8992 if (ie == NULL)
8993 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8995 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8996 relpath)== -1)
8997 return got_error_from_errno("asprintf");
8999 switch (status) {
9000 case GOT_STATUS_ADD:
9001 case GOT_STATUS_MODIFY:
9002 /* XXX could sb.st_mode be passed in by our caller? */
9003 if (lstat(ondisk_path, &sb) == -1) {
9004 err = got_error_from_errno2("lstat", ondisk_path);
9005 break;
9007 if (a->patch_cb) {
9008 if (status == GOT_STATUS_ADD) {
9009 int choice = GOT_PATCH_CHOICE_NONE;
9010 err = (*a->patch_cb)(&choice, a->patch_arg,
9011 status, ie->path, NULL, 1, 1);
9012 if (err)
9013 break;
9014 if (choice != GOT_PATCH_CHOICE_YES)
9015 break;
9016 } else {
9017 err = create_patched_content(&path_content, 0,
9018 staged_blob_id ? staged_blob_id : blob_id,
9019 ondisk_path, dirfd, de_name, ie->path,
9020 a->repo, a->patch_cb, a->patch_arg);
9021 if (err || path_content == NULL)
9022 break;
9025 err = got_object_blob_create(&new_staged_blob_id,
9026 path_content ? path_content : ondisk_path, a->repo);
9027 if (err)
9028 break;
9029 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9030 SHA1_DIGEST_LENGTH);
9031 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9032 stage = GOT_FILEIDX_STAGE_ADD;
9033 else
9034 stage = GOT_FILEIDX_STAGE_MODIFY;
9035 got_fileindex_entry_stage_set(ie, stage);
9036 if (S_ISLNK(sb.st_mode)) {
9037 int is_bad_symlink = 0;
9038 if (!a->allow_bad_symlinks) {
9039 char target_path[PATH_MAX];
9040 ssize_t target_len;
9041 target_len = readlink(ondisk_path, target_path,
9042 sizeof(target_path));
9043 if (target_len == -1) {
9044 err = got_error_from_errno2("readlink",
9045 ondisk_path);
9046 break;
9048 err = is_bad_symlink_target(&is_bad_symlink,
9049 target_path, target_len, ondisk_path,
9050 a->worktree->root_path,
9051 a->worktree->meta_dir);
9052 if (err)
9053 break;
9054 if (is_bad_symlink) {
9055 err = got_error_path(ondisk_path,
9056 GOT_ERR_BAD_SYMLINK);
9057 break;
9060 if (is_bad_symlink)
9061 got_fileindex_entry_staged_filetype_set(ie,
9062 GOT_FILEIDX_MODE_BAD_SYMLINK);
9063 else
9064 got_fileindex_entry_staged_filetype_set(ie,
9065 GOT_FILEIDX_MODE_SYMLINK);
9066 } else {
9067 got_fileindex_entry_staged_filetype_set(ie,
9068 GOT_FILEIDX_MODE_REGULAR_FILE);
9070 a->staged_something = 1;
9071 if (a->status_cb == NULL)
9072 break;
9073 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9074 get_staged_status(ie), relpath, blob_id,
9075 new_staged_blob_id, NULL, dirfd, de_name);
9076 if (err)
9077 break;
9079 * When staging the reverse of the staged diff,
9080 * implicitly unstage the file.
9082 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9083 sizeof(ie->blob_sha1)) == 0) {
9084 got_fileindex_entry_stage_set(ie,
9085 GOT_FILEIDX_STAGE_NONE);
9087 break;
9088 case GOT_STATUS_DELETE:
9089 if (staged_status == GOT_STATUS_DELETE)
9090 break;
9091 if (a->patch_cb) {
9092 int choice = GOT_PATCH_CHOICE_NONE;
9093 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9094 ie->path, NULL, 1, 1);
9095 if (err)
9096 break;
9097 if (choice == GOT_PATCH_CHOICE_NO)
9098 break;
9099 if (choice != GOT_PATCH_CHOICE_YES) {
9100 err = got_error(GOT_ERR_PATCH_CHOICE);
9101 break;
9104 stage = GOT_FILEIDX_STAGE_DELETE;
9105 got_fileindex_entry_stage_set(ie, stage);
9106 a->staged_something = 1;
9107 if (a->status_cb == NULL)
9108 break;
9109 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9110 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9111 de_name);
9112 break;
9113 case GOT_STATUS_NO_CHANGE:
9114 break;
9115 case GOT_STATUS_CONFLICT:
9116 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9117 break;
9118 case GOT_STATUS_NONEXISTENT:
9119 err = got_error_set_errno(ENOENT, relpath);
9120 break;
9121 default:
9122 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9123 break;
9126 if (path_content && unlink(path_content) == -1 && err == NULL)
9127 err = got_error_from_errno2("unlink", path_content);
9128 free(path_content);
9129 free(ondisk_path);
9130 free(new_staged_blob_id);
9131 return err;
9134 const struct got_error *
9135 got_worktree_stage(struct got_worktree *worktree,
9136 struct got_pathlist_head *paths,
9137 got_worktree_status_cb status_cb, void *status_arg,
9138 got_worktree_patch_cb patch_cb, void *patch_arg,
9139 int allow_bad_symlinks, struct got_repository *repo)
9141 const struct got_error *err = NULL, *sync_err, *unlockerr;
9142 struct got_pathlist_entry *pe;
9143 struct got_fileindex *fileindex = NULL;
9144 char *fileindex_path = NULL;
9145 struct got_reference *head_ref = NULL;
9146 struct got_object_id *head_commit_id = NULL;
9147 struct check_stage_ok_arg oka;
9148 struct stage_path_arg spa;
9150 err = lock_worktree(worktree, LOCK_EX);
9151 if (err)
9152 return err;
9154 err = got_ref_open(&head_ref, repo,
9155 got_worktree_get_head_ref_name(worktree), 0);
9156 if (err)
9157 goto done;
9158 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9159 if (err)
9160 goto done;
9161 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9162 if (err)
9163 goto done;
9165 /* Check pre-conditions before staging anything. */
9166 oka.head_commit_id = head_commit_id;
9167 oka.worktree = worktree;
9168 oka.fileindex = fileindex;
9169 oka.repo = repo;
9170 oka.have_changes = 0;
9171 TAILQ_FOREACH(pe, paths, entry) {
9172 err = worktree_status(worktree, pe->path, fileindex, repo,
9173 check_stage_ok, &oka, NULL, NULL, 1, 0);
9174 if (err)
9175 goto done;
9177 if (!oka.have_changes) {
9178 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9179 goto done;
9182 spa.worktree = worktree;
9183 spa.fileindex = fileindex;
9184 spa.repo = repo;
9185 spa.patch_cb = patch_cb;
9186 spa.patch_arg = patch_arg;
9187 spa.status_cb = status_cb;
9188 spa.status_arg = status_arg;
9189 spa.staged_something = 0;
9190 spa.allow_bad_symlinks = allow_bad_symlinks;
9191 TAILQ_FOREACH(pe, paths, entry) {
9192 err = worktree_status(worktree, pe->path, fileindex, repo,
9193 stage_path, &spa, NULL, NULL, 1, 0);
9194 if (err)
9195 goto done;
9197 if (!spa.staged_something) {
9198 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9199 goto done;
9202 sync_err = sync_fileindex(fileindex, fileindex_path);
9203 if (sync_err && err == NULL)
9204 err = sync_err;
9205 done:
9206 if (head_ref)
9207 got_ref_close(head_ref);
9208 free(head_commit_id);
9209 free(fileindex_path);
9210 if (fileindex)
9211 got_fileindex_free(fileindex);
9212 unlockerr = lock_worktree(worktree, LOCK_SH);
9213 if (unlockerr && err == NULL)
9214 err = unlockerr;
9215 return err;
9218 struct unstage_path_arg {
9219 struct got_worktree *worktree;
9220 struct got_fileindex *fileindex;
9221 struct got_repository *repo;
9222 got_worktree_checkout_cb progress_cb;
9223 void *progress_arg;
9224 got_worktree_patch_cb patch_cb;
9225 void *patch_arg;
9228 static const struct got_error *
9229 create_unstaged_content(char **path_unstaged_content,
9230 char **path_new_staged_content, struct got_object_id *blob_id,
9231 struct got_object_id *staged_blob_id, const char *relpath,
9232 struct got_repository *repo,
9233 got_worktree_patch_cb patch_cb, void *patch_arg)
9235 const struct got_error *err, *free_err;
9236 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9237 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9238 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9239 struct got_diffreg_result *diffreg_result = NULL;
9240 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9241 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9242 int fd1 = -1, fd2 = -1;
9244 *path_unstaged_content = NULL;
9245 *path_new_staged_content = NULL;
9247 err = got_object_id_str(&label1, blob_id);
9248 if (err)
9249 return err;
9251 fd1 = got_opentempfd();
9252 if (fd1 == -1) {
9253 err = got_error_from_errno("got_opentempfd");
9254 goto done;
9256 fd2 = got_opentempfd();
9257 if (fd2 == -1) {
9258 err = got_error_from_errno("got_opentempfd");
9259 goto done;
9262 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9263 if (err)
9264 goto done;
9266 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9267 if (err)
9268 goto done;
9270 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9271 if (err)
9272 goto done;
9274 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9275 fd2);
9276 if (err)
9277 goto done;
9279 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9280 if (err)
9281 goto done;
9283 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9284 if (err)
9285 goto done;
9287 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9288 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9289 if (err)
9290 goto done;
9292 err = got_opentemp_named(path_unstaged_content, &outfile,
9293 "got-unstaged-content", "");
9294 if (err)
9295 goto done;
9296 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9297 "got-new-staged-content", "");
9298 if (err)
9299 goto done;
9301 if (fseek(f1, 0L, SEEK_SET) == -1) {
9302 err = got_ferror(f1, GOT_ERR_IO);
9303 goto done;
9305 if (fseek(f2, 0L, SEEK_SET) == -1) {
9306 err = got_ferror(f2, GOT_ERR_IO);
9307 goto done;
9309 /* Count the number of actual changes in the diff result. */
9310 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9311 struct diff_chunk_context cc = {};
9312 diff_chunk_context_load_change(&cc, &nchunks_used,
9313 diffreg_result->result, n, 0);
9314 nchanges++;
9316 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9317 int choice;
9318 err = apply_or_reject_change(&choice, &nchunks_used,
9319 diffreg_result->result, n, relpath, f1, f2,
9320 &line_cur1, &line_cur2,
9321 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9322 if (err)
9323 goto done;
9324 if (choice == GOT_PATCH_CHOICE_YES)
9325 have_content = 1;
9326 else
9327 have_rejected_content = 1;
9328 if (choice == GOT_PATCH_CHOICE_QUIT)
9329 break;
9331 if (have_content || have_rejected_content)
9332 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9333 outfile, rejectfile);
9334 done:
9335 free(label1);
9336 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9337 err = got_error_from_errno("close");
9338 if (blob)
9339 got_object_blob_close(blob);
9340 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9341 err = got_error_from_errno("close");
9342 if (staged_blob)
9343 got_object_blob_close(staged_blob);
9344 free_err = got_diffreg_result_free(diffreg_result);
9345 if (free_err && err == NULL)
9346 err = free_err;
9347 if (f1 && fclose(f1) == EOF && err == NULL)
9348 err = got_error_from_errno2("fclose", path1);
9349 if (f2 && fclose(f2) == EOF && err == NULL)
9350 err = got_error_from_errno2("fclose", path2);
9351 if (outfile && fclose(outfile) == EOF && err == NULL)
9352 err = got_error_from_errno2("fclose", *path_unstaged_content);
9353 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9354 err = got_error_from_errno2("fclose", *path_new_staged_content);
9355 if (path1 && unlink(path1) == -1 && err == NULL)
9356 err = got_error_from_errno2("unlink", path1);
9357 if (path2 && unlink(path2) == -1 && err == NULL)
9358 err = got_error_from_errno2("unlink", path2);
9359 if (err || !have_content) {
9360 if (*path_unstaged_content &&
9361 unlink(*path_unstaged_content) == -1 && err == NULL)
9362 err = got_error_from_errno2("unlink",
9363 *path_unstaged_content);
9364 free(*path_unstaged_content);
9365 *path_unstaged_content = NULL;
9367 if (err || !have_content || !have_rejected_content) {
9368 if (*path_new_staged_content &&
9369 unlink(*path_new_staged_content) == -1 && err == NULL)
9370 err = got_error_from_errno2("unlink",
9371 *path_new_staged_content);
9372 free(*path_new_staged_content);
9373 *path_new_staged_content = NULL;
9375 free(path1);
9376 free(path2);
9377 return err;
9380 static const struct got_error *
9381 unstage_hunks(struct got_object_id *staged_blob_id,
9382 struct got_blob_object *blob_base,
9383 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9384 const char *ondisk_path, const char *label_orig,
9385 struct got_worktree *worktree, struct got_repository *repo,
9386 got_worktree_patch_cb patch_cb, void *patch_arg,
9387 got_worktree_checkout_cb progress_cb, void *progress_arg)
9389 const struct got_error *err = NULL;
9390 char *path_unstaged_content = NULL;
9391 char *path_new_staged_content = NULL;
9392 char *parent = NULL, *base_path = NULL;
9393 char *blob_base_path = NULL;
9394 struct got_object_id *new_staged_blob_id = NULL;
9395 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9396 struct stat sb;
9398 err = create_unstaged_content(&path_unstaged_content,
9399 &path_new_staged_content, blob_id, staged_blob_id,
9400 ie->path, repo, patch_cb, patch_arg);
9401 if (err)
9402 return err;
9404 if (path_unstaged_content == NULL)
9405 return NULL;
9407 if (path_new_staged_content) {
9408 err = got_object_blob_create(&new_staged_blob_id,
9409 path_new_staged_content, repo);
9410 if (err)
9411 goto done;
9414 f = fopen(path_unstaged_content, "re");
9415 if (f == NULL) {
9416 err = got_error_from_errno2("fopen",
9417 path_unstaged_content);
9418 goto done;
9420 if (fstat(fileno(f), &sb) == -1) {
9421 err = got_error_from_errno2("fstat", path_unstaged_content);
9422 goto done;
9424 if (got_fileindex_entry_staged_filetype_get(ie) ==
9425 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9426 char link_target[PATH_MAX];
9427 size_t r;
9428 r = fread(link_target, 1, sizeof(link_target), f);
9429 if (r == 0 && ferror(f)) {
9430 err = got_error_from_errno("fread");
9431 goto done;
9433 if (r >= sizeof(link_target)) { /* should not happen */
9434 err = got_error(GOT_ERR_NO_SPACE);
9435 goto done;
9437 link_target[r] = '\0';
9438 err = merge_symlink(worktree, blob_base,
9439 ondisk_path, ie->path, label_orig, link_target,
9440 worktree->base_commit_id, repo, progress_cb,
9441 progress_arg);
9442 } else {
9443 int local_changes_subsumed;
9445 err = got_path_dirname(&parent, ondisk_path);
9446 if (err)
9447 return err;
9449 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9450 parent) == -1) {
9451 err = got_error_from_errno("asprintf");
9452 base_path = NULL;
9453 goto done;
9456 err = got_opentemp_named(&blob_base_path, &f_base,
9457 base_path, "");
9458 if (err)
9459 goto done;
9460 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9461 blob_base);
9462 if (err)
9463 goto done;
9466 * In order the run a 3-way merge with a symlink we copy the symlink's
9467 * target path into a temporary file and use that file with diff3.
9469 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9470 err = dump_symlink_target_path_to_file(&f_deriv2,
9471 ondisk_path);
9472 if (err)
9473 goto done;
9474 } else {
9475 int fd;
9476 fd = open(ondisk_path,
9477 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9478 if (fd == -1) {
9479 err = got_error_from_errno2("open", ondisk_path);
9480 goto done;
9482 f_deriv2 = fdopen(fd, "r");
9483 if (f_deriv2 == NULL) {
9484 err = got_error_from_errno2("fdopen", ondisk_path);
9485 close(fd);
9486 goto done;
9490 err = merge_file(&local_changes_subsumed, worktree,
9491 f_base, f, f_deriv2, ondisk_path, ie->path,
9492 got_fileindex_perms_to_st(ie),
9493 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9494 repo, progress_cb, progress_arg);
9496 if (err)
9497 goto done;
9499 if (new_staged_blob_id) {
9500 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9501 SHA1_DIGEST_LENGTH);
9502 } else {
9503 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9504 got_fileindex_entry_staged_filetype_set(ie, 0);
9506 done:
9507 free(new_staged_blob_id);
9508 if (path_unstaged_content &&
9509 unlink(path_unstaged_content) == -1 && err == NULL)
9510 err = got_error_from_errno2("unlink", path_unstaged_content);
9511 if (path_new_staged_content &&
9512 unlink(path_new_staged_content) == -1 && err == NULL)
9513 err = got_error_from_errno2("unlink", path_new_staged_content);
9514 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9515 err = got_error_from_errno2("unlink", blob_base_path);
9516 if (f_base && fclose(f_base) == EOF && err == NULL)
9517 err = got_error_from_errno2("fclose", path_unstaged_content);
9518 if (f && fclose(f) == EOF && err == NULL)
9519 err = got_error_from_errno2("fclose", path_unstaged_content);
9520 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9521 err = got_error_from_errno2("fclose", ondisk_path);
9522 free(path_unstaged_content);
9523 free(path_new_staged_content);
9524 free(blob_base_path);
9525 free(parent);
9526 free(base_path);
9527 return err;
9530 static const struct got_error *
9531 unstage_path(void *arg, unsigned char status,
9532 unsigned char staged_status, const char *relpath,
9533 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9534 struct got_object_id *commit_id, int dirfd, const char *de_name)
9536 const struct got_error *err = NULL;
9537 struct unstage_path_arg *a = arg;
9538 struct got_fileindex_entry *ie;
9539 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9540 char *ondisk_path = NULL;
9541 char *id_str = NULL, *label_orig = NULL;
9542 int local_changes_subsumed;
9543 struct stat sb;
9544 int fd1 = -1, fd2 = -1;
9546 if (staged_status != GOT_STATUS_ADD &&
9547 staged_status != GOT_STATUS_MODIFY &&
9548 staged_status != GOT_STATUS_DELETE)
9549 return NULL;
9551 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9552 if (ie == NULL)
9553 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9555 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9556 == -1)
9557 return got_error_from_errno("asprintf");
9559 err = got_object_id_str(&id_str,
9560 commit_id ? commit_id : a->worktree->base_commit_id);
9561 if (err)
9562 goto done;
9563 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9564 id_str) == -1) {
9565 err = got_error_from_errno("asprintf");
9566 goto done;
9569 fd1 = got_opentempfd();
9570 if (fd1 == -1) {
9571 err = got_error_from_errno("got_opentempfd");
9572 goto done;
9574 fd2 = got_opentempfd();
9575 if (fd2 == -1) {
9576 err = got_error_from_errno("got_opentempfd");
9577 goto done;
9580 switch (staged_status) {
9581 case GOT_STATUS_MODIFY:
9582 err = got_object_open_as_blob(&blob_base, a->repo,
9583 blob_id, 8192, fd1);
9584 if (err)
9585 break;
9586 /* fall through */
9587 case GOT_STATUS_ADD:
9588 if (a->patch_cb) {
9589 if (staged_status == GOT_STATUS_ADD) {
9590 int choice = GOT_PATCH_CHOICE_NONE;
9591 err = (*a->patch_cb)(&choice, a->patch_arg,
9592 staged_status, ie->path, NULL, 1, 1);
9593 if (err)
9594 break;
9595 if (choice != GOT_PATCH_CHOICE_YES)
9596 break;
9597 } else {
9598 err = unstage_hunks(staged_blob_id,
9599 blob_base, blob_id, ie, ondisk_path,
9600 label_orig, a->worktree, a->repo,
9601 a->patch_cb, a->patch_arg,
9602 a->progress_cb, a->progress_arg);
9603 break; /* Done with this file. */
9606 err = got_object_open_as_blob(&blob_staged, a->repo,
9607 staged_blob_id, 8192, fd2);
9608 if (err)
9609 break;
9610 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9611 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9612 case GOT_FILEIDX_MODE_REGULAR_FILE:
9613 err = merge_blob(&local_changes_subsumed, a->worktree,
9614 blob_base, ondisk_path, relpath,
9615 got_fileindex_perms_to_st(ie), label_orig,
9616 blob_staged, commit_id ? commit_id :
9617 a->worktree->base_commit_id, a->repo,
9618 a->progress_cb, a->progress_arg);
9619 break;
9620 case GOT_FILEIDX_MODE_SYMLINK:
9621 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9622 char *staged_target;
9623 err = got_object_blob_read_to_str(
9624 &staged_target, blob_staged);
9625 if (err)
9626 goto done;
9627 err = merge_symlink(a->worktree, blob_base,
9628 ondisk_path, relpath, label_orig,
9629 staged_target, commit_id ? commit_id :
9630 a->worktree->base_commit_id,
9631 a->repo, a->progress_cb, a->progress_arg);
9632 free(staged_target);
9633 } else {
9634 err = merge_blob(&local_changes_subsumed,
9635 a->worktree, blob_base, ondisk_path,
9636 relpath, got_fileindex_perms_to_st(ie),
9637 label_orig, blob_staged,
9638 commit_id ? commit_id :
9639 a->worktree->base_commit_id, a->repo,
9640 a->progress_cb, a->progress_arg);
9642 break;
9643 default:
9644 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9645 break;
9647 if (err == NULL) {
9648 got_fileindex_entry_stage_set(ie,
9649 GOT_FILEIDX_STAGE_NONE);
9650 got_fileindex_entry_staged_filetype_set(ie, 0);
9652 break;
9653 case GOT_STATUS_DELETE:
9654 if (a->patch_cb) {
9655 int choice = GOT_PATCH_CHOICE_NONE;
9656 err = (*a->patch_cb)(&choice, a->patch_arg,
9657 staged_status, ie->path, NULL, 1, 1);
9658 if (err)
9659 break;
9660 if (choice == GOT_PATCH_CHOICE_NO)
9661 break;
9662 if (choice != GOT_PATCH_CHOICE_YES) {
9663 err = got_error(GOT_ERR_PATCH_CHOICE);
9664 break;
9667 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9668 got_fileindex_entry_staged_filetype_set(ie, 0);
9669 err = get_file_status(&status, &sb, ie, ondisk_path,
9670 dirfd, de_name, a->repo);
9671 if (err)
9672 break;
9673 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9674 break;
9676 done:
9677 free(ondisk_path);
9678 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9679 err = got_error_from_errno("close");
9680 if (blob_base)
9681 got_object_blob_close(blob_base);
9682 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9683 err = got_error_from_errno("close");
9684 if (blob_staged)
9685 got_object_blob_close(blob_staged);
9686 free(id_str);
9687 free(label_orig);
9688 return err;
9691 const struct got_error *
9692 got_worktree_unstage(struct got_worktree *worktree,
9693 struct got_pathlist_head *paths,
9694 got_worktree_checkout_cb progress_cb, void *progress_arg,
9695 got_worktree_patch_cb patch_cb, void *patch_arg,
9696 struct got_repository *repo)
9698 const struct got_error *err = NULL, *sync_err, *unlockerr;
9699 struct got_pathlist_entry *pe;
9700 struct got_fileindex *fileindex = NULL;
9701 char *fileindex_path = NULL;
9702 struct unstage_path_arg upa;
9704 err = lock_worktree(worktree, LOCK_EX);
9705 if (err)
9706 return err;
9708 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9709 if (err)
9710 goto done;
9712 upa.worktree = worktree;
9713 upa.fileindex = fileindex;
9714 upa.repo = repo;
9715 upa.progress_cb = progress_cb;
9716 upa.progress_arg = progress_arg;
9717 upa.patch_cb = patch_cb;
9718 upa.patch_arg = patch_arg;
9719 TAILQ_FOREACH(pe, paths, entry) {
9720 err = worktree_status(worktree, pe->path, fileindex, repo,
9721 unstage_path, &upa, NULL, NULL, 1, 0);
9722 if (err)
9723 goto done;
9726 sync_err = sync_fileindex(fileindex, fileindex_path);
9727 if (sync_err && err == NULL)
9728 err = sync_err;
9729 done:
9730 free(fileindex_path);
9731 if (fileindex)
9732 got_fileindex_free(fileindex);
9733 unlockerr = lock_worktree(worktree, LOCK_SH);
9734 if (unlockerr && err == NULL)
9735 err = unlockerr;
9736 return err;
9739 struct report_file_info_arg {
9740 struct got_worktree *worktree;
9741 got_worktree_path_info_cb info_cb;
9742 void *info_arg;
9743 struct got_pathlist_head *paths;
9744 got_cancel_cb cancel_cb;
9745 void *cancel_arg;
9748 static const struct got_error *
9749 report_file_info(void *arg, struct got_fileindex_entry *ie)
9751 const struct got_error *err;
9752 struct report_file_info_arg *a = arg;
9753 struct got_pathlist_entry *pe;
9754 struct got_object_id blob_id, staged_blob_id, commit_id;
9755 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9756 struct got_object_id *commit_idp = NULL;
9757 int stage;
9759 if (a->cancel_cb) {
9760 err = a->cancel_cb(a->cancel_arg);
9761 if (err)
9762 return err;
9765 TAILQ_FOREACH(pe, a->paths, entry) {
9766 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9767 got_path_is_child(ie->path, pe->path, pe->path_len))
9768 break;
9770 if (pe == NULL) /* not found */
9771 return NULL;
9773 if (got_fileindex_entry_has_blob(ie))
9774 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9775 stage = got_fileindex_entry_stage_get(ie);
9776 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9777 stage == GOT_FILEIDX_STAGE_ADD) {
9778 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9779 &staged_blob_id, ie);
9782 if (got_fileindex_entry_has_commit(ie))
9783 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9785 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9786 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9789 const struct got_error *
9790 got_worktree_path_info(struct got_worktree *worktree,
9791 struct got_pathlist_head *paths,
9792 got_worktree_path_info_cb info_cb, void *info_arg,
9793 got_cancel_cb cancel_cb, void *cancel_arg)
9796 const struct got_error *err = NULL, *unlockerr;
9797 struct got_fileindex *fileindex = NULL;
9798 char *fileindex_path = NULL;
9799 struct report_file_info_arg arg;
9801 err = lock_worktree(worktree, LOCK_SH);
9802 if (err)
9803 return err;
9805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9806 if (err)
9807 goto done;
9809 arg.worktree = worktree;
9810 arg.info_cb = info_cb;
9811 arg.info_arg = info_arg;
9812 arg.paths = paths;
9813 arg.cancel_cb = cancel_cb;
9814 arg.cancel_arg = cancel_arg;
9815 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9816 &arg);
9817 done:
9818 free(fileindex_path);
9819 if (fileindex)
9820 got_fileindex_free(fileindex);
9821 unlockerr = lock_worktree(worktree, LOCK_UN);
9822 if (unlockerr && err == NULL)
9823 err = unlockerr;
9824 return err;
9827 static const struct got_error *
9828 patch_check_path(const char *p, char **path, unsigned char *status,
9829 unsigned char *staged_status, struct got_fileindex *fileindex,
9830 struct got_worktree *worktree, struct got_repository *repo)
9832 const struct got_error *err;
9833 struct got_fileindex_entry *ie;
9834 struct stat sb;
9835 char *ondisk_path = NULL;
9837 err = got_worktree_resolve_path(path, worktree, p);
9838 if (err)
9839 return err;
9841 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9842 *path[0] ? "/" : "", *path) == -1)
9843 return got_error_from_errno("asprintf");
9845 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9846 if (ie) {
9847 *staged_status = get_staged_status(ie);
9848 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9849 repo);
9850 if (err)
9851 goto done;
9852 } else {
9853 *staged_status = GOT_STATUS_NO_CHANGE;
9854 *status = GOT_STATUS_UNVERSIONED;
9855 if (lstat(ondisk_path, &sb) == -1) {
9856 if (errno != ENOENT) {
9857 err = got_error_from_errno2("lstat",
9858 ondisk_path);
9859 goto done;
9861 *status = GOT_STATUS_NONEXISTENT;
9865 done:
9866 free(ondisk_path);
9867 return err;
9870 static const struct got_error *
9871 patch_can_rm(const char *path, unsigned char status,
9872 unsigned char staged_status)
9874 if (status == GOT_STATUS_NONEXISTENT)
9875 return got_error_set_errno(ENOENT, path);
9876 if (status != GOT_STATUS_NO_CHANGE &&
9877 status != GOT_STATUS_ADD &&
9878 status != GOT_STATUS_MODIFY &&
9879 status != GOT_STATUS_MODE_CHANGE)
9880 return got_error_path(path, GOT_ERR_FILE_STATUS);
9881 if (staged_status == GOT_STATUS_DELETE)
9882 return got_error_path(path, GOT_ERR_FILE_STATUS);
9883 return NULL;
9886 static const struct got_error *
9887 patch_can_add(const char *path, unsigned char status)
9889 if (status != GOT_STATUS_NONEXISTENT)
9890 return got_error_path(path, GOT_ERR_FILE_STATUS);
9891 return NULL;
9894 static const struct got_error *
9895 patch_can_edit(const char *path, unsigned char status,
9896 unsigned char staged_status)
9898 if (status == GOT_STATUS_NONEXISTENT)
9899 return got_error_set_errno(ENOENT, path);
9900 if (status != GOT_STATUS_NO_CHANGE &&
9901 status != GOT_STATUS_ADD &&
9902 status != GOT_STATUS_MODIFY)
9903 return got_error_path(path, GOT_ERR_FILE_STATUS);
9904 if (staged_status == GOT_STATUS_DELETE)
9905 return got_error_path(path, GOT_ERR_FILE_STATUS);
9906 return NULL;
9909 const struct got_error *
9910 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9911 char **fileindex_path, struct got_worktree *worktree)
9913 return open_fileindex(fileindex, fileindex_path, worktree);
9916 const struct got_error *
9917 got_worktree_patch_check_path(const char *old, const char *new,
9918 char **oldpath, char **newpath, struct got_worktree *worktree,
9919 struct got_repository *repo, struct got_fileindex *fileindex)
9921 const struct got_error *err = NULL;
9922 int file_renamed = 0;
9923 unsigned char status_old, staged_status_old;
9924 unsigned char status_new, staged_status_new;
9926 *oldpath = NULL;
9927 *newpath = NULL;
9929 err = patch_check_path(old != NULL ? old : new, oldpath,
9930 &status_old, &staged_status_old, fileindex, worktree, repo);
9931 if (err)
9932 goto done;
9934 err = patch_check_path(new != NULL ? new : old, newpath,
9935 &status_new, &staged_status_new, fileindex, worktree, repo);
9936 if (err)
9937 goto done;
9939 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9940 file_renamed = 1;
9942 if (old != NULL && new == NULL)
9943 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9944 else if (file_renamed) {
9945 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9946 if (err == NULL)
9947 err = patch_can_add(*newpath, status_new);
9948 } else if (old == NULL)
9949 err = patch_can_add(*newpath, status_new);
9950 else
9951 err = patch_can_edit(*newpath, status_new, staged_status_new);
9953 done:
9954 if (err) {
9955 free(*oldpath);
9956 *oldpath = NULL;
9957 free(*newpath);
9958 *newpath = NULL;
9960 return err;
9963 const struct got_error *
9964 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9965 struct got_worktree *worktree, struct got_fileindex *fileindex,
9966 got_worktree_checkout_cb progress_cb, void *progress_arg)
9968 struct schedule_addition_args saa;
9970 memset(&saa, 0, sizeof(saa));
9971 saa.worktree = worktree;
9972 saa.fileindex = fileindex;
9973 saa.progress_cb = progress_cb;
9974 saa.progress_arg = progress_arg;
9975 saa.repo = repo;
9977 return worktree_status(worktree, path, fileindex, repo,
9978 schedule_addition, &saa, NULL, NULL, 1, 0);
9981 const struct got_error *
9982 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9983 struct got_worktree *worktree, struct got_fileindex *fileindex,
9984 got_worktree_delete_cb progress_cb, void *progress_arg)
9986 const struct got_error *err;
9987 struct schedule_deletion_args sda;
9988 char *ondisk_status_path;
9990 memset(&sda, 0, sizeof(sda));
9991 sda.worktree = worktree;
9992 sda.fileindex = fileindex;
9993 sda.progress_cb = progress_cb;
9994 sda.progress_arg = progress_arg;
9995 sda.repo = repo;
9996 sda.delete_local_mods = 0;
9997 sda.keep_on_disk = 0;
9998 sda.ignore_missing_paths = 0;
9999 sda.status_codes = NULL;
10000 if (asprintf(&ondisk_status_path, "%s/%s",
10001 got_worktree_get_root_path(worktree), path) == -1)
10002 return got_error_from_errno("asprintf");
10003 sda.status_path = ondisk_status_path;
10004 sda.status_path_len = strlen(ondisk_status_path);
10006 err = worktree_status(worktree, path, fileindex, repo,
10007 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10008 free(ondisk_status_path);
10009 return err;
10012 const struct got_error *
10013 got_worktree_patch_complete(struct got_fileindex *fileindex,
10014 const char *fileindex_path)
10016 const struct got_error *err = NULL;
10018 err = sync_fileindex(fileindex, fileindex_path);
10019 got_fileindex_free(fileindex);
10021 return err;