Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t
69 apply_umask(mode_t mode)
70 {
71 mode_t um;
73 um = umask(000);
74 umask(um);
75 return mode & ~um;
76 }
78 static const struct got_error *
79 create_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 char *path;
84 if (asprintf(&path, "%s/%s", path_got, name) == -1)
85 return got_error_from_errno("asprintf");
87 err = got_path_create_file(path, content);
88 free(path);
89 return err;
90 }
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
94 {
95 const struct got_error *err = NULL;
96 FILE *tmpfile = NULL;
97 char *tmppath = NULL;
98 char *path = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno("asprintf");
102 path = NULL;
103 goto done;
106 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
107 if (err)
108 goto done;
110 if (content) {
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno2("fprintf", tmppath);
114 goto done;
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno3("rename", tmppath, path);
120 unlink(tmppath);
121 goto done;
124 done:
125 if (fclose(tmpfile) == EOF && err == NULL)
126 err = got_error_from_errno2("fclose", tmppath);
127 free(tmppath);
128 return err;
131 static const struct got_error *
132 write_head_ref(const char *path_got, struct got_reference *head_ref)
134 const struct got_error *err = NULL;
135 char *refstr = NULL;
137 if (got_ref_is_symbolic(head_ref)) {
138 refstr = got_ref_to_str(head_ref);
139 if (refstr == NULL)
140 return got_error_from_errno("got_ref_to_str");
141 } else {
142 refstr = strdup(got_ref_get_name(head_ref));
143 if (refstr == NULL)
144 return got_error_from_errno("strdup");
146 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
147 free(refstr);
148 return err;
151 const struct got_error *
152 got_worktree_init(const char *path, struct got_reference *head_ref,
153 const char *prefix, const char *meta_dir, struct got_repository *repo)
155 const struct got_error *err = NULL;
156 struct got_object_id *commit_id = NULL;
157 uuid_t uuid;
158 uint32_t uuid_status;
159 int obj_type;
160 char *path_got = NULL;
161 char *formatstr = NULL;
162 char *absprefix = NULL;
163 char *basestr = NULL;
164 char *uuidstr = NULL;
166 if (strcmp(path, got_repo_get_path(repo)) == 0) {
167 err = got_error(GOT_ERR_WORKTREE_REPO);
168 goto done;
171 err = got_ref_resolve(&commit_id, repo, head_ref);
172 if (err)
173 return err;
174 err = got_object_get_type(&obj_type, repo, commit_id);
175 if (err)
176 return err;
177 if (obj_type != GOT_OBJ_TYPE_COMMIT)
178 return got_error(GOT_ERR_OBJ_TYPE);
180 if (!got_path_is_absolute(prefix)) {
181 if (asprintf(&absprefix, "/%s", prefix) == -1)
182 return got_error_from_errno("asprintf");
185 /* Create top-level directory (may already exist). */
186 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
187 err = got_error_from_errno2("mkdir", path);
188 goto done;
191 /* Create .got/.cvg directory (may already exist). */
192 if (asprintf(&path_got, "%s/%s", path, meta_dir) == -1) {
193 err = got_error_from_errno("asprintf");
194 goto done;
196 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
197 err = got_error_from_errno2("mkdir", path_got);
198 goto done;
201 /* Create an empty lock file. */
202 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
203 if (err)
204 goto done;
206 /* Create an empty file index. */
207 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
208 if (err)
209 goto done;
211 /* Write the HEAD reference. */
212 err = write_head_ref(path_got, head_ref);
213 if (err)
214 goto done;
216 /* Record our base commit. */
217 err = got_object_id_str(&basestr, commit_id);
218 if (err)
219 goto done;
220 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
221 if (err)
222 goto done;
224 /* Store path to repository. */
225 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
226 got_repo_get_path(repo));
227 if (err)
228 goto done;
230 /* Store in-repository path prefix. */
231 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
232 absprefix ? absprefix : prefix);
233 if (err)
234 goto done;
236 /* Generate UUID. */
237 uuid_create(&uuid, &uuid_status);
238 if (uuid_status != uuid_s_ok) {
239 err = got_error_uuid(uuid_status, "uuid_create");
240 goto done;
242 uuid_to_string(&uuid, &uuidstr, &uuid_status);
243 if (uuid_status != uuid_s_ok) {
244 err = got_error_uuid(uuid_status, "uuid_to_string");
245 goto done;
247 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
248 if (err)
249 goto done;
251 /* Stamp work tree with format file. */
252 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
253 err = got_error_from_errno("asprintf");
254 goto done;
256 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
257 if (err)
258 goto done;
260 done:
261 free(commit_id);
262 free(path_got);
263 free(formatstr);
264 free(absprefix);
265 free(basestr);
266 free(uuidstr);
267 return err;
270 const struct got_error *
271 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
272 const char *path_prefix)
274 char *absprefix = NULL;
276 if (!got_path_is_absolute(path_prefix)) {
277 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
278 return got_error_from_errno("asprintf");
280 *match = (strcmp(absprefix ? absprefix : path_prefix,
281 worktree->path_prefix) == 0);
282 free(absprefix);
283 return NULL;
286 const char *
287 got_worktree_get_head_ref_name(struct got_worktree *worktree)
289 return worktree->head_ref_name;
292 const struct got_error *
293 got_worktree_set_head_ref(struct got_worktree *worktree,
294 struct got_reference *head_ref)
296 const struct got_error *err = NULL;
297 char *path_got = NULL, *head_ref_name = NULL;
299 if (asprintf(&path_got, "%s/%s", worktree->root_path,
300 worktree->meta_dir) == -1) {
301 err = got_error_from_errno("asprintf");
302 path_got = NULL;
303 goto done;
306 head_ref_name = strdup(got_ref_get_name(head_ref));
307 if (head_ref_name == NULL) {
308 err = got_error_from_errno("strdup");
309 goto done;
312 err = write_head_ref(path_got, head_ref);
313 if (err)
314 goto done;
316 free(worktree->head_ref_name);
317 worktree->head_ref_name = head_ref_name;
318 done:
319 free(path_got);
320 if (err)
321 free(head_ref_name);
322 return err;
325 struct got_object_id *
326 got_worktree_get_base_commit_id(struct got_worktree *worktree)
328 return worktree->base_commit_id;
331 const struct got_error *
332 got_worktree_set_base_commit_id(struct got_worktree *worktree,
333 struct got_repository *repo, struct got_object_id *commit_id)
335 const struct got_error *err;
336 struct got_object *obj = NULL;
337 char *id_str = NULL;
338 char *path_got = NULL;
340 if (asprintf(&path_got, "%s/%s", worktree->root_path,
341 worktree->meta_dir) == -1) {
342 err = got_error_from_errno("asprintf");
343 path_got = NULL;
344 goto done;
347 err = got_object_open(&obj, repo, commit_id);
348 if (err)
349 return err;
351 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
353 goto done;
356 /* Record our base commit. */
357 err = got_object_id_str(&id_str, commit_id);
358 if (err)
359 goto done;
360 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
361 if (err)
362 goto done;
364 free(worktree->base_commit_id);
365 worktree->base_commit_id = got_object_id_dup(commit_id);
366 if (worktree->base_commit_id == NULL) {
367 err = got_error_from_errno("got_object_id_dup");
368 goto done;
370 done:
371 if (obj)
372 got_object_close(obj);
373 free(id_str);
374 free(path_got);
375 return err;
378 const struct got_gotconfig *
379 got_worktree_get_gotconfig(struct got_worktree *worktree)
381 return worktree->gotconfig;
384 static const struct got_error *
385 lock_worktree(struct got_worktree *worktree, int operation)
387 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
388 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
389 : got_error_from_errno2("flock",
390 got_worktree_get_root_path(worktree)));
391 return NULL;
394 static const struct got_error *
395 add_dir_on_disk(struct got_worktree *worktree, const char *path)
397 const struct got_error *err = NULL;
398 char *abspath;
400 /* We only accept worktree-relative paths here. */
401 if (got_path_is_absolute(path)) {
402 return got_error_fmt(GOT_ERR_BAD_PATH,
403 "%s does not accept absolute paths: %s",
404 __func__, path);
407 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
408 return got_error_from_errno("asprintf");
410 err = got_path_mkdir(abspath);
411 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
412 struct stat sb;
413 err = NULL;
414 if (lstat(abspath, &sb) == -1) {
415 err = got_error_from_errno2("lstat", abspath);
416 } else if (!S_ISDIR(sb.st_mode)) {
417 /* TODO directory is obstructed; do something */
418 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
421 free(abspath);
422 return err;
425 static const struct got_error *
426 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
428 const struct got_error *err = NULL;
429 uint8_t fbuf1[8192];
430 uint8_t fbuf2[8192];
431 size_t flen1 = 0, flen2 = 0;
433 *same = 1;
435 for (;;) {
436 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
437 if (flen1 == 0 && ferror(f1)) {
438 err = got_error_from_errno("fread");
439 break;
441 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
442 if (flen2 == 0 && ferror(f2)) {
443 err = got_error_from_errno("fread");
444 break;
446 if (flen1 == 0) {
447 if (flen2 != 0)
448 *same = 0;
449 break;
450 } else if (flen2 == 0) {
451 if (flen1 != 0)
452 *same = 0;
453 break;
454 } else if (flen1 == flen2) {
455 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
456 *same = 0;
457 break;
459 } else {
460 *same = 0;
461 break;
465 return err;
468 static const struct got_error *
469 check_files_equal(int *same, FILE *f1, FILE *f2)
471 struct stat sb;
472 size_t size1, size2;
474 *same = 1;
476 if (fstat(fileno(f1), &sb) != 0)
477 return got_error_from_errno("fstat");
478 size1 = sb.st_size;
480 if (fstat(fileno(f2), &sb) != 0)
481 return got_error_from_errno("fstat");
482 size2 = sb.st_size;
484 if (size1 != size2) {
485 *same = 0;
486 return NULL;
489 if (fseek(f1, 0L, SEEK_SET) == -1)
490 return got_ferror(f1, GOT_ERR_IO);
491 if (fseek(f2, 0L, SEEK_SET) == -1)
492 return got_ferror(f2, GOT_ERR_IO);
494 return check_file_contents_equal(same, f1, f2);
497 static const struct got_error *
498 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
500 uint8_t fbuf[65536];
501 size_t flen;
502 ssize_t outlen;
504 *outsize = 0;
506 if (fseek(f, 0L, SEEK_SET) == -1)
507 return got_ferror(f, GOT_ERR_IO);
509 for (;;) {
510 flen = fread(fbuf, 1, sizeof(fbuf), f);
511 if (flen == 0) {
512 if (ferror(f))
513 return got_error_from_errno("fread");
514 if (feof(f))
515 break;
517 outlen = write(outfd, fbuf, flen);
518 if (outlen == -1)
519 return got_error_from_errno("write");
520 if (outlen != flen)
521 return got_error(GOT_ERR_IO);
522 *outsize += outlen;
525 return NULL;
528 static const struct got_error *
529 merge_binary_file(int *overlapcnt, int merged_fd,
530 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
531 const char *label_deriv, const char *label_orig, const char *label_deriv2,
532 const char *ondisk_path)
534 const struct got_error *err = NULL;
535 int same_content, changed_deriv, changed_deriv2;
536 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
537 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
538 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
539 char *base_path_orig = NULL, *base_path_deriv = NULL;
540 char *base_path_deriv2 = NULL;
542 *overlapcnt = 0;
544 err = check_files_equal(&same_content, f_deriv, f_deriv2);
545 if (err)
546 return err;
548 if (same_content)
549 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
551 err = check_files_equal(&same_content, f_deriv, f_orig);
552 if (err)
553 return err;
554 changed_deriv = !same_content;
555 err = check_files_equal(&same_content, f_deriv2, f_orig);
556 if (err)
557 return err;
558 changed_deriv2 = !same_content;
560 if (changed_deriv && changed_deriv2) {
561 *overlapcnt = 1;
562 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
563 err = got_error_from_errno("asprintf");
564 goto done;
566 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
567 err = got_error_from_errno("asprintf");
568 goto done;
570 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
571 err = got_error_from_errno("asprintf");
572 goto done;
574 err = got_opentemp_named_fd(&path_orig, &fd_orig,
575 base_path_orig, "");
576 if (err)
577 goto done;
578 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
579 base_path_deriv, "");
580 if (err)
581 goto done;
582 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
583 base_path_deriv2, "");
584 if (err)
585 goto done;
586 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
587 if (err)
588 goto done;
589 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
590 if (err)
591 goto done;
592 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
593 if (err)
594 goto done;
595 if (dprintf(merged_fd, "Binary files differ and cannot be "
596 "merged automatically:\n") < 0) {
597 err = got_error_from_errno("dprintf");
598 goto done;
600 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
602 label_deriv ? " " : "",
603 label_deriv ? label_deriv : "",
604 path_deriv) < 0) {
605 err = got_error_from_errno("dprintf");
606 goto done;
608 if (size_orig > 0) {
609 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
610 GOT_DIFF_CONFLICT_MARKER_ORIG,
611 label_orig ? " " : "",
612 label_orig ? label_orig : "",
613 path_orig) < 0) {
614 err = got_error_from_errno("dprintf");
615 goto done;
618 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
619 GOT_DIFF_CONFLICT_MARKER_SEP,
620 path_deriv2,
621 GOT_DIFF_CONFLICT_MARKER_END,
622 label_deriv2 ? " " : "",
623 label_deriv2 ? label_deriv2 : "") < 0) {
624 err = got_error_from_errno("dprintf");
625 goto done;
627 } else if (changed_deriv)
628 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
629 else if (changed_deriv2)
630 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
631 done:
632 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
633 err == NULL)
634 err = got_error_from_errno2("unlink", path_orig);
635 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
636 err = got_error_from_errno2("close", path_orig);
637 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
638 err = got_error_from_errno2("close", path_deriv);
639 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
640 err = got_error_from_errno2("close", path_deriv2);
641 free(path_orig);
642 free(path_deriv);
643 free(path_deriv2);
644 free(base_path_orig);
645 free(base_path_deriv);
646 free(base_path_deriv2);
647 return err;
650 /*
651 * Perform a 3-way merge where the file f_orig acts as the common
652 * ancestor, the file f_deriv acts as the first derived version,
653 * and the file f_deriv2 acts as the second derived version.
654 * The merge result will be written to a new file at ondisk_path; any
655 * existing file at this path will be replaced.
656 */
657 static const struct got_error *
658 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
659 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
660 const char *path, uint16_t st_mode,
661 const char *label_orig, const char *label_deriv, const char *label_deriv2,
662 enum got_diff_algorithm diff_algo, struct got_repository *repo,
663 got_worktree_checkout_cb progress_cb, void *progress_arg)
665 const struct got_error *err = NULL;
666 int merged_fd = -1;
667 FILE *f_merged = NULL;
668 char *merged_path = NULL, *base_path = NULL;
669 int overlapcnt = 0;
670 char *parent = NULL;
672 *local_changes_subsumed = 0;
674 err = got_path_dirname(&parent, ondisk_path);
675 if (err)
676 return err;
678 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
679 err = got_error_from_errno("asprintf");
680 goto done;
683 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
684 if (err)
685 goto done;
687 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
688 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
689 if (err) {
690 if (err->code != GOT_ERR_FILE_BINARY)
691 goto done;
692 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
693 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
694 ondisk_path);
695 if (err)
696 goto done;
699 err = (*progress_cb)(progress_arg,
700 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
701 if (err)
702 goto done;
704 if (fsync(merged_fd) != 0) {
705 err = got_error_from_errno("fsync");
706 goto done;
709 f_merged = fdopen(merged_fd, "r");
710 if (f_merged == NULL) {
711 err = got_error_from_errno("fdopen");
712 goto done;
714 merged_fd = -1;
716 /* Check if a clean merge has subsumed all local changes. */
717 if (overlapcnt == 0) {
718 err = check_files_equal(local_changes_subsumed, f_deriv,
719 f_merged);
720 if (err)
721 goto done;
724 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
725 err = got_error_from_errno2("fchmod", merged_path);
726 goto done;
729 if (rename(merged_path, ondisk_path) != 0) {
730 err = got_error_from_errno3("rename", merged_path,
731 ondisk_path);
732 goto done;
734 done:
735 if (err) {
736 if (merged_path)
737 unlink(merged_path);
739 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
740 err = got_error_from_errno("close");
741 if (f_merged && fclose(f_merged) == EOF && err == NULL)
742 err = got_error_from_errno("fclose");
743 free(merged_path);
744 free(base_path);
745 free(parent);
746 return err;
749 static const struct got_error *
750 update_symlink(const char *ondisk_path, const char *target_path,
751 size_t target_len)
753 /* This is not atomic but matches what 'ln -sf' does. */
754 if (unlink(ondisk_path) == -1)
755 return got_error_from_errno2("unlink", ondisk_path);
756 if (symlink(target_path, ondisk_path) == -1)
757 return got_error_from_errno3("symlink", target_path,
758 ondisk_path);
759 return NULL;
762 /*
763 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
764 * in the work tree with a file that contains conflict markers and the
765 * conflicting target paths of the original version, a "derived version"
766 * of a symlink from an incoming change, and a local version of the symlink.
768 * The original versions's target path can be NULL if it is not available,
769 * such as if both derived versions added a new symlink at the same path.
771 * The incoming derived symlink target is NULL in case the incoming change
772 * has deleted this symlink.
773 */
774 static const struct got_error *
775 install_symlink_conflict(const char *deriv_target,
776 struct got_object_id *deriv_base_commit_id, const char *orig_target,
777 const char *label_orig, const char *local_target, const char *ondisk_path)
779 const struct got_error *err;
780 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
781 FILE *f = NULL;
783 err = got_object_id_str(&id_str, deriv_base_commit_id);
784 if (err)
785 return got_error_from_errno("asprintf");
787 if (asprintf(&label_deriv, "%s: commit %s",
788 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
793 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
794 if (err)
795 goto done;
797 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
798 err = got_error_from_errno2("fchmod", path);
799 goto done;
802 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
803 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
804 deriv_target ? deriv_target : "(symlink was deleted)",
805 orig_target ? label_orig : "",
806 orig_target ? "\n" : "",
807 orig_target ? orig_target : "",
808 orig_target ? "\n" : "",
809 GOT_DIFF_CONFLICT_MARKER_SEP,
810 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
811 err = got_error_from_errno2("fprintf", path);
812 goto done;
815 if (unlink(ondisk_path) == -1) {
816 err = got_error_from_errno2("unlink", ondisk_path);
817 goto done;
819 if (rename(path, ondisk_path) == -1) {
820 err = got_error_from_errno3("rename", path, ondisk_path);
821 goto done;
823 done:
824 if (f != NULL && fclose(f) == EOF && err == NULL)
825 err = got_error_from_errno2("fclose", path);
826 free(path);
827 free(id_str);
828 free(label_deriv);
829 return err;
832 /* forward declaration */
833 static const struct got_error *
834 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
835 const char *, const char *, uint16_t, const char *,
836 struct got_blob_object *, struct got_object_id *,
837 struct got_repository *, got_worktree_checkout_cb, void *);
839 /*
840 * Merge a symlink into the work tree, where blob_orig acts as the common
841 * ancestor, deriv_target is the link target of the first derived version,
842 * and the symlink on disk acts as the second derived version.
843 * Assume that contents of both blobs represent symlinks.
844 */
845 static const struct got_error *
846 merge_symlink(struct got_worktree *worktree,
847 struct got_blob_object *blob_orig, const char *ondisk_path,
848 const char *path, const char *label_orig, const char *deriv_target,
849 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
850 got_worktree_checkout_cb progress_cb, void *progress_arg)
852 const struct got_error *err = NULL;
853 char *ancestor_target = NULL;
854 struct stat sb;
855 ssize_t ondisk_len, deriv_len;
856 char ondisk_target[PATH_MAX];
857 int have_local_change = 0;
858 int have_incoming_change = 0;
860 if (lstat(ondisk_path, &sb) == -1)
861 return got_error_from_errno2("lstat", ondisk_path);
863 ondisk_len = readlink(ondisk_path, ondisk_target,
864 sizeof(ondisk_target));
865 if (ondisk_len == -1) {
866 err = got_error_from_errno2("readlink",
867 ondisk_path);
868 goto done;
870 ondisk_target[ondisk_len] = '\0';
872 if (blob_orig) {
873 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
874 if (err)
875 goto done;
878 if (ancestor_target == NULL ||
879 (ondisk_len != strlen(ancestor_target) ||
880 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
881 have_local_change = 1;
883 deriv_len = strlen(deriv_target);
884 if (ancestor_target == NULL ||
885 (deriv_len != strlen(ancestor_target) ||
886 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
887 have_incoming_change = 1;
889 if (!have_local_change && !have_incoming_change) {
890 if (ancestor_target) {
891 /* Both sides made the same change. */
892 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
893 path);
894 } else if (deriv_len == ondisk_len &&
895 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
896 /* Both sides added the same symlink. */
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
898 path);
899 } else {
900 /* Both sides added symlinks which don't match. */
901 err = install_symlink_conflict(deriv_target,
902 deriv_base_commit_id, ancestor_target,
903 label_orig, ondisk_target, ondisk_path);
904 if (err)
905 goto done;
906 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
907 path);
909 } else if (!have_local_change && have_incoming_change) {
910 /* Apply the incoming change. */
911 err = update_symlink(ondisk_path, deriv_target,
912 strlen(deriv_target));
913 if (err)
914 goto done;
915 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
916 } else if (have_local_change && have_incoming_change) {
917 if (deriv_len == ondisk_len &&
918 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
919 /* Both sides made the same change. */
920 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
921 path);
922 } else {
923 err = install_symlink_conflict(deriv_target,
924 deriv_base_commit_id, ancestor_target, label_orig,
925 ondisk_target, ondisk_path);
926 if (err)
927 goto done;
928 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
929 path);
933 done:
934 free(ancestor_target);
935 return err;
938 static const struct got_error *
939 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
941 const struct got_error *err = NULL;
942 char target_path[PATH_MAX];
943 ssize_t target_len;
944 size_t n;
945 FILE *f;
947 *outfile = NULL;
949 f = got_opentemp();
950 if (f == NULL)
951 return got_error_from_errno("got_opentemp");
952 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
953 if (target_len == -1) {
954 err = got_error_from_errno2("readlink", ondisk_path);
955 goto done;
957 n = fwrite(target_path, 1, target_len, f);
958 if (n != target_len) {
959 err = got_ferror(f, GOT_ERR_IO);
960 goto done;
962 if (fflush(f) == EOF) {
963 err = got_error_from_errno("fflush");
964 goto done;
966 if (fseek(f, 0L, SEEK_SET) == -1) {
967 err = got_ferror(f, GOT_ERR_IO);
968 goto done;
970 done:
971 if (err)
972 fclose(f);
973 else
974 *outfile = f;
975 return err;
978 /*
979 * Perform a 3-way merge where blob_orig acts as the common ancestor,
980 * blob_deriv acts as the first derived version, and the file on disk
981 * acts as the second derived version.
982 */
983 static const struct got_error *
984 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
985 struct got_blob_object *blob_orig, const char *ondisk_path,
986 const char *path, uint16_t st_mode, const char *label_orig,
987 struct got_blob_object *blob_deriv,
988 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
989 got_worktree_checkout_cb progress_cb, void *progress_arg)
991 const struct got_error *err = NULL;
992 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
993 char *blob_orig_path = NULL;
994 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
995 char *label_deriv = NULL, *parent = NULL;
997 *local_changes_subsumed = 0;
999 err = got_path_dirname(&parent, ondisk_path);
1000 if (err)
1001 return err;
1003 if (blob_orig) {
1004 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1005 parent) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 base_path = NULL;
1008 goto done;
1011 err = got_opentemp_named(&blob_orig_path, &f_orig,
1012 base_path, "");
1013 if (err)
1014 goto done;
1015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1016 blob_orig);
1017 if (err)
1018 goto done;
1019 free(base_path);
1020 } else {
1022 * No common ancestor exists. This is an "add vs add" conflict
1023 * and we simply use an empty ancestor file to make both files
1024 * appear in the merged result in their entirety.
1026 f_orig = got_opentemp();
1027 if (f_orig == NULL) {
1028 err = got_error_from_errno("got_opentemp");
1029 goto done;
1033 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 base_path = NULL;
1036 goto done;
1039 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1040 if (err)
1041 goto done;
1042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1043 blob_deriv);
1044 if (err)
1045 goto done;
1047 err = got_object_id_str(&id_str, deriv_base_commit_id);
1048 if (err)
1049 goto done;
1050 if (asprintf(&label_deriv, "%s: commit %s",
1051 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1052 err = got_error_from_errno("asprintf");
1053 goto done;
1057 * In order the run a 3-way merge with a symlink we copy the symlink's
1058 * target path into a temporary file and use that file with diff3.
1060 if (S_ISLNK(st_mode)) {
1061 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1062 if (err)
1063 goto done;
1064 } else {
1065 int fd;
1066 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1067 if (fd == -1) {
1068 err = got_error_from_errno2("open", ondisk_path);
1069 goto done;
1071 f_deriv2 = fdopen(fd, "r");
1072 if (f_deriv2 == NULL) {
1073 err = got_error_from_errno2("fdopen", ondisk_path);
1074 close(fd);
1075 goto done;
1079 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1080 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1081 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1082 done:
1083 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1084 err = got_error_from_errno("fclose");
1085 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1086 err = got_error_from_errno("fclose");
1087 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1089 free(base_path);
1090 if (blob_orig_path) {
1091 unlink(blob_orig_path);
1092 free(blob_orig_path);
1094 if (blob_deriv_path) {
1095 unlink(blob_deriv_path);
1096 free(blob_deriv_path);
1098 free(id_str);
1099 free(label_deriv);
1100 free(parent);
1101 return err;
1104 static const struct got_error *
1105 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1106 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1107 int wt_fd, const char *path, struct got_object_id *blob_id)
1109 const struct got_error *err = NULL;
1110 struct got_fileindex_entry *new_ie;
1112 *new_iep = NULL;
1114 err = got_fileindex_entry_alloc(&new_ie, path);
1115 if (err)
1116 return err;
1118 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1119 blob_id->sha1, base_commit_id->sha1, 1);
1120 if (err)
1121 goto done;
1123 err = got_fileindex_entry_add(fileindex, new_ie);
1124 done:
1125 if (err)
1126 got_fileindex_entry_free(new_ie);
1127 else
1128 *new_iep = new_ie;
1129 return err;
1132 static mode_t
1133 get_ondisk_perms(int executable, mode_t st_mode)
1135 mode_t xbits = S_IXUSR;
1137 if (executable) {
1138 /* Map read bits to execute bits. */
1139 if (st_mode & S_IRGRP)
1140 xbits |= S_IXGRP;
1141 if (st_mode & S_IROTH)
1142 xbits |= S_IXOTH;
1143 return st_mode | xbits;
1146 return st_mode;
1149 /* forward declaration */
1150 static const struct got_error *
1151 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1152 const char *path, mode_t te_mode, mode_t st_mode,
1153 struct got_blob_object *blob, int restoring_missing_file,
1154 int reverting_versioned_file, int installing_bad_symlink,
1155 int path_is_unversioned, struct got_repository *repo,
1156 got_worktree_checkout_cb progress_cb, void *progress_arg);
1159 * This function assumes that the provided symlink target points at a
1160 * safe location in the work tree!
1162 static const struct got_error *
1163 replace_existing_symlink(int *did_something, const char *ondisk_path,
1164 const char *target_path, size_t target_len)
1166 const struct got_error *err = NULL;
1167 ssize_t elen;
1168 char etarget[PATH_MAX];
1169 int fd;
1171 *did_something = 0;
1174 * "Bad" symlinks (those pointing outside the work tree or into the
1175 * .got directory) are installed in the work tree as a regular file
1176 * which contains the bad symlink target path.
1177 * The new symlink target has already been checked for safety by our
1178 * caller. If we can successfully open a regular file then we simply
1179 * replace this file with a symlink below.
1181 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1182 if (fd == -1) {
1183 if (!got_err_open_nofollow_on_symlink())
1184 return got_error_from_errno2("open", ondisk_path);
1186 /* We are updating an existing on-disk symlink. */
1187 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1188 if (elen == -1)
1189 return got_error_from_errno2("readlink", ondisk_path);
1191 if (elen == target_len &&
1192 memcmp(etarget, target_path, target_len) == 0)
1193 return NULL; /* nothing to do */
1196 *did_something = 1;
1197 err = update_symlink(ondisk_path, target_path, target_len);
1198 if (fd != -1 && close(fd) == -1 && err == NULL)
1199 err = got_error_from_errno2("close", ondisk_path);
1200 return err;
1203 static const struct got_error *
1204 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1205 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1206 const char *meta_dir)
1208 const struct got_error *err = NULL;
1209 char canonpath[PATH_MAX];
1210 char *path_got = NULL;
1212 *is_bad_symlink = 0;
1214 if (target_len >= sizeof(canonpath)) {
1215 *is_bad_symlink = 1;
1216 return NULL;
1220 * We do not use realpath(3) to resolve the symlink's target
1221 * path because we don't want to resolve symlinks recursively.
1222 * Instead we make the path absolute and then canonicalize it.
1223 * Relative symlink target lookup should begin at the directory
1224 * in which the blob object is being installed.
1226 if (!got_path_is_absolute(target_path)) {
1227 char *abspath, *parent;
1228 err = got_path_dirname(&parent, ondisk_path);
1229 if (err)
1230 return err;
1231 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1232 free(parent);
1233 return got_error_from_errno("asprintf");
1235 free(parent);
1236 if (strlen(abspath) >= sizeof(canonpath)) {
1237 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1238 free(abspath);
1239 return err;
1241 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1242 free(abspath);
1243 if (err)
1244 return err;
1245 } else {
1246 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1247 if (err)
1248 return err;
1251 /* Only allow symlinks pointing at paths within the work tree. */
1252 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1253 *is_bad_symlink = 1;
1254 return NULL;
1257 /* Do not allow symlinks pointing into the meta directory. */
1258 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1259 return got_error_from_errno("asprintf");
1260 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1261 *is_bad_symlink = 1;
1263 free(path_got);
1264 return NULL;
1267 static const struct got_error *
1268 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1269 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1270 int restoring_missing_file, int reverting_versioned_file,
1271 int path_is_unversioned, int allow_bad_symlinks,
1272 struct got_repository *repo,
1273 got_worktree_checkout_cb progress_cb, void *progress_arg)
1275 const struct got_error *err = NULL;
1276 char target_path[PATH_MAX];
1277 size_t len, target_len = 0;
1278 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1279 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1281 *is_bad_symlink = 0;
1284 * Blob object content specifies the target path of the link.
1285 * If a symbolic link cannot be installed we instead create
1286 * a regular file which contains the link target path stored
1287 * in the blob object.
1289 do {
1290 err = got_object_blob_read_block(&len, blob);
1291 if (err)
1292 return err;
1294 if (len + target_len >= sizeof(target_path)) {
1295 /* Path too long; install as a regular file. */
1296 *is_bad_symlink = 1;
1297 got_object_blob_rewind(blob);
1298 return install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file,
1301 1, path_is_unversioned, repo, progress_cb,
1302 progress_arg);
1304 if (len > 0) {
1305 /* Skip blob object header first time around. */
1306 memcpy(target_path + target_len, buf + hdrlen,
1307 len - hdrlen);
1308 target_len += len - hdrlen;
1309 hdrlen = 0;
1311 } while (len != 0);
1312 target_path[target_len] = '\0';
1314 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1315 ondisk_path, worktree->root_path, worktree->meta_dir);
1316 if (err)
1317 return err;
1319 if (*is_bad_symlink && !allow_bad_symlinks) {
1320 /* install as a regular file */
1321 got_object_blob_rewind(blob);
1322 err = install_blob(worktree, ondisk_path, path,
1323 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1324 restoring_missing_file, reverting_versioned_file, 1,
1325 path_is_unversioned, repo, progress_cb, progress_arg);
1326 return err;
1329 if (symlink(target_path, ondisk_path) == -1) {
1330 if (errno == EEXIST) {
1331 int symlink_replaced;
1332 if (path_is_unversioned) {
1333 err = (*progress_cb)(progress_arg,
1334 GOT_STATUS_UNVERSIONED, path);
1335 return err;
1337 err = replace_existing_symlink(&symlink_replaced,
1338 ondisk_path, target_path, target_len);
1339 if (err)
1340 return err;
1341 if (progress_cb) {
1342 if (symlink_replaced) {
1343 err = (*progress_cb)(progress_arg,
1344 reverting_versioned_file ?
1345 GOT_STATUS_REVERT :
1346 GOT_STATUS_UPDATE, path);
1347 } else {
1348 err = (*progress_cb)(progress_arg,
1349 GOT_STATUS_EXISTS, path);
1352 return err; /* Nothing else to do. */
1355 if (errno == ENOENT) {
1356 char *parent;
1357 err = got_path_dirname(&parent, ondisk_path);
1358 if (err)
1359 return err;
1360 err = got_path_mkdir(parent);
1361 free(parent);
1362 if (err)
1363 return err;
1365 * Retry, and fall through to error handling
1366 * below if this second attempt fails.
1368 if (symlink(target_path, ondisk_path) != -1) {
1369 err = NULL; /* success */
1370 if (progress_cb) {
1371 err = (*progress_cb)(progress_arg,
1372 reverting_versioned_file ?
1373 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1374 path);
1376 return err;
1380 /* Handle errors from first or second creation attempt. */
1381 if (errno == ENAMETOOLONG) {
1382 /* bad target path; install as a regular file */
1383 *is_bad_symlink = 1;
1384 got_object_blob_rewind(blob);
1385 err = install_blob(worktree, ondisk_path, path,
1386 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1387 restoring_missing_file, reverting_versioned_file, 1,
1388 path_is_unversioned, repo,
1389 progress_cb, progress_arg);
1390 } else if (errno == ENOTDIR) {
1391 err = got_error_path(ondisk_path,
1392 GOT_ERR_FILE_OBSTRUCTED);
1393 } else {
1394 err = got_error_from_errno3("symlink",
1395 target_path, ondisk_path);
1397 } else if (progress_cb)
1398 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1399 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1400 return err;
1403 static const struct got_error *
1404 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1405 const char *path, mode_t te_mode, mode_t st_mode,
1406 struct got_blob_object *blob, int restoring_missing_file,
1407 int reverting_versioned_file, int installing_bad_symlink,
1408 int path_is_unversioned, struct got_repository *repo,
1409 got_worktree_checkout_cb progress_cb, void *progress_arg)
1411 const struct got_error *err = NULL;
1412 int fd = -1;
1413 size_t len, hdrlen;
1414 int update = 0;
1415 char *tmppath = NULL;
1416 mode_t mode;
1418 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1419 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1420 O_CLOEXEC, mode);
1421 if (fd == -1) {
1422 if (errno == ENOENT || errno == ENOTDIR) {
1423 char *parent;
1424 err = got_path_dirname(&parent, path);
1425 if (err)
1426 return err;
1427 err = add_dir_on_disk(worktree, parent);
1428 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1429 err = got_error_path(path, err->code);
1430 free(parent);
1431 if (err)
1432 return err;
1433 fd = open(ondisk_path,
1434 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1435 mode);
1436 if (fd == -1)
1437 return got_error_from_errno2("open",
1438 ondisk_path);
1439 } else if (errno == EEXIST) {
1440 if (path_is_unversioned) {
1441 err = (*progress_cb)(progress_arg,
1442 GOT_STATUS_UNVERSIONED, path);
1443 goto done;
1445 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1446 !S_ISREG(st_mode) && !installing_bad_symlink) {
1447 /* TODO file is obstructed; do something */
1448 err = got_error_path(ondisk_path,
1449 GOT_ERR_FILE_OBSTRUCTED);
1450 goto done;
1451 } else {
1452 err = got_opentemp_named_fd(&tmppath, &fd,
1453 ondisk_path, "");
1454 if (err)
1455 goto done;
1456 update = 1;
1458 if (fchmod(fd, apply_umask(mode)) == -1) {
1459 err = got_error_from_errno2("fchmod",
1460 tmppath);
1461 goto done;
1464 } else
1465 return got_error_from_errno2("open", ondisk_path);
1468 if (progress_cb) {
1469 if (restoring_missing_file)
1470 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1471 path);
1472 else if (reverting_versioned_file)
1473 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1474 path);
1475 else
1476 err = (*progress_cb)(progress_arg,
1477 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1478 if (err)
1479 goto done;
1482 hdrlen = got_object_blob_get_hdrlen(blob);
1483 do {
1484 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1485 err = got_object_blob_read_block(&len, blob);
1486 if (err)
1487 break;
1488 if (len > 0) {
1489 /* Skip blob object header first time around. */
1490 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1491 if (outlen == -1) {
1492 err = got_error_from_errno("write");
1493 goto done;
1494 } else if (outlen != len - hdrlen) {
1495 err = got_error(GOT_ERR_IO);
1496 goto done;
1498 hdrlen = 0;
1500 } while (len != 0);
1502 if (fsync(fd) != 0) {
1503 err = got_error_from_errno("fsync");
1504 goto done;
1507 if (update) {
1508 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1509 err = got_error_from_errno2("unlink", ondisk_path);
1510 goto done;
1512 if (rename(tmppath, ondisk_path) != 0) {
1513 err = got_error_from_errno3("rename", tmppath,
1514 ondisk_path);
1515 goto done;
1517 free(tmppath);
1518 tmppath = NULL;
1521 done:
1522 if (fd != -1 && close(fd) == -1 && err == NULL)
1523 err = got_error_from_errno("close");
1524 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1525 err = got_error_from_errno2("unlink", tmppath);
1526 free(tmppath);
1527 return err;
1531 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1532 * conflict marker is found in newly added lines only.
1534 static const struct got_error *
1535 get_modified_file_content_status(unsigned char *status,
1536 struct got_blob_object *blob, const char *path, struct stat *sb,
1537 FILE *ondisk_file)
1539 const struct got_error *err, *free_err;
1540 const char *markers[3] = {
1541 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 GOT_DIFF_CONFLICT_MARKER_SEP,
1543 GOT_DIFF_CONFLICT_MARKER_END
1545 FILE *f1 = NULL;
1546 struct got_diffreg_result *diffreg_result = NULL;
1547 struct diff_result *r;
1548 int nchunks_parsed, n, i = 0, ln = 0;
1549 char *line = NULL;
1550 size_t linesize = 0;
1551 ssize_t linelen;
1553 if (*status != GOT_STATUS_MODIFY)
1554 return NULL;
1556 f1 = got_opentemp();
1557 if (f1 == NULL)
1558 return got_error_from_errno("got_opentemp");
1560 if (blob) {
1561 got_object_blob_rewind(blob);
1562 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1563 if (err)
1564 goto done;
1567 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1568 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1569 if (err)
1570 goto done;
1572 r = diffreg_result->result;
1574 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1575 struct diff_chunk *c;
1576 struct diff_chunk_context cc = {};
1577 off_t pos;
1580 * We can optimise a little by advancing straight
1581 * to the next chunk if this one has no added lines.
1583 c = diff_chunk_get(r, n);
1585 if (diff_chunk_type(c) != CHUNK_PLUS) {
1586 nchunks_parsed = 1;
1587 continue; /* removed or unchanged lines */
1590 pos = diff_chunk_get_right_start_pos(c);
1591 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1592 err = got_ferror(ondisk_file, GOT_ERR_IO);
1593 goto done;
1596 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1597 ln = cc.right.start;
1599 while (ln < cc.right.end) {
1600 linelen = getline(&line, &linesize, ondisk_file);
1601 if (linelen == -1) {
1602 if (feof(ondisk_file))
1603 break;
1604 err = got_ferror(ondisk_file, GOT_ERR_IO);
1605 break;
1608 if (line && strncmp(line, markers[i],
1609 strlen(markers[i])) == 0) {
1610 if (strcmp(markers[i],
1611 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1612 *status = GOT_STATUS_CONFLICT;
1613 goto done;
1614 } else
1615 i++;
1617 ++ln;
1621 done:
1622 free(line);
1623 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1624 err = got_error_from_errno("fclose");
1625 free_err = got_diffreg_result_free(diffreg_result);
1626 if (err == NULL)
1627 err = free_err;
1629 return err;
1632 static int
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1639 static int
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1660 default:
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1673 ssize_t elen;
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1681 do {
1682 err = got_object_blob_read_block(&len, blob);
1683 if (err)
1684 return err;
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1692 if (len > 0) {
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1695 len - hdrlen);
1696 target_len += len - hdrlen;
1697 hdrlen = 0;
1699 } while (len != 0);
1700 target_path[target_len] = '\0';
1702 if (dirfd != -1) {
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 if (elen == -1)
1705 return got_error_from_errno2("readlinkat", abspath);
1706 } else {
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1708 if (elen == -1)
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1715 return NULL;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1725 size_t hdrlen;
1726 int fd = -1, fd1 = -1;
1727 FILE *f = NULL;
1728 uint8_t fbuf[8192];
1729 struct got_blob_object *blob = NULL;
1730 size_t flen, blen;
1731 unsigned char staged_status;
1733 staged_status = get_staged_status(ie);
1734 *status = GOT_STATUS_NO_CHANGE;
1735 memset(sb, 0, sizeof(*sb));
1738 * Whenever the caller provides a directory descriptor and a
1739 * directory entry name for the file, use them! This prevents
1740 * race conditions if filesystem paths change beneath our feet.
1742 if (dirfd != -1) {
1743 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1744 if (errno == ENOENT) {
1745 if (got_fileindex_entry_has_file_on_disk(ie))
1746 *status = GOT_STATUS_MISSING;
1747 else
1748 *status = GOT_STATUS_DELETE;
1749 goto done;
1751 err = got_error_from_errno2("fstatat", abspath);
1752 goto done;
1754 } else {
1755 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1756 if (fd == -1 && errno != ENOENT &&
1757 !got_err_open_nofollow_on_symlink())
1758 return got_error_from_errno2("open", abspath);
1759 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1760 if (lstat(abspath, sb) == -1)
1761 return got_error_from_errno2("lstat", abspath);
1762 } else if (fd == -1 || fstat(fd, sb) == -1) {
1763 if (errno == ENOENT) {
1764 if (got_fileindex_entry_has_file_on_disk(ie))
1765 *status = GOT_STATUS_MISSING;
1766 else
1767 *status = GOT_STATUS_DELETE;
1768 goto done;
1770 err = got_error_from_errno2("fstat", abspath);
1771 goto done;
1775 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1776 *status = GOT_STATUS_OBSTRUCTED;
1777 goto done;
1780 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1781 *status = GOT_STATUS_DELETE;
1782 goto done;
1783 } else if (!got_fileindex_entry_has_blob(ie) &&
1784 staged_status != GOT_STATUS_ADD) {
1785 *status = GOT_STATUS_ADD;
1786 goto done;
1789 if (!stat_info_differs(ie, sb))
1790 goto done;
1792 if (S_ISLNK(sb->st_mode) &&
1793 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1794 *status = GOT_STATUS_MODIFY;
1795 goto done;
1798 if (staged_status == GOT_STATUS_MODIFY ||
1799 staged_status == GOT_STATUS_ADD)
1800 got_fileindex_entry_get_staged_blob_id(&id, ie);
1801 else
1802 got_fileindex_entry_get_blob_id(&id, ie);
1804 fd1 = got_opentempfd();
1805 if (fd1 == -1) {
1806 err = got_error_from_errno("got_opentempfd");
1807 goto done;
1809 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1810 if (err)
1811 goto done;
1813 if (S_ISLNK(sb->st_mode)) {
1814 err = get_symlink_modification_status(status, ie,
1815 abspath, dirfd, de_name, blob);
1816 goto done;
1819 if (dirfd != -1) {
1820 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1821 if (fd == -1) {
1822 err = got_error_from_errno2("openat", abspath);
1823 goto done;
1827 f = fdopen(fd, "r");
1828 if (f == NULL) {
1829 err = got_error_from_errno2("fdopen", abspath);
1830 goto done;
1832 fd = -1;
1833 hdrlen = got_object_blob_get_hdrlen(blob);
1834 for (;;) {
1835 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1836 err = got_object_blob_read_block(&blen, blob);
1837 if (err)
1838 goto done;
1839 /* Skip length of blob object header first time around. */
1840 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1841 if (flen == 0 && ferror(f)) {
1842 err = got_error_from_errno("fread");
1843 goto done;
1845 if (blen - hdrlen == 0) {
1846 if (flen != 0)
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1849 } else if (flen == 0) {
1850 if (blen - hdrlen != 0)
1851 *status = GOT_STATUS_MODIFY;
1852 break;
1853 } else if (blen - hdrlen == flen) {
1854 /* Skip blob object header first time around. */
1855 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1856 *status = GOT_STATUS_MODIFY;
1857 break;
1859 } else {
1860 *status = GOT_STATUS_MODIFY;
1861 break;
1863 hdrlen = 0;
1866 if (*status == GOT_STATUS_MODIFY) {
1867 rewind(f);
1868 err = get_modified_file_content_status(status, blob, ie->path,
1869 sb, f);
1870 } else if (xbit_differs(ie, sb->st_mode))
1871 *status = GOT_STATUS_MODE_CHANGE;
1872 done:
1873 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1874 err = got_error_from_errno("close");
1875 if (blob)
1876 got_object_blob_close(blob);
1877 if (f != NULL && fclose(f) == EOF && err == NULL)
1878 err = got_error_from_errno2("fclose", abspath);
1879 if (fd != -1 && close(fd) == -1 && err == NULL)
1880 err = got_error_from_errno2("close", abspath);
1881 return err;
1885 * Update timestamps in the file index if a file is unmodified and
1886 * we had to run a full content comparison to find out.
1888 static const struct got_error *
1889 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1890 struct got_fileindex_entry *ie, struct stat *sb)
1892 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1893 return got_fileindex_entry_update(ie, wt_fd, path,
1894 ie->blob_sha1, ie->commit_sha1, 1);
1896 return NULL;
1899 static const struct got_error *remove_ondisk_file(const char *, const char *);
1901 static const struct got_error *
1902 update_blob(struct got_worktree *worktree,
1903 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1904 struct got_tree_entry *te, const char *path,
1905 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1906 void *progress_arg)
1908 const struct got_error *err = NULL;
1909 struct got_blob_object *blob = NULL;
1910 char *ondisk_path = NULL;
1911 unsigned char status = GOT_STATUS_NO_CHANGE;
1912 struct stat sb;
1913 int fd1 = -1, fd2 = -1;
1915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1916 return got_error_from_errno("asprintf");
1918 if (ie) {
1919 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1920 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1921 goto done;
1923 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1924 repo);
1925 if (err)
1926 goto done;
1927 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1928 sb.st_mode = got_fileindex_perms_to_st(ie);
1929 } else {
1930 if (stat(ondisk_path, &sb) == -1) {
1931 if (errno != ENOENT && errno != ENOTDIR) {
1932 err = got_error_from_errno2("stat",
1933 ondisk_path);
1934 goto done;
1936 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1937 status = GOT_STATUS_UNVERSIONED;
1938 } else {
1939 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1940 status = GOT_STATUS_UNVERSIONED;
1941 else
1942 status = GOT_STATUS_OBSTRUCTED;
1946 if (status == GOT_STATUS_OBSTRUCTED) {
1947 if (ie)
1948 got_fileindex_entry_mark_skipped(ie);
1949 err = (*progress_cb)(progress_arg, status, path);
1950 goto done;
1952 if (status == GOT_STATUS_CONFLICT) {
1953 if (ie)
1954 got_fileindex_entry_mark_skipped(ie);
1955 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1956 path);
1957 goto done;
1960 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1961 if (status == GOT_STATUS_UNVERSIONED) {
1962 err = (*progress_cb)(progress_arg, status, path);
1963 } else if (status != GOT_STATUS_NO_CHANGE &&
1964 status != GOT_STATUS_DELETE &&
1965 status != GOT_STATUS_NONEXISTENT &&
1966 status != GOT_STATUS_MISSING) {
1967 err = (*progress_cb)(progress_arg,
1968 GOT_STATUS_CANNOT_DELETE, path);
1969 } else if (ie) {
1970 if (status != GOT_STATUS_DELETE &&
1971 status != GOT_STATUS_NONEXISTENT &&
1972 status != GOT_STATUS_MISSING) {
1973 err = remove_ondisk_file(worktree->root_path,
1974 ie->path);
1975 if (err && !(err->code == GOT_ERR_ERRNO &&
1976 errno == ENOENT))
1977 goto done;
1979 got_fileindex_entry_remove(fileindex, ie);
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1981 ie->path);
1983 goto done; /* nothing else to do */
1986 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1987 (S_ISLNK(te->mode) ||
1988 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1990 * This is a regular file or an installed bad symlink.
1991 * If the file index indicates that this file is already
1992 * up-to-date with respect to the repository we can skip
1993 * updating contents of this file.
1995 if (got_fileindex_entry_has_commit(ie) &&
1996 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1997 SHA1_DIGEST_LENGTH) == 0) {
1998 /* Same commit. */
1999 err = sync_timestamps(worktree->root_fd,
2000 path, status, ie, &sb);
2001 if (err)
2002 goto done;
2003 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2004 path);
2005 goto done;
2007 if (got_fileindex_entry_has_blob(ie) &&
2008 memcmp(ie->blob_sha1, te->id.sha1,
2009 SHA1_DIGEST_LENGTH) == 0) {
2010 /* Different commit but the same blob. */
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 /* Update the base commit ID of this file. */
2013 memcpy(ie->commit_sha1,
2014 worktree->base_commit_id->sha1,
2015 sizeof(ie->commit_sha1));
2017 err = sync_timestamps(worktree->root_fd,
2018 path, status, ie, &sb);
2019 if (err)
2020 goto done;
2021 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2022 path);
2023 goto done;
2027 fd1 = got_opentempfd();
2028 if (fd1 == -1) {
2029 err = got_error_from_errno("got_opentempfd");
2030 goto done;
2032 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2033 if (err)
2034 goto done;
2036 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2037 int update_timestamps;
2038 struct got_blob_object *blob2 = NULL;
2039 char *label_orig = NULL;
2040 if (got_fileindex_entry_has_blob(ie)) {
2041 fd2 = got_opentempfd();
2042 if (fd2 == -1) {
2043 err = got_error_from_errno("got_opentempfd");
2044 goto done;
2046 struct got_object_id id2;
2047 got_fileindex_entry_get_blob_id(&id2, ie);
2048 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2049 fd2);
2050 if (err)
2051 goto done;
2053 if (got_fileindex_entry_has_commit(ie)) {
2054 char id_str[SHA1_DIGEST_STRING_LENGTH];
2055 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2056 sizeof(id_str)) == NULL) {
2057 err = got_error_path(id_str,
2058 GOT_ERR_BAD_OBJ_ID_STR);
2059 goto done;
2061 if (asprintf(&label_orig, "%s: commit %s",
2062 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2063 err = got_error_from_errno("asprintf");
2064 goto done;
2067 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2068 char *link_target;
2069 err = got_object_blob_read_to_str(&link_target, blob);
2070 if (err)
2071 goto done;
2072 err = merge_symlink(worktree, blob2, ondisk_path, path,
2073 label_orig, link_target, worktree->base_commit_id,
2074 repo, progress_cb, progress_arg);
2075 free(link_target);
2076 } else {
2077 err = merge_blob(&update_timestamps, worktree, blob2,
2078 ondisk_path, path, sb.st_mode, label_orig, blob,
2079 worktree->base_commit_id, repo,
2080 progress_cb, progress_arg);
2082 free(label_orig);
2083 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2084 err = got_error_from_errno("close");
2085 goto done;
2087 if (blob2)
2088 got_object_blob_close(blob2);
2089 if (err)
2090 goto done;
2092 * Do not update timestamps of files with local changes.
2093 * Otherwise, a future status walk would treat them as
2094 * unmodified files again.
2096 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2097 blob->id.sha1, worktree->base_commit_id->sha1,
2098 update_timestamps);
2099 } else if (status == GOT_STATUS_MODE_CHANGE) {
2100 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2101 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2102 } else if (status == GOT_STATUS_DELETE) {
2103 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2104 if (err)
2105 goto done;
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2108 if (err)
2109 goto done;
2110 } else {
2111 int is_bad_symlink = 0;
2112 if (S_ISLNK(te->mode)) {
2113 err = install_symlink(&is_bad_symlink, worktree,
2114 ondisk_path, path, blob,
2115 status == GOT_STATUS_MISSING, 0,
2116 status == GOT_STATUS_UNVERSIONED, 0,
2117 repo, progress_cb, progress_arg);
2118 } else {
2119 err = install_blob(worktree, ondisk_path, path,
2120 te->mode, sb.st_mode, blob,
2121 status == GOT_STATUS_MISSING, 0, 0,
2122 status == GOT_STATUS_UNVERSIONED, repo,
2123 progress_cb, progress_arg);
2125 if (err)
2126 goto done;
2128 if (ie) {
2129 err = got_fileindex_entry_update(ie,
2130 worktree->root_fd, path, blob->id.sha1,
2131 worktree->base_commit_id->sha1, 1);
2132 } else {
2133 err = create_fileindex_entry(&ie, fileindex,
2134 worktree->base_commit_id, worktree->root_fd, path,
2135 &blob->id);
2137 if (err)
2138 goto done;
2140 if (is_bad_symlink) {
2141 got_fileindex_entry_filetype_set(ie,
2142 GOT_FILEIDX_MODE_BAD_SYMLINK);
2146 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2147 err = got_error_from_errno("close");
2148 goto done;
2150 got_object_blob_close(blob);
2151 done:
2152 free(ondisk_path);
2153 return err;
2156 static const struct got_error *
2157 remove_ondisk_file(const char *root_path, const char *path)
2159 const struct got_error *err = NULL;
2160 char *ondisk_path = NULL, *parent = NULL;
2162 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2163 return got_error_from_errno("asprintf");
2165 if (unlink(ondisk_path) == -1) {
2166 if (errno != ENOENT)
2167 err = got_error_from_errno2("unlink", ondisk_path);
2168 } else {
2169 size_t root_len = strlen(root_path);
2170 err = got_path_dirname(&parent, ondisk_path);
2171 if (err)
2172 goto done;
2173 while (got_path_cmp(parent, root_path,
2174 strlen(parent), root_len) != 0) {
2175 free(ondisk_path);
2176 ondisk_path = parent;
2177 parent = NULL;
2178 if (rmdir(ondisk_path) == -1) {
2179 if (errno != ENOTEMPTY)
2180 err = got_error_from_errno2("rmdir",
2181 ondisk_path);
2182 break;
2184 err = got_path_dirname(&parent, ondisk_path);
2185 if (err)
2186 break;
2189 done:
2190 free(ondisk_path);
2191 free(parent);
2192 return err;
2195 static const struct got_error *
2196 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2197 struct got_fileindex_entry *ie, struct got_repository *repo,
2198 got_worktree_checkout_cb progress_cb, void *progress_arg)
2200 const struct got_error *err = NULL;
2201 unsigned char status;
2202 struct stat sb;
2203 char *ondisk_path;
2205 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2206 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2208 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2209 == -1)
2210 return got_error_from_errno("asprintf");
2212 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2213 if (err)
2214 goto done;
2216 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2217 char ondisk_target[PATH_MAX];
2218 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2219 sizeof(ondisk_target));
2220 if (ondisk_len == -1) {
2221 err = got_error_from_errno2("readlink", ondisk_path);
2222 goto done;
2224 ondisk_target[ondisk_len] = '\0';
2225 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2226 NULL, NULL, /* XXX pass common ancestor info? */
2227 ondisk_target, ondisk_path);
2228 if (err)
2229 goto done;
2230 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2231 ie->path);
2232 goto done;
2235 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2236 status == GOT_STATUS_ADD) {
2237 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2238 if (err)
2239 goto done;
2241 * Preserve the working file and change the deleted blob's
2242 * entry into a schedule-add entry.
2244 err = got_fileindex_entry_update(ie, worktree->root_fd,
2245 ie->path, NULL, NULL, 0);
2246 } else {
2247 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2248 if (err)
2249 goto done;
2250 if (status == GOT_STATUS_NO_CHANGE) {
2251 err = remove_ondisk_file(worktree->root_path, ie->path);
2252 if (err)
2253 goto done;
2255 got_fileindex_entry_remove(fileindex, ie);
2257 done:
2258 free(ondisk_path);
2259 return err;
2262 struct diff_cb_arg {
2263 struct got_fileindex *fileindex;
2264 struct got_worktree *worktree;
2265 struct got_repository *repo;
2266 got_worktree_checkout_cb progress_cb;
2267 void *progress_arg;
2268 got_cancel_cb cancel_cb;
2269 void *cancel_arg;
2272 static const struct got_error *
2273 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2274 struct got_tree_entry *te, const char *parent_path)
2276 const struct got_error *err = NULL;
2277 struct diff_cb_arg *a = arg;
2279 if (a->cancel_cb) {
2280 err = a->cancel_cb(a->cancel_arg);
2281 if (err)
2282 return err;
2285 return update_blob(a->worktree, a->fileindex, ie, te,
2286 ie->path, a->repo, a->progress_cb, a->progress_arg);
2289 static const struct got_error *
2290 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2292 const struct got_error *err = NULL;
2293 struct diff_cb_arg *a = arg;
2295 if (a->cancel_cb) {
2296 err = a->cancel_cb(a->cancel_arg);
2297 if (err)
2298 return err;
2301 return delete_blob(a->worktree, a->fileindex, ie,
2302 a->repo, a->progress_cb, a->progress_arg);
2305 static const struct got_error *
2306 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2308 const struct got_error *err = NULL;
2309 struct diff_cb_arg *a = arg;
2310 char *path;
2312 if (a->cancel_cb) {
2313 err = a->cancel_cb(a->cancel_arg);
2314 if (err)
2315 return err;
2318 if (got_object_tree_entry_is_submodule(te))
2319 return NULL;
2321 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2322 return NULL;
2324 if (asprintf(&path, "%s%s%s", parent_path,
2325 parent_path[0] ? "/" : "", te->name)
2326 == -1)
2327 return got_error_from_errno("asprintf");
2329 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2330 a->repo, a->progress_cb, a->progress_arg);
2332 free(path);
2333 return err;
2336 const struct got_error *
2337 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2339 uint32_t uuid_status;
2341 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2342 if (uuid_status != uuid_s_ok) {
2343 *uuidstr = NULL;
2344 return got_error_uuid(uuid_status, "uuid_to_string");
2347 return NULL;
2350 static const struct got_error *
2351 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2353 const struct got_error *err = NULL;
2354 char *uuidstr = NULL;
2356 *refname = NULL;
2358 err = got_worktree_get_uuid(&uuidstr, worktree);
2359 if (err)
2360 return err;
2362 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2363 err = got_error_from_errno("asprintf");
2364 *refname = NULL;
2366 free(uuidstr);
2367 return err;
2370 const struct got_error *
2371 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2372 const char *prefix)
2374 return get_ref_name(refname, worktree, prefix);
2377 static const struct got_error *
2378 get_base_ref_name(char **refname, struct got_worktree *worktree)
2380 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2383 static const struct got_error *
2384 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2386 return get_ref_name(refname, worktree,
2387 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2390 static const struct got_error *
2391 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2396 static const struct got_error *
2397 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2403 static const struct got_error *
2404 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2410 static const struct got_error *
2411 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2413 return get_ref_name(refname, worktree,
2414 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2417 static const struct got_error *
2418 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2420 return get_ref_name(refname, worktree,
2421 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2424 static const struct got_error *
2425 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2427 return get_ref_name(refname, worktree,
2428 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2431 static const struct got_error *
2432 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2434 return get_ref_name(refname, worktree,
2435 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2438 const struct got_error *
2439 got_worktree_get_histedit_script_path(char **path,
2440 struct got_worktree *worktree)
2442 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2443 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2444 *path = NULL;
2445 return got_error_from_errno("asprintf");
2447 return NULL;
2450 static const struct got_error *
2451 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2453 return get_ref_name(refname, worktree,
2454 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2457 static const struct got_error *
2458 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2460 return get_ref_name(refname, worktree,
2461 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2465 * Prevent Git's garbage collector from deleting our base commit by
2466 * setting a reference to our base commit's ID.
2468 static const struct got_error *
2469 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2471 const struct got_error *err = NULL;
2472 struct got_reference *ref = NULL;
2473 char *refname;
2475 err = get_base_ref_name(&refname, worktree);
2476 if (err)
2477 return err;
2479 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2480 if (err)
2481 goto done;
2483 err = got_ref_write(ref, repo);
2484 done:
2485 free(refname);
2486 if (ref)
2487 got_ref_close(ref);
2488 return err;
2491 static const struct got_error *
2492 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2494 const struct got_error *err = NULL;
2496 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2497 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2498 err = got_error_from_errno("asprintf");
2499 *fileindex_path = NULL;
2501 return err;
2505 static const struct got_error *
2506 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2507 struct got_worktree *worktree)
2509 const struct got_error *err = NULL;
2510 FILE *index = NULL;
2512 *fileindex_path = NULL;
2513 *fileindex = got_fileindex_alloc();
2514 if (*fileindex == NULL)
2515 return got_error_from_errno("got_fileindex_alloc");
2517 err = get_fileindex_path(fileindex_path, worktree);
2518 if (err)
2519 goto done;
2521 index = fopen(*fileindex_path, "rbe");
2522 if (index == NULL) {
2523 if (errno != ENOENT)
2524 err = got_error_from_errno2("fopen", *fileindex_path);
2525 } else {
2526 err = got_fileindex_read(*fileindex, index);
2527 if (fclose(index) == EOF && err == NULL)
2528 err = got_error_from_errno("fclose");
2530 done:
2531 if (err) {
2532 free(*fileindex_path);
2533 *fileindex_path = NULL;
2534 got_fileindex_free(*fileindex);
2535 *fileindex = NULL;
2537 return err;
2540 struct bump_base_commit_id_arg {
2541 struct got_object_id *base_commit_id;
2542 const char *path;
2543 size_t path_len;
2544 const char *entry_name;
2545 got_worktree_checkout_cb progress_cb;
2546 void *progress_arg;
2549 static const struct got_error *
2550 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2552 const struct got_error *err;
2553 struct bump_base_commit_id_arg *a = arg;
2555 if (a->entry_name) {
2556 if (strcmp(ie->path, a->path) != 0)
2557 return NULL;
2558 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2559 return NULL;
2561 if (got_fileindex_entry_was_skipped(ie))
2562 return NULL;
2564 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2565 SHA1_DIGEST_LENGTH) == 0)
2566 return NULL;
2568 if (a->progress_cb) {
2569 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2570 ie->path);
2571 if (err)
2572 return err;
2574 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2575 return NULL;
2578 /* Bump base commit ID of all files within an updated part of the work tree. */
2579 static const struct got_error *
2580 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2581 struct got_fileindex *fileindex,
2582 got_worktree_checkout_cb progress_cb, void *progress_arg)
2584 struct bump_base_commit_id_arg bbc_arg;
2586 bbc_arg.base_commit_id = worktree->base_commit_id;
2587 bbc_arg.entry_name = NULL;
2588 bbc_arg.path = "";
2589 bbc_arg.path_len = 0;
2590 bbc_arg.progress_cb = progress_cb;
2591 bbc_arg.progress_arg = progress_arg;
2593 return got_fileindex_for_each_entry_safe(fileindex,
2594 bump_base_commit_id, &bbc_arg);
2597 static const struct got_error *
2598 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2600 const struct got_error *err = NULL;
2601 char *new_fileindex_path = NULL;
2602 FILE *new_index = NULL;
2603 struct timespec timeout;
2605 err = got_opentemp_named(&new_fileindex_path, &new_index,
2606 fileindex_path, "");
2607 if (err)
2608 goto done;
2610 err = got_fileindex_write(fileindex, new_index);
2611 if (err)
2612 goto done;
2614 if (rename(new_fileindex_path, fileindex_path) != 0) {
2615 err = got_error_from_errno3("rename", new_fileindex_path,
2616 fileindex_path);
2617 unlink(new_fileindex_path);
2621 * Sleep for a short amount of time to ensure that files modified after
2622 * this program exits have a different time stamp from the one which
2623 * was recorded in the file index.
2625 timeout.tv_sec = 0;
2626 timeout.tv_nsec = 1;
2627 nanosleep(&timeout, NULL);
2628 done:
2629 if (new_index)
2630 fclose(new_index);
2631 free(new_fileindex_path);
2632 return err;
2635 static const struct got_error *
2636 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2637 struct got_object_id **tree_id, const char *wt_relpath,
2638 struct got_commit_object *base_commit, struct got_worktree *worktree,
2639 struct got_repository *repo)
2641 const struct got_error *err = NULL;
2642 struct got_object_id *id = NULL;
2643 char *in_repo_path = NULL;
2644 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2646 *entry_type = GOT_OBJ_TYPE_ANY;
2647 *tree_relpath = NULL;
2648 *tree_id = NULL;
2650 if (wt_relpath[0] == '\0') {
2651 /* Check out all files within the work tree. */
2652 *entry_type = GOT_OBJ_TYPE_TREE;
2653 *tree_relpath = strdup("");
2654 if (*tree_relpath == NULL) {
2655 err = got_error_from_errno("strdup");
2656 goto done;
2658 err = got_object_id_by_path(tree_id, repo, base_commit,
2659 worktree->path_prefix);
2660 if (err)
2661 goto done;
2662 return NULL;
2665 /* Check out a subset of files in the work tree. */
2667 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2668 is_root_wt ? "" : "/", wt_relpath) == -1) {
2669 err = got_error_from_errno("asprintf");
2670 goto done;
2673 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2674 if (err)
2675 goto done;
2677 free(in_repo_path);
2678 in_repo_path = NULL;
2680 err = got_object_get_type(entry_type, repo, id);
2681 if (err)
2682 goto done;
2684 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2685 /* Check out a single file. */
2686 if (strchr(wt_relpath, '/') == NULL) {
2687 /* Check out a single file in work tree's root dir. */
2688 in_repo_path = strdup(worktree->path_prefix);
2689 if (in_repo_path == NULL) {
2690 err = got_error_from_errno("strdup");
2691 goto done;
2693 *tree_relpath = strdup("");
2694 if (*tree_relpath == NULL) {
2695 err = got_error_from_errno("strdup");
2696 goto done;
2698 } else {
2699 /* Check out a single file in a subdirectory. */
2700 err = got_path_dirname(tree_relpath, wt_relpath);
2701 if (err)
2702 return err;
2703 if (asprintf(&in_repo_path, "%s%s%s",
2704 worktree->path_prefix, is_root_wt ? "" : "/",
2705 *tree_relpath) == -1) {
2706 err = got_error_from_errno("asprintf");
2707 goto done;
2710 err = got_object_id_by_path(tree_id, repo,
2711 base_commit, in_repo_path);
2712 } else {
2713 /* Check out all files within a subdirectory. */
2714 *tree_id = got_object_id_dup(id);
2715 if (*tree_id == NULL) {
2716 err = got_error_from_errno("got_object_id_dup");
2717 goto done;
2719 *tree_relpath = strdup(wt_relpath);
2720 if (*tree_relpath == NULL) {
2721 err = got_error_from_errno("strdup");
2722 goto done;
2725 done:
2726 free(id);
2727 free(in_repo_path);
2728 if (err) {
2729 *entry_type = GOT_OBJ_TYPE_ANY;
2730 free(*tree_relpath);
2731 *tree_relpath = NULL;
2732 free(*tree_id);
2733 *tree_id = NULL;
2735 return err;
2738 static const struct got_error *
2739 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2740 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2741 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2742 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2744 const struct got_error *err = NULL;
2745 struct got_commit_object *commit = NULL;
2746 struct got_tree_object *tree = NULL;
2747 struct got_fileindex_diff_tree_cb diff_cb;
2748 struct diff_cb_arg arg;
2750 err = ref_base_commit(worktree, repo);
2751 if (err) {
2752 if (!(err->code == GOT_ERR_ERRNO &&
2753 (errno == EACCES || errno == EROFS)))
2754 goto done;
2755 err = (*progress_cb)(progress_arg,
2756 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2757 if (err)
2758 return err;
2761 err = got_object_open_as_commit(&commit, repo,
2762 worktree->base_commit_id);
2763 if (err)
2764 goto done;
2766 err = got_object_open_as_tree(&tree, repo, tree_id);
2767 if (err)
2768 goto done;
2770 if (entry_name &&
2771 got_object_tree_find_entry(tree, entry_name) == NULL) {
2772 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2773 goto done;
2776 diff_cb.diff_old_new = diff_old_new;
2777 diff_cb.diff_old = diff_old;
2778 diff_cb.diff_new = diff_new;
2779 arg.fileindex = fileindex;
2780 arg.worktree = worktree;
2781 arg.repo = repo;
2782 arg.progress_cb = progress_cb;
2783 arg.progress_arg = progress_arg;
2784 arg.cancel_cb = cancel_cb;
2785 arg.cancel_arg = cancel_arg;
2786 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2787 entry_name, repo, &diff_cb, &arg);
2788 done:
2789 if (tree)
2790 got_object_tree_close(tree);
2791 if (commit)
2792 got_object_commit_close(commit);
2793 return err;
2796 const struct got_error *
2797 got_worktree_checkout_files(struct got_worktree *worktree,
2798 struct got_pathlist_head *paths, struct got_repository *repo,
2799 got_worktree_checkout_cb progress_cb, void *progress_arg,
2800 got_cancel_cb cancel_cb, void *cancel_arg)
2802 const struct got_error *err = NULL, *sync_err, *unlockerr;
2803 struct got_commit_object *commit = NULL;
2804 struct got_tree_object *tree = NULL;
2805 struct got_fileindex *fileindex = NULL;
2806 char *fileindex_path = NULL;
2807 struct got_pathlist_entry *pe;
2808 struct tree_path_data {
2809 STAILQ_ENTRY(tree_path_data) entry;
2810 struct got_object_id *tree_id;
2811 int entry_type;
2812 char *relpath;
2813 char *entry_name;
2814 } *tpd = NULL;
2815 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2817 STAILQ_INIT(&tree_paths);
2819 err = lock_worktree(worktree, LOCK_EX);
2820 if (err)
2821 return err;
2823 err = got_object_open_as_commit(&commit, repo,
2824 worktree->base_commit_id);
2825 if (err)
2826 goto done;
2828 /* Map all specified paths to in-repository trees. */
2829 TAILQ_FOREACH(pe, paths, entry) {
2830 tpd = malloc(sizeof(*tpd));
2831 if (tpd == NULL) {
2832 err = got_error_from_errno("malloc");
2833 goto done;
2836 err = find_tree_entry_for_checkout(&tpd->entry_type,
2837 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2838 worktree, repo);
2839 if (err) {
2840 free(tpd);
2841 goto done;
2844 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2845 err = got_path_basename(&tpd->entry_name, pe->path);
2846 if (err) {
2847 free(tpd->relpath);
2848 free(tpd->tree_id);
2849 free(tpd);
2850 goto done;
2852 } else
2853 tpd->entry_name = NULL;
2855 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2859 * Read the file index.
2860 * Checking out files is supposed to be an idempotent operation.
2861 * If the on-disk file index is incomplete we will try to complete it.
2863 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2864 if (err)
2865 goto done;
2867 tpd = STAILQ_FIRST(&tree_paths);
2868 TAILQ_FOREACH(pe, paths, entry) {
2869 struct bump_base_commit_id_arg bbc_arg;
2871 err = checkout_files(worktree, fileindex, tpd->relpath,
2872 tpd->tree_id, tpd->entry_name, repo,
2873 progress_cb, progress_arg, cancel_cb, cancel_arg);
2874 if (err)
2875 break;
2877 bbc_arg.base_commit_id = worktree->base_commit_id;
2878 bbc_arg.entry_name = tpd->entry_name;
2879 bbc_arg.path = pe->path;
2880 bbc_arg.path_len = pe->path_len;
2881 bbc_arg.progress_cb = progress_cb;
2882 bbc_arg.progress_arg = progress_arg;
2883 err = got_fileindex_for_each_entry_safe(fileindex,
2884 bump_base_commit_id, &bbc_arg);
2885 if (err)
2886 break;
2888 tpd = STAILQ_NEXT(tpd, entry);
2890 sync_err = sync_fileindex(fileindex, fileindex_path);
2891 if (sync_err && err == NULL)
2892 err = sync_err;
2893 done:
2894 free(fileindex_path);
2895 if (tree)
2896 got_object_tree_close(tree);
2897 if (commit)
2898 got_object_commit_close(commit);
2899 if (fileindex)
2900 got_fileindex_free(fileindex);
2901 while (!STAILQ_EMPTY(&tree_paths)) {
2902 tpd = STAILQ_FIRST(&tree_paths);
2903 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2904 free(tpd->relpath);
2905 free(tpd->tree_id);
2906 free(tpd);
2908 unlockerr = lock_worktree(worktree, LOCK_SH);
2909 if (unlockerr && err == NULL)
2910 err = unlockerr;
2911 return err;
2914 static const struct got_error *
2915 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2916 struct got_fileindex_entry *ie, const char *ondisk_path,
2917 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2918 int restoring_missing_file, int reverting_versioned_file,
2919 int path_is_unversioned, int allow_bad_symlinks,
2920 struct got_repository *repo,
2921 got_worktree_checkout_cb progress_cb, void *progress_arg)
2923 const struct got_error *err = NULL;
2924 int is_bad_symlink = 0;
2926 if (S_ISLNK(mode2)) {
2927 err = install_symlink(&is_bad_symlink,
2928 worktree, ondisk_path, path2, blob2,
2929 restoring_missing_file,
2930 reverting_versioned_file,
2931 path_is_unversioned, allow_bad_symlinks,
2932 repo, progress_cb, progress_arg);
2933 } else {
2934 err = install_blob(worktree, ondisk_path, path2,
2935 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2936 restoring_missing_file, reverting_versioned_file, 0,
2937 path_is_unversioned, repo, progress_cb, progress_arg);
2939 if (err)
2940 return err;
2941 if (ie == NULL) {
2942 /* Adding an unversioned file. */
2943 err = got_fileindex_entry_alloc(&ie, path2);
2944 if (err)
2945 return err;
2946 err = got_fileindex_entry_update(ie,
2947 worktree->root_fd, path2, NULL, NULL, 1);
2948 if (err) {
2949 got_fileindex_entry_free(ie);
2950 return err;
2952 err = got_fileindex_entry_add(fileindex, ie);
2953 if (err) {
2954 got_fileindex_entry_free(ie);
2955 return err;
2957 } else {
2958 /* Re-adding a locally deleted file. */
2959 err = got_fileindex_entry_update(ie,
2960 worktree->root_fd, path2, ie->blob_sha1,
2961 worktree->base_commit_id->sha1, 0);
2962 if (err)
2963 return err;
2966 if (is_bad_symlink) {
2967 got_fileindex_entry_filetype_set(ie,
2968 GOT_FILEIDX_MODE_BAD_SYMLINK);
2971 return NULL;
2974 struct merge_file_cb_arg {
2975 struct got_worktree *worktree;
2976 struct got_fileindex *fileindex;
2977 got_worktree_checkout_cb progress_cb;
2978 void *progress_arg;
2979 got_cancel_cb cancel_cb;
2980 void *cancel_arg;
2981 const char *label_orig;
2982 struct got_object_id *commit_id2;
2983 int allow_bad_symlinks;
2986 static const struct got_error *
2987 merge_file_cb(void *arg, struct got_blob_object *blob1,
2988 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2989 struct got_object_id *id1, struct got_object_id *id2,
2990 const char *path1, const char *path2,
2991 mode_t mode1, mode_t mode2, struct got_repository *repo)
2993 static const struct got_error *err = NULL;
2994 struct merge_file_cb_arg *a = arg;
2995 struct got_fileindex_entry *ie;
2996 char *ondisk_path = NULL;
2997 struct stat sb;
2998 unsigned char status;
2999 int local_changes_subsumed;
3000 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3001 char *id_str = NULL, *label_deriv2 = NULL;
3002 struct got_object_id *id = NULL;
3004 if (blob1 && blob2) {
3005 ie = got_fileindex_entry_get(a->fileindex, path2,
3006 strlen(path2));
3007 if (ie == NULL)
3008 return (*a->progress_cb)(a->progress_arg,
3009 GOT_STATUS_MISSING, path2);
3011 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3012 path2) == -1)
3013 return got_error_from_errno("asprintf");
3015 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3016 repo);
3017 if (err)
3018 goto done;
3020 if (status == GOT_STATUS_DELETE) {
3021 err = (*a->progress_cb)(a->progress_arg,
3022 GOT_STATUS_MERGE, path2);
3023 goto done;
3025 if (status != GOT_STATUS_NO_CHANGE &&
3026 status != GOT_STATUS_MODIFY &&
3027 status != GOT_STATUS_CONFLICT &&
3028 status != GOT_STATUS_ADD) {
3029 err = (*a->progress_cb)(a->progress_arg, status, path2);
3030 goto done;
3033 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3034 char *link_target2;
3035 err = got_object_blob_read_to_str(&link_target2, blob2);
3036 if (err)
3037 goto done;
3038 err = merge_symlink(a->worktree, blob1, ondisk_path,
3039 path2, a->label_orig, link_target2, a->commit_id2,
3040 repo, a->progress_cb, a->progress_arg);
3041 free(link_target2);
3042 } else {
3043 int fd;
3045 f_orig = got_opentemp();
3046 if (f_orig == NULL) {
3047 err = got_error_from_errno("got_opentemp");
3048 goto done;
3050 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3051 f_orig, blob1);
3052 if (err)
3053 goto done;
3055 f_deriv2 = got_opentemp();
3056 if (f_deriv2 == NULL)
3057 goto done;
3058 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3059 f_deriv2, blob2);
3060 if (err)
3061 goto done;
3063 fd = open(ondisk_path,
3064 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3065 if (fd == -1) {
3066 err = got_error_from_errno2("open",
3067 ondisk_path);
3068 goto done;
3070 f_deriv = fdopen(fd, "r");
3071 if (f_deriv == NULL) {
3072 err = got_error_from_errno2("fdopen",
3073 ondisk_path);
3074 close(fd);
3075 goto done;
3077 err = got_object_id_str(&id_str, a->commit_id2);
3078 if (err)
3079 goto done;
3080 if (asprintf(&label_deriv2, "%s: commit %s",
3081 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3082 err = got_error_from_errno("asprintf");
3083 goto done;
3085 err = merge_file(&local_changes_subsumed, a->worktree,
3086 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3087 mode2, a->label_orig, NULL, label_deriv2,
3088 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3089 a->progress_cb, a->progress_arg);
3091 } else if (blob1) {
3092 ie = got_fileindex_entry_get(a->fileindex, path1,
3093 strlen(path1));
3094 if (ie == NULL)
3095 return (*a->progress_cb)(a->progress_arg,
3096 GOT_STATUS_MISSING, path1);
3098 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3099 path1) == -1)
3100 return got_error_from_errno("asprintf");
3102 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3103 repo);
3104 if (err)
3105 goto done;
3107 switch (status) {
3108 case GOT_STATUS_NO_CHANGE:
3109 err = (*a->progress_cb)(a->progress_arg,
3110 GOT_STATUS_DELETE, path1);
3111 if (err)
3112 goto done;
3113 err = remove_ondisk_file(a->worktree->root_path, path1);
3114 if (err)
3115 goto done;
3116 if (ie)
3117 got_fileindex_entry_mark_deleted_from_disk(ie);
3118 break;
3119 case GOT_STATUS_DELETE:
3120 case GOT_STATUS_MISSING:
3121 err = (*a->progress_cb)(a->progress_arg,
3122 GOT_STATUS_DELETE, path1);
3123 if (err)
3124 goto done;
3125 if (ie)
3126 got_fileindex_entry_mark_deleted_from_disk(ie);
3127 break;
3128 case GOT_STATUS_MODIFY: {
3129 FILE *blob1_f;
3130 off_t blob1_size;
3131 int obj_type;
3133 * Delete the file only if its content already
3134 * exists in the repository.
3136 err = got_object_blob_file_create(&id, &blob1_f,
3137 &blob1_size, path1);
3138 if (err)
3139 goto done;
3140 if (fclose(blob1_f) == EOF) {
3141 err = got_error_from_errno("fclose");
3142 goto done;
3145 /* Implied existence check. */
3146 err = got_object_get_type(&obj_type, repo, id);
3147 if (err) {
3148 if (err->code != GOT_ERR_NO_OBJ)
3149 goto done;
3150 err = (*a->progress_cb)(a->progress_arg,
3151 GOT_STATUS_CANNOT_DELETE, path1);
3152 goto done;
3153 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3154 err = (*a->progress_cb)(a->progress_arg,
3155 GOT_STATUS_CANNOT_DELETE, path1);
3156 goto done;
3158 err = (*a->progress_cb)(a->progress_arg,
3159 GOT_STATUS_DELETE, path1);
3160 if (err)
3161 goto done;
3162 err = remove_ondisk_file(a->worktree->root_path,
3163 path1);
3164 if (err)
3165 goto done;
3166 if (ie)
3167 got_fileindex_entry_mark_deleted_from_disk(ie);
3168 break;
3170 case GOT_STATUS_ADD: {
3171 FILE *blob1_f;
3172 off_t blob1_size;
3174 * Delete the file only if its content already
3175 * exists in the repository.
3177 err = got_object_blob_file_create(&id, &blob1_f,
3178 &blob1_size, path1);
3179 if (err)
3180 goto done;
3181 if (fclose(blob1_f) == EOF) {
3182 err = got_error_from_errno("fclose");
3183 goto done;
3185 if (got_object_id_cmp(id, id1) == 0) {
3186 err = (*a->progress_cb)(a->progress_arg,
3187 GOT_STATUS_DELETE, path1);
3188 if (err)
3189 goto done;
3190 err = remove_ondisk_file(a->worktree->root_path,
3191 path1);
3192 if (err)
3193 goto done;
3194 if (ie)
3195 got_fileindex_entry_remove(a->fileindex,
3196 ie);
3197 } else {
3198 err = (*a->progress_cb)(a->progress_arg,
3199 GOT_STATUS_CANNOT_DELETE, path1);
3201 if (err)
3202 goto done;
3203 break;
3205 case GOT_STATUS_CONFLICT:
3206 err = (*a->progress_cb)(a->progress_arg,
3207 GOT_STATUS_CANNOT_DELETE, path1);
3208 if (err)
3209 goto done;
3210 break;
3211 case GOT_STATUS_OBSTRUCTED:
3212 err = (*a->progress_cb)(a->progress_arg, status, path1);
3213 if (err)
3214 goto done;
3215 break;
3216 default:
3217 break;
3219 } else if (blob2) {
3220 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3221 path2) == -1)
3222 return got_error_from_errno("asprintf");
3223 ie = got_fileindex_entry_get(a->fileindex, path2,
3224 strlen(path2));
3225 if (ie) {
3226 err = get_file_status(&status, &sb, ie, ondisk_path,
3227 -1, NULL, repo);
3228 if (err)
3229 goto done;
3230 if (status != GOT_STATUS_NO_CHANGE &&
3231 status != GOT_STATUS_MODIFY &&
3232 status != GOT_STATUS_CONFLICT &&
3233 status != GOT_STATUS_ADD &&
3234 status != GOT_STATUS_DELETE) {
3235 err = (*a->progress_cb)(a->progress_arg,
3236 status, path2);
3237 goto done;
3239 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3240 char *link_target2;
3241 err = got_object_blob_read_to_str(&link_target2,
3242 blob2);
3243 if (err)
3244 goto done;
3245 err = merge_symlink(a->worktree, NULL,
3246 ondisk_path, path2, a->label_orig,
3247 link_target2, a->commit_id2, repo,
3248 a->progress_cb, a->progress_arg);
3249 free(link_target2);
3250 } else if (S_ISREG(sb.st_mode)) {
3251 err = merge_blob(&local_changes_subsumed,
3252 a->worktree, NULL, ondisk_path, path2,
3253 sb.st_mode, a->label_orig, blob2,
3254 a->commit_id2, repo, a->progress_cb,
3255 a->progress_arg);
3256 } else if (status != GOT_STATUS_DELETE) {
3257 err = got_error_path(ondisk_path,
3258 GOT_ERR_FILE_OBSTRUCTED);
3260 if (err)
3261 goto done;
3262 if (status == GOT_STATUS_DELETE) {
3263 /* Re-add file with content from new blob. */
3264 err = add_file(a->worktree, a->fileindex, ie,
3265 ondisk_path, path2, blob2, mode2,
3266 0, 0, 0, a->allow_bad_symlinks,
3267 repo, a->progress_cb, a->progress_arg);
3268 if (err)
3269 goto done;
3271 } else {
3272 err = add_file(a->worktree, a->fileindex, NULL,
3273 ondisk_path, path2, blob2, mode2,
3274 0, 0, 1, a->allow_bad_symlinks,
3275 repo, a->progress_cb, a->progress_arg);
3276 if (err)
3277 goto done;
3280 done:
3281 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3282 err = got_error_from_errno("fclose");
3283 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3284 err = got_error_from_errno("fclose");
3285 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3286 err = got_error_from_errno("fclose");
3287 free(id_str);
3288 free(id);
3289 free(label_deriv2);
3290 free(ondisk_path);
3291 return err;
3294 struct check_mixed_commits_args {
3295 struct got_worktree *worktree;
3296 got_cancel_cb cancel_cb;
3297 void *cancel_arg;
3300 static const struct got_error *
3301 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3303 const struct got_error *err = NULL;
3304 struct check_mixed_commits_args *a = arg;
3306 if (a->cancel_cb) {
3307 err = a->cancel_cb(a->cancel_arg);
3308 if (err)
3309 return err;
3312 /* Reject merges into a work tree with mixed base commits. */
3313 if (got_fileindex_entry_has_commit(ie) &&
3314 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3315 SHA1_DIGEST_LENGTH) != 0)
3316 return got_error(GOT_ERR_MIXED_COMMITS);
3318 return NULL;
3321 const struct got_error *
3322 got_worktree_get_state(char *state, struct got_repository *repo,
3323 struct got_worktree *worktree,
3324 got_cancel_cb cancel_cb, void *cancel_arg)
3326 const struct got_error *err;
3327 struct got_object_id *base_id, *head_id = NULL;
3328 struct got_reference *head_ref;
3329 struct got_fileindex *fileindex = NULL;
3330 char *fileindex_path = NULL;
3331 struct check_mixed_commits_args cma;
3333 if (worktree == NULL)
3334 return got_error(GOT_ERR_NOT_WORKTREE);
3336 err = got_ref_open(&head_ref, repo,
3337 got_worktree_get_head_ref_name(worktree), 0);
3338 if (err)
3339 return err;
3341 err = got_ref_resolve(&head_id, repo, head_ref);
3342 if (err)
3343 goto done;
3345 *state = GOT_WORKTREE_STATE_UNKNOWN;
3346 base_id = got_worktree_get_base_commit_id(worktree);
3348 cma.worktree = worktree;
3349 cma.cancel_cb = cancel_cb;
3350 cma.cancel_arg = cancel_arg;
3352 if (got_object_id_cmp(base_id, head_id) == 0) {
3353 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3354 if (err)
3355 goto done;
3357 err = got_fileindex_for_each_entry_safe(fileindex,
3358 check_mixed_commits, &cma);
3359 if (err == NULL)
3360 *state = GOT_WORKTREE_STATE_UPTODATE;
3361 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3362 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3363 err = NULL;
3365 } else
3366 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3368 done:
3369 free(head_id);
3370 free(fileindex_path);
3371 got_ref_close(head_ref);
3372 if (fileindex != NULL)
3373 got_fileindex_free(fileindex);
3374 return err;
3377 struct check_merge_conflicts_arg {
3378 struct got_worktree *worktree;
3379 struct got_fileindex *fileindex;
3380 struct got_repository *repo;
3383 static const struct got_error *
3384 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3385 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3386 struct got_object_id *id1, struct got_object_id *id2,
3387 const char *path1, const char *path2,
3388 mode_t mode1, mode_t mode2, struct got_repository *repo)
3390 const struct got_error *err = NULL;
3391 struct check_merge_conflicts_arg *a = arg;
3392 unsigned char status;
3393 struct stat sb;
3394 struct got_fileindex_entry *ie;
3395 const char *path = path2 ? path2 : path1;
3396 struct got_object_id *id = id2 ? id2 : id1;
3397 char *ondisk_path;
3399 if (id == NULL)
3400 return NULL;
3402 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3403 if (ie == NULL)
3404 return NULL;
3406 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3407 == -1)
3408 return got_error_from_errno("asprintf");
3410 /* Reject merges into a work tree with conflicted files. */
3411 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3412 free(ondisk_path);
3413 if (err)
3414 return err;
3415 if (status == GOT_STATUS_CONFLICT)
3416 return got_error(GOT_ERR_CONFLICTS);
3418 return NULL;
3421 static const struct got_error *
3422 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3423 const char *fileindex_path, struct got_object_id *commit_id1,
3424 struct got_object_id *commit_id2, struct got_repository *repo,
3425 got_worktree_checkout_cb progress_cb, void *progress_arg,
3426 got_cancel_cb cancel_cb, void *cancel_arg)
3428 const struct got_error *err = NULL, *sync_err;
3429 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3430 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3431 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3432 struct check_merge_conflicts_arg cmc_arg;
3433 struct merge_file_cb_arg arg;
3434 char *label_orig = NULL;
3435 FILE *f1 = NULL, *f2 = NULL;
3436 int fd1 = -1, fd2 = -1;
3438 if (commit_id1) {
3439 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3440 if (err)
3441 goto done;
3442 err = got_object_id_by_path(&tree_id1, repo, commit1,
3443 worktree->path_prefix);
3444 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3445 goto done;
3447 if (tree_id1) {
3448 char *id_str;
3450 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3451 if (err)
3452 goto done;
3454 err = got_object_id_str(&id_str, commit_id1);
3455 if (err)
3456 goto done;
3458 if (asprintf(&label_orig, "%s: commit %s",
3459 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3460 err = got_error_from_errno("asprintf");
3461 free(id_str);
3462 goto done;
3464 free(id_str);
3466 f1 = got_opentemp();
3467 if (f1 == NULL) {
3468 err = got_error_from_errno("got_opentemp");
3469 goto done;
3473 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3474 if (err)
3475 goto done;
3477 err = got_object_id_by_path(&tree_id2, repo, commit2,
3478 worktree->path_prefix);
3479 if (err)
3480 goto done;
3482 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3483 if (err)
3484 goto done;
3486 f2 = got_opentemp();
3487 if (f2 == NULL) {
3488 err = got_error_from_errno("got_opentemp");
3489 goto done;
3492 fd1 = got_opentempfd();
3493 if (fd1 == -1) {
3494 err = got_error_from_errno("got_opentempfd");
3495 goto done;
3498 fd2 = got_opentempfd();
3499 if (fd2 == -1) {
3500 err = got_error_from_errno("got_opentempfd");
3501 goto done;
3504 cmc_arg.worktree = worktree;
3505 cmc_arg.fileindex = fileindex;
3506 cmc_arg.repo = repo;
3507 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3508 check_merge_conflicts, &cmc_arg, 0);
3509 if (err)
3510 goto done;
3512 arg.worktree = worktree;
3513 arg.fileindex = fileindex;
3514 arg.progress_cb = progress_cb;
3515 arg.progress_arg = progress_arg;
3516 arg.cancel_cb = cancel_cb;
3517 arg.cancel_arg = cancel_arg;
3518 arg.label_orig = label_orig;
3519 arg.commit_id2 = commit_id2;
3520 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3521 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3522 merge_file_cb, &arg, 1);
3523 sync_err = sync_fileindex(fileindex, fileindex_path);
3524 if (sync_err && err == NULL)
3525 err = sync_err;
3526 done:
3527 if (commit1)
3528 got_object_commit_close(commit1);
3529 if (commit2)
3530 got_object_commit_close(commit2);
3531 if (tree1)
3532 got_object_tree_close(tree1);
3533 if (tree2)
3534 got_object_tree_close(tree2);
3535 if (f1 && fclose(f1) == EOF && err == NULL)
3536 err = got_error_from_errno("fclose");
3537 if (f2 && fclose(f2) == EOF && err == NULL)
3538 err = got_error_from_errno("fclose");
3539 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3540 err = got_error_from_errno("close");
3541 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3542 err = got_error_from_errno("close");
3543 free(label_orig);
3544 return err;
3547 const struct got_error *
3548 got_worktree_merge_files(struct got_worktree *worktree,
3549 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3550 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3551 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3553 const struct got_error *err, *unlockerr;
3554 char *fileindex_path = NULL;
3555 struct got_fileindex *fileindex = NULL;
3556 struct check_mixed_commits_args cma;
3558 err = lock_worktree(worktree, LOCK_EX);
3559 if (err)
3560 return err;
3562 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3563 if (err)
3564 goto done;
3566 cma.worktree = worktree;
3567 cma.cancel_cb = cancel_cb;
3568 cma.cancel_arg = cancel_arg;
3570 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3571 &cma);
3572 if (err)
3573 goto done;
3575 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3576 commit_id2, repo, progress_cb, progress_arg,
3577 cancel_cb, cancel_arg);
3578 done:
3579 if (fileindex)
3580 got_fileindex_free(fileindex);
3581 free(fileindex_path);
3582 unlockerr = lock_worktree(worktree, LOCK_SH);
3583 if (unlockerr && err == NULL)
3584 err = unlockerr;
3585 return err;
3588 struct diff_dir_cb_arg {
3589 struct got_fileindex *fileindex;
3590 struct got_worktree *worktree;
3591 const char *status_path;
3592 size_t status_path_len;
3593 struct got_repository *repo;
3594 got_worktree_status_cb status_cb;
3595 void *status_arg;
3596 got_cancel_cb cancel_cb;
3597 void *cancel_arg;
3598 /* A pathlist containing per-directory pathlists of ignore patterns. */
3599 struct got_pathlist_head *ignores;
3600 int report_unchanged;
3601 int no_ignores;
3604 static const struct got_error *
3605 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3606 int dirfd, const char *de_name,
3607 got_worktree_status_cb status_cb, void *status_arg,
3608 struct got_repository *repo, int report_unchanged)
3610 const struct got_error *err = NULL;
3611 unsigned char status = GOT_STATUS_NO_CHANGE;
3612 unsigned char staged_status;
3613 struct stat sb;
3614 struct got_object_id blob_id, commit_id, staged_blob_id;
3615 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3616 struct got_object_id *staged_blob_idp = NULL;
3618 staged_status = get_staged_status(ie);
3619 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3620 if (err)
3621 return err;
3623 if (status == GOT_STATUS_NO_CHANGE &&
3624 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3625 return NULL;
3627 if (got_fileindex_entry_has_blob(ie))
3628 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3629 if (got_fileindex_entry_has_commit(ie))
3630 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3631 if (staged_status == GOT_STATUS_ADD ||
3632 staged_status == GOT_STATUS_MODIFY) {
3633 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3634 &staged_blob_id, ie);
3637 return (*status_cb)(status_arg, status, staged_status,
3638 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3641 static const struct got_error *
3642 status_old_new(void *arg, struct got_fileindex_entry *ie,
3643 struct dirent *de, const char *parent_path, int dirfd)
3645 const struct got_error *err = NULL;
3646 struct diff_dir_cb_arg *a = arg;
3647 char *abspath;
3649 if (a->cancel_cb) {
3650 err = a->cancel_cb(a->cancel_arg);
3651 if (err)
3652 return err;
3655 if (got_path_cmp(parent_path, a->status_path,
3656 strlen(parent_path), a->status_path_len) != 0 &&
3657 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3658 return NULL;
3660 if (parent_path[0]) {
3661 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3662 parent_path, de->d_name) == -1)
3663 return got_error_from_errno("asprintf");
3664 } else {
3665 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3666 de->d_name) == -1)
3667 return got_error_from_errno("asprintf");
3670 err = report_file_status(ie, abspath, dirfd, de->d_name,
3671 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3672 free(abspath);
3673 return err;
3676 static const struct got_error *
3677 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3679 const struct got_error *err;
3680 struct diff_dir_cb_arg *a = arg;
3681 struct got_object_id blob_id, commit_id;
3682 unsigned char status;
3684 if (a->cancel_cb) {
3685 err = a->cancel_cb(a->cancel_arg);
3686 if (err)
3687 return err;
3690 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3691 return NULL;
3693 got_fileindex_entry_get_blob_id(&blob_id, ie);
3694 got_fileindex_entry_get_commit_id(&commit_id, ie);
3695 if (got_fileindex_entry_has_file_on_disk(ie))
3696 status = GOT_STATUS_MISSING;
3697 else
3698 status = GOT_STATUS_DELETE;
3699 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3700 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3703 static void
3704 free_ignores(struct got_pathlist_head *ignores)
3706 struct got_pathlist_entry *pe;
3708 TAILQ_FOREACH(pe, ignores, entry) {
3709 struct got_pathlist_head *ignorelist = pe->data;
3711 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3713 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3716 static const struct got_error *
3717 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3719 const struct got_error *err = NULL;
3720 struct got_pathlist_entry *pe = NULL;
3721 struct got_pathlist_head *ignorelist;
3722 char *line = NULL, *pattern, *dirpath = NULL;
3723 size_t linesize = 0;
3724 ssize_t linelen;
3726 ignorelist = calloc(1, sizeof(*ignorelist));
3727 if (ignorelist == NULL)
3728 return got_error_from_errno("calloc");
3729 TAILQ_INIT(ignorelist);
3731 while ((linelen = getline(&line, &linesize, f)) != -1) {
3732 if (linelen > 0 && line[linelen - 1] == '\n')
3733 line[linelen - 1] = '\0';
3735 /* Git's ignores may contain comments. */
3736 if (line[0] == '#')
3737 continue;
3739 /* Git's negated patterns are not (yet?) supported. */
3740 if (line[0] == '!')
3741 continue;
3743 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3744 line) == -1) {
3745 err = got_error_from_errno("asprintf");
3746 goto done;
3748 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3749 if (err)
3750 goto done;
3752 if (ferror(f)) {
3753 err = got_error_from_errno("getline");
3754 goto done;
3757 dirpath = strdup(path);
3758 if (dirpath == NULL) {
3759 err = got_error_from_errno("strdup");
3760 goto done;
3762 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3763 done:
3764 free(line);
3765 if (err || pe == NULL) {
3766 free(dirpath);
3767 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3769 return err;
3772 static int
3773 match_path(const char *pattern, size_t pattern_len, const char *path,
3774 int flags)
3776 char buf[PATH_MAX];
3779 * Trailing slashes signify directories.
3780 * Append a * to make such patterns conform to fnmatch rules.
3782 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3783 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3784 return FNM_NOMATCH; /* XXX */
3786 return fnmatch(buf, path, flags);
3789 return fnmatch(pattern, path, flags);
3792 static int
3793 match_ignores(struct got_pathlist_head *ignores, const char *path)
3795 struct got_pathlist_entry *pe;
3797 /* Handle patterns which match in all directories. */
3798 TAILQ_FOREACH(pe, ignores, entry) {
3799 struct got_pathlist_head *ignorelist = pe->data;
3800 struct got_pathlist_entry *pi;
3802 TAILQ_FOREACH(pi, ignorelist, entry) {
3803 const char *p;
3805 if (pi->path_len < 3 ||
3806 strncmp(pi->path, "**/", 3) != 0)
3807 continue;
3808 p = path;
3809 while (*p) {
3810 if (match_path(pi->path + 3,
3811 pi->path_len - 3, p,
3812 FNM_PATHNAME | FNM_LEADING_DIR)) {
3813 /* Retry in next directory. */
3814 while (*p && *p != '/')
3815 p++;
3816 while (*p == '/')
3817 p++;
3818 continue;
3820 return 1;
3826 * The ignores pathlist contains ignore lists from children before
3827 * parents, so we can find the most specific ignorelist by walking
3828 * ignores backwards.
3830 pe = TAILQ_LAST(ignores, got_pathlist_head);
3831 while (pe) {
3832 if (got_path_is_child(path, pe->path, pe->path_len)) {
3833 struct got_pathlist_head *ignorelist = pe->data;
3834 struct got_pathlist_entry *pi;
3835 TAILQ_FOREACH(pi, ignorelist, entry) {
3836 int flags = FNM_LEADING_DIR;
3837 if (strstr(pi->path, "/**/") == NULL)
3838 flags |= FNM_PATHNAME;
3839 if (match_path(pi->path, pi->path_len,
3840 path, flags))
3841 continue;
3842 return 1;
3845 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3848 return 0;
3851 static const struct got_error *
3852 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3853 const char *path, int dirfd, const char *ignores_filename)
3855 const struct got_error *err = NULL;
3856 char *ignorespath;
3857 int fd = -1;
3858 FILE *ignoresfile = NULL;
3860 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3861 path[0] ? "/" : "", ignores_filename) == -1)
3862 return got_error_from_errno("asprintf");
3864 if (dirfd != -1) {
3865 fd = openat(dirfd, ignores_filename,
3866 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3867 if (fd == -1) {
3868 if (errno != ENOENT && errno != EACCES)
3869 err = got_error_from_errno2("openat",
3870 ignorespath);
3871 } else {
3872 ignoresfile = fdopen(fd, "r");
3873 if (ignoresfile == NULL)
3874 err = got_error_from_errno2("fdopen",
3875 ignorespath);
3876 else {
3877 fd = -1;
3878 err = read_ignores(ignores, path, ignoresfile);
3881 } else {
3882 ignoresfile = fopen(ignorespath, "re");
3883 if (ignoresfile == NULL) {
3884 if (errno != ENOENT && errno != EACCES)
3885 err = got_error_from_errno2("fopen",
3886 ignorespath);
3887 } else
3888 err = read_ignores(ignores, path, ignoresfile);
3891 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3892 err = got_error_from_errno2("fclose", path);
3893 if (fd != -1 && close(fd) == -1 && err == NULL)
3894 err = got_error_from_errno2("close", path);
3895 free(ignorespath);
3896 return err;
3899 static const struct got_error *
3900 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3901 int dirfd)
3903 const struct got_error *err = NULL;
3904 struct diff_dir_cb_arg *a = arg;
3905 char *path = NULL;
3907 if (ignore != NULL)
3908 *ignore = 0;
3910 if (a->cancel_cb) {
3911 err = a->cancel_cb(a->cancel_arg);
3912 if (err)
3913 return err;
3916 if (parent_path[0]) {
3917 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3918 return got_error_from_errno("asprintf");
3919 } else {
3920 path = de->d_name;
3923 if (de->d_type == DT_DIR) {
3924 if (!a->no_ignores && ignore != NULL &&
3925 match_ignores(a->ignores, path))
3926 *ignore = 1;
3927 } else if (!match_ignores(a->ignores, path) &&
3928 got_path_is_child(path, a->status_path, a->status_path_len))
3929 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3930 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3931 if (parent_path[0])
3932 free(path);
3933 return err;
3936 static const struct got_error *
3937 status_traverse(void *arg, const char *path, int dirfd)
3939 const struct got_error *err = NULL;
3940 struct diff_dir_cb_arg *a = arg;
3942 if (a->no_ignores)
3943 return NULL;
3945 err = add_ignores(a->ignores, a->worktree->root_path,
3946 path, dirfd, ".cvsignore");
3947 if (err)
3948 return err;
3950 err = add_ignores(a->ignores, a->worktree->root_path, path,
3951 dirfd, ".gitignore");
3953 return err;
3956 static const struct got_error *
3957 report_single_file_status(const char *path, const char *ondisk_path,
3958 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3959 void *status_arg, struct got_repository *repo, int report_unchanged,
3960 struct got_pathlist_head *ignores, int no_ignores)
3962 struct got_fileindex_entry *ie;
3963 struct stat sb;
3965 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3966 if (ie)
3967 return report_file_status(ie, ondisk_path, -1, NULL,
3968 status_cb, status_arg, repo, report_unchanged);
3970 if (lstat(ondisk_path, &sb) == -1) {
3971 if (errno != ENOENT)
3972 return got_error_from_errno2("lstat", ondisk_path);
3973 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3974 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3977 if (!no_ignores && match_ignores(ignores, path))
3978 return NULL;
3980 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3981 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3982 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3984 return NULL;
3987 static const struct got_error *
3988 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3989 const char *root_path, const char *path)
3991 const struct got_error *err;
3992 char *parent_path, *next_parent_path = NULL;
3994 err = add_ignores(ignores, root_path, "", -1,
3995 ".cvsignore");
3996 if (err)
3997 return err;
3999 err = add_ignores(ignores, root_path, "", -1,
4000 ".gitignore");
4001 if (err)
4002 return err;
4004 err = got_path_dirname(&parent_path, path);
4005 if (err) {
4006 if (err->code == GOT_ERR_BAD_PATH)
4007 return NULL; /* cannot traverse parent */
4008 return err;
4010 for (;;) {
4011 err = add_ignores(ignores, root_path, parent_path, -1,
4012 ".cvsignore");
4013 if (err)
4014 break;
4015 err = add_ignores(ignores, root_path, parent_path, -1,
4016 ".gitignore");
4017 if (err)
4018 break;
4019 err = got_path_dirname(&next_parent_path, parent_path);
4020 if (err) {
4021 if (err->code == GOT_ERR_BAD_PATH)
4022 err = NULL; /* traversed everything */
4023 break;
4025 if (got_path_is_root_dir(parent_path))
4026 break;
4027 free(parent_path);
4028 parent_path = next_parent_path;
4029 next_parent_path = NULL;
4032 free(parent_path);
4033 free(next_parent_path);
4034 return err;
4037 struct find_missing_children_args {
4038 const char *parent_path;
4039 size_t parent_len;
4040 struct got_pathlist_head *children;
4041 got_cancel_cb cancel_cb;
4042 void *cancel_arg;
4045 static const struct got_error *
4046 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4048 const struct got_error *err = NULL;
4049 struct find_missing_children_args *a = arg;
4051 if (a->cancel_cb) {
4052 err = a->cancel_cb(a->cancel_arg);
4053 if (err)
4054 return err;
4057 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4058 err = got_pathlist_append(a->children, ie->path, NULL);
4060 return err;
4063 static const struct got_error *
4064 report_children(struct got_pathlist_head *children,
4065 struct got_worktree *worktree, struct got_fileindex *fileindex,
4066 struct got_repository *repo, int is_root_dir, int report_unchanged,
4067 struct got_pathlist_head *ignores, int no_ignores,
4068 got_worktree_status_cb status_cb, void *status_arg,
4069 got_cancel_cb cancel_cb, void *cancel_arg)
4071 const struct got_error *err = NULL;
4072 struct got_pathlist_entry *pe;
4073 char *ondisk_path = NULL;
4075 TAILQ_FOREACH(pe, children, entry) {
4076 if (cancel_cb) {
4077 err = cancel_cb(cancel_arg);
4078 if (err)
4079 break;
4082 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4083 !is_root_dir ? "/" : "", pe->path) == -1) {
4084 err = got_error_from_errno("asprintf");
4085 ondisk_path = NULL;
4086 break;
4089 err = report_single_file_status(pe->path, ondisk_path,
4090 fileindex, status_cb, status_arg, repo, report_unchanged,
4091 ignores, no_ignores);
4092 if (err)
4093 break;
4095 free(ondisk_path);
4096 ondisk_path = NULL;
4099 free(ondisk_path);
4100 return err;
4103 static const struct got_error *
4104 worktree_status(struct got_worktree *worktree, const char *path,
4105 struct got_fileindex *fileindex, struct got_repository *repo,
4106 got_worktree_status_cb status_cb, void *status_arg,
4107 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4108 int report_unchanged)
4110 const struct got_error *err = NULL;
4111 int fd = -1;
4112 struct got_fileindex_diff_dir_cb fdiff_cb;
4113 struct diff_dir_cb_arg arg;
4114 char *ondisk_path = NULL;
4115 struct got_pathlist_head ignores, missing_children;
4116 struct got_fileindex_entry *ie;
4118 TAILQ_INIT(&ignores);
4119 TAILQ_INIT(&missing_children);
4121 if (asprintf(&ondisk_path, "%s%s%s",
4122 worktree->root_path, path[0] ? "/" : "", path) == -1)
4123 return got_error_from_errno("asprintf");
4125 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4126 if (ie) {
4127 err = report_single_file_status(path, ondisk_path,
4128 fileindex, status_cb, status_arg, repo,
4129 report_unchanged, &ignores, no_ignores);
4130 goto done;
4131 } else {
4132 struct find_missing_children_args fmca;
4133 fmca.parent_path = path;
4134 fmca.parent_len = strlen(path);
4135 fmca.children = &missing_children;
4136 fmca.cancel_cb = cancel_cb;
4137 fmca.cancel_arg = cancel_arg;
4138 err = got_fileindex_for_each_entry_safe(fileindex,
4139 find_missing_children, &fmca);
4140 if (err)
4141 goto done;
4144 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4145 if (fd == -1) {
4146 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4147 !got_err_open_nofollow_on_symlink())
4148 err = got_error_from_errno2("open", ondisk_path);
4149 else {
4150 if (!no_ignores) {
4151 err = add_ignores_from_parent_paths(&ignores,
4152 worktree->root_path, ondisk_path);
4153 if (err)
4154 goto done;
4156 if (TAILQ_EMPTY(&missing_children)) {
4157 err = report_single_file_status(path,
4158 ondisk_path, fileindex,
4159 status_cb, status_arg, repo,
4160 report_unchanged, &ignores, no_ignores);
4161 if (err)
4162 goto done;
4163 } else {
4164 err = report_children(&missing_children,
4165 worktree, fileindex, repo,
4166 (path[0] == '\0'), report_unchanged,
4167 &ignores, no_ignores,
4168 status_cb, status_arg,
4169 cancel_cb, cancel_arg);
4170 if (err)
4171 goto done;
4174 } else {
4175 fdiff_cb.diff_old_new = status_old_new;
4176 fdiff_cb.diff_old = status_old;
4177 fdiff_cb.diff_new = status_new;
4178 fdiff_cb.diff_traverse = status_traverse;
4179 arg.fileindex = fileindex;
4180 arg.worktree = worktree;
4181 arg.status_path = path;
4182 arg.status_path_len = strlen(path);
4183 arg.repo = repo;
4184 arg.status_cb = status_cb;
4185 arg.status_arg = status_arg;
4186 arg.cancel_cb = cancel_cb;
4187 arg.cancel_arg = cancel_arg;
4188 arg.report_unchanged = report_unchanged;
4189 arg.no_ignores = no_ignores;
4190 if (!no_ignores) {
4191 err = add_ignores_from_parent_paths(&ignores,
4192 worktree->root_path, path);
4193 if (err)
4194 goto done;
4196 arg.ignores = &ignores;
4197 err = got_fileindex_diff_dir(fileindex, fd,
4198 worktree->root_path, path, repo, &fdiff_cb, &arg);
4200 done:
4201 free_ignores(&ignores);
4202 if (fd != -1 && close(fd) == -1 && err == NULL)
4203 err = got_error_from_errno("close");
4204 free(ondisk_path);
4205 return err;
4208 const struct got_error *
4209 got_worktree_status(struct got_worktree *worktree,
4210 struct got_pathlist_head *paths, struct got_repository *repo,
4211 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4212 got_cancel_cb cancel_cb, void *cancel_arg)
4214 const struct got_error *err = NULL;
4215 char *fileindex_path = NULL;
4216 struct got_fileindex *fileindex = NULL;
4217 struct got_pathlist_entry *pe;
4219 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4220 if (err)
4221 return err;
4223 TAILQ_FOREACH(pe, paths, entry) {
4224 err = worktree_status(worktree, pe->path, fileindex, repo,
4225 status_cb, status_arg, cancel_cb, cancel_arg,
4226 no_ignores, 0);
4227 if (err)
4228 break;
4230 free(fileindex_path);
4231 got_fileindex_free(fileindex);
4232 return err;
4235 const struct got_error *
4236 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4237 const char *arg)
4239 const struct got_error *err = NULL;
4240 char *resolved = NULL, *cwd = NULL, *path = NULL;
4241 size_t len;
4242 struct stat sb;
4243 char *abspath = NULL;
4244 char canonpath[PATH_MAX];
4246 *wt_path = NULL;
4248 cwd = getcwd(NULL, 0);
4249 if (cwd == NULL)
4250 return got_error_from_errno("getcwd");
4252 if (lstat(arg, &sb) == -1) {
4253 if (errno != ENOENT) {
4254 err = got_error_from_errno2("lstat", arg);
4255 goto done;
4257 sb.st_mode = 0;
4259 if (S_ISLNK(sb.st_mode)) {
4261 * We cannot use realpath(3) with symlinks since we want to
4262 * operate on the symlink itself.
4263 * But we can make the path absolute, assuming it is relative
4264 * to the current working directory, and then canonicalize it.
4266 if (!got_path_is_absolute(arg)) {
4267 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4268 err = got_error_from_errno("asprintf");
4269 goto done;
4273 err = got_canonpath(abspath ? abspath : arg, canonpath,
4274 sizeof(canonpath));
4275 if (err)
4276 goto done;
4277 resolved = strdup(canonpath);
4278 if (resolved == NULL) {
4279 err = got_error_from_errno("strdup");
4280 goto done;
4282 } else {
4283 resolved = realpath(arg, NULL);
4284 if (resolved == NULL) {
4285 if (errno != ENOENT) {
4286 err = got_error_from_errno2("realpath", arg);
4287 goto done;
4289 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4290 err = got_error_from_errno("asprintf");
4291 goto done;
4293 err = got_canonpath(abspath, canonpath,
4294 sizeof(canonpath));
4295 if (err)
4296 goto done;
4297 resolved = strdup(canonpath);
4298 if (resolved == NULL) {
4299 err = got_error_from_errno("strdup");
4300 goto done;
4305 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4306 strlen(got_worktree_get_root_path(worktree)))) {
4307 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4308 goto done;
4311 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4312 err = got_path_skip_common_ancestor(&path,
4313 got_worktree_get_root_path(worktree), resolved);
4314 if (err)
4315 goto done;
4316 } else {
4317 path = strdup("");
4318 if (path == NULL) {
4319 err = got_error_from_errno("strdup");
4320 goto done;
4324 /* XXX status walk can't deal with trailing slash! */
4325 len = strlen(path);
4326 while (len > 0 && path[len - 1] == '/') {
4327 path[len - 1] = '\0';
4328 len--;
4330 done:
4331 free(abspath);
4332 free(resolved);
4333 free(cwd);
4334 if (err == NULL)
4335 *wt_path = path;
4336 else
4337 free(path);
4338 return err;
4341 struct schedule_addition_args {
4342 struct got_worktree *worktree;
4343 struct got_fileindex *fileindex;
4344 got_worktree_checkout_cb progress_cb;
4345 void *progress_arg;
4346 struct got_repository *repo;
4349 static int
4350 add_noop_status(unsigned char status)
4352 return (status == GOT_STATUS_ADD ||
4353 status == GOT_STATUS_MODIFY ||
4354 status == GOT_STATUS_CONFLICT ||
4355 status == GOT_STATUS_MODE_CHANGE ||
4356 status == GOT_STATUS_NO_CHANGE);
4359 static const struct got_error *
4360 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4361 const char *relpath, struct got_object_id *blob_id,
4362 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4363 int dirfd, const char *de_name)
4365 struct schedule_addition_args *a = arg;
4366 const struct got_error *err = NULL;
4367 struct got_fileindex_entry *ie;
4368 struct stat sb;
4369 char *ondisk_path;
4371 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4372 relpath) == -1)
4373 return got_error_from_errno("asprintf");
4375 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4376 if (ie) {
4377 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4378 de_name, a->repo);
4379 if (err)
4380 goto done;
4381 /* Re-adding an existing entry is a no-op. */
4382 if (staged_status == GOT_STATUS_NO_CHANGE &&
4383 add_noop_status(status))
4384 goto done;
4385 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4386 if (err)
4387 goto done;
4390 if (status != GOT_STATUS_UNVERSIONED) {
4391 if (status == GOT_STATUS_NONEXISTENT)
4392 err = got_error_set_errno(ENOENT, ondisk_path);
4393 else
4394 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4395 goto done;
4398 err = got_fileindex_entry_alloc(&ie, relpath);
4399 if (err)
4400 goto done;
4401 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4402 relpath, NULL, NULL, 1);
4403 if (err) {
4404 got_fileindex_entry_free(ie);
4405 goto done;
4407 err = got_fileindex_entry_add(a->fileindex, ie);
4408 if (err) {
4409 got_fileindex_entry_free(ie);
4410 goto done;
4412 done:
4413 free(ondisk_path);
4414 if (err)
4415 return err;
4416 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4417 return NULL;
4418 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4421 const struct got_error *
4422 got_worktree_schedule_add(struct got_worktree *worktree,
4423 struct got_pathlist_head *paths,
4424 got_worktree_checkout_cb progress_cb, void *progress_arg,
4425 struct got_repository *repo, int no_ignores)
4427 struct got_fileindex *fileindex = NULL;
4428 char *fileindex_path = NULL;
4429 const struct got_error *err = NULL, *sync_err, *unlockerr;
4430 struct got_pathlist_entry *pe;
4431 struct schedule_addition_args saa;
4433 err = lock_worktree(worktree, LOCK_EX);
4434 if (err)
4435 return err;
4437 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4438 if (err)
4439 goto done;
4441 saa.worktree = worktree;
4442 saa.fileindex = fileindex;
4443 saa.progress_cb = progress_cb;
4444 saa.progress_arg = progress_arg;
4445 saa.repo = repo;
4447 TAILQ_FOREACH(pe, paths, entry) {
4448 err = worktree_status(worktree, pe->path, fileindex, repo,
4449 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4450 if (err)
4451 break;
4453 sync_err = sync_fileindex(fileindex, fileindex_path);
4454 if (sync_err && err == NULL)
4455 err = sync_err;
4456 done:
4457 free(fileindex_path);
4458 if (fileindex)
4459 got_fileindex_free(fileindex);
4460 unlockerr = lock_worktree(worktree, LOCK_SH);
4461 if (unlockerr && err == NULL)
4462 err = unlockerr;
4463 return err;
4466 struct schedule_deletion_args {
4467 struct got_worktree *worktree;
4468 struct got_fileindex *fileindex;
4469 got_worktree_delete_cb progress_cb;
4470 void *progress_arg;
4471 struct got_repository *repo;
4472 int delete_local_mods;
4473 int keep_on_disk;
4474 int ignore_missing_paths;
4475 const char *status_path;
4476 size_t status_path_len;
4477 const char *status_codes;
4480 static const struct got_error *
4481 schedule_for_deletion(void *arg, unsigned char status,
4482 unsigned char staged_status, const char *relpath,
4483 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4484 struct got_object_id *commit_id, int dirfd, const char *de_name)
4486 struct schedule_deletion_args *a = arg;
4487 const struct got_error *err = NULL;
4488 struct got_fileindex_entry *ie = NULL;
4489 struct stat sb;
4490 char *ondisk_path;
4492 if (status == GOT_STATUS_NONEXISTENT) {
4493 if (a->ignore_missing_paths)
4494 return NULL;
4495 return got_error_set_errno(ENOENT, relpath);
4498 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4499 if (ie == NULL)
4500 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4502 staged_status = get_staged_status(ie);
4503 if (staged_status != GOT_STATUS_NO_CHANGE) {
4504 if (staged_status == GOT_STATUS_DELETE)
4505 return NULL;
4506 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4509 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4510 relpath) == -1)
4511 return got_error_from_errno("asprintf");
4513 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4514 a->repo);
4515 if (err)
4516 goto done;
4518 if (a->status_codes) {
4519 size_t ncodes = strlen(a->status_codes);
4520 int i;
4521 for (i = 0; i < ncodes ; i++) {
4522 if (status == a->status_codes[i])
4523 break;
4525 if (i == ncodes) {
4526 /* Do not delete files in non-matching status. */
4527 free(ondisk_path);
4528 return NULL;
4530 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4531 a->status_codes[i] != GOT_STATUS_MISSING) {
4532 static char msg[64];
4533 snprintf(msg, sizeof(msg),
4534 "invalid status code '%c'", a->status_codes[i]);
4535 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4536 goto done;
4540 if (status != GOT_STATUS_NO_CHANGE) {
4541 if (status == GOT_STATUS_DELETE)
4542 goto done;
4543 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4544 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4545 goto done;
4547 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4548 err = got_error_set_errno(ENOENT, relpath);
4549 goto done;
4551 if (status != GOT_STATUS_MODIFY &&
4552 status != GOT_STATUS_MISSING) {
4553 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4554 goto done;
4558 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4559 size_t root_len;
4561 if (dirfd != -1) {
4562 if (unlinkat(dirfd, de_name, 0) == -1) {
4563 err = got_error_from_errno2("unlinkat",
4564 ondisk_path);
4565 goto done;
4567 } else if (unlink(ondisk_path) == -1) {
4568 err = got_error_from_errno2("unlink", ondisk_path);
4569 goto done;
4572 root_len = strlen(a->worktree->root_path);
4573 do {
4574 char *parent;
4576 err = got_path_dirname(&parent, ondisk_path);
4577 if (err)
4578 goto done;
4579 free(ondisk_path);
4580 ondisk_path = parent;
4581 if (got_path_cmp(ondisk_path, a->status_path,
4582 strlen(ondisk_path), a->status_path_len) != 0 &&
4583 !got_path_is_child(ondisk_path, a->status_path,
4584 a->status_path_len))
4585 break;
4586 if (rmdir(ondisk_path) == -1) {
4587 if (errno != ENOTEMPTY)
4588 err = got_error_from_errno2("rmdir",
4589 ondisk_path);
4590 break;
4592 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4593 strlen(ondisk_path), root_len) != 0);
4596 got_fileindex_entry_mark_deleted_from_disk(ie);
4597 done:
4598 free(ondisk_path);
4599 if (err)
4600 return err;
4601 if (status == GOT_STATUS_DELETE)
4602 return NULL;
4603 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4604 staged_status, relpath);
4607 const struct got_error *
4608 got_worktree_schedule_delete(struct got_worktree *worktree,
4609 struct got_pathlist_head *paths, int delete_local_mods,
4610 const char *status_codes,
4611 got_worktree_delete_cb progress_cb, void *progress_arg,
4612 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4614 struct got_fileindex *fileindex = NULL;
4615 char *fileindex_path = NULL;
4616 const struct got_error *err = NULL, *sync_err, *unlockerr;
4617 struct got_pathlist_entry *pe;
4618 struct schedule_deletion_args sda;
4620 err = lock_worktree(worktree, LOCK_EX);
4621 if (err)
4622 return err;
4624 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4625 if (err)
4626 goto done;
4628 sda.worktree = worktree;
4629 sda.fileindex = fileindex;
4630 sda.progress_cb = progress_cb;
4631 sda.progress_arg = progress_arg;
4632 sda.repo = repo;
4633 sda.delete_local_mods = delete_local_mods;
4634 sda.keep_on_disk = keep_on_disk;
4635 sda.ignore_missing_paths = ignore_missing_paths;
4636 sda.status_codes = status_codes;
4638 TAILQ_FOREACH(pe, paths, entry) {
4639 char *ondisk_status_path;
4641 if (asprintf(&ondisk_status_path, "%s%s%s",
4642 got_worktree_get_root_path(worktree),
4643 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4644 err = got_error_from_errno("asprintf");
4645 goto done;
4647 sda.status_path = ondisk_status_path;
4648 sda.status_path_len = strlen(ondisk_status_path);
4649 err = worktree_status(worktree, pe->path, fileindex, repo,
4650 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4651 free(ondisk_status_path);
4652 if (err)
4653 break;
4655 sync_err = sync_fileindex(fileindex, fileindex_path);
4656 if (sync_err && err == NULL)
4657 err = sync_err;
4658 done:
4659 free(fileindex_path);
4660 if (fileindex)
4661 got_fileindex_free(fileindex);
4662 unlockerr = lock_worktree(worktree, LOCK_SH);
4663 if (unlockerr && err == NULL)
4664 err = unlockerr;
4665 return err;
4668 static const struct got_error *
4669 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4671 const struct got_error *err = NULL;
4672 char *line = NULL;
4673 size_t linesize = 0, n;
4674 ssize_t linelen;
4676 linelen = getline(&line, &linesize, infile);
4677 if (linelen == -1) {
4678 if (ferror(infile)) {
4679 err = got_error_from_errno("getline");
4680 goto done;
4682 return NULL;
4684 if (outfile) {
4685 n = fwrite(line, 1, linelen, outfile);
4686 if (n != linelen) {
4687 err = got_ferror(outfile, GOT_ERR_IO);
4688 goto done;
4691 if (rejectfile) {
4692 n = fwrite(line, 1, linelen, rejectfile);
4693 if (n != linelen)
4694 err = got_ferror(rejectfile, GOT_ERR_IO);
4696 done:
4697 free(line);
4698 return err;
4701 static const struct got_error *
4702 skip_one_line(FILE *f)
4704 char *line = NULL;
4705 size_t linesize = 0;
4706 ssize_t linelen;
4708 linelen = getline(&line, &linesize, f);
4709 if (linelen == -1) {
4710 if (ferror(f))
4711 return got_error_from_errno("getline");
4712 return NULL;
4714 free(line);
4715 return NULL;
4718 static const struct got_error *
4719 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4720 int start_old, int end_old, int start_new, int end_new,
4721 FILE *outfile, FILE *rejectfile)
4723 const struct got_error *err;
4725 /* Copy old file's lines leading up to patch. */
4726 while (!feof(f1) && *line_cur1 < start_old) {
4727 err = copy_one_line(f1, outfile, NULL);
4728 if (err)
4729 return err;
4730 (*line_cur1)++;
4732 /* Skip new file's lines leading up to patch. */
4733 while (!feof(f2) && *line_cur2 < start_new) {
4734 if (rejectfile)
4735 err = copy_one_line(f2, NULL, rejectfile);
4736 else
4737 err = skip_one_line(f2);
4738 if (err)
4739 return err;
4740 (*line_cur2)++;
4742 /* Copy patched lines. */
4743 while (!feof(f2) && *line_cur2 <= end_new) {
4744 err = copy_one_line(f2, outfile, NULL);
4745 if (err)
4746 return err;
4747 (*line_cur2)++;
4749 /* Skip over old file's replaced lines. */
4750 while (!feof(f1) && *line_cur1 <= end_old) {
4751 if (rejectfile)
4752 err = copy_one_line(f1, NULL, rejectfile);
4753 else
4754 err = skip_one_line(f1);
4755 if (err)
4756 return err;
4757 (*line_cur1)++;
4760 return NULL;
4763 static const struct got_error *
4764 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4765 FILE *outfile, FILE *rejectfile)
4767 const struct got_error *err;
4769 if (outfile) {
4770 /* Copy old file's lines until EOF. */
4771 while (!feof(f1)) {
4772 err = copy_one_line(f1, outfile, NULL);
4773 if (err)
4774 return err;
4775 (*line_cur1)++;
4778 if (rejectfile) {
4779 /* Copy new file's lines until EOF. */
4780 while (!feof(f2)) {
4781 err = copy_one_line(f2, NULL, rejectfile);
4782 if (err)
4783 return err;
4784 (*line_cur2)++;
4788 return NULL;
4791 static const struct got_error *
4792 apply_or_reject_change(int *choice, int *nchunks_used,
4793 struct diff_result *diff_result, int n,
4794 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4795 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4796 got_worktree_patch_cb patch_cb, void *patch_arg)
4798 const struct got_error *err = NULL;
4799 struct diff_chunk_context cc = {};
4800 int start_old, end_old, start_new, end_new;
4801 FILE *hunkfile;
4802 struct diff_output_unidiff_state *diff_state;
4803 struct diff_input_info diff_info;
4804 int rc;
4806 *choice = GOT_PATCH_CHOICE_NONE;
4808 /* Get changed line numbers without context lines for copy_change(). */
4809 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4810 start_old = cc.left.start;
4811 end_old = cc.left.end;
4812 start_new = cc.right.start;
4813 end_new = cc.right.end;
4815 /* Get the same change with context lines for display. */
4816 memset(&cc, 0, sizeof(cc));
4817 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4819 memset(&diff_info, 0, sizeof(diff_info));
4820 diff_info.left_path = relpath;
4821 diff_info.right_path = relpath;
4823 diff_state = diff_output_unidiff_state_alloc();
4824 if (diff_state == NULL)
4825 return got_error_set_errno(ENOMEM,
4826 "diff_output_unidiff_state_alloc");
4828 hunkfile = got_opentemp();
4829 if (hunkfile == NULL) {
4830 err = got_error_from_errno("got_opentemp");
4831 goto done;
4834 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4835 diff_result, &cc);
4836 if (rc != DIFF_RC_OK) {
4837 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4838 goto done;
4841 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4842 err = got_ferror(hunkfile, GOT_ERR_IO);
4843 goto done;
4846 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4847 hunkfile, changeno, nchanges);
4848 if (err)
4849 goto done;
4851 switch (*choice) {
4852 case GOT_PATCH_CHOICE_YES:
4853 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4854 end_old, start_new, end_new, outfile, rejectfile);
4855 break;
4856 case GOT_PATCH_CHOICE_NO:
4857 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4858 end_old, start_new, end_new, rejectfile, outfile);
4859 break;
4860 case GOT_PATCH_CHOICE_QUIT:
4861 break;
4862 default:
4863 err = got_error(GOT_ERR_PATCH_CHOICE);
4864 break;
4866 done:
4867 diff_output_unidiff_state_free(diff_state);
4868 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4869 err = got_error_from_errno("fclose");
4870 return err;
4873 struct revert_file_args {
4874 struct got_worktree *worktree;
4875 struct got_fileindex *fileindex;
4876 got_worktree_checkout_cb progress_cb;
4877 void *progress_arg;
4878 got_worktree_patch_cb patch_cb;
4879 void *patch_arg;
4880 struct got_repository *repo;
4881 int unlink_added_files;
4882 struct got_pathlist_head *added_files_to_unlink;
4885 static const struct got_error *
4886 create_patched_content(char **path_outfile, int reverse_patch,
4887 struct got_object_id *blob_id, const char *path2,
4888 int dirfd2, const char *de_name2,
4889 const char *relpath, struct got_repository *repo,
4890 got_worktree_patch_cb patch_cb, void *patch_arg)
4892 const struct got_error *err, *free_err;
4893 struct got_blob_object *blob = NULL;
4894 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4895 int fd = -1, fd2 = -1;
4896 char link_target[PATH_MAX];
4897 ssize_t link_len = 0;
4898 char *path1 = NULL, *id_str = NULL;
4899 struct stat sb2;
4900 struct got_diffreg_result *diffreg_result = NULL;
4901 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4902 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4904 *path_outfile = NULL;
4906 err = got_object_id_str(&id_str, blob_id);
4907 if (err)
4908 return err;
4910 if (dirfd2 != -1) {
4911 fd2 = openat(dirfd2, de_name2,
4912 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4913 if (fd2 == -1) {
4914 if (!got_err_open_nofollow_on_symlink()) {
4915 err = got_error_from_errno2("openat", path2);
4916 goto done;
4918 link_len = readlinkat(dirfd2, de_name2,
4919 link_target, sizeof(link_target));
4920 if (link_len == -1) {
4921 return got_error_from_errno2("readlinkat",
4922 path2);
4924 sb2.st_mode = S_IFLNK;
4925 sb2.st_size = link_len;
4927 } else {
4928 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4929 if (fd2 == -1) {
4930 if (!got_err_open_nofollow_on_symlink()) {
4931 err = got_error_from_errno2("open", path2);
4932 goto done;
4934 link_len = readlink(path2, link_target,
4935 sizeof(link_target));
4936 if (link_len == -1)
4937 return got_error_from_errno2("readlink", path2);
4938 sb2.st_mode = S_IFLNK;
4939 sb2.st_size = link_len;
4942 if (fd2 != -1) {
4943 if (fstat(fd2, &sb2) == -1) {
4944 err = got_error_from_errno2("fstat", path2);
4945 goto done;
4948 f2 = fdopen(fd2, "r");
4949 if (f2 == NULL) {
4950 err = got_error_from_errno2("fdopen", path2);
4951 goto done;
4953 fd2 = -1;
4954 } else {
4955 size_t n;
4956 f2 = got_opentemp();
4957 if (f2 == NULL) {
4958 err = got_error_from_errno2("got_opentemp", path2);
4959 goto done;
4961 n = fwrite(link_target, 1, link_len, f2);
4962 if (n != link_len) {
4963 err = got_ferror(f2, GOT_ERR_IO);
4964 goto done;
4966 if (fflush(f2) == EOF) {
4967 err = got_error_from_errno("fflush");
4968 goto done;
4970 rewind(f2);
4973 fd = got_opentempfd();
4974 if (fd == -1) {
4975 err = got_error_from_errno("got_opentempfd");
4976 goto done;
4979 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4980 if (err)
4981 goto done;
4983 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4984 if (err)
4985 goto done;
4987 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4988 if (err)
4989 goto done;
4991 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4992 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4993 if (err)
4994 goto done;
4996 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4997 "");
4998 if (err)
4999 goto done;
5001 if (fseek(f1, 0L, SEEK_SET) == -1)
5002 return got_ferror(f1, GOT_ERR_IO);
5003 if (fseek(f2, 0L, SEEK_SET) == -1)
5004 return got_ferror(f2, GOT_ERR_IO);
5006 /* Count the number of actual changes in the diff result. */
5007 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5008 struct diff_chunk_context cc = {};
5009 diff_chunk_context_load_change(&cc, &nchunks_used,
5010 diffreg_result->result, n, 0);
5011 nchanges++;
5013 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5014 int choice;
5015 err = apply_or_reject_change(&choice, &nchunks_used,
5016 diffreg_result->result, n, relpath, f1, f2,
5017 &line_cur1, &line_cur2,
5018 reverse_patch ? NULL : outfile,
5019 reverse_patch ? outfile : NULL,
5020 ++i, nchanges, patch_cb, patch_arg);
5021 if (err)
5022 goto done;
5023 if (choice == GOT_PATCH_CHOICE_YES)
5024 have_content = 1;
5025 else if (choice == GOT_PATCH_CHOICE_QUIT)
5026 break;
5028 if (have_content) {
5029 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5030 reverse_patch ? NULL : outfile,
5031 reverse_patch ? outfile : NULL);
5032 if (err)
5033 goto done;
5035 if (!S_ISLNK(sb2.st_mode)) {
5036 mode_t mode;
5038 mode = apply_umask(sb2.st_mode);
5039 if (fchmod(fileno(outfile), mode) == -1) {
5040 err = got_error_from_errno2("fchmod", path2);
5041 goto done;
5045 done:
5046 free(id_str);
5047 if (fd != -1 && close(fd) == -1 && err == NULL)
5048 err = got_error_from_errno("close");
5049 if (blob)
5050 got_object_blob_close(blob);
5051 free_err = got_diffreg_result_free(diffreg_result);
5052 if (err == NULL)
5053 err = free_err;
5054 if (f1 && fclose(f1) == EOF && err == NULL)
5055 err = got_error_from_errno2("fclose", path1);
5056 if (f2 && fclose(f2) == EOF && err == NULL)
5057 err = got_error_from_errno2("fclose", path2);
5058 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5059 err = got_error_from_errno2("close", path2);
5060 if (outfile && fclose(outfile) == EOF && err == NULL)
5061 err = got_error_from_errno2("fclose", *path_outfile);
5062 if (path1 && unlink(path1) == -1 && err == NULL)
5063 err = got_error_from_errno2("unlink", path1);
5064 if (err || !have_content) {
5065 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5066 err = got_error_from_errno2("unlink", *path_outfile);
5067 free(*path_outfile);
5068 *path_outfile = NULL;
5070 free(path1);
5071 return err;
5074 static const struct got_error *
5075 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5076 const char *relpath, struct got_object_id *blob_id,
5077 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5078 int dirfd, const char *de_name)
5080 struct revert_file_args *a = arg;
5081 const struct got_error *err = NULL;
5082 char *parent_path = NULL;
5083 struct got_fileindex_entry *ie;
5084 struct got_commit_object *base_commit = NULL;
5085 struct got_tree_object *tree = NULL;
5086 struct got_object_id *tree_id = NULL;
5087 const struct got_tree_entry *te = NULL;
5088 char *tree_path = NULL, *te_name;
5089 char *ondisk_path = NULL, *path_content = NULL;
5090 struct got_blob_object *blob = NULL;
5091 int fd = -1;
5093 /* Reverting a staged deletion is a no-op. */
5094 if (status == GOT_STATUS_DELETE &&
5095 staged_status != GOT_STATUS_NO_CHANGE)
5096 return NULL;
5098 if (status == GOT_STATUS_UNVERSIONED)
5099 return (*a->progress_cb)(a->progress_arg,
5100 GOT_STATUS_UNVERSIONED, relpath);
5102 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5103 if (ie == NULL)
5104 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5106 /* Construct in-repository path of tree which contains this blob. */
5107 err = got_path_dirname(&parent_path, ie->path);
5108 if (err) {
5109 if (err->code != GOT_ERR_BAD_PATH)
5110 goto done;
5111 parent_path = strdup("/");
5112 if (parent_path == NULL) {
5113 err = got_error_from_errno("strdup");
5114 goto done;
5117 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5118 tree_path = strdup(parent_path);
5119 if (tree_path == NULL) {
5120 err = got_error_from_errno("strdup");
5121 goto done;
5123 } else {
5124 if (got_path_is_root_dir(parent_path)) {
5125 tree_path = strdup(a->worktree->path_prefix);
5126 if (tree_path == NULL) {
5127 err = got_error_from_errno("strdup");
5128 goto done;
5130 } else {
5131 if (asprintf(&tree_path, "%s/%s",
5132 a->worktree->path_prefix, parent_path) == -1) {
5133 err = got_error_from_errno("asprintf");
5134 goto done;
5139 err = got_object_open_as_commit(&base_commit, a->repo,
5140 a->worktree->base_commit_id);
5141 if (err)
5142 goto done;
5144 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5145 if (err) {
5146 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5147 (status == GOT_STATUS_ADD ||
5148 staged_status == GOT_STATUS_ADD)))
5149 goto done;
5150 } else {
5151 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5152 if (err)
5153 goto done;
5155 err = got_path_basename(&te_name, ie->path);
5156 if (err)
5157 goto done;
5159 te = got_object_tree_find_entry(tree, te_name);
5160 free(te_name);
5161 if (te == NULL && status != GOT_STATUS_ADD &&
5162 staged_status != GOT_STATUS_ADD) {
5163 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5164 goto done;
5168 switch (status) {
5169 case GOT_STATUS_ADD:
5170 if (a->patch_cb) {
5171 int choice = GOT_PATCH_CHOICE_NONE;
5172 err = (*a->patch_cb)(&choice, a->patch_arg,
5173 status, ie->path, NULL, 1, 1);
5174 if (err)
5175 goto done;
5176 if (choice != GOT_PATCH_CHOICE_YES)
5177 break;
5179 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5180 ie->path);
5181 if (err)
5182 goto done;
5183 got_fileindex_entry_remove(a->fileindex, ie);
5184 if (a->unlink_added_files) {
5185 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5187 if (a->added_files_to_unlink) {
5188 struct got_pathlist_entry *pe;
5190 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5191 entry) {
5192 if (got_path_cmp(pe->path, relpath,
5193 pe->path_len, strlen(relpath)))
5194 continue;
5195 do_unlink = 1;
5196 break;
5200 if (do_unlink) {
5201 if (asprintf(&ondisk_path, "%s/%s",
5202 got_worktree_get_root_path(a->worktree),
5203 relpath) == -1) {
5204 err = got_error_from_errno("asprintf");
5205 goto done;
5207 if (unlink(ondisk_path) == -1) {
5208 err = got_error_from_errno2("unlink",
5209 ondisk_path);
5210 break;
5214 break;
5215 case GOT_STATUS_DELETE:
5216 if (a->patch_cb) {
5217 int choice = GOT_PATCH_CHOICE_NONE;
5218 err = (*a->patch_cb)(&choice, a->patch_arg,
5219 status, ie->path, NULL, 1, 1);
5220 if (err)
5221 goto done;
5222 if (choice != GOT_PATCH_CHOICE_YES)
5223 break;
5225 /* fall through */
5226 case GOT_STATUS_MODIFY:
5227 case GOT_STATUS_MODE_CHANGE:
5228 case GOT_STATUS_CONFLICT:
5229 case GOT_STATUS_MISSING: {
5230 struct got_object_id id;
5231 if (staged_status == GOT_STATUS_ADD ||
5232 staged_status == GOT_STATUS_MODIFY)
5233 got_fileindex_entry_get_staged_blob_id(&id, ie);
5234 else
5235 got_fileindex_entry_get_blob_id(&id, ie);
5236 fd = got_opentempfd();
5237 if (fd == -1) {
5238 err = got_error_from_errno("got_opentempfd");
5239 goto done;
5242 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5243 if (err)
5244 goto done;
5246 if (asprintf(&ondisk_path, "%s/%s",
5247 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5248 err = got_error_from_errno("asprintf");
5249 goto done;
5252 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5253 status == GOT_STATUS_CONFLICT)) {
5254 int is_bad_symlink = 0;
5255 err = create_patched_content(&path_content, 1, &id,
5256 ondisk_path, dirfd, de_name, ie->path, a->repo,
5257 a->patch_cb, a->patch_arg);
5258 if (err || path_content == NULL)
5259 break;
5260 if (te && S_ISLNK(te->mode)) {
5261 if (unlink(path_content) == -1) {
5262 err = got_error_from_errno2("unlink",
5263 path_content);
5264 break;
5266 err = install_symlink(&is_bad_symlink,
5267 a->worktree, ondisk_path, ie->path,
5268 blob, 0, 1, 0, 0, a->repo,
5269 a->progress_cb, a->progress_arg);
5270 } else {
5271 if (rename(path_content, ondisk_path) == -1) {
5272 err = got_error_from_errno3("rename",
5273 path_content, ondisk_path);
5274 goto done;
5277 } else {
5278 int is_bad_symlink = 0;
5279 if (te && S_ISLNK(te->mode)) {
5280 err = install_symlink(&is_bad_symlink,
5281 a->worktree, ondisk_path, ie->path,
5282 blob, 0, 1, 0, 0, a->repo,
5283 a->progress_cb, a->progress_arg);
5284 } else {
5285 err = install_blob(a->worktree, ondisk_path,
5286 ie->path,
5287 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5288 got_fileindex_perms_to_st(ie), blob,
5289 0, 1, 0, 0, a->repo,
5290 a->progress_cb, a->progress_arg);
5292 if (err)
5293 goto done;
5294 if (status == GOT_STATUS_DELETE ||
5295 status == GOT_STATUS_MODE_CHANGE) {
5296 err = got_fileindex_entry_update(ie,
5297 a->worktree->root_fd, relpath,
5298 blob->id.sha1,
5299 a->worktree->base_commit_id->sha1, 1);
5300 if (err)
5301 goto done;
5303 if (is_bad_symlink) {
5304 got_fileindex_entry_filetype_set(ie,
5305 GOT_FILEIDX_MODE_BAD_SYMLINK);
5308 break;
5310 default:
5311 break;
5313 done:
5314 free(ondisk_path);
5315 free(path_content);
5316 free(parent_path);
5317 free(tree_path);
5318 if (fd != -1 && close(fd) == -1 && err == NULL)
5319 err = got_error_from_errno("close");
5320 if (blob)
5321 got_object_blob_close(blob);
5322 if (tree)
5323 got_object_tree_close(tree);
5324 free(tree_id);
5325 if (base_commit)
5326 got_object_commit_close(base_commit);
5327 return err;
5330 const struct got_error *
5331 got_worktree_revert(struct got_worktree *worktree,
5332 struct got_pathlist_head *paths,
5333 got_worktree_checkout_cb progress_cb, void *progress_arg,
5334 got_worktree_patch_cb patch_cb, void *patch_arg,
5335 struct got_repository *repo)
5337 struct got_fileindex *fileindex = NULL;
5338 char *fileindex_path = NULL;
5339 const struct got_error *err = NULL, *unlockerr = NULL;
5340 const struct got_error *sync_err = NULL;
5341 struct got_pathlist_entry *pe;
5342 struct revert_file_args rfa;
5344 err = lock_worktree(worktree, LOCK_EX);
5345 if (err)
5346 return err;
5348 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5349 if (err)
5350 goto done;
5352 rfa.worktree = worktree;
5353 rfa.fileindex = fileindex;
5354 rfa.progress_cb = progress_cb;
5355 rfa.progress_arg = progress_arg;
5356 rfa.patch_cb = patch_cb;
5357 rfa.patch_arg = patch_arg;
5358 rfa.repo = repo;
5359 rfa.unlink_added_files = 0;
5360 TAILQ_FOREACH(pe, paths, entry) {
5361 err = worktree_status(worktree, pe->path, fileindex, repo,
5362 revert_file, &rfa, NULL, NULL, 1, 0);
5363 if (err)
5364 break;
5366 sync_err = sync_fileindex(fileindex, fileindex_path);
5367 if (sync_err && err == NULL)
5368 err = sync_err;
5369 done:
5370 free(fileindex_path);
5371 if (fileindex)
5372 got_fileindex_free(fileindex);
5373 unlockerr = lock_worktree(worktree, LOCK_SH);
5374 if (unlockerr && err == NULL)
5375 err = unlockerr;
5376 return err;
5379 static void
5380 free_commitable(struct got_commitable *ct)
5382 free(ct->path);
5383 free(ct->in_repo_path);
5384 free(ct->ondisk_path);
5385 free(ct->blob_id);
5386 free(ct->base_blob_id);
5387 free(ct->staged_blob_id);
5388 free(ct->base_commit_id);
5389 free(ct);
5392 struct collect_commitables_arg {
5393 struct got_pathlist_head *commitable_paths;
5394 struct got_repository *repo;
5395 struct got_worktree *worktree;
5396 struct got_fileindex *fileindex;
5397 int have_staged_files;
5398 int allow_bad_symlinks;
5399 int diff_header_shown;
5400 int commit_conflicts;
5401 FILE *diff_outfile;
5402 FILE *f1;
5403 FILE *f2;
5407 * Create a file which contains the target path of a symlink so we can feed
5408 * it as content to the diff engine.
5410 static const struct got_error *
5411 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5412 const char *abspath)
5414 const struct got_error *err = NULL;
5415 char target_path[PATH_MAX];
5416 ssize_t target_len, outlen;
5418 *fd = -1;
5420 if (dirfd != -1) {
5421 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5422 if (target_len == -1)
5423 return got_error_from_errno2("readlinkat", abspath);
5424 } else {
5425 target_len = readlink(abspath, target_path, PATH_MAX);
5426 if (target_len == -1)
5427 return got_error_from_errno2("readlink", abspath);
5430 *fd = got_opentempfd();
5431 if (*fd == -1)
5432 return got_error_from_errno("got_opentempfd");
5434 outlen = write(*fd, target_path, target_len);
5435 if (outlen == -1) {
5436 err = got_error_from_errno("got_opentempfd");
5437 goto done;
5440 if (lseek(*fd, 0, SEEK_SET) == -1) {
5441 err = got_error_from_errno2("lseek", abspath);
5442 goto done;
5444 done:
5445 if (err) {
5446 close(*fd);
5447 *fd = -1;
5449 return err;
5452 static const struct got_error *
5453 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5454 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5455 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5457 const struct got_error *err = NULL;
5458 struct got_blob_object *blob1 = NULL;
5459 int fd = -1, fd1 = -1, fd2 = -1;
5460 FILE *ondisk_file = NULL;
5461 char *label1 = NULL;
5462 struct stat sb;
5463 off_t size1 = 0;
5464 int f2_exists = 0;
5465 char *id_str = NULL;
5467 memset(&sb, 0, sizeof(sb));
5469 if (diff_staged) {
5470 if (ct->staged_status != GOT_STATUS_MODIFY &&
5471 ct->staged_status != GOT_STATUS_ADD &&
5472 ct->staged_status != GOT_STATUS_DELETE)
5473 return NULL;
5474 } else {
5475 if (ct->status != GOT_STATUS_MODIFY &&
5476 ct->status != GOT_STATUS_ADD &&
5477 ct->status != GOT_STATUS_DELETE &&
5478 ct->status != GOT_STATUS_CONFLICT)
5479 return NULL;
5482 err = got_opentemp_truncate(f1);
5483 if (err)
5484 return got_error_from_errno("got_opentemp_truncate");
5485 err = got_opentemp_truncate(f2);
5486 if (err)
5487 return got_error_from_errno("got_opentemp_truncate");
5489 if (!*diff_header_shown) {
5490 err = got_object_id_str(&id_str, worktree->base_commit_id);
5491 if (err)
5492 return err;
5493 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5494 got_worktree_get_root_path(worktree));
5495 fprintf(diff_outfile, "commit - %s\n", id_str);
5496 fprintf(diff_outfile, "path + %s%s\n",
5497 got_worktree_get_root_path(worktree),
5498 diff_staged ? " (staged changes)" : "");
5499 *diff_header_shown = 1;
5502 if (diff_staged) {
5503 const char *label1 = NULL, *label2 = NULL;
5504 switch (ct->staged_status) {
5505 case GOT_STATUS_MODIFY:
5506 label1 = ct->path;
5507 label2 = ct->path;
5508 break;
5509 case GOT_STATUS_ADD:
5510 label2 = ct->path;
5511 break;
5512 case GOT_STATUS_DELETE:
5513 label1 = ct->path;
5514 break;
5515 default:
5516 return got_error(GOT_ERR_FILE_STATUS);
5518 fd1 = got_opentempfd();
5519 if (fd1 == -1) {
5520 err = got_error_from_errno("got_opentempfd");
5521 goto done;
5523 fd2 = got_opentempfd();
5524 if (fd2 == -1) {
5525 err = got_error_from_errno("got_opentempfd");
5526 goto done;
5528 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5529 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5530 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5531 NULL, repo, diff_outfile);
5532 goto done;
5535 fd1 = got_opentempfd();
5536 if (fd1 == -1) {
5537 err = got_error_from_errno("got_opentempfd");
5538 goto done;
5541 if (ct->status != GOT_STATUS_ADD) {
5542 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5543 8192, fd1);
5544 if (err)
5545 goto done;
5548 if (ct->status != GOT_STATUS_DELETE) {
5549 if (dirfd != -1) {
5550 fd = openat(dirfd, de_name,
5551 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5552 if (fd == -1) {
5553 if (!got_err_open_nofollow_on_symlink()) {
5554 err = got_error_from_errno2("openat",
5555 ct->ondisk_path);
5556 goto done;
5558 err = get_symlink_target_file(&fd, dirfd,
5559 de_name, ct->ondisk_path);
5560 if (err)
5561 goto done;
5563 } else {
5564 fd = open(ct->ondisk_path,
5565 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5566 if (fd == -1) {
5567 if (!got_err_open_nofollow_on_symlink()) {
5568 err = got_error_from_errno2("open",
5569 ct->ondisk_path);
5570 goto done;
5572 err = get_symlink_target_file(&fd, dirfd,
5573 de_name, ct->ondisk_path);
5574 if (err)
5575 goto done;
5578 if (fstatat(fd, ct->ondisk_path, &sb,
5579 AT_SYMLINK_NOFOLLOW) == -1) {
5580 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5581 goto done;
5583 ondisk_file = fdopen(fd, "r");
5584 if (ondisk_file == NULL) {
5585 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5586 goto done;
5588 fd = -1;
5589 f2_exists = 1;
5592 if (blob1) {
5593 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5594 f1, blob1);
5595 if (err)
5596 goto done;
5599 err = got_diff_blob_file(blob1, f1, size1, label1,
5600 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5601 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5602 done:
5603 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5604 err = got_error_from_errno("close");
5605 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5606 err = got_error_from_errno("close");
5607 if (blob1)
5608 got_object_blob_close(blob1);
5609 if (fd != -1 && close(fd) == -1 && err == NULL)
5610 err = got_error_from_errno("close");
5611 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5612 err = got_error_from_errno("fclose");
5613 return err;
5616 static const struct got_error *
5617 collect_commitables(void *arg, unsigned char status,
5618 unsigned char staged_status, const char *relpath,
5619 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5620 struct got_object_id *commit_id, int dirfd, const char *de_name)
5622 struct collect_commitables_arg *a = arg;
5623 const struct got_error *err = NULL;
5624 struct got_commitable *ct = NULL;
5625 struct got_pathlist_entry *new = NULL;
5626 char *parent_path = NULL, *path = NULL;
5627 struct stat sb;
5629 if (a->have_staged_files) {
5630 if (staged_status != GOT_STATUS_MODIFY &&
5631 staged_status != GOT_STATUS_ADD &&
5632 staged_status != GOT_STATUS_DELETE)
5633 return NULL;
5634 } else {
5635 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5636 printf("C %s\n", relpath);
5637 return got_error(GOT_ERR_COMMIT_CONFLICT);
5640 if (status != GOT_STATUS_MODIFY &&
5641 status != GOT_STATUS_MODE_CHANGE &&
5642 status != GOT_STATUS_ADD &&
5643 status != GOT_STATUS_DELETE &&
5644 status != GOT_STATUS_CONFLICT)
5645 return NULL;
5648 if (asprintf(&path, "/%s", relpath) == -1) {
5649 err = got_error_from_errno("asprintf");
5650 goto done;
5652 if (strcmp(path, "/") == 0) {
5653 parent_path = strdup("");
5654 if (parent_path == NULL)
5655 return got_error_from_errno("strdup");
5656 } else {
5657 err = got_path_dirname(&parent_path, path);
5658 if (err)
5659 return err;
5662 ct = calloc(1, sizeof(*ct));
5663 if (ct == NULL) {
5664 err = got_error_from_errno("calloc");
5665 goto done;
5668 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5669 relpath) == -1) {
5670 err = got_error_from_errno("asprintf");
5671 goto done;
5674 if (staged_status == GOT_STATUS_ADD ||
5675 staged_status == GOT_STATUS_MODIFY) {
5676 struct got_fileindex_entry *ie;
5677 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5678 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5679 case GOT_FILEIDX_MODE_REGULAR_FILE:
5680 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5681 ct->mode = S_IFREG;
5682 break;
5683 case GOT_FILEIDX_MODE_SYMLINK:
5684 ct->mode = S_IFLNK;
5685 break;
5686 default:
5687 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5688 goto done;
5690 ct->mode |= got_fileindex_entry_perms_get(ie);
5691 } else if (status != GOT_STATUS_DELETE &&
5692 staged_status != GOT_STATUS_DELETE) {
5693 if (dirfd != -1) {
5694 if (fstatat(dirfd, de_name, &sb,
5695 AT_SYMLINK_NOFOLLOW) == -1) {
5696 err = got_error_from_errno2("fstatat",
5697 ct->ondisk_path);
5698 goto done;
5700 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5701 err = got_error_from_errno2("lstat", ct->ondisk_path);
5702 goto done;
5704 ct->mode = sb.st_mode;
5707 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5708 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5709 relpath) == -1) {
5710 err = got_error_from_errno("asprintf");
5711 goto done;
5714 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5715 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5716 int is_bad_symlink;
5717 char target_path[PATH_MAX];
5718 ssize_t target_len;
5719 target_len = readlink(ct->ondisk_path, target_path,
5720 sizeof(target_path));
5721 if (target_len == -1) {
5722 err = got_error_from_errno2("readlink",
5723 ct->ondisk_path);
5724 goto done;
5726 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5727 target_len, ct->ondisk_path, a->worktree->root_path,
5728 a->worktree->meta_dir);
5729 if (err)
5730 goto done;
5731 if (is_bad_symlink) {
5732 err = got_error_path(ct->ondisk_path,
5733 GOT_ERR_BAD_SYMLINK);
5734 goto done;
5739 ct->status = status;
5740 ct->staged_status = staged_status;
5741 ct->blob_id = NULL; /* will be filled in when blob gets created */
5742 if (ct->status != GOT_STATUS_ADD &&
5743 ct->staged_status != GOT_STATUS_ADD) {
5744 ct->base_blob_id = got_object_id_dup(blob_id);
5745 if (ct->base_blob_id == NULL) {
5746 err = got_error_from_errno("got_object_id_dup");
5747 goto done;
5749 ct->base_commit_id = got_object_id_dup(commit_id);
5750 if (ct->base_commit_id == NULL) {
5751 err = got_error_from_errno("got_object_id_dup");
5752 goto done;
5755 if (ct->staged_status == GOT_STATUS_ADD ||
5756 ct->staged_status == GOT_STATUS_MODIFY) {
5757 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5758 if (ct->staged_blob_id == NULL) {
5759 err = got_error_from_errno("got_object_id_dup");
5760 goto done;
5763 ct->path = strdup(path);
5764 if (ct->path == NULL) {
5765 err = got_error_from_errno("strdup");
5766 goto done;
5768 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5769 if (err)
5770 goto done;
5772 if (a->diff_outfile && ct && new != NULL) {
5773 err = append_ct_diff(ct, &a->diff_header_shown,
5774 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5775 a->have_staged_files, a->repo, a->worktree);
5776 if (err)
5777 goto done;
5779 done:
5780 if (ct && (err || new == NULL))
5781 free_commitable(ct);
5782 free(parent_path);
5783 free(path);
5784 return err;
5787 static const struct got_error *write_tree(struct got_object_id **, int *,
5788 struct got_tree_object *, const char *, struct got_pathlist_head *,
5789 got_worktree_status_cb status_cb, void *status_arg,
5790 struct got_repository *);
5792 static const struct got_error *
5793 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5794 struct got_tree_entry *te, const char *parent_path,
5795 struct got_pathlist_head *commitable_paths,
5796 got_worktree_status_cb status_cb, void *status_arg,
5797 struct got_repository *repo)
5799 const struct got_error *err = NULL;
5800 struct got_tree_object *subtree;
5801 char *subpath;
5803 if (asprintf(&subpath, "%s%s%s", parent_path,
5804 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5805 return got_error_from_errno("asprintf");
5807 err = got_object_open_as_tree(&subtree, repo, &te->id);
5808 if (err)
5809 return err;
5811 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5812 commitable_paths, status_cb, status_arg, repo);
5813 got_object_tree_close(subtree);
5814 free(subpath);
5815 return err;
5818 static const struct got_error *
5819 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5821 const struct got_error *err = NULL;
5822 char *ct_parent_path = NULL;
5824 *match = 0;
5826 if (strchr(ct->in_repo_path, '/') == NULL) {
5827 *match = got_path_is_root_dir(path);
5828 return NULL;
5831 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5832 if (err)
5833 return err;
5834 *match = (strcmp(path, ct_parent_path) == 0);
5835 free(ct_parent_path);
5836 return err;
5839 static mode_t
5840 get_ct_file_mode(struct got_commitable *ct)
5842 if (S_ISLNK(ct->mode))
5843 return S_IFLNK;
5845 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5848 static const struct got_error *
5849 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5850 struct got_tree_entry *te, struct got_commitable *ct)
5852 const struct got_error *err = NULL;
5854 *new_te = NULL;
5856 err = got_object_tree_entry_dup(new_te, te);
5857 if (err)
5858 goto done;
5860 (*new_te)->mode = get_ct_file_mode(ct);
5862 if (ct->staged_status == GOT_STATUS_MODIFY)
5863 memcpy(&(*new_te)->id, ct->staged_blob_id,
5864 sizeof((*new_te)->id));
5865 else
5866 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5867 done:
5868 if (err && *new_te) {
5869 free(*new_te);
5870 *new_te = NULL;
5872 return err;
5875 static const struct got_error *
5876 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5877 struct got_commitable *ct)
5879 const struct got_error *err = NULL;
5880 char *ct_name = NULL;
5882 *new_te = NULL;
5884 *new_te = calloc(1, sizeof(**new_te));
5885 if (*new_te == NULL)
5886 return got_error_from_errno("calloc");
5888 err = got_path_basename(&ct_name, ct->path);
5889 if (err)
5890 goto done;
5891 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5892 sizeof((*new_te)->name)) {
5893 err = got_error(GOT_ERR_NO_SPACE);
5894 goto done;
5897 (*new_te)->mode = get_ct_file_mode(ct);
5899 if (ct->staged_status == GOT_STATUS_ADD)
5900 memcpy(&(*new_te)->id, ct->staged_blob_id,
5901 sizeof((*new_te)->id));
5902 else
5903 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5904 done:
5905 free(ct_name);
5906 if (err && *new_te) {
5907 free(*new_te);
5908 *new_te = NULL;
5910 return err;
5913 static const struct got_error *
5914 insert_tree_entry(struct got_tree_entry *new_te,
5915 struct got_pathlist_head *paths)
5917 const struct got_error *err = NULL;
5918 struct got_pathlist_entry *new_pe;
5920 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5921 if (err)
5922 return err;
5923 if (new_pe == NULL)
5924 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5925 return NULL;
5928 static const struct got_error *
5929 report_ct_status(struct got_commitable *ct,
5930 got_worktree_status_cb status_cb, void *status_arg)
5932 const char *ct_path = ct->path;
5933 unsigned char status;
5935 if (status_cb == NULL) /* no commit progress output desired */
5936 return NULL;
5938 while (ct_path[0] == '/')
5939 ct_path++;
5941 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5942 status = ct->staged_status;
5943 else
5944 status = ct->status;
5946 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5947 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5950 static const struct got_error *
5951 match_modified_subtree(int *modified, struct got_tree_entry *te,
5952 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5954 const struct got_error *err = NULL;
5955 struct got_pathlist_entry *pe;
5956 char *te_path;
5958 *modified = 0;
5960 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5961 got_path_is_root_dir(base_tree_path) ? "" : "/",
5962 te->name) == -1)
5963 return got_error_from_errno("asprintf");
5965 TAILQ_FOREACH(pe, commitable_paths, entry) {
5966 struct got_commitable *ct = pe->data;
5967 *modified = got_path_is_child(ct->in_repo_path, te_path,
5968 strlen(te_path));
5969 if (*modified)
5970 break;
5973 free(te_path);
5974 return err;
5977 static const struct got_error *
5978 match_deleted_or_modified_ct(struct got_commitable **ctp,
5979 struct got_tree_entry *te, const char *base_tree_path,
5980 struct got_pathlist_head *commitable_paths)
5982 const struct got_error *err = NULL;
5983 struct got_pathlist_entry *pe;
5985 *ctp = NULL;
5987 TAILQ_FOREACH(pe, commitable_paths, entry) {
5988 struct got_commitable *ct = pe->data;
5989 char *ct_name = NULL;
5990 int path_matches;
5992 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5993 if (ct->status != GOT_STATUS_MODIFY &&
5994 ct->status != GOT_STATUS_MODE_CHANGE &&
5995 ct->status != GOT_STATUS_DELETE &&
5996 ct->status != GOT_STATUS_CONFLICT)
5997 continue;
5998 } else {
5999 if (ct->staged_status != GOT_STATUS_MODIFY &&
6000 ct->staged_status != GOT_STATUS_DELETE)
6001 continue;
6004 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6005 continue;
6007 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6008 if (err)
6009 return err;
6010 if (!path_matches)
6011 continue;
6013 err = got_path_basename(&ct_name, pe->path);
6014 if (err)
6015 return err;
6017 if (strcmp(te->name, ct_name) != 0) {
6018 free(ct_name);
6019 continue;
6021 free(ct_name);
6023 *ctp = ct;
6024 break;
6027 return err;
6030 static const struct got_error *
6031 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6032 const char *child_path, const char *path_base_tree,
6033 struct got_pathlist_head *commitable_paths,
6034 got_worktree_status_cb status_cb, void *status_arg,
6035 struct got_repository *repo)
6037 const struct got_error *err = NULL;
6038 struct got_tree_entry *new_te;
6039 char *subtree_path;
6040 struct got_object_id *id = NULL;
6041 int nentries;
6043 *new_tep = NULL;
6045 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6046 got_path_is_root_dir(path_base_tree) ? "" : "/",
6047 child_path) == -1)
6048 return got_error_from_errno("asprintf");
6050 new_te = calloc(1, sizeof(*new_te));
6051 if (new_te == NULL)
6052 return got_error_from_errno("calloc");
6053 new_te->mode = S_IFDIR;
6055 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6056 sizeof(new_te->name)) {
6057 err = got_error(GOT_ERR_NO_SPACE);
6058 goto done;
6060 err = write_tree(&id, &nentries, NULL, subtree_path,
6061 commitable_paths, status_cb, status_arg, repo);
6062 if (err) {
6063 free(new_te);
6064 goto done;
6066 memcpy(&new_te->id, id, sizeof(new_te->id));
6067 done:
6068 free(id);
6069 free(subtree_path);
6070 if (err == NULL)
6071 *new_tep = new_te;
6072 return err;
6075 static const struct got_error *
6076 write_tree(struct got_object_id **new_tree_id, int *nentries,
6077 struct got_tree_object *base_tree, const char *path_base_tree,
6078 struct got_pathlist_head *commitable_paths,
6079 got_worktree_status_cb status_cb, void *status_arg,
6080 struct got_repository *repo)
6082 const struct got_error *err = NULL;
6083 struct got_pathlist_head paths;
6084 struct got_tree_entry *te, *new_te = NULL;
6085 struct got_pathlist_entry *pe;
6087 TAILQ_INIT(&paths);
6088 *nentries = 0;
6090 /* Insert, and recurse into, newly added entries first. */
6091 TAILQ_FOREACH(pe, commitable_paths, entry) {
6092 struct got_commitable *ct = pe->data;
6093 char *child_path = NULL, *slash;
6095 if ((ct->status != GOT_STATUS_ADD &&
6096 ct->staged_status != GOT_STATUS_ADD) ||
6097 (ct->flags & GOT_COMMITABLE_ADDED))
6098 continue;
6100 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6101 strlen(path_base_tree)))
6102 continue;
6104 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6105 ct->in_repo_path);
6106 if (err)
6107 goto done;
6109 slash = strchr(child_path, '/');
6110 if (slash == NULL) {
6111 err = alloc_added_blob_tree_entry(&new_te, ct);
6112 if (err)
6113 goto done;
6114 err = report_ct_status(ct, status_cb, status_arg);
6115 if (err)
6116 goto done;
6117 ct->flags |= GOT_COMMITABLE_ADDED;
6118 err = insert_tree_entry(new_te, &paths);
6119 if (err)
6120 goto done;
6121 (*nentries)++;
6122 } else {
6123 *slash = '\0'; /* trim trailing path components */
6124 if (base_tree == NULL ||
6125 got_object_tree_find_entry(base_tree, child_path)
6126 == NULL) {
6127 err = make_subtree_for_added_blob(&new_te,
6128 child_path, path_base_tree,
6129 commitable_paths, status_cb, status_arg,
6130 repo);
6131 if (err)
6132 goto done;
6133 err = insert_tree_entry(new_te, &paths);
6134 if (err)
6135 goto done;
6136 (*nentries)++;
6141 if (base_tree) {
6142 int i, nbase_entries;
6143 /* Handle modified and deleted entries. */
6144 nbase_entries = got_object_tree_get_nentries(base_tree);
6145 for (i = 0; i < nbase_entries; i++) {
6146 struct got_commitable *ct = NULL;
6148 te = got_object_tree_get_entry(base_tree, i);
6149 if (got_object_tree_entry_is_submodule(te)) {
6150 /* Entry is a submodule; just copy it. */
6151 err = got_object_tree_entry_dup(&new_te, te);
6152 if (err)
6153 goto done;
6154 err = insert_tree_entry(new_te, &paths);
6155 if (err)
6156 goto done;
6157 (*nentries)++;
6158 continue;
6161 if (S_ISDIR(te->mode)) {
6162 int modified;
6163 err = got_object_tree_entry_dup(&new_te, te);
6164 if (err)
6165 goto done;
6166 err = match_modified_subtree(&modified, te,
6167 path_base_tree, commitable_paths);
6168 if (err)
6169 goto done;
6170 /* Avoid recursion into unmodified subtrees. */
6171 if (modified) {
6172 struct got_object_id *new_id;
6173 int nsubentries;
6174 err = write_subtree(&new_id,
6175 &nsubentries, te,
6176 path_base_tree, commitable_paths,
6177 status_cb, status_arg, repo);
6178 if (err)
6179 goto done;
6180 if (nsubentries == 0) {
6181 /* All entries were deleted. */
6182 free(new_id);
6183 continue;
6185 memcpy(&new_te->id, new_id,
6186 sizeof(new_te->id));
6187 free(new_id);
6189 err = insert_tree_entry(new_te, &paths);
6190 if (err)
6191 goto done;
6192 (*nentries)++;
6193 continue;
6196 err = match_deleted_or_modified_ct(&ct, te,
6197 path_base_tree, commitable_paths);
6198 if (err)
6199 goto done;
6200 if (ct) {
6201 /* NB: Deleted entries get dropped here. */
6202 if (ct->status == GOT_STATUS_MODIFY ||
6203 ct->status == GOT_STATUS_MODE_CHANGE ||
6204 ct->status == GOT_STATUS_CONFLICT ||
6205 ct->staged_status == GOT_STATUS_MODIFY) {
6206 err = alloc_modified_blob_tree_entry(
6207 &new_te, te, ct);
6208 if (err)
6209 goto done;
6210 err = insert_tree_entry(new_te, &paths);
6211 if (err)
6212 goto done;
6213 (*nentries)++;
6215 err = report_ct_status(ct, status_cb,
6216 status_arg);
6217 if (err)
6218 goto done;
6219 } else {
6220 /* Entry is unchanged; just copy it. */
6221 err = got_object_tree_entry_dup(&new_te, te);
6222 if (err)
6223 goto done;
6224 err = insert_tree_entry(new_te, &paths);
6225 if (err)
6226 goto done;
6227 (*nentries)++;
6232 /* Write new list of entries; deleted entries have been dropped. */
6233 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6234 done:
6235 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6236 return err;
6239 static const struct got_error *
6240 update_fileindex_after_commit(struct got_worktree *worktree,
6241 struct got_pathlist_head *commitable_paths,
6242 struct got_object_id *new_base_commit_id,
6243 struct got_fileindex *fileindex, int have_staged_files)
6245 const struct got_error *err = NULL;
6246 struct got_pathlist_entry *pe;
6247 char *relpath = NULL;
6249 TAILQ_FOREACH(pe, commitable_paths, entry) {
6250 struct got_fileindex_entry *ie;
6251 struct got_commitable *ct = pe->data;
6253 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6255 err = got_path_skip_common_ancestor(&relpath,
6256 worktree->root_path, ct->ondisk_path);
6257 if (err)
6258 goto done;
6260 if (ie) {
6261 if (ct->status == GOT_STATUS_DELETE ||
6262 ct->staged_status == GOT_STATUS_DELETE) {
6263 got_fileindex_entry_remove(fileindex, ie);
6264 } else if (ct->staged_status == GOT_STATUS_ADD ||
6265 ct->staged_status == GOT_STATUS_MODIFY) {
6266 got_fileindex_entry_stage_set(ie,
6267 GOT_FILEIDX_STAGE_NONE);
6268 got_fileindex_entry_staged_filetype_set(ie, 0);
6270 err = got_fileindex_entry_update(ie,
6271 worktree->root_fd, relpath,
6272 ct->staged_blob_id->sha1,
6273 new_base_commit_id->sha1,
6274 !have_staged_files);
6275 } else
6276 err = got_fileindex_entry_update(ie,
6277 worktree->root_fd, relpath,
6278 ct->blob_id->sha1,
6279 new_base_commit_id->sha1,
6280 !have_staged_files);
6281 } else {
6282 err = got_fileindex_entry_alloc(&ie, pe->path);
6283 if (err)
6284 goto done;
6285 err = got_fileindex_entry_update(ie,
6286 worktree->root_fd, relpath, ct->blob_id->sha1,
6287 new_base_commit_id->sha1, 1);
6288 if (err) {
6289 got_fileindex_entry_free(ie);
6290 goto done;
6292 err = got_fileindex_entry_add(fileindex, ie);
6293 if (err) {
6294 got_fileindex_entry_free(ie);
6295 goto done;
6298 free(relpath);
6299 relpath = NULL;
6301 done:
6302 free(relpath);
6303 return err;
6307 static const struct got_error *
6308 check_out_of_date(const char *in_repo_path, unsigned char status,
6309 unsigned char staged_status, struct got_object_id *base_blob_id,
6310 struct got_object_id *base_commit_id,
6311 struct got_object_id *head_commit_id, struct got_repository *repo,
6312 int ood_errcode)
6314 const struct got_error *err = NULL;
6315 struct got_commit_object *commit = NULL;
6316 struct got_object_id *id = NULL;
6318 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6319 /* Trivial case: base commit == head commit */
6320 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6321 return NULL;
6323 * Ensure file content which local changes were based
6324 * on matches file content in the branch head.
6326 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6327 if (err)
6328 goto done;
6329 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6330 if (err) {
6331 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6332 err = got_error(ood_errcode);
6333 goto done;
6334 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6335 err = got_error(ood_errcode);
6336 } else {
6337 /* Require that added files don't exist in the branch head. */
6338 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6339 if (err)
6340 goto done;
6341 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6342 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6343 goto done;
6344 err = id ? got_error(ood_errcode) : NULL;
6346 done:
6347 free(id);
6348 if (commit)
6349 got_object_commit_close(commit);
6350 return err;
6353 static const struct got_error *
6354 commit_worktree(struct got_object_id **new_commit_id,
6355 struct got_pathlist_head *commitable_paths,
6356 struct got_object_id *head_commit_id,
6357 struct got_object_id *parent_id2,
6358 struct got_worktree *worktree,
6359 const char *author, const char *committer, char *diff_path,
6360 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6361 got_worktree_status_cb status_cb, void *status_arg,
6362 struct got_repository *repo)
6364 const struct got_error *err = NULL, *unlockerr = NULL;
6365 struct got_pathlist_entry *pe;
6366 const char *head_ref_name = NULL;
6367 struct got_commit_object *head_commit = NULL;
6368 struct got_reference *head_ref2 = NULL;
6369 struct got_object_id *head_commit_id2 = NULL;
6370 struct got_tree_object *head_tree = NULL;
6371 struct got_object_id *new_tree_id = NULL;
6372 int nentries, nparents = 0;
6373 struct got_object_id_queue parent_ids;
6374 struct got_object_qid *pid = NULL;
6375 char *logmsg = NULL;
6376 time_t timestamp;
6378 *new_commit_id = NULL;
6380 STAILQ_INIT(&parent_ids);
6382 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6383 if (err)
6384 goto done;
6386 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6387 if (err)
6388 goto done;
6390 if (commit_msg_cb != NULL) {
6391 err = commit_msg_cb(commitable_paths, diff_path,
6392 &logmsg, commit_arg);
6393 if (err)
6394 goto done;
6397 if (logmsg == NULL || strlen(logmsg) == 0) {
6398 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6399 goto done;
6402 /* Create blobs from added and modified files and record their IDs. */
6403 TAILQ_FOREACH(pe, commitable_paths, entry) {
6404 struct got_commitable *ct = pe->data;
6405 char *ondisk_path;
6407 /* Blobs for staged files already exist. */
6408 if (ct->staged_status == GOT_STATUS_ADD ||
6409 ct->staged_status == GOT_STATUS_MODIFY)
6410 continue;
6412 if (ct->status != GOT_STATUS_ADD &&
6413 ct->status != GOT_STATUS_MODIFY &&
6414 ct->status != GOT_STATUS_MODE_CHANGE &&
6415 ct->status != GOT_STATUS_CONFLICT)
6416 continue;
6418 if (asprintf(&ondisk_path, "%s/%s",
6419 worktree->root_path, pe->path) == -1) {
6420 err = got_error_from_errno("asprintf");
6421 goto done;
6423 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6424 free(ondisk_path);
6425 if (err)
6426 goto done;
6429 /* Recursively write new tree objects. */
6430 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6431 commitable_paths, status_cb, status_arg, repo);
6432 if (err)
6433 goto done;
6435 err = got_object_qid_alloc(&pid, head_commit_id);
6436 if (err)
6437 goto done;
6438 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6439 nparents++;
6440 if (parent_id2) {
6441 err = got_object_qid_alloc(&pid, parent_id2);
6442 if (err)
6443 goto done;
6444 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6445 nparents++;
6447 timestamp = time(NULL);
6448 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6449 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6450 if (logmsg != NULL)
6451 free(logmsg);
6452 if (err)
6453 goto done;
6455 /* Check if a concurrent commit to our branch has occurred. */
6456 head_ref_name = got_worktree_get_head_ref_name(worktree);
6457 if (head_ref_name == NULL) {
6458 err = got_error_from_errno("got_worktree_get_head_ref_name");
6459 goto done;
6461 /* Lock the reference here to prevent concurrent modification. */
6462 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6463 if (err)
6464 goto done;
6465 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6466 if (err)
6467 goto done;
6468 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6469 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6470 goto done;
6472 /* Update branch head in repository. */
6473 err = got_ref_change_ref(head_ref2, *new_commit_id);
6474 if (err)
6475 goto done;
6476 err = got_ref_write(head_ref2, repo);
6477 if (err)
6478 goto done;
6480 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6481 if (err)
6482 goto done;
6484 err = ref_base_commit(worktree, repo);
6485 if (err)
6486 goto done;
6487 done:
6488 got_object_id_queue_free(&parent_ids);
6489 if (head_tree)
6490 got_object_tree_close(head_tree);
6491 if (head_commit)
6492 got_object_commit_close(head_commit);
6493 free(head_commit_id2);
6494 if (head_ref2) {
6495 unlockerr = got_ref_unlock(head_ref2);
6496 if (unlockerr && err == NULL)
6497 err = unlockerr;
6498 got_ref_close(head_ref2);
6500 return err;
6503 static const struct got_error *
6504 check_path_is_commitable(const char *path,
6505 struct got_pathlist_head *commitable_paths)
6507 struct got_pathlist_entry *cpe = NULL;
6508 size_t path_len = strlen(path);
6510 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6511 struct got_commitable *ct = cpe->data;
6512 const char *ct_path = ct->path;
6514 while (ct_path[0] == '/')
6515 ct_path++;
6517 if (strcmp(path, ct_path) == 0 ||
6518 got_path_is_child(ct_path, path, path_len))
6519 break;
6522 if (cpe == NULL)
6523 return got_error_path(path, GOT_ERR_BAD_PATH);
6525 return NULL;
6528 static const struct got_error *
6529 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6531 int *have_staged_files = arg;
6533 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6534 *have_staged_files = 1;
6535 return got_error(GOT_ERR_CANCELLED);
6538 return NULL;
6541 static const struct got_error *
6542 check_non_staged_files(struct got_fileindex *fileindex,
6543 struct got_pathlist_head *paths)
6545 struct got_pathlist_entry *pe;
6546 struct got_fileindex_entry *ie;
6548 TAILQ_FOREACH(pe, paths, entry) {
6549 if (pe->path[0] == '\0')
6550 continue;
6551 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6552 if (ie == NULL)
6553 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6554 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6555 return got_error_path(pe->path,
6556 GOT_ERR_FILE_NOT_STAGED);
6559 return NULL;
6562 const struct got_error *
6563 got_worktree_commit(struct got_object_id **new_commit_id,
6564 struct got_worktree *worktree, struct got_pathlist_head *paths,
6565 const char *author, const char *committer, int allow_bad_symlinks,
6566 int show_diff, int commit_conflicts,
6567 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6568 got_worktree_status_cb status_cb, void *status_arg,
6569 struct got_repository *repo)
6571 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6572 struct got_fileindex *fileindex = NULL;
6573 char *fileindex_path = NULL;
6574 struct got_pathlist_head commitable_paths;
6575 struct collect_commitables_arg cc_arg;
6576 struct got_pathlist_entry *pe;
6577 struct got_reference *head_ref = NULL;
6578 struct got_object_id *head_commit_id = NULL;
6579 char *diff_path = NULL;
6580 int have_staged_files = 0;
6582 *new_commit_id = NULL;
6584 memset(&cc_arg, 0, sizeof(cc_arg));
6585 TAILQ_INIT(&commitable_paths);
6587 err = lock_worktree(worktree, LOCK_EX);
6588 if (err)
6589 goto done;
6591 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6592 if (err)
6593 goto done;
6595 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6596 if (err)
6597 goto done;
6599 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6600 if (err)
6601 goto done;
6603 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6604 &have_staged_files);
6605 if (err && err->code != GOT_ERR_CANCELLED)
6606 goto done;
6607 if (have_staged_files) {
6608 err = check_non_staged_files(fileindex, paths);
6609 if (err)
6610 goto done;
6613 cc_arg.commitable_paths = &commitable_paths;
6614 cc_arg.worktree = worktree;
6615 cc_arg.fileindex = fileindex;
6616 cc_arg.repo = repo;
6617 cc_arg.have_staged_files = have_staged_files;
6618 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6619 cc_arg.diff_header_shown = 0;
6620 cc_arg.commit_conflicts = commit_conflicts;
6621 if (show_diff) {
6622 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6623 GOT_TMPDIR_STR "/got", ".diff");
6624 if (err)
6625 goto done;
6626 cc_arg.f1 = got_opentemp();
6627 if (cc_arg.f1 == NULL) {
6628 err = got_error_from_errno("got_opentemp");
6629 goto done;
6631 cc_arg.f2 = got_opentemp();
6632 if (cc_arg.f2 == NULL) {
6633 err = got_error_from_errno("got_opentemp");
6634 goto done;
6638 TAILQ_FOREACH(pe, paths, entry) {
6639 err = worktree_status(worktree, pe->path, fileindex, repo,
6640 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6641 if (err)
6642 goto done;
6645 if (show_diff) {
6646 if (fflush(cc_arg.diff_outfile) == EOF) {
6647 err = got_error_from_errno("fflush");
6648 goto done;
6652 if (TAILQ_EMPTY(&commitable_paths)) {
6653 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6654 goto done;
6657 TAILQ_FOREACH(pe, paths, entry) {
6658 err = check_path_is_commitable(pe->path, &commitable_paths);
6659 if (err)
6660 goto done;
6663 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6664 struct got_commitable *ct = pe->data;
6665 const char *ct_path = ct->in_repo_path;
6667 while (ct_path[0] == '/')
6668 ct_path++;
6669 err = check_out_of_date(ct_path, ct->status,
6670 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6671 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6672 if (err)
6673 goto done;
6677 err = commit_worktree(new_commit_id, &commitable_paths,
6678 head_commit_id, NULL, worktree, author, committer,
6679 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6680 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6681 if (err)
6682 goto done;
6684 err = update_fileindex_after_commit(worktree, &commitable_paths,
6685 *new_commit_id, fileindex, have_staged_files);
6686 sync_err = sync_fileindex(fileindex, fileindex_path);
6687 if (sync_err && err == NULL)
6688 err = sync_err;
6689 done:
6690 if (fileindex)
6691 got_fileindex_free(fileindex);
6692 free(fileindex_path);
6693 unlockerr = lock_worktree(worktree, LOCK_SH);
6694 if (unlockerr && err == NULL)
6695 err = unlockerr;
6696 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6697 struct got_commitable *ct = pe->data;
6699 free_commitable(ct);
6701 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6702 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6703 err = got_error_from_errno2("unlink", diff_path);
6704 free(diff_path);
6705 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6706 err == NULL)
6707 err = got_error_from_errno("fclose");
6708 return err;
6711 const char *
6712 got_commitable_get_path(struct got_commitable *ct)
6714 return ct->path;
6717 unsigned int
6718 got_commitable_get_status(struct got_commitable *ct)
6720 return ct->status;
6723 struct check_rebase_ok_arg {
6724 struct got_worktree *worktree;
6725 struct got_repository *repo;
6728 static const struct got_error *
6729 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6731 const struct got_error *err = NULL;
6732 struct check_rebase_ok_arg *a = arg;
6733 unsigned char status;
6734 struct stat sb;
6735 char *ondisk_path;
6737 /* Reject rebase of a work tree with mixed base commits. */
6738 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6739 SHA1_DIGEST_LENGTH))
6740 return got_error(GOT_ERR_MIXED_COMMITS);
6742 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6743 == -1)
6744 return got_error_from_errno("asprintf");
6746 /* Reject rebase of a work tree with modified or staged files. */
6747 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6748 free(ondisk_path);
6749 if (err)
6750 return err;
6752 if (status != GOT_STATUS_NO_CHANGE)
6753 return got_error(GOT_ERR_MODIFIED);
6754 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6755 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6757 return NULL;
6760 const struct got_error *
6761 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6762 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6763 struct got_worktree *worktree, struct got_reference *branch,
6764 struct got_repository *repo)
6766 const struct got_error *err = NULL;
6767 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6768 char *branch_ref_name = NULL;
6769 char *fileindex_path = NULL;
6770 struct check_rebase_ok_arg ok_arg;
6771 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6772 struct got_object_id *wt_branch_tip = NULL;
6774 *new_base_branch_ref = NULL;
6775 *tmp_branch = NULL;
6776 *fileindex = NULL;
6778 err = lock_worktree(worktree, LOCK_EX);
6779 if (err)
6780 return err;
6782 err = open_fileindex(fileindex, &fileindex_path, worktree);
6783 if (err)
6784 goto done;
6786 ok_arg.worktree = worktree;
6787 ok_arg.repo = repo;
6788 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6789 &ok_arg);
6790 if (err)
6791 goto done;
6793 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6794 if (err)
6795 goto done;
6797 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6798 if (err)
6799 goto done;
6801 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6802 if (err)
6803 goto done;
6805 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6806 0);
6807 if (err)
6808 goto done;
6810 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6811 if (err)
6812 goto done;
6813 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6814 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6815 goto done;
6818 err = got_ref_alloc_symref(new_base_branch_ref,
6819 new_base_branch_ref_name, wt_branch);
6820 if (err)
6821 goto done;
6822 err = got_ref_write(*new_base_branch_ref, repo);
6823 if (err)
6824 goto done;
6826 /* TODO Lock original branch's ref while rebasing? */
6828 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6829 if (err)
6830 goto done;
6832 err = got_ref_write(branch_ref, repo);
6833 if (err)
6834 goto done;
6836 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6837 worktree->base_commit_id);
6838 if (err)
6839 goto done;
6840 err = got_ref_write(*tmp_branch, repo);
6841 if (err)
6842 goto done;
6844 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6845 if (err)
6846 goto done;
6847 done:
6848 free(fileindex_path);
6849 free(tmp_branch_name);
6850 free(new_base_branch_ref_name);
6851 free(branch_ref_name);
6852 if (branch_ref)
6853 got_ref_close(branch_ref);
6854 if (wt_branch)
6855 got_ref_close(wt_branch);
6856 free(wt_branch_tip);
6857 if (err) {
6858 if (*new_base_branch_ref) {
6859 got_ref_close(*new_base_branch_ref);
6860 *new_base_branch_ref = NULL;
6862 if (*tmp_branch) {
6863 got_ref_close(*tmp_branch);
6864 *tmp_branch = NULL;
6866 if (*fileindex) {
6867 got_fileindex_free(*fileindex);
6868 *fileindex = NULL;
6870 lock_worktree(worktree, LOCK_SH);
6872 return err;
6875 const struct got_error *
6876 got_worktree_rebase_continue(struct got_object_id **commit_id,
6877 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6878 struct got_reference **branch, struct got_fileindex **fileindex,
6879 struct got_worktree *worktree, struct got_repository *repo)
6881 const struct got_error *err;
6882 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6883 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6884 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6885 char *fileindex_path = NULL;
6886 int have_staged_files = 0;
6888 *commit_id = NULL;
6889 *new_base_branch = NULL;
6890 *tmp_branch = NULL;
6891 *branch = NULL;
6892 *fileindex = NULL;
6894 err = lock_worktree(worktree, LOCK_EX);
6895 if (err)
6896 return err;
6898 err = open_fileindex(fileindex, &fileindex_path, worktree);
6899 if (err)
6900 goto done;
6902 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6903 &have_staged_files);
6904 if (err && err->code != GOT_ERR_CANCELLED)
6905 goto done;
6906 if (have_staged_files) {
6907 err = got_error(GOT_ERR_STAGED_PATHS);
6908 goto done;
6911 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6912 if (err)
6913 goto done;
6915 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6916 if (err)
6917 goto done;
6919 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6920 if (err)
6921 goto done;
6923 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6924 if (err)
6925 goto done;
6927 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6928 if (err)
6929 goto done;
6931 err = got_ref_open(branch, repo,
6932 got_ref_get_symref_target(branch_ref), 0);
6933 if (err)
6934 goto done;
6936 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6937 if (err)
6938 goto done;
6940 err = got_ref_resolve(commit_id, repo, commit_ref);
6941 if (err)
6942 goto done;
6944 err = got_ref_open(new_base_branch, repo,
6945 new_base_branch_ref_name, 0);
6946 if (err)
6947 goto done;
6949 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6950 if (err)
6951 goto done;
6952 done:
6953 free(commit_ref_name);
6954 free(branch_ref_name);
6955 free(fileindex_path);
6956 if (commit_ref)
6957 got_ref_close(commit_ref);
6958 if (branch_ref)
6959 got_ref_close(branch_ref);
6960 if (err) {
6961 free(*commit_id);
6962 *commit_id = NULL;
6963 if (*tmp_branch) {
6964 got_ref_close(*tmp_branch);
6965 *tmp_branch = NULL;
6967 if (*new_base_branch) {
6968 got_ref_close(*new_base_branch);
6969 *new_base_branch = NULL;
6971 if (*branch) {
6972 got_ref_close(*branch);
6973 *branch = NULL;
6975 if (*fileindex) {
6976 got_fileindex_free(*fileindex);
6977 *fileindex = NULL;
6979 lock_worktree(worktree, LOCK_SH);
6981 return err;
6984 const struct got_error *
6985 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6987 const struct got_error *err;
6988 char *tmp_branch_name = NULL;
6990 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6991 if (err)
6992 return err;
6994 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6995 free(tmp_branch_name);
6996 return NULL;
6999 static const struct got_error *
7000 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7001 const char *diff_path, char **logmsg, void *arg)
7003 *logmsg = arg;
7004 return NULL;
7007 static const struct got_error *
7008 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7009 const char *path, struct got_object_id *blob_id,
7010 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7011 int dirfd, const char *de_name)
7013 return NULL;
7016 struct collect_merged_paths_arg {
7017 got_worktree_checkout_cb progress_cb;
7018 void *progress_arg;
7019 struct got_pathlist_head *merged_paths;
7022 static const struct got_error *
7023 collect_merged_paths(void *arg, unsigned char status, const char *path)
7025 const struct got_error *err;
7026 struct collect_merged_paths_arg *a = arg;
7027 char *p;
7028 struct got_pathlist_entry *new;
7030 err = (*a->progress_cb)(a->progress_arg, status, path);
7031 if (err)
7032 return err;
7034 if (status != GOT_STATUS_MERGE &&
7035 status != GOT_STATUS_ADD &&
7036 status != GOT_STATUS_DELETE &&
7037 status != GOT_STATUS_CONFLICT)
7038 return NULL;
7040 p = strdup(path);
7041 if (p == NULL)
7042 return got_error_from_errno("strdup");
7044 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7045 if (err || new == NULL)
7046 free(p);
7047 return err;
7050 static const struct got_error *
7051 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7052 int is_rebase, struct got_repository *repo)
7054 const struct got_error *err;
7055 struct got_reference *commit_ref = NULL;
7057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7058 if (err) {
7059 if (err->code != GOT_ERR_NOT_REF)
7060 goto done;
7061 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7062 if (err)
7063 goto done;
7064 err = got_ref_write(commit_ref, repo);
7065 if (err)
7066 goto done;
7067 } else if (is_rebase) {
7068 struct got_object_id *stored_id;
7069 int cmp;
7071 err = got_ref_resolve(&stored_id, repo, commit_ref);
7072 if (err)
7073 goto done;
7074 cmp = got_object_id_cmp(commit_id, stored_id);
7075 free(stored_id);
7076 if (cmp != 0) {
7077 err = got_error(GOT_ERR_REBASE_COMMITID);
7078 goto done;
7081 done:
7082 if (commit_ref)
7083 got_ref_close(commit_ref);
7084 return err;
7087 static const struct got_error *
7088 rebase_merge_files(struct got_pathlist_head *merged_paths,
7089 const char *commit_ref_name, struct got_worktree *worktree,
7090 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7091 struct got_object_id *commit_id, struct got_repository *repo,
7092 got_worktree_checkout_cb progress_cb, void *progress_arg,
7093 got_cancel_cb cancel_cb, void *cancel_arg)
7095 const struct got_error *err;
7096 struct got_reference *commit_ref = NULL;
7097 struct collect_merged_paths_arg cmp_arg;
7098 char *fileindex_path;
7100 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7102 err = get_fileindex_path(&fileindex_path, worktree);
7103 if (err)
7104 return err;
7106 cmp_arg.progress_cb = progress_cb;
7107 cmp_arg.progress_arg = progress_arg;
7108 cmp_arg.merged_paths = merged_paths;
7109 err = merge_files(worktree, fileindex, fileindex_path,
7110 parent_commit_id, commit_id, repo, collect_merged_paths,
7111 &cmp_arg, cancel_cb, cancel_arg);
7112 if (commit_ref)
7113 got_ref_close(commit_ref);
7114 return err;
7117 const struct got_error *
7118 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7119 struct got_worktree *worktree, struct got_fileindex *fileindex,
7120 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7121 struct got_repository *repo,
7122 got_worktree_checkout_cb progress_cb, void *progress_arg,
7123 got_cancel_cb cancel_cb, void *cancel_arg)
7125 const struct got_error *err;
7126 char *commit_ref_name;
7128 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7129 if (err)
7130 return err;
7132 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7133 if (err)
7134 goto done;
7136 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7137 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7138 progress_arg, cancel_cb, cancel_arg);
7139 done:
7140 free(commit_ref_name);
7141 return err;
7144 const struct got_error *
7145 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7146 struct got_worktree *worktree, struct got_fileindex *fileindex,
7147 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7148 struct got_repository *repo,
7149 got_worktree_checkout_cb progress_cb, void *progress_arg,
7150 got_cancel_cb cancel_cb, void *cancel_arg)
7152 const struct got_error *err;
7153 char *commit_ref_name;
7155 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7156 if (err)
7157 return err;
7159 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7160 if (err)
7161 goto done;
7163 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7164 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7165 progress_arg, cancel_cb, cancel_arg);
7166 done:
7167 free(commit_ref_name);
7168 return err;
7171 static const struct got_error *
7172 rebase_commit(struct got_object_id **new_commit_id,
7173 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7174 struct got_worktree *worktree, struct got_fileindex *fileindex,
7175 struct got_reference *tmp_branch, const char *committer,
7176 struct got_commit_object *orig_commit, const char *new_logmsg,
7177 int allow_conflict, struct got_repository *repo)
7179 const struct got_error *err, *sync_err;
7180 struct got_pathlist_head commitable_paths;
7181 struct collect_commitables_arg cc_arg;
7182 char *fileindex_path = NULL;
7183 struct got_reference *head_ref = NULL;
7184 struct got_object_id *head_commit_id = NULL;
7185 char *logmsg = NULL;
7187 memset(&cc_arg, 0, sizeof(cc_arg));
7188 TAILQ_INIT(&commitable_paths);
7189 *new_commit_id = NULL;
7191 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7193 err = get_fileindex_path(&fileindex_path, worktree);
7194 if (err)
7195 return err;
7197 cc_arg.commitable_paths = &commitable_paths;
7198 cc_arg.worktree = worktree;
7199 cc_arg.repo = repo;
7200 cc_arg.have_staged_files = 0;
7201 cc_arg.commit_conflicts = allow_conflict;
7203 * If possible get the status of individual files directly to
7204 * avoid crawling the entire work tree once per rebased commit.
7206 * Ideally, merged_paths would contain a list of commitables
7207 * we could use so we could skip worktree_status() entirely.
7208 * However, we would then need carefully keep track of cumulative
7209 * effects of operations such as file additions and deletions
7210 * in 'got histedit -f' (folding multiple commits into one),
7211 * and this extra complexity is not really worth it.
7213 if (merged_paths) {
7214 struct got_pathlist_entry *pe;
7215 TAILQ_FOREACH(pe, merged_paths, entry) {
7216 err = worktree_status(worktree, pe->path, fileindex,
7217 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7218 0);
7219 if (err)
7220 goto done;
7222 } else {
7223 err = worktree_status(worktree, "", fileindex, repo,
7224 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7225 if (err)
7226 goto done;
7229 if (TAILQ_EMPTY(&commitable_paths)) {
7230 /* No-op change; commit will be elided. */
7231 err = got_ref_delete(commit_ref, repo);
7232 if (err)
7233 goto done;
7234 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7235 goto done;
7238 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7239 if (err)
7240 goto done;
7242 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7243 if (err)
7244 goto done;
7246 if (new_logmsg) {
7247 logmsg = strdup(new_logmsg);
7248 if (logmsg == NULL) {
7249 err = got_error_from_errno("strdup");
7250 goto done;
7252 } else {
7253 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7254 if (err)
7255 goto done;
7258 /* NB: commit_worktree will call free(logmsg) */
7259 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7260 NULL, worktree, got_object_commit_get_author(orig_commit),
7261 committer ? committer :
7262 got_object_commit_get_committer(orig_commit), NULL,
7263 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7264 if (err)
7265 goto done;
7267 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7268 if (err)
7269 goto done;
7271 err = got_ref_delete(commit_ref, repo);
7272 if (err)
7273 goto done;
7275 err = update_fileindex_after_commit(worktree, &commitable_paths,
7276 *new_commit_id, fileindex, 0);
7277 sync_err = sync_fileindex(fileindex, fileindex_path);
7278 if (sync_err && err == NULL)
7279 err = sync_err;
7280 done:
7281 free(fileindex_path);
7282 free(head_commit_id);
7283 if (head_ref)
7284 got_ref_close(head_ref);
7285 if (err) {
7286 free(*new_commit_id);
7287 *new_commit_id = NULL;
7289 return err;
7292 const struct got_error *
7293 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7294 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7295 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7296 const char *committer, struct got_commit_object *orig_commit,
7297 struct got_object_id *orig_commit_id, int allow_conflict,
7298 struct got_repository *repo)
7300 const struct got_error *err;
7301 char *commit_ref_name;
7302 struct got_reference *commit_ref = NULL;
7303 struct got_object_id *commit_id = NULL;
7305 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7306 if (err)
7307 return err;
7309 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7310 if (err)
7311 goto done;
7312 err = got_ref_resolve(&commit_id, repo, commit_ref);
7313 if (err)
7314 goto done;
7315 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7316 err = got_error(GOT_ERR_REBASE_COMMITID);
7317 goto done;
7320 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7321 worktree, fileindex, tmp_branch, committer, orig_commit,
7322 NULL, allow_conflict, repo);
7323 done:
7324 if (commit_ref)
7325 got_ref_close(commit_ref);
7326 free(commit_ref_name);
7327 free(commit_id);
7328 return err;
7331 const struct got_error *
7332 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7333 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7334 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7335 const char *committer, struct got_commit_object *orig_commit,
7336 struct got_object_id *orig_commit_id, const char *new_logmsg,
7337 int allow_conflict, struct got_repository *repo)
7339 const struct got_error *err;
7340 char *commit_ref_name;
7341 struct got_reference *commit_ref = NULL;
7343 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7344 if (err)
7345 return err;
7347 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7348 if (err)
7349 goto done;
7351 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7352 worktree, fileindex, tmp_branch, committer, orig_commit,
7353 new_logmsg, allow_conflict, repo);
7354 done:
7355 if (commit_ref)
7356 got_ref_close(commit_ref);
7357 free(commit_ref_name);
7358 return err;
7361 const struct got_error *
7362 got_worktree_rebase_postpone(struct got_worktree *worktree,
7363 struct got_fileindex *fileindex)
7365 if (fileindex)
7366 got_fileindex_free(fileindex);
7367 return lock_worktree(worktree, LOCK_SH);
7370 static const struct got_error *
7371 delete_ref(const char *name, struct got_repository *repo)
7373 const struct got_error *err;
7374 struct got_reference *ref;
7376 err = got_ref_open(&ref, repo, name, 0);
7377 if (err) {
7378 if (err->code == GOT_ERR_NOT_REF)
7379 return NULL;
7380 return err;
7383 err = got_ref_delete(ref, repo);
7384 got_ref_close(ref);
7385 return err;
7388 static const struct got_error *
7389 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7391 const struct got_error *err;
7392 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7393 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7395 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7396 if (err)
7397 goto done;
7398 err = delete_ref(tmp_branch_name, repo);
7399 if (err)
7400 goto done;
7402 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7403 if (err)
7404 goto done;
7405 err = delete_ref(new_base_branch_ref_name, repo);
7406 if (err)
7407 goto done;
7409 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7410 if (err)
7411 goto done;
7412 err = delete_ref(branch_ref_name, repo);
7413 if (err)
7414 goto done;
7416 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7417 if (err)
7418 goto done;
7419 err = delete_ref(commit_ref_name, repo);
7420 if (err)
7421 goto done;
7423 done:
7424 free(tmp_branch_name);
7425 free(new_base_branch_ref_name);
7426 free(branch_ref_name);
7427 free(commit_ref_name);
7428 return err;
7431 static const struct got_error *
7432 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7433 struct got_object_id *new_commit_id, struct got_repository *repo)
7435 const struct got_error *err;
7436 struct got_reference *ref = NULL;
7437 struct got_object_id *old_commit_id = NULL;
7438 const char *branch_name = NULL;
7439 char *new_id_str = NULL;
7440 char *refname = NULL;
7442 branch_name = got_ref_get_name(branch);
7443 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7444 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7445 branch_name += 11;
7447 err = got_object_id_str(&new_id_str, new_commit_id);
7448 if (err)
7449 return err;
7451 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7452 new_id_str) == -1) {
7453 err = got_error_from_errno("asprintf");
7454 goto done;
7457 err = got_ref_resolve(&old_commit_id, repo, branch);
7458 if (err)
7459 goto done;
7461 err = got_ref_alloc(&ref, refname, old_commit_id);
7462 if (err)
7463 goto done;
7465 err = got_ref_write(ref, repo);
7466 done:
7467 free(new_id_str);
7468 free(refname);
7469 free(old_commit_id);
7470 if (ref)
7471 got_ref_close(ref);
7472 return err;
7475 const struct got_error *
7476 got_worktree_rebase_complete(struct got_worktree *worktree,
7477 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7478 struct got_reference *rebased_branch, struct got_repository *repo,
7479 int create_backup)
7481 const struct got_error *err, *unlockerr, *sync_err;
7482 struct got_object_id *new_head_commit_id = NULL;
7483 char *fileindex_path = NULL;
7485 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7486 if (err)
7487 return err;
7489 if (create_backup) {
7490 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7491 rebased_branch, new_head_commit_id, repo);
7492 if (err)
7493 goto done;
7496 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7497 if (err)
7498 goto done;
7500 err = got_ref_write(rebased_branch, repo);
7501 if (err)
7502 goto done;
7504 err = got_worktree_set_head_ref(worktree, rebased_branch);
7505 if (err)
7506 goto done;
7508 err = delete_rebase_refs(worktree, repo);
7509 if (err)
7510 goto done;
7512 err = get_fileindex_path(&fileindex_path, worktree);
7513 if (err)
7514 goto done;
7515 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7516 sync_err = sync_fileindex(fileindex, fileindex_path);
7517 if (sync_err && err == NULL)
7518 err = sync_err;
7519 done:
7520 got_fileindex_free(fileindex);
7521 free(fileindex_path);
7522 free(new_head_commit_id);
7523 unlockerr = lock_worktree(worktree, LOCK_SH);
7524 if (unlockerr && err == NULL)
7525 err = unlockerr;
7526 return err;
7529 static const struct got_error *
7530 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7531 struct got_object_id *id1, struct got_object_id *id2,
7532 struct got_repository *repo)
7534 const struct got_error *err;
7535 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7536 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7538 if (id1) {
7539 err = got_object_open_as_commit(&commit1, repo, id1);
7540 if (err)
7541 goto done;
7543 err = got_object_open_as_tree(&tree1, repo,
7544 got_object_commit_get_tree_id(commit1));
7545 if (err)
7546 goto done;
7549 if (id2) {
7550 err = got_object_open_as_commit(&commit2, repo, id2);
7551 if (err)
7552 goto done;
7554 err = got_object_open_as_tree(&tree2, repo,
7555 got_object_commit_get_tree_id(commit2));
7556 if (err)
7557 goto done;
7560 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7561 got_diff_tree_collect_changed_paths, paths, 0);
7562 if (err)
7563 goto done;
7564 done:
7565 if (commit1)
7566 got_object_commit_close(commit1);
7567 if (commit2)
7568 got_object_commit_close(commit2);
7569 if (tree1)
7570 got_object_tree_close(tree1);
7571 if (tree2)
7572 got_object_tree_close(tree2);
7573 return err;
7576 static const struct got_error *
7577 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7578 struct got_object_id *id1, struct got_object_id *id2,
7579 const char *path_prefix, struct got_repository *repo)
7581 const struct got_error *err;
7582 struct got_pathlist_head merged_paths;
7583 struct got_pathlist_entry *pe;
7584 char *abspath = NULL, *wt_path = NULL;
7586 TAILQ_INIT(&merged_paths);
7588 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7589 if (err)
7590 goto done;
7592 TAILQ_FOREACH(pe, &merged_paths, entry) {
7593 struct got_diff_changed_path *change = pe->data;
7595 if (change->status != GOT_STATUS_ADD)
7596 continue;
7598 if (got_path_is_root_dir(path_prefix)) {
7599 wt_path = strdup(pe->path);
7600 if (wt_path == NULL) {
7601 err = got_error_from_errno("strdup");
7602 goto done;
7604 } else {
7605 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7606 err = got_error_from_errno("asprintf");
7607 goto done;
7610 err = got_path_skip_common_ancestor(&wt_path,
7611 path_prefix, abspath);
7612 if (err)
7613 goto done;
7614 free(abspath);
7615 abspath = NULL;
7618 err = got_pathlist_append(added_paths, wt_path, NULL);
7619 if (err)
7620 goto done;
7621 wt_path = NULL;
7624 done:
7625 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7626 free(abspath);
7627 free(wt_path);
7628 return err;
7631 static const struct got_error *
7632 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7633 struct got_object_id *id, const char *path_prefix,
7634 struct got_repository *repo)
7636 const struct got_error *err;
7637 struct got_commit_object *commit = NULL;
7638 struct got_object_qid *pid;
7640 err = got_object_open_as_commit(&commit, repo, id);
7641 if (err)
7642 goto done;
7644 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7646 err = get_paths_added_between_commits(added_paths,
7647 pid ? &pid->id : NULL, id, path_prefix, repo);
7648 if (err)
7649 goto done;
7650 done:
7651 if (commit)
7652 got_object_commit_close(commit);
7653 return err;
7656 const struct got_error *
7657 got_worktree_rebase_abort(struct got_worktree *worktree,
7658 struct got_fileindex *fileindex, struct got_repository *repo,
7659 struct got_reference *new_base_branch,
7660 got_worktree_checkout_cb progress_cb, void *progress_arg)
7662 const struct got_error *err, *unlockerr, *sync_err;
7663 struct got_reference *resolved = NULL;
7664 struct got_object_id *commit_id = NULL;
7665 struct got_object_id *merged_commit_id = NULL;
7666 struct got_commit_object *commit = NULL;
7667 char *fileindex_path = NULL;
7668 char *commit_ref_name = NULL;
7669 struct got_reference *commit_ref = NULL;
7670 struct revert_file_args rfa;
7671 struct got_object_id *tree_id = NULL;
7672 struct got_pathlist_head added_paths;
7674 TAILQ_INIT(&added_paths);
7676 err = lock_worktree(worktree, LOCK_EX);
7677 if (err)
7678 return err;
7680 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7681 if (err)
7682 goto done;
7684 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7685 if (err)
7686 goto done;
7688 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7689 if (err)
7690 goto done;
7693 * Determine which files in added status can be safely removed
7694 * from disk while reverting changes in the work tree.
7695 * We want to avoid deleting unrelated files which were added by
7696 * the user for conflict resolution purposes.
7698 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7699 got_worktree_get_path_prefix(worktree), repo);
7700 if (err)
7701 goto done;
7703 err = got_ref_open(&resolved, repo,
7704 got_ref_get_symref_target(new_base_branch), 0);
7705 if (err)
7706 goto done;
7708 err = got_worktree_set_head_ref(worktree, resolved);
7709 if (err)
7710 goto done;
7713 * XXX commits to the base branch could have happened while
7714 * we were busy rebasing; should we store the original commit ID
7715 * when rebase begins and read it back here?
7717 err = got_ref_resolve(&commit_id, repo, resolved);
7718 if (err)
7719 goto done;
7721 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7722 if (err)
7723 goto done;
7725 err = got_object_open_as_commit(&commit, repo,
7726 worktree->base_commit_id);
7727 if (err)
7728 goto done;
7730 err = got_object_id_by_path(&tree_id, repo, commit,
7731 worktree->path_prefix);
7732 if (err)
7733 goto done;
7735 err = delete_rebase_refs(worktree, repo);
7736 if (err)
7737 goto done;
7739 err = get_fileindex_path(&fileindex_path, worktree);
7740 if (err)
7741 goto done;
7743 rfa.worktree = worktree;
7744 rfa.fileindex = fileindex;
7745 rfa.progress_cb = progress_cb;
7746 rfa.progress_arg = progress_arg;
7747 rfa.patch_cb = NULL;
7748 rfa.patch_arg = NULL;
7749 rfa.repo = repo;
7750 rfa.unlink_added_files = 1;
7751 rfa.added_files_to_unlink = &added_paths;
7752 err = worktree_status(worktree, "", fileindex, repo,
7753 revert_file, &rfa, NULL, NULL, 1, 0);
7754 if (err)
7755 goto sync;
7757 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7758 repo, progress_cb, progress_arg, NULL, NULL);
7759 sync:
7760 sync_err = sync_fileindex(fileindex, fileindex_path);
7761 if (sync_err && err == NULL)
7762 err = sync_err;
7763 done:
7764 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7765 got_ref_close(resolved);
7766 free(tree_id);
7767 free(commit_id);
7768 free(merged_commit_id);
7769 if (commit)
7770 got_object_commit_close(commit);
7771 if (fileindex)
7772 got_fileindex_free(fileindex);
7773 free(fileindex_path);
7774 free(commit_ref_name);
7775 if (commit_ref)
7776 got_ref_close(commit_ref);
7778 unlockerr = lock_worktree(worktree, LOCK_SH);
7779 if (unlockerr && err == NULL)
7780 err = unlockerr;
7781 return err;
7784 const struct got_error *
7785 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7786 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7787 struct got_fileindex **fileindex, struct got_worktree *worktree,
7788 struct got_repository *repo)
7790 const struct got_error *err = NULL;
7791 char *tmp_branch_name = NULL;
7792 char *branch_ref_name = NULL;
7793 char *base_commit_ref_name = NULL;
7794 char *fileindex_path = NULL;
7795 struct check_rebase_ok_arg ok_arg;
7796 struct got_reference *wt_branch = NULL;
7797 struct got_reference *base_commit_ref = NULL;
7799 *tmp_branch = NULL;
7800 *branch_ref = NULL;
7801 *base_commit_id = NULL;
7802 *fileindex = NULL;
7804 err = lock_worktree(worktree, LOCK_EX);
7805 if (err)
7806 return err;
7808 err = open_fileindex(fileindex, &fileindex_path, worktree);
7809 if (err)
7810 goto done;
7812 ok_arg.worktree = worktree;
7813 ok_arg.repo = repo;
7814 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7815 &ok_arg);
7816 if (err)
7817 goto done;
7819 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7820 if (err)
7821 goto done;
7823 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7824 if (err)
7825 goto done;
7827 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7828 worktree);
7829 if (err)
7830 goto done;
7832 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7833 0);
7834 if (err)
7835 goto done;
7837 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7838 if (err)
7839 goto done;
7841 err = got_ref_write(*branch_ref, repo);
7842 if (err)
7843 goto done;
7845 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7846 worktree->base_commit_id);
7847 if (err)
7848 goto done;
7849 err = got_ref_write(base_commit_ref, repo);
7850 if (err)
7851 goto done;
7852 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7853 if (*base_commit_id == NULL) {
7854 err = got_error_from_errno("got_object_id_dup");
7855 goto done;
7858 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7859 worktree->base_commit_id);
7860 if (err)
7861 goto done;
7862 err = got_ref_write(*tmp_branch, repo);
7863 if (err)
7864 goto done;
7866 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7867 if (err)
7868 goto done;
7869 done:
7870 free(fileindex_path);
7871 free(tmp_branch_name);
7872 free(branch_ref_name);
7873 free(base_commit_ref_name);
7874 if (wt_branch)
7875 got_ref_close(wt_branch);
7876 if (err) {
7877 if (*branch_ref) {
7878 got_ref_close(*branch_ref);
7879 *branch_ref = NULL;
7881 if (*tmp_branch) {
7882 got_ref_close(*tmp_branch);
7883 *tmp_branch = NULL;
7885 free(*base_commit_id);
7886 if (*fileindex) {
7887 got_fileindex_free(*fileindex);
7888 *fileindex = NULL;
7890 lock_worktree(worktree, LOCK_SH);
7892 return err;
7895 const struct got_error *
7896 got_worktree_histedit_postpone(struct got_worktree *worktree,
7897 struct got_fileindex *fileindex)
7899 if (fileindex)
7900 got_fileindex_free(fileindex);
7901 return lock_worktree(worktree, LOCK_SH);
7904 const struct got_error *
7905 got_worktree_histedit_in_progress(int *in_progress,
7906 struct got_worktree *worktree)
7908 const struct got_error *err;
7909 char *tmp_branch_name = NULL;
7911 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7912 if (err)
7913 return err;
7915 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7916 free(tmp_branch_name);
7917 return NULL;
7920 const struct got_error *
7921 got_worktree_histedit_continue(struct got_object_id **commit_id,
7922 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7923 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7924 struct got_worktree *worktree, struct got_repository *repo)
7926 const struct got_error *err;
7927 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7928 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7929 struct got_reference *commit_ref = NULL;
7930 struct got_reference *base_commit_ref = NULL;
7931 char *fileindex_path = NULL;
7932 int have_staged_files = 0;
7934 *commit_id = NULL;
7935 *tmp_branch = NULL;
7936 *base_commit_id = NULL;
7937 *fileindex = NULL;
7939 err = lock_worktree(worktree, LOCK_EX);
7940 if (err)
7941 return err;
7943 err = open_fileindex(fileindex, &fileindex_path, worktree);
7944 if (err)
7945 goto done;
7947 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7948 &have_staged_files);
7949 if (err && err->code != GOT_ERR_CANCELLED)
7950 goto done;
7951 if (have_staged_files) {
7952 err = got_error(GOT_ERR_STAGED_PATHS);
7953 goto done;
7956 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7957 if (err)
7958 goto done;
7960 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7961 if (err)
7962 goto done;
7964 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7965 if (err)
7966 goto done;
7968 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7969 worktree);
7970 if (err)
7971 goto done;
7973 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7974 if (err)
7975 goto done;
7977 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7978 if (err)
7979 goto done;
7980 err = got_ref_resolve(commit_id, repo, commit_ref);
7981 if (err)
7982 goto done;
7984 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7985 if (err)
7986 goto done;
7987 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7988 if (err)
7989 goto done;
7991 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7992 if (err)
7993 goto done;
7994 done:
7995 free(commit_ref_name);
7996 free(branch_ref_name);
7997 free(fileindex_path);
7998 if (commit_ref)
7999 got_ref_close(commit_ref);
8000 if (base_commit_ref)
8001 got_ref_close(base_commit_ref);
8002 if (err) {
8003 free(*commit_id);
8004 *commit_id = NULL;
8005 free(*base_commit_id);
8006 *base_commit_id = NULL;
8007 if (*tmp_branch) {
8008 got_ref_close(*tmp_branch);
8009 *tmp_branch = NULL;
8011 if (*fileindex) {
8012 got_fileindex_free(*fileindex);
8013 *fileindex = NULL;
8015 lock_worktree(worktree, LOCK_EX);
8017 return err;
8020 static const struct got_error *
8021 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8023 const struct got_error *err;
8024 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8025 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8027 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8028 if (err)
8029 goto done;
8030 err = delete_ref(tmp_branch_name, repo);
8031 if (err)
8032 goto done;
8034 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8035 worktree);
8036 if (err)
8037 goto done;
8038 err = delete_ref(base_commit_ref_name, repo);
8039 if (err)
8040 goto done;
8042 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8043 if (err)
8044 goto done;
8045 err = delete_ref(branch_ref_name, repo);
8046 if (err)
8047 goto done;
8049 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8050 if (err)
8051 goto done;
8052 err = delete_ref(commit_ref_name, repo);
8053 if (err)
8054 goto done;
8055 done:
8056 free(tmp_branch_name);
8057 free(base_commit_ref_name);
8058 free(branch_ref_name);
8059 free(commit_ref_name);
8060 return err;
8063 const struct got_error *
8064 got_worktree_histedit_abort(struct got_worktree *worktree,
8065 struct got_fileindex *fileindex, struct got_repository *repo,
8066 struct got_reference *branch, struct got_object_id *base_commit_id,
8067 got_worktree_checkout_cb progress_cb, void *progress_arg)
8069 const struct got_error *err, *unlockerr, *sync_err;
8070 struct got_reference *resolved = NULL;
8071 char *fileindex_path = NULL;
8072 struct got_object_id *merged_commit_id = NULL;
8073 struct got_commit_object *commit = NULL;
8074 char *commit_ref_name = NULL;
8075 struct got_reference *commit_ref = NULL;
8076 struct got_object_id *tree_id = NULL;
8077 struct revert_file_args rfa;
8078 struct got_pathlist_head added_paths;
8080 TAILQ_INIT(&added_paths);
8082 err = lock_worktree(worktree, LOCK_EX);
8083 if (err)
8084 return err;
8086 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8087 if (err)
8088 goto done;
8090 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8091 if (err) {
8092 if (err->code != GOT_ERR_NOT_REF)
8093 goto done;
8094 /* Can happen on early abort due to invalid histedit script. */
8095 commit_ref = NULL;
8098 if (commit_ref) {
8099 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8100 if (err)
8101 goto done;
8104 * Determine which files in added status can be safely removed
8105 * from disk while reverting changes in the work tree.
8106 * We want to avoid deleting unrelated files added by the
8107 * user during conflict resolution or during histedit -e.
8109 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8110 got_worktree_get_path_prefix(worktree), repo);
8111 if (err)
8112 goto done;
8115 err = got_ref_open(&resolved, repo,
8116 got_ref_get_symref_target(branch), 0);
8117 if (err)
8118 goto done;
8120 err = got_worktree_set_head_ref(worktree, resolved);
8121 if (err)
8122 goto done;
8124 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8125 if (err)
8126 goto done;
8128 err = got_object_open_as_commit(&commit, repo,
8129 worktree->base_commit_id);
8130 if (err)
8131 goto done;
8133 err = got_object_id_by_path(&tree_id, repo, commit,
8134 worktree->path_prefix);
8135 if (err)
8136 goto done;
8138 err = delete_histedit_refs(worktree, repo);
8139 if (err)
8140 goto done;
8142 err = get_fileindex_path(&fileindex_path, worktree);
8143 if (err)
8144 goto done;
8146 rfa.worktree = worktree;
8147 rfa.fileindex = fileindex;
8148 rfa.progress_cb = progress_cb;
8149 rfa.progress_arg = progress_arg;
8150 rfa.patch_cb = NULL;
8151 rfa.patch_arg = NULL;
8152 rfa.repo = repo;
8153 rfa.unlink_added_files = 1;
8154 rfa.added_files_to_unlink = &added_paths;
8155 err = worktree_status(worktree, "", fileindex, repo,
8156 revert_file, &rfa, NULL, NULL, 1, 0);
8157 if (err)
8158 goto sync;
8160 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8161 repo, progress_cb, progress_arg, NULL, NULL);
8162 sync:
8163 sync_err = sync_fileindex(fileindex, fileindex_path);
8164 if (sync_err && err == NULL)
8165 err = sync_err;
8166 done:
8167 if (resolved)
8168 got_ref_close(resolved);
8169 if (commit_ref)
8170 got_ref_close(commit_ref);
8171 free(merged_commit_id);
8172 free(tree_id);
8173 free(fileindex_path);
8174 free(commit_ref_name);
8176 unlockerr = lock_worktree(worktree, LOCK_SH);
8177 if (unlockerr && err == NULL)
8178 err = unlockerr;
8179 return err;
8182 const struct got_error *
8183 got_worktree_histedit_complete(struct got_worktree *worktree,
8184 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8185 struct got_reference *edited_branch, struct got_repository *repo)
8187 const struct got_error *err, *unlockerr, *sync_err;
8188 struct got_object_id *new_head_commit_id = NULL;
8189 struct got_reference *resolved = NULL;
8190 char *fileindex_path = NULL;
8192 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8193 if (err)
8194 return err;
8196 err = got_ref_open(&resolved, repo,
8197 got_ref_get_symref_target(edited_branch), 0);
8198 if (err)
8199 goto done;
8201 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8202 resolved, new_head_commit_id, repo);
8203 if (err)
8204 goto done;
8206 err = got_ref_change_ref(resolved, new_head_commit_id);
8207 if (err)
8208 goto done;
8210 err = got_ref_write(resolved, repo);
8211 if (err)
8212 goto done;
8214 err = got_worktree_set_head_ref(worktree, resolved);
8215 if (err)
8216 goto done;
8218 err = delete_histedit_refs(worktree, repo);
8219 if (err)
8220 goto done;
8222 err = get_fileindex_path(&fileindex_path, worktree);
8223 if (err)
8224 goto done;
8225 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8226 sync_err = sync_fileindex(fileindex, fileindex_path);
8227 if (sync_err && err == NULL)
8228 err = sync_err;
8229 done:
8230 got_fileindex_free(fileindex);
8231 free(fileindex_path);
8232 free(new_head_commit_id);
8233 unlockerr = lock_worktree(worktree, LOCK_SH);
8234 if (unlockerr && err == NULL)
8235 err = unlockerr;
8236 return err;
8239 const struct got_error *
8240 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8241 struct got_object_id *commit_id, struct got_repository *repo)
8243 const struct got_error *err;
8244 char *commit_ref_name;
8246 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8247 if (err)
8248 return err;
8250 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8251 if (err)
8252 goto done;
8254 err = delete_ref(commit_ref_name, repo);
8255 done:
8256 free(commit_ref_name);
8257 return err;
8260 const struct got_error *
8261 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8262 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8263 struct got_worktree *worktree, const char *refname,
8264 struct got_repository *repo)
8266 const struct got_error *err = NULL;
8267 char *fileindex_path = NULL;
8268 struct check_rebase_ok_arg ok_arg;
8270 *fileindex = NULL;
8271 *branch_ref = NULL;
8272 *base_branch_ref = NULL;
8274 err = lock_worktree(worktree, LOCK_EX);
8275 if (err)
8276 return err;
8278 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8279 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8280 "cannot integrate a branch into itself; "
8281 "update -b or different branch name required");
8282 goto done;
8285 err = open_fileindex(fileindex, &fileindex_path, worktree);
8286 if (err)
8287 goto done;
8289 /* Preconditions are the same as for rebase. */
8290 ok_arg.worktree = worktree;
8291 ok_arg.repo = repo;
8292 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8293 &ok_arg);
8294 if (err)
8295 goto done;
8297 err = got_ref_open(branch_ref, repo, refname, 1);
8298 if (err)
8299 goto done;
8301 err = got_ref_open(base_branch_ref, repo,
8302 got_worktree_get_head_ref_name(worktree), 1);
8303 done:
8304 if (err) {
8305 if (*branch_ref) {
8306 got_ref_close(*branch_ref);
8307 *branch_ref = NULL;
8309 if (*base_branch_ref) {
8310 got_ref_close(*base_branch_ref);
8311 *base_branch_ref = NULL;
8313 if (*fileindex) {
8314 got_fileindex_free(*fileindex);
8315 *fileindex = NULL;
8317 lock_worktree(worktree, LOCK_SH);
8319 return err;
8322 const struct got_error *
8323 got_worktree_integrate_continue(struct got_worktree *worktree,
8324 struct got_fileindex *fileindex, struct got_repository *repo,
8325 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8326 got_worktree_checkout_cb progress_cb, void *progress_arg,
8327 got_cancel_cb cancel_cb, void *cancel_arg)
8329 const struct got_error *err = NULL, *sync_err, *unlockerr;
8330 char *fileindex_path = NULL;
8331 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8332 struct got_commit_object *commit = NULL;
8334 err = get_fileindex_path(&fileindex_path, worktree);
8335 if (err)
8336 goto done;
8338 err = got_ref_resolve(&commit_id, repo, branch_ref);
8339 if (err)
8340 goto done;
8342 err = got_object_open_as_commit(&commit, repo, commit_id);
8343 if (err)
8344 goto done;
8346 err = got_object_id_by_path(&tree_id, repo, commit,
8347 worktree->path_prefix);
8348 if (err)
8349 goto done;
8351 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8352 if (err)
8353 goto done;
8355 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8356 progress_cb, progress_arg, cancel_cb, cancel_arg);
8357 if (err)
8358 goto sync;
8360 err = got_ref_change_ref(base_branch_ref, commit_id);
8361 if (err)
8362 goto sync;
8364 err = got_ref_write(base_branch_ref, repo);
8365 if (err)
8366 goto sync;
8368 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8369 sync:
8370 sync_err = sync_fileindex(fileindex, fileindex_path);
8371 if (sync_err && err == NULL)
8372 err = sync_err;
8374 done:
8375 unlockerr = got_ref_unlock(branch_ref);
8376 if (unlockerr && err == NULL)
8377 err = unlockerr;
8378 got_ref_close(branch_ref);
8380 unlockerr = got_ref_unlock(base_branch_ref);
8381 if (unlockerr && err == NULL)
8382 err = unlockerr;
8383 got_ref_close(base_branch_ref);
8385 got_fileindex_free(fileindex);
8386 free(fileindex_path);
8387 free(tree_id);
8388 if (commit)
8389 got_object_commit_close(commit);
8391 unlockerr = lock_worktree(worktree, LOCK_SH);
8392 if (unlockerr && err == NULL)
8393 err = unlockerr;
8394 return err;
8397 const struct got_error *
8398 got_worktree_integrate_abort(struct got_worktree *worktree,
8399 struct got_fileindex *fileindex, struct got_repository *repo,
8400 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8402 const struct got_error *err = NULL, *unlockerr = NULL;
8404 got_fileindex_free(fileindex);
8406 err = lock_worktree(worktree, LOCK_SH);
8408 unlockerr = got_ref_unlock(branch_ref);
8409 if (unlockerr && err == NULL)
8410 err = unlockerr;
8411 got_ref_close(branch_ref);
8413 unlockerr = got_ref_unlock(base_branch_ref);
8414 if (unlockerr && err == NULL)
8415 err = unlockerr;
8416 got_ref_close(base_branch_ref);
8418 return err;
8421 const struct got_error *
8422 got_worktree_merge_postpone(struct got_worktree *worktree,
8423 struct got_fileindex *fileindex)
8425 const struct got_error *err, *sync_err;
8426 char *fileindex_path = NULL;
8428 err = get_fileindex_path(&fileindex_path, worktree);
8429 if (err)
8430 goto done;
8432 sync_err = sync_fileindex(fileindex, fileindex_path);
8434 err = lock_worktree(worktree, LOCK_SH);
8435 if (sync_err && err == NULL)
8436 err = sync_err;
8437 done:
8438 got_fileindex_free(fileindex);
8439 free(fileindex_path);
8440 return err;
8443 static const struct got_error *
8444 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8446 const struct got_error *err;
8447 char *branch_refname = NULL, *commit_refname = NULL;
8449 err = get_merge_branch_ref_name(&branch_refname, worktree);
8450 if (err)
8451 goto done;
8452 err = delete_ref(branch_refname, repo);
8453 if (err)
8454 goto done;
8456 err = get_merge_commit_ref_name(&commit_refname, worktree);
8457 if (err)
8458 goto done;
8459 err = delete_ref(commit_refname, repo);
8460 if (err)
8461 goto done;
8463 done:
8464 free(branch_refname);
8465 free(commit_refname);
8466 return err;
8469 struct merge_commit_msg_arg {
8470 struct got_worktree *worktree;
8471 const char *branch_name;
8474 static const struct got_error *
8475 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8476 const char *diff_path, char **logmsg, void *arg)
8478 struct merge_commit_msg_arg *a = arg;
8480 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8481 got_worktree_get_head_ref_name(a->worktree)) == -1)
8482 return got_error_from_errno("asprintf");
8484 return NULL;
8488 const struct got_error *
8489 got_worktree_merge_branch(struct got_worktree *worktree,
8490 struct got_fileindex *fileindex,
8491 struct got_object_id *yca_commit_id,
8492 struct got_object_id *branch_tip,
8493 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8494 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8496 const struct got_error *err;
8497 char *fileindex_path = NULL;
8498 struct check_mixed_commits_args cma;
8500 err = get_fileindex_path(&fileindex_path, worktree);
8501 if (err)
8502 goto done;
8504 cma.worktree = worktree;
8505 cma.cancel_cb = cancel_cb;
8506 cma.cancel_arg = cancel_arg;
8508 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8509 &cma);
8510 if (err)
8511 goto done;
8513 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8514 branch_tip, repo, progress_cb, progress_arg,
8515 cancel_cb, cancel_arg);
8516 done:
8517 free(fileindex_path);
8518 return err;
8521 const struct got_error *
8522 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8523 struct got_worktree *worktree, struct got_fileindex *fileindex,
8524 const char *author, const char *committer, int allow_bad_symlinks,
8525 struct got_object_id *branch_tip, const char *branch_name,
8526 int allow_conflict, struct got_repository *repo,
8527 got_worktree_status_cb status_cb, void *status_arg)
8530 const struct got_error *err = NULL, *sync_err;
8531 struct got_pathlist_head commitable_paths;
8532 struct collect_commitables_arg cc_arg;
8533 struct got_pathlist_entry *pe;
8534 struct got_reference *head_ref = NULL;
8535 struct got_object_id *head_commit_id = NULL;
8536 int have_staged_files = 0;
8537 struct merge_commit_msg_arg mcm_arg;
8538 char *fileindex_path = NULL;
8540 memset(&cc_arg, 0, sizeof(cc_arg));
8541 *new_commit_id = NULL;
8543 TAILQ_INIT(&commitable_paths);
8545 err = get_fileindex_path(&fileindex_path, worktree);
8546 if (err)
8547 goto done;
8549 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8550 if (err)
8551 goto done;
8553 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8554 if (err)
8555 goto done;
8557 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8558 &have_staged_files);
8559 if (err && err->code != GOT_ERR_CANCELLED)
8560 goto done;
8561 if (have_staged_files) {
8562 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8563 goto done;
8566 cc_arg.commitable_paths = &commitable_paths;
8567 cc_arg.worktree = worktree;
8568 cc_arg.fileindex = fileindex;
8569 cc_arg.repo = repo;
8570 cc_arg.have_staged_files = have_staged_files;
8571 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8572 cc_arg.commit_conflicts = allow_conflict;
8573 err = worktree_status(worktree, "", fileindex, repo,
8574 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8575 if (err)
8576 goto done;
8578 mcm_arg.worktree = worktree;
8579 mcm_arg.branch_name = branch_name;
8580 err = commit_worktree(new_commit_id, &commitable_paths,
8581 head_commit_id, branch_tip, worktree, author, committer, NULL,
8582 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8583 if (err)
8584 goto done;
8586 err = update_fileindex_after_commit(worktree, &commitable_paths,
8587 *new_commit_id, fileindex, have_staged_files);
8588 sync_err = sync_fileindex(fileindex, fileindex_path);
8589 if (sync_err && err == NULL)
8590 err = sync_err;
8591 done:
8592 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8593 struct got_commitable *ct = pe->data;
8595 free_commitable(ct);
8597 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8598 free(fileindex_path);
8599 return err;
8602 const struct got_error *
8603 got_worktree_merge_complete(struct got_worktree *worktree,
8604 struct got_fileindex *fileindex, struct got_repository *repo)
8606 const struct got_error *err, *unlockerr, *sync_err;
8607 char *fileindex_path = NULL;
8609 err = delete_merge_refs(worktree, repo);
8610 if (err)
8611 goto done;
8613 err = get_fileindex_path(&fileindex_path, worktree);
8614 if (err)
8615 goto done;
8616 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8617 sync_err = sync_fileindex(fileindex, fileindex_path);
8618 if (sync_err && err == NULL)
8619 err = sync_err;
8620 done:
8621 got_fileindex_free(fileindex);
8622 free(fileindex_path);
8623 unlockerr = lock_worktree(worktree, LOCK_SH);
8624 if (unlockerr && err == NULL)
8625 err = unlockerr;
8626 return err;
8629 const struct got_error *
8630 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8631 struct got_repository *repo)
8633 const struct got_error *err;
8634 char *branch_refname = NULL;
8635 struct got_reference *branch_ref = NULL;
8637 *in_progress = 0;
8639 err = get_merge_branch_ref_name(&branch_refname, worktree);
8640 if (err)
8641 return err;
8642 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8643 free(branch_refname);
8644 if (err) {
8645 if (err->code != GOT_ERR_NOT_REF)
8646 return err;
8647 } else
8648 *in_progress = 1;
8650 return NULL;
8653 const struct got_error *got_worktree_merge_prepare(
8654 struct got_fileindex **fileindex, struct got_worktree *worktree,
8655 struct got_repository *repo)
8657 const struct got_error *err = NULL;
8658 char *fileindex_path = NULL;
8659 struct got_reference *wt_branch = NULL;
8660 struct got_object_id *wt_branch_tip = NULL;
8661 struct check_rebase_ok_arg ok_arg;
8663 *fileindex = NULL;
8665 err = lock_worktree(worktree, LOCK_EX);
8666 if (err)
8667 return err;
8669 err = open_fileindex(fileindex, &fileindex_path, worktree);
8670 if (err)
8671 goto done;
8673 /* Preconditions are the same as for rebase. */
8674 ok_arg.worktree = worktree;
8675 ok_arg.repo = repo;
8676 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8677 &ok_arg);
8678 if (err)
8679 goto done;
8681 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8682 0);
8683 if (err)
8684 goto done;
8686 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8687 if (err)
8688 goto done;
8690 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8691 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8692 goto done;
8695 done:
8696 free(fileindex_path);
8697 if (wt_branch)
8698 got_ref_close(wt_branch);
8699 free(wt_branch_tip);
8700 if (err) {
8701 if (*fileindex) {
8702 got_fileindex_free(*fileindex);
8703 *fileindex = NULL;
8705 lock_worktree(worktree, LOCK_SH);
8707 return err;
8710 const struct got_error *got_worktree_merge_write_refs(
8711 struct got_worktree *worktree, struct got_reference *branch,
8712 struct got_repository *repo)
8714 const struct got_error *err = NULL;
8715 char *branch_refname = NULL, *commit_refname = NULL;
8716 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8717 struct got_object_id *branch_tip = NULL;
8719 err = get_merge_branch_ref_name(&branch_refname, worktree);
8720 if (err)
8721 return err;
8723 err = get_merge_commit_ref_name(&commit_refname, worktree);
8724 if (err)
8725 return err;
8727 err = got_ref_resolve(&branch_tip, repo, branch);
8728 if (err)
8729 goto done;
8731 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8732 if (err)
8733 goto done;
8734 err = got_ref_write(branch_ref, repo);
8735 if (err)
8736 goto done;
8738 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8739 if (err)
8740 goto done;
8741 err = got_ref_write(commit_ref, repo);
8742 if (err)
8743 goto done;
8745 done:
8746 free(branch_refname);
8747 free(commit_refname);
8748 if (branch_ref)
8749 got_ref_close(branch_ref);
8750 if (commit_ref)
8751 got_ref_close(commit_ref);
8752 free(branch_tip);
8753 return err;
8756 const struct got_error *
8757 got_worktree_merge_continue(char **branch_name,
8758 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8759 struct got_worktree *worktree, struct got_repository *repo)
8761 const struct got_error *err;
8762 char *commit_refname = NULL, *branch_refname = NULL;
8763 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8764 char *fileindex_path = NULL;
8765 int have_staged_files = 0;
8767 *branch_name = NULL;
8768 *branch_tip = NULL;
8769 *fileindex = NULL;
8771 err = lock_worktree(worktree, LOCK_EX);
8772 if (err)
8773 return err;
8775 err = open_fileindex(fileindex, &fileindex_path, worktree);
8776 if (err)
8777 goto done;
8779 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8780 &have_staged_files);
8781 if (err && err->code != GOT_ERR_CANCELLED)
8782 goto done;
8783 if (have_staged_files) {
8784 err = got_error(GOT_ERR_STAGED_PATHS);
8785 goto done;
8788 err = get_merge_branch_ref_name(&branch_refname, worktree);
8789 if (err)
8790 goto done;
8792 err = get_merge_commit_ref_name(&commit_refname, worktree);
8793 if (err)
8794 goto done;
8796 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8797 if (err)
8798 goto done;
8800 if (!got_ref_is_symbolic(branch_ref)) {
8801 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8802 "%s is not a symbolic reference",
8803 got_ref_get_name(branch_ref));
8804 goto done;
8806 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8807 if (*branch_name == NULL) {
8808 err = got_error_from_errno("strdup");
8809 goto done;
8812 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8813 if (err)
8814 goto done;
8816 err = got_ref_resolve(branch_tip, repo, commit_ref);
8817 if (err)
8818 goto done;
8819 done:
8820 free(commit_refname);
8821 free(branch_refname);
8822 free(fileindex_path);
8823 if (commit_ref)
8824 got_ref_close(commit_ref);
8825 if (branch_ref)
8826 got_ref_close(branch_ref);
8827 if (err) {
8828 if (*branch_name) {
8829 free(*branch_name);
8830 *branch_name = NULL;
8832 free(*branch_tip);
8833 *branch_tip = NULL;
8834 if (*fileindex) {
8835 got_fileindex_free(*fileindex);
8836 *fileindex = NULL;
8838 lock_worktree(worktree, LOCK_SH);
8840 return err;
8843 const struct got_error *
8844 got_worktree_merge_abort(struct got_worktree *worktree,
8845 struct got_fileindex *fileindex, struct got_repository *repo,
8846 got_worktree_checkout_cb progress_cb, void *progress_arg)
8848 const struct got_error *err, *unlockerr, *sync_err;
8849 struct got_commit_object *commit = NULL;
8850 char *fileindex_path = NULL;
8851 struct revert_file_args rfa;
8852 char *commit_ref_name = NULL;
8853 struct got_reference *commit_ref = NULL;
8854 struct got_object_id *merged_commit_id = NULL;
8855 struct got_object_id *tree_id = NULL;
8856 struct got_pathlist_head added_paths;
8858 TAILQ_INIT(&added_paths);
8860 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8861 if (err)
8862 goto done;
8864 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8865 if (err)
8866 goto done;
8868 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8869 if (err)
8870 goto done;
8873 * Determine which files in added status can be safely removed
8874 * from disk while reverting changes in the work tree.
8875 * We want to avoid deleting unrelated files which were added by
8876 * the user for conflict resolution purposes.
8878 err = get_paths_added_between_commits(&added_paths,
8879 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8880 got_worktree_get_path_prefix(worktree), repo);
8881 if (err)
8882 goto done;
8885 err = got_object_open_as_commit(&commit, repo,
8886 worktree->base_commit_id);
8887 if (err)
8888 goto done;
8890 err = got_object_id_by_path(&tree_id, repo, commit,
8891 worktree->path_prefix);
8892 if (err)
8893 goto done;
8895 err = delete_merge_refs(worktree, repo);
8896 if (err)
8897 goto done;
8899 err = get_fileindex_path(&fileindex_path, worktree);
8900 if (err)
8901 goto done;
8903 rfa.worktree = worktree;
8904 rfa.fileindex = fileindex;
8905 rfa.progress_cb = progress_cb;
8906 rfa.progress_arg = progress_arg;
8907 rfa.patch_cb = NULL;
8908 rfa.patch_arg = NULL;
8909 rfa.repo = repo;
8910 rfa.unlink_added_files = 1;
8911 rfa.added_files_to_unlink = &added_paths;
8912 err = worktree_status(worktree, "", fileindex, repo,
8913 revert_file, &rfa, NULL, NULL, 1, 0);
8914 if (err)
8915 goto sync;
8917 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8918 repo, progress_cb, progress_arg, NULL, NULL);
8919 sync:
8920 sync_err = sync_fileindex(fileindex, fileindex_path);
8921 if (sync_err && err == NULL)
8922 err = sync_err;
8923 done:
8924 free(tree_id);
8925 free(merged_commit_id);
8926 if (commit)
8927 got_object_commit_close(commit);
8928 if (fileindex)
8929 got_fileindex_free(fileindex);
8930 free(fileindex_path);
8931 if (commit_ref)
8932 got_ref_close(commit_ref);
8933 free(commit_ref_name);
8935 unlockerr = lock_worktree(worktree, LOCK_SH);
8936 if (unlockerr && err == NULL)
8937 err = unlockerr;
8938 return err;
8941 struct check_stage_ok_arg {
8942 struct got_object_id *head_commit_id;
8943 struct got_worktree *worktree;
8944 struct got_fileindex *fileindex;
8945 struct got_repository *repo;
8946 int have_changes;
8949 static const struct got_error *
8950 check_stage_ok(void *arg, unsigned char status,
8951 unsigned char staged_status, const char *relpath,
8952 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8953 struct got_object_id *commit_id, int dirfd, const char *de_name)
8955 struct check_stage_ok_arg *a = arg;
8956 const struct got_error *err = NULL;
8957 struct got_fileindex_entry *ie;
8958 struct got_object_id base_commit_id;
8959 struct got_object_id *base_commit_idp = NULL;
8960 char *in_repo_path = NULL, *p;
8962 if (status == GOT_STATUS_UNVERSIONED ||
8963 status == GOT_STATUS_NO_CHANGE)
8964 return NULL;
8965 if (status == GOT_STATUS_NONEXISTENT)
8966 return got_error_set_errno(ENOENT, relpath);
8968 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8969 if (ie == NULL)
8970 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8972 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8973 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8974 relpath) == -1)
8975 return got_error_from_errno("asprintf");
8977 if (got_fileindex_entry_has_commit(ie)) {
8978 base_commit_idp = got_fileindex_entry_get_commit_id(
8979 &base_commit_id, ie);
8982 if (status == GOT_STATUS_CONFLICT) {
8983 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8984 goto done;
8985 } else if (status != GOT_STATUS_ADD &&
8986 status != GOT_STATUS_MODIFY &&
8987 status != GOT_STATUS_DELETE) {
8988 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8989 goto done;
8992 a->have_changes = 1;
8994 p = in_repo_path;
8995 while (p[0] == '/')
8996 p++;
8997 err = check_out_of_date(p, status, staged_status,
8998 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8999 GOT_ERR_STAGE_OUT_OF_DATE);
9000 done:
9001 free(in_repo_path);
9002 return err;
9005 struct stage_path_arg {
9006 struct got_worktree *worktree;
9007 struct got_fileindex *fileindex;
9008 struct got_repository *repo;
9009 got_worktree_status_cb status_cb;
9010 void *status_arg;
9011 got_worktree_patch_cb patch_cb;
9012 void *patch_arg;
9013 int staged_something;
9014 int allow_bad_symlinks;
9017 static const struct got_error *
9018 stage_path(void *arg, unsigned char status,
9019 unsigned char staged_status, const char *relpath,
9020 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9021 struct got_object_id *commit_id, int dirfd, const char *de_name)
9023 struct stage_path_arg *a = arg;
9024 const struct got_error *err = NULL;
9025 struct got_fileindex_entry *ie;
9026 char *ondisk_path = NULL, *path_content = NULL;
9027 uint32_t stage;
9028 struct got_object_id *new_staged_blob_id = NULL;
9029 struct stat sb;
9031 if (status == GOT_STATUS_UNVERSIONED)
9032 return NULL;
9034 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9035 if (ie == NULL)
9036 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9038 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9039 relpath)== -1)
9040 return got_error_from_errno("asprintf");
9042 switch (status) {
9043 case GOT_STATUS_ADD:
9044 case GOT_STATUS_MODIFY:
9045 /* XXX could sb.st_mode be passed in by our caller? */
9046 if (lstat(ondisk_path, &sb) == -1) {
9047 err = got_error_from_errno2("lstat", ondisk_path);
9048 break;
9050 if (a->patch_cb) {
9051 if (status == GOT_STATUS_ADD) {
9052 int choice = GOT_PATCH_CHOICE_NONE;
9053 err = (*a->patch_cb)(&choice, a->patch_arg,
9054 status, ie->path, NULL, 1, 1);
9055 if (err)
9056 break;
9057 if (choice != GOT_PATCH_CHOICE_YES)
9058 break;
9059 } else {
9060 err = create_patched_content(&path_content, 0,
9061 staged_blob_id ? staged_blob_id : blob_id,
9062 ondisk_path, dirfd, de_name, ie->path,
9063 a->repo, a->patch_cb, a->patch_arg);
9064 if (err || path_content == NULL)
9065 break;
9068 err = got_object_blob_create(&new_staged_blob_id,
9069 path_content ? path_content : ondisk_path, a->repo);
9070 if (err)
9071 break;
9072 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9073 SHA1_DIGEST_LENGTH);
9074 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9075 stage = GOT_FILEIDX_STAGE_ADD;
9076 else
9077 stage = GOT_FILEIDX_STAGE_MODIFY;
9078 got_fileindex_entry_stage_set(ie, stage);
9079 if (S_ISLNK(sb.st_mode)) {
9080 int is_bad_symlink = 0;
9081 if (!a->allow_bad_symlinks) {
9082 char target_path[PATH_MAX];
9083 ssize_t target_len;
9084 target_len = readlink(ondisk_path, target_path,
9085 sizeof(target_path));
9086 if (target_len == -1) {
9087 err = got_error_from_errno2("readlink",
9088 ondisk_path);
9089 break;
9091 err = is_bad_symlink_target(&is_bad_symlink,
9092 target_path, target_len, ondisk_path,
9093 a->worktree->root_path,
9094 a->worktree->meta_dir);
9095 if (err)
9096 break;
9097 if (is_bad_symlink) {
9098 err = got_error_path(ondisk_path,
9099 GOT_ERR_BAD_SYMLINK);
9100 break;
9103 if (is_bad_symlink)
9104 got_fileindex_entry_staged_filetype_set(ie,
9105 GOT_FILEIDX_MODE_BAD_SYMLINK);
9106 else
9107 got_fileindex_entry_staged_filetype_set(ie,
9108 GOT_FILEIDX_MODE_SYMLINK);
9109 } else {
9110 got_fileindex_entry_staged_filetype_set(ie,
9111 GOT_FILEIDX_MODE_REGULAR_FILE);
9113 a->staged_something = 1;
9114 if (a->status_cb == NULL)
9115 break;
9116 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9117 get_staged_status(ie), relpath, blob_id,
9118 new_staged_blob_id, NULL, dirfd, de_name);
9119 if (err)
9120 break;
9122 * When staging the reverse of the staged diff,
9123 * implicitly unstage the file.
9125 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9126 sizeof(ie->blob_sha1)) == 0) {
9127 got_fileindex_entry_stage_set(ie,
9128 GOT_FILEIDX_STAGE_NONE);
9130 break;
9131 case GOT_STATUS_DELETE:
9132 if (staged_status == GOT_STATUS_DELETE)
9133 break;
9134 if (a->patch_cb) {
9135 int choice = GOT_PATCH_CHOICE_NONE;
9136 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9137 ie->path, NULL, 1, 1);
9138 if (err)
9139 break;
9140 if (choice == GOT_PATCH_CHOICE_NO)
9141 break;
9142 if (choice != GOT_PATCH_CHOICE_YES) {
9143 err = got_error(GOT_ERR_PATCH_CHOICE);
9144 break;
9147 stage = GOT_FILEIDX_STAGE_DELETE;
9148 got_fileindex_entry_stage_set(ie, stage);
9149 a->staged_something = 1;
9150 if (a->status_cb == NULL)
9151 break;
9152 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9153 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9154 de_name);
9155 break;
9156 case GOT_STATUS_NO_CHANGE:
9157 break;
9158 case GOT_STATUS_CONFLICT:
9159 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9160 break;
9161 case GOT_STATUS_NONEXISTENT:
9162 err = got_error_set_errno(ENOENT, relpath);
9163 break;
9164 default:
9165 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9166 break;
9169 if (path_content && unlink(path_content) == -1 && err == NULL)
9170 err = got_error_from_errno2("unlink", path_content);
9171 free(path_content);
9172 free(ondisk_path);
9173 free(new_staged_blob_id);
9174 return err;
9177 const struct got_error *
9178 got_worktree_stage(struct got_worktree *worktree,
9179 struct got_pathlist_head *paths,
9180 got_worktree_status_cb status_cb, void *status_arg,
9181 got_worktree_patch_cb patch_cb, void *patch_arg,
9182 int allow_bad_symlinks, struct got_repository *repo)
9184 const struct got_error *err = NULL, *sync_err, *unlockerr;
9185 struct got_pathlist_entry *pe;
9186 struct got_fileindex *fileindex = NULL;
9187 char *fileindex_path = NULL;
9188 struct got_reference *head_ref = NULL;
9189 struct got_object_id *head_commit_id = NULL;
9190 struct check_stage_ok_arg oka;
9191 struct stage_path_arg spa;
9193 err = lock_worktree(worktree, LOCK_EX);
9194 if (err)
9195 return err;
9197 err = got_ref_open(&head_ref, repo,
9198 got_worktree_get_head_ref_name(worktree), 0);
9199 if (err)
9200 goto done;
9201 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9202 if (err)
9203 goto done;
9204 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9205 if (err)
9206 goto done;
9208 /* Check pre-conditions before staging anything. */
9209 oka.head_commit_id = head_commit_id;
9210 oka.worktree = worktree;
9211 oka.fileindex = fileindex;
9212 oka.repo = repo;
9213 oka.have_changes = 0;
9214 TAILQ_FOREACH(pe, paths, entry) {
9215 err = worktree_status(worktree, pe->path, fileindex, repo,
9216 check_stage_ok, &oka, NULL, NULL, 1, 0);
9217 if (err)
9218 goto done;
9220 if (!oka.have_changes) {
9221 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9222 goto done;
9225 spa.worktree = worktree;
9226 spa.fileindex = fileindex;
9227 spa.repo = repo;
9228 spa.patch_cb = patch_cb;
9229 spa.patch_arg = patch_arg;
9230 spa.status_cb = status_cb;
9231 spa.status_arg = status_arg;
9232 spa.staged_something = 0;
9233 spa.allow_bad_symlinks = allow_bad_symlinks;
9234 TAILQ_FOREACH(pe, paths, entry) {
9235 err = worktree_status(worktree, pe->path, fileindex, repo,
9236 stage_path, &spa, NULL, NULL, 1, 0);
9237 if (err)
9238 goto done;
9240 if (!spa.staged_something) {
9241 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9242 goto done;
9245 sync_err = sync_fileindex(fileindex, fileindex_path);
9246 if (sync_err && err == NULL)
9247 err = sync_err;
9248 done:
9249 if (head_ref)
9250 got_ref_close(head_ref);
9251 free(head_commit_id);
9252 free(fileindex_path);
9253 if (fileindex)
9254 got_fileindex_free(fileindex);
9255 unlockerr = lock_worktree(worktree, LOCK_SH);
9256 if (unlockerr && err == NULL)
9257 err = unlockerr;
9258 return err;
9261 struct unstage_path_arg {
9262 struct got_worktree *worktree;
9263 struct got_fileindex *fileindex;
9264 struct got_repository *repo;
9265 got_worktree_checkout_cb progress_cb;
9266 void *progress_arg;
9267 got_worktree_patch_cb patch_cb;
9268 void *patch_arg;
9271 static const struct got_error *
9272 create_unstaged_content(char **path_unstaged_content,
9273 char **path_new_staged_content, struct got_object_id *blob_id,
9274 struct got_object_id *staged_blob_id, const char *relpath,
9275 struct got_repository *repo,
9276 got_worktree_patch_cb patch_cb, void *patch_arg)
9278 const struct got_error *err, *free_err;
9279 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9280 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9281 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9282 struct got_diffreg_result *diffreg_result = NULL;
9283 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9284 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9285 int fd1 = -1, fd2 = -1;
9287 *path_unstaged_content = NULL;
9288 *path_new_staged_content = NULL;
9290 err = got_object_id_str(&label1, blob_id);
9291 if (err)
9292 return err;
9294 fd1 = got_opentempfd();
9295 if (fd1 == -1) {
9296 err = got_error_from_errno("got_opentempfd");
9297 goto done;
9299 fd2 = got_opentempfd();
9300 if (fd2 == -1) {
9301 err = got_error_from_errno("got_opentempfd");
9302 goto done;
9305 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9306 if (err)
9307 goto done;
9309 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9310 if (err)
9311 goto done;
9313 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9314 if (err)
9315 goto done;
9317 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9318 fd2);
9319 if (err)
9320 goto done;
9322 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9323 if (err)
9324 goto done;
9326 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9327 if (err)
9328 goto done;
9330 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9331 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9332 if (err)
9333 goto done;
9335 err = got_opentemp_named(path_unstaged_content, &outfile,
9336 "got-unstaged-content", "");
9337 if (err)
9338 goto done;
9339 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9340 "got-new-staged-content", "");
9341 if (err)
9342 goto done;
9344 if (fseek(f1, 0L, SEEK_SET) == -1) {
9345 err = got_ferror(f1, GOT_ERR_IO);
9346 goto done;
9348 if (fseek(f2, 0L, SEEK_SET) == -1) {
9349 err = got_ferror(f2, GOT_ERR_IO);
9350 goto done;
9352 /* Count the number of actual changes in the diff result. */
9353 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9354 struct diff_chunk_context cc = {};
9355 diff_chunk_context_load_change(&cc, &nchunks_used,
9356 diffreg_result->result, n, 0);
9357 nchanges++;
9359 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9360 int choice;
9361 err = apply_or_reject_change(&choice, &nchunks_used,
9362 diffreg_result->result, n, relpath, f1, f2,
9363 &line_cur1, &line_cur2,
9364 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9365 if (err)
9366 goto done;
9367 if (choice == GOT_PATCH_CHOICE_YES)
9368 have_content = 1;
9369 else
9370 have_rejected_content = 1;
9371 if (choice == GOT_PATCH_CHOICE_QUIT)
9372 break;
9374 if (have_content || have_rejected_content)
9375 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9376 outfile, rejectfile);
9377 done:
9378 free(label1);
9379 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9380 err = got_error_from_errno("close");
9381 if (blob)
9382 got_object_blob_close(blob);
9383 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9384 err = got_error_from_errno("close");
9385 if (staged_blob)
9386 got_object_blob_close(staged_blob);
9387 free_err = got_diffreg_result_free(diffreg_result);
9388 if (free_err && err == NULL)
9389 err = free_err;
9390 if (f1 && fclose(f1) == EOF && err == NULL)
9391 err = got_error_from_errno2("fclose", path1);
9392 if (f2 && fclose(f2) == EOF && err == NULL)
9393 err = got_error_from_errno2("fclose", path2);
9394 if (outfile && fclose(outfile) == EOF && err == NULL)
9395 err = got_error_from_errno2("fclose", *path_unstaged_content);
9396 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9397 err = got_error_from_errno2("fclose", *path_new_staged_content);
9398 if (path1 && unlink(path1) == -1 && err == NULL)
9399 err = got_error_from_errno2("unlink", path1);
9400 if (path2 && unlink(path2) == -1 && err == NULL)
9401 err = got_error_from_errno2("unlink", path2);
9402 if (err || !have_content) {
9403 if (*path_unstaged_content &&
9404 unlink(*path_unstaged_content) == -1 && err == NULL)
9405 err = got_error_from_errno2("unlink",
9406 *path_unstaged_content);
9407 free(*path_unstaged_content);
9408 *path_unstaged_content = NULL;
9410 if (err || !have_content || !have_rejected_content) {
9411 if (*path_new_staged_content &&
9412 unlink(*path_new_staged_content) == -1 && err == NULL)
9413 err = got_error_from_errno2("unlink",
9414 *path_new_staged_content);
9415 free(*path_new_staged_content);
9416 *path_new_staged_content = NULL;
9418 free(path1);
9419 free(path2);
9420 return err;
9423 static const struct got_error *
9424 unstage_hunks(struct got_object_id *staged_blob_id,
9425 struct got_blob_object *blob_base,
9426 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9427 const char *ondisk_path, const char *label_orig,
9428 struct got_worktree *worktree, struct got_repository *repo,
9429 got_worktree_patch_cb patch_cb, void *patch_arg,
9430 got_worktree_checkout_cb progress_cb, void *progress_arg)
9432 const struct got_error *err = NULL;
9433 char *path_unstaged_content = NULL;
9434 char *path_new_staged_content = NULL;
9435 char *parent = NULL, *base_path = NULL;
9436 char *blob_base_path = NULL;
9437 struct got_object_id *new_staged_blob_id = NULL;
9438 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9439 struct stat sb;
9441 err = create_unstaged_content(&path_unstaged_content,
9442 &path_new_staged_content, blob_id, staged_blob_id,
9443 ie->path, repo, patch_cb, patch_arg);
9444 if (err)
9445 return err;
9447 if (path_unstaged_content == NULL)
9448 return NULL;
9450 if (path_new_staged_content) {
9451 err = got_object_blob_create(&new_staged_blob_id,
9452 path_new_staged_content, repo);
9453 if (err)
9454 goto done;
9457 f = fopen(path_unstaged_content, "re");
9458 if (f == NULL) {
9459 err = got_error_from_errno2("fopen",
9460 path_unstaged_content);
9461 goto done;
9463 if (fstat(fileno(f), &sb) == -1) {
9464 err = got_error_from_errno2("fstat", path_unstaged_content);
9465 goto done;
9467 if (got_fileindex_entry_staged_filetype_get(ie) ==
9468 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9469 char link_target[PATH_MAX];
9470 size_t r;
9471 r = fread(link_target, 1, sizeof(link_target), f);
9472 if (r == 0 && ferror(f)) {
9473 err = got_error_from_errno("fread");
9474 goto done;
9476 if (r >= sizeof(link_target)) { /* should not happen */
9477 err = got_error(GOT_ERR_NO_SPACE);
9478 goto done;
9480 link_target[r] = '\0';
9481 err = merge_symlink(worktree, blob_base,
9482 ondisk_path, ie->path, label_orig, link_target,
9483 worktree->base_commit_id, repo, progress_cb,
9484 progress_arg);
9485 } else {
9486 int local_changes_subsumed;
9488 err = got_path_dirname(&parent, ondisk_path);
9489 if (err)
9490 return err;
9492 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9493 parent) == -1) {
9494 err = got_error_from_errno("asprintf");
9495 base_path = NULL;
9496 goto done;
9499 err = got_opentemp_named(&blob_base_path, &f_base,
9500 base_path, "");
9501 if (err)
9502 goto done;
9503 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9504 blob_base);
9505 if (err)
9506 goto done;
9509 * In order the run a 3-way merge with a symlink we copy the symlink's
9510 * target path into a temporary file and use that file with diff3.
9512 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9513 err = dump_symlink_target_path_to_file(&f_deriv2,
9514 ondisk_path);
9515 if (err)
9516 goto done;
9517 } else {
9518 int fd;
9519 fd = open(ondisk_path,
9520 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9521 if (fd == -1) {
9522 err = got_error_from_errno2("open", ondisk_path);
9523 goto done;
9525 f_deriv2 = fdopen(fd, "r");
9526 if (f_deriv2 == NULL) {
9527 err = got_error_from_errno2("fdopen", ondisk_path);
9528 close(fd);
9529 goto done;
9533 err = merge_file(&local_changes_subsumed, worktree,
9534 f_base, f, f_deriv2, ondisk_path, ie->path,
9535 got_fileindex_perms_to_st(ie),
9536 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9537 repo, progress_cb, progress_arg);
9539 if (err)
9540 goto done;
9542 if (new_staged_blob_id) {
9543 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9544 SHA1_DIGEST_LENGTH);
9545 } else {
9546 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9547 got_fileindex_entry_staged_filetype_set(ie, 0);
9549 done:
9550 free(new_staged_blob_id);
9551 if (path_unstaged_content &&
9552 unlink(path_unstaged_content) == -1 && err == NULL)
9553 err = got_error_from_errno2("unlink", path_unstaged_content);
9554 if (path_new_staged_content &&
9555 unlink(path_new_staged_content) == -1 && err == NULL)
9556 err = got_error_from_errno2("unlink", path_new_staged_content);
9557 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9558 err = got_error_from_errno2("unlink", blob_base_path);
9559 if (f_base && fclose(f_base) == EOF && err == NULL)
9560 err = got_error_from_errno2("fclose", path_unstaged_content);
9561 if (f && fclose(f) == EOF && err == NULL)
9562 err = got_error_from_errno2("fclose", path_unstaged_content);
9563 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9564 err = got_error_from_errno2("fclose", ondisk_path);
9565 free(path_unstaged_content);
9566 free(path_new_staged_content);
9567 free(blob_base_path);
9568 free(parent);
9569 free(base_path);
9570 return err;
9573 static const struct got_error *
9574 unstage_path(void *arg, unsigned char status,
9575 unsigned char staged_status, const char *relpath,
9576 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9577 struct got_object_id *commit_id, int dirfd, const char *de_name)
9579 const struct got_error *err = NULL;
9580 struct unstage_path_arg *a = arg;
9581 struct got_fileindex_entry *ie;
9582 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9583 char *ondisk_path = NULL;
9584 char *id_str = NULL, *label_orig = NULL;
9585 int local_changes_subsumed;
9586 struct stat sb;
9587 int fd1 = -1, fd2 = -1;
9589 if (staged_status != GOT_STATUS_ADD &&
9590 staged_status != GOT_STATUS_MODIFY &&
9591 staged_status != GOT_STATUS_DELETE)
9592 return NULL;
9594 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9595 if (ie == NULL)
9596 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9598 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9599 == -1)
9600 return got_error_from_errno("asprintf");
9602 err = got_object_id_str(&id_str,
9603 commit_id ? commit_id : a->worktree->base_commit_id);
9604 if (err)
9605 goto done;
9606 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9607 id_str) == -1) {
9608 err = got_error_from_errno("asprintf");
9609 goto done;
9612 fd1 = got_opentempfd();
9613 if (fd1 == -1) {
9614 err = got_error_from_errno("got_opentempfd");
9615 goto done;
9617 fd2 = got_opentempfd();
9618 if (fd2 == -1) {
9619 err = got_error_from_errno("got_opentempfd");
9620 goto done;
9623 switch (staged_status) {
9624 case GOT_STATUS_MODIFY:
9625 err = got_object_open_as_blob(&blob_base, a->repo,
9626 blob_id, 8192, fd1);
9627 if (err)
9628 break;
9629 /* fall through */
9630 case GOT_STATUS_ADD:
9631 if (a->patch_cb) {
9632 if (staged_status == GOT_STATUS_ADD) {
9633 int choice = GOT_PATCH_CHOICE_NONE;
9634 err = (*a->patch_cb)(&choice, a->patch_arg,
9635 staged_status, ie->path, NULL, 1, 1);
9636 if (err)
9637 break;
9638 if (choice != GOT_PATCH_CHOICE_YES)
9639 break;
9640 } else {
9641 err = unstage_hunks(staged_blob_id,
9642 blob_base, blob_id, ie, ondisk_path,
9643 label_orig, a->worktree, a->repo,
9644 a->patch_cb, a->patch_arg,
9645 a->progress_cb, a->progress_arg);
9646 break; /* Done with this file. */
9649 err = got_object_open_as_blob(&blob_staged, a->repo,
9650 staged_blob_id, 8192, fd2);
9651 if (err)
9652 break;
9653 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9654 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9655 case GOT_FILEIDX_MODE_REGULAR_FILE:
9656 err = merge_blob(&local_changes_subsumed, a->worktree,
9657 blob_base, ondisk_path, relpath,
9658 got_fileindex_perms_to_st(ie), label_orig,
9659 blob_staged, commit_id ? commit_id :
9660 a->worktree->base_commit_id, a->repo,
9661 a->progress_cb, a->progress_arg);
9662 break;
9663 case GOT_FILEIDX_MODE_SYMLINK:
9664 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9665 char *staged_target;
9666 err = got_object_blob_read_to_str(
9667 &staged_target, blob_staged);
9668 if (err)
9669 goto done;
9670 err = merge_symlink(a->worktree, blob_base,
9671 ondisk_path, relpath, label_orig,
9672 staged_target, commit_id ? commit_id :
9673 a->worktree->base_commit_id,
9674 a->repo, a->progress_cb, a->progress_arg);
9675 free(staged_target);
9676 } else {
9677 err = merge_blob(&local_changes_subsumed,
9678 a->worktree, blob_base, ondisk_path,
9679 relpath, got_fileindex_perms_to_st(ie),
9680 label_orig, blob_staged,
9681 commit_id ? commit_id :
9682 a->worktree->base_commit_id, a->repo,
9683 a->progress_cb, a->progress_arg);
9685 break;
9686 default:
9687 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9688 break;
9690 if (err == NULL) {
9691 got_fileindex_entry_stage_set(ie,
9692 GOT_FILEIDX_STAGE_NONE);
9693 got_fileindex_entry_staged_filetype_set(ie, 0);
9695 break;
9696 case GOT_STATUS_DELETE:
9697 if (a->patch_cb) {
9698 int choice = GOT_PATCH_CHOICE_NONE;
9699 err = (*a->patch_cb)(&choice, a->patch_arg,
9700 staged_status, ie->path, NULL, 1, 1);
9701 if (err)
9702 break;
9703 if (choice == GOT_PATCH_CHOICE_NO)
9704 break;
9705 if (choice != GOT_PATCH_CHOICE_YES) {
9706 err = got_error(GOT_ERR_PATCH_CHOICE);
9707 break;
9710 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9711 got_fileindex_entry_staged_filetype_set(ie, 0);
9712 err = get_file_status(&status, &sb, ie, ondisk_path,
9713 dirfd, de_name, a->repo);
9714 if (err)
9715 break;
9716 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9717 break;
9719 done:
9720 free(ondisk_path);
9721 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9722 err = got_error_from_errno("close");
9723 if (blob_base)
9724 got_object_blob_close(blob_base);
9725 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9726 err = got_error_from_errno("close");
9727 if (blob_staged)
9728 got_object_blob_close(blob_staged);
9729 free(id_str);
9730 free(label_orig);
9731 return err;
9734 const struct got_error *
9735 got_worktree_unstage(struct got_worktree *worktree,
9736 struct got_pathlist_head *paths,
9737 got_worktree_checkout_cb progress_cb, void *progress_arg,
9738 got_worktree_patch_cb patch_cb, void *patch_arg,
9739 struct got_repository *repo)
9741 const struct got_error *err = NULL, *sync_err, *unlockerr;
9742 struct got_pathlist_entry *pe;
9743 struct got_fileindex *fileindex = NULL;
9744 char *fileindex_path = NULL;
9745 struct unstage_path_arg upa;
9747 err = lock_worktree(worktree, LOCK_EX);
9748 if (err)
9749 return err;
9751 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9752 if (err)
9753 goto done;
9755 upa.worktree = worktree;
9756 upa.fileindex = fileindex;
9757 upa.repo = repo;
9758 upa.progress_cb = progress_cb;
9759 upa.progress_arg = progress_arg;
9760 upa.patch_cb = patch_cb;
9761 upa.patch_arg = patch_arg;
9762 TAILQ_FOREACH(pe, paths, entry) {
9763 err = worktree_status(worktree, pe->path, fileindex, repo,
9764 unstage_path, &upa, NULL, NULL, 1, 0);
9765 if (err)
9766 goto done;
9769 sync_err = sync_fileindex(fileindex, fileindex_path);
9770 if (sync_err && err == NULL)
9771 err = sync_err;
9772 done:
9773 free(fileindex_path);
9774 if (fileindex)
9775 got_fileindex_free(fileindex);
9776 unlockerr = lock_worktree(worktree, LOCK_SH);
9777 if (unlockerr && err == NULL)
9778 err = unlockerr;
9779 return err;
9782 struct report_file_info_arg {
9783 struct got_worktree *worktree;
9784 got_worktree_path_info_cb info_cb;
9785 void *info_arg;
9786 struct got_pathlist_head *paths;
9787 got_cancel_cb cancel_cb;
9788 void *cancel_arg;
9791 static const struct got_error *
9792 report_file_info(void *arg, struct got_fileindex_entry *ie)
9794 const struct got_error *err;
9795 struct report_file_info_arg *a = arg;
9796 struct got_pathlist_entry *pe;
9797 struct got_object_id blob_id, staged_blob_id, commit_id;
9798 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9799 struct got_object_id *commit_idp = NULL;
9800 int stage;
9802 if (a->cancel_cb) {
9803 err = a->cancel_cb(a->cancel_arg);
9804 if (err)
9805 return err;
9808 TAILQ_FOREACH(pe, a->paths, entry) {
9809 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9810 got_path_is_child(ie->path, pe->path, pe->path_len))
9811 break;
9813 if (pe == NULL) /* not found */
9814 return NULL;
9816 if (got_fileindex_entry_has_blob(ie))
9817 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9818 stage = got_fileindex_entry_stage_get(ie);
9819 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9820 stage == GOT_FILEIDX_STAGE_ADD) {
9821 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9822 &staged_blob_id, ie);
9825 if (got_fileindex_entry_has_commit(ie))
9826 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9828 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9829 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9832 const struct got_error *
9833 got_worktree_path_info(struct got_worktree *worktree,
9834 struct got_pathlist_head *paths,
9835 got_worktree_path_info_cb info_cb, void *info_arg,
9836 got_cancel_cb cancel_cb, void *cancel_arg)
9839 const struct got_error *err = NULL, *unlockerr;
9840 struct got_fileindex *fileindex = NULL;
9841 char *fileindex_path = NULL;
9842 struct report_file_info_arg arg;
9844 err = lock_worktree(worktree, LOCK_SH);
9845 if (err)
9846 return err;
9848 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9849 if (err)
9850 goto done;
9852 arg.worktree = worktree;
9853 arg.info_cb = info_cb;
9854 arg.info_arg = info_arg;
9855 arg.paths = paths;
9856 arg.cancel_cb = cancel_cb;
9857 arg.cancel_arg = cancel_arg;
9858 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9859 &arg);
9860 done:
9861 free(fileindex_path);
9862 if (fileindex)
9863 got_fileindex_free(fileindex);
9864 unlockerr = lock_worktree(worktree, LOCK_UN);
9865 if (unlockerr && err == NULL)
9866 err = unlockerr;
9867 return err;
9870 static const struct got_error *
9871 patch_check_path(const char *p, char **path, unsigned char *status,
9872 unsigned char *staged_status, struct got_fileindex *fileindex,
9873 struct got_worktree *worktree, struct got_repository *repo)
9875 const struct got_error *err;
9876 struct got_fileindex_entry *ie;
9877 struct stat sb;
9878 char *ondisk_path = NULL;
9880 err = got_worktree_resolve_path(path, worktree, p);
9881 if (err)
9882 return err;
9884 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9885 *path[0] ? "/" : "", *path) == -1)
9886 return got_error_from_errno("asprintf");
9888 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9889 if (ie) {
9890 *staged_status = get_staged_status(ie);
9891 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9892 repo);
9893 if (err)
9894 goto done;
9895 } else {
9896 *staged_status = GOT_STATUS_NO_CHANGE;
9897 *status = GOT_STATUS_UNVERSIONED;
9898 if (lstat(ondisk_path, &sb) == -1) {
9899 if (errno != ENOENT) {
9900 err = got_error_from_errno2("lstat",
9901 ondisk_path);
9902 goto done;
9904 *status = GOT_STATUS_NONEXISTENT;
9908 done:
9909 free(ondisk_path);
9910 return err;
9913 static const struct got_error *
9914 patch_can_rm(const char *path, unsigned char status,
9915 unsigned char staged_status)
9917 if (status == GOT_STATUS_NONEXISTENT)
9918 return got_error_set_errno(ENOENT, path);
9919 if (status != GOT_STATUS_NO_CHANGE &&
9920 status != GOT_STATUS_ADD &&
9921 status != GOT_STATUS_MODIFY &&
9922 status != GOT_STATUS_MODE_CHANGE)
9923 return got_error_path(path, GOT_ERR_FILE_STATUS);
9924 if (staged_status == GOT_STATUS_DELETE)
9925 return got_error_path(path, GOT_ERR_FILE_STATUS);
9926 return NULL;
9929 static const struct got_error *
9930 patch_can_add(const char *path, unsigned char status)
9932 if (status != GOT_STATUS_NONEXISTENT)
9933 return got_error_path(path, GOT_ERR_FILE_STATUS);
9934 return NULL;
9937 static const struct got_error *
9938 patch_can_edit(const char *path, unsigned char status,
9939 unsigned char staged_status)
9941 if (status == GOT_STATUS_NONEXISTENT)
9942 return got_error_set_errno(ENOENT, path);
9943 if (status != GOT_STATUS_NO_CHANGE &&
9944 status != GOT_STATUS_ADD &&
9945 status != GOT_STATUS_MODIFY)
9946 return got_error_path(path, GOT_ERR_FILE_STATUS);
9947 if (staged_status == GOT_STATUS_DELETE)
9948 return got_error_path(path, GOT_ERR_FILE_STATUS);
9949 return NULL;
9952 const struct got_error *
9953 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9954 char **fileindex_path, struct got_worktree *worktree)
9956 return open_fileindex(fileindex, fileindex_path, worktree);
9959 const struct got_error *
9960 got_worktree_patch_check_path(const char *old, const char *new,
9961 char **oldpath, char **newpath, struct got_worktree *worktree,
9962 struct got_repository *repo, struct got_fileindex *fileindex)
9964 const struct got_error *err = NULL;
9965 int file_renamed = 0;
9966 unsigned char status_old, staged_status_old;
9967 unsigned char status_new, staged_status_new;
9969 *oldpath = NULL;
9970 *newpath = NULL;
9972 err = patch_check_path(old != NULL ? old : new, oldpath,
9973 &status_old, &staged_status_old, fileindex, worktree, repo);
9974 if (err)
9975 goto done;
9977 err = patch_check_path(new != NULL ? new : old, newpath,
9978 &status_new, &staged_status_new, fileindex, worktree, repo);
9979 if (err)
9980 goto done;
9982 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9983 file_renamed = 1;
9985 if (old != NULL && new == NULL)
9986 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9987 else if (file_renamed) {
9988 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9989 if (err == NULL)
9990 err = patch_can_add(*newpath, status_new);
9991 } else if (old == NULL)
9992 err = patch_can_add(*newpath, status_new);
9993 else
9994 err = patch_can_edit(*newpath, status_new, staged_status_new);
9996 done:
9997 if (err) {
9998 free(*oldpath);
9999 *oldpath = NULL;
10000 free(*newpath);
10001 *newpath = NULL;
10003 return err;
10006 const struct got_error *
10007 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10008 struct got_worktree *worktree, struct got_fileindex *fileindex,
10009 got_worktree_checkout_cb progress_cb, void *progress_arg)
10011 struct schedule_addition_args saa;
10013 memset(&saa, 0, sizeof(saa));
10014 saa.worktree = worktree;
10015 saa.fileindex = fileindex;
10016 saa.progress_cb = progress_cb;
10017 saa.progress_arg = progress_arg;
10018 saa.repo = repo;
10020 return worktree_status(worktree, path, fileindex, repo,
10021 schedule_addition, &saa, NULL, NULL, 1, 0);
10024 const struct got_error *
10025 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10026 struct got_worktree *worktree, struct got_fileindex *fileindex,
10027 got_worktree_delete_cb progress_cb, void *progress_arg)
10029 const struct got_error *err;
10030 struct schedule_deletion_args sda;
10031 char *ondisk_status_path;
10033 memset(&sda, 0, sizeof(sda));
10034 sda.worktree = worktree;
10035 sda.fileindex = fileindex;
10036 sda.progress_cb = progress_cb;
10037 sda.progress_arg = progress_arg;
10038 sda.repo = repo;
10039 sda.delete_local_mods = 0;
10040 sda.keep_on_disk = 0;
10041 sda.ignore_missing_paths = 0;
10042 sda.status_codes = NULL;
10043 if (asprintf(&ondisk_status_path, "%s/%s",
10044 got_worktree_get_root_path(worktree), path) == -1)
10045 return got_error_from_errno("asprintf");
10046 sda.status_path = ondisk_status_path;
10047 sda.status_path_len = strlen(ondisk_status_path);
10049 err = worktree_status(worktree, path, fileindex, repo,
10050 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10051 free(ondisk_status_path);
10052 return err;
10055 const struct got_error *
10056 got_worktree_patch_complete(struct got_fileindex *fileindex,
10057 const char *fileindex_path)
10059 const struct got_error *err = NULL;
10061 err = sync_fileindex(fileindex, fileindex_path);
10062 got_fileindex_free(fileindex);
10064 return err;