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;
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 int update_timestamps;
2048 struct got_blob_object *blob2 = NULL;
2049 char *label_orig = NULL;
2050 if (got_fileindex_entry_has_blob(ie)) {
2051 fd2 = got_opentempfd();
2052 if (fd2 == -1) {
2053 err = got_error_from_errno("got_opentempfd");
2054 goto done;
2056 struct got_object_id id2;
2057 got_fileindex_entry_get_blob_id(&id2, ie);
2058 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2059 fd2);
2060 if (err)
2061 goto done;
2063 if (got_fileindex_entry_has_commit(ie)) {
2064 char id_str[SHA1_DIGEST_STRING_LENGTH];
2065 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2066 sizeof(id_str)) == NULL) {
2067 err = got_error_path(id_str,
2068 GOT_ERR_BAD_OBJ_ID_STR);
2069 goto done;
2071 if (asprintf(&label_orig, "%s: commit %s",
2072 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2073 err = got_error_from_errno("asprintf");
2074 goto done;
2077 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2078 char *link_target;
2079 err = got_object_blob_read_to_str(&link_target, blob);
2080 if (err)
2081 goto done;
2082 err = merge_symlink(worktree, blob2, ondisk_path, path,
2083 label_orig, link_target, worktree->base_commit_id,
2084 repo, progress_cb, progress_arg);
2085 free(link_target);
2086 } else {
2087 err = merge_blob(&update_timestamps, worktree, blob2,
2088 ondisk_path, path, sb.st_mode, label_orig, blob,
2089 worktree->base_commit_id, repo,
2090 progress_cb, progress_arg);
2092 free(label_orig);
2093 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2094 err = got_error_from_errno("close");
2095 goto done;
2097 if (blob2)
2098 got_object_blob_close(blob2);
2099 if (err)
2100 goto done;
2102 * Do not update timestamps of files with local changes.
2103 * Otherwise, a future status walk would treat them as
2104 * unmodified files again.
2106 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2107 blob->id.sha1, worktree->base_commit_id->sha1,
2108 update_timestamps);
2109 } else if (status == GOT_STATUS_MODE_CHANGE) {
2110 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2111 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2112 } else if (status == GOT_STATUS_DELETE) {
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2114 if (err)
2115 goto done;
2116 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2117 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2118 if (err)
2119 goto done;
2120 } else {
2121 int is_bad_symlink = 0;
2122 if (S_ISLNK(te->mode)) {
2123 err = install_symlink(&is_bad_symlink, worktree,
2124 ondisk_path, path, blob,
2125 status == GOT_STATUS_MISSING, 0,
2126 status == GOT_STATUS_UNVERSIONED, 0,
2127 repo, progress_cb, progress_arg);
2128 } else {
2129 err = install_blob(worktree, ondisk_path, path,
2130 te->mode, sb.st_mode, blob,
2131 status == GOT_STATUS_MISSING, 0, 0,
2132 status == GOT_STATUS_UNVERSIONED,
2133 &update_timestamps,
2134 repo, progress_cb, progress_arg);
2136 if (err)
2137 goto done;
2139 if (ie) {
2140 err = got_fileindex_entry_update(ie,
2141 worktree->root_fd, path, blob->id.sha1,
2142 worktree->base_commit_id->sha1, 1);
2143 } else {
2144 err = create_fileindex_entry(&ie, fileindex,
2145 worktree->base_commit_id, worktree->root_fd, path,
2146 &blob->id, update_timestamps);
2148 if (err)
2149 goto done;
2151 if (is_bad_symlink) {
2152 got_fileindex_entry_filetype_set(ie,
2153 GOT_FILEIDX_MODE_BAD_SYMLINK);
2157 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2158 err = got_error_from_errno("close");
2159 goto done;
2161 got_object_blob_close(blob);
2162 done:
2163 free(ondisk_path);
2164 return err;
2167 static const struct got_error *
2168 remove_ondisk_file(const char *root_path, const char *path)
2170 const struct got_error *err = NULL;
2171 char *ondisk_path = NULL, *parent = NULL;
2173 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2174 return got_error_from_errno("asprintf");
2176 if (unlink(ondisk_path) == -1) {
2177 if (errno != ENOENT)
2178 err = got_error_from_errno2("unlink", ondisk_path);
2179 } else {
2180 size_t root_len = strlen(root_path);
2181 err = got_path_dirname(&parent, ondisk_path);
2182 if (err)
2183 goto done;
2184 while (got_path_cmp(parent, root_path,
2185 strlen(parent), root_len) != 0) {
2186 free(ondisk_path);
2187 ondisk_path = parent;
2188 parent = NULL;
2189 if (rmdir(ondisk_path) == -1) {
2190 if (errno != ENOTEMPTY)
2191 err = got_error_from_errno2("rmdir",
2192 ondisk_path);
2193 break;
2195 err = got_path_dirname(&parent, ondisk_path);
2196 if (err)
2197 break;
2200 done:
2201 free(ondisk_path);
2202 free(parent);
2203 return err;
2206 static const struct got_error *
2207 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2208 struct got_fileindex_entry *ie, struct got_repository *repo,
2209 got_worktree_checkout_cb progress_cb, void *progress_arg)
2211 const struct got_error *err = NULL;
2212 unsigned char status;
2213 struct stat sb;
2214 char *ondisk_path;
2216 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2217 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2219 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2220 == -1)
2221 return got_error_from_errno("asprintf");
2223 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2224 if (err)
2225 goto done;
2227 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2228 char ondisk_target[PATH_MAX];
2229 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2230 sizeof(ondisk_target));
2231 if (ondisk_len == -1) {
2232 err = got_error_from_errno2("readlink", ondisk_path);
2233 goto done;
2235 ondisk_target[ondisk_len] = '\0';
2236 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2237 NULL, NULL, /* XXX pass common ancestor info? */
2238 ondisk_target, ondisk_path);
2239 if (err)
2240 goto done;
2241 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2242 ie->path);
2243 goto done;
2246 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2247 status == GOT_STATUS_ADD) {
2248 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2249 if (err)
2250 goto done;
2252 * Preserve the working file and change the deleted blob's
2253 * entry into a schedule-add entry.
2255 err = got_fileindex_entry_update(ie, worktree->root_fd,
2256 ie->path, NULL, NULL, 0);
2257 } else {
2258 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2259 if (err)
2260 goto done;
2261 if (status == GOT_STATUS_NO_CHANGE) {
2262 err = remove_ondisk_file(worktree->root_path, ie->path);
2263 if (err)
2264 goto done;
2266 got_fileindex_entry_remove(fileindex, ie);
2268 done:
2269 free(ondisk_path);
2270 return err;
2273 struct diff_cb_arg {
2274 struct got_fileindex *fileindex;
2275 struct got_worktree *worktree;
2276 struct got_repository *repo;
2277 got_worktree_checkout_cb progress_cb;
2278 void *progress_arg;
2279 got_cancel_cb cancel_cb;
2280 void *cancel_arg;
2283 static const struct got_error *
2284 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2285 struct got_tree_entry *te, const char *parent_path)
2287 const struct got_error *err = NULL;
2288 struct diff_cb_arg *a = arg;
2290 if (a->cancel_cb) {
2291 err = a->cancel_cb(a->cancel_arg);
2292 if (err)
2293 return err;
2296 return update_blob(a->worktree, a->fileindex, ie, te,
2297 ie->path, a->repo, a->progress_cb, a->progress_arg);
2300 static const struct got_error *
2301 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2303 const struct got_error *err = NULL;
2304 struct diff_cb_arg *a = arg;
2306 if (a->cancel_cb) {
2307 err = a->cancel_cb(a->cancel_arg);
2308 if (err)
2309 return err;
2312 return delete_blob(a->worktree, a->fileindex, ie,
2313 a->repo, a->progress_cb, a->progress_arg);
2316 static const struct got_error *
2317 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2319 const struct got_error *err = NULL;
2320 struct diff_cb_arg *a = arg;
2321 char *path;
2323 if (a->cancel_cb) {
2324 err = a->cancel_cb(a->cancel_arg);
2325 if (err)
2326 return err;
2329 if (got_object_tree_entry_is_submodule(te))
2330 return NULL;
2332 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2333 return NULL;
2335 if (asprintf(&path, "%s%s%s", parent_path,
2336 parent_path[0] ? "/" : "", te->name)
2337 == -1)
2338 return got_error_from_errno("asprintf");
2340 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2341 a->repo, a->progress_cb, a->progress_arg);
2343 free(path);
2344 return err;
2347 const struct got_error *
2348 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2350 uint32_t uuid_status;
2352 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2353 if (uuid_status != uuid_s_ok) {
2354 *uuidstr = NULL;
2355 return got_error_uuid(uuid_status, "uuid_to_string");
2358 return NULL;
2361 static const struct got_error *
2362 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2364 const struct got_error *err = NULL;
2365 char *uuidstr = NULL;
2367 *refname = NULL;
2369 err = got_worktree_get_uuid(&uuidstr, worktree);
2370 if (err)
2371 return err;
2373 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2374 err = got_error_from_errno("asprintf");
2375 *refname = NULL;
2377 free(uuidstr);
2378 return err;
2381 const struct got_error *
2382 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2383 const char *prefix)
2385 return get_ref_name(refname, worktree, prefix);
2388 static const struct got_error *
2389 get_base_ref_name(char **refname, struct got_worktree *worktree)
2391 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2394 static const struct got_error *
2395 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2397 return get_ref_name(refname, worktree,
2398 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2401 static const struct got_error *
2402 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2404 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2407 static const struct got_error *
2408 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2410 return get_ref_name(refname, worktree,
2411 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2414 static const struct got_error *
2415 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2417 return get_ref_name(refname, worktree,
2418 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2421 static const struct got_error *
2422 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2424 return get_ref_name(refname, worktree,
2425 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2428 static const struct got_error *
2429 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2431 return get_ref_name(refname, worktree,
2432 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2435 static const struct got_error *
2436 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2438 return get_ref_name(refname, worktree,
2439 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2442 static const struct got_error *
2443 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2445 return get_ref_name(refname, worktree,
2446 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2449 const struct got_error *
2450 got_worktree_get_histedit_script_path(char **path,
2451 struct got_worktree *worktree)
2453 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2454 worktree->meta_dir, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2455 *path = NULL;
2456 return got_error_from_errno("asprintf");
2458 return NULL;
2461 static const struct got_error *
2462 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2464 return get_ref_name(refname, worktree,
2465 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2468 static const struct got_error *
2469 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2471 return get_ref_name(refname, worktree,
2472 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2476 * Prevent Git's garbage collector from deleting our base commit by
2477 * setting a reference to our base commit's ID.
2479 static const struct got_error *
2480 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2482 const struct got_error *err = NULL;
2483 struct got_reference *ref = NULL;
2484 char *refname;
2486 err = get_base_ref_name(&refname, worktree);
2487 if (err)
2488 return err;
2490 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2491 if (err)
2492 goto done;
2494 err = got_ref_write(ref, repo);
2495 done:
2496 free(refname);
2497 if (ref)
2498 got_ref_close(ref);
2499 return err;
2502 static const struct got_error *
2503 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2505 const struct got_error *err = NULL;
2507 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2508 worktree->meta_dir, GOT_WORKTREE_FILE_INDEX) == -1) {
2509 err = got_error_from_errno("asprintf");
2510 *fileindex_path = NULL;
2512 return err;
2516 static const struct got_error *
2517 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2518 struct got_worktree *worktree)
2520 const struct got_error *err = NULL;
2521 FILE *index = NULL;
2523 *fileindex_path = NULL;
2524 *fileindex = got_fileindex_alloc();
2525 if (*fileindex == NULL)
2526 return got_error_from_errno("got_fileindex_alloc");
2528 err = get_fileindex_path(fileindex_path, worktree);
2529 if (err)
2530 goto done;
2532 index = fopen(*fileindex_path, "rbe");
2533 if (index == NULL) {
2534 if (errno != ENOENT)
2535 err = got_error_from_errno2("fopen", *fileindex_path);
2536 } else {
2537 err = got_fileindex_read(*fileindex, index);
2538 if (fclose(index) == EOF && err == NULL)
2539 err = got_error_from_errno("fclose");
2541 done:
2542 if (err) {
2543 free(*fileindex_path);
2544 *fileindex_path = NULL;
2545 got_fileindex_free(*fileindex);
2546 *fileindex = NULL;
2548 return err;
2551 struct bump_base_commit_id_arg {
2552 struct got_object_id *base_commit_id;
2553 const char *path;
2554 size_t path_len;
2555 const char *entry_name;
2556 got_worktree_checkout_cb progress_cb;
2557 void *progress_arg;
2560 static const struct got_error *
2561 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2563 const struct got_error *err;
2564 struct bump_base_commit_id_arg *a = arg;
2566 if (a->entry_name) {
2567 if (strcmp(ie->path, a->path) != 0)
2568 return NULL;
2569 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2570 return NULL;
2572 if (got_fileindex_entry_was_skipped(ie))
2573 return NULL;
2575 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2576 SHA1_DIGEST_LENGTH) == 0)
2577 return NULL;
2579 if (a->progress_cb) {
2580 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2581 ie->path);
2582 if (err)
2583 return err;
2585 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2586 return NULL;
2589 /* Bump base commit ID of all files within an updated part of the work tree. */
2590 static const struct got_error *
2591 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2592 struct got_fileindex *fileindex,
2593 got_worktree_checkout_cb progress_cb, void *progress_arg)
2595 struct bump_base_commit_id_arg bbc_arg;
2597 bbc_arg.base_commit_id = worktree->base_commit_id;
2598 bbc_arg.entry_name = NULL;
2599 bbc_arg.path = "";
2600 bbc_arg.path_len = 0;
2601 bbc_arg.progress_cb = progress_cb;
2602 bbc_arg.progress_arg = progress_arg;
2604 return got_fileindex_for_each_entry_safe(fileindex,
2605 bump_base_commit_id, &bbc_arg);
2608 static const struct got_error *
2609 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2611 const struct got_error *err = NULL;
2612 char *new_fileindex_path = NULL;
2613 FILE *new_index = NULL;
2614 struct timespec timeout;
2616 err = got_opentemp_named(&new_fileindex_path, &new_index,
2617 fileindex_path, "");
2618 if (err)
2619 goto done;
2621 err = got_fileindex_write(fileindex, new_index);
2622 if (err)
2623 goto done;
2625 if (rename(new_fileindex_path, fileindex_path) != 0) {
2626 err = got_error_from_errno3("rename", new_fileindex_path,
2627 fileindex_path);
2628 unlink(new_fileindex_path);
2632 * Sleep for a short amount of time to ensure that files modified after
2633 * this program exits have a different time stamp from the one which
2634 * was recorded in the file index.
2636 timeout.tv_sec = 0;
2637 timeout.tv_nsec = 1;
2638 nanosleep(&timeout, NULL);
2639 done:
2640 if (new_index)
2641 fclose(new_index);
2642 free(new_fileindex_path);
2643 return err;
2646 static const struct got_error *
2647 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2648 struct got_object_id **tree_id, const char *wt_relpath,
2649 struct got_commit_object *base_commit, struct got_worktree *worktree,
2650 struct got_repository *repo)
2652 const struct got_error *err = NULL;
2653 struct got_object_id *id = NULL;
2654 char *in_repo_path = NULL;
2655 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2657 *entry_type = GOT_OBJ_TYPE_ANY;
2658 *tree_relpath = NULL;
2659 *tree_id = NULL;
2661 if (wt_relpath[0] == '\0') {
2662 /* Check out all files within the work tree. */
2663 *entry_type = GOT_OBJ_TYPE_TREE;
2664 *tree_relpath = strdup("");
2665 if (*tree_relpath == NULL) {
2666 err = got_error_from_errno("strdup");
2667 goto done;
2669 err = got_object_id_by_path(tree_id, repo, base_commit,
2670 worktree->path_prefix);
2671 if (err)
2672 goto done;
2673 return NULL;
2676 /* Check out a subset of files in the work tree. */
2678 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2679 is_root_wt ? "" : "/", wt_relpath) == -1) {
2680 err = got_error_from_errno("asprintf");
2681 goto done;
2684 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2685 if (err)
2686 goto done;
2688 free(in_repo_path);
2689 in_repo_path = NULL;
2691 err = got_object_get_type(entry_type, repo, id);
2692 if (err)
2693 goto done;
2695 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2696 /* Check out a single file. */
2697 if (strchr(wt_relpath, '/') == NULL) {
2698 /* Check out a single file in work tree's root dir. */
2699 in_repo_path = strdup(worktree->path_prefix);
2700 if (in_repo_path == NULL) {
2701 err = got_error_from_errno("strdup");
2702 goto done;
2704 *tree_relpath = strdup("");
2705 if (*tree_relpath == NULL) {
2706 err = got_error_from_errno("strdup");
2707 goto done;
2709 } else {
2710 /* Check out a single file in a subdirectory. */
2711 err = got_path_dirname(tree_relpath, wt_relpath);
2712 if (err)
2713 return err;
2714 if (asprintf(&in_repo_path, "%s%s%s",
2715 worktree->path_prefix, is_root_wt ? "" : "/",
2716 *tree_relpath) == -1) {
2717 err = got_error_from_errno("asprintf");
2718 goto done;
2721 err = got_object_id_by_path(tree_id, repo,
2722 base_commit, in_repo_path);
2723 } else {
2724 /* Check out all files within a subdirectory. */
2725 *tree_id = got_object_id_dup(id);
2726 if (*tree_id == NULL) {
2727 err = got_error_from_errno("got_object_id_dup");
2728 goto done;
2730 *tree_relpath = strdup(wt_relpath);
2731 if (*tree_relpath == NULL) {
2732 err = got_error_from_errno("strdup");
2733 goto done;
2736 done:
2737 free(id);
2738 free(in_repo_path);
2739 if (err) {
2740 *entry_type = GOT_OBJ_TYPE_ANY;
2741 free(*tree_relpath);
2742 *tree_relpath = NULL;
2743 free(*tree_id);
2744 *tree_id = NULL;
2746 return err;
2749 static const struct got_error *
2750 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2751 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2752 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2753 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2755 const struct got_error *err = NULL;
2756 struct got_commit_object *commit = NULL;
2757 struct got_tree_object *tree = NULL;
2758 struct got_fileindex_diff_tree_cb diff_cb;
2759 struct diff_cb_arg arg;
2761 err = ref_base_commit(worktree, repo);
2762 if (err) {
2763 if (!(err->code == GOT_ERR_ERRNO &&
2764 (errno == EACCES || errno == EROFS)))
2765 goto done;
2766 err = (*progress_cb)(progress_arg,
2767 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2768 if (err)
2769 return err;
2772 err = got_object_open_as_commit(&commit, repo,
2773 worktree->base_commit_id);
2774 if (err)
2775 goto done;
2777 err = got_object_open_as_tree(&tree, repo, tree_id);
2778 if (err)
2779 goto done;
2781 if (entry_name &&
2782 got_object_tree_find_entry(tree, entry_name) == NULL) {
2783 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2784 goto done;
2787 diff_cb.diff_old_new = diff_old_new;
2788 diff_cb.diff_old = diff_old;
2789 diff_cb.diff_new = diff_new;
2790 arg.fileindex = fileindex;
2791 arg.worktree = worktree;
2792 arg.repo = repo;
2793 arg.progress_cb = progress_cb;
2794 arg.progress_arg = progress_arg;
2795 arg.cancel_cb = cancel_cb;
2796 arg.cancel_arg = cancel_arg;
2797 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2798 entry_name, repo, &diff_cb, &arg);
2799 done:
2800 if (tree)
2801 got_object_tree_close(tree);
2802 if (commit)
2803 got_object_commit_close(commit);
2804 return err;
2807 const struct got_error *
2808 got_worktree_checkout_files(struct got_worktree *worktree,
2809 struct got_pathlist_head *paths, struct got_repository *repo,
2810 got_worktree_checkout_cb progress_cb, void *progress_arg,
2811 got_cancel_cb cancel_cb, void *cancel_arg)
2813 const struct got_error *err = NULL, *sync_err, *unlockerr;
2814 struct got_commit_object *commit = NULL;
2815 struct got_tree_object *tree = NULL;
2816 struct got_fileindex *fileindex = NULL;
2817 char *fileindex_path = NULL;
2818 struct got_pathlist_entry *pe;
2819 struct tree_path_data {
2820 STAILQ_ENTRY(tree_path_data) entry;
2821 struct got_object_id *tree_id;
2822 int entry_type;
2823 char *relpath;
2824 char *entry_name;
2825 } *tpd = NULL;
2826 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2828 STAILQ_INIT(&tree_paths);
2830 err = lock_worktree(worktree, LOCK_EX);
2831 if (err)
2832 return err;
2834 err = got_object_open_as_commit(&commit, repo,
2835 worktree->base_commit_id);
2836 if (err)
2837 goto done;
2839 /* Map all specified paths to in-repository trees. */
2840 TAILQ_FOREACH(pe, paths, entry) {
2841 tpd = malloc(sizeof(*tpd));
2842 if (tpd == NULL) {
2843 err = got_error_from_errno("malloc");
2844 goto done;
2847 err = find_tree_entry_for_checkout(&tpd->entry_type,
2848 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2849 worktree, repo);
2850 if (err) {
2851 free(tpd);
2852 goto done;
2855 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2856 err = got_path_basename(&tpd->entry_name, pe->path);
2857 if (err) {
2858 free(tpd->relpath);
2859 free(tpd->tree_id);
2860 free(tpd);
2861 goto done;
2863 } else
2864 tpd->entry_name = NULL;
2866 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2870 * Read the file index.
2871 * Checking out files is supposed to be an idempotent operation.
2872 * If the on-disk file index is incomplete we will try to complete it.
2874 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2875 if (err)
2876 goto done;
2878 tpd = STAILQ_FIRST(&tree_paths);
2879 TAILQ_FOREACH(pe, paths, entry) {
2880 struct bump_base_commit_id_arg bbc_arg;
2882 err = checkout_files(worktree, fileindex, tpd->relpath,
2883 tpd->tree_id, tpd->entry_name, repo,
2884 progress_cb, progress_arg, cancel_cb, cancel_arg);
2885 if (err)
2886 break;
2888 bbc_arg.base_commit_id = worktree->base_commit_id;
2889 bbc_arg.entry_name = tpd->entry_name;
2890 bbc_arg.path = pe->path;
2891 bbc_arg.path_len = pe->path_len;
2892 bbc_arg.progress_cb = progress_cb;
2893 bbc_arg.progress_arg = progress_arg;
2894 err = got_fileindex_for_each_entry_safe(fileindex,
2895 bump_base_commit_id, &bbc_arg);
2896 if (err)
2897 break;
2899 tpd = STAILQ_NEXT(tpd, entry);
2901 sync_err = sync_fileindex(fileindex, fileindex_path);
2902 if (sync_err && err == NULL)
2903 err = sync_err;
2904 done:
2905 free(fileindex_path);
2906 if (tree)
2907 got_object_tree_close(tree);
2908 if (commit)
2909 got_object_commit_close(commit);
2910 if (fileindex)
2911 got_fileindex_free(fileindex);
2912 while (!STAILQ_EMPTY(&tree_paths)) {
2913 tpd = STAILQ_FIRST(&tree_paths);
2914 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2915 free(tpd->relpath);
2916 free(tpd->tree_id);
2917 free(tpd);
2919 unlockerr = lock_worktree(worktree, LOCK_SH);
2920 if (unlockerr && err == NULL)
2921 err = unlockerr;
2922 return err;
2925 static const struct got_error *
2926 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2927 struct got_fileindex_entry *ie, const char *ondisk_path,
2928 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2929 int restoring_missing_file, int reverting_versioned_file,
2930 int path_is_unversioned, int allow_bad_symlinks,
2931 struct got_repository *repo,
2932 got_worktree_checkout_cb progress_cb, void *progress_arg)
2934 const struct got_error *err = NULL;
2935 int is_bad_symlink = 0;
2937 if (S_ISLNK(mode2)) {
2938 err = install_symlink(&is_bad_symlink,
2939 worktree, ondisk_path, path2, blob2,
2940 restoring_missing_file,
2941 reverting_versioned_file,
2942 path_is_unversioned, allow_bad_symlinks,
2943 repo, progress_cb, progress_arg);
2944 } else {
2945 err = install_blob(worktree, ondisk_path, path2,
2946 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2947 restoring_missing_file, reverting_versioned_file, 0,
2948 path_is_unversioned, NULL, repo,
2949 progress_cb, progress_arg);
2951 if (err)
2952 return err;
2953 if (ie == NULL) {
2954 /* Adding an unversioned file. */
2955 err = got_fileindex_entry_alloc(&ie, path2);
2956 if (err)
2957 return err;
2958 err = got_fileindex_entry_update(ie,
2959 worktree->root_fd, path2, NULL, NULL, 1);
2960 if (err) {
2961 got_fileindex_entry_free(ie);
2962 return err;
2964 err = got_fileindex_entry_add(fileindex, ie);
2965 if (err) {
2966 got_fileindex_entry_free(ie);
2967 return err;
2969 } else {
2970 /* Re-adding a locally deleted file. */
2971 err = got_fileindex_entry_update(ie,
2972 worktree->root_fd, path2, ie->blob_sha1,
2973 worktree->base_commit_id->sha1, 0);
2974 if (err)
2975 return err;
2978 if (is_bad_symlink) {
2979 got_fileindex_entry_filetype_set(ie,
2980 GOT_FILEIDX_MODE_BAD_SYMLINK);
2983 return NULL;
2986 struct merge_file_cb_arg {
2987 struct got_worktree *worktree;
2988 struct got_fileindex *fileindex;
2989 got_worktree_checkout_cb progress_cb;
2990 void *progress_arg;
2991 got_cancel_cb cancel_cb;
2992 void *cancel_arg;
2993 const char *label_orig;
2994 struct got_object_id *commit_id2;
2995 int allow_bad_symlinks;
2998 static const struct got_error *
2999 merge_file_cb(void *arg, struct got_blob_object *blob1,
3000 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3001 struct got_object_id *id1, struct got_object_id *id2,
3002 const char *path1, const char *path2,
3003 mode_t mode1, mode_t mode2, struct got_repository *repo)
3005 static const struct got_error *err = NULL;
3006 struct merge_file_cb_arg *a = arg;
3007 struct got_fileindex_entry *ie;
3008 char *ondisk_path = NULL;
3009 struct stat sb;
3010 unsigned char status;
3011 int local_changes_subsumed;
3012 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
3013 char *id_str = NULL, *label_deriv2 = NULL;
3014 struct got_object_id *id = NULL;
3016 if (blob1 && blob2) {
3017 ie = got_fileindex_entry_get(a->fileindex, path2,
3018 strlen(path2));
3019 if (ie == NULL)
3020 return (*a->progress_cb)(a->progress_arg,
3021 GOT_STATUS_MISSING, path2);
3023 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3024 path2) == -1)
3025 return got_error_from_errno("asprintf");
3027 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3028 repo);
3029 if (err)
3030 goto done;
3032 if (status == GOT_STATUS_DELETE) {
3033 err = (*a->progress_cb)(a->progress_arg,
3034 GOT_STATUS_MERGE, path2);
3035 goto done;
3037 if (status != GOT_STATUS_NO_CHANGE &&
3038 status != GOT_STATUS_MODIFY &&
3039 status != GOT_STATUS_CONFLICT &&
3040 status != GOT_STATUS_ADD) {
3041 err = (*a->progress_cb)(a->progress_arg, status, path2);
3042 goto done;
3045 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3046 char *link_target2;
3047 err = got_object_blob_read_to_str(&link_target2, blob2);
3048 if (err)
3049 goto done;
3050 err = merge_symlink(a->worktree, blob1, ondisk_path,
3051 path2, a->label_orig, link_target2, a->commit_id2,
3052 repo, a->progress_cb, a->progress_arg);
3053 free(link_target2);
3054 } else {
3055 int fd;
3057 f_orig = got_opentemp();
3058 if (f_orig == NULL) {
3059 err = got_error_from_errno("got_opentemp");
3060 goto done;
3062 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3063 f_orig, blob1);
3064 if (err)
3065 goto done;
3067 f_deriv2 = got_opentemp();
3068 if (f_deriv2 == NULL)
3069 goto done;
3070 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3071 f_deriv2, blob2);
3072 if (err)
3073 goto done;
3075 fd = open(ondisk_path,
3076 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3077 if (fd == -1) {
3078 err = got_error_from_errno2("open",
3079 ondisk_path);
3080 goto done;
3082 f_deriv = fdopen(fd, "r");
3083 if (f_deriv == NULL) {
3084 err = got_error_from_errno2("fdopen",
3085 ondisk_path);
3086 close(fd);
3087 goto done;
3089 err = got_object_id_str(&id_str, a->commit_id2);
3090 if (err)
3091 goto done;
3092 if (asprintf(&label_deriv2, "%s: commit %s",
3093 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3094 err = got_error_from_errno("asprintf");
3095 goto done;
3097 err = merge_file(&local_changes_subsumed, a->worktree,
3098 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3099 mode2, a->label_orig, NULL, label_deriv2,
3100 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3101 a->progress_cb, a->progress_arg);
3103 } else if (blob1) {
3104 ie = got_fileindex_entry_get(a->fileindex, path1,
3105 strlen(path1));
3106 if (ie == NULL)
3107 return (*a->progress_cb)(a->progress_arg,
3108 GOT_STATUS_MISSING, path1);
3110 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3111 path1) == -1)
3112 return got_error_from_errno("asprintf");
3114 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3115 repo);
3116 if (err)
3117 goto done;
3119 switch (status) {
3120 case GOT_STATUS_NO_CHANGE:
3121 err = (*a->progress_cb)(a->progress_arg,
3122 GOT_STATUS_DELETE, path1);
3123 if (err)
3124 goto done;
3125 err = remove_ondisk_file(a->worktree->root_path, path1);
3126 if (err)
3127 goto done;
3128 if (ie)
3129 got_fileindex_entry_mark_deleted_from_disk(ie);
3130 break;
3131 case GOT_STATUS_DELETE:
3132 case GOT_STATUS_MISSING:
3133 err = (*a->progress_cb)(a->progress_arg,
3134 GOT_STATUS_DELETE, path1);
3135 if (err)
3136 goto done;
3137 if (ie)
3138 got_fileindex_entry_mark_deleted_from_disk(ie);
3139 break;
3140 case GOT_STATUS_MODIFY: {
3141 FILE *blob1_f;
3142 off_t blob1_size;
3143 int obj_type;
3145 * Delete the file only if its content already
3146 * exists in the repository.
3148 err = got_object_blob_file_create(&id, &blob1_f,
3149 &blob1_size, path1);
3150 if (err)
3151 goto done;
3152 if (fclose(blob1_f) == EOF) {
3153 err = got_error_from_errno("fclose");
3154 goto done;
3157 /* Implied existence check. */
3158 err = got_object_get_type(&obj_type, repo, id);
3159 if (err) {
3160 if (err->code != GOT_ERR_NO_OBJ)
3161 goto done;
3162 err = (*a->progress_cb)(a->progress_arg,
3163 GOT_STATUS_CANNOT_DELETE, path1);
3164 goto done;
3165 } else if (obj_type != GOT_OBJ_TYPE_BLOB) {
3166 err = (*a->progress_cb)(a->progress_arg,
3167 GOT_STATUS_CANNOT_DELETE, path1);
3168 goto done;
3170 err = (*a->progress_cb)(a->progress_arg,
3171 GOT_STATUS_DELETE, path1);
3172 if (err)
3173 goto done;
3174 err = remove_ondisk_file(a->worktree->root_path,
3175 path1);
3176 if (err)
3177 goto done;
3178 if (ie)
3179 got_fileindex_entry_mark_deleted_from_disk(ie);
3180 break;
3182 case GOT_STATUS_ADD: {
3183 FILE *blob1_f;
3184 off_t blob1_size;
3186 * Delete the file only if its content already
3187 * exists in the repository.
3189 err = got_object_blob_file_create(&id, &blob1_f,
3190 &blob1_size, path1);
3191 if (err)
3192 goto done;
3193 if (fclose(blob1_f) == EOF) {
3194 err = got_error_from_errno("fclose");
3195 goto done;
3197 if (got_object_id_cmp(id, id1) == 0) {
3198 err = (*a->progress_cb)(a->progress_arg,
3199 GOT_STATUS_DELETE, path1);
3200 if (err)
3201 goto done;
3202 err = remove_ondisk_file(a->worktree->root_path,
3203 path1);
3204 if (err)
3205 goto done;
3206 if (ie)
3207 got_fileindex_entry_remove(a->fileindex,
3208 ie);
3209 } else {
3210 err = (*a->progress_cb)(a->progress_arg,
3211 GOT_STATUS_CANNOT_DELETE, path1);
3213 if (err)
3214 goto done;
3215 break;
3217 case GOT_STATUS_CONFLICT:
3218 err = (*a->progress_cb)(a->progress_arg,
3219 GOT_STATUS_CANNOT_DELETE, path1);
3220 if (err)
3221 goto done;
3222 break;
3223 case GOT_STATUS_OBSTRUCTED:
3224 err = (*a->progress_cb)(a->progress_arg, status, path1);
3225 if (err)
3226 goto done;
3227 break;
3228 default:
3229 break;
3231 } else if (blob2) {
3232 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3233 path2) == -1)
3234 return got_error_from_errno("asprintf");
3235 ie = got_fileindex_entry_get(a->fileindex, path2,
3236 strlen(path2));
3237 if (ie) {
3238 err = get_file_status(&status, &sb, ie, ondisk_path,
3239 -1, NULL, repo);
3240 if (err)
3241 goto done;
3242 if (status != GOT_STATUS_NO_CHANGE &&
3243 status != GOT_STATUS_MODIFY &&
3244 status != GOT_STATUS_CONFLICT &&
3245 status != GOT_STATUS_ADD &&
3246 status != GOT_STATUS_DELETE) {
3247 err = (*a->progress_cb)(a->progress_arg,
3248 status, path2);
3249 goto done;
3251 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3252 char *link_target2;
3253 err = got_object_blob_read_to_str(&link_target2,
3254 blob2);
3255 if (err)
3256 goto done;
3257 err = merge_symlink(a->worktree, NULL,
3258 ondisk_path, path2, a->label_orig,
3259 link_target2, a->commit_id2, repo,
3260 a->progress_cb, a->progress_arg);
3261 free(link_target2);
3262 } else if (S_ISREG(sb.st_mode)) {
3263 err = merge_blob(&local_changes_subsumed,
3264 a->worktree, NULL, ondisk_path, path2,
3265 sb.st_mode, a->label_orig, blob2,
3266 a->commit_id2, repo, a->progress_cb,
3267 a->progress_arg);
3268 } else if (status != GOT_STATUS_DELETE) {
3269 err = got_error_path(ondisk_path,
3270 GOT_ERR_FILE_OBSTRUCTED);
3272 if (err)
3273 goto done;
3274 if (status == GOT_STATUS_DELETE) {
3275 /* Re-add file with content from new blob. */
3276 err = add_file(a->worktree, a->fileindex, ie,
3277 ondisk_path, path2, blob2, mode2,
3278 0, 0, 0, a->allow_bad_symlinks,
3279 repo, a->progress_cb, a->progress_arg);
3280 if (err)
3281 goto done;
3283 } else {
3284 err = add_file(a->worktree, a->fileindex, NULL,
3285 ondisk_path, path2, blob2, mode2,
3286 0, 0, 1, a->allow_bad_symlinks,
3287 repo, a->progress_cb, a->progress_arg);
3288 if (err)
3289 goto done;
3292 done:
3293 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3294 err = got_error_from_errno("fclose");
3295 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3296 err = got_error_from_errno("fclose");
3297 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3298 err = got_error_from_errno("fclose");
3299 free(id_str);
3300 free(id);
3301 free(label_deriv2);
3302 free(ondisk_path);
3303 return err;
3306 struct check_mixed_commits_args {
3307 struct got_worktree *worktree;
3308 got_cancel_cb cancel_cb;
3309 void *cancel_arg;
3312 static const struct got_error *
3313 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3315 const struct got_error *err = NULL;
3316 struct check_mixed_commits_args *a = arg;
3318 if (a->cancel_cb) {
3319 err = a->cancel_cb(a->cancel_arg);
3320 if (err)
3321 return err;
3324 /* Reject merges into a work tree with mixed base commits. */
3325 if (got_fileindex_entry_has_commit(ie) &&
3326 memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3327 SHA1_DIGEST_LENGTH) != 0)
3328 return got_error(GOT_ERR_MIXED_COMMITS);
3330 return NULL;
3333 const struct got_error *
3334 got_worktree_get_state(char *state, struct got_repository *repo,
3335 struct got_worktree *worktree,
3336 got_cancel_cb cancel_cb, void *cancel_arg)
3338 const struct got_error *err;
3339 struct got_object_id *base_id, *head_id = NULL;
3340 struct got_reference *head_ref;
3341 struct got_fileindex *fileindex = NULL;
3342 char *fileindex_path = NULL;
3343 struct check_mixed_commits_args cma;
3345 if (worktree == NULL)
3346 return got_error(GOT_ERR_NOT_WORKTREE);
3348 err = got_ref_open(&head_ref, repo,
3349 got_worktree_get_head_ref_name(worktree), 0);
3350 if (err)
3351 return err;
3353 err = got_ref_resolve(&head_id, repo, head_ref);
3354 if (err)
3355 goto done;
3357 *state = GOT_WORKTREE_STATE_UNKNOWN;
3358 base_id = got_worktree_get_base_commit_id(worktree);
3360 cma.worktree = worktree;
3361 cma.cancel_cb = cancel_cb;
3362 cma.cancel_arg = cancel_arg;
3364 if (got_object_id_cmp(base_id, head_id) == 0) {
3365 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3366 if (err)
3367 goto done;
3369 err = got_fileindex_for_each_entry_safe(fileindex,
3370 check_mixed_commits, &cma);
3371 if (err == NULL)
3372 *state = GOT_WORKTREE_STATE_UPTODATE;
3373 else if (err->code == GOT_ERR_MIXED_COMMITS) {
3374 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3375 err = NULL;
3377 } else
3378 *state = GOT_WORKTREE_STATE_OUTOFDATE;
3380 done:
3381 free(head_id);
3382 free(fileindex_path);
3383 got_ref_close(head_ref);
3384 if (fileindex != NULL)
3385 got_fileindex_free(fileindex);
3386 return err;
3389 struct check_merge_conflicts_arg {
3390 struct got_worktree *worktree;
3391 struct got_fileindex *fileindex;
3392 struct got_repository *repo;
3395 static const struct got_error *
3396 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3397 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3398 struct got_object_id *id1, struct got_object_id *id2,
3399 const char *path1, const char *path2,
3400 mode_t mode1, mode_t mode2, struct got_repository *repo)
3402 const struct got_error *err = NULL;
3403 struct check_merge_conflicts_arg *a = arg;
3404 unsigned char status;
3405 struct stat sb;
3406 struct got_fileindex_entry *ie;
3407 const char *path = path2 ? path2 : path1;
3408 struct got_object_id *id = id2 ? id2 : id1;
3409 char *ondisk_path;
3411 if (id == NULL)
3412 return NULL;
3414 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3415 if (ie == NULL)
3416 return NULL;
3418 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3419 == -1)
3420 return got_error_from_errno("asprintf");
3422 /* Reject merges into a work tree with conflicted files. */
3423 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3424 free(ondisk_path);
3425 if (err)
3426 return err;
3427 if (status == GOT_STATUS_CONFLICT)
3428 return got_error(GOT_ERR_CONFLICTS);
3430 return NULL;
3433 static const struct got_error *
3434 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3435 const char *fileindex_path, struct got_object_id *commit_id1,
3436 struct got_object_id *commit_id2, struct got_repository *repo,
3437 got_worktree_checkout_cb progress_cb, void *progress_arg,
3438 got_cancel_cb cancel_cb, void *cancel_arg)
3440 const struct got_error *err = NULL, *sync_err;
3441 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3442 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3443 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3444 struct check_merge_conflicts_arg cmc_arg;
3445 struct merge_file_cb_arg arg;
3446 char *label_orig = NULL;
3447 FILE *f1 = NULL, *f2 = NULL;
3448 int fd1 = -1, fd2 = -1;
3450 if (commit_id1) {
3451 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3452 if (err)
3453 goto done;
3454 err = got_object_id_by_path(&tree_id1, repo, commit1,
3455 worktree->path_prefix);
3456 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3457 goto done;
3459 if (tree_id1) {
3460 char *id_str;
3462 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3463 if (err)
3464 goto done;
3466 err = got_object_id_str(&id_str, commit_id1);
3467 if (err)
3468 goto done;
3470 if (asprintf(&label_orig, "%s: commit %s",
3471 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3472 err = got_error_from_errno("asprintf");
3473 free(id_str);
3474 goto done;
3476 free(id_str);
3478 f1 = got_opentemp();
3479 if (f1 == NULL) {
3480 err = got_error_from_errno("got_opentemp");
3481 goto done;
3485 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3486 if (err)
3487 goto done;
3489 err = got_object_id_by_path(&tree_id2, repo, commit2,
3490 worktree->path_prefix);
3491 if (err)
3492 goto done;
3494 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3495 if (err)
3496 goto done;
3498 f2 = got_opentemp();
3499 if (f2 == NULL) {
3500 err = got_error_from_errno("got_opentemp");
3501 goto done;
3504 fd1 = got_opentempfd();
3505 if (fd1 == -1) {
3506 err = got_error_from_errno("got_opentempfd");
3507 goto done;
3510 fd2 = got_opentempfd();
3511 if (fd2 == -1) {
3512 err = got_error_from_errno("got_opentempfd");
3513 goto done;
3516 cmc_arg.worktree = worktree;
3517 cmc_arg.fileindex = fileindex;
3518 cmc_arg.repo = repo;
3519 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3520 check_merge_conflicts, &cmc_arg, 0);
3521 if (err)
3522 goto done;
3524 arg.worktree = worktree;
3525 arg.fileindex = fileindex;
3526 arg.progress_cb = progress_cb;
3527 arg.progress_arg = progress_arg;
3528 arg.cancel_cb = cancel_cb;
3529 arg.cancel_arg = cancel_arg;
3530 arg.label_orig = label_orig;
3531 arg.commit_id2 = commit_id2;
3532 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3533 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3534 merge_file_cb, &arg, 1);
3535 sync_err = sync_fileindex(fileindex, fileindex_path);
3536 if (sync_err && err == NULL)
3537 err = sync_err;
3538 done:
3539 if (commit1)
3540 got_object_commit_close(commit1);
3541 if (commit2)
3542 got_object_commit_close(commit2);
3543 if (tree1)
3544 got_object_tree_close(tree1);
3545 if (tree2)
3546 got_object_tree_close(tree2);
3547 if (f1 && fclose(f1) == EOF && err == NULL)
3548 err = got_error_from_errno("fclose");
3549 if (f2 && fclose(f2) == EOF && err == NULL)
3550 err = got_error_from_errno("fclose");
3551 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3552 err = got_error_from_errno("close");
3553 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3554 err = got_error_from_errno("close");
3555 free(label_orig);
3556 return err;
3559 const struct got_error *
3560 got_worktree_merge_files(struct got_worktree *worktree,
3561 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3565 const struct got_error *err, *unlockerr;
3566 char *fileindex_path = NULL;
3567 struct got_fileindex *fileindex = NULL;
3568 struct check_mixed_commits_args cma;
3570 err = lock_worktree(worktree, LOCK_EX);
3571 if (err)
3572 return err;
3574 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3575 if (err)
3576 goto done;
3578 cma.worktree = worktree;
3579 cma.cancel_cb = cancel_cb;
3580 cma.cancel_arg = cancel_arg;
3582 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3583 &cma);
3584 if (err)
3585 goto done;
3587 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3588 commit_id2, repo, progress_cb, progress_arg,
3589 cancel_cb, cancel_arg);
3590 done:
3591 if (fileindex)
3592 got_fileindex_free(fileindex);
3593 free(fileindex_path);
3594 unlockerr = lock_worktree(worktree, LOCK_SH);
3595 if (unlockerr && err == NULL)
3596 err = unlockerr;
3597 return err;
3600 struct diff_dir_cb_arg {
3601 struct got_fileindex *fileindex;
3602 struct got_worktree *worktree;
3603 const char *status_path;
3604 size_t status_path_len;
3605 struct got_repository *repo;
3606 got_worktree_status_cb status_cb;
3607 void *status_arg;
3608 got_cancel_cb cancel_cb;
3609 void *cancel_arg;
3610 /* A pathlist containing per-directory pathlists of ignore patterns. */
3611 struct got_pathlist_head *ignores;
3612 int report_unchanged;
3613 int no_ignores;
3616 static const struct got_error *
3617 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3618 int dirfd, const char *de_name,
3619 got_worktree_status_cb status_cb, void *status_arg,
3620 struct got_repository *repo, int report_unchanged)
3622 const struct got_error *err = NULL;
3623 unsigned char status = GOT_STATUS_NO_CHANGE;
3624 unsigned char staged_status;
3625 struct stat sb;
3626 struct got_object_id blob_id, commit_id, staged_blob_id;
3627 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3628 struct got_object_id *staged_blob_idp = NULL;
3630 staged_status = get_staged_status(ie);
3631 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3632 if (err)
3633 return err;
3635 if (status == GOT_STATUS_NO_CHANGE &&
3636 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3637 return NULL;
3639 if (got_fileindex_entry_has_blob(ie))
3640 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3641 if (got_fileindex_entry_has_commit(ie))
3642 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3643 if (staged_status == GOT_STATUS_ADD ||
3644 staged_status == GOT_STATUS_MODIFY) {
3645 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3646 &staged_blob_id, ie);
3649 return (*status_cb)(status_arg, status, staged_status,
3650 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3653 static const struct got_error *
3654 status_old_new(void *arg, struct got_fileindex_entry *ie,
3655 struct dirent *de, const char *parent_path, int dirfd)
3657 const struct got_error *err = NULL;
3658 struct diff_dir_cb_arg *a = arg;
3659 char *abspath;
3661 if (a->cancel_cb) {
3662 err = a->cancel_cb(a->cancel_arg);
3663 if (err)
3664 return err;
3667 if (got_path_cmp(parent_path, a->status_path,
3668 strlen(parent_path), a->status_path_len) != 0 &&
3669 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3670 return NULL;
3672 if (parent_path[0]) {
3673 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3674 parent_path, de->d_name) == -1)
3675 return got_error_from_errno("asprintf");
3676 } else {
3677 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3678 de->d_name) == -1)
3679 return got_error_from_errno("asprintf");
3682 err = report_file_status(ie, abspath, dirfd, de->d_name,
3683 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3684 free(abspath);
3685 return err;
3688 static const struct got_error *
3689 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3691 const struct got_error *err;
3692 struct diff_dir_cb_arg *a = arg;
3693 struct got_object_id blob_id, commit_id;
3694 unsigned char status;
3696 if (a->cancel_cb) {
3697 err = a->cancel_cb(a->cancel_arg);
3698 if (err)
3699 return err;
3702 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3703 return NULL;
3705 got_fileindex_entry_get_blob_id(&blob_id, ie);
3706 got_fileindex_entry_get_commit_id(&commit_id, ie);
3707 if (got_fileindex_entry_has_file_on_disk(ie))
3708 status = GOT_STATUS_MISSING;
3709 else
3710 status = GOT_STATUS_DELETE;
3711 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3712 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3715 static void
3716 free_ignores(struct got_pathlist_head *ignores)
3718 struct got_pathlist_entry *pe;
3720 TAILQ_FOREACH(pe, ignores, entry) {
3721 struct got_pathlist_head *ignorelist = pe->data;
3723 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3725 got_pathlist_free(ignores, GOT_PATHLIST_FREE_ALL);
3728 static const struct got_error *
3729 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3731 const struct got_error *err = NULL;
3732 struct got_pathlist_entry *pe = NULL;
3733 struct got_pathlist_head *ignorelist;
3734 char *line = NULL, *pattern, *dirpath = NULL;
3735 size_t linesize = 0;
3736 ssize_t linelen;
3738 ignorelist = calloc(1, sizeof(*ignorelist));
3739 if (ignorelist == NULL)
3740 return got_error_from_errno("calloc");
3741 TAILQ_INIT(ignorelist);
3743 while ((linelen = getline(&line, &linesize, f)) != -1) {
3744 if (linelen > 0 && line[linelen - 1] == '\n')
3745 line[linelen - 1] = '\0';
3747 /* Git's ignores may contain comments. */
3748 if (line[0] == '#')
3749 continue;
3751 /* Git's negated patterns are not (yet?) supported. */
3752 if (line[0] == '!')
3753 continue;
3755 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3756 line) == -1) {
3757 err = got_error_from_errno("asprintf");
3758 goto done;
3760 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3761 if (err)
3762 goto done;
3764 if (ferror(f)) {
3765 err = got_error_from_errno("getline");
3766 goto done;
3769 dirpath = strdup(path);
3770 if (dirpath == NULL) {
3771 err = got_error_from_errno("strdup");
3772 goto done;
3774 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3775 done:
3776 free(line);
3777 if (err || pe == NULL) {
3778 free(dirpath);
3779 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3780 free(ignorelist);
3782 return err;
3785 static int
3786 match_path(const char *pattern, size_t pattern_len, const char *path,
3787 int flags)
3789 char buf[PATH_MAX];
3792 * Trailing slashes signify directories.
3793 * Append a * to make such patterns conform to fnmatch rules.
3795 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3796 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3797 return FNM_NOMATCH; /* XXX */
3799 return fnmatch(buf, path, flags);
3802 return fnmatch(pattern, path, flags);
3805 static int
3806 match_ignores(struct got_pathlist_head *ignores, const char *path)
3808 struct got_pathlist_entry *pe;
3810 /* Handle patterns which match in all directories. */
3811 TAILQ_FOREACH(pe, ignores, entry) {
3812 struct got_pathlist_head *ignorelist = pe->data;
3813 struct got_pathlist_entry *pi;
3815 TAILQ_FOREACH(pi, ignorelist, entry) {
3816 const char *p;
3818 if (pi->path_len < 3 ||
3819 strncmp(pi->path, "**/", 3) != 0)
3820 continue;
3821 p = path;
3822 while (*p) {
3823 if (match_path(pi->path + 3,
3824 pi->path_len - 3, p,
3825 FNM_PATHNAME | FNM_LEADING_DIR)) {
3826 /* Retry in next directory. */
3827 while (*p && *p != '/')
3828 p++;
3829 while (*p == '/')
3830 p++;
3831 continue;
3833 return 1;
3839 * The ignores pathlist contains ignore lists from children before
3840 * parents, so we can find the most specific ignorelist by walking
3841 * ignores backwards.
3843 pe = TAILQ_LAST(ignores, got_pathlist_head);
3844 while (pe) {
3845 if (got_path_is_child(path, pe->path, pe->path_len)) {
3846 struct got_pathlist_head *ignorelist = pe->data;
3847 struct got_pathlist_entry *pi;
3848 TAILQ_FOREACH(pi, ignorelist, entry) {
3849 int flags = FNM_LEADING_DIR;
3850 if (strstr(pi->path, "/**/") == NULL)
3851 flags |= FNM_PATHNAME;
3852 if (match_path(pi->path, pi->path_len,
3853 path, flags))
3854 continue;
3855 return 1;
3858 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3861 return 0;
3864 static const struct got_error *
3865 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3866 const char *path, int dirfd, const char *ignores_filename)
3868 const struct got_error *err = NULL;
3869 char *ignorespath;
3870 int fd = -1;
3871 FILE *ignoresfile = NULL;
3873 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3874 path[0] ? "/" : "", ignores_filename) == -1)
3875 return got_error_from_errno("asprintf");
3877 if (dirfd != -1) {
3878 fd = openat(dirfd, ignores_filename,
3879 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3880 if (fd == -1) {
3881 if (errno != ENOENT && errno != EACCES)
3882 err = got_error_from_errno2("openat",
3883 ignorespath);
3884 } else {
3885 ignoresfile = fdopen(fd, "r");
3886 if (ignoresfile == NULL)
3887 err = got_error_from_errno2("fdopen",
3888 ignorespath);
3889 else {
3890 fd = -1;
3891 err = read_ignores(ignores, path, ignoresfile);
3894 } else {
3895 ignoresfile = fopen(ignorespath, "re");
3896 if (ignoresfile == NULL) {
3897 if (errno != ENOENT && errno != EACCES)
3898 err = got_error_from_errno2("fopen",
3899 ignorespath);
3900 } else
3901 err = read_ignores(ignores, path, ignoresfile);
3904 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3905 err = got_error_from_errno2("fclose", path);
3906 if (fd != -1 && close(fd) == -1 && err == NULL)
3907 err = got_error_from_errno2("close", path);
3908 free(ignorespath);
3909 return err;
3912 static const struct got_error *
3913 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3914 int dirfd)
3916 const struct got_error *err = NULL;
3917 struct diff_dir_cb_arg *a = arg;
3918 char *path = NULL;
3920 if (ignore != NULL)
3921 *ignore = 0;
3923 if (a->cancel_cb) {
3924 err = a->cancel_cb(a->cancel_arg);
3925 if (err)
3926 return err;
3929 if (parent_path[0]) {
3930 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3931 return got_error_from_errno("asprintf");
3932 } else {
3933 path = de->d_name;
3936 if (de->d_type == DT_DIR) {
3937 if (!a->no_ignores && ignore != NULL &&
3938 match_ignores(a->ignores, path))
3939 *ignore = 1;
3940 } else if (!match_ignores(a->ignores, path) &&
3941 got_path_is_child(path, a->status_path, a->status_path_len))
3942 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3943 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3944 if (parent_path[0])
3945 free(path);
3946 return err;
3949 static const struct got_error *
3950 status_traverse(void *arg, const char *path, int dirfd)
3952 const struct got_error *err = NULL;
3953 struct diff_dir_cb_arg *a = arg;
3955 if (a->no_ignores)
3956 return NULL;
3958 err = add_ignores(a->ignores, a->worktree->root_path,
3959 path, dirfd, ".cvsignore");
3960 if (err)
3961 return err;
3963 err = add_ignores(a->ignores, a->worktree->root_path, path,
3964 dirfd, ".gitignore");
3966 return err;
3969 static const struct got_error *
3970 report_single_file_status(const char *path, const char *ondisk_path,
3971 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3972 void *status_arg, struct got_repository *repo, int report_unchanged,
3973 struct got_pathlist_head *ignores, int no_ignores)
3975 struct got_fileindex_entry *ie;
3976 struct stat sb;
3978 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3979 if (ie)
3980 return report_file_status(ie, ondisk_path, -1, NULL,
3981 status_cb, status_arg, repo, report_unchanged);
3983 if (lstat(ondisk_path, &sb) == -1) {
3984 if (errno != ENOENT)
3985 return got_error_from_errno2("lstat", ondisk_path);
3986 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3987 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3990 if (!no_ignores && match_ignores(ignores, path))
3991 return NULL;
3993 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3994 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3995 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3997 return NULL;
4000 static const struct got_error *
4001 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
4002 const char *root_path, const char *path)
4004 const struct got_error *err;
4005 char *parent_path, *next_parent_path = NULL;
4007 err = add_ignores(ignores, root_path, "", -1,
4008 ".cvsignore");
4009 if (err)
4010 return err;
4012 err = add_ignores(ignores, root_path, "", -1,
4013 ".gitignore");
4014 if (err)
4015 return err;
4017 err = got_path_dirname(&parent_path, path);
4018 if (err) {
4019 if (err->code == GOT_ERR_BAD_PATH)
4020 return NULL; /* cannot traverse parent */
4021 return err;
4023 for (;;) {
4024 err = add_ignores(ignores, root_path, parent_path, -1,
4025 ".cvsignore");
4026 if (err)
4027 break;
4028 err = add_ignores(ignores, root_path, parent_path, -1,
4029 ".gitignore");
4030 if (err)
4031 break;
4032 err = got_path_dirname(&next_parent_path, parent_path);
4033 if (err) {
4034 if (err->code == GOT_ERR_BAD_PATH)
4035 err = NULL; /* traversed everything */
4036 break;
4038 if (got_path_is_root_dir(parent_path))
4039 break;
4040 free(parent_path);
4041 parent_path = next_parent_path;
4042 next_parent_path = NULL;
4045 free(parent_path);
4046 free(next_parent_path);
4047 return err;
4050 struct find_missing_children_args {
4051 const char *parent_path;
4052 size_t parent_len;
4053 struct got_pathlist_head *children;
4054 got_cancel_cb cancel_cb;
4055 void *cancel_arg;
4058 static const struct got_error *
4059 find_missing_children(void *arg, struct got_fileindex_entry *ie)
4061 const struct got_error *err = NULL;
4062 struct find_missing_children_args *a = arg;
4064 if (a->cancel_cb) {
4065 err = a->cancel_cb(a->cancel_arg);
4066 if (err)
4067 return err;
4070 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
4071 err = got_pathlist_append(a->children, ie->path, NULL);
4073 return err;
4076 static const struct got_error *
4077 report_children(struct got_pathlist_head *children,
4078 struct got_worktree *worktree, struct got_fileindex *fileindex,
4079 struct got_repository *repo, int is_root_dir, int report_unchanged,
4080 struct got_pathlist_head *ignores, int no_ignores,
4081 got_worktree_status_cb status_cb, void *status_arg,
4082 got_cancel_cb cancel_cb, void *cancel_arg)
4084 const struct got_error *err = NULL;
4085 struct got_pathlist_entry *pe;
4086 char *ondisk_path = NULL;
4088 TAILQ_FOREACH(pe, children, entry) {
4089 if (cancel_cb) {
4090 err = cancel_cb(cancel_arg);
4091 if (err)
4092 break;
4095 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
4096 !is_root_dir ? "/" : "", pe->path) == -1) {
4097 err = got_error_from_errno("asprintf");
4098 ondisk_path = NULL;
4099 break;
4102 err = report_single_file_status(pe->path, ondisk_path,
4103 fileindex, status_cb, status_arg, repo, report_unchanged,
4104 ignores, no_ignores);
4105 if (err)
4106 break;
4108 free(ondisk_path);
4109 ondisk_path = NULL;
4112 free(ondisk_path);
4113 return err;
4116 static const struct got_error *
4117 worktree_status(struct got_worktree *worktree, const char *path,
4118 struct got_fileindex *fileindex, struct got_repository *repo,
4119 got_worktree_status_cb status_cb, void *status_arg,
4120 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
4121 int report_unchanged)
4123 const struct got_error *err = NULL;
4124 int fd = -1;
4125 struct got_fileindex_diff_dir_cb fdiff_cb;
4126 struct diff_dir_cb_arg arg;
4127 char *ondisk_path = NULL;
4128 struct got_pathlist_head ignores, missing_children;
4129 struct got_fileindex_entry *ie;
4131 TAILQ_INIT(&ignores);
4132 TAILQ_INIT(&missing_children);
4134 if (asprintf(&ondisk_path, "%s%s%s",
4135 worktree->root_path, path[0] ? "/" : "", path) == -1)
4136 return got_error_from_errno("asprintf");
4138 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
4139 if (ie) {
4140 err = report_single_file_status(path, ondisk_path,
4141 fileindex, status_cb, status_arg, repo,
4142 report_unchanged, &ignores, no_ignores);
4143 goto done;
4144 } else {
4145 struct find_missing_children_args fmca;
4146 fmca.parent_path = path;
4147 fmca.parent_len = strlen(path);
4148 fmca.children = &missing_children;
4149 fmca.cancel_cb = cancel_cb;
4150 fmca.cancel_arg = cancel_arg;
4151 err = got_fileindex_for_each_entry_safe(fileindex,
4152 find_missing_children, &fmca);
4153 if (err)
4154 goto done;
4157 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
4158 if (fd == -1) {
4159 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
4160 !got_err_open_nofollow_on_symlink())
4161 err = got_error_from_errno2("open", ondisk_path);
4162 else {
4163 if (!no_ignores) {
4164 err = add_ignores_from_parent_paths(&ignores,
4165 worktree->root_path, ondisk_path);
4166 if (err)
4167 goto done;
4169 if (TAILQ_EMPTY(&missing_children)) {
4170 err = report_single_file_status(path,
4171 ondisk_path, fileindex,
4172 status_cb, status_arg, repo,
4173 report_unchanged, &ignores, no_ignores);
4174 if (err)
4175 goto done;
4176 } else {
4177 err = report_children(&missing_children,
4178 worktree, fileindex, repo,
4179 (path[0] == '\0'), report_unchanged,
4180 &ignores, no_ignores,
4181 status_cb, status_arg,
4182 cancel_cb, cancel_arg);
4183 if (err)
4184 goto done;
4187 } else {
4188 fdiff_cb.diff_old_new = status_old_new;
4189 fdiff_cb.diff_old = status_old;
4190 fdiff_cb.diff_new = status_new;
4191 fdiff_cb.diff_traverse = status_traverse;
4192 arg.fileindex = fileindex;
4193 arg.worktree = worktree;
4194 arg.status_path = path;
4195 arg.status_path_len = strlen(path);
4196 arg.repo = repo;
4197 arg.status_cb = status_cb;
4198 arg.status_arg = status_arg;
4199 arg.cancel_cb = cancel_cb;
4200 arg.cancel_arg = cancel_arg;
4201 arg.report_unchanged = report_unchanged;
4202 arg.no_ignores = no_ignores;
4203 if (!no_ignores) {
4204 err = add_ignores_from_parent_paths(&ignores,
4205 worktree->root_path, path);
4206 if (err)
4207 goto done;
4209 arg.ignores = &ignores;
4210 err = got_fileindex_diff_dir(fileindex, fd,
4211 worktree->root_path, path, repo, &fdiff_cb, &arg);
4213 done:
4214 free_ignores(&ignores);
4215 got_pathlist_free(&missing_children, GOT_PATHLIST_FREE_NONE);
4216 if (fd != -1 && close(fd) == -1 && err == NULL)
4217 err = got_error_from_errno("close");
4218 free(ondisk_path);
4219 return err;
4222 const struct got_error *
4223 got_worktree_status(struct got_worktree *worktree,
4224 struct got_pathlist_head *paths, struct got_repository *repo,
4225 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4226 got_cancel_cb cancel_cb, void *cancel_arg)
4228 const struct got_error *err = NULL;
4229 char *fileindex_path = NULL;
4230 struct got_fileindex *fileindex = NULL;
4231 struct got_pathlist_entry *pe;
4233 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4234 if (err)
4235 return err;
4237 TAILQ_FOREACH(pe, paths, entry) {
4238 err = worktree_status(worktree, pe->path, fileindex, repo,
4239 status_cb, status_arg, cancel_cb, cancel_arg,
4240 no_ignores, 0);
4241 if (err)
4242 break;
4244 free(fileindex_path);
4245 got_fileindex_free(fileindex);
4246 return err;
4249 const struct got_error *
4250 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4251 const char *arg)
4253 const struct got_error *err = NULL;
4254 char *resolved = NULL, *cwd = NULL, *path = NULL;
4255 size_t len;
4256 struct stat sb;
4257 char *abspath = NULL;
4258 char canonpath[PATH_MAX];
4260 *wt_path = NULL;
4262 cwd = getcwd(NULL, 0);
4263 if (cwd == NULL)
4264 return got_error_from_errno("getcwd");
4266 if (lstat(arg, &sb) == -1) {
4267 if (errno != ENOENT) {
4268 err = got_error_from_errno2("lstat", arg);
4269 goto done;
4271 sb.st_mode = 0;
4273 if (S_ISLNK(sb.st_mode)) {
4275 * We cannot use realpath(3) with symlinks since we want to
4276 * operate on the symlink itself.
4277 * But we can make the path absolute, assuming it is relative
4278 * to the current working directory, and then canonicalize it.
4280 if (!got_path_is_absolute(arg)) {
4281 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4282 err = got_error_from_errno("asprintf");
4283 goto done;
4287 err = got_canonpath(abspath ? abspath : arg, canonpath,
4288 sizeof(canonpath));
4289 if (err)
4290 goto done;
4291 resolved = strdup(canonpath);
4292 if (resolved == NULL) {
4293 err = got_error_from_errno("strdup");
4294 goto done;
4296 } else {
4297 resolved = realpath(arg, NULL);
4298 if (resolved == NULL) {
4299 if (errno != ENOENT) {
4300 err = got_error_from_errno2("realpath", arg);
4301 goto done;
4303 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4304 err = got_error_from_errno("asprintf");
4305 goto done;
4307 err = got_canonpath(abspath, canonpath,
4308 sizeof(canonpath));
4309 if (err)
4310 goto done;
4311 resolved = strdup(canonpath);
4312 if (resolved == NULL) {
4313 err = got_error_from_errno("strdup");
4314 goto done;
4319 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4320 strlen(got_worktree_get_root_path(worktree)))) {
4321 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4322 goto done;
4325 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4326 err = got_path_skip_common_ancestor(&path,
4327 got_worktree_get_root_path(worktree), resolved);
4328 if (err)
4329 goto done;
4330 } else {
4331 path = strdup("");
4332 if (path == NULL) {
4333 err = got_error_from_errno("strdup");
4334 goto done;
4338 /* XXX status walk can't deal with trailing slash! */
4339 len = strlen(path);
4340 while (len > 0 && path[len - 1] == '/') {
4341 path[len - 1] = '\0';
4342 len--;
4344 done:
4345 free(abspath);
4346 free(resolved);
4347 free(cwd);
4348 if (err == NULL)
4349 *wt_path = path;
4350 else
4351 free(path);
4352 return err;
4355 struct schedule_addition_args {
4356 struct got_worktree *worktree;
4357 struct got_fileindex *fileindex;
4358 got_worktree_checkout_cb progress_cb;
4359 void *progress_arg;
4360 struct got_repository *repo;
4363 static int
4364 add_noop_status(unsigned char status)
4366 return (status == GOT_STATUS_ADD ||
4367 status == GOT_STATUS_MODIFY ||
4368 status == GOT_STATUS_CONFLICT ||
4369 status == GOT_STATUS_MODE_CHANGE ||
4370 status == GOT_STATUS_NO_CHANGE);
4373 static const struct got_error *
4374 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4375 const char *relpath, struct got_object_id *blob_id,
4376 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4377 int dirfd, const char *de_name)
4379 struct schedule_addition_args *a = arg;
4380 const struct got_error *err = NULL;
4381 struct got_fileindex_entry *ie;
4382 struct stat sb;
4383 char *ondisk_path;
4385 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4386 relpath) == -1)
4387 return got_error_from_errno("asprintf");
4389 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4390 if (ie) {
4391 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4392 de_name, a->repo);
4393 if (err)
4394 goto done;
4395 /* Re-adding an existing entry is a no-op. */
4396 if (staged_status == GOT_STATUS_NO_CHANGE &&
4397 add_noop_status(status))
4398 goto done;
4399 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4400 if (err)
4401 goto done;
4404 if (status != GOT_STATUS_UNVERSIONED) {
4405 if (status == GOT_STATUS_NONEXISTENT)
4406 err = got_error_set_errno(ENOENT, ondisk_path);
4407 else
4408 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4409 goto done;
4412 err = got_fileindex_entry_alloc(&ie, relpath);
4413 if (err)
4414 goto done;
4415 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4416 relpath, NULL, NULL, 1);
4417 if (err) {
4418 got_fileindex_entry_free(ie);
4419 goto done;
4421 err = got_fileindex_entry_add(a->fileindex, ie);
4422 if (err) {
4423 got_fileindex_entry_free(ie);
4424 goto done;
4426 done:
4427 free(ondisk_path);
4428 if (err)
4429 return err;
4430 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4431 return NULL;
4432 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4435 const struct got_error *
4436 got_worktree_schedule_add(struct got_worktree *worktree,
4437 struct got_pathlist_head *paths,
4438 got_worktree_checkout_cb progress_cb, void *progress_arg,
4439 struct got_repository *repo, int no_ignores)
4441 struct got_fileindex *fileindex = NULL;
4442 char *fileindex_path = NULL;
4443 const struct got_error *err = NULL, *sync_err, *unlockerr;
4444 struct got_pathlist_entry *pe;
4445 struct schedule_addition_args saa;
4447 err = lock_worktree(worktree, LOCK_EX);
4448 if (err)
4449 return err;
4451 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4452 if (err)
4453 goto done;
4455 saa.worktree = worktree;
4456 saa.fileindex = fileindex;
4457 saa.progress_cb = progress_cb;
4458 saa.progress_arg = progress_arg;
4459 saa.repo = repo;
4461 TAILQ_FOREACH(pe, paths, entry) {
4462 err = worktree_status(worktree, pe->path, fileindex, repo,
4463 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4464 if (err)
4465 break;
4467 sync_err = sync_fileindex(fileindex, fileindex_path);
4468 if (sync_err && err == NULL)
4469 err = sync_err;
4470 done:
4471 free(fileindex_path);
4472 if (fileindex)
4473 got_fileindex_free(fileindex);
4474 unlockerr = lock_worktree(worktree, LOCK_SH);
4475 if (unlockerr && err == NULL)
4476 err = unlockerr;
4477 return err;
4480 struct schedule_deletion_args {
4481 struct got_worktree *worktree;
4482 struct got_fileindex *fileindex;
4483 got_worktree_delete_cb progress_cb;
4484 void *progress_arg;
4485 struct got_repository *repo;
4486 int delete_local_mods;
4487 int keep_on_disk;
4488 int ignore_missing_paths;
4489 const char *status_path;
4490 size_t status_path_len;
4491 const char *status_codes;
4494 static const struct got_error *
4495 schedule_for_deletion(void *arg, unsigned char status,
4496 unsigned char staged_status, const char *relpath,
4497 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4498 struct got_object_id *commit_id, int dirfd, const char *de_name)
4500 struct schedule_deletion_args *a = arg;
4501 const struct got_error *err = NULL;
4502 struct got_fileindex_entry *ie = NULL;
4503 struct stat sb;
4504 char *ondisk_path;
4506 if (status == GOT_STATUS_NONEXISTENT) {
4507 if (a->ignore_missing_paths)
4508 return NULL;
4509 return got_error_set_errno(ENOENT, relpath);
4512 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4513 if (ie == NULL)
4514 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4516 staged_status = get_staged_status(ie);
4517 if (staged_status != GOT_STATUS_NO_CHANGE) {
4518 if (staged_status == GOT_STATUS_DELETE)
4519 return NULL;
4520 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4523 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4524 relpath) == -1)
4525 return got_error_from_errno("asprintf");
4527 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4528 a->repo);
4529 if (err)
4530 goto done;
4532 if (a->status_codes) {
4533 size_t ncodes = strlen(a->status_codes);
4534 int i;
4535 for (i = 0; i < ncodes ; i++) {
4536 if (status == a->status_codes[i])
4537 break;
4539 if (i == ncodes) {
4540 /* Do not delete files in non-matching status. */
4541 free(ondisk_path);
4542 return NULL;
4544 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4545 a->status_codes[i] != GOT_STATUS_MISSING) {
4546 static char msg[64];
4547 snprintf(msg, sizeof(msg),
4548 "invalid status code '%c'", a->status_codes[i]);
4549 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4550 goto done;
4554 if (status != GOT_STATUS_NO_CHANGE) {
4555 if (status == GOT_STATUS_DELETE)
4556 goto done;
4557 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4558 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4559 goto done;
4561 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4562 err = got_error_set_errno(ENOENT, relpath);
4563 goto done;
4565 if (status != GOT_STATUS_MODIFY &&
4566 status != GOT_STATUS_MISSING) {
4567 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4568 goto done;
4572 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4573 size_t root_len;
4575 if (dirfd != -1) {
4576 if (unlinkat(dirfd, de_name, 0) == -1) {
4577 err = got_error_from_errno2("unlinkat",
4578 ondisk_path);
4579 goto done;
4581 } else if (unlink(ondisk_path) == -1) {
4582 err = got_error_from_errno2("unlink", ondisk_path);
4583 goto done;
4586 root_len = strlen(a->worktree->root_path);
4587 do {
4588 char *parent;
4590 err = got_path_dirname(&parent, ondisk_path);
4591 if (err)
4592 goto done;
4593 free(ondisk_path);
4594 ondisk_path = parent;
4595 if (got_path_cmp(ondisk_path, a->status_path,
4596 strlen(ondisk_path), a->status_path_len) != 0 &&
4597 !got_path_is_child(ondisk_path, a->status_path,
4598 a->status_path_len))
4599 break;
4600 if (rmdir(ondisk_path) == -1) {
4601 if (errno != ENOTEMPTY)
4602 err = got_error_from_errno2("rmdir",
4603 ondisk_path);
4604 break;
4606 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4607 strlen(ondisk_path), root_len) != 0);
4610 if (got_fileindex_entry_has_blob(ie))
4611 got_fileindex_entry_mark_deleted_from_disk(ie);
4612 else
4613 got_fileindex_entry_remove(a->fileindex, ie);
4614 done:
4615 free(ondisk_path);
4616 if (err)
4617 return err;
4618 if (status == GOT_STATUS_DELETE)
4619 return NULL;
4620 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4621 staged_status, relpath);
4624 const struct got_error *
4625 got_worktree_schedule_delete(struct got_worktree *worktree,
4626 struct got_pathlist_head *paths, int delete_local_mods,
4627 const char *status_codes,
4628 got_worktree_delete_cb progress_cb, void *progress_arg,
4629 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4631 struct got_fileindex *fileindex = NULL;
4632 char *fileindex_path = NULL;
4633 const struct got_error *err = NULL, *sync_err, *unlockerr;
4634 struct got_pathlist_entry *pe;
4635 struct schedule_deletion_args sda;
4637 err = lock_worktree(worktree, LOCK_EX);
4638 if (err)
4639 return err;
4641 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4642 if (err)
4643 goto done;
4645 sda.worktree = worktree;
4646 sda.fileindex = fileindex;
4647 sda.progress_cb = progress_cb;
4648 sda.progress_arg = progress_arg;
4649 sda.repo = repo;
4650 sda.delete_local_mods = delete_local_mods;
4651 sda.keep_on_disk = keep_on_disk;
4652 sda.ignore_missing_paths = ignore_missing_paths;
4653 sda.status_codes = status_codes;
4655 TAILQ_FOREACH(pe, paths, entry) {
4656 char *ondisk_status_path;
4658 if (asprintf(&ondisk_status_path, "%s%s%s",
4659 got_worktree_get_root_path(worktree),
4660 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4661 err = got_error_from_errno("asprintf");
4662 goto done;
4664 sda.status_path = ondisk_status_path;
4665 sda.status_path_len = strlen(ondisk_status_path);
4666 err = worktree_status(worktree, pe->path, fileindex, repo,
4667 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4668 free(ondisk_status_path);
4669 if (err)
4670 break;
4672 sync_err = sync_fileindex(fileindex, fileindex_path);
4673 if (sync_err && err == NULL)
4674 err = sync_err;
4675 done:
4676 free(fileindex_path);
4677 if (fileindex)
4678 got_fileindex_free(fileindex);
4679 unlockerr = lock_worktree(worktree, LOCK_SH);
4680 if (unlockerr && err == NULL)
4681 err = unlockerr;
4682 return err;
4685 static const struct got_error *
4686 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4688 const struct got_error *err = NULL;
4689 char *line = NULL;
4690 size_t linesize = 0, n;
4691 ssize_t linelen;
4693 linelen = getline(&line, &linesize, infile);
4694 if (linelen == -1) {
4695 if (ferror(infile)) {
4696 err = got_error_from_errno("getline");
4697 goto done;
4699 return NULL;
4701 if (outfile) {
4702 n = fwrite(line, 1, linelen, outfile);
4703 if (n != linelen) {
4704 err = got_ferror(outfile, GOT_ERR_IO);
4705 goto done;
4708 if (rejectfile) {
4709 n = fwrite(line, 1, linelen, rejectfile);
4710 if (n != linelen)
4711 err = got_ferror(rejectfile, GOT_ERR_IO);
4713 done:
4714 free(line);
4715 return err;
4718 static const struct got_error *
4719 skip_one_line(FILE *f)
4721 char *line = NULL;
4722 size_t linesize = 0;
4723 ssize_t linelen;
4725 linelen = getline(&line, &linesize, f);
4726 if (linelen == -1) {
4727 if (ferror(f))
4728 return got_error_from_errno("getline");
4729 return NULL;
4731 free(line);
4732 return NULL;
4735 static const struct got_error *
4736 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4737 int start_old, int end_old, int start_new, int end_new,
4738 FILE *outfile, FILE *rejectfile)
4740 const struct got_error *err;
4742 /* Copy old file's lines leading up to patch. */
4743 while (!feof(f1) && *line_cur1 < start_old) {
4744 err = copy_one_line(f1, outfile, NULL);
4745 if (err)
4746 return err;
4747 (*line_cur1)++;
4749 /* Skip new file's lines leading up to patch. */
4750 while (!feof(f2) && *line_cur2 < start_new) {
4751 if (rejectfile)
4752 err = copy_one_line(f2, NULL, rejectfile);
4753 else
4754 err = skip_one_line(f2);
4755 if (err)
4756 return err;
4757 (*line_cur2)++;
4759 /* Copy patched lines. */
4760 while (!feof(f2) && *line_cur2 <= end_new) {
4761 err = copy_one_line(f2, outfile, NULL);
4762 if (err)
4763 return err;
4764 (*line_cur2)++;
4766 /* Skip over old file's replaced lines. */
4767 while (!feof(f1) && *line_cur1 <= end_old) {
4768 if (rejectfile)
4769 err = copy_one_line(f1, NULL, rejectfile);
4770 else
4771 err = skip_one_line(f1);
4772 if (err)
4773 return err;
4774 (*line_cur1)++;
4777 return NULL;
4780 static const struct got_error *
4781 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4782 FILE *outfile, FILE *rejectfile)
4784 const struct got_error *err;
4786 if (outfile) {
4787 /* Copy old file's lines until EOF. */
4788 while (!feof(f1)) {
4789 err = copy_one_line(f1, outfile, NULL);
4790 if (err)
4791 return err;
4792 (*line_cur1)++;
4795 if (rejectfile) {
4796 /* Copy new file's lines until EOF. */
4797 while (!feof(f2)) {
4798 err = copy_one_line(f2, NULL, rejectfile);
4799 if (err)
4800 return err;
4801 (*line_cur2)++;
4805 return NULL;
4808 static const struct got_error *
4809 apply_or_reject_change(int *choice, int *nchunks_used,
4810 struct diff_result *diff_result, int n,
4811 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4812 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4813 got_worktree_patch_cb patch_cb, void *patch_arg)
4815 const struct got_error *err = NULL;
4816 struct diff_chunk_context cc = {};
4817 int start_old, end_old, start_new, end_new;
4818 FILE *hunkfile;
4819 struct diff_output_unidiff_state *diff_state;
4820 struct diff_input_info diff_info;
4821 int rc;
4823 *choice = GOT_PATCH_CHOICE_NONE;
4825 /* Get changed line numbers without context lines for copy_change(). */
4826 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4827 start_old = cc.left.start;
4828 end_old = cc.left.end;
4829 start_new = cc.right.start;
4830 end_new = cc.right.end;
4832 /* Get the same change with context lines for display. */
4833 memset(&cc, 0, sizeof(cc));
4834 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4836 memset(&diff_info, 0, sizeof(diff_info));
4837 diff_info.left_path = relpath;
4838 diff_info.right_path = relpath;
4840 diff_state = diff_output_unidiff_state_alloc();
4841 if (diff_state == NULL)
4842 return got_error_set_errno(ENOMEM,
4843 "diff_output_unidiff_state_alloc");
4845 hunkfile = got_opentemp();
4846 if (hunkfile == NULL) {
4847 err = got_error_from_errno("got_opentemp");
4848 goto done;
4851 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4852 diff_result, &cc);
4853 if (rc != DIFF_RC_OK) {
4854 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4855 goto done;
4858 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4859 err = got_ferror(hunkfile, GOT_ERR_IO);
4860 goto done;
4863 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4864 hunkfile, changeno, nchanges);
4865 if (err)
4866 goto done;
4868 switch (*choice) {
4869 case GOT_PATCH_CHOICE_YES:
4870 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4871 end_old, start_new, end_new, outfile, rejectfile);
4872 break;
4873 case GOT_PATCH_CHOICE_NO:
4874 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4875 end_old, start_new, end_new, rejectfile, outfile);
4876 break;
4877 case GOT_PATCH_CHOICE_QUIT:
4878 break;
4879 default:
4880 err = got_error(GOT_ERR_PATCH_CHOICE);
4881 break;
4883 done:
4884 diff_output_unidiff_state_free(diff_state);
4885 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4886 err = got_error_from_errno("fclose");
4887 return err;
4890 struct revert_file_args {
4891 struct got_worktree *worktree;
4892 struct got_fileindex *fileindex;
4893 got_worktree_checkout_cb progress_cb;
4894 void *progress_arg;
4895 got_worktree_patch_cb patch_cb;
4896 void *patch_arg;
4897 struct got_repository *repo;
4898 int unlink_added_files;
4899 struct got_pathlist_head *added_files_to_unlink;
4902 static const struct got_error *
4903 create_patched_content(char **path_outfile, int reverse_patch,
4904 struct got_object_id *blob_id, const char *path2,
4905 int dirfd2, const char *de_name2,
4906 const char *relpath, struct got_repository *repo,
4907 got_worktree_patch_cb patch_cb, void *patch_arg)
4909 const struct got_error *err, *free_err;
4910 struct got_blob_object *blob = NULL;
4911 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4912 int fd = -1, fd2 = -1;
4913 char link_target[PATH_MAX];
4914 ssize_t link_len = 0;
4915 char *path1 = NULL, *id_str = NULL;
4916 struct stat sb2;
4917 struct got_diffreg_result *diffreg_result = NULL;
4918 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4919 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4921 *path_outfile = NULL;
4923 err = got_object_id_str(&id_str, blob_id);
4924 if (err)
4925 return err;
4927 if (dirfd2 != -1) {
4928 fd2 = openat(dirfd2, de_name2,
4929 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4930 if (fd2 == -1) {
4931 if (!got_err_open_nofollow_on_symlink()) {
4932 err = got_error_from_errno2("openat", path2);
4933 goto done;
4935 link_len = readlinkat(dirfd2, de_name2,
4936 link_target, sizeof(link_target));
4937 if (link_len == -1) {
4938 return got_error_from_errno2("readlinkat",
4939 path2);
4941 sb2.st_mode = S_IFLNK;
4942 sb2.st_size = link_len;
4944 } else {
4945 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4946 if (fd2 == -1) {
4947 if (!got_err_open_nofollow_on_symlink()) {
4948 err = got_error_from_errno2("open", path2);
4949 goto done;
4951 link_len = readlink(path2, link_target,
4952 sizeof(link_target));
4953 if (link_len == -1)
4954 return got_error_from_errno2("readlink", path2);
4955 sb2.st_mode = S_IFLNK;
4956 sb2.st_size = link_len;
4959 if (fd2 != -1) {
4960 if (fstat(fd2, &sb2) == -1) {
4961 err = got_error_from_errno2("fstat", path2);
4962 goto done;
4965 f2 = fdopen(fd2, "r");
4966 if (f2 == NULL) {
4967 err = got_error_from_errno2("fdopen", path2);
4968 goto done;
4970 fd2 = -1;
4971 } else {
4972 size_t n;
4973 f2 = got_opentemp();
4974 if (f2 == NULL) {
4975 err = got_error_from_errno2("got_opentemp", path2);
4976 goto done;
4978 n = fwrite(link_target, 1, link_len, f2);
4979 if (n != link_len) {
4980 err = got_ferror(f2, GOT_ERR_IO);
4981 goto done;
4983 if (fflush(f2) == EOF) {
4984 err = got_error_from_errno("fflush");
4985 goto done;
4987 rewind(f2);
4990 fd = got_opentempfd();
4991 if (fd == -1) {
4992 err = got_error_from_errno("got_opentempfd");
4993 goto done;
4996 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4997 if (err)
4998 goto done;
5000 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
5001 if (err)
5002 goto done;
5004 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
5005 if (err)
5006 goto done;
5008 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
5009 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
5010 if (err)
5011 goto done;
5013 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
5014 "");
5015 if (err)
5016 goto done;
5018 if (fseek(f1, 0L, SEEK_SET) == -1)
5019 return got_ferror(f1, GOT_ERR_IO);
5020 if (fseek(f2, 0L, SEEK_SET) == -1)
5021 return got_ferror(f2, GOT_ERR_IO);
5023 /* Count the number of actual changes in the diff result. */
5024 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5025 struct diff_chunk_context cc = {};
5026 diff_chunk_context_load_change(&cc, &nchunks_used,
5027 diffreg_result->result, n, 0);
5028 nchanges++;
5030 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
5031 int choice;
5032 err = apply_or_reject_change(&choice, &nchunks_used,
5033 diffreg_result->result, n, relpath, f1, f2,
5034 &line_cur1, &line_cur2,
5035 reverse_patch ? NULL : outfile,
5036 reverse_patch ? outfile : NULL,
5037 ++i, nchanges, patch_cb, patch_arg);
5038 if (err)
5039 goto done;
5040 if (choice == GOT_PATCH_CHOICE_YES)
5041 have_content = 1;
5042 else if (choice == GOT_PATCH_CHOICE_QUIT)
5043 break;
5045 if (have_content) {
5046 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
5047 reverse_patch ? NULL : outfile,
5048 reverse_patch ? outfile : NULL);
5049 if (err)
5050 goto done;
5052 if (!S_ISLNK(sb2.st_mode)) {
5053 mode_t mode;
5055 mode = apply_umask(sb2.st_mode);
5056 if (fchmod(fileno(outfile), mode) == -1) {
5057 err = got_error_from_errno2("fchmod", path2);
5058 goto done;
5062 done:
5063 free(id_str);
5064 if (fd != -1 && close(fd) == -1 && err == NULL)
5065 err = got_error_from_errno("close");
5066 if (blob)
5067 got_object_blob_close(blob);
5068 free_err = got_diffreg_result_free(diffreg_result);
5069 if (err == NULL)
5070 err = free_err;
5071 if (f1 && fclose(f1) == EOF && err == NULL)
5072 err = got_error_from_errno2("fclose", path1);
5073 if (f2 && fclose(f2) == EOF && err == NULL)
5074 err = got_error_from_errno2("fclose", path2);
5075 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5076 err = got_error_from_errno2("close", path2);
5077 if (outfile && fclose(outfile) == EOF && err == NULL)
5078 err = got_error_from_errno2("fclose", *path_outfile);
5079 if (path1 && unlink(path1) == -1 && err == NULL)
5080 err = got_error_from_errno2("unlink", path1);
5081 if (err || !have_content) {
5082 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
5083 err = got_error_from_errno2("unlink", *path_outfile);
5084 free(*path_outfile);
5085 *path_outfile = NULL;
5087 free(path1);
5088 return err;
5091 static const struct got_error *
5092 revert_file(void *arg, unsigned char status, unsigned char staged_status,
5093 const char *relpath, struct got_object_id *blob_id,
5094 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5095 int dirfd, const char *de_name)
5097 struct revert_file_args *a = arg;
5098 const struct got_error *err = NULL;
5099 char *parent_path = NULL;
5100 struct got_fileindex_entry *ie;
5101 struct got_commit_object *base_commit = NULL;
5102 struct got_tree_object *tree = NULL;
5103 struct got_object_id *tree_id = NULL;
5104 const struct got_tree_entry *te = NULL;
5105 char *tree_path = NULL, *te_name;
5106 char *ondisk_path = NULL, *path_content = NULL;
5107 struct got_blob_object *blob = NULL;
5108 int fd = -1;
5110 /* Reverting a staged deletion is a no-op. */
5111 if (status == GOT_STATUS_DELETE &&
5112 staged_status != GOT_STATUS_NO_CHANGE)
5113 return NULL;
5115 if (status == GOT_STATUS_UNVERSIONED)
5116 return (*a->progress_cb)(a->progress_arg,
5117 GOT_STATUS_UNVERSIONED, relpath);
5119 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5120 if (ie == NULL)
5121 return got_error_path(relpath, GOT_ERR_BAD_PATH);
5123 /* Construct in-repository path of tree which contains this blob. */
5124 err = got_path_dirname(&parent_path, ie->path);
5125 if (err) {
5126 if (err->code != GOT_ERR_BAD_PATH)
5127 goto done;
5128 parent_path = strdup("/");
5129 if (parent_path == NULL) {
5130 err = got_error_from_errno("strdup");
5131 goto done;
5134 if (got_path_is_root_dir(a->worktree->path_prefix)) {
5135 tree_path = strdup(parent_path);
5136 if (tree_path == NULL) {
5137 err = got_error_from_errno("strdup");
5138 goto done;
5140 } else {
5141 if (got_path_is_root_dir(parent_path)) {
5142 tree_path = strdup(a->worktree->path_prefix);
5143 if (tree_path == NULL) {
5144 err = got_error_from_errno("strdup");
5145 goto done;
5147 } else {
5148 if (asprintf(&tree_path, "%s/%s",
5149 a->worktree->path_prefix, parent_path) == -1) {
5150 err = got_error_from_errno("asprintf");
5151 goto done;
5156 err = got_object_open_as_commit(&base_commit, a->repo,
5157 a->worktree->base_commit_id);
5158 if (err)
5159 goto done;
5161 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
5162 if (err) {
5163 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
5164 (status == GOT_STATUS_ADD ||
5165 staged_status == GOT_STATUS_ADD)))
5166 goto done;
5167 } else {
5168 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5169 if (err)
5170 goto done;
5172 err = got_path_basename(&te_name, ie->path);
5173 if (err)
5174 goto done;
5176 te = got_object_tree_find_entry(tree, te_name);
5177 free(te_name);
5178 if (te == NULL && status != GOT_STATUS_ADD &&
5179 staged_status != GOT_STATUS_ADD) {
5180 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5181 goto done;
5185 switch (status) {
5186 case GOT_STATUS_ADD:
5187 if (a->patch_cb) {
5188 int choice = GOT_PATCH_CHOICE_NONE;
5189 err = (*a->patch_cb)(&choice, a->patch_arg,
5190 status, ie->path, NULL, 1, 1);
5191 if (err)
5192 goto done;
5193 if (choice != GOT_PATCH_CHOICE_YES)
5194 break;
5196 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5197 ie->path);
5198 if (err)
5199 goto done;
5200 got_fileindex_entry_remove(a->fileindex, ie);
5201 if (a->unlink_added_files) {
5202 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5204 if (a->added_files_to_unlink) {
5205 struct got_pathlist_entry *pe;
5207 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5208 entry) {
5209 if (got_path_cmp(pe->path, relpath,
5210 pe->path_len, strlen(relpath)))
5211 continue;
5212 do_unlink = 1;
5213 break;
5217 if (do_unlink) {
5218 if (asprintf(&ondisk_path, "%s/%s",
5219 got_worktree_get_root_path(a->worktree),
5220 relpath) == -1) {
5221 err = got_error_from_errno("asprintf");
5222 goto done;
5224 if (unlink(ondisk_path) == -1) {
5225 err = got_error_from_errno2("unlink",
5226 ondisk_path);
5227 break;
5231 break;
5232 case GOT_STATUS_DELETE:
5233 if (a->patch_cb) {
5234 int choice = GOT_PATCH_CHOICE_NONE;
5235 err = (*a->patch_cb)(&choice, a->patch_arg,
5236 status, ie->path, NULL, 1, 1);
5237 if (err)
5238 goto done;
5239 if (choice != GOT_PATCH_CHOICE_YES)
5240 break;
5242 /* fall through */
5243 case GOT_STATUS_MODIFY:
5244 case GOT_STATUS_MODE_CHANGE:
5245 case GOT_STATUS_CONFLICT:
5246 case GOT_STATUS_MISSING: {
5247 struct got_object_id id;
5248 if (staged_status == GOT_STATUS_ADD ||
5249 staged_status == GOT_STATUS_MODIFY)
5250 got_fileindex_entry_get_staged_blob_id(&id, ie);
5251 else
5252 got_fileindex_entry_get_blob_id(&id, ie);
5253 fd = got_opentempfd();
5254 if (fd == -1) {
5255 err = got_error_from_errno("got_opentempfd");
5256 goto done;
5259 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5260 if (err)
5261 goto done;
5263 if (asprintf(&ondisk_path, "%s/%s",
5264 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5265 err = got_error_from_errno("asprintf");
5266 goto done;
5269 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5270 status == GOT_STATUS_CONFLICT)) {
5271 int is_bad_symlink = 0;
5272 err = create_patched_content(&path_content, 1, &id,
5273 ondisk_path, dirfd, de_name, ie->path, a->repo,
5274 a->patch_cb, a->patch_arg);
5275 if (err || path_content == NULL)
5276 break;
5277 if (te && S_ISLNK(te->mode)) {
5278 if (unlink(path_content) == -1) {
5279 err = got_error_from_errno2("unlink",
5280 path_content);
5281 break;
5283 err = install_symlink(&is_bad_symlink,
5284 a->worktree, ondisk_path, ie->path,
5285 blob, 0, 1, 0, 0, a->repo,
5286 a->progress_cb, a->progress_arg);
5287 } else {
5288 if (rename(path_content, ondisk_path) == -1) {
5289 err = got_error_from_errno3("rename",
5290 path_content, ondisk_path);
5291 goto done;
5294 } else {
5295 int is_bad_symlink = 0;
5296 if (te && S_ISLNK(te->mode)) {
5297 err = install_symlink(&is_bad_symlink,
5298 a->worktree, ondisk_path, ie->path,
5299 blob, 0, 1, 0, 0, a->repo,
5300 a->progress_cb, a->progress_arg);
5301 } else {
5302 err = install_blob(a->worktree, ondisk_path,
5303 ie->path,
5304 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5305 got_fileindex_perms_to_st(ie), blob,
5306 0, 1, 0, 0, NULL, a->repo,
5307 a->progress_cb, a->progress_arg);
5309 if (err)
5310 goto done;
5311 if (status == GOT_STATUS_DELETE ||
5312 status == GOT_STATUS_MODE_CHANGE) {
5313 err = got_fileindex_entry_update(ie,
5314 a->worktree->root_fd, relpath,
5315 blob->id.sha1,
5316 a->worktree->base_commit_id->sha1, 1);
5317 if (err)
5318 goto done;
5320 if (is_bad_symlink) {
5321 got_fileindex_entry_filetype_set(ie,
5322 GOT_FILEIDX_MODE_BAD_SYMLINK);
5325 break;
5327 default:
5328 break;
5330 done:
5331 free(ondisk_path);
5332 free(path_content);
5333 free(parent_path);
5334 free(tree_path);
5335 if (fd != -1 && close(fd) == -1 && err == NULL)
5336 err = got_error_from_errno("close");
5337 if (blob)
5338 got_object_blob_close(blob);
5339 if (tree)
5340 got_object_tree_close(tree);
5341 free(tree_id);
5342 if (base_commit)
5343 got_object_commit_close(base_commit);
5344 return err;
5347 const struct got_error *
5348 got_worktree_revert(struct got_worktree *worktree,
5349 struct got_pathlist_head *paths,
5350 got_worktree_checkout_cb progress_cb, void *progress_arg,
5351 got_worktree_patch_cb patch_cb, void *patch_arg,
5352 struct got_repository *repo)
5354 struct got_fileindex *fileindex = NULL;
5355 char *fileindex_path = NULL;
5356 const struct got_error *err = NULL, *unlockerr = NULL;
5357 const struct got_error *sync_err = NULL;
5358 struct got_pathlist_entry *pe;
5359 struct revert_file_args rfa;
5361 err = lock_worktree(worktree, LOCK_EX);
5362 if (err)
5363 return err;
5365 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5366 if (err)
5367 goto done;
5369 rfa.worktree = worktree;
5370 rfa.fileindex = fileindex;
5371 rfa.progress_cb = progress_cb;
5372 rfa.progress_arg = progress_arg;
5373 rfa.patch_cb = patch_cb;
5374 rfa.patch_arg = patch_arg;
5375 rfa.repo = repo;
5376 rfa.unlink_added_files = 0;
5377 TAILQ_FOREACH(pe, paths, entry) {
5378 err = worktree_status(worktree, pe->path, fileindex, repo,
5379 revert_file, &rfa, NULL, NULL, 1, 0);
5380 if (err)
5381 break;
5383 sync_err = sync_fileindex(fileindex, fileindex_path);
5384 if (sync_err && err == NULL)
5385 err = sync_err;
5386 done:
5387 free(fileindex_path);
5388 if (fileindex)
5389 got_fileindex_free(fileindex);
5390 unlockerr = lock_worktree(worktree, LOCK_SH);
5391 if (unlockerr && err == NULL)
5392 err = unlockerr;
5393 return err;
5396 static void
5397 free_commitable(struct got_commitable *ct)
5399 free(ct->path);
5400 free(ct->in_repo_path);
5401 free(ct->ondisk_path);
5402 free(ct->blob_id);
5403 free(ct->base_blob_id);
5404 free(ct->staged_blob_id);
5405 free(ct->base_commit_id);
5406 free(ct);
5409 struct collect_commitables_arg {
5410 struct got_pathlist_head *commitable_paths;
5411 struct got_repository *repo;
5412 struct got_worktree *worktree;
5413 struct got_fileindex *fileindex;
5414 int have_staged_files;
5415 int allow_bad_symlinks;
5416 int diff_header_shown;
5417 int commit_conflicts;
5418 FILE *diff_outfile;
5419 FILE *f1;
5420 FILE *f2;
5424 * Create a file which contains the target path of a symlink so we can feed
5425 * it as content to the diff engine.
5427 static const struct got_error *
5428 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5429 const char *abspath)
5431 const struct got_error *err = NULL;
5432 char target_path[PATH_MAX];
5433 ssize_t target_len, outlen;
5435 *fd = -1;
5437 if (dirfd != -1) {
5438 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5439 if (target_len == -1)
5440 return got_error_from_errno2("readlinkat", abspath);
5441 } else {
5442 target_len = readlink(abspath, target_path, PATH_MAX);
5443 if (target_len == -1)
5444 return got_error_from_errno2("readlink", abspath);
5447 *fd = got_opentempfd();
5448 if (*fd == -1)
5449 return got_error_from_errno("got_opentempfd");
5451 outlen = write(*fd, target_path, target_len);
5452 if (outlen == -1) {
5453 err = got_error_from_errno("got_opentempfd");
5454 goto done;
5457 if (lseek(*fd, 0, SEEK_SET) == -1) {
5458 err = got_error_from_errno2("lseek", abspath);
5459 goto done;
5461 done:
5462 if (err) {
5463 close(*fd);
5464 *fd = -1;
5466 return err;
5469 static const struct got_error *
5470 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5471 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5472 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5474 const struct got_error *err = NULL;
5475 struct got_blob_object *blob1 = NULL;
5476 int fd = -1, fd1 = -1, fd2 = -1;
5477 FILE *ondisk_file = NULL;
5478 char *label1 = NULL;
5479 struct stat sb;
5480 off_t size1 = 0;
5481 int f2_exists = 0;
5482 char *id_str = NULL;
5484 memset(&sb, 0, sizeof(sb));
5486 if (diff_staged) {
5487 if (ct->staged_status != GOT_STATUS_MODIFY &&
5488 ct->staged_status != GOT_STATUS_ADD &&
5489 ct->staged_status != GOT_STATUS_DELETE)
5490 return NULL;
5491 } else {
5492 if (ct->status != GOT_STATUS_MODIFY &&
5493 ct->status != GOT_STATUS_ADD &&
5494 ct->status != GOT_STATUS_DELETE &&
5495 ct->status != GOT_STATUS_CONFLICT)
5496 return NULL;
5499 err = got_opentemp_truncate(f1);
5500 if (err)
5501 return got_error_from_errno("got_opentemp_truncate");
5502 err = got_opentemp_truncate(f2);
5503 if (err)
5504 return got_error_from_errno("got_opentemp_truncate");
5506 if (!*diff_header_shown) {
5507 err = got_object_id_str(&id_str, worktree->base_commit_id);
5508 if (err)
5509 return err;
5510 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5511 got_worktree_get_root_path(worktree));
5512 fprintf(diff_outfile, "commit - %s\n", id_str);
5513 fprintf(diff_outfile, "path + %s%s\n",
5514 got_worktree_get_root_path(worktree),
5515 diff_staged ? " (staged changes)" : "");
5516 *diff_header_shown = 1;
5519 if (diff_staged) {
5520 const char *label1 = NULL, *label2 = NULL;
5521 switch (ct->staged_status) {
5522 case GOT_STATUS_MODIFY:
5523 label1 = ct->path;
5524 label2 = ct->path;
5525 break;
5526 case GOT_STATUS_ADD:
5527 label2 = ct->path;
5528 break;
5529 case GOT_STATUS_DELETE:
5530 label1 = ct->path;
5531 break;
5532 default:
5533 return got_error(GOT_ERR_FILE_STATUS);
5535 fd1 = got_opentempfd();
5536 if (fd1 == -1) {
5537 err = got_error_from_errno("got_opentempfd");
5538 goto done;
5540 fd2 = got_opentempfd();
5541 if (fd2 == -1) {
5542 err = got_error_from_errno("got_opentempfd");
5543 goto done;
5545 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5546 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5547 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5548 NULL, repo, diff_outfile);
5549 goto done;
5552 fd1 = got_opentempfd();
5553 if (fd1 == -1) {
5554 err = got_error_from_errno("got_opentempfd");
5555 goto done;
5558 if (ct->status != GOT_STATUS_ADD) {
5559 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5560 8192, fd1);
5561 if (err)
5562 goto done;
5565 if (ct->status != GOT_STATUS_DELETE) {
5566 if (dirfd != -1) {
5567 fd = openat(dirfd, de_name,
5568 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5569 if (fd == -1) {
5570 if (!got_err_open_nofollow_on_symlink()) {
5571 err = got_error_from_errno2("openat",
5572 ct->ondisk_path);
5573 goto done;
5575 err = get_symlink_target_file(&fd, dirfd,
5576 de_name, ct->ondisk_path);
5577 if (err)
5578 goto done;
5580 } else {
5581 fd = open(ct->ondisk_path,
5582 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5583 if (fd == -1) {
5584 if (!got_err_open_nofollow_on_symlink()) {
5585 err = got_error_from_errno2("open",
5586 ct->ondisk_path);
5587 goto done;
5589 err = get_symlink_target_file(&fd, dirfd,
5590 de_name, ct->ondisk_path);
5591 if (err)
5592 goto done;
5595 if (fstatat(fd, ct->ondisk_path, &sb,
5596 AT_SYMLINK_NOFOLLOW) == -1) {
5597 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5598 goto done;
5600 ondisk_file = fdopen(fd, "r");
5601 if (ondisk_file == NULL) {
5602 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5603 goto done;
5605 fd = -1;
5606 f2_exists = 1;
5609 if (blob1) {
5610 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5611 f1, blob1);
5612 if (err)
5613 goto done;
5616 err = got_diff_blob_file(blob1, f1, size1, label1,
5617 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5618 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5619 done:
5620 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5621 err = got_error_from_errno("close");
5622 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5623 err = got_error_from_errno("close");
5624 if (blob1)
5625 got_object_blob_close(blob1);
5626 if (fd != -1 && close(fd) == -1 && err == NULL)
5627 err = got_error_from_errno("close");
5628 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5629 err = got_error_from_errno("fclose");
5630 return err;
5633 static const struct got_error *
5634 collect_commitables(void *arg, unsigned char status,
5635 unsigned char staged_status, const char *relpath,
5636 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5637 struct got_object_id *commit_id, int dirfd, const char *de_name)
5639 struct collect_commitables_arg *a = arg;
5640 const struct got_error *err = NULL;
5641 struct got_commitable *ct = NULL;
5642 struct got_pathlist_entry *new = NULL;
5643 char *parent_path = NULL, *path = NULL;
5644 struct stat sb;
5646 if (a->have_staged_files) {
5647 if (staged_status != GOT_STATUS_MODIFY &&
5648 staged_status != GOT_STATUS_ADD &&
5649 staged_status != GOT_STATUS_DELETE)
5650 return NULL;
5651 } else {
5652 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5653 printf("C %s\n", relpath);
5654 return got_error(GOT_ERR_COMMIT_CONFLICT);
5657 if (status != GOT_STATUS_MODIFY &&
5658 status != GOT_STATUS_MODE_CHANGE &&
5659 status != GOT_STATUS_ADD &&
5660 status != GOT_STATUS_DELETE &&
5661 status != GOT_STATUS_CONFLICT)
5662 return NULL;
5665 if (asprintf(&path, "/%s", relpath) == -1) {
5666 err = got_error_from_errno("asprintf");
5667 goto done;
5669 if (strcmp(path, "/") == 0) {
5670 parent_path = strdup("");
5671 if (parent_path == NULL)
5672 return got_error_from_errno("strdup");
5673 } else {
5674 err = got_path_dirname(&parent_path, path);
5675 if (err)
5676 return err;
5679 ct = calloc(1, sizeof(*ct));
5680 if (ct == NULL) {
5681 err = got_error_from_errno("calloc");
5682 goto done;
5685 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5686 relpath) == -1) {
5687 err = got_error_from_errno("asprintf");
5688 goto done;
5691 if (staged_status == GOT_STATUS_ADD ||
5692 staged_status == GOT_STATUS_MODIFY) {
5693 struct got_fileindex_entry *ie;
5694 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5695 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5696 case GOT_FILEIDX_MODE_REGULAR_FILE:
5697 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5698 ct->mode = S_IFREG;
5699 break;
5700 case GOT_FILEIDX_MODE_SYMLINK:
5701 ct->mode = S_IFLNK;
5702 break;
5703 default:
5704 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5705 goto done;
5707 ct->mode |= got_fileindex_entry_perms_get(ie);
5708 } else if (status != GOT_STATUS_DELETE &&
5709 staged_status != GOT_STATUS_DELETE) {
5710 if (dirfd != -1) {
5711 if (fstatat(dirfd, de_name, &sb,
5712 AT_SYMLINK_NOFOLLOW) == -1) {
5713 err = got_error_from_errno2("fstatat",
5714 ct->ondisk_path);
5715 goto done;
5717 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5718 err = got_error_from_errno2("lstat", ct->ondisk_path);
5719 goto done;
5721 ct->mode = sb.st_mode;
5724 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5725 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5726 relpath) == -1) {
5727 err = got_error_from_errno("asprintf");
5728 goto done;
5731 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5732 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5733 int is_bad_symlink;
5734 char target_path[PATH_MAX];
5735 ssize_t target_len;
5736 target_len = readlink(ct->ondisk_path, target_path,
5737 sizeof(target_path));
5738 if (target_len == -1) {
5739 err = got_error_from_errno2("readlink",
5740 ct->ondisk_path);
5741 goto done;
5743 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5744 target_len, ct->ondisk_path, a->worktree->root_path,
5745 a->worktree->meta_dir);
5746 if (err)
5747 goto done;
5748 if (is_bad_symlink) {
5749 err = got_error_path(ct->ondisk_path,
5750 GOT_ERR_BAD_SYMLINK);
5751 goto done;
5756 ct->status = status;
5757 ct->staged_status = staged_status;
5758 ct->blob_id = NULL; /* will be filled in when blob gets created */
5759 if (ct->status != GOT_STATUS_ADD &&
5760 ct->staged_status != GOT_STATUS_ADD) {
5761 ct->base_blob_id = got_object_id_dup(blob_id);
5762 if (ct->base_blob_id == NULL) {
5763 err = got_error_from_errno("got_object_id_dup");
5764 goto done;
5766 ct->base_commit_id = got_object_id_dup(commit_id);
5767 if (ct->base_commit_id == NULL) {
5768 err = got_error_from_errno("got_object_id_dup");
5769 goto done;
5772 if (ct->staged_status == GOT_STATUS_ADD ||
5773 ct->staged_status == GOT_STATUS_MODIFY) {
5774 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5775 if (ct->staged_blob_id == NULL) {
5776 err = got_error_from_errno("got_object_id_dup");
5777 goto done;
5780 ct->path = strdup(path);
5781 if (ct->path == NULL) {
5782 err = got_error_from_errno("strdup");
5783 goto done;
5786 if (a->diff_outfile) {
5787 err = append_ct_diff(ct, &a->diff_header_shown,
5788 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5789 a->have_staged_files, a->repo, a->worktree);
5790 if (err)
5791 goto done;
5794 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5795 done:
5796 if (ct && (err || new == NULL))
5797 free_commitable(ct);
5798 free(parent_path);
5799 free(path);
5800 return err;
5803 static const struct got_error *write_tree(struct got_object_id **, int *,
5804 struct got_tree_object *, const char *, struct got_pathlist_head *,
5805 got_worktree_status_cb status_cb, void *status_arg,
5806 struct got_repository *);
5808 static const struct got_error *
5809 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5810 struct got_tree_entry *te, const char *parent_path,
5811 struct got_pathlist_head *commitable_paths,
5812 got_worktree_status_cb status_cb, void *status_arg,
5813 struct got_repository *repo)
5815 const struct got_error *err = NULL;
5816 struct got_tree_object *subtree;
5817 char *subpath;
5819 if (asprintf(&subpath, "%s%s%s", parent_path,
5820 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5821 return got_error_from_errno("asprintf");
5823 err = got_object_open_as_tree(&subtree, repo, &te->id);
5824 if (err)
5825 return err;
5827 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5828 commitable_paths, status_cb, status_arg, repo);
5829 got_object_tree_close(subtree);
5830 free(subpath);
5831 return err;
5834 static const struct got_error *
5835 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5837 const struct got_error *err = NULL;
5838 char *ct_parent_path = NULL;
5840 *match = 0;
5842 if (strchr(ct->in_repo_path, '/') == NULL) {
5843 *match = got_path_is_root_dir(path);
5844 return NULL;
5847 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5848 if (err)
5849 return err;
5850 *match = (strcmp(path, ct_parent_path) == 0);
5851 free(ct_parent_path);
5852 return err;
5855 static mode_t
5856 get_ct_file_mode(struct got_commitable *ct)
5858 if (S_ISLNK(ct->mode))
5859 return S_IFLNK;
5861 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5864 static const struct got_error *
5865 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5866 struct got_tree_entry *te, struct got_commitable *ct)
5868 const struct got_error *err = NULL;
5870 *new_te = NULL;
5872 err = got_object_tree_entry_dup(new_te, te);
5873 if (err)
5874 goto done;
5876 (*new_te)->mode = get_ct_file_mode(ct);
5878 if (ct->staged_status == GOT_STATUS_MODIFY)
5879 memcpy(&(*new_te)->id, ct->staged_blob_id,
5880 sizeof((*new_te)->id));
5881 else
5882 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5883 done:
5884 if (err && *new_te) {
5885 free(*new_te);
5886 *new_te = NULL;
5888 return err;
5891 static const struct got_error *
5892 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5893 struct got_commitable *ct)
5895 const struct got_error *err = NULL;
5896 char *ct_name = NULL;
5898 *new_te = NULL;
5900 *new_te = calloc(1, sizeof(**new_te));
5901 if (*new_te == NULL)
5902 return got_error_from_errno("calloc");
5904 err = got_path_basename(&ct_name, ct->path);
5905 if (err)
5906 goto done;
5907 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5908 sizeof((*new_te)->name)) {
5909 err = got_error(GOT_ERR_NO_SPACE);
5910 goto done;
5913 (*new_te)->mode = get_ct_file_mode(ct);
5915 if (ct->staged_status == GOT_STATUS_ADD)
5916 memcpy(&(*new_te)->id, ct->staged_blob_id,
5917 sizeof((*new_te)->id));
5918 else
5919 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5920 done:
5921 free(ct_name);
5922 if (err && *new_te) {
5923 free(*new_te);
5924 *new_te = NULL;
5926 return err;
5929 static const struct got_error *
5930 insert_tree_entry(struct got_tree_entry *new_te,
5931 struct got_pathlist_head *paths)
5933 const struct got_error *err = NULL;
5934 struct got_pathlist_entry *new_pe;
5936 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5937 if (err)
5938 return err;
5939 if (new_pe == NULL)
5940 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5941 return NULL;
5944 static const struct got_error *
5945 report_ct_status(struct got_commitable *ct,
5946 got_worktree_status_cb status_cb, void *status_arg)
5948 const char *ct_path = ct->path;
5949 unsigned char status;
5951 if (status_cb == NULL) /* no commit progress output desired */
5952 return NULL;
5954 while (ct_path[0] == '/')
5955 ct_path++;
5957 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5958 status = ct->staged_status;
5959 else
5960 status = ct->status;
5962 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5963 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5966 static const struct got_error *
5967 match_modified_subtree(int *modified, struct got_tree_entry *te,
5968 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5970 const struct got_error *err = NULL;
5971 struct got_pathlist_entry *pe;
5972 char *te_path;
5974 *modified = 0;
5976 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5977 got_path_is_root_dir(base_tree_path) ? "" : "/",
5978 te->name) == -1)
5979 return got_error_from_errno("asprintf");
5981 TAILQ_FOREACH(pe, commitable_paths, entry) {
5982 struct got_commitable *ct = pe->data;
5983 *modified = got_path_is_child(ct->in_repo_path, te_path,
5984 strlen(te_path));
5985 if (*modified)
5986 break;
5989 free(te_path);
5990 return err;
5993 static const struct got_error *
5994 match_deleted_or_modified_ct(struct got_commitable **ctp,
5995 struct got_tree_entry *te, const char *base_tree_path,
5996 struct got_pathlist_head *commitable_paths)
5998 const struct got_error *err = NULL;
5999 struct got_pathlist_entry *pe;
6001 *ctp = NULL;
6003 TAILQ_FOREACH(pe, commitable_paths, entry) {
6004 struct got_commitable *ct = pe->data;
6005 char *ct_name = NULL;
6006 int path_matches;
6008 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
6009 if (ct->status != GOT_STATUS_MODIFY &&
6010 ct->status != GOT_STATUS_MODE_CHANGE &&
6011 ct->status != GOT_STATUS_DELETE &&
6012 ct->status != GOT_STATUS_CONFLICT)
6013 continue;
6014 } else {
6015 if (ct->staged_status != GOT_STATUS_MODIFY &&
6016 ct->staged_status != GOT_STATUS_DELETE)
6017 continue;
6020 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
6021 continue;
6023 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
6024 if (err)
6025 return err;
6026 if (!path_matches)
6027 continue;
6029 err = got_path_basename(&ct_name, pe->path);
6030 if (err)
6031 return err;
6033 if (strcmp(te->name, ct_name) != 0) {
6034 free(ct_name);
6035 continue;
6037 free(ct_name);
6039 *ctp = ct;
6040 break;
6043 return err;
6046 static const struct got_error *
6047 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
6048 const char *child_path, const char *path_base_tree,
6049 struct got_pathlist_head *commitable_paths,
6050 got_worktree_status_cb status_cb, void *status_arg,
6051 struct got_repository *repo)
6053 const struct got_error *err = NULL;
6054 struct got_tree_entry *new_te;
6055 char *subtree_path;
6056 struct got_object_id *id = NULL;
6057 int nentries;
6059 *new_tep = NULL;
6061 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
6062 got_path_is_root_dir(path_base_tree) ? "" : "/",
6063 child_path) == -1)
6064 return got_error_from_errno("asprintf");
6066 new_te = calloc(1, sizeof(*new_te));
6067 if (new_te == NULL)
6068 return got_error_from_errno("calloc");
6069 new_te->mode = S_IFDIR;
6071 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
6072 sizeof(new_te->name)) {
6073 err = got_error(GOT_ERR_NO_SPACE);
6074 goto done;
6076 err = write_tree(&id, &nentries, NULL, subtree_path,
6077 commitable_paths, status_cb, status_arg, repo);
6078 if (err) {
6079 free(new_te);
6080 goto done;
6082 memcpy(&new_te->id, id, sizeof(new_te->id));
6083 done:
6084 free(id);
6085 free(subtree_path);
6086 if (err == NULL)
6087 *new_tep = new_te;
6088 return err;
6091 static const struct got_error *
6092 write_tree(struct got_object_id **new_tree_id, int *nentries,
6093 struct got_tree_object *base_tree, const char *path_base_tree,
6094 struct got_pathlist_head *commitable_paths,
6095 got_worktree_status_cb status_cb, void *status_arg,
6096 struct got_repository *repo)
6098 const struct got_error *err = NULL;
6099 struct got_pathlist_head paths;
6100 struct got_tree_entry *te, *new_te = NULL;
6101 struct got_pathlist_entry *pe;
6103 TAILQ_INIT(&paths);
6104 *nentries = 0;
6106 /* Insert, and recurse into, newly added entries first. */
6107 TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 struct got_commitable *ct = pe->data;
6109 char *child_path = NULL, *slash;
6111 if ((ct->status != GOT_STATUS_ADD &&
6112 ct->staged_status != GOT_STATUS_ADD) ||
6113 (ct->flags & GOT_COMMITABLE_ADDED))
6114 continue;
6116 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
6117 strlen(path_base_tree)))
6118 continue;
6120 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
6121 ct->in_repo_path);
6122 if (err)
6123 goto done;
6125 slash = strchr(child_path, '/');
6126 if (slash == NULL) {
6127 err = alloc_added_blob_tree_entry(&new_te, ct);
6128 if (err)
6129 goto done;
6130 err = report_ct_status(ct, status_cb, status_arg);
6131 if (err)
6132 goto done;
6133 ct->flags |= GOT_COMMITABLE_ADDED;
6134 err = insert_tree_entry(new_te, &paths);
6135 if (err)
6136 goto done;
6137 (*nentries)++;
6138 } else {
6139 *slash = '\0'; /* trim trailing path components */
6140 if (base_tree == NULL ||
6141 got_object_tree_find_entry(base_tree, child_path)
6142 == NULL) {
6143 err = make_subtree_for_added_blob(&new_te,
6144 child_path, path_base_tree,
6145 commitable_paths, status_cb, status_arg,
6146 repo);
6147 if (err)
6148 goto done;
6149 err = insert_tree_entry(new_te, &paths);
6150 if (err)
6151 goto done;
6152 (*nentries)++;
6157 if (base_tree) {
6158 int i, nbase_entries;
6159 /* Handle modified and deleted entries. */
6160 nbase_entries = got_object_tree_get_nentries(base_tree);
6161 for (i = 0; i < nbase_entries; i++) {
6162 struct got_commitable *ct = NULL;
6164 te = got_object_tree_get_entry(base_tree, i);
6165 if (got_object_tree_entry_is_submodule(te)) {
6166 /* Entry is a submodule; just copy it. */
6167 err = got_object_tree_entry_dup(&new_te, te);
6168 if (err)
6169 goto done;
6170 err = insert_tree_entry(new_te, &paths);
6171 if (err)
6172 goto done;
6173 (*nentries)++;
6174 continue;
6177 if (S_ISDIR(te->mode)) {
6178 int modified;
6179 err = got_object_tree_entry_dup(&new_te, te);
6180 if (err)
6181 goto done;
6182 err = match_modified_subtree(&modified, te,
6183 path_base_tree, commitable_paths);
6184 if (err)
6185 goto done;
6186 /* Avoid recursion into unmodified subtrees. */
6187 if (modified) {
6188 struct got_object_id *new_id;
6189 int nsubentries;
6190 err = write_subtree(&new_id,
6191 &nsubentries, te,
6192 path_base_tree, commitable_paths,
6193 status_cb, status_arg, repo);
6194 if (err)
6195 goto done;
6196 if (nsubentries == 0) {
6197 /* All entries were deleted. */
6198 free(new_id);
6199 continue;
6201 memcpy(&new_te->id, new_id,
6202 sizeof(new_te->id));
6203 free(new_id);
6205 err = insert_tree_entry(new_te, &paths);
6206 if (err)
6207 goto done;
6208 (*nentries)++;
6209 continue;
6212 err = match_deleted_or_modified_ct(&ct, te,
6213 path_base_tree, commitable_paths);
6214 if (err)
6215 goto done;
6216 if (ct) {
6217 /* NB: Deleted entries get dropped here. */
6218 if (ct->status == GOT_STATUS_MODIFY ||
6219 ct->status == GOT_STATUS_MODE_CHANGE ||
6220 ct->status == GOT_STATUS_CONFLICT ||
6221 ct->staged_status == GOT_STATUS_MODIFY) {
6222 err = alloc_modified_blob_tree_entry(
6223 &new_te, te, ct);
6224 if (err)
6225 goto done;
6226 err = insert_tree_entry(new_te, &paths);
6227 if (err)
6228 goto done;
6229 (*nentries)++;
6231 err = report_ct_status(ct, status_cb,
6232 status_arg);
6233 if (err)
6234 goto done;
6235 } else {
6236 /* Entry is unchanged; just copy it. */
6237 err = got_object_tree_entry_dup(&new_te, te);
6238 if (err)
6239 goto done;
6240 err = insert_tree_entry(new_te, &paths);
6241 if (err)
6242 goto done;
6243 (*nentries)++;
6248 /* Write new list of entries; deleted entries have been dropped. */
6249 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6250 done:
6251 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6252 return err;
6255 static const struct got_error *
6256 update_fileindex_after_commit(struct got_worktree *worktree,
6257 struct got_pathlist_head *commitable_paths,
6258 struct got_object_id *new_base_commit_id,
6259 struct got_fileindex *fileindex, int have_staged_files)
6261 const struct got_error *err = NULL;
6262 struct got_pathlist_entry *pe;
6263 char *relpath = NULL;
6265 TAILQ_FOREACH(pe, commitable_paths, entry) {
6266 struct got_fileindex_entry *ie;
6267 struct got_commitable *ct = pe->data;
6269 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6271 err = got_path_skip_common_ancestor(&relpath,
6272 worktree->root_path, ct->ondisk_path);
6273 if (err)
6274 goto done;
6276 if (ie) {
6277 if (ct->status == GOT_STATUS_DELETE ||
6278 ct->staged_status == GOT_STATUS_DELETE) {
6279 got_fileindex_entry_remove(fileindex, ie);
6280 } else if (ct->staged_status == GOT_STATUS_ADD ||
6281 ct->staged_status == GOT_STATUS_MODIFY) {
6282 got_fileindex_entry_stage_set(ie,
6283 GOT_FILEIDX_STAGE_NONE);
6284 got_fileindex_entry_staged_filetype_set(ie, 0);
6286 err = got_fileindex_entry_update(ie,
6287 worktree->root_fd, relpath,
6288 ct->staged_blob_id->sha1,
6289 new_base_commit_id->sha1,
6290 !have_staged_files);
6291 } else
6292 err = got_fileindex_entry_update(ie,
6293 worktree->root_fd, relpath,
6294 ct->blob_id->sha1,
6295 new_base_commit_id->sha1,
6296 !have_staged_files);
6297 } else {
6298 err = got_fileindex_entry_alloc(&ie, pe->path);
6299 if (err)
6300 goto done;
6301 err = got_fileindex_entry_update(ie,
6302 worktree->root_fd, relpath, ct->blob_id->sha1,
6303 new_base_commit_id->sha1, 1);
6304 if (err) {
6305 got_fileindex_entry_free(ie);
6306 goto done;
6308 err = got_fileindex_entry_add(fileindex, ie);
6309 if (err) {
6310 got_fileindex_entry_free(ie);
6311 goto done;
6314 free(relpath);
6315 relpath = NULL;
6317 done:
6318 free(relpath);
6319 return err;
6323 static const struct got_error *
6324 check_out_of_date(const char *in_repo_path, unsigned char status,
6325 unsigned char staged_status, struct got_object_id *base_blob_id,
6326 struct got_object_id *base_commit_id,
6327 struct got_object_id *head_commit_id, struct got_repository *repo,
6328 int ood_errcode)
6330 const struct got_error *err = NULL;
6331 struct got_commit_object *commit = NULL;
6332 struct got_object_id *id = NULL;
6334 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6335 /* Trivial case: base commit == head commit */
6336 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6337 return NULL;
6339 * Ensure file content which local changes were based
6340 * on matches file content in the branch head.
6342 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6343 if (err)
6344 goto done;
6345 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6346 if (err) {
6347 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6348 err = got_error(ood_errcode);
6349 goto done;
6350 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6351 err = got_error(ood_errcode);
6352 } else {
6353 /* Require that added files don't exist in the branch head. */
6354 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6355 if (err)
6356 goto done;
6357 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6358 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6359 goto done;
6360 err = id ? got_error(ood_errcode) : NULL;
6362 done:
6363 free(id);
6364 if (commit)
6365 got_object_commit_close(commit);
6366 return err;
6369 static const struct got_error *
6370 commit_worktree(struct got_object_id **new_commit_id,
6371 struct got_pathlist_head *commitable_paths,
6372 struct got_object_id *head_commit_id,
6373 struct got_object_id *parent_id2,
6374 struct got_worktree *worktree,
6375 const char *author, const char *committer, char *diff_path,
6376 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6377 got_worktree_status_cb status_cb, void *status_arg,
6378 struct got_repository *repo)
6380 const struct got_error *err = NULL, *unlockerr = NULL;
6381 struct got_pathlist_entry *pe;
6382 const char *head_ref_name = NULL;
6383 struct got_commit_object *head_commit = NULL;
6384 struct got_reference *head_ref2 = NULL;
6385 struct got_object_id *head_commit_id2 = NULL;
6386 struct got_tree_object *head_tree = NULL;
6387 struct got_object_id *new_tree_id = NULL;
6388 int nentries, nparents = 0;
6389 struct got_object_id_queue parent_ids;
6390 struct got_object_qid *pid = NULL;
6391 char *logmsg = NULL;
6392 time_t timestamp;
6394 *new_commit_id = NULL;
6396 STAILQ_INIT(&parent_ids);
6398 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6399 if (err)
6400 goto done;
6402 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6403 if (err)
6404 goto done;
6406 if (commit_msg_cb != NULL) {
6407 err = commit_msg_cb(commitable_paths, diff_path,
6408 &logmsg, commit_arg);
6409 if (err)
6410 goto done;
6413 if (logmsg == NULL || strlen(logmsg) == 0) {
6414 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6415 goto done;
6418 /* Create blobs from added and modified files and record their IDs. */
6419 TAILQ_FOREACH(pe, commitable_paths, entry) {
6420 struct got_commitable *ct = pe->data;
6421 char *ondisk_path;
6423 /* Blobs for staged files already exist. */
6424 if (ct->staged_status == GOT_STATUS_ADD ||
6425 ct->staged_status == GOT_STATUS_MODIFY)
6426 continue;
6428 if (ct->status != GOT_STATUS_ADD &&
6429 ct->status != GOT_STATUS_MODIFY &&
6430 ct->status != GOT_STATUS_MODE_CHANGE &&
6431 ct->status != GOT_STATUS_CONFLICT)
6432 continue;
6434 if (asprintf(&ondisk_path, "%s/%s",
6435 worktree->root_path, pe->path) == -1) {
6436 err = got_error_from_errno("asprintf");
6437 goto done;
6439 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6440 free(ondisk_path);
6441 if (err)
6442 goto done;
6445 /* Recursively write new tree objects. */
6446 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6447 commitable_paths, status_cb, status_arg, repo);
6448 if (err)
6449 goto done;
6451 err = got_object_qid_alloc(&pid, head_commit_id);
6452 if (err)
6453 goto done;
6454 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6455 nparents++;
6456 if (parent_id2) {
6457 err = got_object_qid_alloc(&pid, parent_id2);
6458 if (err)
6459 goto done;
6460 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6461 nparents++;
6463 timestamp = time(NULL);
6464 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6465 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6466 if (logmsg != NULL)
6467 free(logmsg);
6468 if (err)
6469 goto done;
6471 /* Check if a concurrent commit to our branch has occurred. */
6472 head_ref_name = got_worktree_get_head_ref_name(worktree);
6473 if (head_ref_name == NULL) {
6474 err = got_error_from_errno("got_worktree_get_head_ref_name");
6475 goto done;
6477 /* Lock the reference here to prevent concurrent modification. */
6478 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6479 if (err)
6480 goto done;
6481 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6482 if (err)
6483 goto done;
6484 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6485 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6486 goto done;
6488 /* Update branch head in repository. */
6489 err = got_ref_change_ref(head_ref2, *new_commit_id);
6490 if (err)
6491 goto done;
6492 err = got_ref_write(head_ref2, repo);
6493 if (err)
6494 goto done;
6496 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6497 if (err)
6498 goto done;
6500 err = ref_base_commit(worktree, repo);
6501 if (err)
6502 goto done;
6503 done:
6504 got_object_id_queue_free(&parent_ids);
6505 if (head_tree)
6506 got_object_tree_close(head_tree);
6507 if (head_commit)
6508 got_object_commit_close(head_commit);
6509 free(head_commit_id2);
6510 if (head_ref2) {
6511 unlockerr = got_ref_unlock(head_ref2);
6512 if (unlockerr && err == NULL)
6513 err = unlockerr;
6514 got_ref_close(head_ref2);
6516 return err;
6519 static const struct got_error *
6520 check_path_is_commitable(const char *path,
6521 struct got_pathlist_head *commitable_paths)
6523 struct got_pathlist_entry *cpe = NULL;
6524 size_t path_len = strlen(path);
6526 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6527 struct got_commitable *ct = cpe->data;
6528 const char *ct_path = ct->path;
6530 while (ct_path[0] == '/')
6531 ct_path++;
6533 if (strcmp(path, ct_path) == 0 ||
6534 got_path_is_child(ct_path, path, path_len))
6535 break;
6538 if (cpe == NULL)
6539 return got_error_path(path, GOT_ERR_BAD_PATH);
6541 return NULL;
6544 static const struct got_error *
6545 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6547 int *have_staged_files = arg;
6549 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6550 *have_staged_files = 1;
6551 return got_error(GOT_ERR_CANCELLED);
6554 return NULL;
6557 static const struct got_error *
6558 check_non_staged_files(struct got_fileindex *fileindex,
6559 struct got_pathlist_head *paths)
6561 struct got_pathlist_entry *pe;
6562 struct got_fileindex_entry *ie;
6564 TAILQ_FOREACH(pe, paths, entry) {
6565 if (pe->path[0] == '\0')
6566 continue;
6567 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6568 if (ie == NULL)
6569 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6570 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6571 return got_error_path(pe->path,
6572 GOT_ERR_FILE_NOT_STAGED);
6575 return NULL;
6578 const struct got_error *
6579 got_worktree_commit(struct got_object_id **new_commit_id,
6580 struct got_worktree *worktree, struct got_pathlist_head *paths,
6581 const char *author, const char *committer, int allow_bad_symlinks,
6582 int show_diff, int commit_conflicts,
6583 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6584 got_worktree_status_cb status_cb, void *status_arg,
6585 struct got_repository *repo)
6587 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6588 struct got_fileindex *fileindex = NULL;
6589 char *fileindex_path = NULL;
6590 struct got_pathlist_head commitable_paths;
6591 struct collect_commitables_arg cc_arg;
6592 struct got_pathlist_entry *pe;
6593 struct got_reference *head_ref = NULL;
6594 struct got_object_id *head_commit_id = NULL;
6595 char *diff_path = NULL;
6596 int have_staged_files = 0;
6598 *new_commit_id = NULL;
6600 memset(&cc_arg, 0, sizeof(cc_arg));
6601 TAILQ_INIT(&commitable_paths);
6603 err = lock_worktree(worktree, LOCK_EX);
6604 if (err)
6605 goto done;
6607 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6608 if (err)
6609 goto done;
6611 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6612 if (err)
6613 goto done;
6615 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6616 if (err)
6617 goto done;
6619 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6620 &have_staged_files);
6621 if (err && err->code != GOT_ERR_CANCELLED)
6622 goto done;
6623 if (have_staged_files) {
6624 err = check_non_staged_files(fileindex, paths);
6625 if (err)
6626 goto done;
6629 cc_arg.commitable_paths = &commitable_paths;
6630 cc_arg.worktree = worktree;
6631 cc_arg.fileindex = fileindex;
6632 cc_arg.repo = repo;
6633 cc_arg.have_staged_files = have_staged_files;
6634 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6635 cc_arg.diff_header_shown = 0;
6636 cc_arg.commit_conflicts = commit_conflicts;
6637 if (show_diff) {
6638 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6639 GOT_TMPDIR_STR "/got", ".diff");
6640 if (err)
6641 goto done;
6642 cc_arg.f1 = got_opentemp();
6643 if (cc_arg.f1 == NULL) {
6644 err = got_error_from_errno("got_opentemp");
6645 goto done;
6647 cc_arg.f2 = got_opentemp();
6648 if (cc_arg.f2 == NULL) {
6649 err = got_error_from_errno("got_opentemp");
6650 goto done;
6654 TAILQ_FOREACH(pe, paths, entry) {
6655 err = worktree_status(worktree, pe->path, fileindex, repo,
6656 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6657 if (err)
6658 goto done;
6661 if (show_diff) {
6662 if (fflush(cc_arg.diff_outfile) == EOF) {
6663 err = got_error_from_errno("fflush");
6664 goto done;
6668 if (TAILQ_EMPTY(&commitable_paths)) {
6669 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6670 goto done;
6673 TAILQ_FOREACH(pe, paths, entry) {
6674 err = check_path_is_commitable(pe->path, &commitable_paths);
6675 if (err)
6676 goto done;
6679 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6680 struct got_commitable *ct = pe->data;
6681 const char *ct_path = ct->in_repo_path;
6683 while (ct_path[0] == '/')
6684 ct_path++;
6685 err = check_out_of_date(ct_path, ct->status,
6686 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6687 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6688 if (err)
6689 goto done;
6693 err = commit_worktree(new_commit_id, &commitable_paths,
6694 head_commit_id, NULL, worktree, author, committer,
6695 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6696 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6697 if (err)
6698 goto done;
6700 err = update_fileindex_after_commit(worktree, &commitable_paths,
6701 *new_commit_id, fileindex, have_staged_files);
6702 sync_err = sync_fileindex(fileindex, fileindex_path);
6703 if (sync_err && err == NULL)
6704 err = sync_err;
6705 done:
6706 if (fileindex)
6707 got_fileindex_free(fileindex);
6708 free(fileindex_path);
6709 unlockerr = lock_worktree(worktree, LOCK_SH);
6710 if (unlockerr && err == NULL)
6711 err = unlockerr;
6712 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6713 struct got_commitable *ct = pe->data;
6715 free_commitable(ct);
6717 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6718 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6719 err = got_error_from_errno2("unlink", diff_path);
6720 free(diff_path);
6721 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6722 err == NULL)
6723 err = got_error_from_errno("fclose");
6724 return err;
6727 const char *
6728 got_commitable_get_path(struct got_commitable *ct)
6730 return ct->path;
6733 unsigned int
6734 got_commitable_get_status(struct got_commitable *ct)
6736 return ct->status;
6739 struct check_rebase_ok_arg {
6740 struct got_worktree *worktree;
6741 struct got_repository *repo;
6744 static const struct got_error *
6745 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6747 const struct got_error *err = NULL;
6748 struct check_rebase_ok_arg *a = arg;
6749 unsigned char status;
6750 struct stat sb;
6751 char *ondisk_path;
6753 /* Reject rebase of a work tree with mixed base commits. */
6754 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6755 SHA1_DIGEST_LENGTH))
6756 return got_error(GOT_ERR_MIXED_COMMITS);
6758 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6759 == -1)
6760 return got_error_from_errno("asprintf");
6762 /* Reject rebase of a work tree with modified or staged files. */
6763 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6764 free(ondisk_path);
6765 if (err)
6766 return err;
6768 if (status != GOT_STATUS_NO_CHANGE)
6769 return got_error(GOT_ERR_MODIFIED);
6770 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6771 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6773 return NULL;
6776 const struct got_error *
6777 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6778 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6779 struct got_worktree *worktree, struct got_reference *branch,
6780 struct got_repository *repo)
6782 const struct got_error *err = NULL;
6783 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6784 char *branch_ref_name = NULL;
6785 char *fileindex_path = NULL;
6786 struct check_rebase_ok_arg ok_arg;
6787 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6788 struct got_object_id *wt_branch_tip = NULL;
6790 *new_base_branch_ref = NULL;
6791 *tmp_branch = NULL;
6792 *fileindex = NULL;
6794 err = lock_worktree(worktree, LOCK_EX);
6795 if (err)
6796 return err;
6798 err = open_fileindex(fileindex, &fileindex_path, worktree);
6799 if (err)
6800 goto done;
6802 ok_arg.worktree = worktree;
6803 ok_arg.repo = repo;
6804 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6805 &ok_arg);
6806 if (err)
6807 goto done;
6809 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6810 if (err)
6811 goto done;
6813 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6814 if (err)
6815 goto done;
6817 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6818 if (err)
6819 goto done;
6821 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6822 0);
6823 if (err)
6824 goto done;
6826 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6827 if (err)
6828 goto done;
6829 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6830 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6831 goto done;
6834 err = got_ref_alloc_symref(new_base_branch_ref,
6835 new_base_branch_ref_name, wt_branch);
6836 if (err)
6837 goto done;
6838 err = got_ref_write(*new_base_branch_ref, repo);
6839 if (err)
6840 goto done;
6842 /* TODO Lock original branch's ref while rebasing? */
6844 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6845 if (err)
6846 goto done;
6848 err = got_ref_write(branch_ref, repo);
6849 if (err)
6850 goto done;
6852 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6853 worktree->base_commit_id);
6854 if (err)
6855 goto done;
6856 err = got_ref_write(*tmp_branch, repo);
6857 if (err)
6858 goto done;
6860 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6861 if (err)
6862 goto done;
6863 done:
6864 free(fileindex_path);
6865 free(tmp_branch_name);
6866 free(new_base_branch_ref_name);
6867 free(branch_ref_name);
6868 if (branch_ref)
6869 got_ref_close(branch_ref);
6870 if (wt_branch)
6871 got_ref_close(wt_branch);
6872 free(wt_branch_tip);
6873 if (err) {
6874 if (*new_base_branch_ref) {
6875 got_ref_close(*new_base_branch_ref);
6876 *new_base_branch_ref = NULL;
6878 if (*tmp_branch) {
6879 got_ref_close(*tmp_branch);
6880 *tmp_branch = NULL;
6882 if (*fileindex) {
6883 got_fileindex_free(*fileindex);
6884 *fileindex = NULL;
6886 lock_worktree(worktree, LOCK_SH);
6888 return err;
6891 const struct got_error *
6892 got_worktree_rebase_continue(struct got_object_id **commit_id,
6893 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6894 struct got_reference **branch, struct got_fileindex **fileindex,
6895 struct got_worktree *worktree, struct got_repository *repo)
6897 const struct got_error *err;
6898 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6899 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6900 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6901 char *fileindex_path = NULL;
6902 int have_staged_files = 0;
6904 *commit_id = NULL;
6905 *new_base_branch = NULL;
6906 *tmp_branch = NULL;
6907 *branch = NULL;
6908 *fileindex = NULL;
6910 err = lock_worktree(worktree, LOCK_EX);
6911 if (err)
6912 return err;
6914 err = open_fileindex(fileindex, &fileindex_path, worktree);
6915 if (err)
6916 goto done;
6918 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6919 &have_staged_files);
6920 if (err && err->code != GOT_ERR_CANCELLED)
6921 goto done;
6922 if (have_staged_files) {
6923 err = got_error(GOT_ERR_STAGED_PATHS);
6924 goto done;
6927 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6928 if (err)
6929 goto done;
6931 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6932 if (err)
6933 goto done;
6935 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6936 if (err)
6937 goto done;
6939 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6940 if (err)
6941 goto done;
6943 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6944 if (err)
6945 goto done;
6947 err = got_ref_open(branch, repo,
6948 got_ref_get_symref_target(branch_ref), 0);
6949 if (err)
6950 goto done;
6952 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6953 if (err)
6954 goto done;
6956 err = got_ref_resolve(commit_id, repo, commit_ref);
6957 if (err)
6958 goto done;
6960 err = got_ref_open(new_base_branch, repo,
6961 new_base_branch_ref_name, 0);
6962 if (err)
6963 goto done;
6965 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6966 if (err)
6967 goto done;
6968 done:
6969 free(commit_ref_name);
6970 free(branch_ref_name);
6971 free(fileindex_path);
6972 if (commit_ref)
6973 got_ref_close(commit_ref);
6974 if (branch_ref)
6975 got_ref_close(branch_ref);
6976 if (err) {
6977 free(*commit_id);
6978 *commit_id = NULL;
6979 if (*tmp_branch) {
6980 got_ref_close(*tmp_branch);
6981 *tmp_branch = NULL;
6983 if (*new_base_branch) {
6984 got_ref_close(*new_base_branch);
6985 *new_base_branch = NULL;
6987 if (*branch) {
6988 got_ref_close(*branch);
6989 *branch = NULL;
6991 if (*fileindex) {
6992 got_fileindex_free(*fileindex);
6993 *fileindex = NULL;
6995 lock_worktree(worktree, LOCK_SH);
6997 return err;
7000 const struct got_error *
7001 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
7003 const struct got_error *err;
7004 char *tmp_branch_name = NULL;
7006 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7007 if (err)
7008 return err;
7010 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7011 free(tmp_branch_name);
7012 return NULL;
7015 static const struct got_error *
7016 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
7017 const char *diff_path, char **logmsg, void *arg)
7019 *logmsg = arg;
7020 return NULL;
7023 static const struct got_error *
7024 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
7025 const char *path, struct got_object_id *blob_id,
7026 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7027 int dirfd, const char *de_name)
7029 return NULL;
7032 struct collect_merged_paths_arg {
7033 got_worktree_checkout_cb progress_cb;
7034 void *progress_arg;
7035 struct got_pathlist_head *merged_paths;
7038 static const struct got_error *
7039 collect_merged_paths(void *arg, unsigned char status, const char *path)
7041 const struct got_error *err;
7042 struct collect_merged_paths_arg *a = arg;
7043 char *p;
7044 struct got_pathlist_entry *new;
7046 err = (*a->progress_cb)(a->progress_arg, status, path);
7047 if (err)
7048 return err;
7050 if (status != GOT_STATUS_MERGE &&
7051 status != GOT_STATUS_ADD &&
7052 status != GOT_STATUS_DELETE &&
7053 status != GOT_STATUS_CONFLICT)
7054 return NULL;
7056 p = strdup(path);
7057 if (p == NULL)
7058 return got_error_from_errno("strdup");
7060 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
7061 if (err || new == NULL)
7062 free(p);
7063 return err;
7066 static const struct got_error *
7067 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
7068 int is_rebase, struct got_repository *repo)
7070 const struct got_error *err;
7071 struct got_reference *commit_ref = NULL;
7073 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7074 if (err) {
7075 if (err->code != GOT_ERR_NOT_REF)
7076 goto done;
7077 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
7078 if (err)
7079 goto done;
7080 err = got_ref_write(commit_ref, repo);
7081 if (err)
7082 goto done;
7083 } else if (is_rebase) {
7084 struct got_object_id *stored_id;
7085 int cmp;
7087 err = got_ref_resolve(&stored_id, repo, commit_ref);
7088 if (err)
7089 goto done;
7090 cmp = got_object_id_cmp(commit_id, stored_id);
7091 free(stored_id);
7092 if (cmp != 0) {
7093 err = got_error(GOT_ERR_REBASE_COMMITID);
7094 goto done;
7097 done:
7098 if (commit_ref)
7099 got_ref_close(commit_ref);
7100 return err;
7103 static const struct got_error *
7104 rebase_merge_files(struct got_pathlist_head *merged_paths,
7105 const char *commit_ref_name, struct got_worktree *worktree,
7106 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
7107 struct got_object_id *commit_id, struct got_repository *repo,
7108 got_worktree_checkout_cb progress_cb, void *progress_arg,
7109 got_cancel_cb cancel_cb, void *cancel_arg)
7111 const struct got_error *err;
7112 struct got_reference *commit_ref = NULL;
7113 struct collect_merged_paths_arg cmp_arg;
7114 char *fileindex_path;
7116 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7118 err = get_fileindex_path(&fileindex_path, worktree);
7119 if (err)
7120 return err;
7122 cmp_arg.progress_cb = progress_cb;
7123 cmp_arg.progress_arg = progress_arg;
7124 cmp_arg.merged_paths = merged_paths;
7125 err = merge_files(worktree, fileindex, fileindex_path,
7126 parent_commit_id, commit_id, repo, collect_merged_paths,
7127 &cmp_arg, cancel_cb, cancel_arg);
7128 if (commit_ref)
7129 got_ref_close(commit_ref);
7130 return err;
7133 const struct got_error *
7134 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
7135 struct got_worktree *worktree, struct got_fileindex *fileindex,
7136 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7137 struct got_repository *repo,
7138 got_worktree_checkout_cb progress_cb, void *progress_arg,
7139 got_cancel_cb cancel_cb, void *cancel_arg)
7141 const struct got_error *err;
7142 char *commit_ref_name;
7144 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7145 if (err)
7146 return err;
7148 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
7149 if (err)
7150 goto done;
7152 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7153 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7154 progress_arg, cancel_cb, cancel_arg);
7155 done:
7156 free(commit_ref_name);
7157 return err;
7160 const struct got_error *
7161 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
7162 struct got_worktree *worktree, struct got_fileindex *fileindex,
7163 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
7164 struct got_repository *repo,
7165 got_worktree_checkout_cb progress_cb, void *progress_arg,
7166 got_cancel_cb cancel_cb, void *cancel_arg)
7168 const struct got_error *err;
7169 char *commit_ref_name;
7171 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7172 if (err)
7173 return err;
7175 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7176 if (err)
7177 goto done;
7179 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7180 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7181 progress_arg, cancel_cb, cancel_arg);
7182 done:
7183 free(commit_ref_name);
7184 return err;
7187 static const struct got_error *
7188 rebase_commit(struct got_object_id **new_commit_id,
7189 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7190 struct got_worktree *worktree, struct got_fileindex *fileindex,
7191 struct got_reference *tmp_branch, const char *committer,
7192 struct got_commit_object *orig_commit, const char *new_logmsg,
7193 int allow_conflict, struct got_repository *repo)
7195 const struct got_error *err, *sync_err;
7196 struct got_pathlist_head commitable_paths;
7197 struct collect_commitables_arg cc_arg;
7198 char *fileindex_path = NULL;
7199 struct got_reference *head_ref = NULL;
7200 struct got_object_id *head_commit_id = NULL;
7201 char *logmsg = NULL;
7203 memset(&cc_arg, 0, sizeof(cc_arg));
7204 TAILQ_INIT(&commitable_paths);
7205 *new_commit_id = NULL;
7207 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7209 err = get_fileindex_path(&fileindex_path, worktree);
7210 if (err)
7211 return err;
7213 cc_arg.commitable_paths = &commitable_paths;
7214 cc_arg.worktree = worktree;
7215 cc_arg.repo = repo;
7216 cc_arg.have_staged_files = 0;
7217 cc_arg.commit_conflicts = allow_conflict;
7219 * If possible get the status of individual files directly to
7220 * avoid crawling the entire work tree once per rebased commit.
7222 * Ideally, merged_paths would contain a list of commitables
7223 * we could use so we could skip worktree_status() entirely.
7224 * However, we would then need carefully keep track of cumulative
7225 * effects of operations such as file additions and deletions
7226 * in 'got histedit -f' (folding multiple commits into one),
7227 * and this extra complexity is not really worth it.
7229 if (merged_paths) {
7230 struct got_pathlist_entry *pe;
7231 TAILQ_FOREACH(pe, merged_paths, entry) {
7232 err = worktree_status(worktree, pe->path, fileindex,
7233 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7234 0);
7235 if (err)
7236 goto done;
7238 } else {
7239 err = worktree_status(worktree, "", fileindex, repo,
7240 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7241 if (err)
7242 goto done;
7245 if (TAILQ_EMPTY(&commitable_paths)) {
7246 /* No-op change; commit will be elided. */
7247 err = got_ref_delete(commit_ref, repo);
7248 if (err)
7249 goto done;
7250 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7251 goto done;
7254 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7255 if (err)
7256 goto done;
7258 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7259 if (err)
7260 goto done;
7262 if (new_logmsg) {
7263 logmsg = strdup(new_logmsg);
7264 if (logmsg == NULL) {
7265 err = got_error_from_errno("strdup");
7266 goto done;
7268 } else {
7269 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7270 if (err)
7271 goto done;
7274 /* NB: commit_worktree will call free(logmsg) */
7275 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7276 NULL, worktree, got_object_commit_get_author(orig_commit),
7277 committer ? committer :
7278 got_object_commit_get_committer(orig_commit), NULL,
7279 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7280 if (err)
7281 goto done;
7283 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7284 if (err)
7285 goto done;
7287 err = got_ref_delete(commit_ref, repo);
7288 if (err)
7289 goto done;
7291 err = update_fileindex_after_commit(worktree, &commitable_paths,
7292 *new_commit_id, fileindex, 0);
7293 sync_err = sync_fileindex(fileindex, fileindex_path);
7294 if (sync_err && err == NULL)
7295 err = sync_err;
7296 done:
7297 free(fileindex_path);
7298 free(head_commit_id);
7299 if (head_ref)
7300 got_ref_close(head_ref);
7301 if (err) {
7302 free(*new_commit_id);
7303 *new_commit_id = NULL;
7305 return err;
7308 const struct got_error *
7309 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7310 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7311 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7312 const char *committer, struct got_commit_object *orig_commit,
7313 struct got_object_id *orig_commit_id, int allow_conflict,
7314 struct got_repository *repo)
7316 const struct got_error *err;
7317 char *commit_ref_name;
7318 struct got_reference *commit_ref = NULL;
7319 struct got_object_id *commit_id = NULL;
7321 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7322 if (err)
7323 return err;
7325 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7326 if (err)
7327 goto done;
7328 err = got_ref_resolve(&commit_id, repo, commit_ref);
7329 if (err)
7330 goto done;
7331 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7332 err = got_error(GOT_ERR_REBASE_COMMITID);
7333 goto done;
7336 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7337 worktree, fileindex, tmp_branch, committer, orig_commit,
7338 NULL, allow_conflict, repo);
7339 done:
7340 if (commit_ref)
7341 got_ref_close(commit_ref);
7342 free(commit_ref_name);
7343 free(commit_id);
7344 return err;
7347 const struct got_error *
7348 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7349 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7350 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7351 const char *committer, struct got_commit_object *orig_commit,
7352 struct got_object_id *orig_commit_id, const char *new_logmsg,
7353 int allow_conflict, struct got_repository *repo)
7355 const struct got_error *err;
7356 char *commit_ref_name;
7357 struct got_reference *commit_ref = NULL;
7359 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7360 if (err)
7361 return err;
7363 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7364 if (err)
7365 goto done;
7367 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7368 worktree, fileindex, tmp_branch, committer, orig_commit,
7369 new_logmsg, allow_conflict, repo);
7370 done:
7371 if (commit_ref)
7372 got_ref_close(commit_ref);
7373 free(commit_ref_name);
7374 return err;
7377 const struct got_error *
7378 got_worktree_rebase_postpone(struct got_worktree *worktree,
7379 struct got_fileindex *fileindex)
7381 if (fileindex)
7382 got_fileindex_free(fileindex);
7383 return lock_worktree(worktree, LOCK_SH);
7386 static const struct got_error *
7387 delete_ref(const char *name, struct got_repository *repo)
7389 const struct got_error *err;
7390 struct got_reference *ref;
7392 err = got_ref_open(&ref, repo, name, 0);
7393 if (err) {
7394 if (err->code == GOT_ERR_NOT_REF)
7395 return NULL;
7396 return err;
7399 err = got_ref_delete(ref, repo);
7400 got_ref_close(ref);
7401 return err;
7404 static const struct got_error *
7405 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7407 const struct got_error *err;
7408 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7409 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7411 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7412 if (err)
7413 goto done;
7414 err = delete_ref(tmp_branch_name, repo);
7415 if (err)
7416 goto done;
7418 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7419 if (err)
7420 goto done;
7421 err = delete_ref(new_base_branch_ref_name, repo);
7422 if (err)
7423 goto done;
7425 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7426 if (err)
7427 goto done;
7428 err = delete_ref(branch_ref_name, repo);
7429 if (err)
7430 goto done;
7432 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7433 if (err)
7434 goto done;
7435 err = delete_ref(commit_ref_name, repo);
7436 if (err)
7437 goto done;
7439 done:
7440 free(tmp_branch_name);
7441 free(new_base_branch_ref_name);
7442 free(branch_ref_name);
7443 free(commit_ref_name);
7444 return err;
7447 static const struct got_error *
7448 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7449 struct got_object_id *new_commit_id, struct got_repository *repo)
7451 const struct got_error *err;
7452 struct got_reference *ref = NULL;
7453 struct got_object_id *old_commit_id = NULL;
7454 const char *branch_name = NULL;
7455 char *new_id_str = NULL;
7456 char *refname = NULL;
7458 branch_name = got_ref_get_name(branch);
7459 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7460 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7461 branch_name += 11;
7463 err = got_object_id_str(&new_id_str, new_commit_id);
7464 if (err)
7465 return err;
7467 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7468 new_id_str) == -1) {
7469 err = got_error_from_errno("asprintf");
7470 goto done;
7473 err = got_ref_resolve(&old_commit_id, repo, branch);
7474 if (err)
7475 goto done;
7477 err = got_ref_alloc(&ref, refname, old_commit_id);
7478 if (err)
7479 goto done;
7481 err = got_ref_write(ref, repo);
7482 done:
7483 free(new_id_str);
7484 free(refname);
7485 free(old_commit_id);
7486 if (ref)
7487 got_ref_close(ref);
7488 return err;
7491 const struct got_error *
7492 got_worktree_rebase_complete(struct got_worktree *worktree,
7493 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7494 struct got_reference *rebased_branch, struct got_repository *repo,
7495 int create_backup)
7497 const struct got_error *err, *unlockerr, *sync_err;
7498 struct got_object_id *new_head_commit_id = NULL;
7499 char *fileindex_path = NULL;
7501 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7502 if (err)
7503 return err;
7505 if (create_backup) {
7506 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7507 rebased_branch, new_head_commit_id, repo);
7508 if (err)
7509 goto done;
7512 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7513 if (err)
7514 goto done;
7516 err = got_ref_write(rebased_branch, repo);
7517 if (err)
7518 goto done;
7520 err = got_worktree_set_head_ref(worktree, rebased_branch);
7521 if (err)
7522 goto done;
7524 err = delete_rebase_refs(worktree, repo);
7525 if (err)
7526 goto done;
7528 err = get_fileindex_path(&fileindex_path, worktree);
7529 if (err)
7530 goto done;
7531 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7532 sync_err = sync_fileindex(fileindex, fileindex_path);
7533 if (sync_err && err == NULL)
7534 err = sync_err;
7535 done:
7536 got_fileindex_free(fileindex);
7537 free(fileindex_path);
7538 free(new_head_commit_id);
7539 unlockerr = lock_worktree(worktree, LOCK_SH);
7540 if (unlockerr && err == NULL)
7541 err = unlockerr;
7542 return err;
7545 static const struct got_error *
7546 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7547 struct got_object_id *id1, struct got_object_id *id2,
7548 struct got_repository *repo)
7550 const struct got_error *err;
7551 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7552 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7554 if (id1) {
7555 err = got_object_open_as_commit(&commit1, repo, id1);
7556 if (err)
7557 goto done;
7559 err = got_object_open_as_tree(&tree1, repo,
7560 got_object_commit_get_tree_id(commit1));
7561 if (err)
7562 goto done;
7565 if (id2) {
7566 err = got_object_open_as_commit(&commit2, repo, id2);
7567 if (err)
7568 goto done;
7570 err = got_object_open_as_tree(&tree2, repo,
7571 got_object_commit_get_tree_id(commit2));
7572 if (err)
7573 goto done;
7576 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7577 got_diff_tree_collect_changed_paths, paths, 0);
7578 if (err)
7579 goto done;
7580 done:
7581 if (commit1)
7582 got_object_commit_close(commit1);
7583 if (commit2)
7584 got_object_commit_close(commit2);
7585 if (tree1)
7586 got_object_tree_close(tree1);
7587 if (tree2)
7588 got_object_tree_close(tree2);
7589 return err;
7592 static const struct got_error *
7593 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7594 struct got_object_id *id1, struct got_object_id *id2,
7595 const char *path_prefix, struct got_repository *repo)
7597 const struct got_error *err;
7598 struct got_pathlist_head merged_paths;
7599 struct got_pathlist_entry *pe;
7600 char *abspath = NULL, *wt_path = NULL;
7602 TAILQ_INIT(&merged_paths);
7604 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7605 if (err)
7606 goto done;
7608 TAILQ_FOREACH(pe, &merged_paths, entry) {
7609 struct got_diff_changed_path *change = pe->data;
7611 if (change->status != GOT_STATUS_ADD)
7612 continue;
7614 if (got_path_is_root_dir(path_prefix)) {
7615 wt_path = strdup(pe->path);
7616 if (wt_path == NULL) {
7617 err = got_error_from_errno("strdup");
7618 goto done;
7620 } else {
7621 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7622 err = got_error_from_errno("asprintf");
7623 goto done;
7626 err = got_path_skip_common_ancestor(&wt_path,
7627 path_prefix, abspath);
7628 if (err)
7629 goto done;
7630 free(abspath);
7631 abspath = NULL;
7634 err = got_pathlist_append(added_paths, wt_path, NULL);
7635 if (err)
7636 goto done;
7637 wt_path = NULL;
7640 done:
7641 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7642 free(abspath);
7643 free(wt_path);
7644 return err;
7647 static const struct got_error *
7648 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7649 struct got_object_id *id, const char *path_prefix,
7650 struct got_repository *repo)
7652 const struct got_error *err;
7653 struct got_commit_object *commit = NULL;
7654 struct got_object_qid *pid;
7656 err = got_object_open_as_commit(&commit, repo, id);
7657 if (err)
7658 goto done;
7660 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7662 err = get_paths_added_between_commits(added_paths,
7663 pid ? &pid->id : NULL, id, path_prefix, repo);
7664 if (err)
7665 goto done;
7666 done:
7667 if (commit)
7668 got_object_commit_close(commit);
7669 return err;
7672 const struct got_error *
7673 got_worktree_rebase_abort(struct got_worktree *worktree,
7674 struct got_fileindex *fileindex, struct got_repository *repo,
7675 struct got_reference *new_base_branch,
7676 got_worktree_checkout_cb progress_cb, void *progress_arg)
7678 const struct got_error *err, *unlockerr, *sync_err;
7679 struct got_reference *resolved = NULL;
7680 struct got_object_id *commit_id = NULL;
7681 struct got_object_id *merged_commit_id = NULL;
7682 struct got_commit_object *commit = NULL;
7683 char *fileindex_path = NULL;
7684 char *commit_ref_name = NULL;
7685 struct got_reference *commit_ref = NULL;
7686 struct revert_file_args rfa;
7687 struct got_object_id *tree_id = NULL;
7688 struct got_pathlist_head added_paths;
7690 TAILQ_INIT(&added_paths);
7692 err = lock_worktree(worktree, LOCK_EX);
7693 if (err)
7694 return err;
7696 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7697 if (err)
7698 goto done;
7700 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7701 if (err)
7702 goto done;
7704 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7705 if (err)
7706 goto done;
7709 * Determine which files in added status can be safely removed
7710 * from disk while reverting changes in the work tree.
7711 * We want to avoid deleting unrelated files which were added by
7712 * the user for conflict resolution purposes.
7714 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7715 got_worktree_get_path_prefix(worktree), repo);
7716 if (err)
7717 goto done;
7719 err = got_ref_open(&resolved, repo,
7720 got_ref_get_symref_target(new_base_branch), 0);
7721 if (err)
7722 goto done;
7724 err = got_worktree_set_head_ref(worktree, resolved);
7725 if (err)
7726 goto done;
7729 * XXX commits to the base branch could have happened while
7730 * we were busy rebasing; should we store the original commit ID
7731 * when rebase begins and read it back here?
7733 err = got_ref_resolve(&commit_id, repo, resolved);
7734 if (err)
7735 goto done;
7737 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7738 if (err)
7739 goto done;
7741 err = got_object_open_as_commit(&commit, repo,
7742 worktree->base_commit_id);
7743 if (err)
7744 goto done;
7746 err = got_object_id_by_path(&tree_id, repo, commit,
7747 worktree->path_prefix);
7748 if (err)
7749 goto done;
7751 err = delete_rebase_refs(worktree, repo);
7752 if (err)
7753 goto done;
7755 err = get_fileindex_path(&fileindex_path, worktree);
7756 if (err)
7757 goto done;
7759 rfa.worktree = worktree;
7760 rfa.fileindex = fileindex;
7761 rfa.progress_cb = progress_cb;
7762 rfa.progress_arg = progress_arg;
7763 rfa.patch_cb = NULL;
7764 rfa.patch_arg = NULL;
7765 rfa.repo = repo;
7766 rfa.unlink_added_files = 1;
7767 rfa.added_files_to_unlink = &added_paths;
7768 err = worktree_status(worktree, "", fileindex, repo,
7769 revert_file, &rfa, NULL, NULL, 1, 0);
7770 if (err)
7771 goto sync;
7773 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7774 repo, progress_cb, progress_arg, NULL, NULL);
7775 sync:
7776 sync_err = sync_fileindex(fileindex, fileindex_path);
7777 if (sync_err && err == NULL)
7778 err = sync_err;
7779 done:
7780 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7781 got_ref_close(resolved);
7782 free(tree_id);
7783 free(commit_id);
7784 free(merged_commit_id);
7785 if (commit)
7786 got_object_commit_close(commit);
7787 if (fileindex)
7788 got_fileindex_free(fileindex);
7789 free(fileindex_path);
7790 free(commit_ref_name);
7791 if (commit_ref)
7792 got_ref_close(commit_ref);
7794 unlockerr = lock_worktree(worktree, LOCK_SH);
7795 if (unlockerr && err == NULL)
7796 err = unlockerr;
7797 return err;
7800 const struct got_error *
7801 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7802 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7803 struct got_fileindex **fileindex, struct got_worktree *worktree,
7804 struct got_repository *repo)
7806 const struct got_error *err = NULL;
7807 char *tmp_branch_name = NULL;
7808 char *branch_ref_name = NULL;
7809 char *base_commit_ref_name = NULL;
7810 char *fileindex_path = NULL;
7811 struct check_rebase_ok_arg ok_arg;
7812 struct got_reference *wt_branch = NULL;
7813 struct got_reference *base_commit_ref = NULL;
7815 *tmp_branch = NULL;
7816 *branch_ref = NULL;
7817 *base_commit_id = NULL;
7818 *fileindex = NULL;
7820 err = lock_worktree(worktree, LOCK_EX);
7821 if (err)
7822 return err;
7824 err = open_fileindex(fileindex, &fileindex_path, worktree);
7825 if (err)
7826 goto done;
7828 ok_arg.worktree = worktree;
7829 ok_arg.repo = repo;
7830 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7831 &ok_arg);
7832 if (err)
7833 goto done;
7835 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7836 if (err)
7837 goto done;
7839 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7840 if (err)
7841 goto done;
7843 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7844 worktree);
7845 if (err)
7846 goto done;
7848 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7849 0);
7850 if (err)
7851 goto done;
7853 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7854 if (err)
7855 goto done;
7857 err = got_ref_write(*branch_ref, repo);
7858 if (err)
7859 goto done;
7861 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7862 worktree->base_commit_id);
7863 if (err)
7864 goto done;
7865 err = got_ref_write(base_commit_ref, repo);
7866 if (err)
7867 goto done;
7868 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7869 if (*base_commit_id == NULL) {
7870 err = got_error_from_errno("got_object_id_dup");
7871 goto done;
7874 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7875 worktree->base_commit_id);
7876 if (err)
7877 goto done;
7878 err = got_ref_write(*tmp_branch, repo);
7879 if (err)
7880 goto done;
7882 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7883 if (err)
7884 goto done;
7885 done:
7886 free(fileindex_path);
7887 free(tmp_branch_name);
7888 free(branch_ref_name);
7889 free(base_commit_ref_name);
7890 if (wt_branch)
7891 got_ref_close(wt_branch);
7892 if (err) {
7893 if (*branch_ref) {
7894 got_ref_close(*branch_ref);
7895 *branch_ref = NULL;
7897 if (*tmp_branch) {
7898 got_ref_close(*tmp_branch);
7899 *tmp_branch = NULL;
7901 free(*base_commit_id);
7902 if (*fileindex) {
7903 got_fileindex_free(*fileindex);
7904 *fileindex = NULL;
7906 lock_worktree(worktree, LOCK_SH);
7908 return err;
7911 const struct got_error *
7912 got_worktree_histedit_postpone(struct got_worktree *worktree,
7913 struct got_fileindex *fileindex)
7915 if (fileindex)
7916 got_fileindex_free(fileindex);
7917 return lock_worktree(worktree, LOCK_SH);
7920 const struct got_error *
7921 got_worktree_histedit_in_progress(int *in_progress,
7922 struct got_worktree *worktree)
7924 const struct got_error *err;
7925 char *tmp_branch_name = NULL;
7927 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7928 if (err)
7929 return err;
7931 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7932 free(tmp_branch_name);
7933 return NULL;
7936 const struct got_error *
7937 got_worktree_histedit_continue(struct got_object_id **commit_id,
7938 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7939 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7940 struct got_worktree *worktree, struct got_repository *repo)
7942 const struct got_error *err;
7943 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7944 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7945 struct got_reference *commit_ref = NULL;
7946 struct got_reference *base_commit_ref = NULL;
7947 char *fileindex_path = NULL;
7948 int have_staged_files = 0;
7950 *commit_id = NULL;
7951 *tmp_branch = 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 (*fileindex) {
8028 got_fileindex_free(*fileindex);
8029 *fileindex = NULL;
8031 lock_worktree(worktree, LOCK_EX);
8033 return err;
8036 static const struct got_error *
8037 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
8039 const struct got_error *err;
8040 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
8041 char *branch_ref_name = NULL, *commit_ref_name = NULL;
8043 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
8044 if (err)
8045 goto done;
8046 err = delete_ref(tmp_branch_name, repo);
8047 if (err)
8048 goto done;
8050 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
8051 worktree);
8052 if (err)
8053 goto done;
8054 err = delete_ref(base_commit_ref_name, repo);
8055 if (err)
8056 goto done;
8058 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
8059 if (err)
8060 goto done;
8061 err = delete_ref(branch_ref_name, repo);
8062 if (err)
8063 goto done;
8065 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8066 if (err)
8067 goto done;
8068 err = delete_ref(commit_ref_name, repo);
8069 if (err)
8070 goto done;
8071 done:
8072 free(tmp_branch_name);
8073 free(base_commit_ref_name);
8074 free(branch_ref_name);
8075 free(commit_ref_name);
8076 return err;
8079 const struct got_error *
8080 got_worktree_histedit_abort(struct got_worktree *worktree,
8081 struct got_fileindex *fileindex, struct got_repository *repo,
8082 struct got_reference *branch, struct got_object_id *base_commit_id,
8083 got_worktree_checkout_cb progress_cb, void *progress_arg)
8085 const struct got_error *err, *unlockerr, *sync_err;
8086 struct got_reference *resolved = NULL;
8087 char *fileindex_path = NULL;
8088 struct got_object_id *merged_commit_id = NULL;
8089 struct got_commit_object *commit = NULL;
8090 char *commit_ref_name = NULL;
8091 struct got_reference *commit_ref = NULL;
8092 struct got_object_id *tree_id = NULL;
8093 struct revert_file_args rfa;
8094 struct got_pathlist_head added_paths;
8096 TAILQ_INIT(&added_paths);
8098 err = lock_worktree(worktree, LOCK_EX);
8099 if (err)
8100 return err;
8102 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8103 if (err)
8104 goto done;
8106 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8107 if (err) {
8108 if (err->code != GOT_ERR_NOT_REF)
8109 goto done;
8110 /* Can happen on early abort due to invalid histedit script. */
8111 commit_ref = NULL;
8114 if (commit_ref) {
8115 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8116 if (err)
8117 goto done;
8120 * Determine which files in added status can be safely removed
8121 * from disk while reverting changes in the work tree.
8122 * We want to avoid deleting unrelated files added by the
8123 * user during conflict resolution or during histedit -e.
8125 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
8126 got_worktree_get_path_prefix(worktree), repo);
8127 if (err)
8128 goto done;
8131 err = got_ref_open(&resolved, repo,
8132 got_ref_get_symref_target(branch), 0);
8133 if (err)
8134 goto done;
8136 err = got_worktree_set_head_ref(worktree, resolved);
8137 if (err)
8138 goto done;
8140 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
8141 if (err)
8142 goto done;
8144 err = got_object_open_as_commit(&commit, repo,
8145 worktree->base_commit_id);
8146 if (err)
8147 goto done;
8149 err = got_object_id_by_path(&tree_id, repo, commit,
8150 worktree->path_prefix);
8151 if (err)
8152 goto done;
8154 err = delete_histedit_refs(worktree, repo);
8155 if (err)
8156 goto done;
8158 err = get_fileindex_path(&fileindex_path, worktree);
8159 if (err)
8160 goto done;
8162 rfa.worktree = worktree;
8163 rfa.fileindex = fileindex;
8164 rfa.progress_cb = progress_cb;
8165 rfa.progress_arg = progress_arg;
8166 rfa.patch_cb = NULL;
8167 rfa.patch_arg = NULL;
8168 rfa.repo = repo;
8169 rfa.unlink_added_files = 1;
8170 rfa.added_files_to_unlink = &added_paths;
8171 err = worktree_status(worktree, "", fileindex, repo,
8172 revert_file, &rfa, NULL, NULL, 1, 0);
8173 if (err)
8174 goto sync;
8176 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8177 repo, progress_cb, progress_arg, NULL, NULL);
8178 sync:
8179 sync_err = sync_fileindex(fileindex, fileindex_path);
8180 if (sync_err && err == NULL)
8181 err = sync_err;
8182 done:
8183 if (resolved)
8184 got_ref_close(resolved);
8185 if (commit_ref)
8186 got_ref_close(commit_ref);
8187 free(merged_commit_id);
8188 free(tree_id);
8189 free(fileindex_path);
8190 free(commit_ref_name);
8192 unlockerr = lock_worktree(worktree, LOCK_SH);
8193 if (unlockerr && err == NULL)
8194 err = unlockerr;
8195 return err;
8198 const struct got_error *
8199 got_worktree_histedit_complete(struct got_worktree *worktree,
8200 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8201 struct got_reference *edited_branch, struct got_repository *repo)
8203 const struct got_error *err, *unlockerr, *sync_err;
8204 struct got_object_id *new_head_commit_id = NULL;
8205 struct got_reference *resolved = NULL;
8206 char *fileindex_path = NULL;
8208 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8209 if (err)
8210 return err;
8212 err = got_ref_open(&resolved, repo,
8213 got_ref_get_symref_target(edited_branch), 0);
8214 if (err)
8215 goto done;
8217 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8218 resolved, new_head_commit_id, repo);
8219 if (err)
8220 goto done;
8222 err = got_ref_change_ref(resolved, new_head_commit_id);
8223 if (err)
8224 goto done;
8226 err = got_ref_write(resolved, repo);
8227 if (err)
8228 goto done;
8230 err = got_worktree_set_head_ref(worktree, resolved);
8231 if (err)
8232 goto done;
8234 err = delete_histedit_refs(worktree, repo);
8235 if (err)
8236 goto done;
8238 err = get_fileindex_path(&fileindex_path, worktree);
8239 if (err)
8240 goto done;
8241 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8242 sync_err = sync_fileindex(fileindex, fileindex_path);
8243 if (sync_err && err == NULL)
8244 err = sync_err;
8245 done:
8246 got_fileindex_free(fileindex);
8247 free(fileindex_path);
8248 free(new_head_commit_id);
8249 unlockerr = lock_worktree(worktree, LOCK_SH);
8250 if (unlockerr && err == NULL)
8251 err = unlockerr;
8252 return err;
8255 const struct got_error *
8256 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8257 struct got_object_id *commit_id, struct got_repository *repo)
8259 const struct got_error *err;
8260 char *commit_ref_name;
8262 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8263 if (err)
8264 return err;
8266 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8267 if (err)
8268 goto done;
8270 err = delete_ref(commit_ref_name, repo);
8271 done:
8272 free(commit_ref_name);
8273 return err;
8276 const struct got_error *
8277 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8278 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8279 struct got_worktree *worktree, const char *refname,
8280 struct got_repository *repo)
8282 const struct got_error *err = NULL;
8283 char *fileindex_path = NULL;
8284 struct check_rebase_ok_arg ok_arg;
8286 *fileindex = NULL;
8287 *branch_ref = NULL;
8288 *base_branch_ref = NULL;
8290 err = lock_worktree(worktree, LOCK_EX);
8291 if (err)
8292 return err;
8294 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8295 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8296 "cannot integrate a branch into itself; "
8297 "update -b or different branch name required");
8298 goto done;
8301 err = open_fileindex(fileindex, &fileindex_path, worktree);
8302 if (err)
8303 goto done;
8305 /* Preconditions are the same as for rebase. */
8306 ok_arg.worktree = worktree;
8307 ok_arg.repo = repo;
8308 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8309 &ok_arg);
8310 if (err)
8311 goto done;
8313 err = got_ref_open(branch_ref, repo, refname, 1);
8314 if (err)
8315 goto done;
8317 err = got_ref_open(base_branch_ref, repo,
8318 got_worktree_get_head_ref_name(worktree), 1);
8319 done:
8320 if (err) {
8321 if (*branch_ref) {
8322 got_ref_close(*branch_ref);
8323 *branch_ref = NULL;
8325 if (*base_branch_ref) {
8326 got_ref_close(*base_branch_ref);
8327 *base_branch_ref = NULL;
8329 if (*fileindex) {
8330 got_fileindex_free(*fileindex);
8331 *fileindex = NULL;
8333 lock_worktree(worktree, LOCK_SH);
8335 return err;
8338 const struct got_error *
8339 got_worktree_integrate_continue(struct got_worktree *worktree,
8340 struct got_fileindex *fileindex, struct got_repository *repo,
8341 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8342 got_worktree_checkout_cb progress_cb, void *progress_arg,
8343 got_cancel_cb cancel_cb, void *cancel_arg)
8345 const struct got_error *err = NULL, *sync_err, *unlockerr;
8346 char *fileindex_path = NULL;
8347 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8348 struct got_commit_object *commit = NULL;
8350 err = get_fileindex_path(&fileindex_path, worktree);
8351 if (err)
8352 goto done;
8354 err = got_ref_resolve(&commit_id, repo, branch_ref);
8355 if (err)
8356 goto done;
8358 err = got_object_open_as_commit(&commit, repo, commit_id);
8359 if (err)
8360 goto done;
8362 err = got_object_id_by_path(&tree_id, repo, commit,
8363 worktree->path_prefix);
8364 if (err)
8365 goto done;
8367 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8368 if (err)
8369 goto done;
8371 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8372 progress_cb, progress_arg, cancel_cb, cancel_arg);
8373 if (err)
8374 goto sync;
8376 err = got_ref_change_ref(base_branch_ref, commit_id);
8377 if (err)
8378 goto sync;
8380 err = got_ref_write(base_branch_ref, repo);
8381 if (err)
8382 goto sync;
8384 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8385 sync:
8386 sync_err = sync_fileindex(fileindex, fileindex_path);
8387 if (sync_err && err == NULL)
8388 err = sync_err;
8390 done:
8391 unlockerr = got_ref_unlock(branch_ref);
8392 if (unlockerr && err == NULL)
8393 err = unlockerr;
8394 got_ref_close(branch_ref);
8396 unlockerr = got_ref_unlock(base_branch_ref);
8397 if (unlockerr && err == NULL)
8398 err = unlockerr;
8399 got_ref_close(base_branch_ref);
8401 got_fileindex_free(fileindex);
8402 free(fileindex_path);
8403 free(tree_id);
8404 if (commit)
8405 got_object_commit_close(commit);
8407 unlockerr = lock_worktree(worktree, LOCK_SH);
8408 if (unlockerr && err == NULL)
8409 err = unlockerr;
8410 return err;
8413 const struct got_error *
8414 got_worktree_integrate_abort(struct got_worktree *worktree,
8415 struct got_fileindex *fileindex, struct got_repository *repo,
8416 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8418 const struct got_error *err = NULL, *unlockerr = NULL;
8420 got_fileindex_free(fileindex);
8422 err = lock_worktree(worktree, LOCK_SH);
8424 unlockerr = got_ref_unlock(branch_ref);
8425 if (unlockerr && err == NULL)
8426 err = unlockerr;
8427 got_ref_close(branch_ref);
8429 unlockerr = got_ref_unlock(base_branch_ref);
8430 if (unlockerr && err == NULL)
8431 err = unlockerr;
8432 got_ref_close(base_branch_ref);
8434 return err;
8437 const struct got_error *
8438 got_worktree_merge_postpone(struct got_worktree *worktree,
8439 struct got_fileindex *fileindex)
8441 const struct got_error *err, *sync_err;
8442 char *fileindex_path = NULL;
8444 err = get_fileindex_path(&fileindex_path, worktree);
8445 if (err)
8446 goto done;
8448 sync_err = sync_fileindex(fileindex, fileindex_path);
8450 err = lock_worktree(worktree, LOCK_SH);
8451 if (sync_err && err == NULL)
8452 err = sync_err;
8453 done:
8454 got_fileindex_free(fileindex);
8455 free(fileindex_path);
8456 return err;
8459 static const struct got_error *
8460 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8462 const struct got_error *err;
8463 char *branch_refname = NULL, *commit_refname = NULL;
8465 err = get_merge_branch_ref_name(&branch_refname, worktree);
8466 if (err)
8467 goto done;
8468 err = delete_ref(branch_refname, repo);
8469 if (err)
8470 goto done;
8472 err = get_merge_commit_ref_name(&commit_refname, worktree);
8473 if (err)
8474 goto done;
8475 err = delete_ref(commit_refname, repo);
8476 if (err)
8477 goto done;
8479 done:
8480 free(branch_refname);
8481 free(commit_refname);
8482 return err;
8485 struct merge_commit_msg_arg {
8486 struct got_worktree *worktree;
8487 const char *branch_name;
8490 static const struct got_error *
8491 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8492 const char *diff_path, char **logmsg, void *arg)
8494 struct merge_commit_msg_arg *a = arg;
8496 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8497 got_worktree_get_head_ref_name(a->worktree)) == -1)
8498 return got_error_from_errno("asprintf");
8500 return NULL;
8504 const struct got_error *
8505 got_worktree_merge_branch(struct got_worktree *worktree,
8506 struct got_fileindex *fileindex,
8507 struct got_object_id *yca_commit_id,
8508 struct got_object_id *branch_tip,
8509 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8510 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8512 const struct got_error *err;
8513 char *fileindex_path = NULL;
8514 struct check_mixed_commits_args cma;
8516 err = get_fileindex_path(&fileindex_path, worktree);
8517 if (err)
8518 goto done;
8520 cma.worktree = worktree;
8521 cma.cancel_cb = cancel_cb;
8522 cma.cancel_arg = cancel_arg;
8524 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8525 &cma);
8526 if (err)
8527 goto done;
8529 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8530 branch_tip, repo, progress_cb, progress_arg,
8531 cancel_cb, cancel_arg);
8532 done:
8533 free(fileindex_path);
8534 return err;
8537 const struct got_error *
8538 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8539 struct got_worktree *worktree, struct got_fileindex *fileindex,
8540 const char *author, const char *committer, int allow_bad_symlinks,
8541 struct got_object_id *branch_tip, const char *branch_name,
8542 int allow_conflict, struct got_repository *repo,
8543 got_worktree_status_cb status_cb, void *status_arg)
8546 const struct got_error *err = NULL, *sync_err;
8547 struct got_pathlist_head commitable_paths;
8548 struct collect_commitables_arg cc_arg;
8549 struct got_pathlist_entry *pe;
8550 struct got_reference *head_ref = NULL;
8551 struct got_object_id *head_commit_id = NULL;
8552 int have_staged_files = 0;
8553 struct merge_commit_msg_arg mcm_arg;
8554 char *fileindex_path = NULL;
8556 memset(&cc_arg, 0, sizeof(cc_arg));
8557 *new_commit_id = NULL;
8559 TAILQ_INIT(&commitable_paths);
8561 err = get_fileindex_path(&fileindex_path, worktree);
8562 if (err)
8563 goto done;
8565 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8566 if (err)
8567 goto done;
8569 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8570 if (err)
8571 goto done;
8573 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8574 &have_staged_files);
8575 if (err && err->code != GOT_ERR_CANCELLED)
8576 goto done;
8577 if (have_staged_files) {
8578 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8579 goto done;
8582 cc_arg.commitable_paths = &commitable_paths;
8583 cc_arg.worktree = worktree;
8584 cc_arg.fileindex = fileindex;
8585 cc_arg.repo = repo;
8586 cc_arg.have_staged_files = have_staged_files;
8587 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8588 cc_arg.commit_conflicts = allow_conflict;
8589 err = worktree_status(worktree, "", fileindex, repo,
8590 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8591 if (err)
8592 goto done;
8594 mcm_arg.worktree = worktree;
8595 mcm_arg.branch_name = branch_name;
8596 err = commit_worktree(new_commit_id, &commitable_paths,
8597 head_commit_id, branch_tip, worktree, author, committer, NULL,
8598 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8599 if (err)
8600 goto done;
8602 err = update_fileindex_after_commit(worktree, &commitable_paths,
8603 *new_commit_id, fileindex, have_staged_files);
8604 sync_err = sync_fileindex(fileindex, fileindex_path);
8605 if (sync_err && err == NULL)
8606 err = sync_err;
8607 done:
8608 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8609 struct got_commitable *ct = pe->data;
8611 free_commitable(ct);
8613 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8614 free(fileindex_path);
8615 return err;
8618 const struct got_error *
8619 got_worktree_merge_complete(struct got_worktree *worktree,
8620 struct got_fileindex *fileindex, struct got_repository *repo)
8622 const struct got_error *err, *unlockerr, *sync_err;
8623 char *fileindex_path = NULL;
8625 err = delete_merge_refs(worktree, repo);
8626 if (err)
8627 goto done;
8629 err = get_fileindex_path(&fileindex_path, worktree);
8630 if (err)
8631 goto done;
8632 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8633 sync_err = sync_fileindex(fileindex, fileindex_path);
8634 if (sync_err && err == NULL)
8635 err = sync_err;
8636 done:
8637 got_fileindex_free(fileindex);
8638 free(fileindex_path);
8639 unlockerr = lock_worktree(worktree, LOCK_SH);
8640 if (unlockerr && err == NULL)
8641 err = unlockerr;
8642 return err;
8645 const struct got_error *
8646 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8647 struct got_repository *repo)
8649 const struct got_error *err;
8650 char *branch_refname = NULL;
8651 struct got_reference *branch_ref = NULL;
8653 *in_progress = 0;
8655 err = get_merge_branch_ref_name(&branch_refname, worktree);
8656 if (err)
8657 return err;
8658 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8659 free(branch_refname);
8660 if (err) {
8661 if (err->code != GOT_ERR_NOT_REF)
8662 return err;
8663 } else
8664 *in_progress = 1;
8666 return NULL;
8669 const struct got_error *got_worktree_merge_prepare(
8670 struct got_fileindex **fileindex, struct got_worktree *worktree,
8671 struct got_repository *repo)
8673 const struct got_error *err = NULL;
8674 char *fileindex_path = NULL;
8675 struct got_reference *wt_branch = NULL;
8676 struct got_object_id *wt_branch_tip = NULL;
8677 struct check_rebase_ok_arg ok_arg;
8679 *fileindex = NULL;
8681 err = lock_worktree(worktree, LOCK_EX);
8682 if (err)
8683 return err;
8685 err = open_fileindex(fileindex, &fileindex_path, worktree);
8686 if (err)
8687 goto done;
8689 /* Preconditions are the same as for rebase. */
8690 ok_arg.worktree = worktree;
8691 ok_arg.repo = repo;
8692 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8693 &ok_arg);
8694 if (err)
8695 goto done;
8697 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8698 0);
8699 if (err)
8700 goto done;
8702 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8703 if (err)
8704 goto done;
8706 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8707 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8708 goto done;
8711 done:
8712 free(fileindex_path);
8713 if (wt_branch)
8714 got_ref_close(wt_branch);
8715 free(wt_branch_tip);
8716 if (err) {
8717 if (*fileindex) {
8718 got_fileindex_free(*fileindex);
8719 *fileindex = NULL;
8721 lock_worktree(worktree, LOCK_SH);
8723 return err;
8726 const struct got_error *got_worktree_merge_write_refs(
8727 struct got_worktree *worktree, struct got_reference *branch,
8728 struct got_repository *repo)
8730 const struct got_error *err = NULL;
8731 char *branch_refname = NULL, *commit_refname = NULL;
8732 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8733 struct got_object_id *branch_tip = NULL;
8735 err = get_merge_branch_ref_name(&branch_refname, worktree);
8736 if (err)
8737 return err;
8739 err = get_merge_commit_ref_name(&commit_refname, worktree);
8740 if (err)
8741 return err;
8743 err = got_ref_resolve(&branch_tip, repo, branch);
8744 if (err)
8745 goto done;
8747 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8748 if (err)
8749 goto done;
8750 err = got_ref_write(branch_ref, repo);
8751 if (err)
8752 goto done;
8754 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8755 if (err)
8756 goto done;
8757 err = got_ref_write(commit_ref, repo);
8758 if (err)
8759 goto done;
8761 done:
8762 free(branch_refname);
8763 free(commit_refname);
8764 if (branch_ref)
8765 got_ref_close(branch_ref);
8766 if (commit_ref)
8767 got_ref_close(commit_ref);
8768 free(branch_tip);
8769 return err;
8772 const struct got_error *
8773 got_worktree_merge_continue(char **branch_name,
8774 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8775 struct got_worktree *worktree, struct got_repository *repo)
8777 const struct got_error *err;
8778 char *commit_refname = NULL, *branch_refname = NULL;
8779 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8780 char *fileindex_path = NULL;
8781 int have_staged_files = 0;
8783 *branch_name = NULL;
8784 *branch_tip = NULL;
8785 *fileindex = NULL;
8787 err = lock_worktree(worktree, LOCK_EX);
8788 if (err)
8789 return err;
8791 err = open_fileindex(fileindex, &fileindex_path, worktree);
8792 if (err)
8793 goto done;
8795 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8796 &have_staged_files);
8797 if (err && err->code != GOT_ERR_CANCELLED)
8798 goto done;
8799 if (have_staged_files) {
8800 err = got_error(GOT_ERR_STAGED_PATHS);
8801 goto done;
8804 err = get_merge_branch_ref_name(&branch_refname, worktree);
8805 if (err)
8806 goto done;
8808 err = get_merge_commit_ref_name(&commit_refname, worktree);
8809 if (err)
8810 goto done;
8812 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8813 if (err)
8814 goto done;
8816 if (!got_ref_is_symbolic(branch_ref)) {
8817 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8818 "%s is not a symbolic reference",
8819 got_ref_get_name(branch_ref));
8820 goto done;
8822 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8823 if (*branch_name == NULL) {
8824 err = got_error_from_errno("strdup");
8825 goto done;
8828 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8829 if (err)
8830 goto done;
8832 err = got_ref_resolve(branch_tip, repo, commit_ref);
8833 if (err)
8834 goto done;
8835 done:
8836 free(commit_refname);
8837 free(branch_refname);
8838 free(fileindex_path);
8839 if (commit_ref)
8840 got_ref_close(commit_ref);
8841 if (branch_ref)
8842 got_ref_close(branch_ref);
8843 if (err) {
8844 if (*branch_name) {
8845 free(*branch_name);
8846 *branch_name = NULL;
8848 free(*branch_tip);
8849 *branch_tip = NULL;
8850 if (*fileindex) {
8851 got_fileindex_free(*fileindex);
8852 *fileindex = NULL;
8854 lock_worktree(worktree, LOCK_SH);
8856 return err;
8859 const struct got_error *
8860 got_worktree_merge_abort(struct got_worktree *worktree,
8861 struct got_fileindex *fileindex, struct got_repository *repo,
8862 got_worktree_checkout_cb progress_cb, void *progress_arg)
8864 const struct got_error *err, *unlockerr, *sync_err;
8865 struct got_commit_object *commit = NULL;
8866 char *fileindex_path = NULL;
8867 struct revert_file_args rfa;
8868 char *commit_ref_name = NULL;
8869 struct got_reference *commit_ref = NULL;
8870 struct got_object_id *merged_commit_id = NULL;
8871 struct got_object_id *tree_id = NULL;
8872 struct got_pathlist_head added_paths;
8874 TAILQ_INIT(&added_paths);
8876 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8877 if (err)
8878 goto done;
8880 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8881 if (err)
8882 goto done;
8884 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8885 if (err)
8886 goto done;
8889 * Determine which files in added status can be safely removed
8890 * from disk while reverting changes in the work tree.
8891 * We want to avoid deleting unrelated files which were added by
8892 * the user for conflict resolution purposes.
8894 err = get_paths_added_between_commits(&added_paths,
8895 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8896 got_worktree_get_path_prefix(worktree), repo);
8897 if (err)
8898 goto done;
8901 err = got_object_open_as_commit(&commit, repo,
8902 worktree->base_commit_id);
8903 if (err)
8904 goto done;
8906 err = got_object_id_by_path(&tree_id, repo, commit,
8907 worktree->path_prefix);
8908 if (err)
8909 goto done;
8911 err = delete_merge_refs(worktree, repo);
8912 if (err)
8913 goto done;
8915 err = get_fileindex_path(&fileindex_path, worktree);
8916 if (err)
8917 goto done;
8919 rfa.worktree = worktree;
8920 rfa.fileindex = fileindex;
8921 rfa.progress_cb = progress_cb;
8922 rfa.progress_arg = progress_arg;
8923 rfa.patch_cb = NULL;
8924 rfa.patch_arg = NULL;
8925 rfa.repo = repo;
8926 rfa.unlink_added_files = 1;
8927 rfa.added_files_to_unlink = &added_paths;
8928 err = worktree_status(worktree, "", fileindex, repo,
8929 revert_file, &rfa, NULL, NULL, 1, 0);
8930 if (err)
8931 goto sync;
8933 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8934 repo, progress_cb, progress_arg, NULL, NULL);
8935 sync:
8936 sync_err = sync_fileindex(fileindex, fileindex_path);
8937 if (sync_err && err == NULL)
8938 err = sync_err;
8939 done:
8940 free(tree_id);
8941 free(merged_commit_id);
8942 if (commit)
8943 got_object_commit_close(commit);
8944 if (fileindex)
8945 got_fileindex_free(fileindex);
8946 free(fileindex_path);
8947 if (commit_ref)
8948 got_ref_close(commit_ref);
8949 free(commit_ref_name);
8951 unlockerr = lock_worktree(worktree, LOCK_SH);
8952 if (unlockerr && err == NULL)
8953 err = unlockerr;
8954 return err;
8957 struct check_stage_ok_arg {
8958 struct got_object_id *head_commit_id;
8959 struct got_worktree *worktree;
8960 struct got_fileindex *fileindex;
8961 struct got_repository *repo;
8962 int have_changes;
8965 static const struct got_error *
8966 check_stage_ok(void *arg, unsigned char status,
8967 unsigned char staged_status, const char *relpath,
8968 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8969 struct got_object_id *commit_id, int dirfd, const char *de_name)
8971 struct check_stage_ok_arg *a = arg;
8972 const struct got_error *err = NULL;
8973 struct got_fileindex_entry *ie;
8974 struct got_object_id base_commit_id;
8975 struct got_object_id *base_commit_idp = NULL;
8976 char *in_repo_path = NULL, *p;
8978 if (status == GOT_STATUS_UNVERSIONED ||
8979 status == GOT_STATUS_NO_CHANGE)
8980 return NULL;
8981 if (status == GOT_STATUS_NONEXISTENT)
8982 return got_error_set_errno(ENOENT, relpath);
8984 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8985 if (ie == NULL)
8986 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8988 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8989 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8990 relpath) == -1)
8991 return got_error_from_errno("asprintf");
8993 if (got_fileindex_entry_has_commit(ie)) {
8994 base_commit_idp = got_fileindex_entry_get_commit_id(
8995 &base_commit_id, ie);
8998 if (status == GOT_STATUS_CONFLICT) {
8999 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
9000 goto done;
9001 } else if (status != GOT_STATUS_ADD &&
9002 status != GOT_STATUS_MODIFY &&
9003 status != GOT_STATUS_DELETE) {
9004 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
9005 goto done;
9008 a->have_changes = 1;
9010 p = in_repo_path;
9011 while (p[0] == '/')
9012 p++;
9013 err = check_out_of_date(p, status, staged_status,
9014 blob_id, base_commit_idp, a->head_commit_id, a->repo,
9015 GOT_ERR_STAGE_OUT_OF_DATE);
9016 done:
9017 free(in_repo_path);
9018 return err;
9021 struct stage_path_arg {
9022 struct got_worktree *worktree;
9023 struct got_fileindex *fileindex;
9024 struct got_repository *repo;
9025 got_worktree_status_cb status_cb;
9026 void *status_arg;
9027 got_worktree_patch_cb patch_cb;
9028 void *patch_arg;
9029 int staged_something;
9030 int allow_bad_symlinks;
9033 static const struct got_error *
9034 stage_path(void *arg, unsigned char status,
9035 unsigned char staged_status, const char *relpath,
9036 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9037 struct got_object_id *commit_id, int dirfd, const char *de_name)
9039 struct stage_path_arg *a = arg;
9040 const struct got_error *err = NULL;
9041 struct got_fileindex_entry *ie;
9042 char *ondisk_path = NULL, *path_content = NULL;
9043 uint32_t stage;
9044 struct got_object_id *new_staged_blob_id = NULL;
9045 struct stat sb;
9047 if (status == GOT_STATUS_UNVERSIONED)
9048 return NULL;
9050 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9051 if (ie == NULL)
9052 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9054 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
9055 relpath)== -1)
9056 return got_error_from_errno("asprintf");
9058 switch (status) {
9059 case GOT_STATUS_ADD:
9060 case GOT_STATUS_MODIFY:
9061 /* XXX could sb.st_mode be passed in by our caller? */
9062 if (lstat(ondisk_path, &sb) == -1) {
9063 err = got_error_from_errno2("lstat", ondisk_path);
9064 break;
9066 if (a->patch_cb) {
9067 if (status == GOT_STATUS_ADD) {
9068 int choice = GOT_PATCH_CHOICE_NONE;
9069 err = (*a->patch_cb)(&choice, a->patch_arg,
9070 status, ie->path, NULL, 1, 1);
9071 if (err)
9072 break;
9073 if (choice != GOT_PATCH_CHOICE_YES)
9074 break;
9075 } else {
9076 err = create_patched_content(&path_content, 0,
9077 staged_blob_id ? staged_blob_id : blob_id,
9078 ondisk_path, dirfd, de_name, ie->path,
9079 a->repo, a->patch_cb, a->patch_arg);
9080 if (err || path_content == NULL)
9081 break;
9084 err = got_object_blob_create(&new_staged_blob_id,
9085 path_content ? path_content : ondisk_path, a->repo);
9086 if (err)
9087 break;
9088 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9089 SHA1_DIGEST_LENGTH);
9090 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
9091 stage = GOT_FILEIDX_STAGE_ADD;
9092 else
9093 stage = GOT_FILEIDX_STAGE_MODIFY;
9094 got_fileindex_entry_stage_set(ie, stage);
9095 if (S_ISLNK(sb.st_mode)) {
9096 int is_bad_symlink = 0;
9097 if (!a->allow_bad_symlinks) {
9098 char target_path[PATH_MAX];
9099 ssize_t target_len;
9100 target_len = readlink(ondisk_path, target_path,
9101 sizeof(target_path));
9102 if (target_len == -1) {
9103 err = got_error_from_errno2("readlink",
9104 ondisk_path);
9105 break;
9107 err = is_bad_symlink_target(&is_bad_symlink,
9108 target_path, target_len, ondisk_path,
9109 a->worktree->root_path,
9110 a->worktree->meta_dir);
9111 if (err)
9112 break;
9113 if (is_bad_symlink) {
9114 err = got_error_path(ondisk_path,
9115 GOT_ERR_BAD_SYMLINK);
9116 break;
9119 if (is_bad_symlink)
9120 got_fileindex_entry_staged_filetype_set(ie,
9121 GOT_FILEIDX_MODE_BAD_SYMLINK);
9122 else
9123 got_fileindex_entry_staged_filetype_set(ie,
9124 GOT_FILEIDX_MODE_SYMLINK);
9125 } else {
9126 got_fileindex_entry_staged_filetype_set(ie,
9127 GOT_FILEIDX_MODE_REGULAR_FILE);
9129 a->staged_something = 1;
9130 if (a->status_cb == NULL)
9131 break;
9132 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9133 get_staged_status(ie), relpath, blob_id,
9134 new_staged_blob_id, NULL, dirfd, de_name);
9135 if (err)
9136 break;
9138 * When staging the reverse of the staged diff,
9139 * implicitly unstage the file.
9141 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
9142 sizeof(ie->blob_sha1)) == 0) {
9143 got_fileindex_entry_stage_set(ie,
9144 GOT_FILEIDX_STAGE_NONE);
9146 break;
9147 case GOT_STATUS_DELETE:
9148 if (staged_status == GOT_STATUS_DELETE)
9149 break;
9150 if (a->patch_cb) {
9151 int choice = GOT_PATCH_CHOICE_NONE;
9152 err = (*a->patch_cb)(&choice, a->patch_arg, status,
9153 ie->path, NULL, 1, 1);
9154 if (err)
9155 break;
9156 if (choice == GOT_PATCH_CHOICE_NO)
9157 break;
9158 if (choice != GOT_PATCH_CHOICE_YES) {
9159 err = got_error(GOT_ERR_PATCH_CHOICE);
9160 break;
9163 stage = GOT_FILEIDX_STAGE_DELETE;
9164 got_fileindex_entry_stage_set(ie, stage);
9165 a->staged_something = 1;
9166 if (a->status_cb == NULL)
9167 break;
9168 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
9169 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
9170 de_name);
9171 break;
9172 case GOT_STATUS_NO_CHANGE:
9173 break;
9174 case GOT_STATUS_CONFLICT:
9175 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9176 break;
9177 case GOT_STATUS_NONEXISTENT:
9178 err = got_error_set_errno(ENOENT, relpath);
9179 break;
9180 default:
9181 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9182 break;
9185 if (path_content && unlink(path_content) == -1 && err == NULL)
9186 err = got_error_from_errno2("unlink", path_content);
9187 free(path_content);
9188 free(ondisk_path);
9189 free(new_staged_blob_id);
9190 return err;
9193 const struct got_error *
9194 got_worktree_stage(struct got_worktree *worktree,
9195 struct got_pathlist_head *paths,
9196 got_worktree_status_cb status_cb, void *status_arg,
9197 got_worktree_patch_cb patch_cb, void *patch_arg,
9198 int allow_bad_symlinks, struct got_repository *repo)
9200 const struct got_error *err = NULL, *sync_err, *unlockerr;
9201 struct got_pathlist_entry *pe;
9202 struct got_fileindex *fileindex = NULL;
9203 char *fileindex_path = NULL;
9204 struct got_reference *head_ref = NULL;
9205 struct got_object_id *head_commit_id = NULL;
9206 struct check_stage_ok_arg oka;
9207 struct stage_path_arg spa;
9209 err = lock_worktree(worktree, LOCK_EX);
9210 if (err)
9211 return err;
9213 err = got_ref_open(&head_ref, repo,
9214 got_worktree_get_head_ref_name(worktree), 0);
9215 if (err)
9216 goto done;
9217 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9218 if (err)
9219 goto done;
9220 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9221 if (err)
9222 goto done;
9224 /* Check pre-conditions before staging anything. */
9225 oka.head_commit_id = head_commit_id;
9226 oka.worktree = worktree;
9227 oka.fileindex = fileindex;
9228 oka.repo = repo;
9229 oka.have_changes = 0;
9230 TAILQ_FOREACH(pe, paths, entry) {
9231 err = worktree_status(worktree, pe->path, fileindex, repo,
9232 check_stage_ok, &oka, NULL, NULL, 1, 0);
9233 if (err)
9234 goto done;
9236 if (!oka.have_changes) {
9237 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9238 goto done;
9241 spa.worktree = worktree;
9242 spa.fileindex = fileindex;
9243 spa.repo = repo;
9244 spa.patch_cb = patch_cb;
9245 spa.patch_arg = patch_arg;
9246 spa.status_cb = status_cb;
9247 spa.status_arg = status_arg;
9248 spa.staged_something = 0;
9249 spa.allow_bad_symlinks = allow_bad_symlinks;
9250 TAILQ_FOREACH(pe, paths, entry) {
9251 err = worktree_status(worktree, pe->path, fileindex, repo,
9252 stage_path, &spa, NULL, NULL, 1, 0);
9253 if (err)
9254 goto done;
9256 if (!spa.staged_something) {
9257 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9258 goto done;
9261 sync_err = sync_fileindex(fileindex, fileindex_path);
9262 if (sync_err && err == NULL)
9263 err = sync_err;
9264 done:
9265 if (head_ref)
9266 got_ref_close(head_ref);
9267 free(head_commit_id);
9268 free(fileindex_path);
9269 if (fileindex)
9270 got_fileindex_free(fileindex);
9271 unlockerr = lock_worktree(worktree, LOCK_SH);
9272 if (unlockerr && err == NULL)
9273 err = unlockerr;
9274 return err;
9277 struct unstage_path_arg {
9278 struct got_worktree *worktree;
9279 struct got_fileindex *fileindex;
9280 struct got_repository *repo;
9281 got_worktree_checkout_cb progress_cb;
9282 void *progress_arg;
9283 got_worktree_patch_cb patch_cb;
9284 void *patch_arg;
9287 static const struct got_error *
9288 create_unstaged_content(char **path_unstaged_content,
9289 char **path_new_staged_content, struct got_object_id *blob_id,
9290 struct got_object_id *staged_blob_id, const char *relpath,
9291 struct got_repository *repo,
9292 got_worktree_patch_cb patch_cb, void *patch_arg)
9294 const struct got_error *err, *free_err;
9295 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9296 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9297 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9298 struct got_diffreg_result *diffreg_result = NULL;
9299 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9300 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9301 int fd1 = -1, fd2 = -1;
9303 *path_unstaged_content = NULL;
9304 *path_new_staged_content = NULL;
9306 err = got_object_id_str(&label1, blob_id);
9307 if (err)
9308 return err;
9310 fd1 = got_opentempfd();
9311 if (fd1 == -1) {
9312 err = got_error_from_errno("got_opentempfd");
9313 goto done;
9315 fd2 = got_opentempfd();
9316 if (fd2 == -1) {
9317 err = got_error_from_errno("got_opentempfd");
9318 goto done;
9321 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9322 if (err)
9323 goto done;
9325 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9326 if (err)
9327 goto done;
9329 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9330 if (err)
9331 goto done;
9333 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9334 fd2);
9335 if (err)
9336 goto done;
9338 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9339 if (err)
9340 goto done;
9342 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9343 if (err)
9344 goto done;
9346 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9347 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9348 if (err)
9349 goto done;
9351 err = got_opentemp_named(path_unstaged_content, &outfile,
9352 "got-unstaged-content", "");
9353 if (err)
9354 goto done;
9355 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9356 "got-new-staged-content", "");
9357 if (err)
9358 goto done;
9360 if (fseek(f1, 0L, SEEK_SET) == -1) {
9361 err = got_ferror(f1, GOT_ERR_IO);
9362 goto done;
9364 if (fseek(f2, 0L, SEEK_SET) == -1) {
9365 err = got_ferror(f2, GOT_ERR_IO);
9366 goto done;
9368 /* Count the number of actual changes in the diff result. */
9369 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9370 struct diff_chunk_context cc = {};
9371 diff_chunk_context_load_change(&cc, &nchunks_used,
9372 diffreg_result->result, n, 0);
9373 nchanges++;
9375 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9376 int choice;
9377 err = apply_or_reject_change(&choice, &nchunks_used,
9378 diffreg_result->result, n, relpath, f1, f2,
9379 &line_cur1, &line_cur2,
9380 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9381 if (err)
9382 goto done;
9383 if (choice == GOT_PATCH_CHOICE_YES)
9384 have_content = 1;
9385 else
9386 have_rejected_content = 1;
9387 if (choice == GOT_PATCH_CHOICE_QUIT)
9388 break;
9390 if (have_content || have_rejected_content)
9391 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9392 outfile, rejectfile);
9393 done:
9394 free(label1);
9395 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9396 err = got_error_from_errno("close");
9397 if (blob)
9398 got_object_blob_close(blob);
9399 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9400 err = got_error_from_errno("close");
9401 if (staged_blob)
9402 got_object_blob_close(staged_blob);
9403 free_err = got_diffreg_result_free(diffreg_result);
9404 if (free_err && err == NULL)
9405 err = free_err;
9406 if (f1 && fclose(f1) == EOF && err == NULL)
9407 err = got_error_from_errno2("fclose", path1);
9408 if (f2 && fclose(f2) == EOF && err == NULL)
9409 err = got_error_from_errno2("fclose", path2);
9410 if (outfile && fclose(outfile) == EOF && err == NULL)
9411 err = got_error_from_errno2("fclose", *path_unstaged_content);
9412 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9413 err = got_error_from_errno2("fclose", *path_new_staged_content);
9414 if (path1 && unlink(path1) == -1 && err == NULL)
9415 err = got_error_from_errno2("unlink", path1);
9416 if (path2 && unlink(path2) == -1 && err == NULL)
9417 err = got_error_from_errno2("unlink", path2);
9418 if (err || !have_content) {
9419 if (*path_unstaged_content &&
9420 unlink(*path_unstaged_content) == -1 && err == NULL)
9421 err = got_error_from_errno2("unlink",
9422 *path_unstaged_content);
9423 free(*path_unstaged_content);
9424 *path_unstaged_content = NULL;
9426 if (err || !have_content || !have_rejected_content) {
9427 if (*path_new_staged_content &&
9428 unlink(*path_new_staged_content) == -1 && err == NULL)
9429 err = got_error_from_errno2("unlink",
9430 *path_new_staged_content);
9431 free(*path_new_staged_content);
9432 *path_new_staged_content = NULL;
9434 free(path1);
9435 free(path2);
9436 return err;
9439 static const struct got_error *
9440 unstage_hunks(struct got_object_id *staged_blob_id,
9441 struct got_blob_object *blob_base,
9442 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9443 const char *ondisk_path, const char *label_orig,
9444 struct got_worktree *worktree, struct got_repository *repo,
9445 got_worktree_patch_cb patch_cb, void *patch_arg,
9446 got_worktree_checkout_cb progress_cb, void *progress_arg)
9448 const struct got_error *err = NULL;
9449 char *path_unstaged_content = NULL;
9450 char *path_new_staged_content = NULL;
9451 char *parent = NULL, *base_path = NULL;
9452 char *blob_base_path = NULL;
9453 struct got_object_id *new_staged_blob_id = NULL;
9454 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9455 struct stat sb;
9457 err = create_unstaged_content(&path_unstaged_content,
9458 &path_new_staged_content, blob_id, staged_blob_id,
9459 ie->path, repo, patch_cb, patch_arg);
9460 if (err)
9461 return err;
9463 if (path_unstaged_content == NULL)
9464 return NULL;
9466 if (path_new_staged_content) {
9467 err = got_object_blob_create(&new_staged_blob_id,
9468 path_new_staged_content, repo);
9469 if (err)
9470 goto done;
9473 f = fopen(path_unstaged_content, "re");
9474 if (f == NULL) {
9475 err = got_error_from_errno2("fopen",
9476 path_unstaged_content);
9477 goto done;
9479 if (fstat(fileno(f), &sb) == -1) {
9480 err = got_error_from_errno2("fstat", path_unstaged_content);
9481 goto done;
9483 if (got_fileindex_entry_staged_filetype_get(ie) ==
9484 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9485 char link_target[PATH_MAX];
9486 size_t r;
9487 r = fread(link_target, 1, sizeof(link_target), f);
9488 if (r == 0 && ferror(f)) {
9489 err = got_error_from_errno("fread");
9490 goto done;
9492 if (r >= sizeof(link_target)) { /* should not happen */
9493 err = got_error(GOT_ERR_NO_SPACE);
9494 goto done;
9496 link_target[r] = '\0';
9497 err = merge_symlink(worktree, blob_base,
9498 ondisk_path, ie->path, label_orig, link_target,
9499 worktree->base_commit_id, repo, progress_cb,
9500 progress_arg);
9501 } else {
9502 int local_changes_subsumed;
9504 err = got_path_dirname(&parent, ondisk_path);
9505 if (err)
9506 return err;
9508 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9509 parent) == -1) {
9510 err = got_error_from_errno("asprintf");
9511 base_path = NULL;
9512 goto done;
9515 err = got_opentemp_named(&blob_base_path, &f_base,
9516 base_path, "");
9517 if (err)
9518 goto done;
9519 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9520 blob_base);
9521 if (err)
9522 goto done;
9525 * In order the run a 3-way merge with a symlink we copy the symlink's
9526 * target path into a temporary file and use that file with diff3.
9528 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9529 err = dump_symlink_target_path_to_file(&f_deriv2,
9530 ondisk_path);
9531 if (err)
9532 goto done;
9533 } else {
9534 int fd;
9535 fd = open(ondisk_path,
9536 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9537 if (fd == -1) {
9538 err = got_error_from_errno2("open", ondisk_path);
9539 goto done;
9541 f_deriv2 = fdopen(fd, "r");
9542 if (f_deriv2 == NULL) {
9543 err = got_error_from_errno2("fdopen", ondisk_path);
9544 close(fd);
9545 goto done;
9549 err = merge_file(&local_changes_subsumed, worktree,
9550 f_base, f, f_deriv2, ondisk_path, ie->path,
9551 got_fileindex_perms_to_st(ie),
9552 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9553 repo, progress_cb, progress_arg);
9555 if (err)
9556 goto done;
9558 if (new_staged_blob_id) {
9559 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9560 SHA1_DIGEST_LENGTH);
9561 } else {
9562 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9563 got_fileindex_entry_staged_filetype_set(ie, 0);
9565 done:
9566 free(new_staged_blob_id);
9567 if (path_unstaged_content &&
9568 unlink(path_unstaged_content) == -1 && err == NULL)
9569 err = got_error_from_errno2("unlink", path_unstaged_content);
9570 if (path_new_staged_content &&
9571 unlink(path_new_staged_content) == -1 && err == NULL)
9572 err = got_error_from_errno2("unlink", path_new_staged_content);
9573 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9574 err = got_error_from_errno2("unlink", blob_base_path);
9575 if (f_base && fclose(f_base) == EOF && err == NULL)
9576 err = got_error_from_errno2("fclose", path_unstaged_content);
9577 if (f && fclose(f) == EOF && err == NULL)
9578 err = got_error_from_errno2("fclose", path_unstaged_content);
9579 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9580 err = got_error_from_errno2("fclose", ondisk_path);
9581 free(path_unstaged_content);
9582 free(path_new_staged_content);
9583 free(blob_base_path);
9584 free(parent);
9585 free(base_path);
9586 return err;
9589 static const struct got_error *
9590 unstage_path(void *arg, unsigned char status,
9591 unsigned char staged_status, const char *relpath,
9592 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9593 struct got_object_id *commit_id, int dirfd, const char *de_name)
9595 const struct got_error *err = NULL;
9596 struct unstage_path_arg *a = arg;
9597 struct got_fileindex_entry *ie;
9598 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9599 char *ondisk_path = NULL;
9600 char *id_str = NULL, *label_orig = NULL;
9601 int local_changes_subsumed;
9602 struct stat sb;
9603 int fd1 = -1, fd2 = -1;
9605 if (staged_status != GOT_STATUS_ADD &&
9606 staged_status != GOT_STATUS_MODIFY &&
9607 staged_status != GOT_STATUS_DELETE)
9608 return NULL;
9610 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9611 if (ie == NULL)
9612 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9614 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9615 == -1)
9616 return got_error_from_errno("asprintf");
9618 err = got_object_id_str(&id_str,
9619 commit_id ? commit_id : a->worktree->base_commit_id);
9620 if (err)
9621 goto done;
9622 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9623 id_str) == -1) {
9624 err = got_error_from_errno("asprintf");
9625 goto done;
9628 fd1 = got_opentempfd();
9629 if (fd1 == -1) {
9630 err = got_error_from_errno("got_opentempfd");
9631 goto done;
9633 fd2 = got_opentempfd();
9634 if (fd2 == -1) {
9635 err = got_error_from_errno("got_opentempfd");
9636 goto done;
9639 switch (staged_status) {
9640 case GOT_STATUS_MODIFY:
9641 err = got_object_open_as_blob(&blob_base, a->repo,
9642 blob_id, 8192, fd1);
9643 if (err)
9644 break;
9645 /* fall through */
9646 case GOT_STATUS_ADD:
9647 if (a->patch_cb) {
9648 if (staged_status == GOT_STATUS_ADD) {
9649 int choice = GOT_PATCH_CHOICE_NONE;
9650 err = (*a->patch_cb)(&choice, a->patch_arg,
9651 staged_status, ie->path, NULL, 1, 1);
9652 if (err)
9653 break;
9654 if (choice != GOT_PATCH_CHOICE_YES)
9655 break;
9656 } else {
9657 err = unstage_hunks(staged_blob_id,
9658 blob_base, blob_id, ie, ondisk_path,
9659 label_orig, a->worktree, a->repo,
9660 a->patch_cb, a->patch_arg,
9661 a->progress_cb, a->progress_arg);
9662 break; /* Done with this file. */
9665 err = got_object_open_as_blob(&blob_staged, a->repo,
9666 staged_blob_id, 8192, fd2);
9667 if (err)
9668 break;
9669 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9670 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9671 case GOT_FILEIDX_MODE_REGULAR_FILE:
9672 err = merge_blob(&local_changes_subsumed, a->worktree,
9673 blob_base, ondisk_path, relpath,
9674 got_fileindex_perms_to_st(ie), label_orig,
9675 blob_staged, commit_id ? commit_id :
9676 a->worktree->base_commit_id, a->repo,
9677 a->progress_cb, a->progress_arg);
9678 break;
9679 case GOT_FILEIDX_MODE_SYMLINK:
9680 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9681 char *staged_target;
9682 err = got_object_blob_read_to_str(
9683 &staged_target, blob_staged);
9684 if (err)
9685 goto done;
9686 err = merge_symlink(a->worktree, blob_base,
9687 ondisk_path, relpath, label_orig,
9688 staged_target, commit_id ? commit_id :
9689 a->worktree->base_commit_id,
9690 a->repo, a->progress_cb, a->progress_arg);
9691 free(staged_target);
9692 } else {
9693 err = merge_blob(&local_changes_subsumed,
9694 a->worktree, blob_base, ondisk_path,
9695 relpath, got_fileindex_perms_to_st(ie),
9696 label_orig, blob_staged,
9697 commit_id ? commit_id :
9698 a->worktree->base_commit_id, a->repo,
9699 a->progress_cb, a->progress_arg);
9701 break;
9702 default:
9703 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9704 break;
9706 if (err == NULL) {
9707 got_fileindex_entry_stage_set(ie,
9708 GOT_FILEIDX_STAGE_NONE);
9709 got_fileindex_entry_staged_filetype_set(ie, 0);
9711 break;
9712 case GOT_STATUS_DELETE:
9713 if (a->patch_cb) {
9714 int choice = GOT_PATCH_CHOICE_NONE;
9715 err = (*a->patch_cb)(&choice, a->patch_arg,
9716 staged_status, ie->path, NULL, 1, 1);
9717 if (err)
9718 break;
9719 if (choice == GOT_PATCH_CHOICE_NO)
9720 break;
9721 if (choice != GOT_PATCH_CHOICE_YES) {
9722 err = got_error(GOT_ERR_PATCH_CHOICE);
9723 break;
9726 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9727 got_fileindex_entry_staged_filetype_set(ie, 0);
9728 err = get_file_status(&status, &sb, ie, ondisk_path,
9729 dirfd, de_name, a->repo);
9730 if (err)
9731 break;
9732 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9733 break;
9735 done:
9736 free(ondisk_path);
9737 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9738 err = got_error_from_errno("close");
9739 if (blob_base)
9740 got_object_blob_close(blob_base);
9741 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9742 err = got_error_from_errno("close");
9743 if (blob_staged)
9744 got_object_blob_close(blob_staged);
9745 free(id_str);
9746 free(label_orig);
9747 return err;
9750 const struct got_error *
9751 got_worktree_unstage(struct got_worktree *worktree,
9752 struct got_pathlist_head *paths,
9753 got_worktree_checkout_cb progress_cb, void *progress_arg,
9754 got_worktree_patch_cb patch_cb, void *patch_arg,
9755 struct got_repository *repo)
9757 const struct got_error *err = NULL, *sync_err, *unlockerr;
9758 struct got_pathlist_entry *pe;
9759 struct got_fileindex *fileindex = NULL;
9760 char *fileindex_path = NULL;
9761 struct unstage_path_arg upa;
9763 err = lock_worktree(worktree, LOCK_EX);
9764 if (err)
9765 return err;
9767 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9768 if (err)
9769 goto done;
9771 upa.worktree = worktree;
9772 upa.fileindex = fileindex;
9773 upa.repo = repo;
9774 upa.progress_cb = progress_cb;
9775 upa.progress_arg = progress_arg;
9776 upa.patch_cb = patch_cb;
9777 upa.patch_arg = patch_arg;
9778 TAILQ_FOREACH(pe, paths, entry) {
9779 err = worktree_status(worktree, pe->path, fileindex, repo,
9780 unstage_path, &upa, NULL, NULL, 1, 0);
9781 if (err)
9782 goto done;
9785 sync_err = sync_fileindex(fileindex, fileindex_path);
9786 if (sync_err && err == NULL)
9787 err = sync_err;
9788 done:
9789 free(fileindex_path);
9790 if (fileindex)
9791 got_fileindex_free(fileindex);
9792 unlockerr = lock_worktree(worktree, LOCK_SH);
9793 if (unlockerr && err == NULL)
9794 err = unlockerr;
9795 return err;
9798 struct report_file_info_arg {
9799 struct got_worktree *worktree;
9800 got_worktree_path_info_cb info_cb;
9801 void *info_arg;
9802 struct got_pathlist_head *paths;
9803 got_cancel_cb cancel_cb;
9804 void *cancel_arg;
9807 static const struct got_error *
9808 report_file_info(void *arg, struct got_fileindex_entry *ie)
9810 const struct got_error *err;
9811 struct report_file_info_arg *a = arg;
9812 struct got_pathlist_entry *pe;
9813 struct got_object_id blob_id, staged_blob_id, commit_id;
9814 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9815 struct got_object_id *commit_idp = NULL;
9816 int stage;
9818 if (a->cancel_cb) {
9819 err = a->cancel_cb(a->cancel_arg);
9820 if (err)
9821 return err;
9824 TAILQ_FOREACH(pe, a->paths, entry) {
9825 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9826 got_path_is_child(ie->path, pe->path, pe->path_len))
9827 break;
9829 if (pe == NULL) /* not found */
9830 return NULL;
9832 if (got_fileindex_entry_has_blob(ie))
9833 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9834 stage = got_fileindex_entry_stage_get(ie);
9835 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9836 stage == GOT_FILEIDX_STAGE_ADD) {
9837 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9838 &staged_blob_id, ie);
9841 if (got_fileindex_entry_has_commit(ie))
9842 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9844 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9845 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9848 const struct got_error *
9849 got_worktree_path_info(struct got_worktree *worktree,
9850 struct got_pathlist_head *paths,
9851 got_worktree_path_info_cb info_cb, void *info_arg,
9852 got_cancel_cb cancel_cb, void *cancel_arg)
9855 const struct got_error *err = NULL, *unlockerr;
9856 struct got_fileindex *fileindex = NULL;
9857 char *fileindex_path = NULL;
9858 struct report_file_info_arg arg;
9860 err = lock_worktree(worktree, LOCK_SH);
9861 if (err)
9862 return err;
9864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9865 if (err)
9866 goto done;
9868 arg.worktree = worktree;
9869 arg.info_cb = info_cb;
9870 arg.info_arg = info_arg;
9871 arg.paths = paths;
9872 arg.cancel_cb = cancel_cb;
9873 arg.cancel_arg = cancel_arg;
9874 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9875 &arg);
9876 done:
9877 free(fileindex_path);
9878 if (fileindex)
9879 got_fileindex_free(fileindex);
9880 unlockerr = lock_worktree(worktree, LOCK_UN);
9881 if (unlockerr && err == NULL)
9882 err = unlockerr;
9883 return err;
9886 static const struct got_error *
9887 patch_check_path(const char *p, char **path, unsigned char *status,
9888 unsigned char *staged_status, struct got_fileindex *fileindex,
9889 struct got_worktree *worktree, struct got_repository *repo)
9891 const struct got_error *err;
9892 struct got_fileindex_entry *ie;
9893 struct stat sb;
9894 char *ondisk_path = NULL;
9896 err = got_worktree_resolve_path(path, worktree, p);
9897 if (err)
9898 return err;
9900 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9901 *path[0] ? "/" : "", *path) == -1)
9902 return got_error_from_errno("asprintf");
9904 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9905 if (ie) {
9906 *staged_status = get_staged_status(ie);
9907 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9908 repo);
9909 if (err)
9910 goto done;
9911 } else {
9912 *staged_status = GOT_STATUS_NO_CHANGE;
9913 *status = GOT_STATUS_UNVERSIONED;
9914 if (lstat(ondisk_path, &sb) == -1) {
9915 if (errno != ENOENT) {
9916 err = got_error_from_errno2("lstat",
9917 ondisk_path);
9918 goto done;
9920 *status = GOT_STATUS_NONEXISTENT;
9924 done:
9925 free(ondisk_path);
9926 return err;
9929 static const struct got_error *
9930 patch_can_rm(const char *path, unsigned char status,
9931 unsigned char staged_status)
9933 if (status == GOT_STATUS_NONEXISTENT)
9934 return got_error_set_errno(ENOENT, path);
9935 if (status != GOT_STATUS_NO_CHANGE &&
9936 status != GOT_STATUS_ADD &&
9937 status != GOT_STATUS_MODIFY &&
9938 status != GOT_STATUS_MODE_CHANGE)
9939 return got_error_path(path, GOT_ERR_FILE_STATUS);
9940 if (staged_status == GOT_STATUS_DELETE)
9941 return got_error_path(path, GOT_ERR_FILE_STATUS);
9942 return NULL;
9945 static const struct got_error *
9946 patch_can_add(const char *path, unsigned char status)
9948 if (status != GOT_STATUS_NONEXISTENT)
9949 return got_error_path(path, GOT_ERR_FILE_STATUS);
9950 return NULL;
9953 static const struct got_error *
9954 patch_can_edit(const char *path, unsigned char status,
9955 unsigned char staged_status)
9957 if (status == GOT_STATUS_NONEXISTENT)
9958 return got_error_set_errno(ENOENT, path);
9959 if (status != GOT_STATUS_NO_CHANGE &&
9960 status != GOT_STATUS_ADD &&
9961 status != GOT_STATUS_MODIFY)
9962 return got_error_path(path, GOT_ERR_FILE_STATUS);
9963 if (staged_status == GOT_STATUS_DELETE)
9964 return got_error_path(path, GOT_ERR_FILE_STATUS);
9965 return NULL;
9968 const struct got_error *
9969 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9970 char **fileindex_path, struct got_worktree *worktree)
9972 return open_fileindex(fileindex, fileindex_path, worktree);
9975 const struct got_error *
9976 got_worktree_patch_check_path(const char *old, const char *new,
9977 char **oldpath, char **newpath, struct got_worktree *worktree,
9978 struct got_repository *repo, struct got_fileindex *fileindex)
9980 const struct got_error *err = NULL;
9981 int file_renamed = 0;
9982 unsigned char status_old, staged_status_old;
9983 unsigned char status_new, staged_status_new;
9985 *oldpath = NULL;
9986 *newpath = NULL;
9988 err = patch_check_path(old != NULL ? old : new, oldpath,
9989 &status_old, &staged_status_old, fileindex, worktree, repo);
9990 if (err)
9991 goto done;
9993 err = patch_check_path(new != NULL ? new : old, newpath,
9994 &status_new, &staged_status_new, fileindex, worktree, repo);
9995 if (err)
9996 goto done;
9998 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9999 file_renamed = 1;
10001 if (old != NULL && new == NULL)
10002 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10003 else if (file_renamed) {
10004 err = patch_can_rm(*oldpath, status_old, staged_status_old);
10005 if (err == NULL)
10006 err = patch_can_add(*newpath, status_new);
10007 } else if (old == NULL)
10008 err = patch_can_add(*newpath, status_new);
10009 else
10010 err = patch_can_edit(*newpath, status_new, staged_status_new);
10012 done:
10013 if (err) {
10014 free(*oldpath);
10015 *oldpath = NULL;
10016 free(*newpath);
10017 *newpath = NULL;
10019 return err;
10022 const struct got_error *
10023 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
10024 struct got_worktree *worktree, struct got_fileindex *fileindex,
10025 got_worktree_checkout_cb progress_cb, void *progress_arg)
10027 struct schedule_addition_args saa;
10029 memset(&saa, 0, sizeof(saa));
10030 saa.worktree = worktree;
10031 saa.fileindex = fileindex;
10032 saa.progress_cb = progress_cb;
10033 saa.progress_arg = progress_arg;
10034 saa.repo = repo;
10036 return worktree_status(worktree, path, fileindex, repo,
10037 schedule_addition, &saa, NULL, NULL, 1, 0);
10040 const struct got_error *
10041 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
10042 struct got_worktree *worktree, struct got_fileindex *fileindex,
10043 got_worktree_delete_cb progress_cb, void *progress_arg)
10045 const struct got_error *err;
10046 struct schedule_deletion_args sda;
10047 char *ondisk_status_path;
10049 memset(&sda, 0, sizeof(sda));
10050 sda.worktree = worktree;
10051 sda.fileindex = fileindex;
10052 sda.progress_cb = progress_cb;
10053 sda.progress_arg = progress_arg;
10054 sda.repo = repo;
10055 sda.delete_local_mods = 0;
10056 sda.keep_on_disk = 0;
10057 sda.ignore_missing_paths = 0;
10058 sda.status_codes = NULL;
10059 if (asprintf(&ondisk_status_path, "%s/%s",
10060 got_worktree_get_root_path(worktree), path) == -1)
10061 return got_error_from_errno("asprintf");
10062 sda.status_path = ondisk_status_path;
10063 sda.status_path_len = strlen(ondisk_status_path);
10065 err = worktree_status(worktree, path, fileindex, repo,
10066 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
10067 free(ondisk_status_path);
10068 return err;
10071 const struct got_error *
10072 got_worktree_patch_complete(struct got_fileindex *fileindex,
10073 const char *fileindex_path)
10075 const struct got_error *err = NULL;
10077 err = sync_fileindex(fileindex, fileindex_path);
10078 got_fileindex_free(fileindex);
10080 return err;