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 struct diff_cb_arg *a = arg;
2278 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2279 return got_error(GOT_ERR_CANCELLED);
2281 return update_blob(a->worktree, a->fileindex, ie, te,
2282 ie->path, a->repo, a->progress_cb, a->progress_arg);
2285 static const struct got_error *
2286 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2288 struct diff_cb_arg *a = arg;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 return delete_blob(a->worktree, a->fileindex, ie,
2294 a->repo, a->progress_cb, a->progress_arg);
2297 static const struct got_error *
2298 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2300 struct diff_cb_arg *a = arg;
2301 const struct got_error *err;
2302 char *path;
2304 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2305 return got_error(GOT_ERR_CANCELLED);
2307 if (got_object_tree_entry_is_submodule(te))
2308 return NULL;
2310 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2311 return NULL;
2313 if (asprintf(&path, "%s%s%s", parent_path,
2314 parent_path[0] ? "/" : "", te->name)
2315 == -1)
2316 return got_error_from_errno("asprintf");
2318 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2319 a->repo, a->progress_cb, a->progress_arg);
2321 free(path);
2322 return err;
2325 const struct got_error *
2326 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2328 uint32_t uuid_status;
2330 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2331 if (uuid_status != uuid_s_ok) {
2332 *uuidstr = NULL;
2333 return got_error_uuid(uuid_status, "uuid_to_string");
2336 return NULL;
2339 static const struct got_error *
2340 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2342 const struct got_error *err = NULL;
2343 char *uuidstr = NULL;
2345 *refname = NULL;
2347 err = got_worktree_get_uuid(&uuidstr, worktree);
2348 if (err)
2349 return err;
2351 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2352 err = got_error_from_errno("asprintf");
2353 *refname = NULL;
2355 free(uuidstr);
2356 return err;
2359 const struct got_error *
2360 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2361 const char *prefix)
2363 return get_ref_name(refname, worktree, prefix);
2366 static const struct got_error *
2367 get_base_ref_name(char **refname, struct got_worktree *worktree)
2369 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2372 static const struct got_error *
2373 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2379 static const struct got_error *
2380 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2382 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2385 static const struct got_error *
2386 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2388 return get_ref_name(refname, worktree,
2389 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2392 static const struct got_error *
2393 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2395 return get_ref_name(refname, worktree,
2396 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2399 static const struct got_error *
2400 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2402 return get_ref_name(refname, worktree,
2403 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2406 static const struct got_error *
2407 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2409 return get_ref_name(refname, worktree,
2410 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2413 static const struct got_error *
2414 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2416 return get_ref_name(refname, worktree,
2417 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2420 static const struct got_error *
2421 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2423 return get_ref_name(refname, worktree,
2424 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2427 const struct got_error *
2428 got_worktree_get_histedit_script_path(char **path,
2429 struct got_worktree *worktree)
2431 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2432 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2433 *path = NULL;
2434 return got_error_from_errno("asprintf");
2436 return NULL;
2439 static const struct got_error *
2440 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2442 return get_ref_name(refname, worktree,
2443 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2446 static const struct got_error *
2447 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2449 return get_ref_name(refname, worktree,
2450 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2454 * Prevent Git's garbage collector from deleting our base commit by
2455 * setting a reference to our base commit's ID.
2457 static const struct got_error *
2458 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2460 const struct got_error *err = NULL;
2461 struct got_reference *ref = NULL;
2462 char *refname;
2464 err = get_base_ref_name(&refname, worktree);
2465 if (err)
2466 return err;
2468 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2469 if (err)
2470 goto done;
2472 err = got_ref_write(ref, repo);
2473 done:
2474 free(refname);
2475 if (ref)
2476 got_ref_close(ref);
2477 return err;
2480 static const struct got_error *
2481 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2483 const struct got_error *err = NULL;
2485 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2486 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2487 err = got_error_from_errno("asprintf");
2488 *fileindex_path = NULL;
2490 return err;
2494 static const struct got_error *
2495 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2496 struct got_worktree *worktree)
2498 const struct got_error *err = NULL;
2499 FILE *index = NULL;
2501 *fileindex_path = NULL;
2502 *fileindex = got_fileindex_alloc();
2503 if (*fileindex == NULL)
2504 return got_error_from_errno("got_fileindex_alloc");
2506 err = get_fileindex_path(fileindex_path, worktree);
2507 if (err)
2508 goto done;
2510 index = fopen(*fileindex_path, "rbe");
2511 if (index == NULL) {
2512 if (errno != ENOENT)
2513 err = got_error_from_errno2("fopen", *fileindex_path);
2514 } else {
2515 err = got_fileindex_read(*fileindex, index);
2516 if (fclose(index) == EOF && err == NULL)
2517 err = got_error_from_errno("fclose");
2519 done:
2520 if (err) {
2521 free(*fileindex_path);
2522 *fileindex_path = NULL;
2523 got_fileindex_free(*fileindex);
2524 *fileindex = NULL;
2526 return err;
2529 struct bump_base_commit_id_arg {
2530 struct got_object_id *base_commit_id;
2531 const char *path;
2532 size_t path_len;
2533 const char *entry_name;
2534 got_worktree_checkout_cb progress_cb;
2535 void *progress_arg;
2538 static const struct got_error *
2539 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2541 const struct got_error *err;
2542 struct bump_base_commit_id_arg *a = arg;
2544 if (a->entry_name) {
2545 if (strcmp(ie->path, a->path) != 0)
2546 return NULL;
2547 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2548 return NULL;
2550 if (got_fileindex_entry_was_skipped(ie))
2551 return NULL;
2553 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2554 SHA1_DIGEST_LENGTH) == 0)
2555 return NULL;
2557 if (a->progress_cb) {
2558 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2559 ie->path);
2560 if (err)
2561 return err;
2563 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2564 return NULL;
2567 /* Bump base commit ID of all files within an updated part of the work tree. */
2568 static const struct got_error *
2569 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2570 struct got_fileindex *fileindex,
2571 got_worktree_checkout_cb progress_cb, void *progress_arg)
2573 struct bump_base_commit_id_arg bbc_arg;
2575 bbc_arg.base_commit_id = worktree->base_commit_id;
2576 bbc_arg.entry_name = NULL;
2577 bbc_arg.path = "";
2578 bbc_arg.path_len = 0;
2579 bbc_arg.progress_cb = progress_cb;
2580 bbc_arg.progress_arg = progress_arg;
2582 return got_fileindex_for_each_entry_safe(fileindex,
2583 bump_base_commit_id, &bbc_arg);
2586 static const struct got_error *
2587 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2589 const struct got_error *err = NULL;
2590 char *new_fileindex_path = NULL;
2591 FILE *new_index = NULL;
2592 struct timespec timeout;
2594 err = got_opentemp_named(&new_fileindex_path, &new_index,
2595 fileindex_path, "");
2596 if (err)
2597 goto done;
2599 err = got_fileindex_write(fileindex, new_index);
2600 if (err)
2601 goto done;
2603 if (rename(new_fileindex_path, fileindex_path) != 0) {
2604 err = got_error_from_errno3("rename", new_fileindex_path,
2605 fileindex_path);
2606 unlink(new_fileindex_path);
2610 * Sleep for a short amount of time to ensure that files modified after
2611 * this program exits have a different time stamp from the one which
2612 * was recorded in the file index.
2614 timeout.tv_sec = 0;
2615 timeout.tv_nsec = 1;
2616 nanosleep(&timeout, NULL);
2617 done:
2618 if (new_index)
2619 fclose(new_index);
2620 free(new_fileindex_path);
2621 return err;
2624 static const struct got_error *
2625 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2626 struct got_object_id **tree_id, const char *wt_relpath,
2627 struct got_commit_object *base_commit, struct got_worktree *worktree,
2628 struct got_repository *repo)
2630 const struct got_error *err = NULL;
2631 struct got_object_id *id = NULL;
2632 char *in_repo_path = NULL;
2633 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2635 *entry_type = GOT_OBJ_TYPE_ANY;
2636 *tree_relpath = NULL;
2637 *tree_id = NULL;
2639 if (wt_relpath[0] == '\0') {
2640 /* Check out all files within the work tree. */
2641 *entry_type = GOT_OBJ_TYPE_TREE;
2642 *tree_relpath = strdup("");
2643 if (*tree_relpath == NULL) {
2644 err = got_error_from_errno("strdup");
2645 goto done;
2647 err = got_object_id_by_path(tree_id, repo, base_commit,
2648 worktree->path_prefix);
2649 if (err)
2650 goto done;
2651 return NULL;
2654 /* Check out a subset of files in the work tree. */
2656 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2657 is_root_wt ? "" : "/", wt_relpath) == -1) {
2658 err = got_error_from_errno("asprintf");
2659 goto done;
2662 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2663 if (err)
2664 goto done;
2666 free(in_repo_path);
2667 in_repo_path = NULL;
2669 err = got_object_get_type(entry_type, repo, id);
2670 if (err)
2671 goto done;
2673 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2674 /* Check out a single file. */
2675 if (strchr(wt_relpath, '/') == NULL) {
2676 /* Check out a single file in work tree's root dir. */
2677 in_repo_path = strdup(worktree->path_prefix);
2678 if (in_repo_path == NULL) {
2679 err = got_error_from_errno("strdup");
2680 goto done;
2682 *tree_relpath = strdup("");
2683 if (*tree_relpath == NULL) {
2684 err = got_error_from_errno("strdup");
2685 goto done;
2687 } else {
2688 /* Check out a single file in a subdirectory. */
2689 err = got_path_dirname(tree_relpath, wt_relpath);
2690 if (err)
2691 return err;
2692 if (asprintf(&in_repo_path, "%s%s%s",
2693 worktree->path_prefix, is_root_wt ? "" : "/",
2694 *tree_relpath) == -1) {
2695 err = got_error_from_errno("asprintf");
2696 goto done;
2699 err = got_object_id_by_path(tree_id, repo,
2700 base_commit, in_repo_path);
2701 } else {
2702 /* Check out all files within a subdirectory. */
2703 *tree_id = got_object_id_dup(id);
2704 if (*tree_id == NULL) {
2705 err = got_error_from_errno("got_object_id_dup");
2706 goto done;
2708 *tree_relpath = strdup(wt_relpath);
2709 if (*tree_relpath == NULL) {
2710 err = got_error_from_errno("strdup");
2711 goto done;
2714 done:
2715 free(id);
2716 free(in_repo_path);
2717 if (err) {
2718 *entry_type = GOT_OBJ_TYPE_ANY;
2719 free(*tree_relpath);
2720 *tree_relpath = NULL;
2721 free(*tree_id);
2722 *tree_id = NULL;
2724 return err;
2727 static const struct got_error *
2728 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2729 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2731 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2733 const struct got_error *err = NULL;
2734 struct got_commit_object *commit = NULL;
2735 struct got_tree_object *tree = NULL;
2736 struct got_fileindex_diff_tree_cb diff_cb;
2737 struct diff_cb_arg arg;
2739 err = ref_base_commit(worktree, repo);
2740 if (err) {
2741 if (!(err->code == GOT_ERR_ERRNO &&
2742 (errno == EACCES || errno == EROFS)))
2743 goto done;
2744 err = (*progress_cb)(progress_arg,
2745 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2746 if (err)
2747 return err;
2750 err = got_object_open_as_commit(&commit, repo,
2751 worktree->base_commit_id);
2752 if (err)
2753 goto done;
2755 err = got_object_open_as_tree(&tree, repo, tree_id);
2756 if (err)
2757 goto done;
2759 if (entry_name &&
2760 got_object_tree_find_entry(tree, entry_name) == NULL) {
2761 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2762 goto done;
2765 diff_cb.diff_old_new = diff_old_new;
2766 diff_cb.diff_old = diff_old;
2767 diff_cb.diff_new = diff_new;
2768 arg.fileindex = fileindex;
2769 arg.worktree = worktree;
2770 arg.repo = repo;
2771 arg.progress_cb = progress_cb;
2772 arg.progress_arg = progress_arg;
2773 arg.cancel_cb = cancel_cb;
2774 arg.cancel_arg = cancel_arg;
2775 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2776 entry_name, repo, &diff_cb, &arg);
2777 done:
2778 if (tree)
2779 got_object_tree_close(tree);
2780 if (commit)
2781 got_object_commit_close(commit);
2782 return err;
2785 const struct got_error *
2786 got_worktree_checkout_files(struct got_worktree *worktree,
2787 struct got_pathlist_head *paths, struct got_repository *repo,
2788 got_worktree_checkout_cb progress_cb, void *progress_arg,
2789 got_cancel_cb cancel_cb, void *cancel_arg)
2791 const struct got_error *err = NULL, *sync_err, *unlockerr;
2792 struct got_commit_object *commit = NULL;
2793 struct got_tree_object *tree = NULL;
2794 struct got_fileindex *fileindex = NULL;
2795 char *fileindex_path = NULL;
2796 struct got_pathlist_entry *pe;
2797 struct tree_path_data {
2798 STAILQ_ENTRY(tree_path_data) entry;
2799 struct got_object_id *tree_id;
2800 int entry_type;
2801 char *relpath;
2802 char *entry_name;
2803 } *tpd = NULL;
2804 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2806 STAILQ_INIT(&tree_paths);
2808 err = lock_worktree(worktree, LOCK_EX);
2809 if (err)
2810 return err;
2812 err = got_object_open_as_commit(&commit, repo,
2813 worktree->base_commit_id);
2814 if (err)
2815 goto done;
2817 /* Map all specified paths to in-repository trees. */
2818 TAILQ_FOREACH(pe, paths, entry) {
2819 tpd = malloc(sizeof(*tpd));
2820 if (tpd == NULL) {
2821 err = got_error_from_errno("malloc");
2822 goto done;
2825 err = find_tree_entry_for_checkout(&tpd->entry_type,
2826 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2827 worktree, repo);
2828 if (err) {
2829 free(tpd);
2830 goto done;
2833 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2834 err = got_path_basename(&tpd->entry_name, pe->path);
2835 if (err) {
2836 free(tpd->relpath);
2837 free(tpd->tree_id);
2838 free(tpd);
2839 goto done;
2841 } else
2842 tpd->entry_name = NULL;
2844 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2848 * Read the file index.
2849 * Checking out files is supposed to be an idempotent operation.
2850 * If the on-disk file index is incomplete we will try to complete it.
2852 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2853 if (err)
2854 goto done;
2856 tpd = STAILQ_FIRST(&tree_paths);
2857 TAILQ_FOREACH(pe, paths, entry) {
2858 struct bump_base_commit_id_arg bbc_arg;
2860 err = checkout_files(worktree, fileindex, tpd->relpath,
2861 tpd->tree_id, tpd->entry_name, repo,
2862 progress_cb, progress_arg, cancel_cb, cancel_arg);
2863 if (err)
2864 break;
2866 bbc_arg.base_commit_id = worktree->base_commit_id;
2867 bbc_arg.entry_name = tpd->entry_name;
2868 bbc_arg.path = pe->path;
2869 bbc_arg.path_len = pe->path_len;
2870 bbc_arg.progress_cb = progress_cb;
2871 bbc_arg.progress_arg = progress_arg;
2872 err = got_fileindex_for_each_entry_safe(fileindex,
2873 bump_base_commit_id, &bbc_arg);
2874 if (err)
2875 break;
2877 tpd = STAILQ_NEXT(tpd, entry);
2879 sync_err = sync_fileindex(fileindex, fileindex_path);
2880 if (sync_err && err == NULL)
2881 err = sync_err;
2882 done:
2883 free(fileindex_path);
2884 if (tree)
2885 got_object_tree_close(tree);
2886 if (commit)
2887 got_object_commit_close(commit);
2888 if (fileindex)
2889 got_fileindex_free(fileindex);
2890 while (!STAILQ_EMPTY(&tree_paths)) {
2891 tpd = STAILQ_FIRST(&tree_paths);
2892 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2893 free(tpd->relpath);
2894 free(tpd->tree_id);
2895 free(tpd);
2897 unlockerr = lock_worktree(worktree, LOCK_SH);
2898 if (unlockerr && err == NULL)
2899 err = unlockerr;
2900 return err;
2903 static const struct got_error *
2904 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2905 struct got_fileindex_entry *ie, const char *ondisk_path,
2906 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2907 int restoring_missing_file, int reverting_versioned_file,
2908 int path_is_unversioned, int allow_bad_symlinks,
2909 struct got_repository *repo,
2910 got_worktree_checkout_cb progress_cb, void *progress_arg)
2912 const struct got_error *err = NULL;
2913 int is_bad_symlink = 0;
2915 if (S_ISLNK(mode2)) {
2916 err = install_symlink(&is_bad_symlink,
2917 worktree, ondisk_path, path2, blob2,
2918 restoring_missing_file,
2919 reverting_versioned_file,
2920 path_is_unversioned, allow_bad_symlinks,
2921 repo, progress_cb, progress_arg);
2922 } else {
2923 err = install_blob(worktree, ondisk_path, path2,
2924 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2925 restoring_missing_file, reverting_versioned_file, 0,
2926 path_is_unversioned, repo, progress_cb, progress_arg);
2928 if (err)
2929 return err;
2930 if (ie == NULL) {
2931 /* Adding an unversioned file. */
2932 err = got_fileindex_entry_alloc(&ie, path2);
2933 if (err)
2934 return err;
2935 err = got_fileindex_entry_update(ie,
2936 worktree->root_fd, path2, NULL, NULL, 1);
2937 if (err) {
2938 got_fileindex_entry_free(ie);
2939 return err;
2941 err = got_fileindex_entry_add(fileindex, ie);
2942 if (err) {
2943 got_fileindex_entry_free(ie);
2944 return err;
2946 } else {
2947 /* Re-adding a locally deleted file. */
2948 err = got_fileindex_entry_update(ie,
2949 worktree->root_fd, path2, ie->blob_sha1,
2950 worktree->base_commit_id->sha1, 0);
2951 if (err)
2952 return err;
2955 if (is_bad_symlink) {
2956 got_fileindex_entry_filetype_set(ie,
2957 GOT_FILEIDX_MODE_BAD_SYMLINK);
2960 return NULL;
2963 struct merge_file_cb_arg {
2964 struct got_worktree *worktree;
2965 struct got_fileindex *fileindex;
2966 got_worktree_checkout_cb progress_cb;
2967 void *progress_arg;
2968 got_cancel_cb cancel_cb;
2969 void *cancel_arg;
2970 const char *label_orig;
2971 struct got_object_id *commit_id2;
2972 int allow_bad_symlinks;
2975 static const struct got_error *
2976 merge_file_cb(void *arg, struct got_blob_object *blob1,
2977 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2978 struct got_object_id *id1, struct got_object_id *id2,
2979 const char *path1, const char *path2,
2980 mode_t mode1, mode_t mode2, struct got_repository *repo)
2982 static const struct got_error *err = NULL;
2983 struct merge_file_cb_arg *a = arg;
2984 struct got_fileindex_entry *ie;
2985 char *ondisk_path = NULL;
2986 struct stat sb;
2987 unsigned char status;
2988 int local_changes_subsumed;
2989 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2990 char *id_str = NULL, *label_deriv2 = NULL;
2992 if (blob1 && blob2) {
2993 ie = got_fileindex_entry_get(a->fileindex, path2,
2994 strlen(path2));
2995 if (ie == NULL)
2996 return (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_MISSING, path2);
2999 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3000 path2) == -1)
3001 return got_error_from_errno("asprintf");
3003 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3004 repo);
3005 if (err)
3006 goto done;
3008 if (status == GOT_STATUS_DELETE) {
3009 err = (*a->progress_cb)(a->progress_arg,
3010 GOT_STATUS_MERGE, path2);
3011 goto done;
3013 if (status != GOT_STATUS_NO_CHANGE &&
3014 status != GOT_STATUS_MODIFY &&
3015 status != GOT_STATUS_CONFLICT &&
3016 status != GOT_STATUS_ADD) {
3017 err = (*a->progress_cb)(a->progress_arg, status, path2);
3018 goto done;
3021 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3022 char *link_target2;
3023 err = got_object_blob_read_to_str(&link_target2, blob2);
3024 if (err)
3025 goto done;
3026 err = merge_symlink(a->worktree, blob1, ondisk_path,
3027 path2, a->label_orig, link_target2, a->commit_id2,
3028 repo, a->progress_cb, a->progress_arg);
3029 free(link_target2);
3030 } else {
3031 int fd;
3033 f_orig = got_opentemp();
3034 if (f_orig == NULL) {
3035 err = got_error_from_errno("got_opentemp");
3036 goto done;
3038 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3039 f_orig, blob1);
3040 if (err)
3041 goto done;
3043 f_deriv2 = got_opentemp();
3044 if (f_deriv2 == NULL)
3045 goto done;
3046 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3047 f_deriv2, blob2);
3048 if (err)
3049 goto done;
3051 fd = open(ondisk_path,
3052 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3053 if (fd == -1) {
3054 err = got_error_from_errno2("open",
3055 ondisk_path);
3056 goto done;
3058 f_deriv = fdopen(fd, "r");
3059 if (f_deriv == NULL) {
3060 err = got_error_from_errno2("fdopen",
3061 ondisk_path);
3062 close(fd);
3063 goto done;
3065 err = got_object_id_str(&id_str, a->commit_id2);
3066 if (err)
3067 goto done;
3068 if (asprintf(&label_deriv2, "%s: commit %s",
3069 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3070 err = got_error_from_errno("asprintf");
3071 goto done;
3073 err = merge_file(&local_changes_subsumed, a->worktree,
3074 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3075 mode2, a->label_orig, NULL, label_deriv2,
3076 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3077 a->progress_cb, a->progress_arg);
3079 } else if (blob1) {
3080 ie = got_fileindex_entry_get(a->fileindex, path1,
3081 strlen(path1));
3082 if (ie == NULL)
3083 return (*a->progress_cb)(a->progress_arg,
3084 GOT_STATUS_MISSING, path1);
3086 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3087 path1) == -1)
3088 return got_error_from_errno("asprintf");
3090 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3091 repo);
3092 if (err)
3093 goto done;
3095 switch (status) {
3096 case GOT_STATUS_NO_CHANGE:
3097 err = (*a->progress_cb)(a->progress_arg,
3098 GOT_STATUS_DELETE, path1);
3099 if (err)
3100 goto done;
3101 err = remove_ondisk_file(a->worktree->root_path, path1);
3102 if (err)
3103 goto done;
3104 if (ie)
3105 got_fileindex_entry_mark_deleted_from_disk(ie);
3106 break;
3107 case GOT_STATUS_DELETE:
3108 case GOT_STATUS_MISSING:
3109 err = (*a->progress_cb)(a->progress_arg,
3110 GOT_STATUS_DELETE, path1);
3111 if (err)
3112 goto done;
3113 if (ie)
3114 got_fileindex_entry_mark_deleted_from_disk(ie);
3115 break;
3116 case GOT_STATUS_ADD: {
3117 struct got_object_id *id;
3118 FILE *blob1_f;
3119 off_t blob1_size;
3121 * Delete the added file only if its content already
3122 * exists in the repository.
3124 err = got_object_blob_file_create(&id, &blob1_f,
3125 &blob1_size, path1);
3126 if (err)
3127 goto done;
3128 if (got_object_id_cmp(id, id1) == 0) {
3129 err = (*a->progress_cb)(a->progress_arg,
3130 GOT_STATUS_DELETE, path1);
3131 if (err)
3132 goto done;
3133 err = remove_ondisk_file(a->worktree->root_path,
3134 path1);
3135 if (err)
3136 goto done;
3137 if (ie)
3138 got_fileindex_entry_remove(a->fileindex,
3139 ie);
3140 } else {
3141 err = (*a->progress_cb)(a->progress_arg,
3142 GOT_STATUS_CANNOT_DELETE, path1);
3144 if (fclose(blob1_f) == EOF && err == NULL)
3145 err = got_error_from_errno("fclose");
3146 free(id);
3147 if (err)
3148 goto done;
3149 break;
3151 case GOT_STATUS_MODIFY:
3152 case GOT_STATUS_CONFLICT:
3153 err = (*a->progress_cb)(a->progress_arg,
3154 GOT_STATUS_CANNOT_DELETE, path1);
3155 if (err)
3156 goto done;
3157 break;
3158 case GOT_STATUS_OBSTRUCTED:
3159 err = (*a->progress_cb)(a->progress_arg, status, path1);
3160 if (err)
3161 goto done;
3162 break;
3163 default:
3164 break;
3166 } else if (blob2) {
3167 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3168 path2) == -1)
3169 return got_error_from_errno("asprintf");
3170 ie = got_fileindex_entry_get(a->fileindex, path2,
3171 strlen(path2));
3172 if (ie) {
3173 err = get_file_status(&status, &sb, ie, ondisk_path,
3174 -1, NULL, repo);
3175 if (err)
3176 goto done;
3177 if (status != GOT_STATUS_NO_CHANGE &&
3178 status != GOT_STATUS_MODIFY &&
3179 status != GOT_STATUS_CONFLICT &&
3180 status != GOT_STATUS_ADD &&
3181 status != GOT_STATUS_DELETE) {
3182 err = (*a->progress_cb)(a->progress_arg,
3183 status, path2);
3184 goto done;
3186 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3187 char *link_target2;
3188 err = got_object_blob_read_to_str(&link_target2,
3189 blob2);
3190 if (err)
3191 goto done;
3192 err = merge_symlink(a->worktree, NULL,
3193 ondisk_path, path2, a->label_orig,
3194 link_target2, a->commit_id2, repo,
3195 a->progress_cb, a->progress_arg);
3196 free(link_target2);
3197 } else if (S_ISREG(sb.st_mode)) {
3198 err = merge_blob(&local_changes_subsumed,
3199 a->worktree, NULL, ondisk_path, path2,
3200 sb.st_mode, a->label_orig, blob2,
3201 a->commit_id2, repo, a->progress_cb,
3202 a->progress_arg);
3203 } else if (status != GOT_STATUS_DELETE) {
3204 err = got_error_path(ondisk_path,
3205 GOT_ERR_FILE_OBSTRUCTED);
3207 if (err)
3208 goto done;
3209 if (status == GOT_STATUS_DELETE) {
3210 /* Re-add file with content from new blob. */
3211 err = add_file(a->worktree, a->fileindex, ie,
3212 ondisk_path, path2, blob2, mode2,
3213 0, 0, 0, a->allow_bad_symlinks,
3214 repo, a->progress_cb, a->progress_arg);
3215 if (err)
3216 goto done;
3218 } else {
3219 err = add_file(a->worktree, a->fileindex, NULL,
3220 ondisk_path, path2, blob2, mode2,
3221 0, 0, 1, a->allow_bad_symlinks,
3222 repo, a->progress_cb, a->progress_arg);
3223 if (err)
3224 goto done;
3227 done:
3228 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3229 err = got_error_from_errno("fclose");
3230 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3231 err = got_error_from_errno("fclose");
3232 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3233 err = got_error_from_errno("fclose");
3234 free(id_str);
3235 free(label_deriv2);
3236 free(ondisk_path);
3237 return err;
3240 struct check_mixed_commits_args {
3241 struct got_worktree *worktree;
3242 got_cancel_cb cancel_cb;
3243 void *cancel_arg;
3246 static const struct got_error *
3247 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3249 const struct got_error *err;
3250 struct check_mixed_commits_args *a = arg;
3252 if (a->cancel_cb) {
3253 err = a->cancel_cb(a->cancel_arg);
3254 if (err)
3255 return err;
3258 /* Reject merges into a work tree with mixed base commits. */
3259 if (got_fileindex_entry_has_commit(ie) &&
3260 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3261 SHA1_DIGEST_LENGTH) != 0)
3262 return got_error(GOT_ERR_MIXED_COMMITS);
3264 return NULL;
3267 const struct got_error *
3268 got_worktree_get_state(char *state, struct got_repository *repo,
3269 struct got_worktree *worktree,
3270 got_cancel_cb cancel_cb, void *cancel_arg)
3272 const struct got_error *err;
3273 struct got_object_id *base_id, *head_id = NULL;
3274 struct got_reference *head_ref;
3275 struct got_fileindex *fileindex = NULL;
3276 char *fileindex_path = NULL;
3277 struct check_mixed_commits_args cma;
3279 if (worktree == NULL)
3280 return got_error(GOT_ERR_NOT_WORKTREE);
3282 err = got_ref_open(&head_ref, repo,
3283 got_worktree_get_head_ref_name(worktree), 0);
3284 if (err)
3285 return err;
3287 err = got_ref_resolve(&head_id, repo, head_ref);
3288 if (err)
3289 goto done;
3291 *state = GOT_WORKTREE_STATE_UNKNOWN;
3292 base_id = got_worktree_get_base_commit_id(worktree);
3294 cma.worktree = worktree;
3295 cma.cancel_cb = cancel_cb;
3296 cma.cancel_arg = cancel_arg;
3298 if (got_object_id_cmp(base_id, head_id) == 0) {
3299 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3300 if (err)
3301 goto done;
3303 err = got_fileindex_for_each_entry_safe(fileindex,
3304 check_mixed_commits, &cma);
3305 if (err == NULL)
3306 *state = GOT_WORKTREE_STATE_UPTODATE;
3307 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3308 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3309 err = NULL;
3311 } else
3312 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3314 done:
3315 free(head_id);
3316 free(fileindex_path);
3317 got_ref_close(head_ref);
3318 if (fileindex != NULL)
3319 got_fileindex_free(fileindex);
3320 return err;
3323 struct check_merge_conflicts_arg {
3324 struct got_worktree *worktree;
3325 struct got_fileindex *fileindex;
3326 struct got_repository *repo;
3329 static const struct got_error *
3330 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3331 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3332 struct got_object_id *id1, struct got_object_id *id2,
3333 const char *path1, const char *path2,
3334 mode_t mode1, mode_t mode2, struct got_repository *repo)
3336 const struct got_error *err = NULL;
3337 struct check_merge_conflicts_arg *a = arg;
3338 unsigned char status;
3339 struct stat sb;
3340 struct got_fileindex_entry *ie;
3341 const char *path = path2 ? path2 : path1;
3342 struct got_object_id *id = id2 ? id2 : id1;
3343 char *ondisk_path;
3345 if (id == NULL)
3346 return NULL;
3348 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3349 if (ie == NULL)
3350 return NULL;
3352 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3353 == -1)
3354 return got_error_from_errno("asprintf");
3356 /* Reject merges into a work tree with conflicted files. */
3357 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3358 free(ondisk_path);
3359 if (err)
3360 return err;
3361 if (status == GOT_STATUS_CONFLICT)
3362 return got_error(GOT_ERR_CONFLICTS);
3364 return NULL;
3367 static const struct got_error *
3368 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3369 const char *fileindex_path, struct got_object_id *commit_id1,
3370 struct got_object_id *commit_id2, struct got_repository *repo,
3371 got_worktree_checkout_cb progress_cb, void *progress_arg,
3372 got_cancel_cb cancel_cb, void *cancel_arg)
3374 const struct got_error *err = NULL, *sync_err;
3375 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3376 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3377 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3378 struct check_merge_conflicts_arg cmc_arg;
3379 struct merge_file_cb_arg arg;
3380 char *label_orig = NULL;
3381 FILE *f1 = NULL, *f2 = NULL;
3382 int fd1 = -1, fd2 = -1;
3384 if (commit_id1) {
3385 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3386 if (err)
3387 goto done;
3388 err = got_object_id_by_path(&tree_id1, repo, commit1,
3389 worktree->path_prefix);
3390 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3391 goto done;
3393 if (tree_id1) {
3394 char *id_str;
3396 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3397 if (err)
3398 goto done;
3400 err = got_object_id_str(&id_str, commit_id1);
3401 if (err)
3402 goto done;
3404 if (asprintf(&label_orig, "%s: commit %s",
3405 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3406 err = got_error_from_errno("asprintf");
3407 free(id_str);
3408 goto done;
3410 free(id_str);
3412 f1 = got_opentemp();
3413 if (f1 == NULL) {
3414 err = got_error_from_errno("got_opentemp");
3415 goto done;
3419 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3420 if (err)
3421 goto done;
3423 err = got_object_id_by_path(&tree_id2, repo, commit2,
3424 worktree->path_prefix);
3425 if (err)
3426 goto done;
3428 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3429 if (err)
3430 goto done;
3432 f2 = got_opentemp();
3433 if (f2 == NULL) {
3434 err = got_error_from_errno("got_opentemp");
3435 goto done;
3438 fd1 = got_opentempfd();
3439 if (fd1 == -1) {
3440 err = got_error_from_errno("got_opentempfd");
3441 goto done;
3444 fd2 = got_opentempfd();
3445 if (fd2 == -1) {
3446 err = got_error_from_errno("got_opentempfd");
3447 goto done;
3450 cmc_arg.worktree = worktree;
3451 cmc_arg.fileindex = fileindex;
3452 cmc_arg.repo = repo;
3453 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3454 check_merge_conflicts, &cmc_arg, 0);
3455 if (err)
3456 goto done;
3458 arg.worktree = worktree;
3459 arg.fileindex = fileindex;
3460 arg.progress_cb = progress_cb;
3461 arg.progress_arg = progress_arg;
3462 arg.cancel_cb = cancel_cb;
3463 arg.cancel_arg = cancel_arg;
3464 arg.label_orig = label_orig;
3465 arg.commit_id2 = commit_id2;
3466 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3467 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3468 merge_file_cb, &arg, 1);
3469 sync_err = sync_fileindex(fileindex, fileindex_path);
3470 if (sync_err && err == NULL)
3471 err = sync_err;
3472 done:
3473 if (commit1)
3474 got_object_commit_close(commit1);
3475 if (commit2)
3476 got_object_commit_close(commit2);
3477 if (tree1)
3478 got_object_tree_close(tree1);
3479 if (tree2)
3480 got_object_tree_close(tree2);
3481 if (f1 && fclose(f1) == EOF && err == NULL)
3482 err = got_error_from_errno("fclose");
3483 if (f2 && fclose(f2) == EOF && err == NULL)
3484 err = got_error_from_errno("fclose");
3485 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3486 err = got_error_from_errno("close");
3487 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3488 err = got_error_from_errno("close");
3489 free(label_orig);
3490 return err;
3493 const struct got_error *
3494 got_worktree_merge_files(struct got_worktree *worktree,
3495 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3496 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3497 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3499 const struct got_error *err, *unlockerr;
3500 char *fileindex_path = NULL;
3501 struct got_fileindex *fileindex = NULL;
3502 struct check_mixed_commits_args cma;
3504 err = lock_worktree(worktree, LOCK_EX);
3505 if (err)
3506 return err;
3508 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3509 if (err)
3510 goto done;
3512 cma.worktree = worktree;
3513 cma.cancel_cb = cancel_cb;
3514 cma.cancel_arg = cancel_arg;
3516 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3517 &cma);
3518 if (err)
3519 goto done;
3521 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3522 commit_id2, repo, progress_cb, progress_arg,
3523 cancel_cb, cancel_arg);
3524 done:
3525 if (fileindex)
3526 got_fileindex_free(fileindex);
3527 free(fileindex_path);
3528 unlockerr = lock_worktree(worktree, LOCK_SH);
3529 if (unlockerr && err == NULL)
3530 err = unlockerr;
3531 return err;
3534 struct diff_dir_cb_arg {
3535 struct got_fileindex *fileindex;
3536 struct got_worktree *worktree;
3537 const char *status_path;
3538 size_t status_path_len;
3539 struct got_repository *repo;
3540 got_worktree_status_cb status_cb;
3541 void *status_arg;
3542 got_cancel_cb cancel_cb;
3543 void *cancel_arg;
3544 /* A pathlist containing per-directory pathlists of ignore patterns. */
3545 struct got_pathlist_head *ignores;
3546 int report_unchanged;
3547 int no_ignores;
3550 static const struct got_error *
3551 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3552 int dirfd, const char *de_name,
3553 got_worktree_status_cb status_cb, void *status_arg,
3554 struct got_repository *repo, int report_unchanged)
3556 const struct got_error *err = NULL;
3557 unsigned char status = GOT_STATUS_NO_CHANGE;
3558 unsigned char staged_status;
3559 struct stat sb;
3560 struct got_object_id blob_id, commit_id, staged_blob_id;
3561 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3562 struct got_object_id *staged_blob_idp = NULL;
3564 staged_status = get_staged_status(ie);
3565 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3566 if (err)
3567 return err;
3569 if (status == GOT_STATUS_NO_CHANGE &&
3570 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3571 return NULL;
3573 if (got_fileindex_entry_has_blob(ie))
3574 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3575 if (got_fileindex_entry_has_commit(ie))
3576 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3577 if (staged_status == GOT_STATUS_ADD ||
3578 staged_status == GOT_STATUS_MODIFY) {
3579 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3580 &staged_blob_id, ie);
3583 return (*status_cb)(status_arg, status, staged_status,
3584 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3587 static const struct got_error *
3588 status_old_new(void *arg, struct got_fileindex_entry *ie,
3589 struct dirent *de, const char *parent_path, int dirfd)
3591 const struct got_error *err = NULL;
3592 struct diff_dir_cb_arg *a = arg;
3593 char *abspath;
3595 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3596 return got_error(GOT_ERR_CANCELLED);
3598 if (got_path_cmp(parent_path, a->status_path,
3599 strlen(parent_path), a->status_path_len) != 0 &&
3600 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3601 return NULL;
3603 if (parent_path[0]) {
3604 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3605 parent_path, de->d_name) == -1)
3606 return got_error_from_errno("asprintf");
3607 } else {
3608 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3609 de->d_name) == -1)
3610 return got_error_from_errno("asprintf");
3613 err = report_file_status(ie, abspath, dirfd, de->d_name,
3614 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3615 free(abspath);
3616 return err;
3619 static const struct got_error *
3620 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3622 struct diff_dir_cb_arg *a = arg;
3623 struct got_object_id blob_id, commit_id;
3624 unsigned char status;
3626 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3627 return got_error(GOT_ERR_CANCELLED);
3629 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3630 return NULL;
3632 got_fileindex_entry_get_blob_id(&blob_id, ie);
3633 got_fileindex_entry_get_commit_id(&commit_id, ie);
3634 if (got_fileindex_entry_has_file_on_disk(ie))
3635 status = GOT_STATUS_MISSING;
3636 else
3637 status = GOT_STATUS_DELETE;
3638 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3639 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3642 static void
3643 free_ignores(struct got_pathlist_head *ignores)
3645 struct got_pathlist_entry *pe;
3647 TAILQ_FOREACH(pe, ignores, entry) {
3648 struct got_pathlist_head *ignorelist = pe->data;
3650 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3652 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3655 static const struct got_error *
3656 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3658 const struct got_error *err = NULL;
3659 struct got_pathlist_entry *pe = NULL;
3660 struct got_pathlist_head *ignorelist;
3661 char *line = NULL, *pattern, *dirpath = NULL;
3662 size_t linesize = 0;
3663 ssize_t linelen;
3665 ignorelist = calloc(1, sizeof(*ignorelist));
3666 if (ignorelist == NULL)
3667 return got_error_from_errno("calloc");
3668 TAILQ_INIT(ignorelist);
3670 while ((linelen = getline(&line, &linesize, f)) != -1) {
3671 if (linelen > 0 && line[linelen - 1] == '\n')
3672 line[linelen - 1] = '\0';
3674 /* Git's ignores may contain comments. */
3675 if (line[0] == '#')
3676 continue;
3678 /* Git's negated patterns are not (yet?) supported. */
3679 if (line[0] == '!')
3680 continue;
3682 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3683 line) == -1) {
3684 err = got_error_from_errno("asprintf");
3685 goto done;
3687 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3688 if (err)
3689 goto done;
3691 if (ferror(f)) {
3692 err = got_error_from_errno("getline");
3693 goto done;
3696 dirpath = strdup(path);
3697 if (dirpath == NULL) {
3698 err = got_error_from_errno("strdup");
3699 goto done;
3701 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3702 done:
3703 free(line);
3704 if (err || pe == NULL) {
3705 free(dirpath);
3706 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3708 return err;
3711 static int
3712 match_path(const char *pattern, size_t pattern_len, const char *path,
3713 int flags)
3715 char buf[PATH_MAX];
3718 * Trailing slashes signify directories.
3719 * Append a * to make such patterns conform to fnmatch rules.
3721 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3722 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3723 return FNM_NOMATCH; /* XXX */
3725 return fnmatch(buf, path, flags);
3728 return fnmatch(pattern, path, flags);
3731 static int
3732 match_ignores(struct got_pathlist_head *ignores, const char *path)
3734 struct got_pathlist_entry *pe;
3736 /* Handle patterns which match in all directories. */
3737 TAILQ_FOREACH(pe, ignores, entry) {
3738 struct got_pathlist_head *ignorelist = pe->data;
3739 struct got_pathlist_entry *pi;
3741 TAILQ_FOREACH(pi, ignorelist, entry) {
3742 const char *p;
3744 if (pi->path_len < 3 ||
3745 strncmp(pi->path, "**/", 3) != 0)
3746 continue;
3747 p = path;
3748 while (*p) {
3749 if (match_path(pi->path + 3,
3750 pi->path_len - 3, p,
3751 FNM_PATHNAME | FNM_LEADING_DIR)) {
3752 /* Retry in next directory. */
3753 while (*p && *p != '/')
3754 p++;
3755 while (*p == '/')
3756 p++;
3757 continue;
3759 return 1;
3765 * The ignores pathlist contains ignore lists from children before
3766 * parents, so we can find the most specific ignorelist by walking
3767 * ignores backwards.
3769 pe = TAILQ_LAST(ignores, got_pathlist_head);
3770 while (pe) {
3771 if (got_path_is_child(path, pe->path, pe->path_len)) {
3772 struct got_pathlist_head *ignorelist = pe->data;
3773 struct got_pathlist_entry *pi;
3774 TAILQ_FOREACH(pi, ignorelist, entry) {
3775 int flags = FNM_LEADING_DIR;
3776 if (strstr(pi->path, "/**/") == NULL)
3777 flags |= FNM_PATHNAME;
3778 if (match_path(pi->path, pi->path_len,
3779 path, flags))
3780 continue;
3781 return 1;
3784 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3787 return 0;
3790 static const struct got_error *
3791 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3792 const char *path, int dirfd, const char *ignores_filename)
3794 const struct got_error *err = NULL;
3795 char *ignorespath;
3796 int fd = -1;
3797 FILE *ignoresfile = NULL;
3799 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3800 path[0] ? "/" : "", ignores_filename) == -1)
3801 return got_error_from_errno("asprintf");
3803 if (dirfd != -1) {
3804 fd = openat(dirfd, ignores_filename,
3805 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3806 if (fd == -1) {
3807 if (errno != ENOENT && errno != EACCES)
3808 err = got_error_from_errno2("openat",
3809 ignorespath);
3810 } else {
3811 ignoresfile = fdopen(fd, "r");
3812 if (ignoresfile == NULL)
3813 err = got_error_from_errno2("fdopen",
3814 ignorespath);
3815 else {
3816 fd = -1;
3817 err = read_ignores(ignores, path, ignoresfile);
3820 } else {
3821 ignoresfile = fopen(ignorespath, "re");
3822 if (ignoresfile == NULL) {
3823 if (errno != ENOENT && errno != EACCES)
3824 err = got_error_from_errno2("fopen",
3825 ignorespath);
3826 } else
3827 err = read_ignores(ignores, path, ignoresfile);
3830 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3831 err = got_error_from_errno2("fclose", path);
3832 if (fd != -1 && close(fd) == -1 && err == NULL)
3833 err = got_error_from_errno2("close", path);
3834 free(ignorespath);
3835 return err;
3838 static const struct got_error *
3839 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3840 int dirfd)
3842 const struct got_error *err = NULL;
3843 struct diff_dir_cb_arg *a = arg;
3844 char *path = NULL;
3846 if (ignore != NULL)
3847 *ignore = 0;
3849 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3850 return got_error(GOT_ERR_CANCELLED);
3852 if (parent_path[0]) {
3853 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3854 return got_error_from_errno("asprintf");
3855 } else {
3856 path = de->d_name;
3859 if (de->d_type == DT_DIR) {
3860 if (!a->no_ignores && ignore != NULL &&
3861 match_ignores(a->ignores, path))
3862 *ignore = 1;
3863 } else if (!match_ignores(a->ignores, path) &&
3864 got_path_is_child(path, a->status_path, a->status_path_len))
3865 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3866 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3867 if (parent_path[0])
3868 free(path);
3869 return err;
3872 static const struct got_error *
3873 status_traverse(void *arg, const char *path, int dirfd)
3875 const struct got_error *err = NULL;
3876 struct diff_dir_cb_arg *a = arg;
3878 if (a->no_ignores)
3879 return NULL;
3881 err = add_ignores(a->ignores, a->worktree->root_path,
3882 path, dirfd, ".cvsignore");
3883 if (err)
3884 return err;
3886 err = add_ignores(a->ignores, a->worktree->root_path, path,
3887 dirfd, ".gitignore");
3889 return err;
3892 static const struct got_error *
3893 report_single_file_status(const char *path, const char *ondisk_path,
3894 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3895 void *status_arg, struct got_repository *repo, int report_unchanged,
3896 struct got_pathlist_head *ignores, int no_ignores)
3898 struct got_fileindex_entry *ie;
3899 struct stat sb;
3901 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3902 if (ie)
3903 return report_file_status(ie, ondisk_path, -1, NULL,
3904 status_cb, status_arg, repo, report_unchanged);
3906 if (lstat(ondisk_path, &sb) == -1) {
3907 if (errno != ENOENT)
3908 return got_error_from_errno2("lstat", ondisk_path);
3909 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3910 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3913 if (!no_ignores && match_ignores(ignores, path))
3914 return NULL;
3916 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3917 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3918 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3920 return NULL;
3923 static const struct got_error *
3924 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3925 const char *root_path, const char *path)
3927 const struct got_error *err;
3928 char *parent_path, *next_parent_path = NULL;
3930 err = add_ignores(ignores, root_path, "", -1,
3931 ".cvsignore");
3932 if (err)
3933 return err;
3935 err = add_ignores(ignores, root_path, "", -1,
3936 ".gitignore");
3937 if (err)
3938 return err;
3940 err = got_path_dirname(&parent_path, path);
3941 if (err) {
3942 if (err->code == GOT_ERR_BAD_PATH)
3943 return NULL; /* cannot traverse parent */
3944 return err;
3946 for (;;) {
3947 err = add_ignores(ignores, root_path, parent_path, -1,
3948 ".cvsignore");
3949 if (err)
3950 break;
3951 err = add_ignores(ignores, root_path, parent_path, -1,
3952 ".gitignore");
3953 if (err)
3954 break;
3955 err = got_path_dirname(&next_parent_path, parent_path);
3956 if (err) {
3957 if (err->code == GOT_ERR_BAD_PATH)
3958 err = NULL; /* traversed everything */
3959 break;
3961 if (got_path_is_root_dir(parent_path))
3962 break;
3963 free(parent_path);
3964 parent_path = next_parent_path;
3965 next_parent_path = NULL;
3968 free(parent_path);
3969 free(next_parent_path);
3970 return err;
3973 struct find_missing_children_args {
3974 const char *parent_path;
3975 size_t parent_len;
3976 struct got_pathlist_head *children;
3977 got_cancel_cb cancel_cb;
3978 void *cancel_arg;
3981 static const struct got_error *
3982 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3984 const struct got_error *err = NULL;
3985 struct find_missing_children_args *a = arg;
3987 if (a->cancel_cb) {
3988 err = a->cancel_cb(a->cancel_arg);
3989 if (err)
3990 return err;
3993 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3994 err = got_pathlist_append(a->children, ie->path, NULL);
3996 return err;
3999 static const struct got_error *
4000 report_children(struct got_pathlist_head *children,
4001 struct got_worktree *worktree, struct got_fileindex *fileindex,
4002 struct got_repository *repo, int is_root_dir, int report_unchanged,
4003 struct got_pathlist_head *ignores, int no_ignores,
4004 got_worktree_status_cb status_cb, void *status_arg,
4005 got_cancel_cb cancel_cb, void *cancel_arg)
4007 const struct got_error *err = NULL;
4008 struct got_pathlist_entry *pe;
4009 char *ondisk_path = NULL;
4011 TAILQ_FOREACH(pe, children, entry) {
4012 if (cancel_cb) {
4013 err = cancel_cb(cancel_arg);
4014 if (err)
4015 break;
4018 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4019 !is_root_dir ? "/" : "", pe->path) == -1) {
4020 err = got_error_from_errno("asprintf");
4021 ondisk_path = NULL;
4022 break;
4025 err = report_single_file_status(pe->path, ondisk_path,
4026 fileindex, status_cb, status_arg, repo, report_unchanged,
4027 ignores, no_ignores);
4028 if (err)
4029 break;
4031 free(ondisk_path);
4032 ondisk_path = NULL;
4035 free(ondisk_path);
4036 return err;
4039 static const struct got_error *
4040 worktree_status(struct got_worktree *worktree, const char *path,
4041 struct got_fileindex *fileindex, struct got_repository *repo,
4042 got_worktree_status_cb status_cb, void *status_arg,
4043 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4044 int report_unchanged)
4046 const struct got_error *err = NULL;
4047 int fd = -1;
4048 struct got_fileindex_diff_dir_cb fdiff_cb;
4049 struct diff_dir_cb_arg arg;
4050 char *ondisk_path = NULL;
4051 struct got_pathlist_head ignores, missing_children;
4052 struct got_fileindex_entry *ie;
4054 TAILQ_INIT(&ignores);
4055 TAILQ_INIT(&missing_children);
4057 if (asprintf(&ondisk_path, "%s%s%s",
4058 worktree->root_path, path[0] ? "/" : "", path) == -1)
4059 return got_error_from_errno("asprintf");
4061 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4062 if (ie) {
4063 err = report_single_file_status(path, ondisk_path,
4064 fileindex, status_cb, status_arg, repo,
4065 report_unchanged, &ignores, no_ignores);
4066 goto done;
4067 } else {
4068 struct find_missing_children_args fmca;
4069 fmca.parent_path = path;
4070 fmca.parent_len = strlen(path);
4071 fmca.children = &missing_children;
4072 fmca.cancel_cb = cancel_cb;
4073 fmca.cancel_arg = cancel_arg;
4074 err = got_fileindex_for_each_entry_safe(fileindex,
4075 find_missing_children, &fmca);
4076 if (err)
4077 goto done;
4080 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4081 if (fd == -1) {
4082 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4083 !got_err_open_nofollow_on_symlink())
4084 err = got_error_from_errno2("open", ondisk_path);
4085 else {
4086 if (!no_ignores) {
4087 err = add_ignores_from_parent_paths(&ignores,
4088 worktree->root_path, ondisk_path);
4089 if (err)
4090 goto done;
4092 if (TAILQ_EMPTY(&missing_children)) {
4093 err = report_single_file_status(path,
4094 ondisk_path, fileindex,
4095 status_cb, status_arg, repo,
4096 report_unchanged, &ignores, no_ignores);
4097 if (err)
4098 goto done;
4099 } else {
4100 err = report_children(&missing_children,
4101 worktree, fileindex, repo,
4102 (path[0] == '\0'), report_unchanged,
4103 &ignores, no_ignores,
4104 status_cb, status_arg,
4105 cancel_cb, cancel_arg);
4106 if (err)
4107 goto done;
4110 } else {
4111 fdiff_cb.diff_old_new = status_old_new;
4112 fdiff_cb.diff_old = status_old;
4113 fdiff_cb.diff_new = status_new;
4114 fdiff_cb.diff_traverse = status_traverse;
4115 arg.fileindex = fileindex;
4116 arg.worktree = worktree;
4117 arg.status_path = path;
4118 arg.status_path_len = strlen(path);
4119 arg.repo = repo;
4120 arg.status_cb = status_cb;
4121 arg.status_arg = status_arg;
4122 arg.cancel_cb = cancel_cb;
4123 arg.cancel_arg = cancel_arg;
4124 arg.report_unchanged = report_unchanged;
4125 arg.no_ignores = no_ignores;
4126 if (!no_ignores) {
4127 err = add_ignores_from_parent_paths(&ignores,
4128 worktree->root_path, path);
4129 if (err)
4130 goto done;
4132 arg.ignores = &ignores;
4133 err = got_fileindex_diff_dir(fileindex, fd,
4134 worktree->root_path, path, repo, &fdiff_cb, &arg);
4136 done:
4137 free_ignores(&ignores);
4138 if (fd != -1 && close(fd) == -1 && err == NULL)
4139 err = got_error_from_errno("close");
4140 free(ondisk_path);
4141 return err;
4144 const struct got_error *
4145 got_worktree_status(struct got_worktree *worktree,
4146 struct got_pathlist_head *paths, struct got_repository *repo,
4147 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4148 got_cancel_cb cancel_cb, void *cancel_arg)
4150 const struct got_error *err = NULL;
4151 char *fileindex_path = NULL;
4152 struct got_fileindex *fileindex = NULL;
4153 struct got_pathlist_entry *pe;
4155 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4156 if (err)
4157 return err;
4159 TAILQ_FOREACH(pe, paths, entry) {
4160 err = worktree_status(worktree, pe->path, fileindex, repo,
4161 status_cb, status_arg, cancel_cb, cancel_arg,
4162 no_ignores, 0);
4163 if (err)
4164 break;
4166 free(fileindex_path);
4167 got_fileindex_free(fileindex);
4168 return err;
4171 const struct got_error *
4172 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4173 const char *arg)
4175 const struct got_error *err = NULL;
4176 char *resolved = NULL, *cwd = NULL, *path = NULL;
4177 size_t len;
4178 struct stat sb;
4179 char *abspath = NULL;
4180 char canonpath[PATH_MAX];
4182 *wt_path = NULL;
4184 cwd = getcwd(NULL, 0);
4185 if (cwd == NULL)
4186 return got_error_from_errno("getcwd");
4188 if (lstat(arg, &sb) == -1) {
4189 if (errno != ENOENT) {
4190 err = got_error_from_errno2("lstat", arg);
4191 goto done;
4193 sb.st_mode = 0;
4195 if (S_ISLNK(sb.st_mode)) {
4197 * We cannot use realpath(3) with symlinks since we want to
4198 * operate on the symlink itself.
4199 * But we can make the path absolute, assuming it is relative
4200 * to the current working directory, and then canonicalize it.
4202 if (!got_path_is_absolute(arg)) {
4203 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4204 err = got_error_from_errno("asprintf");
4205 goto done;
4209 err = got_canonpath(abspath ? abspath : arg, canonpath,
4210 sizeof(canonpath));
4211 if (err)
4212 goto done;
4213 resolved = strdup(canonpath);
4214 if (resolved == NULL) {
4215 err = got_error_from_errno("strdup");
4216 goto done;
4218 } else {
4219 resolved = realpath(arg, NULL);
4220 if (resolved == NULL) {
4221 if (errno != ENOENT) {
4222 err = got_error_from_errno2("realpath", arg);
4223 goto done;
4225 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4226 err = got_error_from_errno("asprintf");
4227 goto done;
4229 err = got_canonpath(abspath, canonpath,
4230 sizeof(canonpath));
4231 if (err)
4232 goto done;
4233 resolved = strdup(canonpath);
4234 if (resolved == NULL) {
4235 err = got_error_from_errno("strdup");
4236 goto done;
4241 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4242 strlen(got_worktree_get_root_path(worktree)))) {
4243 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4244 goto done;
4247 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4248 err = got_path_skip_common_ancestor(&path,
4249 got_worktree_get_root_path(worktree), resolved);
4250 if (err)
4251 goto done;
4252 } else {
4253 path = strdup("");
4254 if (path == NULL) {
4255 err = got_error_from_errno("strdup");
4256 goto done;
4260 /* XXX status walk can't deal with trailing slash! */
4261 len = strlen(path);
4262 while (len > 0 && path[len - 1] == '/') {
4263 path[len - 1] = '\0';
4264 len--;
4266 done:
4267 free(abspath);
4268 free(resolved);
4269 free(cwd);
4270 if (err == NULL)
4271 *wt_path = path;
4272 else
4273 free(path);
4274 return err;
4277 struct schedule_addition_args {
4278 struct got_worktree *worktree;
4279 struct got_fileindex *fileindex;
4280 got_worktree_checkout_cb progress_cb;
4281 void *progress_arg;
4282 struct got_repository *repo;
4285 static int
4286 add_noop_status(unsigned char status)
4288 return (status == GOT_STATUS_ADD ||
4289 status == GOT_STATUS_MODIFY ||
4290 status == GOT_STATUS_CONFLICT ||
4291 status == GOT_STATUS_MODE_CHANGE ||
4292 status == GOT_STATUS_NO_CHANGE);
4295 static const struct got_error *
4296 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4297 const char *relpath, struct got_object_id *blob_id,
4298 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4299 int dirfd, const char *de_name)
4301 struct schedule_addition_args *a = arg;
4302 const struct got_error *err = NULL;
4303 struct got_fileindex_entry *ie;
4304 struct stat sb;
4305 char *ondisk_path;
4307 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4308 relpath) == -1)
4309 return got_error_from_errno("asprintf");
4311 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4312 if (ie) {
4313 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4314 de_name, a->repo);
4315 if (err)
4316 goto done;
4317 /* Re-adding an existing entry is a no-op. */
4318 if (staged_status == GOT_STATUS_NO_CHANGE &&
4319 add_noop_status(status))
4320 goto done;
4321 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4322 if (err)
4323 goto done;
4326 if (status != GOT_STATUS_UNVERSIONED) {
4327 if (status == GOT_STATUS_NONEXISTENT)
4328 err = got_error_set_errno(ENOENT, ondisk_path);
4329 else
4330 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4331 goto done;
4334 err = got_fileindex_entry_alloc(&ie, relpath);
4335 if (err)
4336 goto done;
4337 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4338 relpath, NULL, NULL, 1);
4339 if (err) {
4340 got_fileindex_entry_free(ie);
4341 goto done;
4343 err = got_fileindex_entry_add(a->fileindex, ie);
4344 if (err) {
4345 got_fileindex_entry_free(ie);
4346 goto done;
4348 done:
4349 free(ondisk_path);
4350 if (err)
4351 return err;
4352 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4353 return NULL;
4354 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4357 const struct got_error *
4358 got_worktree_schedule_add(struct got_worktree *worktree,
4359 struct got_pathlist_head *paths,
4360 got_worktree_checkout_cb progress_cb, void *progress_arg,
4361 struct got_repository *repo, int no_ignores)
4363 struct got_fileindex *fileindex = NULL;
4364 char *fileindex_path = NULL;
4365 const struct got_error *err = NULL, *sync_err, *unlockerr;
4366 struct got_pathlist_entry *pe;
4367 struct schedule_addition_args saa;
4369 err = lock_worktree(worktree, LOCK_EX);
4370 if (err)
4371 return err;
4373 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4374 if (err)
4375 goto done;
4377 saa.worktree = worktree;
4378 saa.fileindex = fileindex;
4379 saa.progress_cb = progress_cb;
4380 saa.progress_arg = progress_arg;
4381 saa.repo = repo;
4383 TAILQ_FOREACH(pe, paths, entry) {
4384 err = worktree_status(worktree, pe->path, fileindex, repo,
4385 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4386 if (err)
4387 break;
4389 sync_err = sync_fileindex(fileindex, fileindex_path);
4390 if (sync_err && err == NULL)
4391 err = sync_err;
4392 done:
4393 free(fileindex_path);
4394 if (fileindex)
4395 got_fileindex_free(fileindex);
4396 unlockerr = lock_worktree(worktree, LOCK_SH);
4397 if (unlockerr && err == NULL)
4398 err = unlockerr;
4399 return err;
4402 struct schedule_deletion_args {
4403 struct got_worktree *worktree;
4404 struct got_fileindex *fileindex;
4405 got_worktree_delete_cb progress_cb;
4406 void *progress_arg;
4407 struct got_repository *repo;
4408 int delete_local_mods;
4409 int keep_on_disk;
4410 int ignore_missing_paths;
4411 const char *status_path;
4412 size_t status_path_len;
4413 const char *status_codes;
4416 static const struct got_error *
4417 schedule_for_deletion(void *arg, unsigned char status,
4418 unsigned char staged_status, const char *relpath,
4419 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4420 struct got_object_id *commit_id, int dirfd, const char *de_name)
4422 struct schedule_deletion_args *a = arg;
4423 const struct got_error *err = NULL;
4424 struct got_fileindex_entry *ie = NULL;
4425 struct stat sb;
4426 char *ondisk_path;
4428 if (status == GOT_STATUS_NONEXISTENT) {
4429 if (a->ignore_missing_paths)
4430 return NULL;
4431 return got_error_set_errno(ENOENT, relpath);
4434 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4435 if (ie == NULL)
4436 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4438 staged_status = get_staged_status(ie);
4439 if (staged_status != GOT_STATUS_NO_CHANGE) {
4440 if (staged_status == GOT_STATUS_DELETE)
4441 return NULL;
4442 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4445 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4446 relpath) == -1)
4447 return got_error_from_errno("asprintf");
4449 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4450 a->repo);
4451 if (err)
4452 goto done;
4454 if (a->status_codes) {
4455 size_t ncodes = strlen(a->status_codes);
4456 int i;
4457 for (i = 0; i < ncodes ; i++) {
4458 if (status == a->status_codes[i])
4459 break;
4461 if (i == ncodes) {
4462 /* Do not delete files in non-matching status. */
4463 free(ondisk_path);
4464 return NULL;
4466 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4467 a->status_codes[i] != GOT_STATUS_MISSING) {
4468 static char msg[64];
4469 snprintf(msg, sizeof(msg),
4470 "invalid status code '%c'", a->status_codes[i]);
4471 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4472 goto done;
4476 if (status != GOT_STATUS_NO_CHANGE) {
4477 if (status == GOT_STATUS_DELETE)
4478 goto done;
4479 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4480 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4481 goto done;
4483 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4484 err = got_error_set_errno(ENOENT, relpath);
4485 goto done;
4487 if (status != GOT_STATUS_MODIFY &&
4488 status != GOT_STATUS_MISSING) {
4489 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4490 goto done;
4494 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4495 size_t root_len;
4497 if (dirfd != -1) {
4498 if (unlinkat(dirfd, de_name, 0) == -1) {
4499 err = got_error_from_errno2("unlinkat",
4500 ondisk_path);
4501 goto done;
4503 } else if (unlink(ondisk_path) == -1) {
4504 err = got_error_from_errno2("unlink", ondisk_path);
4505 goto done;
4508 root_len = strlen(a->worktree->root_path);
4509 do {
4510 char *parent;
4512 err = got_path_dirname(&parent, ondisk_path);
4513 if (err)
4514 goto done;
4515 free(ondisk_path);
4516 ondisk_path = parent;
4517 if (got_path_cmp(ondisk_path, a->status_path,
4518 strlen(ondisk_path), a->status_path_len) != 0 &&
4519 !got_path_is_child(ondisk_path, a->status_path,
4520 a->status_path_len))
4521 break;
4522 if (rmdir(ondisk_path) == -1) {
4523 if (errno != ENOTEMPTY)
4524 err = got_error_from_errno2("rmdir",
4525 ondisk_path);
4526 break;
4528 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4529 strlen(ondisk_path), root_len) != 0);
4532 got_fileindex_entry_mark_deleted_from_disk(ie);
4533 done:
4534 free(ondisk_path);
4535 if (err)
4536 return err;
4537 if (status == GOT_STATUS_DELETE)
4538 return NULL;
4539 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4540 staged_status, relpath);
4543 const struct got_error *
4544 got_worktree_schedule_delete(struct got_worktree *worktree,
4545 struct got_pathlist_head *paths, int delete_local_mods,
4546 const char *status_codes,
4547 got_worktree_delete_cb progress_cb, void *progress_arg,
4548 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4550 struct got_fileindex *fileindex = NULL;
4551 char *fileindex_path = NULL;
4552 const struct got_error *err = NULL, *sync_err, *unlockerr;
4553 struct got_pathlist_entry *pe;
4554 struct schedule_deletion_args sda;
4556 err = lock_worktree(worktree, LOCK_EX);
4557 if (err)
4558 return err;
4560 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4561 if (err)
4562 goto done;
4564 sda.worktree = worktree;
4565 sda.fileindex = fileindex;
4566 sda.progress_cb = progress_cb;
4567 sda.progress_arg = progress_arg;
4568 sda.repo = repo;
4569 sda.delete_local_mods = delete_local_mods;
4570 sda.keep_on_disk = keep_on_disk;
4571 sda.ignore_missing_paths = ignore_missing_paths;
4572 sda.status_codes = status_codes;
4574 TAILQ_FOREACH(pe, paths, entry) {
4575 char *ondisk_status_path;
4577 if (asprintf(&ondisk_status_path, "%s%s%s",
4578 got_worktree_get_root_path(worktree),
4579 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4580 err = got_error_from_errno("asprintf");
4581 goto done;
4583 sda.status_path = ondisk_status_path;
4584 sda.status_path_len = strlen(ondisk_status_path);
4585 err = worktree_status(worktree, pe->path, fileindex, repo,
4586 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4587 free(ondisk_status_path);
4588 if (err)
4589 break;
4591 sync_err = sync_fileindex(fileindex, fileindex_path);
4592 if (sync_err && err == NULL)
4593 err = sync_err;
4594 done:
4595 free(fileindex_path);
4596 if (fileindex)
4597 got_fileindex_free(fileindex);
4598 unlockerr = lock_worktree(worktree, LOCK_SH);
4599 if (unlockerr && err == NULL)
4600 err = unlockerr;
4601 return err;
4604 static const struct got_error *
4605 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4607 const struct got_error *err = NULL;
4608 char *line = NULL;
4609 size_t linesize = 0, n;
4610 ssize_t linelen;
4612 linelen = getline(&line, &linesize, infile);
4613 if (linelen == -1) {
4614 if (ferror(infile)) {
4615 err = got_error_from_errno("getline");
4616 goto done;
4618 return NULL;
4620 if (outfile) {
4621 n = fwrite(line, 1, linelen, outfile);
4622 if (n != linelen) {
4623 err = got_ferror(outfile, GOT_ERR_IO);
4624 goto done;
4627 if (rejectfile) {
4628 n = fwrite(line, 1, linelen, rejectfile);
4629 if (n != linelen)
4630 err = got_ferror(rejectfile, GOT_ERR_IO);
4632 done:
4633 free(line);
4634 return err;
4637 static const struct got_error *
4638 skip_one_line(FILE *f)
4640 char *line = NULL;
4641 size_t linesize = 0;
4642 ssize_t linelen;
4644 linelen = getline(&line, &linesize, f);
4645 if (linelen == -1) {
4646 if (ferror(f))
4647 return got_error_from_errno("getline");
4648 return NULL;
4650 free(line);
4651 return NULL;
4654 static const struct got_error *
4655 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4656 int start_old, int end_old, int start_new, int end_new,
4657 FILE *outfile, FILE *rejectfile)
4659 const struct got_error *err;
4661 /* Copy old file's lines leading up to patch. */
4662 while (!feof(f1) && *line_cur1 < start_old) {
4663 err = copy_one_line(f1, outfile, NULL);
4664 if (err)
4665 return err;
4666 (*line_cur1)++;
4668 /* Skip new file's lines leading up to patch. */
4669 while (!feof(f2) && *line_cur2 < start_new) {
4670 if (rejectfile)
4671 err = copy_one_line(f2, NULL, rejectfile);
4672 else
4673 err = skip_one_line(f2);
4674 if (err)
4675 return err;
4676 (*line_cur2)++;
4678 /* Copy patched lines. */
4679 while (!feof(f2) && *line_cur2 <= end_new) {
4680 err = copy_one_line(f2, outfile, NULL);
4681 if (err)
4682 return err;
4683 (*line_cur2)++;
4685 /* Skip over old file's replaced lines. */
4686 while (!feof(f1) && *line_cur1 <= end_old) {
4687 if (rejectfile)
4688 err = copy_one_line(f1, NULL, rejectfile);
4689 else
4690 err = skip_one_line(f1);
4691 if (err)
4692 return err;
4693 (*line_cur1)++;
4696 return NULL;
4699 static const struct got_error *
4700 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4701 FILE *outfile, FILE *rejectfile)
4703 const struct got_error *err;
4705 if (outfile) {
4706 /* Copy old file's lines until EOF. */
4707 while (!feof(f1)) {
4708 err = copy_one_line(f1, outfile, NULL);
4709 if (err)
4710 return err;
4711 (*line_cur1)++;
4714 if (rejectfile) {
4715 /* Copy new file's lines until EOF. */
4716 while (!feof(f2)) {
4717 err = copy_one_line(f2, NULL, rejectfile);
4718 if (err)
4719 return err;
4720 (*line_cur2)++;
4724 return NULL;
4727 static const struct got_error *
4728 apply_or_reject_change(int *choice, int *nchunks_used,
4729 struct diff_result *diff_result, int n,
4730 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4731 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4732 got_worktree_patch_cb patch_cb, void *patch_arg)
4734 const struct got_error *err = NULL;
4735 struct diff_chunk_context cc = {};
4736 int start_old, end_old, start_new, end_new;
4737 FILE *hunkfile;
4738 struct diff_output_unidiff_state *diff_state;
4739 struct diff_input_info diff_info;
4740 int rc;
4742 *choice = GOT_PATCH_CHOICE_NONE;
4744 /* Get changed line numbers without context lines for copy_change(). */
4745 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4746 start_old = cc.left.start;
4747 end_old = cc.left.end;
4748 start_new = cc.right.start;
4749 end_new = cc.right.end;
4751 /* Get the same change with context lines for display. */
4752 memset(&cc, 0, sizeof(cc));
4753 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4755 memset(&diff_info, 0, sizeof(diff_info));
4756 diff_info.left_path = relpath;
4757 diff_info.right_path = relpath;
4759 diff_state = diff_output_unidiff_state_alloc();
4760 if (diff_state == NULL)
4761 return got_error_set_errno(ENOMEM,
4762 "diff_output_unidiff_state_alloc");
4764 hunkfile = got_opentemp();
4765 if (hunkfile == NULL) {
4766 err = got_error_from_errno("got_opentemp");
4767 goto done;
4770 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4771 diff_result, &cc);
4772 if (rc != DIFF_RC_OK) {
4773 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4774 goto done;
4777 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4778 err = got_ferror(hunkfile, GOT_ERR_IO);
4779 goto done;
4782 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4783 hunkfile, changeno, nchanges);
4784 if (err)
4785 goto done;
4787 switch (*choice) {
4788 case GOT_PATCH_CHOICE_YES:
4789 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4790 end_old, start_new, end_new, outfile, rejectfile);
4791 break;
4792 case GOT_PATCH_CHOICE_NO:
4793 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4794 end_old, start_new, end_new, rejectfile, outfile);
4795 break;
4796 case GOT_PATCH_CHOICE_QUIT:
4797 break;
4798 default:
4799 err = got_error(GOT_ERR_PATCH_CHOICE);
4800 break;
4802 done:
4803 diff_output_unidiff_state_free(diff_state);
4804 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4805 err = got_error_from_errno("fclose");
4806 return err;
4809 struct revert_file_args {
4810 struct got_worktree *worktree;
4811 struct got_fileindex *fileindex;
4812 got_worktree_checkout_cb progress_cb;
4813 void *progress_arg;
4814 got_worktree_patch_cb patch_cb;
4815 void *patch_arg;
4816 struct got_repository *repo;
4817 int unlink_added_files;
4818 struct got_pathlist_head *added_files_to_unlink;
4821 static const struct got_error *
4822 create_patched_content(char **path_outfile, int reverse_patch,
4823 struct got_object_id *blob_id, const char *path2,
4824 int dirfd2, const char *de_name2,
4825 const char *relpath, struct got_repository *repo,
4826 got_worktree_patch_cb patch_cb, void *patch_arg)
4828 const struct got_error *err, *free_err;
4829 struct got_blob_object *blob = NULL;
4830 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4831 int fd = -1, fd2 = -1;
4832 char link_target[PATH_MAX];
4833 ssize_t link_len = 0;
4834 char *path1 = NULL, *id_str = NULL;
4835 struct stat sb2;
4836 struct got_diffreg_result *diffreg_result = NULL;
4837 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4838 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4840 *path_outfile = NULL;
4842 err = got_object_id_str(&id_str, blob_id);
4843 if (err)
4844 return err;
4846 if (dirfd2 != -1) {
4847 fd2 = openat(dirfd2, de_name2,
4848 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4849 if (fd2 == -1) {
4850 if (!got_err_open_nofollow_on_symlink()) {
4851 err = got_error_from_errno2("openat", path2);
4852 goto done;
4854 link_len = readlinkat(dirfd2, de_name2,
4855 link_target, sizeof(link_target));
4856 if (link_len == -1) {
4857 return got_error_from_errno2("readlinkat",
4858 path2);
4860 sb2.st_mode = S_IFLNK;
4861 sb2.st_size = link_len;
4863 } else {
4864 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4865 if (fd2 == -1) {
4866 if (!got_err_open_nofollow_on_symlink()) {
4867 err = got_error_from_errno2("open", path2);
4868 goto done;
4870 link_len = readlink(path2, link_target,
4871 sizeof(link_target));
4872 if (link_len == -1)
4873 return got_error_from_errno2("readlink", path2);
4874 sb2.st_mode = S_IFLNK;
4875 sb2.st_size = link_len;
4878 if (fd2 != -1) {
4879 if (fstat(fd2, &sb2) == -1) {
4880 err = got_error_from_errno2("fstat", path2);
4881 goto done;
4884 f2 = fdopen(fd2, "r");
4885 if (f2 == NULL) {
4886 err = got_error_from_errno2("fdopen", path2);
4887 goto done;
4889 fd2 = -1;
4890 } else {
4891 size_t n;
4892 f2 = got_opentemp();
4893 if (f2 == NULL) {
4894 err = got_error_from_errno2("got_opentemp", path2);
4895 goto done;
4897 n = fwrite(link_target, 1, link_len, f2);
4898 if (n != link_len) {
4899 err = got_ferror(f2, GOT_ERR_IO);
4900 goto done;
4902 if (fflush(f2) == EOF) {
4903 err = got_error_from_errno("fflush");
4904 goto done;
4906 rewind(f2);
4909 fd = got_opentempfd();
4910 if (fd == -1) {
4911 err = got_error_from_errno("got_opentempfd");
4912 goto done;
4915 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4916 if (err)
4917 goto done;
4919 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4920 if (err)
4921 goto done;
4923 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4924 if (err)
4925 goto done;
4927 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4928 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4929 if (err)
4930 goto done;
4932 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4933 "");
4934 if (err)
4935 goto done;
4937 if (fseek(f1, 0L, SEEK_SET) == -1)
4938 return got_ferror(f1, GOT_ERR_IO);
4939 if (fseek(f2, 0L, SEEK_SET) == -1)
4940 return got_ferror(f2, GOT_ERR_IO);
4942 /* Count the number of actual changes in the diff result. */
4943 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4944 struct diff_chunk_context cc = {};
4945 diff_chunk_context_load_change(&cc, &nchunks_used,
4946 diffreg_result->result, n, 0);
4947 nchanges++;
4949 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4950 int choice;
4951 err = apply_or_reject_change(&choice, &nchunks_used,
4952 diffreg_result->result, n, relpath, f1, f2,
4953 &line_cur1, &line_cur2,
4954 reverse_patch ? NULL : outfile,
4955 reverse_patch ? outfile : NULL,
4956 ++i, nchanges, patch_cb, patch_arg);
4957 if (err)
4958 goto done;
4959 if (choice == GOT_PATCH_CHOICE_YES)
4960 have_content = 1;
4961 else if (choice == GOT_PATCH_CHOICE_QUIT)
4962 break;
4964 if (have_content) {
4965 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4966 reverse_patch ? NULL : outfile,
4967 reverse_patch ? outfile : NULL);
4968 if (err)
4969 goto done;
4971 if (!S_ISLNK(sb2.st_mode)) {
4972 mode_t mode;
4974 mode = apply_umask(sb2.st_mode);
4975 if (fchmod(fileno(outfile), mode) == -1) {
4976 err = got_error_from_errno2("fchmod", path2);
4977 goto done;
4981 done:
4982 free(id_str);
4983 if (fd != -1 && close(fd) == -1 && err == NULL)
4984 err = got_error_from_errno("close");
4985 if (blob)
4986 got_object_blob_close(blob);
4987 free_err = got_diffreg_result_free(diffreg_result);
4988 if (err == NULL)
4989 err = free_err;
4990 if (f1 && fclose(f1) == EOF && err == NULL)
4991 err = got_error_from_errno2("fclose", path1);
4992 if (f2 && fclose(f2) == EOF && err == NULL)
4993 err = got_error_from_errno2("fclose", path2);
4994 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4995 err = got_error_from_errno2("close", path2);
4996 if (outfile && fclose(outfile) == EOF && err == NULL)
4997 err = got_error_from_errno2("fclose", *path_outfile);
4998 if (path1 && unlink(path1) == -1 && err == NULL)
4999 err = got_error_from_errno2("unlink", path1);
5000 if (err || !have_content) {
5001 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5002 err = got_error_from_errno2("unlink", *path_outfile);
5003 free(*path_outfile);
5004 *path_outfile = NULL;
5006 free(path1);
5007 return err;
5010 static const struct got_error *
5011 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5012 const char *relpath, struct got_object_id *blob_id,
5013 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5014 int dirfd, const char *de_name)
5016 struct revert_file_args *a = arg;
5017 const struct got_error *err = NULL;
5018 char *parent_path = NULL;
5019 struct got_fileindex_entry *ie;
5020 struct got_commit_object *base_commit = NULL;
5021 struct got_tree_object *tree = NULL;
5022 struct got_object_id *tree_id = NULL;
5023 const struct got_tree_entry *te = NULL;
5024 char *tree_path = NULL, *te_name;
5025 char *ondisk_path = NULL, *path_content = NULL;
5026 struct got_blob_object *blob = NULL;
5027 int fd = -1;
5029 /* Reverting a staged deletion is a no-op. */
5030 if (status == GOT_STATUS_DELETE &&
5031 staged_status != GOT_STATUS_NO_CHANGE)
5032 return NULL;
5034 if (status == GOT_STATUS_UNVERSIONED)
5035 return (*a->progress_cb)(a->progress_arg,
5036 GOT_STATUS_UNVERSIONED, relpath);
5038 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5039 if (ie == NULL)
5040 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5042 /* Construct in-repository path of tree which contains this blob. */
5043 err = got_path_dirname(&parent_path, ie->path);
5044 if (err) {
5045 if (err->code != GOT_ERR_BAD_PATH)
5046 goto done;
5047 parent_path = strdup("/");
5048 if (parent_path == NULL) {
5049 err = got_error_from_errno("strdup");
5050 goto done;
5053 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5054 tree_path = strdup(parent_path);
5055 if (tree_path == NULL) {
5056 err = got_error_from_errno("strdup");
5057 goto done;
5059 } else {
5060 if (got_path_is_root_dir(parent_path)) {
5061 tree_path = strdup(a->worktree->path_prefix);
5062 if (tree_path == NULL) {
5063 err = got_error_from_errno("strdup");
5064 goto done;
5066 } else {
5067 if (asprintf(&tree_path, "%s/%s",
5068 a->worktree->path_prefix, parent_path) == -1) {
5069 err = got_error_from_errno("asprintf");
5070 goto done;
5075 err = got_object_open_as_commit(&base_commit, a->repo,
5076 a->worktree->base_commit_id);
5077 if (err)
5078 goto done;
5080 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5081 if (err) {
5082 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5083 (status == GOT_STATUS_ADD ||
5084 staged_status == GOT_STATUS_ADD)))
5085 goto done;
5086 } else {
5087 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5088 if (err)
5089 goto done;
5091 err = got_path_basename(&te_name, ie->path);
5092 if (err)
5093 goto done;
5095 te = got_object_tree_find_entry(tree, te_name);
5096 free(te_name);
5097 if (te == NULL && status != GOT_STATUS_ADD &&
5098 staged_status != GOT_STATUS_ADD) {
5099 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5100 goto done;
5104 switch (status) {
5105 case GOT_STATUS_ADD:
5106 if (a->patch_cb) {
5107 int choice = GOT_PATCH_CHOICE_NONE;
5108 err = (*a->patch_cb)(&choice, a->patch_arg,
5109 status, ie->path, NULL, 1, 1);
5110 if (err)
5111 goto done;
5112 if (choice != GOT_PATCH_CHOICE_YES)
5113 break;
5115 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5116 ie->path);
5117 if (err)
5118 goto done;
5119 got_fileindex_entry_remove(a->fileindex, ie);
5120 if (a->unlink_added_files) {
5121 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5123 if (a->added_files_to_unlink) {
5124 struct got_pathlist_entry *pe;
5126 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5127 entry) {
5128 if (got_path_cmp(pe->path, relpath,
5129 pe->path_len, strlen(relpath)))
5130 continue;
5131 do_unlink = 1;
5132 break;
5136 if (do_unlink) {
5137 if (asprintf(&ondisk_path, "%s/%s",
5138 got_worktree_get_root_path(a->worktree),
5139 relpath) == -1) {
5140 err = got_error_from_errno("asprintf");
5141 goto done;
5143 if (unlink(ondisk_path) == -1) {
5144 err = got_error_from_errno2("unlink",
5145 ondisk_path);
5146 break;
5150 break;
5151 case GOT_STATUS_DELETE:
5152 if (a->patch_cb) {
5153 int choice = GOT_PATCH_CHOICE_NONE;
5154 err = (*a->patch_cb)(&choice, a->patch_arg,
5155 status, ie->path, NULL, 1, 1);
5156 if (err)
5157 goto done;
5158 if (choice != GOT_PATCH_CHOICE_YES)
5159 break;
5161 /* fall through */
5162 case GOT_STATUS_MODIFY:
5163 case GOT_STATUS_MODE_CHANGE:
5164 case GOT_STATUS_CONFLICT:
5165 case GOT_STATUS_MISSING: {
5166 struct got_object_id id;
5167 if (staged_status == GOT_STATUS_ADD ||
5168 staged_status == GOT_STATUS_MODIFY)
5169 got_fileindex_entry_get_staged_blob_id(&id, ie);
5170 else
5171 got_fileindex_entry_get_blob_id(&id, ie);
5172 fd = got_opentempfd();
5173 if (fd == -1) {
5174 err = got_error_from_errno("got_opentempfd");
5175 goto done;
5178 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5179 if (err)
5180 goto done;
5182 if (asprintf(&ondisk_path, "%s/%s",
5183 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5184 err = got_error_from_errno("asprintf");
5185 goto done;
5188 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5189 status == GOT_STATUS_CONFLICT)) {
5190 int is_bad_symlink = 0;
5191 err = create_patched_content(&path_content, 1, &id,
5192 ondisk_path, dirfd, de_name, ie->path, a->repo,
5193 a->patch_cb, a->patch_arg);
5194 if (err || path_content == NULL)
5195 break;
5196 if (te && S_ISLNK(te->mode)) {
5197 if (unlink(path_content) == -1) {
5198 err = got_error_from_errno2("unlink",
5199 path_content);
5200 break;
5202 err = install_symlink(&is_bad_symlink,
5203 a->worktree, ondisk_path, ie->path,
5204 blob, 0, 1, 0, 0, a->repo,
5205 a->progress_cb, a->progress_arg);
5206 } else {
5207 if (rename(path_content, ondisk_path) == -1) {
5208 err = got_error_from_errno3("rename",
5209 path_content, ondisk_path);
5210 goto done;
5213 } else {
5214 int is_bad_symlink = 0;
5215 if (te && S_ISLNK(te->mode)) {
5216 err = install_symlink(&is_bad_symlink,
5217 a->worktree, ondisk_path, ie->path,
5218 blob, 0, 1, 0, 0, a->repo,
5219 a->progress_cb, a->progress_arg);
5220 } else {
5221 err = install_blob(a->worktree, ondisk_path,
5222 ie->path,
5223 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5224 got_fileindex_perms_to_st(ie), blob,
5225 0, 1, 0, 0, a->repo,
5226 a->progress_cb, a->progress_arg);
5228 if (err)
5229 goto done;
5230 if (status == GOT_STATUS_DELETE ||
5231 status == GOT_STATUS_MODE_CHANGE) {
5232 err = got_fileindex_entry_update(ie,
5233 a->worktree->root_fd, relpath,
5234 blob->id.sha1,
5235 a->worktree->base_commit_id->sha1, 1);
5236 if (err)
5237 goto done;
5239 if (is_bad_symlink) {
5240 got_fileindex_entry_filetype_set(ie,
5241 GOT_FILEIDX_MODE_BAD_SYMLINK);
5244 break;
5246 default:
5247 break;
5249 done:
5250 free(ondisk_path);
5251 free(path_content);
5252 free(parent_path);
5253 free(tree_path);
5254 if (fd != -1 && close(fd) == -1 && err == NULL)
5255 err = got_error_from_errno("close");
5256 if (blob)
5257 got_object_blob_close(blob);
5258 if (tree)
5259 got_object_tree_close(tree);
5260 free(tree_id);
5261 if (base_commit)
5262 got_object_commit_close(base_commit);
5263 return err;
5266 const struct got_error *
5267 got_worktree_revert(struct got_worktree *worktree,
5268 struct got_pathlist_head *paths,
5269 got_worktree_checkout_cb progress_cb, void *progress_arg,
5270 got_worktree_patch_cb patch_cb, void *patch_arg,
5271 struct got_repository *repo)
5273 struct got_fileindex *fileindex = NULL;
5274 char *fileindex_path = NULL;
5275 const struct got_error *err = NULL, *unlockerr = NULL;
5276 const struct got_error *sync_err = NULL;
5277 struct got_pathlist_entry *pe;
5278 struct revert_file_args rfa;
5280 err = lock_worktree(worktree, LOCK_EX);
5281 if (err)
5282 return err;
5284 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5285 if (err)
5286 goto done;
5288 rfa.worktree = worktree;
5289 rfa.fileindex = fileindex;
5290 rfa.progress_cb = progress_cb;
5291 rfa.progress_arg = progress_arg;
5292 rfa.patch_cb = patch_cb;
5293 rfa.patch_arg = patch_arg;
5294 rfa.repo = repo;
5295 rfa.unlink_added_files = 0;
5296 TAILQ_FOREACH(pe, paths, entry) {
5297 err = worktree_status(worktree, pe->path, fileindex, repo,
5298 revert_file, &rfa, NULL, NULL, 1, 0);
5299 if (err)
5300 break;
5302 sync_err = sync_fileindex(fileindex, fileindex_path);
5303 if (sync_err && err == NULL)
5304 err = sync_err;
5305 done:
5306 free(fileindex_path);
5307 if (fileindex)
5308 got_fileindex_free(fileindex);
5309 unlockerr = lock_worktree(worktree, LOCK_SH);
5310 if (unlockerr && err == NULL)
5311 err = unlockerr;
5312 return err;
5315 static void
5316 free_commitable(struct got_commitable *ct)
5318 free(ct->path);
5319 free(ct->in_repo_path);
5320 free(ct->ondisk_path);
5321 free(ct->blob_id);
5322 free(ct->base_blob_id);
5323 free(ct->staged_blob_id);
5324 free(ct->base_commit_id);
5325 free(ct);
5328 struct collect_commitables_arg {
5329 struct got_pathlist_head *commitable_paths;
5330 struct got_repository *repo;
5331 struct got_worktree *worktree;
5332 struct got_fileindex *fileindex;
5333 int have_staged_files;
5334 int allow_bad_symlinks;
5335 int diff_header_shown;
5336 int commit_conflicts;
5337 FILE *diff_outfile;
5338 FILE *f1;
5339 FILE *f2;
5343 * Create a file which contains the target path of a symlink so we can feed
5344 * it as content to the diff engine.
5346 static const struct got_error *
5347 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5348 const char *abspath)
5350 const struct got_error *err = NULL;
5351 char target_path[PATH_MAX];
5352 ssize_t target_len, outlen;
5354 *fd = -1;
5356 if (dirfd != -1) {
5357 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5358 if (target_len == -1)
5359 return got_error_from_errno2("readlinkat", abspath);
5360 } else {
5361 target_len = readlink(abspath, target_path, PATH_MAX);
5362 if (target_len == -1)
5363 return got_error_from_errno2("readlink", abspath);
5366 *fd = got_opentempfd();
5367 if (*fd == -1)
5368 return got_error_from_errno("got_opentempfd");
5370 outlen = write(*fd, target_path, target_len);
5371 if (outlen == -1) {
5372 err = got_error_from_errno("got_opentempfd");
5373 goto done;
5376 if (lseek(*fd, 0, SEEK_SET) == -1) {
5377 err = got_error_from_errno2("lseek", abspath);
5378 goto done;
5380 done:
5381 if (err) {
5382 close(*fd);
5383 *fd = -1;
5385 return err;
5388 static const struct got_error *
5389 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5390 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5391 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5393 const struct got_error *err = NULL;
5394 struct got_blob_object *blob1 = NULL;
5395 int fd = -1, fd1 = -1, fd2 = -1;
5396 FILE *ondisk_file = NULL;
5397 char *label1 = NULL;
5398 struct stat sb;
5399 off_t size1 = 0;
5400 int f2_exists = 0;
5401 char *id_str = NULL;
5403 memset(&sb, 0, sizeof(sb));
5405 if (diff_staged) {
5406 if (ct->staged_status != GOT_STATUS_MODIFY &&
5407 ct->staged_status != GOT_STATUS_ADD &&
5408 ct->staged_status != GOT_STATUS_DELETE)
5409 return NULL;
5410 } else {
5411 if (ct->status != GOT_STATUS_MODIFY &&
5412 ct->status != GOT_STATUS_ADD &&
5413 ct->status != GOT_STATUS_DELETE &&
5414 ct->status != GOT_STATUS_CONFLICT)
5415 return NULL;
5418 err = got_opentemp_truncate(f1);
5419 if (err)
5420 return got_error_from_errno("got_opentemp_truncate");
5421 err = got_opentemp_truncate(f2);
5422 if (err)
5423 return got_error_from_errno("got_opentemp_truncate");
5425 if (!*diff_header_shown) {
5426 err = got_object_id_str(&id_str, worktree->base_commit_id);
5427 if (err)
5428 return err;
5429 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5430 got_worktree_get_root_path(worktree));
5431 fprintf(diff_outfile, "commit - %s\n", id_str);
5432 fprintf(diff_outfile, "path + %s%s\n",
5433 got_worktree_get_root_path(worktree),
5434 diff_staged ? " (staged changes)" : "");
5435 *diff_header_shown = 1;
5438 if (diff_staged) {
5439 const char *label1 = NULL, *label2 = NULL;
5440 switch (ct->staged_status) {
5441 case GOT_STATUS_MODIFY:
5442 label1 = ct->path;
5443 label2 = ct->path;
5444 break;
5445 case GOT_STATUS_ADD:
5446 label2 = ct->path;
5447 break;
5448 case GOT_STATUS_DELETE:
5449 label1 = ct->path;
5450 break;
5451 default:
5452 return got_error(GOT_ERR_FILE_STATUS);
5454 fd1 = got_opentempfd();
5455 if (fd1 == -1) {
5456 err = got_error_from_errno("got_opentempfd");
5457 goto done;
5459 fd2 = got_opentempfd();
5460 if (fd2 == -1) {
5461 err = got_error_from_errno("got_opentempfd");
5462 goto done;
5464 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5465 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5466 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5467 NULL, repo, diff_outfile);
5468 goto done;
5471 fd1 = got_opentempfd();
5472 if (fd1 == -1) {
5473 err = got_error_from_errno("got_opentempfd");
5474 goto done;
5477 if (ct->status != GOT_STATUS_ADD) {
5478 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5479 8192, fd1);
5480 if (err)
5481 goto done;
5484 if (ct->status != GOT_STATUS_DELETE) {
5485 if (dirfd != -1) {
5486 fd = openat(dirfd, de_name,
5487 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5488 if (fd == -1) {
5489 if (!got_err_open_nofollow_on_symlink()) {
5490 err = got_error_from_errno2("openat",
5491 ct->ondisk_path);
5492 goto done;
5494 err = get_symlink_target_file(&fd, dirfd,
5495 de_name, ct->ondisk_path);
5496 if (err)
5497 goto done;
5499 } else {
5500 fd = open(ct->ondisk_path,
5501 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5502 if (fd == -1) {
5503 if (!got_err_open_nofollow_on_symlink()) {
5504 err = got_error_from_errno2("open",
5505 ct->ondisk_path);
5506 goto done;
5508 err = get_symlink_target_file(&fd, dirfd,
5509 de_name, ct->ondisk_path);
5510 if (err)
5511 goto done;
5514 if (fstatat(fd, ct->ondisk_path, &sb,
5515 AT_SYMLINK_NOFOLLOW) == -1) {
5516 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5517 goto done;
5519 ondisk_file = fdopen(fd, "r");
5520 if (ondisk_file == NULL) {
5521 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5522 goto done;
5524 fd = -1;
5525 f2_exists = 1;
5528 if (blob1) {
5529 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5530 f1, blob1);
5531 if (err)
5532 goto done;
5535 err = got_diff_blob_file(blob1, f1, size1, label1,
5536 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5537 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5538 done:
5539 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5540 err = got_error_from_errno("close");
5541 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5542 err = got_error_from_errno("close");
5543 if (blob1)
5544 got_object_blob_close(blob1);
5545 if (fd != -1 && close(fd) == -1 && err == NULL)
5546 err = got_error_from_errno("close");
5547 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5548 err = got_error_from_errno("fclose");
5549 return err;
5552 static const struct got_error *
5553 collect_commitables(void *arg, unsigned char status,
5554 unsigned char staged_status, const char *relpath,
5555 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5556 struct got_object_id *commit_id, int dirfd, const char *de_name)
5558 struct collect_commitables_arg *a = arg;
5559 const struct got_error *err = NULL;
5560 struct got_commitable *ct = NULL;
5561 struct got_pathlist_entry *new = NULL;
5562 char *parent_path = NULL, *path = NULL;
5563 struct stat sb;
5565 if (a->have_staged_files) {
5566 if (staged_status != GOT_STATUS_MODIFY &&
5567 staged_status != GOT_STATUS_ADD &&
5568 staged_status != GOT_STATUS_DELETE)
5569 return NULL;
5570 } else {
5571 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5572 printf("C %s\n", relpath);
5573 return got_error(GOT_ERR_COMMIT_CONFLICT);
5576 if (status != GOT_STATUS_MODIFY &&
5577 status != GOT_STATUS_MODE_CHANGE &&
5578 status != GOT_STATUS_ADD &&
5579 status != GOT_STATUS_DELETE &&
5580 status != GOT_STATUS_CONFLICT)
5581 return NULL;
5584 if (asprintf(&path, "/%s", relpath) == -1) {
5585 err = got_error_from_errno("asprintf");
5586 goto done;
5588 if (strcmp(path, "/") == 0) {
5589 parent_path = strdup("");
5590 if (parent_path == NULL)
5591 return got_error_from_errno("strdup");
5592 } else {
5593 err = got_path_dirname(&parent_path, path);
5594 if (err)
5595 return err;
5598 ct = calloc(1, sizeof(*ct));
5599 if (ct == NULL) {
5600 err = got_error_from_errno("calloc");
5601 goto done;
5604 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5605 relpath) == -1) {
5606 err = got_error_from_errno("asprintf");
5607 goto done;
5610 if (staged_status == GOT_STATUS_ADD ||
5611 staged_status == GOT_STATUS_MODIFY) {
5612 struct got_fileindex_entry *ie;
5613 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5614 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5615 case GOT_FILEIDX_MODE_REGULAR_FILE:
5616 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5617 ct->mode = S_IFREG;
5618 break;
5619 case GOT_FILEIDX_MODE_SYMLINK:
5620 ct->mode = S_IFLNK;
5621 break;
5622 default:
5623 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5624 goto done;
5626 ct->mode |= got_fileindex_entry_perms_get(ie);
5627 } else if (status != GOT_STATUS_DELETE &&
5628 staged_status != GOT_STATUS_DELETE) {
5629 if (dirfd != -1) {
5630 if (fstatat(dirfd, de_name, &sb,
5631 AT_SYMLINK_NOFOLLOW) == -1) {
5632 err = got_error_from_errno2("fstatat",
5633 ct->ondisk_path);
5634 goto done;
5636 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5637 err = got_error_from_errno2("lstat", ct->ondisk_path);
5638 goto done;
5640 ct->mode = sb.st_mode;
5643 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5644 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5645 relpath) == -1) {
5646 err = got_error_from_errno("asprintf");
5647 goto done;
5650 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5651 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5652 int is_bad_symlink;
5653 char target_path[PATH_MAX];
5654 ssize_t target_len;
5655 target_len = readlink(ct->ondisk_path, target_path,
5656 sizeof(target_path));
5657 if (target_len == -1) {
5658 err = got_error_from_errno2("readlink",
5659 ct->ondisk_path);
5660 goto done;
5662 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5663 target_len, ct->ondisk_path, a->worktree->root_path,
5664 a->worktree->meta_dir);
5665 if (err)
5666 goto done;
5667 if (is_bad_symlink) {
5668 err = got_error_path(ct->ondisk_path,
5669 GOT_ERR_BAD_SYMLINK);
5670 goto done;
5675 ct->status = status;
5676 ct->staged_status = staged_status;
5677 ct->blob_id = NULL; /* will be filled in when blob gets created */
5678 if (ct->status != GOT_STATUS_ADD &&
5679 ct->staged_status != GOT_STATUS_ADD) {
5680 ct->base_blob_id = got_object_id_dup(blob_id);
5681 if (ct->base_blob_id == NULL) {
5682 err = got_error_from_errno("got_object_id_dup");
5683 goto done;
5685 ct->base_commit_id = got_object_id_dup(commit_id);
5686 if (ct->base_commit_id == NULL) {
5687 err = got_error_from_errno("got_object_id_dup");
5688 goto done;
5691 if (ct->staged_status == GOT_STATUS_ADD ||
5692 ct->staged_status == GOT_STATUS_MODIFY) {
5693 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5694 if (ct->staged_blob_id == NULL) {
5695 err = got_error_from_errno("got_object_id_dup");
5696 goto done;
5699 ct->path = strdup(path);
5700 if (ct->path == NULL) {
5701 err = got_error_from_errno("strdup");
5702 goto done;
5704 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5705 if (err)
5706 goto done;
5708 if (a->diff_outfile && ct && new != NULL) {
5709 err = append_ct_diff(ct, &a->diff_header_shown,
5710 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5711 a->have_staged_files, a->repo, a->worktree);
5712 if (err)
5713 goto done;
5715 done:
5716 if (ct && (err || new == NULL))
5717 free_commitable(ct);
5718 free(parent_path);
5719 free(path);
5720 return err;
5723 static const struct got_error *write_tree(struct got_object_id **, int *,
5724 struct got_tree_object *, const char *, struct got_pathlist_head *,
5725 got_worktree_status_cb status_cb, void *status_arg,
5726 struct got_repository *);
5728 static const struct got_error *
5729 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5730 struct got_tree_entry *te, const char *parent_path,
5731 struct got_pathlist_head *commitable_paths,
5732 got_worktree_status_cb status_cb, void *status_arg,
5733 struct got_repository *repo)
5735 const struct got_error *err = NULL;
5736 struct got_tree_object *subtree;
5737 char *subpath;
5739 if (asprintf(&subpath, "%s%s%s", parent_path,
5740 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5741 return got_error_from_errno("asprintf");
5743 err = got_object_open_as_tree(&subtree, repo, &te->id);
5744 if (err)
5745 return err;
5747 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5748 commitable_paths, status_cb, status_arg, repo);
5749 got_object_tree_close(subtree);
5750 free(subpath);
5751 return err;
5754 static const struct got_error *
5755 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5757 const struct got_error *err = NULL;
5758 char *ct_parent_path = NULL;
5760 *match = 0;
5762 if (strchr(ct->in_repo_path, '/') == NULL) {
5763 *match = got_path_is_root_dir(path);
5764 return NULL;
5767 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5768 if (err)
5769 return err;
5770 *match = (strcmp(path, ct_parent_path) == 0);
5771 free(ct_parent_path);
5772 return err;
5775 static mode_t
5776 get_ct_file_mode(struct got_commitable *ct)
5778 if (S_ISLNK(ct->mode))
5779 return S_IFLNK;
5781 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5784 static const struct got_error *
5785 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5786 struct got_tree_entry *te, struct got_commitable *ct)
5788 const struct got_error *err = NULL;
5790 *new_te = NULL;
5792 err = got_object_tree_entry_dup(new_te, te);
5793 if (err)
5794 goto done;
5796 (*new_te)->mode = get_ct_file_mode(ct);
5798 if (ct->staged_status == GOT_STATUS_MODIFY)
5799 memcpy(&(*new_te)->id, ct->staged_blob_id,
5800 sizeof((*new_te)->id));
5801 else
5802 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5803 done:
5804 if (err && *new_te) {
5805 free(*new_te);
5806 *new_te = NULL;
5808 return err;
5811 static const struct got_error *
5812 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5813 struct got_commitable *ct)
5815 const struct got_error *err = NULL;
5816 char *ct_name = NULL;
5818 *new_te = NULL;
5820 *new_te = calloc(1, sizeof(**new_te));
5821 if (*new_te == NULL)
5822 return got_error_from_errno("calloc");
5824 err = got_path_basename(&ct_name, ct->path);
5825 if (err)
5826 goto done;
5827 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5828 sizeof((*new_te)->name)) {
5829 err = got_error(GOT_ERR_NO_SPACE);
5830 goto done;
5833 (*new_te)->mode = get_ct_file_mode(ct);
5835 if (ct->staged_status == GOT_STATUS_ADD)
5836 memcpy(&(*new_te)->id, ct->staged_blob_id,
5837 sizeof((*new_te)->id));
5838 else
5839 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5840 done:
5841 free(ct_name);
5842 if (err && *new_te) {
5843 free(*new_te);
5844 *new_te = NULL;
5846 return err;
5849 static const struct got_error *
5850 insert_tree_entry(struct got_tree_entry *new_te,
5851 struct got_pathlist_head *paths)
5853 const struct got_error *err = NULL;
5854 struct got_pathlist_entry *new_pe;
5856 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5857 if (err)
5858 return err;
5859 if (new_pe == NULL)
5860 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5861 return NULL;
5864 static const struct got_error *
5865 report_ct_status(struct got_commitable *ct,
5866 got_worktree_status_cb status_cb, void *status_arg)
5868 const char *ct_path = ct->path;
5869 unsigned char status;
5871 if (status_cb == NULL) /* no commit progress output desired */
5872 return NULL;
5874 while (ct_path[0] == '/')
5875 ct_path++;
5877 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5878 status = ct->staged_status;
5879 else
5880 status = ct->status;
5882 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5883 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5886 static const struct got_error *
5887 match_modified_subtree(int *modified, struct got_tree_entry *te,
5888 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5890 const struct got_error *err = NULL;
5891 struct got_pathlist_entry *pe;
5892 char *te_path;
5894 *modified = 0;
5896 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5897 got_path_is_root_dir(base_tree_path) ? "" : "/",
5898 te->name) == -1)
5899 return got_error_from_errno("asprintf");
5901 TAILQ_FOREACH(pe, commitable_paths, entry) {
5902 struct got_commitable *ct = pe->data;
5903 *modified = got_path_is_child(ct->in_repo_path, te_path,
5904 strlen(te_path));
5905 if (*modified)
5906 break;
5909 free(te_path);
5910 return err;
5913 static const struct got_error *
5914 match_deleted_or_modified_ct(struct got_commitable **ctp,
5915 struct got_tree_entry *te, const char *base_tree_path,
5916 struct got_pathlist_head *commitable_paths)
5918 const struct got_error *err = NULL;
5919 struct got_pathlist_entry *pe;
5921 *ctp = NULL;
5923 TAILQ_FOREACH(pe, commitable_paths, entry) {
5924 struct got_commitable *ct = pe->data;
5925 char *ct_name = NULL;
5926 int path_matches;
5928 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5929 if (ct->status != GOT_STATUS_MODIFY &&
5930 ct->status != GOT_STATUS_MODE_CHANGE &&
5931 ct->status != GOT_STATUS_DELETE &&
5932 ct->status != GOT_STATUS_CONFLICT)
5933 continue;
5934 } else {
5935 if (ct->staged_status != GOT_STATUS_MODIFY &&
5936 ct->staged_status != GOT_STATUS_DELETE)
5937 continue;
5940 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5941 continue;
5943 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5944 if (err)
5945 return err;
5946 if (!path_matches)
5947 continue;
5949 err = got_path_basename(&ct_name, pe->path);
5950 if (err)
5951 return err;
5953 if (strcmp(te->name, ct_name) != 0) {
5954 free(ct_name);
5955 continue;
5957 free(ct_name);
5959 *ctp = ct;
5960 break;
5963 return err;
5966 static const struct got_error *
5967 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5968 const char *child_path, const char *path_base_tree,
5969 struct got_pathlist_head *commitable_paths,
5970 got_worktree_status_cb status_cb, void *status_arg,
5971 struct got_repository *repo)
5973 const struct got_error *err = NULL;
5974 struct got_tree_entry *new_te;
5975 char *subtree_path;
5976 struct got_object_id *id = NULL;
5977 int nentries;
5979 *new_tep = NULL;
5981 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5982 got_path_is_root_dir(path_base_tree) ? "" : "/",
5983 child_path) == -1)
5984 return got_error_from_errno("asprintf");
5986 new_te = calloc(1, sizeof(*new_te));
5987 if (new_te == NULL)
5988 return got_error_from_errno("calloc");
5989 new_te->mode = S_IFDIR;
5991 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5992 sizeof(new_te->name)) {
5993 err = got_error(GOT_ERR_NO_SPACE);
5994 goto done;
5996 err = write_tree(&id, &nentries, NULL, subtree_path,
5997 commitable_paths, status_cb, status_arg, repo);
5998 if (err) {
5999 free(new_te);
6000 goto done;
6002 memcpy(&new_te->id, id, sizeof(new_te->id));
6003 done:
6004 free(id);
6005 free(subtree_path);
6006 if (err == NULL)
6007 *new_tep = new_te;
6008 return err;
6011 static const struct got_error *
6012 write_tree(struct got_object_id **new_tree_id, int *nentries,
6013 struct got_tree_object *base_tree, const char *path_base_tree,
6014 struct got_pathlist_head *commitable_paths,
6015 got_worktree_status_cb status_cb, void *status_arg,
6016 struct got_repository *repo)
6018 const struct got_error *err = NULL;
6019 struct got_pathlist_head paths;
6020 struct got_tree_entry *te, *new_te = NULL;
6021 struct got_pathlist_entry *pe;
6023 TAILQ_INIT(&paths);
6024 *nentries = 0;
6026 /* Insert, and recurse into, newly added entries first. */
6027 TAILQ_FOREACH(pe, commitable_paths, entry) {
6028 struct got_commitable *ct = pe->data;
6029 char *child_path = NULL, *slash;
6031 if ((ct->status != GOT_STATUS_ADD &&
6032 ct->staged_status != GOT_STATUS_ADD) ||
6033 (ct->flags & GOT_COMMITABLE_ADDED))
6034 continue;
6036 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6037 strlen(path_base_tree)))
6038 continue;
6040 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6041 ct->in_repo_path);
6042 if (err)
6043 goto done;
6045 slash = strchr(child_path, '/');
6046 if (slash == NULL) {
6047 err = alloc_added_blob_tree_entry(&new_te, ct);
6048 if (err)
6049 goto done;
6050 err = report_ct_status(ct, status_cb, status_arg);
6051 if (err)
6052 goto done;
6053 ct->flags |= GOT_COMMITABLE_ADDED;
6054 err = insert_tree_entry(new_te, &paths);
6055 if (err)
6056 goto done;
6057 (*nentries)++;
6058 } else {
6059 *slash = '\0'; /* trim trailing path components */
6060 if (base_tree == NULL ||
6061 got_object_tree_find_entry(base_tree, child_path)
6062 == NULL) {
6063 err = make_subtree_for_added_blob(&new_te,
6064 child_path, path_base_tree,
6065 commitable_paths, status_cb, status_arg,
6066 repo);
6067 if (err)
6068 goto done;
6069 err = insert_tree_entry(new_te, &paths);
6070 if (err)
6071 goto done;
6072 (*nentries)++;
6077 if (base_tree) {
6078 int i, nbase_entries;
6079 /* Handle modified and deleted entries. */
6080 nbase_entries = got_object_tree_get_nentries(base_tree);
6081 for (i = 0; i < nbase_entries; i++) {
6082 struct got_commitable *ct = NULL;
6084 te = got_object_tree_get_entry(base_tree, i);
6085 if (got_object_tree_entry_is_submodule(te)) {
6086 /* Entry is a submodule; just copy it. */
6087 err = got_object_tree_entry_dup(&new_te, te);
6088 if (err)
6089 goto done;
6090 err = insert_tree_entry(new_te, &paths);
6091 if (err)
6092 goto done;
6093 (*nentries)++;
6094 continue;
6097 if (S_ISDIR(te->mode)) {
6098 int modified;
6099 err = got_object_tree_entry_dup(&new_te, te);
6100 if (err)
6101 goto done;
6102 err = match_modified_subtree(&modified, te,
6103 path_base_tree, commitable_paths);
6104 if (err)
6105 goto done;
6106 /* Avoid recursion into unmodified subtrees. */
6107 if (modified) {
6108 struct got_object_id *new_id;
6109 int nsubentries;
6110 err = write_subtree(&new_id,
6111 &nsubentries, te,
6112 path_base_tree, commitable_paths,
6113 status_cb, status_arg, repo);
6114 if (err)
6115 goto done;
6116 if (nsubentries == 0) {
6117 /* All entries were deleted. */
6118 free(new_id);
6119 continue;
6121 memcpy(&new_te->id, new_id,
6122 sizeof(new_te->id));
6123 free(new_id);
6125 err = insert_tree_entry(new_te, &paths);
6126 if (err)
6127 goto done;
6128 (*nentries)++;
6129 continue;
6132 err = match_deleted_or_modified_ct(&ct, te,
6133 path_base_tree, commitable_paths);
6134 if (err)
6135 goto done;
6136 if (ct) {
6137 /* NB: Deleted entries get dropped here. */
6138 if (ct->status == GOT_STATUS_MODIFY ||
6139 ct->status == GOT_STATUS_MODE_CHANGE ||
6140 ct->status == GOT_STATUS_CONFLICT ||
6141 ct->staged_status == GOT_STATUS_MODIFY) {
6142 err = alloc_modified_blob_tree_entry(
6143 &new_te, te, ct);
6144 if (err)
6145 goto done;
6146 err = insert_tree_entry(new_te, &paths);
6147 if (err)
6148 goto done;
6149 (*nentries)++;
6151 err = report_ct_status(ct, status_cb,
6152 status_arg);
6153 if (err)
6154 goto done;
6155 } else {
6156 /* Entry is unchanged; just copy it. */
6157 err = got_object_tree_entry_dup(&new_te, te);
6158 if (err)
6159 goto done;
6160 err = insert_tree_entry(new_te, &paths);
6161 if (err)
6162 goto done;
6163 (*nentries)++;
6168 /* Write new list of entries; deleted entries have been dropped. */
6169 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6170 done:
6171 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6172 return err;
6175 static const struct got_error *
6176 update_fileindex_after_commit(struct got_worktree *worktree,
6177 struct got_pathlist_head *commitable_paths,
6178 struct got_object_id *new_base_commit_id,
6179 struct got_fileindex *fileindex, int have_staged_files)
6181 const struct got_error *err = NULL;
6182 struct got_pathlist_entry *pe;
6183 char *relpath = NULL;
6185 TAILQ_FOREACH(pe, commitable_paths, entry) {
6186 struct got_fileindex_entry *ie;
6187 struct got_commitable *ct = pe->data;
6189 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6191 err = got_path_skip_common_ancestor(&relpath,
6192 worktree->root_path, ct->ondisk_path);
6193 if (err)
6194 goto done;
6196 if (ie) {
6197 if (ct->status == GOT_STATUS_DELETE ||
6198 ct->staged_status == GOT_STATUS_DELETE) {
6199 got_fileindex_entry_remove(fileindex, ie);
6200 } else if (ct->staged_status == GOT_STATUS_ADD ||
6201 ct->staged_status == GOT_STATUS_MODIFY) {
6202 got_fileindex_entry_stage_set(ie,
6203 GOT_FILEIDX_STAGE_NONE);
6204 got_fileindex_entry_staged_filetype_set(ie, 0);
6206 err = got_fileindex_entry_update(ie,
6207 worktree->root_fd, relpath,
6208 ct->staged_blob_id->sha1,
6209 new_base_commit_id->sha1,
6210 !have_staged_files);
6211 } else
6212 err = got_fileindex_entry_update(ie,
6213 worktree->root_fd, relpath,
6214 ct->blob_id->sha1,
6215 new_base_commit_id->sha1,
6216 !have_staged_files);
6217 } else {
6218 err = got_fileindex_entry_alloc(&ie, pe->path);
6219 if (err)
6220 goto done;
6221 err = got_fileindex_entry_update(ie,
6222 worktree->root_fd, relpath, ct->blob_id->sha1,
6223 new_base_commit_id->sha1, 1);
6224 if (err) {
6225 got_fileindex_entry_free(ie);
6226 goto done;
6228 err = got_fileindex_entry_add(fileindex, ie);
6229 if (err) {
6230 got_fileindex_entry_free(ie);
6231 goto done;
6234 free(relpath);
6235 relpath = NULL;
6237 done:
6238 free(relpath);
6239 return err;
6243 static const struct got_error *
6244 check_out_of_date(const char *in_repo_path, unsigned char status,
6245 unsigned char staged_status, struct got_object_id *base_blob_id,
6246 struct got_object_id *base_commit_id,
6247 struct got_object_id *head_commit_id, struct got_repository *repo,
6248 int ood_errcode)
6250 const struct got_error *err = NULL;
6251 struct got_commit_object *commit = NULL;
6252 struct got_object_id *id = NULL;
6254 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6255 /* Trivial case: base commit == head commit */
6256 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6257 return NULL;
6259 * Ensure file content which local changes were based
6260 * on matches file content in the branch head.
6262 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6263 if (err)
6264 goto done;
6265 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6266 if (err) {
6267 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6268 err = got_error(ood_errcode);
6269 goto done;
6270 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6271 err = got_error(ood_errcode);
6272 } else {
6273 /* Require that added files don't exist in the branch head. */
6274 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6275 if (err)
6276 goto done;
6277 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6278 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6279 goto done;
6280 err = id ? got_error(ood_errcode) : NULL;
6282 done:
6283 free(id);
6284 if (commit)
6285 got_object_commit_close(commit);
6286 return err;
6289 static const struct got_error *
6290 commit_worktree(struct got_object_id **new_commit_id,
6291 struct got_pathlist_head *commitable_paths,
6292 struct got_object_id *head_commit_id,
6293 struct got_object_id *parent_id2,
6294 struct got_worktree *worktree,
6295 const char *author, const char *committer, char *diff_path,
6296 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6297 got_worktree_status_cb status_cb, void *status_arg,
6298 struct got_repository *repo)
6300 const struct got_error *err = NULL, *unlockerr = NULL;
6301 struct got_pathlist_entry *pe;
6302 const char *head_ref_name = NULL;
6303 struct got_commit_object *head_commit = NULL;
6304 struct got_reference *head_ref2 = NULL;
6305 struct got_object_id *head_commit_id2 = NULL;
6306 struct got_tree_object *head_tree = NULL;
6307 struct got_object_id *new_tree_id = NULL;
6308 int nentries, nparents = 0;
6309 struct got_object_id_queue parent_ids;
6310 struct got_object_qid *pid = NULL;
6311 char *logmsg = NULL;
6312 time_t timestamp;
6314 *new_commit_id = NULL;
6316 STAILQ_INIT(&parent_ids);
6318 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6319 if (err)
6320 goto done;
6322 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6323 if (err)
6324 goto done;
6326 if (commit_msg_cb != NULL) {
6327 err = commit_msg_cb(commitable_paths, diff_path,
6328 &logmsg, commit_arg);
6329 if (err)
6330 goto done;
6333 if (logmsg == NULL || strlen(logmsg) == 0) {
6334 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6335 goto done;
6338 /* Create blobs from added and modified files and record their IDs. */
6339 TAILQ_FOREACH(pe, commitable_paths, entry) {
6340 struct got_commitable *ct = pe->data;
6341 char *ondisk_path;
6343 /* Blobs for staged files already exist. */
6344 if (ct->staged_status == GOT_STATUS_ADD ||
6345 ct->staged_status == GOT_STATUS_MODIFY)
6346 continue;
6348 if (ct->status != GOT_STATUS_ADD &&
6349 ct->status != GOT_STATUS_MODIFY &&
6350 ct->status != GOT_STATUS_MODE_CHANGE &&
6351 ct->status != GOT_STATUS_CONFLICT)
6352 continue;
6354 if (asprintf(&ondisk_path, "%s/%s",
6355 worktree->root_path, pe->path) == -1) {
6356 err = got_error_from_errno("asprintf");
6357 goto done;
6359 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6360 free(ondisk_path);
6361 if (err)
6362 goto done;
6365 /* Recursively write new tree objects. */
6366 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6367 commitable_paths, status_cb, status_arg, repo);
6368 if (err)
6369 goto done;
6371 err = got_object_qid_alloc(&pid, head_commit_id);
6372 if (err)
6373 goto done;
6374 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6375 nparents++;
6376 if (parent_id2) {
6377 err = got_object_qid_alloc(&pid, parent_id2);
6378 if (err)
6379 goto done;
6380 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6381 nparents++;
6383 timestamp = time(NULL);
6384 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6385 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6386 if (logmsg != NULL)
6387 free(logmsg);
6388 if (err)
6389 goto done;
6391 /* Check if a concurrent commit to our branch has occurred. */
6392 head_ref_name = got_worktree_get_head_ref_name(worktree);
6393 if (head_ref_name == NULL) {
6394 err = got_error_from_errno("got_worktree_get_head_ref_name");
6395 goto done;
6397 /* Lock the reference here to prevent concurrent modification. */
6398 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6399 if (err)
6400 goto done;
6401 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6402 if (err)
6403 goto done;
6404 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6405 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6406 goto done;
6408 /* Update branch head in repository. */
6409 err = got_ref_change_ref(head_ref2, *new_commit_id);
6410 if (err)
6411 goto done;
6412 err = got_ref_write(head_ref2, repo);
6413 if (err)
6414 goto done;
6416 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6417 if (err)
6418 goto done;
6420 err = ref_base_commit(worktree, repo);
6421 if (err)
6422 goto done;
6423 done:
6424 got_object_id_queue_free(&parent_ids);
6425 if (head_tree)
6426 got_object_tree_close(head_tree);
6427 if (head_commit)
6428 got_object_commit_close(head_commit);
6429 free(head_commit_id2);
6430 if (head_ref2) {
6431 unlockerr = got_ref_unlock(head_ref2);
6432 if (unlockerr && err == NULL)
6433 err = unlockerr;
6434 got_ref_close(head_ref2);
6436 return err;
6439 static const struct got_error *
6440 check_path_is_commitable(const char *path,
6441 struct got_pathlist_head *commitable_paths)
6443 struct got_pathlist_entry *cpe = NULL;
6444 size_t path_len = strlen(path);
6446 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6447 struct got_commitable *ct = cpe->data;
6448 const char *ct_path = ct->path;
6450 while (ct_path[0] == '/')
6451 ct_path++;
6453 if (strcmp(path, ct_path) == 0 ||
6454 got_path_is_child(ct_path, path, path_len))
6455 break;
6458 if (cpe == NULL)
6459 return got_error_path(path, GOT_ERR_BAD_PATH);
6461 return NULL;
6464 static const struct got_error *
6465 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6467 int *have_staged_files = arg;
6469 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6470 *have_staged_files = 1;
6471 return got_error(GOT_ERR_CANCELLED);
6474 return NULL;
6477 static const struct got_error *
6478 check_non_staged_files(struct got_fileindex *fileindex,
6479 struct got_pathlist_head *paths)
6481 struct got_pathlist_entry *pe;
6482 struct got_fileindex_entry *ie;
6484 TAILQ_FOREACH(pe, paths, entry) {
6485 if (pe->path[0] == '\0')
6486 continue;
6487 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6488 if (ie == NULL)
6489 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6490 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6491 return got_error_path(pe->path,
6492 GOT_ERR_FILE_NOT_STAGED);
6495 return NULL;
6498 const struct got_error *
6499 got_worktree_commit(struct got_object_id **new_commit_id,
6500 struct got_worktree *worktree, struct got_pathlist_head *paths,
6501 const char *author, const char *committer, int allow_bad_symlinks,
6502 int show_diff, int commit_conflicts,
6503 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6504 got_worktree_status_cb status_cb, void *status_arg,
6505 struct got_repository *repo)
6507 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6508 struct got_fileindex *fileindex = NULL;
6509 char *fileindex_path = NULL;
6510 struct got_pathlist_head commitable_paths;
6511 struct collect_commitables_arg cc_arg;
6512 struct got_pathlist_entry *pe;
6513 struct got_reference *head_ref = NULL;
6514 struct got_object_id *head_commit_id = NULL;
6515 char *diff_path = NULL;
6516 int have_staged_files = 0;
6518 *new_commit_id = NULL;
6520 memset(&cc_arg, 0, sizeof(cc_arg));
6521 TAILQ_INIT(&commitable_paths);
6523 err = lock_worktree(worktree, LOCK_EX);
6524 if (err)
6525 goto done;
6527 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6528 if (err)
6529 goto done;
6531 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6532 if (err)
6533 goto done;
6535 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6536 if (err)
6537 goto done;
6539 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6540 &have_staged_files);
6541 if (err && err->code != GOT_ERR_CANCELLED)
6542 goto done;
6543 if (have_staged_files) {
6544 err = check_non_staged_files(fileindex, paths);
6545 if (err)
6546 goto done;
6549 cc_arg.commitable_paths = &commitable_paths;
6550 cc_arg.worktree = worktree;
6551 cc_arg.fileindex = fileindex;
6552 cc_arg.repo = repo;
6553 cc_arg.have_staged_files = have_staged_files;
6554 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6555 cc_arg.diff_header_shown = 0;
6556 cc_arg.commit_conflicts = commit_conflicts;
6557 if (show_diff) {
6558 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6559 GOT_TMPDIR_STR "/got", ".diff");
6560 if (err)
6561 goto done;
6562 cc_arg.f1 = got_opentemp();
6563 if (cc_arg.f1 == NULL) {
6564 err = got_error_from_errno("got_opentemp");
6565 goto done;
6567 cc_arg.f2 = got_opentemp();
6568 if (cc_arg.f2 == NULL) {
6569 err = got_error_from_errno("got_opentemp");
6570 goto done;
6574 TAILQ_FOREACH(pe, paths, entry) {
6575 err = worktree_status(worktree, pe->path, fileindex, repo,
6576 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6577 if (err)
6578 goto done;
6581 if (show_diff) {
6582 if (fflush(cc_arg.diff_outfile) == EOF) {
6583 err = got_error_from_errno("fflush");
6584 goto done;
6588 if (TAILQ_EMPTY(&commitable_paths)) {
6589 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6590 goto done;
6593 TAILQ_FOREACH(pe, paths, entry) {
6594 err = check_path_is_commitable(pe->path, &commitable_paths);
6595 if (err)
6596 goto done;
6599 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6600 struct got_commitable *ct = pe->data;
6601 const char *ct_path = ct->in_repo_path;
6603 while (ct_path[0] == '/')
6604 ct_path++;
6605 err = check_out_of_date(ct_path, ct->status,
6606 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6607 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6608 if (err)
6609 goto done;
6613 err = commit_worktree(new_commit_id, &commitable_paths,
6614 head_commit_id, NULL, worktree, author, committer,
6615 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6616 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6617 if (err)
6618 goto done;
6620 err = update_fileindex_after_commit(worktree, &commitable_paths,
6621 *new_commit_id, fileindex, have_staged_files);
6622 sync_err = sync_fileindex(fileindex, fileindex_path);
6623 if (sync_err && err == NULL)
6624 err = sync_err;
6625 done:
6626 if (fileindex)
6627 got_fileindex_free(fileindex);
6628 free(fileindex_path);
6629 unlockerr = lock_worktree(worktree, LOCK_SH);
6630 if (unlockerr && err == NULL)
6631 err = unlockerr;
6632 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6633 struct got_commitable *ct = pe->data;
6635 free_commitable(ct);
6637 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6638 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6639 err = got_error_from_errno2("unlink", diff_path);
6640 free(diff_path);
6641 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6642 err == NULL)
6643 err = got_error_from_errno("fclose");
6644 return err;
6647 const char *
6648 got_commitable_get_path(struct got_commitable *ct)
6650 return ct->path;
6653 unsigned int
6654 got_commitable_get_status(struct got_commitable *ct)
6656 return ct->status;
6659 struct check_rebase_ok_arg {
6660 struct got_worktree *worktree;
6661 struct got_repository *repo;
6664 static const struct got_error *
6665 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6667 const struct got_error *err = NULL;
6668 struct check_rebase_ok_arg *a = arg;
6669 unsigned char status;
6670 struct stat sb;
6671 char *ondisk_path;
6673 /* Reject rebase of a work tree with mixed base commits. */
6674 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6675 SHA1_DIGEST_LENGTH))
6676 return got_error(GOT_ERR_MIXED_COMMITS);
6678 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6679 == -1)
6680 return got_error_from_errno("asprintf");
6682 /* Reject rebase of a work tree with modified or staged files. */
6683 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6684 free(ondisk_path);
6685 if (err)
6686 return err;
6688 if (status != GOT_STATUS_NO_CHANGE)
6689 return got_error(GOT_ERR_MODIFIED);
6690 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6691 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6693 return NULL;
6696 const struct got_error *
6697 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6698 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6699 struct got_worktree *worktree, struct got_reference *branch,
6700 struct got_repository *repo)
6702 const struct got_error *err = NULL;
6703 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6704 char *branch_ref_name = NULL;
6705 char *fileindex_path = NULL;
6706 struct check_rebase_ok_arg ok_arg;
6707 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6708 struct got_object_id *wt_branch_tip = NULL;
6710 *new_base_branch_ref = NULL;
6711 *tmp_branch = NULL;
6712 *fileindex = NULL;
6714 err = lock_worktree(worktree, LOCK_EX);
6715 if (err)
6716 return err;
6718 err = open_fileindex(fileindex, &fileindex_path, worktree);
6719 if (err)
6720 goto done;
6722 ok_arg.worktree = worktree;
6723 ok_arg.repo = repo;
6724 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6725 &ok_arg);
6726 if (err)
6727 goto done;
6729 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6730 if (err)
6731 goto done;
6733 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6734 if (err)
6735 goto done;
6737 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6738 if (err)
6739 goto done;
6741 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6742 0);
6743 if (err)
6744 goto done;
6746 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6747 if (err)
6748 goto done;
6749 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6750 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6751 goto done;
6754 err = got_ref_alloc_symref(new_base_branch_ref,
6755 new_base_branch_ref_name, wt_branch);
6756 if (err)
6757 goto done;
6758 err = got_ref_write(*new_base_branch_ref, repo);
6759 if (err)
6760 goto done;
6762 /* TODO Lock original branch's ref while rebasing? */
6764 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6765 if (err)
6766 goto done;
6768 err = got_ref_write(branch_ref, repo);
6769 if (err)
6770 goto done;
6772 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6773 worktree->base_commit_id);
6774 if (err)
6775 goto done;
6776 err = got_ref_write(*tmp_branch, repo);
6777 if (err)
6778 goto done;
6780 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6781 if (err)
6782 goto done;
6783 done:
6784 free(fileindex_path);
6785 free(tmp_branch_name);
6786 free(new_base_branch_ref_name);
6787 free(branch_ref_name);
6788 if (branch_ref)
6789 got_ref_close(branch_ref);
6790 if (wt_branch)
6791 got_ref_close(wt_branch);
6792 free(wt_branch_tip);
6793 if (err) {
6794 if (*new_base_branch_ref) {
6795 got_ref_close(*new_base_branch_ref);
6796 *new_base_branch_ref = NULL;
6798 if (*tmp_branch) {
6799 got_ref_close(*tmp_branch);
6800 *tmp_branch = NULL;
6802 if (*fileindex) {
6803 got_fileindex_free(*fileindex);
6804 *fileindex = NULL;
6806 lock_worktree(worktree, LOCK_SH);
6808 return err;
6811 const struct got_error *
6812 got_worktree_rebase_continue(struct got_object_id **commit_id,
6813 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6814 struct got_reference **branch, struct got_fileindex **fileindex,
6815 struct got_worktree *worktree, struct got_repository *repo)
6817 const struct got_error *err;
6818 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6819 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6820 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6821 char *fileindex_path = NULL;
6822 int have_staged_files = 0;
6824 *commit_id = NULL;
6825 *new_base_branch = NULL;
6826 *tmp_branch = NULL;
6827 *branch = NULL;
6828 *fileindex = NULL;
6830 err = lock_worktree(worktree, LOCK_EX);
6831 if (err)
6832 return err;
6834 err = open_fileindex(fileindex, &fileindex_path, worktree);
6835 if (err)
6836 goto done;
6838 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6839 &have_staged_files);
6840 if (err && err->code != GOT_ERR_CANCELLED)
6841 goto done;
6842 if (have_staged_files) {
6843 err = got_error(GOT_ERR_STAGED_PATHS);
6844 goto done;
6847 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6848 if (err)
6849 goto done;
6851 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6852 if (err)
6853 goto done;
6855 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6856 if (err)
6857 goto done;
6859 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6860 if (err)
6861 goto done;
6863 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6864 if (err)
6865 goto done;
6867 err = got_ref_open(branch, repo,
6868 got_ref_get_symref_target(branch_ref), 0);
6869 if (err)
6870 goto done;
6872 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6873 if (err)
6874 goto done;
6876 err = got_ref_resolve(commit_id, repo, commit_ref);
6877 if (err)
6878 goto done;
6880 err = got_ref_open(new_base_branch, repo,
6881 new_base_branch_ref_name, 0);
6882 if (err)
6883 goto done;
6885 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6886 if (err)
6887 goto done;
6888 done:
6889 free(commit_ref_name);
6890 free(branch_ref_name);
6891 free(fileindex_path);
6892 if (commit_ref)
6893 got_ref_close(commit_ref);
6894 if (branch_ref)
6895 got_ref_close(branch_ref);
6896 if (err) {
6897 free(*commit_id);
6898 *commit_id = NULL;
6899 if (*tmp_branch) {
6900 got_ref_close(*tmp_branch);
6901 *tmp_branch = NULL;
6903 if (*new_base_branch) {
6904 got_ref_close(*new_base_branch);
6905 *new_base_branch = NULL;
6907 if (*branch) {
6908 got_ref_close(*branch);
6909 *branch = NULL;
6911 if (*fileindex) {
6912 got_fileindex_free(*fileindex);
6913 *fileindex = NULL;
6915 lock_worktree(worktree, LOCK_SH);
6917 return err;
6920 const struct got_error *
6921 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6923 const struct got_error *err;
6924 char *tmp_branch_name = NULL;
6926 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6927 if (err)
6928 return err;
6930 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6931 free(tmp_branch_name);
6932 return NULL;
6935 static const struct got_error *
6936 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6937 const char *diff_path, char **logmsg, void *arg)
6939 *logmsg = arg;
6940 return NULL;
6943 static const struct got_error *
6944 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6945 const char *path, struct got_object_id *blob_id,
6946 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6947 int dirfd, const char *de_name)
6949 return NULL;
6952 struct collect_merged_paths_arg {
6953 got_worktree_checkout_cb progress_cb;
6954 void *progress_arg;
6955 struct got_pathlist_head *merged_paths;
6958 static const struct got_error *
6959 collect_merged_paths(void *arg, unsigned char status, const char *path)
6961 const struct got_error *err;
6962 struct collect_merged_paths_arg *a = arg;
6963 char *p;
6964 struct got_pathlist_entry *new;
6966 err = (*a->progress_cb)(a->progress_arg, status, path);
6967 if (err)
6968 return err;
6970 if (status != GOT_STATUS_MERGE &&
6971 status != GOT_STATUS_ADD &&
6972 status != GOT_STATUS_DELETE &&
6973 status != GOT_STATUS_CONFLICT)
6974 return NULL;
6976 p = strdup(path);
6977 if (p == NULL)
6978 return got_error_from_errno("strdup");
6980 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6981 if (err || new == NULL)
6982 free(p);
6983 return err;
6986 static const struct got_error *
6987 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6988 int is_rebase, struct got_repository *repo)
6990 const struct got_error *err;
6991 struct got_reference *commit_ref = NULL;
6993 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6994 if (err) {
6995 if (err->code != GOT_ERR_NOT_REF)
6996 goto done;
6997 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6998 if (err)
6999 goto done;
7000 err = got_ref_write(commit_ref, repo);
7001 if (err)
7002 goto done;
7003 } else if (is_rebase) {
7004 struct got_object_id *stored_id;
7005 int cmp;
7007 err = got_ref_resolve(&stored_id, repo, commit_ref);
7008 if (err)
7009 goto done;
7010 cmp = got_object_id_cmp(commit_id, stored_id);
7011 free(stored_id);
7012 if (cmp != 0) {
7013 err = got_error(GOT_ERR_REBASE_COMMITID);
7014 goto done;
7017 done:
7018 if (commit_ref)
7019 got_ref_close(commit_ref);
7020 return err;
7023 static const struct got_error *
7024 rebase_merge_files(struct got_pathlist_head *merged_paths,
7025 const char *commit_ref_name, struct got_worktree *worktree,
7026 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7027 struct got_object_id *commit_id, struct got_repository *repo,
7028 got_worktree_checkout_cb progress_cb, void *progress_arg,
7029 got_cancel_cb cancel_cb, void *cancel_arg)
7031 const struct got_error *err;
7032 struct got_reference *commit_ref = NULL;
7033 struct collect_merged_paths_arg cmp_arg;
7034 char *fileindex_path;
7036 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7038 err = get_fileindex_path(&fileindex_path, worktree);
7039 if (err)
7040 return err;
7042 cmp_arg.progress_cb = progress_cb;
7043 cmp_arg.progress_arg = progress_arg;
7044 cmp_arg.merged_paths = merged_paths;
7045 err = merge_files(worktree, fileindex, fileindex_path,
7046 parent_commit_id, commit_id, repo, collect_merged_paths,
7047 &cmp_arg, cancel_cb, cancel_arg);
7048 if (commit_ref)
7049 got_ref_close(commit_ref);
7050 return err;
7053 const struct got_error *
7054 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7055 struct got_worktree *worktree, struct got_fileindex *fileindex,
7056 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7057 struct got_repository *repo,
7058 got_worktree_checkout_cb progress_cb, void *progress_arg,
7059 got_cancel_cb cancel_cb, void *cancel_arg)
7061 const struct got_error *err;
7062 char *commit_ref_name;
7064 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7065 if (err)
7066 return err;
7068 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7069 if (err)
7070 goto done;
7072 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7073 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7074 progress_arg, cancel_cb, cancel_arg);
7075 done:
7076 free(commit_ref_name);
7077 return err;
7080 const struct got_error *
7081 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7082 struct got_worktree *worktree, struct got_fileindex *fileindex,
7083 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7084 struct got_repository *repo,
7085 got_worktree_checkout_cb progress_cb, void *progress_arg,
7086 got_cancel_cb cancel_cb, void *cancel_arg)
7088 const struct got_error *err;
7089 char *commit_ref_name;
7091 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7092 if (err)
7093 return err;
7095 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7096 if (err)
7097 goto done;
7099 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7100 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7101 progress_arg, cancel_cb, cancel_arg);
7102 done:
7103 free(commit_ref_name);
7104 return err;
7107 static const struct got_error *
7108 rebase_commit(struct got_object_id **new_commit_id,
7109 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7110 struct got_worktree *worktree, struct got_fileindex *fileindex,
7111 struct got_reference *tmp_branch, const char *committer,
7112 struct got_commit_object *orig_commit, const char *new_logmsg,
7113 int allow_conflict, struct got_repository *repo)
7115 const struct got_error *err, *sync_err;
7116 struct got_pathlist_head commitable_paths;
7117 struct collect_commitables_arg cc_arg;
7118 char *fileindex_path = NULL;
7119 struct got_reference *head_ref = NULL;
7120 struct got_object_id *head_commit_id = NULL;
7121 char *logmsg = NULL;
7123 memset(&cc_arg, 0, sizeof(cc_arg));
7124 TAILQ_INIT(&commitable_paths);
7125 *new_commit_id = NULL;
7127 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7129 err = get_fileindex_path(&fileindex_path, worktree);
7130 if (err)
7131 return err;
7133 cc_arg.commitable_paths = &commitable_paths;
7134 cc_arg.worktree = worktree;
7135 cc_arg.repo = repo;
7136 cc_arg.have_staged_files = 0;
7137 cc_arg.commit_conflicts = allow_conflict;
7139 * If possible get the status of individual files directly to
7140 * avoid crawling the entire work tree once per rebased commit.
7142 * Ideally, merged_paths would contain a list of commitables
7143 * we could use so we could skip worktree_status() entirely.
7144 * However, we would then need carefully keep track of cumulative
7145 * effects of operations such as file additions and deletions
7146 * in 'got histedit -f' (folding multiple commits into one),
7147 * and this extra complexity is not really worth it.
7149 if (merged_paths) {
7150 struct got_pathlist_entry *pe;
7151 TAILQ_FOREACH(pe, merged_paths, entry) {
7152 err = worktree_status(worktree, pe->path, fileindex,
7153 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7154 0);
7155 if (err)
7156 goto done;
7158 } else {
7159 err = worktree_status(worktree, "", fileindex, repo,
7160 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7161 if (err)
7162 goto done;
7165 if (TAILQ_EMPTY(&commitable_paths)) {
7166 /* No-op change; commit will be elided. */
7167 err = got_ref_delete(commit_ref, repo);
7168 if (err)
7169 goto done;
7170 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7171 goto done;
7174 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7175 if (err)
7176 goto done;
7178 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7179 if (err)
7180 goto done;
7182 if (new_logmsg) {
7183 logmsg = strdup(new_logmsg);
7184 if (logmsg == NULL) {
7185 err = got_error_from_errno("strdup");
7186 goto done;
7188 } else {
7189 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7190 if (err)
7191 goto done;
7194 /* NB: commit_worktree will call free(logmsg) */
7195 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7196 NULL, worktree, got_object_commit_get_author(orig_commit),
7197 committer ? committer :
7198 got_object_commit_get_committer(orig_commit), NULL,
7199 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7200 if (err)
7201 goto done;
7203 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7204 if (err)
7205 goto done;
7207 err = got_ref_delete(commit_ref, repo);
7208 if (err)
7209 goto done;
7211 err = update_fileindex_after_commit(worktree, &commitable_paths,
7212 *new_commit_id, fileindex, 0);
7213 sync_err = sync_fileindex(fileindex, fileindex_path);
7214 if (sync_err && err == NULL)
7215 err = sync_err;
7216 done:
7217 free(fileindex_path);
7218 free(head_commit_id);
7219 if (head_ref)
7220 got_ref_close(head_ref);
7221 if (err) {
7222 free(*new_commit_id);
7223 *new_commit_id = NULL;
7225 return err;
7228 const struct got_error *
7229 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7230 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7231 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7232 const char *committer, struct got_commit_object *orig_commit,
7233 struct got_object_id *orig_commit_id, int allow_conflict,
7234 struct got_repository *repo)
7236 const struct got_error *err;
7237 char *commit_ref_name;
7238 struct got_reference *commit_ref = NULL;
7239 struct got_object_id *commit_id = NULL;
7241 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7242 if (err)
7243 return err;
7245 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7246 if (err)
7247 goto done;
7248 err = got_ref_resolve(&commit_id, repo, commit_ref);
7249 if (err)
7250 goto done;
7251 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7252 err = got_error(GOT_ERR_REBASE_COMMITID);
7253 goto done;
7256 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7257 worktree, fileindex, tmp_branch, committer, orig_commit,
7258 NULL, allow_conflict, repo);
7259 done:
7260 if (commit_ref)
7261 got_ref_close(commit_ref);
7262 free(commit_ref_name);
7263 free(commit_id);
7264 return err;
7267 const struct got_error *
7268 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7269 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7270 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7271 const char *committer, struct got_commit_object *orig_commit,
7272 struct got_object_id *orig_commit_id, const char *new_logmsg,
7273 int allow_conflict, struct got_repository *repo)
7275 const struct got_error *err;
7276 char *commit_ref_name;
7277 struct got_reference *commit_ref = NULL;
7279 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7280 if (err)
7281 return err;
7283 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7284 if (err)
7285 goto done;
7287 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7288 worktree, fileindex, tmp_branch, committer, orig_commit,
7289 new_logmsg, allow_conflict, repo);
7290 done:
7291 if (commit_ref)
7292 got_ref_close(commit_ref);
7293 free(commit_ref_name);
7294 return err;
7297 const struct got_error *
7298 got_worktree_rebase_postpone(struct got_worktree *worktree,
7299 struct got_fileindex *fileindex)
7301 if (fileindex)
7302 got_fileindex_free(fileindex);
7303 return lock_worktree(worktree, LOCK_SH);
7306 static const struct got_error *
7307 delete_ref(const char *name, struct got_repository *repo)
7309 const struct got_error *err;
7310 struct got_reference *ref;
7312 err = got_ref_open(&ref, repo, name, 0);
7313 if (err) {
7314 if (err->code == GOT_ERR_NOT_REF)
7315 return NULL;
7316 return err;
7319 err = got_ref_delete(ref, repo);
7320 got_ref_close(ref);
7321 return err;
7324 static const struct got_error *
7325 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7327 const struct got_error *err;
7328 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7329 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7331 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7332 if (err)
7333 goto done;
7334 err = delete_ref(tmp_branch_name, repo);
7335 if (err)
7336 goto done;
7338 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7339 if (err)
7340 goto done;
7341 err = delete_ref(new_base_branch_ref_name, repo);
7342 if (err)
7343 goto done;
7345 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7346 if (err)
7347 goto done;
7348 err = delete_ref(branch_ref_name, repo);
7349 if (err)
7350 goto done;
7352 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7353 if (err)
7354 goto done;
7355 err = delete_ref(commit_ref_name, repo);
7356 if (err)
7357 goto done;
7359 done:
7360 free(tmp_branch_name);
7361 free(new_base_branch_ref_name);
7362 free(branch_ref_name);
7363 free(commit_ref_name);
7364 return err;
7367 static const struct got_error *
7368 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7369 struct got_object_id *new_commit_id, struct got_repository *repo)
7371 const struct got_error *err;
7372 struct got_reference *ref = NULL;
7373 struct got_object_id *old_commit_id = NULL;
7374 const char *branch_name = NULL;
7375 char *new_id_str = NULL;
7376 char *refname = NULL;
7378 branch_name = got_ref_get_name(branch);
7379 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7380 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7381 branch_name += 11;
7383 err = got_object_id_str(&new_id_str, new_commit_id);
7384 if (err)
7385 return err;
7387 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7388 new_id_str) == -1) {
7389 err = got_error_from_errno("asprintf");
7390 goto done;
7393 err = got_ref_resolve(&old_commit_id, repo, branch);
7394 if (err)
7395 goto done;
7397 err = got_ref_alloc(&ref, refname, old_commit_id);
7398 if (err)
7399 goto done;
7401 err = got_ref_write(ref, repo);
7402 done:
7403 free(new_id_str);
7404 free(refname);
7405 free(old_commit_id);
7406 if (ref)
7407 got_ref_close(ref);
7408 return err;
7411 const struct got_error *
7412 got_worktree_rebase_complete(struct got_worktree *worktree,
7413 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7414 struct got_reference *rebased_branch, struct got_repository *repo,
7415 int create_backup)
7417 const struct got_error *err, *unlockerr, *sync_err;
7418 struct got_object_id *new_head_commit_id = NULL;
7419 char *fileindex_path = NULL;
7421 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7422 if (err)
7423 return err;
7425 if (create_backup) {
7426 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7427 rebased_branch, new_head_commit_id, repo);
7428 if (err)
7429 goto done;
7432 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7433 if (err)
7434 goto done;
7436 err = got_ref_write(rebased_branch, repo);
7437 if (err)
7438 goto done;
7440 err = got_worktree_set_head_ref(worktree, rebased_branch);
7441 if (err)
7442 goto done;
7444 err = delete_rebase_refs(worktree, repo);
7445 if (err)
7446 goto done;
7448 err = get_fileindex_path(&fileindex_path, worktree);
7449 if (err)
7450 goto done;
7451 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7452 sync_err = sync_fileindex(fileindex, fileindex_path);
7453 if (sync_err && err == NULL)
7454 err = sync_err;
7455 done:
7456 got_fileindex_free(fileindex);
7457 free(fileindex_path);
7458 free(new_head_commit_id);
7459 unlockerr = lock_worktree(worktree, LOCK_SH);
7460 if (unlockerr && err == NULL)
7461 err = unlockerr;
7462 return err;
7465 static const struct got_error *
7466 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7467 struct got_object_id *id1, struct got_object_id *id2,
7468 struct got_repository *repo)
7470 const struct got_error *err;
7471 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7472 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7474 if (id1) {
7475 err = got_object_open_as_commit(&commit1, repo, id1);
7476 if (err)
7477 goto done;
7479 err = got_object_open_as_tree(&tree1, repo,
7480 got_object_commit_get_tree_id(commit1));
7481 if (err)
7482 goto done;
7485 if (id2) {
7486 err = got_object_open_as_commit(&commit2, repo, id2);
7487 if (err)
7488 goto done;
7490 err = got_object_open_as_tree(&tree2, repo,
7491 got_object_commit_get_tree_id(commit2));
7492 if (err)
7493 goto done;
7496 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7497 got_diff_tree_collect_changed_paths, paths, 0);
7498 if (err)
7499 goto done;
7500 done:
7501 if (commit1)
7502 got_object_commit_close(commit1);
7503 if (commit2)
7504 got_object_commit_close(commit2);
7505 if (tree1)
7506 got_object_tree_close(tree1);
7507 if (tree2)
7508 got_object_tree_close(tree2);
7509 return err;
7512 static const struct got_error *
7513 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7514 struct got_object_id *id1, struct got_object_id *id2,
7515 const char *path_prefix, struct got_repository *repo)
7517 const struct got_error *err;
7518 struct got_pathlist_head merged_paths;
7519 struct got_pathlist_entry *pe;
7520 char *abspath = NULL, *wt_path = NULL;
7522 TAILQ_INIT(&merged_paths);
7524 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7525 if (err)
7526 goto done;
7528 TAILQ_FOREACH(pe, &merged_paths, entry) {
7529 struct got_diff_changed_path *change = pe->data;
7531 if (change->status != GOT_STATUS_ADD)
7532 continue;
7534 if (got_path_is_root_dir(path_prefix)) {
7535 wt_path = strdup(pe->path);
7536 if (wt_path == NULL) {
7537 err = got_error_from_errno("strdup");
7538 goto done;
7540 } else {
7541 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7542 err = got_error_from_errno("asprintf");
7543 goto done;
7546 err = got_path_skip_common_ancestor(&wt_path,
7547 path_prefix, abspath);
7548 if (err)
7549 goto done;
7550 free(abspath);
7551 abspath = NULL;
7554 err = got_pathlist_append(added_paths, wt_path, NULL);
7555 if (err)
7556 goto done;
7557 wt_path = NULL;
7560 done:
7561 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7562 free(abspath);
7563 free(wt_path);
7564 return err;
7567 static const struct got_error *
7568 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7569 struct got_object_id *id, const char *path_prefix,
7570 struct got_repository *repo)
7572 const struct got_error *err;
7573 struct got_commit_object *commit = NULL;
7574 struct got_object_qid *pid;
7576 err = got_object_open_as_commit(&commit, repo, id);
7577 if (err)
7578 goto done;
7580 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7582 err = get_paths_added_between_commits(added_paths,
7583 pid ? &pid->id : NULL, id, path_prefix, repo);
7584 if (err)
7585 goto done;
7586 done:
7587 if (commit)
7588 got_object_commit_close(commit);
7589 return err;
7592 const struct got_error *
7593 got_worktree_rebase_abort(struct got_worktree *worktree,
7594 struct got_fileindex *fileindex, struct got_repository *repo,
7595 struct got_reference *new_base_branch,
7596 got_worktree_checkout_cb progress_cb, void *progress_arg)
7598 const struct got_error *err, *unlockerr, *sync_err;
7599 struct got_reference *resolved = NULL;
7600 struct got_object_id *commit_id = NULL;
7601 struct got_object_id *merged_commit_id = NULL;
7602 struct got_commit_object *commit = NULL;
7603 char *fileindex_path = NULL;
7604 char *commit_ref_name = NULL;
7605 struct got_reference *commit_ref = NULL;
7606 struct revert_file_args rfa;
7607 struct got_object_id *tree_id = NULL;
7608 struct got_pathlist_head added_paths;
7610 TAILQ_INIT(&added_paths);
7612 err = lock_worktree(worktree, LOCK_EX);
7613 if (err)
7614 return err;
7616 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7617 if (err)
7618 goto done;
7620 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7621 if (err)
7622 goto done;
7624 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7625 if (err)
7626 goto done;
7629 * Determine which files in added status can be safely removed
7630 * from disk while reverting changes in the work tree.
7631 * We want to avoid deleting unrelated files which were added by
7632 * the user for conflict resolution purposes.
7634 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7635 got_worktree_get_path_prefix(worktree), repo);
7636 if (err)
7637 goto done;
7639 err = got_ref_open(&resolved, repo,
7640 got_ref_get_symref_target(new_base_branch), 0);
7641 if (err)
7642 goto done;
7644 err = got_worktree_set_head_ref(worktree, resolved);
7645 if (err)
7646 goto done;
7649 * XXX commits to the base branch could have happened while
7650 * we were busy rebasing; should we store the original commit ID
7651 * when rebase begins and read it back here?
7653 err = got_ref_resolve(&commit_id, repo, resolved);
7654 if (err)
7655 goto done;
7657 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7658 if (err)
7659 goto done;
7661 err = got_object_open_as_commit(&commit, repo,
7662 worktree->base_commit_id);
7663 if (err)
7664 goto done;
7666 err = got_object_id_by_path(&tree_id, repo, commit,
7667 worktree->path_prefix);
7668 if (err)
7669 goto done;
7671 err = delete_rebase_refs(worktree, repo);
7672 if (err)
7673 goto done;
7675 err = get_fileindex_path(&fileindex_path, worktree);
7676 if (err)
7677 goto done;
7679 rfa.worktree = worktree;
7680 rfa.fileindex = fileindex;
7681 rfa.progress_cb = progress_cb;
7682 rfa.progress_arg = progress_arg;
7683 rfa.patch_cb = NULL;
7684 rfa.patch_arg = NULL;
7685 rfa.repo = repo;
7686 rfa.unlink_added_files = 1;
7687 rfa.added_files_to_unlink = &added_paths;
7688 err = worktree_status(worktree, "", fileindex, repo,
7689 revert_file, &rfa, NULL, NULL, 1, 0);
7690 if (err)
7691 goto sync;
7693 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7694 repo, progress_cb, progress_arg, NULL, NULL);
7695 sync:
7696 sync_err = sync_fileindex(fileindex, fileindex_path);
7697 if (sync_err && err == NULL)
7698 err = sync_err;
7699 done:
7700 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7701 got_ref_close(resolved);
7702 free(tree_id);
7703 free(commit_id);
7704 free(merged_commit_id);
7705 if (commit)
7706 got_object_commit_close(commit);
7707 if (fileindex)
7708 got_fileindex_free(fileindex);
7709 free(fileindex_path);
7710 free(commit_ref_name);
7711 if (commit_ref)
7712 got_ref_close(commit_ref);
7714 unlockerr = lock_worktree(worktree, LOCK_SH);
7715 if (unlockerr && err == NULL)
7716 err = unlockerr;
7717 return err;
7720 const struct got_error *
7721 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7722 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7723 struct got_fileindex **fileindex, struct got_worktree *worktree,
7724 struct got_repository *repo)
7726 const struct got_error *err = NULL;
7727 char *tmp_branch_name = NULL;
7728 char *branch_ref_name = NULL;
7729 char *base_commit_ref_name = NULL;
7730 char *fileindex_path = NULL;
7731 struct check_rebase_ok_arg ok_arg;
7732 struct got_reference *wt_branch = NULL;
7733 struct got_reference *base_commit_ref = NULL;
7735 *tmp_branch = NULL;
7736 *branch_ref = NULL;
7737 *base_commit_id = NULL;
7738 *fileindex = NULL;
7740 err = lock_worktree(worktree, LOCK_EX);
7741 if (err)
7742 return err;
7744 err = open_fileindex(fileindex, &fileindex_path, worktree);
7745 if (err)
7746 goto done;
7748 ok_arg.worktree = worktree;
7749 ok_arg.repo = repo;
7750 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7751 &ok_arg);
7752 if (err)
7753 goto done;
7755 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7756 if (err)
7757 goto done;
7759 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7760 if (err)
7761 goto done;
7763 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7764 worktree);
7765 if (err)
7766 goto done;
7768 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7769 0);
7770 if (err)
7771 goto done;
7773 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7774 if (err)
7775 goto done;
7777 err = got_ref_write(*branch_ref, repo);
7778 if (err)
7779 goto done;
7781 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7782 worktree->base_commit_id);
7783 if (err)
7784 goto done;
7785 err = got_ref_write(base_commit_ref, repo);
7786 if (err)
7787 goto done;
7788 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7789 if (*base_commit_id == NULL) {
7790 err = got_error_from_errno("got_object_id_dup");
7791 goto done;
7794 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7795 worktree->base_commit_id);
7796 if (err)
7797 goto done;
7798 err = got_ref_write(*tmp_branch, repo);
7799 if (err)
7800 goto done;
7802 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7803 if (err)
7804 goto done;
7805 done:
7806 free(fileindex_path);
7807 free(tmp_branch_name);
7808 free(branch_ref_name);
7809 free(base_commit_ref_name);
7810 if (wt_branch)
7811 got_ref_close(wt_branch);
7812 if (err) {
7813 if (*branch_ref) {
7814 got_ref_close(*branch_ref);
7815 *branch_ref = NULL;
7817 if (*tmp_branch) {
7818 got_ref_close(*tmp_branch);
7819 *tmp_branch = NULL;
7821 free(*base_commit_id);
7822 if (*fileindex) {
7823 got_fileindex_free(*fileindex);
7824 *fileindex = NULL;
7826 lock_worktree(worktree, LOCK_SH);
7828 return err;
7831 const struct got_error *
7832 got_worktree_histedit_postpone(struct got_worktree *worktree,
7833 struct got_fileindex *fileindex)
7835 if (fileindex)
7836 got_fileindex_free(fileindex);
7837 return lock_worktree(worktree, LOCK_SH);
7840 const struct got_error *
7841 got_worktree_histedit_in_progress(int *in_progress,
7842 struct got_worktree *worktree)
7844 const struct got_error *err;
7845 char *tmp_branch_name = NULL;
7847 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7848 if (err)
7849 return err;
7851 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7852 free(tmp_branch_name);
7853 return NULL;
7856 const struct got_error *
7857 got_worktree_histedit_continue(struct got_object_id **commit_id,
7858 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7859 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7860 struct got_worktree *worktree, struct got_repository *repo)
7862 const struct got_error *err;
7863 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7864 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7865 struct got_reference *commit_ref = NULL;
7866 struct got_reference *base_commit_ref = NULL;
7867 char *fileindex_path = NULL;
7868 int have_staged_files = 0;
7870 *commit_id = NULL;
7871 *tmp_branch = NULL;
7872 *base_commit_id = NULL;
7873 *fileindex = NULL;
7875 err = lock_worktree(worktree, LOCK_EX);
7876 if (err)
7877 return err;
7879 err = open_fileindex(fileindex, &fileindex_path, worktree);
7880 if (err)
7881 goto done;
7883 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7884 &have_staged_files);
7885 if (err && err->code != GOT_ERR_CANCELLED)
7886 goto done;
7887 if (have_staged_files) {
7888 err = got_error(GOT_ERR_STAGED_PATHS);
7889 goto done;
7892 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7893 if (err)
7894 goto done;
7896 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7897 if (err)
7898 goto done;
7900 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7901 if (err)
7902 goto done;
7904 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7905 worktree);
7906 if (err)
7907 goto done;
7909 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7910 if (err)
7911 goto done;
7913 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7914 if (err)
7915 goto done;
7916 err = got_ref_resolve(commit_id, repo, commit_ref);
7917 if (err)
7918 goto done;
7920 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7921 if (err)
7922 goto done;
7923 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7924 if (err)
7925 goto done;
7927 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7928 if (err)
7929 goto done;
7930 done:
7931 free(commit_ref_name);
7932 free(branch_ref_name);
7933 free(fileindex_path);
7934 if (commit_ref)
7935 got_ref_close(commit_ref);
7936 if (base_commit_ref)
7937 got_ref_close(base_commit_ref);
7938 if (err) {
7939 free(*commit_id);
7940 *commit_id = NULL;
7941 free(*base_commit_id);
7942 *base_commit_id = NULL;
7943 if (*tmp_branch) {
7944 got_ref_close(*tmp_branch);
7945 *tmp_branch = NULL;
7947 if (*fileindex) {
7948 got_fileindex_free(*fileindex);
7949 *fileindex = NULL;
7951 lock_worktree(worktree, LOCK_EX);
7953 return err;
7956 static const struct got_error *
7957 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7959 const struct got_error *err;
7960 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7961 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7963 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7964 if (err)
7965 goto done;
7966 err = delete_ref(tmp_branch_name, repo);
7967 if (err)
7968 goto done;
7970 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7971 worktree);
7972 if (err)
7973 goto done;
7974 err = delete_ref(base_commit_ref_name, repo);
7975 if (err)
7976 goto done;
7978 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7979 if (err)
7980 goto done;
7981 err = delete_ref(branch_ref_name, repo);
7982 if (err)
7983 goto done;
7985 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7986 if (err)
7987 goto done;
7988 err = delete_ref(commit_ref_name, repo);
7989 if (err)
7990 goto done;
7991 done:
7992 free(tmp_branch_name);
7993 free(base_commit_ref_name);
7994 free(branch_ref_name);
7995 free(commit_ref_name);
7996 return err;
7999 const struct got_error *
8000 got_worktree_histedit_abort(struct got_worktree *worktree,
8001 struct got_fileindex *fileindex, struct got_repository *repo,
8002 struct got_reference *branch, struct got_object_id *base_commit_id,
8003 got_worktree_checkout_cb progress_cb, void *progress_arg)
8005 const struct got_error *err, *unlockerr, *sync_err;
8006 struct got_reference *resolved = NULL;
8007 char *fileindex_path = NULL;
8008 struct got_object_id *merged_commit_id = NULL;
8009 struct got_commit_object *commit = NULL;
8010 char *commit_ref_name = NULL;
8011 struct got_reference *commit_ref = NULL;
8012 struct got_object_id *tree_id = NULL;
8013 struct revert_file_args rfa;
8014 struct got_pathlist_head added_paths;
8016 TAILQ_INIT(&added_paths);
8018 err = lock_worktree(worktree, LOCK_EX);
8019 if (err)
8020 return err;
8022 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8023 if (err)
8024 goto done;
8026 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8027 if (err) {
8028 if (err->code != GOT_ERR_NOT_REF)
8029 goto done;
8030 /* Can happen on early abort due to invalid histedit script. */
8031 commit_ref = NULL;
8034 if (commit_ref) {
8035 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8036 if (err)
8037 goto done;
8040 * Determine which files in added status can be safely removed
8041 * from disk while reverting changes in the work tree.
8042 * We want to avoid deleting unrelated files added by the
8043 * user during conflict resolution or during histedit -e.
8045 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8046 got_worktree_get_path_prefix(worktree), repo);
8047 if (err)
8048 goto done;
8051 err = got_ref_open(&resolved, repo,
8052 got_ref_get_symref_target(branch), 0);
8053 if (err)
8054 goto done;
8056 err = got_worktree_set_head_ref(worktree, resolved);
8057 if (err)
8058 goto done;
8060 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8061 if (err)
8062 goto done;
8064 err = got_object_open_as_commit(&commit, repo,
8065 worktree->base_commit_id);
8066 if (err)
8067 goto done;
8069 err = got_object_id_by_path(&tree_id, repo, commit,
8070 worktree->path_prefix);
8071 if (err)
8072 goto done;
8074 err = delete_histedit_refs(worktree, repo);
8075 if (err)
8076 goto done;
8078 err = get_fileindex_path(&fileindex_path, worktree);
8079 if (err)
8080 goto done;
8082 rfa.worktree = worktree;
8083 rfa.fileindex = fileindex;
8084 rfa.progress_cb = progress_cb;
8085 rfa.progress_arg = progress_arg;
8086 rfa.patch_cb = NULL;
8087 rfa.patch_arg = NULL;
8088 rfa.repo = repo;
8089 rfa.unlink_added_files = 1;
8090 rfa.added_files_to_unlink = &added_paths;
8091 err = worktree_status(worktree, "", fileindex, repo,
8092 revert_file, &rfa, NULL, NULL, 1, 0);
8093 if (err)
8094 goto sync;
8096 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8097 repo, progress_cb, progress_arg, NULL, NULL);
8098 sync:
8099 sync_err = sync_fileindex(fileindex, fileindex_path);
8100 if (sync_err && err == NULL)
8101 err = sync_err;
8102 done:
8103 if (resolved)
8104 got_ref_close(resolved);
8105 if (commit_ref)
8106 got_ref_close(commit_ref);
8107 free(merged_commit_id);
8108 free(tree_id);
8109 free(fileindex_path);
8110 free(commit_ref_name);
8112 unlockerr = lock_worktree(worktree, LOCK_SH);
8113 if (unlockerr && err == NULL)
8114 err = unlockerr;
8115 return err;
8118 const struct got_error *
8119 got_worktree_histedit_complete(struct got_worktree *worktree,
8120 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8121 struct got_reference *edited_branch, struct got_repository *repo)
8123 const struct got_error *err, *unlockerr, *sync_err;
8124 struct got_object_id *new_head_commit_id = NULL;
8125 struct got_reference *resolved = NULL;
8126 char *fileindex_path = NULL;
8128 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8129 if (err)
8130 return err;
8132 err = got_ref_open(&resolved, repo,
8133 got_ref_get_symref_target(edited_branch), 0);
8134 if (err)
8135 goto done;
8137 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8138 resolved, new_head_commit_id, repo);
8139 if (err)
8140 goto done;
8142 err = got_ref_change_ref(resolved, new_head_commit_id);
8143 if (err)
8144 goto done;
8146 err = got_ref_write(resolved, repo);
8147 if (err)
8148 goto done;
8150 err = got_worktree_set_head_ref(worktree, resolved);
8151 if (err)
8152 goto done;
8154 err = delete_histedit_refs(worktree, repo);
8155 if (err)
8156 goto done;
8158 err = get_fileindex_path(&fileindex_path, worktree);
8159 if (err)
8160 goto done;
8161 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8162 sync_err = sync_fileindex(fileindex, fileindex_path);
8163 if (sync_err && err == NULL)
8164 err = sync_err;
8165 done:
8166 got_fileindex_free(fileindex);
8167 free(fileindex_path);
8168 free(new_head_commit_id);
8169 unlockerr = lock_worktree(worktree, LOCK_SH);
8170 if (unlockerr && err == NULL)
8171 err = unlockerr;
8172 return err;
8175 const struct got_error *
8176 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8177 struct got_object_id *commit_id, struct got_repository *repo)
8179 const struct got_error *err;
8180 char *commit_ref_name;
8182 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8183 if (err)
8184 return err;
8186 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8187 if (err)
8188 goto done;
8190 err = delete_ref(commit_ref_name, repo);
8191 done:
8192 free(commit_ref_name);
8193 return err;
8196 const struct got_error *
8197 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8198 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8199 struct got_worktree *worktree, const char *refname,
8200 struct got_repository *repo)
8202 const struct got_error *err = NULL;
8203 char *fileindex_path = NULL;
8204 struct check_rebase_ok_arg ok_arg;
8206 *fileindex = NULL;
8207 *branch_ref = NULL;
8208 *base_branch_ref = NULL;
8210 err = lock_worktree(worktree, LOCK_EX);
8211 if (err)
8212 return err;
8214 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8215 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8216 "cannot integrate a branch into itself; "
8217 "update -b or different branch name required");
8218 goto done;
8221 err = open_fileindex(fileindex, &fileindex_path, worktree);
8222 if (err)
8223 goto done;
8225 /* Preconditions are the same as for rebase. */
8226 ok_arg.worktree = worktree;
8227 ok_arg.repo = repo;
8228 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8229 &ok_arg);
8230 if (err)
8231 goto done;
8233 err = got_ref_open(branch_ref, repo, refname, 1);
8234 if (err)
8235 goto done;
8237 err = got_ref_open(base_branch_ref, repo,
8238 got_worktree_get_head_ref_name(worktree), 1);
8239 done:
8240 if (err) {
8241 if (*branch_ref) {
8242 got_ref_close(*branch_ref);
8243 *branch_ref = NULL;
8245 if (*base_branch_ref) {
8246 got_ref_close(*base_branch_ref);
8247 *base_branch_ref = NULL;
8249 if (*fileindex) {
8250 got_fileindex_free(*fileindex);
8251 *fileindex = NULL;
8253 lock_worktree(worktree, LOCK_SH);
8255 return err;
8258 const struct got_error *
8259 got_worktree_integrate_continue(struct got_worktree *worktree,
8260 struct got_fileindex *fileindex, struct got_repository *repo,
8261 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8262 got_worktree_checkout_cb progress_cb, void *progress_arg,
8263 got_cancel_cb cancel_cb, void *cancel_arg)
8265 const struct got_error *err = NULL, *sync_err, *unlockerr;
8266 char *fileindex_path = NULL;
8267 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8268 struct got_commit_object *commit = NULL;
8270 err = get_fileindex_path(&fileindex_path, worktree);
8271 if (err)
8272 goto done;
8274 err = got_ref_resolve(&commit_id, repo, branch_ref);
8275 if (err)
8276 goto done;
8278 err = got_object_open_as_commit(&commit, repo, commit_id);
8279 if (err)
8280 goto done;
8282 err = got_object_id_by_path(&tree_id, repo, commit,
8283 worktree->path_prefix);
8284 if (err)
8285 goto done;
8287 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8288 if (err)
8289 goto done;
8291 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8292 progress_cb, progress_arg, cancel_cb, cancel_arg);
8293 if (err)
8294 goto sync;
8296 err = got_ref_change_ref(base_branch_ref, commit_id);
8297 if (err)
8298 goto sync;
8300 err = got_ref_write(base_branch_ref, repo);
8301 if (err)
8302 goto sync;
8304 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8305 sync:
8306 sync_err = sync_fileindex(fileindex, fileindex_path);
8307 if (sync_err && err == NULL)
8308 err = sync_err;
8310 done:
8311 unlockerr = got_ref_unlock(branch_ref);
8312 if (unlockerr && err == NULL)
8313 err = unlockerr;
8314 got_ref_close(branch_ref);
8316 unlockerr = got_ref_unlock(base_branch_ref);
8317 if (unlockerr && err == NULL)
8318 err = unlockerr;
8319 got_ref_close(base_branch_ref);
8321 got_fileindex_free(fileindex);
8322 free(fileindex_path);
8323 free(tree_id);
8324 if (commit)
8325 got_object_commit_close(commit);
8327 unlockerr = lock_worktree(worktree, LOCK_SH);
8328 if (unlockerr && err == NULL)
8329 err = unlockerr;
8330 return err;
8333 const struct got_error *
8334 got_worktree_integrate_abort(struct got_worktree *worktree,
8335 struct got_fileindex *fileindex, struct got_repository *repo,
8336 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8338 const struct got_error *err = NULL, *unlockerr = NULL;
8340 got_fileindex_free(fileindex);
8342 err = lock_worktree(worktree, LOCK_SH);
8344 unlockerr = got_ref_unlock(branch_ref);
8345 if (unlockerr && err == NULL)
8346 err = unlockerr;
8347 got_ref_close(branch_ref);
8349 unlockerr = got_ref_unlock(base_branch_ref);
8350 if (unlockerr && err == NULL)
8351 err = unlockerr;
8352 got_ref_close(base_branch_ref);
8354 return err;
8357 const struct got_error *
8358 got_worktree_merge_postpone(struct got_worktree *worktree,
8359 struct got_fileindex *fileindex)
8361 const struct got_error *err, *sync_err;
8362 char *fileindex_path = NULL;
8364 err = get_fileindex_path(&fileindex_path, worktree);
8365 if (err)
8366 goto done;
8368 sync_err = sync_fileindex(fileindex, fileindex_path);
8370 err = lock_worktree(worktree, LOCK_SH);
8371 if (sync_err && err == NULL)
8372 err = sync_err;
8373 done:
8374 got_fileindex_free(fileindex);
8375 free(fileindex_path);
8376 return err;
8379 static const struct got_error *
8380 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8382 const struct got_error *err;
8383 char *branch_refname = NULL, *commit_refname = NULL;
8385 err = get_merge_branch_ref_name(&branch_refname, worktree);
8386 if (err)
8387 goto done;
8388 err = delete_ref(branch_refname, repo);
8389 if (err)
8390 goto done;
8392 err = get_merge_commit_ref_name(&commit_refname, worktree);
8393 if (err)
8394 goto done;
8395 err = delete_ref(commit_refname, repo);
8396 if (err)
8397 goto done;
8399 done:
8400 free(branch_refname);
8401 free(commit_refname);
8402 return err;
8405 struct merge_commit_msg_arg {
8406 struct got_worktree *worktree;
8407 const char *branch_name;
8410 static const struct got_error *
8411 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8412 const char *diff_path, char **logmsg, void *arg)
8414 struct merge_commit_msg_arg *a = arg;
8416 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8417 got_worktree_get_head_ref_name(a->worktree)) == -1)
8418 return got_error_from_errno("asprintf");
8420 return NULL;
8424 const struct got_error *
8425 got_worktree_merge_branch(struct got_worktree *worktree,
8426 struct got_fileindex *fileindex,
8427 struct got_object_id *yca_commit_id,
8428 struct got_object_id *branch_tip,
8429 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8430 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8432 const struct got_error *err;
8433 char *fileindex_path = NULL;
8434 struct check_mixed_commits_args cma;
8436 err = get_fileindex_path(&fileindex_path, worktree);
8437 if (err)
8438 goto done;
8440 cma.worktree = worktree;
8441 cma.cancel_cb = cancel_cb;
8442 cma.cancel_arg = cancel_arg;
8444 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8445 worktree);
8446 if (err)
8447 goto done;
8449 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8450 branch_tip, repo, progress_cb, progress_arg,
8451 cancel_cb, cancel_arg);
8452 done:
8453 free(fileindex_path);
8454 return err;
8457 const struct got_error *
8458 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8459 struct got_worktree *worktree, struct got_fileindex *fileindex,
8460 const char *author, const char *committer, int allow_bad_symlinks,
8461 struct got_object_id *branch_tip, const char *branch_name,
8462 int allow_conflict, struct got_repository *repo,
8463 got_worktree_status_cb status_cb, void *status_arg)
8466 const struct got_error *err = NULL, *sync_err;
8467 struct got_pathlist_head commitable_paths;
8468 struct collect_commitables_arg cc_arg;
8469 struct got_pathlist_entry *pe;
8470 struct got_reference *head_ref = NULL;
8471 struct got_object_id *head_commit_id = NULL;
8472 int have_staged_files = 0;
8473 struct merge_commit_msg_arg mcm_arg;
8474 char *fileindex_path = NULL;
8476 memset(&cc_arg, 0, sizeof(cc_arg));
8477 *new_commit_id = NULL;
8479 TAILQ_INIT(&commitable_paths);
8481 err = get_fileindex_path(&fileindex_path, worktree);
8482 if (err)
8483 goto done;
8485 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8486 if (err)
8487 goto done;
8489 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8490 if (err)
8491 goto done;
8493 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8494 &have_staged_files);
8495 if (err && err->code != GOT_ERR_CANCELLED)
8496 goto done;
8497 if (have_staged_files) {
8498 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8499 goto done;
8502 cc_arg.commitable_paths = &commitable_paths;
8503 cc_arg.worktree = worktree;
8504 cc_arg.fileindex = fileindex;
8505 cc_arg.repo = repo;
8506 cc_arg.have_staged_files = have_staged_files;
8507 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8508 cc_arg.commit_conflicts = allow_conflict;
8509 err = worktree_status(worktree, "", fileindex, repo,
8510 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8511 if (err)
8512 goto done;
8514 mcm_arg.worktree = worktree;
8515 mcm_arg.branch_name = branch_name;
8516 err = commit_worktree(new_commit_id, &commitable_paths,
8517 head_commit_id, branch_tip, worktree, author, committer, NULL,
8518 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8519 if (err)
8520 goto done;
8522 err = update_fileindex_after_commit(worktree, &commitable_paths,
8523 *new_commit_id, fileindex, have_staged_files);
8524 sync_err = sync_fileindex(fileindex, fileindex_path);
8525 if (sync_err && err == NULL)
8526 err = sync_err;
8527 done:
8528 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8529 struct got_commitable *ct = pe->data;
8531 free_commitable(ct);
8533 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8534 free(fileindex_path);
8535 return err;
8538 const struct got_error *
8539 got_worktree_merge_complete(struct got_worktree *worktree,
8540 struct got_fileindex *fileindex, struct got_repository *repo)
8542 const struct got_error *err, *unlockerr, *sync_err;
8543 char *fileindex_path = NULL;
8545 err = delete_merge_refs(worktree, repo);
8546 if (err)
8547 goto done;
8549 err = get_fileindex_path(&fileindex_path, worktree);
8550 if (err)
8551 goto done;
8552 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8553 sync_err = sync_fileindex(fileindex, fileindex_path);
8554 if (sync_err && err == NULL)
8555 err = sync_err;
8556 done:
8557 got_fileindex_free(fileindex);
8558 free(fileindex_path);
8559 unlockerr = lock_worktree(worktree, LOCK_SH);
8560 if (unlockerr && err == NULL)
8561 err = unlockerr;
8562 return err;
8565 const struct got_error *
8566 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8567 struct got_repository *repo)
8569 const struct got_error *err;
8570 char *branch_refname = NULL;
8571 struct got_reference *branch_ref = NULL;
8573 *in_progress = 0;
8575 err = get_merge_branch_ref_name(&branch_refname, worktree);
8576 if (err)
8577 return err;
8578 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8579 free(branch_refname);
8580 if (err) {
8581 if (err->code != GOT_ERR_NOT_REF)
8582 return err;
8583 } else
8584 *in_progress = 1;
8586 return NULL;
8589 const struct got_error *got_worktree_merge_prepare(
8590 struct got_fileindex **fileindex, struct got_worktree *worktree,
8591 struct got_repository *repo)
8593 const struct got_error *err = NULL;
8594 char *fileindex_path = NULL;
8595 struct got_reference *wt_branch = NULL;
8596 struct got_object_id *wt_branch_tip = NULL;
8597 struct check_rebase_ok_arg ok_arg;
8599 *fileindex = NULL;
8601 err = lock_worktree(worktree, LOCK_EX);
8602 if (err)
8603 return err;
8605 err = open_fileindex(fileindex, &fileindex_path, worktree);
8606 if (err)
8607 goto done;
8609 /* Preconditions are the same as for rebase. */
8610 ok_arg.worktree = worktree;
8611 ok_arg.repo = repo;
8612 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8613 &ok_arg);
8614 if (err)
8615 goto done;
8617 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8618 0);
8619 if (err)
8620 goto done;
8622 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8623 if (err)
8624 goto done;
8626 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8627 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8628 goto done;
8631 done:
8632 free(fileindex_path);
8633 if (wt_branch)
8634 got_ref_close(wt_branch);
8635 free(wt_branch_tip);
8636 if (err) {
8637 if (*fileindex) {
8638 got_fileindex_free(*fileindex);
8639 *fileindex = NULL;
8641 lock_worktree(worktree, LOCK_SH);
8643 return err;
8646 const struct got_error *got_worktree_merge_write_refs(
8647 struct got_worktree *worktree, struct got_reference *branch,
8648 struct got_repository *repo)
8650 const struct got_error *err = NULL;
8651 char *branch_refname = NULL, *commit_refname = NULL;
8652 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8653 struct got_object_id *branch_tip = NULL;
8655 err = get_merge_branch_ref_name(&branch_refname, worktree);
8656 if (err)
8657 return err;
8659 err = get_merge_commit_ref_name(&commit_refname, worktree);
8660 if (err)
8661 return err;
8663 err = got_ref_resolve(&branch_tip, repo, branch);
8664 if (err)
8665 goto done;
8667 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8668 if (err)
8669 goto done;
8670 err = got_ref_write(branch_ref, repo);
8671 if (err)
8672 goto done;
8674 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8675 if (err)
8676 goto done;
8677 err = got_ref_write(commit_ref, repo);
8678 if (err)
8679 goto done;
8681 done:
8682 free(branch_refname);
8683 free(commit_refname);
8684 if (branch_ref)
8685 got_ref_close(branch_ref);
8686 if (commit_ref)
8687 got_ref_close(commit_ref);
8688 free(branch_tip);
8689 return err;
8692 const struct got_error *
8693 got_worktree_merge_continue(char **branch_name,
8694 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8695 struct got_worktree *worktree, struct got_repository *repo)
8697 const struct got_error *err;
8698 char *commit_refname = NULL, *branch_refname = NULL;
8699 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8700 char *fileindex_path = NULL;
8701 int have_staged_files = 0;
8703 *branch_name = NULL;
8704 *branch_tip = NULL;
8705 *fileindex = NULL;
8707 err = lock_worktree(worktree, LOCK_EX);
8708 if (err)
8709 return err;
8711 err = open_fileindex(fileindex, &fileindex_path, worktree);
8712 if (err)
8713 goto done;
8715 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8716 &have_staged_files);
8717 if (err && err->code != GOT_ERR_CANCELLED)
8718 goto done;
8719 if (have_staged_files) {
8720 err = got_error(GOT_ERR_STAGED_PATHS);
8721 goto done;
8724 err = get_merge_branch_ref_name(&branch_refname, worktree);
8725 if (err)
8726 goto done;
8728 err = get_merge_commit_ref_name(&commit_refname, worktree);
8729 if (err)
8730 goto done;
8732 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8733 if (err)
8734 goto done;
8736 if (!got_ref_is_symbolic(branch_ref)) {
8737 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8738 "%s is not a symbolic reference",
8739 got_ref_get_name(branch_ref));
8740 goto done;
8742 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8743 if (*branch_name == NULL) {
8744 err = got_error_from_errno("strdup");
8745 goto done;
8748 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8749 if (err)
8750 goto done;
8752 err = got_ref_resolve(branch_tip, repo, commit_ref);
8753 if (err)
8754 goto done;
8755 done:
8756 free(commit_refname);
8757 free(branch_refname);
8758 free(fileindex_path);
8759 if (commit_ref)
8760 got_ref_close(commit_ref);
8761 if (branch_ref)
8762 got_ref_close(branch_ref);
8763 if (err) {
8764 if (*branch_name) {
8765 free(*branch_name);
8766 *branch_name = NULL;
8768 free(*branch_tip);
8769 *branch_tip = NULL;
8770 if (*fileindex) {
8771 got_fileindex_free(*fileindex);
8772 *fileindex = NULL;
8774 lock_worktree(worktree, LOCK_SH);
8776 return err;
8779 const struct got_error *
8780 got_worktree_merge_abort(struct got_worktree *worktree,
8781 struct got_fileindex *fileindex, struct got_repository *repo,
8782 got_worktree_checkout_cb progress_cb, void *progress_arg)
8784 const struct got_error *err, *unlockerr, *sync_err;
8785 struct got_commit_object *commit = NULL;
8786 char *fileindex_path = NULL;
8787 struct revert_file_args rfa;
8788 char *commit_ref_name = NULL;
8789 struct got_reference *commit_ref = NULL;
8790 struct got_object_id *merged_commit_id = NULL;
8791 struct got_object_id *tree_id = NULL;
8792 struct got_pathlist_head added_paths;
8794 TAILQ_INIT(&added_paths);
8796 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8797 if (err)
8798 goto done;
8800 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8801 if (err)
8802 goto done;
8804 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8805 if (err)
8806 goto done;
8809 * Determine which files in added status can be safely removed
8810 * from disk while reverting changes in the work tree.
8811 * We want to avoid deleting unrelated files which were added by
8812 * the user for conflict resolution purposes.
8814 err = get_paths_added_between_commits(&added_paths,
8815 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8816 got_worktree_get_path_prefix(worktree), repo);
8817 if (err)
8818 goto done;
8821 err = got_object_open_as_commit(&commit, repo,
8822 worktree->base_commit_id);
8823 if (err)
8824 goto done;
8826 err = got_object_id_by_path(&tree_id, repo, commit,
8827 worktree->path_prefix);
8828 if (err)
8829 goto done;
8831 err = delete_merge_refs(worktree, repo);
8832 if (err)
8833 goto done;
8835 err = get_fileindex_path(&fileindex_path, worktree);
8836 if (err)
8837 goto done;
8839 rfa.worktree = worktree;
8840 rfa.fileindex = fileindex;
8841 rfa.progress_cb = progress_cb;
8842 rfa.progress_arg = progress_arg;
8843 rfa.patch_cb = NULL;
8844 rfa.patch_arg = NULL;
8845 rfa.repo = repo;
8846 rfa.unlink_added_files = 1;
8847 rfa.added_files_to_unlink = &added_paths;
8848 err = worktree_status(worktree, "", fileindex, repo,
8849 revert_file, &rfa, NULL, NULL, 1, 0);
8850 if (err)
8851 goto sync;
8853 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8854 repo, progress_cb, progress_arg, NULL, NULL);
8855 sync:
8856 sync_err = sync_fileindex(fileindex, fileindex_path);
8857 if (sync_err && err == NULL)
8858 err = sync_err;
8859 done:
8860 free(tree_id);
8861 free(merged_commit_id);
8862 if (commit)
8863 got_object_commit_close(commit);
8864 if (fileindex)
8865 got_fileindex_free(fileindex);
8866 free(fileindex_path);
8867 if (commit_ref)
8868 got_ref_close(commit_ref);
8869 free(commit_ref_name);
8871 unlockerr = lock_worktree(worktree, LOCK_SH);
8872 if (unlockerr && err == NULL)
8873 err = unlockerr;
8874 return err;
8877 struct check_stage_ok_arg {
8878 struct got_object_id *head_commit_id;
8879 struct got_worktree *worktree;
8880 struct got_fileindex *fileindex;
8881 struct got_repository *repo;
8882 int have_changes;
8885 static const struct got_error *
8886 check_stage_ok(void *arg, unsigned char status,
8887 unsigned char staged_status, const char *relpath,
8888 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8889 struct got_object_id *commit_id, int dirfd, const char *de_name)
8891 struct check_stage_ok_arg *a = arg;
8892 const struct got_error *err = NULL;
8893 struct got_fileindex_entry *ie;
8894 struct got_object_id base_commit_id;
8895 struct got_object_id *base_commit_idp = NULL;
8896 char *in_repo_path = NULL, *p;
8898 if (status == GOT_STATUS_UNVERSIONED ||
8899 status == GOT_STATUS_NO_CHANGE)
8900 return NULL;
8901 if (status == GOT_STATUS_NONEXISTENT)
8902 return got_error_set_errno(ENOENT, relpath);
8904 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8905 if (ie == NULL)
8906 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8908 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8909 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8910 relpath) == -1)
8911 return got_error_from_errno("asprintf");
8913 if (got_fileindex_entry_has_commit(ie)) {
8914 base_commit_idp = got_fileindex_entry_get_commit_id(
8915 &base_commit_id, ie);
8918 if (status == GOT_STATUS_CONFLICT) {
8919 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8920 goto done;
8921 } else if (status != GOT_STATUS_ADD &&
8922 status != GOT_STATUS_MODIFY &&
8923 status != GOT_STATUS_DELETE) {
8924 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8925 goto done;
8928 a->have_changes = 1;
8930 p = in_repo_path;
8931 while (p[0] == '/')
8932 p++;
8933 err = check_out_of_date(p, status, staged_status,
8934 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8935 GOT_ERR_STAGE_OUT_OF_DATE);
8936 done:
8937 free(in_repo_path);
8938 return err;
8941 struct stage_path_arg {
8942 struct got_worktree *worktree;
8943 struct got_fileindex *fileindex;
8944 struct got_repository *repo;
8945 got_worktree_status_cb status_cb;
8946 void *status_arg;
8947 got_worktree_patch_cb patch_cb;
8948 void *patch_arg;
8949 int staged_something;
8950 int allow_bad_symlinks;
8953 static const struct got_error *
8954 stage_path(void *arg, unsigned char status,
8955 unsigned char staged_status, const char *relpath,
8956 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8957 struct got_object_id *commit_id, int dirfd, const char *de_name)
8959 struct stage_path_arg *a = arg;
8960 const struct got_error *err = NULL;
8961 struct got_fileindex_entry *ie;
8962 char *ondisk_path = NULL, *path_content = NULL;
8963 uint32_t stage;
8964 struct got_object_id *new_staged_blob_id = NULL;
8965 struct stat sb;
8967 if (status == GOT_STATUS_UNVERSIONED)
8968 return NULL;
8970 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8971 if (ie == NULL)
8972 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8974 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8975 relpath)== -1)
8976 return got_error_from_errno("asprintf");
8978 switch (status) {
8979 case GOT_STATUS_ADD:
8980 case GOT_STATUS_MODIFY:
8981 /* XXX could sb.st_mode be passed in by our caller? */
8982 if (lstat(ondisk_path, &sb) == -1) {
8983 err = got_error_from_errno2("lstat", ondisk_path);
8984 break;
8986 if (a->patch_cb) {
8987 if (status == GOT_STATUS_ADD) {
8988 int choice = GOT_PATCH_CHOICE_NONE;
8989 err = (*a->patch_cb)(&choice, a->patch_arg,
8990 status, ie->path, NULL, 1, 1);
8991 if (err)
8992 break;
8993 if (choice != GOT_PATCH_CHOICE_YES)
8994 break;
8995 } else {
8996 err = create_patched_content(&path_content, 0,
8997 staged_blob_id ? staged_blob_id : blob_id,
8998 ondisk_path, dirfd, de_name, ie->path,
8999 a->repo, a->patch_cb, a->patch_arg);
9000 if (err || path_content == NULL)
9001 break;
9004 err = got_object_blob_create(&new_staged_blob_id,
9005 path_content ? path_content : ondisk_path, a->repo);
9006 if (err)
9007 break;
9008 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9009 SHA1_DIGEST_LENGTH);
9010 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9011 stage = GOT_FILEIDX_STAGE_ADD;
9012 else
9013 stage = GOT_FILEIDX_STAGE_MODIFY;
9014 got_fileindex_entry_stage_set(ie, stage);
9015 if (S_ISLNK(sb.st_mode)) {
9016 int is_bad_symlink = 0;
9017 if (!a->allow_bad_symlinks) {
9018 char target_path[PATH_MAX];
9019 ssize_t target_len;
9020 target_len = readlink(ondisk_path, target_path,
9021 sizeof(target_path));
9022 if (target_len == -1) {
9023 err = got_error_from_errno2("readlink",
9024 ondisk_path);
9025 break;
9027 err = is_bad_symlink_target(&is_bad_symlink,
9028 target_path, target_len, ondisk_path,
9029 a->worktree->root_path,
9030 a->worktree->meta_dir);
9031 if (err)
9032 break;
9033 if (is_bad_symlink) {
9034 err = got_error_path(ondisk_path,
9035 GOT_ERR_BAD_SYMLINK);
9036 break;
9039 if (is_bad_symlink)
9040 got_fileindex_entry_staged_filetype_set(ie,
9041 GOT_FILEIDX_MODE_BAD_SYMLINK);
9042 else
9043 got_fileindex_entry_staged_filetype_set(ie,
9044 GOT_FILEIDX_MODE_SYMLINK);
9045 } else {
9046 got_fileindex_entry_staged_filetype_set(ie,
9047 GOT_FILEIDX_MODE_REGULAR_FILE);
9049 a->staged_something = 1;
9050 if (a->status_cb == NULL)
9051 break;
9052 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9053 get_staged_status(ie), relpath, blob_id,
9054 new_staged_blob_id, NULL, dirfd, de_name);
9055 if (err)
9056 break;
9058 * When staging the reverse of the staged diff,
9059 * implicitly unstage the file.
9061 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9062 sizeof(ie->blob_sha1)) == 0) {
9063 got_fileindex_entry_stage_set(ie,
9064 GOT_FILEIDX_STAGE_NONE);
9066 break;
9067 case GOT_STATUS_DELETE:
9068 if (staged_status == GOT_STATUS_DELETE)
9069 break;
9070 if (a->patch_cb) {
9071 int choice = GOT_PATCH_CHOICE_NONE;
9072 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9073 ie->path, NULL, 1, 1);
9074 if (err)
9075 break;
9076 if (choice == GOT_PATCH_CHOICE_NO)
9077 break;
9078 if (choice != GOT_PATCH_CHOICE_YES) {
9079 err = got_error(GOT_ERR_PATCH_CHOICE);
9080 break;
9083 stage = GOT_FILEIDX_STAGE_DELETE;
9084 got_fileindex_entry_stage_set(ie, stage);
9085 a->staged_something = 1;
9086 if (a->status_cb == NULL)
9087 break;
9088 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9089 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9090 de_name);
9091 break;
9092 case GOT_STATUS_NO_CHANGE:
9093 break;
9094 case GOT_STATUS_CONFLICT:
9095 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9096 break;
9097 case GOT_STATUS_NONEXISTENT:
9098 err = got_error_set_errno(ENOENT, relpath);
9099 break;
9100 default:
9101 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9102 break;
9105 if (path_content && unlink(path_content) == -1 && err == NULL)
9106 err = got_error_from_errno2("unlink", path_content);
9107 free(path_content);
9108 free(ondisk_path);
9109 free(new_staged_blob_id);
9110 return err;
9113 const struct got_error *
9114 got_worktree_stage(struct got_worktree *worktree,
9115 struct got_pathlist_head *paths,
9116 got_worktree_status_cb status_cb, void *status_arg,
9117 got_worktree_patch_cb patch_cb, void *patch_arg,
9118 int allow_bad_symlinks, struct got_repository *repo)
9120 const struct got_error *err = NULL, *sync_err, *unlockerr;
9121 struct got_pathlist_entry *pe;
9122 struct got_fileindex *fileindex = NULL;
9123 char *fileindex_path = NULL;
9124 struct got_reference *head_ref = NULL;
9125 struct got_object_id *head_commit_id = NULL;
9126 struct check_stage_ok_arg oka;
9127 struct stage_path_arg spa;
9129 err = lock_worktree(worktree, LOCK_EX);
9130 if (err)
9131 return err;
9133 err = got_ref_open(&head_ref, repo,
9134 got_worktree_get_head_ref_name(worktree), 0);
9135 if (err)
9136 goto done;
9137 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9138 if (err)
9139 goto done;
9140 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9141 if (err)
9142 goto done;
9144 /* Check pre-conditions before staging anything. */
9145 oka.head_commit_id = head_commit_id;
9146 oka.worktree = worktree;
9147 oka.fileindex = fileindex;
9148 oka.repo = repo;
9149 oka.have_changes = 0;
9150 TAILQ_FOREACH(pe, paths, entry) {
9151 err = worktree_status(worktree, pe->path, fileindex, repo,
9152 check_stage_ok, &oka, NULL, NULL, 1, 0);
9153 if (err)
9154 goto done;
9156 if (!oka.have_changes) {
9157 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9158 goto done;
9161 spa.worktree = worktree;
9162 spa.fileindex = fileindex;
9163 spa.repo = repo;
9164 spa.patch_cb = patch_cb;
9165 spa.patch_arg = patch_arg;
9166 spa.status_cb = status_cb;
9167 spa.status_arg = status_arg;
9168 spa.staged_something = 0;
9169 spa.allow_bad_symlinks = allow_bad_symlinks;
9170 TAILQ_FOREACH(pe, paths, entry) {
9171 err = worktree_status(worktree, pe->path, fileindex, repo,
9172 stage_path, &spa, NULL, NULL, 1, 0);
9173 if (err)
9174 goto done;
9176 if (!spa.staged_something) {
9177 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9178 goto done;
9181 sync_err = sync_fileindex(fileindex, fileindex_path);
9182 if (sync_err && err == NULL)
9183 err = sync_err;
9184 done:
9185 if (head_ref)
9186 got_ref_close(head_ref);
9187 free(head_commit_id);
9188 free(fileindex_path);
9189 if (fileindex)
9190 got_fileindex_free(fileindex);
9191 unlockerr = lock_worktree(worktree, LOCK_SH);
9192 if (unlockerr && err == NULL)
9193 err = unlockerr;
9194 return err;
9197 struct unstage_path_arg {
9198 struct got_worktree *worktree;
9199 struct got_fileindex *fileindex;
9200 struct got_repository *repo;
9201 got_worktree_checkout_cb progress_cb;
9202 void *progress_arg;
9203 got_worktree_patch_cb patch_cb;
9204 void *patch_arg;
9207 static const struct got_error *
9208 create_unstaged_content(char **path_unstaged_content,
9209 char **path_new_staged_content, struct got_object_id *blob_id,
9210 struct got_object_id *staged_blob_id, const char *relpath,
9211 struct got_repository *repo,
9212 got_worktree_patch_cb patch_cb, void *patch_arg)
9214 const struct got_error *err, *free_err;
9215 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9216 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9217 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9218 struct got_diffreg_result *diffreg_result = NULL;
9219 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9220 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9221 int fd1 = -1, fd2 = -1;
9223 *path_unstaged_content = NULL;
9224 *path_new_staged_content = NULL;
9226 err = got_object_id_str(&label1, blob_id);
9227 if (err)
9228 return err;
9230 fd1 = got_opentempfd();
9231 if (fd1 == -1) {
9232 err = got_error_from_errno("got_opentempfd");
9233 goto done;
9235 fd2 = got_opentempfd();
9236 if (fd2 == -1) {
9237 err = got_error_from_errno("got_opentempfd");
9238 goto done;
9241 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9242 if (err)
9243 goto done;
9245 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9246 if (err)
9247 goto done;
9249 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9250 if (err)
9251 goto done;
9253 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9254 fd2);
9255 if (err)
9256 goto done;
9258 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9259 if (err)
9260 goto done;
9262 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9263 if (err)
9264 goto done;
9266 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9267 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9268 if (err)
9269 goto done;
9271 err = got_opentemp_named(path_unstaged_content, &outfile,
9272 "got-unstaged-content", "");
9273 if (err)
9274 goto done;
9275 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9276 "got-new-staged-content", "");
9277 if (err)
9278 goto done;
9280 if (fseek(f1, 0L, SEEK_SET) == -1) {
9281 err = got_ferror(f1, GOT_ERR_IO);
9282 goto done;
9284 if (fseek(f2, 0L, SEEK_SET) == -1) {
9285 err = got_ferror(f2, GOT_ERR_IO);
9286 goto done;
9288 /* Count the number of actual changes in the diff result. */
9289 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9290 struct diff_chunk_context cc = {};
9291 diff_chunk_context_load_change(&cc, &nchunks_used,
9292 diffreg_result->result, n, 0);
9293 nchanges++;
9295 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9296 int choice;
9297 err = apply_or_reject_change(&choice, &nchunks_used,
9298 diffreg_result->result, n, relpath, f1, f2,
9299 &line_cur1, &line_cur2,
9300 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9301 if (err)
9302 goto done;
9303 if (choice == GOT_PATCH_CHOICE_YES)
9304 have_content = 1;
9305 else
9306 have_rejected_content = 1;
9307 if (choice == GOT_PATCH_CHOICE_QUIT)
9308 break;
9310 if (have_content || have_rejected_content)
9311 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9312 outfile, rejectfile);
9313 done:
9314 free(label1);
9315 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9316 err = got_error_from_errno("close");
9317 if (blob)
9318 got_object_blob_close(blob);
9319 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9320 err = got_error_from_errno("close");
9321 if (staged_blob)
9322 got_object_blob_close(staged_blob);
9323 free_err = got_diffreg_result_free(diffreg_result);
9324 if (free_err && err == NULL)
9325 err = free_err;
9326 if (f1 && fclose(f1) == EOF && err == NULL)
9327 err = got_error_from_errno2("fclose", path1);
9328 if (f2 && fclose(f2) == EOF && err == NULL)
9329 err = got_error_from_errno2("fclose", path2);
9330 if (outfile && fclose(outfile) == EOF && err == NULL)
9331 err = got_error_from_errno2("fclose", *path_unstaged_content);
9332 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9333 err = got_error_from_errno2("fclose", *path_new_staged_content);
9334 if (path1 && unlink(path1) == -1 && err == NULL)
9335 err = got_error_from_errno2("unlink", path1);
9336 if (path2 && unlink(path2) == -1 && err == NULL)
9337 err = got_error_from_errno2("unlink", path2);
9338 if (err || !have_content) {
9339 if (*path_unstaged_content &&
9340 unlink(*path_unstaged_content) == -1 && err == NULL)
9341 err = got_error_from_errno2("unlink",
9342 *path_unstaged_content);
9343 free(*path_unstaged_content);
9344 *path_unstaged_content = NULL;
9346 if (err || !have_content || !have_rejected_content) {
9347 if (*path_new_staged_content &&
9348 unlink(*path_new_staged_content) == -1 && err == NULL)
9349 err = got_error_from_errno2("unlink",
9350 *path_new_staged_content);
9351 free(*path_new_staged_content);
9352 *path_new_staged_content = NULL;
9354 free(path1);
9355 free(path2);
9356 return err;
9359 static const struct got_error *
9360 unstage_hunks(struct got_object_id *staged_blob_id,
9361 struct got_blob_object *blob_base,
9362 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9363 const char *ondisk_path, const char *label_orig,
9364 struct got_worktree *worktree, struct got_repository *repo,
9365 got_worktree_patch_cb patch_cb, void *patch_arg,
9366 got_worktree_checkout_cb progress_cb, void *progress_arg)
9368 const struct got_error *err = NULL;
9369 char *path_unstaged_content = NULL;
9370 char *path_new_staged_content = NULL;
9371 char *parent = NULL, *base_path = NULL;
9372 char *blob_base_path = NULL;
9373 struct got_object_id *new_staged_blob_id = NULL;
9374 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9375 struct stat sb;
9377 err = create_unstaged_content(&path_unstaged_content,
9378 &path_new_staged_content, blob_id, staged_blob_id,
9379 ie->path, repo, patch_cb, patch_arg);
9380 if (err)
9381 return err;
9383 if (path_unstaged_content == NULL)
9384 return NULL;
9386 if (path_new_staged_content) {
9387 err = got_object_blob_create(&new_staged_blob_id,
9388 path_new_staged_content, repo);
9389 if (err)
9390 goto done;
9393 f = fopen(path_unstaged_content, "re");
9394 if (f == NULL) {
9395 err = got_error_from_errno2("fopen",
9396 path_unstaged_content);
9397 goto done;
9399 if (fstat(fileno(f), &sb) == -1) {
9400 err = got_error_from_errno2("fstat", path_unstaged_content);
9401 goto done;
9403 if (got_fileindex_entry_staged_filetype_get(ie) ==
9404 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9405 char link_target[PATH_MAX];
9406 size_t r;
9407 r = fread(link_target, 1, sizeof(link_target), f);
9408 if (r == 0 && ferror(f)) {
9409 err = got_error_from_errno("fread");
9410 goto done;
9412 if (r >= sizeof(link_target)) { /* should not happen */
9413 err = got_error(GOT_ERR_NO_SPACE);
9414 goto done;
9416 link_target[r] = '\0';
9417 err = merge_symlink(worktree, blob_base,
9418 ondisk_path, ie->path, label_orig, link_target,
9419 worktree->base_commit_id, repo, progress_cb,
9420 progress_arg);
9421 } else {
9422 int local_changes_subsumed;
9424 err = got_path_dirname(&parent, ondisk_path);
9425 if (err)
9426 return err;
9428 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9429 parent) == -1) {
9430 err = got_error_from_errno("asprintf");
9431 base_path = NULL;
9432 goto done;
9435 err = got_opentemp_named(&blob_base_path, &f_base,
9436 base_path, "");
9437 if (err)
9438 goto done;
9439 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9440 blob_base);
9441 if (err)
9442 goto done;
9445 * In order the run a 3-way merge with a symlink we copy the symlink's
9446 * target path into a temporary file and use that file with diff3.
9448 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9449 err = dump_symlink_target_path_to_file(&f_deriv2,
9450 ondisk_path);
9451 if (err)
9452 goto done;
9453 } else {
9454 int fd;
9455 fd = open(ondisk_path,
9456 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9457 if (fd == -1) {
9458 err = got_error_from_errno2("open", ondisk_path);
9459 goto done;
9461 f_deriv2 = fdopen(fd, "r");
9462 if (f_deriv2 == NULL) {
9463 err = got_error_from_errno2("fdopen", ondisk_path);
9464 close(fd);
9465 goto done;
9469 err = merge_file(&local_changes_subsumed, worktree,
9470 f_base, f, f_deriv2, ondisk_path, ie->path,
9471 got_fileindex_perms_to_st(ie),
9472 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9473 repo, progress_cb, progress_arg);
9475 if (err)
9476 goto done;
9478 if (new_staged_blob_id) {
9479 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9480 SHA1_DIGEST_LENGTH);
9481 } else {
9482 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9483 got_fileindex_entry_staged_filetype_set(ie, 0);
9485 done:
9486 free(new_staged_blob_id);
9487 if (path_unstaged_content &&
9488 unlink(path_unstaged_content) == -1 && err == NULL)
9489 err = got_error_from_errno2("unlink", path_unstaged_content);
9490 if (path_new_staged_content &&
9491 unlink(path_new_staged_content) == -1 && err == NULL)
9492 err = got_error_from_errno2("unlink", path_new_staged_content);
9493 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9494 err = got_error_from_errno2("unlink", blob_base_path);
9495 if (f_base && fclose(f_base) == EOF && err == NULL)
9496 err = got_error_from_errno2("fclose", path_unstaged_content);
9497 if (f && fclose(f) == EOF && err == NULL)
9498 err = got_error_from_errno2("fclose", path_unstaged_content);
9499 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9500 err = got_error_from_errno2("fclose", ondisk_path);
9501 free(path_unstaged_content);
9502 free(path_new_staged_content);
9503 free(blob_base_path);
9504 free(parent);
9505 free(base_path);
9506 return err;
9509 static const struct got_error *
9510 unstage_path(void *arg, unsigned char status,
9511 unsigned char staged_status, const char *relpath,
9512 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9513 struct got_object_id *commit_id, int dirfd, const char *de_name)
9515 const struct got_error *err = NULL;
9516 struct unstage_path_arg *a = arg;
9517 struct got_fileindex_entry *ie;
9518 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9519 char *ondisk_path = NULL;
9520 char *id_str = NULL, *label_orig = NULL;
9521 int local_changes_subsumed;
9522 struct stat sb;
9523 int fd1 = -1, fd2 = -1;
9525 if (staged_status != GOT_STATUS_ADD &&
9526 staged_status != GOT_STATUS_MODIFY &&
9527 staged_status != GOT_STATUS_DELETE)
9528 return NULL;
9530 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9531 if (ie == NULL)
9532 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9534 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9535 == -1)
9536 return got_error_from_errno("asprintf");
9538 err = got_object_id_str(&id_str,
9539 commit_id ? commit_id : a->worktree->base_commit_id);
9540 if (err)
9541 goto done;
9542 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9543 id_str) == -1) {
9544 err = got_error_from_errno("asprintf");
9545 goto done;
9548 fd1 = got_opentempfd();
9549 if (fd1 == -1) {
9550 err = got_error_from_errno("got_opentempfd");
9551 goto done;
9553 fd2 = got_opentempfd();
9554 if (fd2 == -1) {
9555 err = got_error_from_errno("got_opentempfd");
9556 goto done;
9559 switch (staged_status) {
9560 case GOT_STATUS_MODIFY:
9561 err = got_object_open_as_blob(&blob_base, a->repo,
9562 blob_id, 8192, fd1);
9563 if (err)
9564 break;
9565 /* fall through */
9566 case GOT_STATUS_ADD:
9567 if (a->patch_cb) {
9568 if (staged_status == GOT_STATUS_ADD) {
9569 int choice = GOT_PATCH_CHOICE_NONE;
9570 err = (*a->patch_cb)(&choice, a->patch_arg,
9571 staged_status, ie->path, NULL, 1, 1);
9572 if (err)
9573 break;
9574 if (choice != GOT_PATCH_CHOICE_YES)
9575 break;
9576 } else {
9577 err = unstage_hunks(staged_blob_id,
9578 blob_base, blob_id, ie, ondisk_path,
9579 label_orig, a->worktree, a->repo,
9580 a->patch_cb, a->patch_arg,
9581 a->progress_cb, a->progress_arg);
9582 break; /* Done with this file. */
9585 err = got_object_open_as_blob(&blob_staged, a->repo,
9586 staged_blob_id, 8192, fd2);
9587 if (err)
9588 break;
9589 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9590 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9591 case GOT_FILEIDX_MODE_REGULAR_FILE:
9592 err = merge_blob(&local_changes_subsumed, a->worktree,
9593 blob_base, ondisk_path, relpath,
9594 got_fileindex_perms_to_st(ie), label_orig,
9595 blob_staged, commit_id ? commit_id :
9596 a->worktree->base_commit_id, a->repo,
9597 a->progress_cb, a->progress_arg);
9598 break;
9599 case GOT_FILEIDX_MODE_SYMLINK:
9600 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9601 char *staged_target;
9602 err = got_object_blob_read_to_str(
9603 &staged_target, blob_staged);
9604 if (err)
9605 goto done;
9606 err = merge_symlink(a->worktree, blob_base,
9607 ondisk_path, relpath, label_orig,
9608 staged_target, commit_id ? commit_id :
9609 a->worktree->base_commit_id,
9610 a->repo, a->progress_cb, a->progress_arg);
9611 free(staged_target);
9612 } else {
9613 err = merge_blob(&local_changes_subsumed,
9614 a->worktree, blob_base, ondisk_path,
9615 relpath, got_fileindex_perms_to_st(ie),
9616 label_orig, blob_staged,
9617 commit_id ? commit_id :
9618 a->worktree->base_commit_id, a->repo,
9619 a->progress_cb, a->progress_arg);
9621 break;
9622 default:
9623 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9624 break;
9626 if (err == NULL) {
9627 got_fileindex_entry_stage_set(ie,
9628 GOT_FILEIDX_STAGE_NONE);
9629 got_fileindex_entry_staged_filetype_set(ie, 0);
9631 break;
9632 case GOT_STATUS_DELETE:
9633 if (a->patch_cb) {
9634 int choice = GOT_PATCH_CHOICE_NONE;
9635 err = (*a->patch_cb)(&choice, a->patch_arg,
9636 staged_status, ie->path, NULL, 1, 1);
9637 if (err)
9638 break;
9639 if (choice == GOT_PATCH_CHOICE_NO)
9640 break;
9641 if (choice != GOT_PATCH_CHOICE_YES) {
9642 err = got_error(GOT_ERR_PATCH_CHOICE);
9643 break;
9646 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9647 got_fileindex_entry_staged_filetype_set(ie, 0);
9648 err = get_file_status(&status, &sb, ie, ondisk_path,
9649 dirfd, de_name, a->repo);
9650 if (err)
9651 break;
9652 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9653 break;
9655 done:
9656 free(ondisk_path);
9657 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9658 err = got_error_from_errno("close");
9659 if (blob_base)
9660 got_object_blob_close(blob_base);
9661 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9662 err = got_error_from_errno("close");
9663 if (blob_staged)
9664 got_object_blob_close(blob_staged);
9665 free(id_str);
9666 free(label_orig);
9667 return err;
9670 const struct got_error *
9671 got_worktree_unstage(struct got_worktree *worktree,
9672 struct got_pathlist_head *paths,
9673 got_worktree_checkout_cb progress_cb, void *progress_arg,
9674 got_worktree_patch_cb patch_cb, void *patch_arg,
9675 struct got_repository *repo)
9677 const struct got_error *err = NULL, *sync_err, *unlockerr;
9678 struct got_pathlist_entry *pe;
9679 struct got_fileindex *fileindex = NULL;
9680 char *fileindex_path = NULL;
9681 struct unstage_path_arg upa;
9683 err = lock_worktree(worktree, LOCK_EX);
9684 if (err)
9685 return err;
9687 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9688 if (err)
9689 goto done;
9691 upa.worktree = worktree;
9692 upa.fileindex = fileindex;
9693 upa.repo = repo;
9694 upa.progress_cb = progress_cb;
9695 upa.progress_arg = progress_arg;
9696 upa.patch_cb = patch_cb;
9697 upa.patch_arg = patch_arg;
9698 TAILQ_FOREACH(pe, paths, entry) {
9699 err = worktree_status(worktree, pe->path, fileindex, repo,
9700 unstage_path, &upa, NULL, NULL, 1, 0);
9701 if (err)
9702 goto done;
9705 sync_err = sync_fileindex(fileindex, fileindex_path);
9706 if (sync_err && err == NULL)
9707 err = sync_err;
9708 done:
9709 free(fileindex_path);
9710 if (fileindex)
9711 got_fileindex_free(fileindex);
9712 unlockerr = lock_worktree(worktree, LOCK_SH);
9713 if (unlockerr && err == NULL)
9714 err = unlockerr;
9715 return err;
9718 struct report_file_info_arg {
9719 struct got_worktree *worktree;
9720 got_worktree_path_info_cb info_cb;
9721 void *info_arg;
9722 struct got_pathlist_head *paths;
9723 got_cancel_cb cancel_cb;
9724 void *cancel_arg;
9727 static const struct got_error *
9728 report_file_info(void *arg, struct got_fileindex_entry *ie)
9730 struct report_file_info_arg *a = arg;
9731 struct got_pathlist_entry *pe;
9732 struct got_object_id blob_id, staged_blob_id, commit_id;
9733 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9734 struct got_object_id *commit_idp = NULL;
9735 int stage;
9737 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9738 return got_error(GOT_ERR_CANCELLED);
9740 TAILQ_FOREACH(pe, a->paths, entry) {
9741 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9742 got_path_is_child(ie->path, pe->path, pe->path_len))
9743 break;
9745 if (pe == NULL) /* not found */
9746 return NULL;
9748 if (got_fileindex_entry_has_blob(ie))
9749 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9750 stage = got_fileindex_entry_stage_get(ie);
9751 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9752 stage == GOT_FILEIDX_STAGE_ADD) {
9753 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9754 &staged_blob_id, ie);
9757 if (got_fileindex_entry_has_commit(ie))
9758 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9760 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9761 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9764 const struct got_error *
9765 got_worktree_path_info(struct got_worktree *worktree,
9766 struct got_pathlist_head *paths,
9767 got_worktree_path_info_cb info_cb, void *info_arg,
9768 got_cancel_cb cancel_cb, void *cancel_arg)
9771 const struct got_error *err = NULL, *unlockerr;
9772 struct got_fileindex *fileindex = NULL;
9773 char *fileindex_path = NULL;
9774 struct report_file_info_arg arg;
9776 err = lock_worktree(worktree, LOCK_SH);
9777 if (err)
9778 return err;
9780 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9781 if (err)
9782 goto done;
9784 arg.worktree = worktree;
9785 arg.info_cb = info_cb;
9786 arg.info_arg = info_arg;
9787 arg.paths = paths;
9788 arg.cancel_cb = cancel_cb;
9789 arg.cancel_arg = cancel_arg;
9790 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9791 &arg);
9792 done:
9793 free(fileindex_path);
9794 if (fileindex)
9795 got_fileindex_free(fileindex);
9796 unlockerr = lock_worktree(worktree, LOCK_UN);
9797 if (unlockerr && err == NULL)
9798 err = unlockerr;
9799 return err;
9802 static const struct got_error *
9803 patch_check_path(const char *p, char **path, unsigned char *status,
9804 unsigned char *staged_status, struct got_fileindex *fileindex,
9805 struct got_worktree *worktree, struct got_repository *repo)
9807 const struct got_error *err;
9808 struct got_fileindex_entry *ie;
9809 struct stat sb;
9810 char *ondisk_path = NULL;
9812 err = got_worktree_resolve_path(path, worktree, p);
9813 if (err)
9814 return err;
9816 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9817 *path[0] ? "/" : "", *path) == -1)
9818 return got_error_from_errno("asprintf");
9820 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9821 if (ie) {
9822 *staged_status = get_staged_status(ie);
9823 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9824 repo);
9825 if (err)
9826 goto done;
9827 } else {
9828 *staged_status = GOT_STATUS_NO_CHANGE;
9829 *status = GOT_STATUS_UNVERSIONED;
9830 if (lstat(ondisk_path, &sb) == -1) {
9831 if (errno != ENOENT) {
9832 err = got_error_from_errno2("lstat",
9833 ondisk_path);
9834 goto done;
9836 *status = GOT_STATUS_NONEXISTENT;
9840 done:
9841 free(ondisk_path);
9842 return err;
9845 static const struct got_error *
9846 patch_can_rm(const char *path, unsigned char status,
9847 unsigned char staged_status)
9849 if (status == GOT_STATUS_NONEXISTENT)
9850 return got_error_set_errno(ENOENT, path);
9851 if (status != GOT_STATUS_NO_CHANGE &&
9852 status != GOT_STATUS_ADD &&
9853 status != GOT_STATUS_MODIFY &&
9854 status != GOT_STATUS_MODE_CHANGE)
9855 return got_error_path(path, GOT_ERR_FILE_STATUS);
9856 if (staged_status == GOT_STATUS_DELETE)
9857 return got_error_path(path, GOT_ERR_FILE_STATUS);
9858 return NULL;
9861 static const struct got_error *
9862 patch_can_add(const char *path, unsigned char status)
9864 if (status != GOT_STATUS_NONEXISTENT)
9865 return got_error_path(path, GOT_ERR_FILE_STATUS);
9866 return NULL;
9869 static const struct got_error *
9870 patch_can_edit(const char *path, unsigned char status,
9871 unsigned char staged_status)
9873 if (status == GOT_STATUS_NONEXISTENT)
9874 return got_error_set_errno(ENOENT, path);
9875 if (status != GOT_STATUS_NO_CHANGE &&
9876 status != GOT_STATUS_ADD &&
9877 status != GOT_STATUS_MODIFY)
9878 return got_error_path(path, GOT_ERR_FILE_STATUS);
9879 if (staged_status == GOT_STATUS_DELETE)
9880 return got_error_path(path, GOT_ERR_FILE_STATUS);
9881 return NULL;
9884 const struct got_error *
9885 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9886 char **fileindex_path, struct got_worktree *worktree)
9888 return open_fileindex(fileindex, fileindex_path, worktree);
9891 const struct got_error *
9892 got_worktree_patch_check_path(const char *old, const char *new,
9893 char **oldpath, char **newpath, struct got_worktree *worktree,
9894 struct got_repository *repo, struct got_fileindex *fileindex)
9896 const struct got_error *err = NULL;
9897 int file_renamed = 0;
9898 unsigned char status_old, staged_status_old;
9899 unsigned char status_new, staged_status_new;
9901 *oldpath = NULL;
9902 *newpath = NULL;
9904 err = patch_check_path(old != NULL ? old : new, oldpath,
9905 &status_old, &staged_status_old, fileindex, worktree, repo);
9906 if (err)
9907 goto done;
9909 err = patch_check_path(new != NULL ? new : old, newpath,
9910 &status_new, &staged_status_new, fileindex, worktree, repo);
9911 if (err)
9912 goto done;
9914 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9915 file_renamed = 1;
9917 if (old != NULL && new == NULL)
9918 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9919 else if (file_renamed) {
9920 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9921 if (err == NULL)
9922 err = patch_can_add(*newpath, status_new);
9923 } else if (old == NULL)
9924 err = patch_can_add(*newpath, status_new);
9925 else
9926 err = patch_can_edit(*newpath, status_new, staged_status_new);
9928 done:
9929 if (err) {
9930 free(*oldpath);
9931 *oldpath = NULL;
9932 free(*newpath);
9933 *newpath = NULL;
9935 return err;
9938 const struct got_error *
9939 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9940 struct got_worktree *worktree, struct got_fileindex *fileindex,
9941 got_worktree_checkout_cb progress_cb, void *progress_arg)
9943 struct schedule_addition_args saa;
9945 memset(&saa, 0, sizeof(saa));
9946 saa.worktree = worktree;
9947 saa.fileindex = fileindex;
9948 saa.progress_cb = progress_cb;
9949 saa.progress_arg = progress_arg;
9950 saa.repo = repo;
9952 return worktree_status(worktree, path, fileindex, repo,
9953 schedule_addition, &saa, NULL, NULL, 1, 0);
9956 const struct got_error *
9957 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9958 struct got_worktree *worktree, struct got_fileindex *fileindex,
9959 got_worktree_delete_cb progress_cb, void *progress_arg)
9961 const struct got_error *err;
9962 struct schedule_deletion_args sda;
9963 char *ondisk_status_path;
9965 memset(&sda, 0, sizeof(sda));
9966 sda.worktree = worktree;
9967 sda.fileindex = fileindex;
9968 sda.progress_cb = progress_cb;
9969 sda.progress_arg = progress_arg;
9970 sda.repo = repo;
9971 sda.delete_local_mods = 0;
9972 sda.keep_on_disk = 0;
9973 sda.ignore_missing_paths = 0;
9974 sda.status_codes = NULL;
9975 if (asprintf(&ondisk_status_path, "%s/%s",
9976 got_worktree_get_root_path(worktree), path) == -1)
9977 return got_error_from_errno("asprintf");
9978 sda.status_path = ondisk_status_path;
9979 sda.status_path_len = strlen(ondisk_status_path);
9981 err = worktree_status(worktree, path, fileindex, repo,
9982 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9983 free(ondisk_status_path);
9984 return err;
9987 const struct got_error *
9988 got_worktree_patch_complete(struct got_fileindex *fileindex,
9989 const char *fileindex_path)
9991 const struct got_error *err = NULL;
9993 err = sync_fileindex(fileindex, fileindex_path);
9994 got_fileindex_free(fileindex);
9996 return err;