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,
1108 int update_timestamps)
1110 const struct got_error *err = NULL;
1111 struct got_fileindex_entry *new_ie;
1113 *new_iep = NULL;
1115 err = got_fileindex_entry_alloc(&new_ie, path);
1116 if (err)
1117 return err;
1119 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1120 blob_id->sha1, base_commit_id->sha1, update_timestamps);
1121 if (err)
1122 goto done;
1124 err = got_fileindex_entry_add(fileindex, new_ie);
1125 done:
1126 if (err)
1127 got_fileindex_entry_free(new_ie);
1128 else
1129 *new_iep = new_ie;
1130 return err;
1133 static mode_t
1134 get_ondisk_perms(int executable, mode_t st_mode)
1136 mode_t xbits = S_IXUSR;
1138 if (executable) {
1139 /* Map read bits to execute bits. */
1140 if (st_mode & S_IRGRP)
1141 xbits |= S_IXGRP;
1142 if (st_mode & S_IROTH)
1143 xbits |= S_IXOTH;
1144 return st_mode | xbits;
1147 return st_mode;
1150 /* forward declaration */
1151 static const struct got_error *
1152 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1153 const char *path, mode_t te_mode, mode_t st_mode,
1154 struct got_blob_object *blob, int restoring_missing_file,
1155 int reverting_versioned_file, int installing_bad_symlink,
1156 int path_is_unversioned, int *update_timestamps,
1157 struct got_repository *repo,
1158 got_worktree_checkout_cb progress_cb, void *progress_arg);
1161 * This function assumes that the provided symlink target points at a
1162 * safe location in the work tree!
1164 static const struct got_error *
1165 replace_existing_symlink(int *did_something, const char *ondisk_path,
1166 const char *target_path, size_t target_len)
1168 const struct got_error *err = NULL;
1169 ssize_t elen;
1170 char etarget[PATH_MAX];
1171 int fd;
1173 *did_something = 0;
1176 * "Bad" symlinks (those pointing outside the work tree or into the
1177 * .got directory) are installed in the work tree as a regular file
1178 * which contains the bad symlink target path.
1179 * The new symlink target has already been checked for safety by our
1180 * caller. If we can successfully open a regular file then we simply
1181 * replace this file with a symlink below.
1183 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1184 if (fd == -1) {
1185 if (!got_err_open_nofollow_on_symlink())
1186 return got_error_from_errno2("open", ondisk_path);
1188 /* We are updating an existing on-disk symlink. */
1189 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1190 if (elen == -1)
1191 return got_error_from_errno2("readlink", ondisk_path);
1193 if (elen == target_len &&
1194 memcmp(etarget, target_path, target_len) == 0)
1195 return NULL; /* nothing to do */
1198 *did_something = 1;
1199 err = update_symlink(ondisk_path, target_path, target_len);
1200 if (fd != -1 && close(fd) == -1 && err == NULL)
1201 err = got_error_from_errno2("close", ondisk_path);
1202 return err;
1205 static const struct got_error *
1206 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1207 size_t target_len, const char *ondisk_path, const char *wtroot_path,
1208 const char *meta_dir)
1210 const struct got_error *err = NULL;
1211 char canonpath[PATH_MAX];
1212 char *path_got = NULL;
1214 *is_bad_symlink = 0;
1216 if (target_len >= sizeof(canonpath)) {
1217 *is_bad_symlink = 1;
1218 return NULL;
1222 * We do not use realpath(3) to resolve the symlink's target
1223 * path because we don't want to resolve symlinks recursively.
1224 * Instead we make the path absolute and then canonicalize it.
1225 * Relative symlink target lookup should begin at the directory
1226 * in which the blob object is being installed.
1228 if (!got_path_is_absolute(target_path)) {
1229 char *abspath, *parent;
1230 err = got_path_dirname(&parent, ondisk_path);
1231 if (err)
1232 return err;
1233 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1234 free(parent);
1235 return got_error_from_errno("asprintf");
1237 free(parent);
1238 if (strlen(abspath) >= sizeof(canonpath)) {
1239 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1240 free(abspath);
1241 return err;
1243 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1244 free(abspath);
1245 if (err)
1246 return err;
1247 } else {
1248 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1249 if (err)
1250 return err;
1253 /* Only allow symlinks pointing at paths within the work tree. */
1254 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1255 *is_bad_symlink = 1;
1256 return NULL;
1259 /* Do not allow symlinks pointing into the meta directory. */
1260 if (asprintf(&path_got, "%s/%s", wtroot_path, meta_dir) == -1)
1261 return got_error_from_errno("asprintf");
1262 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1263 *is_bad_symlink = 1;
1265 free(path_got);
1266 return NULL;
1269 static const struct got_error *
1270 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1271 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1272 int restoring_missing_file, int reverting_versioned_file,
1273 int path_is_unversioned, int allow_bad_symlinks,
1274 struct got_repository *repo,
1275 got_worktree_checkout_cb progress_cb, void *progress_arg)
1277 const struct got_error *err = NULL;
1278 char target_path[PATH_MAX];
1279 size_t len, target_len = 0;
1280 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1281 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1283 *is_bad_symlink = 0;
1286 * Blob object content specifies the target path of the link.
1287 * If a symbolic link cannot be installed we instead create
1288 * a regular file which contains the link target path stored
1289 * in the blob object.
1291 do {
1292 err = got_object_blob_read_block(&len, blob);
1293 if (err)
1294 return err;
1296 if (len + target_len >= sizeof(target_path)) {
1297 /* Path too long; install as a regular file. */
1298 *is_bad_symlink = 1;
1299 got_object_blob_rewind(blob);
1300 return install_blob(worktree, ondisk_path, path,
1301 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1302 restoring_missing_file, reverting_versioned_file,
1303 1, path_is_unversioned, NULL, repo, progress_cb,
1304 progress_arg);
1306 if (len > 0) {
1307 /* Skip blob object header first time around. */
1308 memcpy(target_path + target_len, buf + hdrlen,
1309 len - hdrlen);
1310 target_len += len - hdrlen;
1311 hdrlen = 0;
1313 } while (len != 0);
1314 target_path[target_len] = '\0';
1316 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1317 ondisk_path, worktree->root_path, worktree->meta_dir);
1318 if (err)
1319 return err;
1321 if (*is_bad_symlink && !allow_bad_symlinks) {
1322 /* install as a regular file */
1323 got_object_blob_rewind(blob);
1324 err = install_blob(worktree, ondisk_path, path,
1325 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1326 restoring_missing_file, reverting_versioned_file, 1,
1327 path_is_unversioned, NULL, repo,
1328 progress_cb, progress_arg);
1329 return err;
1332 if (symlink(target_path, ondisk_path) == -1) {
1333 if (errno == EEXIST) {
1334 int symlink_replaced;
1335 if (path_is_unversioned) {
1336 err = (*progress_cb)(progress_arg,
1337 GOT_STATUS_UNVERSIONED, path);
1338 return err;
1340 err = replace_existing_symlink(&symlink_replaced,
1341 ondisk_path, target_path, target_len);
1342 if (err)
1343 return err;
1344 if (progress_cb) {
1345 if (symlink_replaced) {
1346 err = (*progress_cb)(progress_arg,
1347 reverting_versioned_file ?
1348 GOT_STATUS_REVERT :
1349 GOT_STATUS_UPDATE, path);
1350 } else {
1351 err = (*progress_cb)(progress_arg,
1352 GOT_STATUS_EXISTS, path);
1355 return err; /* Nothing else to do. */
1358 if (errno == ENOENT) {
1359 char *parent;
1360 err = got_path_dirname(&parent, ondisk_path);
1361 if (err)
1362 return err;
1363 err = got_path_mkdir(parent);
1364 free(parent);
1365 if (err)
1366 return err;
1368 * Retry, and fall through to error handling
1369 * below if this second attempt fails.
1371 if (symlink(target_path, ondisk_path) != -1) {
1372 err = NULL; /* success */
1373 if (progress_cb) {
1374 err = (*progress_cb)(progress_arg,
1375 reverting_versioned_file ?
1376 GOT_STATUS_REVERT : GOT_STATUS_ADD,
1377 path);
1379 return err;
1383 /* Handle errors from first or second creation attempt. */
1384 if (errno == ENAMETOOLONG) {
1385 /* bad target path; install as a regular file */
1386 *is_bad_symlink = 1;
1387 got_object_blob_rewind(blob);
1388 err = install_blob(worktree, ondisk_path, path,
1389 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1390 restoring_missing_file, reverting_versioned_file, 1,
1391 path_is_unversioned, NULL, repo,
1392 progress_cb, progress_arg);
1393 } else if (errno == ENOTDIR) {
1394 err = got_error_path(ondisk_path,
1395 GOT_ERR_FILE_OBSTRUCTED);
1396 } else {
1397 err = got_error_from_errno3("symlink",
1398 target_path, ondisk_path);
1400 } else if (progress_cb)
1401 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1402 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1403 return err;
1406 static const struct got_error *
1407 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1408 const char *path, mode_t te_mode, mode_t st_mode,
1409 struct got_blob_object *blob, int restoring_missing_file,
1410 int reverting_versioned_file, int installing_bad_symlink,
1411 int path_is_unversioned, int *update_timestamps,
1412 struct got_repository *repo,
1413 got_worktree_checkout_cb progress_cb, void *progress_arg)
1415 const struct got_error *err = NULL;
1416 int fd = -1;
1417 size_t len, hdrlen;
1418 int update = 0;
1419 char *tmppath = NULL;
1420 mode_t mode;
1422 if (update_timestamps)
1423 *update_timestamps = 1;
1425 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1426 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1427 O_CLOEXEC, mode);
1428 if (fd == -1) {
1429 if (errno == ENOENT || errno == ENOTDIR) {
1430 char *parent;
1431 err = got_path_dirname(&parent, path);
1432 if (err)
1433 return err;
1434 err = add_dir_on_disk(worktree, parent);
1435 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1436 err = got_error_path(path, err->code);
1437 free(parent);
1438 if (err)
1439 return err;
1440 fd = open(ondisk_path,
1441 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1442 mode);
1443 if (fd == -1)
1444 return got_error_from_errno2("open",
1445 ondisk_path);
1446 } else if (errno == EEXIST) {
1447 if (path_is_unversioned) {
1448 if (update_timestamps)
1449 *update_timestamps = 0;
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_EXISTS, path);
1452 goto done;
1454 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1455 !S_ISREG(st_mode) && !installing_bad_symlink) {
1456 /* TODO file is obstructed; do something */
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1459 goto done;
1460 } else {
1461 err = got_opentemp_named_fd(&tmppath, &fd,
1462 ondisk_path, "");
1463 if (err)
1464 goto done;
1465 update = 1;
1467 if (fchmod(fd, apply_umask(mode)) == -1) {
1468 err = got_error_from_errno2("fchmod",
1469 tmppath);
1470 goto done;
1473 } else
1474 return got_error_from_errno2("open", ondisk_path);
1477 if (progress_cb) {
1478 if (restoring_missing_file)
1479 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1480 path);
1481 else if (reverting_versioned_file)
1482 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1483 path);
1484 else
1485 err = (*progress_cb)(progress_arg,
1486 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1487 if (err)
1488 goto done;
1491 hdrlen = got_object_blob_get_hdrlen(blob);
1492 do {
1493 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1494 err = got_object_blob_read_block(&len, blob);
1495 if (err)
1496 break;
1497 if (len > 0) {
1498 /* Skip blob object header first time around. */
1499 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1500 if (outlen == -1) {
1501 err = got_error_from_errno("write");
1502 goto done;
1503 } else if (outlen != len - hdrlen) {
1504 err = got_error(GOT_ERR_IO);
1505 goto done;
1507 hdrlen = 0;
1509 } while (len != 0);
1511 if (fsync(fd) != 0) {
1512 err = got_error_from_errno("fsync");
1513 goto done;
1516 if (update) {
1517 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1518 err = got_error_from_errno2("unlink", ondisk_path);
1519 goto done;
1521 if (rename(tmppath, ondisk_path) != 0) {
1522 err = got_error_from_errno3("rename", tmppath,
1523 ondisk_path);
1524 goto done;
1526 free(tmppath);
1527 tmppath = NULL;
1530 done:
1531 if (fd != -1 && close(fd) == -1 && err == NULL)
1532 err = got_error_from_errno("close");
1533 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1534 err = got_error_from_errno2("unlink", tmppath);
1535 free(tmppath);
1536 return err;
1540 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1541 * conflict marker is found in newly added lines only.
1543 static const struct got_error *
1544 get_modified_file_content_status(unsigned char *status,
1545 struct got_blob_object *blob, const char *path, struct stat *sb,
1546 FILE *ondisk_file)
1548 const struct got_error *err, *free_err;
1549 const char *markers[3] = {
1550 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1551 GOT_DIFF_CONFLICT_MARKER_SEP,
1552 GOT_DIFF_CONFLICT_MARKER_END
1554 FILE *f1 = NULL;
1555 struct got_diffreg_result *diffreg_result = NULL;
1556 struct diff_result *r;
1557 int nchunks_parsed, n, i = 0, ln = 0;
1558 char *line = NULL;
1559 size_t linesize = 0;
1560 ssize_t linelen;
1562 if (*status != GOT_STATUS_MODIFY)
1563 return NULL;
1565 f1 = got_opentemp();
1566 if (f1 == NULL)
1567 return got_error_from_errno("got_opentemp");
1569 if (blob) {
1570 got_object_blob_rewind(blob);
1571 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1572 if (err)
1573 goto done;
1576 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1577 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1578 if (err)
1579 goto done;
1581 r = diffreg_result->result;
1583 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1584 struct diff_chunk *c;
1585 struct diff_chunk_context cc = {};
1586 off_t pos;
1589 * We can optimise a little by advancing straight
1590 * to the next chunk if this one has no added lines.
1592 c = diff_chunk_get(r, n);
1594 if (diff_chunk_type(c) != CHUNK_PLUS) {
1595 nchunks_parsed = 1;
1596 continue; /* removed or unchanged lines */
1599 pos = diff_chunk_get_right_start_pos(c);
1600 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1601 err = got_ferror(ondisk_file, GOT_ERR_IO);
1602 goto done;
1605 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1606 ln = cc.right.start;
1608 while (ln < cc.right.end) {
1609 linelen = getline(&line, &linesize, ondisk_file);
1610 if (linelen == -1) {
1611 if (feof(ondisk_file))
1612 break;
1613 err = got_ferror(ondisk_file, GOT_ERR_IO);
1614 break;
1617 if (line && strncmp(line, markers[i],
1618 strlen(markers[i])) == 0) {
1619 if (strcmp(markers[i],
1620 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1621 *status = GOT_STATUS_CONFLICT;
1622 goto done;
1623 } else
1624 i++;
1626 ++ln;
1630 done:
1631 free(line);
1632 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1633 err = got_error_from_errno("fclose");
1634 free_err = got_diffreg_result_free(diffreg_result);
1635 if (err == NULL)
1636 err = free_err;
1638 return err;
1641 static int
1642 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1644 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1645 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1648 static int
1649 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1651 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1652 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1653 ie->mtime_sec == sb->st_mtim.tv_sec &&
1654 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1655 ie->size == (sb->st_size & 0xffffffff) &&
1656 !xbit_differs(ie, sb->st_mode));
1659 static unsigned char
1660 get_staged_status(struct got_fileindex_entry *ie)
1662 switch (got_fileindex_entry_stage_get(ie)) {
1663 case GOT_FILEIDX_STAGE_ADD:
1664 return GOT_STATUS_ADD;
1665 case GOT_FILEIDX_STAGE_DELETE:
1666 return GOT_STATUS_DELETE;
1667 case GOT_FILEIDX_STAGE_MODIFY:
1668 return GOT_STATUS_MODIFY;
1669 default:
1670 return GOT_STATUS_NO_CHANGE;
1674 static const struct got_error *
1675 get_symlink_modification_status(unsigned char *status,
1676 struct got_fileindex_entry *ie, const char *abspath,
1677 int dirfd, const char *de_name, struct got_blob_object *blob)
1679 const struct got_error *err = NULL;
1680 char target_path[PATH_MAX];
1681 char etarget[PATH_MAX];
1682 ssize_t elen;
1683 size_t len, target_len = 0;
1684 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1685 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1687 *status = GOT_STATUS_NO_CHANGE;
1689 /* Blob object content specifies the target path of the link. */
1690 do {
1691 err = got_object_blob_read_block(&len, blob);
1692 if (err)
1693 return err;
1694 if (len + target_len >= sizeof(target_path)) {
1696 * Should not happen. The blob contents were OK
1697 * when this symlink was installed.
1699 return got_error(GOT_ERR_NO_SPACE);
1701 if (len > 0) {
1702 /* Skip blob object header first time around. */
1703 memcpy(target_path + target_len, buf + hdrlen,
1704 len - hdrlen);
1705 target_len += len - hdrlen;
1706 hdrlen = 0;
1708 } while (len != 0);
1709 target_path[target_len] = '\0';
1711 if (dirfd != -1) {
1712 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1713 if (elen == -1)
1714 return got_error_from_errno2("readlinkat", abspath);
1715 } else {
1716 elen = readlink(abspath, etarget, sizeof(etarget));
1717 if (elen == -1)
1718 return got_error_from_errno2("readlink", abspath);
1721 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1722 *status = GOT_STATUS_MODIFY;
1724 return NULL;
1727 static const struct got_error *
1728 get_file_status(unsigned char *status, struct stat *sb,
1729 struct got_fileindex_entry *ie, const char *abspath,
1730 int dirfd, const char *de_name, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 struct got_object_id id;
1734 size_t hdrlen;
1735 int fd = -1, fd1 = -1;
1736 FILE *f = NULL;
1737 uint8_t fbuf[8192];
1738 struct got_blob_object *blob = NULL;
1739 size_t flen, blen;
1740 unsigned char staged_status;
1742 staged_status = get_staged_status(ie);
1743 *status = GOT_STATUS_NO_CHANGE;
1744 memset(sb, 0, sizeof(*sb));
1747 * Whenever the caller provides a directory descriptor and a
1748 * directory entry name for the file, use them! This prevents
1749 * race conditions if filesystem paths change beneath our feet.
1751 if (dirfd != -1) {
1752 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1753 if (errno == ENOENT) {
1754 if (got_fileindex_entry_has_file_on_disk(ie))
1755 *status = GOT_STATUS_MISSING;
1756 else
1757 *status = GOT_STATUS_DELETE;
1758 goto done;
1760 err = got_error_from_errno2("fstatat", abspath);
1761 goto done;
1763 } else {
1764 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1765 if (fd == -1 && errno != ENOENT &&
1766 !got_err_open_nofollow_on_symlink())
1767 return got_error_from_errno2("open", abspath);
1768 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1769 if (lstat(abspath, sb) == -1)
1770 return got_error_from_errno2("lstat", abspath);
1771 } else if (fd == -1 || fstat(fd, sb) == -1) {
1772 if (errno == ENOENT) {
1773 if (got_fileindex_entry_has_file_on_disk(ie))
1774 *status = GOT_STATUS_MISSING;
1775 else
1776 *status = GOT_STATUS_DELETE;
1777 goto done;
1779 err = got_error_from_errno2("fstat", abspath);
1780 goto done;
1784 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1785 *status = GOT_STATUS_OBSTRUCTED;
1786 goto done;
1789 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1790 *status = GOT_STATUS_DELETE;
1791 goto done;
1792 } else if (!got_fileindex_entry_has_blob(ie) &&
1793 staged_status != GOT_STATUS_ADD) {
1794 *status = GOT_STATUS_ADD;
1795 goto done;
1798 if (!stat_info_differs(ie, sb))
1799 goto done;
1801 if (S_ISLNK(sb->st_mode) &&
1802 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1803 *status = GOT_STATUS_MODIFY;
1804 goto done;
1807 if (staged_status == GOT_STATUS_MODIFY ||
1808 staged_status == GOT_STATUS_ADD)
1809 got_fileindex_entry_get_staged_blob_id(&id, ie);
1810 else
1811 got_fileindex_entry_get_blob_id(&id, ie);
1813 fd1 = got_opentempfd();
1814 if (fd1 == -1) {
1815 err = got_error_from_errno("got_opentempfd");
1816 goto done;
1818 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1819 if (err)
1820 goto done;
1822 if (S_ISLNK(sb->st_mode)) {
1823 err = get_symlink_modification_status(status, ie,
1824 abspath, dirfd, de_name, blob);
1825 goto done;
1828 if (dirfd != -1) {
1829 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1830 if (fd == -1) {
1831 err = got_error_from_errno2("openat", abspath);
1832 goto done;
1836 f = fdopen(fd, "r");
1837 if (f == NULL) {
1838 err = got_error_from_errno2("fdopen", abspath);
1839 goto done;
1841 fd = -1;
1842 hdrlen = got_object_blob_get_hdrlen(blob);
1843 for (;;) {
1844 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1845 err = got_object_blob_read_block(&blen, blob);
1846 if (err)
1847 goto done;
1848 /* Skip length of blob object header first time around. */
1849 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1850 if (flen == 0 && ferror(f)) {
1851 err = got_error_from_errno("fread");
1852 goto done;
1854 if (blen - hdrlen == 0) {
1855 if (flen != 0)
1856 *status = GOT_STATUS_MODIFY;
1857 break;
1858 } else if (flen == 0) {
1859 if (blen - hdrlen != 0)
1860 *status = GOT_STATUS_MODIFY;
1861 break;
1862 } else if (blen - hdrlen == flen) {
1863 /* Skip blob object header first time around. */
1864 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1865 *status = GOT_STATUS_MODIFY;
1866 break;
1868 } else {
1869 *status = GOT_STATUS_MODIFY;
1870 break;
1872 hdrlen = 0;
1875 if (*status == GOT_STATUS_MODIFY) {
1876 rewind(f);
1877 err = get_modified_file_content_status(status, blob, ie->path,
1878 sb, f);
1879 } else if (xbit_differs(ie, sb->st_mode))
1880 *status = GOT_STATUS_MODE_CHANGE;
1881 done:
1882 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1883 err = got_error_from_errno("close");
1884 if (blob)
1885 got_object_blob_close(blob);
1886 if (f != NULL && fclose(f) == EOF && err == NULL)
1887 err = got_error_from_errno2("fclose", abspath);
1888 if (fd != -1 && close(fd) == -1 && err == NULL)
1889 err = got_error_from_errno2("close", abspath);
1890 return err;
1894 * Update timestamps in the file index if a file is unmodified and
1895 * we had to run a full content comparison to find out.
1897 static const struct got_error *
1898 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1899 struct got_fileindex_entry *ie, struct stat *sb)
1901 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1902 return got_fileindex_entry_update(ie, wt_fd, path,
1903 ie->blob_sha1, ie->commit_sha1, 1);
1905 return NULL;
1908 static const struct got_error *remove_ondisk_file(const char *, const char *);
1910 static const struct got_error *
1911 update_blob(struct got_worktree *worktree,
1912 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1913 struct got_tree_entry *te, const char *path,
1914 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1915 void *progress_arg)
1917 const struct got_error *err = NULL;
1918 struct got_blob_object *blob = NULL;
1919 char *ondisk_path = NULL;
1920 unsigned char status = GOT_STATUS_NO_CHANGE;
1921 struct stat sb;
1922 int fd1 = -1, fd2 = -1;
1923 int update_timestamps = 0;
1925 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1926 return got_error_from_errno("asprintf");
1928 if (ie) {
1929 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1930 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1931 goto done;
1933 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1934 repo);
1935 if (err)
1936 goto done;
1937 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1938 sb.st_mode = got_fileindex_perms_to_st(ie);
1939 } else {
1940 if (stat(ondisk_path, &sb) == -1) {
1941 if (errno != ENOENT && errno != ENOTDIR) {
1942 err = got_error_from_errno2("stat",
1943 ondisk_path);
1944 goto done;
1946 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1947 status = GOT_STATUS_UNVERSIONED;
1948 } else {
1949 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1950 status = GOT_STATUS_UNVERSIONED;
1951 else
1952 status = GOT_STATUS_OBSTRUCTED;
1956 if (status == GOT_STATUS_OBSTRUCTED) {
1957 if (ie)
1958 got_fileindex_entry_mark_skipped(ie);
1959 err = (*progress_cb)(progress_arg, status, path);
1960 goto done;
1962 if (status == GOT_STATUS_CONFLICT) {
1963 if (ie)
1964 got_fileindex_entry_mark_skipped(ie);
1965 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1966 path);
1967 goto done;
1970 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1971 if (status == GOT_STATUS_UNVERSIONED) {
1972 err = (*progress_cb)(progress_arg, status, path);
1973 } else if (status != GOT_STATUS_NO_CHANGE &&
1974 status != GOT_STATUS_DELETE &&
1975 status != GOT_STATUS_NONEXISTENT &&
1976 status != GOT_STATUS_MISSING) {
1977 err = (*progress_cb)(progress_arg,
1978 GOT_STATUS_CANNOT_DELETE, path);
1979 } else if (ie) {
1980 if (status != GOT_STATUS_DELETE &&
1981 status != GOT_STATUS_NONEXISTENT &&
1982 status != GOT_STATUS_MISSING) {
1983 err = remove_ondisk_file(worktree->root_path,
1984 ie->path);
1985 if (err && !(err->code == GOT_ERR_ERRNO &&
1986 errno == ENOENT))
1987 goto done;
1989 got_fileindex_entry_remove(fileindex, ie);
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1991 ie->path);
1993 goto done; /* nothing else to do */
1996 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1997 (S_ISLNK(te->mode) ||
1998 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
2000 * This is a regular file or an installed bad symlink.
2001 * If the file index indicates that this file is already
2002 * up-to-date with respect to the repository we can skip
2003 * updating contents of this file.
2005 if (got_fileindex_entry_has_commit(ie) &&
2006 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
2007 SHA1_DIGEST_LENGTH) == 0) {
2008 /* Same commit. */
2009 err = sync_timestamps(worktree->root_fd,
2010 path, status, ie, &sb);
2011 if (err)
2012 goto done;
2013 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2014 path);
2015 goto done;
2017 if (got_fileindex_entry_has_blob(ie) &&
2018 memcmp(ie->blob_sha1, te->id.sha1,
2019 SHA1_DIGEST_LENGTH) == 0) {
2020 /* Different commit but the same blob. */
2021 if (got_fileindex_entry_has_commit(ie)) {
2022 /* Update the base commit ID of this file. */
2023 memcpy(ie->commit_sha1,
2024 worktree->base_commit_id->sha1,
2025 sizeof(ie->commit_sha1));
2027 err = sync_timestamps(worktree->root_fd,
2028 path, status, ie, &sb);
2029 if (err)
2030 goto done;
2031 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2032 path);
2033 goto done;
2037 fd1 = got_opentempfd();
2038 if (fd1 == -1) {
2039 err = got_error_from_errno("got_opentempfd");
2040 goto done;
2042 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2043 if (err)
2044 goto done;
2046 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2047 struct got_blob_object *blob2 = NULL;
2048 char *label_orig = NULL;
2049 if (got_fileindex_entry_has_blob(ie)) {
2050 fd2 = got_opentempfd();
2051 if (fd2 == -1) {
2052 err = got_error_from_errno("got_opentempfd");
2053 goto done;
2055 struct got_object_id id2;
2056 got_fileindex_entry_get_blob_id(&id2, ie);
2057 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2058 fd2);
2059 if (err)
2060 goto done;
2062 if (got_fileindex_entry_has_commit(ie)) {
2063 char id_str[SHA1_DIGEST_STRING_LENGTH];
2064 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2065 sizeof(id_str)) == NULL) {
2066 err = got_error_path(id_str,
2067 GOT_ERR_BAD_OBJ_ID_STR);
2068 goto done;
2070 if (asprintf(&label_orig, "%s: commit %s",
2071 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2072 err = got_error_from_errno("asprintf");
2073 goto done;
2076 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2077 char *link_target;
2078 err = got_object_blob_read_to_str(&link_target, blob);
2079 if (err)
2080 goto done;
2081 err = merge_symlink(worktree, blob2, ondisk_path, path,
2082 label_orig, link_target, worktree->base_commit_id,
2083 repo, progress_cb, progress_arg);
2084 free(link_target);
2085 } else {
2086 err = merge_blob(&update_timestamps, worktree, blob2,
2087 ondisk_path, path, sb.st_mode, label_orig, blob,
2088 worktree->base_commit_id, repo,
2089 progress_cb, progress_arg);
2091 free(label_orig);
2092 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2093 err = got_error_from_errno("close");
2094 goto done;
2096 if (blob2)
2097 got_object_blob_close(blob2);
2098 if (err)
2099 goto done;
2101 * Do not update timestamps of files with local changes.
2102 * Otherwise, a future status walk would treat them as
2103 * unmodified files again.
2105 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2106 blob->id.sha1, worktree->base_commit_id->sha1,
2107 update_timestamps);
2108 } else if (status == GOT_STATUS_MODE_CHANGE) {
2109 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2110 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2111 } else if (status == GOT_STATUS_DELETE) {
2112 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2113 if (err)
2114 goto done;
2115 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2116 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2117 if (err)
2118 goto done;
2119 } else {
2120 int is_bad_symlink = 0;
2121 if (S_ISLNK(te->mode)) {
2122 err = install_symlink(&is_bad_symlink, worktree,
2123 ondisk_path, path, blob,
2124 status == GOT_STATUS_MISSING, 0,
2125 status == GOT_STATUS_UNVERSIONED, 0,
2126 repo, progress_cb, progress_arg);
2127 } else {
2128 err = install_blob(worktree, ondisk_path, path,
2129 te->mode, sb.st_mode, blob,
2130 status == GOT_STATUS_MISSING, 0, 0,
2131 status == GOT_STATUS_UNVERSIONED,
2132 &update_timestamps,
2133 repo, progress_cb, progress_arg);
2135 if (err)
2136 goto done;
2138 if (ie) {
2139 err = got_fileindex_entry_update(ie,
2140 worktree->root_fd, path, blob->id.sha1,
2141 worktree->base_commit_id->sha1, 1);
2142 } else {
2143 err = create_fileindex_entry(&ie, fileindex,
2144 worktree->base_commit_id, worktree->root_fd, path,
2145 &blob->id, update_timestamps);
2147 if (err)
2148 goto done;
2150 if (is_bad_symlink) {
2151 got_fileindex_entry_filetype_set(ie,
2152 GOT_FILEIDX_MODE_BAD_SYMLINK);
2156 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2157 err = got_error_from_errno("close");
2158 goto done;
2160 got_object_blob_close(blob);
2161 done:
2162 free(ondisk_path);
2163 return err;
2166 static const struct got_error *
2167 remove_ondisk_file(const char *root_path, const char *path)
2169 const struct got_error *err = NULL;
2170 char *ondisk_path = NULL, *parent = NULL;
2172 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2173 return got_error_from_errno("asprintf");
2175 if (unlink(ondisk_path) == -1) {
2176 if (errno != ENOENT)
2177 err = got_error_from_errno2("unlink", ondisk_path);
2178 } else {
2179 size_t root_len = strlen(root_path);
2180 err = got_path_dirname(&parent, ondisk_path);
2181 if (err)
2182 goto done;
2183 while (got_path_cmp(parent, root_path,
2184 strlen(parent), root_len) != 0) {
2185 free(ondisk_path);
2186 ondisk_path = parent;
2187 parent = NULL;
2188 if (rmdir(ondisk_path) == -1) {
2189 if (errno != ENOTEMPTY)
2190 err = got_error_from_errno2("rmdir",
2191 ondisk_path);
2192 break;
2194 err = got_path_dirname(&parent, ondisk_path);
2195 if (err)
2196 break;
2199 done:
2200 free(ondisk_path);
2201 free(parent);
2202 return err;
2205 static const struct got_error *
2206 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2207 struct got_fileindex_entry *ie, struct got_repository *repo,
2208 got_worktree_checkout_cb progress_cb, void *progress_arg)
2210 const struct got_error *err = NULL;
2211 unsigned char status;
2212 struct stat sb;
2213 char *ondisk_path;
2215 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2216 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2218 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2219 == -1)
2220 return got_error_from_errno("asprintf");
2222 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2223 if (err)
2224 goto done;
2226 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2227 char ondisk_target[PATH_MAX];
2228 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2229 sizeof(ondisk_target));
2230 if (ondisk_len == -1) {
2231 err = got_error_from_errno2("readlink", ondisk_path);
2232 goto done;
2234 ondisk_target[ondisk_len] = '\0';
2235 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2236 NULL, NULL, /* XXX pass common ancestor info? */
2237 ondisk_target, ondisk_path);
2238 if (err)
2239 goto done;
2240 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2241 ie->path);
2242 goto done;
2245 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2246 status == GOT_STATUS_ADD) {
2247 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2248 if (err)
2249 goto done;
2251 * Preserve the working file and change the deleted blob's
2252 * entry into a schedule-add entry.
2254 err = got_fileindex_entry_update(ie, worktree->root_fd,
2255 ie->path, NULL, NULL, 0);
2256 } else {
2257 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2258 if (err)
2259 goto done;
2260 if (status == GOT_STATUS_NO_CHANGE) {
2261 err = remove_ondisk_file(worktree->root_path, ie->path);
2262 if (err)
2263 goto done;
2265 got_fileindex_entry_remove(fileindex, ie);
2267 done:
2268 free(ondisk_path);
2269 return err;
2272 struct diff_cb_arg {
2273 struct got_fileindex *fileindex;
2274 struct got_worktree *worktree;
2275 struct got_repository *repo;
2276 got_worktree_checkout_cb progress_cb;
2277 void *progress_arg;
2278 got_cancel_cb cancel_cb;
2279 void *cancel_arg;
2282 static const struct got_error *
2283 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2284 struct got_tree_entry *te, const char *parent_path)
2286 const struct got_error *err = NULL;
2287 struct diff_cb_arg *a = arg;
2289 if (a->cancel_cb) {
2290 err = a->cancel_cb(a->cancel_arg);
2291 if (err)
2292 return err;
2295 return update_blob(a->worktree, a->fileindex, ie, te,
2296 ie->path, a->repo, a->progress_cb, a->progress_arg);
2299 static const struct got_error *
2300 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2302 const struct got_error *err = NULL;
2303 struct diff_cb_arg *a = arg;
2305 if (a->cancel_cb) {
2306 err = a->cancel_cb(a->cancel_arg);
2307 if (err)
2308 return err;
2311 return delete_blob(a->worktree, a->fileindex, ie,
2312 a->repo, a->progress_cb, a->progress_arg);
2315 static const struct got_error *
2316 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2318 const struct got_error *err = NULL;
2319 struct diff_cb_arg *a = arg;
2320 char *path;
2322 if (a->cancel_cb) {
2323 err = a->cancel_cb(a->cancel_arg);
2324 if (err)
2325 return err;
2328 if (got_object_tree_entry_is_submodule(te))
2329 return NULL;
2331 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2332 return NULL;
2334 if (asprintf(&path, "%s%s%s", parent_path,
2335 parent_path[0] ? "/" : "", te->name)
2336 == -1)
2337 return got_error_from_errno("asprintf");
2339 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2340 a->repo, a->progress_cb, a->progress_arg);
2342 free(path);
2343 return err;
2346 const struct got_error *
2347 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2349 uint32_t uuid_status;
2351 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2352 if (uuid_status != uuid_s_ok) {
2353 *uuidstr = NULL;
2354 return got_error_uuid(uuid_status, "uuid_to_string");
2357 return NULL;
2360 static const struct got_error *
2361 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2363 const struct got_error *err = NULL;
2364 char *uuidstr = NULL;
2366 *refname = NULL;
2368 err = got_worktree_get_uuid(&uuidstr, worktree);
2369 if (err)
2370 return err;
2372 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 *refname = NULL;
2376 free(uuidstr);
2377 return err;
2380 const struct got_error *
2381 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2382 const char *prefix)
2384 return get_ref_name(refname, worktree, prefix);
2387 static const struct got_error *
2388 get_base_ref_name(char **refname, struct got_worktree *worktree)
2390 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2393 static const struct got_error *
2394 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2396 return get_ref_name(refname, worktree,
2397 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2400 static const struct got_error *
2401 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2403 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2406 static const struct got_error *
2407 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2409 return get_ref_name(refname, worktree,
2410 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2413 static const struct got_error *
2414 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2416 return get_ref_name(refname, worktree,
2417 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2420 static const struct got_error *
2421 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2423 return get_ref_name(refname, worktree,
2424 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2427 static const struct got_error *
2428 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2430 return get_ref_name(refname, worktree,
2431 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2434 static const struct got_error *
2435 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2437 return get_ref_name(refname, worktree,
2438 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2441 static const struct got_error *
2442 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2444 return get_ref_name(refname, worktree,
2445 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2448 const struct got_error *
2449 got_worktree_get_histedit_script_path(char **path,
2450 struct got_worktree *worktree)
2452 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2453 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2454 *path = NULL;
2455 return got_error_from_errno("asprintf");
2457 return NULL;
2460 static const struct got_error *
2461 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2463 return get_ref_name(refname, worktree,
2464 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2467 static const struct got_error *
2468 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2470 return get_ref_name(refname, worktree,
2471 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2475 * Prevent Git's garbage collector from deleting our base commit by
2476 * setting a reference to our base commit's ID.
2478 static const struct got_error *
2479 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2481 const struct got_error *err = NULL;
2482 struct got_reference *ref = NULL;
2483 char *refname;
2485 err = get_base_ref_name(&refname, worktree);
2486 if (err)
2487 return err;
2489 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2490 if (err)
2491 goto done;
2493 err = got_ref_write(ref, repo);
2494 done:
2495 free(refname);
2496 if (ref)
2497 got_ref_close(ref);
2498 return err;
2501 static const struct got_error *
2502 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2504 const struct got_error *err = NULL;
2506 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2507 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2508 err = got_error_from_errno("asprintf");
2509 *fileindex_path = NULL;
2511 return err;
2515 static const struct got_error *
2516 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2517 struct got_worktree *worktree)
2519 const struct got_error *err = NULL;
2520 FILE *index = NULL;
2522 *fileindex_path = NULL;
2523 *fileindex = got_fileindex_alloc();
2524 if (*fileindex == NULL)
2525 return got_error_from_errno("got_fileindex_alloc");
2527 err = get_fileindex_path(fileindex_path, worktree);
2528 if (err)
2529 goto done;
2531 index = fopen(*fileindex_path, "rbe");
2532 if (index == NULL) {
2533 if (errno != ENOENT)
2534 err = got_error_from_errno2("fopen", *fileindex_path);
2535 } else {
2536 err = got_fileindex_read(*fileindex, index);
2537 if (fclose(index) == EOF && err == NULL)
2538 err = got_error_from_errno("fclose");
2540 done:
2541 if (err) {
2542 free(*fileindex_path);
2543 *fileindex_path = NULL;
2544 got_fileindex_free(*fileindex);
2545 *fileindex = NULL;
2547 return err;
2550 struct bump_base_commit_id_arg {
2551 struct got_object_id *base_commit_id;
2552 const char *path;
2553 size_t path_len;
2554 const char *entry_name;
2555 got_worktree_checkout_cb progress_cb;
2556 void *progress_arg;
2559 static const struct got_error *
2560 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2562 const struct got_error *err;
2563 struct bump_base_commit_id_arg *a = arg;
2565 if (a->entry_name) {
2566 if (strcmp(ie->path, a->path) != 0)
2567 return NULL;
2568 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2569 return NULL;
2571 if (got_fileindex_entry_was_skipped(ie))
2572 return NULL;
2574 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2575 SHA1_DIGEST_LENGTH) == 0)
2576 return NULL;
2578 if (a->progress_cb) {
2579 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2580 ie->path);
2581 if (err)
2582 return err;
2584 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2585 return NULL;
2588 /* Bump base commit ID of all files within an updated part of the work tree. */
2589 static const struct got_error *
2590 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2591 struct got_fileindex *fileindex,
2592 got_worktree_checkout_cb progress_cb, void *progress_arg)
2594 struct bump_base_commit_id_arg bbc_arg;
2596 bbc_arg.base_commit_id = worktree->base_commit_id;
2597 bbc_arg.entry_name = NULL;
2598 bbc_arg.path = "";
2599 bbc_arg.path_len = 0;
2600 bbc_arg.progress_cb = progress_cb;
2601 bbc_arg.progress_arg = progress_arg;
2603 return got_fileindex_for_each_entry_safe(fileindex,
2604 bump_base_commit_id, &bbc_arg);
2607 static const struct got_error *
2608 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2610 const struct got_error *err = NULL;
2611 char *new_fileindex_path = NULL;
2612 FILE *new_index = NULL;
2613 struct timespec timeout;
2615 err = got_opentemp_named(&new_fileindex_path, &new_index,
2616 fileindex_path, "");
2617 if (err)
2618 goto done;
2620 err = got_fileindex_write(fileindex, new_index);
2621 if (err)
2622 goto done;
2624 if (rename(new_fileindex_path, fileindex_path) != 0) {
2625 err = got_error_from_errno3("rename", new_fileindex_path,
2626 fileindex_path);
2627 unlink(new_fileindex_path);
2631 * Sleep for a short amount of time to ensure that files modified after
2632 * this program exits have a different time stamp from the one which
2633 * was recorded in the file index.
2635 timeout.tv_sec = 0;
2636 timeout.tv_nsec = 1;
2637 nanosleep(&timeout, NULL);
2638 done:
2639 if (new_index)
2640 fclose(new_index);
2641 free(new_fileindex_path);
2642 return err;
2645 static const struct got_error *
2646 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2647 struct got_object_id **tree_id, const char *wt_relpath,
2648 struct got_commit_object *base_commit, struct got_worktree *worktree,
2649 struct got_repository *repo)
2651 const struct got_error *err = NULL;
2652 struct got_object_id *id = NULL;
2653 char *in_repo_path = NULL;
2654 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2656 *entry_type = GOT_OBJ_TYPE_ANY;
2657 *tree_relpath = NULL;
2658 *tree_id = NULL;
2660 if (wt_relpath[0] == '\0') {
2661 /* Check out all files within the work tree. */
2662 *entry_type = GOT_OBJ_TYPE_TREE;
2663 *tree_relpath = strdup("");
2664 if (*tree_relpath == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2668 err = got_object_id_by_path(tree_id, repo, base_commit,
2669 worktree->path_prefix);
2670 if (err)
2671 goto done;
2672 return NULL;
2675 /* Check out a subset of files in the work tree. */
2677 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2678 is_root_wt ? "" : "/", wt_relpath) == -1) {
2679 err = got_error_from_errno("asprintf");
2680 goto done;
2683 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2684 if (err)
2685 goto done;
2687 free(in_repo_path);
2688 in_repo_path = NULL;
2690 err = got_object_get_type(entry_type, repo, id);
2691 if (err)
2692 goto done;
2694 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2695 /* Check out a single file. */
2696 if (strchr(wt_relpath, '/') == NULL) {
2697 /* Check out a single file in work tree's root dir. */
2698 in_repo_path = strdup(worktree->path_prefix);
2699 if (in_repo_path == NULL) {
2700 err = got_error_from_errno("strdup");
2701 goto done;
2703 *tree_relpath = strdup("");
2704 if (*tree_relpath == NULL) {
2705 err = got_error_from_errno("strdup");
2706 goto done;
2708 } else {
2709 /* Check out a single file in a subdirectory. */
2710 err = got_path_dirname(tree_relpath, wt_relpath);
2711 if (err)
2712 return err;
2713 if (asprintf(&in_repo_path, "%s%s%s",
2714 worktree->path_prefix, is_root_wt ? "" : "/",
2715 *tree_relpath) == -1) {
2716 err = got_error_from_errno("asprintf");
2717 goto done;
2720 err = got_object_id_by_path(tree_id, repo,
2721 base_commit, in_repo_path);
2722 } else {
2723 /* Check out all files within a subdirectory. */
2724 *tree_id = got_object_id_dup(id);
2725 if (*tree_id == NULL) {
2726 err = got_error_from_errno("got_object_id_dup");
2727 goto done;
2729 *tree_relpath = strdup(wt_relpath);
2730 if (*tree_relpath == NULL) {
2731 err = got_error_from_errno("strdup");
2732 goto done;
2735 done:
2736 free(id);
2737 free(in_repo_path);
2738 if (err) {
2739 *entry_type = GOT_OBJ_TYPE_ANY;
2740 free(*tree_relpath);
2741 *tree_relpath = NULL;
2742 free(*tree_id);
2743 *tree_id = NULL;
2745 return err;
2748 static const struct got_error *
2749 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2750 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2751 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2752 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2754 const struct got_error *err = NULL;
2755 struct got_commit_object *commit = NULL;
2756 struct got_tree_object *tree = NULL;
2757 struct got_fileindex_diff_tree_cb diff_cb;
2758 struct diff_cb_arg arg;
2760 err = ref_base_commit(worktree, repo);
2761 if (err) {
2762 if (!(err->code == GOT_ERR_ERRNO &&
2763 (errno == EACCES || errno == EROFS)))
2764 goto done;
2765 err = (*progress_cb)(progress_arg,
2766 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2767 if (err)
2768 return err;
2771 err = got_object_open_as_commit(&commit, repo,
2772 worktree->base_commit_id);
2773 if (err)
2774 goto done;
2776 err = got_object_open_as_tree(&tree, repo, tree_id);
2777 if (err)
2778 goto done;
2780 if (entry_name &&
2781 got_object_tree_find_entry(tree, entry_name) == NULL) {
2782 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2783 goto done;
2786 diff_cb.diff_old_new = diff_old_new;
2787 diff_cb.diff_old = diff_old;
2788 diff_cb.diff_new = diff_new;
2789 arg.fileindex = fileindex;
2790 arg.worktree = worktree;
2791 arg.repo = repo;
2792 arg.progress_cb = progress_cb;
2793 arg.progress_arg = progress_arg;
2794 arg.cancel_cb = cancel_cb;
2795 arg.cancel_arg = cancel_arg;
2796 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2797 entry_name, repo, &diff_cb, &arg);
2798 done:
2799 if (tree)
2800 got_object_tree_close(tree);
2801 if (commit)
2802 got_object_commit_close(commit);
2803 return err;
2806 const struct got_error *
2807 got_worktree_checkout_files(struct got_worktree *worktree,
2808 struct got_pathlist_head *paths, struct got_repository *repo,
2809 got_worktree_checkout_cb progress_cb, void *progress_arg,
2810 got_cancel_cb cancel_cb, void *cancel_arg)
2812 const struct got_error *err = NULL, *sync_err, *unlockerr;
2813 struct got_commit_object *commit = NULL;
2814 struct got_tree_object *tree = NULL;
2815 struct got_fileindex *fileindex = NULL;
2816 char *fileindex_path = NULL;
2817 struct got_pathlist_entry *pe;
2818 struct tree_path_data {
2819 STAILQ_ENTRY(tree_path_data) entry;
2820 struct got_object_id *tree_id;
2821 int entry_type;
2822 char *relpath;
2823 char *entry_name;
2824 } *tpd = NULL;
2825 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2827 STAILQ_INIT(&tree_paths);
2829 err = lock_worktree(worktree, LOCK_EX);
2830 if (err)
2831 return err;
2833 err = got_object_open_as_commit(&commit, repo,
2834 worktree->base_commit_id);
2835 if (err)
2836 goto done;
2838 /* Map all specified paths to in-repository trees. */
2839 TAILQ_FOREACH(pe, paths, entry) {
2840 tpd = malloc(sizeof(*tpd));
2841 if (tpd == NULL) {
2842 err = got_error_from_errno("malloc");
2843 goto done;
2846 err = find_tree_entry_for_checkout(&tpd->entry_type,
2847 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2848 worktree, repo);
2849 if (err) {
2850 free(tpd);
2851 goto done;
2854 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2855 err = got_path_basename(&tpd->entry_name, pe->path);
2856 if (err) {
2857 free(tpd->relpath);
2858 free(tpd->tree_id);
2859 free(tpd);
2860 goto done;
2862 } else
2863 tpd->entry_name = NULL;
2865 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2869 * Read the file index.
2870 * Checking out files is supposed to be an idempotent operation.
2871 * If the on-disk file index is incomplete we will try to complete it.
2873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2874 if (err)
2875 goto done;
2877 tpd = STAILQ_FIRST(&tree_paths);
2878 TAILQ_FOREACH(pe, paths, entry) {
2879 struct bump_base_commit_id_arg bbc_arg;
2881 err = checkout_files(worktree, fileindex, tpd->relpath,
2882 tpd->tree_id, tpd->entry_name, repo,
2883 progress_cb, progress_arg, cancel_cb, cancel_arg);
2884 if (err)
2885 break;
2887 bbc_arg.base_commit_id = worktree->base_commit_id;
2888 bbc_arg.entry_name = tpd->entry_name;
2889 bbc_arg.path = pe->path;
2890 bbc_arg.path_len = pe->path_len;
2891 bbc_arg.progress_cb = progress_cb;
2892 bbc_arg.progress_arg = progress_arg;
2893 err = got_fileindex_for_each_entry_safe(fileindex,
2894 bump_base_commit_id, &bbc_arg);
2895 if (err)
2896 break;
2898 tpd = STAILQ_NEXT(tpd, entry);
2900 sync_err = sync_fileindex(fileindex, fileindex_path);
2901 if (sync_err && err == NULL)
2902 err = sync_err;
2903 done:
2904 free(fileindex_path);
2905 if (tree)
2906 got_object_tree_close(tree);
2907 if (commit)
2908 got_object_commit_close(commit);
2909 if (fileindex)
2910 got_fileindex_free(fileindex);
2911 while (!STAILQ_EMPTY(&tree_paths)) {
2912 tpd = STAILQ_FIRST(&tree_paths);
2913 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2914 free(tpd->relpath);
2915 free(tpd->tree_id);
2916 free(tpd);
2918 unlockerr = lock_worktree(worktree, LOCK_SH);
2919 if (unlockerr && err == NULL)
2920 err = unlockerr;
2921 return err;
2924 static const struct got_error *
2925 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2926 struct got_fileindex_entry *ie, const char *ondisk_path,
2927 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2928 int restoring_missing_file, int reverting_versioned_file,
2929 int path_is_unversioned, int allow_bad_symlinks,
2930 struct got_repository *repo,
2931 got_worktree_checkout_cb progress_cb, void *progress_arg)
2933 const struct got_error *err = NULL;
2934 int is_bad_symlink = 0;
2936 if (S_ISLNK(mode2)) {
2937 err = install_symlink(&is_bad_symlink,
2938 worktree, ondisk_path, path2, blob2,
2939 restoring_missing_file,
2940 reverting_versioned_file,
2941 path_is_unversioned, allow_bad_symlinks,
2942 repo, progress_cb, progress_arg);
2943 } else {
2944 err = install_blob(worktree, ondisk_path, path2,
2945 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2946 restoring_missing_file, reverting_versioned_file, 0,
2947 path_is_unversioned, NULL, repo,
2948 progress_cb, progress_arg);
2950 if (err)
2951 return err;
2952 if (ie == NULL) {
2953 /* Adding an unversioned file. */
2954 err = got_fileindex_entry_alloc(&ie, path2);
2955 if (err)
2956 return err;
2957 err = got_fileindex_entry_update(ie,
2958 worktree->root_fd, path2, NULL, NULL, 1);
2959 if (err) {
2960 got_fileindex_entry_free(ie);
2961 return err;
2963 err = got_fileindex_entry_add(fileindex, ie);
2964 if (err) {
2965 got_fileindex_entry_free(ie);
2966 return err;
2968 } else {
2969 /* Re-adding a locally deleted file. */
2970 err = got_fileindex_entry_update(ie,
2971 worktree->root_fd, path2, ie->blob_sha1,
2972 worktree->base_commit_id->sha1, 0);
2973 if (err)
2974 return err;
2977 if (is_bad_symlink) {
2978 got_fileindex_entry_filetype_set(ie,
2979 GOT_FILEIDX_MODE_BAD_SYMLINK);
2982 return NULL;
2985 struct merge_file_cb_arg {
2986 struct got_worktree *worktree;
2987 struct got_fileindex *fileindex;
2988 got_worktree_checkout_cb progress_cb;
2989 void *progress_arg;
2990 got_cancel_cb cancel_cb;
2991 void *cancel_arg;
2992 const char *label_orig;
2993 struct got_object_id *commit_id2;
2994 int allow_bad_symlinks;
2997 static const struct got_error *
2998 merge_file_cb(void *arg, struct got_blob_object *blob1,
2999 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3000 struct got_object_id *id1, struct got_object_id *id2,
3001 const char *path1, const char *path2,
3002 mode_t mode1, mode_t mode2, struct got_repository *repo)
3004 static const struct got_error *err = NULL;
3005 struct merge_file_cb_arg *a = arg;
3006 struct got_fileindex_entry *ie;
3007 char *ondisk_path = NULL;
3008 struct stat sb;
3009 unsigned char status;
3010 int local_changes_subsumed;
3011 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3012 char *id_str = NULL, *label_deriv2 = NULL;
3013 struct got_object_id *id = NULL;
3015 if (blob1 && blob2) {
3016 ie = got_fileindex_entry_get(a->fileindex, path2,
3017 strlen(path2));
3018 if (ie == NULL)
3019 return (*a->progress_cb)(a->progress_arg,
3020 GOT_STATUS_MISSING, path2);
3022 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3023 path2) == -1)
3024 return got_error_from_errno("asprintf");
3026 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3027 repo);
3028 if (err)
3029 goto done;
3031 if (status == GOT_STATUS_DELETE) {
3032 err = (*a->progress_cb)(a->progress_arg,
3033 GOT_STATUS_MERGE, path2);
3034 goto done;
3036 if (status != GOT_STATUS_NO_CHANGE &&
3037 status != GOT_STATUS_MODIFY &&
3038 status != GOT_STATUS_CONFLICT &&
3039 status != GOT_STATUS_ADD) {
3040 err = (*a->progress_cb)(a->progress_arg, status, path2);
3041 goto done;
3044 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3045 char *link_target2;
3046 err = got_object_blob_read_to_str(&link_target2, blob2);
3047 if (err)
3048 goto done;
3049 err = merge_symlink(a->worktree, blob1, ondisk_path,
3050 path2, a->label_orig, link_target2, a->commit_id2,
3051 repo, a->progress_cb, a->progress_arg);
3052 free(link_target2);
3053 } else {
3054 int fd;
3056 f_orig = got_opentemp();
3057 if (f_orig == NULL) {
3058 err = got_error_from_errno("got_opentemp");
3059 goto done;
3061 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3062 f_orig, blob1);
3063 if (err)
3064 goto done;
3066 f_deriv2 = got_opentemp();
3067 if (f_deriv2 == NULL)
3068 goto done;
3069 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3070 f_deriv2, blob2);
3071 if (err)
3072 goto done;
3074 fd = open(ondisk_path,
3075 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3076 if (fd == -1) {
3077 err = got_error_from_errno2("open",
3078 ondisk_path);
3079 goto done;
3081 f_deriv = fdopen(fd, "r");
3082 if (f_deriv == NULL) {
3083 err = got_error_from_errno2("fdopen",
3084 ondisk_path);
3085 close(fd);
3086 goto done;
3088 err = got_object_id_str(&id_str, a->commit_id2);
3089 if (err)
3090 goto done;
3091 if (asprintf(&label_deriv2, "%s: commit %s",
3092 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3093 err = got_error_from_errno("asprintf");
3094 goto done;
3096 err = merge_file(&local_changes_subsumed, a->worktree,
3097 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3098 mode2, a->label_orig, NULL, label_deriv2,
3099 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3100 a->progress_cb, a->progress_arg);
3102 } else if (blob1) {
3103 ie = got_fileindex_entry_get(a->fileindex, path1,
3104 strlen(path1));
3105 if (ie == NULL)
3106 return (*a->progress_cb)(a->progress_arg,
3107 GOT_STATUS_MISSING, path1);
3109 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3110 path1) == -1)
3111 return got_error_from_errno("asprintf");
3113 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3114 repo);
3115 if (err)
3116 goto done;
3118 switch (status) {
3119 case GOT_STATUS_NO_CHANGE:
3120 err = (*a->progress_cb)(a->progress_arg,
3121 GOT_STATUS_DELETE, path1);
3122 if (err)
3123 goto done;
3124 err = remove_ondisk_file(a->worktree->root_path, path1);
3125 if (err)
3126 goto done;
3127 if (ie)
3128 got_fileindex_entry_mark_deleted_from_disk(ie);
3129 break;
3130 case GOT_STATUS_DELETE:
3131 case GOT_STATUS_MISSING:
3132 err = (*a->progress_cb)(a->progress_arg,
3133 GOT_STATUS_DELETE, path1);
3134 if (err)
3135 goto done;
3136 if (ie)
3137 got_fileindex_entry_mark_deleted_from_disk(ie);
3138 break;
3139 case GOT_STATUS_MODIFY: {
3140 FILE *blob1_f;
3141 off_t blob1_size;
3142 int obj_type;
3144 * Delete the file only if its content already
3145 * exists in the repository.
3147 err = got_object_blob_file_create(&id, &blob1_f,
3148 &blob1_size, path1);
3149 if (err)
3150 goto done;
3151 if (fclose(blob1_f) == EOF) {
3152 err = got_error_from_errno("fclose");
3153 goto done;
3156 /* Implied existence check. */
3157 err = got_object_get_type(&obj_type, repo, id);
3158 if (err) {
3159 if (err->code != GOT_ERR_NO_OBJ)
3160 goto done;
3161 err = (*a->progress_cb)(a->progress_arg,
3162 GOT_STATUS_CANNOT_DELETE, path1);
3163 goto done;
3164 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3165 err = (*a->progress_cb)(a->progress_arg,
3166 GOT_STATUS_CANNOT_DELETE, path1);
3167 goto done;
3169 err = (*a->progress_cb)(a->progress_arg,
3170 GOT_STATUS_DELETE, path1);
3171 if (err)
3172 goto done;
3173 err = remove_ondisk_file(a->worktree->root_path,
3174 path1);
3175 if (err)
3176 goto done;
3177 if (ie)
3178 got_fileindex_entry_mark_deleted_from_disk(ie);
3179 break;
3181 case GOT_STATUS_ADD: {
3182 FILE *blob1_f;
3183 off_t blob1_size;
3185 * Delete the file only if its content already
3186 * exists in the repository.
3188 err = got_object_blob_file_create(&id, &blob1_f,
3189 &blob1_size, path1);
3190 if (err)
3191 goto done;
3192 if (fclose(blob1_f) == EOF) {
3193 err = got_error_from_errno("fclose");
3194 goto done;
3196 if (got_object_id_cmp(id, id1) == 0) {
3197 err = (*a->progress_cb)(a->progress_arg,
3198 GOT_STATUS_DELETE, path1);
3199 if (err)
3200 goto done;
3201 err = remove_ondisk_file(a->worktree->root_path,
3202 path1);
3203 if (err)
3204 goto done;
3205 if (ie)
3206 got_fileindex_entry_remove(a->fileindex,
3207 ie);
3208 } else {
3209 err = (*a->progress_cb)(a->progress_arg,
3210 GOT_STATUS_CANNOT_DELETE, path1);
3212 if (err)
3213 goto done;
3214 break;
3216 case GOT_STATUS_CONFLICT:
3217 err = (*a->progress_cb)(a->progress_arg,
3218 GOT_STATUS_CANNOT_DELETE, path1);
3219 if (err)
3220 goto done;
3221 break;
3222 case GOT_STATUS_OBSTRUCTED:
3223 err = (*a->progress_cb)(a->progress_arg, status, path1);
3224 if (err)
3225 goto done;
3226 break;
3227 default:
3228 break;
3230 } else if (blob2) {
3231 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3232 path2) == -1)
3233 return got_error_from_errno("asprintf");
3234 ie = got_fileindex_entry_get(a->fileindex, path2,
3235 strlen(path2));
3236 if (ie) {
3237 err = get_file_status(&status, &sb, ie, ondisk_path,
3238 -1, NULL, repo);
3239 if (err)
3240 goto done;
3241 if (status != GOT_STATUS_NO_CHANGE &&
3242 status != GOT_STATUS_MODIFY &&
3243 status != GOT_STATUS_CONFLICT &&
3244 status != GOT_STATUS_ADD &&
3245 status != GOT_STATUS_DELETE) {
3246 err = (*a->progress_cb)(a->progress_arg,
3247 status, path2);
3248 goto done;
3250 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3251 char *link_target2;
3252 err = got_object_blob_read_to_str(&link_target2,
3253 blob2);
3254 if (err)
3255 goto done;
3256 err = merge_symlink(a->worktree, NULL,
3257 ondisk_path, path2, a->label_orig,
3258 link_target2, a->commit_id2, repo,
3259 a->progress_cb, a->progress_arg);
3260 free(link_target2);
3261 } else if (S_ISREG(sb.st_mode)) {
3262 err = merge_blob(&local_changes_subsumed,
3263 a->worktree, NULL, ondisk_path, path2,
3264 sb.st_mode, a->label_orig, blob2,
3265 a->commit_id2, repo, a->progress_cb,
3266 a->progress_arg);
3267 } else if (status != GOT_STATUS_DELETE) {
3268 err = got_error_path(ondisk_path,
3269 GOT_ERR_FILE_OBSTRUCTED);
3271 if (err)
3272 goto done;
3273 if (status == GOT_STATUS_DELETE) {
3274 /* Re-add file with content from new blob. */
3275 err = add_file(a->worktree, a->fileindex, ie,
3276 ondisk_path, path2, blob2, mode2,
3277 0, 0, 0, a->allow_bad_symlinks,
3278 repo, a->progress_cb, a->progress_arg);
3279 if (err)
3280 goto done;
3282 } else {
3283 err = add_file(a->worktree, a->fileindex, NULL,
3284 ondisk_path, path2, blob2, mode2,
3285 0, 0, 1, a->allow_bad_symlinks,
3286 repo, a->progress_cb, a->progress_arg);
3287 if (err)
3288 goto done;
3291 done:
3292 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3293 err = got_error_from_errno("fclose");
3294 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3295 err = got_error_from_errno("fclose");
3296 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3297 err = got_error_from_errno("fclose");
3298 free(id_str);
3299 free(id);
3300 free(label_deriv2);
3301 free(ondisk_path);
3302 return err;
3305 struct check_mixed_commits_args {
3306 struct got_worktree *worktree;
3307 got_cancel_cb cancel_cb;
3308 void *cancel_arg;
3311 static const struct got_error *
3312 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3314 const struct got_error *err = NULL;
3315 struct check_mixed_commits_args *a = arg;
3317 if (a->cancel_cb) {
3318 err = a->cancel_cb(a->cancel_arg);
3319 if (err)
3320 return err;
3323 /* Reject merges into a work tree with mixed base commits. */
3324 if (got_fileindex_entry_has_commit(ie) &&
3325 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3326 SHA1_DIGEST_LENGTH) != 0)
3327 return got_error(GOT_ERR_MIXED_COMMITS);
3329 return NULL;
3332 const struct got_error *
3333 got_worktree_get_state(char *state, struct got_repository *repo,
3334 struct got_worktree *worktree,
3335 got_cancel_cb cancel_cb, void *cancel_arg)
3337 const struct got_error *err;
3338 struct got_object_id *base_id, *head_id = NULL;
3339 struct got_reference *head_ref;
3340 struct got_fileindex *fileindex = NULL;
3341 char *fileindex_path = NULL;
3342 struct check_mixed_commits_args cma;
3344 if (worktree == NULL)
3345 return got_error(GOT_ERR_NOT_WORKTREE);
3347 err = got_ref_open(&head_ref, repo,
3348 got_worktree_get_head_ref_name(worktree), 0);
3349 if (err)
3350 return err;
3352 err = got_ref_resolve(&head_id, repo, head_ref);
3353 if (err)
3354 goto done;
3356 *state = GOT_WORKTREE_STATE_UNKNOWN;
3357 base_id = got_worktree_get_base_commit_id(worktree);
3359 cma.worktree = worktree;
3360 cma.cancel_cb = cancel_cb;
3361 cma.cancel_arg = cancel_arg;
3363 if (got_object_id_cmp(base_id, head_id) == 0) {
3364 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3365 if (err)
3366 goto done;
3368 err = got_fileindex_for_each_entry_safe(fileindex,
3369 check_mixed_commits, &cma);
3370 if (err == NULL)
3371 *state = GOT_WORKTREE_STATE_UPTODATE;
3372 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3373 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3374 err = NULL;
3376 } else
3377 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3379 done:
3380 free(head_id);
3381 free(fileindex_path);
3382 got_ref_close(head_ref);
3383 if (fileindex != NULL)
3384 got_fileindex_free(fileindex);
3385 return err;
3388 struct check_merge_conflicts_arg {
3389 struct got_worktree *worktree;
3390 struct got_fileindex *fileindex;
3391 struct got_repository *repo;
3394 static const struct got_error *
3395 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3396 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3397 struct got_object_id *id1, struct got_object_id *id2,
3398 const char *path1, const char *path2,
3399 mode_t mode1, mode_t mode2, struct got_repository *repo)
3401 const struct got_error *err = NULL;
3402 struct check_merge_conflicts_arg *a = arg;
3403 unsigned char status;
3404 struct stat sb;
3405 struct got_fileindex_entry *ie;
3406 const char *path = path2 ? path2 : path1;
3407 struct got_object_id *id = id2 ? id2 : id1;
3408 char *ondisk_path;
3410 if (id == NULL)
3411 return NULL;
3413 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3414 if (ie == NULL)
3415 return NULL;
3417 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3418 == -1)
3419 return got_error_from_errno("asprintf");
3421 /* Reject merges into a work tree with conflicted files. */
3422 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3423 free(ondisk_path);
3424 if (err)
3425 return err;
3426 if (status == GOT_STATUS_CONFLICT)
3427 return got_error(GOT_ERR_CONFLICTS);
3429 return NULL;
3432 static const struct got_error *
3433 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3434 const char *fileindex_path, struct got_object_id *commit_id1,
3435 struct got_object_id *commit_id2, struct got_repository *repo,
3436 got_worktree_checkout_cb progress_cb, void *progress_arg,
3437 got_cancel_cb cancel_cb, void *cancel_arg)
3439 const struct got_error *err = NULL, *sync_err;
3440 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3441 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3442 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3443 struct check_merge_conflicts_arg cmc_arg;
3444 struct merge_file_cb_arg arg;
3445 char *label_orig = NULL;
3446 FILE *f1 = NULL, *f2 = NULL;
3447 int fd1 = -1, fd2 = -1;
3449 if (commit_id1) {
3450 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3451 if (err)
3452 goto done;
3453 err = got_object_id_by_path(&tree_id1, repo, commit1,
3454 worktree->path_prefix);
3455 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3456 goto done;
3458 if (tree_id1) {
3459 char *id_str;
3461 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3462 if (err)
3463 goto done;
3465 err = got_object_id_str(&id_str, commit_id1);
3466 if (err)
3467 goto done;
3469 if (asprintf(&label_orig, "%s: commit %s",
3470 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3471 err = got_error_from_errno("asprintf");
3472 free(id_str);
3473 goto done;
3475 free(id_str);
3477 f1 = got_opentemp();
3478 if (f1 == NULL) {
3479 err = got_error_from_errno("got_opentemp");
3480 goto done;
3484 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3485 if (err)
3486 goto done;
3488 err = got_object_id_by_path(&tree_id2, repo, commit2,
3489 worktree->path_prefix);
3490 if (err)
3491 goto done;
3493 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3494 if (err)
3495 goto done;
3497 f2 = got_opentemp();
3498 if (f2 == NULL) {
3499 err = got_error_from_errno("got_opentemp");
3500 goto done;
3503 fd1 = got_opentempfd();
3504 if (fd1 == -1) {
3505 err = got_error_from_errno("got_opentempfd");
3506 goto done;
3509 fd2 = got_opentempfd();
3510 if (fd2 == -1) {
3511 err = got_error_from_errno("got_opentempfd");
3512 goto done;
3515 cmc_arg.worktree = worktree;
3516 cmc_arg.fileindex = fileindex;
3517 cmc_arg.repo = repo;
3518 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3519 check_merge_conflicts, &cmc_arg, 0);
3520 if (err)
3521 goto done;
3523 arg.worktree = worktree;
3524 arg.fileindex = fileindex;
3525 arg.progress_cb = progress_cb;
3526 arg.progress_arg = progress_arg;
3527 arg.cancel_cb = cancel_cb;
3528 arg.cancel_arg = cancel_arg;
3529 arg.label_orig = label_orig;
3530 arg.commit_id2 = commit_id2;
3531 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3532 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3533 merge_file_cb, &arg, 1);
3534 sync_err = sync_fileindex(fileindex, fileindex_path);
3535 if (sync_err && err == NULL)
3536 err = sync_err;
3537 done:
3538 if (commit1)
3539 got_object_commit_close(commit1);
3540 if (commit2)
3541 got_object_commit_close(commit2);
3542 if (tree1)
3543 got_object_tree_close(tree1);
3544 if (tree2)
3545 got_object_tree_close(tree2);
3546 if (f1 && fclose(f1) == EOF && err == NULL)
3547 err = got_error_from_errno("fclose");
3548 if (f2 && fclose(f2) == EOF && err == NULL)
3549 err = got_error_from_errno("fclose");
3550 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3551 err = got_error_from_errno("close");
3552 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3553 err = got_error_from_errno("close");
3554 free(label_orig);
3555 return err;
3558 const struct got_error *
3559 got_worktree_merge_files(struct got_worktree *worktree,
3560 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3561 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3562 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3564 const struct got_error *err, *unlockerr;
3565 char *fileindex_path = NULL;
3566 struct got_fileindex *fileindex = NULL;
3567 struct check_mixed_commits_args cma;
3569 err = lock_worktree(worktree, LOCK_EX);
3570 if (err)
3571 return err;
3573 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3574 if (err)
3575 goto done;
3577 cma.worktree = worktree;
3578 cma.cancel_cb = cancel_cb;
3579 cma.cancel_arg = cancel_arg;
3581 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3582 &cma);
3583 if (err)
3584 goto done;
3586 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3587 commit_id2, repo, progress_cb, progress_arg,
3588 cancel_cb, cancel_arg);
3589 done:
3590 if (fileindex)
3591 got_fileindex_free(fileindex);
3592 free(fileindex_path);
3593 unlockerr = lock_worktree(worktree, LOCK_SH);
3594 if (unlockerr && err == NULL)
3595 err = unlockerr;
3596 return err;
3599 struct diff_dir_cb_arg {
3600 struct got_fileindex *fileindex;
3601 struct got_worktree *worktree;
3602 const char *status_path;
3603 size_t status_path_len;
3604 struct got_repository *repo;
3605 got_worktree_status_cb status_cb;
3606 void *status_arg;
3607 got_cancel_cb cancel_cb;
3608 void *cancel_arg;
3609 /* A pathlist containing per-directory pathlists of ignore patterns. */
3610 struct got_pathlist_head *ignores;
3611 int report_unchanged;
3612 int no_ignores;
3615 static const struct got_error *
3616 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3617 int dirfd, const char *de_name,
3618 got_worktree_status_cb status_cb, void *status_arg,
3619 struct got_repository *repo, int report_unchanged)
3621 const struct got_error *err = NULL;
3622 unsigned char status = GOT_STATUS_NO_CHANGE;
3623 unsigned char staged_status;
3624 struct stat sb;
3625 struct got_object_id blob_id, commit_id, staged_blob_id;
3626 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3627 struct got_object_id *staged_blob_idp = NULL;
3629 staged_status = get_staged_status(ie);
3630 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3631 if (err)
3632 return err;
3634 if (status == GOT_STATUS_NO_CHANGE &&
3635 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3636 return NULL;
3638 if (got_fileindex_entry_has_blob(ie))
3639 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3640 if (got_fileindex_entry_has_commit(ie))
3641 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3642 if (staged_status == GOT_STATUS_ADD ||
3643 staged_status == GOT_STATUS_MODIFY) {
3644 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3645 &staged_blob_id, ie);
3648 return (*status_cb)(status_arg, status, staged_status,
3649 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3652 static const struct got_error *
3653 status_old_new(void *arg, struct got_fileindex_entry *ie,
3654 struct dirent *de, const char *parent_path, int dirfd)
3656 const struct got_error *err = NULL;
3657 struct diff_dir_cb_arg *a = arg;
3658 char *abspath;
3660 if (a->cancel_cb) {
3661 err = a->cancel_cb(a->cancel_arg);
3662 if (err)
3663 return err;
3666 if (got_path_cmp(parent_path, a->status_path,
3667 strlen(parent_path), a->status_path_len) != 0 &&
3668 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3669 return NULL;
3671 if (parent_path[0]) {
3672 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3673 parent_path, de->d_name) == -1)
3674 return got_error_from_errno("asprintf");
3675 } else {
3676 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3677 de->d_name) == -1)
3678 return got_error_from_errno("asprintf");
3681 err = report_file_status(ie, abspath, dirfd, de->d_name,
3682 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3683 free(abspath);
3684 return err;
3687 static const struct got_error *
3688 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3690 const struct got_error *err;
3691 struct diff_dir_cb_arg *a = arg;
3692 struct got_object_id blob_id, commit_id;
3693 unsigned char status;
3695 if (a->cancel_cb) {
3696 err = a->cancel_cb(a->cancel_arg);
3697 if (err)
3698 return err;
3701 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3702 return NULL;
3704 got_fileindex_entry_get_blob_id(&blob_id, ie);
3705 got_fileindex_entry_get_commit_id(&commit_id, ie);
3706 if (got_fileindex_entry_has_file_on_disk(ie))
3707 status = GOT_STATUS_MISSING;
3708 else
3709 status = GOT_STATUS_DELETE;
3710 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3711 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3714 static void
3715 free_ignores(struct got_pathlist_head *ignores)
3717 struct got_pathlist_entry *pe;
3719 TAILQ_FOREACH(pe, ignores, entry) {
3720 struct got_pathlist_head *ignorelist = pe->data;
3722 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3724 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3727 static const struct got_error *
3728 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3730 const struct got_error *err = NULL;
3731 struct got_pathlist_entry *pe = NULL;
3732 struct got_pathlist_head *ignorelist;
3733 char *line = NULL, *pattern, *dirpath = NULL;
3734 size_t linesize = 0;
3735 ssize_t linelen;
3737 ignorelist = calloc(1, sizeof(*ignorelist));
3738 if (ignorelist == NULL)
3739 return got_error_from_errno("calloc");
3740 TAILQ_INIT(ignorelist);
3742 while ((linelen = getline(&line, &linesize, f)) != -1) {
3743 if (linelen > 0 && line[linelen - 1] == '\n')
3744 line[linelen - 1] = '\0';
3746 /* Git's ignores may contain comments. */
3747 if (line[0] == '#')
3748 continue;
3750 /* Git's negated patterns are not (yet?) supported. */
3751 if (line[0] == '!')
3752 continue;
3754 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3755 line) == -1) {
3756 err = got_error_from_errno("asprintf");
3757 goto done;
3759 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3760 if (err)
3761 goto done;
3763 if (ferror(f)) {
3764 err = got_error_from_errno("getline");
3765 goto done;
3768 dirpath = strdup(path);
3769 if (dirpath == NULL) {
3770 err = got_error_from_errno("strdup");
3771 goto done;
3773 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3774 done:
3775 free(line);
3776 if (err || pe == NULL) {
3777 free(dirpath);
3778 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3779 free(ignorelist);
3781 return err;
3784 static int
3785 match_path(const char *pattern, size_t pattern_len, const char *path,
3786 int flags)
3788 char buf[PATH_MAX];
3791 * Trailing slashes signify directories.
3792 * Append a * to make such patterns conform to fnmatch rules.
3794 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3795 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3796 return FNM_NOMATCH; /* XXX */
3798 return fnmatch(buf, path, flags);
3801 return fnmatch(pattern, path, flags);
3804 static int
3805 match_ignores(struct got_pathlist_head *ignores, const char *path)
3807 struct got_pathlist_entry *pe;
3809 /* Handle patterns which match in all directories. */
3810 TAILQ_FOREACH(pe, ignores, entry) {
3811 struct got_pathlist_head *ignorelist = pe->data;
3812 struct got_pathlist_entry *pi;
3814 TAILQ_FOREACH(pi, ignorelist, entry) {
3815 const char *p;
3817 if (pi->path_len < 3 ||
3818 strncmp(pi->path, "**/", 3) != 0)
3819 continue;
3820 p = path;
3821 while (*p) {
3822 if (match_path(pi->path + 3,
3823 pi->path_len - 3, p,
3824 FNM_PATHNAME | FNM_LEADING_DIR)) {
3825 /* Retry in next directory. */
3826 while (*p && *p != '/')
3827 p++;
3828 while (*p == '/')
3829 p++;
3830 continue;
3832 return 1;
3838 * The ignores pathlist contains ignore lists from children before
3839 * parents, so we can find the most specific ignorelist by walking
3840 * ignores backwards.
3842 pe = TAILQ_LAST(ignores, got_pathlist_head);
3843 while (pe) {
3844 if (got_path_is_child(path, pe->path, pe->path_len)) {
3845 struct got_pathlist_head *ignorelist = pe->data;
3846 struct got_pathlist_entry *pi;
3847 TAILQ_FOREACH(pi, ignorelist, entry) {
3848 int flags = FNM_LEADING_DIR;
3849 if (strstr(pi->path, "/**/") == NULL)
3850 flags |= FNM_PATHNAME;
3851 if (match_path(pi->path, pi->path_len,
3852 path, flags))
3853 continue;
3854 return 1;
3857 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3860 return 0;
3863 static const struct got_error *
3864 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3865 const char *path, int dirfd, const char *ignores_filename)
3867 const struct got_error *err = NULL;
3868 char *ignorespath;
3869 int fd = -1;
3870 FILE *ignoresfile = NULL;
3872 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3873 path[0] ? "/" : "", ignores_filename) == -1)
3874 return got_error_from_errno("asprintf");
3876 if (dirfd != -1) {
3877 fd = openat(dirfd, ignores_filename,
3878 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3879 if (fd == -1) {
3880 if (errno != ENOENT && errno != EACCES)
3881 err = got_error_from_errno2("openat",
3882 ignorespath);
3883 } else {
3884 ignoresfile = fdopen(fd, "r");
3885 if (ignoresfile == NULL)
3886 err = got_error_from_errno2("fdopen",
3887 ignorespath);
3888 else {
3889 fd = -1;
3890 err = read_ignores(ignores, path, ignoresfile);
3893 } else {
3894 ignoresfile = fopen(ignorespath, "re");
3895 if (ignoresfile == NULL) {
3896 if (errno != ENOENT && errno != EACCES)
3897 err = got_error_from_errno2("fopen",
3898 ignorespath);
3899 } else
3900 err = read_ignores(ignores, path, ignoresfile);
3903 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3904 err = got_error_from_errno2("fclose", path);
3905 if (fd != -1 && close(fd) == -1 && err == NULL)
3906 err = got_error_from_errno2("close", path);
3907 free(ignorespath);
3908 return err;
3911 static const struct got_error *
3912 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3913 int dirfd)
3915 const struct got_error *err = NULL;
3916 struct diff_dir_cb_arg *a = arg;
3917 char *path = NULL;
3919 if (ignore != NULL)
3920 *ignore = 0;
3922 if (a->cancel_cb) {
3923 err = a->cancel_cb(a->cancel_arg);
3924 if (err)
3925 return err;
3928 if (parent_path[0]) {
3929 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3930 return got_error_from_errno("asprintf");
3931 } else {
3932 path = de->d_name;
3935 if (de->d_type == DT_DIR) {
3936 if (!a->no_ignores && ignore != NULL &&
3937 match_ignores(a->ignores, path))
3938 *ignore = 1;
3939 } else if (!match_ignores(a->ignores, path) &&
3940 got_path_is_child(path, a->status_path, a->status_path_len))
3941 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3942 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3943 if (parent_path[0])
3944 free(path);
3945 return err;
3948 static const struct got_error *
3949 status_traverse(void *arg, const char *path, int dirfd)
3951 const struct got_error *err = NULL;
3952 struct diff_dir_cb_arg *a = arg;
3954 if (a->no_ignores)
3955 return NULL;
3957 err = add_ignores(a->ignores, a->worktree->root_path,
3958 path, dirfd, ".cvsignore");
3959 if (err)
3960 return err;
3962 err = add_ignores(a->ignores, a->worktree->root_path, path,
3963 dirfd, ".gitignore");
3965 return err;
3968 static const struct got_error *
3969 report_single_file_status(const char *path, const char *ondisk_path,
3970 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3971 void *status_arg, struct got_repository *repo, int report_unchanged,
3972 struct got_pathlist_head *ignores, int no_ignores)
3974 struct got_fileindex_entry *ie;
3975 struct stat sb;
3977 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3978 if (ie)
3979 return report_file_status(ie, ondisk_path, -1, NULL,
3980 status_cb, status_arg, repo, report_unchanged);
3982 if (lstat(ondisk_path, &sb) == -1) {
3983 if (errno != ENOENT)
3984 return got_error_from_errno2("lstat", ondisk_path);
3985 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3986 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3989 if (!no_ignores && match_ignores(ignores, path))
3990 return NULL;
3992 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3993 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3994 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3996 return NULL;
3999 static const struct got_error *
4000 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4001 const char *root_path, const char *path)
4003 const struct got_error *err;
4004 char *parent_path, *next_parent_path = NULL;
4006 err = add_ignores(ignores, root_path, "", -1,
4007 ".cvsignore");
4008 if (err)
4009 return err;
4011 err = add_ignores(ignores, root_path, "", -1,
4012 ".gitignore");
4013 if (err)
4014 return err;
4016 err = got_path_dirname(&parent_path, path);
4017 if (err) {
4018 if (err->code == GOT_ERR_BAD_PATH)
4019 return NULL; /* cannot traverse parent */
4020 return err;
4022 for (;;) {
4023 err = add_ignores(ignores, root_path, parent_path, -1,
4024 ".cvsignore");
4025 if (err)
4026 break;
4027 err = add_ignores(ignores, root_path, parent_path, -1,
4028 ".gitignore");
4029 if (err)
4030 break;
4031 err = got_path_dirname(&next_parent_path, parent_path);
4032 if (err) {
4033 if (err->code == GOT_ERR_BAD_PATH)
4034 err = NULL; /* traversed everything */
4035 break;
4037 if (got_path_is_root_dir(parent_path))
4038 break;
4039 free(parent_path);
4040 parent_path = next_parent_path;
4041 next_parent_path = NULL;
4044 free(parent_path);
4045 free(next_parent_path);
4046 return err;
4049 struct find_missing_children_args {
4050 const char *parent_path;
4051 size_t parent_len;
4052 struct got_pathlist_head *children;
4053 got_cancel_cb cancel_cb;
4054 void *cancel_arg;
4057 static const struct got_error *
4058 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4060 const struct got_error *err = NULL;
4061 struct find_missing_children_args *a = arg;
4063 if (a->cancel_cb) {
4064 err = a->cancel_cb(a->cancel_arg);
4065 if (err)
4066 return err;
4069 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4070 err = got_pathlist_append(a->children, ie->path, NULL);
4072 return err;
4075 static const struct got_error *
4076 report_children(struct got_pathlist_head *children,
4077 struct got_worktree *worktree, struct got_fileindex *fileindex,
4078 struct got_repository *repo, int is_root_dir, int report_unchanged,
4079 struct got_pathlist_head *ignores, int no_ignores,
4080 got_worktree_status_cb status_cb, void *status_arg,
4081 got_cancel_cb cancel_cb, void *cancel_arg)
4083 const struct got_error *err = NULL;
4084 struct got_pathlist_entry *pe;
4085 char *ondisk_path = NULL;
4087 TAILQ_FOREACH(pe, children, entry) {
4088 if (cancel_cb) {
4089 err = cancel_cb(cancel_arg);
4090 if (err)
4091 break;
4094 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4095 !is_root_dir ? "/" : "", pe->path) == -1) {
4096 err = got_error_from_errno("asprintf");
4097 ondisk_path = NULL;
4098 break;
4101 err = report_single_file_status(pe->path, ondisk_path,
4102 fileindex, status_cb, status_arg, repo, report_unchanged,
4103 ignores, no_ignores);
4104 if (err)
4105 break;
4107 free(ondisk_path);
4108 ondisk_path = NULL;
4111 free(ondisk_path);
4112 return err;
4115 static const struct got_error *
4116 worktree_status(struct got_worktree *worktree, const char *path,
4117 struct got_fileindex *fileindex, struct got_repository *repo,
4118 got_worktree_status_cb status_cb, void *status_arg,
4119 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4120 int report_unchanged)
4122 const struct got_error *err = NULL;
4123 int fd = -1;
4124 struct got_fileindex_diff_dir_cb fdiff_cb;
4125 struct diff_dir_cb_arg arg;
4126 char *ondisk_path = NULL;
4127 struct got_pathlist_head ignores, missing_children;
4128 struct got_fileindex_entry *ie;
4130 TAILQ_INIT(&ignores);
4131 TAILQ_INIT(&missing_children);
4133 if (asprintf(&ondisk_path, "%s%s%s",
4134 worktree->root_path, path[0] ? "/" : "", path) == -1)
4135 return got_error_from_errno("asprintf");
4137 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4138 if (ie) {
4139 err = report_single_file_status(path, ondisk_path,
4140 fileindex, status_cb, status_arg, repo,
4141 report_unchanged, &ignores, no_ignores);
4142 goto done;
4143 } else {
4144 struct find_missing_children_args fmca;
4145 fmca.parent_path = path;
4146 fmca.parent_len = strlen(path);
4147 fmca.children = &missing_children;
4148 fmca.cancel_cb = cancel_cb;
4149 fmca.cancel_arg = cancel_arg;
4150 err = got_fileindex_for_each_entry_safe(fileindex,
4151 find_missing_children, &fmca);
4152 if (err)
4153 goto done;
4156 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4157 if (fd == -1) {
4158 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4159 !got_err_open_nofollow_on_symlink())
4160 err = got_error_from_errno2("open", ondisk_path);
4161 else {
4162 if (!no_ignores) {
4163 err = add_ignores_from_parent_paths(&ignores,
4164 worktree->root_path, ondisk_path);
4165 if (err)
4166 goto done;
4168 if (TAILQ_EMPTY(&missing_children)) {
4169 err = report_single_file_status(path,
4170 ondisk_path, fileindex,
4171 status_cb, status_arg, repo,
4172 report_unchanged, &ignores, no_ignores);
4173 if (err)
4174 goto done;
4175 } else {
4176 err = report_children(&missing_children,
4177 worktree, fileindex, repo,
4178 (path[0] == '\0'), report_unchanged,
4179 &ignores, no_ignores,
4180 status_cb, status_arg,
4181 cancel_cb, cancel_arg);
4182 if (err)
4183 goto done;
4186 } else {
4187 fdiff_cb.diff_old_new = status_old_new;
4188 fdiff_cb.diff_old = status_old;
4189 fdiff_cb.diff_new = status_new;
4190 fdiff_cb.diff_traverse = status_traverse;
4191 arg.fileindex = fileindex;
4192 arg.worktree = worktree;
4193 arg.status_path = path;
4194 arg.status_path_len = strlen(path);
4195 arg.repo = repo;
4196 arg.status_cb = status_cb;
4197 arg.status_arg = status_arg;
4198 arg.cancel_cb = cancel_cb;
4199 arg.cancel_arg = cancel_arg;
4200 arg.report_unchanged = report_unchanged;
4201 arg.no_ignores = no_ignores;
4202 if (!no_ignores) {
4203 err = add_ignores_from_parent_paths(&ignores,
4204 worktree->root_path, path);
4205 if (err)
4206 goto done;
4208 arg.ignores = &ignores;
4209 err = got_fileindex_diff_dir(fileindex, fd,
4210 worktree->root_path, path, repo, &fdiff_cb, &arg);
4212 done:
4213 free_ignores(&ignores);
4214 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4215 if (fd != -1 && close(fd) == -1 && err == NULL)
4216 err = got_error_from_errno("close");
4217 free(ondisk_path);
4218 return err;
4221 const struct got_error *
4222 got_worktree_status(struct got_worktree *worktree,
4223 struct got_pathlist_head *paths, struct got_repository *repo,
4224 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4225 got_cancel_cb cancel_cb, void *cancel_arg)
4227 const struct got_error *err = NULL;
4228 char *fileindex_path = NULL;
4229 struct got_fileindex *fileindex = NULL;
4230 struct got_pathlist_entry *pe;
4232 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4233 if (err)
4234 return err;
4236 TAILQ_FOREACH(pe, paths, entry) {
4237 err = worktree_status(worktree, pe->path, fileindex, repo,
4238 status_cb, status_arg, cancel_cb, cancel_arg,
4239 no_ignores, 0);
4240 if (err)
4241 break;
4243 free(fileindex_path);
4244 got_fileindex_free(fileindex);
4245 return err;
4248 const struct got_error *
4249 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4250 const char *arg)
4252 const struct got_error *err = NULL;
4253 char *resolved = NULL, *cwd = NULL, *path = NULL;
4254 size_t len;
4255 struct stat sb;
4256 char *abspath = NULL;
4257 char canonpath[PATH_MAX];
4259 *wt_path = NULL;
4261 cwd = getcwd(NULL, 0);
4262 if (cwd == NULL)
4263 return got_error_from_errno("getcwd");
4265 if (lstat(arg, &sb) == -1) {
4266 if (errno != ENOENT) {
4267 err = got_error_from_errno2("lstat", arg);
4268 goto done;
4270 sb.st_mode = 0;
4272 if (S_ISLNK(sb.st_mode)) {
4274 * We cannot use realpath(3) with symlinks since we want to
4275 * operate on the symlink itself.
4276 * But we can make the path absolute, assuming it is relative
4277 * to the current working directory, and then canonicalize it.
4279 if (!got_path_is_absolute(arg)) {
4280 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4281 err = got_error_from_errno("asprintf");
4282 goto done;
4286 err = got_canonpath(abspath ? abspath : arg, canonpath,
4287 sizeof(canonpath));
4288 if (err)
4289 goto done;
4290 resolved = strdup(canonpath);
4291 if (resolved == NULL) {
4292 err = got_error_from_errno("strdup");
4293 goto done;
4295 } else {
4296 resolved = realpath(arg, NULL);
4297 if (resolved == NULL) {
4298 if (errno != ENOENT) {
4299 err = got_error_from_errno2("realpath", arg);
4300 goto done;
4302 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4303 err = got_error_from_errno("asprintf");
4304 goto done;
4306 err = got_canonpath(abspath, canonpath,
4307 sizeof(canonpath));
4308 if (err)
4309 goto done;
4310 resolved = strdup(canonpath);
4311 if (resolved == NULL) {
4312 err = got_error_from_errno("strdup");
4313 goto done;
4318 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4319 strlen(got_worktree_get_root_path(worktree)))) {
4320 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4321 goto done;
4324 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4325 err = got_path_skip_common_ancestor(&path,
4326 got_worktree_get_root_path(worktree), resolved);
4327 if (err)
4328 goto done;
4329 } else {
4330 path = strdup("");
4331 if (path == NULL) {
4332 err = got_error_from_errno("strdup");
4333 goto done;
4337 /* XXX status walk can't deal with trailing slash! */
4338 len = strlen(path);
4339 while (len > 0 && path[len - 1] == '/') {
4340 path[len - 1] = '\0';
4341 len--;
4343 done:
4344 free(abspath);
4345 free(resolved);
4346 free(cwd);
4347 if (err == NULL)
4348 *wt_path = path;
4349 else
4350 free(path);
4351 return err;
4354 struct schedule_addition_args {
4355 struct got_worktree *worktree;
4356 struct got_fileindex *fileindex;
4357 got_worktree_checkout_cb progress_cb;
4358 void *progress_arg;
4359 struct got_repository *repo;
4362 static int
4363 add_noop_status(unsigned char status)
4365 return (status == GOT_STATUS_ADD ||
4366 status == GOT_STATUS_MODIFY ||
4367 status == GOT_STATUS_CONFLICT ||
4368 status == GOT_STATUS_MODE_CHANGE ||
4369 status == GOT_STATUS_NO_CHANGE);
4372 static const struct got_error *
4373 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4374 const char *relpath, struct got_object_id *blob_id,
4375 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4376 int dirfd, const char *de_name)
4378 struct schedule_addition_args *a = arg;
4379 const struct got_error *err = NULL;
4380 struct got_fileindex_entry *ie;
4381 struct stat sb;
4382 char *ondisk_path;
4384 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4385 relpath) == -1)
4386 return got_error_from_errno("asprintf");
4388 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4389 if (ie) {
4390 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4391 de_name, a->repo);
4392 if (err)
4393 goto done;
4394 /* Re-adding an existing entry is a no-op. */
4395 if (staged_status == GOT_STATUS_NO_CHANGE &&
4396 add_noop_status(status))
4397 goto done;
4398 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4399 if (err)
4400 goto done;
4403 if (status != GOT_STATUS_UNVERSIONED) {
4404 if (status == GOT_STATUS_NONEXISTENT)
4405 err = got_error_set_errno(ENOENT, ondisk_path);
4406 else
4407 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4408 goto done;
4411 err = got_fileindex_entry_alloc(&ie, relpath);
4412 if (err)
4413 goto done;
4414 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4415 relpath, NULL, NULL, 1);
4416 if (err) {
4417 got_fileindex_entry_free(ie);
4418 goto done;
4420 err = got_fileindex_entry_add(a->fileindex, ie);
4421 if (err) {
4422 got_fileindex_entry_free(ie);
4423 goto done;
4425 done:
4426 free(ondisk_path);
4427 if (err)
4428 return err;
4429 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4430 return NULL;
4431 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4434 const struct got_error *
4435 got_worktree_schedule_add(struct got_worktree *worktree,
4436 struct got_pathlist_head *paths,
4437 got_worktree_checkout_cb progress_cb, void *progress_arg,
4438 struct got_repository *repo, int no_ignores)
4440 struct got_fileindex *fileindex = NULL;
4441 char *fileindex_path = NULL;
4442 const struct got_error *err = NULL, *sync_err, *unlockerr;
4443 struct got_pathlist_entry *pe;
4444 struct schedule_addition_args saa;
4446 err = lock_worktree(worktree, LOCK_EX);
4447 if (err)
4448 return err;
4450 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4451 if (err)
4452 goto done;
4454 saa.worktree = worktree;
4455 saa.fileindex = fileindex;
4456 saa.progress_cb = progress_cb;
4457 saa.progress_arg = progress_arg;
4458 saa.repo = repo;
4460 TAILQ_FOREACH(pe, paths, entry) {
4461 err = worktree_status(worktree, pe->path, fileindex, repo,
4462 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4463 if (err)
4464 break;
4466 sync_err = sync_fileindex(fileindex, fileindex_path);
4467 if (sync_err && err == NULL)
4468 err = sync_err;
4469 done:
4470 free(fileindex_path);
4471 if (fileindex)
4472 got_fileindex_free(fileindex);
4473 unlockerr = lock_worktree(worktree, LOCK_SH);
4474 if (unlockerr && err == NULL)
4475 err = unlockerr;
4476 return err;
4479 struct schedule_deletion_args {
4480 struct got_worktree *worktree;
4481 struct got_fileindex *fileindex;
4482 got_worktree_delete_cb progress_cb;
4483 void *progress_arg;
4484 struct got_repository *repo;
4485 int delete_local_mods;
4486 int keep_on_disk;
4487 int ignore_missing_paths;
4488 const char *status_path;
4489 size_t status_path_len;
4490 const char *status_codes;
4493 static const struct got_error *
4494 schedule_for_deletion(void *arg, unsigned char status,
4495 unsigned char staged_status, const char *relpath,
4496 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4497 struct got_object_id *commit_id, int dirfd, const char *de_name)
4499 struct schedule_deletion_args *a = arg;
4500 const struct got_error *err = NULL;
4501 struct got_fileindex_entry *ie = NULL;
4502 struct stat sb;
4503 char *ondisk_path;
4505 if (status == GOT_STATUS_NONEXISTENT) {
4506 if (a->ignore_missing_paths)
4507 return NULL;
4508 return got_error_set_errno(ENOENT, relpath);
4511 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4512 if (ie == NULL)
4513 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4515 staged_status = get_staged_status(ie);
4516 if (staged_status != GOT_STATUS_NO_CHANGE) {
4517 if (staged_status == GOT_STATUS_DELETE)
4518 return NULL;
4519 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4522 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4523 relpath) == -1)
4524 return got_error_from_errno("asprintf");
4526 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4527 a->repo);
4528 if (err)
4529 goto done;
4531 if (a->status_codes) {
4532 size_t ncodes = strlen(a->status_codes);
4533 int i;
4534 for (i = 0; i < ncodes ; i++) {
4535 if (status == a->status_codes[i])
4536 break;
4538 if (i == ncodes) {
4539 /* Do not delete files in non-matching status. */
4540 free(ondisk_path);
4541 return NULL;
4543 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4544 a->status_codes[i] != GOT_STATUS_MISSING) {
4545 static char msg[64];
4546 snprintf(msg, sizeof(msg),
4547 "invalid status code '%c'", a->status_codes[i]);
4548 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4549 goto done;
4553 if (status != GOT_STATUS_NO_CHANGE) {
4554 if (status == GOT_STATUS_DELETE)
4555 goto done;
4556 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4557 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4558 goto done;
4560 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4561 err = got_error_set_errno(ENOENT, relpath);
4562 goto done;
4564 if (status != GOT_STATUS_MODIFY &&
4565 status != GOT_STATUS_MISSING) {
4566 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4567 goto done;
4571 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4572 size_t root_len;
4574 if (dirfd != -1) {
4575 if (unlinkat(dirfd, de_name, 0) == -1) {
4576 err = got_error_from_errno2("unlinkat",
4577 ondisk_path);
4578 goto done;
4580 } else if (unlink(ondisk_path) == -1) {
4581 err = got_error_from_errno2("unlink", ondisk_path);
4582 goto done;
4585 root_len = strlen(a->worktree->root_path);
4586 do {
4587 char *parent;
4589 err = got_path_dirname(&parent, ondisk_path);
4590 if (err)
4591 goto done;
4592 free(ondisk_path);
4593 ondisk_path = parent;
4594 if (got_path_cmp(ondisk_path, a->status_path,
4595 strlen(ondisk_path), a->status_path_len) != 0 &&
4596 !got_path_is_child(ondisk_path, a->status_path,
4597 a->status_path_len))
4598 break;
4599 if (rmdir(ondisk_path) == -1) {
4600 if (errno != ENOTEMPTY)
4601 err = got_error_from_errno2("rmdir",
4602 ondisk_path);
4603 break;
4605 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4606 strlen(ondisk_path), root_len) != 0);
4609 if (got_fileindex_entry_has_blob(ie))
4610 got_fileindex_entry_mark_deleted_from_disk(ie);
4611 else
4612 got_fileindex_entry_remove(a->fileindex, ie);
4613 done:
4614 free(ondisk_path);
4615 if (err)
4616 return err;
4617 if (status == GOT_STATUS_DELETE)
4618 return NULL;
4619 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4620 staged_status, relpath);
4623 const struct got_error *
4624 got_worktree_schedule_delete(struct got_worktree *worktree,
4625 struct got_pathlist_head *paths, int delete_local_mods,
4626 const char *status_codes,
4627 got_worktree_delete_cb progress_cb, void *progress_arg,
4628 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4630 struct got_fileindex *fileindex = NULL;
4631 char *fileindex_path = NULL;
4632 const struct got_error *err = NULL, *sync_err, *unlockerr;
4633 struct got_pathlist_entry *pe;
4634 struct schedule_deletion_args sda;
4636 err = lock_worktree(worktree, LOCK_EX);
4637 if (err)
4638 return err;
4640 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4641 if (err)
4642 goto done;
4644 sda.worktree = worktree;
4645 sda.fileindex = fileindex;
4646 sda.progress_cb = progress_cb;
4647 sda.progress_arg = progress_arg;
4648 sda.repo = repo;
4649 sda.delete_local_mods = delete_local_mods;
4650 sda.keep_on_disk = keep_on_disk;
4651 sda.ignore_missing_paths = ignore_missing_paths;
4652 sda.status_codes = status_codes;
4654 TAILQ_FOREACH(pe, paths, entry) {
4655 char *ondisk_status_path;
4657 if (asprintf(&ondisk_status_path, "%s%s%s",
4658 got_worktree_get_root_path(worktree),
4659 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4660 err = got_error_from_errno("asprintf");
4661 goto done;
4663 sda.status_path = ondisk_status_path;
4664 sda.status_path_len = strlen(ondisk_status_path);
4665 err = worktree_status(worktree, pe->path, fileindex, repo,
4666 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4667 free(ondisk_status_path);
4668 if (err)
4669 break;
4671 sync_err = sync_fileindex(fileindex, fileindex_path);
4672 if (sync_err && err == NULL)
4673 err = sync_err;
4674 done:
4675 free(fileindex_path);
4676 if (fileindex)
4677 got_fileindex_free(fileindex);
4678 unlockerr = lock_worktree(worktree, LOCK_SH);
4679 if (unlockerr && err == NULL)
4680 err = unlockerr;
4681 return err;
4684 static const struct got_error *
4685 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4687 const struct got_error *err = NULL;
4688 char *line = NULL;
4689 size_t linesize = 0, n;
4690 ssize_t linelen;
4692 linelen = getline(&line, &linesize, infile);
4693 if (linelen == -1) {
4694 if (ferror(infile)) {
4695 err = got_error_from_errno("getline");
4696 goto done;
4698 return NULL;
4700 if (outfile) {
4701 n = fwrite(line, 1, linelen, outfile);
4702 if (n != linelen) {
4703 err = got_ferror(outfile, GOT_ERR_IO);
4704 goto done;
4707 if (rejectfile) {
4708 n = fwrite(line, 1, linelen, rejectfile);
4709 if (n != linelen)
4710 err = got_ferror(rejectfile, GOT_ERR_IO);
4712 done:
4713 free(line);
4714 return err;
4717 static const struct got_error *
4718 skip_one_line(FILE *f)
4720 char *line = NULL;
4721 size_t linesize = 0;
4722 ssize_t linelen;
4724 linelen = getline(&line, &linesize, f);
4725 if (linelen == -1) {
4726 if (ferror(f))
4727 return got_error_from_errno("getline");
4728 return NULL;
4730 free(line);
4731 return NULL;
4734 static const struct got_error *
4735 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4736 int start_old, int end_old, int start_new, int end_new,
4737 FILE *outfile, FILE *rejectfile)
4739 const struct got_error *err;
4741 /* Copy old file's lines leading up to patch. */
4742 while (!feof(f1) && *line_cur1 < start_old) {
4743 err = copy_one_line(f1, outfile, NULL);
4744 if (err)
4745 return err;
4746 (*line_cur1)++;
4748 /* Skip new file's lines leading up to patch. */
4749 while (!feof(f2) && *line_cur2 < start_new) {
4750 if (rejectfile)
4751 err = copy_one_line(f2, NULL, rejectfile);
4752 else
4753 err = skip_one_line(f2);
4754 if (err)
4755 return err;
4756 (*line_cur2)++;
4758 /* Copy patched lines. */
4759 while (!feof(f2) && *line_cur2 <= end_new) {
4760 err = copy_one_line(f2, outfile, NULL);
4761 if (err)
4762 return err;
4763 (*line_cur2)++;
4765 /* Skip over old file's replaced lines. */
4766 while (!feof(f1) && *line_cur1 <= end_old) {
4767 if (rejectfile)
4768 err = copy_one_line(f1, NULL, rejectfile);
4769 else
4770 err = skip_one_line(f1);
4771 if (err)
4772 return err;
4773 (*line_cur1)++;
4776 return NULL;
4779 static const struct got_error *
4780 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4781 FILE *outfile, FILE *rejectfile)
4783 const struct got_error *err;
4785 if (outfile) {
4786 /* Copy old file's lines until EOF. */
4787 while (!feof(f1)) {
4788 err = copy_one_line(f1, outfile, NULL);
4789 if (err)
4790 return err;
4791 (*line_cur1)++;
4794 if (rejectfile) {
4795 /* Copy new file's lines until EOF. */
4796 while (!feof(f2)) {
4797 err = copy_one_line(f2, NULL, rejectfile);
4798 if (err)
4799 return err;
4800 (*line_cur2)++;
4804 return NULL;
4807 static const struct got_error *
4808 apply_or_reject_change(int *choice, int *nchunks_used,
4809 struct diff_result *diff_result, int n,
4810 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4811 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4812 got_worktree_patch_cb patch_cb, void *patch_arg)
4814 const struct got_error *err = NULL;
4815 struct diff_chunk_context cc = {};
4816 int start_old, end_old, start_new, end_new;
4817 FILE *hunkfile;
4818 struct diff_output_unidiff_state *diff_state;
4819 struct diff_input_info diff_info;
4820 int rc;
4822 *choice = GOT_PATCH_CHOICE_NONE;
4824 /* Get changed line numbers without context lines for copy_change(). */
4825 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4826 start_old = cc.left.start;
4827 end_old = cc.left.end;
4828 start_new = cc.right.start;
4829 end_new = cc.right.end;
4831 /* Get the same change with context lines for display. */
4832 memset(&cc, 0, sizeof(cc));
4833 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4835 memset(&diff_info, 0, sizeof(diff_info));
4836 diff_info.left_path = relpath;
4837 diff_info.right_path = relpath;
4839 diff_state = diff_output_unidiff_state_alloc();
4840 if (diff_state == NULL)
4841 return got_error_set_errno(ENOMEM,
4842 "diff_output_unidiff_state_alloc");
4844 hunkfile = got_opentemp();
4845 if (hunkfile == NULL) {
4846 err = got_error_from_errno("got_opentemp");
4847 goto done;
4850 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4851 diff_result, &cc);
4852 if (rc != DIFF_RC_OK) {
4853 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4854 goto done;
4857 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4858 err = got_ferror(hunkfile, GOT_ERR_IO);
4859 goto done;
4862 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4863 hunkfile, changeno, nchanges);
4864 if (err)
4865 goto done;
4867 switch (*choice) {
4868 case GOT_PATCH_CHOICE_YES:
4869 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4870 end_old, start_new, end_new, outfile, rejectfile);
4871 break;
4872 case GOT_PATCH_CHOICE_NO:
4873 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4874 end_old, start_new, end_new, rejectfile, outfile);
4875 break;
4876 case GOT_PATCH_CHOICE_QUIT:
4877 break;
4878 default:
4879 err = got_error(GOT_ERR_PATCH_CHOICE);
4880 break;
4882 done:
4883 diff_output_unidiff_state_free(diff_state);
4884 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4885 err = got_error_from_errno("fclose");
4886 return err;
4889 struct revert_file_args {
4890 struct got_worktree *worktree;
4891 struct got_fileindex *fileindex;
4892 got_worktree_checkout_cb progress_cb;
4893 void *progress_arg;
4894 got_worktree_patch_cb patch_cb;
4895 void *patch_arg;
4896 struct got_repository *repo;
4897 int unlink_added_files;
4898 struct got_pathlist_head *added_files_to_unlink;
4901 static const struct got_error *
4902 create_patched_content(char **path_outfile, int reverse_patch,
4903 struct got_object_id *blob_id, const char *path2,
4904 int dirfd2, const char *de_name2,
4905 const char *relpath, struct got_repository *repo,
4906 got_worktree_patch_cb patch_cb, void *patch_arg)
4908 const struct got_error *err, *free_err;
4909 struct got_blob_object *blob = NULL;
4910 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4911 int fd = -1, fd2 = -1;
4912 char link_target[PATH_MAX];
4913 ssize_t link_len = 0;
4914 char *path1 = NULL, *id_str = NULL;
4915 struct stat sb2;
4916 struct got_diffreg_result *diffreg_result = NULL;
4917 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4918 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4920 *path_outfile = NULL;
4922 err = got_object_id_str(&id_str, blob_id);
4923 if (err)
4924 return err;
4926 if (dirfd2 != -1) {
4927 fd2 = openat(dirfd2, de_name2,
4928 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4929 if (fd2 == -1) {
4930 if (!got_err_open_nofollow_on_symlink()) {
4931 err = got_error_from_errno2("openat", path2);
4932 goto done;
4934 link_len = readlinkat(dirfd2, de_name2,
4935 link_target, sizeof(link_target));
4936 if (link_len == -1) {
4937 return got_error_from_errno2("readlinkat",
4938 path2);
4940 sb2.st_mode = S_IFLNK;
4941 sb2.st_size = link_len;
4943 } else {
4944 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4945 if (fd2 == -1) {
4946 if (!got_err_open_nofollow_on_symlink()) {
4947 err = got_error_from_errno2("open", path2);
4948 goto done;
4950 link_len = readlink(path2, link_target,
4951 sizeof(link_target));
4952 if (link_len == -1)
4953 return got_error_from_errno2("readlink", path2);
4954 sb2.st_mode = S_IFLNK;
4955 sb2.st_size = link_len;
4958 if (fd2 != -1) {
4959 if (fstat(fd2, &sb2) == -1) {
4960 err = got_error_from_errno2("fstat", path2);
4961 goto done;
4964 f2 = fdopen(fd2, "r");
4965 if (f2 == NULL) {
4966 err = got_error_from_errno2("fdopen", path2);
4967 goto done;
4969 fd2 = -1;
4970 } else {
4971 size_t n;
4972 f2 = got_opentemp();
4973 if (f2 == NULL) {
4974 err = got_error_from_errno2("got_opentemp", path2);
4975 goto done;
4977 n = fwrite(link_target, 1, link_len, f2);
4978 if (n != link_len) {
4979 err = got_ferror(f2, GOT_ERR_IO);
4980 goto done;
4982 if (fflush(f2) == EOF) {
4983 err = got_error_from_errno("fflush");
4984 goto done;
4986 rewind(f2);
4989 fd = got_opentempfd();
4990 if (fd == -1) {
4991 err = got_error_from_errno("got_opentempfd");
4992 goto done;
4995 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4996 if (err)
4997 goto done;
4999 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5000 if (err)
5001 goto done;
5003 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5004 if (err)
5005 goto done;
5007 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5008 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5009 if (err)
5010 goto done;
5012 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5013 "");
5014 if (err)
5015 goto done;
5017 if (fseek(f1, 0L, SEEK_SET) == -1)
5018 return got_ferror(f1, GOT_ERR_IO);
5019 if (fseek(f2, 0L, SEEK_SET) == -1)
5020 return got_ferror(f2, GOT_ERR_IO);
5022 /* Count the number of actual changes in the diff result. */
5023 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5024 struct diff_chunk_context cc = {};
5025 diff_chunk_context_load_change(&cc, &nchunks_used,
5026 diffreg_result->result, n, 0);
5027 nchanges++;
5029 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5030 int choice;
5031 err = apply_or_reject_change(&choice, &nchunks_used,
5032 diffreg_result->result, n, relpath, f1, f2,
5033 &line_cur1, &line_cur2,
5034 reverse_patch ? NULL : outfile,
5035 reverse_patch ? outfile : NULL,
5036 ++i, nchanges, patch_cb, patch_arg);
5037 if (err)
5038 goto done;
5039 if (choice == GOT_PATCH_CHOICE_YES)
5040 have_content = 1;
5041 else if (choice == GOT_PATCH_CHOICE_QUIT)
5042 break;
5044 if (have_content) {
5045 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5046 reverse_patch ? NULL : outfile,
5047 reverse_patch ? outfile : NULL);
5048 if (err)
5049 goto done;
5051 if (!S_ISLNK(sb2.st_mode)) {
5052 mode_t mode;
5054 mode = apply_umask(sb2.st_mode);
5055 if (fchmod(fileno(outfile), mode) == -1) {
5056 err = got_error_from_errno2("fchmod", path2);
5057 goto done;
5061 done:
5062 free(id_str);
5063 if (fd != -1 && close(fd) == -1 && err == NULL)
5064 err = got_error_from_errno("close");
5065 if (blob)
5066 got_object_blob_close(blob);
5067 free_err = got_diffreg_result_free(diffreg_result);
5068 if (err == NULL)
5069 err = free_err;
5070 if (f1 && fclose(f1) == EOF && err == NULL)
5071 err = got_error_from_errno2("fclose", path1);
5072 if (f2 && fclose(f2) == EOF && err == NULL)
5073 err = got_error_from_errno2("fclose", path2);
5074 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5075 err = got_error_from_errno2("close", path2);
5076 if (outfile && fclose(outfile) == EOF && err == NULL)
5077 err = got_error_from_errno2("fclose", *path_outfile);
5078 if (path1 && unlink(path1) == -1 && err == NULL)
5079 err = got_error_from_errno2("unlink", path1);
5080 if (err || !have_content) {
5081 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5082 err = got_error_from_errno2("unlink", *path_outfile);
5083 free(*path_outfile);
5084 *path_outfile = NULL;
5086 free(path1);
5087 return err;
5090 static const struct got_error *
5091 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5092 const char *relpath, struct got_object_id *blob_id,
5093 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5094 int dirfd, const char *de_name)
5096 struct revert_file_args *a = arg;
5097 const struct got_error *err = NULL;
5098 char *parent_path = NULL;
5099 struct got_fileindex_entry *ie;
5100 struct got_commit_object *base_commit = NULL;
5101 struct got_tree_object *tree = NULL;
5102 struct got_object_id *tree_id = NULL;
5103 const struct got_tree_entry *te = NULL;
5104 char *tree_path = NULL, *te_name;
5105 char *ondisk_path = NULL, *path_content = NULL;
5106 struct got_blob_object *blob = NULL;
5107 int fd = -1;
5109 /* Reverting a staged deletion is a no-op. */
5110 if (status == GOT_STATUS_DELETE &&
5111 staged_status != GOT_STATUS_NO_CHANGE)
5112 return NULL;
5114 if (status == GOT_STATUS_UNVERSIONED)
5115 return (*a->progress_cb)(a->progress_arg,
5116 GOT_STATUS_UNVERSIONED, relpath);
5118 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5119 if (ie == NULL)
5120 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5122 /* Construct in-repository path of tree which contains this blob. */
5123 err = got_path_dirname(&parent_path, ie->path);
5124 if (err) {
5125 if (err->code != GOT_ERR_BAD_PATH)
5126 goto done;
5127 parent_path = strdup("/");
5128 if (parent_path == NULL) {
5129 err = got_error_from_errno("strdup");
5130 goto done;
5133 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5134 tree_path = strdup(parent_path);
5135 if (tree_path == NULL) {
5136 err = got_error_from_errno("strdup");
5137 goto done;
5139 } else {
5140 if (got_path_is_root_dir(parent_path)) {
5141 tree_path = strdup(a->worktree->path_prefix);
5142 if (tree_path == NULL) {
5143 err = got_error_from_errno("strdup");
5144 goto done;
5146 } else {
5147 if (asprintf(&tree_path, "%s/%s",
5148 a->worktree->path_prefix, parent_path) == -1) {
5149 err = got_error_from_errno("asprintf");
5150 goto done;
5155 err = got_object_open_as_commit(&base_commit, a->repo,
5156 a->worktree->base_commit_id);
5157 if (err)
5158 goto done;
5160 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5161 if (err) {
5162 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5163 (status == GOT_STATUS_ADD ||
5164 staged_status == GOT_STATUS_ADD)))
5165 goto done;
5166 } else {
5167 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5168 if (err)
5169 goto done;
5171 err = got_path_basename(&te_name, ie->path);
5172 if (err)
5173 goto done;
5175 te = got_object_tree_find_entry(tree, te_name);
5176 free(te_name);
5177 if (te == NULL && status != GOT_STATUS_ADD &&
5178 staged_status != GOT_STATUS_ADD) {
5179 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5180 goto done;
5184 switch (status) {
5185 case GOT_STATUS_ADD:
5186 if (a->patch_cb) {
5187 int choice = GOT_PATCH_CHOICE_NONE;
5188 err = (*a->patch_cb)(&choice, a->patch_arg,
5189 status, ie->path, NULL, 1, 1);
5190 if (err)
5191 goto done;
5192 if (choice != GOT_PATCH_CHOICE_YES)
5193 break;
5195 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5196 ie->path);
5197 if (err)
5198 goto done;
5199 got_fileindex_entry_remove(a->fileindex, ie);
5200 if (a->unlink_added_files) {
5201 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5203 if (a->added_files_to_unlink) {
5204 struct got_pathlist_entry *pe;
5206 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5207 entry) {
5208 if (got_path_cmp(pe->path, relpath,
5209 pe->path_len, strlen(relpath)))
5210 continue;
5211 do_unlink = 1;
5212 break;
5216 if (do_unlink) {
5217 if (asprintf(&ondisk_path, "%s/%s",
5218 got_worktree_get_root_path(a->worktree),
5219 relpath) == -1) {
5220 err = got_error_from_errno("asprintf");
5221 goto done;
5223 if (unlink(ondisk_path) == -1) {
5224 err = got_error_from_errno2("unlink",
5225 ondisk_path);
5226 break;
5230 break;
5231 case GOT_STATUS_DELETE:
5232 if (a->patch_cb) {
5233 int choice = GOT_PATCH_CHOICE_NONE;
5234 err = (*a->patch_cb)(&choice, a->patch_arg,
5235 status, ie->path, NULL, 1, 1);
5236 if (err)
5237 goto done;
5238 if (choice != GOT_PATCH_CHOICE_YES)
5239 break;
5241 /* fall through */
5242 case GOT_STATUS_MODIFY:
5243 case GOT_STATUS_MODE_CHANGE:
5244 case GOT_STATUS_CONFLICT:
5245 case GOT_STATUS_MISSING: {
5246 struct got_object_id id;
5247 if (staged_status == GOT_STATUS_ADD ||
5248 staged_status == GOT_STATUS_MODIFY)
5249 got_fileindex_entry_get_staged_blob_id(&id, ie);
5250 else
5251 got_fileindex_entry_get_blob_id(&id, ie);
5252 fd = got_opentempfd();
5253 if (fd == -1) {
5254 err = got_error_from_errno("got_opentempfd");
5255 goto done;
5258 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5259 if (err)
5260 goto done;
5262 if (asprintf(&ondisk_path, "%s/%s",
5263 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5264 err = got_error_from_errno("asprintf");
5265 goto done;
5268 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5269 status == GOT_STATUS_CONFLICT)) {
5270 int is_bad_symlink = 0;
5271 err = create_patched_content(&path_content, 1, &id,
5272 ondisk_path, dirfd, de_name, ie->path, a->repo,
5273 a->patch_cb, a->patch_arg);
5274 if (err || path_content == NULL)
5275 break;
5276 if (te && S_ISLNK(te->mode)) {
5277 if (unlink(path_content) == -1) {
5278 err = got_error_from_errno2("unlink",
5279 path_content);
5280 break;
5282 err = install_symlink(&is_bad_symlink,
5283 a->worktree, ondisk_path, ie->path,
5284 blob, 0, 1, 0, 0, a->repo,
5285 a->progress_cb, a->progress_arg);
5286 } else {
5287 if (rename(path_content, ondisk_path) == -1) {
5288 err = got_error_from_errno3("rename",
5289 path_content, ondisk_path);
5290 goto done;
5293 } else {
5294 int is_bad_symlink = 0;
5295 if (te && S_ISLNK(te->mode)) {
5296 err = install_symlink(&is_bad_symlink,
5297 a->worktree, ondisk_path, ie->path,
5298 blob, 0, 1, 0, 0, a->repo,
5299 a->progress_cb, a->progress_arg);
5300 } else {
5301 err = install_blob(a->worktree, ondisk_path,
5302 ie->path,
5303 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5304 got_fileindex_perms_to_st(ie), blob,
5305 0, 1, 0, 0, NULL, a->repo,
5306 a->progress_cb, a->progress_arg);
5308 if (err)
5309 goto done;
5310 if (status == GOT_STATUS_DELETE ||
5311 status == GOT_STATUS_MODE_CHANGE) {
5312 err = got_fileindex_entry_update(ie,
5313 a->worktree->root_fd, relpath,
5314 blob->id.sha1,
5315 a->worktree->base_commit_id->sha1, 1);
5316 if (err)
5317 goto done;
5319 if (is_bad_symlink) {
5320 got_fileindex_entry_filetype_set(ie,
5321 GOT_FILEIDX_MODE_BAD_SYMLINK);
5324 break;
5326 default:
5327 break;
5329 done:
5330 free(ondisk_path);
5331 free(path_content);
5332 free(parent_path);
5333 free(tree_path);
5334 if (fd != -1 && close(fd) == -1 && err == NULL)
5335 err = got_error_from_errno("close");
5336 if (blob)
5337 got_object_blob_close(blob);
5338 if (tree)
5339 got_object_tree_close(tree);
5340 free(tree_id);
5341 if (base_commit)
5342 got_object_commit_close(base_commit);
5343 return err;
5346 const struct got_error *
5347 got_worktree_revert(struct got_worktree *worktree,
5348 struct got_pathlist_head *paths,
5349 got_worktree_checkout_cb progress_cb, void *progress_arg,
5350 got_worktree_patch_cb patch_cb, void *patch_arg,
5351 struct got_repository *repo)
5353 struct got_fileindex *fileindex = NULL;
5354 char *fileindex_path = NULL;
5355 const struct got_error *err = NULL, *unlockerr = NULL;
5356 const struct got_error *sync_err = NULL;
5357 struct got_pathlist_entry *pe;
5358 struct revert_file_args rfa;
5360 err = lock_worktree(worktree, LOCK_EX);
5361 if (err)
5362 return err;
5364 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5365 if (err)
5366 goto done;
5368 rfa.worktree = worktree;
5369 rfa.fileindex = fileindex;
5370 rfa.progress_cb = progress_cb;
5371 rfa.progress_arg = progress_arg;
5372 rfa.patch_cb = patch_cb;
5373 rfa.patch_arg = patch_arg;
5374 rfa.repo = repo;
5375 rfa.unlink_added_files = 0;
5376 TAILQ_FOREACH(pe, paths, entry) {
5377 err = worktree_status(worktree, pe->path, fileindex, repo,
5378 revert_file, &rfa, NULL, NULL, 1, 0);
5379 if (err)
5380 break;
5382 sync_err = sync_fileindex(fileindex, fileindex_path);
5383 if (sync_err && err == NULL)
5384 err = sync_err;
5385 done:
5386 free(fileindex_path);
5387 if (fileindex)
5388 got_fileindex_free(fileindex);
5389 unlockerr = lock_worktree(worktree, LOCK_SH);
5390 if (unlockerr && err == NULL)
5391 err = unlockerr;
5392 return err;
5395 static void
5396 free_commitable(struct got_commitable *ct)
5398 free(ct->path);
5399 free(ct->in_repo_path);
5400 free(ct->ondisk_path);
5401 free(ct->blob_id);
5402 free(ct->base_blob_id);
5403 free(ct->staged_blob_id);
5404 free(ct->base_commit_id);
5405 free(ct);
5408 struct collect_commitables_arg {
5409 struct got_pathlist_head *commitable_paths;
5410 struct got_repository *repo;
5411 struct got_worktree *worktree;
5412 struct got_fileindex *fileindex;
5413 int have_staged_files;
5414 int allow_bad_symlinks;
5415 int diff_header_shown;
5416 int commit_conflicts;
5417 FILE *diff_outfile;
5418 FILE *f1;
5419 FILE *f2;
5423 * Create a file which contains the target path of a symlink so we can feed
5424 * it as content to the diff engine.
5426 static const struct got_error *
5427 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5428 const char *abspath)
5430 const struct got_error *err = NULL;
5431 char target_path[PATH_MAX];
5432 ssize_t target_len, outlen;
5434 *fd = -1;
5436 if (dirfd != -1) {
5437 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5438 if (target_len == -1)
5439 return got_error_from_errno2("readlinkat", abspath);
5440 } else {
5441 target_len = readlink(abspath, target_path, PATH_MAX);
5442 if (target_len == -1)
5443 return got_error_from_errno2("readlink", abspath);
5446 *fd = got_opentempfd();
5447 if (*fd == -1)
5448 return got_error_from_errno("got_opentempfd");
5450 outlen = write(*fd, target_path, target_len);
5451 if (outlen == -1) {
5452 err = got_error_from_errno("got_opentempfd");
5453 goto done;
5456 if (lseek(*fd, 0, SEEK_SET) == -1) {
5457 err = got_error_from_errno2("lseek", abspath);
5458 goto done;
5460 done:
5461 if (err) {
5462 close(*fd);
5463 *fd = -1;
5465 return err;
5468 static const struct got_error *
5469 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5470 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5471 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5473 const struct got_error *err = NULL;
5474 struct got_blob_object *blob1 = NULL;
5475 int fd = -1, fd1 = -1, fd2 = -1;
5476 FILE *ondisk_file = NULL;
5477 char *label1 = NULL;
5478 struct stat sb;
5479 off_t size1 = 0;
5480 int f2_exists = 0;
5481 char *id_str = NULL;
5483 memset(&sb, 0, sizeof(sb));
5485 if (diff_staged) {
5486 if (ct->staged_status != GOT_STATUS_MODIFY &&
5487 ct->staged_status != GOT_STATUS_ADD &&
5488 ct->staged_status != GOT_STATUS_DELETE)
5489 return NULL;
5490 } else {
5491 if (ct->status != GOT_STATUS_MODIFY &&
5492 ct->status != GOT_STATUS_ADD &&
5493 ct->status != GOT_STATUS_DELETE &&
5494 ct->status != GOT_STATUS_CONFLICT)
5495 return NULL;
5498 err = got_opentemp_truncate(f1);
5499 if (err)
5500 return got_error_from_errno("got_opentemp_truncate");
5501 err = got_opentemp_truncate(f2);
5502 if (err)
5503 return got_error_from_errno("got_opentemp_truncate");
5505 if (!*diff_header_shown) {
5506 err = got_object_id_str(&id_str, worktree->base_commit_id);
5507 if (err)
5508 return err;
5509 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5510 got_worktree_get_root_path(worktree));
5511 fprintf(diff_outfile, "commit - %s\n", id_str);
5512 fprintf(diff_outfile, "path + %s%s\n",
5513 got_worktree_get_root_path(worktree),
5514 diff_staged ? " (staged changes)" : "");
5515 *diff_header_shown = 1;
5518 if (diff_staged) {
5519 const char *label1 = NULL, *label2 = NULL;
5520 switch (ct->staged_status) {
5521 case GOT_STATUS_MODIFY:
5522 label1 = ct->path;
5523 label2 = ct->path;
5524 break;
5525 case GOT_STATUS_ADD:
5526 label2 = ct->path;
5527 break;
5528 case GOT_STATUS_DELETE:
5529 label1 = ct->path;
5530 break;
5531 default:
5532 return got_error(GOT_ERR_FILE_STATUS);
5534 fd1 = got_opentempfd();
5535 if (fd1 == -1) {
5536 err = got_error_from_errno("got_opentempfd");
5537 goto done;
5539 fd2 = got_opentempfd();
5540 if (fd2 == -1) {
5541 err = got_error_from_errno("got_opentempfd");
5542 goto done;
5544 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5545 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5546 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5547 NULL, repo, diff_outfile);
5548 goto done;
5551 fd1 = got_opentempfd();
5552 if (fd1 == -1) {
5553 err = got_error_from_errno("got_opentempfd");
5554 goto done;
5557 if (ct->status != GOT_STATUS_ADD) {
5558 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5559 8192, fd1);
5560 if (err)
5561 goto done;
5564 if (ct->status != GOT_STATUS_DELETE) {
5565 if (dirfd != -1) {
5566 fd = openat(dirfd, de_name,
5567 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5568 if (fd == -1) {
5569 if (!got_err_open_nofollow_on_symlink()) {
5570 err = got_error_from_errno2("openat",
5571 ct->ondisk_path);
5572 goto done;
5574 err = get_symlink_target_file(&fd, dirfd,
5575 de_name, ct->ondisk_path);
5576 if (err)
5577 goto done;
5579 } else {
5580 fd = open(ct->ondisk_path,
5581 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5582 if (fd == -1) {
5583 if (!got_err_open_nofollow_on_symlink()) {
5584 err = got_error_from_errno2("open",
5585 ct->ondisk_path);
5586 goto done;
5588 err = get_symlink_target_file(&fd, dirfd,
5589 de_name, ct->ondisk_path);
5590 if (err)
5591 goto done;
5594 if (fstatat(fd, ct->ondisk_path, &sb,
5595 AT_SYMLINK_NOFOLLOW) == -1) {
5596 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5597 goto done;
5599 ondisk_file = fdopen(fd, "r");
5600 if (ondisk_file == NULL) {
5601 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5602 goto done;
5604 fd = -1;
5605 f2_exists = 1;
5608 if (blob1) {
5609 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5610 f1, blob1);
5611 if (err)
5612 goto done;
5615 err = got_diff_blob_file(blob1, f1, size1, label1,
5616 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5617 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5618 done:
5619 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5620 err = got_error_from_errno("close");
5621 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5622 err = got_error_from_errno("close");
5623 if (blob1)
5624 got_object_blob_close(blob1);
5625 if (fd != -1 && close(fd) == -1 && err == NULL)
5626 err = got_error_from_errno("close");
5627 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5628 err = got_error_from_errno("fclose");
5629 return err;
5632 static const struct got_error *
5633 collect_commitables(void *arg, unsigned char status,
5634 unsigned char staged_status, const char *relpath,
5635 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5636 struct got_object_id *commit_id, int dirfd, const char *de_name)
5638 struct collect_commitables_arg *a = arg;
5639 const struct got_error *err = NULL;
5640 struct got_commitable *ct = NULL;
5641 struct got_pathlist_entry *new = NULL;
5642 char *parent_path = NULL, *path = NULL;
5643 struct stat sb;
5645 if (a->have_staged_files) {
5646 if (staged_status != GOT_STATUS_MODIFY &&
5647 staged_status != GOT_STATUS_ADD &&
5648 staged_status != GOT_STATUS_DELETE)
5649 return NULL;
5650 } else {
5651 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5652 printf("C %s\n", relpath);
5653 return got_error(GOT_ERR_COMMIT_CONFLICT);
5656 if (status != GOT_STATUS_MODIFY &&
5657 status != GOT_STATUS_MODE_CHANGE &&
5658 status != GOT_STATUS_ADD &&
5659 status != GOT_STATUS_DELETE &&
5660 status != GOT_STATUS_CONFLICT)
5661 return NULL;
5664 if (asprintf(&path, "/%s", relpath) == -1) {
5665 err = got_error_from_errno("asprintf");
5666 goto done;
5668 if (strcmp(path, "/") == 0) {
5669 parent_path = strdup("");
5670 if (parent_path == NULL)
5671 return got_error_from_errno("strdup");
5672 } else {
5673 err = got_path_dirname(&parent_path, path);
5674 if (err)
5675 return err;
5678 ct = calloc(1, sizeof(*ct));
5679 if (ct == NULL) {
5680 err = got_error_from_errno("calloc");
5681 goto done;
5684 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5685 relpath) == -1) {
5686 err = got_error_from_errno("asprintf");
5687 goto done;
5690 if (staged_status == GOT_STATUS_ADD ||
5691 staged_status == GOT_STATUS_MODIFY) {
5692 struct got_fileindex_entry *ie;
5693 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5694 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5695 case GOT_FILEIDX_MODE_REGULAR_FILE:
5696 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5697 ct->mode = S_IFREG;
5698 break;
5699 case GOT_FILEIDX_MODE_SYMLINK:
5700 ct->mode = S_IFLNK;
5701 break;
5702 default:
5703 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5704 goto done;
5706 ct->mode |= got_fileindex_entry_perms_get(ie);
5707 } else if (status != GOT_STATUS_DELETE &&
5708 staged_status != GOT_STATUS_DELETE) {
5709 if (dirfd != -1) {
5710 if (fstatat(dirfd, de_name, &sb,
5711 AT_SYMLINK_NOFOLLOW) == -1) {
5712 err = got_error_from_errno2("fstatat",
5713 ct->ondisk_path);
5714 goto done;
5716 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5717 err = got_error_from_errno2("lstat", ct->ondisk_path);
5718 goto done;
5720 ct->mode = sb.st_mode;
5723 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5724 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5725 relpath) == -1) {
5726 err = got_error_from_errno("asprintf");
5727 goto done;
5730 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5731 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5732 int is_bad_symlink;
5733 char target_path[PATH_MAX];
5734 ssize_t target_len;
5735 target_len = readlink(ct->ondisk_path, target_path,
5736 sizeof(target_path));
5737 if (target_len == -1) {
5738 err = got_error_from_errno2("readlink",
5739 ct->ondisk_path);
5740 goto done;
5742 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5743 target_len, ct->ondisk_path, a->worktree->root_path,
5744 a->worktree->meta_dir);
5745 if (err)
5746 goto done;
5747 if (is_bad_symlink) {
5748 err = got_error_path(ct->ondisk_path,
5749 GOT_ERR_BAD_SYMLINK);
5750 goto done;
5755 ct->status = status;
5756 ct->staged_status = staged_status;
5757 ct->blob_id = NULL; /* will be filled in when blob gets created */
5758 if (ct->status != GOT_STATUS_ADD &&
5759 ct->staged_status != GOT_STATUS_ADD) {
5760 ct->base_blob_id = got_object_id_dup(blob_id);
5761 if (ct->base_blob_id == NULL) {
5762 err = got_error_from_errno("got_object_id_dup");
5763 goto done;
5765 ct->base_commit_id = got_object_id_dup(commit_id);
5766 if (ct->base_commit_id == NULL) {
5767 err = got_error_from_errno("got_object_id_dup");
5768 goto done;
5771 if (ct->staged_status == GOT_STATUS_ADD ||
5772 ct->staged_status == GOT_STATUS_MODIFY) {
5773 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5774 if (ct->staged_blob_id == NULL) {
5775 err = got_error_from_errno("got_object_id_dup");
5776 goto done;
5779 ct->path = strdup(path);
5780 if (ct->path == NULL) {
5781 err = got_error_from_errno("strdup");
5782 goto done;
5785 if (a->diff_outfile) {
5786 err = append_ct_diff(ct, &a->diff_header_shown,
5787 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5788 a->have_staged_files, a->repo, a->worktree);
5789 if (err)
5790 goto done;
5793 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5794 done:
5795 if (ct && (err || new == NULL))
5796 free_commitable(ct);
5797 free(parent_path);
5798 free(path);
5799 return err;
5802 static const struct got_error *write_tree(struct got_object_id **, int *,
5803 struct got_tree_object *, const char *, struct got_pathlist_head *,
5804 got_worktree_status_cb status_cb, void *status_arg,
5805 struct got_repository *);
5807 static const struct got_error *
5808 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5809 struct got_tree_entry *te, const char *parent_path,
5810 struct got_pathlist_head *commitable_paths,
5811 got_worktree_status_cb status_cb, void *status_arg,
5812 struct got_repository *repo)
5814 const struct got_error *err = NULL;
5815 struct got_tree_object *subtree;
5816 char *subpath;
5818 if (asprintf(&subpath, "%s%s%s", parent_path,
5819 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5820 return got_error_from_errno("asprintf");
5822 err = got_object_open_as_tree(&subtree, repo, &te->id);
5823 if (err)
5824 return err;
5826 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5827 commitable_paths, status_cb, status_arg, repo);
5828 got_object_tree_close(subtree);
5829 free(subpath);
5830 return err;
5833 static const struct got_error *
5834 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5836 const struct got_error *err = NULL;
5837 char *ct_parent_path = NULL;
5839 *match = 0;
5841 if (strchr(ct->in_repo_path, '/') == NULL) {
5842 *match = got_path_is_root_dir(path);
5843 return NULL;
5846 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5847 if (err)
5848 return err;
5849 *match = (strcmp(path, ct_parent_path) == 0);
5850 free(ct_parent_path);
5851 return err;
5854 static mode_t
5855 get_ct_file_mode(struct got_commitable *ct)
5857 if (S_ISLNK(ct->mode))
5858 return S_IFLNK;
5860 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5863 static const struct got_error *
5864 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5865 struct got_tree_entry *te, struct got_commitable *ct)
5867 const struct got_error *err = NULL;
5869 *new_te = NULL;
5871 err = got_object_tree_entry_dup(new_te, te);
5872 if (err)
5873 goto done;
5875 (*new_te)->mode = get_ct_file_mode(ct);
5877 if (ct->staged_status == GOT_STATUS_MODIFY)
5878 memcpy(&(*new_te)->id, ct->staged_blob_id,
5879 sizeof((*new_te)->id));
5880 else
5881 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5882 done:
5883 if (err && *new_te) {
5884 free(*new_te);
5885 *new_te = NULL;
5887 return err;
5890 static const struct got_error *
5891 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5892 struct got_commitable *ct)
5894 const struct got_error *err = NULL;
5895 char *ct_name = NULL;
5897 *new_te = NULL;
5899 *new_te = calloc(1, sizeof(**new_te));
5900 if (*new_te == NULL)
5901 return got_error_from_errno("calloc");
5903 err = got_path_basename(&ct_name, ct->path);
5904 if (err)
5905 goto done;
5906 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5907 sizeof((*new_te)->name)) {
5908 err = got_error(GOT_ERR_NO_SPACE);
5909 goto done;
5912 (*new_te)->mode = get_ct_file_mode(ct);
5914 if (ct->staged_status == GOT_STATUS_ADD)
5915 memcpy(&(*new_te)->id, ct->staged_blob_id,
5916 sizeof((*new_te)->id));
5917 else
5918 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5919 done:
5920 free(ct_name);
5921 if (err && *new_te) {
5922 free(*new_te);
5923 *new_te = NULL;
5925 return err;
5928 static const struct got_error *
5929 insert_tree_entry(struct got_tree_entry *new_te,
5930 struct got_pathlist_head *paths)
5932 const struct got_error *err = NULL;
5933 struct got_pathlist_entry *new_pe;
5935 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5936 if (err)
5937 return err;
5938 if (new_pe == NULL)
5939 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5940 return NULL;
5943 static const struct got_error *
5944 report_ct_status(struct got_commitable *ct,
5945 got_worktree_status_cb status_cb, void *status_arg)
5947 const char *ct_path = ct->path;
5948 unsigned char status;
5950 if (status_cb == NULL) /* no commit progress output desired */
5951 return NULL;
5953 while (ct_path[0] == '/')
5954 ct_path++;
5956 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5957 status = ct->staged_status;
5958 else
5959 status = ct->status;
5961 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5962 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5965 static const struct got_error *
5966 match_modified_subtree(int *modified, struct got_tree_entry *te,
5967 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5969 const struct got_error *err = NULL;
5970 struct got_pathlist_entry *pe;
5971 char *te_path;
5973 *modified = 0;
5975 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5976 got_path_is_root_dir(base_tree_path) ? "" : "/",
5977 te->name) == -1)
5978 return got_error_from_errno("asprintf");
5980 TAILQ_FOREACH(pe, commitable_paths, entry) {
5981 struct got_commitable *ct = pe->data;
5982 *modified = got_path_is_child(ct->in_repo_path, te_path,
5983 strlen(te_path));
5984 if (*modified)
5985 break;
5988 free(te_path);
5989 return err;
5992 static const struct got_error *
5993 match_deleted_or_modified_ct(struct got_commitable **ctp,
5994 struct got_tree_entry *te, const char *base_tree_path,
5995 struct got_pathlist_head *commitable_paths)
5997 const struct got_error *err = NULL;
5998 struct got_pathlist_entry *pe;
6000 *ctp = NULL;
6002 TAILQ_FOREACH(pe, commitable_paths, entry) {
6003 struct got_commitable *ct = pe->data;
6004 char *ct_name = NULL;
6005 int path_matches;
6007 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6008 if (ct->status != GOT_STATUS_MODIFY &&
6009 ct->status != GOT_STATUS_MODE_CHANGE &&
6010 ct->status != GOT_STATUS_DELETE &&
6011 ct->status != GOT_STATUS_CONFLICT)
6012 continue;
6013 } else {
6014 if (ct->staged_status != GOT_STATUS_MODIFY &&
6015 ct->staged_status != GOT_STATUS_DELETE)
6016 continue;
6019 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6020 continue;
6022 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6023 if (err)
6024 return err;
6025 if (!path_matches)
6026 continue;
6028 err = got_path_basename(&ct_name, pe->path);
6029 if (err)
6030 return err;
6032 if (strcmp(te->name, ct_name) != 0) {
6033 free(ct_name);
6034 continue;
6036 free(ct_name);
6038 *ctp = ct;
6039 break;
6042 return err;
6045 static const struct got_error *
6046 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6047 const char *child_path, const char *path_base_tree,
6048 struct got_pathlist_head *commitable_paths,
6049 got_worktree_status_cb status_cb, void *status_arg,
6050 struct got_repository *repo)
6052 const struct got_error *err = NULL;
6053 struct got_tree_entry *new_te;
6054 char *subtree_path;
6055 struct got_object_id *id = NULL;
6056 int nentries;
6058 *new_tep = NULL;
6060 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6061 got_path_is_root_dir(path_base_tree) ? "" : "/",
6062 child_path) == -1)
6063 return got_error_from_errno("asprintf");
6065 new_te = calloc(1, sizeof(*new_te));
6066 if (new_te == NULL)
6067 return got_error_from_errno("calloc");
6068 new_te->mode = S_IFDIR;
6070 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6071 sizeof(new_te->name)) {
6072 err = got_error(GOT_ERR_NO_SPACE);
6073 goto done;
6075 err = write_tree(&id, &nentries, NULL, subtree_path,
6076 commitable_paths, status_cb, status_arg, repo);
6077 if (err) {
6078 free(new_te);
6079 goto done;
6081 memcpy(&new_te->id, id, sizeof(new_te->id));
6082 done:
6083 free(id);
6084 free(subtree_path);
6085 if (err == NULL)
6086 *new_tep = new_te;
6087 return err;
6090 static const struct got_error *
6091 write_tree(struct got_object_id **new_tree_id, int *nentries,
6092 struct got_tree_object *base_tree, const char *path_base_tree,
6093 struct got_pathlist_head *commitable_paths,
6094 got_worktree_status_cb status_cb, void *status_arg,
6095 struct got_repository *repo)
6097 const struct got_error *err = NULL;
6098 struct got_pathlist_head paths;
6099 struct got_tree_entry *te, *new_te = NULL;
6100 struct got_pathlist_entry *pe;
6102 TAILQ_INIT(&paths);
6103 *nentries = 0;
6105 /* Insert, and recurse into, newly added entries first. */
6106 TAILQ_FOREACH(pe, commitable_paths, entry) {
6107 struct got_commitable *ct = pe->data;
6108 char *child_path = NULL, *slash;
6110 if ((ct->status != GOT_STATUS_ADD &&
6111 ct->staged_status != GOT_STATUS_ADD) ||
6112 (ct->flags & GOT_COMMITABLE_ADDED))
6113 continue;
6115 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6116 strlen(path_base_tree)))
6117 continue;
6119 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6120 ct->in_repo_path);
6121 if (err)
6122 goto done;
6124 slash = strchr(child_path, '/');
6125 if (slash == NULL) {
6126 err = alloc_added_blob_tree_entry(&new_te, ct);
6127 if (err)
6128 goto done;
6129 err = report_ct_status(ct, status_cb, status_arg);
6130 if (err)
6131 goto done;
6132 ct->flags |= GOT_COMMITABLE_ADDED;
6133 err = insert_tree_entry(new_te, &paths);
6134 if (err)
6135 goto done;
6136 (*nentries)++;
6137 } else {
6138 *slash = '\0'; /* trim trailing path components */
6139 if (base_tree == NULL ||
6140 got_object_tree_find_entry(base_tree, child_path)
6141 == NULL) {
6142 err = make_subtree_for_added_blob(&new_te,
6143 child_path, path_base_tree,
6144 commitable_paths, status_cb, status_arg,
6145 repo);
6146 if (err)
6147 goto done;
6148 err = insert_tree_entry(new_te, &paths);
6149 if (err)
6150 goto done;
6151 (*nentries)++;
6156 if (base_tree) {
6157 int i, nbase_entries;
6158 /* Handle modified and deleted entries. */
6159 nbase_entries = got_object_tree_get_nentries(base_tree);
6160 for (i = 0; i < nbase_entries; i++) {
6161 struct got_commitable *ct = NULL;
6163 te = got_object_tree_get_entry(base_tree, i);
6164 if (got_object_tree_entry_is_submodule(te)) {
6165 /* Entry is a submodule; just copy it. */
6166 err = got_object_tree_entry_dup(&new_te, te);
6167 if (err)
6168 goto done;
6169 err = insert_tree_entry(new_te, &paths);
6170 if (err)
6171 goto done;
6172 (*nentries)++;
6173 continue;
6176 if (S_ISDIR(te->mode)) {
6177 int modified;
6178 err = got_object_tree_entry_dup(&new_te, te);
6179 if (err)
6180 goto done;
6181 err = match_modified_subtree(&modified, te,
6182 path_base_tree, commitable_paths);
6183 if (err)
6184 goto done;
6185 /* Avoid recursion into unmodified subtrees. */
6186 if (modified) {
6187 struct got_object_id *new_id;
6188 int nsubentries;
6189 err = write_subtree(&new_id,
6190 &nsubentries, te,
6191 path_base_tree, commitable_paths,
6192 status_cb, status_arg, repo);
6193 if (err)
6194 goto done;
6195 if (nsubentries == 0) {
6196 /* All entries were deleted. */
6197 free(new_id);
6198 continue;
6200 memcpy(&new_te->id, new_id,
6201 sizeof(new_te->id));
6202 free(new_id);
6204 err = insert_tree_entry(new_te, &paths);
6205 if (err)
6206 goto done;
6207 (*nentries)++;
6208 continue;
6211 err = match_deleted_or_modified_ct(&ct, te,
6212 path_base_tree, commitable_paths);
6213 if (err)
6214 goto done;
6215 if (ct) {
6216 /* NB: Deleted entries get dropped here. */
6217 if (ct->status == GOT_STATUS_MODIFY ||
6218 ct->status == GOT_STATUS_MODE_CHANGE ||
6219 ct->status == GOT_STATUS_CONFLICT ||
6220 ct->staged_status == GOT_STATUS_MODIFY) {
6221 err = alloc_modified_blob_tree_entry(
6222 &new_te, te, ct);
6223 if (err)
6224 goto done;
6225 err = insert_tree_entry(new_te, &paths);
6226 if (err)
6227 goto done;
6228 (*nentries)++;
6230 err = report_ct_status(ct, status_cb,
6231 status_arg);
6232 if (err)
6233 goto done;
6234 } else {
6235 /* Entry is unchanged; just copy it. */
6236 err = got_object_tree_entry_dup(&new_te, te);
6237 if (err)
6238 goto done;
6239 err = insert_tree_entry(new_te, &paths);
6240 if (err)
6241 goto done;
6242 (*nentries)++;
6247 /* Write new list of entries; deleted entries have been dropped. */
6248 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6249 done:
6250 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6251 return err;
6254 static const struct got_error *
6255 update_fileindex_after_commit(struct got_worktree *worktree,
6256 struct got_pathlist_head *commitable_paths,
6257 struct got_object_id *new_base_commit_id,
6258 struct got_fileindex *fileindex, int have_staged_files)
6260 const struct got_error *err = NULL;
6261 struct got_pathlist_entry *pe;
6262 char *relpath = NULL;
6264 TAILQ_FOREACH(pe, commitable_paths, entry) {
6265 struct got_fileindex_entry *ie;
6266 struct got_commitable *ct = pe->data;
6268 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6270 err = got_path_skip_common_ancestor(&relpath,
6271 worktree->root_path, ct->ondisk_path);
6272 if (err)
6273 goto done;
6275 if (ie) {
6276 if (ct->status == GOT_STATUS_DELETE ||
6277 ct->staged_status == GOT_STATUS_DELETE) {
6278 got_fileindex_entry_remove(fileindex, ie);
6279 } else if (ct->staged_status == GOT_STATUS_ADD ||
6280 ct->staged_status == GOT_STATUS_MODIFY) {
6281 got_fileindex_entry_stage_set(ie,
6282 GOT_FILEIDX_STAGE_NONE);
6283 got_fileindex_entry_staged_filetype_set(ie, 0);
6285 err = got_fileindex_entry_update(ie,
6286 worktree->root_fd, relpath,
6287 ct->staged_blob_id->sha1,
6288 new_base_commit_id->sha1,
6289 !have_staged_files);
6290 } else
6291 err = got_fileindex_entry_update(ie,
6292 worktree->root_fd, relpath,
6293 ct->blob_id->sha1,
6294 new_base_commit_id->sha1,
6295 !have_staged_files);
6296 } else {
6297 err = got_fileindex_entry_alloc(&ie, pe->path);
6298 if (err)
6299 goto done;
6300 err = got_fileindex_entry_update(ie,
6301 worktree->root_fd, relpath, ct->blob_id->sha1,
6302 new_base_commit_id->sha1, 1);
6303 if (err) {
6304 got_fileindex_entry_free(ie);
6305 goto done;
6307 err = got_fileindex_entry_add(fileindex, ie);
6308 if (err) {
6309 got_fileindex_entry_free(ie);
6310 goto done;
6313 free(relpath);
6314 relpath = NULL;
6316 done:
6317 free(relpath);
6318 return err;
6322 static const struct got_error *
6323 check_out_of_date(const char *in_repo_path, unsigned char status,
6324 unsigned char staged_status, struct got_object_id *base_blob_id,
6325 struct got_object_id *base_commit_id,
6326 struct got_object_id *head_commit_id, struct got_repository *repo,
6327 int ood_errcode)
6329 const struct got_error *err = NULL;
6330 struct got_commit_object *commit = NULL;
6331 struct got_object_id *id = NULL;
6333 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6334 /* Trivial case: base commit == head commit */
6335 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6336 return NULL;
6338 * Ensure file content which local changes were based
6339 * on matches file content in the branch head.
6341 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6342 if (err)
6343 goto done;
6344 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6345 if (err) {
6346 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6347 err = got_error(ood_errcode);
6348 goto done;
6349 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6350 err = got_error(ood_errcode);
6351 } else {
6352 /* Require that added files don't exist in the branch head. */
6353 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6354 if (err)
6355 goto done;
6356 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6357 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6358 goto done;
6359 err = id ? got_error(ood_errcode) : NULL;
6361 done:
6362 free(id);
6363 if (commit)
6364 got_object_commit_close(commit);
6365 return err;
6368 static const struct got_error *
6369 commit_worktree(struct got_object_id **new_commit_id,
6370 struct got_pathlist_head *commitable_paths,
6371 struct got_object_id *head_commit_id,
6372 struct got_object_id *parent_id2,
6373 struct got_worktree *worktree,
6374 const char *author, const char *committer, char *diff_path,
6375 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6376 got_worktree_status_cb status_cb, void *status_arg,
6377 struct got_repository *repo)
6379 const struct got_error *err = NULL, *unlockerr = NULL;
6380 struct got_pathlist_entry *pe;
6381 const char *head_ref_name = NULL;
6382 struct got_commit_object *head_commit = NULL;
6383 struct got_reference *head_ref2 = NULL;
6384 struct got_object_id *head_commit_id2 = NULL;
6385 struct got_tree_object *head_tree = NULL;
6386 struct got_object_id *new_tree_id = NULL;
6387 int nentries, nparents = 0;
6388 struct got_object_id_queue parent_ids;
6389 struct got_object_qid *pid = NULL;
6390 char *logmsg = NULL;
6391 time_t timestamp;
6393 *new_commit_id = NULL;
6395 STAILQ_INIT(&parent_ids);
6397 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6398 if (err)
6399 goto done;
6401 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6402 if (err)
6403 goto done;
6405 if (commit_msg_cb != NULL) {
6406 err = commit_msg_cb(commitable_paths, diff_path,
6407 &logmsg, commit_arg);
6408 if (err)
6409 goto done;
6412 if (logmsg == NULL || strlen(logmsg) == 0) {
6413 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6414 goto done;
6417 /* Create blobs from added and modified files and record their IDs. */
6418 TAILQ_FOREACH(pe, commitable_paths, entry) {
6419 struct got_commitable *ct = pe->data;
6420 char *ondisk_path;
6422 /* Blobs for staged files already exist. */
6423 if (ct->staged_status == GOT_STATUS_ADD ||
6424 ct->staged_status == GOT_STATUS_MODIFY)
6425 continue;
6427 if (ct->status != GOT_STATUS_ADD &&
6428 ct->status != GOT_STATUS_MODIFY &&
6429 ct->status != GOT_STATUS_MODE_CHANGE &&
6430 ct->status != GOT_STATUS_CONFLICT)
6431 continue;
6433 if (asprintf(&ondisk_path, "%s/%s",
6434 worktree->root_path, pe->path) == -1) {
6435 err = got_error_from_errno("asprintf");
6436 goto done;
6438 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6439 free(ondisk_path);
6440 if (err)
6441 goto done;
6444 /* Recursively write new tree objects. */
6445 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6446 commitable_paths, status_cb, status_arg, repo);
6447 if (err)
6448 goto done;
6450 err = got_object_qid_alloc(&pid, head_commit_id);
6451 if (err)
6452 goto done;
6453 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6454 nparents++;
6455 if (parent_id2) {
6456 err = got_object_qid_alloc(&pid, parent_id2);
6457 if (err)
6458 goto done;
6459 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6460 nparents++;
6462 timestamp = time(NULL);
6463 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6464 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6465 if (logmsg != NULL)
6466 free(logmsg);
6467 if (err)
6468 goto done;
6470 /* Check if a concurrent commit to our branch has occurred. */
6471 head_ref_name = got_worktree_get_head_ref_name(worktree);
6472 if (head_ref_name == NULL) {
6473 err = got_error_from_errno("got_worktree_get_head_ref_name");
6474 goto done;
6476 /* Lock the reference here to prevent concurrent modification. */
6477 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6478 if (err)
6479 goto done;
6480 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6481 if (err)
6482 goto done;
6483 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6484 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6485 goto done;
6487 /* Update branch head in repository. */
6488 err = got_ref_change_ref(head_ref2, *new_commit_id);
6489 if (err)
6490 goto done;
6491 err = got_ref_write(head_ref2, repo);
6492 if (err)
6493 goto done;
6495 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6496 if (err)
6497 goto done;
6499 err = ref_base_commit(worktree, repo);
6500 if (err)
6501 goto done;
6502 done:
6503 got_object_id_queue_free(&parent_ids);
6504 if (head_tree)
6505 got_object_tree_close(head_tree);
6506 if (head_commit)
6507 got_object_commit_close(head_commit);
6508 free(head_commit_id2);
6509 if (head_ref2) {
6510 unlockerr = got_ref_unlock(head_ref2);
6511 if (unlockerr && err == NULL)
6512 err = unlockerr;
6513 got_ref_close(head_ref2);
6515 return err;
6518 static const struct got_error *
6519 check_path_is_commitable(const char *path,
6520 struct got_pathlist_head *commitable_paths)
6522 struct got_pathlist_entry *cpe = NULL;
6523 size_t path_len = strlen(path);
6525 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6526 struct got_commitable *ct = cpe->data;
6527 const char *ct_path = ct->path;
6529 while (ct_path[0] == '/')
6530 ct_path++;
6532 if (strcmp(path, ct_path) == 0 ||
6533 got_path_is_child(ct_path, path, path_len))
6534 break;
6537 if (cpe == NULL)
6538 return got_error_path(path, GOT_ERR_BAD_PATH);
6540 return NULL;
6543 static const struct got_error *
6544 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6546 int *have_staged_files = arg;
6548 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6549 *have_staged_files = 1;
6550 return got_error(GOT_ERR_CANCELLED);
6553 return NULL;
6556 static const struct got_error *
6557 check_non_staged_files(struct got_fileindex *fileindex,
6558 struct got_pathlist_head *paths)
6560 struct got_pathlist_entry *pe;
6561 struct got_fileindex_entry *ie;
6563 TAILQ_FOREACH(pe, paths, entry) {
6564 if (pe->path[0] == '\0')
6565 continue;
6566 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6567 if (ie == NULL)
6568 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6569 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6570 return got_error_path(pe->path,
6571 GOT_ERR_FILE_NOT_STAGED);
6574 return NULL;
6577 const struct got_error *
6578 got_worktree_commit(struct got_object_id **new_commit_id,
6579 struct got_worktree *worktree, struct got_pathlist_head *paths,
6580 const char *author, const char *committer, int allow_bad_symlinks,
6581 int show_diff, int commit_conflicts,
6582 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6583 got_worktree_status_cb status_cb, void *status_arg,
6584 struct got_repository *repo)
6586 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6587 struct got_fileindex *fileindex = NULL;
6588 char *fileindex_path = NULL;
6589 struct got_pathlist_head commitable_paths;
6590 struct collect_commitables_arg cc_arg;
6591 struct got_pathlist_entry *pe;
6592 struct got_reference *head_ref = NULL;
6593 struct got_object_id *head_commit_id = NULL;
6594 char *diff_path = NULL;
6595 int have_staged_files = 0;
6597 *new_commit_id = NULL;
6599 memset(&cc_arg, 0, sizeof(cc_arg));
6600 TAILQ_INIT(&commitable_paths);
6602 err = lock_worktree(worktree, LOCK_EX);
6603 if (err)
6604 goto done;
6606 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6607 if (err)
6608 goto done;
6610 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6611 if (err)
6612 goto done;
6614 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6615 if (err)
6616 goto done;
6618 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6619 &have_staged_files);
6620 if (err && err->code != GOT_ERR_CANCELLED)
6621 goto done;
6622 if (have_staged_files) {
6623 err = check_non_staged_files(fileindex, paths);
6624 if (err)
6625 goto done;
6628 cc_arg.commitable_paths = &commitable_paths;
6629 cc_arg.worktree = worktree;
6630 cc_arg.fileindex = fileindex;
6631 cc_arg.repo = repo;
6632 cc_arg.have_staged_files = have_staged_files;
6633 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6634 cc_arg.diff_header_shown = 0;
6635 cc_arg.commit_conflicts = commit_conflicts;
6636 if (show_diff) {
6637 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6638 GOT_TMPDIR_STR "/got", ".diff");
6639 if (err)
6640 goto done;
6641 cc_arg.f1 = got_opentemp();
6642 if (cc_arg.f1 == NULL) {
6643 err = got_error_from_errno("got_opentemp");
6644 goto done;
6646 cc_arg.f2 = got_opentemp();
6647 if (cc_arg.f2 == NULL) {
6648 err = got_error_from_errno("got_opentemp");
6649 goto done;
6653 TAILQ_FOREACH(pe, paths, entry) {
6654 err = worktree_status(worktree, pe->path, fileindex, repo,
6655 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6656 if (err)
6657 goto done;
6660 if (show_diff) {
6661 if (fflush(cc_arg.diff_outfile) == EOF) {
6662 err = got_error_from_errno("fflush");
6663 goto done;
6667 if (TAILQ_EMPTY(&commitable_paths)) {
6668 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6669 goto done;
6672 TAILQ_FOREACH(pe, paths, entry) {
6673 err = check_path_is_commitable(pe->path, &commitable_paths);
6674 if (err)
6675 goto done;
6678 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6679 struct got_commitable *ct = pe->data;
6680 const char *ct_path = ct->in_repo_path;
6682 while (ct_path[0] == '/')
6683 ct_path++;
6684 err = check_out_of_date(ct_path, ct->status,
6685 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6686 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6687 if (err)
6688 goto done;
6692 err = commit_worktree(new_commit_id, &commitable_paths,
6693 head_commit_id, NULL, worktree, author, committer,
6694 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6695 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6696 if (err)
6697 goto done;
6699 err = update_fileindex_after_commit(worktree, &commitable_paths,
6700 *new_commit_id, fileindex, have_staged_files);
6701 sync_err = sync_fileindex(fileindex, fileindex_path);
6702 if (sync_err && err == NULL)
6703 err = sync_err;
6704 done:
6705 if (fileindex)
6706 got_fileindex_free(fileindex);
6707 free(fileindex_path);
6708 unlockerr = lock_worktree(worktree, LOCK_SH);
6709 if (unlockerr && err == NULL)
6710 err = unlockerr;
6711 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6712 struct got_commitable *ct = pe->data;
6714 free_commitable(ct);
6716 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6717 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6718 err = got_error_from_errno2("unlink", diff_path);
6719 free(diff_path);
6720 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6721 err == NULL)
6722 err = got_error_from_errno("fclose");
6723 return err;
6726 const char *
6727 got_commitable_get_path(struct got_commitable *ct)
6729 return ct->path;
6732 unsigned int
6733 got_commitable_get_status(struct got_commitable *ct)
6735 return ct->status;
6738 struct check_rebase_ok_arg {
6739 struct got_worktree *worktree;
6740 struct got_repository *repo;
6743 static const struct got_error *
6744 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6746 const struct got_error *err = NULL;
6747 struct check_rebase_ok_arg *a = arg;
6748 unsigned char status;
6749 struct stat sb;
6750 char *ondisk_path;
6752 /* Reject rebase of a work tree with mixed base commits. */
6753 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6754 SHA1_DIGEST_LENGTH))
6755 return got_error(GOT_ERR_MIXED_COMMITS);
6757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6758 == -1)
6759 return got_error_from_errno("asprintf");
6761 /* Reject rebase of a work tree with modified or staged files. */
6762 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6763 free(ondisk_path);
6764 if (err)
6765 return err;
6767 if (status != GOT_STATUS_NO_CHANGE)
6768 return got_error(GOT_ERR_MODIFIED);
6769 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6770 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6772 return NULL;
6775 const struct got_error *
6776 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6777 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6778 struct got_worktree *worktree, struct got_reference *branch,
6779 struct got_repository *repo)
6781 const struct got_error *err = NULL;
6782 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6783 char *branch_ref_name = NULL;
6784 char *fileindex_path = NULL;
6785 struct check_rebase_ok_arg ok_arg;
6786 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6787 struct got_object_id *wt_branch_tip = NULL;
6789 *new_base_branch_ref = NULL;
6790 *tmp_branch = NULL;
6791 *fileindex = NULL;
6793 err = lock_worktree(worktree, LOCK_EX);
6794 if (err)
6795 return err;
6797 err = open_fileindex(fileindex, &fileindex_path, worktree);
6798 if (err)
6799 goto done;
6801 ok_arg.worktree = worktree;
6802 ok_arg.repo = repo;
6803 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6804 &ok_arg);
6805 if (err)
6806 goto done;
6808 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6809 if (err)
6810 goto done;
6812 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6813 if (err)
6814 goto done;
6816 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6817 if (err)
6818 goto done;
6820 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6821 0);
6822 if (err)
6823 goto done;
6825 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6826 if (err)
6827 goto done;
6828 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6829 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6830 goto done;
6833 err = got_ref_alloc_symref(new_base_branch_ref,
6834 new_base_branch_ref_name, wt_branch);
6835 if (err)
6836 goto done;
6837 err = got_ref_write(*new_base_branch_ref, repo);
6838 if (err)
6839 goto done;
6841 /* TODO Lock original branch's ref while rebasing? */
6843 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6844 if (err)
6845 goto done;
6847 err = got_ref_write(branch_ref, repo);
6848 if (err)
6849 goto done;
6851 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6852 worktree->base_commit_id);
6853 if (err)
6854 goto done;
6855 err = got_ref_write(*tmp_branch, repo);
6856 if (err)
6857 goto done;
6859 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6860 if (err)
6861 goto done;
6862 done:
6863 free(fileindex_path);
6864 free(tmp_branch_name);
6865 free(new_base_branch_ref_name);
6866 free(branch_ref_name);
6867 if (branch_ref)
6868 got_ref_close(branch_ref);
6869 if (wt_branch)
6870 got_ref_close(wt_branch);
6871 free(wt_branch_tip);
6872 if (err) {
6873 if (*new_base_branch_ref) {
6874 got_ref_close(*new_base_branch_ref);
6875 *new_base_branch_ref = NULL;
6877 if (*tmp_branch) {
6878 got_ref_close(*tmp_branch);
6879 *tmp_branch = NULL;
6881 if (*fileindex) {
6882 got_fileindex_free(*fileindex);
6883 *fileindex = NULL;
6885 lock_worktree(worktree, LOCK_SH);
6887 return err;
6890 const struct got_error *
6891 got_worktree_rebase_continue(struct got_object_id **commit_id,
6892 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6893 struct got_reference **branch, struct got_fileindex **fileindex,
6894 struct got_worktree *worktree, struct got_repository *repo)
6896 const struct got_error *err;
6897 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6898 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6899 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6900 char *fileindex_path = NULL;
6901 int have_staged_files = 0;
6903 *commit_id = NULL;
6904 *new_base_branch = NULL;
6905 *tmp_branch = NULL;
6906 *branch = NULL;
6907 *fileindex = NULL;
6909 err = lock_worktree(worktree, LOCK_EX);
6910 if (err)
6911 return err;
6913 err = open_fileindex(fileindex, &fileindex_path, worktree);
6914 if (err)
6915 goto done;
6917 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6918 &have_staged_files);
6919 if (err && err->code != GOT_ERR_CANCELLED)
6920 goto done;
6921 if (have_staged_files) {
6922 err = got_error(GOT_ERR_STAGED_PATHS);
6923 goto done;
6926 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6927 if (err)
6928 goto done;
6930 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6931 if (err)
6932 goto done;
6934 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6935 if (err)
6936 goto done;
6938 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6939 if (err)
6940 goto done;
6942 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(branch, repo,
6947 got_ref_get_symref_target(branch_ref), 0);
6948 if (err)
6949 goto done;
6951 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6952 if (err)
6953 goto done;
6955 err = got_ref_resolve(commit_id, repo, commit_ref);
6956 if (err)
6957 goto done;
6959 err = got_ref_open(new_base_branch, repo,
6960 new_base_branch_ref_name, 0);
6961 if (err)
6962 goto done;
6964 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6965 if (err)
6966 goto done;
6967 done:
6968 free(commit_ref_name);
6969 free(branch_ref_name);
6970 free(fileindex_path);
6971 if (commit_ref)
6972 got_ref_close(commit_ref);
6973 if (branch_ref)
6974 got_ref_close(branch_ref);
6975 if (err) {
6976 free(*commit_id);
6977 *commit_id = NULL;
6978 if (*tmp_branch) {
6979 got_ref_close(*tmp_branch);
6980 *tmp_branch = NULL;
6982 if (*new_base_branch) {
6983 got_ref_close(*new_base_branch);
6984 *new_base_branch = NULL;
6986 if (*branch) {
6987 got_ref_close(*branch);
6988 *branch = NULL;
6990 if (*fileindex) {
6991 got_fileindex_free(*fileindex);
6992 *fileindex = NULL;
6994 lock_worktree(worktree, LOCK_SH);
6996 return err;
6999 const struct got_error *
7000 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7002 const struct got_error *err;
7003 char *tmp_branch_name = NULL;
7005 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7006 if (err)
7007 return err;
7009 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7010 free(tmp_branch_name);
7011 return NULL;
7014 static const struct got_error *
7015 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7016 const char *diff_path, char **logmsg, void *arg)
7018 *logmsg = arg;
7019 return NULL;
7022 static const struct got_error *
7023 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7024 const char *path, struct got_object_id *blob_id,
7025 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7026 int dirfd, const char *de_name)
7028 return NULL;
7031 struct collect_merged_paths_arg {
7032 got_worktree_checkout_cb progress_cb;
7033 void *progress_arg;
7034 struct got_pathlist_head *merged_paths;
7037 static const struct got_error *
7038 collect_merged_paths(void *arg, unsigned char status, const char *path)
7040 const struct got_error *err;
7041 struct collect_merged_paths_arg *a = arg;
7042 char *p;
7043 struct got_pathlist_entry *new;
7045 err = (*a->progress_cb)(a->progress_arg, status, path);
7046 if (err)
7047 return err;
7049 if (status != GOT_STATUS_MERGE &&
7050 status != GOT_STATUS_ADD &&
7051 status != GOT_STATUS_DELETE &&
7052 status != GOT_STATUS_CONFLICT)
7053 return NULL;
7055 p = strdup(path);
7056 if (p == NULL)
7057 return got_error_from_errno("strdup");
7059 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7060 if (err || new == NULL)
7061 free(p);
7062 return err;
7065 static const struct got_error *
7066 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7067 int is_rebase, struct got_repository *repo)
7069 const struct got_error *err;
7070 struct got_reference *commit_ref = NULL;
7072 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7073 if (err) {
7074 if (err->code != GOT_ERR_NOT_REF)
7075 goto done;
7076 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7077 if (err)
7078 goto done;
7079 err = got_ref_write(commit_ref, repo);
7080 if (err)
7081 goto done;
7082 } else if (is_rebase) {
7083 struct got_object_id *stored_id;
7084 int cmp;
7086 err = got_ref_resolve(&stored_id, repo, commit_ref);
7087 if (err)
7088 goto done;
7089 cmp = got_object_id_cmp(commit_id, stored_id);
7090 free(stored_id);
7091 if (cmp != 0) {
7092 err = got_error(GOT_ERR_REBASE_COMMITID);
7093 goto done;
7096 done:
7097 if (commit_ref)
7098 got_ref_close(commit_ref);
7099 return err;
7102 static const struct got_error *
7103 rebase_merge_files(struct got_pathlist_head *merged_paths,
7104 const char *commit_ref_name, struct got_worktree *worktree,
7105 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7106 struct got_object_id *commit_id, struct got_repository *repo,
7107 got_worktree_checkout_cb progress_cb, void *progress_arg,
7108 got_cancel_cb cancel_cb, void *cancel_arg)
7110 const struct got_error *err;
7111 struct got_reference *commit_ref = NULL;
7112 struct collect_merged_paths_arg cmp_arg;
7113 char *fileindex_path;
7115 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7117 err = get_fileindex_path(&fileindex_path, worktree);
7118 if (err)
7119 return err;
7121 cmp_arg.progress_cb = progress_cb;
7122 cmp_arg.progress_arg = progress_arg;
7123 cmp_arg.merged_paths = merged_paths;
7124 err = merge_files(worktree, fileindex, fileindex_path,
7125 parent_commit_id, commit_id, repo, collect_merged_paths,
7126 &cmp_arg, cancel_cb, cancel_arg);
7127 if (commit_ref)
7128 got_ref_close(commit_ref);
7129 return err;
7132 const struct got_error *
7133 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7134 struct got_worktree *worktree, struct got_fileindex *fileindex,
7135 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7136 struct got_repository *repo,
7137 got_worktree_checkout_cb progress_cb, void *progress_arg,
7138 got_cancel_cb cancel_cb, void *cancel_arg)
7140 const struct got_error *err;
7141 char *commit_ref_name;
7143 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7144 if (err)
7145 return err;
7147 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7148 if (err)
7149 goto done;
7151 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7152 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7153 progress_arg, cancel_cb, cancel_arg);
7154 done:
7155 free(commit_ref_name);
7156 return err;
7159 const struct got_error *
7160 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7161 struct got_worktree *worktree, struct got_fileindex *fileindex,
7162 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7163 struct got_repository *repo,
7164 got_worktree_checkout_cb progress_cb, void *progress_arg,
7165 got_cancel_cb cancel_cb, void *cancel_arg)
7167 const struct got_error *err;
7168 char *commit_ref_name;
7170 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7171 if (err)
7172 return err;
7174 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7175 if (err)
7176 goto done;
7178 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7179 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7180 progress_arg, cancel_cb, cancel_arg);
7181 done:
7182 free(commit_ref_name);
7183 return err;
7186 static const struct got_error *
7187 rebase_commit(struct got_object_id **new_commit_id,
7188 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7189 struct got_worktree *worktree, struct got_fileindex *fileindex,
7190 struct got_reference *tmp_branch, const char *committer,
7191 struct got_commit_object *orig_commit, const char *new_logmsg,
7192 int allow_conflict, struct got_repository *repo)
7194 const struct got_error *err, *sync_err;
7195 struct got_pathlist_head commitable_paths;
7196 struct collect_commitables_arg cc_arg;
7197 char *fileindex_path = NULL;
7198 struct got_reference *head_ref = NULL;
7199 struct got_object_id *head_commit_id = NULL;
7200 char *logmsg = NULL;
7202 memset(&cc_arg, 0, sizeof(cc_arg));
7203 TAILQ_INIT(&commitable_paths);
7204 *new_commit_id = NULL;
7206 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7208 err = get_fileindex_path(&fileindex_path, worktree);
7209 if (err)
7210 return err;
7212 cc_arg.commitable_paths = &commitable_paths;
7213 cc_arg.worktree = worktree;
7214 cc_arg.repo = repo;
7215 cc_arg.have_staged_files = 0;
7216 cc_arg.commit_conflicts = allow_conflict;
7218 * If possible get the status of individual files directly to
7219 * avoid crawling the entire work tree once per rebased commit.
7221 * Ideally, merged_paths would contain a list of commitables
7222 * we could use so we could skip worktree_status() entirely.
7223 * However, we would then need carefully keep track of cumulative
7224 * effects of operations such as file additions and deletions
7225 * in 'got histedit -f' (folding multiple commits into one),
7226 * and this extra complexity is not really worth it.
7228 if (merged_paths) {
7229 struct got_pathlist_entry *pe;
7230 TAILQ_FOREACH(pe, merged_paths, entry) {
7231 err = worktree_status(worktree, pe->path, fileindex,
7232 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7233 0);
7234 if (err)
7235 goto done;
7237 } else {
7238 err = worktree_status(worktree, "", fileindex, repo,
7239 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7240 if (err)
7241 goto done;
7244 if (TAILQ_EMPTY(&commitable_paths)) {
7245 /* No-op change; commit will be elided. */
7246 err = got_ref_delete(commit_ref, repo);
7247 if (err)
7248 goto done;
7249 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7250 goto done;
7253 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7254 if (err)
7255 goto done;
7257 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7258 if (err)
7259 goto done;
7261 if (new_logmsg) {
7262 logmsg = strdup(new_logmsg);
7263 if (logmsg == NULL) {
7264 err = got_error_from_errno("strdup");
7265 goto done;
7267 } else {
7268 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7269 if (err)
7270 goto done;
7273 /* NB: commit_worktree will call free(logmsg) */
7274 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7275 NULL, worktree, got_object_commit_get_author(orig_commit),
7276 committer ? committer :
7277 got_object_commit_get_committer(orig_commit), NULL,
7278 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7279 if (err)
7280 goto done;
7282 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7283 if (err)
7284 goto done;
7286 err = got_ref_delete(commit_ref, repo);
7287 if (err)
7288 goto done;
7290 err = update_fileindex_after_commit(worktree, &commitable_paths,
7291 *new_commit_id, fileindex, 0);
7292 sync_err = sync_fileindex(fileindex, fileindex_path);
7293 if (sync_err && err == NULL)
7294 err = sync_err;
7295 done:
7296 free(fileindex_path);
7297 free(head_commit_id);
7298 if (head_ref)
7299 got_ref_close(head_ref);
7300 if (err) {
7301 free(*new_commit_id);
7302 *new_commit_id = NULL;
7304 return err;
7307 const struct got_error *
7308 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7309 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7310 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7311 const char *committer, struct got_commit_object *orig_commit,
7312 struct got_object_id *orig_commit_id, int allow_conflict,
7313 struct got_repository *repo)
7315 const struct got_error *err;
7316 char *commit_ref_name;
7317 struct got_reference *commit_ref = NULL;
7318 struct got_object_id *commit_id = NULL;
7320 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7321 if (err)
7322 return err;
7324 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7325 if (err)
7326 goto done;
7327 err = got_ref_resolve(&commit_id, repo, commit_ref);
7328 if (err)
7329 goto done;
7330 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7331 err = got_error(GOT_ERR_REBASE_COMMITID);
7332 goto done;
7335 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7336 worktree, fileindex, tmp_branch, committer, orig_commit,
7337 NULL, allow_conflict, repo);
7338 done:
7339 if (commit_ref)
7340 got_ref_close(commit_ref);
7341 free(commit_ref_name);
7342 free(commit_id);
7343 return err;
7346 const struct got_error *
7347 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7348 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7349 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7350 const char *committer, struct got_commit_object *orig_commit,
7351 struct got_object_id *orig_commit_id, const char *new_logmsg,
7352 int allow_conflict, struct got_repository *repo)
7354 const struct got_error *err;
7355 char *commit_ref_name;
7356 struct got_reference *commit_ref = NULL;
7358 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7359 if (err)
7360 return err;
7362 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7363 if (err)
7364 goto done;
7366 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7367 worktree, fileindex, tmp_branch, committer, orig_commit,
7368 new_logmsg, allow_conflict, repo);
7369 done:
7370 if (commit_ref)
7371 got_ref_close(commit_ref);
7372 free(commit_ref_name);
7373 return err;
7376 const struct got_error *
7377 got_worktree_rebase_postpone(struct got_worktree *worktree,
7378 struct got_fileindex *fileindex)
7380 if (fileindex)
7381 got_fileindex_free(fileindex);
7382 return lock_worktree(worktree, LOCK_SH);
7385 static const struct got_error *
7386 delete_ref(const char *name, struct got_repository *repo)
7388 const struct got_error *err;
7389 struct got_reference *ref;
7391 err = got_ref_open(&ref, repo, name, 0);
7392 if (err) {
7393 if (err->code == GOT_ERR_NOT_REF)
7394 return NULL;
7395 return err;
7398 err = got_ref_delete(ref, repo);
7399 got_ref_close(ref);
7400 return err;
7403 static const struct got_error *
7404 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7406 const struct got_error *err;
7407 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7408 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7410 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7411 if (err)
7412 goto done;
7413 err = delete_ref(tmp_branch_name, repo);
7414 if (err)
7415 goto done;
7417 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7418 if (err)
7419 goto done;
7420 err = delete_ref(new_base_branch_ref_name, repo);
7421 if (err)
7422 goto done;
7424 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7425 if (err)
7426 goto done;
7427 err = delete_ref(branch_ref_name, repo);
7428 if (err)
7429 goto done;
7431 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7432 if (err)
7433 goto done;
7434 err = delete_ref(commit_ref_name, repo);
7435 if (err)
7436 goto done;
7438 done:
7439 free(tmp_branch_name);
7440 free(new_base_branch_ref_name);
7441 free(branch_ref_name);
7442 free(commit_ref_name);
7443 return err;
7446 static const struct got_error *
7447 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7448 struct got_object_id *new_commit_id, struct got_repository *repo)
7450 const struct got_error *err;
7451 struct got_reference *ref = NULL;
7452 struct got_object_id *old_commit_id = NULL;
7453 const char *branch_name = NULL;
7454 char *new_id_str = NULL;
7455 char *refname = NULL;
7457 branch_name = got_ref_get_name(branch);
7458 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7459 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7460 branch_name += 11;
7462 err = got_object_id_str(&new_id_str, new_commit_id);
7463 if (err)
7464 return err;
7466 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7467 new_id_str) == -1) {
7468 err = got_error_from_errno("asprintf");
7469 goto done;
7472 err = got_ref_resolve(&old_commit_id, repo, branch);
7473 if (err)
7474 goto done;
7476 err = got_ref_alloc(&ref, refname, old_commit_id);
7477 if (err)
7478 goto done;
7480 err = got_ref_write(ref, repo);
7481 done:
7482 free(new_id_str);
7483 free(refname);
7484 free(old_commit_id);
7485 if (ref)
7486 got_ref_close(ref);
7487 return err;
7490 const struct got_error *
7491 got_worktree_rebase_complete(struct got_worktree *worktree,
7492 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7493 struct got_reference *rebased_branch, struct got_repository *repo,
7494 int create_backup)
7496 const struct got_error *err, *unlockerr, *sync_err;
7497 struct got_object_id *new_head_commit_id = NULL;
7498 char *fileindex_path = NULL;
7500 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7501 if (err)
7502 return err;
7504 if (create_backup) {
7505 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7506 rebased_branch, new_head_commit_id, repo);
7507 if (err)
7508 goto done;
7511 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7512 if (err)
7513 goto done;
7515 err = got_ref_write(rebased_branch, repo);
7516 if (err)
7517 goto done;
7519 err = got_worktree_set_head_ref(worktree, rebased_branch);
7520 if (err)
7521 goto done;
7523 err = delete_rebase_refs(worktree, repo);
7524 if (err)
7525 goto done;
7527 err = get_fileindex_path(&fileindex_path, worktree);
7528 if (err)
7529 goto done;
7530 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7531 sync_err = sync_fileindex(fileindex, fileindex_path);
7532 if (sync_err && err == NULL)
7533 err = sync_err;
7534 done:
7535 got_fileindex_free(fileindex);
7536 free(fileindex_path);
7537 free(new_head_commit_id);
7538 unlockerr = lock_worktree(worktree, LOCK_SH);
7539 if (unlockerr && err == NULL)
7540 err = unlockerr;
7541 return err;
7544 static const struct got_error *
7545 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7546 struct got_object_id *id1, struct got_object_id *id2,
7547 struct got_repository *repo)
7549 const struct got_error *err;
7550 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7551 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7553 if (id1) {
7554 err = got_object_open_as_commit(&commit1, repo, id1);
7555 if (err)
7556 goto done;
7558 err = got_object_open_as_tree(&tree1, repo,
7559 got_object_commit_get_tree_id(commit1));
7560 if (err)
7561 goto done;
7564 if (id2) {
7565 err = got_object_open_as_commit(&commit2, repo, id2);
7566 if (err)
7567 goto done;
7569 err = got_object_open_as_tree(&tree2, repo,
7570 got_object_commit_get_tree_id(commit2));
7571 if (err)
7572 goto done;
7575 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7576 got_diff_tree_collect_changed_paths, paths, 0);
7577 if (err)
7578 goto done;
7579 done:
7580 if (commit1)
7581 got_object_commit_close(commit1);
7582 if (commit2)
7583 got_object_commit_close(commit2);
7584 if (tree1)
7585 got_object_tree_close(tree1);
7586 if (tree2)
7587 got_object_tree_close(tree2);
7588 return err;
7591 static const struct got_error *
7592 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7593 struct got_object_id *id1, struct got_object_id *id2,
7594 const char *path_prefix, struct got_repository *repo)
7596 const struct got_error *err;
7597 struct got_pathlist_head merged_paths;
7598 struct got_pathlist_entry *pe;
7599 char *abspath = NULL, *wt_path = NULL;
7601 TAILQ_INIT(&merged_paths);
7603 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7604 if (err)
7605 goto done;
7607 TAILQ_FOREACH(pe, &merged_paths, entry) {
7608 struct got_diff_changed_path *change = pe->data;
7610 if (change->status != GOT_STATUS_ADD)
7611 continue;
7613 if (got_path_is_root_dir(path_prefix)) {
7614 wt_path = strdup(pe->path);
7615 if (wt_path == NULL) {
7616 err = got_error_from_errno("strdup");
7617 goto done;
7619 } else {
7620 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7621 err = got_error_from_errno("asprintf");
7622 goto done;
7625 err = got_path_skip_common_ancestor(&wt_path,
7626 path_prefix, abspath);
7627 if (err)
7628 goto done;
7629 free(abspath);
7630 abspath = NULL;
7633 err = got_pathlist_append(added_paths, wt_path, NULL);
7634 if (err)
7635 goto done;
7636 wt_path = NULL;
7639 done:
7640 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7641 free(abspath);
7642 free(wt_path);
7643 return err;
7646 static const struct got_error *
7647 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7648 struct got_object_id *id, const char *path_prefix,
7649 struct got_repository *repo)
7651 const struct got_error *err;
7652 struct got_commit_object *commit = NULL;
7653 struct got_object_qid *pid;
7655 err = got_object_open_as_commit(&commit, repo, id);
7656 if (err)
7657 goto done;
7659 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7661 err = get_paths_added_between_commits(added_paths,
7662 pid ? &pid->id : NULL, id, path_prefix, repo);
7663 if (err)
7664 goto done;
7665 done:
7666 if (commit)
7667 got_object_commit_close(commit);
7668 return err;
7671 const struct got_error *
7672 got_worktree_rebase_abort(struct got_worktree *worktree,
7673 struct got_fileindex *fileindex, struct got_repository *repo,
7674 struct got_reference *new_base_branch,
7675 got_worktree_checkout_cb progress_cb, void *progress_arg)
7677 const struct got_error *err, *unlockerr, *sync_err;
7678 struct got_reference *resolved = NULL;
7679 struct got_object_id *commit_id = NULL;
7680 struct got_object_id *merged_commit_id = NULL;
7681 struct got_commit_object *commit = NULL;
7682 char *fileindex_path = NULL;
7683 char *commit_ref_name = NULL;
7684 struct got_reference *commit_ref = NULL;
7685 struct revert_file_args rfa;
7686 struct got_object_id *tree_id = NULL;
7687 struct got_pathlist_head added_paths;
7689 TAILQ_INIT(&added_paths);
7691 err = lock_worktree(worktree, LOCK_EX);
7692 if (err)
7693 return err;
7695 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7696 if (err)
7697 goto done;
7699 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7700 if (err)
7701 goto done;
7703 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7704 if (err)
7705 goto done;
7708 * Determine which files in added status can be safely removed
7709 * from disk while reverting changes in the work tree.
7710 * We want to avoid deleting unrelated files which were added by
7711 * the user for conflict resolution purposes.
7713 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7714 got_worktree_get_path_prefix(worktree), repo);
7715 if (err)
7716 goto done;
7718 err = got_ref_open(&resolved, repo,
7719 got_ref_get_symref_target(new_base_branch), 0);
7720 if (err)
7721 goto done;
7723 err = got_worktree_set_head_ref(worktree, resolved);
7724 if (err)
7725 goto done;
7728 * XXX commits to the base branch could have happened while
7729 * we were busy rebasing; should we store the original commit ID
7730 * when rebase begins and read it back here?
7732 err = got_ref_resolve(&commit_id, repo, resolved);
7733 if (err)
7734 goto done;
7736 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7737 if (err)
7738 goto done;
7740 err = got_object_open_as_commit(&commit, repo,
7741 worktree->base_commit_id);
7742 if (err)
7743 goto done;
7745 err = got_object_id_by_path(&tree_id, repo, commit,
7746 worktree->path_prefix);
7747 if (err)
7748 goto done;
7750 err = delete_rebase_refs(worktree, repo);
7751 if (err)
7752 goto done;
7754 err = get_fileindex_path(&fileindex_path, worktree);
7755 if (err)
7756 goto done;
7758 rfa.worktree = worktree;
7759 rfa.fileindex = fileindex;
7760 rfa.progress_cb = progress_cb;
7761 rfa.progress_arg = progress_arg;
7762 rfa.patch_cb = NULL;
7763 rfa.patch_arg = NULL;
7764 rfa.repo = repo;
7765 rfa.unlink_added_files = 1;
7766 rfa.added_files_to_unlink = &added_paths;
7767 err = worktree_status(worktree, "", fileindex, repo,
7768 revert_file, &rfa, NULL, NULL, 1, 0);
7769 if (err)
7770 goto sync;
7772 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7773 repo, progress_cb, progress_arg, NULL, NULL);
7774 sync:
7775 sync_err = sync_fileindex(fileindex, fileindex_path);
7776 if (sync_err && err == NULL)
7777 err = sync_err;
7778 done:
7779 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7780 got_ref_close(resolved);
7781 free(tree_id);
7782 free(commit_id);
7783 free(merged_commit_id);
7784 if (commit)
7785 got_object_commit_close(commit);
7786 if (fileindex)
7787 got_fileindex_free(fileindex);
7788 free(fileindex_path);
7789 free(commit_ref_name);
7790 if (commit_ref)
7791 got_ref_close(commit_ref);
7793 unlockerr = lock_worktree(worktree, LOCK_SH);
7794 if (unlockerr && err == NULL)
7795 err = unlockerr;
7796 return err;
7799 const struct got_error *
7800 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7801 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7802 struct got_fileindex **fileindex, struct got_worktree *worktree,
7803 struct got_repository *repo)
7805 const struct got_error *err = NULL;
7806 char *tmp_branch_name = NULL;
7807 char *branch_ref_name = NULL;
7808 char *base_commit_ref_name = NULL;
7809 char *fileindex_path = NULL;
7810 struct check_rebase_ok_arg ok_arg;
7811 struct got_reference *wt_branch = NULL;
7812 struct got_reference *base_commit_ref = NULL;
7814 *tmp_branch = NULL;
7815 *branch_ref = NULL;
7816 *base_commit_id = NULL;
7817 *fileindex = NULL;
7819 err = lock_worktree(worktree, LOCK_EX);
7820 if (err)
7821 return err;
7823 err = open_fileindex(fileindex, &fileindex_path, worktree);
7824 if (err)
7825 goto done;
7827 ok_arg.worktree = worktree;
7828 ok_arg.repo = repo;
7829 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7830 &ok_arg);
7831 if (err)
7832 goto done;
7834 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7835 if (err)
7836 goto done;
7838 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7839 if (err)
7840 goto done;
7842 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7843 worktree);
7844 if (err)
7845 goto done;
7847 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7848 0);
7849 if (err)
7850 goto done;
7852 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7853 if (err)
7854 goto done;
7856 err = got_ref_write(*branch_ref, repo);
7857 if (err)
7858 goto done;
7860 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7861 worktree->base_commit_id);
7862 if (err)
7863 goto done;
7864 err = got_ref_write(base_commit_ref, repo);
7865 if (err)
7866 goto done;
7867 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7868 if (*base_commit_id == NULL) {
7869 err = got_error_from_errno("got_object_id_dup");
7870 goto done;
7873 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7874 worktree->base_commit_id);
7875 if (err)
7876 goto done;
7877 err = got_ref_write(*tmp_branch, repo);
7878 if (err)
7879 goto done;
7881 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7882 if (err)
7883 goto done;
7884 done:
7885 free(fileindex_path);
7886 free(tmp_branch_name);
7887 free(branch_ref_name);
7888 free(base_commit_ref_name);
7889 if (wt_branch)
7890 got_ref_close(wt_branch);
7891 if (err) {
7892 if (*branch_ref) {
7893 got_ref_close(*branch_ref);
7894 *branch_ref = NULL;
7896 if (*tmp_branch) {
7897 got_ref_close(*tmp_branch);
7898 *tmp_branch = NULL;
7900 free(*base_commit_id);
7901 if (*fileindex) {
7902 got_fileindex_free(*fileindex);
7903 *fileindex = NULL;
7905 lock_worktree(worktree, LOCK_SH);
7907 return err;
7910 const struct got_error *
7911 got_worktree_histedit_postpone(struct got_worktree *worktree,
7912 struct got_fileindex *fileindex)
7914 if (fileindex)
7915 got_fileindex_free(fileindex);
7916 return lock_worktree(worktree, LOCK_SH);
7919 const struct got_error *
7920 got_worktree_histedit_in_progress(int *in_progress,
7921 struct got_worktree *worktree)
7923 const struct got_error *err;
7924 char *tmp_branch_name = NULL;
7926 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7927 if (err)
7928 return err;
7930 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7931 free(tmp_branch_name);
7932 return NULL;
7935 const struct got_error *
7936 got_worktree_histedit_continue(struct got_object_id **commit_id,
7937 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7938 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7939 struct got_worktree *worktree, struct got_repository *repo)
7941 const struct got_error *err;
7942 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7943 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7944 struct got_reference *commit_ref = NULL;
7945 struct got_reference *base_commit_ref = NULL;
7946 char *fileindex_path = NULL;
7947 int have_staged_files = 0;
7949 *commit_id = NULL;
7950 *tmp_branch = NULL;
7951 *branch_ref = NULL;
7952 *base_commit_id = NULL;
7953 *fileindex = NULL;
7955 err = lock_worktree(worktree, LOCK_EX);
7956 if (err)
7957 return err;
7959 err = open_fileindex(fileindex, &fileindex_path, worktree);
7960 if (err)
7961 goto done;
7963 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7964 &have_staged_files);
7965 if (err && err->code != GOT_ERR_CANCELLED)
7966 goto done;
7967 if (have_staged_files) {
7968 err = got_error(GOT_ERR_STAGED_PATHS);
7969 goto done;
7972 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7973 if (err)
7974 goto done;
7976 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7977 if (err)
7978 goto done;
7980 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7981 if (err)
7982 goto done;
7984 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7985 worktree);
7986 if (err)
7987 goto done;
7989 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7990 if (err)
7991 goto done;
7993 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7994 if (err)
7995 goto done;
7996 err = got_ref_resolve(commit_id, repo, commit_ref);
7997 if (err)
7998 goto done;
8000 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
8001 if (err)
8002 goto done;
8003 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
8004 if (err)
8005 goto done;
8007 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
8008 if (err)
8009 goto done;
8010 done:
8011 free(commit_ref_name);
8012 free(branch_ref_name);
8013 free(fileindex_path);
8014 if (commit_ref)
8015 got_ref_close(commit_ref);
8016 if (base_commit_ref)
8017 got_ref_close(base_commit_ref);
8018 if (err) {
8019 free(*commit_id);
8020 *commit_id = NULL;
8021 free(*base_commit_id);
8022 *base_commit_id = NULL;
8023 if (*tmp_branch) {
8024 got_ref_close(*tmp_branch);
8025 *tmp_branch = NULL;
8027 if (*branch_ref) {
8028 got_ref_close(*branch_ref);
8029 *branch_ref = NULL;
8031 if (*fileindex) {
8032 got_fileindex_free(*fileindex);
8033 *fileindex = NULL;
8035 lock_worktree(worktree, LOCK_EX);
8037 return err;
8040 static const struct got_error *
8041 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8043 const struct got_error *err;
8044 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8045 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8047 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8048 if (err)
8049 goto done;
8050 err = delete_ref(tmp_branch_name, repo);
8051 if (err)
8052 goto done;
8054 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8055 worktree);
8056 if (err)
8057 goto done;
8058 err = delete_ref(base_commit_ref_name, repo);
8059 if (err)
8060 goto done;
8062 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8063 if (err)
8064 goto done;
8065 err = delete_ref(branch_ref_name, repo);
8066 if (err)
8067 goto done;
8069 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8070 if (err)
8071 goto done;
8072 err = delete_ref(commit_ref_name, repo);
8073 if (err)
8074 goto done;
8075 done:
8076 free(tmp_branch_name);
8077 free(base_commit_ref_name);
8078 free(branch_ref_name);
8079 free(commit_ref_name);
8080 return err;
8083 const struct got_error *
8084 got_worktree_histedit_abort(struct got_worktree *worktree,
8085 struct got_fileindex *fileindex, struct got_repository *repo,
8086 struct got_reference *branch, struct got_object_id *base_commit_id,
8087 got_worktree_checkout_cb progress_cb, void *progress_arg)
8089 const struct got_error *err, *unlockerr, *sync_err;
8090 struct got_reference *resolved = NULL;
8091 char *fileindex_path = NULL;
8092 struct got_object_id *merged_commit_id = NULL;
8093 struct got_commit_object *commit = NULL;
8094 char *commit_ref_name = NULL;
8095 struct got_reference *commit_ref = NULL;
8096 struct got_object_id *tree_id = NULL;
8097 struct revert_file_args rfa;
8098 struct got_pathlist_head added_paths;
8100 TAILQ_INIT(&added_paths);
8102 err = lock_worktree(worktree, LOCK_EX);
8103 if (err)
8104 return err;
8106 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8107 if (err)
8108 goto done;
8110 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8111 if (err) {
8112 if (err->code != GOT_ERR_NOT_REF)
8113 goto done;
8114 /* Can happen on early abort due to invalid histedit script. */
8115 commit_ref = NULL;
8118 if (commit_ref) {
8119 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8120 if (err)
8121 goto done;
8124 * Determine which files in added status can be safely removed
8125 * from disk while reverting changes in the work tree.
8126 * We want to avoid deleting unrelated files added by the
8127 * user during conflict resolution or during histedit -e.
8129 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8130 got_worktree_get_path_prefix(worktree), repo);
8131 if (err)
8132 goto done;
8135 err = got_ref_open(&resolved, repo,
8136 got_ref_get_symref_target(branch), 0);
8137 if (err)
8138 goto done;
8140 err = got_worktree_set_head_ref(worktree, resolved);
8141 if (err)
8142 goto done;
8144 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8145 if (err)
8146 goto done;
8148 err = got_object_open_as_commit(&commit, repo,
8149 worktree->base_commit_id);
8150 if (err)
8151 goto done;
8153 err = got_object_id_by_path(&tree_id, repo, commit,
8154 worktree->path_prefix);
8155 if (err)
8156 goto done;
8158 err = delete_histedit_refs(worktree, repo);
8159 if (err)
8160 goto done;
8162 err = get_fileindex_path(&fileindex_path, worktree);
8163 if (err)
8164 goto done;
8166 rfa.worktree = worktree;
8167 rfa.fileindex = fileindex;
8168 rfa.progress_cb = progress_cb;
8169 rfa.progress_arg = progress_arg;
8170 rfa.patch_cb = NULL;
8171 rfa.patch_arg = NULL;
8172 rfa.repo = repo;
8173 rfa.unlink_added_files = 1;
8174 rfa.added_files_to_unlink = &added_paths;
8175 err = worktree_status(worktree, "", fileindex, repo,
8176 revert_file, &rfa, NULL, NULL, 1, 0);
8177 if (err)
8178 goto sync;
8180 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8181 repo, progress_cb, progress_arg, NULL, NULL);
8182 sync:
8183 sync_err = sync_fileindex(fileindex, fileindex_path);
8184 if (sync_err && err == NULL)
8185 err = sync_err;
8186 done:
8187 if (resolved)
8188 got_ref_close(resolved);
8189 if (commit_ref)
8190 got_ref_close(commit_ref);
8191 free(merged_commit_id);
8192 free(tree_id);
8193 free(fileindex_path);
8194 free(commit_ref_name);
8196 unlockerr = lock_worktree(worktree, LOCK_SH);
8197 if (unlockerr && err == NULL)
8198 err = unlockerr;
8199 return err;
8202 const struct got_error *
8203 got_worktree_histedit_complete(struct got_worktree *worktree,
8204 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8205 struct got_reference *edited_branch, struct got_repository *repo)
8207 const struct got_error *err, *unlockerr, *sync_err;
8208 struct got_object_id *new_head_commit_id = NULL;
8209 struct got_reference *resolved = NULL;
8210 char *fileindex_path = NULL;
8212 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8213 if (err)
8214 return err;
8216 err = got_ref_open(&resolved, repo,
8217 got_ref_get_symref_target(edited_branch), 0);
8218 if (err)
8219 goto done;
8221 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8222 resolved, new_head_commit_id, repo);
8223 if (err)
8224 goto done;
8226 err = got_ref_change_ref(resolved, new_head_commit_id);
8227 if (err)
8228 goto done;
8230 err = got_ref_write(resolved, repo);
8231 if (err)
8232 goto done;
8234 err = got_worktree_set_head_ref(worktree, resolved);
8235 if (err)
8236 goto done;
8238 err = delete_histedit_refs(worktree, repo);
8239 if (err)
8240 goto done;
8242 err = get_fileindex_path(&fileindex_path, worktree);
8243 if (err)
8244 goto done;
8245 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8246 sync_err = sync_fileindex(fileindex, fileindex_path);
8247 if (sync_err && err == NULL)
8248 err = sync_err;
8249 done:
8250 got_fileindex_free(fileindex);
8251 free(fileindex_path);
8252 free(new_head_commit_id);
8253 unlockerr = lock_worktree(worktree, LOCK_SH);
8254 if (unlockerr && err == NULL)
8255 err = unlockerr;
8256 return err;
8259 const struct got_error *
8260 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8261 struct got_object_id *commit_id, struct got_repository *repo)
8263 const struct got_error *err;
8264 char *commit_ref_name;
8266 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8267 if (err)
8268 return err;
8270 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8271 if (err)
8272 goto done;
8274 err = delete_ref(commit_ref_name, repo);
8275 done:
8276 free(commit_ref_name);
8277 return err;
8280 const struct got_error *
8281 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8282 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8283 struct got_worktree *worktree, const char *refname,
8284 struct got_repository *repo)
8286 const struct got_error *err = NULL;
8287 char *fileindex_path = NULL;
8288 struct check_rebase_ok_arg ok_arg;
8290 *fileindex = NULL;
8291 *branch_ref = NULL;
8292 *base_branch_ref = NULL;
8294 err = lock_worktree(worktree, LOCK_EX);
8295 if (err)
8296 return err;
8298 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8299 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8300 "cannot integrate a branch into itself; "
8301 "update -b or different branch name required");
8302 goto done;
8305 err = open_fileindex(fileindex, &fileindex_path, worktree);
8306 if (err)
8307 goto done;
8309 /* Preconditions are the same as for rebase. */
8310 ok_arg.worktree = worktree;
8311 ok_arg.repo = repo;
8312 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8313 &ok_arg);
8314 if (err)
8315 goto done;
8317 err = got_ref_open(branch_ref, repo, refname, 1);
8318 if (err)
8319 goto done;
8321 err = got_ref_open(base_branch_ref, repo,
8322 got_worktree_get_head_ref_name(worktree), 1);
8323 done:
8324 if (err) {
8325 if (*branch_ref) {
8326 got_ref_close(*branch_ref);
8327 *branch_ref = NULL;
8329 if (*base_branch_ref) {
8330 got_ref_close(*base_branch_ref);
8331 *base_branch_ref = NULL;
8333 if (*fileindex) {
8334 got_fileindex_free(*fileindex);
8335 *fileindex = NULL;
8337 lock_worktree(worktree, LOCK_SH);
8339 return err;
8342 const struct got_error *
8343 got_worktree_integrate_continue(struct got_worktree *worktree,
8344 struct got_fileindex *fileindex, struct got_repository *repo,
8345 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8346 got_worktree_checkout_cb progress_cb, void *progress_arg,
8347 got_cancel_cb cancel_cb, void *cancel_arg)
8349 const struct got_error *err = NULL, *sync_err, *unlockerr;
8350 char *fileindex_path = NULL;
8351 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8352 struct got_commit_object *commit = NULL;
8354 err = get_fileindex_path(&fileindex_path, worktree);
8355 if (err)
8356 goto done;
8358 err = got_ref_resolve(&commit_id, repo, branch_ref);
8359 if (err)
8360 goto done;
8362 err = got_object_open_as_commit(&commit, repo, commit_id);
8363 if (err)
8364 goto done;
8366 err = got_object_id_by_path(&tree_id, repo, commit,
8367 worktree->path_prefix);
8368 if (err)
8369 goto done;
8371 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8372 if (err)
8373 goto done;
8375 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8376 progress_cb, progress_arg, cancel_cb, cancel_arg);
8377 if (err)
8378 goto sync;
8380 err = got_ref_change_ref(base_branch_ref, commit_id);
8381 if (err)
8382 goto sync;
8384 err = got_ref_write(base_branch_ref, repo);
8385 if (err)
8386 goto sync;
8388 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8389 sync:
8390 sync_err = sync_fileindex(fileindex, fileindex_path);
8391 if (sync_err && err == NULL)
8392 err = sync_err;
8394 done:
8395 unlockerr = got_ref_unlock(branch_ref);
8396 if (unlockerr && err == NULL)
8397 err = unlockerr;
8398 got_ref_close(branch_ref);
8400 unlockerr = got_ref_unlock(base_branch_ref);
8401 if (unlockerr && err == NULL)
8402 err = unlockerr;
8403 got_ref_close(base_branch_ref);
8405 got_fileindex_free(fileindex);
8406 free(fileindex_path);
8407 free(tree_id);
8408 if (commit)
8409 got_object_commit_close(commit);
8411 unlockerr = lock_worktree(worktree, LOCK_SH);
8412 if (unlockerr && err == NULL)
8413 err = unlockerr;
8414 return err;
8417 const struct got_error *
8418 got_worktree_integrate_abort(struct got_worktree *worktree,
8419 struct got_fileindex *fileindex, struct got_repository *repo,
8420 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8422 const struct got_error *err = NULL, *unlockerr = NULL;
8424 got_fileindex_free(fileindex);
8426 err = lock_worktree(worktree, LOCK_SH);
8428 unlockerr = got_ref_unlock(branch_ref);
8429 if (unlockerr && err == NULL)
8430 err = unlockerr;
8431 got_ref_close(branch_ref);
8433 unlockerr = got_ref_unlock(base_branch_ref);
8434 if (unlockerr && err == NULL)
8435 err = unlockerr;
8436 got_ref_close(base_branch_ref);
8438 return err;
8441 const struct got_error *
8442 got_worktree_merge_postpone(struct got_worktree *worktree,
8443 struct got_fileindex *fileindex)
8445 const struct got_error *err, *sync_err;
8446 char *fileindex_path = NULL;
8448 err = get_fileindex_path(&fileindex_path, worktree);
8449 if (err)
8450 goto done;
8452 sync_err = sync_fileindex(fileindex, fileindex_path);
8454 err = lock_worktree(worktree, LOCK_SH);
8455 if (sync_err && err == NULL)
8456 err = sync_err;
8457 done:
8458 got_fileindex_free(fileindex);
8459 free(fileindex_path);
8460 return err;
8463 static const struct got_error *
8464 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8466 const struct got_error *err;
8467 char *branch_refname = NULL, *commit_refname = NULL;
8469 err = get_merge_branch_ref_name(&branch_refname, worktree);
8470 if (err)
8471 goto done;
8472 err = delete_ref(branch_refname, repo);
8473 if (err)
8474 goto done;
8476 err = get_merge_commit_ref_name(&commit_refname, worktree);
8477 if (err)
8478 goto done;
8479 err = delete_ref(commit_refname, repo);
8480 if (err)
8481 goto done;
8483 done:
8484 free(branch_refname);
8485 free(commit_refname);
8486 return err;
8489 struct merge_commit_msg_arg {
8490 struct got_worktree *worktree;
8491 const char *branch_name;
8494 static const struct got_error *
8495 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8496 const char *diff_path, char **logmsg, void *arg)
8498 struct merge_commit_msg_arg *a = arg;
8500 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8501 got_worktree_get_head_ref_name(a->worktree)) == -1)
8502 return got_error_from_errno("asprintf");
8504 return NULL;
8508 const struct got_error *
8509 got_worktree_merge_branch(struct got_worktree *worktree,
8510 struct got_fileindex *fileindex,
8511 struct got_object_id *yca_commit_id,
8512 struct got_object_id *branch_tip,
8513 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8514 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8516 const struct got_error *err;
8517 char *fileindex_path = NULL;
8518 struct check_mixed_commits_args cma;
8520 err = get_fileindex_path(&fileindex_path, worktree);
8521 if (err)
8522 goto done;
8524 cma.worktree = worktree;
8525 cma.cancel_cb = cancel_cb;
8526 cma.cancel_arg = cancel_arg;
8528 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8529 &cma);
8530 if (err)
8531 goto done;
8533 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8534 branch_tip, repo, progress_cb, progress_arg,
8535 cancel_cb, cancel_arg);
8536 done:
8537 free(fileindex_path);
8538 return err;
8541 const struct got_error *
8542 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8543 struct got_worktree *worktree, struct got_fileindex *fileindex,
8544 const char *author, const char *committer, int allow_bad_symlinks,
8545 struct got_object_id *branch_tip, const char *branch_name,
8546 int allow_conflict, struct got_repository *repo,
8547 got_worktree_status_cb status_cb, void *status_arg)
8550 const struct got_error *err = NULL, *sync_err;
8551 struct got_pathlist_head commitable_paths;
8552 struct collect_commitables_arg cc_arg;
8553 struct got_pathlist_entry *pe;
8554 struct got_reference *head_ref = NULL;
8555 struct got_object_id *head_commit_id = NULL;
8556 int have_staged_files = 0;
8557 struct merge_commit_msg_arg mcm_arg;
8558 char *fileindex_path = NULL;
8560 memset(&cc_arg, 0, sizeof(cc_arg));
8561 *new_commit_id = NULL;
8563 TAILQ_INIT(&commitable_paths);
8565 err = get_fileindex_path(&fileindex_path, worktree);
8566 if (err)
8567 goto done;
8569 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8570 if (err)
8571 goto done;
8573 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8574 if (err)
8575 goto done;
8577 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8578 &have_staged_files);
8579 if (err && err->code != GOT_ERR_CANCELLED)
8580 goto done;
8581 if (have_staged_files) {
8582 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8583 goto done;
8586 cc_arg.commitable_paths = &commitable_paths;
8587 cc_arg.worktree = worktree;
8588 cc_arg.fileindex = fileindex;
8589 cc_arg.repo = repo;
8590 cc_arg.have_staged_files = have_staged_files;
8591 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8592 cc_arg.commit_conflicts = allow_conflict;
8593 err = worktree_status(worktree, "", fileindex, repo,
8594 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8595 if (err)
8596 goto done;
8598 mcm_arg.worktree = worktree;
8599 mcm_arg.branch_name = branch_name;
8600 err = commit_worktree(new_commit_id, &commitable_paths,
8601 head_commit_id, branch_tip, worktree, author, committer, NULL,
8602 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8603 if (err)
8604 goto done;
8606 err = update_fileindex_after_commit(worktree, &commitable_paths,
8607 *new_commit_id, fileindex, have_staged_files);
8608 sync_err = sync_fileindex(fileindex, fileindex_path);
8609 if (sync_err && err == NULL)
8610 err = sync_err;
8611 done:
8612 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8613 struct got_commitable *ct = pe->data;
8615 free_commitable(ct);
8617 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8618 free(fileindex_path);
8619 return err;
8622 const struct got_error *
8623 got_worktree_merge_complete(struct got_worktree *worktree,
8624 struct got_fileindex *fileindex, struct got_repository *repo)
8626 const struct got_error *err, *unlockerr, *sync_err;
8627 char *fileindex_path = NULL;
8629 err = delete_merge_refs(worktree, repo);
8630 if (err)
8631 goto done;
8633 err = get_fileindex_path(&fileindex_path, worktree);
8634 if (err)
8635 goto done;
8636 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8637 sync_err = sync_fileindex(fileindex, fileindex_path);
8638 if (sync_err && err == NULL)
8639 err = sync_err;
8640 done:
8641 got_fileindex_free(fileindex);
8642 free(fileindex_path);
8643 unlockerr = lock_worktree(worktree, LOCK_SH);
8644 if (unlockerr && err == NULL)
8645 err = unlockerr;
8646 return err;
8649 const struct got_error *
8650 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8651 struct got_repository *repo)
8653 const struct got_error *err;
8654 char *branch_refname = NULL;
8655 struct got_reference *branch_ref = NULL;
8657 *in_progress = 0;
8659 err = get_merge_branch_ref_name(&branch_refname, worktree);
8660 if (err)
8661 return err;
8662 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8663 free(branch_refname);
8664 if (err) {
8665 if (err->code != GOT_ERR_NOT_REF)
8666 return err;
8667 } else
8668 *in_progress = 1;
8670 return NULL;
8673 const struct got_error *got_worktree_merge_prepare(
8674 struct got_fileindex **fileindex, struct got_worktree *worktree,
8675 struct got_repository *repo)
8677 const struct got_error *err = NULL;
8678 char *fileindex_path = NULL;
8679 struct got_reference *wt_branch = NULL;
8680 struct got_object_id *wt_branch_tip = NULL;
8681 struct check_rebase_ok_arg ok_arg;
8683 *fileindex = NULL;
8685 err = lock_worktree(worktree, LOCK_EX);
8686 if (err)
8687 return err;
8689 err = open_fileindex(fileindex, &fileindex_path, worktree);
8690 if (err)
8691 goto done;
8693 /* Preconditions are the same as for rebase. */
8694 ok_arg.worktree = worktree;
8695 ok_arg.repo = repo;
8696 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8697 &ok_arg);
8698 if (err)
8699 goto done;
8701 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8702 0);
8703 if (err)
8704 goto done;
8706 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8707 if (err)
8708 goto done;
8710 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8711 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8712 goto done;
8715 done:
8716 free(fileindex_path);
8717 if (wt_branch)
8718 got_ref_close(wt_branch);
8719 free(wt_branch_tip);
8720 if (err) {
8721 if (*fileindex) {
8722 got_fileindex_free(*fileindex);
8723 *fileindex = NULL;
8725 lock_worktree(worktree, LOCK_SH);
8727 return err;
8730 const struct got_error *got_worktree_merge_write_refs(
8731 struct got_worktree *worktree, struct got_reference *branch,
8732 struct got_repository *repo)
8734 const struct got_error *err = NULL;
8735 char *branch_refname = NULL, *commit_refname = NULL;
8736 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8737 struct got_object_id *branch_tip = NULL;
8739 err = get_merge_branch_ref_name(&branch_refname, worktree);
8740 if (err)
8741 return err;
8743 err = get_merge_commit_ref_name(&commit_refname, worktree);
8744 if (err)
8745 return err;
8747 err = got_ref_resolve(&branch_tip, repo, branch);
8748 if (err)
8749 goto done;
8751 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8752 if (err)
8753 goto done;
8754 err = got_ref_write(branch_ref, repo);
8755 if (err)
8756 goto done;
8758 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8759 if (err)
8760 goto done;
8761 err = got_ref_write(commit_ref, repo);
8762 if (err)
8763 goto done;
8765 done:
8766 free(branch_refname);
8767 free(commit_refname);
8768 if (branch_ref)
8769 got_ref_close(branch_ref);
8770 if (commit_ref)
8771 got_ref_close(commit_ref);
8772 free(branch_tip);
8773 return err;
8776 const struct got_error *
8777 got_worktree_merge_continue(char **branch_name,
8778 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8779 struct got_worktree *worktree, struct got_repository *repo)
8781 const struct got_error *err;
8782 char *commit_refname = NULL, *branch_refname = NULL;
8783 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8784 char *fileindex_path = NULL;
8785 int have_staged_files = 0;
8787 *branch_name = NULL;
8788 *branch_tip = NULL;
8789 *fileindex = NULL;
8791 err = lock_worktree(worktree, LOCK_EX);
8792 if (err)
8793 return err;
8795 err = open_fileindex(fileindex, &fileindex_path, worktree);
8796 if (err)
8797 goto done;
8799 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8800 &have_staged_files);
8801 if (err && err->code != GOT_ERR_CANCELLED)
8802 goto done;
8803 if (have_staged_files) {
8804 err = got_error(GOT_ERR_STAGED_PATHS);
8805 goto done;
8808 err = get_merge_branch_ref_name(&branch_refname, worktree);
8809 if (err)
8810 goto done;
8812 err = get_merge_commit_ref_name(&commit_refname, worktree);
8813 if (err)
8814 goto done;
8816 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8817 if (err)
8818 goto done;
8820 if (!got_ref_is_symbolic(branch_ref)) {
8821 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8822 "%s is not a symbolic reference",
8823 got_ref_get_name(branch_ref));
8824 goto done;
8826 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8827 if (*branch_name == NULL) {
8828 err = got_error_from_errno("strdup");
8829 goto done;
8832 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8833 if (err)
8834 goto done;
8836 err = got_ref_resolve(branch_tip, repo, commit_ref);
8837 if (err)
8838 goto done;
8839 done:
8840 free(commit_refname);
8841 free(branch_refname);
8842 free(fileindex_path);
8843 if (commit_ref)
8844 got_ref_close(commit_ref);
8845 if (branch_ref)
8846 got_ref_close(branch_ref);
8847 if (err) {
8848 if (*branch_name) {
8849 free(*branch_name);
8850 *branch_name = NULL;
8852 free(*branch_tip);
8853 *branch_tip = NULL;
8854 if (*fileindex) {
8855 got_fileindex_free(*fileindex);
8856 *fileindex = NULL;
8858 lock_worktree(worktree, LOCK_SH);
8860 return err;
8863 const struct got_error *
8864 got_worktree_merge_abort(struct got_worktree *worktree,
8865 struct got_fileindex *fileindex, struct got_repository *repo,
8866 got_worktree_checkout_cb progress_cb, void *progress_arg)
8868 const struct got_error *err, *unlockerr, *sync_err;
8869 struct got_commit_object *commit = NULL;
8870 char *fileindex_path = NULL;
8871 struct revert_file_args rfa;
8872 char *commit_ref_name = NULL;
8873 struct got_reference *commit_ref = NULL;
8874 struct got_object_id *merged_commit_id = NULL;
8875 struct got_object_id *tree_id = NULL;
8876 struct got_pathlist_head added_paths;
8878 TAILQ_INIT(&added_paths);
8880 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8881 if (err)
8882 goto done;
8884 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8885 if (err)
8886 goto done;
8888 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8889 if (err)
8890 goto done;
8893 * Determine which files in added status can be safely removed
8894 * from disk while reverting changes in the work tree.
8895 * We want to avoid deleting unrelated files which were added by
8896 * the user for conflict resolution purposes.
8898 err = get_paths_added_between_commits(&added_paths,
8899 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8900 got_worktree_get_path_prefix(worktree), repo);
8901 if (err)
8902 goto done;
8905 err = got_object_open_as_commit(&commit, repo,
8906 worktree->base_commit_id);
8907 if (err)
8908 goto done;
8910 err = got_object_id_by_path(&tree_id, repo, commit,
8911 worktree->path_prefix);
8912 if (err)
8913 goto done;
8915 err = delete_merge_refs(worktree, repo);
8916 if (err)
8917 goto done;
8919 err = get_fileindex_path(&fileindex_path, worktree);
8920 if (err)
8921 goto done;
8923 rfa.worktree = worktree;
8924 rfa.fileindex = fileindex;
8925 rfa.progress_cb = progress_cb;
8926 rfa.progress_arg = progress_arg;
8927 rfa.patch_cb = NULL;
8928 rfa.patch_arg = NULL;
8929 rfa.repo = repo;
8930 rfa.unlink_added_files = 1;
8931 rfa.added_files_to_unlink = &added_paths;
8932 err = worktree_status(worktree, "", fileindex, repo,
8933 revert_file, &rfa, NULL, NULL, 1, 0);
8934 if (err)
8935 goto sync;
8937 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8938 repo, progress_cb, progress_arg, NULL, NULL);
8939 sync:
8940 sync_err = sync_fileindex(fileindex, fileindex_path);
8941 if (sync_err && err == NULL)
8942 err = sync_err;
8943 done:
8944 free(tree_id);
8945 free(merged_commit_id);
8946 if (commit)
8947 got_object_commit_close(commit);
8948 if (fileindex)
8949 got_fileindex_free(fileindex);
8950 free(fileindex_path);
8951 if (commit_ref)
8952 got_ref_close(commit_ref);
8953 free(commit_ref_name);
8955 unlockerr = lock_worktree(worktree, LOCK_SH);
8956 if (unlockerr && err == NULL)
8957 err = unlockerr;
8958 return err;
8961 struct check_stage_ok_arg {
8962 struct got_object_id *head_commit_id;
8963 struct got_worktree *worktree;
8964 struct got_fileindex *fileindex;
8965 struct got_repository *repo;
8966 int have_changes;
8969 static const struct got_error *
8970 check_stage_ok(void *arg, unsigned char status,
8971 unsigned char staged_status, const char *relpath,
8972 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8973 struct got_object_id *commit_id, int dirfd, const char *de_name)
8975 struct check_stage_ok_arg *a = arg;
8976 const struct got_error *err = NULL;
8977 struct got_fileindex_entry *ie;
8978 struct got_object_id base_commit_id;
8979 struct got_object_id *base_commit_idp = NULL;
8980 char *in_repo_path = NULL, *p;
8982 if (status == GOT_STATUS_UNVERSIONED ||
8983 status == GOT_STATUS_NO_CHANGE)
8984 return NULL;
8985 if (status == GOT_STATUS_NONEXISTENT)
8986 return got_error_set_errno(ENOENT, relpath);
8988 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8989 if (ie == NULL)
8990 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8992 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8993 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8994 relpath) == -1)
8995 return got_error_from_errno("asprintf");
8997 if (got_fileindex_entry_has_commit(ie)) {
8998 base_commit_idp = got_fileindex_entry_get_commit_id(
8999 &base_commit_id, ie);
9002 if (status == GOT_STATUS_CONFLICT) {
9003 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9004 goto done;
9005 } else if (status != GOT_STATUS_ADD &&
9006 status != GOT_STATUS_MODIFY &&
9007 status != GOT_STATUS_DELETE) {
9008 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9009 goto done;
9012 a->have_changes = 1;
9014 p = in_repo_path;
9015 while (p[0] == '/')
9016 p++;
9017 err = check_out_of_date(p, status, staged_status,
9018 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9019 GOT_ERR_STAGE_OUT_OF_DATE);
9020 done:
9021 free(in_repo_path);
9022 return err;
9025 struct stage_path_arg {
9026 struct got_worktree *worktree;
9027 struct got_fileindex *fileindex;
9028 struct got_repository *repo;
9029 got_worktree_status_cb status_cb;
9030 void *status_arg;
9031 got_worktree_patch_cb patch_cb;
9032 void *patch_arg;
9033 int staged_something;
9034 int allow_bad_symlinks;
9037 static const struct got_error *
9038 stage_path(void *arg, unsigned char status,
9039 unsigned char staged_status, const char *relpath,
9040 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9041 struct got_object_id *commit_id, int dirfd, const char *de_name)
9043 struct stage_path_arg *a = arg;
9044 const struct got_error *err = NULL;
9045 struct got_fileindex_entry *ie;
9046 char *ondisk_path = NULL, *path_content = NULL;
9047 uint32_t stage;
9048 struct got_object_id *new_staged_blob_id = NULL;
9049 struct stat sb;
9051 if (status == GOT_STATUS_UNVERSIONED)
9052 return NULL;
9054 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9055 if (ie == NULL)
9056 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9058 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9059 relpath)== -1)
9060 return got_error_from_errno("asprintf");
9062 switch (status) {
9063 case GOT_STATUS_ADD:
9064 case GOT_STATUS_MODIFY:
9065 /* XXX could sb.st_mode be passed in by our caller? */
9066 if (lstat(ondisk_path, &sb) == -1) {
9067 err = got_error_from_errno2("lstat", ondisk_path);
9068 break;
9070 if (a->patch_cb) {
9071 if (status == GOT_STATUS_ADD) {
9072 int choice = GOT_PATCH_CHOICE_NONE;
9073 err = (*a->patch_cb)(&choice, a->patch_arg,
9074 status, ie->path, NULL, 1, 1);
9075 if (err)
9076 break;
9077 if (choice != GOT_PATCH_CHOICE_YES)
9078 break;
9079 } else {
9080 err = create_patched_content(&path_content, 0,
9081 staged_blob_id ? staged_blob_id : blob_id,
9082 ondisk_path, dirfd, de_name, ie->path,
9083 a->repo, a->patch_cb, a->patch_arg);
9084 if (err || path_content == NULL)
9085 break;
9088 err = got_object_blob_create(&new_staged_blob_id,
9089 path_content ? path_content : ondisk_path, a->repo);
9090 if (err)
9091 break;
9092 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9093 SHA1_DIGEST_LENGTH);
9094 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9095 stage = GOT_FILEIDX_STAGE_ADD;
9096 else
9097 stage = GOT_FILEIDX_STAGE_MODIFY;
9098 got_fileindex_entry_stage_set(ie, stage);
9099 if (S_ISLNK(sb.st_mode)) {
9100 int is_bad_symlink = 0;
9101 if (!a->allow_bad_symlinks) {
9102 char target_path[PATH_MAX];
9103 ssize_t target_len;
9104 target_len = readlink(ondisk_path, target_path,
9105 sizeof(target_path));
9106 if (target_len == -1) {
9107 err = got_error_from_errno2("readlink",
9108 ondisk_path);
9109 break;
9111 err = is_bad_symlink_target(&is_bad_symlink,
9112 target_path, target_len, ondisk_path,
9113 a->worktree->root_path,
9114 a->worktree->meta_dir);
9115 if (err)
9116 break;
9117 if (is_bad_symlink) {
9118 err = got_error_path(ondisk_path,
9119 GOT_ERR_BAD_SYMLINK);
9120 break;
9123 if (is_bad_symlink)
9124 got_fileindex_entry_staged_filetype_set(ie,
9125 GOT_FILEIDX_MODE_BAD_SYMLINK);
9126 else
9127 got_fileindex_entry_staged_filetype_set(ie,
9128 GOT_FILEIDX_MODE_SYMLINK);
9129 } else {
9130 got_fileindex_entry_staged_filetype_set(ie,
9131 GOT_FILEIDX_MODE_REGULAR_FILE);
9133 a->staged_something = 1;
9134 if (a->status_cb == NULL)
9135 break;
9136 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9137 get_staged_status(ie), relpath, blob_id,
9138 new_staged_blob_id, NULL, dirfd, de_name);
9139 if (err)
9140 break;
9142 * When staging the reverse of the staged diff,
9143 * implicitly unstage the file.
9145 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9146 sizeof(ie->blob_sha1)) == 0) {
9147 got_fileindex_entry_stage_set(ie,
9148 GOT_FILEIDX_STAGE_NONE);
9150 break;
9151 case GOT_STATUS_DELETE:
9152 if (staged_status == GOT_STATUS_DELETE)
9153 break;
9154 if (a->patch_cb) {
9155 int choice = GOT_PATCH_CHOICE_NONE;
9156 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9157 ie->path, NULL, 1, 1);
9158 if (err)
9159 break;
9160 if (choice == GOT_PATCH_CHOICE_NO)
9161 break;
9162 if (choice != GOT_PATCH_CHOICE_YES) {
9163 err = got_error(GOT_ERR_PATCH_CHOICE);
9164 break;
9167 stage = GOT_FILEIDX_STAGE_DELETE;
9168 got_fileindex_entry_stage_set(ie, stage);
9169 a->staged_something = 1;
9170 if (a->status_cb == NULL)
9171 break;
9172 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9173 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9174 de_name);
9175 break;
9176 case GOT_STATUS_NO_CHANGE:
9177 break;
9178 case GOT_STATUS_CONFLICT:
9179 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9180 break;
9181 case GOT_STATUS_NONEXISTENT:
9182 err = got_error_set_errno(ENOENT, relpath);
9183 break;
9184 default:
9185 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9186 break;
9189 if (path_content && unlink(path_content) == -1 && err == NULL)
9190 err = got_error_from_errno2("unlink", path_content);
9191 free(path_content);
9192 free(ondisk_path);
9193 free(new_staged_blob_id);
9194 return err;
9197 const struct got_error *
9198 got_worktree_stage(struct got_worktree *worktree,
9199 struct got_pathlist_head *paths,
9200 got_worktree_status_cb status_cb, void *status_arg,
9201 got_worktree_patch_cb patch_cb, void *patch_arg,
9202 int allow_bad_symlinks, struct got_repository *repo)
9204 const struct got_error *err = NULL, *sync_err, *unlockerr;
9205 struct got_pathlist_entry *pe;
9206 struct got_fileindex *fileindex = NULL;
9207 char *fileindex_path = NULL;
9208 struct got_reference *head_ref = NULL;
9209 struct got_object_id *head_commit_id = NULL;
9210 struct check_stage_ok_arg oka;
9211 struct stage_path_arg spa;
9213 err = lock_worktree(worktree, LOCK_EX);
9214 if (err)
9215 return err;
9217 err = got_ref_open(&head_ref, repo,
9218 got_worktree_get_head_ref_name(worktree), 0);
9219 if (err)
9220 goto done;
9221 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9222 if (err)
9223 goto done;
9224 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9225 if (err)
9226 goto done;
9228 /* Check pre-conditions before staging anything. */
9229 oka.head_commit_id = head_commit_id;
9230 oka.worktree = worktree;
9231 oka.fileindex = fileindex;
9232 oka.repo = repo;
9233 oka.have_changes = 0;
9234 TAILQ_FOREACH(pe, paths, entry) {
9235 err = worktree_status(worktree, pe->path, fileindex, repo,
9236 check_stage_ok, &oka, NULL, NULL, 1, 0);
9237 if (err)
9238 goto done;
9240 if (!oka.have_changes) {
9241 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9242 goto done;
9245 spa.worktree = worktree;
9246 spa.fileindex = fileindex;
9247 spa.repo = repo;
9248 spa.patch_cb = patch_cb;
9249 spa.patch_arg = patch_arg;
9250 spa.status_cb = status_cb;
9251 spa.status_arg = status_arg;
9252 spa.staged_something = 0;
9253 spa.allow_bad_symlinks = allow_bad_symlinks;
9254 TAILQ_FOREACH(pe, paths, entry) {
9255 err = worktree_status(worktree, pe->path, fileindex, repo,
9256 stage_path, &spa, NULL, NULL, 1, 0);
9257 if (err)
9258 goto done;
9260 if (!spa.staged_something) {
9261 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9262 goto done;
9265 sync_err = sync_fileindex(fileindex, fileindex_path);
9266 if (sync_err && err == NULL)
9267 err = sync_err;
9268 done:
9269 if (head_ref)
9270 got_ref_close(head_ref);
9271 free(head_commit_id);
9272 free(fileindex_path);
9273 if (fileindex)
9274 got_fileindex_free(fileindex);
9275 unlockerr = lock_worktree(worktree, LOCK_SH);
9276 if (unlockerr && err == NULL)
9277 err = unlockerr;
9278 return err;
9281 struct unstage_path_arg {
9282 struct got_worktree *worktree;
9283 struct got_fileindex *fileindex;
9284 struct got_repository *repo;
9285 got_worktree_checkout_cb progress_cb;
9286 void *progress_arg;
9287 got_worktree_patch_cb patch_cb;
9288 void *patch_arg;
9291 static const struct got_error *
9292 create_unstaged_content(char **path_unstaged_content,
9293 char **path_new_staged_content, struct got_object_id *blob_id,
9294 struct got_object_id *staged_blob_id, const char *relpath,
9295 struct got_repository *repo,
9296 got_worktree_patch_cb patch_cb, void *patch_arg)
9298 const struct got_error *err, *free_err;
9299 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9300 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9301 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9302 struct got_diffreg_result *diffreg_result = NULL;
9303 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9304 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9305 int fd1 = -1, fd2 = -1;
9307 *path_unstaged_content = NULL;
9308 *path_new_staged_content = NULL;
9310 err = got_object_id_str(&label1, blob_id);
9311 if (err)
9312 return err;
9314 fd1 = got_opentempfd();
9315 if (fd1 == -1) {
9316 err = got_error_from_errno("got_opentempfd");
9317 goto done;
9319 fd2 = got_opentempfd();
9320 if (fd2 == -1) {
9321 err = got_error_from_errno("got_opentempfd");
9322 goto done;
9325 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9326 if (err)
9327 goto done;
9329 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9330 if (err)
9331 goto done;
9333 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9334 if (err)
9335 goto done;
9337 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9338 fd2);
9339 if (err)
9340 goto done;
9342 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9343 if (err)
9344 goto done;
9346 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9347 if (err)
9348 goto done;
9350 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9351 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9352 if (err)
9353 goto done;
9355 err = got_opentemp_named(path_unstaged_content, &outfile,
9356 "got-unstaged-content", "");
9357 if (err)
9358 goto done;
9359 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9360 "got-new-staged-content", "");
9361 if (err)
9362 goto done;
9364 if (fseek(f1, 0L, SEEK_SET) == -1) {
9365 err = got_ferror(f1, GOT_ERR_IO);
9366 goto done;
9368 if (fseek(f2, 0L, SEEK_SET) == -1) {
9369 err = got_ferror(f2, GOT_ERR_IO);
9370 goto done;
9372 /* Count the number of actual changes in the diff result. */
9373 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9374 struct diff_chunk_context cc = {};
9375 diff_chunk_context_load_change(&cc, &nchunks_used,
9376 diffreg_result->result, n, 0);
9377 nchanges++;
9379 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9380 int choice;
9381 err = apply_or_reject_change(&choice, &nchunks_used,
9382 diffreg_result->result, n, relpath, f1, f2,
9383 &line_cur1, &line_cur2,
9384 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9385 if (err)
9386 goto done;
9387 if (choice == GOT_PATCH_CHOICE_YES)
9388 have_content = 1;
9389 else
9390 have_rejected_content = 1;
9391 if (choice == GOT_PATCH_CHOICE_QUIT)
9392 break;
9394 if (have_content || have_rejected_content)
9395 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9396 outfile, rejectfile);
9397 done:
9398 free(label1);
9399 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9400 err = got_error_from_errno("close");
9401 if (blob)
9402 got_object_blob_close(blob);
9403 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9404 err = got_error_from_errno("close");
9405 if (staged_blob)
9406 got_object_blob_close(staged_blob);
9407 free_err = got_diffreg_result_free(diffreg_result);
9408 if (free_err && err == NULL)
9409 err = free_err;
9410 if (f1 && fclose(f1) == EOF && err == NULL)
9411 err = got_error_from_errno2("fclose", path1);
9412 if (f2 && fclose(f2) == EOF && err == NULL)
9413 err = got_error_from_errno2("fclose", path2);
9414 if (outfile && fclose(outfile) == EOF && err == NULL)
9415 err = got_error_from_errno2("fclose", *path_unstaged_content);
9416 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9417 err = got_error_from_errno2("fclose", *path_new_staged_content);
9418 if (path1 && unlink(path1) == -1 && err == NULL)
9419 err = got_error_from_errno2("unlink", path1);
9420 if (path2 && unlink(path2) == -1 && err == NULL)
9421 err = got_error_from_errno2("unlink", path2);
9422 if (err || !have_content) {
9423 if (*path_unstaged_content &&
9424 unlink(*path_unstaged_content) == -1 && err == NULL)
9425 err = got_error_from_errno2("unlink",
9426 *path_unstaged_content);
9427 free(*path_unstaged_content);
9428 *path_unstaged_content = NULL;
9430 if (err || !have_content || !have_rejected_content) {
9431 if (*path_new_staged_content &&
9432 unlink(*path_new_staged_content) == -1 && err == NULL)
9433 err = got_error_from_errno2("unlink",
9434 *path_new_staged_content);
9435 free(*path_new_staged_content);
9436 *path_new_staged_content = NULL;
9438 free(path1);
9439 free(path2);
9440 return err;
9443 static const struct got_error *
9444 unstage_hunks(struct got_object_id *staged_blob_id,
9445 struct got_blob_object *blob_base,
9446 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9447 const char *ondisk_path, const char *label_orig,
9448 struct got_worktree *worktree, struct got_repository *repo,
9449 got_worktree_patch_cb patch_cb, void *patch_arg,
9450 got_worktree_checkout_cb progress_cb, void *progress_arg)
9452 const struct got_error *err = NULL;
9453 char *path_unstaged_content = NULL;
9454 char *path_new_staged_content = NULL;
9455 char *parent = NULL, *base_path = NULL;
9456 char *blob_base_path = NULL;
9457 struct got_object_id *new_staged_blob_id = NULL;
9458 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9459 struct stat sb;
9461 err = create_unstaged_content(&path_unstaged_content,
9462 &path_new_staged_content, blob_id, staged_blob_id,
9463 ie->path, repo, patch_cb, patch_arg);
9464 if (err)
9465 return err;
9467 if (path_unstaged_content == NULL)
9468 return NULL;
9470 if (path_new_staged_content) {
9471 err = got_object_blob_create(&new_staged_blob_id,
9472 path_new_staged_content, repo);
9473 if (err)
9474 goto done;
9477 f = fopen(path_unstaged_content, "re");
9478 if (f == NULL) {
9479 err = got_error_from_errno2("fopen",
9480 path_unstaged_content);
9481 goto done;
9483 if (fstat(fileno(f), &sb) == -1) {
9484 err = got_error_from_errno2("fstat", path_unstaged_content);
9485 goto done;
9487 if (got_fileindex_entry_staged_filetype_get(ie) ==
9488 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9489 char link_target[PATH_MAX];
9490 size_t r;
9491 r = fread(link_target, 1, sizeof(link_target), f);
9492 if (r == 0 && ferror(f)) {
9493 err = got_error_from_errno("fread");
9494 goto done;
9496 if (r >= sizeof(link_target)) { /* should not happen */
9497 err = got_error(GOT_ERR_NO_SPACE);
9498 goto done;
9500 link_target[r] = '\0';
9501 err = merge_symlink(worktree, blob_base,
9502 ondisk_path, ie->path, label_orig, link_target,
9503 worktree->base_commit_id, repo, progress_cb,
9504 progress_arg);
9505 } else {
9506 int local_changes_subsumed;
9508 err = got_path_dirname(&parent, ondisk_path);
9509 if (err)
9510 return err;
9512 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9513 parent) == -1) {
9514 err = got_error_from_errno("asprintf");
9515 base_path = NULL;
9516 goto done;
9519 err = got_opentemp_named(&blob_base_path, &f_base,
9520 base_path, "");
9521 if (err)
9522 goto done;
9523 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9524 blob_base);
9525 if (err)
9526 goto done;
9529 * In order the run a 3-way merge with a symlink we copy the symlink's
9530 * target path into a temporary file and use that file with diff3.
9532 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9533 err = dump_symlink_target_path_to_file(&f_deriv2,
9534 ondisk_path);
9535 if (err)
9536 goto done;
9537 } else {
9538 int fd;
9539 fd = open(ondisk_path,
9540 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9541 if (fd == -1) {
9542 err = got_error_from_errno2("open", ondisk_path);
9543 goto done;
9545 f_deriv2 = fdopen(fd, "r");
9546 if (f_deriv2 == NULL) {
9547 err = got_error_from_errno2("fdopen", ondisk_path);
9548 close(fd);
9549 goto done;
9553 err = merge_file(&local_changes_subsumed, worktree,
9554 f_base, f, f_deriv2, ondisk_path, ie->path,
9555 got_fileindex_perms_to_st(ie),
9556 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9557 repo, progress_cb, progress_arg);
9559 if (err)
9560 goto done;
9562 if (new_staged_blob_id) {
9563 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9564 SHA1_DIGEST_LENGTH);
9565 } else {
9566 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9567 got_fileindex_entry_staged_filetype_set(ie, 0);
9569 done:
9570 free(new_staged_blob_id);
9571 if (path_unstaged_content &&
9572 unlink(path_unstaged_content) == -1 && err == NULL)
9573 err = got_error_from_errno2("unlink", path_unstaged_content);
9574 if (path_new_staged_content &&
9575 unlink(path_new_staged_content) == -1 && err == NULL)
9576 err = got_error_from_errno2("unlink", path_new_staged_content);
9577 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9578 err = got_error_from_errno2("unlink", blob_base_path);
9579 if (f_base && fclose(f_base) == EOF && err == NULL)
9580 err = got_error_from_errno2("fclose", path_unstaged_content);
9581 if (f && fclose(f) == EOF && err == NULL)
9582 err = got_error_from_errno2("fclose", path_unstaged_content);
9583 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9584 err = got_error_from_errno2("fclose", ondisk_path);
9585 free(path_unstaged_content);
9586 free(path_new_staged_content);
9587 free(blob_base_path);
9588 free(parent);
9589 free(base_path);
9590 return err;
9593 static const struct got_error *
9594 unstage_path(void *arg, unsigned char status,
9595 unsigned char staged_status, const char *relpath,
9596 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9597 struct got_object_id *commit_id, int dirfd, const char *de_name)
9599 const struct got_error *err = NULL;
9600 struct unstage_path_arg *a = arg;
9601 struct got_fileindex_entry *ie;
9602 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9603 char *ondisk_path = NULL;
9604 char *id_str = NULL, *label_orig = NULL;
9605 int local_changes_subsumed;
9606 struct stat sb;
9607 int fd1 = -1, fd2 = -1;
9609 if (staged_status != GOT_STATUS_ADD &&
9610 staged_status != GOT_STATUS_MODIFY &&
9611 staged_status != GOT_STATUS_DELETE)
9612 return NULL;
9614 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9615 if (ie == NULL)
9616 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9618 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9619 == -1)
9620 return got_error_from_errno("asprintf");
9622 err = got_object_id_str(&id_str,
9623 commit_id ? commit_id : a->worktree->base_commit_id);
9624 if (err)
9625 goto done;
9626 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9627 id_str) == -1) {
9628 err = got_error_from_errno("asprintf");
9629 goto done;
9632 fd1 = got_opentempfd();
9633 if (fd1 == -1) {
9634 err = got_error_from_errno("got_opentempfd");
9635 goto done;
9637 fd2 = got_opentempfd();
9638 if (fd2 == -1) {
9639 err = got_error_from_errno("got_opentempfd");
9640 goto done;
9643 switch (staged_status) {
9644 case GOT_STATUS_MODIFY:
9645 err = got_object_open_as_blob(&blob_base, a->repo,
9646 blob_id, 8192, fd1);
9647 if (err)
9648 break;
9649 /* fall through */
9650 case GOT_STATUS_ADD:
9651 if (a->patch_cb) {
9652 if (staged_status == GOT_STATUS_ADD) {
9653 int choice = GOT_PATCH_CHOICE_NONE;
9654 err = (*a->patch_cb)(&choice, a->patch_arg,
9655 staged_status, ie->path, NULL, 1, 1);
9656 if (err)
9657 break;
9658 if (choice != GOT_PATCH_CHOICE_YES)
9659 break;
9660 } else {
9661 err = unstage_hunks(staged_blob_id,
9662 blob_base, blob_id, ie, ondisk_path,
9663 label_orig, a->worktree, a->repo,
9664 a->patch_cb, a->patch_arg,
9665 a->progress_cb, a->progress_arg);
9666 break; /* Done with this file. */
9669 err = got_object_open_as_blob(&blob_staged, a->repo,
9670 staged_blob_id, 8192, fd2);
9671 if (err)
9672 break;
9673 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9674 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9675 case GOT_FILEIDX_MODE_REGULAR_FILE:
9676 err = merge_blob(&local_changes_subsumed, a->worktree,
9677 blob_base, ondisk_path, relpath,
9678 got_fileindex_perms_to_st(ie), label_orig,
9679 blob_staged, commit_id ? commit_id :
9680 a->worktree->base_commit_id, a->repo,
9681 a->progress_cb, a->progress_arg);
9682 break;
9683 case GOT_FILEIDX_MODE_SYMLINK:
9684 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9685 char *staged_target;
9686 err = got_object_blob_read_to_str(
9687 &staged_target, blob_staged);
9688 if (err)
9689 goto done;
9690 err = merge_symlink(a->worktree, blob_base,
9691 ondisk_path, relpath, label_orig,
9692 staged_target, commit_id ? commit_id :
9693 a->worktree->base_commit_id,
9694 a->repo, a->progress_cb, a->progress_arg);
9695 free(staged_target);
9696 } else {
9697 err = merge_blob(&local_changes_subsumed,
9698 a->worktree, blob_base, ondisk_path,
9699 relpath, got_fileindex_perms_to_st(ie),
9700 label_orig, blob_staged,
9701 commit_id ? commit_id :
9702 a->worktree->base_commit_id, a->repo,
9703 a->progress_cb, a->progress_arg);
9705 break;
9706 default:
9707 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9708 break;
9710 if (err == NULL) {
9711 got_fileindex_entry_stage_set(ie,
9712 GOT_FILEIDX_STAGE_NONE);
9713 got_fileindex_entry_staged_filetype_set(ie, 0);
9715 break;
9716 case GOT_STATUS_DELETE:
9717 if (a->patch_cb) {
9718 int choice = GOT_PATCH_CHOICE_NONE;
9719 err = (*a->patch_cb)(&choice, a->patch_arg,
9720 staged_status, ie->path, NULL, 1, 1);
9721 if (err)
9722 break;
9723 if (choice == GOT_PATCH_CHOICE_NO)
9724 break;
9725 if (choice != GOT_PATCH_CHOICE_YES) {
9726 err = got_error(GOT_ERR_PATCH_CHOICE);
9727 break;
9730 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9731 got_fileindex_entry_staged_filetype_set(ie, 0);
9732 err = get_file_status(&status, &sb, ie, ondisk_path,
9733 dirfd, de_name, a->repo);
9734 if (err)
9735 break;
9736 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9737 break;
9739 done:
9740 free(ondisk_path);
9741 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9742 err = got_error_from_errno("close");
9743 if (blob_base)
9744 got_object_blob_close(blob_base);
9745 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9746 err = got_error_from_errno("close");
9747 if (blob_staged)
9748 got_object_blob_close(blob_staged);
9749 free(id_str);
9750 free(label_orig);
9751 return err;
9754 const struct got_error *
9755 got_worktree_unstage(struct got_worktree *worktree,
9756 struct got_pathlist_head *paths,
9757 got_worktree_checkout_cb progress_cb, void *progress_arg,
9758 got_worktree_patch_cb patch_cb, void *patch_arg,
9759 struct got_repository *repo)
9761 const struct got_error *err = NULL, *sync_err, *unlockerr;
9762 struct got_pathlist_entry *pe;
9763 struct got_fileindex *fileindex = NULL;
9764 char *fileindex_path = NULL;
9765 struct unstage_path_arg upa;
9767 err = lock_worktree(worktree, LOCK_EX);
9768 if (err)
9769 return err;
9771 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9772 if (err)
9773 goto done;
9775 upa.worktree = worktree;
9776 upa.fileindex = fileindex;
9777 upa.repo = repo;
9778 upa.progress_cb = progress_cb;
9779 upa.progress_arg = progress_arg;
9780 upa.patch_cb = patch_cb;
9781 upa.patch_arg = patch_arg;
9782 TAILQ_FOREACH(pe, paths, entry) {
9783 err = worktree_status(worktree, pe->path, fileindex, repo,
9784 unstage_path, &upa, NULL, NULL, 1, 0);
9785 if (err)
9786 goto done;
9789 sync_err = sync_fileindex(fileindex, fileindex_path);
9790 if (sync_err && err == NULL)
9791 err = sync_err;
9792 done:
9793 free(fileindex_path);
9794 if (fileindex)
9795 got_fileindex_free(fileindex);
9796 unlockerr = lock_worktree(worktree, LOCK_SH);
9797 if (unlockerr && err == NULL)
9798 err = unlockerr;
9799 return err;
9802 struct report_file_info_arg {
9803 struct got_worktree *worktree;
9804 got_worktree_path_info_cb info_cb;
9805 void *info_arg;
9806 struct got_pathlist_head *paths;
9807 got_cancel_cb cancel_cb;
9808 void *cancel_arg;
9811 static const struct got_error *
9812 report_file_info(void *arg, struct got_fileindex_entry *ie)
9814 const struct got_error *err;
9815 struct report_file_info_arg *a = arg;
9816 struct got_pathlist_entry *pe;
9817 struct got_object_id blob_id, staged_blob_id, commit_id;
9818 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9819 struct got_object_id *commit_idp = NULL;
9820 int stage;
9822 if (a->cancel_cb) {
9823 err = a->cancel_cb(a->cancel_arg);
9824 if (err)
9825 return err;
9828 TAILQ_FOREACH(pe, a->paths, entry) {
9829 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9830 got_path_is_child(ie->path, pe->path, pe->path_len))
9831 break;
9833 if (pe == NULL) /* not found */
9834 return NULL;
9836 if (got_fileindex_entry_has_blob(ie))
9837 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9838 stage = got_fileindex_entry_stage_get(ie);
9839 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9840 stage == GOT_FILEIDX_STAGE_ADD) {
9841 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9842 &staged_blob_id, ie);
9845 if (got_fileindex_entry_has_commit(ie))
9846 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9848 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9849 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9852 const struct got_error *
9853 got_worktree_path_info(struct got_worktree *worktree,
9854 struct got_pathlist_head *paths,
9855 got_worktree_path_info_cb info_cb, void *info_arg,
9856 got_cancel_cb cancel_cb, void *cancel_arg)
9859 const struct got_error *err = NULL, *unlockerr;
9860 struct got_fileindex *fileindex = NULL;
9861 char *fileindex_path = NULL;
9862 struct report_file_info_arg arg;
9864 err = lock_worktree(worktree, LOCK_SH);
9865 if (err)
9866 return err;
9868 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9869 if (err)
9870 goto done;
9872 arg.worktree = worktree;
9873 arg.info_cb = info_cb;
9874 arg.info_arg = info_arg;
9875 arg.paths = paths;
9876 arg.cancel_cb = cancel_cb;
9877 arg.cancel_arg = cancel_arg;
9878 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9879 &arg);
9880 done:
9881 free(fileindex_path);
9882 if (fileindex)
9883 got_fileindex_free(fileindex);
9884 unlockerr = lock_worktree(worktree, LOCK_UN);
9885 if (unlockerr && err == NULL)
9886 err = unlockerr;
9887 return err;
9890 static const struct got_error *
9891 patch_check_path(const char *p, char **path, unsigned char *status,
9892 unsigned char *staged_status, struct got_fileindex *fileindex,
9893 struct got_worktree *worktree, struct got_repository *repo)
9895 const struct got_error *err;
9896 struct got_fileindex_entry *ie;
9897 struct stat sb;
9898 char *ondisk_path = NULL;
9900 err = got_worktree_resolve_path(path, worktree, p);
9901 if (err)
9902 return err;
9904 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9905 *path[0] ? "/" : "", *path) == -1)
9906 return got_error_from_errno("asprintf");
9908 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9909 if (ie) {
9910 *staged_status = get_staged_status(ie);
9911 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9912 repo);
9913 if (err)
9914 goto done;
9915 } else {
9916 *staged_status = GOT_STATUS_NO_CHANGE;
9917 *status = GOT_STATUS_UNVERSIONED;
9918 if (lstat(ondisk_path, &sb) == -1) {
9919 if (errno != ENOENT) {
9920 err = got_error_from_errno2("lstat",
9921 ondisk_path);
9922 goto done;
9924 *status = GOT_STATUS_NONEXISTENT;
9928 done:
9929 free(ondisk_path);
9930 return err;
9933 static const struct got_error *
9934 patch_can_rm(const char *path, unsigned char status,
9935 unsigned char staged_status)
9937 if (status == GOT_STATUS_NONEXISTENT)
9938 return got_error_set_errno(ENOENT, path);
9939 if (status != GOT_STATUS_NO_CHANGE &&
9940 status != GOT_STATUS_ADD &&
9941 status != GOT_STATUS_MODIFY &&
9942 status != GOT_STATUS_MODE_CHANGE)
9943 return got_error_path(path, GOT_ERR_FILE_STATUS);
9944 if (staged_status == GOT_STATUS_DELETE)
9945 return got_error_path(path, GOT_ERR_FILE_STATUS);
9946 return NULL;
9949 static const struct got_error *
9950 patch_can_add(const char *path, unsigned char status)
9952 if (status != GOT_STATUS_NONEXISTENT)
9953 return got_error_path(path, GOT_ERR_FILE_STATUS);
9954 return NULL;
9957 static const struct got_error *
9958 patch_can_edit(const char *path, unsigned char status,
9959 unsigned char staged_status)
9961 if (status == GOT_STATUS_NONEXISTENT)
9962 return got_error_set_errno(ENOENT, path);
9963 if (status != GOT_STATUS_NO_CHANGE &&
9964 status != GOT_STATUS_ADD &&
9965 status != GOT_STATUS_MODIFY)
9966 return got_error_path(path, GOT_ERR_FILE_STATUS);
9967 if (staged_status == GOT_STATUS_DELETE)
9968 return got_error_path(path, GOT_ERR_FILE_STATUS);
9969 return NULL;
9972 const struct got_error *
9973 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9974 char **fileindex_path, struct got_worktree *worktree)
9976 return open_fileindex(fileindex, fileindex_path, worktree);
9979 const struct got_error *
9980 got_worktree_patch_check_path(const char *old, const char *new,
9981 char **oldpath, char **newpath, struct got_worktree *worktree,
9982 struct got_repository *repo, struct got_fileindex *fileindex)
9984 const struct got_error *err = NULL;
9985 int file_renamed = 0;
9986 unsigned char status_old, staged_status_old;
9987 unsigned char status_new, staged_status_new;
9989 *oldpath = NULL;
9990 *newpath = NULL;
9992 err = patch_check_path(old != NULL ? old : new, oldpath,
9993 &status_old, &staged_status_old, fileindex, worktree, repo);
9994 if (err)
9995 goto done;
9997 err = patch_check_path(new != NULL ? new : old, newpath,
9998 &status_new, &staged_status_new, fileindex, worktree, repo);
9999 if (err)
10000 goto done;
10002 if (old != NULL && new != NULL && strcmp(old, new) != 0)
10003 file_renamed = 1;
10005 if (old != NULL && new == NULL)
10006 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10007 else if (file_renamed) {
10008 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10009 if (err == NULL)
10010 err = patch_can_add(*newpath, status_new);
10011 } else if (old == NULL)
10012 err = patch_can_add(*newpath, status_new);
10013 else
10014 err = patch_can_edit(*newpath, status_new, staged_status_new);
10016 done:
10017 if (err) {
10018 free(*oldpath);
10019 *oldpath = NULL;
10020 free(*newpath);
10021 *newpath = NULL;
10023 return err;
10026 const struct got_error *
10027 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10028 struct got_worktree *worktree, struct got_fileindex *fileindex,
10029 got_worktree_checkout_cb progress_cb, void *progress_arg)
10031 struct schedule_addition_args saa;
10033 memset(&saa, 0, sizeof(saa));
10034 saa.worktree = worktree;
10035 saa.fileindex = fileindex;
10036 saa.progress_cb = progress_cb;
10037 saa.progress_arg = progress_arg;
10038 saa.repo = repo;
10040 return worktree_status(worktree, path, fileindex, repo,
10041 schedule_addition, &saa, NULL, NULL, 1, 0);
10044 const struct got_error *
10045 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10046 struct got_worktree *worktree, struct got_fileindex *fileindex,
10047 got_worktree_delete_cb progress_cb, void *progress_arg)
10049 const struct got_error *err;
10050 struct schedule_deletion_args sda;
10051 char *ondisk_status_path;
10053 memset(&sda, 0, sizeof(sda));
10054 sda.worktree = worktree;
10055 sda.fileindex = fileindex;
10056 sda.progress_cb = progress_cb;
10057 sda.progress_arg = progress_arg;
10058 sda.repo = repo;
10059 sda.delete_local_mods = 0;
10060 sda.keep_on_disk = 0;
10061 sda.ignore_missing_paths = 0;
10062 sda.status_codes = NULL;
10063 if (asprintf(&ondisk_status_path, "%s/%s",
10064 got_worktree_get_root_path(worktree), path) == -1)
10065 return got_error_from_errno("asprintf");
10066 sda.status_path = ondisk_status_path;
10067 sda.status_path_len = strlen(ondisk_status_path);
10069 err = worktree_status(worktree, path, fileindex, repo,
10070 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10071 free(ondisk_status_path);
10072 return err;
10075 const struct got_error *
10076 got_worktree_patch_complete(struct got_fileindex *fileindex,
10077 const char *fileindex_path)
10079 const struct got_error *err = NULL;
10081 err = sync_fileindex(fileindex, fileindex_path);
10082 got_fileindex_free(fileindex);
10084 return err;